common shape: code refactoring & data optimization.
[platform/core/graphics/tizenvg.git] / src / lib / gl_engine / tvgGlGeometry.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_GL_GEOMETRY_H_
24 #define _TVG_GL_GEOMETRY_H_
25
26 #include <math.h>
27 #include <vector>
28 #include "tvgGlCommon.h"
29
30 #define PI 3.1415926535897932384626433832795f
31
32 #define MVP_MATRIX() \
33     float mvp[4*4] = { \
34         mTransform.scale, 0.0, 0.0f, 0.0f, \
35         0.0, mTransform.scale, 0.0f, 0.0f, \
36         0.0f, 0.0f, mTransform.scale, 0.0f, \
37         (mTransform.x * 2.0f) * (mTransform.scale / mTransform.w), -(mTransform.y * 2.0f) * (mTransform.scale / mTransform.h), 0.0f, 1.0f \
38     };
39
40 #define ROTATION_MATRIX(xPivot, yPivot) \
41     auto radian = mTransform.angle / 180.0f * PI; \
42     auto cosVal = cosf(radian);  \
43     auto sinVal = sinf(radian); \
44     float rotate[4*4] = { \
45         cosVal, -sinVal, 0.0f, 0.0f, \
46         sinVal, cosVal, 0.0f, 0.0f, \
47         0.0f, 0.0f, 1.0f, 0.0f, \
48         (xPivot * (1.0f - cosVal)) - (yPivot * sinVal), (yPivot *(1.0f - cosVal)) + (xPivot * sinVal), 0.0f, 1.0f \
49     };
50
51 #define MULTIPLY_MATRIX(A, B, transform) \
52     for(auto i = 0; i < 4; ++i) \
53     { \
54         for(auto j = 0; j < 4; ++j) \
55         { \
56             float sum = 0.0; \
57             for (auto k = 0; k < 4; ++k) \
58                 sum += A[k*4+i] * B[j*4+k]; \
59             transform[j*4+i] = sum; \
60         } \
61     }
62
63 #define GET_TRANSFORMATION(xPivot, yPivot, transform) \
64     MVP_MATRIX(); \
65     ROTATION_MATRIX(xPivot, yPivot); \
66     MULTIPLY_MATRIX(mvp, rotate, transform);
67
68 class GlPoint
69 {
70 public:
71     float x = 0.0f;
72     float y = 0.0f;
73
74     GlPoint() = default;
75
76     GlPoint(float pX, float pY):x(pX), y(pY)
77     {
78     }
79
80     GlPoint(const Point& rhs):GlPoint(rhs.x, rhs.y)
81     {
82     }
83
84     GlPoint(const GlPoint& rhs) = default;
85     GlPoint(GlPoint&& rhs) = default;
86
87     GlPoint& operator= (const GlPoint& rhs) = default;
88     GlPoint& operator= (GlPoint&& rhs) = default;
89
90     GlPoint& operator= (const Point& rhs)
91     {
92         x = rhs.x;
93         y = rhs.y;
94         return *this;
95     }
96
97     bool operator== (const GlPoint& rhs)
98     {
99         if (&rhs == this) return true;
100         if (rhs.x == this->x && rhs.y == this->y) return true;
101         return false;
102     }
103
104     bool operator!= (const GlPoint& rhs)
105     {
106         if (&rhs == this) return true;
107         if (rhs.x != this->x || rhs.y != this->y) return true;
108         return false;
109     }
110
111     GlPoint operator+ (const GlPoint& rhs) const
112     {
113         return GlPoint(x + rhs.x, y + rhs.y);
114     }
115
116     GlPoint operator+ (const float c) const
117     {
118         return GlPoint(x + c, y + c);
119     }
120
121     GlPoint operator- (const GlPoint& rhs) const
122     {
123         return GlPoint(x - rhs.x, y - rhs.y);
124     }
125
126     GlPoint operator- (const float c) const
127     {
128         return GlPoint(x - c, y - c);
129     }
130
131     GlPoint operator* (const GlPoint& rhs) const
132     {
133         return GlPoint(x * rhs.x, y * rhs.y);
134     }
135
136     GlPoint operator* (const float c) const
137     {
138         return GlPoint(x * c, y * c);
139     }
140
141     GlPoint operator/ (const GlPoint& rhs) const
142     {
143         return GlPoint(x / rhs.x, y / rhs.y);
144     }
145
146     GlPoint operator/ (const float c) const
147     {
148         return GlPoint(x / c, y / c);
149     }
150
151     void mod()
152     {
153         x = fabsf(x);
154         y = fabsf(y);
155     }
156
157     void normalize()
158     {
159         auto length = sqrtf((x * x) + (y * y));
160         if (length != 0.0f) {
161             const auto inverseLen = 1.0f / length;
162             x *= inverseLen;
163             y *= inverseLen;
164         }
165     }
166 };
167
168 typedef GlPoint GlSize;
169
170 struct SmoothPoint
171 {
172     GlPoint orgPt;
173     GlPoint fillOuterBlur;
174     GlPoint fillOuter;
175     GlPoint strokeOuterBlur;
176     GlPoint strokeOuter;
177     GlPoint strokeInnerBlur;
178     GlPoint strokeInner;
179
180     SmoothPoint(GlPoint pt)
181         :orgPt(pt),
182         fillOuterBlur(pt),
183         fillOuter(pt),
184         strokeOuterBlur(pt),
185         strokeOuter(pt),
186         strokeInnerBlur(pt),
187         strokeInner(pt)
188     {
189     }
190 };
191
192 struct PointNormals
193 {
194     GlPoint normal1;
195     GlPoint normal2;
196     GlPoint normalF;
197 };
198
199 struct VertexData
200 {
201    GlPoint point;
202    float opacity = 0.0f;
203 };
204
205 struct VertexDataArray
206 {
207     vector<VertexData>   vertices;
208     vector<uint32_t>     indices;
209 };
210
211 struct GlPrimitive
212 {
213     vector<SmoothPoint> mAAPoints;
214     VertexDataArray mFill;
215     VertexDataArray mStroke;
216     GlPoint mTopLeft;
217     GlPoint mBottomRight;
218     bool mIsClosed = false;
219 };
220
221 struct GlTransform
222 {
223     float x = 0.0f;
224     float y = 0.0f;
225     float angle = 0.0f;
226     float scale = 1.0f;
227     float w;
228     float h;
229     float matrix[16];
230 };
231
232 class GlGpuBuffer;
233
234 class GlGeometry
235 {
236 public:
237
238     uint32_t getPrimitiveCount();
239     const GlSize getPrimitiveSize(const uint32_t primitiveIndex) const;
240     bool decomposeOutline(const RenderShape& rshape);
241     bool generateAAPoints(TVG_UNUSED const RenderShape& rshape, float strokeWd, RenderUpdateFlag flag);
242     bool tesselate(TVG_UNUSED const RenderShape& rshape, float viewWd, float viewHt, RenderUpdateFlag flag);
243     void disableVertex(uint32_t location);
244     void draw(const uint32_t location, const uint32_t primitiveIndex, RenderUpdateFlag flag);
245     void updateTransform(const RenderTransform* transform, float w, float h);
246     float* getTransforMatrix();
247
248 private:
249     GlPoint normalizePoint(const GlPoint &pt, float viewWd, float viewHt);
250     void addGeometryPoint(VertexDataArray &geometry, const GlPoint &pt, float viewWd, float viewHt, float opacity);
251     GlPoint getNormal(const GlPoint &p1, const GlPoint &p2);
252     float dotProduct(const GlPoint &p1, const GlPoint &p2);
253     GlPoint extendEdge(const GlPoint &pt, const GlPoint &normal, float scalar);
254
255     void addPoint(GlPrimitive& primitve, const GlPoint &pt, GlPoint &min, GlPoint &max);
256     void addTriangleFanIndices(uint32_t &curPt, vector<uint32_t> &indices);
257     void addQuadIndices(uint32_t &curPt, vector<uint32_t> &indices);
258     bool isBezierFlat(const GlPoint &p1, const GlPoint &c1, const GlPoint &c2, const GlPoint &p2);
259     void decomposeCubicCurve(GlPrimitive& primitve, const GlPoint &pt1, const GlPoint &cpt1, const GlPoint &cpt2, const GlPoint &pt2, GlPoint &min, GlPoint &max);
260     void updateBuffer(const uint32_t location, const VertexDataArray& vertexArray);
261
262     unique_ptr<GlGpuBuffer> mGpuBuffer;
263     vector<GlPrimitive>     mPrimitives;
264     GlTransform             mTransform;
265 };
266
267 #endif /* _TVG_GL_GEOMETRY_H_ */