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_RENDER_ANIMATION_H
17 #define RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_RENDER_ANIMATION_H
18 
19 #include <parcel.h>
20 #include <refbase.h>
21 #include "animation/rs_animation_common.h"
22 #include "animation/rs_animation_fraction.h"
23 #include "common/rs_macros.h"
24 #include "modifier/rs_render_property.h"
25 
26 namespace OHOS {
27 namespace Rosen {
28 class RSRenderNode;
29 
30 enum class AnimationState {
31     INITIALIZED,
32     RUNNING,
33     PAUSED,
34     FINISHED,
35 };
36 
37 class RSB_EXPORT RSRenderAnimation : public Parcelable {
38 public:
39     RSRenderAnimation(const RSRenderAnimation&) = delete;
40     RSRenderAnimation(const RSRenderAnimation&&) = delete;
41     RSRenderAnimation& operator=(const RSRenderAnimation&) = delete;
42     RSRenderAnimation& operator=(const RSRenderAnimation&&) = delete;
43     ~RSRenderAnimation() override = default;
44     AnimationId GetAnimationId() const;
45     void Start();
46     void Finish();
47     void FinishOnPosition(RSInteractiveAnimationPosition pos);
48     void SetReversedAndContinue();
49     void Pause();
50     void Resume();
51     void SetFraction(float fraction);
52     void SetReversed(bool isReversed);
53     bool Marshalling(Parcel& parcel) const override;
54     virtual bool Animate(int64_t time);
55 
56     bool IsStarted() const;
57     bool IsRunning() const;
58     bool IsPaused() const;
59     bool IsFinished() const;
60     void DumpAnimation(std::string& out) const;
61     virtual void DumpAnimationType(std::string& out) const;
62 
SetAnimationId(AnimationId id)63     void SetAnimationId(AnimationId id)
64     {
65         id_ = id;
66     }
67 
SetDuration(int value)68     void SetDuration(int value)
69     {
70         animationFraction_.SetDuration(value);
71     }
72 
GetDuration()73     int GetDuration() const
74     {
75         return animationFraction_.GetDuration();
76     }
77 
SetStartDelay(int value)78     void SetStartDelay(int value)
79     {
80         animationFraction_.SetStartDelay(value);
81     }
82 
GetStartDelay()83     int GetStartDelay() const
84     {
85         return animationFraction_.GetStartDelay();
86     }
87 
SetRepeatCount(int value)88     void SetRepeatCount(int value)
89     {
90         animationFraction_.SetRepeatCount(value);
91     }
92 
GetRepeatCount()93     int GetRepeatCount() const
94     {
95         return animationFraction_.GetRepeatCount();
96     }
97 
SetSpeed(float value)98     void SetSpeed(float value)
99     {
100         animationFraction_.SetSpeed(value);
101     }
102 
GetSpeed()103     float GetSpeed() const
104     {
105         return animationFraction_.GetSpeed();
106     }
107 
SetAutoReverse(bool value)108     void SetAutoReverse(bool value)
109     {
110         animationFraction_.SetAutoReverse(value);
111     }
112 
GetAutoReverse()113     bool GetAutoReverse() const
114     {
115         return animationFraction_.GetAutoReverse();
116     }
117 
SetFillMode(const FillMode & value)118     void SetFillMode(const FillMode& value)
119     {
120         animationFraction_.SetFillMode(value);
121     }
122 
GetFillMode()123     const FillMode& GetFillMode() const
124     {
125         return animationFraction_.GetFillMode();
126     }
127 
SetDirection(bool isForward)128     void SetDirection(bool isForward)
129     {
130         animationFraction_.SetDirection(isForward);
131     }
132 
GetDirection()133     bool GetDirection() const
134     {
135         return animationFraction_.GetDirection();
136     }
137 
SetFrameRateRange(FrameRateRange range)138     void SetFrameRateRange(FrameRateRange range)
139     {
140         animationFraction_.SetFrameRateRange(range);
141     }
142 
GetFrameRateRange()143     FrameRateRange GetFrameRateRange() const
144     {
145         return animationFraction_.GetFrameRateRange();
146     }
147 
IsCalculateAniamtionValue()148     bool IsCalculateAniamtionValue() const
149     {
150         return calculateAnimationValue_;
151     }
152 
GetNeedUpdateStartTime()153     bool GetNeedUpdateStartTime() const
154     {
155         return needUpdateStartTime_;
156     }
157 
SetValueFraction(float fraction)158     void SetValueFraction(float fraction)
159     {
160         lastValueFraction_ = fraction;
161     }
162 
GetValueFraction()163     float GetValueFraction() const
164     {
165         return lastValueFraction_;
166     }
167 
168     void Attach(RSRenderNode* renderNode);
169     void Detach(bool forceDetach = false);
170     RSRenderNode* GetTarget() const;
171 
172     NodeId GetTargetId() const;
173     const std::string GetTargetName() const;
174 
175     virtual PropertyId GetPropertyId() const;
176 
AttachRenderProperty(const std::shared_ptr<RSRenderPropertyBase> & property)177     virtual void AttachRenderProperty(const std::shared_ptr<RSRenderPropertyBase>& property) {};
178 
179     void SetStartTime(int64_t);
180 
181     const std::shared_ptr<RSRenderPropertyBase>& GetAnimateVelocity() const;
182 
183     static bool isCalcAnimateVelocity_;
184 
185 protected:
186     explicit RSRenderAnimation(AnimationId id);
187     RSRenderAnimation() = default;
188     virtual bool ParseParam(Parcel& parcel);
189     void SetFractionInner(float fraction);
190 
OnSetFraction(float fraction)191     virtual void OnSetFraction(float fraction) {}
192 
UpdateFractionAfterContinue()193     virtual void UpdateFractionAfterContinue() {}
194 
OnAttach()195     virtual void OnAttach() {}
196 
OnDetach()197     virtual void OnDetach() {}
198 
OnInitialize(int64_t time)199     virtual void OnInitialize(int64_t time)
200     {
201         needInitialize_ = false;
202     }
203 
OnAnimate(float fraction)204     virtual void OnAnimate(float fraction) {}
205 
DumpFraction(float fraction,int64_t time)206     virtual void DumpFraction(float fraction, int64_t time) {}
207 
OnRemoveOnCompletion()208     virtual void OnRemoveOnCompletion() {}
209 
RecordLastAnimateValue()210     virtual void RecordLastAnimateValue() {}
211 
UpdateAnimateVelocity(float frameInterval)212     virtual void UpdateAnimateVelocity(float frameInterval) {}
213 
ProcessAnimateVelocityUnderAngleRotation(float frameInterval)214     virtual void ProcessAnimateVelocityUnderAngleRotation(float frameInterval) {}
215 
216     void FinishOnCurrentPosition();
217 
218     RSAnimationFraction animationFraction_;
219 
220     // calculateAnimationValue_ is embedded modify for stat animate frame drop
221     bool calculateAnimationValue_ { true };
222 
223     std::shared_ptr<RSRenderPropertyBase> animateVelocity_;
224 
225     std::pair<bool, float> fractionChangeInfo_ = { false, 0.0f };
226 
227 private:
228     void ProcessFillModeOnStart(float startFraction);
229 
230     void ProcessFillModeOnFinish(float endFraction);
231 
232     void ProcessOnRepeatFinish();
233 
SetRepeatCallbackEnable(bool isEnable)234     void SetRepeatCallbackEnable(bool isEnable)
235     {
236         animationFraction_.SetRepeatCallbackEnable(isEnable);
237     }
238 
GetRepeatCallbackEnable()239     bool GetRepeatCallbackEnable() const
240     {
241         return animationFraction_.GetRepeatCallbackEnable();
242     }
243 
244     AnimationId id_ = 0;
245     NodeId targetId_ = 0;
246     std::string targetName_ = "";
247     AnimationState state_ { AnimationState::INITIALIZED };
248     bool needUpdateStartTime_ { true };
249     bool needInitialize_ { true };
250     RSRenderNode* target_ { nullptr };
251     float lastValueFraction_ { 0.0f };
252 
253     friend class RSAnimation;
254     friend class RSModifierManager;
255 #ifdef RS_PROFILER_ENABLED
256     friend class RSProfiler;
257 #endif
258 };
259 } // namespace Rosen
260 } // namespace OHOS
261 
262 #endif // RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_RENDER_ANIMATION_H
263