1 /* 2 * Copyright (c) 2021-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_ANIMATION_TIMING_CURVE_H 17 #define RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_ANIMATION_TIMING_CURVE_H 18 19 #include <functional> 20 #include <map> 21 #include <memory> 22 #include <optional> 23 24 #include "common/rs_macros.h" 25 26 namespace OHOS { 27 namespace Rosen { 28 class RSInterpolator; 29 enum class StepsCurvePosition; 30 31 namespace { 32 // minimumAmplitudeRatio_ is used for interpointSpring to determine the ending accuracy of spring animation. 33 // the smaller the minimumAmplitudeRatio_, the closer it is to the endpoint at the end of the animation, 34 // and the longer the animation duration. 35 constexpr float DEFAULT_AMPLITUDE_RATIO = 0.00025f; 36 constexpr float DEFAULT_AMPLITUDE_RATIO_SPRING = 0.001f; 37 constexpr float DEFAULT_RESPONSE = 0.55f; 38 constexpr float DEFAULT_DAMPING_RATIO = 0.825f; 39 constexpr float DEFAULT_BLEND_DURATION = 0.0f; 40 } // namespace 41 42 struct SpringParams { 43 float response_ { 0.0f }; 44 float dampingRatio_ { 0.0f }; 45 float blendDuration_ { 0.0f }; 46 float initialVelocity_ { 0.0f }; 47 float minimumAmplitudeRatio_ { DEFAULT_AMPLITUDE_RATIO }; 48 }; 49 50 class RSC_EXPORT RSAnimationTimingCurve final { 51 public: 52 static const RSAnimationTimingCurve DEFAULT; 53 static const RSAnimationTimingCurve LINEAR; 54 static const RSAnimationTimingCurve EASE; 55 static const RSAnimationTimingCurve EASE_IN; 56 static const RSAnimationTimingCurve EASE_OUT; 57 static const RSAnimationTimingCurve EASE_IN_OUT; 58 static const RSAnimationTimingCurve SPRING; 59 static const RSAnimationTimingCurve INTERACTIVE_SPRING; 60 61 static RSAnimationTimingCurve CreateCustomCurve(const std::function<float(float)>& customCurveFunc); 62 static RSAnimationTimingCurve CreateCubicCurve(float ctrlX1, float ctrlY1, float ctrlX2, float ctrlY2); 63 static RSAnimationTimingCurve CreateStepsCurve(int32_t steps, StepsCurvePosition position); 64 // Create interpolating spring, which duration is determined by TimingProtocol. Multiple animations on the same 65 // property will run simultaneously and act additively. 66 static RSAnimationTimingCurve CreateSpringCurve(float velocity, float mass, float stiffness, float damping); 67 // Create interpolating spring, which duration is determined by the spring model. Multiple animations on the same 68 // property will run simultaneously and act additively. 69 static RSAnimationTimingCurve CreateInterpolatingSpring(float mass, float stiffness, float damping, float velocity, 70 float minimumAmplitudeRatio = DEFAULT_AMPLITUDE_RATIO); 71 // Create physical spring, which duration is determined by the spring model. When mixed with other physical spring 72 // animations on the same property, each animation will be replaced by their successor, preserving velocity from one 73 // animation to the next. 74 static RSAnimationTimingCurve CreateSpring(float response, float dampingRatio, float blendDuration = 0.0f, 75 float minimumAmplitudeRatio = DEFAULT_AMPLITUDE_RATIO_SPRING); 76 77 RSAnimationTimingCurve(); 78 RSAnimationTimingCurve(const RSAnimationTimingCurve& timingCurve) = default; 79 RSAnimationTimingCurve& operator=(const RSAnimationTimingCurve& timingCurve) = default; 80 virtual ~RSAnimationTimingCurve() = default; 81 82 enum class CurveType { INTERPOLATING, SPRING, INTERPOLATING_SPRING }; 83 CurveType type_ { CurveType::INTERPOLATING }; 84 85 private: 86 RSAnimationTimingCurve(const std::shared_ptr<RSInterpolator>& interpolator); 87 RSAnimationTimingCurve(const std::function<float(float)>& customCurveFunc); 88 RSAnimationTimingCurve(float response, float dampingRatio, float blendDuration, float minimumAmplitudeRatio); 89 RSAnimationTimingCurve( 90 float response, float dampingRatio, float initialVelocity, CurveType curveType, float minimumAmplitudeRatio); 91 92 std::optional<SpringParams> springParams_; 93 std::shared_ptr<RSInterpolator> GetInterpolator(int duration) const; 94 95 std::shared_ptr<RSInterpolator> interpolator_; 96 std::function<float(float)> customCurveFunc_; 97 98 friend class RSCurveAnimation; 99 friend class RSImplicitAnimator; 100 friend class RSInterpolatingSpringAnimation; 101 friend class RSKeyframeAnimation; 102 friend class RSPathAnimation; 103 friend class RSSpringAnimation; 104 friend class RSTransition; 105 106 friend class ParticleParams; 107 template<typename T> 108 friend class Change; 109 }; 110 } // namespace Rosen 111 } // namespace OHOS 112 113 #endif // RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_ANIMATION_TIMING_CURVE_H 114