common: revise canvas interfaces.
[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     Result rotate(float degree) noexcept;
103     Result scale(float factor) noexcept;
104     Result translate(float x, float y) noexcept;
105     Result transform(const Matrix& m) noexcept;
106     Result bounds(float* x, float* y, float* w, float* h) const noexcept;
107
108     _TVG_DECALRE_IDENTIFIER();
109     _TVG_DECLARE_PRIVATE(Paint);
110 };
111
112
113 /**
114  * @class Fill
115  *
116  * @ingroup ThorVG
117  *
118  * @brief description...
119  *
120  */
121 class TVG_EXPORT Fill
122 {
123 public:
124     struct ColorStop
125     {
126         float offset;
127         uint8_t r, g, b, a;
128     };
129
130     virtual ~Fill();
131
132     Result colorStops(const ColorStop* colorStops, uint32_t cnt) noexcept;
133     Result spread(FillSpread s) noexcept;
134
135     uint32_t colorStops(const ColorStop** colorStops) const noexcept;
136     FillSpread spread() const noexcept;
137
138     _TVG_DECALRE_IDENTIFIER();
139     _TVG_DECLARE_PRIVATE(Fill);
140 };
141
142
143 /**
144  * @class Canvas
145  *
146  * @ingroup ThorVG
147  *
148  * @brief description...
149  *
150  */
151 class TVG_EXPORT Canvas
152 {
153 public:
154     Canvas(RenderMethod*);
155     virtual ~Canvas();
156
157     Result reserve(uint32_t n) noexcept;
158     virtual Result push(std::unique_ptr<Paint> paint) noexcept;
159     virtual Result clear() noexcept;
160     virtual Result update(Paint* paint) noexcept;
161     virtual Result draw() noexcept;
162     virtual Result sync() noexcept;
163
164     _TVG_DECLARE_ACCESSOR(Scene);
165     _TVG_DECLARE_PRIVATE(Canvas);
166 };
167
168
169
170 /**
171  * @class LinearGradient
172  *
173  * @ingroup ThorVG
174  *
175  * @brief description...
176  *
177  */
178 class TVG_EXPORT LinearGradient final : public Fill
179 {
180 public:
181     ~LinearGradient();
182
183     Result linear(float x1, float y1, float x2, float y2) noexcept;
184     Result linear(float* x1, float* y1, float* x2, float* y2) const noexcept;
185
186     static std::unique_ptr<LinearGradient> gen() noexcept;
187
188     _TVG_DECLARE_PRIVATE(LinearGradient);
189 };
190
191
192 /**
193  * @class RadialGradient
194  *
195  * @ingroup ThorVG
196  *
197  * @brief description...
198  *
199  */
200 class TVG_EXPORT RadialGradient final : public Fill
201 {
202 public:
203     ~RadialGradient();
204
205     Result radial(float cx, float cy, float radius) noexcept;
206     Result radial(float* cx, float* cy, float* radius) const noexcept;
207
208     static std::unique_ptr<RadialGradient> gen() noexcept;
209
210     _TVG_DECLARE_PRIVATE(RadialGradient);
211 };
212
213
214
215 /**
216  * @class Shape
217  *
218  * @ingroup ThorVG
219  *
220  * @brief description...
221  *
222  */
223 class TVG_EXPORT Shape final : public Paint
224 {
225 public:
226     ~Shape();
227
228     Result reset() noexcept;
229
230     //Path
231     Result moveTo(float x, float y) noexcept;
232     Result lineTo(float x, float y) noexcept;
233     Result cubicTo(float cx1, float cy1, float cx2, float cy2, float x, float y) noexcept;
234     Result close() noexcept;
235
236     //Shape
237     Result appendRect(float x, float y, float w, float h, float rx, float ry) noexcept;
238     Result appendCircle(float cx, float cy, float rx, float ry) noexcept;
239     Result appendPath(const PathCommand* cmds, uint32_t cmdCnt, const Point* pts, uint32_t ptsCnt) noexcept;
240
241     //Stroke
242     Result stroke(float width) noexcept;
243     Result stroke(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept;
244     Result stroke(const float* dashPattern, uint32_t cnt) noexcept;
245     Result stroke(StrokeCap cap) noexcept;
246     Result stroke(StrokeJoin join) noexcept;
247
248     //Fill
249     Result fill(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept;
250     Result fill(std::unique_ptr<Fill> f) 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
258     float strokeWidth() const noexcept;
259     Result strokeColor(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) const noexcept;
260     uint32_t strokeDash(const float** dashPattern) const noexcept;
261     StrokeCap strokeCap() const noexcept;
262     StrokeJoin strokeJoin() const noexcept;
263
264     static std::unique_ptr<Shape> gen() noexcept;
265
266     _TVG_DECLARE_PRIVATE(Shape);
267     _TVG_DECLARE_ACCESSOR(Canvas);
268     _TVG_DECLARE_ACCESSOR(Scene);
269 };
270
271
272 /**
273  * @class Scene
274  *
275  * @ingroup ThorVG
276  *
277  * @brief description...
278  *
279  */
280 class TVG_EXPORT Scene final : public Paint
281 {
282 public:
283     ~Scene();
284
285     Result push(std::unique_ptr<Paint> paint) noexcept;
286     Result reserve(uint32_t size) noexcept;
287     Result load(const std::string& path) noexcept;
288
289     static std::unique_ptr<Scene> gen() noexcept;
290
291     _TVG_DECLARE_ACCESSOR(Canvas);
292     _TVG_DECLARE_PRIVATE(Scene);
293 };
294
295
296 /**
297  * @class SwCanvas
298  *
299  * @ingroup ThorVG
300  *
301   @brief description...
302  *
303  */
304 class TVG_EXPORT SwCanvas final : public Canvas
305 {
306 public:
307     ~SwCanvas();
308
309     Result target(uint32_t* buffer, uint32_t stride, uint32_t w, uint32_t h) noexcept;
310
311     static std::unique_ptr<SwCanvas> gen() noexcept;
312
313     _TVG_DECLARE_PRIVATE(SwCanvas);
314 };
315
316
317 /**
318  * @class GlCanvas
319  *
320  * @ingroup ThorVG
321  *
322  * @brief description...
323  *
324  */
325 class TVG_EXPORT GlCanvas final : public Canvas
326 {
327 public:
328     ~GlCanvas();
329
330     //TODO: Gl Specific methods. Need gl backend configuration methods as well.
331     Result target(uint32_t* buffer, uint32_t stride, uint32_t w, uint32_t h) noexcept;
332
333     static std::unique_ptr<GlCanvas> gen() noexcept;
334
335     _TVG_DECLARE_PRIVATE(GlCanvas);
336 };
337
338
339 /**
340  * @class Engine
341  *
342  * @ingroup ThorVG
343  *
344  * @brief description...
345  *
346  */
347 class TVG_EXPORT Initializer final
348 {
349 public:
350
351     /**
352      * @brief ...
353      *
354      * @param[in] arg ...
355      *
356      * @note ...
357      *
358      * @return ...
359      *
360      * @see ...
361      */
362     static Result init(CanvasEngine engine) noexcept;
363     static Result term(CanvasEngine engine) noexcept;
364
365     _TVG_DISABLE_CTOR(Initializer);
366 };
367
368 } //namespace
369
370 #ifdef __cplusplus
371 }
372 #endif
373
374 #endif //_THORVG_H_