1 /*
2 * Copyright (c) 2022-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_spring_animation.h"
17
18 #include "animation/rs_animation_common.h"
19 #include "animation/rs_render_spring_animation.h"
20 #include "command/rs_animation_command.h"
21 #include "modifier/rs_modifier_manager.h"
22 #include "modifier/rs_modifier_manager_map.h"
23 #include "modifier/rs_property.h"
24 #include "platform/common/rs_log.h"
25 #include "transaction/rs_transaction_proxy.h"
26 #include "ui/rs_node.h"
27
28 namespace OHOS {
29 namespace Rosen {
RSSpringAnimation(std::shared_ptr<RSPropertyBase> property,const std::shared_ptr<RSPropertyBase> & byValue)30 RSSpringAnimation::RSSpringAnimation(std::shared_ptr<RSPropertyBase> property,
31 const std::shared_ptr<RSPropertyBase>& byValue) : RSPropertyAnimation(property)
32 {
33 isDelta_ = true;
34 byValue_ = byValue;
35 }
36
RSSpringAnimation(std::shared_ptr<RSPropertyBase> property,const std::shared_ptr<RSPropertyBase> & startValue,const std::shared_ptr<RSPropertyBase> & endValue)37 RSSpringAnimation::RSSpringAnimation(std::shared_ptr<RSPropertyBase> property,
38 const std::shared_ptr<RSPropertyBase>& startValue, const std::shared_ptr<RSPropertyBase>& endValue)
39 : RSPropertyAnimation(property)
40 {
41 isDelta_ = false;
42 startValue_ = startValue;
43 endValue_ = endValue;
44 }
45
SetTimingCurve(const RSAnimationTimingCurve & timingCurve)46 void RSSpringAnimation::SetTimingCurve(const RSAnimationTimingCurve& timingCurve)
47 {
48 if (timingCurve.type_ != RSAnimationTimingCurve::CurveType::SPRING) {
49 ROSEN_LOGE("RSSpringAnimation::SetTimingCurve: invalid timing curve type");
50 return;
51 }
52 timingCurve_ = timingCurve;
53 }
54
GetTimingCurve() const55 const RSAnimationTimingCurve& RSSpringAnimation::GetTimingCurve() const
56 {
57 return timingCurve_;
58 }
59
SetZeroThreshold(const float zeroThreshold)60 void RSSpringAnimation::SetZeroThreshold(const float zeroThreshold)
61 {
62 constexpr float ZERO = 0.0f;
63 if (zeroThreshold_ < ZERO) {
64 ROSEN_LOGE("RSSpringAnimation::SetZeroThreshold: invalid threshold.");
65 return;
66 }
67 zeroThreshold_ = zeroThreshold;
68 isLogicallyFinishCallback_ = true;
69 }
70
OnStart()71 void RSSpringAnimation::OnStart()
72 {
73 RSPropertyAnimation::OnStart();
74 auto animation = std::make_shared<RSRenderSpringAnimation>(GetId(), GetPropertyId(),
75 originValue_->GetRenderProperty(), startValue_->GetRenderProperty(), endValue_->GetRenderProperty());
76 // 300: placeholder for estimated duration, will be replaced by real duration on animation start.
77 SetDuration(300);
78 UpdateParamToRenderAnimation(animation);
79 animation->SetSpringParameters(timingCurve_.response_, timingCurve_.dampingRatio_, timingCurve_.blendDuration_);
80 animation->SetAdditive(GetAdditive());
81 if (GetIsLogicallyFinishCallback()) {
82 animation->SetZeroThreshold(zeroThreshold_);
83 }
84 if (initialVelocity_) {
85 animation->SetInitialVelocity(initialVelocity_->GetRenderProperty());
86 }
87 if (isCustom_) {
88 animation->AttachRenderProperty(property_->GetRenderProperty());
89 StartUIAnimation(animation);
90 } else {
91 StartRenderAnimation(animation);
92 }
93 }
94
StartRenderAnimation(const std::shared_ptr<RSRenderSpringAnimation> & animation)95 void RSSpringAnimation::StartRenderAnimation(const std::shared_ptr<RSRenderSpringAnimation>& animation)
96 {
97 auto target = GetTarget().lock();
98 if (target == nullptr) {
99 ROSEN_LOGE("Failed to start spring animation, target is null!");
100 return;
101 }
102 auto transactionProxy = RSTransactionProxy::GetInstance();
103 if (transactionProxy == nullptr) {
104 ROSEN_LOGE("Failed to start spring animation, transaction proxy is null!");
105 return;
106 }
107
108 std::unique_ptr<RSCommand> command = std::make_unique<RSAnimationCreateSpring>(target->GetId(), animation);
109 transactionProxy->AddCommand(command, target->IsRenderServiceNode(), target->GetFollowType(), target->GetId());
110 if (target->NeedForcedSendToRemote()) {
111 std::unique_ptr<RSCommand> commandForRemote =
112 std::make_unique<RSAnimationCreateSpring>(target->GetId(), animation);
113 transactionProxy->AddCommand(commandForRemote, true, target->GetFollowType(), target->GetId());
114 }
115 }
116
StartUIAnimation(const std::shared_ptr<RSRenderSpringAnimation> & animation)117 void RSSpringAnimation::StartUIAnimation(const std::shared_ptr<RSRenderSpringAnimation>& animation)
118 {
119 StartCustomAnimation(animation);
120 auto& modifierManager = RSModifierManagerMap::Instance()->GetModifierManager(gettid());
121 if (modifierManager == nullptr) {
122 ROSEN_LOGE("RSSpringAnimation::StartUIAnimation: failed to get modifier manager, "
123 "animationId: %{public}" PRIu64 "!", GetId());
124 return;
125 }
126
127 auto propertyId = GetPropertyId();
128 auto prevAnimation = modifierManager->QuerySpringAnimation(propertyId);
129 modifierManager->RegisterSpringAnimation(propertyId, GetId());
130 // stop running the previous animation and inherit velocity from it
131 animation->InheritSpringAnimation(prevAnimation);
132 }
133
GetIsLogicallyFinishCallback() const134 bool RSSpringAnimation::GetIsLogicallyFinishCallback() const
135 {
136 return isLogicallyFinishCallback_;
137 }
138
SetInitialVelocity(const std::shared_ptr<RSPropertyBase> & velocity)139 void RSSpringAnimation::SetInitialVelocity(const std::shared_ptr<RSPropertyBase>& velocity)
140 {
141 if (!velocity) {
142 ROSEN_LOGE("RSSpringAnimation::SetInitialVelocity: velocity is a nullptr.");
143 }
144 initialVelocity_ = velocity;
145 }
146 } // namespace Rosen
147 } // namespace OHOS
148