Merge branch 'devel/master' into devel/graphics
[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) 2021 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
46 {
47 public:
48   using UploadCompletedSignalType = Dali::VectorAnimationRenderer::UploadCompletedSignalType;
49
50   /**
51    * Flags for re-sending data to the vector animation thread
52    */
53   enum ResendFlags
54   {
55     RESEND_PLAY_RANGE    = 1 << 0,
56     RESEND_LOOP_COUNT    = 1 << 1,
57     RESEND_STOP_BEHAVIOR = 1 << 2,
58     RESEND_LOOPING_MODE  = 1 << 3,
59     RESEND_CURRENT_FRAME = 1 << 4,
60     RESEND_SIZE          = 1 << 5,
61     RESEND_PLAY_STATE    = 1 << 6
62   };
63
64   /**
65    * @brief Structure used to pass parameters to the vector animation task
66    */
67   struct AnimationData
68   {
69     AnimationData()
70     : resendFlag(0),
71       playRange(),
72       playState(),
73       stopBehavior(DevelImageVisual::StopBehavior::CURRENT_FRAME),
74       loopingMode(DevelImageVisual::LoopingMode::RESTART),
75       currentFrame(0),
76       width(0),
77       height(0),
78       loopCount(-1)
79     {
80     }
81
82     AnimationData& operator=(const AnimationData& rhs)
83     {
84       resendFlag |= rhs.resendFlag; // OR resend flag
85       playRange    = rhs.playRange;
86       playState    = rhs.playState;
87       stopBehavior = rhs.stopBehavior;
88       loopingMode  = rhs.loopingMode;
89       currentFrame = rhs.currentFrame;
90       width        = rhs.width;
91       height       = rhs.height;
92       loopCount    = rhs.loopCount;
93       return *this;
94     }
95
96     uint32_t                             resendFlag;
97     Property::Array                      playRange;
98     DevelImageVisual::PlayState::Type    playState;
99     DevelImageVisual::StopBehavior::Type stopBehavior;
100     DevelImageVisual::LoopingMode::Type  loopingMode;
101     uint32_t                             currentFrame;
102     uint32_t                             width;
103     uint32_t                             height;
104     int32_t                              loopCount;
105   };
106
107   /**
108    * @brief Constructor.
109    *
110    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
111    * @param[in] url The url of the vector animation file
112    */
113   VectorAnimationTask(VisualFactoryCache& factoryCache, const std::string& url);
114
115   /**
116    * @brief Destructor.
117    */
118   ~VectorAnimationTask() override;
119
120   /**
121    * @brief Finalizes the task.
122    */
123   void Finalize();
124
125   /**
126    * @brief Sets the renderer used to display the result image.
127    *
128    * @param[in] renderer The renderer used to display the result image
129    */
130   void SetRenderer(Renderer renderer);
131
132   /**
133    * @brief Sets data to specify animation playback.
134    * @param[in] data The animation data
135    */
136   void SetAnimationData(const AnimationData& data);
137
138   /**
139    * @brief This callback is called after the animation is finished.
140    * @param[in] callback The animation finished callback
141    */
142   void SetAnimationFinishedCallback(EventThreadCallback* callback);
143
144   /**
145    * @brief Gets the playing range in frame number.
146    * @param[out] startFrame The frame number to specify minimum progress.
147    * @param[out] endFrame The frame number to specify maximum progress.
148    */
149   void GetPlayRange(uint32_t& startFrame, uint32_t& endFrame);
150
151   /**
152    * @brief Retrieves the current frame number of the animation.
153    * @return The current frame number
154    */
155   uint32_t GetCurrentFrameNumber() const;
156
157   /**
158    * @brief Retrieves the total frame number of the animation.
159    * @return The total frame number
160    */
161   uint32_t GetTotalFrameNumber() const;
162
163   /**
164    * @brief Gets the default size of the file,.
165    * @return The default size of the file
166    */
167   void GetDefaultSize(uint32_t& width, uint32_t& height) const;
168
169   /**
170    * @brief Gets the layer information of all the child layers.
171    * @param[out] map The layer information
172    */
173   void GetLayerInfo(Property::Map& map) const;
174
175   /**
176    * @brief Connect to this signal to be notified when the texture upload is completed.
177    * @return The signal to connect to.
178    */
179   UploadCompletedSignalType& UploadCompletedSignal();
180
181   /**
182    * @brief Rasterizes the current frame.
183    * @return true if the animation is running, false otherwise.
184    */
185   bool Rasterize();
186
187   /**
188    * @brief Calculates the time for the next frame rasterization.
189    * @return The time for the next frame rasterization.
190    */
191   std::chrono::time_point<std::chrono::system_clock> CalculateNextFrameTime(bool renderNow);
192
193   /**
194    * @brief Gets the time for the next frame rasterization.
195    * @return The time for the next frame rasterization.
196    */
197   std::chrono::time_point<std::chrono::system_clock> GetNextFrameTime();
198
199 private:
200   /**
201    * @brief Initializes the vector renderer.
202    */
203   void Initialize();
204
205   /**
206    * @brief Play the vector animation.
207    */
208   void PlayAnimation();
209
210   /**
211    * @brief Stop the vector animation.
212    */
213   void StopAnimation();
214
215   /**
216    * @brief Pause the vector animation.
217    */
218   void PauseAnimation();
219
220   /**
221    * @brief Sets the target image size.
222    *
223    * @param[in] width The target image width
224    * @param[in] height The target image height
225    */
226   void SetSize(uint32_t width, uint32_t height);
227
228   /**
229    * @brief Enable looping for 'count' repeats. -1 means to repeat forever.
230    * @param[in] count The number of times to loop
231    */
232   void SetLoopCount(int32_t count);
233
234   /**
235    * @brief Set the playing range in frame number.
236    * @param[in] playRange The array to specify minimum and maximum progress.
237    * The animation will play between those values.
238    */
239   void SetPlayRange(const Property::Array& playRange);
240
241   /**
242    * @brief Sets the current frame number of the animation.
243    * @param[in] frameNumber The new frame number between [0, the maximum frame number] or between the play range if specified.
244    */
245   void SetCurrentFrameNumber(uint32_t frameNumber);
246
247   /**
248    * @brief Sets the stop behavior of the animation. This is performed when the animation is stopped.
249    * @param[in] stopBehavior The stop behavior
250    */
251   void SetStopBehavior(DevelImageVisual::StopBehavior::Type stopBehavior);
252
253   /**
254    * @brief Sets the looping mode.
255    * Animation plays forwards and then restarts from the beginning or runs backwards again.
256    * @param[in] loopingMode The looping mode
257    */
258   void SetLoopingMode(DevelImageVisual::LoopingMode::Type loopingMode);
259
260   /**
261    * @brief Gets the frame number when the animation is stopped according to the stop behavior.
262    */
263   uint32_t GetStoppedFrame(uint32_t startFrame, uint32_t endFrame, uint32_t currentFrame);
264
265   /**
266    * @brief Applies the animation data set by the main thread.
267    */
268   void ApplyAnimationData();
269
270   // Undefined
271   VectorAnimationTask(const VectorAnimationTask& task) = delete;
272
273   // Undefined
274   VectorAnimationTask& operator=(const VectorAnimationTask& task) = delete;
275
276 private:
277   enum class PlayState
278   {
279     STOPPING, ///< The animation is stopping
280     STOPPED,  ///< The animation has stopped
281     PLAYING,  ///< The animation is playing
282     PAUSED    ///< The animation is paused
283   };
284
285   std::string                                        mUrl;
286   VectorAnimationRenderer                            mVectorRenderer;
287   AnimationData                                      mAnimationData[2];
288   VectorAnimationThread&                             mVectorAnimationThread;
289   ConditionalWait                                    mConditionalWait;
290   std::unique_ptr<EventThreadCallback>               mAnimationFinishedTrigger;
291   PlayState                                          mPlayState;
292   DevelImageVisual::StopBehavior::Type               mStopBehavior;
293   DevelImageVisual::LoopingMode::Type                mLoopingMode;
294   std::chrono::time_point<std::chrono::system_clock> mNextFrameStartTime;
295   int64_t                                            mFrameDurationNanoSeconds;
296   float                                              mFrameRate;
297   uint32_t                                           mCurrentFrame;
298   uint32_t                                           mTotalFrame;
299   uint32_t                                           mStartFrame;
300   uint32_t                                           mEndFrame;
301   uint32_t                                           mWidth;
302   uint32_t                                           mHeight;
303   uint32_t                                           mAnimationDataIndex;
304   int32_t                                            mLoopCount;
305   int32_t                                            mCurrentLoop;
306   bool                                               mForward;
307   bool                                               mUpdateFrameNumber;
308   bool                                               mNeedAnimationFinishedTrigger;
309   bool                                               mAnimationDataUpdated;
310   bool                                               mDestroyTask;
311 };
312
313 } // namespace Internal
314
315 } // namespace Toolkit
316
317 } // namespace Dali
318
319 #endif // DALI_TOOLKIT_VECTOR_ANIMATION_TASK_H