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 #ifndef RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_RENDER_SPRING_ANIMATION_H 17 #define RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_RENDER_SPRING_ANIMATION_H 18 19 #include "animation/rs_render_property_animation.h" 20 #include "animation/rs_spring_model.h" 21 #include "common/rs_macros.h" 22 23 namespace OHOS { 24 namespace Rosen { 25 class RSB_EXPORT RSRenderSpringAnimation : public RSRenderPropertyAnimation { 26 public: 27 explicit RSRenderSpringAnimation(AnimationId id, const PropertyId& propertyId, 28 const std::shared_ptr<RSRenderPropertyBase>& originValue, 29 const std::shared_ptr<RSRenderPropertyBase>& startValue, 30 const std::shared_ptr<RSRenderPropertyBase>& endValue); 31 32 void DumpAnimationInfo(std::string& out) const override; 33 void SetSpringParameters(float response, float dampingRatio, float blendDuration = 0.0f, 34 float minimumAmplitudeRatio = SPRING_MIN_AMPLITUDE_RATIO); 35 void SetZeroThreshold(float zeroThreshold); 36 void SetInitialVelocity(const std::shared_ptr<RSRenderPropertyBase>& velocity); 37 void InheritSpringAnimation(const std::shared_ptr<RSRenderAnimation>& prevAnimation); 38 39 ~RSRenderSpringAnimation() override = default; 40 41 #ifdef ROSEN_OHOS 42 bool Marshalling(Parcel& parcel) const override; 43 [[nodiscard]] static RSRenderSpringAnimation* Unmarshalling(Parcel& parcel); 44 #endif 45 protected: 46 void OnAnimate(float fraction) override; 47 48 void OnAttach() override; 49 void OnDetach() override; 50 void OnInitialize(int64_t time) override; 51 void InitValueEstimator() override; 52 53 private: 54 #ifdef ROSEN_OHOS 55 bool ParseParam(Parcel& parcel) override; 56 #endif 57 RSRenderSpringAnimation() = default; 58 59 // status inherited related 60 float prevMappedTime_ = 0.0f; 61 // return current <value, velocity> as a tuple 62 std::tuple<std::shared_ptr<RSRenderPropertyBase>, std::shared_ptr<RSRenderPropertyBase>, 63 std::shared_ptr<RSRenderPropertyBase>> GetSpringStatus() const; 64 bool InheritSpringStatus(const RSRenderSpringAnimation* from); 65 std::shared_ptr<RSRenderPropertyBase> CalculateVelocity(float time) const; 66 bool GetNeedLogicallyFinishCallback() const; 67 void CallLogicallyFinishCallback() const; 68 69 // spring model related 70 float response_ = 0.0f; 71 float dampingRatio_ = 0.0f; 72 float minimumAmplitudeRatio_ = SPRING_MIN_AMPLITUDE_RATIO; 73 74 // blend related 75 uint64_t blendDuration_ = 0; 76 77 std::shared_ptr<RSRenderPropertyBase> startValue_; 78 std::shared_ptr<RSRenderPropertyBase> endValue_; 79 std::shared_ptr<RSRenderPropertyBase> initialVelocity_; 80 std::shared_ptr<RSSpringValueEstimatorBase> springValueEstimator_; 81 bool needLogicallyFinishCallback_ = false; 82 83 // used to determine whether the animation is near finish 84 float zeroThreshold_ = 0.0f; 85 86 friend class RSSpringAnimation; 87 }; 88 } // namespace Rosen 89 } // namespace OHOS 90 91 #endif // RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_RENDER_SPRING_ANIMATION_H 92