[Tizen](Vector) Ensure that all animation data is applied at once
[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) 2019 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/public-api/object/property-array.h>
22 #include <dali/devel-api/adaptor-framework/event-thread-callback.h>
23 #include <dali/devel-api/adaptor-framework/vector-animation-renderer.h>
24 #include <dali/devel-api/threading/conditional-wait.h>
25 #include <memory>
26 #include <chrono>
27
28 // INTERNAL INCLUDES
29 #include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
30
31 namespace Dali
32 {
33
34 namespace Toolkit
35 {
36
37 namespace Internal
38 {
39
40 class VisualFactoryCache;
41 class VectorAnimationThread;
42 class VectorAnimationTask;
43 typedef IntrusivePtr< VectorAnimationTask > VectorAnimationTaskPtr;
44
45 /**
46  * The task of the vector animation.
47  */
48 class VectorAnimationTask : public RefObject
49 {
50 public:
51
52   using UploadCompletedSignalType = Dali::VectorAnimationRenderer::UploadCompletedSignalType;
53
54   /**
55    * Flags for re-sending data to the vector animation thread
56    */
57   enum ResendFlags
58   {
59     RESEND_PLAY_RANGE    = 1 << 0,
60     RESEND_LOOP_COUNT    = 1 << 1,
61     RESEND_STOP_BEHAVIOR = 1 << 2,
62     RESEND_LOOPING_MODE  = 1 << 3,
63     RESEND_CURRENT_FRAME = 1 << 4,
64     RESEND_SIZE          = 1 << 5,
65     RESEND_PLAY_STATE    = 1 << 6
66   };
67
68   /**
69    * @brief Structure used to pass parameters to the vector animation task
70    */
71   struct AnimationData
72   {
73     AnimationData()
74     : resendFlag( 0 ),
75       playRange(),
76       playState(),
77       stopBehavior( DevelImageVisual::StopBehavior::CURRENT_FRAME ),
78       loopingMode( DevelImageVisual::LoopingMode::RESTART ),
79       currentFrame( 0 ),
80       width( 0 ),
81       height( 0 ),
82       loopCount( -1 )
83     {
84     }
85
86     uint32_t                             resendFlag;
87     Property::Array                      playRange;
88     DevelImageVisual::PlayState::Type    playState;
89     DevelImageVisual::StopBehavior::Type stopBehavior;
90     DevelImageVisual::LoopingMode::Type  loopingMode;
91     uint32_t                             currentFrame;
92     uint32_t                             width;
93     uint32_t                             height;
94     int32_t                              loopCount;
95   };
96
97   /**
98    * @brief Constructor.
99    *
100    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
101    * @param[in] url The url of the vector animation file
102    */
103   VectorAnimationTask( VisualFactoryCache& factoryCache, const std::string& url );
104
105   /**
106    * @brief Destructor.
107    */
108   virtual ~VectorAnimationTask();
109
110   /**
111    * @brief Finalizes the task.
112    */
113   void Finalize();
114
115   /**
116    * @brief Sets the renderer used to display the result image.
117    *
118    * @param[in] renderer The renderer used to display the result image
119    */
120   void SetRenderer( Renderer renderer );
121
122   /**
123    * @brief Sets data to specify animation playback.
124    * @param[in] data The animation data
125    */
126   void SetAnimationData( const AnimationData& data );
127
128   /**
129    * @brief This callback is called after the animation is finished.
130    * @param[in] callback The animation finished callback
131    */
132   void SetAnimationFinishedCallback( EventThreadCallback* callback );
133
134   /**
135    * @brief Gets the playing range in frame number.
136    * @param[out] startFrame The frame number to specify minimum progress.
137    * @param[out] endFrame The frame number to specify maximum progress.
138    */
139   void GetPlayRange( uint32_t& startFrame, uint32_t& endFrame );
140
141   /**
142    * @brief Get the play state
143    * @return The play state
144    */
145   DevelImageVisual::PlayState::Type GetPlayState() const;
146
147   /**
148    * @brief Retrieves the current frame number of the animation.
149    * @return The current frame number
150    */
151   uint32_t GetCurrentFrameNumber() const;
152
153   /**
154    * @brief Retrieves the total frame number of the animation.
155    * @return The total frame number
156    */
157   uint32_t GetTotalFrameNumber() const;
158
159   /**
160    * @brief Gets the default size of the file,.
161    * @return The default size of the file
162    */
163   void GetDefaultSize( uint32_t& width, uint32_t& height ) const;
164
165   /**
166    * @brief Gets the layer information of all the child layers.
167    * @param[out] map The layer information
168    */
169   void GetLayerInfo( Property::Map& map ) const;
170
171   /**
172    * @brief Connect to this signal to be notified when the texture upload is completed.
173    * @return The signal to connect to.
174    */
175   UploadCompletedSignalType& UploadCompletedSignal();
176
177   /**
178    * @brief Rasterizes the current frame.
179    * @return true if the animation is running, false otherwise.
180    */
181   bool Rasterize();
182
183   /**
184    * @brief Calculates the time for the next frame rasterization.
185    * @return The time for the next frame rasterization.
186    */
187   std::chrono::time_point< std::chrono::system_clock > CalculateNextFrameTime( bool renderNow );
188
189   /**
190    * @brief Gets the time for the next frame rasterization.
191    * @return The time for the next frame rasterization.
192    */
193   std::chrono::time_point< std::chrono::system_clock > GetNextFrameTime();
194
195 private:
196
197   /**
198    * @brief Initializes the vector renderer.
199    */
200   void Initialize();
201
202   /**
203    * @brief Play the vector animation.
204    */
205   void PlayAnimation();
206
207   /**
208    * @brief Stop the vector animation.
209    */
210   void StopAnimation();
211
212   /**
213    * @brief Pause the vector animation.
214    */
215   void PauseAnimation();
216
217   /**
218    * @brief Render one frame. The current frame number will be increased.
219    */
220   void RenderFrame();
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   // Undefined
268   VectorAnimationTask( const VectorAnimationTask& task ) = delete;
269
270   // Undefined
271   VectorAnimationTask& operator=( const VectorAnimationTask& task ) = delete;
272
273 private:
274
275   enum class PlayState
276   {
277     STOPPING,  ///< The animation is stopping
278     STOPPED,   ///< The animation has stopped
279     PLAYING,   ///< The animation is playing
280     PAUSED     ///< The animation is paused
281   };
282
283   std::string                            mUrl;
284   VectorAnimationRenderer                mVectorRenderer;
285   VectorAnimationThread&                 mVectorAnimationThread;
286   ConditionalWait                        mConditionalWait;
287   std::unique_ptr< EventThreadCallback > mAnimationFinishedTrigger;
288   PlayState                              mPlayState;
289   DevelImageVisual::StopBehavior::Type   mStopBehavior;
290   DevelImageVisual::LoopingMode::Type    mLoopingMode;
291   std::chrono::time_point< std::chrono::system_clock > mNextFrameStartTime;
292   int64_t                                mFrameDurationNanoSeconds;
293   float                                  mFrameRate;
294   uint32_t                               mCurrentFrame;
295   uint32_t                               mTotalFrame;
296   uint32_t                               mStartFrame;
297   uint32_t                               mEndFrame;
298   uint32_t                               mWidth;
299   uint32_t                               mHeight;
300   int32_t                                mLoopCount;
301   int32_t                                mCurrentLoop;
302   bool                                   mResourceReady;
303   bool                                   mCurrentFrameUpdated;
304   bool                                   mCurrentLoopUpdated;
305   bool                                   mForward;
306   bool                                   mUpdateFrameNumber;
307   bool                                   mNeedAnimationFinishedTrigger;
308
309 };
310
311 } // namespace Internal
312
313 } // namespace Toolkit
314
315 } // namespace Dali
316
317 #endif // DALI_TOOLKIT_VECTOR_ANIMATION_TASK_H