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 "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
29 namespace OHOS {
30 namespace Rosen {
31 class RSRootRenderNode;
32
AddAnimation(const std::shared_ptr<RSRenderAnimation> & animation)33 void RSAnimationManager::AddAnimation(const std::shared_ptr<RSRenderAnimation>& animation)
34 {
35 AnimationId key = animation->GetAnimationId();
36 if (animations_.find(key) != animations_.end()) {
37 ROSEN_LOGE("RSAnimationManager::AddAnimation, The animation already exists when is added");
38 return;
39 }
40 animations_.emplace(key, animation);
41 OnAnimationAdd(animation);
42 }
43
RemoveAnimation(AnimationId keyId)44 void RSAnimationManager::RemoveAnimation(AnimationId keyId)
45 {
46 auto animationItr = animations_.find(keyId);
47 if (animationItr == animations_.end()) {
48 ROSEN_LOGE("RSAnimationManager::RemoveAnimation, The Animation does not exist when is deleted");
49 return;
50 }
51 animationItr->second->Finish();
52 animationItr->second->Detach();
53 OnAnimationRemove(animationItr->second);
54 animations_.erase(animationItr);
55 }
56
FilterAnimationByPid(pid_t pid)57 void RSAnimationManager::FilterAnimationByPid(pid_t pid)
58 {
59 ROSEN_LOGI("RSAnimationManager::FilterAnimationByPid removing all animations belong to pid %d", pid);
60 // remove all animations belong to given pid (by matching higher 32 bits of animation id)
61 EraseIf(animations_, [pid, this](const auto& pair) -> bool {
62 if (static_cast<pid_t>(pair.first >> 32) != pid) {
63 return false;
64 }
65 pair.second->Finish();
66 pair.second->Detach();
67 OnAnimationRemove(pair.second);
68 return true;
69 });
70 }
71
Animate(int64_t time,bool nodeIsOnTheTree)72 std::pair<bool, bool> RSAnimationManager::Animate(int64_t time, bool nodeIsOnTheTree)
73 {
74 // process animation
75 bool hasRunningAnimation = false;
76 bool needRequestNextVsync = false;
77 // iterate and execute all animations, remove finished animations
78 EraseIf(animations_, [this, &hasRunningAnimation, time,
79 &needRequestNextVsync, nodeIsOnTheTree](auto& iter) -> bool {
80 auto& animation = iter.second;
81 if (!nodeIsOnTheTree && animation->GetRepeatCount() == -1) {
82 hasRunningAnimation = animation->IsRunning() || hasRunningAnimation;
83 return false;
84 }
85 bool isFinished = animation->Animate(time);
86 if (isFinished) {
87 OnAnimationFinished(animation);
88 } else {
89 hasRunningAnimation = animation->IsRunning() || hasRunningAnimation;
90 needRequestNextVsync = animation->IsRunning() || needRequestNextVsync;
91 }
92 return isFinished;
93 });
94
95 return { hasRunningAnimation, needRequestNextVsync };
96 }
97
GetAnimation(AnimationId id) const98 const std::shared_ptr<RSRenderAnimation> RSAnimationManager::GetAnimation(AnimationId id) const
99 {
100 auto animationItr = animations_.find(id);
101 if (animationItr == animations_.end()) {
102 ROSEN_LOGE("RSAnimationManager::GetAnimation, animation [%" PRIu64 "] not found", id);
103 return nullptr;
104 }
105 return animationItr->second;
106 }
107
OnAnimationRemove(const std::shared_ptr<RSRenderAnimation> & animation)108 void RSAnimationManager::OnAnimationRemove(const std::shared_ptr<RSRenderAnimation>& animation)
109 {
110 animationNum_[animation->GetPropertyId()]--;
111 }
112
OnAnimationAdd(const std::shared_ptr<RSRenderAnimation> & animation)113 void RSAnimationManager::OnAnimationAdd(const std::shared_ptr<RSRenderAnimation>& animation)
114 {
115 animationNum_[animation->GetPropertyId()]++;
116 }
117
OnAnimationFinished(const std::shared_ptr<RSRenderAnimation> & animation)118 void RSAnimationManager::OnAnimationFinished(const std::shared_ptr<RSRenderAnimation>& animation)
119 {
120 NodeId targetId = animation->GetTargetId();
121 AnimationId animationId = animation->GetAnimationId();
122
123 std::unique_ptr<RSCommand> command =
124 std::make_unique<RSAnimationFinishCallback>(targetId, animationId);
125 RSMessageProcessor::Instance().AddUIMessage(ExtractPid(animationId), command);
126 OnAnimationRemove(animation);
127 animation->Detach();
128 }
129
RegisterSpringAnimation(PropertyId propertyId,AnimationId animId)130 void RSAnimationManager::RegisterSpringAnimation(PropertyId propertyId, AnimationId animId)
131 {
132 springAnimations_[propertyId] = animId;
133 }
134
UnregisterSpringAnimation(PropertyId propertyId,AnimationId animId)135 void RSAnimationManager::UnregisterSpringAnimation(PropertyId propertyId, AnimationId animId)
136 {
137 auto it = springAnimations_.find(propertyId);
138 if (it != springAnimations_.end() && it->second == animId) {
139 springAnimations_.erase(it);
140 }
141 }
142
QuerySpringAnimation(PropertyId propertyId)143 std::shared_ptr<RSRenderAnimation> RSAnimationManager::QuerySpringAnimation(PropertyId propertyId)
144 {
145 auto it = springAnimations_.find(propertyId);
146 if (it == springAnimations_.end() || it->second == 0) {
147 return nullptr;
148 }
149 return GetAnimation(it->second);
150 }
151 } // namespace Rosen
152 } // namespace OHOS
153