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_animation_trace_utils.h"
19 #include "animation/rs_value_estimator.h"
20 #include "platform/common/rs_log.h"
21 #include "transaction/rs_marshalling_helper.h"
22
23 namespace OHOS {
24 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)25 RSRenderCurveAnimation::RSRenderCurveAnimation(AnimationId id, const PropertyId& propertyId,
26 const std::shared_ptr<RSRenderPropertyBase>& originValue, const std::shared_ptr<RSRenderPropertyBase>& startValue,
27 const std::shared_ptr<RSRenderPropertyBase>& endValue) : RSRenderPropertyAnimation(id, propertyId, originValue),
28 startValue_(startValue), endValue_(endValue)
29 {}
30
DumpAnimationInfo(std::string & out) const31 void RSRenderCurveAnimation::DumpAnimationInfo(std::string& out) const
32 {
33 out.append("Type:RSRenderCurveAnimation");
34 DumpProperty(out);
35 RSPropertyType type = RSPropertyType::INVALID;
36 if (property_ != nullptr) {
37 type = property_->GetPropertyType();
38 }
39 out.append(", StartValue: ").append(RSAnimationTraceUtils::GetInstance().ParseRenderPropertyValue(startValue_));
40 out.append(", EndValue: ").append(RSAnimationTraceUtils::GetInstance().ParseRenderPropertyValue(endValue_));
41 }
42
SetInterpolator(const std::shared_ptr<RSInterpolator> & interpolator)43 void RSRenderCurveAnimation::SetInterpolator(const std::shared_ptr<RSInterpolator>& interpolator)
44 {
45 interpolator_ = interpolator;
46 }
47
GetInterpolator() const48 const std::shared_ptr<RSInterpolator>& RSRenderCurveAnimation::GetInterpolator() const
49 {
50 return interpolator_;
51 }
52
OnSetFraction(float fraction)53 void RSRenderCurveAnimation::OnSetFraction(float fraction)
54 {
55 if (valueEstimator_ == nullptr) {
56 return;
57 }
58 valueEstimator_->UpdateAnimationValue(fraction, GetAdditive());
59 SetValueFraction(fraction);
60 fractionChangeInfo_ = { true, fraction };
61 }
62
UpdateFractionAfterContinue()63 void RSRenderCurveAnimation::UpdateFractionAfterContinue()
64 {
65 auto& [bChangeFraction, valueFraction] = fractionChangeInfo_;
66 if (valueEstimator_ != nullptr && bChangeFraction) {
67 SetFractionInner(valueEstimator_->EstimateFraction(interpolator_, valueFraction, GetDuration()));
68 bChangeFraction = false;
69 valueFraction = 0.0f;
70 }
71 }
72
OnAnimate(float fraction)73 void RSRenderCurveAnimation::OnAnimate(float fraction)
74 {
75 OnAnimateInner(fraction, interpolator_);
76 }
77
OnAnimateInner(float fraction,const std::shared_ptr<RSInterpolator> & interpolator)78 void RSRenderCurveAnimation::OnAnimateInner(float fraction, const std::shared_ptr<RSInterpolator>& interpolator)
79 {
80 if (GetPropertyId() == 0) {
81 // calculateAnimationValue_ is embedded modify for stat animate frame drop
82 calculateAnimationValue_ = false;
83 return;
84 }
85
86 if (valueEstimator_ == nullptr || interpolator == nullptr) {
87 return;
88 }
89 auto interpolatorValue = interpolator->Interpolate(fraction);
90 SetValueFraction(interpolatorValue);
91 valueEstimator_->UpdateAnimationValue(interpolatorValue, GetAdditive());
92 }
93
InitValueEstimator()94 void RSRenderCurveAnimation::InitValueEstimator()
95 {
96 if (valueEstimator_ == nullptr) {
97 valueEstimator_ = property_->CreateRSValueEstimator(RSValueEstimatorType::CURVE_VALUE_ESTIMATOR);
98 }
99 if (valueEstimator_ == nullptr) {
100 ROSEN_LOGE("RSRenderCurveAnimation::InitValueEstimator, valueEstimator_ is nullptr.");
101 return;
102 }
103 valueEstimator_->InitCurveAnimationValue(property_, startValue_, endValue_, lastValue_);
104 }
105 } // namespace Rosen
106 } // namespace OHOS
107