[dali_2.1.30] Merge branch 'devel/master'
[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) 2022 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-image-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/adaptor-framework/log-factory-interface.h>
27 #include <dali/integration-api/processor-interface.h>
28 #include <dali/public-api/common/intrusive-ptr.h>
29 #include <dali/public-api/common/vector-wrapper.h>
30 #include <dali/public-api/images/pixel-data.h>
31 #include <dali/public-api/object/ref-object.h>
32 #include <memory>
33
34 // INTERNAL INCLUDES
35 #include <dali-toolkit/internal/helpers/round-robin-container-view.h>
36 #include <dali-toolkit/internal/visuals/visual-url.h>
37
38 namespace Dali
39 {
40 namespace Toolkit
41 {
42 namespace Internal
43 {
44 class SvgVisual;
45 typedef IntrusivePtr<SvgVisual> SvgVisualPtr;
46 class SvgTask;
47 typedef IntrusivePtr<SvgTask> SvgTaskPtr;
48 class SvgRasterizeManager;
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 SvgTask : public RefObject
60 {
61 public:
62   /**
63    * Constructor
64    * @param[in] svgVisual The visual which the rasterized image to be applied.
65    * @param[in] vectorRenderer The vector rasterizer.
66    */
67   SvgTask(SvgVisual* svgVisual, VectorImageRenderer vectorRenderer);
68
69   /**
70    * Destructor.
71    */
72   virtual ~SvgTask() = default;
73
74   /**
75    * Process the task
76    */
77   virtual void Process() = 0;
78
79   /**
80    * Whether the task is ready to process.
81    * @return True if the task is ready to process.
82    */
83   virtual bool IsReady()
84   {
85     return true;
86   }
87
88   /**
89    * Whether the task has succeeded.
90    * @return True if the task has succeeded.
91    */
92   bool HasSucceeded() const;
93
94   /**
95    * Get the svg visual
96    */
97   SvgVisual* GetSvgVisual() const;
98
99   /**
100    * Get the rasterization result.
101    * @return The pixel data with the rasterized pixels.
102    */
103   virtual PixelData GetPixelData() const;
104
105 private:
106   // Undefined
107   SvgTask(const SvgTask& task) = delete;
108
109   // Undefined
110   SvgTask& operator=(const SvgTask& task) = delete;
111
112 protected:
113   SvgVisualPtr        mSvgVisual;
114   VectorImageRenderer mVectorRenderer;
115   bool                mHasSucceeded;
116 };
117
118 class SvgLoadingTask : public SvgTask
119 {
120 public:
121   /**
122    * Constructor
123    * @param[in] svgVisual The visual which the rasterized image to be applied.
124    * @param[in] vectorRenderer The vector rasterizer.
125    * @param[in] url The URL to svg resource to use.
126    * @param[in] dpi The DPI of the screen.
127    */
128   SvgLoadingTask(SvgVisual* svgVisual, VectorImageRenderer vectorRenderer, const VisualUrl& url, float dpi);
129
130   /**
131    * Destructor.
132    */
133   ~SvgLoadingTask() override;
134
135   /**
136    * Process the task
137    */
138   void Process() override;
139
140 private:
141   // Undefined
142   SvgLoadingTask(const SvgLoadingTask& task) = delete;
143
144   // Undefined
145   SvgLoadingTask& operator=(const SvgLoadingTask& task) = delete;
146
147 private:
148   VisualUrl mUrl;
149   float     mDpi;
150 };
151
152 class SvgRasterizingTask : public SvgTask
153 {
154 public:
155   /**
156    * Constructor
157    * @param[in] svgVisual The visual which the rasterized image to be applied.
158    * @param[in] vectorRenderer The vector rasterizer.
159    * @param[in] width The rasterization width.
160    * @param[in] height The rasterization height.
161    */
162   SvgRasterizingTask(SvgVisual* svgVisual, VectorImageRenderer vectorRenderer, unsigned int width, unsigned int height);
163
164   /**
165    * Destructor.
166    */
167   ~SvgRasterizingTask() override;
168
169   /**
170    * Process the task accodring to the type
171    */
172   void Process() override;
173
174   /**
175    * Whether the task is ready to process.
176    * @return True if the task is ready to process.
177    */
178   bool IsReady() override;
179
180   /**
181    * Get the rasterization result.
182    * @return The pixel data with the rasterized pixels.
183    */
184   PixelData GetPixelData() const override;
185
186 private:
187   // Undefined
188   SvgRasterizingTask(const SvgRasterizingTask& task) = delete;
189
190   // Undefined
191   SvgRasterizingTask& operator=(const SvgRasterizingTask& task) = delete;
192
193 private:
194   PixelData mPixelData;
195   uint32_t  mWidth;
196   uint32_t  mHeight;
197 };
198
199 /**
200  * The worker thread for SVG rasterization.
201  */
202 class SvgRasterizeThread : public Thread
203 {
204 public:
205   /**
206    * Constructor.
207    */
208   SvgRasterizeThread(SvgRasterizeManager& svgRasterizeManager);
209
210   /**
211    * Destructor.
212    */
213   ~SvgRasterizeThread() override;
214
215   /**
216    * @brief Request the thread to rasterizes the task.
217    * @return True if the request succeeds, otherwise false.
218    */
219   bool RequestRasterize();
220
221 protected:
222   /**
223    * The entry function of the worker thread.
224    * It rasterizes the image.
225    */
226   void Run() override;
227
228 private:
229   // Undefined
230   SvgRasterizeThread(const SvgRasterizeThread& thread) = delete;
231
232   // Undefined
233   SvgRasterizeThread& operator=(const SvgRasterizeThread& thread) = delete;
234
235 private:
236   ConditionalWait                  mConditionalWait;
237   const Dali::LogFactoryInterface& mLogFactory;
238   SvgRasterizeManager&             mSvgRasterizeManager;
239   bool                             mDestroyThread;
240   bool                             mIsThreadStarted;
241   bool                             mIsThreadIdle;
242 };
243
244 /**
245  * The manager for SVG rasterization.
246  */
247 class SvgRasterizeManager : Integration::Processor
248 {
249 public:
250   /**
251    * Constructor.
252    *
253    * @param[in] trigger The trigger to wake up the main thread.
254    */
255   SvgRasterizeManager();
256
257   /**
258    * Destructor.
259    */
260   ~SvgRasterizeManager() override;
261
262   /**
263    * Add a rasterization task into the waiting queue, called by main thread.
264    *
265    * @param[in] task The task added to the queue.
266    */
267   void AddTask(SvgTaskPtr task);
268
269   /**
270    * Pop the next task out from the completed queue, called by main thread.
271    *
272    * @return The next task in the completed queue.
273    */
274   SvgTaskPtr NextCompletedTask();
275
276   /**
277    * Remove the task with the given visual from the waiting queue, called by main thread.
278    *
279    * Typically called when the actor is put off stage, so the renderer is not needed anymore.
280    *
281    * @param[in] visual The visual pointer.
282    */
283   void RemoveTask(SvgVisual* visual);
284
285   /**
286    * @copydoc Dali::Integration::Processor::Process()
287    */
288   void Process(bool postProcessor) override;
289
290   /**
291    * Pop the next task out from the queue.
292    *
293    * @return The next task to be processed.
294    */
295   SvgTaskPtr NextTaskToProcess();
296
297   /**
298    * Add a task in to the queue
299    *
300    * @param[in] task The task added to the queue.
301    */
302   void AddCompletedTask(SvgTaskPtr task);
303
304 private:
305   /**
306    * Applies the rasterized image to material
307    */
308   void ApplyRasterizedSVGToSampler();
309
310   /**
311    * @brief Unregister a previously registered processor
312    *
313    */
314   void UnregisterProcessor();
315
316 private:
317   /**
318    * @brief Helper class to keep the relation between SvgRasterizeThread and corresponding container
319    */
320   class RasterizeHelper
321   {
322   public:
323     /**
324      * @brief Create an RasterizeHelper.
325      *
326      * @param[in] svgRasterizeManager Reference to the SvgRasterizeManager
327      */
328     RasterizeHelper(SvgRasterizeManager& svgRasterizeManager);
329
330     /**
331      * @brief Request the thread to rasterizes the task.
332      * @return True if the request succeeds, otherwise false.
333      */
334     bool RequestRasterize();
335
336   public:
337     RasterizeHelper(const RasterizeHelper&) = delete;
338     RasterizeHelper& operator=(const RasterizeHelper&) = delete;
339
340     RasterizeHelper(RasterizeHelper&& rhs);
341     RasterizeHelper& operator=(RasterizeHelper&& rhs) = delete;
342
343   private:
344     /**
345      * @brief Main constructor that used by all other constructors
346      */
347     RasterizeHelper(std::unique_ptr<SvgRasterizeThread> rasterizer, SvgRasterizeManager& svgRasterizeManager);
348
349   private:
350     std::unique_ptr<SvgRasterizeThread> mRasterizer;
351     SvgRasterizeManager&                mSvgRasterizeManager;
352   };
353
354 private:
355   // Undefined
356   SvgRasterizeManager(const SvgRasterizeManager& thread);
357
358   // Undefined
359   SvgRasterizeManager& operator=(const SvgRasterizeManager& thread);
360
361 private:
362   std::vector<SvgTaskPtr> mRasterizeTasks; //The queue of the tasks waiting to rasterize the SVG image
363   std::vector<SvgTaskPtr> mCompletedTasks; //The queue of the tasks with the SVG rasterization completed
364
365   RoundRobinContainerView<RasterizeHelper> mRasterizers;
366
367   Dali::Mutex                          mMutex;
368   std::unique_ptr<EventThreadCallback> mTrigger;
369   bool                                 mProcessorRegistered;
370 };
371
372 } // namespace Internal
373
374 } // namespace Toolkit
375
376 } // namespace Dali
377
378 #endif // DALI_TOOLKIT_SVG_RASTERIZE_THREAD_H