2 * Copyright (c) 2020-2021 Samsung Electronics Co., Ltd. All rights reserved.
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:
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
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
22 #ifndef _TVG_SW_COMMON_H_
23 #define _TVG_SW_COMMON_H_
25 #include "tvgCommon.h"
26 #include "tvgRender.h"
28 #ifdef THORVG_AVX_VECTOR_SUPPORT
29 #include <immintrin.h>
34 static double timeStamp()
37 gettimeofday(&tv, NULL);
38 return (tv.tv_sec + tv.tv_usec / 1000000.0);
42 #define SW_CURVE_TYPE_POINT 0
43 #define SW_CURVE_TYPE_CUBIC 1
44 #define SW_ANGLE_PI (180L << 16)
45 #define SW_ANGLE_2PI (SW_ANGLE_PI << 1)
46 #define SW_ANGLE_PI2 (SW_ANGLE_PI >> 1)
47 #define SW_ANGLE_PI4 (SW_ANGLE_PI >> 2)
49 using SwCoord = signed long;
50 using SwFixed = signed long long;
56 SwPoint& operator+=(const SwPoint& rhs)
63 SwPoint operator+(const SwPoint& rhs) const
65 return {x + rhs.x, y + rhs.y};
68 SwPoint operator-(const SwPoint& rhs) const
70 return {x - rhs.x, y - rhs.y};
73 bool operator==(const SwPoint& rhs ) const
75 return (x == rhs.x && y == rhs.y);
78 bool operator!=(const SwPoint& rhs) const
80 return (x != rhs.x || y != rhs.y);
85 if (x == 0 && y == 0) return true;
92 if (abs(x) < 2 && abs(y) < 2) return true;
105 uint32_t* cntrs; //the contour end points
106 uint32_t cntrsCnt; //number of contours in glyph
107 uint32_t reservedCntrsCnt;
108 SwPoint* pts; //the outline's points
109 uint32_t ptsCnt; //number of points in the glyph
110 uint32_t reservedPtsCnt;
111 uint8_t* types; //curve type
113 bool opened; //opened path?
136 min.x = min.y = max.x = max.y = 0;
166 struct SwStrokeBorder
172 int32_t start; //index of current sub-path start point
173 bool movable; //true: for ends of lineto borders
182 SwFixed subPathAngle;
183 SwPoint ptStartSubPath;
184 SwFixed subPathLineLength;
189 StrokeJoin joinSaved;
190 SwFill* fill = nullptr;
192 SwStrokeBorder borders[2];
198 bool handleWideStrokes;
215 SwOutline* outline = nullptr;
216 SwStroke* stroke = nullptr;
217 SwFill* fill = nullptr;
218 SwRleData* rle = nullptr;
219 SwRleData* strokeRle = nullptr;
220 SwBBox bbox; //Keep it boundary without stroke region. Using for optimal filling.
222 bool rect = false; //Fast Track: Othogonal rectangle?
227 SwOutline* outline = nullptr;
228 SwRleData* rle = nullptr;
229 uint32_t* data = nullptr;
235 uint32_t (*join)(uint8_t r, uint8_t g, uint8_t b, uint8_t a);
236 uint32_t (*alpha)(uint32_t rgba);
241 struct SwSurface : Surface
243 SwBlender blender; //mandatory
244 SwCompositor* compositor = nullptr; //compositor (optional)
247 struct SwCompositor : Compositor
249 SwSurface* recoverSfc; //Recover surface when composition is started
250 SwCompositor* recoverCmp; //Recover compositor when composition is done
258 SwOutline* outline = nullptr;
259 SwOutline* strokeOutline = nullptr;
260 unsigned allocSize = 0;
263 static inline SwCoord TO_SWCOORD(float val)
265 return SwCoord(val * 64);
268 static inline uint32_t ALPHA_BLEND(uint32_t c, uint32_t a)
270 return (((((c >> 8) & 0x00ff00ff) * a + 0x00ff00ff) & 0xff00ff00) +
271 ((((c & 0x00ff00ff) * a + 0x00ff00ff) >> 8) & 0x00ff00ff));
274 static inline uint32_t COLOR_INTERPOLATE(uint32_t c1, uint32_t a1, uint32_t c2, uint32_t a2)
276 auto t = (((c1 & 0xff00ff) * a1 + (c2 & 0xff00ff) * a2) >> 8) & 0xff00ff;
277 c1 = (((c1 >> 8) & 0xff00ff) * a1 + ((c2 >> 8) & 0xff00ff) * a2) & 0xff00ff00;
281 static inline uint32_t ALPHA_MULTIPLY(uint32_t c, uint32_t a)
283 return ((c * a + 0xff) >> 8);
286 static inline SwCoord HALF_STROKE(float width)
288 return TO_SWCOORD(width * 0.5);
291 int64_t mathMultiply(int64_t a, int64_t b);
292 int64_t mathDivide(int64_t a, int64_t b);
293 int64_t mathMulDiv(int64_t a, int64_t b, int64_t c);
294 void mathRotate(SwPoint& pt, SwFixed angle);
295 SwFixed mathTan(SwFixed angle);
296 SwFixed mathAtan(const SwPoint& pt);
297 SwFixed mathCos(SwFixed angle);
298 SwFixed mathSin(SwFixed angle);
299 void mathSplitCubic(SwPoint* base);
300 SwFixed mathDiff(SwFixed angle1, SwFixed angle2);
301 SwFixed mathLength(const SwPoint& pt);
302 bool mathSmallCubic(const SwPoint* base, SwFixed& angleIn, SwFixed& angleMid, SwFixed& angleOut);
303 SwFixed mathMean(SwFixed angle1, SwFixed angle2);
304 SwPoint mathTransform(const Point* to, const Matrix* transform);
305 bool mathUpdateOutlineBBox(const SwOutline* outline, const SwBBox& clipRegion, SwBBox& renderRegion);
307 void shapeReset(SwShape* shape);
308 bool shapePrepare(SwShape* shape, const Shape* sdata, const Matrix* transform, const SwBBox& clipRegion, SwBBox& renderRegion, SwMpool* mpool, unsigned tid);
309 bool shapePrepared(const SwShape* shape);
310 bool shapeGenRle(SwShape* shape, const Shape* sdata, bool antiAlias, bool hasComposite);
311 void shapeDelOutline(SwShape* shape, SwMpool* mpool, uint32_t tid);
312 void shapeResetStroke(SwShape* shape, const Shape* sdata, const Matrix* transform);
313 bool shapeGenStrokeRle(SwShape* shape, const Shape* sdata, const Matrix* transform, const SwBBox& clipRegion, SwBBox& renderRegion, SwMpool* mpool, unsigned tid);
314 void shapeFree(SwShape* shape);
315 void shapeDelStroke(SwShape* shape);
316 bool shapeGenFillColors(SwShape* shape, const Fill* fill, const Matrix* transform, SwSurface* surface, uint32_t opacity, bool ctable);
317 bool shapeGenStrokeFillColors(SwShape* shape, const Fill* fill, const Matrix* transform, SwSurface* surface, uint32_t opacity, bool ctable);
318 void shapeResetFill(SwShape* shape);
319 void shapeResetStrokeFill(SwShape* shape);
320 void shapeDelFill(SwShape* shape);
321 void shapeDelStrokeFill(SwShape* shape);
323 void strokeReset(SwStroke* stroke, const Shape* shape, const Matrix* transform);
324 bool strokeParseOutline(SwStroke* stroke, const SwOutline& outline);
325 SwOutline* strokeExportOutline(SwStroke* stroke, SwMpool* mpool, unsigned tid);
326 void strokeFree(SwStroke* stroke);
328 bool imagePrepare(SwImage* image, const Picture* pdata, const Matrix* transform, const SwBBox& clipRegion, SwBBox& renderRegion, SwMpool* mpool, unsigned tid);
329 bool imagePrepared(const SwImage* image);
330 bool imageGenRle(SwImage* image, TVG_UNUSED const Picture* pdata, const SwBBox& renderRegion, bool antiAlias);
331 void imageDelOutline(SwImage* image, SwMpool* mpool, uint32_t tid);
332 void imageReset(SwImage* image);
333 void imageFree(SwImage* image);
335 bool fillGenColorTable(SwFill* fill, const Fill* fdata, const Matrix* transform, SwSurface* surface, uint32_t opacity, bool ctable);
336 void fillReset(SwFill* fill);
337 void fillFree(SwFill* fill);
338 void fillFetchLinear(const SwFill* fill, uint32_t* dst, uint32_t y, uint32_t x, uint32_t len);
339 void fillFetchRadial(const SwFill* fill, uint32_t* dst, uint32_t y, uint32_t x, uint32_t len);
341 SwRleData* rleRender(SwRleData* rle, const SwOutline* outline, const SwBBox& renderRegion, bool antiAlias);
342 void rleFree(SwRleData* rle);
343 void rleReset(SwRleData* rle);
344 void rleClipPath(SwRleData *rle, const SwRleData *clip);
345 void rleClipRect(SwRleData *rle, const SwBBox* clip);
346 void rleAlphaMask(SwRleData *rle, const SwRleData *clip);
348 SwMpool* mpoolInit(uint32_t threads);
349 bool mpoolTerm(SwMpool* mpool);
350 bool mpoolClear(SwMpool* mpool);
351 SwOutline* mpoolReqOutline(SwMpool* mpool, unsigned idx);
352 void mpoolRetOutline(SwMpool* mpool, unsigned idx);
353 SwOutline* mpoolReqStrokeOutline(SwMpool* mpool, unsigned idx);
354 void mpoolRetStrokeOutline(SwMpool* mpool, unsigned idx);
356 bool rasterCompositor(SwSurface* surface);
357 bool rasterGradientShape(SwSurface* surface, SwShape* shape, unsigned id);
358 bool rasterSolidShape(SwSurface* surface, SwShape* shape, uint8_t r, uint8_t g, uint8_t b, uint8_t a);
359 bool rasterImage(SwSurface* surface, SwImage* image, const Matrix* transform, const SwBBox& bbox, uint32_t opacity);
360 bool rasterStroke(SwSurface* surface, SwShape* shape, uint8_t r, uint8_t g, uint8_t b, uint8_t a);
361 bool rasterGradientStroke(SwSurface* surface, SwShape* shape, unsigned id);
362 bool rasterClear(SwSurface* surface);
364 static inline void rasterRGBA32(uint32_t *dst, uint32_t val, uint32_t offset, int32_t len)
366 #ifdef THORVG_AVX_VECTOR_SUPPORT
367 //1. calculate how many iterations we need to cover length
368 uint32_t iterations = len / 8;
369 uint32_t avxFilled = iterations * 8;
370 int32_t leftovers = 0;
372 //2. set beginning of the array
374 __m256i_u* avxDst = (__m256i_u*) dst;
377 for (uint32_t i = 0; i < iterations; ++i) {
378 *avxDst = _mm256_set1_epi32(val);
382 //4. fill leftovers (in first step we have to set pointer to place where avx job is done)
383 leftovers = len - avxFilled;
386 while (leftovers--) *dst++ = val;
389 while (len--) *dst++ = val;
393 #endif /* _TVG_SW_COMMON_H_ */