Merge "(Vector) Support dynamic properties" into 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    */
153   void RequestLoad(const std::string& url);
154
155   /**
156    * @brief Queries whether loading is requested.
157    * @return True if loading is requested.
158    */
159   bool IsLoadRequested() const;
160
161   /**
162    * @brief Sets data to specify animation playback.
163    * @param[in] data The animation data
164    */
165   void SetAnimationData(const AnimationData& data);
166
167   /**
168    * @brief This callback is called after the animation is finished.
169    * @param[in] callback The animation finished callback
170    */
171   void SetAnimationFinishedCallback(EventThreadCallback* callback);
172
173   /**
174    * @brief Gets the playing range in frame number.
175    * @param[out] startFrame The frame number to specify minimum progress.
176    * @param[out] endFrame The frame number to specify maximum progress.
177    */
178   void GetPlayRange(uint32_t& startFrame, uint32_t& endFrame);
179
180   /**
181    * @brief Retrieves the current frame number of the animation.
182    * @return The current frame number
183    */
184   uint32_t GetCurrentFrameNumber() const;
185
186   /**
187    * @brief Retrieves the total frame number of the animation.
188    * @return The total frame number
189    */
190   uint32_t GetTotalFrameNumber() const;
191
192   /**
193    * @brief Gets the default size of the file,.
194    * @return The default size of the file
195    */
196   void GetDefaultSize(uint32_t& width, uint32_t& height) const;
197
198   /**
199    * @brief Gets the layer information of all the child layers.
200    * @param[out] map The layer information
201    */
202   void GetLayerInfo(Property::Map& map) const;
203
204   /**
205    * @brief Connect to this signal to be notified when the resource is ready.
206    * @return The signal to connect to.
207    */
208   ResourceReadySignalType& ResourceReadySignal();
209
210   /**
211    * @brief Rasterizes the current frame.
212    * @param[out] keepAnimation true if the animation is running, false otherwise.
213    * @return true if the rasterization succeeded, false otherwise.
214    */
215   bool Rasterize(bool& keepAnimation);
216
217   /**
218    * @brief Calculates the time for the next frame rasterization.
219    * @return The time for the next frame rasterization.
220    */
221   TimePoint CalculateNextFrameTime(bool renderNow);
222
223   /**
224    * @brief Gets the time for the next frame rasterization.
225    * @return The time for the next frame rasterization.
226    */
227   TimePoint GetNextFrameTime();
228
229 private:
230   /**
231    * @brief Loads the animation file.
232    *
233    * @return True if loading succeeded, false otherwise.
234    */
235   bool Load();
236
237   /**
238    * @brief Play the vector animation.
239    */
240   void PlayAnimation();
241
242   /**
243    * @brief Stop the vector animation.
244    */
245   void StopAnimation();
246
247   /**
248    * @brief Pause the vector animation.
249    */
250   void PauseAnimation();
251
252   /**
253    * @brief Sets the target image size.
254    *
255    * @param[in] width The target image width
256    * @param[in] height The target image height
257    */
258   void SetSize(uint32_t width, uint32_t height);
259
260   /**
261    * @brief Enable looping for 'count' repeats. -1 means to repeat forever.
262    * @param[in] count The number of times to loop
263    */
264   void SetLoopCount(int32_t count);
265
266   /**
267    * @brief Set the playing range in frame number.
268    * @param[in] playRange The array to specify minimum and maximum progress.
269    * The animation will play between those values.
270    */
271   void SetPlayRange(const Property::Array& playRange);
272
273   /**
274    * @brief Sets the current frame number of the animation.
275    * @param[in] frameNumber The new frame number between [0, the maximum frame number] or between the play range if specified.
276    */
277   void SetCurrentFrameNumber(uint32_t frameNumber);
278
279   /**
280    * @brief Sets the stop behavior of the animation. This is performed when the animation is stopped.
281    * @param[in] stopBehavior The stop behavior
282    */
283   void SetStopBehavior(DevelImageVisual::StopBehavior::Type stopBehavior);
284
285   /**
286    * @brief Sets the looping mode.
287    * Animation plays forwards and then restarts from the beginning or runs backwards again.
288    * @param[in] loopingMode The looping mode
289    */
290   void SetLoopingMode(DevelImageVisual::LoopingMode::Type loopingMode);
291
292   /**
293    * @brief Gets the frame number when the animation is stopped according to the stop behavior.
294    */
295   uint32_t GetStoppedFrame(uint32_t startFrame, uint32_t endFrame, uint32_t currentFrame);
296
297   /**
298    * @brief Applies the animation data set by the main thread.
299    */
300   void ApplyAnimationData();
301
302   /**
303    * @brief Called when the texture upload is completed.
304    */
305   void OnUploadCompleted();
306
307   /**
308    * @brief Event callback from rasterize thread. This is called when the file loading is completed.
309    */
310   void OnLoadCompleted();
311
312   // Undefined
313   VectorAnimationTask(const VectorAnimationTask& task) = delete;
314
315   // Undefined
316   VectorAnimationTask& operator=(const VectorAnimationTask& task) = delete;
317
318 private:
319   enum class PlayState
320   {
321     STOPPING, ///< The animation is stopping
322     STOPPED,  ///< The animation has stopped
323     PLAYING,  ///< The animation is playing
324     PAUSED    ///< The animation is paused
325   };
326
327   std::string                          mUrl;
328   VectorAnimationRenderer              mVectorRenderer;
329   AnimationData                        mAnimationData[2];
330   VectorAnimationThread&               mVectorAnimationThread;
331   ConditionalWait                      mConditionalWait;
332   ResourceReadySignalType              mResourceReadySignal;
333   std::unique_ptr<EventThreadCallback> mAnimationFinishedTrigger;
334   std::unique_ptr<EventThreadCallback> mLoadCompletedTrigger;
335   PlayState                            mPlayState;
336   DevelImageVisual::StopBehavior::Type mStopBehavior;
337   DevelImageVisual::LoopingMode::Type  mLoopingMode;
338   TimePoint                            mNextFrameStartTime;
339   int64_t                              mFrameDurationMicroSeconds;
340   float                                mFrameRate;
341   uint32_t                             mCurrentFrame;
342   uint32_t                             mTotalFrame;
343   uint32_t                             mStartFrame;
344   uint32_t                             mEndFrame;
345   uint32_t                             mDroppedFrames;
346   uint32_t                             mWidth;
347   uint32_t                             mHeight;
348   uint32_t                             mAnimationDataIndex;
349   int32_t                              mLoopCount;
350   int32_t                              mCurrentLoop;
351   bool                                 mForward;
352   bool                                 mUpdateFrameNumber;
353   bool                                 mNeedAnimationFinishedTrigger;
354   bool                                 mAnimationDataUpdated;
355   bool                                 mDestroyTask;
356   bool                                 mLoadRequest;
357   bool                                 mLoadFailed;
358 };
359
360 } // namespace Internal
361
362 } // namespace Toolkit
363
364 } // namespace Dali
365
366 #endif // DALI_TOOLKIT_VECTOR_ANIMATION_TASK_H