sw_engine: threading optimization
[platform/core/graphics/tizenvg.git] / src / lib / sw_engine / tvgSwCommon.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 _TVG_SW_COMMON_H_
18 #define _TVG_SW_COMMON_H_
19
20 #include <future>
21 #include <thread>
22 #include "tvgCommon.h"
23
24 #if 0
25 #include <sys/time.h>
26 static double timeStamp()
27 {
28    struct timeval tv;
29    gettimeofday(&tv, NULL);
30    return (tv.tv_sec + tv.tv_usec / 1000000.0);
31 }
32 #endif
33
34 using namespace tvg;
35
36 #define SW_CURVE_TYPE_POINT 0
37 #define SW_CURVE_TYPE_CUBIC 1
38 #define SW_OUTLINE_FILL_WINDING 0
39 #define SW_OUTLINE_FILL_EVEN_ODD 1
40 #define SW_ANGLE_PI (180L << 16)
41 #define SW_ANGLE_2PI (SW_ANGLE_PI << 1)
42 #define SW_ANGLE_PI2 (SW_ANGLE_PI >> 1)
43 #define SW_ANGLE_PI4 (SW_ANGLE_PI >> 2)
44
45 using SwCoord = signed long;
46 using SwFixed = signed long long;
47
48 struct SwPoint
49 {
50     SwCoord x, y;
51
52     SwPoint& operator+=(const SwPoint& rhs) {
53         x += rhs.x;
54         y += rhs.y;
55         return *this;
56     }
57
58     SwPoint operator+(const SwPoint& rhs) const {
59         return {x + rhs.x, y + rhs.y};
60     }
61
62     SwPoint operator-(const SwPoint& rhs) const {
63         return {x - rhs.x, y - rhs.y};
64     }
65
66     bool operator==(const SwPoint& rhs ) const {
67         return (x == rhs.x && y == rhs.y);
68     }
69
70     bool operator!=(const SwPoint& rhs) const {
71         return (x != rhs.x || y != rhs.y);
72     }
73
74     bool zero()
75     {
76         if (x == 0 && y == 0) return true;
77         else return false;
78     }
79
80     bool small()
81     {
82         //2 is epsilon...
83         if (abs(x) < 2 && abs(y) < 2) return true;
84         else return false;
85     }
86
87 };
88
89 struct SwSize
90 {
91     SwCoord w, h;
92 };
93
94 struct SwOutline
95 {
96     uint32_t*     cntrs;            //the contour end points
97     uint32_t      cntrsCnt;         //number of contours in glyph
98     uint32_t      reservedCntrsCnt;
99     SwPoint*      pts;              //the outline's points
100     uint32_t      ptsCnt;           //number of points in the glyph
101     uint32_t      reservedPtsCnt;
102     uint8_t*      types;            //curve type
103     uint8_t       fillMode;         //outline fill mode
104     bool          opened;           //opened path?
105 };
106
107 struct SwSpan
108 {
109     int16_t x, y;
110     uint16_t len;
111     uint8_t coverage;
112 };
113
114 struct SwRleData
115 {
116     SwSpan *spans;
117     uint32_t alloc;
118     uint32_t size;
119 };
120
121 struct SwBBox
122 {
123     SwPoint min, max;
124 };
125
126 struct SwStrokeBorder
127 {
128     uint32_t ptsCnt;
129     uint32_t maxPts;
130     SwPoint* pts;
131     uint8_t* tags;
132     int32_t start;     //index of current sub-path start point
133     bool movable;      //true: for ends of lineto borders
134     bool valid;
135 };
136
137 struct SwStroke
138 {
139     SwFixed angleIn;
140     SwFixed angleOut;
141     SwPoint center;
142     SwFixed lineLength;
143     SwFixed subPathAngle;
144     SwPoint ptStartSubPath;
145     SwFixed subPathLineLength;
146     SwFixed width;
147
148     StrokeCap cap;
149     StrokeJoin join;
150     StrokeJoin joinSaved;
151
152     SwStrokeBorder borders[2];
153
154     bool firstPt;
155     bool openSubPath;
156     bool handleWideStrokes;
157 };
158
159 struct SwDashStroke
160 {
161     SwOutline* outline;
162     int32_t curLen;
163     int32_t curIdx;
164     Point ptStart;
165     Point ptCur;
166     float* pattern;
167     uint32_t cnt;
168     bool curOpGap;
169 };
170
171 struct SwFill
172 {
173     struct SwLinear {
174         float dx, dy;
175         float len;
176         float offset;
177     };
178
179     struct SwRadial {
180         float cx, cy;
181         float a;
182         float inv2a;
183     };
184
185     union {
186         SwLinear linear;
187         SwRadial radial;
188     };
189
190     uint32_t* ctable;
191     FillSpread spread;
192     bool translucent;
193 };
194
195 struct SwShape
196 {
197     SwOutline*   outline;
198     SwStroke*    stroke;
199     SwFill*      fill;
200     SwRleData*   rle;
201     SwRleData*   strokeRle;
202     SwBBox       bbox;
203 };
204
205
206 struct SwTask
207 {
208     SwShape shape;
209     const Shape* sdata;
210     SwSize clip;
211     const Matrix* transform;
212     RenderUpdateFlag flags;
213     future<void> prepared;
214 };
215
216 static inline SwPoint TO_SWPOINT(const Point* pt)
217 {
218     return {SwCoord(pt->x * 64), SwCoord(pt->y * 64)};
219 }
220
221
222 static inline SwCoord TO_SWCOORD(float val)
223 {
224     return SwCoord(val * 64);
225 }
226
227
228 static inline uint32_t COLOR_ALPHA(uint32_t rgba)
229 {
230   return (rgba >> 24) & 0xff;
231 }
232
233
234 static inline uint32_t COLOR_ALPHA_BLEND(uint32_t rgba, uint32_t alpha)
235 {
236   return (((((rgba >> 8) & 0x00ff00ff) * alpha) & 0xff00ff00) +
237           ((((rgba & 0x00ff00ff) * alpha) >> 8) & 0x00ff00ff));
238 }
239
240
241 static inline uint32_t COLOR_INTERPOLATE(uint32_t rgba1, uint32_t a, uint32_t rgba2, uint32_t b)
242 {
243    auto t = (((rgba1 & 0xff00ff) * a + (rgba2 & 0xff00ff) * b) >> 8) & 0xff00ff;
244    rgba1 = (((rgba1 >> 8) & 0xff00ff) * a + ((rgba2 >> 8) & 0xff00ff) * b) & 0xff00ff00;
245    return (rgba1 |= t);
246 }
247
248
249 static inline uint32_t COLOR_ARGB_JOIN(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
250 {
251   return (a << 24 | r << 16 | g << 8 | b);
252 }
253
254
255 static inline void COLOR_SET(uint32_t *dst, uint32_t val, uint32_t len)
256 {
257     while (len--) *dst++ = val;
258 }
259
260
261 int64_t mathMultiply(int64_t a, int64_t b);
262 int64_t mathDivide(int64_t a, int64_t b);
263 int64_t mathMulDiv(int64_t a, int64_t b, int64_t c);
264 void mathRotate(SwPoint& pt, SwFixed angle);
265 SwFixed mathTan(SwFixed angle);
266 SwFixed mathAtan(const SwPoint& pt);
267 SwFixed mathCos(SwFixed angle);
268 SwFixed mathSin(SwFixed angle);
269 void mathSplitCubic(SwPoint* base);
270 SwFixed mathDiff(SwFixed angle1, SwFixed angle2);
271 SwFixed mathLength(SwPoint& pt);
272 bool mathSmallCubic(SwPoint* base, SwFixed& angleIn, SwFixed& angleMid, SwFixed& angleOut);
273 SwFixed mathMean(SwFixed angle1, SwFixed angle2);
274
275 void shapeReset(SwShape& shape);
276 bool shapeGenOutline(SwShape& shape, const Shape* sdata);
277 bool shapeGenRle(SwShape& shape, const Shape* sdata, const SwSize& clip, const Matrix* transform);
278 void shapeDelOutline(SwShape& shape);
279 void shapeResetStroke(SwShape& shape, const Shape* sdata);
280 bool shapeGenStrokeRle(SwShape& shape, const Shape* sdata, const SwSize& clip);
281 void shapeFree(SwShape& shape);
282 void shapeDelStroke(SwShape& shape);
283 bool shapeGenFillColors(SwShape& shape, const Fill* fill, const Matrix* transform, bool ctable);
284 void shapeResetFill(SwShape& shape);
285 void shapeDelFill(SwShape& shape);
286
287 void strokeReset(SwStroke& stroke, const Shape* shape);
288 bool strokeParseOutline(SwStroke& stroke, const SwOutline& outline);
289 SwOutline* strokeExportOutline(SwStroke& stroke);
290 void strokeFree(SwStroke* stroke);
291
292 bool fillGenColorTable(SwFill* fill, const Fill* fdata, const Matrix* transform, bool ctable);
293 void fillReset(SwFill* fill);
294 void fillFree(SwFill* fill);
295 void fillFetchLinear(const SwFill* fill, uint32_t* dst, uint32_t y, uint32_t x, uint32_t len);
296 void fillFetchRadial(const SwFill* fill, uint32_t* dst, uint32_t y, uint32_t x, uint32_t len);
297
298 SwRleData* rleRender(const SwOutline* outline, const SwBBox& bbox, const SwSize& clip);
299 void rleFree(SwRleData* rle);
300
301 bool rasterGradientShape(Surface& surface, SwShape& shape, unsigned id);
302 bool rasterSolidShape(Surface& surface, SwShape& shape, uint8_t r, uint8_t g, uint8_t b, uint8_t a);
303 bool rasterStroke(Surface& surface, SwShape& shape, uint8_t r, uint8_t g, uint8_t b, uint8_t a);
304 bool rasterClear(Surface& surface);
305
306 #endif /* _TVG_SW_COMMON_H_ */