1 /* 2 * Copyright (c) 2021 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 #ifndef RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_RENDER_CURVE_ANIMATION_H 17 #define RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_RENDER_CURVE_ANIMATION_H 18 19 #include "animation/rs_interpolator.h" 20 #include "animation/rs_render_property_animation.h" 21 #include "animation/rs_value_estimator.h" 22 #include "platform/common/rs_log.h" 23 24 namespace OHOS { 25 namespace Rosen { 26 template<typename T> 27 class RSRenderCurveAnimation : public RSRenderPropertyAnimation<T> { 28 public: RSRenderCurveAnimation(AnimationId id,const RSAnimatableProperty & property,const T & originValue,const T & startValue,const T & endValue)29 RSRenderCurveAnimation(AnimationId id, const RSAnimatableProperty& property, const T& originValue, 30 const T& startValue, const T& endValue) 31 : RSRenderPropertyAnimation<T>(id, property, originValue), startValue_(startValue), endValue_(endValue) 32 {} 33 34 virtual ~RSRenderCurveAnimation() = default; 35 SetInterpolator(const std::shared_ptr<RSInterpolator> & interpolator)36 void SetInterpolator(const std::shared_ptr<RSInterpolator>& interpolator) 37 { 38 interpolator_ = interpolator; 39 } 40 GetInterpolator()41 const std::shared_ptr<RSInterpolator>& GetInterpolator() const 42 { 43 return interpolator_; 44 } 45 46 #ifdef ROSEN_OHOS Marshalling(Parcel & parcel)47 bool Marshalling(Parcel& parcel) const override 48 { 49 if (!RSRenderPropertyAnimation<T>::Marshalling(parcel)) { 50 ROSEN_LOGE("RSRenderCurveAnimation::Marshalling, RenderPropertyAnimation failed"); 51 return false; 52 } 53 if (!(RSMarshallingHelper::Marshalling(parcel, startValue_) && 54 RSMarshallingHelper::Marshalling(parcel, endValue_) && interpolator_->Marshalling(parcel))) { 55 ROSEN_LOGE("RSRenderCurveAnimation::Marshalling, MarshallingHelper failed"); 56 return false; 57 } 58 return true; 59 } 60 Unmarshalling(Parcel & parcel)61 static RSRenderCurveAnimation* Unmarshalling(Parcel& parcel) 62 { 63 RSRenderCurveAnimation* renderCurveAnimation = new RSRenderCurveAnimation<T>(); 64 if (!renderCurveAnimation->ParseParam(parcel)) { 65 ROSEN_LOGE("RSRenderCurveAnimation::Unmarshalling, failed"); 66 delete renderCurveAnimation; 67 return nullptr; 68 } 69 return renderCurveAnimation; 70 } 71 #endif 72 protected: OnSetFraction(float fraction)73 void OnSetFraction(float fraction) override 74 { 75 OnAnimateInner(fraction, linearInterpolator_); 76 RSRenderAnimation::SetFractionInner(RSValueEstimator::EstimateFraction( 77 interpolator_, RSRenderPropertyAnimation<T>::GetLastValue(), startValue_, endValue_)); 78 } 79 OnAnimate(float fraction)80 void OnAnimate(float fraction) override 81 { 82 OnAnimateInner(fraction, interpolator_); 83 } 84 85 private: 86 #ifdef ROSEN_OHOS ParseParam(Parcel & parcel)87 bool ParseParam(Parcel& parcel) override 88 { 89 if (!RSRenderPropertyAnimation<T>::ParseParam(parcel)) { 90 ROSEN_LOGE("RSRenderCurveAnimation::ParseParam, ParseParam Fail"); 91 return false; 92 } 93 94 if (!(RSMarshallingHelper::Unmarshalling(parcel, startValue_) && 95 RSMarshallingHelper::Unmarshalling(parcel, endValue_))) { 96 ROSEN_LOGE("RSRenderCurveAnimation::ParseParam, Unmarshalling Fail"); 97 return false; 98 } 99 std::shared_ptr<RSInterpolator> interpolator(RSInterpolator::Unmarshalling(parcel)); 100 SetInterpolator(interpolator); 101 return true; 102 } 103 #endif 104 RSRenderCurveAnimation() = default; OnAnimateInner(float fraction,const std::shared_ptr<RSInterpolator> & interpolator)105 void OnAnimateInner(float fraction, const std::shared_ptr<RSInterpolator>& interpolator) 106 { 107 if (RSRenderPropertyAnimation<T>::GetProperty() == RSAnimatableProperty::INVALID) { 108 return; 109 } 110 auto interpolationValue = 111 RSValueEstimator::Estimate(interpolator_->Interpolate(fraction), startValue_, endValue_); 112 RSRenderPropertyAnimation<T>::SetAnimationValue(interpolationValue); 113 } 114 115 T startValue_ {}; 116 T endValue_ {}; 117 std::shared_ptr<RSInterpolator> interpolator_ { RSInterpolator::DEFAULT }; 118 std::shared_ptr<RSInterpolator> linearInterpolator_ { std::make_shared<LinearInterpolator>() }; 119 }; 120 } // namespace Rosen 121 } // namespace OHOS 122 123 #endif // RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_RENDER_CURVE_ANIMATION_H 124