3ac638f8748a9fd36cca93d9147611a0013a8747
[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
29 // INTERNAL INCLUDES
30 #include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
31
32 namespace Dali
33 {
34
35 namespace Toolkit
36 {
37
38 namespace Internal
39 {
40
41 /**
42  * The worker thread for vector image rasterization.
43  */
44 class VectorRasterizeThread : public Thread
45 {
46 public:
47
48   /**
49    * @brief Constructor.
50    *
51    * @param[in] url The url of the vector animation file
52    */
53   VectorRasterizeThread( const std::string& url );
54
55   /**
56    * @brief Destructor.
57    */
58   virtual ~VectorRasterizeThread();
59
60   /**
61    * @brief Sets the renderer used to display the result image.
62    *
63    * @param[in] renderer The renderer used to display the result image
64    */
65   void SetRenderer( Renderer renderer );
66
67   /**
68    * @brief Sets the target image size.
69    *
70    * @param[in] width The target image width
71    * @param[in] height The target image height
72    */
73   void SetSize( uint32_t width, uint32_t height );
74
75   /**
76    * @brief Play the vector animation.
77    */
78   void StartAnimation();
79
80   /**
81    * @brief Stop the vector animation.
82    */
83   void StopAnimation();
84
85   /**
86    * @brief Pause the vector animation.
87    */
88   void PauseAnimation();
89
90   /**
91    * @brief Render one frame. The current frame number will be increased.
92    */
93   void RenderFrame();
94
95   /**
96    * @brief This callback is called after the first frame is ready.
97    * @param[in] callback The resource ready callback
98    */
99   void SetResourceReadyCallback( EventThreadCallback* callback );
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( int16_t count );
112
113   /**
114    * @brief Set the playing range.
115    * @param[in] range Two values between [0,1] to specify minimum and maximum progress.
116    * The animation will play between those values.
117    */
118   void SetPlayRange( Vector2 range );
119
120   /**
121    * @brief Get the play state
122    * @return The play state
123    */
124   DevelImageVisual::PlayState GetPlayState();
125
126 protected:
127
128   /**
129    * @brief The entry function of the worker thread.
130    *        It rasterizes the vector image.
131    */
132   void Run() override;
133
134 private:
135
136   /**
137    * @brief Called by the rasterize thread which ensures a wait if required.
138    * @return false if the thread should stop.
139    */
140   bool IsThreadReady();
141
142   /**
143    * @brief Start rendering
144    */
145   bool StartRender();
146
147   /**
148    * @brief Rasterize the current frame.
149    */
150   void Rasterize();
151
152   // Undefined
153   VectorRasterizeThread( const VectorRasterizeThread& thread ) = delete;
154
155   // Undefined
156   VectorRasterizeThread& operator=( const VectorRasterizeThread& thread ) = delete;
157
158 private:
159
160   std::string                 mUrl;
161   VectorAnimationRenderer     mVectorRenderer;
162   ConditionalWait             mConditionalWait;
163   Dali::Mutex                 mMutex;
164   EventThreadCallback*        mResourceReadyTrigger;
165   EventThreadCallback*        mAnimationFinishedTrigger;
166   Vector2                     mPlayRange;
167   DevelImageVisual::PlayState mPlayState;
168   uint32_t                    mCurrentFrame;
169   uint32_t                    mTotalFrame;
170   uint32_t                    mStartFrame;
171   uint32_t                    mEndFrame;
172   uint32_t                    mWidth;
173   uint32_t                    mHeight;
174   int16_t                     mLoopCount;
175   int16_t                     mCurrentLoop;
176   bool                        mNeedRender;
177   bool                        mDestroyThread;  ///< Whether the thread be destroyed
178   bool                        mResourceReady;
179   const Dali::LogFactoryInterface& mLogFactory; ///< The log factory
180
181 };
182
183 } // namespace Internal
184
185 } // namespace Toolkit
186
187 } // namespace Dali
188
189 #endif // DALI_TOOLKIT_VECTOR_IMAGE_RASTERIZE_THREAD_H