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