Merge "DALi Version 1.4.10" into 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   /**
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 Gets the loop count. -1 means to repeat forever.
116    * @return The number of times to loop
117    */
118   int32_t GetLoopCount() const;
119
120   /**
121    * @brief Set the playing range.
122    * @param[in] range Two values between [0,1] to specify minimum and maximum progress.
123    * The animation will play between those values.
124    */
125   void SetPlayRange( Vector2 range );
126
127   /**
128    * @brief Gets the playing range.
129    * @return The play range defined for the animation
130    */
131   Vector2 GetPlayRange() const;
132
133   /**
134    * @brief Get the play state
135    * @return The play state
136    */
137   DevelImageVisual::PlayState GetPlayState() const;
138
139   /**
140    * @brief Queries whether the resource is ready.
141    * @return true if ready, false otherwise
142    */
143   bool IsResourceReady() const;
144
145   /**
146    * @brief Sets the progress of the animation.
147    * @param[in] progress The new progress as a normalized value between [0,1] or between the play range if specified.
148    */
149   void SetCurrentProgress( float progress );
150
151   /**
152    * @brief Retrieves the current progress of the animation.
153    * @return The current progress as a normalized value between [0,1]
154    */
155   float GetCurrentProgress() const;
156
157 protected:
158
159   /**
160    * @brief The entry function of the worker thread.
161    *        It rasterizes the vector image.
162    */
163   void Run() override;
164
165 private:
166
167   /**
168    * @brief Called by the rasterize thread which ensures a wait if required.
169    * @return false if the thread should stop.
170    */
171   bool IsThreadReady();
172
173   /**
174    * @brief Start rendering
175    */
176   bool StartRender();
177
178   /**
179    * @brief Rasterize the current frame.
180    */
181   void Rasterize();
182
183   // Undefined
184   VectorRasterizeThread( const VectorRasterizeThread& thread ) = delete;
185
186   // Undefined
187   VectorRasterizeThread& operator=( const VectorRasterizeThread& thread ) = delete;
188
189 private:
190
191   std::string                 mUrl;
192   VectorAnimationRenderer     mVectorRenderer;
193   ConditionalWait             mConditionalWait;
194   Dali::Mutex                 mMutex;
195   std::unique_ptr< EventThreadCallback > mResourceReadyTrigger;
196   std::unique_ptr< EventThreadCallback > mAnimationFinishedTrigger;
197   Vector2                     mPlayRange;
198   DevelImageVisual::PlayState mPlayState;
199   int64_t                     mFrameDurationNanoSeconds;
200   float                       mProgress;
201   float                       mFrameRate;
202   uint32_t                    mCurrentFrame;
203   uint32_t                    mTotalFrame;
204   uint32_t                    mStartFrame;
205   uint32_t                    mEndFrame;
206   uint32_t                    mWidth;
207   uint32_t                    mHeight;
208   int32_t                     mLoopCount;
209   int32_t                     mCurrentLoop;
210   bool                        mNeedRender;
211   bool                        mDestroyThread;  ///< Whether the thread be destroyed
212   bool                        mResourceReady;
213   const Dali::LogFactoryInterface& mLogFactory; ///< The log factory
214
215 };
216
217 } // namespace Internal
218
219 } // namespace Toolkit
220
221 } // namespace Dali
222
223 #endif // DALI_TOOLKIT_VECTOR_IMAGE_RASTERIZE_THREAD_H