• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     if (const auto& springParams = timingCurve_.springParams_) {
80         animation->SetSpringParameters(springParams->response_, springParams->dampingRatio_,
81             springParams->blendDuration_, springParams->minimumAmplitudeRatio_);
82     }
83     animation->SetAdditive(GetAdditive());
84     if (GetIsLogicallyFinishCallback()) {
85         animation->SetZeroThreshold(zeroThreshold_);
86     }
87     if (initialVelocity_) {
88         animation->SetInitialVelocity(initialVelocity_->GetRenderProperty());
89     }
90     if (isCustom_) {
91         animation->AttachRenderProperty(property_->GetRenderProperty());
92         StartUIAnimation(animation);
93     } else {
94         StartRenderAnimation(animation);
95     }
96 }
97 
StartRenderAnimation(const std::shared_ptr<RSRenderSpringAnimation> & animation)98 void RSSpringAnimation::StartRenderAnimation(const std::shared_ptr<RSRenderSpringAnimation>& animation)
99 {
100     auto target = GetTarget().lock();
101     if (target == nullptr) {
102         ROSEN_LOGE("Failed to start spring animation, target is null!");
103         return;
104     }
105 
106     std::unique_ptr<RSCommand> command = std::make_unique<RSAnimationCreateSpring>(target->GetId(), animation);
107     target->AddCommand(command, target->IsRenderServiceNode(), target->GetFollowType(), target->GetId());
108     if (target->NeedForcedSendToRemote()) {
109         std::unique_ptr<RSCommand> commandForRemote =
110             std::make_unique<RSAnimationCreateSpring>(target->GetId(), animation);
111         target->AddCommand(commandForRemote, true, target->GetFollowType(), target->GetId());
112     }
113 }
114 
StartUIAnimation(const std::shared_ptr<RSRenderSpringAnimation> & animation)115 void RSSpringAnimation::StartUIAnimation(const std::shared_ptr<RSRenderSpringAnimation>& animation)
116 {
117     StartCustomAnimation(animation);
118     auto target = GetTarget().lock();
119     if (target == nullptr) {
120         ROSEN_LOGE("multi-instance, RSAnimation::StartUIAnimation, target is null!");
121         return;
122     }
123     auto rsUIContext = target->GetRSUIContext();
124     auto& modifierManager = rsUIContext ? rsUIContext->GetRSModifierManager()
125                                         : RSModifierManagerMap::Instance()->GetModifierManager(gettid());
126     if (modifierManager == nullptr) {
127         ROSEN_LOGE("RSSpringAnimation::StartUIAnimation: failed to get modifier manager, "
128             "animationId: %{public}" PRIu64 "!", GetId());
129         return;
130     }
131 
132     auto propertyId = GetPropertyId();
133     auto prevAnimation = modifierManager->QuerySpringAnimation(propertyId);
134     modifierManager->RegisterSpringAnimation(propertyId, GetId());
135     // stop running the previous animation and inherit velocity from it
136     animation->InheritSpringAnimation(prevAnimation);
137 }
138 
GetIsLogicallyFinishCallback() const139 bool RSSpringAnimation::GetIsLogicallyFinishCallback() const
140 {
141     return isLogicallyFinishCallback_;
142 }
143 
SetInitialVelocity(const std::shared_ptr<RSPropertyBase> & velocity)144 void RSSpringAnimation::SetInitialVelocity(const std::shared_ptr<RSPropertyBase>& velocity)
145 {
146     if (!velocity) {
147         ROSEN_LOGE("RSSpringAnimation::SetInitialVelocity: velocity is a nullptr.");
148     }
149     initialVelocity_ = velocity;
150 }
151 } // namespace Rosen
152 } // namespace OHOS
153