DALi Version 2.2.11
[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) 2022 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 <memory>
26
27 // INTERNAL INCLUDES
28 #include <dali-toolkit/internal/visuals/visual-url.h>
29 #include <dali/public-api/adaptor-framework/async-task-manager.h>
30
31 namespace Dali
32 {
33 namespace Toolkit
34 {
35 namespace Internal
36 {
37 class SvgVisual;
38 typedef IntrusivePtr<SvgVisual> SvgVisualPtr;
39 class SvgTask;
40 typedef IntrusivePtr<SvgTask> SvgTaskPtr;
41
42 /**
43  * The svg rasterizing tasks to be processed in the worker thread.
44  *
45  * Life cycle of a rasterizing task is as follows:
46  * 1. Created by SvgVisual in the main thread
47  * 2. Queued in the worked thread waiting to be processed.
48  * 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
49  *    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.
50  */
51 class SvgTask : public AsyncTask
52 {
53 public:
54   /**
55    * Constructor
56    * @param[in] vectorRenderer The vector rasterizer.
57    * @param[in] callback The callback that is called when the operation is completed.
58    */
59   SvgTask(VectorImageRenderer vectorRenderer, CallbackBase* callback);
60
61   /**
62    * Destructor.
63    */
64   virtual ~SvgTask() = default;
65
66   /**
67    * Process the task
68    */
69   virtual void Process() = 0;
70
71   /**
72    * Whether the task is ready to process.
73    * @return True if the task is ready to process.
74    */
75   virtual bool IsReady() = 0;
76
77   /**
78    * Whether the task has succeeded.
79    * @return True if the task has succeeded.
80    */
81   bool HasSucceeded() const;
82
83   /**
84    * @brief Get the task's imageRenderer
85    * @return VectorImageRenderer
86    */
87   VectorImageRenderer GetRenderer();
88
89   /**
90    * Get the rasterization result.
91    * @return The pixel data with the rasterized pixels.
92    */
93   virtual PixelData GetPixelData() const;
94
95 private:
96   // Undefined
97   SvgTask(const SvgTask& task) = delete;
98
99   // Undefined
100   SvgTask& operator=(const SvgTask& task) = delete;
101
102 protected:
103   VectorImageRenderer mVectorRenderer;
104   bool                mHasSucceeded;
105 };
106
107 class SvgLoadingTask : public SvgTask
108 {
109 public:
110   /**
111    * Constructor
112    * @param[in] vectorRenderer The vector rasterizer.
113    * @param[in] url The URL to svg resource to use.
114    * @param[in] dpi The DPI of the screen.
115    * @param[in] callback The callback that is called when the operation is completed.
116    */
117   SvgLoadingTask(VectorImageRenderer vectorRenderer, const VisualUrl& url, float dpi, CallbackBase* callback);
118
119   /**
120    * Destructor.
121    */
122   ~SvgLoadingTask() override;
123
124   /**
125    * Process the task
126    */
127   void Process() override;
128
129   /**
130    * Whether the task is ready to process.
131    * @return True if the task is ready to process.
132    */
133   bool IsReady() override;
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 mUrl;
144   float     mDpi;
145 };
146
147 class SvgRasterizingTask : public SvgTask
148 {
149 public:
150   /**
151    * Constructor
152    * @param[in] vectorRenderer The vector rasterizer.
153    * @param[in] width The rasterization width.
154    * @param[in] height The rasterization height.
155    * @param[in] callback The callback that is called when the operation is completed.
156    */
157   SvgRasterizingTask(VectorImageRenderer vectorRenderer, uint32_t width, uint32_t height, CallbackBase* callback);
158
159   /**
160    * Destructor.
161    */
162   ~SvgRasterizingTask() override;
163
164   /**
165    * Process the task accodring to the type
166    */
167   void Process() override;
168
169   /**
170    * Whether the task is ready to process.
171    * @return True if the task is ready to process.
172    */
173   bool IsReady() override;
174
175   /**
176    * Get the rasterization result.
177    * @return The pixel data with the rasterized pixels.
178    */
179   PixelData GetPixelData() const override;
180
181 private:
182   // Undefined
183   SvgRasterizingTask(const SvgRasterizingTask& task) = delete;
184
185   // Undefined
186   SvgRasterizingTask& operator=(const SvgRasterizingTask& task) = delete;
187
188 private:
189   PixelData mPixelData;
190   uint32_t  mWidth;
191   uint32_t  mHeight;
192 };
193
194 } // namespace Internal
195
196 } // namespace Toolkit
197
198 } // namespace Dali
199
200 #endif // DALI_TOOLKIT_SVG_TASK_H