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