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         auto node = context.GetNodeMap().GetRenderNode<RSRenderNode>(nodeId);
79         if (node == nullptr) {
80             return;
81         }
82         auto animation = node->GetAnimationManager().GetAnimation(animId);
83         if (animation == nullptr) {
84             return;
85         }
86         (*animation.*OP)();
87     }
88     template<void (RSRenderAnimation::*OP)()>
AnimOpReg(RSContext & context,NodeId nodeId,AnimationId animId)89     static void AnimOpReg(RSContext& context, NodeId nodeId, AnimationId animId)
90     {
91         auto node = context.GetNodeMap().GetRenderNode<RSRenderNode>(nodeId);
92         if (node == nullptr) {
93             return;
94         }
95         auto animation = node->GetAnimationManager().GetAnimation(animId);
96         if (animation == nullptr) {
97             return;
98         }
99         (*animation.*OP)();
100         // register node on animation start or resume
101         context.RegisterAnimatingRenderNode(node);
102     }
103     template<typename T, void (RSRenderAnimation::*OP)(T)>
AnimOp(RSContext & context,NodeId nodeId,AnimationId animId,T param)104     static void AnimOp(RSContext& context, NodeId nodeId, AnimationId animId, T param)
105     {
106         auto node = context.GetNodeMap().GetRenderNode<RSRenderNode>(nodeId);
107         if (node == nullptr) {
108             return;
109         }
110         auto animation = node->GetAnimationManager().GetAnimation(animId);
111         if (animation == nullptr) {
112             return;
113         }
114         (*animation.*OP)(param);
115     }
116     static void CreateAnimation(
117         RSContext& context, NodeId targetId, const std::shared_ptr<RSRenderAnimation>& animation);
118     static void CreateParticleAnimation(RSContext& context, NodeId targetId,
119         const std::shared_ptr<RSRenderParticleAnimation>& animation);
120 
121     using AnimationCallbackProcessor = void (*)(NodeId, AnimationId, AnimationCallbackEvent);
122     static void AnimationCallback(RSContext& context,
123                                   NodeId targetId, AnimationId animId, AnimationCallbackEvent event);
124     static RSB_EXPORT void SetAnimationCallbackProcessor(AnimationCallbackProcessor processor);
125     static void CancelAnimation(RSContext& context, NodeId targetId, PropertyId propertyId);
126 
127     static void CreateInteractiveAnimator(RSContext& context,
128         InteractiveImplictAnimatorId targetId, std::vector<std::pair<NodeId, AnimationId>> animations,
129         bool startImmediately);
130     static void DestoryInteractiveAnimator(RSContext& context, InteractiveImplictAnimatorId targetId);
131     static void InteractiveAnimatorAddAnimations(RSContext& context,
132         InteractiveImplictAnimatorId targetId, std::vector<std::pair<NodeId, AnimationId>> animations);
133     static void PauseInteractiveAnimator(RSContext& context, InteractiveImplictAnimatorId targetId);
134     static void ContinueInteractiveAnimator(RSContext& context, InteractiveImplictAnimatorId targetId);
135     static void FinishInteractiveAnimator(RSContext& context,
136         InteractiveImplictAnimatorId targetId, RSInteractiveAnimationPosition finishPos);
137     static void ReverseInteractiveAnimator(RSContext& context, InteractiveImplictAnimatorId targetId);
138     static void SetFractionInteractiveAnimator(RSContext& context,
139         InteractiveImplictAnimatorId targetId, float fraction);
140 };
141 
142 // animation operation
143 ADD_COMMAND(RSAnimationStart,
144     ARG(ANIMATION, ANIMATION_START, AnimationCommandHelper::AnimOpReg<&RSRenderAnimation::Start>, NodeId, AnimationId))
145 ADD_COMMAND(RSAnimationPause,
146     ARG(ANIMATION, ANIMATION_PAUSE, AnimationCommandHelper::AnimOp<&RSRenderAnimation::Pause>, NodeId, AnimationId))
147 ADD_COMMAND(RSAnimationResume, ARG(ANIMATION, ANIMATION_RESUME,
148                                    AnimationCommandHelper::AnimOpReg<&RSRenderAnimation::Resume>, NodeId, AnimationId))
149 ADD_COMMAND(RSAnimationFinish,
150     ARG(ANIMATION, ANIMATION_FINISH, AnimationCommandHelper::AnimOp<&RSRenderAnimation::Finish>, NodeId, AnimationId))
151 ADD_COMMAND(RSAnimationReverse,
152     ARG(ANIMATION, ANIMATION_REVERSE, AnimationCommandHelper::AnimOp<bool, &RSRenderAnimation::SetReversed>, NodeId,
153         AnimationId, bool))
154 ADD_COMMAND(RSAnimationSetFraction,
155     ARG(ANIMATION, ANIMATION_SET_FRACTION, AnimationCommandHelper::AnimOp<float, &RSRenderAnimation::SetFraction>,
156         NodeId, AnimationId, float))
157 ADD_COMMAND(RSAnimationCancel,
158     ARG(ANIMATION, ANIMATION_CANCEL, AnimationCommandHelper::CancelAnimation, NodeId, PropertyId))
159 
160 ADD_COMMAND(RSAnimationCallback,
161     ARG(ANIMATION, ANIMATION_CALLBACK,
162         AnimationCommandHelper::AnimationCallback, NodeId, AnimationId, AnimationCallbackEvent))
163 
164 // create curve animation
165 ADD_COMMAND(RSAnimationCreateCurve, ARG(ANIMATION, ANIMATION_CREATE_CURVE, AnimationCommandHelper::CreateAnimation,
166     NodeId, std::shared_ptr<RSRenderCurveAnimation>))
167 
168 // create particle animation
169 ADD_COMMAND(RSAnimationCreateParticle,
170     ARG(ANIMATION, ANIMATION_CREATE_PARTICLE, AnimationCommandHelper::CreateParticleAnimation, NodeId,
171         std::shared_ptr<RSRenderParticleAnimation>))
172 
173 // create keyframe animation
174 ADD_COMMAND(RSAnimationCreateKeyframe, ARG(ANIMATION, ANIMATION_CREATE_KEYFRAME,
175     AnimationCommandHelper::CreateAnimation, NodeId, std::shared_ptr<RSRenderKeyframeAnimation>))
176 
177 // create path animation
178 ADD_COMMAND(RSAnimationCreatePath, ARG(ANIMATION, ANIMATION_CREATE_PATH,
179     AnimationCommandHelper::CreateAnimation, NodeId, std::shared_ptr<RSRenderPathAnimation>))
180 
181 // create transition animation
182 ADD_COMMAND(RSAnimationCreateTransition, ARG(ANIMATION, ANIMATION_CREATE_TRANSITION,
183     AnimationCommandHelper::CreateAnimation, NodeId, std::shared_ptr<RSRenderTransition>))
184 
185 // create spring animation
186 ADD_COMMAND(RSAnimationCreateSpring, ARG(ANIMATION, ANIMATION_CREATE_SPRING,
187     AnimationCommandHelper::CreateAnimation, NodeId, std::shared_ptr<RSRenderSpringAnimation>))
188 
189 // create interpolating spring animation
190 ADD_COMMAND(RSAnimationCreateInterpolatingSpring,
191     ARG(ANIMATION, ANIMATION_CREATE_INTERPOLATING_SPRING, AnimationCommandHelper::CreateAnimation, NodeId,
192         std::shared_ptr<RSRenderInterpolatingSpringAnimation>))
193 
194 // interactive implict animator operation
195 ADD_COMMAND(RSInteractiveAnimatorCreate, ARG(ANIMATION, INTERACTIVE_ANIMATOR_CREATE,
196     AnimationCommandHelper::CreateInteractiveAnimator, InteractiveImplictAnimatorId,
197     std::vector<std::pair<NodeId, AnimationId>>, bool))
198 ADD_COMMAND(RSInteractiveAnimatorDestory, ARG(ANIMATION, INTERACTIVE_ANIMATOR_DESTORY,
199     AnimationCommandHelper::DestoryInteractiveAnimator, InteractiveImplictAnimatorId))
200 ADD_COMMAND(RSInteractiveAnimatorPause, ARG(ANIMATION, INTERACTIVE_ANIMATOR_PAUSE,
201     AnimationCommandHelper::PauseInteractiveAnimator, InteractiveImplictAnimatorId))
202 ADD_COMMAND(RSInteractiveAnimatorContinue, ARG(ANIMATION, INTERACTIVE_ANIMATOR_CONTINUE,
203     AnimationCommandHelper::ContinueInteractiveAnimator, InteractiveImplictAnimatorId))
204 ADD_COMMAND(RSInteractiveAnimatorFinish, ARG(ANIMATION, INTERACTIVE_ANIMATOR_FINISH,
205     AnimationCommandHelper::FinishInteractiveAnimator, InteractiveImplictAnimatorId, RSInteractiveAnimationPosition))
206 ADD_COMMAND(RSInteractiveAnimatorReverse, ARG(ANIMATION, INTERACTIVE_ANIMATOR_REVERSE,
207     AnimationCommandHelper::ReverseInteractiveAnimator, InteractiveImplictAnimatorId))
208 ADD_COMMAND(RSInteractiveAnimatorSetFraction, ARG(ANIMATION, INTERACTIVE_ANIMATOR_SET_FRACTION,
209     AnimationCommandHelper::SetFractionInteractiveAnimator, InteractiveImplictAnimatorId, float))
210 } // namespace Rosen
211 } // namespace OHOS
212 
213 #endif // ROSEN_RENDER_SERVICE_BASE_COMMAND_RS_ANIMATION_COMMAND_H
214