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