[dali_1.4.45] Merge branch '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) 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/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 <memory>
25 #include <chrono>
26
27 // INTERNAL INCLUDES
28 #include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
29
30 namespace Dali
31 {
32
33 namespace Toolkit
34 {
35
36 namespace Internal
37 {
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
48 {
49 public:
50
51   using UploadCompletedSignalType = Dali::VectorAnimationRenderer::UploadCompletedSignalType;
52
53   /**
54    * @brief Constructor.
55    *
56    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
57    * @param[in] url The url of the vector animation file
58    */
59   VectorAnimationTask( VisualFactoryCache& factoryCache, const std::string& url );
60
61   /**
62    * @brief Destructor.
63    */
64   virtual ~VectorAnimationTask();
65
66   /**
67    * @brief Finalizes the task.
68    */
69   void Finalize();
70
71   /**
72    * @brief Sets the renderer used to display the result image.
73    *
74    * @param[in] renderer The renderer used to display the result image
75    */
76   void SetRenderer( Renderer renderer );
77
78   /**
79    * @brief Sets the target image size.
80    *
81    * @param[in] width The target image width
82    * @param[in] height The target image height
83    */
84   void SetSize( uint32_t width, uint32_t height );
85
86   /**
87    * @brief Play the vector animation.
88    */
89   void PlayAnimation();
90
91   /**
92    * @brief Stop the vector animation.
93    */
94   void StopAnimation();
95
96   /**
97    * @brief Pause the vector animation.
98    */
99   void PauseAnimation();
100
101   /**
102    * @brief Render one frame. The current frame number will be increased.
103    */
104   void RenderFrame();
105
106   /**
107    * @brief This callback is called after the animation is finished.
108    * @param[in] callback The animation finished callback
109    */
110   void SetAnimationFinishedCallback( EventThreadCallback* callback );
111
112   /**
113    * @brief Enable looping for 'count' repeats. -1 means to repeat forever.
114    * @param[in] count The number of times to loop
115    */
116   void SetLoopCount( int32_t count );
117
118   /**
119    * @brief Set the playing range in frame number.
120    * @param[in] startFrame The frame number to specify minimum progress.
121    * @param[in] endFrame The frame number to specify maximum progress.
122    * The animation will play between those values.
123    */
124   void SetPlayRange( uint32_t startFrame, uint32_t endFrame );
125
126   /**
127    * @brief Gets the playing range in frame number.
128    * @param[out] startFrame The frame number to specify minimum progress.
129    * @param[out] endFrame The frame number to specify maximum progress.
130    */
131   void GetPlayRange( uint32_t& startFrame, uint32_t& endFrame );
132
133   /**
134    * @brief Get the play state
135    * @return The play state
136    */
137   DevelImageVisual::PlayState::Type GetPlayState() const;
138
139   /**
140    * @brief Sets the current frame number of the animation.
141    * @param[in] frameNumber The new frame number between [0, the maximum frame number] or between the play range if specified.
142    */
143   void SetCurrentFrameNumber( uint32_t frameNumber );
144
145   /**
146    * @brief Retrieves the current frame number of the animation.
147    * @return The current frame number
148    */
149   uint32_t GetCurrentFrameNumber() const;
150
151   /**
152    * @brief Retrieves the total frame number of the animation.
153    * @return The total frame number
154    */
155   uint32_t GetTotalFrameNumber() const;
156
157   /**
158    * @brief Gets the default size of the file,.
159    * @return The default size of the file
160    */
161   void GetDefaultSize( uint32_t& width, uint32_t& height ) const;
162
163   /**
164    * @brief Sets the stop behavior of the animation. This is performed when the animation is stopped.
165    * @param[in] stopBehavior The stop behavior
166    */
167   void SetStopBehavior( DevelImageVisual::StopBehavior::Type stopBehavior );
168
169   /**
170    * @brief Sets the looping mode.
171    * Animation plays forwards and then restarts from the beginning or runs backwards again.
172    * @param[in] loopingMode The looping mode
173    */
174   void SetLoopingMode( DevelImageVisual::LoopingMode::Type loopingMode );
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   /**
209    * @brief Initializes the vector renderer.
210    */
211   void Initialize();
212
213   /**
214    * @brief Gets the frame number when the animation is stopped according to the stop behavior.
215    */
216   uint32_t GetStoppedFrame( uint32_t startFrame, uint32_t endFrame, uint32_t currentFrame );
217
218   // Undefined
219   VectorAnimationTask( const VectorAnimationTask& task ) = delete;
220
221   // Undefined
222   VectorAnimationTask& operator=( const VectorAnimationTask& task ) = delete;
223
224 private:
225
226   enum class PlayState
227   {
228     STOPPING,  ///< The animation is stopping
229     STOPPED,   ///< The animation has stopped
230     PLAYING,   ///< The animation is playing
231     PAUSED     ///< The animation is paused
232   };
233
234   std::string                            mUrl;
235   VectorAnimationRenderer                mVectorRenderer;
236   VectorAnimationThread&                 mVectorAnimationThread;
237   ConditionalWait                        mConditionalWait;
238   std::unique_ptr< EventThreadCallback > mAnimationFinishedTrigger;
239   Vector2                                mPlayRange;
240   PlayState                              mPlayState;
241   DevelImageVisual::StopBehavior::Type   mStopBehavior;
242   DevelImageVisual::LoopingMode::Type    mLoopingMode;
243   std::chrono::time_point< std::chrono::system_clock > mNextFrameStartTime;
244   int64_t                                mFrameDurationNanoSeconds;
245   float                                  mFrameRate;
246   uint32_t                               mCurrentFrame;
247   uint32_t                               mTotalFrame;
248   uint32_t                               mStartFrame;
249   uint32_t                               mEndFrame;
250   uint32_t                               mWidth;
251   uint32_t                               mHeight;
252   int32_t                                mLoopCount;
253   int32_t                                mCurrentLoop;
254   bool                                   mResourceReady;
255   bool                                   mCurrentFrameUpdated;
256   bool                                   mCurrentLoopUpdated;
257   bool                                   mForward;
258   bool                                   mUpdateFrameNumber;
259   bool                                   mNeedAnimationFinishedTrigger;
260
261 };
262
263 } // namespace Internal
264
265 } // namespace Toolkit
266
267 } // namespace Dali
268
269 #endif // DALI_TOOLKIT_VECTOR_ANIMATION_TASK_H