[dali_2.3.21] 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/adaptor-framework/encoded-image-buffer.h>
23 #include <dali/public-api/common/intrusive-ptr.h>
24 #include <dali/public-api/common/vector-wrapper.h>
25 #include <dali/public-api/images/pixel-data.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    * Whether the task has succeeded.
70    * @return True if the task has succeeded.
71    */
72   bool HasSucceeded() const;
73
74   /**
75    * @brief Get the task's imageRenderer
76    * @return VectorImageRenderer
77    */
78   VectorImageRenderer GetRenderer();
79
80   /**
81    * Get the rasterization result.
82    * @return The pixel data with the rasterized pixels.
83    */
84   virtual PixelData GetPixelData() const;
85
86 private:
87   // Undefined
88   SvgTask(const SvgTask& task) = delete;
89
90   // Undefined
91   SvgTask& operator=(const SvgTask& task) = delete;
92
93 protected:
94   VectorImageRenderer mVectorRenderer;
95   bool                mHasSucceeded;
96 };
97
98 class SvgLoadingTask : public SvgTask
99 {
100 public:
101   /**
102    * Constructor
103    * @param[in] vectorRenderer The vector rasterizer.
104    * @param[in] url The URL to svg resource to use.
105    * @param[in] encodedImageBuffer The resource buffer if required.
106    * @param[in] dpi The DPI of the screen.
107    * @param[in] callback The callback that is called when the operation is completed.
108    */
109   SvgLoadingTask(VectorImageRenderer vectorRenderer, const VisualUrl& url, EncodedImageBuffer encodedImageBuffer, float dpi, CallbackBase* callback);
110
111   /**
112    * Destructor.
113    */
114   ~SvgLoadingTask() override;
115
116 public: // Implementation of AsyncTask
117   /**
118    * @copydoc Dali::AsyncTask::Process()
119    */
120   void Process();
121
122   /**
123    * @copydoc Dali::AsyncTask::IsReady()
124    */
125   bool IsReady();
126
127   /**
128    * @copydoc Dali::AsyncTask::GetTaskName()
129    */
130   std::string_view GetTaskName() const override
131   {
132     return "SvgLoadingTask";
133   }
134
135 private:
136   // Undefined
137   SvgLoadingTask(const SvgLoadingTask& task) = delete;
138
139   // Undefined
140   SvgLoadingTask& operator=(const SvgLoadingTask& task) = delete;
141
142 private:
143   VisualUrl          mImageUrl;
144   EncodedImageBuffer mEncodedImageBuffer;
145   float              mDpi;
146 };
147
148 class SvgRasterizingTask : public SvgTask
149 {
150 public:
151   /**
152    * Constructor
153    * @param[in] vectorRenderer The vector rasterizer.
154    * @param[in] width The rasterization width.
155    * @param[in] height The rasterization height.
156    * @param[in] callback The callback that is called when the operation is completed.
157    */
158   SvgRasterizingTask(VectorImageRenderer vectorRenderer, uint32_t width, uint32_t height, CallbackBase* callback);
159
160   /**
161    * Destructor.
162    */
163   ~SvgRasterizingTask() override;
164
165   /**
166    * Get the rasterization result.
167    * @return The pixel data with the rasterized pixels.
168    */
169   PixelData GetPixelData() const override;
170
171 #ifdef TRACE_ENABLED
172   /**
173    * Set the url of rasterizatoin visual. Only for tracing
174    * @param[in] url The url of this visual
175    */
176   void SetUrl(VisualUrl url)
177   {
178     mImageUrl = std::move(url);
179   }
180 #endif
181
182 public: // Implementation of AsyncTask
183   /**
184    * @copydoc Dali::AsyncTask::Process()
185    */
186   void Process();
187
188   /**
189    * @copydoc Dali::AsyncTask::IsReady()
190    */
191   bool IsReady();
192
193   /**
194    * @copydoc Dali::AsyncTask::GetTaskName()
195    */
196   std::string_view GetTaskName() const override
197   {
198     return "SvgRasterizingTask";
199   }
200
201 private:
202   // Undefined
203   SvgRasterizingTask(const SvgRasterizingTask& task) = delete;
204
205   // Undefined
206   SvgRasterizingTask& operator=(const SvgRasterizingTask& task) = delete;
207
208 private:
209 #ifdef TRACE_ENABLED
210   VisualUrl mImageUrl{};
211 #endif
212   PixelData mPixelData;
213   uint32_t  mWidth;
214   uint32_t  mHeight;
215 };
216
217 } // namespace Internal
218
219 } // namespace Toolkit
220
221 } // namespace Dali
222
223 #endif // DALI_TOOLKIT_SVG_TASK_H