• 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 "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         auto property = GetProperty();
64         if (property != nullptr) {
65             property->Dump(out);
66         }
67     }
68 
GetSize()69     size_t GetSize() {
70         auto property = GetProperty();
71         if (property != nullptr) {
72             return property->GetSize();
73         }
74         return 0;
75     }
76 
GetType()77     virtual RSModifierType GetType()
78     {
79         return RSModifierType::INVALID;
80     }
81 
GetModifierTypeString()82     virtual std::string GetModifierTypeString()
83     {
84         auto modifierTypeString = std::make_shared<RSModifierTypeString>();
85         return modifierTypeString->GetModifierTypeString(GetType());
86     }
87 
88     virtual void Update(const std::shared_ptr<RSRenderPropertyBase>& prop, bool isDelta) = 0;
89 
90     virtual bool Marshalling(Parcel& parcel) = 0;
91     [[nodiscard]] static RSRenderModifier* Unmarshalling(Parcel& parcel);
92 
GetPropertyDrawCmdList()93     virtual std::shared_ptr<Drawing::DrawCmdList> GetPropertyDrawCmdList() const
94     {
95         return nullptr;
96     }
GetDrawCmdListId()97     virtual uint64_t GetDrawCmdListId() const
98     {
99         return 0;
100     }
SetSingleFrameModifier(bool value)101     virtual void SetSingleFrameModifier(bool value) { (void)value; }
GetSingleFrameModifier()102     virtual bool GetSingleFrameModifier() const
103     {
104         return false;
105     }
106 };
107 
108 class RSB_EXPORT RSGeometryTransRenderModifier : public RSRenderModifier {
109 public:
RSGeometryTransRenderModifier(const std::shared_ptr<RSRenderProperty<Drawing::Matrix>> & property)110     RSGeometryTransRenderModifier(const std::shared_ptr<RSRenderProperty<Drawing::Matrix>>& property)
111         : property_(property ? property : std::make_shared<RSRenderProperty<Drawing::Matrix>>()) {}
112     ~RSGeometryTransRenderModifier() override = default;
113     void Apply(RSModifierContext& context) const override;
114     void Update(const std::shared_ptr<RSRenderPropertyBase>& prop, bool isDelta) override;
115     bool Marshalling(Parcel& parcel) override;
GetPropertyId()116     PropertyId GetPropertyId() override
117     {
118         return property_->GetId();
119     }
120 
GetProperty()121     std::shared_ptr<RSRenderPropertyBase> GetProperty() const override
122     {
123         return property_;
124     }
125 
SetType(RSModifierType type)126     void SetType(RSModifierType type)
127     {
128         drawStyle_ = type;
129     }
130 
GetType()131     RSModifierType GetType() override
132     {
133         return drawStyle_;
134     }
135 
136 protected:
137     RSModifierType drawStyle_ = RSModifierType::GEOMETRYTRANS;
138     std::shared_ptr<RSRenderProperty<Drawing::Matrix>> property_;
139 };
140 
141 class RSB_EXPORT RSDrawCmdListRenderModifier : public RSRenderModifier {
142 public:
RSDrawCmdListRenderModifier(const std::shared_ptr<RSRenderProperty<Drawing::DrawCmdListPtr>> & property)143     RSDrawCmdListRenderModifier(const std::shared_ptr<RSRenderProperty<Drawing::DrawCmdListPtr>>& property)
144         : property_(property ? property : std::make_shared<RSRenderProperty<Drawing::DrawCmdListPtr>>())
145     {}
146     ~RSDrawCmdListRenderModifier() override = default;
147     void Apply(RSModifierContext& context) const override;
148     void Update(const std::shared_ptr<RSRenderPropertyBase>& prop, bool isDelta) override;
149     bool Marshalling(Parcel& parcel) override;
150 
GetPropertyId()151     PropertyId GetPropertyId() override
152     {
153         return property_->GetId();
154     }
155 
156 
GetProperty()157     std::shared_ptr<RSRenderPropertyBase> GetProperty() const override
158     {
159         return property_;
160     }
161 
GetType()162     RSModifierType GetType() override
163     {
164         return drawStyle_;
165     }
SetType(RSModifierType type)166     void SetType(RSModifierType type)
167     {
168         drawStyle_ = type;
169         if (property_) {
170             property_->SetModifierType(type);
171         }
172     }
173 
GetPropertyDrawCmdList()174     std::shared_ptr<Drawing::DrawCmdList> GetPropertyDrawCmdList() const override
175     {
176         return property_->Get();
177     }
GetDrawCmdListId()178     uint64_t GetDrawCmdListId() const override
179     {
180         Drawing::DrawCmdListPtr drawCmd = property_->Get();
181         return reinterpret_cast<uint64_t>(drawCmd.get());
182     }
SetSingleFrameModifier(bool value)183     void SetSingleFrameModifier(bool value) override
184     {
185         isSingleFrameModifier_ = value;
186     }
GetSingleFrameModifier()187     bool GetSingleFrameModifier() const override
188     {
189         return isSingleFrameModifier_;
190     }
191 
192 protected:
193     RSModifierType drawStyle_ = RSModifierType::EXTENDED;
194     std::shared_ptr<RSRenderProperty<Drawing::DrawCmdListPtr>> property_;
195     bool isSingleFrameModifier_ = false;
196 };
197 
198 class RSAnimatableRenderModifier : public RSRenderModifier {
199 public:
RSAnimatableRenderModifier(const std::shared_ptr<RSRenderPropertyBase> & property)200     RSAnimatableRenderModifier(const std::shared_ptr<RSRenderPropertyBase>& property)
201         : property_(property ? property : std::make_shared<RSRenderPropertyBase>())
202     {}
203 
204     ~RSAnimatableRenderModifier() override = default;
205 
GetPropertyId()206     PropertyId GetPropertyId() override
207     {
208         return property_->GetId();
209     }
210 
GetProperty()211     std::shared_ptr<RSRenderPropertyBase> GetProperty() const override
212     {
213         return property_;
214     }
215 
216 protected:
217     std::shared_ptr<RSRenderPropertyBase> property_;
218 
219     friend class RSRenderPropertyAnimation;
220 };
221 
222 class RSGeometryRenderModifier : public RSAnimatableRenderModifier {
223 public:
RSGeometryRenderModifier(const std::shared_ptr<RSRenderPropertyBase> & property)224     RSGeometryRenderModifier(const std::shared_ptr<RSRenderPropertyBase>& property)
225         : RSAnimatableRenderModifier(property)
226     {}
227 
228     ~RSGeometryRenderModifier() override = default;
229 };
230 
231 class RSBackgroundRenderModifier : public RSAnimatableRenderModifier {
232 public:
RSBackgroundRenderModifier(const std::shared_ptr<RSRenderPropertyBase> & property)233     RSBackgroundRenderModifier(const std::shared_ptr<RSRenderPropertyBase>& property)
234         : RSAnimatableRenderModifier(property)
235     {}
236 
237     ~RSBackgroundRenderModifier() override = default;
238 };
239 
240 class RSContentRenderModifier : public RSAnimatableRenderModifier {
241 public:
RSContentRenderModifier(const std::shared_ptr<RSRenderPropertyBase> & property)242     RSContentRenderModifier(const std::shared_ptr<RSRenderPropertyBase>& property)
243         : RSAnimatableRenderModifier(property)
244     {}
245 
246     ~RSContentRenderModifier() override = default;
247 };
248 
249 class RSForegroundRenderModifier : public RSAnimatableRenderModifier {
250 public:
RSForegroundRenderModifier(const std::shared_ptr<RSRenderPropertyBase> & property)251     RSForegroundRenderModifier(const std::shared_ptr<RSRenderPropertyBase>& property)
252         : RSAnimatableRenderModifier(property)
253     {}
254 
255     ~RSForegroundRenderModifier() override = default;
256 };
257 
258 class RSOverlayRenderModifier : public RSAnimatableRenderModifier {
259 public:
RSOverlayRenderModifier(const std::shared_ptr<RSRenderPropertyBase> & property)260     RSOverlayRenderModifier(const std::shared_ptr<RSRenderPropertyBase>& property)
261         : RSAnimatableRenderModifier(property)
262     {}
263 
264     ~RSOverlayRenderModifier() override = default;
265 };
266 
267 class RSAppearanceRenderModifier : public RSAnimatableRenderModifier {
268 public:
RSAppearanceRenderModifier(const std::shared_ptr<RSRenderPropertyBase> & property)269     RSAppearanceRenderModifier(const std::shared_ptr<RSRenderPropertyBase>& property)
270         : RSAnimatableRenderModifier(property)
271     {}
272 
273     ~RSAppearanceRenderModifier() override = default;
274 };
275 
276 class RSB_EXPORT RSHDRBrightnessRenderModifier : public RSAnimatableRenderModifier {
277 public:
RSHDRBrightnessRenderModifier(const std::shared_ptr<RSRenderPropertyBase> & property)278     RSHDRBrightnessRenderModifier(const std::shared_ptr<RSRenderPropertyBase>& property)
279         : RSAnimatableRenderModifier(property)
280     {
281         property->SetModifierType(RSModifierType::HDR_BRIGHTNESS);
282     }
283     ~RSHDRBrightnessRenderModifier() override = default;
284     void Apply(RSModifierContext& context) const override;
285     void Update(const std::shared_ptr<RSRenderPropertyBase>& prop, bool isDelta) override;
286     bool Marshalling(Parcel& parcel) override;
GetType()287     RSModifierType GetType() override
288     {
289         return RSModifierType::HDR_BRIGHTNESS;
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 class RSB_EXPORT RSCustomClipToFrameRenderModifier : public RSForegroundRenderModifier {
330 public:
RSCustomClipToFrameRenderModifier(const std::shared_ptr<RSRenderPropertyBase> & property)331     RSCustomClipToFrameRenderModifier(const std::shared_ptr<RSRenderPropertyBase>& property)
332         : RSForegroundRenderModifier(property)
333     {
334         property->SetModifierType(RSModifierType::CUSTOM_CLIP_TO_FRAME);
335     }
336     ~RSCustomClipToFrameRenderModifier() override = default;
337     void Apply(RSModifierContext& context) const override;
338     void Update(const std::shared_ptr<RSRenderPropertyBase>& prop, bool isDelta) override;
339     bool Marshalling(Parcel& parcel) override;
GetType()340     RSModifierType GetType() override
341     {
342         return RSModifierType::CUSTOM_CLIP_TO_FRAME;
343     }
344 };
345 
346 class RSB_EXPORT RSBehindWindowFilterEnabledRenderModifier : public RSBackgroundRenderModifier {
347 public:
RSBehindWindowFilterEnabledRenderModifier(const std::shared_ptr<RSRenderPropertyBase> & property)348     RSBehindWindowFilterEnabledRenderModifier(const std::shared_ptr<RSRenderPropertyBase>& property)
349         : RSBackgroundRenderModifier(property)
350     {
351         property->SetModifierType(RSModifierType::BEHIND_WINDOW_FILTER_ENABLED);
352     }
353     ~RSBehindWindowFilterEnabledRenderModifier() override = default;
Apply(RSModifierContext & context)354     void Apply(RSModifierContext& context) const override {}
355     void Update(const std::shared_ptr<RSRenderPropertyBase>& prop, bool isDelta) override;
356     bool Marshalling(Parcel& parcel) override;
GetType()357     RSModifierType GetType() override
358     {
359         return RSModifierType::BEHIND_WINDOW_FILTER_ENABLED;
360     }
361 };
362 
363 class RSB_EXPORT RSBehindWindowFilterRadiusRenderModifier : public RSBackgroundRenderModifier {
364 public:
RSBehindWindowFilterRadiusRenderModifier(const std::shared_ptr<RSRenderPropertyBase> & property)365     RSBehindWindowFilterRadiusRenderModifier(const std::shared_ptr<RSRenderPropertyBase>& property)
366         : RSBackgroundRenderModifier(property)
367     {
368         property->SetModifierType(RSModifierType::BEHIND_WINDOW_FILTER_RADIUS);
369     }
370     ~RSBehindWindowFilterRadiusRenderModifier() override = default;
Apply(RSModifierContext & context)371     void Apply(RSModifierContext& context) const override {}
372     void Update(const std::shared_ptr<RSRenderPropertyBase>& prop, bool isDelta) override;
373     bool Marshalling(Parcel& parcel) override;
GetType()374     RSModifierType GetType() override
375     {
376         return RSModifierType::BEHIND_WINDOW_FILTER_RADIUS;
377     }
378 };
379 
380 class RSB_EXPORT RSBehindWindowFilterSaturationRenderModifier : public RSBackgroundRenderModifier {
381 public:
RSBehindWindowFilterSaturationRenderModifier(const std::shared_ptr<RSRenderPropertyBase> & property)382     RSBehindWindowFilterSaturationRenderModifier(const std::shared_ptr<RSRenderPropertyBase>& property)
383         : RSBackgroundRenderModifier(property)
384     {
385         property->SetModifierType(RSModifierType::BEHIND_WINDOW_FILTER_SATURATION);
386     }
387     ~RSBehindWindowFilterSaturationRenderModifier() override = default;
Apply(RSModifierContext & context)388     void Apply(RSModifierContext& context) const override {}
389     void Update(const std::shared_ptr<RSRenderPropertyBase>& prop, bool isDelta) override;
390     bool Marshalling(Parcel& parcel) override;
GetType()391     RSModifierType GetType() override
392     {
393         return RSModifierType::BEHIND_WINDOW_FILTER_SATURATION;
394     }
395 };
396 
397 class RSB_EXPORT RSBehindWindowFilterBrightnessRenderModifier : public RSBackgroundRenderModifier {
398 public:
RSBehindWindowFilterBrightnessRenderModifier(const std::shared_ptr<RSRenderPropertyBase> & property)399     RSBehindWindowFilterBrightnessRenderModifier(const std::shared_ptr<RSRenderPropertyBase>& property)
400         : RSBackgroundRenderModifier(property)
401     {
402         property->SetModifierType(RSModifierType::BEHIND_WINDOW_FILTER_BRIGHTNESS);
403     }
404     ~RSBehindWindowFilterBrightnessRenderModifier() override = default;
Apply(RSModifierContext & context)405     void Apply(RSModifierContext& context) const override {}
406     void Update(const std::shared_ptr<RSRenderPropertyBase>& prop, bool isDelta) override;
407     bool Marshalling(Parcel& parcel) override;
GetType()408     RSModifierType GetType() override
409     {
410         return RSModifierType::BEHIND_WINDOW_FILTER_BRIGHTNESS;
411     }
412 };
413 
414 class RSB_EXPORT RSBehindWindowFilterMaskColorRenderModifier : public RSBackgroundRenderModifier {
415 public:
RSBehindWindowFilterMaskColorRenderModifier(const std::shared_ptr<RSRenderPropertyBase> & property)416     RSBehindWindowFilterMaskColorRenderModifier(const std::shared_ptr<RSRenderPropertyBase>& property)
417         : RSBackgroundRenderModifier(property)
418     {
419         property->SetModifierType(RSModifierType::BEHIND_WINDOW_FILTER_MASK_COLOR);
420     }
421     ~RSBehindWindowFilterMaskColorRenderModifier() override = default;
Apply(RSModifierContext & context)422     void Apply(RSModifierContext& context) const override {}
423     void Update(const std::shared_ptr<RSRenderPropertyBase>& prop, bool isDelta) override;
424     bool Marshalling(Parcel& parcel) override;
GetType()425     RSModifierType GetType() override
426     {
427         return RSModifierType::BEHIND_WINDOW_FILTER_MASK_COLOR;
428     }
429 };
430 
431 // declare RenderModifiers like RSBoundsRenderModifier
432 #define DECLARE_ANIMATABLE_MODIFIER(MODIFIER_NAME, TYPE, MODIFIER_TYPE, DELTA_OP, MODIFIER_TIER, THRESHOLD_TYPE) \
433     class RSB_EXPORT RS##MODIFIER_NAME##RenderModifier : public RS##MODIFIER_TIER##RenderModifier {              \
434     public:                                                                                                      \
435         RS##MODIFIER_NAME##RenderModifier(const std::shared_ptr<RSRenderPropertyBase>& property)                 \
436             : RS##MODIFIER_TIER##RenderModifier(property)                                                        \
437         {                                                                                                        \
438             property->SetModifierType(RSModifierType::MODIFIER_TYPE);                                            \
439         }                                                                                                        \
440         virtual ~RS##MODIFIER_NAME##RenderModifier() = default;                                                  \
441         void Apply(RSModifierContext& context) const override;                                                   \
442         void Update(const std::shared_ptr<RSRenderPropertyBase>& prop, bool isDelta) override;                   \
443         bool Marshalling(Parcel& parcel) override;                                                               \
444         RSModifierType GetType() override { return (RSModifierType::MODIFIER_TYPE); }                            \
445         virtual std::string GetModifierTypeString() override { return #MODIFIER_NAME; }                          \
446     };
447 
448 #define DECLARE_NOANIMATABLE_MODIFIER(MODIFIER_NAME, TYPE, MODIFIER_TYPE, MODIFIER_TIER) \
449     DECLARE_ANIMATABLE_MODIFIER(MODIFIER_NAME, TYPE, MODIFIER_TYPE, Add, MODIFIER_TIER, ZERO)
450 
451 DECLARE_NOANIMATABLE_MODIFIER(Particles, RSRenderParticleVector, PARTICLE, Foreground)
452 
453 #include "modifier/rs_modifiers_def.in"
454 
455 #undef DECLARE_ANIMATABLE_MODIFIER
456 #undef DECLARE_NOANIMATABLE_MODIFIER
457 } // namespace Rosen
458 } // namespace OHOS
459 
460 #endif // RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_RENDER_MODIFIER_H
461