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
25 namespace OHOS {
26 namespace Rosen {
27
28 namespace {
29 static AnimationCommandHelper::AnimationCallbackProcessor animationCallbackProcessor = nullptr;
30 }
31
AnimationCallback(RSContext & context,NodeId targetId,AnimationId animId,AnimationCallbackEvent event)32 void AnimationCommandHelper::AnimationCallback(
33 RSContext& context, NodeId targetId, AnimationId animId, AnimationCallbackEvent event)
34 {
35 if (animationCallbackProcessor != nullptr) {
36 animationCallbackProcessor(targetId, animId, event);
37 }
38 }
39
SetAnimationCallbackProcessor(AnimationCallbackProcessor processor)40 void AnimationCommandHelper::SetAnimationCallbackProcessor(AnimationCallbackProcessor processor)
41 {
42 animationCallbackProcessor = processor;
43 }
44
CreateAnimation(RSContext & context,NodeId targetId,const std::shared_ptr<RSRenderAnimation> & animation)45 void AnimationCommandHelper::CreateAnimation(
46 RSContext& context, NodeId targetId, const std::shared_ptr<RSRenderAnimation>& animation)
47 {
48 auto node = context.GetNodeMap().GetRenderNode<RSRenderNode>(targetId);
49 if (node == nullptr) {
50 return;
51 }
52 node->GetAnimationManager().AddAnimation(animation);
53 auto modifier = node->GetModifier(animation->GetPropertyId());
54 if (modifier != nullptr) {
55 animation->AttachRenderProperty(modifier->GetProperty());
56 }
57 auto currentTime = context.GetCurrentTimestamp();
58 animation->SetStartTime(currentTime);
59 animation->Attach(node.get());
60 // register node as animating node
61 context.RegisterAnimatingRenderNode(node);
62 }
63
CreateParticleAnimation(RSContext & context,NodeId targetId,const std::shared_ptr<RSRenderParticleAnimation> & animation)64 void AnimationCommandHelper::CreateParticleAnimation(
65 RSContext& context, NodeId targetId, const std::shared_ptr<RSRenderParticleAnimation>& animation)
66 {
67 auto node = context.GetNodeMap().GetRenderNode<RSRenderNode>(targetId);
68 if (node == nullptr) {
69 return;
70 }
71 auto propertyId = animation->GetPropertyId();
72 node->GetAnimationManager().AddAnimation(animation);
73 auto property = std::make_shared<RSRenderProperty<RSRenderParticleVector>>(
74 animation->GetRenderParticle(), propertyId);
75 auto modifier = std::make_shared<RSParticleRenderModifier>(property);
76 node->AddModifier(modifier);
77 animation->AttachRenderProperty(property);
78 auto currentTime = context.GetCurrentTimestamp();
79 animation->SetStartTime(currentTime);
80 animation->Attach(node.get());
81 // register node as animating node
82 context.RegisterAnimatingRenderNode(node);
83 }
84 } // namespace Rosen
85 } // namespace OHOS
86