Fix visual defect when BorderlineOffset = 1.0f
[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/visuals/visual-url.h>
36
37 namespace Dali
38 {
39 namespace Toolkit
40 {
41 namespace Internal
42 {
43 class SvgVisual;
44 typedef IntrusivePtr<SvgVisual> SvgVisualPtr;
45 class SvgTask;
46 typedef IntrusivePtr<SvgTask> SvgTaskPtr;
47
48 /**
49  * The svg rasterizing tasks to be processed in the worker thread.
50  *
51  * Life cycle of a rasterizing task is as follows:
52  * 1. Created by SvgVisual in the main thread
53  * 2. Queued in the worked thread waiting to be processed.
54  * 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
55  *    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.
56  */
57 class SvgTask : public RefObject
58 {
59 public:
60   /**
61    * Constructor
62    * @param[in] svgVisual The visual which the rasterized image to be applied.
63    * @param[in] vectorRenderer The vector rasterizer.
64    */
65   SvgTask(SvgVisual* svgVisual, VectorImageRenderer vectorRenderer);
66
67   /**
68    * Destructor.
69    */
70   virtual ~SvgTask() = default;
71
72   /**
73    * Process the task
74    */
75   virtual void Process() = 0;
76
77   /**
78    * Whether the task has succeeded.
79    * @return True if the task has succeeded.
80    */
81   bool HasSucceeded() const;
82
83   /**
84    * Get the svg visual
85    */
86   SvgVisual* GetSvgVisual() const;
87
88   /**
89    * Get the rasterization result.
90    * @return The pixel data with the rasterized pixels.
91    */
92   virtual PixelData GetPixelData() const;
93
94 private:
95   // Undefined
96   SvgTask(const SvgTask& task) = delete;
97
98   // Undefined
99   SvgTask& operator=(const SvgTask& task) = delete;
100
101 protected:
102   SvgVisualPtr        mSvgVisual;
103   VectorImageRenderer mVectorRenderer;
104   bool                mHasSucceeded;
105 };
106
107 class SvgLoadingTask : public SvgTask
108 {
109 public:
110   /**
111    * Constructor
112    * @param[in] svgVisual The visual which the rasterized image to be applied.
113    * @param[in] vectorRenderer The vector rasterizer.
114    * @param[in] url The URL to svg resource to use.
115    * @param[in] dpi The DPI of the screen.
116    */
117   SvgLoadingTask(SvgVisual* svgVisual, VectorImageRenderer vectorRenderer, const VisualUrl& url, float dpi);
118
119   /**
120    * Destructor.
121    */
122   ~SvgLoadingTask() override;
123
124   /**
125    * Process the task
126    */
127   void Process() override;
128
129 private:
130   // Undefined
131   SvgLoadingTask(const SvgLoadingTask& task) = delete;
132
133   // Undefined
134   SvgLoadingTask& operator=(const SvgLoadingTask& task) = delete;
135
136 private:
137   VisualUrl mUrl;
138   float     mDpi;
139 };
140
141 class SvgRasterizingTask : public SvgTask
142 {
143 public:
144   /**
145    * Constructor
146    * @param[in] svgVisual The visual which the rasterized image to be applied.
147    * @param[in] vectorRenderer The vector rasterizer.
148    * @param[in] width The rasterization width.
149    * @param[in] height The rasterization height.
150    */
151   SvgRasterizingTask(SvgVisual* svgVisual, VectorImageRenderer vectorRenderer, unsigned int width, unsigned int height);
152
153   /**
154    * Destructor.
155    */
156   ~SvgRasterizingTask() override;
157
158   /**
159    * Process the task accodring to the type
160    */
161   void Process() override;
162
163   /**
164    * Get the rasterization result.
165    * @return The pixel data with the rasterized pixels.
166    */
167   PixelData GetPixelData() const override;
168
169 private:
170   // Undefined
171   SvgRasterizingTask(const SvgRasterizingTask& task) = delete;
172
173   // Undefined
174   SvgRasterizingTask& operator=(const SvgRasterizingTask& task) = delete;
175
176 private:
177   PixelData mPixelData;
178   uint32_t  mWidth;
179   uint32_t  mHeight;
180 };
181
182 /**
183  * The worker thread for SVG rasterization.
184  */
185 class SvgRasterizeThread : public Thread, Integration::Processor
186 {
187 public:
188   /**
189    * Constructor.
190    *
191    * @param[in] trigger The trigger to wake up the main thread.
192    */
193   SvgRasterizeThread();
194
195   /**
196    * Terminate the svg rasterize thread, join and delete.
197    */
198   static void TerminateThread(SvgRasterizeThread*& thread);
199
200   /**
201    * Add a rasterization task into the waiting queue, called by main thread.
202    *
203    * @param[in] task The task added to the queue.
204    */
205   void AddTask(SvgTaskPtr task);
206
207   /**
208    * Pop the next task out from the completed queue, called by main thread.
209    *
210    * @return The next task in the completed queue.
211    */
212   SvgTaskPtr NextCompletedTask();
213
214   /**
215    * Remove the task with the given visual from the waiting queue, called by main thread.
216    *
217    * Typically called when the actor is put off stage, so the renderer is not needed anymore.
218    *
219    * @param[in] visual The visual pointer.
220    */
221   void RemoveTask(SvgVisual* visual);
222
223   /**
224    * @copydoc Dali::Integration::Processor::Process()
225    */
226   void Process(bool postProcessor) override;
227
228 private:
229   /**
230    * Pop the next task out from the queue.
231    *
232    * @return The next task to be processed.
233    */
234   SvgTaskPtr NextTaskToProcess();
235
236   /**
237    * Add a task in to the queue
238    *
239    * @param[in] task The task added to the queue.
240    */
241   void AddCompletedTask(SvgTaskPtr task);
242
243   /**
244    * Applies the rasterized image to material
245    */
246   void ApplyRasterizedSVGToSampler();
247
248   /**
249    * @brief Unregister a previously registered processor
250    *
251    */
252   void UnregisterProcessor();
253
254 protected:
255   /**
256    * Destructor.
257    */
258   ~SvgRasterizeThread() override;
259
260   /**
261    * The entry function of the worker thread.
262    * It fetches task from the Queue, rasterizes the image and apply to the renderer.
263    */
264   void Run() override;
265
266 private:
267   // Undefined
268   SvgRasterizeThread(const SvgRasterizeThread& thread);
269
270   // Undefined
271   SvgRasterizeThread& operator=(const SvgRasterizeThread& thread);
272
273 private:
274   std::vector<SvgTaskPtr> mRasterizeTasks; //The queue of the tasks waiting to rasterize the SVG image
275   std::vector<SvgTaskPtr> mCompletedTasks; //The queue of the tasks with the SVG rasterization completed
276
277   ConditionalWait                      mConditionalWait;
278   Dali::Mutex                          mMutex;
279   std::unique_ptr<EventThreadCallback> mTrigger;
280   const Dali::LogFactoryInterface&     mLogFactory;
281   bool                                 mIsThreadWaiting;
282   bool                                 mProcessorRegistered;
283 };
284
285 } // namespace Internal
286
287 } // namespace Toolkit
288
289 } // namespace Dali
290
291 #endif // DALI_TOOLKIT_SVG_RASTERIZE_THREAD_H