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 "common/rs_optional_trace.h"
25 #include "pipeline/rs_dirty_region_manager.h"
26 #include "pipeline/rs_paint_filter_canvas.h"
27 #include "pipeline/rs_render_node.h"
28 #include "platform/common/rs_log.h"
29
30 namespace OHOS {
31 namespace Rosen {
32 class RSRootRenderNode;
33
DumpAnimations(std::string & out) const34 void RSAnimationManager::DumpAnimations(std::string& out) const
35 {
36 if (animations_.empty()) {
37 return;
38 }
39 const auto lengthTwo = 2;
40 out += ", RSAnimationManager: [";
41 for (auto[id, animation]: animations_) {
42 if (!animation) {
43 continue;
44 }
45 animation->DumpAnimation(out);
46 out += ", ";
47 }
48 out = out.substr(0, out.length() - lengthTwo);
49 out += "]";
50 }
51
AddAnimation(const std::shared_ptr<RSRenderAnimation> & animation)52 void RSAnimationManager::AddAnimation(const std::shared_ptr<RSRenderAnimation>& animation)
53 {
54 AnimationId key = animation->GetAnimationId();
55 if (animations_.find(key) != animations_.end()) {
56 ROSEN_LOGE("RSAnimationManager::AddAnimation, The animation already exists when is added");
57 return;
58 }
59 animations_.emplace(key, animation);
60 }
61
RemoveAnimation(AnimationId keyId)62 void RSAnimationManager::RemoveAnimation(AnimationId keyId)
63 {
64 auto animationItr = animations_.find(keyId);
65 if (animationItr == animations_.end()) {
66 ROSEN_LOGE("RSAnimationManager::RemoveAnimation, The Animation does not exist when is deleted");
67 return;
68 }
69 animationItr->second->Finish();
70 animationItr->second->Detach();
71 animations_.erase(animationItr);
72 }
73
CancelAnimationByPropertyId(PropertyId id)74 void RSAnimationManager::CancelAnimationByPropertyId(PropertyId id)
75 {
76 EraseIf(animations_, [id, this](const auto& pair) {
77 if (pair.second && (pair.second->GetPropertyId() == id)) {
78 OnAnimationFinished(pair.second);
79 return true;
80 }
81 return false;
82 });
83 }
84
FilterAnimationByPid(pid_t pid)85 void RSAnimationManager::FilterAnimationByPid(pid_t pid)
86 {
87 ROSEN_LOGD("RSAnimationManager::FilterAnimationByPid removing all animations belong to pid %{public}llu",
88 (unsigned long long)pid);
89 // remove all animations belong to given pid (by matching higher 32 bits of animation id)
90 EraseIf(animations_, [pid, this](const auto& pair) -> bool {
91 if (ExtractPid(pair.first) != pid) {
92 return false;
93 }
94 pair.second->Finish();
95 pair.second->Detach();
96 return true;
97 });
98 }
99
GetAnimationsSize()100 uint32_t RSAnimationManager::GetAnimationsSize()
101 {
102 return animations_.size();
103 }
104
Animate(int64_t time,bool nodeIsOnTheTree)105 std::tuple<bool, bool, bool> RSAnimationManager::Animate(int64_t time, bool nodeIsOnTheTree)
106 {
107 // process animation
108 bool hasRunningAnimation = false;
109 bool needRequestNextVsync = false;
110 // isCalculateAnimationValue is embedded modify for stat animate frame drop
111 bool isCalculateAnimationValue = false;
112 rsRange_.Reset();
113 rateDecider_.Reset();
114 // iterate and execute all animations, remove finished animations
115 EraseIf(animations_, [this, &hasRunningAnimation, time,
116 &needRequestNextVsync, nodeIsOnTheTree, &isCalculateAnimationValue](auto& iter) -> bool {
117 auto& animation = iter.second;
118 if (!nodeIsOnTheTree && animation->GetRepeatCount() == -1) {
119 hasRunningAnimation = animation->IsRunning() || hasRunningAnimation;
120 return false;
121 }
122 bool isFinished = animation->Animate(time);
123 if (isFinished) {
124 isCalculateAnimationValue = true;
125 OnAnimationFinished(animation);
126 } else {
127 hasRunningAnimation = animation->IsRunning() || hasRunningAnimation;
128 needRequestNextVsync = animation->IsRunning() || needRequestNextVsync;
129 isCalculateAnimationValue = animation->IsCalculateAniamtionValue() || isCalculateAnimationValue;
130
131 auto range = animation->GetFrameRateRange();
132 if (range.IsValid()) {
133 rsRange_.Merge(range);
134 }
135 RS_OPTIONAL_TRACE_BEGIN("AddDecisionElement property id: [" + std::to_string(animation->GetPropertyId()) +
136 "], animation id: [" + std::to_string(animation->GetAnimationId()) + "]");
137 rateDecider_.AddDecisionElement(animation->GetPropertyId(), animation->GetAnimateVelocity(), range);
138 RS_OPTIONAL_TRACE_END();
139 }
140 return isFinished;
141 });
142 rateDecider_.MakeDecision(frameRateGetFunc_);
143 isCalculateAnimationValue = isCalculateAnimationValue && nodeIsOnTheTree;
144
145 return { hasRunningAnimation, needRequestNextVsync, isCalculateAnimationValue };
146 }
147
SetRateDeciderEnable(bool enabled,const FrameRateGetFunc & func)148 void RSAnimationManager::SetRateDeciderEnable(bool enabled, const FrameRateGetFunc& func)
149 {
150 rateDecider_.SetEnable(enabled);
151 frameRateGetFunc_ = func;
152 }
153
SetRateDeciderScaleSize(float width,float height)154 void RSAnimationManager::SetRateDeciderScaleSize(float width, float height)
155 {
156 rateDecider_.SetScaleReferenceSize(width, height);
157 }
158
GetDecideFrameRateRange() const159 const FrameRateRange& RSAnimationManager::GetDecideFrameRateRange() const
160 {
161 return rateDecider_.GetFrameRateRange();
162 }
163
GetFrameRateRange() const164 const FrameRateRange& RSAnimationManager::GetFrameRateRange() const
165 {
166 return rsRange_;
167 }
168
GetAnimation(AnimationId id) const169 const std::shared_ptr<RSRenderAnimation> RSAnimationManager::GetAnimation(AnimationId id) const
170 {
171 auto animationItr = animations_.find(id);
172 if (animationItr == animations_.end()) {
173 ROSEN_LOGD("RSAnimationManager::GetAnimation, animation [%{public}" PRIu64 "] not found", id);
174 return nullptr;
175 }
176 return animationItr->second;
177 }
178
OnAnimationFinished(const std::shared_ptr<RSRenderAnimation> & animation)179 void RSAnimationManager::OnAnimationFinished(const std::shared_ptr<RSRenderAnimation>& animation)
180 {
181 NodeId targetId = animation->GetTargetId();
182 AnimationId animationId = animation->GetAnimationId();
183
184 std::unique_ptr<RSCommand> command =
185 std::make_unique<RSAnimationCallback>(targetId, animationId, FINISHED);
186 RSMessageProcessor::Instance().AddUIMessage(ExtractPid(animationId), std::move(command));
187 animation->Detach();
188 }
189
RegisterSpringAnimation(PropertyId propertyId,AnimationId animId)190 void RSAnimationManager::RegisterSpringAnimation(PropertyId propertyId, AnimationId animId)
191 {
192 springAnimations_[propertyId] = animId;
193 }
194
UnregisterSpringAnimation(PropertyId propertyId,AnimationId animId)195 void RSAnimationManager::UnregisterSpringAnimation(PropertyId propertyId, AnimationId animId)
196 {
197 auto it = springAnimations_.find(propertyId);
198 if (it != springAnimations_.end() && it->second == animId) {
199 springAnimations_.erase(it);
200 }
201 }
202
QuerySpringAnimation(PropertyId propertyId)203 std::shared_ptr<RSRenderAnimation> RSAnimationManager::QuerySpringAnimation(PropertyId propertyId)
204 {
205 auto it = springAnimations_.find(propertyId);
206 if (it == springAnimations_.end() || it->second == 0) {
207 return nullptr;
208 }
209 return GetAnimation(it->second);
210 }
211
RegisterPathAnimation(PropertyId propertyId,AnimationId animId)212 void RSAnimationManager::RegisterPathAnimation(PropertyId propertyId, AnimationId animId)
213 {
214 pathAnimations_[propertyId] = animId;
215 }
216
UnregisterPathAnimation(PropertyId propertyId,AnimationId animId)217 void RSAnimationManager::UnregisterPathAnimation(PropertyId propertyId, AnimationId animId)
218 {
219 auto it = pathAnimations_.find(propertyId);
220 if (it != pathAnimations_.end() && it->second == animId) {
221 pathAnimations_.erase(it);
222 }
223 }
224
QueryPathAnimation(PropertyId propertyId)225 std::shared_ptr<RSRenderAnimation> RSAnimationManager::QueryPathAnimation(PropertyId propertyId)
226 {
227 auto it = pathAnimations_.find(propertyId);
228 if (it == pathAnimations_.end() || it->second == 0) {
229 return nullptr;
230 }
231 return GetAnimation(it->second);
232 }
233
RegisterParticleAnimation(PropertyId propertyId,AnimationId animId)234 void RSAnimationManager::RegisterParticleAnimation(PropertyId propertyId, AnimationId animId)
235 {
236 particleAnimations_[propertyId] = animId;
237 }
238
UnregisterParticleAnimation(PropertyId propertyId,AnimationId animId)239 void RSAnimationManager::UnregisterParticleAnimation(PropertyId propertyId, AnimationId animId)
240 {
241 auto it = particleAnimations_.find(propertyId);
242 if (it != particleAnimations_.end() && it->second == animId) {
243 particleAnimations_.erase(it);
244 }
245 }
246
GetParticleAnimations()247 const std::unordered_map<PropertyId, AnimationId>& RSAnimationManager::GetParticleAnimations()
248 {
249 return particleAnimations_;
250 }
251 } // namespace Rosen
252 } // namespace OHOS
253