• 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_PROP_H
17 #define RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_PROP_H
18 
19 #include <type_traits>
20 #include <unistd.h>
21 
22 #include "animation/rs_animation_manager_map.h"
23 #include "animation/rs_implicit_animator.h"
24 #include "animation/rs_implicit_animator_map.h"
25 #include "animation/rs_motion_path_option.h"
26 #include "animation/rs_ui_animation_manager.h"
27 #include "common/rs_color.h"
28 #include "common/rs_common_def.h"
29 #include "common/rs_macros.h"
30 #include "common/rs_vector2.h"
31 #include "common/rs_vector4.h"
32 #include "modifier/rs_animatable_arithmetic.h"
33 #include "modifier/rs_modifier_manager.h"
34 #include "modifier/rs_modifier_type.h"
35 #include "modifier/rs_render_property.h"
36 #include "pipeline/rs_node_map.h"
37 #include "property/rs_properties_def.h"
38 #include "render/rs_border.h"
39 #include "render/rs_filter.h"
40 #include "render/rs_image.h"
41 #include "render/rs_mask.h"
42 #include "render/rs_path.h"
43 #include "render/rs_shader.h"
44 #include "transaction/rs_transaction_proxy.h"
45 #include "ui/rs_node.h"
46 
47 #ifdef _WIN32
48 #include <windows.h>
49 #define gettid GetCurrentThreadId
50 #endif
51 
52 #ifdef __APPLE__
53 #define gettid getpid
54 #endif
55 
56 #ifdef __gnu_linux__
57 #include <sys/types.h>
58 #include <sys/syscall.h>
59 #define gettid []() -> int32_t { return static_cast<int32_t>(syscall(SYS_gettid)); }
60 #endif
61 
62 namespace OHOS {
63 namespace Rosen {
64 class RSModifier;
65 class RSNode;
66 
67 template<class...>
68 struct make_void { using type = void; };
69 template<class... T>
70 using void_t = typename make_void<T...>::type;
71 
72 template<class T, class = void>
73 struct supports_arithmetic : std::false_type {};
74 template<class T>
75 struct supports_arithmetic<T,
76     void_t<decltype(std::declval<T>() == std::declval<T>())>>
77         : std::true_type {};
78 
79 template<class T, class = void>
80 struct supports_animatable_arithmetic : std::false_type {};
81 template<class T>
82 struct supports_animatable_arithmetic<T,
83     void_t<decltype(std::declval<T>() + std::declval<T>()),
84         decltype(std::declval<T>() - std::declval<T>()),
85         decltype(std::declval<T>() * std::declval<float>()),
86         decltype(std::declval<T>() == std::declval<T>())>>
87         : std::true_type {};
88 
89 class RSC_EXPORT RSPropertyBase : public std::enable_shared_from_this<RSPropertyBase> {
90 public:
91     RSPropertyBase();
92     virtual ~RSPropertyBase() = default;
93 
94     PropertyId GetId() const
95     {
96         return id_;
97     }
98 
99     virtual std::shared_ptr<RSRenderPropertyBase> CreateRenderProperty()
100     {
101         return std::make_shared<RSRenderPropertyBase>(id_);
102     }
103 
104 protected:
105     virtual void SetIsCustom(bool isCustom) {}
106 
107     virtual bool GetIsCustom() const
108     {
109         return false;
110     }
111 
112     virtual void SetValue(const std::shared_ptr<RSPropertyBase>& value) {}
113 
114     virtual std::shared_ptr<RSPropertyBase> Clone() const
115     {
116         return std::make_shared<RSPropertyBase>();
117     }
118 
119     virtual void SetMotionPathOption(const std::shared_ptr<RSMotionPathOption>& motionPathOption) {}
120 
121     virtual RSRenderPropertyType GetPropertyType() const
122     {
123         return RSRenderPropertyType::INVALID;
124     }
125 
126     virtual void UpdateExtendedProperty() const {}
127 
128     virtual void UpdateOnAllAnimationFinish() {}
129 
130     virtual void AddPathAnimation() {}
131 
132     virtual void RemovePathAnimation() {}
133 
134     virtual void UpdateShowingValue(const std::shared_ptr<const RSRenderPropertyBase>& property) {}
135 
136     virtual void AttachModifier(const std::shared_ptr<RSModifier>& modifier) {}
137 
138     virtual void MarkModifierDirty(const std::shared_ptr<RSModifierManager>& modifierManager) {}
139 
140     PropertyId id_;
141     RSModifierType type_ { RSModifierType::INVALID };
142     std::weak_ptr<RSNode> target_;
143 
144 private:
145     virtual std::shared_ptr<RSPropertyBase> Add(const std::shared_ptr<const RSPropertyBase>& value)
146     {
147         return shared_from_this();
148     }
149 
150     virtual std::shared_ptr<RSPropertyBase> Minus(const std::shared_ptr<const RSPropertyBase>& value)
151     {
152         return shared_from_this();
153     }
154 
155     virtual std::shared_ptr<RSPropertyBase> Multiply(const float scale)
156     {
157         return shared_from_this();
158     }
159 
160     virtual bool IsEqual(const std::shared_ptr<const RSPropertyBase>& value) const
161     {
162         return true;
163     }
164 
165     friend std::shared_ptr<RSPropertyBase> operator+=(
166         const std::shared_ptr<RSPropertyBase>& a, const std::shared_ptr<const RSPropertyBase>& b);
167     friend std::shared_ptr<RSPropertyBase> operator-=(
168         const std::shared_ptr<RSPropertyBase>& a, const std::shared_ptr<const RSPropertyBase>& b);
169     friend std::shared_ptr<RSPropertyBase> operator*=(const std::shared_ptr<RSPropertyBase>& value, const float scale);
170     friend std::shared_ptr<RSPropertyBase> operator+(
171         const std::shared_ptr<const RSPropertyBase>& a, const std::shared_ptr<const RSPropertyBase>& b);
172     friend std::shared_ptr<RSPropertyBase> operator-(
173         const std::shared_ptr<const RSPropertyBase>& a, const std::shared_ptr<const RSPropertyBase>& b);
174     friend std::shared_ptr<RSPropertyBase> operator*(
175         const std::shared_ptr<const RSPropertyBase>& value, const float scale);
176     friend bool operator==(
177         const std::shared_ptr<const RSPropertyBase>& a, const std::shared_ptr<const RSPropertyBase>& b);
178     friend bool operator!=(
179         const std::shared_ptr<const RSPropertyBase>& a, const std::shared_ptr<const RSPropertyBase>& b);
180     friend class RSCurveAnimation;
181     friend class RSExtendedModifier;
182     friend class RSImplicitAnimator;
183     friend class RSImplicitCurveAnimationParam;
184     friend class RSImplicitKeyframeAnimationParam;
185     friend class RSImplicitSpringAnimationParam;
186     friend class RSModifier;
187     friend class RSPropertyAnimation;
188     friend class RSPathAnimation;
189     friend class RSKeyframeAnimation;
190     friend class RSSpringAnimation;
191     friend class RSUIAnimationManager;
192 };
193 
194 template<typename T>
195 class RSProperty : public RSPropertyBase {
196     static_assert(std::is_base_of_v<RSArithmetic<T>, T> || supports_arithmetic<T>::value);
197 
198 public:
199     RSProperty() : RSPropertyBase() {}
200     explicit RSProperty(const T& value) : RSPropertyBase()
201     {
202         stagingValue_ = value;
203     }
204     virtual ~RSProperty() = default;
205 
206     virtual void Set(const T& value)
207     {
208         if (ROSEN_EQ(value, stagingValue_) || !IsValid(value)) {
209             return;
210         }
211 
212         stagingValue_ = value;
213         if (target_.lock() == nullptr) {
214             return;
215         }
216 
217         if (isCustom_) {
218             UpdateExtendedProperty();
219         } else {
220             UpdateToRender(stagingValue_, false);
221         }
222     }
223 
224     virtual T Get() const
225     {
226         return stagingValue_;
227     }
228 
229     std::shared_ptr<RSRenderPropertyBase> CreateRenderProperty() override
230     {
231         return std::make_shared<RSRenderProperty<T>>(stagingValue_, id_);
232     }
233 
234 protected:
235     void UpdateToRender(const T& value, bool isDelta, bool forceUpdate = false) const
236     {}
237 
238     void UpdateExtendedProperty() const override
239     {
240         auto node = target_.lock();
241         if (node == nullptr) {
242             return;
243         }
244         node->UpdateExtendedModifier(modifier_);
245     }
246 
247     void SetValue(const std::shared_ptr<RSPropertyBase>& value) override
248     {
249         auto property = std::static_pointer_cast<RSProperty<T>>(value);
250         if (property != nullptr) {
251             stagingValue_ = property->stagingValue_;
252         }
253     }
254 
255     std::shared_ptr<RSPropertyBase> Clone() const override
256     {
257         return std::make_shared<RSProperty<T>>(stagingValue_);
258     }
259 
260     bool IsValid(const T& value)
261     {
262         return true;
263     }
264 
265     void AttachModifier(const std::shared_ptr<RSModifier>& modifier) override
266     {
267         modifier_ = modifier;
268     }
269 
270     void SetIsCustom(bool isCustom) override
271     {
272         isCustom_ = isCustom;
273     }
274 
275     bool GetIsCustom() const override
276     {
277         return isCustom_;
278     }
279 
280     T stagingValue_ {};
281     bool isCustom_ { false };
282     std::weak_ptr<RSModifier> modifier_;
283 
284     friend class RSPathAnimation;
285     friend class RSImplicitAnimator;
286     friend class RSExtendedModifier;
287     friend class RSModifier;
288 };
289 
290 template<typename T>
291 class RSAnimatableProperty : public RSProperty<T> {
292     static_assert(std::is_integral_v<T> || std::is_floating_point_v<T> || std::is_same_v<Color, T> ||
293                   std::is_same_v<Matrix3f, T> || std::is_same_v<Vector2f, T> || std::is_same_v<Vector4f, T> ||
294                   std::is_same_v<Quaternion, T> || std::is_same_v<std::shared_ptr<RSFilter>, T> ||
295                   std::is_same_v<Vector4<Color>, T> || std::is_base_of_v<RSAnimatableArithmetic<T>, T> ||
296                   supports_animatable_arithmetic<T>::value);
297 
298 public:
299     RSAnimatableProperty() {}
300     RSAnimatableProperty(const T& value) : RSProperty<T>(value)
301     {
302         showingValue_ = value;
303     }
304 
305     virtual ~RSAnimatableProperty() = default;
306 
307     void Set(const T& value) override
308     {
309         if (ROSEN_EQ(value, RSProperty<T>::stagingValue_) || !RSProperty<T>::IsValid(value)) {
310             return;
311         }
312 
313         auto node = RSProperty<T>::target_.lock();
314         if (node == nullptr) {
315             RSProperty<T>::stagingValue_ = value;
316             return;
317         }
318 
319         auto implicitAnimator = RSImplicitAnimatorMap::Instance().GetAnimator(gettid());
320         if (implicitAnimator && implicitAnimator->NeedImplicitAnimation()) {
321             auto startValue = std::make_shared<RSAnimatableProperty<T>>(RSProperty<T>::stagingValue_);
322             auto endValue = std::make_shared<RSAnimatableProperty<T>>(value);
323             if (motionPathOption_ != nullptr) {
324                 implicitAnimator->BeginImplicitPathAnimation(motionPathOption_);
325                 implicitAnimator->CreateImplicitAnimation(
326                     node, RSProperty<T>::shared_from_this(), startValue, endValue);
327                 implicitAnimator->EndImplicitPathAnimation();
328             } else {
329                 implicitAnimator->CreateImplicitAnimation(
330                     node, RSProperty<T>::shared_from_this(), startValue, endValue);
331             }
332             return;
333         }
334 
335         if (runningPathNum_ > 0) {
336             return;
337         }
338 
339         bool hasPropertyAnimation = node->HasPropertyAnimation(RSProperty<T>::id_);
340         T sendValue = value;
341         if (hasPropertyAnimation) {
342             sendValue = value - RSProperty<T>::stagingValue_;
343         }
344         RSProperty<T>::stagingValue_ = value;
345         if (RSProperty<T>::isCustom_) {
346             UpdateExtendedAnimatableProperty(sendValue, hasPropertyAnimation);
347         } else {
348             RSProperty<T>::UpdateToRender(sendValue, hasPropertyAnimation);
349         }
350     }
351 
352     T Get() const override
353     {
354         if (RSProperty<T>::isCustom_) {
355             return showingValue_;
356         }
357         return RSProperty<T>::stagingValue_;
358     }
359 
360     std::shared_ptr<RSRenderPropertyBase> CreateRenderProperty() override
361     {
362         return std::make_shared<RSRenderAnimatableProperty<T>>(
363             RSProperty<T>::stagingValue_, RSProperty<T>::id_, GetPropertyType());
364     }
365 
366 protected:
367     void MarkModifierDirty(const std::shared_ptr<RSModifierManager>& modifierManager) override
368     {
369         auto modifier = RSProperty<T>::modifier_.lock();
370         if (modifier != nullptr && modifierManager != nullptr) {
371             modifierManager->AddModifier(modifier);
372         }
373     }
374 
375     void UpdateOnAllAnimationFinish() override
376     {
377         if (RSProperty<T>::isCustom_) {
378             return;
379         }
380         RSProperty<T>::UpdateToRender(RSProperty<T>::stagingValue_, false, true);
381     }
382 
383     void UpdateExtendedAnimatableProperty(const T& value, bool isDelta)
384     {
385         auto renderProperty = std::static_pointer_cast<RSRenderAnimatableProperty<T>>(GetExtendedRenderProperty());
386         if (isDelta) {
387             if (renderProperty != nullptr) {
388                 renderProperty->Set(renderProperty->Get() + value);
389             }
390         } else {
391             showingValue_ = value;
392             RSProperty<T>::UpdateExtendedProperty();
393             if (renderProperty != nullptr) {
394                 renderProperty->Set(value);
395             }
396         }
397     }
398 
399     std::shared_ptr<RSRenderPropertyBase> GetExtendedRenderProperty()
400     {
401         auto animationManager = RSAnimationManagerMap::Instance()->GetAnimationManager(gettid());
402         if (animationManager == nullptr) {
403             return nullptr;
404         }
405 
406         return animationManager->GetRenderProperty(RSProperty<T>::GetId());
407     }
408 
409     void AddPathAnimation() override
410     {
411         runningPathNum_ += 1;
412     }
413 
414     void RemovePathAnimation() override
415     {
416         runningPathNum_ -= 1;
417     }
418 
419     void UpdateShowingValue(const std::shared_ptr<const RSRenderPropertyBase>& property) override
420     {
421         auto renderProperty = std::static_pointer_cast<const RSRenderProperty<T>>(property);
422         if (renderProperty != nullptr) {
423             showingValue_ = renderProperty->Get();
424         }
425     }
426 
427     void SetValue(const std::shared_ptr<RSPropertyBase>& value) override
428     {
429         auto property = std::static_pointer_cast<RSAnimatableProperty<T>>(value);
430         if (property != nullptr && property->GetPropertyType() == GetPropertyType()) {
431             RSProperty<T>::stagingValue_ = property->stagingValue_;
432         }
433     }
434 
435     std::shared_ptr<RSPropertyBase> Clone() const override
436     {
437         return std::make_shared<RSAnimatableProperty<T>>(RSProperty<T>::stagingValue_);
438     }
439 
440     void SetMotionPathOption(const std::shared_ptr<RSMotionPathOption>& motionPathOption) override
441     {
442         motionPathOption_ = motionPathOption;
443     }
444 
445     T showingValue_ {};
446     int runningPathNum_ { 0 };
447     std::shared_ptr<RSMotionPathOption> motionPathOption_ {};
448 
449 private:
450     RSRenderPropertyType GetPropertyType() const override
451     {
452         return RSRenderPropertyType::INVALID;
453     }
454 
455     std::shared_ptr<RSPropertyBase> Add(const std::shared_ptr<const RSPropertyBase>& value) override
456     {
457         auto animatableProperty = std::static_pointer_cast<const RSAnimatableProperty<T>>(value);
458         if (animatableProperty != nullptr) {
459             RSProperty<T>::stagingValue_ = RSProperty<T>::stagingValue_ + animatableProperty->stagingValue_;
460         }
461         return RSProperty<T>::shared_from_this();
462     }
463 
464     std::shared_ptr<RSPropertyBase> Minus(const std::shared_ptr<const RSPropertyBase>& value) override
465     {
466         auto animatableProperty = std::static_pointer_cast<const RSAnimatableProperty<T>>(value);
467         if (animatableProperty != nullptr) {
468             RSProperty<T>::stagingValue_ = RSProperty<T>::stagingValue_ - animatableProperty->stagingValue_;
469         }
470         return RSProperty<T>::shared_from_this();
471     }
472 
473     std::shared_ptr<RSPropertyBase> Multiply(const float scale) override
474     {
475         RSProperty<T>::stagingValue_ = RSProperty<T>::stagingValue_ * scale;
476         return RSProperty<T>::shared_from_this();
477     }
478 
479     bool IsEqual(const std::shared_ptr<const RSPropertyBase>& value) const override
480     {
481         auto animatableProperty = std::static_pointer_cast<const RSAnimatableProperty<T>>(value);
482         if (animatableProperty != nullptr) {
483             return RSProperty<T>::stagingValue_ == animatableProperty->stagingValue_;
484         }
485         return true;
486     }
487 
488     friend class RSPropertyAnimation;
489     friend class RSPathAnimation;
490     friend class RSUIAnimationManager;
491     friend class RSExtendedModifier;
492     friend class RSModifier;
493 };
494 
495 template<>
496 RSC_EXPORT void RSProperty<bool>::UpdateToRender(const bool& value, bool isDelta, bool forceUpdate) const;
497 template<>
498 RSC_EXPORT void RSProperty<float>::UpdateToRender(const float& value, bool isDelta, bool forceUpdate) const;
499 template<>
500 RSC_EXPORT void RSProperty<int>::UpdateToRender(const int& value, bool isDelta, bool forceUpdate) const;
501 template<>
502 RSC_EXPORT void RSProperty<Color>::UpdateToRender(const Color& value, bool isDelta, bool forceUpdate) const;
503 template<>
504 RSC_EXPORT void RSProperty<Gravity>::UpdateToRender(const Gravity& value, bool isDelta, bool forceUpdate) const;
505 template<>
506 RSC_EXPORT void RSProperty<Matrix3f>::UpdateToRender(const Matrix3f& value, bool isDelta, bool forceUpdate) const;
507 template<>
508 RSC_EXPORT void RSProperty<Quaternion>::UpdateToRender(const Quaternion& value, bool isDelta, bool forceUpdate) const;
509 template<>
510 RSC_EXPORT void RSProperty<std::shared_ptr<RSFilter>>::UpdateToRender(
511     const std::shared_ptr<RSFilter>& value, bool isDelta, bool forceUpdate) const;
512 template<>
513 RSC_EXPORT void RSProperty<std::shared_ptr<RSImage>>::UpdateToRender(
514     const std::shared_ptr<RSImage>& value, bool isDelta, bool forceUpdate) const;
515 template<>
516 RSC_EXPORT void RSProperty<std::shared_ptr<RSMask>>::UpdateToRender(
517     const std::shared_ptr<RSMask>& value, bool isDelta, bool forceUpdate) const;
518 template<>
519 RSC_EXPORT void RSProperty<std::shared_ptr<RSPath>>::UpdateToRender(
520     const std::shared_ptr<RSPath>& value, bool isDelta, bool forceUpdate) const;
521 template<>
522 RSC_EXPORT void RSProperty<std::shared_ptr<RSShader>>::UpdateToRender(
523     const std::shared_ptr<RSShader>& value, bool isDelta, bool forceUpdate) const;
524 template<>
525 RSC_EXPORT void RSProperty<Vector2f>::UpdateToRender(const Vector2f& value, bool isDelta, bool forceUpdate) const;
526 template<>
527 RSC_EXPORT void RSProperty<Vector4<uint32_t>>::UpdateToRender(
528     const Vector4<uint32_t>& value, bool isDelta, bool forceUpdate) const;
529 template<>
530 RSC_EXPORT void RSProperty<Vector4<Color>>::UpdateToRender(
531     const Vector4<Color>& value, bool isDelta, bool forceUpdate) const;
532 template<>
533 RSC_EXPORT void RSProperty<Vector4f>::UpdateToRender(const Vector4f& value, bool isDelta, bool forceUpdate) const;
534 
535 template<>
536 RSC_EXPORT bool RSProperty<float>::IsValid(const float& value);
537 template<>
538 RSC_EXPORT bool RSProperty<Vector2f>::IsValid(const Vector2f& value);
539 template<>
540 RSC_EXPORT bool RSProperty<Vector4f>::IsValid(const Vector4f& value);
541 
542 template<>
543 RSC_EXPORT RSRenderPropertyType RSAnimatableProperty<float>::GetPropertyType() const;
544 template<>
545 RSC_EXPORT RSRenderPropertyType RSAnimatableProperty<Color>::GetPropertyType() const;
546 template<>
547 RSC_EXPORT RSRenderPropertyType RSAnimatableProperty<Matrix3f>::GetPropertyType() const;
548 template<>
549 RSC_EXPORT RSRenderPropertyType RSAnimatableProperty<Vector2f>::GetPropertyType() const;
550 template<>
551 RSC_EXPORT RSRenderPropertyType RSAnimatableProperty<Vector4f>::GetPropertyType() const;
552 template<>
553 RSC_EXPORT RSRenderPropertyType RSAnimatableProperty<Quaternion>::GetPropertyType() const;
554 template<>
555 RSC_EXPORT RSRenderPropertyType RSAnimatableProperty<std::shared_ptr<RSFilter>>::GetPropertyType() const;
556 template<>
557 RSC_EXPORT RSRenderPropertyType RSAnimatableProperty<Vector4<Color>>::GetPropertyType() const;
558 } // namespace Rosen
559 } // namespace OHOS
560 
561 #endif // RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_PROP_H
562