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 SAVE_LAYER_ALPHA, // Similar to SAVE_LAYER, but when drawing back to the screen, it will be multiplied by the 115 // alpha of the screen twice. We add this type to avoid incompatibility changes. Don't use 116 // it in the future unless you have a clear understanding of what will happen! 117 MAX = SAVE_LAYER_ALPHA 118 }; 119 120 struct RSDynamicBrightnessPara { 121 Vector4f rates_ {}; 122 float saturation_ = 0.0f; 123 Vector4f posCoeff_ {}; 124 Vector4f negCoeff_ {}; 125 float fraction_ = 1.0; 126 127 RSDynamicBrightnessPara() = default; 128 RSDynamicBrightnessParaRSDynamicBrightnessPara129 RSDynamicBrightnessPara(float rate, float lightUpDegree, float cubicCoeff, float quadCoeff, 130 float saturation, std::array<float, RGB_NUM> posRGB, std::array<float, RGB_NUM> negRGB) 131 { 132 constexpr size_t INDEX_0 = 0; 133 constexpr size_t INDEX_1 = 1; 134 constexpr size_t INDEX_2 = 2; 135 rates_ = Vector4f(cubicCoeff, quadCoeff, rate, lightUpDegree); 136 saturation_ = saturation; 137 posCoeff_ = Vector4f(posRGB[INDEX_0], posRGB[INDEX_1], posRGB[INDEX_2], 0.0f); 138 negCoeff_ = Vector4f(negRGB[INDEX_0], negRGB[INDEX_1], negRGB[INDEX_2], 0.0f); 139 fraction_ = 1.0f; 140 } 141 IsValidRSDynamicBrightnessPara142 inline bool IsValid() const 143 { 144 return ROSEN_LNE(fraction_, 1.0); 145 } 146 147 bool operator==(const RSDynamicBrightnessPara& other) const 148 { 149 return (rates_ == other.rates_ && saturation_ == other.saturation_ && posCoeff_ == other.posCoeff_ && 150 negCoeff_ == other.negCoeff_ && fraction_ == other.fraction_); 151 } 152 }; 153 154 struct RSWaterRipplePara { 155 uint32_t waveCount = 0; 156 float rippleCenterX = 0.5f; 157 float rippleCenterY = 0.7f; 158 uint32_t rippleMode = 1; 159 bool operator==(const RSWaterRipplePara& other) const 160 { 161 return (waveCount == other.waveCount && ROSEN_EQ(rippleCenterX, other.rippleCenterX) && 162 ROSEN_EQ(rippleCenterY, other.rippleCenterY) && rippleMode == other.rippleMode); 163 } 164 }; 165 166 struct RSFlyOutPara { 167 uint32_t flyMode = 0; 168 bool operator==(const RSFlyOutPara& other) const 169 { 170 return (flyMode == other.flyMode); 171 } 172 }; 173 174 class Decoration final { 175 public: Decoration()176 Decoration() {} ~Decoration()177 ~Decoration() {} 178 std::shared_ptr<RSShader> bgShader_ = nullptr; 179 float bgShaderProgress_ = 0.0f; 180 std::shared_ptr<RSImage> bgImage_ = nullptr; 181 RectF bgImageRect_ = RectF(); 182 Vector4f bgImageInnerRect_ = Vector4f(); 183 Color backgroundColor_ = RgbPalette::Transparent(); 184 Color foregroundColor_ = RgbPalette::Transparent(); 185 }; 186 187 class Sandbox final { 188 public: Sandbox()189 Sandbox() {} ~Sandbox()190 ~Sandbox() {} 191 std::optional<Vector2f> position_; 192 std::optional<Drawing::Matrix> matrix_; 193 }; 194 195 enum class IlluminatedType : uint32_t { 196 INVALID = -1, 197 NONE = 0, 198 BORDER, 199 CONTENT, 200 BORDER_CONTENT, 201 BLOOM_BORDER, 202 BLOOM_BORDER_CONTENT, 203 BLEND_BORDER, 204 BLEND_CONTENT, 205 BLEND_BORDER_CONTENT, 206 }; 207 208 class RSLightSource final { 209 public: 210 RSLightSource() = default; ~RSLightSource()211 ~RSLightSource() {} SetLightPosition(const Vector4f & lightPosition)212 void SetLightPosition(const Vector4f& lightPosition) 213 { 214 lightPosition_ = lightPosition; 215 radius_ = CalculateLightRadius(lightPosition_.z_); 216 } SetLightIntensity(float lightIntensity)217 void SetLightIntensity(float lightIntensity) 218 { 219 if (!ROSEN_EQ(intensity_, INVALID_INTENSITY)) { 220 preIntensity_ = intensity_; 221 } 222 intensity_ = lightIntensity; 223 } SetLightColor(Color lightColor)224 void SetLightColor(Color lightColor) 225 { 226 lightColor_ = lightColor; 227 } SetAbsLightPosition(const Vector4f & absLightPosition)228 void SetAbsLightPosition(const Vector4f& absLightPosition) 229 { 230 absLightPosition_ = absLightPosition; 231 } 232 GetLightPosition()233 const Vector4f& GetLightPosition() const 234 { 235 return lightPosition_; 236 } GetAbsLightPosition()237 const Vector4f& GetAbsLightPosition() const 238 { 239 return absLightPosition_; 240 } GetLightIntensity()241 float GetLightIntensity() const 242 { 243 return intensity_; 244 } GetLightColor()245 Color GetLightColor() const 246 { 247 return lightColor_; 248 } 249 IsLightSourceValid()250 bool IsLightSourceValid() const 251 { 252 return !ROSEN_EQ(intensity_, 0.f); 253 } GetPreLightIntensity()254 float GetPreLightIntensity() const 255 { 256 return preIntensity_; 257 } GetLightRadius()258 float GetLightRadius() const 259 { 260 return radius_; 261 } 262 263 private: CalculateLightRadius(float lightPosZ)264 static float CalculateLightRadius(float lightPosZ) 265 { 266 float num = 1.0f / 255; 267 float count = 8; 268 float cos = std::pow(num, 1.f / count); 269 float tan = std::sqrt(static_cast<float>(1 - pow(cos, 2))) / cos; 270 return lightPosZ * tan; 271 } 272 Vector4f lightPosition_ = Vector4f(); 273 Vector4f absLightPosition_ = Vector4f(); // absolute light Position; 274 float intensity_ = 0.f; 275 float preIntensity_ = 0.f; 276 Color lightColor_ = RgbPalette::White(); 277 float radius_ = 0.f; 278 }; 279 280 class RSIlluminated final { 281 public: SetIlluminatedType(IlluminatedType & illuminatedType)282 void SetIlluminatedType(IlluminatedType& illuminatedType) 283 { 284 if (illuminatedType_ != IlluminatedType::INVALID) { 285 preIlluminatedType_ = illuminatedType_; 286 } 287 illuminatedType_ = illuminatedType; 288 } SetBloomIntensity(float bloomIntensity)289 void SetBloomIntensity(float bloomIntensity) 290 { 291 bloomIntensity_ = bloomIntensity; 292 } SetIlluminatedBorderWidth(float illuminatedBorderWidth)293 void SetIlluminatedBorderWidth(float illuminatedBorderWidth) 294 { 295 illuminatedBorderWidth_ = illuminatedBorderWidth; 296 } GetBloomIntensity()297 float GetBloomIntensity() const 298 { 299 return bloomIntensity_; 300 } GetIlluminatedType()301 const IlluminatedType& GetIlluminatedType() const 302 { 303 return illuminatedType_; 304 } GetIlluminatedBorderWidth()305 float GetIlluminatedBorderWidth() const 306 { 307 return illuminatedBorderWidth_; 308 } IsIlluminated()309 bool IsIlluminated() const 310 { 311 return !lightSourcesAndPosMap_.empty(); 312 } IsIlluminatedValid()313 bool IsIlluminatedValid() const 314 { 315 return illuminatedType_ != IlluminatedType::NONE; 316 } GetPreIlluminatedType()317 const IlluminatedType& GetPreIlluminatedType() const 318 { 319 return preIlluminatedType_; 320 } AddLightSourcesAndPos(const std::shared_ptr<RSLightSource> & lightSourcePtr,const Vector4f & lightPos)321 void AddLightSourcesAndPos(const std::shared_ptr<RSLightSource>& lightSourcePtr, const Vector4f& lightPos) 322 { 323 lightSourcesAndPosMap_.insert(std::make_pair(lightSourcePtr, lightPos)); 324 } ClearLightSourcesAndPosMap()325 void ClearLightSourcesAndPosMap() 326 { 327 lightSourcesAndPosMap_.clear(); 328 } GetLightSourcesAndPosMap()329 std::unordered_map<std::shared_ptr<RSLightSource>, Vector4f>& GetLightSourcesAndPosMap() 330 { 331 return lightSourcesAndPosMap_; 332 } 333 334 private: 335 IlluminatedType illuminatedType_ = IlluminatedType::NONE; 336 float bloomIntensity_ = 0.f; 337 float illuminatedBorderWidth_ = 0.f; 338 IlluminatedType preIlluminatedType_ = IlluminatedType::NONE; 339 // saves the mapping between the light source that illuminates the node and the position of lightSource relative to 340 // the node. 341 std::unordered_map<std::shared_ptr<RSLightSource>, Vector4f> lightSourcesAndPosMap_; 342 }; 343 344 enum class UseEffectType : int16_t { 345 EFFECT_COMPONENT = 0, 346 BEHIND_WINDOW, 347 DEFAULT = EFFECT_COMPONENT, 348 MAX = BEHIND_WINDOW 349 }; 350 } // namespace Rosen 351 } // namespace OHOS 352 353 #endif // RENDER_SERVICE_CLIENT_CORE_PROPERTY_RS_PROPERTIES_DEF_H 354