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_keyframe_animation.h" 22 #include "animation/rs_render_path_animation.h" 23 #include "animation/rs_render_spring_animation.h" 24 #include "animation/rs_render_transition.h" 25 #include "command/rs_command_templates.h" 26 #include "common/rs_macros.h" 27 #include "pipeline/rs_render_node.h" 28 29 namespace OHOS { 30 namespace Rosen { 31 enum RSAnimationCommandType : uint16_t { 32 // curve animation 33 ANIMATION_CREATE_CURVE, 34 // keyframe animation 35 ANIMATION_CREATE_KEYFRAME, 36 // path animation 37 ANIMATION_CREATE_PATH, 38 // transition animation 39 ANIMATION_CREATE_TRANSITION, 40 // spring animation 41 ANIMATION_CREATE_SPRING, 42 43 // operations 44 ANIMATION_START, 45 ANIMATION_PAUSE, 46 ANIMATION_RESUME, 47 ANIMATION_FINISH, 48 ANIMATION_REVERSE, 49 ANIMATION_SET_FRACTION, 50 51 // UI operation 52 ANIMATION_FINISH_CALLBACK, 53 }; 54 55 class RSB_EXPORT AnimationCommandHelper { 56 public: 57 template<void (RSRenderAnimation::*OP)()> AnimOp(RSContext & context,NodeId nodeId,AnimationId animId)58 static void AnimOp(RSContext& context, NodeId nodeId, AnimationId animId) 59 { 60 auto node = context.GetNodeMap().GetRenderNode<RSRenderNode>(nodeId); 61 if (node == nullptr) { 62 return; 63 } 64 auto animation = node->GetAnimationManager().GetAnimation(animId); 65 if (animation == nullptr) { 66 return; 67 } 68 (*animation.*OP)(); 69 } 70 template<void (RSRenderAnimation::*OP)()> AnimOpReg(RSContext & context,NodeId nodeId,AnimationId animId)71 static void AnimOpReg(RSContext& context, NodeId nodeId, AnimationId animId) 72 { 73 auto node = context.GetNodeMap().GetRenderNode<RSRenderNode>(nodeId); 74 if (node == nullptr) { 75 return; 76 } 77 auto animation = node->GetAnimationManager().GetAnimation(animId); 78 if (animation == nullptr) { 79 return; 80 } 81 (*animation.*OP)(); 82 // register node on animation start or resume 83 context.RegisterAnimatingRenderNode(node); 84 } 85 template<typename T, void (RSRenderAnimation::*OP)(T)> AnimOp(RSContext & context,NodeId nodeId,AnimationId animId,T param)86 static void AnimOp(RSContext& context, NodeId nodeId, AnimationId animId, T param) 87 { 88 auto node = context.GetNodeMap().GetRenderNode<RSRenderNode>(nodeId); 89 if (node == nullptr) { 90 return; 91 } 92 auto animation = node->GetAnimationManager().GetAnimation(animId); 93 if (animation == nullptr) { 94 return; 95 } 96 (*animation.*OP)(param); 97 } 98 static void CreateAnimation( 99 RSContext& context, NodeId targetId, const std::shared_ptr<RSRenderAnimation>& animation); 100 101 using FinishCallbackProcessor = void (*)(NodeId, AnimationId); 102 static void AnimationFinishCallback(RSContext& context, NodeId targetId, AnimationId animId); 103 static RSB_EXPORT void SetFinishCallbackProcessor(FinishCallbackProcessor processor); 104 }; 105 106 // animation operation 107 ADD_COMMAND(RSAnimationStart, 108 ARG(ANIMATION, ANIMATION_START, AnimationCommandHelper::AnimOpReg<&RSRenderAnimation::Start>, NodeId, AnimationId)) 109 ADD_COMMAND(RSAnimationPause, 110 ARG(ANIMATION, ANIMATION_PAUSE, AnimationCommandHelper::AnimOp<&RSRenderAnimation::Pause>, NodeId, AnimationId)) 111 ADD_COMMAND(RSAnimationResume, ARG(ANIMATION, ANIMATION_RESUME, 112 AnimationCommandHelper::AnimOpReg<&RSRenderAnimation::Resume>, NodeId, AnimationId)) 113 ADD_COMMAND(RSAnimationFinish, 114 ARG(ANIMATION, ANIMATION_FINISH, AnimationCommandHelper::AnimOp<&RSRenderAnimation::Finish>, NodeId, AnimationId)) 115 ADD_COMMAND(RSAnimationReverse, 116 ARG(ANIMATION, ANIMATION_REVERSE, AnimationCommandHelper::AnimOp<bool, &RSRenderAnimation::SetReversed>, NodeId, 117 AnimationId, bool)) 118 ADD_COMMAND(RSAnimationSetFraction, 119 ARG(ANIMATION, ANIMATION_SET_FRACTION, AnimationCommandHelper::AnimOp<float, &RSRenderAnimation::SetFraction>, 120 NodeId, AnimationId, float)) 121 122 ADD_COMMAND(RSAnimationFinishCallback, 123 ARG(ANIMATION, ANIMATION_FINISH_CALLBACK, AnimationCommandHelper::AnimationFinishCallback, NodeId, AnimationId)) 124 125 // create curve animation 126 ADD_COMMAND(RSAnimationCreateCurve, ARG(ANIMATION, ANIMATION_CREATE_CURVE, AnimationCommandHelper::CreateAnimation, 127 NodeId, std::shared_ptr<RSRenderCurveAnimation>)) 128 129 // create keyframe animation 130 ADD_COMMAND(RSAnimationCreateKeyframe,ARG(ANIMATION, ANIMATION_CREATE_KEYFRAME, 131 AnimationCommandHelper::CreateAnimation, NodeId, std::shared_ptr<RSRenderKeyframeAnimation>)) 132 133 // create path animation 134 ADD_COMMAND(RSAnimationCreatePath, ARG(ANIMATION, ANIMATION_CREATE_PATH, 135 AnimationCommandHelper::CreateAnimation, NodeId, std::shared_ptr<RSRenderPathAnimation>)) 136 137 // create transition animation 138 ADD_COMMAND(RSAnimationCreateTransition, ARG(ANIMATION, ANIMATION_CREATE_TRANSITION, 139 AnimationCommandHelper::CreateAnimation, NodeId, std::shared_ptr<RSRenderTransition>)) 140 141 // create spring animation 142 ADD_COMMAND(RSAnimationCreateSpring,ARG(ANIMATION, ANIMATION_CREATE_SPRING, 143 AnimationCommandHelper::CreateAnimation, NodeId, std::shared_ptr<RSRenderSpringAnimation>)) 144 } // namespace Rosen 145 } // namespace OHOS 146 147 #endif // ROSEN_RENDER_SERVICE_BASE_COMMAND_RS_ANIMATION_COMMAND_H 148