• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 Pause();
48     void Resume();
49     void SetFraction(float fraction);
50     void SetReversed(bool isReversed);
51     bool Marshalling(Parcel& parcel) const override;
52     virtual bool Animate(int64_t time);
53 
54     bool IsStarted() const;
55     bool IsRunning() const;
56     bool IsPaused() const;
57     bool IsFinished() const;
58     void DumpAnimation(std::string& out) const;
59     virtual void DumpAnimationType(std::string& out) const;
60 
SetAnimationId(AnimationId id)61     void SetAnimationId(AnimationId id)
62     {
63         id_ = id;
64     }
65 
SetDuration(int value)66     void SetDuration(int value)
67     {
68         animationFraction_.SetDuration(value);
69     }
70 
GetDuration()71     int GetDuration() const
72     {
73         return animationFraction_.GetDuration();
74     }
75 
SetStartDelay(int value)76     void SetStartDelay(int value)
77     {
78         animationFraction_.SetStartDelay(value);
79     }
80 
GetStartDelay()81     int GetStartDelay() const
82     {
83         return animationFraction_.GetStartDelay();
84     }
85 
SetRepeatCount(int value)86     void SetRepeatCount(int value)
87     {
88         animationFraction_.SetRepeatCount(value);
89     }
90 
GetRepeatCount()91     int GetRepeatCount() const
92     {
93         return animationFraction_.GetRepeatCount();
94     }
95 
SetSpeed(float value)96     void SetSpeed(float value)
97     {
98         animationFraction_.SetSpeed(value);
99     }
100 
GetSpeed()101     float GetSpeed() const
102     {
103         return animationFraction_.GetSpeed();
104     }
105 
SetAutoReverse(bool value)106     void SetAutoReverse(bool value)
107     {
108         animationFraction_.SetAutoReverse(value);
109     }
110 
GetAutoReverse()111     bool GetAutoReverse() const
112     {
113         return animationFraction_.GetAutoReverse();
114     }
115 
SetFillMode(const FillMode & value)116     void SetFillMode(const FillMode& value)
117     {
118         animationFraction_.SetFillMode(value);
119     }
120 
GetFillMode()121     const FillMode& GetFillMode() const
122     {
123         return animationFraction_.GetFillMode();
124     }
125 
SetDirection(bool isForward)126     void SetDirection(bool isForward)
127     {
128         animationFraction_.SetDirection(isForward);
129     }
130 
GetDirection()131     bool GetDirection() const
132     {
133         return animationFraction_.GetDirection();
134     }
135 
SetFrameRateRange(FrameRateRange range)136     void SetFrameRateRange(FrameRateRange range)
137     {
138         animationFraction_.SetFrameRateRange(range);
139     }
140 
GetFrameRateRange()141     FrameRateRange GetFrameRateRange() const
142     {
143         return animationFraction_.GetFrameRateRange();
144     }
145 
IsCalculateAniamtionValue()146     bool IsCalculateAniamtionValue() const
147     {
148         return calculateAnimationValue_;
149     }
150 
GetNeedUpdateStartTime()151     bool GetNeedUpdateStartTime() const
152     {
153         return needUpdateStartTime_;
154     }
155 
156     void Attach(RSRenderNode* renderNode);
157     void Detach(bool forceDetach = false);
158     RSRenderNode* GetTarget() const;
159 
160     NodeId GetTargetId() const;
161 
162     virtual PropertyId GetPropertyId() const;
163 
AttachRenderProperty(const std::shared_ptr<RSRenderPropertyBase> & property)164     virtual void AttachRenderProperty(const std::shared_ptr<RSRenderPropertyBase>& property) {};
165 
166     void SetStartTime(int64_t);
167 
168     const std::shared_ptr<RSRenderPropertyBase> GetAnimateVelocity() const;
169 
170     static bool isCalcAnimateVelocity_;
171 
172 protected:
173     explicit RSRenderAnimation(AnimationId id);
174     RSRenderAnimation() = default;
175     virtual bool ParseParam(Parcel& parcel);
176     void SetFractionInner(float fraction);
177 
178     virtual void OnSetFraction(float fraction);
179 
OnAttach()180     virtual void OnAttach() {}
181 
OnDetach()182     virtual void OnDetach() {}
183 
OnInitialize(int64_t time)184     virtual void OnInitialize(int64_t time)
185     {
186         needInitialize_ = false;
187     }
188 
OnAnimate(float fraction)189     virtual void OnAnimate(float fraction) {}
190 
OnRemoveOnCompletion()191     virtual void OnRemoveOnCompletion() {}
192 
RecordLastAnimateValue()193     virtual void RecordLastAnimateValue() {}
194 
UpdateAnimateVelocity(float frameInterval)195     virtual void UpdateAnimateVelocity(float frameInterval) {}
196 
197     void FinishOnCurrentPosition();
198 
199     RSAnimationFraction animationFraction_;
200 
201     // calculateAnimationValue_ is embedded modify for stat animate frame drop
202     bool calculateAnimationValue_ { true };
203 
204     std::shared_ptr<RSRenderPropertyBase> animateVelocity_;
205 
206 private:
207     void ProcessFillModeOnStart(float startFraction);
208 
209     void ProcessFillModeOnFinish(float endFraction);
210 
211     void ProcessOnRepeatFinish();
212 
SetRepeatCallbackEnable(bool isEnable)213     void SetRepeatCallbackEnable(bool isEnable)
214     {
215         animationFraction_.SetRepeatCallbackEnable(isEnable);
216     }
217 
GetRepeatCallbackEnable()218     bool GetRepeatCallbackEnable() const
219     {
220         return animationFraction_.GetRepeatCallbackEnable();
221     }
222 
223     AnimationId id_ = 0;
224     NodeId targetId_ = 0;
225     AnimationState state_ { AnimationState::INITIALIZED };
226     bool needUpdateStartTime_ { true };
227     bool needInitialize_ { true };
228     RSRenderNode* target_ { nullptr };
229 
230     friend class RSAnimation;
231     friend class RSModifierManager;
232 };
233 } // namespace Rosen
234 } // namespace OHOS
235 
236 #endif // RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_RENDER_ANIMATION_H
237