• 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_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 += "Type:RSRenderCurveAnimation";
34     RSRenderPropertyType type = RSRenderPropertyType::INVALID;
35     if (property_ != nullptr) {
36         type = property_->GetPropertyType();
37         out += ", ModifierType: " + std::to_string(static_cast<int16_t>(property_->GetModifierType()));
38     } else {
39         out += ", ModifierType: INVALID";
40     }
41     out += ", StartValue: " + RSAnimationTraceUtils::GetInstance().ParseRenderPropertyVaule(startValue_, type);
42     out += ", EndValue: " + RSAnimationTraceUtils::GetInstance().ParseRenderPropertyVaule(endValue_, type);
43 }
44 
SetInterpolator(const std::shared_ptr<RSInterpolator> & interpolator)45 void RSRenderCurveAnimation::SetInterpolator(const std::shared_ptr<RSInterpolator>& interpolator)
46 {
47     interpolator_ = interpolator;
48 }
49 
GetInterpolator() const50 const std::shared_ptr<RSInterpolator>& RSRenderCurveAnimation::GetInterpolator() const
51 {
52     return interpolator_;
53 }
54 
OnSetFraction(float fraction)55 void RSRenderCurveAnimation::OnSetFraction(float fraction)
56 {
57     if (valueEstimator_ == nullptr) {
58         return;
59     }
60     valueEstimator_->UpdateAnimationValue(fraction, GetAdditive());
61     SetValueFraction(fraction);
62     fractionChangeInfo_ = { true, fraction };
63 }
64 
UpdateFractionAfterContinue()65 void RSRenderCurveAnimation::UpdateFractionAfterContinue()
66 {
67     auto& [bChangeFraction, valueFraction] = fractionChangeInfo_;
68     if (valueEstimator_ != nullptr && bChangeFraction) {
69         SetFractionInner(valueEstimator_->EstimateFraction(interpolator_, valueFraction, GetDuration()));
70         bChangeFraction = false;
71         valueFraction = 0.0f;
72     }
73 }
74 
OnAnimate(float fraction)75 void RSRenderCurveAnimation::OnAnimate(float fraction)
76 {
77     OnAnimateInner(fraction, interpolator_);
78 }
79 
OnAnimateInner(float fraction,const std::shared_ptr<RSInterpolator> & interpolator)80 void RSRenderCurveAnimation::OnAnimateInner(float fraction, const std::shared_ptr<RSInterpolator>& interpolator)
81 {
82     if (GetPropertyId() == 0) {
83         // calculateAnimationValue_ is embedded modify for stat animate frame drop
84         calculateAnimationValue_ = false;
85         return;
86     }
87 
88     if (valueEstimator_ == nullptr || interpolator == nullptr) {
89         return;
90     }
91     auto interpolatorValue = interpolator->Interpolate(fraction);
92     SetValueFraction(interpolatorValue);
93     valueEstimator_->UpdateAnimationValue(interpolatorValue, GetAdditive());
94 }
95 
InitValueEstimator()96 void RSRenderCurveAnimation::InitValueEstimator()
97 {
98     if (valueEstimator_ == nullptr) {
99         valueEstimator_ = property_->CreateRSValueEstimator(RSValueEstimatorType::CURVE_VALUE_ESTIMATOR);
100     }
101     if (valueEstimator_ == nullptr) {
102         ROSEN_LOGE("RSRenderCurveAnimation::InitValueEstimator, valueEstimator_ is nullptr.");
103         return;
104     }
105     valueEstimator_->InitCurveAnimationValue(property_, startValue_, endValue_, lastValue_);
106 }
107 } // namespace Rosen
108 } // namespace OHOS
109