• 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_PROP_H
17 #define RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_RENDER_PROP_H
18 
19 #include "animation/rs_value_estimator.h"
20 #include "common/rs_common_def.h"
21 #include "common/rs_macros.h"
22 #include "modifier/rs_animatable_arithmetic.h"
23 #include "modifier/rs_modifier_type.h"
24 #include "transaction/rs_marshalling_helper.h"
25 
26 namespace OHOS {
27 namespace Rosen {
28 class RSRenderNode;
29 
30 class RSB_EXPORT RSRenderPropertyBase : public std::enable_shared_from_this<RSRenderPropertyBase> {
31 public:
32     RSRenderPropertyBase() = default;
RSRenderPropertyBase(const PropertyId & id)33     RSRenderPropertyBase(const PropertyId& id) : id_(id) {}
34     RSRenderPropertyBase(const RSRenderPropertyBase&) = delete;
35     RSRenderPropertyBase(const RSRenderPropertyBase&&) = delete;
36     RSRenderPropertyBase& operator=(const RSRenderPropertyBase&) = delete;
37     RSRenderPropertyBase& operator=(const RSRenderPropertyBase&&) = delete;
38     virtual ~RSRenderPropertyBase() = default;
39 
GetId()40     PropertyId GetId() const
41     {
42         return id_;
43     }
44 
Attach(std::weak_ptr<RSRenderNode> node)45     void Attach(std::weak_ptr<RSRenderNode> node)
46     {
47         node_ = node;
48         OnChange();
49     }
50 
SetModifierType(RSModifierType type)51     void SetModifierType(RSModifierType type)
52     {
53         modifierType_ = type;
54     }
55 
56     static bool Marshalling(Parcel& parcel, const std::shared_ptr<RSRenderPropertyBase>& val);
57     [[nodiscard]] static bool Unmarshalling(Parcel& parcel, std::shared_ptr<RSRenderPropertyBase>& val);
58 
59 protected:
60     void OnChange() const;
61 
Clone()62     virtual const std::shared_ptr<RSRenderPropertyBase> Clone() const
63     {
64         return nullptr;
65     }
66 
SetValue(const std::shared_ptr<RSRenderPropertyBase> & value)67     virtual void SetValue(const std::shared_ptr<RSRenderPropertyBase>& value) {}
68 
SetPropertyType(const RSRenderPropertyType type)69     virtual void SetPropertyType(const RSRenderPropertyType type) {}
70 
GetPropertyType()71     virtual RSRenderPropertyType GetPropertyType() const
72     {
73         return RSRenderPropertyType::INVALID;
74     }
75 
ToFloat()76     virtual float ToFloat() const
77     {
78         return 1.f;
79     }
80 
CreateRSValueEstimator(const RSValueEstimatorType type)81     virtual std::shared_ptr<RSValueEstimator> CreateRSValueEstimator(const RSValueEstimatorType type)
82     {
83         return nullptr;
84     }
85 
86     PropertyId id_;
87     std::weak_ptr<RSRenderNode> node_;
88     RSModifierType modifierType_ { RSModifierType::INVALID };
89 
90 private:
Add(const std::shared_ptr<const RSRenderPropertyBase> & value)91     virtual std::shared_ptr<RSRenderPropertyBase> Add(const std::shared_ptr<const RSRenderPropertyBase>& value)
92     {
93         return shared_from_this();
94     }
95 
Minus(const std::shared_ptr<const RSRenderPropertyBase> & value)96     virtual std::shared_ptr<RSRenderPropertyBase> Minus(const std::shared_ptr<const RSRenderPropertyBase>& value)
97     {
98         return shared_from_this();
99     }
100 
Multiply(const float scale)101     virtual std::shared_ptr<RSRenderPropertyBase> Multiply(const float scale)
102     {
103         return shared_from_this();
104     }
105 
IsEqual(const std::shared_ptr<const RSRenderPropertyBase> & value)106     virtual bool IsEqual(const std::shared_ptr<const RSRenderPropertyBase>& value) const
107     {
108         return true;
109     }
110 
111     friend std::shared_ptr<RSRenderPropertyBase> operator+=(
112         const std::shared_ptr<RSRenderPropertyBase>& a, const std::shared_ptr<const RSRenderPropertyBase>& b);
113     friend std::shared_ptr<RSRenderPropertyBase> operator-=(
114         const std::shared_ptr<RSRenderPropertyBase>& a, const std::shared_ptr<const RSRenderPropertyBase>& b);
115     friend std::shared_ptr<RSRenderPropertyBase> operator*=(
116         const std::shared_ptr<RSRenderPropertyBase>& value, const float scale);
117     friend std::shared_ptr<RSRenderPropertyBase> operator+(
118         const std::shared_ptr<const RSRenderPropertyBase>& a, const std::shared_ptr<const RSRenderPropertyBase>& b);
119     friend std::shared_ptr<RSRenderPropertyBase> operator-(
120         const std::shared_ptr<const RSRenderPropertyBase>& a, const std::shared_ptr<const RSRenderPropertyBase>& b);
121     friend std::shared_ptr<RSRenderPropertyBase> operator*(
122         const std::shared_ptr<const RSRenderPropertyBase>& value, const float scale);
123     friend bool operator==(
124         const std::shared_ptr<const RSRenderPropertyBase>& a, const std::shared_ptr<const RSRenderPropertyBase>& b);
125     friend bool operator!=(
126         const std::shared_ptr<const RSRenderPropertyBase>& a, const std::shared_ptr<const RSRenderPropertyBase>& b);
127     friend class RSRenderPropertyAnimation;
128     friend class RSMarshallingHelper;
129     friend class RSValueEstimator;
130     friend class RSRenderPathAnimation;
131     friend class RSRenderSpringAnimation;
132     friend class RSRenderInterpolatingSpringAnimation;
133     friend class RSRenderCurveAnimation;
134     friend class RSRenderKeyframeAnimation;
135     template<typename T>
136     friend class RSSpringModel;
137     friend class RSTransitionCustom;
138 };
139 
140 template<typename T>
141 class RSB_EXPORT_TMP RSRenderProperty : public RSRenderPropertyBase {
142 public:
RSRenderProperty()143     RSRenderProperty() : RSRenderPropertyBase(0) {}
RSRenderProperty(const T & value,const PropertyId & id)144     RSRenderProperty(const T& value, const PropertyId& id) : RSRenderPropertyBase(id), stagingValue_(value) {}
RSRenderProperty(const T & value,const PropertyId & id,const RSRenderPropertyType type)145     RSRenderProperty(const T& value, const PropertyId& id, const RSRenderPropertyType type)
146         : RSRenderPropertyBase(id), stagingValue_(value)
147     {}
148     virtual ~RSRenderProperty() = default;
149 
Set(const T & value)150     void Set(const T& value)
151     {
152         if (value == stagingValue_) {
153             return;
154         }
155         if (!IsNeedUpdate(value)) {
156             return;
157         }
158         stagingValue_ = value;
159         OnChange();
160         if (updateUIPropertyFunc_) {
161             updateUIPropertyFunc_(shared_from_this());
162         }
163     }
164 
Get()165     T Get() const
166     {
167         return stagingValue_;
168     }
169 
SetUpdateUIPropertyFunc(const std::function<void (const std::shared_ptr<RSRenderPropertyBase> &)> & updateUIPropertyFunc)170     void SetUpdateUIPropertyFunc(
171         const std::function<void(const std::shared_ptr<RSRenderPropertyBase>&)>& updateUIPropertyFunc)
172     {
173         updateUIPropertyFunc_ = updateUIPropertyFunc;
174     }
175 
176 protected:
177     T stagingValue_;
178     std::function<void(const std::shared_ptr<RSRenderPropertyBase>&)> updateUIPropertyFunc_;
GetPropertyType()179     RSRenderPropertyType GetPropertyType() const
180     {
181         return RSRenderPropertyType::INVALID;
182     }
183 
IsNeedUpdate(const T & value)184     bool IsNeedUpdate(const T& value) const
185     {
186         return true;
187     }
188 
189     friend class RSMarshallingHelper;
190 };
191 
192 template<typename T>
193 class RSB_EXPORT_TMP RSRenderAnimatableProperty : public RSRenderProperty<T> {
194 public:
RSRenderAnimatableProperty()195     RSRenderAnimatableProperty() : RSRenderProperty<T>() {}
RSRenderAnimatableProperty(const T & value)196     RSRenderAnimatableProperty(const T& value) : RSRenderProperty<T>(value, 0) {}
RSRenderAnimatableProperty(const T & value,const PropertyId & id)197     RSRenderAnimatableProperty(const T& value, const PropertyId& id) : RSRenderProperty<T>(value, id) {}
RSRenderAnimatableProperty(const T & value,const PropertyId & id,const RSRenderPropertyType type)198     RSRenderAnimatableProperty(const T& value, const PropertyId& id, const RSRenderPropertyType type)
199         : RSRenderProperty<T>(value, id), type_(type)
200     {}
201     virtual ~RSRenderAnimatableProperty() = default;
202 
203 protected:
Clone()204     const std::shared_ptr<RSRenderPropertyBase> Clone() const override
205     {
206         return std::make_shared<RSRenderAnimatableProperty<T>>(
207             RSRenderProperty<T>::stagingValue_, RSRenderProperty<T>::id_, type_);
208     }
209 
SetValue(const std::shared_ptr<RSRenderPropertyBase> & value)210     void SetValue(const std::shared_ptr<RSRenderPropertyBase>& value) override
211     {
212         auto property = std::static_pointer_cast<RSRenderAnimatableProperty<T>>(value);
213         if (property != nullptr && property->GetPropertyType() == type_) {
214             RSRenderProperty<T>::Set(property->Get());
215         }
216     }
217 
SetPropertyType(const RSRenderPropertyType type)218     void SetPropertyType(const RSRenderPropertyType type) override
219     {
220         type_ = type;
221     }
222 
GetPropertyType()223     virtual RSRenderPropertyType GetPropertyType() const override
224     {
225         return type_;
226     }
227 
ToFloat()228     float ToFloat() const override
229     {
230         return 1.f;
231     }
232 
CreateRSValueEstimator(const RSValueEstimatorType type)233     std::shared_ptr<RSValueEstimator> CreateRSValueEstimator(const RSValueEstimatorType type) override
234     {
235         switch (type) {
236             case RSValueEstimatorType::CURVE_VALUE_ESTIMATOR: {
237                 return std::make_shared<RSCurveValueEstimator<T>>();
238             }
239             case RSValueEstimatorType::KEYFRAME_VALUE_ESTIMATOR: {
240                 return std::make_shared<RSKeyframeValueEstimator<T>>();
241             }
242             default: {
243                 return nullptr;
244             }
245         }
246     }
247 
248 private:
249     RSRenderPropertyType type_ = RSRenderPropertyType::INVALID;
250 
Add(const std::shared_ptr<const RSRenderPropertyBase> & value)251     std::shared_ptr<RSRenderPropertyBase> Add(const std::shared_ptr<const RSRenderPropertyBase>& value) override
252     {
253         auto animatableProperty = std::static_pointer_cast<const RSRenderAnimatableProperty<T>>(value);
254         if (animatableProperty != nullptr) {
255             RSRenderProperty<T>::stagingValue_ = RSRenderProperty<T>::stagingValue_ + animatableProperty->stagingValue_;
256         }
257         return RSRenderProperty<T>::shared_from_this();
258     }
259 
Minus(const std::shared_ptr<const RSRenderPropertyBase> & value)260     std::shared_ptr<RSRenderPropertyBase> Minus(const std::shared_ptr<const RSRenderPropertyBase>& value) override
261     {
262         auto animatableProperty = std::static_pointer_cast<const RSRenderAnimatableProperty<T>>(value);
263         if (animatableProperty != nullptr) {
264             RSRenderProperty<T>::stagingValue_ = RSRenderProperty<T>::stagingValue_ - animatableProperty->stagingValue_;
265         }
266         return RSRenderProperty<T>::shared_from_this();
267     }
268 
Multiply(const float scale)269     std::shared_ptr<RSRenderPropertyBase> Multiply(const float scale) override
270     {
271         RSRenderProperty<T>::stagingValue_ = RSRenderProperty<T>::stagingValue_ * scale;
272         return RSRenderProperty<T>::shared_from_this();
273     }
274 
IsEqual(const std::shared_ptr<const RSRenderPropertyBase> & value)275     bool IsEqual(const std::shared_ptr<const RSRenderPropertyBase>& value) const override
276     {
277         auto animatableProperty = std::static_pointer_cast<const RSRenderAnimatableProperty<T>>(value);
278         if (animatableProperty != nullptr) {
279             return RSRenderProperty<T>::stagingValue_ == animatableProperty->stagingValue_;
280         }
281         return true;
282     }
283 
284     friend class RSMarshallingHelper;
285     friend class RSRenderPathAnimation;
286     friend class RSRenderPropertyBase;
287 };
288 
289 template<>
290 RSB_EXPORT float RSRenderAnimatableProperty<float>::ToFloat() const;
291 template<>
292 RSB_EXPORT float RSRenderAnimatableProperty<Vector4f>::ToFloat() const;
293 template<>
294 RSB_EXPORT float RSRenderAnimatableProperty<Quaternion>::ToFloat() const;
295 template<>
296 RSB_EXPORT float RSRenderAnimatableProperty<Vector2f>::ToFloat() const;
297 
298 #if defined(_WIN32)
299 extern template class RSRenderAnimatableProperty<float>;
300 extern template class RSRenderAnimatableProperty<Vector4f>;
301 extern template class RSRenderAnimatableProperty<Quaternion>;
302 extern template class RSRenderAnimatableProperty<Vector2f>;
303 #endif
304 
305 template<>
306 RSB_EXPORT bool RSRenderProperty<float>::IsNeedUpdate(const float& value) const;
307 
308 #if defined(_WIN32)
309 extern template class RSRenderProperty<float>;
310 #endif
311 } // namespace Rosen
312 } // namespace OHOS
313 
314 #endif // RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_RENDER_PROP_H
315