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 "common/rs_rect.h"
27 #include "modifier/rs_modifier_type.h"
28 #include "modifier/rs_render_property.h"
29 
30 #include "recording/draw_cmd_list.h"
31 #include "utils/matrix.h"
32 
33 namespace OHOS {
34 namespace Rosen {
35 class RSProperties;
36 class RSPaintFilterCanvas;
37 class RSRenderNode;
38 class RSRenderParticleVector;
39 
40 class RSModifierContext {
41 public:
RSModifierContext(RSProperties & property)42     RSModifierContext(RSProperties& property) : properties_(property), canvas_(nullptr) {}
RSModifierContext(RSProperties & property,RSPaintFilterCanvas * canvas)43     RSModifierContext(RSProperties& property, RSPaintFilterCanvas* canvas) : properties_(property), canvas_(canvas) {}
44     RSProperties& properties_;
45     RSPaintFilterCanvas* canvas_;
46 };
47 
48 class RSB_EXPORT RSRenderModifier {
49 public:
50     RSRenderModifier() = default;
51     RSRenderModifier(const RSRenderModifier&) = delete;
52     RSRenderModifier(const RSRenderModifier&&) = delete;
53     RSRenderModifier& operator=(const RSRenderModifier&) = delete;
54     RSRenderModifier& operator=(const RSRenderModifier&&) = delete;
55     virtual ~RSRenderModifier() = default;
56 
57     virtual void Apply(RSModifierContext& context) const = 0;
58 
59     virtual PropertyId GetPropertyId() = 0;
60     virtual std::shared_ptr<RSRenderPropertyBase> GetProperty() const = 0;
Dump(std::string & out)61     void Dump(std::string& out) const
62     {
63         GetProperty()->Dump(out);
64     }
65 
GetType()66     virtual RSModifierType GetType()
67     {
68         return RSModifierType::INVALID;
69     }
70 
GetModifierTypeString()71     virtual std::string GetModifierTypeString()
72     {
73         auto modifierTypeString = std::make_shared<RSModifierTypeString>();
74         return modifierTypeString->GetModifierTypeString(GetType());
75     }
76 
77     virtual void Update(const std::shared_ptr<RSRenderPropertyBase>& prop, bool isDelta) = 0;
78 
79     virtual bool Marshalling(Parcel& parcel) = 0;
80     [[nodiscard]] static RSRenderModifier* Unmarshalling(Parcel& parcel);
81 
GetPropertyDrawCmdList()82     virtual std::shared_ptr<Drawing::DrawCmdList> GetPropertyDrawCmdList() const
83     {
84         return nullptr;
85     }
GetDrawCmdListId()86     virtual uint64_t GetDrawCmdListId() const
87     {
88         return 0;
89     }
SetSingleFrameModifier(bool value)90     virtual void SetSingleFrameModifier(bool value) { (void)value; }
GetSingleFrameModifier()91     virtual bool GetSingleFrameModifier() const
92     {
93         return false;
94     }
95 };
96 
97 class RSB_EXPORT RSGeometryTransRenderModifier : public RSRenderModifier {
98 public:
RSGeometryTransRenderModifier(const std::shared_ptr<RSRenderProperty<Drawing::Matrix>> & property)99     RSGeometryTransRenderModifier(const std::shared_ptr<RSRenderProperty<Drawing::Matrix>>& property)
100         : property_(property ? property : std::make_shared<RSRenderProperty<Drawing::Matrix>>()) {}
101     ~RSGeometryTransRenderModifier() override = default;
102     void Apply(RSModifierContext& context) const override;
103     void Update(const std::shared_ptr<RSRenderPropertyBase>& prop, bool isDelta) override;
104     bool Marshalling(Parcel& parcel) override;
GetPropertyId()105     PropertyId GetPropertyId() override
106     {
107         return property_->GetId();
108     }
109 
GetProperty()110     std::shared_ptr<RSRenderPropertyBase> GetProperty() const override
111     {
112         return property_;
113     }
114 
SetType(RSModifierType type)115     void SetType(RSModifierType type)
116     {
117         drawStyle_ = type;
118     }
119 
GetType()120     RSModifierType GetType() override
121     {
122         return drawStyle_;
123     }
124 
125 protected:
126     RSModifierType drawStyle_ = RSModifierType::GEOMETRYTRANS;
127     std::shared_ptr<RSRenderProperty<Drawing::Matrix>> property_;
128 };
129 
130 class RSB_EXPORT RSDrawCmdListRenderModifier : public RSRenderModifier {
131 public:
RSDrawCmdListRenderModifier(const std::shared_ptr<RSRenderProperty<Drawing::DrawCmdListPtr>> & property)132     RSDrawCmdListRenderModifier(const std::shared_ptr<RSRenderProperty<Drawing::DrawCmdListPtr>>& property)
133         : property_(property ? property : std::make_shared<RSRenderProperty<Drawing::DrawCmdListPtr>>())
134     {}
135     ~RSDrawCmdListRenderModifier() override = default;
136     void Apply(RSModifierContext& context) const override;
137     void Update(const std::shared_ptr<RSRenderPropertyBase>& prop, bool isDelta) override;
138     bool Marshalling(Parcel& parcel) override;
139 
GetPropertyId()140     PropertyId GetPropertyId() override
141     {
142         return property_->GetId();
143     }
144 
GetProperty()145     std::shared_ptr<RSRenderPropertyBase> GetProperty() const override
146     {
147         return property_;
148     }
149 
GetType()150     RSModifierType GetType() override
151     {
152         return drawStyle_;
153     }
SetType(RSModifierType type)154     void SetType(RSModifierType type)
155     {
156         drawStyle_ = type;
157         if (property_) {
158             property_->SetModifierType(type);
159         }
160     }
161 
GetPropertyDrawCmdList()162     std::shared_ptr<Drawing::DrawCmdList> GetPropertyDrawCmdList() const override
163     {
164         return property_->Get();
165     }
GetDrawCmdListId()166     uint64_t GetDrawCmdListId() const override
167     {
168         Drawing::DrawCmdListPtr drawCmd = property_->Get();
169         return reinterpret_cast<uint64_t>(drawCmd.get());
170     }
SetSingleFrameModifier(bool value)171     void SetSingleFrameModifier(bool value) override
172     {
173         isSingleFrameModifier_ = value;
174     }
GetSingleFrameModifier()175     bool GetSingleFrameModifier() const override
176     {
177         return isSingleFrameModifier_;
178     }
179 
180 protected:
181     RSModifierType drawStyle_ = RSModifierType::EXTENDED;
182     std::shared_ptr<RSRenderProperty<Drawing::DrawCmdListPtr>> property_;
183     bool isSingleFrameModifier_ = false;
184 };
185 
186 class RSAnimatableRenderModifier : public RSRenderModifier {
187 public:
RSAnimatableRenderModifier(const std::shared_ptr<RSRenderPropertyBase> & property)188     RSAnimatableRenderModifier(const std::shared_ptr<RSRenderPropertyBase>& property)
189         : property_(property ? property : std::make_shared<RSRenderPropertyBase>())
190     {}
191 
192     ~RSAnimatableRenderModifier() override = default;
193 
GetPropertyId()194     PropertyId GetPropertyId() override
195     {
196         return property_->GetId();
197     }
198 
GetProperty()199     std::shared_ptr<RSRenderPropertyBase> GetProperty() const override
200     {
201         return property_;
202     }
203 
204 protected:
205     std::shared_ptr<RSRenderPropertyBase> property_;
206 
207     friend class RSRenderPropertyAnimation;
208 };
209 
210 class RSGeometryRenderModifier : public RSAnimatableRenderModifier {
211 public:
RSGeometryRenderModifier(const std::shared_ptr<RSRenderPropertyBase> & property)212     RSGeometryRenderModifier(const std::shared_ptr<RSRenderPropertyBase>& property)
213         : RSAnimatableRenderModifier(property)
214     {}
215 
216     ~RSGeometryRenderModifier() override = default;
217 };
218 
219 class RSBackgroundRenderModifier : public RSAnimatableRenderModifier {
220 public:
RSBackgroundRenderModifier(const std::shared_ptr<RSRenderPropertyBase> & property)221     RSBackgroundRenderModifier(const std::shared_ptr<RSRenderPropertyBase>& property)
222         : RSAnimatableRenderModifier(property)
223     {}
224 
225     ~RSBackgroundRenderModifier() override = default;
226 };
227 
228 class RSContentRenderModifier : public RSAnimatableRenderModifier {
229 public:
RSContentRenderModifier(const std::shared_ptr<RSRenderPropertyBase> & property)230     RSContentRenderModifier(const std::shared_ptr<RSRenderPropertyBase>& property)
231         : RSAnimatableRenderModifier(property)
232     {}
233 
234     ~RSContentRenderModifier() override = default;
235 };
236 
237 class RSForegroundRenderModifier : public RSAnimatableRenderModifier {
238 public:
RSForegroundRenderModifier(const std::shared_ptr<RSRenderPropertyBase> & property)239     RSForegroundRenderModifier(const std::shared_ptr<RSRenderPropertyBase>& property)
240         : RSAnimatableRenderModifier(property)
241     {}
242 
243     ~RSForegroundRenderModifier() override = default;
244 };
245 
246 class RSOverlayRenderModifier : public RSAnimatableRenderModifier {
247 public:
RSOverlayRenderModifier(const std::shared_ptr<RSRenderPropertyBase> & property)248     RSOverlayRenderModifier(const std::shared_ptr<RSRenderPropertyBase>& property)
249         : RSAnimatableRenderModifier(property)
250     {}
251 
252     ~RSOverlayRenderModifier() override = default;
253 };
254 
255 class RSAppearanceRenderModifier : public RSAnimatableRenderModifier {
256 public:
RSAppearanceRenderModifier(const std::shared_ptr<RSRenderPropertyBase> & property)257     RSAppearanceRenderModifier(const std::shared_ptr<RSRenderPropertyBase>& property)
258         : RSAnimatableRenderModifier(property)
259     {}
260 
261     ~RSAppearanceRenderModifier() override = default;
262 };
263 
264 
265 class RSB_EXPORT RSEnvForegroundColorRenderModifier : public RSForegroundRenderModifier {
266 public:
RSEnvForegroundColorRenderModifier(const std::shared_ptr<RSRenderPropertyBase> & property)267     RSEnvForegroundColorRenderModifier(const std::shared_ptr<RSRenderPropertyBase>& property)
268         : RSForegroundRenderModifier(property)
269     {
270         property->SetModifierType(RSModifierType::ENV_FOREGROUND_COLOR);
271     }
272     ~RSEnvForegroundColorRenderModifier() override = default;
273     void Apply(RSModifierContext& context) const override;
274     void Update(const std::shared_ptr<RSRenderPropertyBase>& prop, bool isDelta) override;
275     bool Marshalling(Parcel& parcel) override;
GetType()276     RSModifierType GetType() override
277     {
278         return RSModifierType::ENV_FOREGROUND_COLOR;
279     }
280 };
281 
282 class RSB_EXPORT RSEnvForegroundColorStrategyRenderModifier : public RSForegroundRenderModifier {
283 public:
RSEnvForegroundColorStrategyRenderModifier(const std::shared_ptr<RSRenderPropertyBase> & property)284     RSEnvForegroundColorStrategyRenderModifier(const std::shared_ptr<RSRenderPropertyBase>& property)
285         : RSForegroundRenderModifier(property)
286     {
287         property->SetModifierType(RSModifierType::ENV_FOREGROUND_COLOR_STRATEGY);
288     }
289     ~RSEnvForegroundColorStrategyRenderModifier() override = default;
290     void Apply(RSModifierContext& context) const override;
291     void Update(const std::shared_ptr<RSRenderPropertyBase>& prop, bool isDelta) override;
292     bool Marshalling(Parcel& parcel) override;
GetType()293     RSModifierType GetType() override
294     {
295         return RSModifierType::ENV_FOREGROUND_COLOR_STRATEGY;
296     }
297     Color GetInvertBackgroundColor(RSModifierContext& context) const;
298     Color CalculateInvertColor(Color backgroundColor) const;
299 };
300 
301 class RSB_EXPORT RSCustomClipToFrameRenderModifier : public RSForegroundRenderModifier {
302 public:
RSCustomClipToFrameRenderModifier(const std::shared_ptr<RSRenderPropertyBase> & property)303     RSCustomClipToFrameRenderModifier(const std::shared_ptr<RSRenderPropertyBase>& property)
304         : RSForegroundRenderModifier(property)
305     {
306         property_->SetModifierType(RSModifierType::CUSTOM_CLIP_TO_FRAME);
307     }
308     ~RSCustomClipToFrameRenderModifier() override = default;
309     void Apply(RSModifierContext& context) const override;
310     void Update(const std::shared_ptr<RSRenderPropertyBase>& prop, bool isDelta) override;
311     bool Marshalling(Parcel& parcel) override;
GetType()312     RSModifierType GetType() override
313     {
314         return RSModifierType::CUSTOM_CLIP_TO_FRAME;
315     }
316 };
317 
318 class RSB_EXPORT RSBehindWindowFilterRadiusRenderModifier : public RSBackgroundRenderModifier {
319 public:
RSBehindWindowFilterRadiusRenderModifier(const std::shared_ptr<RSRenderPropertyBase> & property)320     RSBehindWindowFilterRadiusRenderModifier(const std::shared_ptr<RSRenderPropertyBase>& property)
321         : RSBackgroundRenderModifier(property)
322     {
323         property->SetModifierType(RSModifierType::BEHIND_WINDOW_FILTER_RADIUS);
324     }
325     ~RSBehindWindowFilterRadiusRenderModifier() override = default;
Apply(RSModifierContext & context)326     void Apply(RSModifierContext& context) const override {}
327     void Update(const std::shared_ptr<RSRenderPropertyBase>& prop, bool isDelta) override;
328     bool Marshalling(Parcel& parcel) override;
GetType()329     RSModifierType GetType() override
330     {
331         return RSModifierType::BEHIND_WINDOW_FILTER_RADIUS;
332     }
333 };
334 
335 class RSB_EXPORT RSBehindWindowFilterSaturationRenderModifier : public RSBackgroundRenderModifier {
336 public:
RSBehindWindowFilterSaturationRenderModifier(const std::shared_ptr<RSRenderPropertyBase> & property)337     RSBehindWindowFilterSaturationRenderModifier(const std::shared_ptr<RSRenderPropertyBase>& property)
338         : RSBackgroundRenderModifier(property)
339     {
340         property->SetModifierType(RSModifierType::BEHIND_WINDOW_FILTER_SATURATION);
341     }
342     ~RSBehindWindowFilterSaturationRenderModifier() override = default;
Apply(RSModifierContext & context)343     void Apply(RSModifierContext& context) const override {}
344     void Update(const std::shared_ptr<RSRenderPropertyBase>& prop, bool isDelta) override;
345     bool Marshalling(Parcel& parcel) override;
GetType()346     RSModifierType GetType() override
347     {
348         return RSModifierType::BEHIND_WINDOW_FILTER_SATURATION;
349     }
350 };
351 
352 class RSB_EXPORT RSBehindWindowFilterBrightnessRenderModifier : public RSBackgroundRenderModifier {
353 public:
RSBehindWindowFilterBrightnessRenderModifier(const std::shared_ptr<RSRenderPropertyBase> & property)354     RSBehindWindowFilterBrightnessRenderModifier(const std::shared_ptr<RSRenderPropertyBase>& property)
355         : RSBackgroundRenderModifier(property)
356     {
357         property->SetModifierType(RSModifierType::BEHIND_WINDOW_FILTER_BRIGHTNESS);
358     }
359     ~RSBehindWindowFilterBrightnessRenderModifier() override = default;
Apply(RSModifierContext & context)360     void Apply(RSModifierContext& context) const override {}
361     void Update(const std::shared_ptr<RSRenderPropertyBase>& prop, bool isDelta) override;
362     bool Marshalling(Parcel& parcel) override;
GetType()363     RSModifierType GetType() override
364     {
365         return RSModifierType::BEHIND_WINDOW_FILTER_BRIGHTNESS;
366     }
367 };
368 
369 class RSB_EXPORT RSBehindWindowFilterMaskColorRenderModifier : public RSBackgroundRenderModifier {
370 public:
RSBehindWindowFilterMaskColorRenderModifier(const std::shared_ptr<RSRenderPropertyBase> & property)371     RSBehindWindowFilterMaskColorRenderModifier(const std::shared_ptr<RSRenderPropertyBase>& property)
372         : RSBackgroundRenderModifier(property)
373     {
374         property->SetModifierType(RSModifierType::BEHIND_WINDOW_FILTER_MASK_COLOR);
375     }
376     ~RSBehindWindowFilterMaskColorRenderModifier() override = default;
Apply(RSModifierContext & context)377     void Apply(RSModifierContext& context) const override {}
378     void Update(const std::shared_ptr<RSRenderPropertyBase>& prop, bool isDelta) override;
379     bool Marshalling(Parcel& parcel) override;
GetType()380     RSModifierType GetType() override
381     {
382         return RSModifierType::BEHIND_WINDOW_FILTER_MASK_COLOR;
383     }
384 };
385 
386 // declare RenderModifiers like RSBoundsRenderModifier
387 #define DECLARE_ANIMATABLE_MODIFIER(MODIFIER_NAME, TYPE, MODIFIER_TYPE, DELTA_OP, MODIFIER_TIER, THRESHOLD_TYPE) \
388     class RSB_EXPORT RS##MODIFIER_NAME##RenderModifier : public RS##MODIFIER_TIER##RenderModifier {              \
389     public:                                                                                                      \
390         RS##MODIFIER_NAME##RenderModifier(const std::shared_ptr<RSRenderPropertyBase>& property)                 \
391             : RS##MODIFIER_TIER##RenderModifier(property)                                                        \
392         {                                                                                                        \
393             property->SetModifierType(RSModifierType::MODIFIER_TYPE);                                            \
394         }                                                                                                        \
395         virtual ~RS##MODIFIER_NAME##RenderModifier() = default;                                                  \
396         void Apply(RSModifierContext& context) const override;                                                   \
397         void Update(const std::shared_ptr<RSRenderPropertyBase>& prop, bool isDelta) override;                   \
398         bool Marshalling(Parcel& parcel) override;                                                               \
399         RSModifierType GetType() override { return (RSModifierType::MODIFIER_TYPE); }                            \
400         virtual std::string GetModifierTypeString() override { return #MODIFIER_NAME; }                          \
401     };
402 
403 #define DECLARE_NOANIMATABLE_MODIFIER(MODIFIER_NAME, TYPE, MODIFIER_TYPE, MODIFIER_TIER) \
404     DECLARE_ANIMATABLE_MODIFIER(MODIFIER_NAME, TYPE, MODIFIER_TYPE, Add, MODIFIER_TIER, ZERO)
405 
406 DECLARE_NOANIMATABLE_MODIFIER(Particles, RSRenderParticleVector, PARTICLE, Foreground)
407 
408 #include "modifier/rs_modifiers_def.in"
409 
410 #undef DECLARE_ANIMATABLE_MODIFIER
411 #undef DECLARE_NOANIMATABLE_MODIFIER
412 } // namespace Rosen
413 } // namespace OHOS
414 
415 #endif // RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_RENDER_MODIFIER_H
416