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 //Each command HAVE TO have UNIQUE ID in ALL HISTORY 34 //If a command is not used and you want to delete it, 35 //just COMMENT it - and never use this value anymore 36 enum RSAnimationCommandType : uint16_t { 37 // curve animation 38 ANIMATION_CREATE_CURVE = 0x0100, 39 // particle animation 40 ANIMATION_CREATE_PARTICLE = 0x0101, 41 // keyframe animation 42 ANIMATION_CREATE_KEYFRAME = 0x0102, 43 // path animation 44 ANIMATION_CREATE_PATH = 0x0103, 45 // transition animation 46 ANIMATION_CREATE_TRANSITION = 0x0104, 47 // spring animation 48 ANIMATION_CREATE_SPRING = 0x0105, 49 // interpolating spring animation 50 ANIMATION_CREATE_INTERPOLATING_SPRING = 0x0106, 51 // particle animation NG 52 ANIMATION_CREATE_PARTICLE_NG = 0x0107, 53 54 // operations 55 ANIMATION_START = 0x0200, 56 ANIMATION_PAUSE = 0x0201, 57 ANIMATION_RESUME = 0x0202, 58 ANIMATION_FINISH = 0x0203, 59 ANIMATION_REVERSE = 0x0204, 60 ANIMATION_SET_FRACTION = 0x0205, 61 ANIMATION_CANCEL = 0x0206, 62 63 // UI operation 64 ANIMATION_CALLBACK = 0x0300, 65 66 // interactive animator operation 67 INTERACTIVE_ANIMATOR_CREATE = 0x0400, 68 INTERACTIVE_ANIMATOR_DESTORY = 0x0401, 69 INTERACTIVE_ANIMATOR_PAUSE = 0x0402, 70 INTERACTIVE_ANIMATOR_CONTINUE = 0x0403, 71 INTERACTIVE_ANIMATOR_FINISH = 0x0404, 72 INTERACTIVE_ANIMATOR_REVERSE = 0x0405, 73 INTERACTIVE_ANIMATOR_SET_FRACTION = 0x0406, 74 }; 75 76 enum AnimationCallbackEvent : uint16_t { REPEAT_FINISHED, FINISHED, LOGICALLY_FINISHED }; 77 78 class RSB_EXPORT AnimationCommandHelper { 79 public: 80 template<void (RSRenderAnimation::*OP)()> AnimOp(RSContext & context,NodeId nodeId,AnimationId animId)81 static void AnimOp(RSContext& context, NodeId nodeId, AnimationId animId) 82 { 83 [[maybe_unused]] auto [node, animation] = GetNodeAndAnimation(context, nodeId, animId, __PRETTY_FUNCTION__); 84 if (animation) { 85 (*animation.*OP)(); 86 } 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, animation] = GetNodeAndAnimation(context, nodeId, animId, __PRETTY_FUNCTION__); 92 if (node && animation) { 93 (*animation.*OP)(); 94 // register node on animation start or resume 95 context.RegisterAnimatingRenderNode(node); 96 } 97 } 98 template<typename T, void (RSRenderAnimation::*OP)(T)> AnimOp(RSContext & context,NodeId nodeId,AnimationId animId,T param)99 static void AnimOp(RSContext& context, NodeId nodeId, AnimationId animId, T param) 100 { 101 [[maybe_unused]] auto [node, animation] = GetNodeAndAnimation(context, nodeId, animId, __PRETTY_FUNCTION__); 102 if (animation) { 103 (*animation.*OP)(param); 104 } 105 } 106 static void CreateAnimation( 107 RSContext& context, NodeId targetId, const std::shared_ptr<RSRenderAnimation>& animation); 108 static void CreateParticleAnimationNG(RSContext& context, NodeId targetId, ModifierId modifierId, 109 const std::shared_ptr<RSRenderParticleAnimation>& animation); 110 111 using AnimationCallbackProcessor = void (*)(NodeId, AnimationId, uint64_t, AnimationCallbackEvent); 112 static void AnimationCallback(RSContext& context, 113 NodeId targetId, AnimationId animId, uint64_t token, 114 AnimationCallbackEvent event); 115 static RSB_EXPORT void SetAnimationCallbackProcessor(AnimationCallbackProcessor processor); 116 static void CancelAnimation(RSContext& context, NodeId targetId, PropertyId propertyId); 117 118 static void CreateInteractiveAnimator(RSContext& context, 119 InteractiveImplictAnimatorId targetId, std::vector<std::pair<NodeId, AnimationId>> animations, 120 bool startImmediately); 121 static void DestoryInteractiveAnimator(RSContext& context, InteractiveImplictAnimatorId targetId); 122 static void InteractiveAnimatorAddAnimations(RSContext& context, 123 InteractiveImplictAnimatorId targetId, std::vector<std::pair<NodeId, AnimationId>> animations); 124 static void PauseInteractiveAnimator(RSContext& context, InteractiveImplictAnimatorId targetId); 125 static void ContinueInteractiveAnimator(RSContext& context, InteractiveImplictAnimatorId targetId); 126 static void FinishInteractiveAnimator(RSContext& context, 127 InteractiveImplictAnimatorId targetId, RSInteractiveAnimationPosition finishPos); 128 static void ReverseInteractiveAnimator(RSContext& context, InteractiveImplictAnimatorId targetId); 129 static void SetFractionInteractiveAnimator(RSContext& context, 130 InteractiveImplictAnimatorId targetId, float fraction); 131 private: 132 using NodeAndAnimationPair = 133 std::pair<const std::shared_ptr<RSRenderNode>, const std::shared_ptr<RSRenderAnimation>>; 134 static NodeAndAnimationPair GetNodeAndAnimation( 135 RSContext& context, NodeId& nodeId, AnimationId& animId, const char* funcName); 136 }; 137 138 // animation operation 139 ADD_COMMAND(RSAnimationStart, 140 ARG(PERMISSION_APP, ANIMATION, ANIMATION_START, 141 AnimationCommandHelper::AnimOpReg<&RSRenderAnimation::Start>, NodeId, AnimationId)) 142 ADD_COMMAND(RSAnimationPause, 143 ARG(PERMISSION_APP, ANIMATION, ANIMATION_PAUSE, 144 AnimationCommandHelper::AnimOp<&RSRenderAnimation::Pause>, NodeId, AnimationId)) 145 ADD_COMMAND(RSAnimationResume, 146 ARG(PERMISSION_APP, ANIMATION, ANIMATION_RESUME, 147 AnimationCommandHelper::AnimOpReg<&RSRenderAnimation::Resume>, NodeId, AnimationId)) 148 ADD_COMMAND(RSAnimationFinish, 149 ARG(PERMISSION_APP, ANIMATION, ANIMATION_FINISH, 150 AnimationCommandHelper::AnimOpReg<&RSRenderAnimation::Finish>, NodeId, AnimationId)) 151 ADD_COMMAND(RSAnimationReverse, 152 ARG(PERMISSION_APP, ANIMATION, ANIMATION_REVERSE, 153 AnimationCommandHelper::AnimOp<bool, &RSRenderAnimation::SetReversed>, NodeId, AnimationId, bool)) 154 ADD_COMMAND(RSAnimationSetFraction, 155 ARG(PERMISSION_APP, ANIMATION, ANIMATION_SET_FRACTION, 156 AnimationCommandHelper::AnimOp<float, &RSRenderAnimation::SetFraction>, NodeId, AnimationId, float)) 157 ADD_COMMAND(RSAnimationCancel, 158 ARG(PERMISSION_APP, ANIMATION, ANIMATION_CANCEL, 159 AnimationCommandHelper::CancelAnimation, NodeId, PropertyId)) 160 161 ADD_COMMAND(RSAnimationCallback, 162 ARG(PERMISSION_APP, ANIMATION, ANIMATION_CALLBACK, 163 AnimationCommandHelper::AnimationCallback, NodeId, AnimationId, uint64_t, AnimationCallbackEvent)) 164 165 // create curve animation 166 ADD_COMMAND(RSAnimationCreateCurve, 167 ARG(PERMISSION_APP, ANIMATION, ANIMATION_CREATE_CURVE, AnimationCommandHelper::CreateAnimation, 168 NodeId, std::shared_ptr<RSRenderCurveAnimation>)) 169 170 // create particle animation NG 171 ADD_COMMAND(RSAnimationCreateParticleNG, 172 ARG(PERMISSION_APP, ANIMATION, ANIMATION_CREATE_PARTICLE_NG, AnimationCommandHelper::CreateParticleAnimationNG, 173 NodeId, ModifierId, std::shared_ptr<RSRenderParticleAnimation>)) 174 175 // create keyframe animation 176 ADD_COMMAND(RSAnimationCreateKeyframe, 177 ARG(PERMISSION_APP, ANIMATION, ANIMATION_CREATE_KEYFRAME, 178 AnimationCommandHelper::CreateAnimation, NodeId, std::shared_ptr<RSRenderKeyframeAnimation>)) 179 180 // create path animation 181 ADD_COMMAND(RSAnimationCreatePath, 182 ARG(PERMISSION_APP, ANIMATION, ANIMATION_CREATE_PATH, 183 AnimationCommandHelper::CreateAnimation, NodeId, std::shared_ptr<RSRenderPathAnimation>)) 184 185 // create transition animation 186 ADD_COMMAND(RSAnimationCreateTransition, 187 ARG(PERMISSION_APP, ANIMATION, ANIMATION_CREATE_TRANSITION, 188 AnimationCommandHelper::CreateAnimation, NodeId, std::shared_ptr<RSRenderTransition>)) 189 190 // create spring animation 191 ADD_COMMAND(RSAnimationCreateSpring, 192 ARG(PERMISSION_APP, ANIMATION, ANIMATION_CREATE_SPRING, 193 AnimationCommandHelper::CreateAnimation, NodeId, std::shared_ptr<RSRenderSpringAnimation>)) 194 195 // create interpolating spring animation 196 ADD_COMMAND(RSAnimationCreateInterpolatingSpring, 197 ARG(PERMISSION_APP, ANIMATION, ANIMATION_CREATE_INTERPOLATING_SPRING, AnimationCommandHelper::CreateAnimation, 198 NodeId, std::shared_ptr<RSRenderInterpolatingSpringAnimation>)) 199 200 // interactive implict animator operation 201 ADD_COMMAND(RSInteractiveAnimatorCreate, 202 ARG(PERMISSION_APP, ANIMATION, INTERACTIVE_ANIMATOR_CREATE, 203 AnimationCommandHelper::CreateInteractiveAnimator, InteractiveImplictAnimatorId, 204 std::vector<std::pair<NodeId, AnimationId>>, bool)) 205 ADD_COMMAND(RSInteractiveAnimatorDestory, 206 ARG(PERMISSION_APP, ANIMATION, INTERACTIVE_ANIMATOR_DESTORY, 207 AnimationCommandHelper::DestoryInteractiveAnimator, InteractiveImplictAnimatorId)) 208 ADD_COMMAND(RSInteractiveAnimatorPause, 209 ARG(PERMISSION_APP, ANIMATION, INTERACTIVE_ANIMATOR_PAUSE, 210 AnimationCommandHelper::PauseInteractiveAnimator, InteractiveImplictAnimatorId)) 211 ADD_COMMAND(RSInteractiveAnimatorContinue, 212 ARG(PERMISSION_APP, ANIMATION, INTERACTIVE_ANIMATOR_CONTINUE, 213 AnimationCommandHelper::ContinueInteractiveAnimator, InteractiveImplictAnimatorId)) 214 ADD_COMMAND(RSInteractiveAnimatorFinish, 215 ARG(PERMISSION_APP, ANIMATION, INTERACTIVE_ANIMATOR_FINISH, 216 AnimationCommandHelper::FinishInteractiveAnimator, InteractiveImplictAnimatorId, 217 RSInteractiveAnimationPosition)) 218 ADD_COMMAND(RSInteractiveAnimatorReverse, 219 ARG(PERMISSION_APP, ANIMATION, INTERACTIVE_ANIMATOR_REVERSE, 220 AnimationCommandHelper::ReverseInteractiveAnimator, InteractiveImplictAnimatorId)) 221 ADD_COMMAND(RSInteractiveAnimatorSetFraction, 222 ARG(PERMISSION_APP, ANIMATION, INTERACTIVE_ANIMATOR_SET_FRACTION, 223 AnimationCommandHelper::SetFractionInteractiveAnimator, InteractiveImplictAnimatorId, float)) 224 } // namespace Rosen 225 } // namespace OHOS 226 227 #endif // ROSEN_RENDER_SERVICE_BASE_COMMAND_RS_ANIMATION_COMMAND_H 228