1c94ca9b11c1ebf8519398e8d71d88c2a8501f55
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / svg / svg-rasterize-thread.h
1 #ifndef DALI_TOOLKIT_SVG_RASTERIZE_THREAD_H
2 #define DALI_TOOLKIT_SVG_RASTERIZE_THREAD_H
3
4 /*
5  * Copyright (c) 2019 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/threading/conditional-wait.h>
23 #include <dali/devel-api/threading/mutex.h>
24 #include <dali/devel-api/threading/thread.h>
25 #include <dali/public-api/images/buffer-image.h>
26 #include <dali/public-api/images/pixel-data.h>
27 #include <dali/public-api/common/intrusive-ptr.h>
28 #include <dali/public-api/common/vector-wrapper.h>
29 #include <dali/public-api/object/ref-object.h>
30 #include <dali/public-api/rendering/texture-set.h>
31 #include <dali-toolkit/internal/visuals/visual-url.h>
32
33 struct NSVGimage;
34 struct NSVGrasterizer;
35
36 namespace Dali
37 {
38
39 namespace Toolkit
40 {
41
42 namespace Internal
43 {
44
45 class SvgVisual;
46 typedef IntrusivePtr< SvgVisual > SvgVisualPtr;
47 class RasterizingTask;
48 typedef IntrusivePtr< RasterizingTask > RasterizingTaskPtr;
49
50 /**
51  * The svg rasterizing tasks to be processed in the worker thread.
52  *
53  * Life cycle of a rasterizing task is as follows:
54  * 1. Created by SvgVisual in the main thread
55  * 2. Queued in the worked thread waiting to be processed.
56  * 3. If this task gets its turn to do the rasterization, it triggers main thread to apply the rasterized image to material then been deleted in main thread call back
57  *    Or if this task is been removed ( new image/size set to the visual or actor off stage) before its turn to be processed, it then been deleted in the worker thread.
58  */
59 class RasterizingTask : public RefObject
60 {
61 public:
62   /**
63    * Constructor
64    *
65    * @param[in] svgRenderer The renderer which the rasterized image to be applied.
66    * @param[in] parsedSvg The parsed svg for rasterizing.
67    *            Note, after the task is added to the worker thread, the worker thread takes over the ownership.
68    *            When the image is to be deleted, delete it in the worker thread by calling SvgRasterizeThread::DeleteImage( parsedSvg ).
69    * @param[in] url The URL to svg resource to use.
70    * @param[in] width The rasterization width.
71    * @param[in] height The rasterization height.
72    */
73   RasterizingTask( SvgVisual* svgRenderer, NSVGimage* parsedSvg, const VisualUrl& url, float dpi, unsigned int width, unsigned int height );
74
75   /**
76    * Do the rasterization with the given rasterizer.
77    *@param[in] rasterizer The rasterizer that rasterize the SVG to a buffer image
78    */
79   void Rasterize( NSVGrasterizer* rasterizer );
80
81   /**
82    * Get the svg visual
83    */
84   SvgVisual* GetSvgVisual() const;
85
86   /**
87    * Get the rasterization result.
88    * @return The pixel data with the rasterized pixels.
89    */
90   PixelData GetPixelData() const;
91
92   /**
93    * Get the parsed data.
94    * @return parsed image data.
95    */
96   NSVGimage* GetParsedImage() const;
97
98   /**
99    * Load svg file
100    */
101   void Load();
102
103 private:
104   // Undefined
105   RasterizingTask( const RasterizingTask& task );
106
107   // Undefined
108   RasterizingTask& operator=( const RasterizingTask& task );
109
110 private:
111   SvgVisualPtr    mSvgVisual;
112   NSVGimage*      mParsedSvg;
113   VisualUrl       mUrl;
114   PixelData       mPixelData;
115   float           mDpi;
116   unsigned int    mWidth;
117   unsigned int    mHeight;
118 };
119
120 /**
121  * The worker thread for SVG rasterization.
122  */
123 class SvgRasterizeThread : public Thread
124 {
125 public:
126
127   /**
128    * Constructor.
129    *
130    * @param[in] trigger The trigger to wake up the main thread.
131    */
132   SvgRasterizeThread( EventThreadCallback* trigger );
133
134   /**
135    * Terminate the svg rasterize thread, join and delete.
136    */
137   static void TerminateThread( SvgRasterizeThread*& thread );
138
139   /**
140    * Add a rasterization task into the waiting queue, called by main thread.
141    *
142    * @param[in] task The task added to the queue.
143    */
144   void AddTask( RasterizingTaskPtr task );
145
146   /**
147    * Pop the next task out from the completed queue, called by main thread.
148    *
149    * @return The next task in the completed queue.
150    */
151   RasterizingTaskPtr NextCompletedTask();
152
153   /**
154    * Remove the task with the given visual from the waiting queue, called by main thread.
155    *
156    * Typically called when the actor is put off stage, so the renderer is not needed anymore.
157    *
158    * @param[in] visual The visual pointer.
159    */
160   void RemoveTask( SvgVisual* visual );
161
162   /**
163    * Delete the parsed SVG image, called by main thread.
164    *
165    * The parsed svg should be delelted in worker thread, as the main thread does not know whether a rasterization of this svg is ongoing.
166    *
167    * @param[in] parsedImage The image to be deleted
168    */
169   void DeleteImage( NSVGimage* parsedSvg );
170
171 private:
172
173   /**
174    * Pop the next task out from the queue.
175    *
176    * @return The next task to be processed.
177    */
178   RasterizingTaskPtr NextTaskToProcess();
179
180   /**
181    * Add a task in to the queue
182    *
183    * @param[in] task The task added to the queue.
184    */
185   void AddCompletedTask( RasterizingTaskPtr task );
186
187 protected:
188
189   /**
190    * Destructor.
191    */
192   virtual ~SvgRasterizeThread();
193
194
195   /**
196    * The entry function of the worker thread.
197    * It fetches task from the Queue, rasterizes the image and apply to the renderer.
198    */
199   void Run() override;
200
201 private:
202
203   // Undefined
204   SvgRasterizeThread( const SvgRasterizeThread& thread );
205
206   // Undefined
207   SvgRasterizeThread& operator=( const SvgRasterizeThread& thread );
208
209 private:
210
211   std::vector<RasterizingTaskPtr>  mRasterizeTasks;     //The queue of the tasks waiting to rasterize the SVG image
212   std::vector <RasterizingTaskPtr> mCompletedTasks;     //The queue of the tasks with the SVG rasterization completed
213   Vector<NSVGimage*>               mDeleteSvg;          //The images that the event thread requested to delete
214
215   ConditionalWait            mConditionalWait;
216   Dali::Mutex                mMutex;
217   EventThreadCallback*       mTrigger;
218
219   NSVGrasterizer*            mRasterizer;
220   bool                       mIsThreadWaiting;
221 };
222
223 } // namespace Internal
224
225 } // namespace Toolkit
226
227 } // namespace Dali
228
229 #endif // DALI_TOOLKIT_SVG_RASTERIZE_THREAD_H