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 #include "animation/rs_animation_manager.h"
17
18 #include <algorithm>
19 #include <string>
20
21 #include "animation/rs_render_animation.h"
22 #include "command/rs_animation_command.h"
23 #include "command/rs_message_processor.h"
24 #include "pipeline/rs_dirty_region_manager.h"
25 #include "pipeline/rs_paint_filter_canvas.h"
26 #include "pipeline/rs_render_node.h"
27 #include "platform/common/rs_log.h"
28 #include "property/rs_transition_properties.h"
29
30 namespace OHOS {
31 namespace Rosen {
32 class RSRootRenderNode;
33
AddAnimation(const std::shared_ptr<RSRenderAnimation> & animation)34 void RSAnimationManager::AddAnimation(const std::shared_ptr<RSRenderAnimation>& animation)
35 {
36 AnimationId key = animation->GetAnimationId();
37 if (animations_.find(key) != animations_.end()) {
38 ROSEN_LOGE("RSAnimationManager::AddAnimation, The animation already exists when is added");
39 return;
40 }
41 animations_.emplace(key, animation);
42 OnAnimationAdd(animation);
43 }
44
RemoveAnimation(AnimationId keyId)45 void RSAnimationManager::RemoveAnimation(AnimationId keyId)
46 {
47 auto animationItr = animations_.find(keyId);
48 if (animationItr == animations_.end()) {
49 ROSEN_LOGE("RSAnimationManager::RemoveAnimation, The Animation does not exist when is deleted");
50 return;
51 }
52 OnAnimationRemove(animationItr->second);
53 animations_.erase(animationItr);
54 }
55
Animate(int64_t time)56 bool RSAnimationManager::Animate(int64_t time)
57 {
58 // process animation
59 bool hasRunningAnimation = false;
60
61 // iterate and execute all animations, remove finished animations
62 std::__libcpp_erase_if_container(animations_, [this, &hasRunningAnimation, time](auto& iter) -> bool {
63 auto& animation = iter.second;
64 bool isFinished = animation->Animate(time);
65 if (isFinished) {
66 OnAnimationFinished(animation);
67 } else {
68 hasRunningAnimation = animation->IsRunning() || hasRunningAnimation ;
69 }
70 return isFinished;
71 });
72
73 return hasRunningAnimation;
74 }
75
GetAnimation(AnimationId id) const76 const std::shared_ptr<RSRenderAnimation> RSAnimationManager::GetAnimation(AnimationId id) const
77 {
78 auto animationItr = animations_.find(id);
79 if (animationItr == animations_.end()) {
80 ROSEN_LOGE("RSAnimationManager::GetAnimation, animtor[%lld] is not found", id);
81 return nullptr;
82 }
83 return animationItr->second;
84 }
85
OnAnimationRemove(const std::shared_ptr<RSRenderAnimation> & animation)86 void RSAnimationManager::OnAnimationRemove(const std::shared_ptr<RSRenderAnimation>& animation)
87 {
88 animationNum_[animation->GetProperty()]--;
89 }
90
OnAnimationAdd(const std::shared_ptr<RSRenderAnimation> & animation)91 void RSAnimationManager::OnAnimationAdd(const std::shared_ptr<RSRenderAnimation>& animation)
92 {
93 animationNum_[animation->GetProperty()]++;
94 }
95
96 namespace {
ExtractPid(AnimationId animId)97 inline constexpr uint32_t ExtractPid(AnimationId animId)
98 {
99 return animId >> 32;
100 }
101 }
102
OnAnimationFinished(const std::shared_ptr<RSRenderAnimation> & animation)103 void RSAnimationManager::OnAnimationFinished(const std::shared_ptr<RSRenderAnimation>& animation)
104 {
105 NodeId targetId = animation->GetTarget() ? animation->GetTarget()->GetId() : 0;
106 AnimationId animationId = animation->GetAnimationId();
107
108 std::unique_ptr<RSCommand> command =
109 std::make_unique<RSAnimationFinishCallback>(targetId, animationId);
110 RSMessageProcessor::Instance().AddUIMessage(ExtractPid(animationId), command);
111 OnAnimationRemove(animation);
112 animation->Detach();
113 }
114
RegisterTransition(AnimationId id,const TransitionCallback & transition)115 void RSAnimationManager::RegisterTransition(AnimationId id, const TransitionCallback& transition)
116 {
117 ClearTransition(id);
118 transition_.push_back({ id, transition });
119 }
120
UnregisterTransition(AnimationId id)121 void RSAnimationManager::UnregisterTransition(AnimationId id)
122 {
123 ClearTransition(id);
124 }
125
ClearTransition(AnimationId id)126 void RSAnimationManager::ClearTransition(AnimationId id)
127 {
128 if (transition_.empty()) {
129 ROSEN_LOGE("RSAnimationManager::ClearTransition, transition_ is empty");
130 return;
131 }
132 transition_.remove_if(
133 [&](std::pair<AnimationId, TransitionCallback>& transition) -> bool { return id == transition.first; });
134 }
135
GetTransitionProperties()136 std::unique_ptr<RSTransitionProperties> RSAnimationManager::GetTransitionProperties()
137 {
138 if (transition_.empty()) {
139 return nullptr;
140 }
141 auto transitionProperties = std::make_unique<RSTransitionProperties>();
142 for (auto& [animationId, transition] : transition_) {
143 if (transition != nullptr) {
144 transition(transitionProperties);
145 }
146 }
147 return transitionProperties;
148 }
149
HasTransition() const150 bool RSAnimationManager::HasTransition() const
151 {
152 return !transition_.empty();
153 }
154 } // namespace Rosen
155 } // namespace OHOS
156