• 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 "common/rs_rect.h"
23 #include "modifier/rs_animatable_arithmetic.h"
24 #include "modifier/rs_modifier_type.h"
25 #include "property/rs_properties_def.h"
26 #include "recording/draw_cmd_list.h"
27 #include "transaction/rs_marshalling_helper.h"
28 
29 namespace OHOS {
30 namespace Rosen {
31 class RSRenderNode;
32 
33 enum PropertyUpdateType : int8_t {
34     UPDATE_TYPE_OVERWRITE,       // overwrite by given value
35     UPDATE_TYPE_INCREMENTAL,     // incremental update by given value
36     UPDATE_TYPE_FORCE_OVERWRITE, // overwrite and cancel all previous animations
37 };
38 
39 class RSB_EXPORT RSRenderPropertyBase : public std::enable_shared_from_this<RSRenderPropertyBase> {
40 public:
41     RSRenderPropertyBase() = default;
RSRenderPropertyBase(const PropertyId & id)42     RSRenderPropertyBase(const PropertyId& id) : id_(id) {}
43     RSRenderPropertyBase(const RSRenderPropertyBase&) = delete;
44     RSRenderPropertyBase(const RSRenderPropertyBase&&) = delete;
45     RSRenderPropertyBase& operator=(const RSRenderPropertyBase&) = delete;
46     RSRenderPropertyBase& operator=(const RSRenderPropertyBase&&) = delete;
47     virtual ~RSRenderPropertyBase() = default;
48 
GetId()49     PropertyId GetId() const
50     {
51         return id_;
52     }
53 
Attach(std::weak_ptr<RSRenderNode> node)54     void Attach(std::weak_ptr<RSRenderNode> node)
55     {
56         node_ = node;
57         OnChange();
58     }
59 
SetModifierType(RSModifierType type)60     void SetModifierType(RSModifierType type)
61     {
62         modifierType_ = type;
63         UpdatePropertyUnit(type);
64     }
65 
Dump(std::string & out)66     virtual void Dump(std::string& out) const
67     {
68     }
69 
70     static bool Marshalling(Parcel& parcel, const std::shared_ptr<RSRenderPropertyBase>& val);
71     [[nodiscard]] static bool Unmarshalling(Parcel& parcel, std::shared_ptr<RSRenderPropertyBase>& val);
72 
73 protected:
74     void OnChange() const;
75 
76     void UpdatePropertyUnit(RSModifierType type);
77 
Clone()78     virtual const std::shared_ptr<RSRenderPropertyBase> Clone() const
79     {
80         return nullptr;
81     }
82 
SetValue(const std::shared_ptr<RSRenderPropertyBase> & value)83     virtual void SetValue(const std::shared_ptr<RSRenderPropertyBase>& value) {}
84 
SetPropertyType(const RSRenderPropertyType type)85     virtual void SetPropertyType(const RSRenderPropertyType type) {}
86 
GetPropertyType()87     virtual RSRenderPropertyType GetPropertyType() const
88     {
89         return RSRenderPropertyType::INVALID;
90     }
91 
SetPropertyUnit(RSPropertyUnit unit)92     virtual void SetPropertyUnit(RSPropertyUnit unit) {}
93 
GetPropertyUnit()94     virtual RSPropertyUnit GetPropertyUnit() const
95     {
96         return RSPropertyUnit::UNKNOWN;
97     }
98 
ToFloat()99     virtual float ToFloat() const
100     {
101         return 1.f;
102     }
103 
CreateRSValueEstimator(const RSValueEstimatorType type)104     virtual std::shared_ptr<RSValueEstimator> CreateRSValueEstimator(const RSValueEstimatorType type)
105     {
106         return nullptr;
107     }
108 
CreateRSSpringValueEstimator()109     virtual std::shared_ptr<RSSpringValueEstimatorBase> CreateRSSpringValueEstimator()
110     {
111         return nullptr;
112     }
113 
IsNearEqual(const std::shared_ptr<RSRenderPropertyBase> & value,float zeroThreshold)114     virtual bool IsNearEqual(const std::shared_ptr<RSRenderPropertyBase>& value, float zeroThreshold) const
115     {
116         return true;
117     }
118 
119     PropertyId id_;
120     std::weak_ptr<RSRenderNode> node_;
121     RSModifierType modifierType_ { RSModifierType::INVALID };
122 
123 private:
Add(const std::shared_ptr<const RSRenderPropertyBase> & value)124     virtual std::shared_ptr<RSRenderPropertyBase> Add(const std::shared_ptr<const RSRenderPropertyBase>& value)
125     {
126         return shared_from_this();
127     }
128 
Minus(const std::shared_ptr<const RSRenderPropertyBase> & value)129     virtual std::shared_ptr<RSRenderPropertyBase> Minus(const std::shared_ptr<const RSRenderPropertyBase>& value)
130     {
131         return shared_from_this();
132     }
133 
Multiply(const float scale)134     virtual std::shared_ptr<RSRenderPropertyBase> Multiply(const float scale)
135     {
136         return shared_from_this();
137     }
138 
IsEqual(const std::shared_ptr<const RSRenderPropertyBase> & value)139     virtual bool IsEqual(const std::shared_ptr<const RSRenderPropertyBase>& value) const
140     {
141         return true;
142     }
143 
144     friend std::shared_ptr<RSRenderPropertyBase> operator+=(
145         const std::shared_ptr<RSRenderPropertyBase>& a, const std::shared_ptr<const RSRenderPropertyBase>& b);
146     friend std::shared_ptr<RSRenderPropertyBase> operator-=(
147         const std::shared_ptr<RSRenderPropertyBase>& a, const std::shared_ptr<const RSRenderPropertyBase>& b);
148     friend std::shared_ptr<RSRenderPropertyBase> operator*=(
149         const std::shared_ptr<RSRenderPropertyBase>& value, const float scale);
150     friend std::shared_ptr<RSRenderPropertyBase> operator+(
151         const std::shared_ptr<const RSRenderPropertyBase>& a, const std::shared_ptr<const RSRenderPropertyBase>& b);
152     friend std::shared_ptr<RSRenderPropertyBase> operator-(
153         const std::shared_ptr<const RSRenderPropertyBase>& a, const std::shared_ptr<const RSRenderPropertyBase>& b);
154     friend std::shared_ptr<RSRenderPropertyBase> operator*(
155         const std::shared_ptr<const RSRenderPropertyBase>& value, const float scale);
156     friend bool operator==(
157         const std::shared_ptr<const RSRenderPropertyBase>& a, const std::shared_ptr<const RSRenderPropertyBase>& b);
158     friend bool operator!=(
159         const std::shared_ptr<const RSRenderPropertyBase>& a, const std::shared_ptr<const RSRenderPropertyBase>& b);
160     friend class RSAnimationRateDecider;
161     friend class RSRenderPropertyAnimation;
162     friend class RSMarshallingHelper;
163     friend class RSValueEstimator;
164     friend class RSRenderPathAnimation;
165     friend class RSRenderSpringAnimation;
166     friend class RSRenderInterpolatingSpringAnimation;
167     friend class RSRenderCurveAnimation;
168     friend class RSRenderKeyframeAnimation;
169     template<typename T>
170     friend class RSSpringModel;
171     friend class RSTransitionCustom;
172     friend class RSAnimationTraceUtils;
173 };
174 
175 template<typename T>
176 class RSB_EXPORT_TMP RSRenderProperty : public RSRenderPropertyBase {
177 public:
RSRenderProperty()178     RSRenderProperty() : RSRenderPropertyBase(0) {}
RSRenderProperty(const T & value,const PropertyId & id)179     RSRenderProperty(const T& value, const PropertyId& id) : RSRenderPropertyBase(id), stagingValue_(value) {}
RSRenderProperty(const T & value,const PropertyId & id,const RSRenderPropertyType type)180     RSRenderProperty(const T& value, const PropertyId& id, const RSRenderPropertyType type)
181         : RSRenderPropertyBase(id), stagingValue_(value)
182     {}
183     virtual ~RSRenderProperty() = default;
184 
Set(const T & value)185     void Set(const T& value)
186     {
187         if (value == stagingValue_) {
188             return;
189         }
190         stagingValue_ = value;
191         OnChange();
192         if (updateUIPropertyFunc_) {
193             updateUIPropertyFunc_(shared_from_this());
194         }
195     }
196 
Get()197     T Get() const
198     {
199         return stagingValue_;
200     }
201 
GetRef()202     T& GetRef()
203     {
204         return stagingValue_;
205     }
206 
Dump(std::string & out)207     void Dump(std::string& out) const override
208     {
209     }
210 
SetUpdateUIPropertyFunc(const std::function<void (const std::shared_ptr<RSRenderPropertyBase> &)> & updateUIPropertyFunc)211     void SetUpdateUIPropertyFunc(
212         const std::function<void(const std::shared_ptr<RSRenderPropertyBase>&)>& updateUIPropertyFunc)
213     {
214         updateUIPropertyFunc_ = updateUIPropertyFunc;
215     }
216 
217 protected:
218     T stagingValue_;
219     std::function<void(const std::shared_ptr<RSRenderPropertyBase>&)> updateUIPropertyFunc_;
GetPropertyType()220     RSRenderPropertyType GetPropertyType() const override
221     {
222         return RSRenderPropertyType::INVALID;
223     }
224 
225     friend class RSMarshallingHelper;
226 };
227 
228 template<typename T>
229 class RSB_EXPORT_TMP RSRenderAnimatableProperty : public RSRenderProperty<T> {
230 public:
RSRenderAnimatableProperty()231     RSRenderAnimatableProperty() : RSRenderProperty<T>() {}
RSRenderAnimatableProperty(const T & value)232     RSRenderAnimatableProperty(const T& value) : RSRenderProperty<T>(value, 0) {}
RSRenderAnimatableProperty(const T & value,const PropertyId & id)233     RSRenderAnimatableProperty(const T& value, const PropertyId& id) : RSRenderProperty<T>(value, id) {}
RSRenderAnimatableProperty(const T & value,const PropertyId & id,const RSRenderPropertyType type)234     RSRenderAnimatableProperty(const T& value, const PropertyId& id, const RSRenderPropertyType type)
235         : RSRenderProperty<T>(value, id), type_(type)
236     {}
RSRenderAnimatableProperty(const T & value,const PropertyId & id,const RSRenderPropertyType type,const RSPropertyUnit unit)237     RSRenderAnimatableProperty(const T& value, const PropertyId& id,
238         const RSRenderPropertyType type, const RSPropertyUnit unit)
239         : RSRenderProperty<T>(value, id), type_(type), unit_(unit)
240     {}
241     virtual ~RSRenderAnimatableProperty() = default;
242 
243 protected:
Clone()244     const std::shared_ptr<RSRenderPropertyBase> Clone() const override
245     {
246         return std::make_shared<RSRenderAnimatableProperty<T>>(
247             RSRenderProperty<T>::stagingValue_, RSRenderProperty<T>::id_, type_, unit_);
248     }
249 
SetValue(const std::shared_ptr<RSRenderPropertyBase> & value)250     void SetValue(const std::shared_ptr<RSRenderPropertyBase>& value) override
251     {
252         auto property = std::static_pointer_cast<RSRenderAnimatableProperty<T>>(value);
253         if (property != nullptr && property->GetPropertyType() == type_) {
254             RSRenderProperty<T>::Set(property->Get());
255         }
256     }
257 
SetPropertyType(const RSRenderPropertyType type)258     void SetPropertyType(const RSRenderPropertyType type) override
259     {
260         type_ = type;
261     }
262 
GetPropertyType()263     virtual RSRenderPropertyType GetPropertyType() const override
264     {
265         return type_;
266     }
267 
SetPropertyUnit(RSPropertyUnit unit)268     void SetPropertyUnit(RSPropertyUnit unit) override
269     {
270         unit_ = unit;
271     }
272 
GetPropertyUnit()273     RSPropertyUnit GetPropertyUnit() const override
274     {
275         return unit_;
276     }
277 
ToFloat()278     float ToFloat() const override
279     {
280         return 1.f;
281     }
282 
IsNearEqual(const std::shared_ptr<RSRenderPropertyBase> & value,float zeroThreshold)283     bool IsNearEqual(const std::shared_ptr<RSRenderPropertyBase>& value, float zeroThreshold) const override
284     {
285         return IsEqual(value);
286     }
287 
CreateRSValueEstimator(const RSValueEstimatorType type)288     std::shared_ptr<RSValueEstimator> CreateRSValueEstimator(const RSValueEstimatorType type) override
289     {
290         switch (type) {
291             case RSValueEstimatorType::CURVE_VALUE_ESTIMATOR: {
292                 return std::make_shared<RSCurveValueEstimator<T>>();
293             }
294             case RSValueEstimatorType::KEYFRAME_VALUE_ESTIMATOR: {
295                 return std::make_shared<RSKeyframeValueEstimator<T>>();
296             }
297             default: {
298                 return nullptr;
299             }
300         }
301     }
302 
CreateRSSpringValueEstimator()303     std::shared_ptr<RSSpringValueEstimatorBase> CreateRSSpringValueEstimator() override
304     {
305         return std::make_shared<RSSpringValueEstimator<T>>();
306     }
307 
308 private:
309     RSRenderPropertyType type_ = RSRenderPropertyType::INVALID;
310     RSPropertyUnit unit_ = RSPropertyUnit::UNKNOWN;
311 
Add(const std::shared_ptr<const RSRenderPropertyBase> & value)312     std::shared_ptr<RSRenderPropertyBase> Add(const std::shared_ptr<const RSRenderPropertyBase>& value) override
313     {
314         auto animatableProperty = std::static_pointer_cast<const RSRenderAnimatableProperty<T>>(value);
315         if (animatableProperty != nullptr) {
316             RSRenderProperty<T>::stagingValue_ = RSRenderProperty<T>::stagingValue_ + animatableProperty->stagingValue_;
317         }
318         return RSRenderProperty<T>::shared_from_this();
319     }
320 
Minus(const std::shared_ptr<const RSRenderPropertyBase> & value)321     std::shared_ptr<RSRenderPropertyBase> Minus(const std::shared_ptr<const RSRenderPropertyBase>& value) override
322     {
323         auto animatableProperty = std::static_pointer_cast<const RSRenderAnimatableProperty<T>>(value);
324         if (animatableProperty != nullptr) {
325             RSRenderProperty<T>::stagingValue_ = RSRenderProperty<T>::stagingValue_ - animatableProperty->stagingValue_;
326         }
327         return RSRenderProperty<T>::shared_from_this();
328     }
329 
Multiply(const float scale)330     std::shared_ptr<RSRenderPropertyBase> Multiply(const float scale) override
331     {
332         RSRenderProperty<T>::stagingValue_ = RSRenderProperty<T>::stagingValue_ * scale;
333         return RSRenderProperty<T>::shared_from_this();
334     }
335 
IsEqual(const std::shared_ptr<const RSRenderPropertyBase> & value)336     bool IsEqual(const std::shared_ptr<const RSRenderPropertyBase>& value) const override
337     {
338         auto animatableProperty = std::static_pointer_cast<const RSRenderAnimatableProperty<T>>(value);
339         if (animatableProperty != nullptr) {
340             return RSRenderProperty<T>::stagingValue_ == animatableProperty->stagingValue_;
341         }
342         return true;
343     }
344 
345     friend class RSMarshallingHelper;
346     friend class RSRenderPathAnimation;
347     friend class RSRenderPropertyBase;
348 };
349 
350 template<>
351 RSB_EXPORT float RSRenderAnimatableProperty<float>::ToFloat() const;
352 template<>
353 RSB_EXPORT float RSRenderAnimatableProperty<Vector4f>::ToFloat() const;
354 template<>
355 RSB_EXPORT float RSRenderAnimatableProperty<Quaternion>::ToFloat() const;
356 template<>
357 RSB_EXPORT float RSRenderAnimatableProperty<Vector2f>::ToFloat() const;
358 
359 template<>
360 RSB_EXPORT void RSRenderProperty<float>::Dump(std::string& out) const;
361 template<>
362 RSB_EXPORT void RSRenderProperty<Vector4f>::Dump(std::string& out) const;
363 template<>
364 RSB_EXPORT void RSRenderProperty<Quaternion>::Dump(std::string& out) const;
365 template<>
366 RSB_EXPORT void RSRenderProperty<Vector2f>::Dump(std::string& out) const;
367 template<>
368 RSB_EXPORT void RSRenderProperty<Matrix3f>::Dump(std::string& out) const;
369 template<>
370 RSB_EXPORT void RSRenderProperty<Color>::Dump(std::string& out) const;
371 template<>
372 RSB_EXPORT void RSRenderProperty<std::shared_ptr<RSFilter>>::Dump(std::string& out) const;
373 template<>
374 RSB_EXPORT void RSRenderProperty<Vector4<Color>>::Dump(std::string& out) const;
375 template<>
376 RSB_EXPORT void RSRenderProperty<RRect>::Dump(std::string& out) const;
377 template<>
378 RSB_EXPORT void RSRenderProperty<Drawing::DrawCmdListPtr>::Dump(std::string& out) const;
379 template<>
380 RSB_EXPORT void RSRenderProperty<ForegroundColorStrategyType>::Dump(std::string& out) const;
381 template<>
382 RSB_EXPORT void RSRenderProperty<SkMatrix>::Dump(std::string& out) const;
383 
384 template<>
385 RSB_EXPORT bool RSRenderAnimatableProperty<float>::IsNearEqual(
386     const std::shared_ptr<RSRenderPropertyBase>& value, float zeroThreshold) const;
387 template<>
388 RSB_EXPORT bool RSRenderAnimatableProperty<Vector4f>::IsNearEqual(
389     const std::shared_ptr<RSRenderPropertyBase>& value, float zeroThreshold) const;
390 template<>
391 RSB_EXPORT bool RSRenderAnimatableProperty<Quaternion>::IsNearEqual(
392     const std::shared_ptr<RSRenderPropertyBase>& value, float zeroThreshold) const;
393 template<>
394 RSB_EXPORT bool RSRenderAnimatableProperty<Vector2f>::IsNearEqual(
395     const std::shared_ptr<RSRenderPropertyBase>& value, float zeroThreshold) const;
396 template<>
397 RSB_EXPORT bool RSRenderAnimatableProperty<Matrix3f>::IsNearEqual(
398     const std::shared_ptr<RSRenderPropertyBase>& value, float zeroThreshold) const;
399 template<>
400 RSB_EXPORT bool RSRenderAnimatableProperty<Color>::IsNearEqual(
401     const std::shared_ptr<RSRenderPropertyBase>& value, float zeroThreshold) const;
402 template<>
403 RSB_EXPORT bool RSRenderAnimatableProperty<std::shared_ptr<RSFilter>>::IsNearEqual(
404     const std::shared_ptr<RSRenderPropertyBase>& value, float zeroThreshold) const;
405 template<>
406 RSB_EXPORT bool RSRenderAnimatableProperty<Vector4<Color>>::IsNearEqual(
407     const std::shared_ptr<RSRenderPropertyBase>& value, float zeroThreshold) const;
408 template<>
409 RSB_EXPORT bool RSRenderAnimatableProperty<RRect>::IsNearEqual(
410     const std::shared_ptr<RSRenderPropertyBase>& value, float zeroThreshold) const;
411 template<>
412 RSB_EXPORT bool RSRenderAnimatableProperty<std::shared_ptr<RSFilter>>::IsEqual(
413     const std::shared_ptr<const RSRenderPropertyBase>& value) const;
414 
415 #if defined(_WIN32)
416 extern template class RSRenderProperty<float>;
417 extern template class RSRenderProperty<Vector4f>;
418 extern template class RSRenderProperty<Quaternion>;
419 extern template class RSRenderProperty<Vector2f>;
420 extern template class RSRenderProperty<Matrix3f>;
421 extern template class RSRenderProperty<Color>;
422 extern template class RSRenderProperty<std::shared_ptr<RSFilter>>;
423 extern template class RSRenderProperty<Vector4<Color>>;
424 extern template class RSRenderProperty<RRect>;
425 extern template class RSRenderProperty<Drawing::DrawCmdListPtr>;
426 extern template class RSRenderProperty<ForegroundColorStrategyType>;
427 extern template class RSRenderProperty<SkMatrix>;
428 
429 extern template class RSRenderAnimatableProperty<float>;
430 extern template class RSRenderAnimatableProperty<Vector4f>;
431 extern template class RSRenderAnimatableProperty<Quaternion>;
432 extern template class RSRenderAnimatableProperty<Vector2f>;
433 extern template class RSRenderAnimatableProperty<RRect>;
434 extern template class RSRenderAnimatableProperty<Matrix3f>;
435 extern template class RSRenderAnimatableProperty<Color>;
436 extern template class RSRenderAnimatableProperty<std::shared_ptr<RSFilter>>;
437 extern template class RSRenderAnimatableProperty<Vector4<Color>>;
438 #endif
439 } // namespace Rosen
440 } // namespace OHOS
441 
442 #endif // RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_RENDER_PROP_H
443