1 #ifndef DALI_TOOLKIT_CANVAS_VIEW_RASTERIZE_THREAD_H
2 #define DALI_TOOLKIT_CANVAS_VIEW_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/canvas-renderer/canvas-renderer.h>
22 #include <dali/devel-api/adaptor-framework/event-thread-callback.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/public-api/common/intrusive-ptr.h>
28 #include <dali/public-api/object/ref-object.h>
29 #include <dali/public-api/rendering/texture-set.h>
34 #include <dali-toolkit/internal/controls/canvas-view/canvas-view-impl.h>
42 using CanvasViewPtr = IntrusivePtr<CanvasView>;
43 class CanvasRendererRasterizingTask;
44 using CanvasRendererRasterizingTaskPtr = IntrusivePtr<CanvasRendererRasterizingTask>;
47 * The canvasview rasterizing tasks to be processed in the worker thread.
49 * Life cycle of a rasterizing task is as follows:
50 * 1. Created by CanvasView in the main thread
51 * 2. Queued in the worked thread waiting to be processed.
52 * 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.
53 * Or if this task is been removed before its turn to be processed, it then been deleted in the worker thread.
55 class CanvasRendererRasterizingTask : public RefObject
60 * @param[in] canvasRenderer The renderer which the rasterized canvas to be applied.
62 CanvasRendererRasterizingTask(CanvasView* canvasView, CanvasRenderer canvasRenderer);
67 ~CanvasRendererRasterizingTask() = default;
70 * Do the rasterization with the mRasterizer.
71 * @return Returns True when it's successful. False otherwise.
77 * @return The CanvasView pointer.
79 CanvasView* GetCanvasView() const;
82 * Get the rasterization result.
83 * @return The texture with the rasterized pixels.
85 Texture GetRasterizedTexture();
89 CanvasRendererRasterizingTask(const CanvasRendererRasterizingTask& task);
92 CanvasRendererRasterizingTask& operator=(const CanvasRendererRasterizingTask& task);
95 CanvasViewPtr mCanvasView;
96 CanvasRenderer mCanvasRenderer;
97 Texture mRasterizedTexture;
101 * The worker thread for CanvasView rasterization.
103 class CanvasViewRasterizeThread : public Thread, Integration::Processor
106 /// @brief ApplyRasterizedImage Event signal type
107 using RasterizationCompletedSignalType = Signal<void(Texture)>;
113 * @param[in] trigger The trigger to wake up the main thread.
115 CanvasViewRasterizeThread();
118 * Terminate the CanvasView rasterize thread, join and delete.
120 * @param[in] thread The rasterize thread.
122 static void TerminateThread(CanvasViewRasterizeThread*& thread);
125 * Add a rasterization task into the waiting queue, called by main thread.
127 * @param[in] task The task added to the queue.
129 void AddTask(CanvasRendererRasterizingTaskPtr task);
132 * Remove the task with the given CanvasView from the waiting queue, called by main thread.
134 * Typically called when the actor is put off stage, so the renderer is not needed anymore.
136 * @param[in] canvasView The CanvasView pointer.
138 void RemoveTask(CanvasView* canvasView);
141 * Applies the rasterized image to material
143 void ApplyRasterized();
146 * @copydoc Dali::Integration::Processor::Process()
148 void Process(bool postProcessor) override;
151 * @brief This signal is emitted when rasterized image is applied.
153 * @return The signal to connect to
155 RasterizationCompletedSignalType& RasterizationCompletedSignal();
159 * Pop the next task out from the queue.
161 * @return The next task to be processed.
163 CanvasRendererRasterizingTaskPtr NextTaskToProcess();
166 * Pop the next task out from the completed queue, called by main thread.
168 * @return The next task in the completed queue.
170 CanvasRendererRasterizingTaskPtr NextCompletedTask();
173 * Add a task in to the queue
175 * @param[in] task The task added to the queue.
177 void AddCompletedTask(CanvasRendererRasterizingTaskPtr task);
180 * @brief Unregister a previously registered processor
183 void UnregisterProcessor();
189 ~CanvasViewRasterizeThread() override;
192 * The entry function of the worker thread.
193 * It fetches task from the Queue, rasterizes the image and apply to the renderer.
199 CanvasViewRasterizeThread(const CanvasViewRasterizeThread& thread);
202 CanvasViewRasterizeThread& operator=(const CanvasViewRasterizeThread& thread);
205 std::vector<CanvasRendererRasterizingTaskPtr> mRasterizeTasks; //The queue of the tasks waiting to rasterize the CanvasView.
206 std::vector<CanvasRendererRasterizingTaskPtr> mCompletedTasks; //The queue of the tasks with the SVG rasterization completed
208 ConditionalWait mConditionalWait;
210 std::unique_ptr<EventThreadCallback> mTrigger;
211 const Dali::LogFactoryInterface& mLogFactory;
212 bool mProcessorRegistered;
213 RasterizationCompletedSignalType mRasterizationCompletedSignal;
216 } // namespace Internal
218 } // namespace Toolkit
222 #endif // DALI_TOOLKIT_CANVAS_VIEW_RASTERIZE_THREAD_H