• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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_keyframe_animation.h"
22 #include "animation/rs_render_path_animation.h"
23 #include "animation/rs_render_transition.h"
24 #include "command/rs_command_templates.h"
25 #include "pipeline/rs_render_node.h"
26 
27 namespace OHOS {
28 namespace Rosen {
29 
30 enum RSAnimationCommandType : uint16_t {
31     // curve animation
32     ANIMATION_CREATE_CURVE_INT,
33     ANIMATION_CREATE_CURVE_FLOAT,
34     ANIMATION_CREATE_CURVE_COLOR,
35     ANIMATION_CREATE_CURVE_MATRIX3F,
36     ANIMATION_CREATE_CURVE_VEC2F,
37     ANIMATION_CREATE_CURVE_VEC4F,
38     ANIMATION_CREATE_CURVE_QUATERNION,
39     ANIMATION_CREATE_CURVE_FILTER,
40     // keyframe animation
41     ANIMATION_CREATE_KEYFRAME_INT,
42     ANIMATION_CREATE_KEYFRAME_FLOAT,
43     ANIMATION_CREATE_KEYFRAME_COLOR,
44     ANIMATION_CREATE_KEYFRAME_MATRIX3F,
45     ANIMATION_CREATE_KEYFRAME_VEC2F,
46     ANIMATION_CREATE_KEYFRAME_VEC4F,
47     ANIMATION_CREATE_KEYFRAME_QUATERNION,
48     ANIMATION_CREATE_KEYFRAME_FILTER,
49     // path animation
50     ANIMATION_CREATE_PATH,
51     // transition animation
52     ANIMATION_CREATE_TRANSITION,
53 
54     // operations
55     ANIMATION_START,
56     ANIMATION_PAUSE,
57     ANIMATION_RESUME,
58     ANIMATION_FINISH,
59     ANIMATION_REVERSE,
60     ANIMATION_SET_FRACTION,
61 
62     // UI operation
63     ANIMATION_FINISH_CALLBACK,
64 };
65 
66 class AnimationCommandHelper {
67 public:
68     template<void (RSRenderAnimation::*OP)()>
AnimOp(RSContext & context,NodeId nodeId,AnimationId animId)69     static void AnimOp(RSContext& context, NodeId nodeId, AnimationId animId)
70     {
71         auto node = context.GetNodeMap().GetRenderNode<RSRenderNode>(nodeId);
72         if (node == nullptr) {
73             return;
74         }
75         auto animation = node->GetAnimationManager().GetAnimation(animId);
76         if (animation == nullptr) {
77             return;
78         }
79         (*animation.*OP)();
80     }
81     template<void (RSRenderAnimation::*OP)()>
AnimOpReg(RSContext & context,NodeId nodeId,AnimationId animId)82     static void AnimOpReg(RSContext& context, NodeId nodeId, AnimationId animId)
83     {
84         auto node = context.GetNodeMap().GetRenderNode<RSRenderNode>(nodeId);
85         if (node == nullptr) {
86             return;
87         }
88         auto animation = node->GetAnimationManager().GetAnimation(animId);
89         if (animation == nullptr) {
90             return;
91         }
92         (*animation.*OP)();
93         // register node on animation start or resume
94         context.RegisterAnimatingRenderNode(node);
95     }
96     template<typename T, void (RSRenderAnimation::*OP)(T)>
AnimOp(RSContext & context,NodeId nodeId,AnimationId animId,T param)97     static void AnimOp(RSContext& context, NodeId nodeId, AnimationId animId, T param)
98     {
99         auto node = context.GetNodeMap().GetRenderNode<RSRenderNode>(nodeId);
100         if (node == nullptr) {
101             return;
102         }
103         auto animation = node->GetAnimationManager().GetAnimation(animId);
104         if (animation == nullptr) {
105             return;
106         }
107         (*animation.*OP)(param);
108     }
CreateAnimation(RSContext & context,NodeId targetId,const std::shared_ptr<RSRenderAnimation> & animation)109     static void CreateAnimation(
110         RSContext& context, NodeId targetId, const std::shared_ptr<RSRenderAnimation>& animation)
111     {
112         auto node = context.GetNodeMap().GetRenderNode<RSRenderNode>(targetId);
113         if (node == nullptr) {
114             return;
115         }
116         node->GetAnimationManager().AddAnimation(animation);
117         animation->Attach(node.get());
118         animation->Start();
119         // register node on animation add
120         context.RegisterAnimatingRenderNode(node);
121     }
122 
123     using FinishCallbackProcessor = void (*)(NodeId, AnimationId);
124     static void AnimationFinishCallback(RSContext& context, NodeId targetId, AnimationId animId);
125     static void SetFinisCallbackProcessor(FinishCallbackProcessor processor);
126 };
127 
128 // animation operation
129 ADD_COMMAND(RSAnimationStart,
130     ARG(ANIMATION, ANIMATION_START, AnimationCommandHelper::AnimOpReg<&RSRenderAnimation::Start>, NodeId, AnimationId))
131 ADD_COMMAND(RSAnimationPause,
132     ARG(ANIMATION, ANIMATION_PAUSE, AnimationCommandHelper::AnimOp<&RSRenderAnimation::Pause>, NodeId, AnimationId))
133 ADD_COMMAND(RSAnimationResume, ARG(ANIMATION, ANIMATION_RESUME,
134                                    AnimationCommandHelper::AnimOpReg<&RSRenderAnimation::Resume>, NodeId, AnimationId))
135 ADD_COMMAND(RSAnimationFinish,
136     ARG(ANIMATION, ANIMATION_FINISH, AnimationCommandHelper::AnimOp<&RSRenderAnimation::Finish>, NodeId, AnimationId))
137 ADD_COMMAND(RSAnimationReverse,
138     ARG(ANIMATION, ANIMATION_REVERSE, AnimationCommandHelper::AnimOp<bool, &RSRenderAnimation::SetReversed>, NodeId,
139         AnimationId, bool))
140 ADD_COMMAND(RSAnimationSetFraction,
141     ARG(ANIMATION, ANIMATION_SET_FRACTION, AnimationCommandHelper::AnimOp<float, &RSRenderAnimation::SetFraction>,
142         NodeId, AnimationId, float))
143 
144 ADD_COMMAND(RSAnimationFinishCallback,
145     ARG(ANIMATION, ANIMATION_FINISH_CALLBACK, AnimationCommandHelper::AnimationFinishCallback, NodeId, AnimationId))
146 
147 // create curve animation
148 ADD_COMMAND(
149     RSAnimationCreateCurveInt, ARG(ANIMATION, ANIMATION_CREATE_CURVE_INT, AnimationCommandHelper::CreateAnimation,
150                                    NodeId, std::shared_ptr<RSRenderCurveAnimation<int>>))
151 ADD_COMMAND(
152     RSAnimationCreateCurveFloat, ARG(ANIMATION, ANIMATION_CREATE_CURVE_FLOAT, AnimationCommandHelper::CreateAnimation,
153                                      NodeId, std::shared_ptr<RSRenderCurveAnimation<float>>))
154 ADD_COMMAND(
155     RSAnimationCreateCurveColor, ARG(ANIMATION, ANIMATION_CREATE_CURVE_COLOR, AnimationCommandHelper::CreateAnimation,
156                                      NodeId, std::shared_ptr<RSRenderCurveAnimation<Color>>))
157 ADD_COMMAND(RSAnimationCreateCurveMatrix3f,
158     ARG(ANIMATION, ANIMATION_CREATE_CURVE_MATRIX3F, AnimationCommandHelper::CreateAnimation, NodeId,
159         std::shared_ptr<RSRenderCurveAnimation<Matrix3f>>))
160 ADD_COMMAND(
161     RSAnimationCreateCurveVec2f, ARG(ANIMATION, ANIMATION_CREATE_CURVE_VEC2F, AnimationCommandHelper::CreateAnimation,
162                                      NodeId, std::shared_ptr<RSRenderCurveAnimation<Vector2f>>))
163 ADD_COMMAND(
164     RSAnimationCreateCurveVec4f, ARG(ANIMATION, ANIMATION_CREATE_CURVE_VEC4F, AnimationCommandHelper::CreateAnimation,
165                                      NodeId, std::shared_ptr<RSRenderCurveAnimation<Vector4f>>))
166 ADD_COMMAND(RSAnimationCreateCurveQuaternion,
167     ARG(ANIMATION, ANIMATION_CREATE_CURVE_QUATERNION, AnimationCommandHelper::CreateAnimation, NodeId,
168         std::shared_ptr<RSRenderCurveAnimation<Quaternion>>))
169 // ADD_COMMAND(
170 //     RSAnimationCreateCurveFilter, ARG(ANIMATION, ANIMATION_CREATE_CURVE_FILTER, AnimationCommandHelper::CreateAnimation,
171 //                                      NodeId, std::shared_ptr<RSRenderCurveAnimation<std::shared_ptr<RSFilter>>>))
172 
173 // create keyframe animation
174 ADD_COMMAND(
175     RSAnimationCreateKeyframeInt, ARG(ANIMATION, ANIMATION_CREATE_KEYFRAME_INT, AnimationCommandHelper::CreateAnimation,
176                                       NodeId, std::shared_ptr<RSRenderKeyframeAnimation<int>>))
177 ADD_COMMAND(RSAnimationCreateKeyframeFloat,
178     ARG(ANIMATION, ANIMATION_CREATE_KEYFRAME_FLOAT, AnimationCommandHelper::CreateAnimation, NodeId,
179         std::shared_ptr<RSRenderKeyframeAnimation<float>>))
180 ADD_COMMAND(RSAnimationCreateKeyframeColor,
181     ARG(ANIMATION, ANIMATION_CREATE_KEYFRAME_COLOR, AnimationCommandHelper::CreateAnimation, NodeId,
182         std::shared_ptr<RSRenderKeyframeAnimation<Color>>))
183 ADD_COMMAND(RSAnimationCreateKeyframeMatrix3f,
184     ARG(ANIMATION, ANIMATION_CREATE_KEYFRAME_MATRIX3F, AnimationCommandHelper::CreateAnimation, NodeId,
185         std::shared_ptr<RSRenderKeyframeAnimation<Matrix3f>>))
186 ADD_COMMAND(RSAnimationCreateKeyframeVec2f,
187     ARG(ANIMATION, ANIMATION_CREATE_KEYFRAME_VEC2F, AnimationCommandHelper::CreateAnimation, NodeId,
188         std::shared_ptr<RSRenderKeyframeAnimation<Vector2f>>))
189 ADD_COMMAND(RSAnimationCreateKeyframeVec4f,
190     ARG(ANIMATION, ANIMATION_CREATE_KEYFRAME_VEC4F, AnimationCommandHelper::CreateAnimation, NodeId,
191         std::shared_ptr<RSRenderKeyframeAnimation<Vector4f>>))
192 ADD_COMMAND(RSAnimationCreateKeyframeQuaternion,
193     ARG(ANIMATION, ANIMATION_CREATE_KEYFRAME_QUATERNION, AnimationCommandHelper::CreateAnimation, NodeId,
194         std::shared_ptr<RSRenderKeyframeAnimation<Quaternion>>))
195 // ADD_COMMAND(RSAnimationCreateKeyframeFilter,
196 //     ARG(ANIMATION, ANIMATION_CREATE_KEYFRAME_FILTER, AnimationCommandHelper::CreateAnimation, NodeId,
197 //         std::shared_ptr<RSRenderKeyframeAnimation<std::shared_ptr<RSFilter>>>))
198 
199 // create path animation
200 ADD_COMMAND(RSAnimationCreatePath, ARG(ANIMATION, ANIMATION_CREATE_PATH, AnimationCommandHelper::CreateAnimation,
201                                        NodeId, std::shared_ptr<RSRenderPathAnimation>))
202 
203 // create transition animation
204 ADD_COMMAND(
205     RSAnimationCreateTransition, ARG(ANIMATION, ANIMATION_CREATE_TRANSITION, AnimationCommandHelper::CreateAnimation,
206                                      NodeId, std::shared_ptr<RSRenderTransition>))
207 
208 } // namespace Rosen
209 } // namespace OHOS
210 
211 #endif // ROSEN_RENDER_SERVICE_BASE_COMMAND_RS_ANIMATION_COMMAND_H
212