Support asynchronous svg loading
[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 RasterizingTask;
46 typedef IntrusivePtr<RasterizingTask> RasterizingTaskPtr;
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 RasterizingTask : 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    * @param[in] url The URL to svg resource to use.
65    * @param[in] dpi The DPI of the screen.
66    * @param[in] width The rasterization width.
67    * @param[in] height The rasterization height.
68    */
69   RasterizingTask(SvgVisual* svgVisual, VectorImageRenderer vectorRenderer, const VisualUrl& url, float dpi, unsigned int width, unsigned int height);
70
71   /**
72    * Destructor.
73    */
74   ~RasterizingTask() override;
75
76   /**
77    * Load svg file
78    */
79   void Load();
80
81   /**
82    * Do the rasterization with the mRasterizer.
83    */
84   void Rasterize();
85
86   /**
87    * Get the svg visual
88    */
89   SvgVisual* GetSvgVisual() const;
90
91   /**
92    * Get the rasterization result.
93    * @return The pixel data with the rasterized pixels.
94    */
95   PixelData GetPixelData() const;
96
97   /**
98    * Whether the resource is loaded.
99    * @return True if the resource is loaded.
100    */
101   bool IsLoaded() const;
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   VectorImageRenderer mVectorRenderer;
113   VisualUrl           mUrl;
114   PixelData           mPixelData;
115   float               mDpi;
116   uint32_t            mWidth;
117   uint32_t            mHeight;
118   bool                mLoadSuccess;
119 };
120
121 /**
122  * The worker thread for SVG rasterization.
123  */
124 class SvgRasterizeThread : public Thread, Integration::Processor
125 {
126 public:
127   /**
128    * Constructor.
129    *
130    * @param[in] trigger The trigger to wake up the main thread.
131    */
132   SvgRasterizeThread();
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    * @copydoc Dali::Integration::Processor::Process()
164    */
165   void Process(bool postProcessor) override;
166
167 private:
168   /**
169    * Pop the next task out from the queue.
170    *
171    * @return The next task to be processed.
172    */
173   RasterizingTaskPtr NextTaskToProcess();
174
175   /**
176    * Add a task in to the queue
177    *
178    * @param[in] task The task added to the queue.
179    */
180   void AddCompletedTask(RasterizingTaskPtr task);
181
182   /**
183    * Applies the rasterized image to material
184    */
185   void ApplyRasterizedSVGToSampler();
186
187   /**
188    * @brief Unregister a previously registered processor
189    *
190    */
191   void UnregisterProcessor();
192
193 protected:
194   /**
195    * Destructor.
196    */
197   ~SvgRasterizeThread() override;
198
199   /**
200    * The entry function of the worker thread.
201    * It fetches task from the Queue, rasterizes the image and apply to the renderer.
202    */
203   void Run() override;
204
205 private:
206   // Undefined
207   SvgRasterizeThread(const SvgRasterizeThread& thread);
208
209   // Undefined
210   SvgRasterizeThread& operator=(const SvgRasterizeThread& thread);
211
212 private:
213   std::vector<RasterizingTaskPtr> mRasterizeTasks; //The queue of the tasks waiting to rasterize the SVG image
214   std::vector<RasterizingTaskPtr> mCompletedTasks; //The queue of the tasks with the SVG rasterization completed
215
216   ConditionalWait                      mConditionalWait;
217   Dali::Mutex                          mMutex;
218   std::unique_ptr<EventThreadCallback> mTrigger;
219   const Dali::LogFactoryInterface&     mLogFactory;
220   bool                                 mIsThreadWaiting;
221   bool                                 mProcessorRegistered;
222 };
223
224 } // namespace Internal
225
226 } // namespace Toolkit
227
228 } // namespace Dali
229
230 #endif // DALI_TOOLKIT_SVG_RASTERIZE_THREAD_H