(Vector) Change properties to get frame numbers
[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 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 protected:
159
160   /**
161    * @brief The entry function of the worker thread.
162    *        It rasterizes the vector image.
163    */
164   void Run() override;
165
166 private:
167
168   /**
169    * @brief Initialize the vector renderer.
170    */
171   void Initialize();
172
173   /**
174    * @brief Rasterize the current frame.
175    */
176   void Rasterize();
177
178   // Undefined
179   VectorRasterizeThread( const VectorRasterizeThread& thread ) = delete;
180
181   // Undefined
182   VectorRasterizeThread& operator=( const VectorRasterizeThread& thread ) = delete;
183
184 private:
185
186   std::string                 mUrl;
187   VectorAnimationRenderer     mVectorRenderer;
188   ConditionalWait             mConditionalWait;
189   std::unique_ptr< EventThreadCallback > mResourceReadyTrigger;
190   std::unique_ptr< EventThreadCallback > mAnimationFinishedTrigger;
191   Vector2                     mPlayRange;
192   DevelImageVisual::PlayState mPlayState;
193   int64_t                     mFrameDurationNanoSeconds;
194   float                       mFrameRate;
195   uint32_t                    mCurrentFrame;
196   uint32_t                    mTotalFrame;
197   uint32_t                    mStartFrame;
198   uint32_t                    mEndFrame;
199   uint32_t                    mWidth;
200   uint32_t                    mHeight;
201   int32_t                     mLoopCount;
202   int32_t                     mCurrentLoop;
203   bool                        mNeedRender;
204   bool                        mDestroyThread;  ///< Whether the thread be destroyed
205   bool                        mResourceReady;
206   bool                        mCurrentFrameUpdated;
207   const Dali::LogFactoryInterface& mLogFactory; ///< The log factory
208
209 };
210
211 } // namespace Internal
212
213 } // namespace Toolkit
214
215 } // namespace Dali
216
217 #endif // DALI_TOOLKIT_VECTOR_IMAGE_RASTERIZE_THREAD_H