1 /*
2  * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef RENDER_SERVICE_CLIENT_CORE_RENDER_RS_MATERIAL_FILTER_H
16 #define RENDER_SERVICE_CLIENT_CORE_RENDER_RS_MATERIAL_FILTER_H
17 
18 #include <optional>
19 
20 #ifdef NEW_SKIA
21 #include "include/effects/SkRuntimeEffect.h"
22 #endif
23 
24 #include "common/rs_color.h"
25 #include "render/rs_hps_blur.h"
26 #include "render/rs_skia_filter.h"
27 #include "render/rs_kawase_blur.h"
28 
29 #include "effect/color_filter.h"
30 #include "draw/color.h"
31 #include "effect/color_matrix.h"
32 #include "effect/image_filter.h"
33 
34 namespace OHOS {
35 namespace Rosen {
36 enum MATERIAL_BLUR_STYLE : int {
37     // card blur style
38     STYLE_CARD_THIN_LIGHT  = 1,
39     STYLE_CARD_LIGHT       = 2,
40     STYLE_CARD_THICK_LIGHT = 3,
41     STYLE_CARD_THIN_DARK   = 4,
42     STYLE_CARD_DARK        = 5,
43     STYLE_CARD_THICK_DARK  = 6,
44 
45     // background blur style
46     STYLE_BACKGROUND_SMALL_LIGHT  = 101,
47     STYLE_BACKGROUND_MEDIUM_LIGHT = 102,
48     STYLE_BACKGROUND_LARGE_LIGHT  = 103,
49     STYLE_BACKGROUND_XLARGE_LIGHT = 104,
50     STYLE_BACKGROUND_SMALL_DARK   = 105,
51     STYLE_BACKGROUND_MEDIUM_DARK  = 106,
52     STYLE_BACKGROUND_LARGE_DARK   = 107,
53     STYLE_BACKGROUND_XLARGE_DARK  = 108
54 };
55 // material blur style params
56 struct MaterialParam {
57     float radius;
58     float saturation;
59     float brightness;
60     RSColor maskColor;
61 };
62 class RSB_EXPORT RSMaterialFilter : public RSDrawingFilterOriginal {
63 public:
64     RSMaterialFilter(int style, float dipScale, BLUR_COLOR_MODE mode, float ratio);
65     RSMaterialFilter(MaterialParam materialParam, BLUR_COLOR_MODE mode);
66     RSMaterialFilter(const RSMaterialFilter&) = delete;
67     RSMaterialFilter operator=(const RSMaterialFilter&) = delete;
68     ~RSMaterialFilter() override;
69     std::shared_ptr<RSFilter> TransformFilter(float fraction) const;
70     bool IsValid() const override;
71     void PreProcess(std::shared_ptr<Drawing::Image> image) override;
72     void PostProcess(Drawing::Canvas& canvas) override;
73     std::shared_ptr<RSDrawingFilterOriginal> Compose(
74         const std::shared_ptr<RSDrawingFilterOriginal>& other) const override;
75     std::string GetDescription() override;
76     std::string GetDetailedDescription() override;
77 
78     std::shared_ptr<RSFilter> Add(const std::shared_ptr<RSFilter>& rhs) override;
79     std::shared_ptr<RSFilter> Sub(const std::shared_ptr<RSFilter>& rhs) override;
80     std::shared_ptr<RSFilter> Multiply(float rhs) override;
81     std::shared_ptr<RSFilter> Negate() override;
82 
83     void DrawImageRect(Drawing::Canvas& canvas, const std::shared_ptr<Drawing::Image>& image,
84         const Drawing::Rect& src, const Drawing::Rect& dst) const override;
85     float GetRadius() const;
86     float GetSaturation() const;
87     float GetBrightness() const;
88     RSColor GetMaskColor() const;
89     BLUR_COLOR_MODE GetColorMode() const;
90     bool CanSkipFrame() const override;
91 
92     bool IsNearEqual(
93         const std::shared_ptr<RSFilter>& other, float threshold = std::numeric_limits<float>::epsilon()) const override;
94     bool IsNearZero(float threshold = std::numeric_limits<float>::epsilon()) const override;
95     void SetGreyCoef(const std::optional<Vector2f>& greyCoef) override;
96 
97     bool IsEqual(const std::shared_ptr<RSFilter>& other) const override;
98     bool IsEqualZero() const override;
99 
100 private:
101     BLUR_COLOR_MODE colorMode_;
102     float radius_ {};
103     float saturation_ = 1.f;
104     float brightness_ = 1.f;
105     RSColor maskColor_ = RSColor();
106     std::optional<Vector2f> greyCoef_;
107 
108     std::shared_ptr<Drawing::ColorFilter> GetColorFilter(float sat, float brightness);
109     std::shared_ptr<Drawing::ImageFilter> CreateMaterialStyle(MATERIAL_BLUR_STYLE style, float dipScale, float ratio);
110     std::shared_ptr<Drawing::ImageFilter> CreateMaterialFilter(float radius, float sat, float brightness);
111     static float RadiusVp2Sigma(float radiusVp, float dipScale);
112 
113     std::shared_ptr<Drawing::ColorFilter> colorFilter_;
114     friend class RSMarshallingHelper;
115 };
116 } // namespace Rosen
117 } // namespace OHOS
118 
119 #endif // RENDER_SERVICE_CLIENT_CORE_RENDER_RS_BLUR_FILTER_H
120