updated copyright.
[platform/core/graphics/tizenvg.git] / src / lib / sw_engine / tvgSwCommon.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_SW_COMMON_H_
24 #define _TVG_SW_COMMON_H_
25
26 #include "tvgCommon.h"
27 #include "tvgRender.h"
28
29 #if 0
30 #include <sys/time.h>
31 static double timeStamp()
32 {
33    struct timeval tv;
34    gettimeofday(&tv, NULL);
35    return (tv.tv_sec + tv.tv_usec / 1000000.0);
36 }
37 #endif
38
39 #define SW_CURVE_TYPE_POINT 0
40 #define SW_CURVE_TYPE_CUBIC 1
41 #define SW_ANGLE_PI (180L << 16)
42 #define SW_ANGLE_2PI (SW_ANGLE_PI << 1)
43 #define SW_ANGLE_PI2 (SW_ANGLE_PI >> 1)
44 #define SW_ANGLE_PI4 (SW_ANGLE_PI >> 2)
45
46 using SwCoord = signed long;
47 using SwFixed = signed long long;
48
49 struct SwPoint
50 {
51     SwCoord x, y;
52
53     SwPoint& operator+=(const SwPoint& rhs)
54     {
55         x += rhs.x;
56         y += rhs.y;
57         return *this;
58     }
59
60     SwPoint operator+(const SwPoint& rhs) const
61     {
62         return {x + rhs.x, y + rhs.y};
63     }
64
65     SwPoint operator-(const SwPoint& rhs) const
66     {
67         return {x - rhs.x, y - rhs.y};
68     }
69
70     bool operator==(const SwPoint& rhs) const
71     {
72         return (x == rhs.x && y == rhs.y);
73     }
74
75     bool operator!=(const SwPoint& rhs) const
76     {
77         return (x != rhs.x || y != rhs.y);
78     }
79
80     bool zero() const
81     {
82         if (x == 0 && y == 0) return true;
83         else return false;
84     }
85
86     bool small() const
87     {
88         //2 is epsilon...
89         if (abs(x) < 2 && abs(y) < 2) return true;
90         else return false;
91     }
92
93 };
94
95 struct SwSize
96 {
97     SwCoord w, h;
98 };
99
100 struct SwOutline
101 {
102     SwPoint*      pts;              //the outline's points
103     uint32_t      ptsCnt;           //number of points in the glyph
104     uint32_t      reservedPtsCnt;
105     uint32_t*     cntrs;            //the contour end points
106     uint16_t      cntrsCnt;         //number of contours in glyph
107     uint16_t      reservedCntrsCnt;
108     uint8_t*      types;            //curve type
109     bool*         closed;           //opened or closed path?
110     FillRule      fillRule;
111 };
112
113 struct SwSpan
114 {
115     uint16_t x, y;
116     uint16_t len;
117     uint8_t coverage;
118 };
119
120 struct SwRleData
121 {
122     SwSpan *spans;
123     uint32_t alloc;
124     uint32_t size;
125 };
126
127 struct SwBBox
128 {
129     SwPoint min, max;
130
131     void reset()
132     {
133         min.x = min.y = max.x = max.y = 0;
134     }
135 };
136
137 struct SwFill
138 {
139     struct SwLinear {
140         float dx, dy;
141         float len;
142         float offset;
143     };
144
145     struct SwRadial {
146         float a11, a12, shiftX;
147         float a21, a22, shiftY;
148         float detSecDeriv;
149         float a;
150     };
151
152     union {
153         SwLinear linear;
154         SwRadial radial;
155     };
156
157     uint32_t* ctable;
158     FillSpread spread;
159
160     bool translucent;
161 };
162
163 struct SwStrokeBorder
164 {
165     uint32_t ptsCnt;
166     uint32_t maxPts;
167     SwPoint* pts;
168     uint8_t* tags;
169     int32_t start;     //index of current sub-path start point
170     bool movable;      //true: for ends of lineto borders
171 };
172
173 struct SwStroke
174 {
175     SwFixed angleIn;
176     SwFixed angleOut;
177     SwPoint center;
178     SwFixed lineLength;
179     SwFixed subPathAngle;
180     SwPoint ptStartSubPath;
181     SwFixed subPathLineLength;
182     SwFixed width;
183
184     StrokeCap cap;
185     StrokeJoin join;
186     StrokeJoin joinSaved;
187     SwFill* fill = nullptr;
188
189     SwStrokeBorder borders[2];
190
191     float sx, sy;
192
193     bool firstPt;
194     bool closedSubPath;
195     bool handleWideStrokes;
196 };
197
198 struct SwDashStroke
199 {
200     SwOutline* outline;
201     float curLen;
202     int32_t curIdx;
203     Point ptStart;
204     Point ptCur;
205     float* pattern;
206     uint32_t cnt;
207     bool curOpGap;
208 };
209
210 struct SwShape
211 {
212     SwOutline*   outline = nullptr;
213     SwStroke*    stroke = nullptr;
214     SwFill*      fill = nullptr;
215     SwRleData*   rle = nullptr;
216     SwRleData*   strokeRle = nullptr;
217     SwBBox       bbox;           //Keep it boundary without stroke region. Using for optimal filling.
218
219     bool         fastTrack = false;   //Fast Track: axis-aligned rectangle without any clips?
220 };
221
222 struct SwImage
223 {
224     SwOutline*   outline = nullptr;
225     SwRleData*   rle = nullptr;
226     uint32_t*    data = nullptr;
227     uint32_t     w, h, stride;
228     int32_t      ox = 0;         //offset x
229     int32_t      oy = 0;         //offset y
230     float        scale;
231
232     bool         direct = false;  //draw image directly (with offset)
233     bool         scaled = false;  //draw scaled image
234 };
235
236 struct SwBlender
237 {
238     uint32_t (*join)(uint8_t r, uint8_t g, uint8_t b, uint8_t a);
239     uint32_t (*lumaValue)(uint32_t c);
240 };
241
242 struct SwCompositor;
243
244 struct SwSurface : Surface
245 {
246     SwBlender blender;                    //mandatory
247     SwCompositor* compositor = nullptr;   //compositor (optional)
248 };
249
250 struct SwCompositor : Compositor
251 {
252     SwSurface* recoverSfc;                  //Recover surface when composition is started
253     SwCompositor* recoverCmp;               //Recover compositor when composition is done
254     SwImage image;
255     SwBBox bbox;
256     bool valid;
257 };
258
259 struct SwMpool
260 {
261     SwOutline* outline;
262     SwOutline* strokeOutline;
263     unsigned allocSize;
264 };
265
266 static inline SwCoord TO_SWCOORD(float val)
267 {
268     return SwCoord(val * 64.0f);
269 }
270
271 static inline uint32_t ALPHA_BLEND(uint32_t c, uint32_t a)
272 {
273     return (((((c >> 8) & 0x00ff00ff) * a + 0x00ff00ff) & 0xff00ff00) +
274             ((((c & 0x00ff00ff) * a + 0x00ff00ff) >> 8) & 0x00ff00ff));
275 }
276
277 static inline uint32_t INTERPOLATE(uint32_t a, uint32_t c0, uint32_t c1)
278 {
279     return (((((((c0 >> 8) & 0xff00ff) - ((c1 >> 8) & 0xff00ff)) * a) + (c1 & 0xff00ff00)) & 0xff00ff00) + ((((((c0 & 0xff00ff) - (c1 & 0xff00ff)) * a) >> 8) + (c1 & 0xff00ff)) & 0xff00ff));
280 }
281
282 static inline SwCoord HALF_STROKE(float width)
283 {
284     return TO_SWCOORD(width * 0.5f);
285 }
286
287 int64_t mathMultiply(int64_t a, int64_t b);
288 int64_t mathDivide(int64_t a, int64_t b);
289 int64_t mathMulDiv(int64_t a, int64_t b, int64_t c);
290 void mathRotate(SwPoint& pt, SwFixed angle);
291 SwFixed mathTan(SwFixed angle);
292 SwFixed mathAtan(const SwPoint& pt);
293 SwFixed mathCos(SwFixed angle);
294 SwFixed mathSin(SwFixed angle);
295 void mathSplitCubic(SwPoint* base);
296 SwFixed mathDiff(SwFixed angle1, SwFixed angle2);
297 SwFixed mathLength(const SwPoint& pt);
298 bool mathSmallCubic(const SwPoint* base, SwFixed& angleIn, SwFixed& angleMid, SwFixed& angleOut);
299 SwFixed mathMean(SwFixed angle1, SwFixed angle2);
300 SwPoint mathTransform(const Point* to, const Matrix* transform);
301 bool mathUpdateOutlineBBox(const SwOutline* outline, const SwBBox& clipRegion, SwBBox& renderRegion, bool fastTrack);
302 bool mathClipBBox(const SwBBox& clipper, SwBBox& clipee);
303
304 void shapeReset(SwShape* shape);
305 bool shapePrepare(SwShape* shape, const Shape* sdata, const Matrix* transform, const SwBBox& clipRegion, SwBBox& renderRegion, SwMpool* mpool, unsigned tid, bool hasComposite);
306 bool shapePrepared(const SwShape* shape);
307 bool shapeGenRle(SwShape* shape, const Shape* sdata, bool antiAlias);
308 void shapeDelOutline(SwShape* shape, SwMpool* mpool, uint32_t tid);
309 void shapeResetStroke(SwShape* shape, const Shape* sdata, const Matrix* transform);
310 bool shapeGenStrokeRle(SwShape* shape, const Shape* sdata, const Matrix* transform, const SwBBox& clipRegion, SwBBox& renderRegion, SwMpool* mpool, unsigned tid);
311 void shapeFree(SwShape* shape);
312 void shapeDelStroke(SwShape* shape);
313 bool shapeGenFillColors(SwShape* shape, const Fill* fill, const Matrix* transform, SwSurface* surface, uint32_t opacity, bool ctable);
314 bool shapeGenStrokeFillColors(SwShape* shape, const Fill* fill, const Matrix* transform, SwSurface* surface, uint32_t opacity, bool ctable);
315 void shapeResetFill(SwShape* shape);
316 void shapeResetStrokeFill(SwShape* shape);
317 void shapeDelFill(SwShape* shape);
318 void shapeDelStrokeFill(SwShape* shape);
319
320 void strokeReset(SwStroke* stroke, const Shape* shape, const Matrix* transform);
321 bool strokeParseOutline(SwStroke* stroke, const SwOutline& outline);
322 SwOutline* strokeExportOutline(SwStroke* stroke, SwMpool* mpool, unsigned tid);
323 void strokeFree(SwStroke* stroke);
324
325 bool imagePrepare(SwImage* image, Polygon* triangles, uint32_t triangleCount, const Matrix* transform, const SwBBox& clipRegion, SwBBox& renderRegion, SwMpool* mpool, unsigned tid);
326 bool imageGenRle(SwImage* image, const SwBBox& renderRegion, bool antiAlias);
327 void imageDelOutline(SwImage* image, SwMpool* mpool, uint32_t tid);
328 void imageReset(SwImage* image);
329 void imageFree(SwImage* image);
330
331 bool fillGenColorTable(SwFill* fill, const Fill* fdata, const Matrix* transform, SwSurface* surface, uint32_t opacity, bool ctable);
332 void fillReset(SwFill* fill);
333 void fillFree(SwFill* fill);
334 void fillFetchLinear(const SwFill* fill, uint32_t* dst, uint32_t y, uint32_t x, uint32_t len);
335 void fillFetchRadial(const SwFill* fill, uint32_t* dst, uint32_t y, uint32_t x, uint32_t len);
336
337 SwRleData* rleRender(SwRleData* rle, const SwOutline* outline, const SwBBox& renderRegion, bool antiAlias);
338 void rleFree(SwRleData* rle);
339 void rleReset(SwRleData* rle);
340 void rleClipPath(SwRleData *rle, const SwRleData *clip);
341 void rleClipRect(SwRleData *rle, const SwBBox* clip);
342
343 SwMpool* mpoolInit(uint32_t threads);
344 bool mpoolTerm(SwMpool* mpool);
345 bool mpoolClear(SwMpool* mpool);
346 SwOutline* mpoolReqOutline(SwMpool* mpool, unsigned idx);
347 void mpoolRetOutline(SwMpool* mpool, unsigned idx);
348 SwOutline* mpoolReqStrokeOutline(SwMpool* mpool, unsigned idx);
349 void mpoolRetStrokeOutline(SwMpool* mpool, unsigned idx);
350
351 bool rasterCompositor(SwSurface* surface);
352 bool rasterGradientShape(SwSurface* surface, SwShape* shape, unsigned id);
353 bool rasterShape(SwSurface* surface, SwShape* shape, uint8_t r, uint8_t g, uint8_t b, uint8_t a);
354 bool rasterImage(SwSurface* surface, SwImage* image, const Matrix* transform, const SwBBox& bbox, uint32_t opacity);
355 bool rasterImageMesh(SwSurface* surface, SwImage* image, const Polygon* triangles, const uint32_t triangleCount, const Matrix* transform, const SwBBox& bbox, uint32_t opacity);
356 bool rasterStroke(SwSurface* surface, SwShape* shape, uint8_t r, uint8_t g, uint8_t b, uint8_t a);
357 bool rasterGradientStroke(SwSurface* surface, SwShape* shape, unsigned id);
358 bool rasterClear(SwSurface* surface);
359 void rasterRGBA32(uint32_t *dst, uint32_t val, uint32_t offset, int32_t len);
360 void rasterUnpremultiply(SwSurface* surface);
361
362 #endif /* _TVG_SW_COMMON_H_ */