• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 FOUNDATION_ACE_FRAMEWORKS_CORE_ANIMATION_ANIMATOR_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_ANIMATION_ANIMATOR_H
18 
19 #include <fstream>
20 #include <list>
21 
22 #include "base/log/ace_trace.h"
23 #include "base/memory/referenced.h"
24 #include "base/utils/macros.h"
25 #include "base/utils/system_properties.h"
26 #include "core/animation/animation_pub.h"
27 #include "core/animation/interpolator.h"
28 #include "core/animation/motion.h"
29 #include "core/animation/scheduler.h"
30 #include "core/animation/status_listener.h"
31 #include "core/animation/time_event.h"
32 #include "core/components/common/properties/animation_option.h"
33 #include "core/components_ng/syntax/if_else_model.h"
34 
35 #ifdef PREVIEW
36 #define CREATE_ANIMATOR(...) AceType::MakeRefPtr<Animator>(__VA_ARGS__)
37 #else
38 #define CREATE_ANIMATOR(...) Animator::CreateAnimator(__FILE__, __LINE__, ##__VA_ARGS__)
39 #endif
40 
41 namespace OHOS::Ace {
42 class ACE_FORCE_EXPORT Animator : public AceType, public StatusListenable {
43     DECLARE_ACE_TYPE(Animator, AceType);
44 
45 public:
46     enum class Status {
47         IDLE,    // when animation not start or been cancel.
48         RUNNING, // play in reverse / forward direction.
49         PAUSED,  // paused by call Pause API.
50         STOPPED, // stopped by call Finish/Stop API or has played to the end.
51     };
52 
53     // Adjust global animation duration, default scale is 1.0f.
54     static void SetDurationScale(float scale);
55 
56     // Animator can play animations.
57     // So far, animation has two types: Interpolator and Motion(physical-based animation).
58     // But only one type will be played at a time. When playing one, the other's setting will be cleared.
59     // 1. Interpolator: Play/Reverse/Stop/Finish will work
60     // 2. Motion: PlayMotion will work
61     Animator(const WeakPtr<PipelineBase>& context, const char* name = nullptr);
62 
63     Animator(const char* name = nullptr);
64 
65     ~Animator() override;
66 
67 private:
CombineStrUint(const char * fileName,int line)68     static std::string CombineStrUint(const char* fileName, int line)
69     {
70         std::string output = fileName;
71         output += " Line : ";
72         output += std::to_string(line);
73         return output;
74     }
75 
76 public:
77     static RefPtr<Animator> CreateAnimator(
78         const char* fileName, int line, const WeakPtr<PipelineBase>& context, const char* name = nullptr)
79     {
80         if (SystemProperties::GetDebugEnabled()) {
81             if (name == nullptr) {
82                 return AceType::MakeRefPtr<Animator>(context, CombineStrUint(fileName, line).c_str());
83             } else {
84                 return AceType::MakeRefPtr<Animator>(context, (CombineStrUint(fileName, line) + name).c_str());
85             }
86         } else {
87             return AceType::MakeRefPtr<Animator>(context, name);
88         }
89     }
90 
91     static RefPtr<Animator> CreateAnimator(const char* fileName, int line, const char* name = nullptr)
92     {
93         if (SystemProperties::GetDebugEnabled()) {
94             if (name == nullptr) {
95                 return AceType::MakeRefPtr<Animator>(CombineStrUint(fileName, line).c_str());
96             } else {
97                 return AceType::MakeRefPtr<Animator>((CombineStrUint(fileName, line) + name).c_str());
98             }
99         } else {
100             return AceType::MakeRefPtr<Animator>(name);
101         }
102     }
103 
104     void AttachScheduler(const WeakPtr<PipelineBase>& context);
105     void AttachSchedulerOnContainer();
106     bool HasScheduler() const;
107 
108     // Every interpolate animation needs to add itself into animator and use the controller to drive.
109     void AddInterpolator(const RefPtr<Interpolator>& animation);
110     void RemoveInterpolator(const RefPtr<Interpolator>& animation);
111     void ClearInterpolators();
112 
113     // Controller (A) can be added to other controller (B) as a proxy when (B) is running.
114     void AddProxyController(const RefPtr<Animator>& proxy);
115     void RemoveProxyController(const RefPtr<Animator>& proxy);
116     void ClearProxyControllers();
117 
118     // Can use these APIs to check controller's current state.
119     Status GetStatus() const;
120     bool IsStopped() const;
121     bool IsRunning() const;
122     bool IsPending() const;
123 
124     // Sets the total running time of the animator to drive the animation.
125     void SetDuration(int32_t duration);
126     int32_t GetDuration() const;
127     void SetStartDelay(int32_t startDelay);
128 
129     // At last will run iteration loops. repeatTimes equals 0 as default.
130     bool SetIteration(int32_t iteration);
131 
132     // fillmode used to decided the attr of last frame or first frame.
133     void SetFillMode(FillMode fillMode);
134 
135     // tempo is used to control speed of animation.
136     void SetTempo(float tempo);
137 
138     // init animation parameters with animationOption
139     void ApplyOption(const AnimationOption& option);
140 
141     // Whether the animation should be played in reverse in turn.
142     void SetAnimationDirection(AnimationDirection direction);
143 
144     // Set Whether or not the animator is allowed to run asynchronously off of the UI thread.
145     void SetAllowRunningAsynchronously(bool runAsync);
146 
147     // Get whether or not the animator is allowed to run asynchronously off of the UI thread.
148     bool GetAllowRunningAsynchronously();
149 
150     // Update the played time, will not trigger OnFrame callback.
151     void UpdatePlayedTime(int32_t playedTime, bool checkReverse = false);
152     int64_t GetPlayedTime() const;
153 
154     // Trigger one frame callback by the given time.
155     void TriggerFrame(int32_t playedTime, bool checkReverse = false);
156 
157     // Motion not support set duration & repeat & Reverse.
158     void PlayMotion(const RefPtr<Motion>& motion);
159 
160     // Play the Animation based current direction.
161     void Play();
162 
163     // Reverse the Animation based current direction.
164     void Reverse();
165 
166     // Means play forward and set direction reverse false.
167     void Forward();
168 
169     // Means play backward and set direction reverse true.
170     void Backward();
171 
172     // Stop at the current frame(Continueable stop).
173     void Pause();
174 
175     // Resume animation from the pause frame.
176     void Resume();
177 
178     // Stop at the current frame(Unrecoverable stop).
179     void Stop();
180 
181     // Stop at the end frame.
182     void Finish();
183 
184     // Stop at the start frame.
185     void Cancel();
186 
187     // Reset isReverse_ value.
188     void ResetIsReverse();
189 
190     // Get Controller Id.
191     int32_t GetId() const;
192 
193     // Get AnimationScale
194     float GetAnimationScale() const;
195 
196     // Get FillMode
197     FillMode GetFillMode() const;
198 
199     // Get Iteration
200     int32_t GetIteration() const;
201 
PreventFrameJank()202     void PreventFrameJank() {
203         needFrameJankReport_ = false;
204     }
205 
206 private:
207     // Screen refresh callback. duration is in millisecond.
208     void OnFrame(int64_t duration);
209 
210     // Callback the played time to the interpolator animation.
211     void NotifyInterpolator(int32_t playedTime);
212 
213     // Callback the played time to the motion animation.
214     void NotifyMotion(int32_t playedTime);
215 
216     void StartInner(bool alwaysNotify);
217 
218     AnimationOption GetAnimationOption();
219 
220     bool IsSupportedRunningAsynchronously();
221 
222     bool StartAsync();
223 
224     bool StartInnerAsync();
225 
226     void StopInnerAsync();
227 
228     // Calculate played loops and remaining in playedTime
229     int32_t GetPlayedLoopsAndRemaining(int32_t& playedTime);
230 
231     bool GetInitAnimationDirection();
232 
233     // update repeatTimesLeft_ and returns true if run out of repeat times.
234     bool UpdateRepeatTimesLeftAndCheckFinished(int32_t playedLoops);
235 
236     void ToggleDirection();
237     float GetNormalizedTime(float playedTime, bool needStop) const;
238 
239     void UpdateScaledTime();
240     void UpdateIteration(int32_t iteration);
241 
242     void Copy(const RefPtr<Animator>& controller);
243 
244     std::list<RefPtr<Interpolator>> interpolators_;
245     std::list<RefPtr<Animator>> proxyControllers_;
246     RefPtr<Scheduler> scheduler_;
247     RefPtr<Motion> motion_;
248     FillMode fillMode_ = FillMode::FORWARDS;
249     AnimationDirection direction_ = AnimationDirection::NORMAL;
250     int32_t duration_ = 0;        // millisecond.
251     int64_t elapsedTime_ = 0;     // millisecond. in range: 0 ~ startDelay_ + INTERPOLATE_DURATION_MAX
252     int32_t startDelay_ = 0;      // millisecond.
253     int32_t repeatTimes_ = 0;     // user configured repeat times.
254     int32_t iteration_ = 1;       // user configured iteration times.
255     int32_t repeatTimesLeft_ = 0; // repeat times for controller to play
256     int32_t scaledDuration_ = 0;
257     int32_t scaledStartDelay_ = 0;
258     int asyncRunningAnimationCount_ = 0;
259     bool isReverse_ = false;
260     bool isResume_ = false;
261     bool isCurDirection_ = false;
262     bool toggleDirectionPending_ = false;
263     bool allowRunningAsynchronously_ = false;
264     bool needFrameJankReport_ = true;
265     Status status_ = Status::IDLE;
266     int32_t controllerId_ = 0;
267     static float scale_;
268     float tempo_ = 1.0f;
269     bool isBothBackwards = false;
270     std::shared_ptr<AceAsyncScopedTrace> asyncTrace_;
271     std::string animatorName_ = "Animator";
272 };
273 
274 } // namespace OHOS::Ace
275 
276 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_ANIMATION_ANIMATOR_H
277