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