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