1 /*
2 * Copyright (c) 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_render_interpolating_spring_animation.h"
17
18 #include "animation/rs_value_estimator.h"
19 #include "pipeline/rs_render_node.h"
20 #include "platform/common/rs_log.h"
21
22 namespace OHOS {
23 namespace Rosen {
24 namespace {
25 constexpr float SECOND_TO_MILLISECOND = 1e3;
26 constexpr float MILLISECOND_TO_SECOND = 1e-3;
27 } // namespace
28
RSRenderInterpolatingSpringAnimation(AnimationId id,const PropertyId & propertyId,const std::shared_ptr<RSRenderPropertyBase> & originValue,const std::shared_ptr<RSRenderPropertyBase> & startValue,const std::shared_ptr<RSRenderPropertyBase> & endValue)29 RSRenderInterpolatingSpringAnimation::RSRenderInterpolatingSpringAnimation(AnimationId id, const PropertyId& propertyId,
30 const std::shared_ptr<RSRenderPropertyBase>& originValue, const std::shared_ptr<RSRenderPropertyBase>& startValue,
31 const std::shared_ptr<RSRenderPropertyBase>& endValue)
32 : RSRenderPropertyAnimation(id, propertyId, originValue), RSSpringModel<float>(), startValue_(startValue),
33 endValue_(endValue)
34 {
35 // spring model is not initialized, so we can't calculate estimated duration
36 }
37
SetSpringParameters(float response,float dampingRatio,float normalizedInitialVelocity,float minimumAmplitudeRatio)38 void RSRenderInterpolatingSpringAnimation::SetSpringParameters(
39 float response, float dampingRatio, float normalizedInitialVelocity, float minimumAmplitudeRatio)
40 {
41 response_ = response;
42 dampingRatio_ = dampingRatio;
43 normalizedInitialVelocity_ = normalizedInitialVelocity;
44 minimumAmplitudeRatio_ = minimumAmplitudeRatio;
45 }
46
47 #ifdef ROSEN_OHOS
Marshalling(Parcel & parcel) const48 bool RSRenderInterpolatingSpringAnimation::Marshalling(Parcel& parcel) const
49 {
50 if (!RSRenderPropertyAnimation::Marshalling(parcel)) {
51 ROSEN_LOGE("RSRenderInterpolatingSpringAnimation::Marshalling, RenderPropertyAnimation failed");
52 return false;
53 }
54 if (!(RSRenderPropertyBase::Marshalling(parcel, startValue_) &&
55 RSRenderPropertyBase::Marshalling(parcel, endValue_))) {
56 ROSEN_LOGE("RSRenderInterpolatingSpringAnimation::Marshalling, MarshallingHelper failed");
57 return false;
58 }
59
60 if (!(RSMarshallingHelper::Marshalling(parcel, response_) &&
61 RSMarshallingHelper::Marshalling(parcel, dampingRatio_) &&
62 RSMarshallingHelper::Marshalling(parcel, normalizedInitialVelocity_) &&
63 RSMarshallingHelper::Marshalling(parcel, minimumAmplitudeRatio_))) {
64 ROSEN_LOGE("RSRenderInterpolatingSpringAnimation::Marshalling, invalid parametter failed");
65 return false;
66 }
67
68 return true;
69 }
70
Unmarshalling(Parcel & parcel)71 RSRenderInterpolatingSpringAnimation* RSRenderInterpolatingSpringAnimation::Unmarshalling(Parcel& parcel)
72 {
73 auto* renderInterpolatingSpringAnimation = new RSRenderInterpolatingSpringAnimation();
74 if (!renderInterpolatingSpringAnimation->ParseParam(parcel)) {
75 ROSEN_LOGE("RenderInterpolatingSpringAnimation::Unmarshalling, failed");
76 delete renderInterpolatingSpringAnimation;
77 return nullptr;
78 }
79 return renderInterpolatingSpringAnimation;
80 }
81
ParseParam(Parcel & parcel)82 bool RSRenderInterpolatingSpringAnimation::ParseParam(Parcel& parcel)
83 {
84 if (!RSRenderPropertyAnimation::ParseParam(parcel)) {
85 ROSEN_LOGE("RSRenderInterpolatingSpringAnimation::ParseParam, ParseParam Fail");
86 return false;
87 }
88
89 if (!(RSRenderPropertyBase::Unmarshalling(parcel, startValue_) &&
90 RSRenderPropertyBase::Unmarshalling(parcel, endValue_))) {
91 return false;
92 }
93
94 if (!(RSMarshallingHelper::Unmarshalling(parcel, response_) &&
95 RSMarshallingHelper::Unmarshalling(parcel, dampingRatio_) &&
96 RSMarshallingHelper::Unmarshalling(parcel, normalizedInitialVelocity_) &&
97 RSMarshallingHelper::Unmarshalling(parcel, minimumAmplitudeRatio_))) {
98 return false;
99 }
100
101 return true;
102 }
103 #endif
104
OnSetFraction(float fraction)105 void RSRenderInterpolatingSpringAnimation::OnSetFraction(float fraction)
106 {
107 // interpolating spring animation should not support set fraction
108 OnAnimate(fraction);
109 }
110
OnAnimate(float fraction)111 void RSRenderInterpolatingSpringAnimation::OnAnimate(float fraction)
112 {
113 if (valueEstimator_ == nullptr) {
114 ROSEN_LOGE("RSRenderInterpolatingSpringAnimation::OnAnimate, valueEstimator_ is nullptr.");
115 return;
116 }
117 if (GetPropertyId() == 0) {
118 return;
119 } else if (ROSEN_EQ(fraction, 1.0f)) {
120 valueEstimator_->UpdateAnimationValue(1.0f, GetAdditive());
121 return;
122 }
123 auto mappedTime = fraction * GetDuration() * MILLISECOND_TO_SECOND;
124 float displacement = 1.0f + CalculateDisplacement(mappedTime);
125 valueEstimator_->UpdateAnimationValue(displacement, GetAdditive());
126 }
127
OnInitialize(int64_t time)128 void RSRenderInterpolatingSpringAnimation::OnInitialize(int64_t time)
129 {
130 // set the initial status of spring model
131 initialOffset_ = -1.0f;
132 initialVelocity_ = initialOffset_ * (-normalizedInitialVelocity_);
133 CalculateSpringParameters();
134 // use duration calculated by spring model as animation duration
135 SetDuration(std::lroundf(EstimateDuration() * SECOND_TO_MILLISECOND));
136 // this will set needInitialize_ to false
137 RSRenderPropertyAnimation::OnInitialize(time);
138 }
139
InitValueEstimator()140 void RSRenderInterpolatingSpringAnimation::InitValueEstimator()
141 {
142 if (valueEstimator_ == nullptr) {
143 valueEstimator_ = property_->CreateRSValueEstimator(RSValueEstimatorType::CURVE_VALUE_ESTIMATOR);
144 }
145 valueEstimator_->InitCurveAnimationValue(property_, startValue_, endValue_, lastValue_);
146 }
147 } // namespace Rosen
148 } // namespace OHOS