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 DumpAnimationType(std::string& out) const override;
33     void SetSpringParameters(float response, float dampingRatio, float blendDuration = 0.0f);
34     void SetZeroThreshold(float zeroThreshold);
35     void SetInitialVelocity(const std::shared_ptr<RSRenderPropertyBase>& velocity);
36     void InheritSpringAnimation(const std::shared_ptr<RSRenderAnimation>& prevAnimation);
37 
38     ~RSRenderSpringAnimation() override = default;
39 
40 #ifdef ROSEN_OHOS
41     bool Marshalling(Parcel& parcel) const override;
42     [[nodiscard]] static RSRenderSpringAnimation* Unmarshalling(Parcel& parcel);
43 #endif
44 protected:
45     void OnAnimate(float fraction) override;
46 
47     void OnAttach() override;
48     void OnDetach() override;
49     void OnInitialize(int64_t time) override;
50     void InitValueEstimator() override;
51 
52 private:
53 #ifdef ROSEN_OHOS
54     bool ParseParam(Parcel& parcel) override;
55 #endif
56     RSRenderSpringAnimation() = default;
57 
58     // status inherited related
59     float prevMappedTime_ = 0.0f;
60     // return current <value, velocity> as a tuple
61     std::tuple<std::shared_ptr<RSRenderPropertyBase>, std::shared_ptr<RSRenderPropertyBase>,
62         std::shared_ptr<RSRenderPropertyBase>> GetSpringStatus() const;
63     bool InheritSpringStatus(const RSRenderSpringAnimation* from);
64     std::shared_ptr<RSRenderPropertyBase> CalculateVelocity(float time) const;
65     bool GetNeedLogicallyFinishCallback() const;
66     void CallLogicallyFinishCallback() const;
67 
68     // spring model related
69     float response_ = 0.0f;
70     float dampingRatio_ = 0.0f;
71 
72     // blend related
73     uint64_t blendDuration_ = 0;
74 
75     std::shared_ptr<RSRenderPropertyBase> startValue_;
76     std::shared_ptr<RSRenderPropertyBase> endValue_;
77     std::shared_ptr<RSRenderPropertyBase> initialVelocity_;
78     std::shared_ptr<RSSpringValueEstimatorBase> springValueEstimator_;
79     bool needLogicallyFinishCallback_ = false;
80 
81     // used to determine whether the animation is near finish
82     float zeroThreshold_ = 0.0f;
83 
84     friend class RSSpringAnimation;
85 };
86 } // namespace Rosen
87 } // namespace OHOS
88 
89 #endif // RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_RENDER_SPRING_ANIMATION_H
90