• 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 
57     // UI operation
58     ANIMATION_CALLBACK,
59 };
60 
61 enum AnimationCallbackEvent : uint16_t { REPEAT_FINISHED, FINISHED };
62 
63 class RSB_EXPORT AnimationCommandHelper {
64 public:
65     template<void (RSRenderAnimation::*OP)()>
AnimOp(RSContext & context,NodeId nodeId,AnimationId animId)66     static void AnimOp(RSContext& context, NodeId nodeId, AnimationId animId)
67     {
68         auto node = context.GetNodeMap().GetRenderNode<RSRenderNode>(nodeId);
69         if (node == nullptr) {
70             return;
71         }
72         auto animation = node->GetAnimationManager().GetAnimation(animId);
73         if (animation == nullptr) {
74             return;
75         }
76         (*animation.*OP)();
77     }
78     template<void (RSRenderAnimation::*OP)()>
AnimOpReg(RSContext & context,NodeId nodeId,AnimationId animId)79     static void AnimOpReg(RSContext& context, NodeId nodeId, AnimationId animId)
80     {
81         auto node = context.GetNodeMap().GetRenderNode<RSRenderNode>(nodeId);
82         if (node == nullptr) {
83             return;
84         }
85         auto animation = node->GetAnimationManager().GetAnimation(animId);
86         if (animation == nullptr) {
87             return;
88         }
89         (*animation.*OP)();
90         // register node on animation start or resume
91         context.RegisterAnimatingRenderNode(node);
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         auto node = context.GetNodeMap().GetRenderNode<RSRenderNode>(nodeId);
97         if (node == nullptr) {
98             return;
99         }
100         auto animation = node->GetAnimationManager().GetAnimation(animId);
101         if (animation == nullptr) {
102             return;
103         }
104         (*animation.*OP)(param);
105     }
106     static void CreateAnimation(
107         RSContext& context, NodeId targetId, const std::shared_ptr<RSRenderAnimation>& animation);
108     static void CreateParticleAnimation(RSContext& context, NodeId targetId,
109         const std::shared_ptr<RSRenderParticleAnimation>& animation);
110 
111     using AnimationCallbackProcessor = void (*)(NodeId, AnimationId, AnimationCallbackEvent);
112     static void AnimationCallback(RSContext& context,
113                                   NodeId targetId, AnimationId animId, AnimationCallbackEvent event);
114     static RSB_EXPORT void SetAnimationCallbackProcessor(AnimationCallbackProcessor processor);
115 };
116 
117 // animation operation
118 ADD_COMMAND(RSAnimationStart,
119     ARG(ANIMATION, ANIMATION_START, AnimationCommandHelper::AnimOpReg<&RSRenderAnimation::Start>, NodeId, AnimationId))
120 ADD_COMMAND(RSAnimationPause,
121     ARG(ANIMATION, ANIMATION_PAUSE, AnimationCommandHelper::AnimOp<&RSRenderAnimation::Pause>, NodeId, AnimationId))
122 ADD_COMMAND(RSAnimationResume, ARG(ANIMATION, ANIMATION_RESUME,
123                                    AnimationCommandHelper::AnimOpReg<&RSRenderAnimation::Resume>, NodeId, AnimationId))
124 ADD_COMMAND(RSAnimationFinish,
125     ARG(ANIMATION, ANIMATION_FINISH, AnimationCommandHelper::AnimOp<&RSRenderAnimation::Finish>, NodeId, AnimationId))
126 ADD_COMMAND(RSAnimationReverse,
127     ARG(ANIMATION, ANIMATION_REVERSE, AnimationCommandHelper::AnimOp<bool, &RSRenderAnimation::SetReversed>, NodeId,
128         AnimationId, bool))
129 ADD_COMMAND(RSAnimationSetFraction,
130     ARG(ANIMATION, ANIMATION_SET_FRACTION, AnimationCommandHelper::AnimOp<float, &RSRenderAnimation::SetFraction>,
131         NodeId, AnimationId, float))
132 
133 ADD_COMMAND(RSAnimationCallback,
134     ARG(ANIMATION, ANIMATION_CALLBACK,
135         AnimationCommandHelper::AnimationCallback, NodeId, AnimationId, AnimationCallbackEvent))
136 
137 // create curve animation
138 ADD_COMMAND(RSAnimationCreateCurve, ARG(ANIMATION, ANIMATION_CREATE_CURVE, AnimationCommandHelper::CreateAnimation,
139     NodeId, std::shared_ptr<RSRenderCurveAnimation>))
140 
141 // create particle animation
142 ADD_COMMAND(RSAnimationCreateParticle,
143     ARG(ANIMATION, ANIMATION_CREATE_PARTICLE, AnimationCommandHelper::CreateParticleAnimation, NodeId,
144         std::shared_ptr<RSRenderParticleAnimation>))
145 
146 // create keyframe animation
147 ADD_COMMAND(RSAnimationCreateKeyframe,ARG(ANIMATION, ANIMATION_CREATE_KEYFRAME,
148     AnimationCommandHelper::CreateAnimation, NodeId, std::shared_ptr<RSRenderKeyframeAnimation>))
149 
150 // create path animation
151 ADD_COMMAND(RSAnimationCreatePath, ARG(ANIMATION, ANIMATION_CREATE_PATH,
152     AnimationCommandHelper::CreateAnimation, NodeId, std::shared_ptr<RSRenderPathAnimation>))
153 
154 // create transition animation
155 ADD_COMMAND(RSAnimationCreateTransition, ARG(ANIMATION, ANIMATION_CREATE_TRANSITION,
156     AnimationCommandHelper::CreateAnimation, NodeId, std::shared_ptr<RSRenderTransition>))
157 
158 // create spring animation
159 ADD_COMMAND(RSAnimationCreateSpring, ARG(ANIMATION, ANIMATION_CREATE_SPRING,
160     AnimationCommandHelper::CreateAnimation, NodeId, std::shared_ptr<RSRenderSpringAnimation>))
161 
162 // create interpolating spring animation
163 ADD_COMMAND(RSAnimationCreateInterpolatingSpring,
164     ARG(ANIMATION, ANIMATION_CREATE_INTERPOLATING_SPRING, AnimationCommandHelper::CreateAnimation, NodeId,
165         std::shared_ptr<RSRenderInterpolatingSpringAnimation>))
166 } // namespace Rosen
167 } // namespace OHOS
168 
169 #endif // ROSEN_RENDER_SERVICE_BASE_COMMAND_RS_ANIMATION_COMMAND_H
170