• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /*
3  * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "modifier/rs_modifier_manager.h"
18 
19 #include "animation/rs_render_animation.h"
20 #include "command/rs_animation_command.h"
21 #include "command/rs_message_processor.h"
22 #include "modifier/rs_property_modifier.h"
23 #include "platform/common/rs_log.h"
24 #include "rs_trace.h"
25 
26 namespace OHOS {
27 namespace Rosen {
AddModifier(const std::shared_ptr<RSModifier> & modifier)28 void RSModifierManager::AddModifier(const std::shared_ptr<RSModifier>& modifier)
29 {
30     modifiers_.insert(modifier);
31 }
32 
Draw()33 void RSModifierManager::Draw()
34 {
35     if (modifiers_.empty()) {
36         return;
37     }
38 
39     RS_TRACE_NAME("RSModifierManager Draw num:[" + std::to_string(modifiers_.size()) + "]");
40     for (auto modifier : modifiers_) {
41         RS_TRACE_NAME("RSModifier::Draw");
42         modifier->UpdateToRender();
43         modifier->SetDirty(false);
44         modifier->ResetRSNodeExtendModifierDirty();
45     }
46     modifiers_.clear();
47 }
48 
AddAnimation(const std::shared_ptr<RSRenderAnimation> & animation)49 void RSModifierManager::AddAnimation(const std::shared_ptr<RSRenderAnimation>& animation)
50 {
51     AnimationId key = animation->GetAnimationId();
52     if (animations_.find(key) != animations_.end()) {
53         ROSEN_LOGE("RSModifierManager::AddAnimation, The animation already exists when is added");
54         return;
55     }
56     animations_.emplace(key, animation);
57 
58     std::shared_ptr<RSRenderDisplaySync> displaySync = std::make_shared<RSRenderDisplaySync>(animation);
59     displaySync->SetExpectedFrameRateRange(animation->GetFrameRateRange());
60     displaySyncs_.emplace(key, displaySync);
61 }
62 
RemoveAnimation(AnimationId keyId)63 void RSModifierManager::RemoveAnimation(AnimationId keyId)
64 {
65     auto animationItr = animations_.find(keyId);
66     if (animationItr == animations_.end()) {
67         ROSEN_LOGE("RSModifierManager::RemoveAnimation, The Animation does not exist when is deleted");
68         return;
69     }
70     animations_.erase(animationItr);
71     displaySyncs_.erase(keyId);
72 }
73 
HasUIAnimation()74 bool RSModifierManager::HasUIAnimation()
75 {
76     return !animations_.empty();
77 }
78 
Animate(int64_t time,int64_t vsyncPeriod)79 bool RSModifierManager::Animate(int64_t time, int64_t vsyncPeriod)
80 {
81     RS_TRACE_NAME("RunningCustomAnimation num:[" + std::to_string(animations_.size()) + "]");
82     // process animation
83     bool hasRunningAnimation = false;
84     rateDecider_.Reset();
85 
86     // iterate and execute all animations, remove finished animations
87     EraseIf(animations_, [this, &hasRunningAnimation, time, vsyncPeriod](auto& iter) -> bool {
88         auto animation = iter.second.lock();
89         if (animation == nullptr) {
90             displaySyncs_.erase(iter.first);
91             return true;
92         }
93 
94         bool isFinished = false;
95         AnimationId animId = animation->GetAnimationId();
96         if (!JudgeAnimateWhetherSkip(animId, time, vsyncPeriod)) {
97             isFinished = animation->Animate(time);
98         }
99 
100         if (isFinished) {
101             OnAnimationFinished(animation);
102         } else {
103             hasRunningAnimation = animation->IsRunning() || hasRunningAnimation;
104             rateDecider_.AddDecisionElement(animation->GetPropertyId(),
105                 animation->GetAnimateVelocity(), animation->GetFrameRateRange());
106         }
107         return isFinished;
108     });
109     rateDecider_.MakeDecision(frameRateGetFunc_);
110 
111     return hasRunningAnimation;
112 }
113 
FlushStartAnimation(int64_t time)114 void RSModifierManager::FlushStartAnimation(int64_t time)
115 {
116     for (auto& iter : animations_) {
117         auto animation = iter.second.lock();
118         if (animation && animation->GetNeedUpdateStartTime()) {
119             animation->SetStartTime(time);
120         }
121     }
122 }
123 
JudgeAnimateWhetherSkip(AnimationId animId,int64_t time,int64_t vsyncPeriod)124 bool RSModifierManager::JudgeAnimateWhetherSkip(AnimationId animId, int64_t time, int64_t vsyncPeriod)
125 {
126     bool isSkip = false;
127     if (!displaySyncs_.count(animId)) {
128         return isSkip;
129     }
130 
131     auto displaySync = displaySyncs_[animId];
132     if (displaySync) {
133         isSkip = displaySync->OnFrameSkip(time, vsyncPeriod, IsDisplaySyncEnabled());
134     }
135 
136     return isSkip;
137 }
138 
SetFrameRateGetFunc(const FrameRateGetFunc & func)139 void RSModifierManager::SetFrameRateGetFunc(const FrameRateGetFunc& func)
140 {
141     frameRateGetFunc_ = func;
142 }
143 
GetFrameRateRange() const144 const FrameRateRange& RSModifierManager::GetFrameRateRange() const
145 {
146     return rateDecider_.GetFrameRateRange();
147 }
148 
OnAnimationFinished(const std::shared_ptr<RSRenderAnimation> & animation)149 void RSModifierManager::OnAnimationFinished(const std::shared_ptr<RSRenderAnimation>& animation)
150 {
151     NodeId targetId = animation->GetTargetId();
152     AnimationId animationId = animation->GetAnimationId();
153     displaySyncs_.erase(animationId);
154 
155     std::unique_ptr<RSCommand> command = std::make_unique<RSAnimationCallback>(targetId, animationId, FINISHED);
156     RSMessageProcessor::Instance().AddUIMessage(ExtractPid(animationId), command);
157 
158     animation->Detach();
159 }
160 
RegisterSpringAnimation(PropertyId propertyId,AnimationId animId)161 void RSModifierManager::RegisterSpringAnimation(PropertyId propertyId, AnimationId animId)
162 {
163     springAnimations_[propertyId] = animId;
164 }
165 
UnregisterSpringAnimation(PropertyId propertyId,AnimationId animId)166 void RSModifierManager::UnregisterSpringAnimation(PropertyId propertyId, AnimationId animId)
167 {
168     auto it = springAnimations_.find(propertyId);
169     if (it != springAnimations_.end() && it->second == animId) {
170         springAnimations_.erase(it);
171     }
172 }
173 
QuerySpringAnimation(PropertyId propertyId)174 std::shared_ptr<RSRenderAnimation> RSModifierManager::QuerySpringAnimation(PropertyId propertyId)
175 {
176     auto it = springAnimations_.find(propertyId);
177     if (it == springAnimations_.end() || it->second == 0) {
178         ROSEN_LOGD("RSModifierManager::QuerySpringAnimation: there is no spring animation on the current property.");
179         return nullptr;
180     }
181     return GetAnimation(it->second);
182 }
183 
GetAnimation(AnimationId id) const184 const std::shared_ptr<RSRenderAnimation> RSModifierManager::GetAnimation(AnimationId id) const
185 {
186     auto animationItr = animations_.find(id);
187     if (animationItr == animations_.end()) {
188         ROSEN_LOGD("RSModifierManager::GetAnimation, animation [%{public}" PRIu64 "] not found", id);
189         return nullptr;
190     }
191     return animationItr->second.lock();
192 }
193 
SetDisplaySyncEnable(bool isDisplaySyncEnabled)194 void RSModifierManager::SetDisplaySyncEnable(bool isDisplaySyncEnabled)
195 {
196     isDisplaySyncEnabled_ = isDisplaySyncEnabled;
197 }
198 
IsDisplaySyncEnabled() const199 bool RSModifierManager::IsDisplaySyncEnabled() const
200 {
201     return isDisplaySyncEnabled_;
202 }
203 } // namespace Rosen
204 } // namespace OHOS
205