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