• 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_implicit_animation_param.h"
17 
18 #include "animation/rs_curve_animation.h"
19 #include "animation/rs_interpolating_spring_animation.h"
20 #include "animation/rs_keyframe_animation.h"
21 #include "animation/rs_motion_path_option.h"
22 #include "animation/rs_path_animation.h"
23 #include "animation/rs_spring_animation.h"
24 #include "animation/rs_transition.h"
25 #include "platform/common/rs_log.h"
26 #include "command/rs_node_showing_command.h"
27 
28 namespace OHOS {
29 namespace Rosen {
RSImplicitAnimationParam(const RSAnimationTimingProtocol & timingProtocol,ImplicitAnimationParamType type)30 RSImplicitAnimationParam::RSImplicitAnimationParam(
31     const RSAnimationTimingProtocol& timingProtocol, ImplicitAnimationParamType type)
32     : animationType_(type), timingProtocol_(timingProtocol)
33 {}
34 
GetType() const35 ImplicitAnimationParamType RSImplicitAnimationParam::GetType() const
36 {
37     return animationType_;
38 }
39 
ApplyTimingProtocol(const std::shared_ptr<RSAnimation> & animation) const40 void RSImplicitAnimationParam::ApplyTimingProtocol(const std::shared_ptr<RSAnimation>& animation) const
41 {
42     animation->SetDuration(timingProtocol_.GetDuration());
43     animation->SetStartDelay(timingProtocol_.GetStartDelay());
44     animation->SetSpeed(timingProtocol_.GetSpeed());
45     animation->SetDirection(timingProtocol_.GetDirection());
46     animation->SetAutoReverse(timingProtocol_.GetAutoReverse());
47     animation->SetRepeatCount(timingProtocol_.GetRepeatCount());
48     animation->SetFillMode(timingProtocol_.GetFillMode());
49     auto range = timingProtocol_.GetFrameRateRange();
50     // Transfer frame rate and component informations
51     if (range.IsValid() || range.componentScene_ != ComponentScene::UNKNOWN_SCENE) {
52         animation->SetFrameRateRange(range);
53     }
54 }
55 
RSImplicitCancelAnimationParam(const RSAnimationTimingProtocol & timingProtocol)56 RSImplicitCancelAnimationParam::RSImplicitCancelAnimationParam(const RSAnimationTimingProtocol& timingProtocol)
57     : RSImplicitAnimationParam(timingProtocol, ImplicitAnimationParamType::CANCEL)
58 {}
59 
AddPropertyToPendingSyncList(const std::shared_ptr<RSPropertyBase> & property)60 void RSImplicitCancelAnimationParam::AddPropertyToPendingSyncList(const std::shared_ptr<RSPropertyBase>& property)
61 {
62     pendingSyncList_.emplace_back(property);
63 }
64 
SyncProperties(const std::shared_ptr<RSUIContext> & rsUIContext)65 CancelAnimationStatus RSImplicitCancelAnimationParam::SyncProperties(const std::shared_ptr<RSUIContext>& rsUIContext)
66 {
67     if (pendingSyncList_.empty()) {
68         return CancelAnimationStatus::EMPTY_PENDING_SYNC_LIST;
69     }
70 
71     // Create sync map
72     RSNodeGetShowingPropertiesAndCancelAnimation::PropertiesMap renderServicePropertiesMap;
73     RSNodeGetShowingPropertiesAndCancelAnimation::PropertiesMap renderThreadPropertiesMap;
74     for (auto& rsProperty : pendingSyncList_) {
75         auto node = rsProperty->target_.lock();
76         if (node == nullptr) {
77             continue;
78         }
79         if (!node->HasPropertyAnimation(rsProperty->GetId()) || rsProperty->GetIsCustom()) {
80             continue;
81         }
82         auto& propertiesMap = node->IsRenderServiceNode() ? renderServicePropertiesMap : renderThreadPropertiesMap;
83         propertiesMap.emplace(std::make_pair<NodeId, PropertyId>(node->GetId(), rsProperty->GetId()),
84             std::make_pair<std::shared_ptr<RSRenderPropertyBase>, std::vector<AnimationId>>(
85                 nullptr, node->GetAnimationByPropertyId(rsProperty->GetId())));
86     }
87     pendingSyncList_.clear();
88 
89     bool ret = true;
90     if (!renderServicePropertiesMap.empty()) {
91         ret = ret && ExecuteSyncPropertiesTask(std::move(renderServicePropertiesMap), true, rsUIContext);
92     }
93     if (!renderThreadPropertiesMap.empty()) {
94         ret = ret && ExecuteSyncPropertiesTask(std::move(renderThreadPropertiesMap), false, rsUIContext);
95     }
96     return ret ? CancelAnimationStatus::SUCCESS : CancelAnimationStatus::TASK_EXECUTION_FAILURE;
97 }
98 
ExecuteSyncPropertiesTask(RSNodeGetShowingPropertiesAndCancelAnimation::PropertiesMap && propertiesMap,bool isRenderService,const std::shared_ptr<RSUIContext> & rsUIContext)99 bool RSImplicitCancelAnimationParam::ExecuteSyncPropertiesTask(
100     RSNodeGetShowingPropertiesAndCancelAnimation::PropertiesMap&& propertiesMap, bool isRenderService,
101     const std::shared_ptr<RSUIContext>& rsUIContext)
102 {
103     // create task and execute it in RS (timeout is 400ms)
104     auto task = std::make_shared<RSNodeGetShowingPropertiesAndCancelAnimation>(4e8, std::move(propertiesMap));
105     if (rsUIContext == nullptr) {
106         RSTransactionProxy::GetInstance()->ExecuteSynchronousTask(task, isRenderService);
107     } else {
108         auto transaction = rsUIContext->GetRSTransaction();
109         if (transaction != nullptr) {
110             transaction->ExecuteSynchronousTask(task, isRenderService);
111         }
112     }
113     // Test if the task is executed successfully
114     if (!task || !task->IsSuccess()) {
115         ROSEN_LOGE("RSImplicitCancelAnimationParam::ExecuteSyncPropertiesTask failed to execute task.");
116         return false;
117     }
118 
119     // Apply task result
120     for (const auto& [key, value] : task->GetProperties()) {
121         const auto& [nodeId, propertyId] = key;
122         auto node = rsUIContext ? rsUIContext->GetNodeMap().GetNode(nodeId) : RSNodeMap::Instance().GetNode(nodeId);
123         if (node == nullptr) {
124             ROSEN_LOGE("RSImplicitCancelAnimationParam::ExecuteSyncPropertiesTask failed to get target node.");
125             continue;
126         }
127         std::shared_ptr<RSPropertyBase> property = nullptr;
128         if (auto prop = node->GetPropertyById(propertyId)) {
129             property = prop;
130         }
131         if (property == nullptr) {
132             ROSEN_LOGE("RSImplicitCancelAnimationParam::ExecuteSyncPropertiesTask failed to get target property.");
133             continue;
134         }
135         node->CancelAnimationByProperty(propertyId, !property->GetIsCustom());
136         const auto& [propertyValue, animations] = value;
137         if (propertyValue != nullptr) {
138             // successfully canceled RS animation and extract value, update ui value
139             property->SetValueFromRender(propertyValue);
140         } else {
141             // property or node is not yet created in RS, just trigger a force update
142             property->UpdateOnAllAnimationFinish();
143         }
144     }
145     return true;
146 }
147 
CreateEmptyAnimation(std::shared_ptr<RSPropertyBase> property,const std::shared_ptr<RSPropertyBase> & startValue,const std::shared_ptr<RSPropertyBase> & endValue) const148 std::shared_ptr<RSAnimation> RSImplicitCancelAnimationParam::CreateEmptyAnimation(
149     std::shared_ptr<RSPropertyBase> property, const std::shared_ptr<RSPropertyBase>& startValue,
150     const std::shared_ptr<RSPropertyBase>& endValue) const
151 {
152     auto curveAnimation = std::make_shared<RSCurveAnimation>(property, endValue - startValue);
153     curveAnimation->SetDuration(0);
154     return curveAnimation;
155 }
156 
RSImplicitCurveAnimationParam(const RSAnimationTimingProtocol & timingProtocol,const RSAnimationTimingCurve & timingCurve)157 RSImplicitCurveAnimationParam::RSImplicitCurveAnimationParam(
158     const RSAnimationTimingProtocol& timingProtocol, const RSAnimationTimingCurve& timingCurve)
159     : RSImplicitAnimationParam(timingProtocol, ImplicitAnimationParamType::CURVE), timingCurve_(timingCurve)
160 {}
161 
CreateAnimation(std::shared_ptr<RSPropertyBase> property,const std::shared_ptr<RSPropertyBase> & startValue,const std::shared_ptr<RSPropertyBase> & endValue) const162 std::shared_ptr<RSAnimation> RSImplicitCurveAnimationParam::CreateAnimation(std::shared_ptr<RSPropertyBase> property,
163     const std::shared_ptr<RSPropertyBase>& startValue, const std::shared_ptr<RSPropertyBase>& endValue) const
164 {
165     auto curveAnimation = std::make_shared<RSCurveAnimation>(property, endValue - startValue);
166     curveAnimation->SetTimingCurve(timingCurve_);
167     curveAnimation->SetIsCustom(property->GetIsCustom());
168     ApplyTimingProtocol(curveAnimation);
169     return curveAnimation;
170 }
171 
RSImplicitKeyframeAnimationParam(const RSAnimationTimingProtocol & timingProtocol,const RSAnimationTimingCurve & timingCurve,float fraction,int duration)172 RSImplicitKeyframeAnimationParam::RSImplicitKeyframeAnimationParam(
173     const RSAnimationTimingProtocol& timingProtocol, const RSAnimationTimingCurve& timingCurve,
174     float fraction, int duration)
175     : RSImplicitAnimationParam(timingProtocol, ImplicitAnimationParamType::KEYFRAME), timingCurve_(timingCurve),
176       fraction_(fraction), duration_(duration)
177 {}
178 
CreateAnimation(std::shared_ptr<RSPropertyBase> property,const bool & isCreateDurationKeyframe,const int & startDuration,const std::shared_ptr<RSPropertyBase> & startValue,const std::shared_ptr<RSPropertyBase> & endValue) const179 std::shared_ptr<RSAnimation> RSImplicitKeyframeAnimationParam::CreateAnimation(
180     std::shared_ptr<RSPropertyBase> property, const bool& isCreateDurationKeyframe, const int& startDuration,
181     const std::shared_ptr<RSPropertyBase>& startValue, const std::shared_ptr<RSPropertyBase>& endValue) const
182 {
183     auto keyFrameAnimation = std::make_shared<RSKeyframeAnimation>(property);
184     keyFrameAnimation->SetDurationKeyframe(isCreateDurationKeyframe);
185     if (isCreateDurationKeyframe) {
186         if (startDuration > INT32_MAX - duration_) {
187             ROSEN_LOGD("RSImplicitKeyframeAnimationParam::AddKeyframe, duration overflow!");
188             keyFrameAnimation->AddKeyFrame(startDuration, INT32_MAX, endValue, timingCurve_);
189         } else {
190             keyFrameAnimation->AddKeyFrame(startDuration, startDuration + duration_, endValue, timingCurve_);
191         }
192     } else {
193         keyFrameAnimation->AddKeyFrame(fraction_, endValue, timingCurve_);
194     }
195     keyFrameAnimation->SetOriginValue(startValue);
196     keyFrameAnimation->SetIsCustom(property->GetIsCustom());
197     ApplyTimingProtocol(keyFrameAnimation);
198     return keyFrameAnimation;
199 }
200 
AddKeyframe(std::shared_ptr<RSAnimation> & animation,const std::shared_ptr<RSPropertyBase> & startValue,const std::shared_ptr<RSPropertyBase> & endValue) const201 void RSImplicitKeyframeAnimationParam::AddKeyframe(std::shared_ptr<RSAnimation>& animation,
202     const std::shared_ptr<RSPropertyBase>& startValue, const std::shared_ptr<RSPropertyBase>& endValue) const
203 {
204     if (animation == nullptr) {
205         return;
206     }
207 
208     auto keyframeAnimation = std::static_pointer_cast<RSKeyframeAnimation>(animation);
209     if (keyframeAnimation != nullptr) {
210         keyframeAnimation->AddKeyFrame(fraction_, endValue, timingCurve_);
211     }
212 }
213 
AddKeyframe(std::shared_ptr<RSAnimation> & animation,const int startDuration,const std::shared_ptr<RSPropertyBase> & startValue,const std::shared_ptr<RSPropertyBase> & endValue) const214 void RSImplicitKeyframeAnimationParam::AddKeyframe(std::shared_ptr<RSAnimation>& animation, const int startDuration,
215     const std::shared_ptr<RSPropertyBase>& startValue, const std::shared_ptr<RSPropertyBase>& endValue) const
216 {
217     if (animation == nullptr) {
218         return;
219     }
220 
221     auto keyframeAnimation = std::static_pointer_cast<RSKeyframeAnimation>(animation);
222     if (keyframeAnimation != nullptr) {
223         if (startDuration > INT32_MAX - duration_) {
224             ROSEN_LOGD("RSImplicitKeyframeAnimationParam::AddKeyframe, duration overflow!");
225             keyframeAnimation->AddKeyFrame(startDuration, INT32_MAX, endValue, timingCurve_);
226         } else {
227             keyframeAnimation->AddKeyFrame(startDuration, startDuration + duration_, endValue, timingCurve_);
228         }
229     }
230 }
231 
RSImplicitPathAnimationParam(const RSAnimationTimingProtocol & timingProtocol,const RSAnimationTimingCurve & timingCurve,const std::shared_ptr<RSMotionPathOption> & motionPathOption)232 RSImplicitPathAnimationParam::RSImplicitPathAnimationParam(const RSAnimationTimingProtocol& timingProtocol,
233     const RSAnimationTimingCurve& timingCurve, const std::shared_ptr<RSMotionPathOption>& motionPathOption)
234     : RSImplicitAnimationParam(timingProtocol, ImplicitAnimationParamType::PATH), timingCurve_(timingCurve),
235       motionPathOption_(motionPathOption)
236 {}
237 
CreateAnimation(std::shared_ptr<RSPropertyBase> property,const std::shared_ptr<RSPropertyBase> & startValue,const std::shared_ptr<RSPropertyBase> & endValue) const238 std::shared_ptr<RSAnimation> RSImplicitPathAnimationParam::CreateAnimation(std::shared_ptr<RSPropertyBase> property,
239     const std::shared_ptr<RSPropertyBase>& startValue, const std::shared_ptr<RSPropertyBase>& endValue) const
240 {
241     if (motionPathOption_ == nullptr) {
242         ROSEN_LOGE("Failed to create path animation, motion path option is null!");
243         return nullptr;
244     }
245 
246     auto pathAnimation =
247         std::make_shared<RSPathAnimation>(property, motionPathOption_->GetPath(), startValue, endValue);
248     pathAnimation->SetBeginFraction(motionPathOption_->GetBeginFraction());
249     pathAnimation->SetEndFraction(motionPathOption_->GetEndFraction());
250     pathAnimation->SetRotationMode(motionPathOption_->GetRotationMode());
251     pathAnimation->SetPathNeedAddOrigin(motionPathOption_->GetPathNeedAddOrigin());
252     pathAnimation->SetTimingCurve(timingCurve_);
253     ApplyTimingProtocol(pathAnimation);
254     return pathAnimation;
255 }
256 
RSImplicitSpringAnimationParam(const RSAnimationTimingProtocol & timingProtocol,const RSAnimationTimingCurve & timingCurve)257 RSImplicitSpringAnimationParam::RSImplicitSpringAnimationParam(
258     const RSAnimationTimingProtocol& timingProtocol, const RSAnimationTimingCurve& timingCurve)
259     : RSImplicitAnimationParam(timingProtocol, ImplicitAnimationParamType::SPRING), timingCurve_(timingCurve)
260 {}
261 
CreateAnimation(std::shared_ptr<RSPropertyBase> property,const std::shared_ptr<RSPropertyBase> & startValue,const std::shared_ptr<RSPropertyBase> & endValue) const262 std::shared_ptr<RSAnimation> RSImplicitSpringAnimationParam::CreateAnimation(std::shared_ptr<RSPropertyBase> property,
263     const std::shared_ptr<RSPropertyBase>& startValue, const std::shared_ptr<RSPropertyBase>& endValue) const
264 {
265     auto springAnimation = std::make_shared<RSSpringAnimation>(property, startValue, endValue);
266     springAnimation->SetTimingCurve(timingCurve_);
267     springAnimation->SetIsCustom(property->GetIsCustom());
268     ApplyTimingProtocol(springAnimation);
269     return springAnimation;
270 }
271 
RSImplicitInterpolatingSpringAnimationParam(const RSAnimationTimingProtocol & timingProtocol,const RSAnimationTimingCurve & timingCurve)272 RSImplicitInterpolatingSpringAnimationParam::RSImplicitInterpolatingSpringAnimationParam(
273     const RSAnimationTimingProtocol& timingProtocol, const RSAnimationTimingCurve& timingCurve)
274     : RSImplicitAnimationParam(timingProtocol, ImplicitAnimationParamType::INTERPOLATING_SPRING),
275       timingCurve_(timingCurve)
276 {}
277 
CreateAnimation(std::shared_ptr<RSPropertyBase> property,const std::shared_ptr<RSPropertyBase> & startValue,const std::shared_ptr<RSPropertyBase> & endValue) const278 std::shared_ptr<RSAnimation> RSImplicitInterpolatingSpringAnimationParam::CreateAnimation(
279     std::shared_ptr<RSPropertyBase> property, const std::shared_ptr<RSPropertyBase>& startValue,
280     const std::shared_ptr<RSPropertyBase>& endValue) const
281 {
282     auto interpolatingSpringAnimation =
283         std::make_shared<RSInterpolatingSpringAnimation>(property, startValue, endValue);
284     interpolatingSpringAnimation->SetTimingCurve(timingCurve_);
285     interpolatingSpringAnimation->SetIsCustom(property->GetIsCustom());
286     ApplyTimingProtocol(interpolatingSpringAnimation);
287     return interpolatingSpringAnimation;
288 }
289 
RSImplicitTransitionParam(const RSAnimationTimingProtocol & timingProtocol,const RSAnimationTimingCurve & timingCurve,const std::shared_ptr<const RSTransitionEffect> & effect,bool isTransitionIn)290 RSImplicitTransitionParam::RSImplicitTransitionParam(const RSAnimationTimingProtocol& timingProtocol,
291     const RSAnimationTimingCurve& timingCurve, const std::shared_ptr<const RSTransitionEffect>& effect,
292     bool isTransitionIn)
293     : RSImplicitAnimationParam(timingProtocol, ImplicitAnimationParamType::TRANSITION), timingCurve_(timingCurve),
294       isTransitionIn_(isTransitionIn), effect_(effect)
295 {}
296 
CreateAnimation()297 std::shared_ptr<RSAnimation> RSImplicitTransitionParam::CreateAnimation()
298 {
299     if (transition_ == nullptr) {
300         transition_ = std::make_shared<RSTransition>(effect_, isTransitionIn_);
301         transition_->SetTimingCurve(timingCurve_);
302         ApplyTimingProtocol(transition_);
303     }
304     return transition_;
305 }
306 
CreateAnimation(const std::shared_ptr<RSPropertyBase> & property,const std::shared_ptr<RSPropertyBase> & startValue,const std::shared_ptr<RSPropertyBase> & endValue)307 std::shared_ptr<RSAnimation> RSImplicitTransitionParam::CreateAnimation(const std::shared_ptr<RSPropertyBase>& property,
308     const std::shared_ptr<RSPropertyBase>& startValue, const std::shared_ptr<RSPropertyBase>& endValue)
309 {
310     if (transition_ == nullptr) {
311         transition_ = std::make_shared<RSTransition>(effect_, isTransitionIn_);
312         transition_->SetTimingCurve(timingCurve_);
313         transition_->SetIsCustom(true);
314         ApplyTimingProtocol(transition_);
315     }
316     return transition_;
317 }
318 } // namespace Rosen
319 } // namespace OHOS
320