1 #ifndef DALI_TOOLKIT_SVG_RASTERIZE_THREAD_H
2 #define DALI_TOOLKIT_SVG_RASTERIZE_THREAD_H
5 * Copyright (c) 2022 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/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>
35 #include <dali-toolkit/internal/helpers/round-robin-container-view.h>
36 #include <dali-toolkit/internal/visuals/visual-url.h>
45 typedef IntrusivePtr<SvgVisual> SvgVisualPtr;
47 typedef IntrusivePtr<SvgTask> SvgTaskPtr;
48 class SvgRasterizeManager;
51 * The svg rasterizing tasks to be processed in the worker thread.
53 * Life cycle of a rasterizing task is as follows:
54 * 1. Created by SvgVisual in the main thread
55 * 2. Queued in the worked thread waiting to be processed.
56 * 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
57 * 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.
59 class SvgTask : public RefObject
64 * @param[in] svgVisual The visual which the rasterized image to be applied.
65 * @param[in] vectorRenderer The vector rasterizer.
67 SvgTask(SvgVisual* svgVisual, VectorImageRenderer vectorRenderer);
72 virtual ~SvgTask() = default;
77 virtual void Process() = 0;
80 * Whether the task is ready to process.
81 * @return True if the task is ready to process.
83 virtual bool IsReady()
89 * Whether the task has succeeded.
90 * @return True if the task has succeeded.
92 bool HasSucceeded() const;
97 SvgVisual* GetSvgVisual() const;
100 * Get the rasterization result.
101 * @return The pixel data with the rasterized pixels.
103 virtual PixelData GetPixelData() const;
107 SvgTask(const SvgTask& task) = delete;
110 SvgTask& operator=(const SvgTask& task) = delete;
113 SvgVisualPtr mSvgVisual;
114 VectorImageRenderer mVectorRenderer;
118 class SvgLoadingTask : public SvgTask
123 * @param[in] svgVisual The visual which the rasterized image to be applied.
124 * @param[in] vectorRenderer The vector rasterizer.
125 * @param[in] url The URL to svg resource to use.
126 * @param[in] dpi The DPI of the screen.
128 SvgLoadingTask(SvgVisual* svgVisual, VectorImageRenderer vectorRenderer, const VisualUrl& url, float dpi);
133 ~SvgLoadingTask() override;
138 void Process() override;
142 SvgLoadingTask(const SvgLoadingTask& task) = delete;
145 SvgLoadingTask& operator=(const SvgLoadingTask& task) = delete;
152 class SvgRasterizingTask : public SvgTask
157 * @param[in] svgVisual The visual which the rasterized image to be applied.
158 * @param[in] vectorRenderer The vector rasterizer.
159 * @param[in] width The rasterization width.
160 * @param[in] height The rasterization height.
162 SvgRasterizingTask(SvgVisual* svgVisual, VectorImageRenderer vectorRenderer, unsigned int width, unsigned int height);
167 ~SvgRasterizingTask() override;
170 * Process the task accodring to the type
172 void Process() override;
175 * Whether the task is ready to process.
176 * @return True if the task is ready to process.
178 bool IsReady() override;
181 * Get the rasterization result.
182 * @return The pixel data with the rasterized pixels.
184 PixelData GetPixelData() const override;
188 SvgRasterizingTask(const SvgRasterizingTask& task) = delete;
191 SvgRasterizingTask& operator=(const SvgRasterizingTask& task) = delete;
194 PixelData mPixelData;
200 * The worker thread for SVG rasterization.
202 class SvgRasterizeThread : public Thread
208 SvgRasterizeThread(SvgRasterizeManager& svgRasterizeManager);
213 ~SvgRasterizeThread() override;
216 * @brief Request the thread to rasterizes the task.
217 * @return True if the request succeeds, otherwise false.
219 bool RequestRasterize();
223 * The entry function of the worker thread.
224 * It rasterizes the image.
230 SvgRasterizeThread(const SvgRasterizeThread& thread) = delete;
233 SvgRasterizeThread& operator=(const SvgRasterizeThread& thread) = delete;
236 ConditionalWait mConditionalWait;
237 const Dali::LogFactoryInterface& mLogFactory;
238 SvgRasterizeManager& mSvgRasterizeManager;
240 bool mIsThreadStarted;
245 * The manager for SVG rasterization.
247 class SvgRasterizeManager : Integration::Processor
253 * @param[in] trigger The trigger to wake up the main thread.
255 SvgRasterizeManager();
260 ~SvgRasterizeManager() override;
263 * Add a rasterization task into the waiting queue, called by main thread.
265 * @param[in] task The task added to the queue.
267 void AddTask(SvgTaskPtr task);
270 * Pop the next task out from the completed queue, called by main thread.
272 * @return The next task in the completed queue.
274 SvgTaskPtr NextCompletedTask();
277 * Remove the task with the given visual from the waiting queue, called by main thread.
279 * Typically called when the actor is put off stage, so the renderer is not needed anymore.
281 * @param[in] visual The visual pointer.
283 void RemoveTask(SvgVisual* visual);
286 * @copydoc Dali::Integration::Processor::Process()
288 void Process(bool postProcessor) override;
291 * Pop the next task out from the queue.
293 * @return The next task to be processed.
295 SvgTaskPtr NextTaskToProcess();
298 * Add a task in to the queue
300 * @param[in] task The task added to the queue.
302 void AddCompletedTask(SvgTaskPtr task);
306 * Applies the rasterized image to material
308 void ApplyRasterizedSVGToSampler();
311 * @brief Unregister a previously registered processor
314 void UnregisterProcessor();
318 * @brief Helper class to keep the relation between SvgRasterizeThread and corresponding container
320 class RasterizeHelper
324 * @brief Create an RasterizeHelper.
326 * @param[in] svgRasterizeManager Reference to the SvgRasterizeManager
328 RasterizeHelper(SvgRasterizeManager& svgRasterizeManager);
331 * @brief Request the thread to rasterizes the task.
332 * @return True if the request succeeds, otherwise false.
334 bool RequestRasterize();
337 RasterizeHelper(const RasterizeHelper&) = delete;
338 RasterizeHelper& operator=(const RasterizeHelper&) = delete;
340 RasterizeHelper(RasterizeHelper&& rhs);
341 RasterizeHelper& operator=(RasterizeHelper&& rhs) = delete;
345 * @brief Main constructor that used by all other constructors
347 RasterizeHelper(std::unique_ptr<SvgRasterizeThread> rasterizer, SvgRasterizeManager& svgRasterizeManager);
350 std::unique_ptr<SvgRasterizeThread> mRasterizer;
351 SvgRasterizeManager& mSvgRasterizeManager;
356 SvgRasterizeManager(const SvgRasterizeManager& thread);
359 SvgRasterizeManager& operator=(const SvgRasterizeManager& thread);
362 std::vector<SvgTaskPtr> mRasterizeTasks; //The queue of the tasks waiting to rasterize the SVG image
363 std::vector<SvgTaskPtr> mCompletedTasks; //The queue of the tasks with the SVG rasterization completed
365 RoundRobinContainerView<RasterizeHelper> mRasterizers;
368 std::unique_ptr<EventThreadCallback> mTrigger;
369 bool mProcessorRegistered;
372 } // namespace Internal
374 } // namespace Toolkit
378 #endif // DALI_TOOLKIT_SVG_RASTERIZE_THREAD_H