• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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_IMPLICIT_ANIMATOR_H
17 #define RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_IMPLICIT_ANIMATOR_H
18 
19 #include <stack>
20 #include <utility>
21 #include <vector>
22 
23 #include "animation/rs_animation_timing_curve.h"
24 #include "animation/rs_animation_timing_protocol.h"
25 #include "common/rs_macros.h"
26 
27 namespace OHOS {
28 namespace Rosen {
29 class AnimationFinishCallback;
30 class AnimationRepeatCallback;
31 class RSAnimation;
32 class RSPropertyBase;
33 class RSImplicitAnimationParam;
34 class RSTransitionEffect;
35 class RSMotionPathOption;
36 class RSNode;
37 class RSUIContext;
38 enum class CancelAnimationStatus;
39 
40 class RSC_EXPORT RSImplicitAnimator {
41 public:
42     RSImplicitAnimator() = default;
43     virtual ~RSImplicitAnimator() = default;
44 
45     // open implicit animation with given animation options, finish callback and repeatcallback
46     int OpenImplicitAnimation(const RSAnimationTimingProtocol& timingProtocol,
47         const RSAnimationTimingCurve& timingCurve, std::shared_ptr<AnimationFinishCallback>&& finishCallback,
48         std::shared_ptr<AnimationRepeatCallback>&& repeatCallback);
49     // open implicit animation with given animation options and finish callback
50     int OpenImplicitAnimation(const RSAnimationTimingProtocol& timingProtocol,
51         const RSAnimationTimingCurve& timingCurve, std::shared_ptr<AnimationFinishCallback>&& finishCallback);
52     // open implicit animation with current options and given finish callback
53     int OpenImplicitAnimation(std::shared_ptr<AnimationFinishCallback>&& finishCallback);
54     // open implicit animation with current callback and given timing protocol & curve
55     int OpenImplicitAnimation(
56         const RSAnimationTimingProtocol& timingProtocol, const RSAnimationTimingCurve& timingCurve);
57 
58     // close implicit animation and return all animations
59     std::vector<std::shared_ptr<RSAnimation>> CloseImplicitAnimation();
60     // close implicit cancel animation and return whether the synchronization was successful
61     CancelAnimationStatus CloseImplicitCancelAnimation();
62 
63     // open implicit animation with given animation options and finish callback
64     int OpenInterActiveImplicitAnimation(bool isAddImplictAnimation, const RSAnimationTimingProtocol& timingProtocol,
65         const RSAnimationTimingCurve& timingCurve, std::shared_ptr<AnimationFinishCallback>&& finishCallback);
66     // interactive animator close implicit animation and return all animations
67     std::vector<std::pair<std::shared_ptr<RSAnimation>, NodeId>> CloseInterActiveImplicitAnimation(
68         bool isAddImplictAnimation);
69 
70     void BeginImplicitKeyFrameAnimation(float fraction, const RSAnimationTimingCurve& timingCurve);
71     void BeginImplicitKeyFrameAnimation(float fraction);
72     void EndImplicitKeyFrameAnimation();
73 
74     void BeginImplicitDurationKeyFrameAnimation(int duration, const RSAnimationTimingCurve& timingCurve);
75     void EndImplicitDurationKeyFrameAnimation();
76 
77     void BeginImplicitTransition(const std::shared_ptr<const RSTransitionEffect>& effect, bool isTransitionIn);
78     void EndImplicitTransition();
79 
80     void BeginImplicitPathAnimation(const std::shared_ptr<RSMotionPathOption>& motionPathOption);
81     void EndImplicitPathAnimation();
82 
83     bool NeedImplicitAnimation();
84 
85     void CreateImplicitAnimation(const std::shared_ptr<RSNode>& target, std::shared_ptr<RSPropertyBase> property,
86         const std::shared_ptr<RSPropertyBase>& startValue, const std::shared_ptr<RSPropertyBase>& endValue);
87 
88     void CreateImplicitAnimationWithInitialVelocity(const std::shared_ptr<RSNode>& target,
89         const std::shared_ptr<RSPropertyBase>& property, const std::shared_ptr<RSPropertyBase>& startValue,
90         const std::shared_ptr<RSPropertyBase>& endValue, const std::shared_ptr<RSPropertyBase>& velocity);
91 
92     void CreateImplicitTransition(RSNode& target);
93     void CancelImplicitAnimation(
94         const std::shared_ptr<RSNode>& target, const std::shared_ptr<RSPropertyBase>& property);
95 
96 private:
97     void EndImplicitAnimation();
98     void BeginImplicitCurveAnimation();
99     void BeginImplicitSpringAnimation();
100     void BeginImplicitInterpolatingSpringAnimation();
101     void BeginImplicitCancelAnimation();
102 
103     void CloseImplicitAnimationInner();
104     void ProcessEmptyAnimations(const std::shared_ptr<AnimationFinishCallback>& finishCallback);
105     void ProcessAnimationFinishCallbackGuaranteeTask();
106 
107     void PushImplicitParam(const std::shared_ptr<RSImplicitAnimationParam>& implicitParam);
108     void PopImplicitParam();
109 
110     void SetPropertyValue(std::shared_ptr<RSPropertyBase> property, const std::shared_ptr<RSPropertyBase>& value);
111 
112     void ExecuteWithoutAnimation(const std::function<void()>& callback);
113 
SetRSUIContext(std::weak_ptr<RSUIContext> rsUIContext)114     void SetRSUIContext(std::weak_ptr<RSUIContext> rsUIContext)
115     {
116         rsUIContext_ = rsUIContext;
117     }
118 
119     std::stack<std::tuple<RSAnimationTimingProtocol, RSAnimationTimingCurve,
120         const std::shared_ptr<AnimationFinishCallback>, std::shared_ptr<AnimationRepeatCallback>>>
121         globalImplicitParams_;
122     std::stack<std::shared_ptr<RSImplicitAnimationParam>> implicitAnimationParams_;
123     std::stack<std::vector<std::pair<std::shared_ptr<RSAnimation>, NodeId>>> implicitAnimations_;
124     std::stack<std::map<std::pair<NodeId, PropertyId>, std::shared_ptr<RSAnimation>>> keyframeAnimations_;
125     std::stack<std::tuple<bool, int, int>> durationKeyframeParams_;
126 
127     bool implicitAnimationDisabled_ { false };
128 
129     std::stack<std::vector<std::pair<std::shared_ptr<RSAnimation>, NodeId>>> interactiveImplicitAnimations_;
130     bool isAddInteractiveAnimator_ { false };
131     std::weak_ptr<RSUIContext> rsUIContext_;
132     friend class RSImplicitAnimatorMap;
133     friend class RSNode;
134     friend class RSUIContext;
135 };
136 } // namespace Rosen
137 } // namespace OHOS
138 
139 #endif // RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_IMPLICIT_ANIMATOR_H
140