1 /* 2 * Copyright (c) 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_RENDER_ADAPTER_GRADIENT_STYLE_MODIFIER_H 16 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_RENDER_ADAPTER_GRADIENT_STYLE_MODIFIER_H 17 18 #include <cstdint> 19 #include <memory> 20 #include <optional> 21 #include <vector> 22 23 #include "common/rs_vector4.h" 24 #include "render_service_client/core/modifier_ng/custom/rs_background_style_modifier.h" 25 26 #include "base/geometry/dimension.h" 27 #include "core/components_ng/property/gradient_property.h" 28 #include "core/components_ng/render/adapter/rosen_modifier_adapter.h" 29 30 namespace OHOS::Ace::NG { 31 class RosenRenderContext; 32 class ColorAnimatableArithmetic : public Rosen::RSAnimatableArithmetic<ColorAnimatableArithmetic> { 33 public: 34 ColorAnimatableArithmetic() = default; 35 explicit ColorAnimatableArithmetic(const Gradient& gradient); 36 37 ColorAnimatableArithmetic Add(const ColorAnimatableArithmetic& value) const override; 38 ColorAnimatableArithmetic Minus(const ColorAnimatableArithmetic& value) const override; 39 ColorAnimatableArithmetic Multiply(const float scale) const override; 40 bool IsEqual(const ColorAnimatableArithmetic& value) const override; GetColors()41 const std::vector<Color>& GetColors() const 42 { 43 return colors_; 44 } 45 PaddingColors(size_t size,const Color & defaultColor)46 void PaddingColors(size_t size, const Color& defaultColor) 47 { 48 auto color = colors_.size() == 0 ? defaultColor : colors_.back(); 49 colors_.insert(colors_.end(), size, color); 50 } 51 52 private: 53 std::vector<Color> colors_; 54 }; 55 56 class ColorStopAnimatableArithmetic : public Rosen::RSAnimatableArithmetic<ColorStopAnimatableArithmetic> { 57 public: 58 ColorStopAnimatableArithmetic() = default; 59 explicit ColorStopAnimatableArithmetic(const Gradient& gradient); 60 61 ColorStopAnimatableArithmetic Add(const ColorStopAnimatableArithmetic& value) const override; 62 ColorStopAnimatableArithmetic Minus(const ColorStopAnimatableArithmetic& value) const override; 63 ColorStopAnimatableArithmetic Multiply(const float scale) const override; 64 bool IsEqual(const ColorStopAnimatableArithmetic& value) const override; GetColorStops()65 const std::vector<Dimension>& GetColorStops() const 66 { 67 return colorStops_; 68 } 69 PaddingColorStops(size_t size,const Dimension & dimension)70 void PaddingColorStops(size_t size, const Dimension& dimension) 71 { 72 colorStops_.insert(colorStops_.end(), size, dimension); 73 } 74 75 private: 76 std::vector<Dimension> colorStops_; 77 }; 78 79 using RSBackgroundStyleModifier = Rosen::ModifierNG::RSBackgroundStyleModifier; 80 81 class [[deprecated]] GradientStyleModifier : public RSBackgroundStyleModifier { 82 public: GradientStyleModifier(const WeakPtr<RosenRenderContext> & context)83 explicit GradientStyleModifier(const WeakPtr<RosenRenderContext>& context) : renderContext_(context) {} 84 ~GradientStyleModifier() override = default; 85 86 void Draw(RSDrawingContext& context) const override; 87 #ifndef USE_ROSEN_DRAWING 88 void PaintGradient(SkCanvas& canvas, const SizeF& frameSize) const; 89 #else 90 void PaintGradient(RSCanvas& canvas, const SizeF& frameSize) const; 91 #endif 92 93 Gradient GetGradient() const; 94 void SetGradient(const Gradient& gradient); 95 void PaddingColors(ColorAnimatableArithmetic& colors, bool repeat); 96 void PaddingColorStops(ColorStopAnimatableArithmetic& colorStops, bool repeat); 97 void SetSizeF(const SizeF& size); 98 99 private: 100 WeakPtr<RosenRenderContext> renderContext_; 101 // Animatable 102 std::shared_ptr<Rosen::RSAnimatableProperty<ColorAnimatableArithmetic>> colors_; 103 std::shared_ptr<Rosen::RSAnimatableProperty<ColorStopAnimatableArithmetic>> colorStops_; 104 std::shared_ptr<Rosen::RSAnimatableProperty<Rosen::Vector2f>> sizeF_; 105 // No animatable 106 std::shared_ptr<Rosen::RSProperty<Gradient>> gradient_; 107 ColorSpace colorSpace_ = ColorSpace::SRGB; 108 }; 109 } // namespace OHOS::Ace::NG 110 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_RENDER_ADAPTER_GRADIENT_STYLE_MODIFIER_H 111