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