Add post processor
[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    */
112   VectorAnimationTask(VisualFactoryCache& factoryCache);
113
114   /**
115    * @brief Destructor.
116    */
117   ~VectorAnimationTask() override;
118
119   /**
120    * @brief Finalizes the task.
121    */
122   void Finalize();
123
124   /**
125    * @brief Loads the animation file.
126    *
127    * @param[in] url The url of the vector animation file
128    * @return True if loading success, false otherwise.
129    */
130   bool Load(const std::string& url);
131
132   /**
133    * @brief Sets the renderer used to display the result image.
134    *
135    * @param[in] renderer The renderer used to display the result image
136    */
137   void SetRenderer(Renderer renderer);
138
139   /**
140    * @brief Sets data to specify animation playback.
141    * @param[in] data The animation data
142    */
143   void SetAnimationData(const AnimationData& data);
144
145   /**
146    * @brief This callback is called after the animation is finished.
147    * @param[in] callback The animation finished callback
148    */
149   void SetAnimationFinishedCallback(EventThreadCallback* callback);
150
151   /**
152    * @brief Gets the playing range in frame number.
153    * @param[out] startFrame The frame number to specify minimum progress.
154    * @param[out] endFrame The frame number to specify maximum progress.
155    */
156   void GetPlayRange(uint32_t& startFrame, uint32_t& endFrame);
157
158   /**
159    * @brief Retrieves the current frame number of the animation.
160    * @return The current frame number
161    */
162   uint32_t GetCurrentFrameNumber() const;
163
164   /**
165    * @brief Retrieves the total frame number of the animation.
166    * @return The total frame number
167    */
168   uint32_t GetTotalFrameNumber() const;
169
170   /**
171    * @brief Gets the default size of the file,.
172    * @return The default size of the file
173    */
174   void GetDefaultSize(uint32_t& width, uint32_t& height) const;
175
176   /**
177    * @brief Gets the layer information of all the child layers.
178    * @param[out] map The layer information
179    */
180   void GetLayerInfo(Property::Map& map) const;
181
182   /**
183    * @brief Connect to this signal to be notified when the texture upload is completed.
184    * @return The signal to connect to.
185    */
186   UploadCompletedSignalType& UploadCompletedSignal();
187
188   /**
189    * @brief Rasterizes the current frame.
190    * @return true if the animation is running, false otherwise.
191    */
192   bool Rasterize();
193
194   /**
195    * @brief Calculates the time for the next frame rasterization.
196    * @return The time for the next frame rasterization.
197    */
198   std::chrono::time_point<std::chrono::system_clock> CalculateNextFrameTime(bool renderNow);
199
200   /**
201    * @brief Gets the time for the next frame rasterization.
202    * @return The time for the next frame rasterization.
203    */
204   std::chrono::time_point<std::chrono::system_clock> GetNextFrameTime();
205
206 private:
207   /**
208    * @brief Play the vector animation.
209    */
210   void PlayAnimation();
211
212   /**
213    * @brief Stop the vector animation.
214    */
215   void StopAnimation();
216
217   /**
218    * @brief Pause the vector animation.
219    */
220   void PauseAnimation();
221
222   /**
223    * @brief Sets the target image size.
224    *
225    * @param[in] width The target image width
226    * @param[in] height The target image height
227    */
228   void SetSize(uint32_t width, uint32_t height);
229
230   /**
231    * @brief Enable looping for 'count' repeats. -1 means to repeat forever.
232    * @param[in] count The number of times to loop
233    */
234   void SetLoopCount(int32_t count);
235
236   /**
237    * @brief Set the playing range in frame number.
238    * @param[in] playRange The array to specify minimum and maximum progress.
239    * The animation will play between those values.
240    */
241   void SetPlayRange(const Property::Array& playRange);
242
243   /**
244    * @brief Sets the current frame number of the animation.
245    * @param[in] frameNumber The new frame number between [0, the maximum frame number] or between the play range if specified.
246    */
247   void SetCurrentFrameNumber(uint32_t frameNumber);
248
249   /**
250    * @brief Sets the stop behavior of the animation. This is performed when the animation is stopped.
251    * @param[in] stopBehavior The stop behavior
252    */
253   void SetStopBehavior(DevelImageVisual::StopBehavior::Type stopBehavior);
254
255   /**
256    * @brief Sets the looping mode.
257    * Animation plays forwards and then restarts from the beginning or runs backwards again.
258    * @param[in] loopingMode The looping mode
259    */
260   void SetLoopingMode(DevelImageVisual::LoopingMode::Type loopingMode);
261
262   /**
263    * @brief Gets the frame number when the animation is stopped according to the stop behavior.
264    */
265   uint32_t GetStoppedFrame(uint32_t startFrame, uint32_t endFrame, uint32_t currentFrame);
266
267   /**
268    * @brief Applies the animation data set by the main thread.
269    */
270   void ApplyAnimationData();
271
272   // Undefined
273   VectorAnimationTask(const VectorAnimationTask& task) = delete;
274
275   // Undefined
276   VectorAnimationTask& operator=(const VectorAnimationTask& task) = delete;
277
278 private:
279   enum class PlayState
280   {
281     STOPPING, ///< The animation is stopping
282     STOPPED,  ///< The animation has stopped
283     PLAYING,  ///< The animation is playing
284     PAUSED    ///< The animation is paused
285   };
286
287   std::string                                        mUrl;
288   VectorAnimationRenderer                            mVectorRenderer;
289   AnimationData                                      mAnimationData[2];
290   VectorAnimationThread&                             mVectorAnimationThread;
291   ConditionalWait                                    mConditionalWait;
292   std::unique_ptr<EventThreadCallback>               mAnimationFinishedTrigger;
293   PlayState                                          mPlayState;
294   DevelImageVisual::StopBehavior::Type               mStopBehavior;
295   DevelImageVisual::LoopingMode::Type                mLoopingMode;
296   std::chrono::time_point<std::chrono::system_clock> mNextFrameStartTime;
297   int64_t                                            mFrameDurationNanoSeconds;
298   float                                              mFrameRate;
299   uint32_t                                           mCurrentFrame;
300   uint32_t                                           mTotalFrame;
301   uint32_t                                           mStartFrame;
302   uint32_t                                           mEndFrame;
303   uint32_t                                           mWidth;
304   uint32_t                                           mHeight;
305   uint32_t                                           mAnimationDataIndex;
306   int32_t                                            mLoopCount;
307   int32_t                                            mCurrentLoop;
308   bool                                               mForward;
309   bool                                               mUpdateFrameNumber;
310   bool                                               mNeedAnimationFinishedTrigger;
311   bool                                               mAnimationDataUpdated;
312   bool                                               mDestroyTask;
313 };
314
315 } // namespace Internal
316
317 } // namespace Toolkit
318
319 } // namespace Dali
320
321 #endif // DALI_TOOLKIT_VECTOR_ANIMATION_TASK_H