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_H 17 #define RENDER_SERVICE_CLIENT_CORE_PROPERTY_RS_PROPERTIES_H 18 19 #include <optional> 20 #include <tuple> 21 #include <vector> 22 23 #include "include/effects/SkColorMatrix.h" 24 25 #include "common/rs_macros.h" 26 #include "common/rs_matrix3.h" 27 #include "animation/rs_render_particle.h" 28 #include "common/rs_vector4.h" 29 #include "modifier/rs_modifier_type.h" 30 #include "property/rs_properties_def.h" 31 #include "render/rs_border.h" 32 #include "render/rs_filter.h" 33 #include "render/rs_gradient_blur_para.h" 34 #include "render/rs_image.h" 35 #include "render/rs_mask.h" 36 #include "render/rs_path.h" 37 #include "render/rs_shader.h" 38 #include "render/rs_shadow.h" 39 40 #ifndef USE_ROSEN_DRAWING 41 #include "property/rs_filter_cache_manager.h" 42 #endif 43 44 namespace OHOS { 45 namespace Rosen { 46 class RSRenderNode; 47 class RSObjAbsGeometry; 48 class RSB_EXPORT RSProperties final { 49 public: 50 RSProperties(); 51 RSProperties(const RSProperties&) = delete; 52 RSProperties(const RSProperties&&) = delete; 53 RSProperties& operator=(const RSProperties&) = delete; 54 RSProperties& operator=(const RSProperties&&) = delete; 55 virtual ~RSProperties(); 56 57 // geometry properties 58 void SetBounds(Vector4f bounds); 59 void SetBoundsSize(Vector2f size); 60 void SetBoundsWidth(float width); 61 void SetBoundsHeight(float height); 62 void SetBoundsPosition(Vector2f position); 63 void SetBoundsPositionX(float positionX); 64 void SetBoundsPositionY(float positionY); 65 Vector4f GetBounds() const; 66 Vector2f GetBoundsSize() const; 67 float GetBoundsWidth() const; 68 float GetBoundsHeight() const; 69 Vector2f GetBoundsPosition() const; 70 float GetBoundsPositionX() const; 71 float GetBoundsPositionY() const; 72 73 void SetFrame(Vector4f frame); 74 void SetFrameSize(Vector2f size); 75 void SetFrameWidth(float width); 76 void SetFrameHeight(float height); 77 void SetFramePosition(Vector2f position); 78 void SetFramePositionX(float positionX); 79 void SetFramePositionY(float positionY); 80 Vector4f GetFrame() const; 81 Vector2f GetFrameSize() const; 82 float GetFrameWidth() const; 83 float GetFrameHeight() const; 84 Vector2f GetFramePosition() const; 85 float GetFramePositionX() const; 86 float GetFramePositionY() const; 87 float GetFrameOffsetX() const; 88 float GetFrameOffsetY() const; 89 90 void SetSandBox(const std::optional<Vector2f>& parentPosition); 91 std::optional<Vector2f> GetSandBox() const; 92 void ResetSandBox(); 93 #ifndef USE_ROSEN_DRAWING 94 void UpdateSandBoxMatrix(const std::optional<SkMatrix>& rootMatrix); 95 std::optional<SkMatrix> GetSandBoxMatrix() const; 96 #else 97 void UpdateSandBoxMatrix(const std::optional<Drawing::Matrix>& rootMatrix); 98 std::optional<Drawing::Matrix> GetSandBoxMatrix() const; 99 #endif 100 101 void SetPositionZ(float positionZ); 102 float GetPositionZ() const; 103 104 void SetPivot(Vector2f pivot); 105 void SetPivotX(float pivotX); 106 void SetPivotY(float pivotY); 107 void SetPivotZ(float pivotZ); 108 Vector2f GetPivot() const; 109 float GetPivotX() const; 110 float GetPivotY() const; 111 float GetPivotZ() const; 112 113 void SetCornerRadius(Vector4f cornerRadius); 114 Vector4f GetCornerRadius() const; 115 116 void SetQuaternion(Quaternion quaternion); 117 void SetRotation(float degree); 118 void SetRotationX(float degree); 119 void SetRotationY(float degree); 120 void SetCameraDistance(float cameraDistance); 121 Quaternion GetQuaternion() const; 122 float GetRotation() const; 123 float GetRotationX() const; 124 float GetRotationY() const; 125 float GetCameraDistance() const; 126 127 void SetTranslate(Vector2f translate); 128 void SetTranslateX(float translate); 129 void SetTranslateY(float translate); 130 void SetTranslateZ(float translate); 131 Vector2f GetTranslate() const; 132 float GetTranslateX() const; 133 float GetTranslateY() const; 134 float GetTranslateZ() const; 135 136 void SetScale(Vector2f scale); 137 void SetScaleX(float sx); 138 void SetScaleY(float sy); 139 Vector2f GetScale() const; 140 float GetScaleX() const; 141 float GetScaleY() const; 142 143 void SetAlpha(float alpha); 144 float GetAlpha() const; 145 void SetAlphaOffscreen(bool alphaOffscreen); 146 bool GetAlphaOffscreen() const; 147 148 void SetSublayerTransform(const std::optional<Matrix3f>& sublayerTransform); 149 const std::optional<Matrix3f>& GetSublayerTransform() const; 150 151 // particle properties 152 void SetParticles(const RSRenderParticleVector& particles); 153 RSRenderParticleVector GetParticles() const; 154 155 // foreground properties 156 void SetForegroundColor(Color color); 157 Color GetForegroundColor() const; 158 159 // background properties 160 void SetBackgroundColor(Color color); 161 Color GetBackgroundColor() const; 162 void SetBackgroundShader(std::shared_ptr<RSShader> shader); 163 std::shared_ptr<RSShader> GetBackgroundShader() const; 164 void SetBgImage(std::shared_ptr<RSImage> image); 165 std::shared_ptr<RSImage> GetBgImage() const; 166 void SetBgImageWidth(float width); 167 void SetBgImageHeight(float height); 168 void SetBgImagePositionX(float positionX); 169 void SetBgImagePositionY(float positionY); 170 float GetBgImageWidth() const; 171 float GetBgImageHeight() const; 172 float GetBgImagePositionX() const; 173 float GetBgImagePositionY() const; 174 175 // border properties 176 void SetBorderColor(Vector4<Color> color); 177 void SetBorderWidth(Vector4f width); 178 void SetBorderStyle(Vector4<uint32_t> style); 179 Vector4<Color> GetBorderColor() const; 180 Vector4f GetBorderWidth() const; 181 Vector4<uint32_t> GetBorderStyle() const; 182 const std::shared_ptr<RSBorder>& GetBorder() const; 183 184 // filter properties 185 void SetBackgroundFilter(std::shared_ptr<RSFilter> backgroundFilter); 186 void SetLinearGradientBlurPara(std::shared_ptr<RSLinearGradientBlurPara> para); 187 void SetDynamicLightUpRate(const std::optional<float>& rate); 188 void SetDynamicLightUpDegree(const std::optional<float>& lightUpDegree); 189 void SetFilter(std::shared_ptr<RSFilter> filter); 190 const std::shared_ptr<RSFilter>& GetBackgroundFilter() const; 191 const std::shared_ptr<RSLinearGradientBlurPara>& GetLinearGradientBlurPara() const; 192 const std::shared_ptr<RSFilter>& GetFilter() const; 193 bool NeedFilter() const; 194 195 // shadow properties 196 void SetShadowColor(Color color); 197 void SetShadowOffsetX(float offsetX); 198 void SetShadowOffsetY(float offsetY); 199 void SetShadowAlpha(float alpha); 200 void SetShadowElevation(float radius); 201 void SetShadowRadius(float radius); 202 void SetShadowPath(std::shared_ptr<RSPath> shadowPath); 203 void SetShadowMask(bool shadowMask); 204 Color GetShadowColor() const; 205 float GetShadowOffsetX() const; 206 float GetShadowOffsetY() const; 207 float GetShadowAlpha() const; 208 float GetShadowElevation() const; 209 float GetShadowRadius() const; 210 const std::optional<float>& GetDynamicLightUpRate() const; 211 const std::optional<float>& GetDynamicLightUpDegree() const; 212 std::shared_ptr<RSPath> GetShadowPath() const; 213 bool GetShadowMask() const; 214 bool IsShadowValid() const; 215 216 void SetFrameGravity(Gravity gravity); 217 Gravity GetFrameGravity() const; 218 219 void SetDrawRegion(std::shared_ptr<RectF> rect); 220 std::shared_ptr<RectF> GetDrawRegion() const; 221 222 void SetClipRRect(RRect clipRRect); 223 RRect GetClipRRect() const; 224 bool GetClipToRRect() const; 225 void SetClipBounds(std::shared_ptr<RSPath> path); 226 std::shared_ptr<RSPath> GetClipBounds() const; 227 void SetClipToBounds(bool clipToBounds); 228 bool GetClipToBounds() const; 229 void SetClipToFrame(bool clipToFrame); 230 bool GetClipToFrame() const; 231 232 void SetVisible(bool visible); 233 bool GetVisible() const; 234 std::string Dump() const; 235 236 void SetMask(std::shared_ptr<RSMask> mask); 237 std::shared_ptr<RSMask> GetMask() const; 238 239 // Pixel Stretch 240 void SetPixelStretch(const std::optional<Vector4f>& stretchSize); 241 const std::optional<Vector4f>& GetPixelStretch() const; 242 void SetPixelStretchPercent(const std::optional<Vector4f>& stretchPercent); 243 const std::optional<Vector4f>& GetPixelStretchPercent() const; 244 RectI GetPixelStretchDirtyRect() const; 245 246 const std::shared_ptr<RSObjAbsGeometry>& GetBoundsGeometry() const; 247 const std::shared_ptr<RSObjGeometry>& GetFrameGeometry() const; 248 #ifndef USE_ROSEN_DRAWING 249 bool UpdateGeometry(const RSProperties* parent, bool dirtyFlag, const std::optional<SkPoint>& offset, 250 const std::optional<SkRect>& clipRect); 251 #else 252 bool UpdateGeometry(const RSProperties* parent, bool dirtyFlag, const std::optional<Drawing::Point>& offset, 253 const std::optional<Drawing::Rect>& clipRect); 254 #endif 255 RectF GetBoundsRect() const; 256 257 bool IsGeoDirty() const; 258 bool IsContentDirty() const; 259 260 void SetSpherize(float spherizeDegree); 261 float GetSpherize() const; 262 bool IsSpherizeValid() const; 263 264 void SetLightUpEffect(float lightUpEffectDegree); 265 float GetLightUpEffect() const; 266 bool IsLightUpEffectValid() const; 267 bool IsDynamicLightUpValid() const; 268 269 // Image effect properties 270 void SetGrayScale(const std::optional<float>& grayScale); 271 const std::optional<float>& GetGrayScale() const; 272 void SetBrightness(const std::optional<float>& brightness); 273 const std::optional<float>& GetBrightness() const; 274 void SetContrast(const std::optional<float>& contrast); 275 const std::optional<float>& GetContrast() const; 276 void SetSaturate(const std::optional<float>& saturate); 277 const std::optional<float>& GetSaturate() const; 278 void SetSepia(const std::optional<float>& sepia); 279 const std::optional<float>& GetSepia() const; 280 void SetInvert(const std::optional<float>& invert); 281 const std::optional<float>& GetInvert() const; 282 void SetHueRotate(const std::optional<float>& hueRotate); 283 const std::optional<float>& GetHueRotate() const; 284 void SetColorBlend(const std::optional<Color>& colorBlend); 285 const std::optional<Color>& GetColorBlend() const; 286 287 #ifndef USE_ROSEN_DRAWING 288 const sk_sp<SkColorFilter>& GetColorFilter() const; 289 #else 290 const std::shared_ptr<Drawing::ColorFilter>& GetColorFilter() const; 291 #endif 292 293 void SetUseEffect(bool useEffect); 294 bool GetUseEffect() const; 295 296 #ifndef USE_ROSEN_DRAWING 297 const std::unique_ptr<RSFilterCacheManager>& GetFilterCacheManager(bool isForeground) const; 298 #endif 299 300 void OnApplyModifiers(); 301 302 private: 303 void ResetProperty(const std::unordered_set<RSModifierType>& dirtyTypes); 304 void SetDirty(); 305 void ResetDirty(); 306 bool IsDirty() const; 307 308 bool NeedClip() const; 309 310 RectF GetFrameRect() const; 311 RectF GetBgImageRect() const; 312 RRect GetRRect() const; 313 RRect GetInnerRRect() const; 314 RectI GetDirtyRect() const; 315 // added for update dirty region dfx 316 RectI GetDirtyRect(RectI& drawRegion) const; 317 318 bool visible_ = true; 319 bool clipToBounds_ = false; 320 bool clipToFrame_ = false; 321 bool isDirty_ = false; 322 bool geoDirty_ = false; 323 bool contentDirty_ = false; 324 bool isDrawn_ = false; 325 326 bool hasBounds_ = false; 327 bool useEffect_ = false; 328 329 Gravity frameGravity_ = Gravity::DEFAULT; 330 331 std::shared_ptr<RectF> drawRegion_ = nullptr; 332 333 float alpha_ = 1.f; 334 bool alphaOffscreen_ = false; 335 336 std::shared_ptr<RSObjAbsGeometry> boundsGeo_; 337 std::shared_ptr<RSObjGeometry> frameGeo_; 338 339 std::shared_ptr<RSFilter> backgroundFilter_ = nullptr; 340 std::shared_ptr<RSLinearGradientBlurPara> linearGradientBlurPara_ = nullptr; 341 std::shared_ptr<RSBorder> border_ = nullptr; 342 std::shared_ptr<RSPath> clipPath_ = nullptr; 343 std::optional<Vector4f> cornerRadius_; 344 std::optional<Decoration> decoration_; 345 std::shared_ptr<RSFilter> filter_ = nullptr; 346 std::shared_ptr<RSMask> mask_ = nullptr; 347 std::optional<RSShadow> shadow_; 348 std::optional<Matrix3f> sublayerTransform_; 349 float spherizeDegree_ = 0.f; 350 float lightUpEffectDegree_ = 1.0f; 351 352 std::weak_ptr<RSRenderNode> backref_; 353 354 std::optional<Vector4f> pixelStretch_; 355 std::optional<Vector4f> pixelStretchPercent_; 356 std::optional<RRect> clipRRect_; 357 358 std::optional<float> grayScale_; 359 std::optional<float> brightness_; 360 std::optional<float> contrast_; 361 std::optional<float> saturate_; 362 std::optional<float> sepia_; 363 std::optional<float> invert_; 364 std::optional<float> hueRotate_; 365 std::optional<float> dynamicLightUpRate_; 366 std::optional<float> dynamicLightUpDegree_; 367 std::optional<Color> colorBlend_; 368 std::optional<RectI> lastRect_; 369 370 // OnApplyModifiers hooks 371 void CheckEmptyBounds(); 372 void GenerateColorFilter(); 373 void CalculatePixelStretch(); 374 void CalculateFrameOffset(); 375 376 // partial update 377 bool colorFilterNeedUpdate_ = false; 378 bool pixelStretchNeedUpdate_ = false; 379 bool filterManagerNeedUpdate_ = false; 380 float frameOffsetX_ = 0.f; 381 float frameOffsetY_ = 0.f; 382 383 #ifndef USE_ROSEN_DRAWING 384 RSRenderParticleVector particles_; 385 sk_sp<SkColorFilter> colorFilter_ = nullptr; 386 #else 387 std::shared_ptr<Drawing::ColorFilter> colorFilter_ = nullptr; 388 #endif 389 390 #ifndef USE_ROSEN_DRAWING 391 void CreateFilterCacheManagerIfNeed(); 392 std::unique_ptr<RSFilterCacheManager> backgroundFilterCacheManager_; 393 std::unique_ptr<RSFilterCacheManager> foregroundFilterCacheManager_; 394 #endif 395 396 std::unique_ptr<Sandbox> sandbox_ = nullptr; 397 398 static const bool FilterCacheEnabled; 399 400 friend class RSCanvasDrawingRenderNode; 401 friend class RSCanvasRenderNode; 402 friend class RSPropertiesPainter; 403 friend class RSRenderNode; 404 }; 405 } // namespace Rosen 406 } // namespace OHOS 407 408 #endif // RENDER_SERVICE_CLIENT_CORE_PROPERTY_RS_PROPERTIES_H 409