[dali_2.2.52] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / svg / svg-task.h
1 #ifndef DALI_TOOLKIT_SVG_TASK_H
2 #define DALI_TOOLKIT_SVG_TASK_H
3
4 /*
5  * Copyright (c) 2023 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/vector-image-renderer.h>
22 #include <dali/public-api/common/intrusive-ptr.h>
23 #include <dali/public-api/common/vector-wrapper.h>
24 #include <dali/public-api/images/pixel-data.h>
25 #include <dali/public-api/adaptor-framework/encoded-image-buffer.h>
26 #include <memory>
27
28 // INTERNAL INCLUDES
29 #include <dali-toolkit/internal/visuals/visual-url.h>
30 #include <dali/public-api/adaptor-framework/async-task-manager.h>
31
32 namespace Dali
33 {
34 namespace Toolkit
35 {
36 namespace Internal
37 {
38 class SvgVisual;
39 typedef IntrusivePtr<SvgVisual> SvgVisualPtr;
40 class SvgTask;
41 typedef IntrusivePtr<SvgTask> SvgTaskPtr;
42
43 /**
44  * The svg rasterizing tasks to be processed in the worker thread.
45  *
46  * Life cycle of a rasterizing task is as follows:
47  * 1. Created by SvgVisual in the main thread
48  * 2. Queued in the worked thread waiting to be processed.
49  * 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
50  *    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.
51  */
52 class SvgTask : public AsyncTask
53 {
54 public:
55   /**
56    * Constructor
57    * @param[in] vectorRenderer The vector rasterizer.
58    * @param[in] callback The callback that is called when the operation is completed.
59    * @param[in] priorityType The priority of this task.
60    */
61   SvgTask(VectorImageRenderer vectorRenderer, CallbackBase* callback, AsyncTask::PriorityType priorityType = AsyncTask::PriorityType::DEFAULT);
62
63   /**
64    * Destructor.
65    */
66   virtual ~SvgTask() = default;
67
68   /**
69    * Process the task
70    */
71   virtual void Process() = 0;
72
73   /**
74    * Whether the task is ready to process.
75    * @return True if the task is ready to process.
76    */
77   virtual bool IsReady() = 0;
78
79   /**
80    * Whether the task has succeeded.
81    * @return True if the task has succeeded.
82    */
83   bool HasSucceeded() const;
84
85   /**
86    * @brief Get the task's imageRenderer
87    * @return VectorImageRenderer
88    */
89   VectorImageRenderer GetRenderer();
90
91   /**
92    * Get the rasterization result.
93    * @return The pixel data with the rasterized pixels.
94    */
95   virtual PixelData GetPixelData() const;
96
97 private:
98   // Undefined
99   SvgTask(const SvgTask& task) = delete;
100
101   // Undefined
102   SvgTask& operator=(const SvgTask& task) = delete;
103
104 protected:
105   VectorImageRenderer mVectorRenderer;
106   bool                mHasSucceeded;
107 };
108
109 class SvgLoadingTask : public SvgTask
110 {
111 public:
112   /**
113    * Constructor
114    * @param[in] vectorRenderer The vector rasterizer.
115    * @param[in] url The URL to svg resource to use.
116    * @param[in] encodedImageBuffer The resource buffer if required.
117    * @param[in] dpi The DPI of the screen.
118    * @param[in] callback The callback that is called when the operation is completed.
119    */
120   SvgLoadingTask(VectorImageRenderer vectorRenderer, const VisualUrl& url, EncodedImageBuffer encodedImageBuffer, float dpi, CallbackBase* callback);
121
122   /**
123    * Destructor.
124    */
125   ~SvgLoadingTask() override;
126
127   /**
128    * Process the task
129    */
130   void Process() override;
131
132   /**
133    * Whether the task is ready to process.
134    * @return True if the task is ready to process.
135    */
136   bool IsReady() override;
137
138 private:
139   // Undefined
140   SvgLoadingTask(const SvgLoadingTask& task) = delete;
141
142   // Undefined
143   SvgLoadingTask& operator=(const SvgLoadingTask& task) = delete;
144
145 private:
146   VisualUrl          mImageUrl;
147   EncodedImageBuffer mEncodedImageBuffer;
148   float              mDpi;
149 };
150
151 class SvgRasterizingTask : public SvgTask
152 {
153 public:
154   /**
155    * Constructor
156    * @param[in] vectorRenderer The vector rasterizer.
157    * @param[in] width The rasterization width.
158    * @param[in] height The rasterization height.
159    * @param[in] callback The callback that is called when the operation is completed.
160    */
161   SvgRasterizingTask(VectorImageRenderer vectorRenderer, uint32_t width, uint32_t height, CallbackBase* callback);
162
163   /**
164    * Destructor.
165    */
166   ~SvgRasterizingTask() override;
167
168   /**
169    * Process the task accodring to the type
170    */
171   void Process() override;
172
173   /**
174    * Whether the task is ready to process.
175    * @return True if the task is ready to process.
176    */
177   bool IsReady() override;
178
179   /**
180    * Get the rasterization result.
181    * @return The pixel data with the rasterized pixels.
182    */
183   PixelData GetPixelData() const override;
184
185 #ifdef TRACE_ENABLED
186   /**
187    * Set the url of rasterizatoin visual. Only for tracing
188    * @param[in] url The url of this visual
189    */
190   void SetUrl(VisualUrl url)
191   {
192     mImageUrl = std::move(url);
193   }
194 #endif
195
196 private:
197   // Undefined
198   SvgRasterizingTask(const SvgRasterizingTask& task) = delete;
199
200   // Undefined
201   SvgRasterizingTask& operator=(const SvgRasterizingTask& task) = delete;
202
203 private:
204 #ifdef TRACE_ENABLED
205   VisualUrl mImageUrl{};
206 #endif
207   PixelData mPixelData;
208   uint32_t  mWidth;
209   uint32_t  mHeight;
210 };
211
212 } // namespace Internal
213
214 } // namespace Toolkit
215
216 } // namespace Dali
217
218 #endif // DALI_TOOLKIT_SVG_TASK_H