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