88fe50255365cc4533baa7ce79a4159d135cd3bb
[platform/core/graphics/tizenvg.git] / src / lib / tvgRender.h
1 /*
2  * Copyright (c) 2020 - 2023 the ThorVG project. All rights reserved.
3
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10
11  * The above copyright notice and this permission notice shall be included in all
12  * copies or substantial portions of the Software.
13
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  */
22
23 #ifndef _TVG_RENDER_H_
24 #define _TVG_RENDER_H_
25
26 #include "tvgCommon.h"
27 #include "tvgArray.h"
28
29 namespace tvg
30 {
31
32 enum RenderUpdateFlag {None = 0, Path = 1, Color = 2, Gradient = 4, Stroke = 8, Transform = 16, Image = 32, GradientStroke = 64, All = 255};
33
34 struct Surface
35 {
36     //TODO: Union for multiple types
37     uint32_t* buffer;
38     uint32_t  stride;
39     uint32_t  w, h;
40     uint32_t  cs;
41 };
42
43 using RenderData = void*;
44
45 struct Compositor
46 {
47     CompositeMethod method;
48     uint32_t        opacity;
49 };
50
51 struct RenderRegion
52 {
53     int32_t x, y, w, h;
54
55     void intersect(const RenderRegion& rhs)
56     {
57         auto x1 = x + w;
58         auto y1 = y + h;
59         auto x2 = rhs.x + rhs.w;
60         auto y2 = rhs.y + rhs.h;
61
62         x = (x > rhs.x) ? x : rhs.x;
63         y = (y > rhs.y) ? y : rhs.y;
64         w = ((x1 < x2) ? x1 : x2) - x;
65         h = ((y1 < y2) ? y1 : y2) - y;
66
67         if (w < 0) w = 0;
68         if (h < 0) h = 0;
69     }
70 };
71
72 struct RenderTransform
73 {
74     Matrix m;             //3x3 Matrix Elements
75     float x = 0.0f;
76     float y = 0.0f;
77     float degree = 0.0f;  //rotation degree
78     float scale = 1.0f;   //scale factor
79     bool overriding = false;  //user transform?
80
81     bool update();
82     void override(const Matrix& m);
83
84     RenderTransform();
85     RenderTransform(const RenderTransform* lhs, const RenderTransform* rhs);
86 };
87
88
89 class RenderMethod
90 {
91 public:
92     virtual ~RenderMethod() {}
93     virtual RenderData prepare(const Shape& shape, RenderData data, const RenderTransform* transform, uint32_t opacity, Array<RenderData>& clips, RenderUpdateFlag flags, bool clipper) = 0;
94     virtual RenderData prepare(Surface* image, Polygon* triangles, uint32_t triangleCnt, RenderData data, const RenderTransform* transform, uint32_t opacity, Array<RenderData>& clips, RenderUpdateFlag flags) = 0;
95     virtual bool preRender() = 0;
96     virtual bool renderShape(RenderData data) = 0;
97     virtual bool renderImage(RenderData data) = 0;
98     virtual bool renderImageMesh(RenderData data) = 0;
99     virtual bool postRender() = 0;
100     virtual bool dispose(RenderData data) = 0;
101     virtual RenderRegion region(RenderData data) = 0;
102     virtual RenderRegion viewport() = 0;
103     virtual bool viewport(const RenderRegion& vp) = 0;
104
105     virtual bool clear() = 0;
106     virtual bool sync() = 0;
107
108     virtual Compositor* target(const RenderRegion& region) = 0;
109     virtual bool beginComposite(Compositor* cmp, CompositeMethod method, uint32_t opacity) = 0;
110     virtual bool endComposite(Compositor* cmp) = 0;
111 };
112
113 }
114
115 #endif //_TVG_RENDER_H_