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     if (range.IsValid()) {
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()65 bool RSImplicitCancelAnimationParam::SyncProperties()
66 {
67     if (pendingSyncList_.empty()) {
68         return false;
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);
92     }
93     if (!renderThreadPropertiesMap.empty()) {
94         ret = ret && ExecuteSyncPropertiesTask(std::move(renderThreadPropertiesMap), false);
95     }
96     return ret;
97 }
98 
ExecuteSyncPropertiesTask(RSNodeGetShowingPropertiesAndCancelAnimation::PropertiesMap && propertiesMap,bool isRenderService)99 bool RSImplicitCancelAnimationParam::ExecuteSyncPropertiesTask(
100     RSNodeGetShowingPropertiesAndCancelAnimation::PropertiesMap&& propertiesMap, bool isRenderService)
101 {
102     // create task and execute it in RS (timeout is 400ms)
103     auto task = std::make_shared<RSNodeGetShowingPropertiesAndCancelAnimation>(4e8, std::move(propertiesMap));
104     RSTransactionProxy::GetInstance()->ExecuteSynchronousTask(task, isRenderService);
105 
106     // Test if the task is executed successfully
107     if (!task || !task->IsSuccess()) {
108         ROSEN_LOGE("RSImplicitCancelAnimationParam::ExecuteSyncPropertiesTask failed to execute task.");
109         return false;
110     }
111 
112     // Apply task result
113     for (const auto& [key, value] : task->GetProperties()) {
114         const auto& [nodeId, propertyId] = key;
115         auto node = RSNodeMap::Instance().GetNode(nodeId);
116         if (node == nullptr) {
117             ROSEN_LOGE("RSImplicitCancelAnimationParam::ExecuteSyncPropertiesTask failed to get target node.");
118             continue;
119         }
120         auto modifier = node->GetModifier(propertyId);
121         if (modifier == nullptr) {
122             ROSEN_LOGE("RSImplicitCancelAnimationParam::ExecuteSyncPropertiesTask failed to get target modifier.");
123             continue;
124         }
125         auto property = modifier->GetProperty();
126         if (property == nullptr) {
127             ROSEN_LOGE("RSImplicitCancelAnimationParam::ExecuteSyncPropertiesTask failed to get target property.");
128             continue;
129         }
130         node->CancelAnimationByProperty(propertyId, !property->GetIsCustom());
131         const auto& [propertyValue, animations] = value;
132         if (propertyValue != nullptr) {
133             // successfully canceled RS animation and extract value, update ui value
134             property->SetValueFromRender(propertyValue);
135         } else {
136             // property or node is not yet created in RS, just trigger a force update
137             property->UpdateOnAllAnimationFinish();
138         }
139     }
140     return true;
141 }
142 
CreateEmptyAnimation(std::shared_ptr<RSPropertyBase> property,const std::shared_ptr<RSPropertyBase> & startValue,const std::shared_ptr<RSPropertyBase> & endValue) const143 std::shared_ptr<RSAnimation> RSImplicitCancelAnimationParam::CreateEmptyAnimation(
144     std::shared_ptr<RSPropertyBase> property, const std::shared_ptr<RSPropertyBase>& startValue,
145     const std::shared_ptr<RSPropertyBase>& endValue) const
146 {
147     auto curveAnimation = std::make_shared<RSCurveAnimation>(property, endValue - startValue);
148     curveAnimation->SetDuration(0);
149     return curveAnimation;
150 }
151 
RSImplicitCurveAnimationParam(const RSAnimationTimingProtocol & timingProtocol,const RSAnimationTimingCurve & timingCurve)152 RSImplicitCurveAnimationParam::RSImplicitCurveAnimationParam(
153     const RSAnimationTimingProtocol& timingProtocol, const RSAnimationTimingCurve& timingCurve)
154     : RSImplicitAnimationParam(timingProtocol, ImplicitAnimationParamType::CURVE), timingCurve_(timingCurve)
155 {}
156 
CreateAnimation(std::shared_ptr<RSPropertyBase> property,const std::shared_ptr<RSPropertyBase> & startValue,const std::shared_ptr<RSPropertyBase> & endValue) const157 std::shared_ptr<RSAnimation> RSImplicitCurveAnimationParam::CreateAnimation(std::shared_ptr<RSPropertyBase> property,
158     const std::shared_ptr<RSPropertyBase>& startValue, const std::shared_ptr<RSPropertyBase>& endValue) const
159 {
160     auto curveAnimation = std::make_shared<RSCurveAnimation>(property, endValue - startValue);
161     curveAnimation->SetTimingCurve(timingCurve_);
162     curveAnimation->SetIsCustom(property->GetIsCustom());
163     ApplyTimingProtocol(curveAnimation);
164     return curveAnimation;
165 }
166 
RSImplicitKeyframeAnimationParam(const RSAnimationTimingProtocol & timingProtocol,const RSAnimationTimingCurve & timingCurve,float fraction,int duration)167 RSImplicitKeyframeAnimationParam::RSImplicitKeyframeAnimationParam(
168     const RSAnimationTimingProtocol& timingProtocol, const RSAnimationTimingCurve& timingCurve,
169     float fraction, int duration)
170     : RSImplicitAnimationParam(timingProtocol, ImplicitAnimationParamType::KEYFRAME), timingCurve_(timingCurve),
171       fraction_(fraction), duration_(duration)
172 {}
173 
CreateAnimation(std::shared_ptr<RSPropertyBase> property,const bool & isCreateDurationKeyframe,const int & startDuration,const std::shared_ptr<RSPropertyBase> & startValue,const std::shared_ptr<RSPropertyBase> & endValue) const174 std::shared_ptr<RSAnimation> RSImplicitKeyframeAnimationParam::CreateAnimation(
175     std::shared_ptr<RSPropertyBase> property, const bool& isCreateDurationKeyframe, const int& startDuration,
176     const std::shared_ptr<RSPropertyBase>& startValue, const std::shared_ptr<RSPropertyBase>& endValue) const
177 {
178     auto keyFrameAnimation = std::make_shared<RSKeyframeAnimation>(property);
179     keyFrameAnimation->SetDurationKeyframe(isCreateDurationKeyframe);
180     if (isCreateDurationKeyframe) {
181         keyFrameAnimation->AddKeyFrame(startDuration, startDuration + duration_, endValue, timingCurve_);
182     } else {
183         keyFrameAnimation->AddKeyFrame(fraction_, endValue, timingCurve_);
184     }
185     keyFrameAnimation->SetOriginValue(startValue);
186     keyFrameAnimation->SetIsCustom(property->GetIsCustom());
187     ApplyTimingProtocol(keyFrameAnimation);
188     return keyFrameAnimation;
189 }
190 
AddKeyframe(std::shared_ptr<RSAnimation> & animation,const std::shared_ptr<RSPropertyBase> & startValue,const std::shared_ptr<RSPropertyBase> & endValue) const191 void RSImplicitKeyframeAnimationParam::AddKeyframe(std::shared_ptr<RSAnimation>& animation,
192     const std::shared_ptr<RSPropertyBase>& startValue, const std::shared_ptr<RSPropertyBase>& endValue) const
193 {
194     if (animation == nullptr) {
195         return;
196     }
197 
198     auto keyframeAnimation = std::static_pointer_cast<RSKeyframeAnimation>(animation);
199     if (keyframeAnimation != nullptr) {
200         keyframeAnimation->AddKeyFrame(fraction_, endValue, timingCurve_);
201     }
202 }
203 
AddKeyframe(std::shared_ptr<RSAnimation> & animation,const int startDuration,const std::shared_ptr<RSPropertyBase> & startValue,const std::shared_ptr<RSPropertyBase> & endValue) const204 void RSImplicitKeyframeAnimationParam::AddKeyframe(std::shared_ptr<RSAnimation>& animation, const int startDuration,
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(startDuration, startDuration + duration_, endValue, timingCurve_);
214     }
215 }
216 
RSImplicitPathAnimationParam(const RSAnimationTimingProtocol & timingProtocol,const RSAnimationTimingCurve & timingCurve,const std::shared_ptr<RSMotionPathOption> & motionPathOption)217 RSImplicitPathAnimationParam::RSImplicitPathAnimationParam(const RSAnimationTimingProtocol& timingProtocol,
218     const RSAnimationTimingCurve& timingCurve, const std::shared_ptr<RSMotionPathOption>& motionPathOption)
219     : RSImplicitAnimationParam(timingProtocol, ImplicitAnimationParamType::PATH), timingCurve_(timingCurve),
220       motionPathOption_(motionPathOption)
221 {}
222 
CreateAnimation(std::shared_ptr<RSPropertyBase> property,const std::shared_ptr<RSPropertyBase> & startValue,const std::shared_ptr<RSPropertyBase> & endValue) const223 std::shared_ptr<RSAnimation> RSImplicitPathAnimationParam::CreateAnimation(std::shared_ptr<RSPropertyBase> property,
224     const std::shared_ptr<RSPropertyBase>& startValue, const std::shared_ptr<RSPropertyBase>& endValue) const
225 {
226     if (motionPathOption_ == nullptr) {
227         ROSEN_LOGE("Failed to create path animation, motion path option is null!");
228         return nullptr;
229     }
230 
231     auto pathAnimation =
232         std::make_shared<RSPathAnimation>(property, motionPathOption_->GetPath(), startValue, endValue);
233     pathAnimation->SetBeginFraction(motionPathOption_->GetBeginFraction());
234     pathAnimation->SetEndFraction(motionPathOption_->GetEndFraction());
235     pathAnimation->SetRotationMode(motionPathOption_->GetRotationMode());
236     pathAnimation->SetPathNeedAddOrigin(motionPathOption_->GetPathNeedAddOrigin());
237     pathAnimation->SetTimingCurve(timingCurve_);
238     ApplyTimingProtocol(pathAnimation);
239     return pathAnimation;
240 }
241 
RSImplicitSpringAnimationParam(const RSAnimationTimingProtocol & timingProtocol,const RSAnimationTimingCurve & timingCurve)242 RSImplicitSpringAnimationParam::RSImplicitSpringAnimationParam(
243     const RSAnimationTimingProtocol& timingProtocol, const RSAnimationTimingCurve& timingCurve)
244     : RSImplicitAnimationParam(timingProtocol, ImplicitAnimationParamType::SPRING), timingCurve_(timingCurve)
245 {}
246 
CreateAnimation(std::shared_ptr<RSPropertyBase> property,const std::shared_ptr<RSPropertyBase> & startValue,const std::shared_ptr<RSPropertyBase> & endValue) const247 std::shared_ptr<RSAnimation> RSImplicitSpringAnimationParam::CreateAnimation(std::shared_ptr<RSPropertyBase> property,
248     const std::shared_ptr<RSPropertyBase>& startValue, const std::shared_ptr<RSPropertyBase>& endValue) const
249 {
250     auto springAnimation = std::make_shared<RSSpringAnimation>(property, startValue, endValue);
251     springAnimation->SetTimingCurve(timingCurve_);
252     springAnimation->SetIsCustom(property->GetIsCustom());
253     ApplyTimingProtocol(springAnimation);
254     return springAnimation;
255 }
256 
RSImplicitInterpolatingSpringAnimationParam(const RSAnimationTimingProtocol & timingProtocol,const RSAnimationTimingCurve & timingCurve)257 RSImplicitInterpolatingSpringAnimationParam::RSImplicitInterpolatingSpringAnimationParam(
258     const RSAnimationTimingProtocol& timingProtocol, const RSAnimationTimingCurve& timingCurve)
259     : RSImplicitAnimationParam(timingProtocol, ImplicitAnimationParamType::INTERPOLATING_SPRING),
260       timingCurve_(timingCurve)
261 {}
262 
CreateAnimation(std::shared_ptr<RSPropertyBase> property,const std::shared_ptr<RSPropertyBase> & startValue,const std::shared_ptr<RSPropertyBase> & endValue) const263 std::shared_ptr<RSAnimation> RSImplicitInterpolatingSpringAnimationParam::CreateAnimation(
264     std::shared_ptr<RSPropertyBase> property, const std::shared_ptr<RSPropertyBase>& startValue,
265     const std::shared_ptr<RSPropertyBase>& endValue) const
266 {
267     auto interpolatingSpringAnimation =
268         std::make_shared<RSInterpolatingSpringAnimation>(property, startValue, endValue);
269     interpolatingSpringAnimation->SetTimingCurve(timingCurve_);
270     interpolatingSpringAnimation->SetIsCustom(property->GetIsCustom());
271     ApplyTimingProtocol(interpolatingSpringAnimation);
272     return interpolatingSpringAnimation;
273 }
274 
RSImplicitTransitionParam(const RSAnimationTimingProtocol & timingProtocol,const RSAnimationTimingCurve & timingCurve,const std::shared_ptr<const RSTransitionEffect> & effect,bool isTransitionIn)275 RSImplicitTransitionParam::RSImplicitTransitionParam(const RSAnimationTimingProtocol& timingProtocol,
276     const RSAnimationTimingCurve& timingCurve, const std::shared_ptr<const RSTransitionEffect>& effect,
277     bool isTransitionIn)
278     : RSImplicitAnimationParam(timingProtocol, ImplicitAnimationParamType::TRANSITION), timingCurve_(timingCurve),
279       isTransitionIn_(isTransitionIn), effect_(effect)
280 {}
281 
CreateAnimation()282 std::shared_ptr<RSAnimation> RSImplicitTransitionParam::CreateAnimation()
283 {
284     if (transition_ == nullptr) {
285         transition_ = std::make_shared<RSTransition>(effect_, isTransitionIn_);
286         transition_->SetTimingCurve(timingCurve_);
287         ApplyTimingProtocol(transition_);
288     }
289     return transition_;
290 }
291 
CreateAnimation(const std::shared_ptr<RSPropertyBase> & property,const std::shared_ptr<RSPropertyBase> & startValue,const std::shared_ptr<RSPropertyBase> & endValue)292 std::shared_ptr<RSAnimation> RSImplicitTransitionParam::CreateAnimation(const std::shared_ptr<RSPropertyBase>& property,
293     const std::shared_ptr<RSPropertyBase>& startValue, const std::shared_ptr<RSPropertyBase>& endValue)
294 {
295     auto& customEffects = isTransitionIn_ ? effect_->customTransitionInEffects_ : effect_->customTransitionOutEffects_;
296     for (auto& customEffect : customEffects) {
297         if (property->modifier_.lock() == std::static_pointer_cast<RSModifier>(customEffect->modifier_)) {
298             customEffect->Custom(property, startValue, endValue);
299             break;
300         }
301     }
302     if (transition_ == nullptr) {
303         transition_ = std::make_shared<RSTransition>(effect_, isTransitionIn_);
304         transition_->SetTimingCurve(timingCurve_);
305         transition_->SetIsCustom(true);
306         ApplyTimingProtocol(transition_);
307     }
308     return transition_;
309 }
310 } // namespace Rosen
311 } // namespace OHOS
312