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_MODIFIER_RS_MODIFIER_H
17 #define RENDER_SERVICE_CLIENT_CORE_MODIFIER_RS_MODIFIER_H
18 
19 #include "common/rs_macros.h"
20 #include "common/rs_obj_abs_geometry.h"
21 #include "modifier/rs_property.h"
22 
23 namespace OHOS {
24 namespace Rosen {
25 class RSC_EXPORT RSModifier : public std::enable_shared_from_this<RSModifier> {
26 public:
RSModifier(const std::shared_ptr<RSPropertyBase> & property)27     explicit RSModifier(const std::shared_ptr<RSPropertyBase>& property)
28         : property_(property ? property : std::make_shared<RSPropertyBase>())
29     {}
30 
31     virtual ~RSModifier() = default;
32 
GetProperty()33     std::shared_ptr<RSPropertyBase> GetProperty()
34     {
35         return property_;
36     }
37 
GetPropertyId()38     PropertyId GetPropertyId()
39     {
40         return property_->id_;
41     }
42 
43 protected:
RSModifier(const std::shared_ptr<RSPropertyBase> & property,const RSModifierType type)44     RSModifier(const std::shared_ptr<RSPropertyBase>& property, const RSModifierType type)
45         : property_(property ? property : std::make_shared<RSPropertyBase>())
46     {
47         property_->type_ = type;
48     }
49 
GetModifierType()50     virtual RSModifierType GetModifierType() const
51     {
52         return RSModifierType::INVALID;
53     }
54 
GetModifierTypeString()55     std::string GetModifierTypeString() const
56     {
57         auto modifierTypeString = std::make_shared<RSModifierTypeString>();
58         return modifierTypeString->GetModifierTypeString(GetModifierType());
59     }
60 
61     virtual RSPropertyModifierType GetPropertyModifierType() const = 0;
62 
GetTarget()63     std::weak_ptr<RSNode> GetTarget() const
64     {
65         return property_->target_;
66     }
67 
Apply(const std::shared_ptr<RSObjAbsGeometry> & geometry)68     virtual void Apply(const std::shared_ptr<RSObjAbsGeometry>& geometry) {}
69 
70     void AttachProperty(const std::shared_ptr<RSPropertyBase>& property);
71 
AttachToNode(const std::shared_ptr<RSNode> target)72     void AttachToNode(const std::shared_ptr<RSNode> target)
73     {
74         property_->target_ = std::weak_ptr<RSNode>(target);
75         property_->AttachModifier(shared_from_this());
76         MarkNodeDirty();
77         OnAttachToNode(target);
78     }
79 
DetachFromNode()80     void DetachFromNode()
81     {
82         MarkNodeDirty();
83         property_->target_.reset();
84     }
85 
SetMotionPathOption(const std::shared_ptr<RSMotionPathOption> & motionPathOption)86     void SetMotionPathOption(const std::shared_ptr<RSMotionPathOption>& motionPathOption)
87     {
88         property_->SetMotionPathOption(motionPathOption);
89     }
90 
GetRenderProperty()91     std::shared_ptr<RSRenderPropertyBase> GetRenderProperty() const
92     {
93         return property_->GetRenderProperty();
94     }
95 
96     virtual std::shared_ptr<RSRenderModifier> CreateRenderModifier() const = 0;
97 
UpdateToRender()98     virtual void UpdateToRender() {}
99 
OnAttachToNode(const std::weak_ptr<RSNode> & target)100     virtual void OnAttachToNode(const std::weak_ptr<RSNode>& target) {}
101 
102     void SetDirty(const bool isDirty);
103 
MarkNodeDirty()104     virtual void MarkNodeDirty() {}
105 
106     void ResetRSNodeExtendModifierDirty();
107 
108     bool isDirty_ { false };
109     std::shared_ptr<RSPropertyBase> property_;
110 
111     friend class RSModifierExtractor;
112     friend class RSModifierManager;
113     friend class RSNode;
114     friend class RSPathAnimation;
115     friend class RSPropertyBase;
116 };
117 
118 class RSC_EXPORT RSGeometryModifier : public RSModifier {
119 public:
RSGeometryModifier(const std::shared_ptr<RSPropertyBase> & property,const RSModifierType type)120     RSGeometryModifier(const std::shared_ptr<RSPropertyBase>& property, const RSModifierType type)
121         : RSModifier(property, type)
122     {}
123 
124     virtual ~RSGeometryModifier() = default;
125 
126 protected:
GetPropertyModifierType()127     RSPropertyModifierType GetPropertyModifierType() const override
128     {
129         return RSPropertyModifierType::GEOMETRY;
130     }
131 
MarkNodeDirty()132     void MarkNodeDirty() override
133     {
134         if (auto node = GetTarget().lock()) {
135             node->MarkDirty(NodeDirtyType::GEOMETRY, true);
136         }
137     }
138 };
139 
140 class RSC_EXPORT RSBackgroundModifier : public RSModifier {
141 public:
RSBackgroundModifier(const std::shared_ptr<RSPropertyBase> & property,const RSModifierType type)142     RSBackgroundModifier(const std::shared_ptr<RSPropertyBase>& property, const RSModifierType type)
143         : RSModifier(property, type)
144     {}
145 
146     virtual ~RSBackgroundModifier() = default;
147 
148 protected:
GetPropertyModifierType()149     RSPropertyModifierType GetPropertyModifierType() const override
150     {
151         return RSPropertyModifierType::BACKGROUND;
152     }
153 };
154 
155 class RSC_EXPORT RSContentModifier : public RSModifier {
156 public:
RSContentModifier(const std::shared_ptr<RSPropertyBase> & property,const RSModifierType type)157     RSContentModifier(const std::shared_ptr<RSPropertyBase>& property, const RSModifierType type)
158         : RSModifier(property, type)
159     {}
160 
161     virtual ~RSContentModifier() = default;
162 
163 protected:
GetPropertyModifierType()164     RSPropertyModifierType GetPropertyModifierType() const override
165     {
166         return RSPropertyModifierType::CONTENT;
167     }
168 };
169 
170 class RSC_EXPORT RSForegroundModifier : public RSModifier {
171 public:
RSForegroundModifier(const std::shared_ptr<RSPropertyBase> & property,const RSModifierType type)172     RSForegroundModifier(const std::shared_ptr<RSPropertyBase>& property, const RSModifierType type)
173         : RSModifier(property, type)
174     {}
175 
176     virtual ~RSForegroundModifier() = default;
177 
178 protected:
GetPropertyModifierType()179     RSPropertyModifierType GetPropertyModifierType() const override
180     {
181         return RSPropertyModifierType::FOREGROUND;
182     }
183 };
184 
185 class RSC_EXPORT RSOverlayModifier : public RSModifier {
186 public:
RSOverlayModifier(const std::shared_ptr<RSPropertyBase> & property,const RSModifierType type)187     RSOverlayModifier(const std::shared_ptr<RSPropertyBase>& property, const RSModifierType type)
188         : RSModifier(property, type)
189     {}
190 
191     virtual ~RSOverlayModifier() = default;
192 
193 protected:
GetPropertyModifierType()194     RSPropertyModifierType GetPropertyModifierType() const override
195     {
196         return RSPropertyModifierType::OVERLAY;
197     }
198 };
199 
200 class RSC_EXPORT RSAppearanceModifier : public RSModifier {
201 public:
RSAppearanceModifier(const std::shared_ptr<RSPropertyBase> & property,const RSModifierType type)202     RSAppearanceModifier(const std::shared_ptr<RSPropertyBase>& property, const RSModifierType type)
203         : RSModifier(property, type)
204     {}
205 
206     virtual ~RSAppearanceModifier() = default;
207 
208 protected:
GetPropertyModifierType()209     RSPropertyModifierType GetPropertyModifierType() const override
210     {
211         return RSPropertyModifierType::APPEARANCE;
212     }
213 
MarkNodeDirty()214     void MarkNodeDirty() override
215     {
216         if (auto node = GetTarget().lock()) {
217             node->MarkDirty(NodeDirtyType::APPEARANCE, true);
218         }
219     }
220 };
221 } // namespace Rosen
222 } // namespace OHOS
223 
224 #endif // RENDER_SERVICE_CLIENT_CORE_MODIFIER_RS_MODIFIER_H
225