[dali_1.4.41] Merge branch 'devel/master'
[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   using UploadCompletedSignalType = Dali::VectorAnimationRenderer::UploadCompletedSignalType;
50
51   /**
52    * @brief Constructor.
53    *
54    * @param[in] url The url of the vector animation file
55    */
56   VectorRasterizeThread( const std::string& url );
57
58   /**
59    * @brief Destructor.
60    */
61   virtual ~VectorRasterizeThread();
62
63   /**
64    * @brief Sets the renderer used to display the result image.
65    *
66    * @param[in] renderer The renderer used to display the result image
67    */
68   void SetRenderer( Renderer renderer );
69
70   /**
71    * @brief Sets the target image size.
72    *
73    * @param[in] width The target image width
74    * @param[in] height The target image height
75    */
76   void SetSize( uint32_t width, uint32_t height );
77
78   /**
79    * @brief Play the vector animation.
80    */
81   void PlayAnimation();
82
83   /**
84    * @brief Stop the vector animation.
85    */
86   void StopAnimation();
87
88   /**
89    * @brief Pause the vector animation.
90    */
91   void PauseAnimation();
92
93   /**
94    * @brief Render one frame. The current frame number will be increased.
95    */
96   void RenderFrame();
97
98   /**
99    * @brief This callback is called after the animation is finished.
100    * @param[in] callback The animation finished callback
101    */
102   void SetAnimationFinishedCallback( EventThreadCallback* callback );
103
104   /**
105    * @brief Enable looping for 'count' repeats. -1 means to repeat forever.
106    * @param[in] count The number of times to loop
107    */
108   void SetLoopCount( int32_t count );
109
110   /**
111    * @brief Set the playing range in frame number.
112    * @param[in] startFrame The frame number to specify minimum progress.
113    * @param[in] endFrame The frame number to specify maximum progress.
114    * The animation will play between those values.
115    */
116   void SetPlayRange( uint32_t startFrame, uint32_t endFrame );
117
118   /**
119    * @brief Get the play state
120    * @return The play state
121    */
122   DevelImageVisual::PlayState::Type GetPlayState() const;
123
124   /**
125    * @brief Queries whether the resource is ready.
126    * @return true if ready, false otherwise
127    */
128   bool IsResourceReady() const;
129
130   /**
131    * @brief Sets the current frame number of the animation.
132    * @param[in] frameNumber The new frame number between [0, the maximum frame number] or between the play range if specified.
133    */
134   void SetCurrentFrameNumber( uint32_t frameNumber );
135
136   /**
137    * @brief Retrieves the current frame number of the animation.
138    * @return The current frame number
139    */
140   uint32_t GetCurrentFrameNumber() const;
141
142   /**
143    * @brief Retrieves the total frame number of the animation.
144    * @return The total frame number
145    */
146   uint32_t GetTotalFrameNumber() const;
147
148   /**
149    * @brief Gets the default size of the file,.
150    * @return The default size of the file
151    */
152   void GetDefaultSize( uint32_t& width, uint32_t& height ) const;
153
154   /**
155    * @brief Sets the stop behavior of the animation. This is performed when the animation is stopped.
156    * @param[in] stopBehavior The stop behavior
157    */
158   void SetStopBehavior( DevelImageVisual::StopBehavior::Type stopBehavior );
159
160   /**
161    * @brief Sets the looping mode.
162    * Animation plays forwards and then restarts from the beginning or runs backwards again.
163    * @param[in] loopingMode The looping mode
164    */
165   void SetLoopingMode( DevelImageVisual::LoopingMode::Type loopingMode );
166
167   /**
168    * @brief Connect to this signal to be notified when the texture upload is completed.
169    * @return The signal to connect to.
170    */
171   UploadCompletedSignalType& UploadCompletedSignal();
172
173 protected:
174
175   /**
176    * @brief The entry function of the worker thread.
177    *        It rasterizes the vector image.
178    */
179   void Run() override;
180
181 private:
182
183   /**
184    * @brief Initializes the vector renderer.
185    */
186   void Initialize();
187
188   /**
189    * @brief Rasterizes the current frame.
190    */
191   void Rasterize();
192
193   /**
194    * @brief Gets the frame number when the animation is stopped according to the stop behavior.
195    */
196   uint32_t GetStoppedFrame( uint32_t startFrame, uint32_t endFrame, uint32_t currentFrame );
197
198   // Undefined
199   VectorRasterizeThread( const VectorRasterizeThread& thread ) = delete;
200
201   // Undefined
202   VectorRasterizeThread& operator=( const VectorRasterizeThread& thread ) = delete;
203
204 private:
205
206   enum class PlayState
207   {
208     STOPPING,  ///< The animation is stopping
209     STOPPED,   ///< The animation has stopped
210     PLAYING,   ///< The animation is playing
211     PAUSED     ///< The animation is paused
212   };
213
214   std::string                 mUrl;
215   VectorAnimationRenderer     mVectorRenderer;
216   ConditionalWait             mConditionalWait;
217   std::unique_ptr< EventThreadCallback > mAnimationFinishedTrigger;
218   Vector2                     mPlayRange;
219   PlayState                   mPlayState;
220   DevelImageVisual::StopBehavior::Type mStopBehavior;
221   DevelImageVisual::LoopingMode::Type mLoopingMode;
222   int64_t                     mFrameDurationNanoSeconds;
223   float                       mFrameRate;
224   uint32_t                    mCurrentFrame;
225   uint32_t                    mTotalFrame;
226   uint32_t                    mStartFrame;
227   uint32_t                    mEndFrame;
228   uint32_t                    mWidth;
229   uint32_t                    mHeight;
230   int32_t                     mLoopCount;
231   int32_t                     mCurrentLoop;
232   bool                        mNeedRender;
233   bool                        mDestroyThread;  ///< Whether the thread be destroyed
234   bool                        mResourceReady;
235   bool                        mCurrentFrameUpdated;
236   bool                        mCurrentLoopUpdated;
237   bool                        mForward;
238   bool                        mUpdateFrameNumber;
239   bool                        mNeedAnimationFinishedTrigger;
240   const Dali::LogFactoryInterface& mLogFactory; ///< The log factory
241
242 };
243
244 } // namespace Internal
245
246 } // namespace Toolkit
247
248 } // namespace Dali
249
250 #endif // DALI_TOOLKIT_VECTOR_IMAGE_RASTERIZE_THREAD_H