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