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 #ifndef USE_ROSEN_DRAWING 21 #include "include/core/SkMatrix.h" 22 #else 23 #include "utils/matrix.h" 24 #endif 25 26 #include "common/rs_color_palette.h" 27 #include "common/rs_rect.h" 28 #include "common/rs_vector4.h" 29 30 namespace OHOS { 31 namespace Rosen { 32 class RSObjGeometry; 33 class RSImage; 34 class RSShader; 35 36 constexpr float INVALID_INTENSITY = -1.f; 37 38 enum class Gravity { 39 CENTER = 0, 40 TOP, 41 BOTTOM, 42 LEFT, 43 RIGHT, 44 TOP_LEFT, 45 TOP_RIGHT, 46 BOTTOM_LEFT, 47 BOTTOM_RIGHT, 48 RESIZE, 49 RESIZE_ASPECT, 50 RESIZE_ASPECT_TOP_LEFT, 51 RESIZE_ASPECT_BOTTOM_RIGHT, 52 RESIZE_ASPECT_FILL, 53 RESIZE_ASPECT_FILL_TOP_LEFT, 54 RESIZE_ASPECT_FILL_BOTTOM_RIGHT, 55 56 DEFAULT = TOP_LEFT 57 }; 58 59 enum class ForegroundColorStrategyType { 60 INVALID = 0, 61 INVERT_BACKGROUNDCOLOR, 62 }; 63 64 enum class OutOfParentType { 65 WITHIN = 0, 66 OUTSIDE, 67 UNKNOWN 68 }; 69 70 // color blend mode, add NONE based on SkBlendMode 71 enum class RSColorBlendMode : int16_t { 72 NONE = 0, // Note: The NONE blend mode is different from SRC_OVER. When using it with 73 // RSColorBlendApplyType::SAVE_LAYER, it does not create an offscreen buffer. However, when using it 74 // with RSColorBlendApplyType::FAST, it does not modify the blend mode of subsequent content. 75 76 CLEAR, // r = 0 77 SRC, // r = s 78 DST, // r = d 79 SRC_OVER, // r = s + (1-sa)*d 80 DST_OVER, // r = d + (1-da)*s 81 SRC_IN, // r = s * da 82 DST_IN, // r = d * sa 83 SRC_OUT, // r = s * (1-da) 84 DST_OUT, // r = d * (1-sa) 85 SRC_ATOP, // r = s*da + d*(1-sa) 86 DST_ATOP, // r = d*sa + s*(1-da) 87 XOR, // r = s*(1-da) + d*(1-sa) 88 PLUS, // r = min(s + d, 1) 89 MODULATE, // r = s*d 90 SCREEN, // r = s + d - s*d 91 92 OVERLAY, // multiply or screen, depending on destination 93 DARKEN, // rc = s + d - max(s*da, d*sa), ra = SRC_OVER 94 LIGHTEN, // rc = s + d - min(s*da, d*sa), ra = SRC_OVER 95 COLOR_DODGE, // brighten destination to reflect source 96 COLOR_BURN, // darken destination to reflect source 97 HARD_LIGHT, // multiply or screen, depending on source 98 SOFT_LIGHT, // lighten or darken, depending on source 99 DIFFERENCE, // rc = s + d - 2*(min(s*da, d*sa)), ra = SRC_OVER 100 EXCLUSION, // rc = s + d - two(s*d), ra = SRC_OVER 101 MULTIPLY, // r = s*(1-da) + d*(1-sa) + s*d 102 103 HUE, // hue of source with saturation and luminosity of destination 104 SATURATION, // saturation of source with hue and luminosity of destination 105 COLOR, // hue and saturation of source with luminosity of destination 106 LUMINOSITY, // luminosity of source with hue and saturation of destination 107 108 MAX = LUMINOSITY, 109 }; 110 111 // color blend apply mode 112 enum class RSColorBlendApplyType : int16_t { 113 FAST, // Apply blending by drawing the content with the blend mode, without using an offscreen buffer 114 115 SAVE_LAYER, // Apply blending by drawing the content onto an offscreen buffer and blend it when drawing it back to 116 // the screen 117 MAX = SAVE_LAYER 118 }; 119 120 class Decoration final { 121 public: Decoration()122 Decoration() {} ~Decoration()123 ~Decoration() {} 124 std::shared_ptr<RSShader> bgShader_ = nullptr; 125 std::shared_ptr<RSImage> bgImage_ = nullptr; 126 RectF bgImageRect_ = RectF(); 127 Color backgroundColor_ = RgbPalette::Transparent(); 128 Color foregroundColor_ = RgbPalette::Transparent(); 129 }; 130 131 class Sandbox final { 132 public: Sandbox()133 Sandbox() {} ~Sandbox()134 ~Sandbox() {} 135 std::optional<Vector2f> position_; 136 #ifndef USE_ROSEN_DRAWING 137 std::optional<SkMatrix> matrix_; 138 #else 139 std::optional<Drawing::Matrix> matrix_; 140 #endif 141 }; 142 143 enum class IlluminatedType : uint32_t { 144 INVALID = -1, 145 NONE = 0, 146 BORDER, 147 CONTENT, 148 BORDER_CONTENT, 149 BLOOM_BORDER, 150 BLOOM_BORDER_CONTENT, 151 }; 152 153 class RSLightSource final { 154 public: 155 RSLightSource() = default; ~RSLightSource()156 ~RSLightSource() {} SetLightPosition(const Vector4f & lightPosition)157 void SetLightPosition(const Vector4f& lightPosition) 158 { 159 lightPosition_ = lightPosition; 160 radius_ = CalculateLightRadius(lightPosition_.z_); 161 } SetLightIntensity(float lightIntensity)162 void SetLightIntensity(float lightIntensity) 163 { 164 if (!ROSEN_EQ(intensity_, INVALID_INTENSITY)) { 165 preIntensity_ = intensity_; 166 } 167 intensity_ = lightIntensity; 168 } SetAbsLightPosition(const Vector4f & absLightPosition)169 void SetAbsLightPosition(const Vector4f& absLightPosition) 170 { 171 absLightPosition_ = absLightPosition; 172 } 173 GetLightPosition()174 const Vector4f& GetLightPosition() const 175 { 176 return lightPosition_; 177 } GetAbsLightPosition()178 const Vector4f& GetAbsLightPosition() const 179 { 180 return absLightPosition_; 181 } GetLightIntensity()182 float GetLightIntensity() const 183 { 184 return intensity_; 185 } 186 IsLightSourceValid()187 bool IsLightSourceValid() const 188 { 189 return !ROSEN_EQ(intensity_, 0.f); 190 } GetPreLightIntensity()191 float GetPreLightIntensity() const 192 { 193 return preIntensity_; 194 } GetLightRadius()195 float GetLightRadius() const 196 { 197 return radius_; 198 } 199 200 private: CalculateLightRadius(float lightPosZ)201 static float CalculateLightRadius(float lightPosZ) 202 { 203 float num = 1.0f / 255; 204 float count = 8; 205 float cos = std::pow(num, 1.f / count); 206 float tan = std::sqrt(static_cast<float>(1 - pow(cos, 2))) / cos; 207 return lightPosZ * tan; 208 } 209 Vector4f lightPosition_ = Vector4f(); 210 Vector4f absLightPosition_ = Vector4f(); // absolute light Position; 211 float intensity_ = 0.f; 212 float preIntensity_ = 0.f; 213 float radius_ = 0.f; 214 }; 215 216 class RSIlluminated final { 217 public: SetIlluminatedType(IlluminatedType & illuminatedType)218 void SetIlluminatedType(IlluminatedType& illuminatedType) 219 { 220 if (illuminatedType_ != IlluminatedType::INVALID) { 221 preIlluminatedType_ = illuminatedType_; 222 } 223 illuminatedType_ = illuminatedType; 224 } SetBloomIntensity(float bloomIntensity)225 void SetBloomIntensity(float bloomIntensity) 226 { 227 bloomIntensity_ = bloomIntensity; 228 } SetIlluminatedBorderWidth(float illuminatedBorderWidth)229 void SetIlluminatedBorderWidth(float illuminatedBorderWidth) 230 { 231 illuminatedBorderWidth_ = illuminatedBorderWidth; 232 } GetBloomIntensity()233 float GetBloomIntensity() const 234 { 235 return bloomIntensity_; 236 } GetIlluminatedType()237 const IlluminatedType& GetIlluminatedType() const 238 { 239 return illuminatedType_; 240 } GetIlluminatedBorderWidth()241 float GetIlluminatedBorderWidth() const 242 { 243 return illuminatedBorderWidth_; 244 } IsIlluminated()245 bool IsIlluminated() const 246 { 247 return !lightSources_.empty(); 248 } IsIlluminatedValid()249 bool IsIlluminatedValid() const 250 { 251 return illuminatedType_ != IlluminatedType::NONE; 252 } GetPreIlluminatedType()253 const IlluminatedType& GetPreIlluminatedType() const 254 { 255 return preIlluminatedType_; 256 } AddLightSource(const std::shared_ptr<RSLightSource> & lightSourcePtr)257 void AddLightSource(const std::shared_ptr<RSLightSource>& lightSourcePtr) 258 { 259 lightSources_.emplace(lightSourcePtr); 260 } ClearLightSource()261 void ClearLightSource() 262 { 263 lightSources_.clear(); 264 } GetLightSources()265 std::unordered_set<std::shared_ptr<RSLightSource>>& GetLightSources() 266 { 267 return lightSources_; 268 } 269 270 private: 271 IlluminatedType illuminatedType_ = IlluminatedType::NONE; 272 float bloomIntensity_ = 0.f; 273 float illuminatedBorderWidth_ = 0.f; 274 IlluminatedType preIlluminatedType_ = IlluminatedType::NONE; 275 // record the light sources that the node is illuminated by. 276 std::unordered_set<std::shared_ptr<RSLightSource>> lightSources_; 277 }; 278 } // namespace Rosen 279 } // namespace OHOS 280 281 #endif // RENDER_SERVICE_CLIENT_CORE_PROPERTY_RS_PROPERTIES_DEF_H 282