ef262bd5ee71b5b23bb38318c5b948ba0678adc4
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / animated-vector-image / vector-rasterize-thread.h
1 #ifndef DALI_TOOLKIT_VECTOR_IMAGE_RASTERIZE_THREAD_H
2 #define DALI_TOOLKIT_VECTOR_IMAGE_RASTERIZE_THREAD_H
3
4 /*
5  * Copyright (c) 2018 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 <dali/devel-api/threading/mutex.h>
25 #include <dali/devel-api/threading/thread.h>
26 #include <dali/integration-api/adaptors/log-factory-interface.h>
27 #include <string>
28 #include <memory>
29
30 // INTERNAL INCLUDES
31 #include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
32
33 namespace Dali
34 {
35
36 namespace Toolkit
37 {
38
39 namespace Internal
40 {
41
42 /**
43  * The worker thread for vector image rasterization.
44  */
45 class VectorRasterizeThread : public Thread
46 {
47 public:
48
49   /**
50    * @brief Constructor.
51    *
52    * @param[in] url The url of the vector animation file
53    */
54   VectorRasterizeThread( const std::string& url );
55
56   /**
57    * @brief Destructor.
58    */
59   virtual ~VectorRasterizeThread();
60
61   /**
62    * @brief Sets the renderer used to display the result image.
63    *
64    * @param[in] renderer The renderer used to display the result image
65    */
66   void SetRenderer( Renderer renderer );
67
68   /**
69    * @brief Sets the target image size.
70    *
71    * @param[in] width The target image width
72    * @param[in] height The target image height
73    */
74   void SetSize( uint32_t width, uint32_t height );
75
76   /**
77    * @brief Play the vector animation.
78    */
79   void PlayAnimation();
80
81   /**
82    * @brief Stop the vector animation.
83    */
84   void StopAnimation();
85
86   /**
87    * @brief Pause the vector animation.
88    */
89   void PauseAnimation();
90
91   /**
92    * @brief Render one frame. The current frame number will be increased.
93    */
94   void RenderFrame();
95
96   /**
97    * @brief This callback is called after the first frame is ready.
98    * @param[in] callback The resource ready callback
99    */
100   void SetResourceReadyCallback( EventThreadCallback* callback );
101
102   /**
103    * @brief This callback is called after the animation is finished.
104    * @param[in] callback The animation finished callback
105    */
106   void SetAnimationFinishedCallback( EventThreadCallback* callback );
107
108   /**
109    * @brief Enable looping for 'count' repeats. -1 means to repeat forever.
110    * @param[in] count The number of times to loop
111    */
112   void SetLoopCount( int32_t count );
113
114   /**
115    * @brief Set the playing range in frame number.
116    * @param[in] startFrame The frame number to specify minimum progress.
117    * @param[in] endFrame The frame number to specify maximum progress.
118    * The animation will play between those values.
119    */
120   void SetPlayRange( uint32_t startFrame, uint32_t endFrame );
121
122   /**
123    * @brief Get the play state
124    * @return The play state
125    */
126   DevelImageVisual::PlayState::Type GetPlayState() const;
127
128   /**
129    * @brief Queries whether the resource is ready.
130    * @return true if ready, false otherwise
131    */
132   bool IsResourceReady() 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 protected:
172
173   /**
174    * @brief The entry function of the worker thread.
175    *        It rasterizes the vector image.
176    */
177   void Run() override;
178
179 private:
180
181   /**
182    * @brief Initializes the vector renderer.
183    */
184   void Initialize();
185
186   /**
187    * @brief Rasterizes the current frame.
188    */
189   void Rasterize();
190
191   /**
192    * @brief Gets the frame number when the animation is stopped according to the stop behavior.
193    */
194   uint32_t GetStoppedFrame( uint32_t startFrame, uint32_t endFrame, uint32_t currentFrame );
195
196   // Undefined
197   VectorRasterizeThread( const VectorRasterizeThread& thread ) = delete;
198
199   // Undefined
200   VectorRasterizeThread& operator=( const VectorRasterizeThread& thread ) = delete;
201
202 private:
203
204   enum class PlayState
205   {
206     STOPPING,  ///< The animation is stopping
207     STOPPED,   ///< The animation has stopped
208     PLAYING,   ///< The animation is playing
209     PAUSED     ///< The animation is paused
210   };
211
212   std::string                 mUrl;
213   VectorAnimationRenderer     mVectorRenderer;
214   ConditionalWait             mConditionalWait;
215   std::unique_ptr< EventThreadCallback > mResourceReadyTrigger;
216   std::unique_ptr< EventThreadCallback > mAnimationFinishedTrigger;
217   Vector2                     mPlayRange;
218   PlayState                   mPlayState;
219   DevelImageVisual::StopBehavior::Type mStopBehavior;
220   DevelImageVisual::LoopingMode::Type mLoopingMode;
221   int64_t                     mFrameDurationNanoSeconds;
222   float                       mFrameRate;
223   uint32_t                    mCurrentFrame;
224   uint32_t                    mTotalFrame;
225   uint32_t                    mStartFrame;
226   uint32_t                    mEndFrame;
227   uint32_t                    mWidth;
228   uint32_t                    mHeight;
229   int32_t                     mLoopCount;
230   int32_t                     mCurrentLoop;
231   bool                        mNeedRender;
232   bool                        mDestroyThread;  ///< Whether the thread be destroyed
233   bool                        mResourceReady;
234   bool                        mCurrentFrameUpdated;
235   bool                        mForward;
236   bool                        mUpdateFrameNumber;
237   const Dali::LogFactoryInterface& mLogFactory; ///< The log factory
238
239 };
240
241 } // namespace Internal
242
243 } // namespace Toolkit
244
245 } // namespace Dali
246
247 #endif // DALI_TOOLKIT_VECTOR_IMAGE_RASTERIZE_THREAD_H