Add CanvasView thread rasterization
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / canvas-view / canvas-view-rasterize-thread.h
1 #ifndef DALI_TOOLKIT_CANVAS_VIEW_RASTERIZE_THREAD_H
2 #define DALI_TOOLKIT_CANVAS_VIEW_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/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>
31 #include <memory>
32 #include <vector>
33
34 // INTERNAL INCLUDES
35 #include <dali-toolkit/internal/controls/canvas-view/canvas-view-impl.h>
36
37 namespace Dali
38 {
39 namespace Toolkit
40 {
41 namespace Internal
42 {
43 using CanvasViewPtr = IntrusivePtr<CanvasView>;
44 class CanvasRendererRasterizingTask;
45 using CanvasRendererRasterizingTaskPtr = IntrusivePtr<CanvasRendererRasterizingTask>;
46
47 /**
48  * The canvasview rasterizing tasks to be processed in the worker thread.
49  *
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.
55  */
56 class CanvasRendererRasterizingTask : public RefObject
57 {
58 public:
59   /**
60    * Constructor
61    * @param[in] canvasRenderer The renderer which the rasterized canvas to be applied.
62    */
63   CanvasRendererRasterizingTask(CanvasView* canvasView, CanvasRenderer canvasRenderer);
64
65   /**
66    * Destructor.
67    */
68   ~CanvasRendererRasterizingTask() = default;
69
70   /**
71    * Do the rasterization with the mRasterizer.
72    * @return Returns True when it's successful. False otherwise.
73    */
74   bool Rasterize();
75
76   /**
77    * Get the CanvasView
78    * @return The CanvasView pointer.
79    */
80   CanvasView* GetCanvasView() const;
81
82   /**
83    * Get the rasterization result.
84    * @return The pixel data with the rasterized pixels.
85    */
86   PixelData GetPixelData() const;
87
88   /**
89    * Get size of rasterization result.
90    * @return The size of the pixel data.
91    */
92   Vector2 GetBufferSize() const;
93
94 private:
95   // Undefined
96   CanvasRendererRasterizingTask(const CanvasRendererRasterizingTask& task);
97
98   // Undefined
99   CanvasRendererRasterizingTask& operator=(const CanvasRendererRasterizingTask& task);
100
101 private:
102   CanvasViewPtr  mCanvasView;
103   CanvasRenderer mCanvasRenderer;
104   PixelData      mPixelData;
105   Vector2        mBufferSize;
106 };
107
108 /**
109  * The worker thread for CanvasView rasterization.
110  */
111 class CanvasViewRasterizeThread : public Thread, Integration::Processor
112 {
113 public:
114   /// @brief ApplyRasterizedImage Event signal type
115   using RasterizationCompletedSignalType = Signal<void(PixelData)>;
116
117 public:
118   /**
119    * Constructor.
120    *
121    * @param[in] trigger The trigger to wake up the main thread.
122    */
123   CanvasViewRasterizeThread();
124
125   /**
126    * Terminate the CanvasView rasterize thread, join and delete.
127    *
128    * @param[in] thread The rasterize thread.
129    */
130   static void TerminateThread(CanvasViewRasterizeThread*& thread);
131
132   /**
133    * Add a rasterization task into the waiting queue, called by main thread.
134    *
135    * @param[in] task The task added to the queue.
136    */
137   void AddTask(CanvasRendererRasterizingTaskPtr task);
138
139   /**
140    * Remove the task with the given CanvasView from the waiting queue, called by main thread.
141    *
142    * Typically called when the actor is put off stage, so the renderer is not needed anymore.
143    *
144    * @param[in] canvasView The CanvasView pointer.
145    */
146   void RemoveTask(CanvasView* canvasView);
147
148   /**
149    * Applies the rasterized image to material
150    */
151   void ApplyRasterized();
152
153   /**
154    * @copydoc Dali::Integration::Processor::Process()
155    */
156   void Process(bool postProcessor) override;
157
158   /**
159    * @brief This signal is emitted when rasterized image is applied.
160    *
161    * @return The signal to connect to
162    */
163   RasterizationCompletedSignalType& RasterizationCompletedSignal();
164
165 private:
166   /**
167    * Pop the next task out from the queue.
168    *
169    * @return The next task to be processed.
170    */
171   CanvasRendererRasterizingTaskPtr NextTaskToProcess();
172
173   /**
174    * Pop the next task out from the completed queue, called by main thread.
175    *
176    * @return The next task in the completed queue.
177    */
178   CanvasRendererRasterizingTaskPtr NextCompletedTask();
179
180   /**
181    * Add a task in to the queue
182    *
183    * @param[in] task The task added to the queue.
184    */
185   void AddCompletedTask(CanvasRendererRasterizingTaskPtr task);
186
187   /**
188    * @brief Unregister a previously registered processor
189    *
190    */
191   void UnregisterProcessor();
192
193 protected:
194   /**
195    * Destructor.
196    */
197   ~CanvasViewRasterizeThread() override;
198
199   /**
200    * The entry function of the worker thread.
201    * It fetches task from the Queue, rasterizes the image and apply to the renderer.
202    */
203   void Run() override;
204
205 private:
206   // Undefined
207   CanvasViewRasterizeThread(const CanvasViewRasterizeThread& thread);
208
209   // Undefined
210   CanvasViewRasterizeThread& operator=(const CanvasViewRasterizeThread& thread);
211
212 private:
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
215
216   ConditionalWait                      mConditionalWait;
217   Dali::Mutex                          mMutex;
218   std::unique_ptr<EventThreadCallback> mTrigger;
219   const Dali::LogFactoryInterface&     mLogFactory;
220   bool                                 mProcessorRegistered;
221   RasterizationCompletedSignalType     mRasterizationCompletedSignal;
222 };
223
224 } // namespace Internal
225
226 } // namespace Toolkit
227
228 } // namespace Dali
229
230 #endif // DALI_TOOLKIT_CANVAS_VIEW_RASTERIZE_THREAD_H