fb4a55c4a39aa9a4e7d76c686990da0b4451e459
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / animated-vector-image / vector-animation-task.h
1 #ifndef DALI_TOOLKIT_VECTOR_ANIMATION_TASK_H
2 #define DALI_TOOLKIT_VECTOR_ANIMATION_TASK_H
3
4 /*
5  * Copyright (c) 2022 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 // EXTERNAL INCLUDES
21 #include <dali/devel-api/adaptor-framework/event-thread-callback.h>
22 #include <dali/devel-api/adaptor-framework/vector-animation-renderer.h>
23 #include <dali/devel-api/threading/conditional-wait.h>
24 #include <dali/public-api/object/property-array.h>
25 #include <chrono>
26 #include <memory>
27
28 // INTERNAL INCLUDES
29 #include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
30
31 namespace Dali
32 {
33 namespace Toolkit
34 {
35 namespace Internal
36 {
37 class VisualFactoryCache;
38 class VectorAnimationThread;
39 class VectorAnimationTask;
40 typedef IntrusivePtr<VectorAnimationTask> VectorAnimationTaskPtr;
41
42 /**
43  * The task of the vector animation.
44  */
45 class VectorAnimationTask : public RefObject, public ConnectionTracker
46 {
47 public:
48   using ResourceReadySignalType = Signal<void(bool)>;
49
50   using TimePoint = std::chrono::time_point<std::chrono::steady_clock>;
51
52   /**
53    * Flags for re-sending data to the vector animation thread
54    */
55   enum ResendFlags
56   {
57     RESEND_PLAY_RANGE          = 1 << 0,
58     RESEND_LOOP_COUNT          = 1 << 1,
59     RESEND_STOP_BEHAVIOR       = 1 << 2,
60     RESEND_LOOPING_MODE        = 1 << 3,
61     RESEND_CURRENT_FRAME       = 1 << 4,
62     RESEND_SIZE                = 1 << 5,
63     RESEND_PLAY_STATE          = 1 << 6,
64     RESEND_NEED_RESOURCE_READY = 1 << 7
65   };
66
67   /**
68    * @brief Structure used to pass parameters to the vector animation task
69    */
70   struct AnimationData
71   {
72     AnimationData()
73     : resendFlag(0),
74       playRange(),
75       playState(),
76       stopBehavior(DevelImageVisual::StopBehavior::CURRENT_FRAME),
77       loopingMode(DevelImageVisual::LoopingMode::RESTART),
78       currentFrame(0),
79       width(0),
80       height(0),
81       loopCount(-1)
82     {
83     }
84
85     AnimationData& operator=(const AnimationData& rhs)
86     {
87       resendFlag |= rhs.resendFlag; // OR resend flag
88       playRange    = rhs.playRange;
89       playState    = rhs.playState;
90       stopBehavior = rhs.stopBehavior;
91       loopingMode  = rhs.loopingMode;
92       currentFrame = rhs.currentFrame;
93       width        = rhs.width;
94       height       = rhs.height;
95       loopCount    = rhs.loopCount;
96       return *this;
97     }
98
99     uint32_t                             resendFlag;
100     Property::Array                      playRange;
101     DevelImageVisual::PlayState::Type    playState;
102     DevelImageVisual::StopBehavior::Type stopBehavior;
103     DevelImageVisual::LoopingMode::Type  loopingMode;
104     uint32_t                             currentFrame;
105     uint32_t                             width;
106     uint32_t                             height;
107     int32_t                              loopCount;
108   };
109
110   /**
111    * @brief Constructor.
112    *
113    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
114    */
115   VectorAnimationTask(VisualFactoryCache& factoryCache);
116
117   /**
118    * @brief Destructor.
119    */
120   ~VectorAnimationTask() override;
121
122   /**
123    * @brief Finalizes the task.
124    */
125   void Finalize();
126
127   /**
128    * @brief Sets the renderer used to display the result image.
129    *
130    * @param[in] renderer The renderer used to display the result image
131    */
132   void SetRenderer(Renderer renderer);
133
134   /**
135    * @brief Request to load the animation file.
136    *
137    * @param[in] url The url of the vector animation file
138    */
139   void RequestLoad(const std::string& url);
140
141   /**
142    * @brief Sets data to specify animation playback.
143    * @param[in] data The animation data
144    */
145   void SetAnimationData(const AnimationData& data);
146
147   /**
148    * @brief This callback is called after the animation is finished.
149    * @param[in] callback The animation finished callback
150    */
151   void SetAnimationFinishedCallback(EventThreadCallback* callback);
152
153   /**
154    * @brief Gets the playing range in frame number.
155    * @param[out] startFrame The frame number to specify minimum progress.
156    * @param[out] endFrame The frame number to specify maximum progress.
157    */
158   void GetPlayRange(uint32_t& startFrame, uint32_t& endFrame);
159
160   /**
161    * @brief Retrieves the current frame number of the animation.
162    * @return The current frame number
163    */
164   uint32_t GetCurrentFrameNumber() const;
165
166   /**
167    * @brief Retrieves the total frame number of the animation.
168    * @return The total frame number
169    */
170   uint32_t GetTotalFrameNumber() const;
171
172   /**
173    * @brief Gets the default size of the file,.
174    * @return The default size of the file
175    */
176   void GetDefaultSize(uint32_t& width, uint32_t& height) const;
177
178   /**
179    * @brief Gets the layer information of all the child layers.
180    * @param[out] map The layer information
181    */
182   void GetLayerInfo(Property::Map& map) const;
183
184   /**
185    * @brief Connect to this signal to be notified when the resource is ready.
186    * @return The signal to connect to.
187    */
188   ResourceReadySignalType& ResourceReadySignal();
189
190   /**
191    * @brief Rasterizes the current frame.
192    * @param[out] keepAnimation true if the animation is running, false otherwise.
193    * @return true if the rasterization succeeded, false otherwise.
194    */
195   bool Rasterize(bool& keepAnimation);
196
197   /**
198    * @brief Calculates the time for the next frame rasterization.
199    * @return The time for the next frame rasterization.
200    */
201   TimePoint CalculateNextFrameTime(bool renderNow);
202
203   /**
204    * @brief Gets the time for the next frame rasterization.
205    * @return The time for the next frame rasterization.
206    */
207   TimePoint GetNextFrameTime();
208
209 private:
210   /**
211    * @brief Loads the animation file.
212    *
213    * @return True if loading succeeded, false otherwise.
214    */
215   bool Load();
216
217   /**
218    * @brief Play the vector animation.
219    */
220   void PlayAnimation();
221
222   /**
223    * @brief Stop the vector animation.
224    */
225   void StopAnimation();
226
227   /**
228    * @brief Pause the vector animation.
229    */
230   void PauseAnimation();
231
232   /**
233    * @brief Sets the target image size.
234    *
235    * @param[in] width The target image width
236    * @param[in] height The target image height
237    */
238   void SetSize(uint32_t width, uint32_t height);
239
240   /**
241    * @brief Enable looping for 'count' repeats. -1 means to repeat forever.
242    * @param[in] count The number of times to loop
243    */
244   void SetLoopCount(int32_t count);
245
246   /**
247    * @brief Set the playing range in frame number.
248    * @param[in] playRange The array to specify minimum and maximum progress.
249    * The animation will play between those values.
250    */
251   void SetPlayRange(const Property::Array& playRange);
252
253   /**
254    * @brief Sets the current frame number of the animation.
255    * @param[in] frameNumber The new frame number between [0, the maximum frame number] or between the play range if specified.
256    */
257   void SetCurrentFrameNumber(uint32_t frameNumber);
258
259   /**
260    * @brief Sets the stop behavior of the animation. This is performed when the animation is stopped.
261    * @param[in] stopBehavior The stop behavior
262    */
263   void SetStopBehavior(DevelImageVisual::StopBehavior::Type stopBehavior);
264
265   /**
266    * @brief Sets the looping mode.
267    * Animation plays forwards and then restarts from the beginning or runs backwards again.
268    * @param[in] loopingMode The looping mode
269    */
270   void SetLoopingMode(DevelImageVisual::LoopingMode::Type loopingMode);
271
272   /**
273    * @brief Gets the frame number when the animation is stopped according to the stop behavior.
274    */
275   uint32_t GetStoppedFrame(uint32_t startFrame, uint32_t endFrame, uint32_t currentFrame);
276
277   /**
278    * @brief Applies the animation data set by the main thread.
279    */
280   void ApplyAnimationData();
281
282   /**
283    * @brief Called when the texture upload is completed.
284    */
285   void OnUploadCompleted();
286
287   /**
288    * @brief Event callback from rasterize thread. This is called when the file loading is completed.
289    */
290   void OnLoadCompleted();
291
292   // Undefined
293   VectorAnimationTask(const VectorAnimationTask& task) = delete;
294
295   // Undefined
296   VectorAnimationTask& operator=(const VectorAnimationTask& task) = delete;
297
298 private:
299   enum class PlayState
300   {
301     STOPPING, ///< The animation is stopping
302     STOPPED,  ///< The animation has stopped
303     PLAYING,  ///< The animation is playing
304     PAUSED    ///< The animation is paused
305   };
306
307   std::string                          mUrl;
308   VectorAnimationRenderer              mVectorRenderer;
309   AnimationData                        mAnimationData[2];
310   VectorAnimationThread&               mVectorAnimationThread;
311   ConditionalWait                      mConditionalWait;
312   ResourceReadySignalType              mResourceReadySignal;
313   std::unique_ptr<EventThreadCallback> mAnimationFinishedTrigger;
314   std::unique_ptr<EventThreadCallback> mLoadCompletedTrigger;
315   PlayState                            mPlayState;
316   DevelImageVisual::StopBehavior::Type mStopBehavior;
317   DevelImageVisual::LoopingMode::Type  mLoopingMode;
318   TimePoint                            mNextFrameStartTime;
319   int64_t                              mFrameDurationMicroSeconds;
320   float                                mFrameRate;
321   uint32_t                             mCurrentFrame;
322   uint32_t                             mTotalFrame;
323   uint32_t                             mStartFrame;
324   uint32_t                             mEndFrame;
325   uint32_t                             mDroppedFrames;
326   uint32_t                             mWidth;
327   uint32_t                             mHeight;
328   uint32_t                             mAnimationDataIndex;
329   int32_t                              mLoopCount;
330   int32_t                              mCurrentLoop;
331   bool                                 mForward;
332   bool                                 mUpdateFrameNumber;
333   bool                                 mNeedAnimationFinishedTrigger;
334   bool                                 mAnimationDataUpdated;
335   bool                                 mDestroyTask;
336   bool                                 mLoadRequest;
337   bool                                 mLoadFailed;
338 };
339
340 } // namespace Internal
341
342 } // namespace Toolkit
343
344 } // namespace Dali
345
346 #endif // DALI_TOOLKIT_VECTOR_ANIMATION_TASK_H