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 #include "command/rs_animation_command.h"
17
18 #include <memory>
19
20 #include "animation/rs_render_particle.h"
21 #include "common/rs_common_def.h"
22 #include "modifier/rs_render_modifier.h"
23 #include "modifier/rs_render_property.h"
24 #include "platform/common/rs_log.h"
25
26 namespace OHOS {
27 namespace Rosen {
28
29 namespace {
30 static AnimationCommandHelper::AnimationCallbackProcessor animationCallbackProcessor = nullptr;
31 }
32
AnimationCallback(RSContext & context,NodeId targetId,AnimationId animId,AnimationCallbackEvent event)33 void AnimationCommandHelper::AnimationCallback(
34 RSContext& context, NodeId targetId, AnimationId animId, AnimationCallbackEvent event)
35 {
36 if (animationCallbackProcessor != nullptr) {
37 animationCallbackProcessor(targetId, animId, event);
38 }
39 }
40
SetAnimationCallbackProcessor(AnimationCallbackProcessor processor)41 void AnimationCommandHelper::SetAnimationCallbackProcessor(AnimationCallbackProcessor processor)
42 {
43 animationCallbackProcessor = processor;
44 }
45
CreateAnimation(RSContext & context,NodeId targetId,const std::shared_ptr<RSRenderAnimation> & animation)46 void AnimationCommandHelper::CreateAnimation(
47 RSContext& context, NodeId targetId, const std::shared_ptr<RSRenderAnimation>& animation)
48 {
49 auto node = context.GetNodeMap().GetRenderNode<RSRenderNode>(targetId);
50 if (node == nullptr) {
51 return;
52 }
53 if (animation == nullptr) {
54 RS_LOGE("AnimationCommandHelper::CreateAnimation, animation is nullptr");
55 return;
56 }
57 node->GetAnimationManager().AddAnimation(animation);
58 auto modifier = node->GetModifier(animation->GetPropertyId());
59 if (modifier != nullptr) {
60 animation->AttachRenderProperty(modifier->GetProperty());
61 }
62 auto currentTime = context.GetCurrentTimestamp();
63 animation->SetStartTime(currentTime);
64 animation->Attach(node.get());
65 // register node as animating node
66 context.RegisterAnimatingRenderNode(node);
67 }
68
CreateParticleAnimation(RSContext & context,NodeId targetId,const std::shared_ptr<RSRenderParticleAnimation> & animation)69 void AnimationCommandHelper::CreateParticleAnimation(
70 RSContext& context, NodeId targetId, const std::shared_ptr<RSRenderParticleAnimation>& animation)
71 {
72 auto node = context.GetNodeMap().GetRenderNode<RSRenderNode>(targetId);
73 if (node == nullptr) {
74 return;
75 }
76 if (animation == nullptr) {
77 RS_LOGE("AnimationCommandHelper::CreateParticleAnimation, animation is nullptr");
78 return;
79 }
80 auto propertyId = animation->GetPropertyId();
81 node->GetAnimationManager().AddAnimation(animation);
82 auto property = std::make_shared<RSRenderProperty<RSRenderParticleVector>>(
83 animation->GetRenderParticle(), propertyId);
84 auto modifier = std::make_shared<RSParticlesRenderModifier>(property);
85 node->AddModifier(modifier);
86 animation->AttachRenderProperty(property);
87 auto currentTime = context.GetCurrentTimestamp();
88 animation->SetStartTime(currentTime);
89 animation->Attach(node.get());
90 // register node as animating node
91 context.RegisterAnimatingRenderNode(node);
92 }
93
CancelAnimation(RSContext & context,NodeId targetId,PropertyId propertyId)94 void AnimationCommandHelper::CancelAnimation(RSContext& context, NodeId targetId, PropertyId propertyId)
95 {
96 auto node = context.GetNodeMap().GetRenderNode<RSRenderNode>(targetId);
97 if (node == nullptr) {
98 return;
99 }
100
101 auto& animationManager = node->GetAnimationManager();
102 animationManager.CancelAnimationByPropertyId(propertyId);
103 }
104 } // namespace Rosen
105 } // namespace OHOS
106