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 
16 #ifndef RENDER_SERVICE_CLIENT_CORE_PROPERTY_RS_PROPERTIES_DEF_H
17 #define RENDER_SERVICE_CLIENT_CORE_PROPERTY_RS_PROPERTIES_DEF_H
18 
19 #include "common/rs_common_def.h"
20 #include "utils/matrix.h"
21 
22 #include "common/rs_color_palette.h"
23 #include "common/rs_rect.h"
24 #include "common/rs_vector4.h"
25 
26 namespace OHOS {
27 namespace Rosen {
28 class RSObjGeometry;
29 class RSImage;
30 class RSShader;
31 
32 constexpr float INVALID_INTENSITY = -1.f;
33 constexpr int RGB_NUM = 3;
34 
35 enum class Gravity {
36     CENTER = 0,
37     TOP,
38     BOTTOM,
39     LEFT,
40     RIGHT,
41     TOP_LEFT,
42     TOP_RIGHT,
43     BOTTOM_LEFT,
44     BOTTOM_RIGHT,
45     RESIZE,
46     RESIZE_ASPECT,
47     RESIZE_ASPECT_TOP_LEFT,
48     RESIZE_ASPECT_BOTTOM_RIGHT,
49     RESIZE_ASPECT_FILL,
50     RESIZE_ASPECT_FILL_TOP_LEFT,
51     RESIZE_ASPECT_FILL_BOTTOM_RIGHT,
52 
53     DEFAULT = TOP_LEFT
54 };
55 
56 enum class ForegroundColorStrategyType {
57     INVALID = 0,
58     INVERT_BACKGROUNDCOLOR,
59 };
60 
61 enum class OutOfParentType {
62     WITHIN = 0,
63     OUTSIDE,
64     UNKNOWN
65 };
66 
67 // color blend mode, add NONE based on SkBlendMode
68 enum class RSColorBlendMode : int16_t {
69     NONE = 0, // Note: The NONE blend mode is different from SRC_OVER. When using it with
70               // RSColorBlendApplyType::SAVE_LAYER, it does not create an offscreen buffer. However, when using it
71               // with RSColorBlendApplyType::FAST, it does not modify the blend mode of subsequent content.
72 
73     CLEAR,    // r = 0
74     SRC,      // r = s
75     DST,      // r = d
76     SRC_OVER, // r = s + (1-sa)*d
77     DST_OVER, // r = d + (1-da)*s
78     SRC_IN,   // r = s * da
79     DST_IN,   // r = d * sa
80     SRC_OUT,  // r = s * (1-da)
81     DST_OUT,  // r = d * (1-sa)
82     SRC_ATOP, // r = s*da + d*(1-sa)
83     DST_ATOP, // r = d*sa + s*(1-da)
84     XOR,      // r = s*(1-da) + d*(1-sa)
85     PLUS,     // r = min(s + d, 1)
86     MODULATE, // r = s*d
87     SCREEN,   // r = s + d - s*d
88 
89     OVERLAY,     // multiply or screen, depending on destination
90     DARKEN,      // rc = s + d - max(s*da, d*sa), ra = SRC_OVER
91     LIGHTEN,     // rc = s + d - min(s*da, d*sa), ra = SRC_OVER
92     COLOR_DODGE, // brighten destination to reflect source
93     COLOR_BURN,  // darken destination to reflect source
94     HARD_LIGHT,  // multiply or screen, depending on source
95     SOFT_LIGHT,  // lighten or darken, depending on source
96     DIFFERENCE,  // rc = s + d - 2*(min(s*da, d*sa)), ra = SRC_OVER
97     EXCLUSION,   // rc = s + d - two(s*d), ra = SRC_OVER
98     MULTIPLY,    // r = s*(1-da) + d*(1-sa) + s*d
99 
100     HUE,        // hue of source with saturation and luminosity of destination
101     SATURATION, // saturation of source with hue and luminosity of destination
102     COLOUR,     // hue and saturation of source with luminosity of destination
103     LUMINOSITY, // luminosity of source with hue and saturation of destination
104 
105     MAX = LUMINOSITY,
106 };
107 
108 // color blend apply mode
109 enum class RSColorBlendApplyType : int16_t {
110     FAST,        // Apply blending by drawing the content with the blend mode, without using an offscreen buffer
111 
112     SAVE_LAYER,  // Apply blending by drawing the content onto an offscreen buffer and blend it when drawing it back to
113                  // the screen
114     MAX = SAVE_LAYER
115 };
116 
117 struct RSDynamicBrightnessPara {
118     Vector4f rates_ {};
119     float saturation_ = 0.0f;
120     Vector4f posCoeff_ {};
121     Vector4f negCoeff_ {};
122     float fraction_ = 1.0;
123 
124     RSDynamicBrightnessPara() = default;
125 
RSDynamicBrightnessParaRSDynamicBrightnessPara126     RSDynamicBrightnessPara(float rate, float lightUpDegree, float cubicCoeff, float quadCoeff,
127         float saturation, std::array<float, RGB_NUM> posRGB, std::array<float, RGB_NUM> negRGB)
128     {
129         constexpr size_t INDEX_0 = 0;
130         constexpr size_t INDEX_1 = 1;
131         constexpr size_t INDEX_2 = 2;
132         rates_ = Vector4f(cubicCoeff, quadCoeff, rate, lightUpDegree);
133         saturation_ = saturation;
134         posCoeff_ = Vector4f(posRGB[INDEX_0], posRGB[INDEX_1], posRGB[INDEX_2], 0.0f);
135         negCoeff_ =  Vector4f(negRGB[INDEX_0], negRGB[INDEX_1], negRGB[INDEX_2], 0.0f);
136         fraction_ = 1.0f;
137     }
138 
IsValidRSDynamicBrightnessPara139     inline bool IsValid() const
140     {
141         return ROSEN_LNE(fraction_, 1.0);
142     }
143 
144     bool operator==(const RSDynamicBrightnessPara& other) const
145     {
146         return (rates_ == other.rates_ && saturation_ == other.saturation_ && posCoeff_ == other.posCoeff_ &&
147             negCoeff_ == other.negCoeff_ && fraction_ == other.fraction_);
148     }
149 };
150 
151 struct RSWaterRipplePara {
152     uint32_t waveCount = 0;
153     float rippleCenterX = 0.5f;
154     float rippleCenterY = 0.7f;
155     uint32_t rippleMode = 1;
156     bool operator==(const RSWaterRipplePara& other) const
157     {
158         return (waveCount == other.waveCount && ROSEN_EQ(rippleCenterX, other.rippleCenterX) &&
159         ROSEN_EQ(rippleCenterY, other.rippleCenterY) && rippleMode == other.rippleMode);
160     }
161 };
162 
163 struct RSFlyOutPara {
164     uint32_t flyMode = 0;
165     bool operator==(const RSFlyOutPara& other) const
166     {
167         return (flyMode == other.flyMode);
168     }
169 };
170 
171 class Decoration final {
172 public:
Decoration()173     Decoration() {}
~Decoration()174     ~Decoration() {}
175     std::shared_ptr<RSShader> bgShader_ = nullptr;
176     std::shared_ptr<RSImage> bgImage_ = nullptr;
177     RectF bgImageRect_ = RectF();
178     Vector4f bgImageInnerRect_ = Vector4f();
179     Color backgroundColor_ = RgbPalette::Transparent();
180     Color foregroundColor_ = RgbPalette::Transparent();
181 };
182 
183 class Sandbox final {
184 public:
Sandbox()185     Sandbox() {}
~Sandbox()186     ~Sandbox() {}
187     std::optional<Vector2f> position_;
188     std::optional<Drawing::Matrix> matrix_;
189 };
190 
191 enum class IlluminatedType : uint32_t {
192     INVALID = -1,
193     NONE = 0,
194     BORDER,
195     CONTENT,
196     BORDER_CONTENT,
197     BLOOM_BORDER,
198     BLOOM_BORDER_CONTENT,
199 };
200 
201 class RSLightSource final {
202 public:
203     RSLightSource() = default;
~RSLightSource()204     ~RSLightSource() {}
SetLightPosition(const Vector4f & lightPosition)205     void SetLightPosition(const Vector4f& lightPosition)
206     {
207         lightPosition_ = lightPosition;
208         radius_ = CalculateLightRadius(lightPosition_.z_);
209     }
SetLightIntensity(float lightIntensity)210     void SetLightIntensity(float lightIntensity)
211     {
212         if (!ROSEN_EQ(intensity_, INVALID_INTENSITY)) {
213             preIntensity_ = intensity_;
214         }
215         intensity_ = lightIntensity;
216     }
SetLightColor(Color lightColor)217     void SetLightColor(Color lightColor)
218     {
219         lightColor_ = lightColor;
220     }
SetAbsLightPosition(const Vector4f & absLightPosition)221     void SetAbsLightPosition(const Vector4f& absLightPosition)
222     {
223         absLightPosition_ = absLightPosition;
224     }
225 
GetLightPosition()226     const Vector4f& GetLightPosition() const
227     {
228         return lightPosition_;
229     }
GetAbsLightPosition()230     const Vector4f& GetAbsLightPosition() const
231     {
232         return absLightPosition_;
233     }
GetLightIntensity()234     float GetLightIntensity() const
235     {
236         return intensity_;
237     }
GetLightColor()238     Color GetLightColor() const
239     {
240         return lightColor_;
241     }
242 
IsLightSourceValid()243     bool IsLightSourceValid() const
244     {
245         return !ROSEN_EQ(intensity_, 0.f);
246     }
GetPreLightIntensity()247     float GetPreLightIntensity() const
248     {
249         return preIntensity_;
250     }
GetLightRadius()251     float GetLightRadius() const
252     {
253         return radius_;
254     }
255 
256 private:
CalculateLightRadius(float lightPosZ)257     static float CalculateLightRadius(float lightPosZ)
258     {
259         float num = 1.0f / 255;
260         float count = 8;
261         float cos = std::pow(num, 1.f / count);
262         float tan = std::sqrt(static_cast<float>(1 - pow(cos, 2))) / cos;
263         return lightPosZ * tan;
264     }
265     Vector4f lightPosition_ = Vector4f();
266     Vector4f absLightPosition_ = Vector4f(); // absolute light Position;
267     float intensity_ = 0.f;
268     float preIntensity_ = 0.f;
269     Color lightColor_ = RgbPalette::White();
270     float radius_ = 0.f;
271 };
272 
273 class RSIlluminated final {
274 public:
SetIlluminatedType(IlluminatedType & illuminatedType)275     void SetIlluminatedType(IlluminatedType& illuminatedType)
276     {
277         if (illuminatedType_ != IlluminatedType::INVALID) {
278             preIlluminatedType_ = illuminatedType_;
279         }
280         illuminatedType_ = illuminatedType;
281     }
SetBloomIntensity(float bloomIntensity)282     void SetBloomIntensity(float bloomIntensity)
283     {
284         bloomIntensity_ = bloomIntensity;
285     }
SetIlluminatedBorderWidth(float illuminatedBorderWidth)286     void SetIlluminatedBorderWidth(float illuminatedBorderWidth)
287     {
288         illuminatedBorderWidth_ = illuminatedBorderWidth;
289     }
GetBloomIntensity()290     float GetBloomIntensity() const
291     {
292         return bloomIntensity_;
293     }
GetIlluminatedType()294     const IlluminatedType& GetIlluminatedType() const
295     {
296         return illuminatedType_;
297     }
GetIlluminatedBorderWidth()298     float GetIlluminatedBorderWidth() const
299     {
300         return illuminatedBorderWidth_;
301     }
IsIlluminated()302     bool IsIlluminated() const
303     {
304         return !lightSourcesAndPosMap_.empty();
305     }
IsIlluminatedValid()306     bool IsIlluminatedValid() const
307     {
308         return illuminatedType_ != IlluminatedType::NONE;
309     }
GetPreIlluminatedType()310     const IlluminatedType& GetPreIlluminatedType() const
311     {
312         return preIlluminatedType_;
313     }
AddLightSourcesAndPos(const std::shared_ptr<RSLightSource> & lightSourcePtr,const Vector4f & lightPos)314     void AddLightSourcesAndPos(const std::shared_ptr<RSLightSource>& lightSourcePtr, const Vector4f& lightPos)
315     {
316         lightSourcesAndPosMap_.insert(std::make_pair(lightSourcePtr, lightPos));
317     }
ClearLightSourcesAndPosMap()318     void ClearLightSourcesAndPosMap()
319     {
320         lightSourcesAndPosMap_.clear();
321     }
GetLightSourcesAndPosMap()322     std::unordered_map<std::shared_ptr<RSLightSource>, Vector4f>& GetLightSourcesAndPosMap()
323     {
324         return lightSourcesAndPosMap_;
325     }
326 
327 private:
328     IlluminatedType illuminatedType_ = IlluminatedType::NONE;
329     float bloomIntensity_ = 0.f;
330     float illuminatedBorderWidth_ = 0.f;
331     IlluminatedType preIlluminatedType_ = IlluminatedType::NONE;
332     // saves the mapping between the light source that illuminates the node and the position of lightSource relative to
333     // the node.
334     std::unordered_map<std::shared_ptr<RSLightSource>, Vector4f> lightSourcesAndPosMap_;
335 };
336 
337 enum class UseEffectType : int16_t {
338     EFFECT_COMPONENT = 0,
339     BEHIND_WINDOW,
340     DEFAULT = EFFECT_COMPONENT,
341     MAX = BEHIND_WINDOW
342 };
343 } // namespace Rosen
344 } // namespace OHOS
345 
346 #endif // RENDER_SERVICE_CLIENT_CORE_PROPERTY_RS_PROPERTIES_DEF_H
347