1 #ifndef DALI_TOOLKIT_CANVAS_VIEW_RASTERIZE_THREAD_H
2 #define DALI_TOOLKIT_CANVAS_VIEW_RASTERIZE_THREAD_H
5 * Copyright (c) 2021 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.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/images/pixel-data.h>
29 #include <dali/public-api/object/ref-object.h>
30 #include <dali/public-api/rendering/texture-set.h>
35 #include <dali-toolkit/internal/controls/canvas-view/canvas-view-impl.h>
43 using CanvasViewPtr = IntrusivePtr<CanvasView>;
44 class CanvasRendererRasterizingTask;
45 using CanvasRendererRasterizingTaskPtr = IntrusivePtr<CanvasRendererRasterizingTask>;
48 * The canvasview rasterizing tasks to be processed in the worker thread.
50 * Life cycle of a rasterizing task is as follows:
51 * 1. Created by CanvasView in the main thread
52 * 2. Queued in the worked thread waiting to be processed.
53 * 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.
54 * Or if this task is been removed before its turn to be processed, it then been deleted in the worker thread.
56 class CanvasRendererRasterizingTask : public RefObject
61 * @param[in] canvasRenderer The renderer which the rasterized canvas to be applied.
63 CanvasRendererRasterizingTask(CanvasView* canvasView, CanvasRenderer canvasRenderer);
68 ~CanvasRendererRasterizingTask() = default;
71 * Do the rasterization with the mRasterizer.
72 * @return Returns True when it's successful. False otherwise.
78 * @return The CanvasView pointer.
80 CanvasView* GetCanvasView() const;
83 * Get the rasterization result.
84 * @return The pixel data with the rasterized pixels.
86 PixelData GetPixelData() const;
89 * Get size of rasterization result.
90 * @return The size of the pixel data.
92 Vector2 GetBufferSize() const;
96 CanvasRendererRasterizingTask(const CanvasRendererRasterizingTask& task);
99 CanvasRendererRasterizingTask& operator=(const CanvasRendererRasterizingTask& task);
102 CanvasViewPtr mCanvasView;
103 CanvasRenderer mCanvasRenderer;
104 PixelData mPixelData;
109 * The worker thread for CanvasView rasterization.
111 class CanvasViewRasterizeThread : public Thread, Integration::Processor
114 /// @brief ApplyRasterizedImage Event signal type
115 using RasterizationCompletedSignalType = Signal<void(PixelData)>;
121 * @param[in] trigger The trigger to wake up the main thread.
123 CanvasViewRasterizeThread();
126 * Terminate the CanvasView rasterize thread, join and delete.
128 * @param[in] thread The rasterize thread.
130 static void TerminateThread(CanvasViewRasterizeThread*& thread);
133 * Add a rasterization task into the waiting queue, called by main thread.
135 * @param[in] task The task added to the queue.
137 void AddTask(CanvasRendererRasterizingTaskPtr task);
140 * Remove the task with the given CanvasView from the waiting queue, called by main thread.
142 * Typically called when the actor is put off stage, so the renderer is not needed anymore.
144 * @param[in] canvasView The CanvasView pointer.
146 void RemoveTask(CanvasView* canvasView);
149 * Applies the rasterized image to material
151 void ApplyRasterized();
154 * @copydoc Dali::Integration::Processor::Process()
156 void Process(bool postProcessor) override;
159 * @brief This signal is emitted when rasterized image is applied.
161 * @return The signal to connect to
163 RasterizationCompletedSignalType& RasterizationCompletedSignal();
167 * Pop the next task out from the queue.
169 * @return The next task to be processed.
171 CanvasRendererRasterizingTaskPtr NextTaskToProcess();
174 * Pop the next task out from the completed queue, called by main thread.
176 * @return The next task in the completed queue.
178 CanvasRendererRasterizingTaskPtr NextCompletedTask();
181 * Add a task in to the queue
183 * @param[in] task The task added to the queue.
185 void AddCompletedTask(CanvasRendererRasterizingTaskPtr task);
188 * @brief Unregister a previously registered processor
191 void UnregisterProcessor();
197 ~CanvasViewRasterizeThread() override;
200 * The entry function of the worker thread.
201 * It fetches task from the Queue, rasterizes the image and apply to the renderer.
207 CanvasViewRasterizeThread(const CanvasViewRasterizeThread& thread);
210 CanvasViewRasterizeThread& operator=(const CanvasViewRasterizeThread& thread);
213 std::vector<CanvasRendererRasterizingTaskPtr> mRasterizeTasks; //The queue of the tasks waiting to rasterize the CanvasView.
214 std::vector<CanvasRendererRasterizingTaskPtr> mCompletedTasks; //The queue of the tasks with the SVG rasterization completed
216 ConditionalWait mConditionalWait;
218 std::unique_ptr<EventThreadCallback> mTrigger;
219 const Dali::LogFactoryInterface& mLogFactory;
220 bool mProcessorRegistered;
221 RasterizationCompletedSignalType mRasterizationCompletedSignal;
224 } // namespace Internal
226 } // namespace Toolkit
230 #endif // DALI_TOOLKIT_CANVAS_VIEW_RASTERIZE_THREAD_H