Merge "SvgLoader: Async conversion of svg data" into tizen
[platform/core/graphics/tizenvg.git] / inc / thorvg.h
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *               http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  */
17 #ifndef _THORVG_H_
18 #define _THORVG_H_
19
20 #include <memory>
21
22 #ifdef TVG_BUILD
23     #define TVG_EXPORT __attribute__ ((visibility ("default")))
24 #else
25     #define TVG_EXPORT
26 #endif
27
28 #ifdef  LOG_TAG
29 #undef  LOG_TAG
30 #endif
31 #define LOG_TAG "TVG"
32
33
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37
38
39 #define _TVG_DECLARE_PRIVATE(A) \
40 protected: \
41     struct Impl; \
42     std::unique_ptr<Impl> pImpl; \
43     A(const A&) = delete; \
44     const A& operator=(const A&) = delete; \
45     A()
46
47 #define _TVG_DISABLE_CTOR(A) \
48     A() = delete; \
49     ~A() = delete
50
51 #define _TVG_DECLARE_ACCESSOR(A) \
52     friend A
53
54 #define _TVG_DECALRE_IDENTIFIER() \
55     auto id() const { return _id; } \
56 protected: \
57     unsigned _id
58
59 namespace tvg
60 {
61
62 class RenderMethod;
63 class Scene;
64 class Canvas;
65
66
67 enum class TVG_EXPORT Result { Success = 0, InvalidArguments, InsufficientCondition, FailedAllocation, MemoryCorruption, NonSupport, Unknown };
68 enum class TVG_EXPORT PathCommand { Close = 0, MoveTo, LineTo, CubicTo };
69 enum class TVG_EXPORT StrokeCap { Square = 0, Round, Butt };
70 enum class TVG_EXPORT StrokeJoin { Bevel = 0, Round, Miter };
71 enum class TVG_EXPORT FillSpread { Pad = 0, Reflect, Repeat };
72 enum class TVG_EXPORT CanvasEngine { Sw = 0, Gl };
73
74
75 struct Point
76 {
77     float x, y;
78 };
79
80
81 struct Matrix
82 {
83     float e11, e12, e13;
84     float e21, e22, e23;
85     float e31, e32, e33;
86 };
87
88
89 /**
90  * @class Paint
91  *
92  * @ingroup ThorVG
93  *
94  * @brief description...
95  *
96  */
97 class TVG_EXPORT Paint
98 {
99 public:
100     virtual ~Paint() {}
101
102     _TVG_DECALRE_IDENTIFIER();
103 };
104
105
106 /**
107  * @class Fill
108  *
109  * @ingroup ThorVG
110  *
111  * @brief description...
112  *
113  */
114 class TVG_EXPORT Fill
115 {
116 public:
117     struct ColorStop
118     {
119         float offset;
120         uint8_t r, g, b, a;
121     };
122
123     virtual ~Fill();
124
125     Result colorStops(const ColorStop* colorStops, uint32_t cnt) noexcept;
126     Result spread(FillSpread s) noexcept;
127
128     uint32_t colorStops(const ColorStop** colorStops) const noexcept;
129     FillSpread spread() const noexcept;
130
131     _TVG_DECALRE_IDENTIFIER();
132     _TVG_DECLARE_PRIVATE(Fill);
133 };
134
135
136 /**
137  * @class Canvas
138  *
139  * @ingroup ThorVG
140  *
141  * @brief description...
142  *
143  */
144 class TVG_EXPORT Canvas
145 {
146 public:
147     Canvas(RenderMethod*);
148     virtual ~Canvas();
149
150     Result reserve(uint32_t n) noexcept;
151     virtual Result push(std::unique_ptr<Paint> paint) noexcept;
152     virtual Result clear() noexcept;
153     virtual Result update() noexcept;
154     virtual Result update(Paint* paint) noexcept;
155     virtual Result draw(bool async = true) noexcept;
156     virtual Result sync() noexcept;
157
158     _TVG_DECLARE_ACCESSOR(Scene);
159     _TVG_DECLARE_PRIVATE(Canvas);
160 };
161
162
163
164 /**
165  * @class LinearGradient
166  *
167  * @ingroup ThorVG
168  *
169  * @brief description...
170  *
171  */
172 class TVG_EXPORT LinearGradient final : public Fill
173 {
174 public:
175     ~LinearGradient();
176
177     Result linear(float x1, float y1, float x2, float y2) noexcept;
178     Result linear(float* x1, float* y1, float* x2, float* y2) const noexcept;
179
180     static std::unique_ptr<LinearGradient> gen() noexcept;
181
182     _TVG_DECLARE_PRIVATE(LinearGradient);
183 };
184
185
186 /**
187  * @class RadialGradient
188  *
189  * @ingroup ThorVG
190  *
191  * @brief description...
192  *
193  */
194 class TVG_EXPORT RadialGradient final : public Fill
195 {
196 public:
197     ~RadialGradient();
198
199     Result radial(float cx, float cy, float radius) noexcept;
200     Result radial(float* cx, float* cy, float* radius) const noexcept;
201
202     static std::unique_ptr<RadialGradient> gen() noexcept;
203
204     _TVG_DECLARE_PRIVATE(RadialGradient);
205 };
206
207
208
209 /**
210  * @class Shape
211  *
212  * @ingroup ThorVG
213  *
214  * @brief description...
215  *
216  */
217 class TVG_EXPORT Shape final : public Paint
218 {
219 public:
220     ~Shape();
221
222     Result reset() noexcept;
223
224     //Path
225     Result moveTo(float x, float y) noexcept;
226     Result lineTo(float x, float y) noexcept;
227     Result cubicTo(float cx1, float cy1, float cx2, float cy2, float x, float y) noexcept;
228     Result close() noexcept;
229
230     //Shape
231     Result appendRect(float x, float y, float w, float h, float rx, float ry) noexcept;
232     Result appendCircle(float cx, float cy, float rx, float ry) noexcept;
233     Result appendPath(const PathCommand* cmds, uint32_t cmdCnt, const Point* pts, uint32_t ptsCnt) noexcept;
234
235     //Stroke
236     Result stroke(float width) noexcept;
237     Result stroke(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept;
238     Result stroke(const float* dashPattern, uint32_t cnt) noexcept;
239     Result stroke(StrokeCap cap) noexcept;
240     Result stroke(StrokeJoin join) noexcept;
241
242     //Fill
243     Result fill(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept;
244     Result fill(std::unique_ptr<Fill> f) noexcept;
245
246     //Transform
247     Result rotate(float degree) noexcept;
248     Result scale(float factor) noexcept;
249     Result translate(float x, float y) noexcept;
250     Result transform(const Matrix& m) noexcept;
251
252     //Getters
253     uint32_t pathCommands(const PathCommand** cmds) const noexcept;
254     uint32_t pathCoords(const Point** pts) const noexcept;
255     Result fill(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) const noexcept;
256     const Fill* fill() const noexcept;
257     Result bounds(float* x, float* y, float* w, float* h) const noexcept;
258
259     float strokeWidth() const noexcept;
260     Result strokeColor(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) const noexcept;
261     uint32_t strokeDash(const float** dashPattern) const noexcept;
262     StrokeCap strokeCap() const noexcept;
263     StrokeJoin strokeJoin() const noexcept;
264
265     static std::unique_ptr<Shape> gen() noexcept;
266
267     _TVG_DECLARE_PRIVATE(Shape);
268     _TVG_DECLARE_ACCESSOR(Canvas);
269     _TVG_DECLARE_ACCESSOR(Scene);
270 };
271
272
273 /**
274  * @class Scene
275  *
276  * @ingroup ThorVG
277  *
278  * @brief description...
279  *
280  */
281 class TVG_EXPORT Scene final : public Paint
282 {
283 public:
284     ~Scene();
285
286     Result push(std::unique_ptr<Paint> paint) noexcept;
287     Result reserve(uint32_t size) noexcept;
288     Result load(const std::string& path) noexcept;
289
290     Result rotate(float degree) noexcept;
291     Result scale(float factor) noexcept;
292     Result translate(float x, float y) noexcept;
293     Result transform(const Matrix& m) noexcept;
294
295     Result bounds(float* x, float* y, float* w, float* h) const noexcept;
296
297     static std::unique_ptr<Scene> gen() noexcept;
298
299     _TVG_DECLARE_ACCESSOR(Canvas);
300     _TVG_DECLARE_PRIVATE(Scene);
301 };
302
303
304 /**
305  * @class SwCanvas
306  *
307  * @ingroup ThorVG
308  *
309   @brief description...
310  *
311  */
312 class TVG_EXPORT SwCanvas final : public Canvas
313 {
314 public:
315     ~SwCanvas();
316
317     Result target(uint32_t* buffer, uint32_t stride, uint32_t w, uint32_t h) noexcept;
318
319     static std::unique_ptr<SwCanvas> gen() noexcept;
320
321     _TVG_DECLARE_PRIVATE(SwCanvas);
322 };
323
324
325 /**
326  * @class GlCanvas
327  *
328  * @ingroup ThorVG
329  *
330  * @brief description...
331  *
332  */
333 class TVG_EXPORT GlCanvas final : public Canvas
334 {
335 public:
336     ~GlCanvas();
337
338     //TODO: Gl Specific methods. Need gl backend configuration methods as well.
339     Result target(uint32_t* buffer, uint32_t stride, uint32_t w, uint32_t h) noexcept;
340
341     static std::unique_ptr<GlCanvas> gen() noexcept;
342
343     _TVG_DECLARE_PRIVATE(GlCanvas);
344 };
345
346
347 /**
348  * @class Engine
349  *
350  * @ingroup ThorVG
351  *
352  * @brief description...
353  *
354  */
355 class TVG_EXPORT Initializer final
356 {
357 public:
358
359     /**
360      * @brief ...
361      *
362      * @param[in] arg ...
363      *
364      * @note ...
365      *
366      * @return ...
367      *
368      * @see ...
369      */
370     static Result init(CanvasEngine engine) noexcept;
371     static Result term(CanvasEngine engine) noexcept;
372
373     _TVG_DISABLE_CTOR(Initializer);
374 };
375
376 } //namespace
377
378 #ifdef __cplusplus
379 }
380 #endif
381
382 #endif //_THORVG_H_