2 * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef _TVG_SW_COMMON_H_
18 #define _TVG_SW_COMMON_H_
20 #include "tvgCommon.h"
22 #ifdef THORVG_AVX_VECTOR_SUPPORT
23 #include <immintrin.h>
28 static double timeStamp()
31 gettimeofday(&tv, NULL);
32 return (tv.tv_sec + tv.tv_usec / 1000000.0);
38 #define SW_CURVE_TYPE_POINT 0
39 #define SW_CURVE_TYPE_CUBIC 1
40 #define SW_OUTLINE_FILL_WINDING 0
41 #define SW_OUTLINE_FILL_EVEN_ODD 1
42 #define SW_ANGLE_PI (180L << 16)
43 #define SW_ANGLE_2PI (SW_ANGLE_PI << 1)
44 #define SW_ANGLE_PI2 (SW_ANGLE_PI >> 1)
45 #define SW_ANGLE_PI4 (SW_ANGLE_PI >> 2)
47 using SwCoord = signed long;
48 using SwFixed = signed long long;
54 SwPoint& operator+=(const SwPoint& rhs) {
60 SwPoint operator+(const SwPoint& rhs) const {
61 return {x + rhs.x, y + rhs.y};
64 SwPoint operator-(const SwPoint& rhs) const {
65 return {x - rhs.x, y - rhs.y};
68 bool operator==(const SwPoint& rhs ) const {
69 return (x == rhs.x && y == rhs.y);
72 bool operator!=(const SwPoint& rhs) const {
73 return (x != rhs.x || y != rhs.y);
78 if (x == 0 && y == 0) return true;
85 if (abs(x) < 2 && abs(y) < 2) return true;
98 uint32_t* cntrs; //the contour end points
99 uint32_t cntrsCnt; //number of contours in glyph
100 uint32_t reservedCntrsCnt;
101 SwPoint* pts; //the outline's points
102 uint32_t ptsCnt; //number of points in the glyph
103 uint32_t reservedPtsCnt;
104 uint8_t* types; //curve type
105 uint8_t fillMode; //outline fill mode
106 bool opened; //opened path?
128 struct SwStrokeBorder
134 int32_t start; //index of current sub-path start point
135 bool movable; //true: for ends of lineto borders
145 SwFixed subPathAngle;
146 SwPoint ptStartSubPath;
147 SwFixed subPathLineLength;
152 StrokeJoin joinSaved;
154 SwStrokeBorder borders[2];
158 bool handleWideStrokes;
203 SwRleData* strokeRle;
206 bool rect; //Fast Track: Othogonal rectangle?
210 static inline SwCoord TO_SWCOORD(float val)
212 return SwCoord(val * 64);
216 static inline uint32_t COLOR_ALPHA(uint32_t rgba)
218 return (rgba >> 24) & 0xff;
222 static inline uint32_t COLOR_ALPHA_BLEND(uint32_t rgba, uint32_t alpha)
224 return (((((rgba >> 8) & 0x00ff00ff) * alpha) & 0xff00ff00) +
225 ((((rgba & 0x00ff00ff) * alpha) >> 8) & 0x00ff00ff));
229 static inline uint32_t COLOR_INTERPOLATE(uint32_t rgba1, uint32_t a, uint32_t rgba2, uint32_t b)
231 auto t = (((rgba1 & 0xff00ff) * a + (rgba2 & 0xff00ff) * b) >> 8) & 0xff00ff;
232 rgba1 = (((rgba1 >> 8) & 0xff00ff) * a + ((rgba2 >> 8) & 0xff00ff) * b) & 0xff00ff00;
237 static inline uint32_t COLOR_ARGB_JOIN(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
239 return (a << 24 | r << 16 | g << 8 | b);
243 int64_t mathMultiply(int64_t a, int64_t b);
244 int64_t mathDivide(int64_t a, int64_t b);
245 int64_t mathMulDiv(int64_t a, int64_t b, int64_t c);
246 void mathRotate(SwPoint& pt, SwFixed angle);
247 SwFixed mathTan(SwFixed angle);
248 SwFixed mathAtan(const SwPoint& pt);
249 SwFixed mathCos(SwFixed angle);
250 SwFixed mathSin(SwFixed angle);
251 void mathSplitCubic(SwPoint* base);
252 SwFixed mathDiff(SwFixed angle1, SwFixed angle2);
253 SwFixed mathLength(SwPoint& pt);
254 bool mathSmallCubic(SwPoint* base, SwFixed& angleIn, SwFixed& angleMid, SwFixed& angleOut);
255 SwFixed mathMean(SwFixed angle1, SwFixed angle2);
257 void shapeReset(SwShape& shape);
258 bool shapeGenOutline(SwShape& shape, const Shape* sdata, const Matrix* transform);
259 bool shapePrepare(SwShape& shape, const Shape* sdata, const SwSize& clip, const Matrix* transform);
260 bool shapeGenRle(SwShape& shape, const Shape* sdata, const SwSize& clip, bool antiAlias);
261 void shapeDelOutline(SwShape& shape);
262 void shapeResetStroke(SwShape& shape, const Shape* sdata);
263 bool shapeGenStrokeRle(SwShape& shape, const Shape* sdata, const Matrix* transform, const SwSize& clip);
264 void shapeFree(SwShape& shape);
265 void shapeDelStroke(SwShape& shape);
266 bool shapeGenFillColors(SwShape& shape, const Fill* fill, const Matrix* transform, bool ctable);
267 void shapeResetFill(SwShape& shape);
268 void shapeDelFill(SwShape& shape);
270 void strokeReset(SwStroke& stroke, const Shape* shape);
271 bool strokeParseOutline(SwStroke& stroke, const SwOutline& outline);
272 SwOutline* strokeExportOutline(SwStroke& stroke);
273 void strokeFree(SwStroke* stroke);
275 bool fillGenColorTable(SwFill* fill, const Fill* fdata, const Matrix* transform, bool ctable);
276 void fillReset(SwFill* fill);
277 void fillFree(SwFill* fill);
278 void fillFetchLinear(const SwFill* fill, uint32_t* dst, uint32_t y, uint32_t x, uint32_t offset, uint32_t len);
279 void fillFetchRadial(const SwFill* fill, uint32_t* dst, uint32_t y, uint32_t x, uint32_t len);
281 SwRleData* rleRender(const SwOutline* outline, const SwBBox& bbox, const SwSize& clip, bool antiAlias);
282 void rleFree(SwRleData* rle);
284 bool rasterGradientShape(Surface& surface, SwShape& shape, unsigned id);
285 bool rasterSolidShape(Surface& surface, SwShape& shape, uint8_t r, uint8_t g, uint8_t b, uint8_t a);
286 bool rasterStroke(Surface& surface, SwShape& shape, uint8_t r, uint8_t g, uint8_t b, uint8_t a);
287 bool rasterClear(Surface& surface);
289 inline void rasterARGB32(uint32_t *dst, uint32_t val, uint32_t offset, int32_t len)
291 #ifdef THORVG_AVX_VECTOR_SUPPORT
292 int32_t align = (8 - (offset % 8)) % 8;
294 auto avxDst = (__m256i*)(dst + offset + align);
295 int32_t i = (len - align);
296 for (;i > 7; i -= 8, ++avxDst) {
297 *avxDst = _mm256_set1_epi32(val);
301 if (align > len) align -= (align - len);
302 auto tmp = dst + offset;
303 for (; align > 0; --align, ++tmp) *tmp = val;
306 dst += offset + (len - i);
307 while (i-- > 0) *(dst++) = val;
310 while (len--) *dst++ = val;
314 #endif /* _TVG_SW_COMMON_H_ */