1 #ifndef DALI_TOOLKIT_SVG_RASTERIZE_THREAD_H
2 #define DALI_TOOLKIT_SVG_RASTERIZE_THREAD_H
5 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
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/devel-api/adaptor-framework/vector-image-renderer.h>
31 #include <dali/integration-api/adaptor-framework/log-factory-interface.h>
32 #include <dali/integration-api/processor-interface.h>
36 #include <dali-toolkit/internal/visuals/visual-url.h>
48 typedef IntrusivePtr< SvgVisual > SvgVisualPtr;
49 class RasterizingTask;
50 typedef IntrusivePtr< RasterizingTask > RasterizingTaskPtr;
53 * The svg rasterizing tasks to be processed in the worker thread.
55 * Life cycle of a rasterizing task is as follows:
56 * 1. Created by SvgVisual in the main thread
57 * 2. Queued in the worked thread waiting to be processed.
58 * 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
59 * 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.
61 class RasterizingTask : public RefObject
66 * @param[in] svgRenderer The renderer which the rasterized image to be applied.
67 * @param[in] url The URL to svg resource to use.
68 * @param[in] width The rasterization width.
69 * @param[in] height The rasterization height.
71 RasterizingTask(SvgVisual* svgRenderer, VectorImageRenderer vectorRenderer, const VisualUrl& url, float dpi, unsigned int width, unsigned int height);
76 ~RasterizingTask() override;
79 * Do the rasterization with the mRasterizer.
86 SvgVisual* GetSvgVisual() const;
89 * Get the rasterization result.
90 * @return The pixel data with the rasterized pixels.
92 PixelData GetPixelData() const;
95 * Get the VectorRenderer.
96 * @return VectorRenderer.
98 VectorImageRenderer GetVectorRenderer() const;
100 * Whether the resource is loaded.
101 * @return True if the resource is loaded.
103 bool IsLoaded() const;
112 RasterizingTask( const RasterizingTask& task );
115 RasterizingTask& operator=( const RasterizingTask& task );
118 SvgVisualPtr mSvgVisual;
119 VectorImageRenderer mVectorRenderer;
121 PixelData mPixelData;
124 unsigned int mHeight;
129 * The worker thread for SVG rasterization.
131 class SvgRasterizeThread : public Thread, Integration::Processor
138 * @param[in] trigger The trigger to wake up the main thread.
140 SvgRasterizeThread();
143 * Terminate the svg rasterize thread, join and delete.
145 static void TerminateThread( SvgRasterizeThread*& thread );
148 * Add a rasterization task into the waiting queue, called by main thread.
150 * @param[in] task The task added to the queue.
152 void AddTask( RasterizingTaskPtr task );
155 * Pop the next task out from the completed queue, called by main thread.
157 * @return The next task in the completed queue.
159 RasterizingTaskPtr NextCompletedTask();
162 * Remove the task with the given visual from the waiting queue, called by main thread.
164 * Typically called when the actor is put off stage, so the renderer is not needed anymore.
166 * @param[in] visual The visual pointer.
168 void RemoveTask( SvgVisual* visual );
171 * Delete the parsed SVG image, called by main thread.
173 * The parsed svg should be deleted in worker thread, as the main thread does not know whether a rasterization of this svg is ongoing.
175 * @param[in] VectorImage The image to be deleted
177 void DeleteImage( VectorImageRenderer vectorImage );
180 * @copydoc Dali::Integration::Processor::Process()
182 void Process() override;
187 * Pop the next task out from the queue.
189 * @return The next task to be processed.
191 RasterizingTaskPtr NextTaskToProcess();
194 * Add a task in to the queue
196 * @param[in] task The task added to the queue.
198 void AddCompletedTask( RasterizingTaskPtr task );
201 * Applies the rasterized image to material
203 void ApplyRasterizedSVGToSampler();
206 * @brief Unregister a previously registered processor
209 void UnregisterProcessor();
216 ~SvgRasterizeThread() override;
220 * The entry function of the worker thread.
221 * It fetches task from the Queue, rasterizes the image and apply to the renderer.
228 SvgRasterizeThread( const SvgRasterizeThread& thread );
231 SvgRasterizeThread& operator=( const SvgRasterizeThread& thread );
235 std::vector<RasterizingTaskPtr> mRasterizeTasks; //The queue of the tasks waiting to rasterize the SVG image
236 std::vector <RasterizingTaskPtr> mCompletedTasks; //The queue of the tasks with the SVG rasterization completed
237 Vector <VectorImageRenderer*> mDeleteSvg; //The images that the event thread requested to delete
239 ConditionalWait mConditionalWait;
241 std::unique_ptr< EventThreadCallback > mTrigger;
242 const Dali::LogFactoryInterface& mLogFactory;
243 bool mIsThreadWaiting;
244 bool mProcessorRegistered;
247 } // namespace Internal
249 } // namespace Toolkit
253 #endif // DALI_TOOLKIT_SVG_RASTERIZE_THREAD_H