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
SetInterpolator(const std::shared_ptr<RSInterpolator> & interpolator)30 void RSRenderCurveAnimation::SetInterpolator(const std::shared_ptr<RSInterpolator>& interpolator)
31 {
32 interpolator_ = interpolator;
33 }
34
GetInterpolator() const35 const std::shared_ptr<RSInterpolator>& RSRenderCurveAnimation::GetInterpolator() const
36 {
37 return interpolator_;
38 }
39
Marshalling(Parcel & parcel) const40 bool RSRenderCurveAnimation::Marshalling(Parcel& parcel) const
41 {
42 if (!RSRenderPropertyAnimation::Marshalling(parcel)) {
43 ROSEN_LOGE("RSRenderCurveAnimation::Marshalling, RenderPropertyAnimation failed");
44 return false;
45 }
46 if (!(RSRenderPropertyBase::Marshalling(parcel, startValue_) &&
47 RSRenderPropertyBase::Marshalling(parcel, endValue_) && interpolator_ != nullptr &&
48 interpolator_->Marshalling(parcel))) {
49 ROSEN_LOGE("RSRenderCurveAnimation::Marshalling, MarshallingHelper failed");
50 return false;
51 }
52 return true;
53 }
54
Unmarshalling(Parcel & parcel)55 RSRenderCurveAnimation* RSRenderCurveAnimation::Unmarshalling(Parcel& parcel)
56 {
57 RSRenderCurveAnimation* renderCurveAnimation = new RSRenderCurveAnimation();
58 if (!renderCurveAnimation->ParseParam(parcel)) {
59 ROSEN_LOGE("RSRenderCurveAnimation::Unmarshalling, failed");
60 delete renderCurveAnimation;
61 return nullptr;
62 }
63 return renderCurveAnimation;
64 }
65
ParseParam(Parcel & parcel)66 bool RSRenderCurveAnimation::ParseParam(Parcel& parcel)
67 {
68 if (!RSRenderPropertyAnimation::ParseParam(parcel)) {
69 ROSEN_LOGE("RSRenderCurveAnimation::ParseParam, ParseParam Fail");
70 return false;
71 }
72
73 if (!(RSRenderPropertyBase::Unmarshalling(parcel, startValue_) &&
74 RSRenderPropertyBase::Unmarshalling(parcel, endValue_))) {
75 ROSEN_LOGE("RSRenderCurveAnimation::ParseParam, Unmarshalling Fail");
76 return false;
77 }
78
79 std::shared_ptr<RSInterpolator> interpolator(RSInterpolator::Unmarshalling(parcel));
80 SetInterpolator(interpolator);
81 return true;
82 }
83
OnSetFraction(float fraction)84 void RSRenderCurveAnimation::OnSetFraction(float fraction)
85 {
86 OnAnimateInner(fraction, linearInterpolator_);
87 SetFractionInner(valueEstimator_->EstimateFraction(interpolator_));
88 }
89
OnAnimate(float fraction)90 void RSRenderCurveAnimation::OnAnimate(float fraction)
91 {
92 OnAnimateInner(fraction, interpolator_);
93 }
94
OnAnimateInner(float fraction,const std::shared_ptr<RSInterpolator> & interpolator)95 void RSRenderCurveAnimation::OnAnimateInner(float fraction, const std::shared_ptr<RSInterpolator>& interpolator)
96 {
97 if (GetPropertyId() == 0) {
98 return;
99 }
100
101 if (valueEstimator_ == nullptr) {
102 return;
103 }
104 valueEstimator_->UpdateAnimationValue(interpolator_->Interpolate(fraction), GetAdditive());
105 }
106
InitValueEstimator()107 void RSRenderCurveAnimation::InitValueEstimator()
108 {
109 if (valueEstimator_ == nullptr) {
110 valueEstimator_ = property_->CreateRSValueEstimator(RSValueEstimatorType::CURVE_VALUE_ESTIMATOR);
111 }
112 valueEstimator_->InitCurveAnimationValue(property_, startValue_, endValue_, lastValue_);
113 }
114 } // namespace Rosen
115 } // namespace OHOS
116