• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
CancelAnimationByPropertyId(PropertyId id)55 void RSAnimationManager::CancelAnimationByPropertyId(PropertyId id)
56 {
57     EraseIf(animations_, [id](const auto& pair) { return (pair.second && (pair.second->GetPropertyId() == id)); });
58 }
59 
FilterAnimationByPid(pid_t pid)60 void RSAnimationManager::FilterAnimationByPid(pid_t pid)
61 {
62     ROSEN_LOGI("RSAnimationManager::FilterAnimationByPid removing all animations belong to pid %d", pid);
63     // remove all animations belong to given pid (by matching higher 32 bits of animation id)
64     EraseIf(animations_, [pid, this](const auto& pair) -> bool {
65         if (ExtractPid(pair.first) != pid) {
66             return false;
67         }
68         pair.second->Finish();
69         pair.second->Detach();
70         return true;
71     });
72 }
73 
Animate(int64_t time,bool nodeIsOnTheTree)74 std::pair<bool, bool> RSAnimationManager::Animate(int64_t time, bool nodeIsOnTheTree)
75 {
76     // process animation
77     bool hasRunningAnimation = false;
78     bool needRequestNextVsync = false;
79 
80     rsRange_.Reset();
81     // iterate and execute all animations, remove finished animations
82     EraseIf(animations_, [this, &hasRunningAnimation, time,
83         &needRequestNextVsync, nodeIsOnTheTree](auto& iter) -> bool {
84         auto& animation = iter.second;
85         if (!nodeIsOnTheTree && animation->GetRepeatCount() == -1) {
86             hasRunningAnimation = animation->IsRunning() || hasRunningAnimation;
87             return false;
88         }
89         bool isFinished = animation->Animate(time);
90         if (isFinished) {
91             OnAnimationFinished(animation);
92         } else {
93             hasRunningAnimation = animation->IsRunning() || hasRunningAnimation;
94             needRequestNextVsync = animation->IsRunning() || needRequestNextVsync;
95 
96             auto range = animation->GetFrameRateRange();
97             if (range.IsValid()) {
98                 rsRange_.Merge(range);
99             }
100         }
101         return isFinished;
102     });
103 
104     return { hasRunningAnimation, needRequestNextVsync };
105 }
106 
GetFrameRateRangeFromRSAnimations()107 FrameRateRange RSAnimationManager::GetFrameRateRangeFromRSAnimations()
108 {
109     return rsRange_;
110 }
111 
GetAnimation(AnimationId id) const112 const std::shared_ptr<RSRenderAnimation> RSAnimationManager::GetAnimation(AnimationId id) const
113 {
114     auto animationItr = animations_.find(id);
115     if (animationItr == animations_.end()) {
116         ROSEN_LOGE("RSAnimationManager::GetAnimation, animation [%" PRIu64 "] not found", id);
117         return nullptr;
118     }
119     return animationItr->second;
120 }
121 
OnAnimationFinished(const std::shared_ptr<RSRenderAnimation> & animation)122 void RSAnimationManager::OnAnimationFinished(const std::shared_ptr<RSRenderAnimation>& animation)
123 {
124     NodeId targetId = animation->GetTargetId();
125     AnimationId animationId = animation->GetAnimationId();
126 
127     std::unique_ptr<RSCommand> command =
128         std::make_unique<RSAnimationCallback>(targetId, animationId, FINISHED);
129     RSMessageProcessor::Instance().AddUIMessage(ExtractPid(animationId), std::move(command));
130     animation->Detach();
131 }
132 
RegisterSpringAnimation(PropertyId propertyId,AnimationId animId)133 void RSAnimationManager::RegisterSpringAnimation(PropertyId propertyId, AnimationId animId)
134 {
135     springAnimations_[propertyId] = animId;
136 }
137 
UnregisterSpringAnimation(PropertyId propertyId,AnimationId animId)138 void RSAnimationManager::UnregisterSpringAnimation(PropertyId propertyId, AnimationId animId)
139 {
140     auto it = springAnimations_.find(propertyId);
141     if (it != springAnimations_.end() && it->second == animId) {
142         springAnimations_.erase(it);
143     }
144 }
145 
QuerySpringAnimation(PropertyId propertyId)146 std::shared_ptr<RSRenderAnimation> RSAnimationManager::QuerySpringAnimation(PropertyId propertyId)
147 {
148     auto it = springAnimations_.find(propertyId);
149     if (it == springAnimations_.end() || it->second == 0) {
150         return nullptr;
151     }
152     return GetAnimation(it->second);
153 }
154 
RegisterPathAnimation(PropertyId propertyId,AnimationId animId)155 void RSAnimationManager::RegisterPathAnimation(PropertyId propertyId, AnimationId animId)
156 {
157     pathAnimations_[propertyId] = animId;
158 }
159 
UnregisterPathAnimation(PropertyId propertyId,AnimationId animId)160 void RSAnimationManager::UnregisterPathAnimation(PropertyId propertyId, AnimationId animId)
161 {
162     auto it = pathAnimations_.find(propertyId);
163     if (it != pathAnimations_.end() && it->second == animId) {
164         pathAnimations_.erase(it);
165     }
166 }
167 
QueryPathAnimation(PropertyId propertyId)168 std::shared_ptr<RSRenderAnimation> RSAnimationManager::QueryPathAnimation(PropertyId propertyId)
169 {
170     auto it = pathAnimations_.find(propertyId);
171     if (it == pathAnimations_.end() || it->second == 0) {
172         return nullptr;
173     }
174     return GetAnimation(it->second);
175 }
176 
RegisterParticleAnimation(PropertyId propertyId,AnimationId animId)177 void RSAnimationManager::RegisterParticleAnimation(PropertyId propertyId, AnimationId animId)
178 {
179     particleAnimations_[propertyId] = animId;
180 }
181 
UnregisterParticleAnimation(PropertyId propertyId,AnimationId animId)182 void RSAnimationManager::UnregisterParticleAnimation(PropertyId propertyId, AnimationId animId)
183 {
184     auto it = particleAnimations_.find(propertyId);
185     if (it != particleAnimations_.end() && it->second == animId) {
186         particleAnimations_.erase(it);
187     }
188 }
189 
GetParticleAnimations()190 std::unordered_map<PropertyId, AnimationId> RSAnimationManager::GetParticleAnimations()
191 {
192     return particleAnimations_;
193 }
194 } // namespace Rosen
195 } // namespace OHOS
196