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_render_curve_animation.h"
17 
18 #include "animation/rs_value_estimator.h"
19 #include "platform/common/rs_log.h"
20 #include "transaction/rs_marshalling_helper.h"
21 
22 namespace OHOS {
23 namespace Rosen {
RSRenderCurveAnimation(AnimationId id,const PropertyId & propertyId,const std::shared_ptr<RSRenderPropertyBase> & originValue,const std::shared_ptr<RSRenderPropertyBase> & startValue,const std::shared_ptr<RSRenderPropertyBase> & endValue)24 RSRenderCurveAnimation::RSRenderCurveAnimation(AnimationId id, const PropertyId& propertyId,
25     const std::shared_ptr<RSRenderPropertyBase>& originValue, const std::shared_ptr<RSRenderPropertyBase>& startValue,
26     const std::shared_ptr<RSRenderPropertyBase>& endValue) : RSRenderPropertyAnimation(id, propertyId, originValue),
27     startValue_(startValue), endValue_(endValue)
28 {}
29 
DumpAnimationType(std::string & out) const30 void RSRenderCurveAnimation::DumpAnimationType(std::string& out) const
31 {
32     out += "Type:RSRenderCurveAnimation";
33 }
34 
SetInterpolator(const std::shared_ptr<RSInterpolator> & interpolator)35 void RSRenderCurveAnimation::SetInterpolator(const std::shared_ptr<RSInterpolator>& interpolator)
36 {
37     interpolator_ = interpolator;
38 }
39 
GetInterpolator() const40 const std::shared_ptr<RSInterpolator>& RSRenderCurveAnimation::GetInterpolator() const
41 {
42     return interpolator_;
43 }
44 
Marshalling(Parcel & parcel) const45 bool RSRenderCurveAnimation::Marshalling(Parcel& parcel) const
46 {
47     if (!RSRenderPropertyAnimation::Marshalling(parcel)) {
48         ROSEN_LOGE("RSRenderCurveAnimation::Marshalling, RenderPropertyAnimation failed");
49         return false;
50     }
51     if (!(RSRenderPropertyBase::Marshalling(parcel, startValue_) &&
52             RSRenderPropertyBase::Marshalling(parcel, endValue_) && interpolator_ != nullptr &&
53             interpolator_->Marshalling(parcel))) {
54         ROSEN_LOGE("RSRenderCurveAnimation::Marshalling, MarshallingHelper failed");
55         return false;
56     }
57     return true;
58 }
59 
Unmarshalling(Parcel & parcel)60 RSRenderCurveAnimation* RSRenderCurveAnimation::Unmarshalling(Parcel& parcel)
61 {
62     RSRenderCurveAnimation* renderCurveAnimation = new RSRenderCurveAnimation();
63     if (!renderCurveAnimation->ParseParam(parcel)) {
64         ROSEN_LOGE("RSRenderCurveAnimation::Unmarshalling, failed");
65         delete renderCurveAnimation;
66         return nullptr;
67     }
68     return renderCurveAnimation;
69 }
70 
ParseParam(Parcel & parcel)71 bool RSRenderCurveAnimation::ParseParam(Parcel& parcel)
72 {
73     if (!RSRenderPropertyAnimation::ParseParam(parcel)) {
74         ROSEN_LOGE("RSRenderCurveAnimation::ParseParam, ParseParam Fail");
75         return false;
76     }
77 
78     if (!(RSRenderPropertyBase::Unmarshalling(parcel, startValue_) &&
79             RSRenderPropertyBase::Unmarshalling(parcel, endValue_))) {
80         ROSEN_LOGE("RSRenderCurveAnimation::ParseParam, Unmarshalling Fail");
81         return false;
82     }
83 
84     std::shared_ptr<RSInterpolator> interpolator(RSInterpolator::Unmarshalling(parcel));
85     if (interpolator == nullptr) {
86         ROSEN_LOGE("RSRenderCurveAnimation::ParseParam, Unmarshalling interpolator failed");
87         return false;
88     }
89     SetInterpolator(interpolator);
90     return true;
91 }
92 
OnSetFraction(float fraction)93 void RSRenderCurveAnimation::OnSetFraction(float fraction)
94 {
95     if (valueEstimator_ == nullptr) {
96         return;
97     }
98     valueEstimator_->UpdateAnimationValue(fraction, GetAdditive());
99     SetValueFraction(fraction);
100     fractionChangeInfo_ = { true, fraction };
101 }
102 
UpdateFractionAfterContinue()103 void RSRenderCurveAnimation::UpdateFractionAfterContinue()
104 {
105     auto& [bChangeFraction, valueFraction] = fractionChangeInfo_;
106     if (valueEstimator_ != nullptr && bChangeFraction) {
107         SetFractionInner(valueEstimator_->EstimateFraction(interpolator_, valueFraction, GetDuration()));
108         bChangeFraction = false;
109         valueFraction = 0.0f;
110     }
111 }
112 
OnAnimate(float fraction)113 void RSRenderCurveAnimation::OnAnimate(float fraction)
114 {
115     OnAnimateInner(fraction, interpolator_);
116 }
117 
OnAnimateInner(float fraction,const std::shared_ptr<RSInterpolator> & interpolator)118 void RSRenderCurveAnimation::OnAnimateInner(float fraction, const std::shared_ptr<RSInterpolator>& interpolator)
119 {
120     if (GetPropertyId() == 0) {
121         // calculateAnimationValue_ is embedded modify for stat animate frame drop
122         calculateAnimationValue_ = false;
123         return;
124     }
125 
126     if (valueEstimator_ == nullptr || interpolator == nullptr) {
127         return;
128     }
129     auto interpolatorValue = interpolator->Interpolate(fraction);
130     SetValueFraction(interpolatorValue);
131     valueEstimator_->UpdateAnimationValue(interpolatorValue, GetAdditive());
132 }
133 
InitValueEstimator()134 void RSRenderCurveAnimation::InitValueEstimator()
135 {
136     if (valueEstimator_ == nullptr) {
137         valueEstimator_ = property_->CreateRSValueEstimator(RSValueEstimatorType::CURVE_VALUE_ESTIMATOR);
138     }
139     if (valueEstimator_ == nullptr) {
140         ROSEN_LOGE("RSRenderCurveAnimation::InitValueEstimator, valueEstimator_ is nullptr.");
141         return;
142     }
143     valueEstimator_->InitCurveAnimationValue(property_, startValue_, endValue_, lastValue_);
144 }
145 } // namespace Rosen
146 } // namespace OHOS
147