• 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 ROSEN_RENDER_SERVICE_BASE_COMMAND_RS_ANIMATION_COMMAND_H
17 #define ROSEN_RENDER_SERVICE_BASE_COMMAND_RS_ANIMATION_COMMAND_H
18 
19 #include "animation/rs_render_animation.h"
20 #include "animation/rs_render_curve_animation.h"
21 #include "animation/rs_render_interpolating_spring_animation.h"
22 #include "animation/rs_render_keyframe_animation.h"
23 #include "animation/rs_render_path_animation.h"
24 #include "animation/rs_render_spring_animation.h"
25 #include "animation/rs_render_transition.h"
26 #include "animation/rs_render_particle_animation.h"
27 #include "command/rs_command_templates.h"
28 #include "common/rs_macros.h"
29 #include "pipeline/rs_render_node.h"
30 
31 namespace OHOS {
32 namespace Rosen {
33 enum RSAnimationCommandType : uint16_t {
34     // curve animation
35     ANIMATION_CREATE_CURVE,
36     // particle animation
37     ANIMATION_CREATE_PARTICLE,
38     // keyframe animation
39     ANIMATION_CREATE_KEYFRAME,
40     // path animation
41     ANIMATION_CREATE_PATH,
42     // transition animation
43     ANIMATION_CREATE_TRANSITION,
44     // spring animation
45     ANIMATION_CREATE_SPRING,
46     // interpolating spring animation
47     ANIMATION_CREATE_INTERPOLATING_SPRING,
48 
49     // operations
50     ANIMATION_START,
51     ANIMATION_PAUSE,
52     ANIMATION_RESUME,
53     ANIMATION_FINISH,
54     ANIMATION_REVERSE,
55     ANIMATION_SET_FRACTION,
56     ANIMATION_CANCEL,
57 
58     // UI operation
59     ANIMATION_CALLBACK,
60 
61     // interactive animator operation
62     INTERACTIVE_ANIMATOR_CREATE,
63     INTERACTIVE_ANIMATOR_DESTORY,
64     INTERACTIVE_ANIMATOR_PAUSE,
65     INTERACTIVE_ANIMATOR_CONTINUE,
66     INTERACTIVE_ANIMATOR_FINISH,
67     INTERACTIVE_ANIMATOR_REVERSE,
68     INTERACTIVE_ANIMATOR_SET_FRACTION,
69 };
70 
71 enum AnimationCallbackEvent : uint16_t { REPEAT_FINISHED, FINISHED, LOGICALLY_FINISHED };
72 
73 class RSB_EXPORT AnimationCommandHelper {
74 public:
75     template<void (RSRenderAnimation::*OP)()>
AnimOp(RSContext & context,NodeId nodeId,AnimationId animId)76     static void AnimOp(RSContext& context, NodeId nodeId, AnimationId animId)
77     {
78         [[maybe_unused]] auto [node, animation] = GetNodeAndAnimation(context, nodeId, animId, __PRETTY_FUNCTION__);
79         if (animation) {
80             (*animation.*OP)();
81         }
82     }
83     template<void (RSRenderAnimation::*OP)()>
AnimOpReg(RSContext & context,NodeId nodeId,AnimationId animId)84     static void AnimOpReg(RSContext& context, NodeId nodeId, AnimationId animId)
85     {
86         auto [node, animation] = GetNodeAndAnimation(context, nodeId, animId, __PRETTY_FUNCTION__);
87         if (node && animation) {
88             (*animation.*OP)();
89             // register node on animation start or resume
90             context.RegisterAnimatingRenderNode(node);
91         }
92     }
93     template<typename T, void (RSRenderAnimation::*OP)(T)>
AnimOp(RSContext & context,NodeId nodeId,AnimationId animId,T param)94     static void AnimOp(RSContext& context, NodeId nodeId, AnimationId animId, T param)
95     {
96         [[maybe_unused]] auto [node, animation] = GetNodeAndAnimation(context, nodeId, animId, __PRETTY_FUNCTION__);
97         if (animation) {
98             (*animation.*OP)(param);
99         }
100     }
101     static void CreateAnimation(
102         RSContext& context, NodeId targetId, const std::shared_ptr<RSRenderAnimation>& animation);
103     static void CreateParticleAnimation(RSContext& context, NodeId targetId,
104         const std::shared_ptr<RSRenderParticleAnimation>& animation);
105 
106     using AnimationCallbackProcessor = void (*)(NodeId, AnimationId, uint64_t, AnimationCallbackEvent);
107     static void AnimationCallback(RSContext& context,
108                                   NodeId targetId, AnimationId animId, uint64_t token,
109                                   AnimationCallbackEvent event);
110     static RSB_EXPORT void SetAnimationCallbackProcessor(AnimationCallbackProcessor processor);
111     static void CancelAnimation(RSContext& context, NodeId targetId, PropertyId propertyId);
112 
113     static void CreateInteractiveAnimator(RSContext& context,
114         InteractiveImplictAnimatorId targetId, std::vector<std::pair<NodeId, AnimationId>> animations,
115         bool startImmediately);
116     static void DestoryInteractiveAnimator(RSContext& context, InteractiveImplictAnimatorId targetId);
117     static void InteractiveAnimatorAddAnimations(RSContext& context,
118         InteractiveImplictAnimatorId targetId, std::vector<std::pair<NodeId, AnimationId>> animations);
119     static void PauseInteractiveAnimator(RSContext& context, InteractiveImplictAnimatorId targetId);
120     static void ContinueInteractiveAnimator(RSContext& context, InteractiveImplictAnimatorId targetId);
121     static void FinishInteractiveAnimator(RSContext& context,
122         InteractiveImplictAnimatorId targetId, RSInteractiveAnimationPosition finishPos);
123     static void ReverseInteractiveAnimator(RSContext& context, InteractiveImplictAnimatorId targetId);
124     static void SetFractionInteractiveAnimator(RSContext& context,
125         InteractiveImplictAnimatorId targetId, float fraction);
126 private:
127     using NodeAndAnimationPair =
128     std::pair<const std::shared_ptr<RSRenderNode>, const std::shared_ptr<RSRenderAnimation>>;
129     static NodeAndAnimationPair GetNodeAndAnimation(
130     RSContext& context, NodeId& nodeId, AnimationId& animId, const char* funcName);
131 };
132 
133 // animation operation
134 ADD_COMMAND(RSAnimationStart,
135     ARG(PERMISSION_APP, ANIMATION, ANIMATION_START,
136         AnimationCommandHelper::AnimOpReg<&RSRenderAnimation::Start>, NodeId, AnimationId))
137 ADD_COMMAND(RSAnimationPause,
138     ARG(PERMISSION_APP, ANIMATION, ANIMATION_PAUSE,
139         AnimationCommandHelper::AnimOp<&RSRenderAnimation::Pause>, NodeId, AnimationId))
140 ADD_COMMAND(RSAnimationResume,
141     ARG(PERMISSION_APP, ANIMATION, ANIMATION_RESUME,
142         AnimationCommandHelper::AnimOpReg<&RSRenderAnimation::Resume>, NodeId, AnimationId))
143 ADD_COMMAND(RSAnimationFinish,
144     ARG(PERMISSION_APP, ANIMATION, ANIMATION_FINISH,
145         AnimationCommandHelper::AnimOpReg<&RSRenderAnimation::Finish>, NodeId, AnimationId))
146 ADD_COMMAND(RSAnimationReverse,
147     ARG(PERMISSION_APP, ANIMATION, ANIMATION_REVERSE,
148         AnimationCommandHelper::AnimOp<bool, &RSRenderAnimation::SetReversed>, NodeId, AnimationId, bool))
149 ADD_COMMAND(RSAnimationSetFraction,
150     ARG(PERMISSION_APP, ANIMATION, ANIMATION_SET_FRACTION,
151         AnimationCommandHelper::AnimOp<float, &RSRenderAnimation::SetFraction>, NodeId, AnimationId, float))
152 ADD_COMMAND(RSAnimationCancel,
153     ARG(PERMISSION_APP, ANIMATION, ANIMATION_CANCEL,
154         AnimationCommandHelper::CancelAnimation, NodeId, PropertyId))
155 
156 ADD_COMMAND(RSAnimationCallback,
157     ARG(PERMISSION_APP, ANIMATION, ANIMATION_CALLBACK,
158         AnimationCommandHelper::AnimationCallback, NodeId, AnimationId, uint64_t, AnimationCallbackEvent))
159 
160 // create curve animation
161 ADD_COMMAND(RSAnimationCreateCurve,
162     ARG(PERMISSION_APP, ANIMATION, ANIMATION_CREATE_CURVE, AnimationCommandHelper::CreateAnimation,
163         NodeId, std::shared_ptr<RSRenderCurveAnimation>))
164 
165 // create particle animation
166 ADD_COMMAND(RSAnimationCreateParticle,
167     ARG(PERMISSION_APP, ANIMATION, ANIMATION_CREATE_PARTICLE, AnimationCommandHelper::CreateParticleAnimation, NodeId,
168         std::shared_ptr<RSRenderParticleAnimation>))
169 
170 // create keyframe animation
171 ADD_COMMAND(RSAnimationCreateKeyframe,
172     ARG(PERMISSION_APP, ANIMATION, ANIMATION_CREATE_KEYFRAME,
173         AnimationCommandHelper::CreateAnimation, NodeId, std::shared_ptr<RSRenderKeyframeAnimation>))
174 
175 // create path animation
176 ADD_COMMAND(RSAnimationCreatePath,
177     ARG(PERMISSION_APP, ANIMATION, ANIMATION_CREATE_PATH,
178         AnimationCommandHelper::CreateAnimation, NodeId, std::shared_ptr<RSRenderPathAnimation>))
179 
180 // create transition animation
181 ADD_COMMAND(RSAnimationCreateTransition,
182     ARG(PERMISSION_APP, ANIMATION, ANIMATION_CREATE_TRANSITION,
183         AnimationCommandHelper::CreateAnimation, NodeId, std::shared_ptr<RSRenderTransition>))
184 
185 // create spring animation
186 ADD_COMMAND(RSAnimationCreateSpring,
187     ARG(PERMISSION_APP, ANIMATION, ANIMATION_CREATE_SPRING,
188         AnimationCommandHelper::CreateAnimation, NodeId, std::shared_ptr<RSRenderSpringAnimation>))
189 
190 // create interpolating spring animation
191 ADD_COMMAND(RSAnimationCreateInterpolatingSpring,
192     ARG(PERMISSION_APP, ANIMATION, ANIMATION_CREATE_INTERPOLATING_SPRING, AnimationCommandHelper::CreateAnimation,
193         NodeId, std::shared_ptr<RSRenderInterpolatingSpringAnimation>))
194 
195 // interactive implict animator operation
196 ADD_COMMAND(RSInteractiveAnimatorCreate,
197     ARG(PERMISSION_APP, ANIMATION, INTERACTIVE_ANIMATOR_CREATE,
198         AnimationCommandHelper::CreateInteractiveAnimator, InteractiveImplictAnimatorId,
199         std::vector<std::pair<NodeId, AnimationId>>, bool))
200 ADD_COMMAND(RSInteractiveAnimatorDestory,
201     ARG(PERMISSION_APP, ANIMATION, INTERACTIVE_ANIMATOR_DESTORY,
202         AnimationCommandHelper::DestoryInteractiveAnimator, InteractiveImplictAnimatorId))
203 ADD_COMMAND(RSInteractiveAnimatorPause,
204     ARG(PERMISSION_APP, ANIMATION, INTERACTIVE_ANIMATOR_PAUSE,
205         AnimationCommandHelper::PauseInteractiveAnimator, InteractiveImplictAnimatorId))
206 ADD_COMMAND(RSInteractiveAnimatorContinue,
207     ARG(PERMISSION_APP, ANIMATION, INTERACTIVE_ANIMATOR_CONTINUE,
208         AnimationCommandHelper::ContinueInteractiveAnimator, InteractiveImplictAnimatorId))
209 ADD_COMMAND(RSInteractiveAnimatorFinish,
210     ARG(PERMISSION_APP, ANIMATION, INTERACTIVE_ANIMATOR_FINISH,
211         AnimationCommandHelper::FinishInteractiveAnimator, InteractiveImplictAnimatorId,
212         RSInteractiveAnimationPosition))
213 ADD_COMMAND(RSInteractiveAnimatorReverse,
214     ARG(PERMISSION_APP, ANIMATION, INTERACTIVE_ANIMATOR_REVERSE,
215         AnimationCommandHelper::ReverseInteractiveAnimator, InteractiveImplictAnimatorId))
216 ADD_COMMAND(RSInteractiveAnimatorSetFraction,
217     ARG(PERMISSION_APP, ANIMATION, INTERACTIVE_ANIMATOR_SET_FRACTION,
218         AnimationCommandHelper::SetFractionInteractiveAnimator, InteractiveImplictAnimatorId, float))
219 } // namespace Rosen
220 } // namespace OHOS
221 
222 #endif // ROSEN_RENDER_SERVICE_BASE_COMMAND_RS_ANIMATION_COMMAND_H
223