• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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_ANIMATION_RS_RENDER_MODIFIER_H
17 #define RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_RENDER_MODIFIER_H
18 
19 #include <memory>
20 
21 #include "parcel.h"
22 #include "rs_modifier_type.h"
23 
24 #include "common/rs_color.h"
25 #include "common/rs_macros.h"
26 #include "memory/rs_dfx_string.h"
27 #include "modifier/rs_modifier_type.h"
28 #include "modifier/rs_render_property.h"
29 
30 #ifndef USE_ROSEN_DRAWING
31 #include "include/core/SkMatrix.h"
32 #include "pipeline/rs_draw_cmd_list.h"
33 #else
34 #include "recording/draw_cmd_list.h"
35 #include "utils/matrix.h"
36 #endif
37 
38 namespace OHOS {
39 namespace Rosen {
40 class RSProperties;
41 class RSPaintFilterCanvas;
42 class RSRenderNode;
43 class RSRenderParticleVector;
44 
45 class RSModifierContext {
46 public:
RSModifierContext(RSProperties & property)47     RSModifierContext(RSProperties& property) : properties_(property), canvas_(nullptr) {}
RSModifierContext(RSProperties & property,RSPaintFilterCanvas * canvas)48     RSModifierContext(RSProperties& property, RSPaintFilterCanvas* canvas) : properties_(property), canvas_(canvas) {}
49     RSProperties& properties_;
50     RSPaintFilterCanvas* canvas_;
51 };
52 
53 class RSB_EXPORT RSRenderModifier {
54 public:
55     RSRenderModifier() = default;
56     RSRenderModifier(const RSRenderModifier&) = delete;
57     RSRenderModifier(const RSRenderModifier&&) = delete;
58     RSRenderModifier& operator=(const RSRenderModifier&) = delete;
59     RSRenderModifier& operator=(const RSRenderModifier&&) = delete;
60     virtual ~RSRenderModifier() = default;
61 
62     virtual void Apply(RSModifierContext& context) const = 0;
63 
64 #ifndef USE_ROSEN_DRAWING
DumpPicture(DfxString & info)65     virtual void DumpPicture(DfxString& info) const
66     {
67         return;
68     }
69 #endif
70     virtual PropertyId GetPropertyId() = 0;
71     virtual std::shared_ptr<RSRenderPropertyBase> GetProperty() = 0;
72     virtual RSModifierType GetType() = 0;
73     virtual void Update(const std::shared_ptr<RSRenderPropertyBase>& prop, bool isDelta) = 0;
74 
75     virtual bool Marshalling(Parcel& parcel) = 0;
76     [[nodiscard]] static RSRenderModifier* Unmarshalling(Parcel& parcel);
77 
GetDrawCmdListId()78     virtual uint64_t GetDrawCmdListId() const
79     {
80         return 0;
81     }
SetSingleFrameModifier(bool value)82     virtual void SetSingleFrameModifier(bool value) { (void)value; }
GetSingleFrameModifier()83     virtual bool GetSingleFrameModifier() const
84     {
85         return false;
86     }
87 };
88 
89 class RSB_EXPORT RSGeometryTransRenderModifier : public RSRenderModifier {
90 public:
91 #ifndef USE_ROSEN_DRAWING
RSGeometryTransRenderModifier(const std::shared_ptr<RSRenderProperty<SkMatrix>> & property)92     RSGeometryTransRenderModifier(const std::shared_ptr<RSRenderProperty<SkMatrix>>& property)
93         : property_(property ? property : std::make_shared<RSRenderProperty<SkMatrix>>()) {}
94 #else
95     RSGeometryTransRenderModifier(const std::shared_ptr<RSRenderProperty<Drawing::Matrix>>& property)
96         : property_(property ? property : std::make_shared<RSRenderProperty<Drawing::Matrix>>()) {}
97 #endif
98     ~RSGeometryTransRenderModifier() override = default;
99     void Apply(RSModifierContext& context) const override;
100     void Update(const std::shared_ptr<RSRenderPropertyBase>& prop, bool isDelta) override;
101     bool Marshalling(Parcel& parcel) override;
GetPropertyId()102     PropertyId GetPropertyId() override
103     {
104         return property_->GetId();
105     }
106 
GetProperty()107     std::shared_ptr<RSRenderPropertyBase> GetProperty() override
108     {
109         return property_;
110     }
111 
SetType(RSModifierType type)112     void SetType(RSModifierType type)
113     {
114         drawStyle_ = type;
115     }
116 
GetType()117     RSModifierType GetType() override
118     {
119         return drawStyle_;
120     }
121 
122 protected:
123     RSModifierType drawStyle_ = RSModifierType::GEOMETRYTRANS;
124 #ifndef USE_ROSEN_DRAWING
125     std::shared_ptr<RSRenderProperty<SkMatrix>> property_;
126 #else
127     std::shared_ptr<RSRenderProperty<Drawing::Matrix>> property_;
128 #endif
129 };
130 
131 class RSB_EXPORT RSDrawCmdListRenderModifier : public RSRenderModifier {
132 public:
133 #ifndef USE_ROSEN_DRAWING
RSDrawCmdListRenderModifier(const std::shared_ptr<RSRenderProperty<DrawCmdListPtr>> & property)134     RSDrawCmdListRenderModifier(const std::shared_ptr<RSRenderProperty<DrawCmdListPtr>>& property)
135         : property_(property ? property : std::make_shared<RSRenderProperty<DrawCmdListPtr>>())
136     {}
137 #else
138     RSDrawCmdListRenderModifier(const std::shared_ptr<RSRenderProperty<Drawing::DrawCmdListPtr>>& property)
139         : property_(property ? property : std::make_shared<RSRenderProperty<Drawing::DrawCmdListPtr>>())
140     {}
141 #endif
142     ~RSDrawCmdListRenderModifier() override = default;
143     void Apply(RSModifierContext& context) const override;
144     void Update(const std::shared_ptr<RSRenderPropertyBase>& prop, bool isDelta) override;
145     bool Marshalling(Parcel& parcel) override;
146 
GetPropertyId()147     PropertyId GetPropertyId() override
148     {
149         return property_->GetId();
150     }
151 
152 #ifndef USE_ROSEN_DRAWING
DumpPicture(DfxString & info)153     void DumpPicture(DfxString& info) const override
154     {
155         if (!property_) {
156             return;
157         }
158         if (!(property_->Get())) {
159             return;
160         }
161         property_->Get()->DumpPicture(info);
162     }
163 #endif
164 
GetProperty()165     std::shared_ptr<RSRenderPropertyBase> GetProperty() override
166     {
167         return property_;
168     }
169 
GetType()170     RSModifierType GetType() override
171     {
172         return drawStyle_;
173     }
SetType(RSModifierType type)174     void SetType(RSModifierType type)
175     {
176         drawStyle_ = type;
177         if (property_) {
178             property_->SetModifierType(type);
179         }
180     }
181 
GetDrawCmdListId()182     uint64_t GetDrawCmdListId() const override
183     {
184 #ifndef USE_ROSEN_DRAWING
185         DrawCmdListPtr drawCmd = property_->Get();
186 #else
187         Drawing::DrawCmdListPtr drawCmd = property_->Get();
188 #endif
189         return reinterpret_cast<uint64_t>(drawCmd.get());
190     }
SetSingleFrameModifier(bool value)191     void SetSingleFrameModifier(bool value) override
192     {
193         isSingleFrameModifier_ = value;
194     }
GetSingleFrameModifier()195     bool GetSingleFrameModifier() const override
196     {
197         return isSingleFrameModifier_;
198     }
199 
200     // functions that are dedicated to driven render [start]
201     RectF GetCmdsClipRect() const;
202     void ApplyForDrivenContent(RSModifierContext& context) const;
203     // functions that are dedicated to driven render [end]
204 protected:
205     RSModifierType drawStyle_ = RSModifierType::EXTENDED;
206 #ifndef USE_ROSEN_DRAWING
207     std::shared_ptr<RSRenderProperty<DrawCmdListPtr>> property_;
208 #else
209     std::shared_ptr<RSRenderProperty<Drawing::DrawCmdListPtr>> property_;
210 #endif
211     bool isSingleFrameModifier_ = false;
212 };
213 
214 class RSAnimatableRenderModifier : public RSRenderModifier {
215 public:
RSAnimatableRenderModifier(const std::shared_ptr<RSRenderPropertyBase> & property)216     RSAnimatableRenderModifier(const std::shared_ptr<RSRenderPropertyBase>& property)
217         : property_(property ? property : std::make_shared<RSRenderPropertyBase>())
218     {}
219 
220     ~RSAnimatableRenderModifier() override = default;
221 
GetPropertyId()222     PropertyId GetPropertyId() override
223     {
224         return property_->GetId();
225     }
226 
GetProperty()227     std::shared_ptr<RSRenderPropertyBase> GetProperty() override
228     {
229         return property_;
230     }
231 
232 protected:
233     std::shared_ptr<RSRenderPropertyBase> property_;
234 
235     friend class RSRenderPropertyAnimation;
236 };
237 
238 class RSGeometryRenderModifier : public RSAnimatableRenderModifier {
239 public:
RSGeometryRenderModifier(const std::shared_ptr<RSRenderPropertyBase> & property)240     RSGeometryRenderModifier(const std::shared_ptr<RSRenderPropertyBase>& property)
241         : RSAnimatableRenderModifier(property)
242     {}
243 
244     ~RSGeometryRenderModifier() override = default;
245 };
246 
247 class RSBackgroundRenderModifier : public RSAnimatableRenderModifier {
248 public:
RSBackgroundRenderModifier(const std::shared_ptr<RSRenderPropertyBase> & property)249     RSBackgroundRenderModifier(const std::shared_ptr<RSRenderPropertyBase>& property)
250         : RSAnimatableRenderModifier(property)
251     {}
252 
253     ~RSBackgroundRenderModifier() override = default;
254 };
255 
256 class RSContentRenderModifier : public RSAnimatableRenderModifier {
257 public:
RSContentRenderModifier(const std::shared_ptr<RSRenderPropertyBase> & property)258     RSContentRenderModifier(const std::shared_ptr<RSRenderPropertyBase>& property)
259         : RSAnimatableRenderModifier(property)
260     {}
261 
262     ~RSContentRenderModifier() override = default;
263 };
264 
265 class RSForegroundRenderModifier : public RSAnimatableRenderModifier {
266 public:
RSForegroundRenderModifier(const std::shared_ptr<RSRenderPropertyBase> & property)267     RSForegroundRenderModifier(const std::shared_ptr<RSRenderPropertyBase>& property)
268         : RSAnimatableRenderModifier(property)
269     {}
270 
271     ~RSForegroundRenderModifier() override = default;
272 };
273 
274 class RSOverlayRenderModifier : public RSAnimatableRenderModifier {
275 public:
RSOverlayRenderModifier(const std::shared_ptr<RSRenderPropertyBase> & property)276     RSOverlayRenderModifier(const std::shared_ptr<RSRenderPropertyBase>& property)
277         : RSAnimatableRenderModifier(property)
278     {}
279 
280     ~RSOverlayRenderModifier() override = default;
281 };
282 
283 class RSAppearanceRenderModifier : public RSAnimatableRenderModifier {
284 public:
RSAppearanceRenderModifier(const std::shared_ptr<RSRenderPropertyBase> & property)285     RSAppearanceRenderModifier(const std::shared_ptr<RSRenderPropertyBase>& property)
286         : RSAnimatableRenderModifier(property)
287     {}
288 
289     ~RSAppearanceRenderModifier() override = default;
290 };
291 
292 
293 class RSB_EXPORT RSEnvForegroundColorRenderModifier : public RSForegroundRenderModifier {
294 public:
RSEnvForegroundColorRenderModifier(const std::shared_ptr<RSRenderPropertyBase> & property)295     RSEnvForegroundColorRenderModifier(const std::shared_ptr<RSRenderPropertyBase>& property)
296         : RSForegroundRenderModifier(property)
297     {
298         property->SetModifierType(RSModifierType::ENV_FOREGROUND_COLOR);
299     }
300     ~RSEnvForegroundColorRenderModifier() override = default;
301     void Apply(RSModifierContext& context) const override;
302     void Update(const std::shared_ptr<RSRenderPropertyBase>& prop, bool isDelta) override;
303     bool Marshalling(Parcel& parcel) override;
GetType()304     RSModifierType GetType() override
305     {
306         return RSModifierType::ENV_FOREGROUND_COLOR;
307     }
308 };
309 
310 class RSB_EXPORT RSEnvForegroundColorStrategyRenderModifier : public RSForegroundRenderModifier {
311 public:
RSEnvForegroundColorStrategyRenderModifier(const std::shared_ptr<RSRenderPropertyBase> & property)312     RSEnvForegroundColorStrategyRenderModifier(const std::shared_ptr<RSRenderPropertyBase>& property)
313         : RSForegroundRenderModifier(property)
314     {
315         property->SetModifierType(RSModifierType::ENV_FOREGROUND_COLOR_STRATEGY);
316     }
317     ~RSEnvForegroundColorStrategyRenderModifier() override = default;
318     void Apply(RSModifierContext& context) const override;
319     void Update(const std::shared_ptr<RSRenderPropertyBase>& prop, bool isDelta) override;
320     bool Marshalling(Parcel& parcel) override;
GetType()321     RSModifierType GetType() override
322     {
323         return RSModifierType::ENV_FOREGROUND_COLOR_STRATEGY;
324     }
325     Color GetInvertBackgroundColor(RSModifierContext& context) const;
326     Color CalculateInvertColor(Color backgroundColor) const;
327 };
328 
329 // declare RenderModifiers like RSBoundsRenderModifier
330 #define DECLARE_ANIMATABLE_MODIFIER(MODIFIER_NAME, TYPE, MODIFIER_TYPE, DELTA_OP, MODIFIER_TIER, THRESHOLD_TYPE) \
331     class RSB_EXPORT RS##MODIFIER_NAME##RenderModifier : public RS##MODIFIER_TIER##RenderModifier {              \
332     public:                                                                                                      \
333         RS##MODIFIER_NAME##RenderModifier(const std::shared_ptr<RSRenderPropertyBase>& property)                 \
334             : RS##MODIFIER_TIER##RenderModifier(property)                                                        \
335         {                                                                                                        \
336             property->SetModifierType(RSModifierType::MODIFIER_TYPE);                                            \
337         }                                                                                                        \
338         virtual ~RS##MODIFIER_NAME##RenderModifier() = default;                                                  \
339         void Apply(RSModifierContext& context) const override;                                                   \
340         void Update(const std::shared_ptr<RSRenderPropertyBase>& prop, bool isDelta) override;                   \
341         bool Marshalling(Parcel& parcel) override;                                                               \
342         RSModifierType GetType() override { return (RSModifierType::MODIFIER_TYPE); }                            \
343     };
344 
345 #define DECLARE_NOANIMATABLE_MODIFIER(MODIFIER_NAME, TYPE, MODIFIER_TYPE, MODIFIER_TIER) \
346     DECLARE_ANIMATABLE_MODIFIER(MODIFIER_NAME, TYPE, MODIFIER_TYPE, Add, MODIFIER_TIER, ZERO)
347 
348 DECLARE_NOANIMATABLE_MODIFIER(Particles, RSRenderParticleVector, PARTICLE, Foreground)
349 
350 #include "modifier/rs_modifiers_def.in"
351 
352 #undef DECLARE_ANIMATABLE_MODIFIER
353 #undef DECLARE_NOANIMATABLE_MODIFIER
354 } // namespace Rosen
355 } // namespace OHOS
356 
357 #endif // RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_RENDER_MODIFIER_H
358