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