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