(Vector) Add CONTENT_INFO property
[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 Sets the renderer used to display the result image.
68    *
69    * @param[in] renderer The renderer used to display the result image
70    */
71   void SetRenderer( Renderer renderer );
72
73   /**
74    * @brief Sets the target image size.
75    *
76    * @param[in] width The target image width
77    * @param[in] height The target image height
78    */
79   void SetSize( uint32_t width, uint32_t height );
80
81   /**
82    * @brief Play the vector animation.
83    */
84   void PlayAnimation();
85
86   /**
87    * @brief Stop the vector animation.
88    */
89   void StopAnimation();
90
91   /**
92    * @brief Pause the vector animation.
93    */
94   void PauseAnimation();
95
96   /**
97    * @brief Render one frame. The current frame number will be increased.
98    */
99   void RenderFrame();
100
101   /**
102    * @brief This callback is called after the animation is finished.
103    * @param[in] callback The animation finished callback
104    */
105   void SetAnimationFinishedCallback( EventThreadCallback* callback );
106
107   /**
108    * @brief Enable looping for 'count' repeats. -1 means to repeat forever.
109    * @param[in] count The number of times to loop
110    */
111   void SetLoopCount( int32_t count );
112
113   /**
114    * @brief Set the playing range in frame number.
115    * @param[in] startFrame The frame number to specify minimum progress.
116    * @param[in] endFrame The frame number to specify maximum progress.
117    * The animation will play between those values.
118    */
119   void SetPlayRange( uint32_t startFrame, uint32_t endFrame );
120
121   /**
122    * @brief Gets the playing range in frame number.
123    * @param[out] startFrame The frame number to specify minimum progress.
124    * @param[out] endFrame The frame number to specify maximum progress.
125    */
126   void GetPlayRange( uint32_t& startFrame, uint32_t& endFrame );
127
128   /**
129    * @brief Get the play state
130    * @return The play state
131    */
132   DevelImageVisual::PlayState::Type GetPlayState() const;
133
134   /**
135    * @brief Sets the current frame number of the animation.
136    * @param[in] frameNumber The new frame number between [0, the maximum frame number] or between the play range if specified.
137    */
138   void SetCurrentFrameNumber( uint32_t frameNumber );
139
140   /**
141    * @brief Retrieves the current frame number of the animation.
142    * @return The current frame number
143    */
144   uint32_t GetCurrentFrameNumber() const;
145
146   /**
147    * @brief Retrieves the total frame number of the animation.
148    * @return The total frame number
149    */
150   uint32_t GetTotalFrameNumber() const;
151
152   /**
153    * @brief Gets the default size of the file,.
154    * @return The default size of the file
155    */
156   void GetDefaultSize( uint32_t& width, uint32_t& height ) const;
157
158   /**
159    * @brief Sets the stop behavior of the animation. This is performed when the animation is stopped.
160    * @param[in] stopBehavior The stop behavior
161    */
162   void SetStopBehavior( DevelImageVisual::StopBehavior::Type stopBehavior );
163
164   /**
165    * @brief Sets the looping mode.
166    * Animation plays forwards and then restarts from the beginning or runs backwards again.
167    * @param[in] loopingMode The looping mode
168    */
169   void SetLoopingMode( DevelImageVisual::LoopingMode::Type loopingMode );
170
171   /**
172    * @brief Gets the layer information of all the child layers.
173    * @param[out] map The layer information
174    */
175   void GetLayerInfo( Property::Map& map ) const;
176
177   /**
178    * @brief Connect to this signal to be notified when the texture upload is completed.
179    * @return The signal to connect to.
180    */
181   UploadCompletedSignalType& UploadCompletedSignal();
182
183   /**
184    * @brief Rasterizes the current frame.
185    * @return true if the animation is running, false otherwise.
186    */
187   bool Rasterize();
188
189   /**
190    * @brief Calculates 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 > CalculateNextFrameTime( bool renderNow );
194
195   /**
196    * @brief Gets the time for the next frame rasterization.
197    * @return The time for the next frame rasterization.
198    */
199   std::chrono::time_point< std::chrono::system_clock > GetNextFrameTime();
200
201 private:
202
203   /**
204    * @brief Initializes the vector renderer.
205    */
206   void Initialize();
207
208   /**
209    * @brief Gets the frame number when the animation is stopped according to the stop behavior.
210    */
211   uint32_t GetStoppedFrame( uint32_t startFrame, uint32_t endFrame, uint32_t currentFrame );
212
213   // Undefined
214   VectorAnimationTask( const VectorAnimationTask& task ) = delete;
215
216   // Undefined
217   VectorAnimationTask& operator=( const VectorAnimationTask& task ) = delete;
218
219 private:
220
221   enum class PlayState
222   {
223     STOPPING,  ///< The animation is stopping
224     STOPPED,   ///< The animation has stopped
225     PLAYING,   ///< The animation is playing
226     PAUSED     ///< The animation is paused
227   };
228
229   std::string                            mUrl;
230   VectorAnimationRenderer                mVectorRenderer;
231   VectorAnimationThread&                 mVectorAnimationThread;
232   ConditionalWait                        mConditionalWait;
233   std::unique_ptr< EventThreadCallback > mAnimationFinishedTrigger;
234   Vector2                                mPlayRange;
235   PlayState                              mPlayState;
236   DevelImageVisual::StopBehavior::Type   mStopBehavior;
237   DevelImageVisual::LoopingMode::Type    mLoopingMode;
238   std::chrono::time_point< std::chrono::system_clock > mNextFrameStartTime;
239   int64_t                                mFrameDurationNanoSeconds;
240   float                                  mFrameRate;
241   uint32_t                               mCurrentFrame;
242   uint32_t                               mTotalFrame;
243   uint32_t                               mStartFrame;
244   uint32_t                               mEndFrame;
245   uint32_t                               mWidth;
246   uint32_t                               mHeight;
247   int32_t                                mLoopCount;
248   int32_t                                mCurrentLoop;
249   bool                                   mResourceReady;
250   bool                                   mCurrentFrameUpdated;
251   bool                                   mCurrentLoopUpdated;
252   bool                                   mForward;
253   bool                                   mUpdateFrameNumber;
254   bool                                   mNeedAnimationFinishedTrigger;
255
256 };
257
258 } // namespace Internal
259
260 } // namespace Toolkit
261
262 } // namespace Dali
263
264 #endif // DALI_TOOLKIT_VECTOR_ANIMATION_TASK_H