f2e3042162195d318f1bce122b7c47c72baecd75
[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] playRange The array to specify minimum and maximum progress.
121    * The animation will play between those values.
122    */
123   void SetPlayRange( Property::Array& playRange );
124
125   /**
126    * @brief Gets the playing range in frame number.
127    * @param[out] startFrame The frame number to specify minimum progress.
128    * @param[out] endFrame The frame number to specify maximum progress.
129    */
130   void GetPlayRange( uint32_t& startFrame, uint32_t& endFrame );
131
132   /**
133    * @brief Get the play state
134    * @return The play state
135    */
136   DevelImageVisual::PlayState::Type GetPlayState() const;
137
138   /**
139    * @brief Sets the current frame number of the animation.
140    * @param[in] frameNumber The new frame number between [0, the maximum frame number] or between the play range if specified.
141    */
142   void SetCurrentFrameNumber( uint32_t frameNumber );
143
144   /**
145    * @brief Retrieves the current frame number of the animation.
146    * @return The current frame number
147    */
148   uint32_t GetCurrentFrameNumber() const;
149
150   /**
151    * @brief Retrieves the total frame number of the animation.
152    * @return The total frame number
153    */
154   uint32_t GetTotalFrameNumber() const;
155
156   /**
157    * @brief Gets the default size of the file,.
158    * @return The default size of the file
159    */
160   void GetDefaultSize( uint32_t& width, uint32_t& height ) const;
161
162   /**
163    * @brief Sets the stop behavior of the animation. This is performed when the animation is stopped.
164    * @param[in] stopBehavior The stop behavior
165    */
166   void SetStopBehavior( DevelImageVisual::StopBehavior::Type stopBehavior );
167
168   /**
169    * @brief Sets the looping mode.
170    * Animation plays forwards and then restarts from the beginning or runs backwards again.
171    * @param[in] loopingMode The looping mode
172    */
173   void SetLoopingMode( DevelImageVisual::LoopingMode::Type loopingMode );
174
175   /**
176    * @brief Gets the layer information of all the child layers.
177    * @param[out] map The layer information
178    */
179   void GetLayerInfo( Property::Map& map ) const;
180
181   /**
182    * @brief Connect to this signal to be notified when the texture upload is completed.
183    * @return The signal to connect to.
184    */
185   UploadCompletedSignalType& UploadCompletedSignal();
186
187   /**
188    * @brief Rasterizes the current frame.
189    * @return true if the animation is running, false otherwise.
190    */
191   bool Rasterize();
192
193   /**
194    * @brief Calculates 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 > CalculateNextFrameTime( bool renderNow );
198
199   /**
200    * @brief Gets the time for the next frame rasterization.
201    * @return The time for the next frame rasterization.
202    */
203   std::chrono::time_point< std::chrono::system_clock > GetNextFrameTime();
204
205 private:
206
207   /**
208    * @brief Initializes the vector renderer.
209    */
210   void Initialize();
211
212   /**
213    * @brief Gets the frame number when the animation is stopped according to the stop behavior.
214    */
215   uint32_t GetStoppedFrame( uint32_t startFrame, uint32_t endFrame, uint32_t currentFrame );
216
217   // Undefined
218   VectorAnimationTask( const VectorAnimationTask& task ) = delete;
219
220   // Undefined
221   VectorAnimationTask& operator=( const VectorAnimationTask& task ) = delete;
222
223 private:
224
225   enum class PlayState
226   {
227     STOPPING,  ///< The animation is stopping
228     STOPPED,   ///< The animation has stopped
229     PLAYING,   ///< The animation is playing
230     PAUSED     ///< The animation is paused
231   };
232
233   std::string                            mUrl;
234   VectorAnimationRenderer                mVectorRenderer;
235   VectorAnimationThread&                 mVectorAnimationThread;
236   ConditionalWait                        mConditionalWait;
237   std::unique_ptr< EventThreadCallback > mAnimationFinishedTrigger;
238   Vector2                                mPlayRange;
239   PlayState                              mPlayState;
240   DevelImageVisual::StopBehavior::Type   mStopBehavior;
241   DevelImageVisual::LoopingMode::Type    mLoopingMode;
242   std::chrono::time_point< std::chrono::system_clock > mNextFrameStartTime;
243   int64_t                                mFrameDurationNanoSeconds;
244   float                                  mFrameRate;
245   uint32_t                               mCurrentFrame;
246   uint32_t                               mTotalFrame;
247   uint32_t                               mStartFrame;
248   uint32_t                               mEndFrame;
249   uint32_t                               mWidth;
250   uint32_t                               mHeight;
251   int32_t                                mLoopCount;
252   int32_t                                mCurrentLoop;
253   bool                                   mResourceReady;
254   bool                                   mCurrentFrameUpdated;
255   bool                                   mCurrentLoopUpdated;
256   bool                                   mForward;
257   bool                                   mUpdateFrameNumber;
258   bool                                   mNeedAnimationFinishedTrigger;
259
260 };
261
262 } // namespace Internal
263
264 } // namespace Toolkit
265
266 } // namespace Dali
267
268 #endif // DALI_TOOLKIT_VECTOR_ANIMATION_TASK_H