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