2 * Copyright (c) 2020 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) {
62 SwPoint operator+(const SwPoint& rhs) const {
63 return {x + rhs.x, y + rhs.y};
66 SwPoint operator-(const SwPoint& rhs) const {
67 return {x - rhs.x, y - rhs.y};
70 bool operator==(const SwPoint& rhs ) const {
71 return (x == rhs.x && y == rhs.y);
74 bool operator!=(const SwPoint& rhs) const {
75 return (x != rhs.x || y != rhs.y);
80 if (x == 0 && y == 0) return true;
87 if (abs(x) < 2 && abs(y) < 2) return true;
100 uint32_t* cntrs; //the contour end points
101 uint32_t cntrsCnt; //number of contours in glyph
102 uint32_t reservedCntrsCnt;
103 SwPoint* pts; //the outline's points
104 uint32_t ptsCnt; //number of points in the glyph
105 uint32_t reservedPtsCnt;
106 uint8_t* types; //curve type
108 bool opened; //opened path?
130 struct SwStrokeBorder
136 int32_t start; //index of current sub-path start point
137 bool movable; //true: for ends of lineto borders
147 SwFixed subPathAngle;
148 SwPoint ptStartSubPath;
149 SwFixed subPathLineLength;
154 StrokeJoin joinSaved;
156 SwStrokeBorder borders[2];
162 bool handleWideStrokes;
205 SwOutline* outline = nullptr;
206 SwStroke* stroke = nullptr;
207 SwFill* fill = nullptr;
208 SwRleData* rle = nullptr;
209 SwRleData* strokeRle = nullptr;
210 SwBBox bbox; //keep it boundary without stroke region. Using for optimal filling.
212 bool rect; //Fast Track: Othogonal rectangle?
217 SwOutline* outline = nullptr;
218 SwRleData* rle = nullptr;
219 uint32_t* data = nullptr;
225 uint32_t (*join)(uint8_t r, uint8_t g, uint8_t b, uint8_t a);
226 uint32_t (*alpha)(uint32_t rgba);
231 struct SwSurface : Surface
233 SwBlender blender; //mandatory
234 SwCompositor* compositor = nullptr; //compositor (optional)
237 struct SwCompositor : Compositor
239 SwSurface* recoverSfc; //Recover surface when composition is started
240 SwCompositor* recoverCmp; //Recover compositor when composition is done
246 static inline SwCoord TO_SWCOORD(float val)
248 return SwCoord(val * 64);
251 static inline uint32_t ALPHA_BLEND(uint32_t c, uint32_t a)
253 return (((((c >> 8) & 0x00ff00ff) * a) & 0xff00ff00) +
254 ((((c & 0x00ff00ff) * a) >> 8) & 0x00ff00ff));
257 static inline uint32_t COLOR_INTERPOLATE(uint32_t c1, uint32_t a1, uint32_t c2, uint32_t a2)
259 auto t = (((c1 & 0xff00ff) * a1 + (c2 & 0xff00ff) * a2) >> 8) & 0xff00ff;
260 c1 = (((c1 >> 8) & 0xff00ff) * a1 + ((c2 >> 8) & 0xff00ff) * a2) & 0xff00ff00;
264 static inline uint8_t ALPHA_MULTIPLY(uint32_t c, uint32_t a)
269 static inline SwCoord HALF_STROKE(float width)
271 return TO_SWCOORD(width * 0.5);
274 int64_t mathMultiply(int64_t a, int64_t b);
275 int64_t mathDivide(int64_t a, int64_t b);
276 int64_t mathMulDiv(int64_t a, int64_t b, int64_t c);
277 void mathRotate(SwPoint& pt, SwFixed angle);
278 SwFixed mathTan(SwFixed angle);
279 SwFixed mathAtan(const SwPoint& pt);
280 SwFixed mathCos(SwFixed angle);
281 SwFixed mathSin(SwFixed angle);
282 void mathSplitCubic(SwPoint* base);
283 SwFixed mathDiff(SwFixed angle1, SwFixed angle2);
284 SwFixed mathLength(SwPoint& pt);
285 bool mathSmallCubic(SwPoint* base, SwFixed& angleIn, SwFixed& angleMid, SwFixed& angleOut);
286 SwFixed mathMean(SwFixed angle1, SwFixed angle2);
287 SwPoint mathTransform(const Point* to, const Matrix* transform);
289 void shapeReset(SwShape* shape);
290 bool shapeGenOutline(SwShape* shape, const Shape* sdata, unsigned tid, const Matrix* transform);
291 bool shapePrepare(SwShape* shape, const Shape* sdata, unsigned tid, const SwSize& clip, const Matrix* transform, SwBBox& bbox);
292 bool shapePrepared(SwShape* shape);
293 bool shapeGenRle(SwShape* shape, const Shape* sdata, const SwSize& clip, bool antiAlias, bool hasComposite);
294 void shapeDelOutline(SwShape* shape, uint32_t tid);
295 void shapeResetStroke(SwShape* shape, const Shape* sdata, const Matrix* transform);
296 bool shapeGenStrokeRle(SwShape* shape, const Shape* sdata, unsigned tid, const Matrix* transform, const SwSize& clip, SwBBox& bbox);
297 void shapeFree(SwShape* shape);
298 void shapeDelStroke(SwShape* shape);
299 bool shapeGenFillColors(SwShape* shape, const Fill* fill, const Matrix* transform, SwSurface* surface, uint32_t opacity, bool ctable);
300 void shapeResetFill(SwShape* shape);
301 void shapeDelFill(SwShape* shape);
303 void strokeReset(SwStroke* stroke, const Shape* shape, const Matrix* transform);
304 bool strokeParseOutline(SwStroke* stroke, const SwOutline& outline);
305 SwOutline* strokeExportOutline(SwStroke* stroke, unsigned tid);
306 void strokeFree(SwStroke* stroke);
308 bool imagePrepare(SwImage* image, const Picture* pdata, unsigned tid, const SwSize& clip, const Matrix* transform, SwBBox& bbox);
309 bool imagePrepared(SwImage* image);
310 bool imageGenRle(SwImage* image, TVG_UNUSED const Picture* pdata, const SwSize& clip, SwBBox& bbox, bool antiAlias, bool hasComposite);
311 void imageDelOutline(SwImage* image, uint32_t tid);
312 void imageReset(SwImage* image);
313 bool imageGenOutline(SwImage* image, const Picture* pdata, unsigned tid, const Matrix* transform);
314 void imageFree(SwImage* image);
316 bool fillGenColorTable(SwFill* fill, const Fill* fdata, const Matrix* transform, SwSurface* surface, uint32_t opacity, bool ctable);
317 void fillReset(SwFill* fill);
318 void fillFree(SwFill* fill);
319 void fillFetchLinear(const SwFill* fill, uint32_t* dst, uint32_t y, uint32_t x, uint32_t offset, uint32_t len);
320 void fillFetchRadial(const SwFill* fill, uint32_t* dst, uint32_t y, uint32_t x, uint32_t len);
322 SwRleData* rleRender(SwRleData* rle, const SwOutline* outline, const SwBBox& bbox, const SwSize& clip, bool antiAlias);
323 void rleFree(SwRleData* rle);
324 void rleReset(SwRleData* rle);
325 void rleClipPath(SwRleData *rle, const SwRleData *clip);
326 void rleClipRect(SwRleData *rle, const SwBBox* clip);
327 void rleAlphaMask(SwRleData *rle, const SwRleData *clip);
329 bool mpoolInit(uint32_t threads);
332 SwOutline* mpoolReqOutline(unsigned idx);
333 void mpoolRetOutline(unsigned idx);
334 SwOutline* mpoolReqStrokeOutline(unsigned idx);
335 void mpoolRetStrokeOutline(unsigned idx);
337 bool rasterCompositor(SwSurface* surface);
338 bool rasterGradientShape(SwSurface* surface, SwShape* shape, unsigned id);
339 bool rasterSolidShape(SwSurface* surface, SwShape* shape, uint8_t r, uint8_t g, uint8_t b, uint8_t a);
340 bool rasterImage(SwSurface* surface, SwImage* image, const Matrix* transform, SwBBox& bbox, uint32_t opacity);
341 bool rasterStroke(SwSurface* surface, SwShape* shape, uint8_t r, uint8_t g, uint8_t b, uint8_t a);
342 bool rasterClear(SwSurface* surface);
344 static inline void rasterRGBA32(uint32_t *dst, uint32_t val, uint32_t offset, int32_t len)
346 #ifdef THORVG_AVX_VECTOR_SUPPORT
347 int32_t align = (8 - (offset % 8)) % 8;
349 auto avxDst = (__m256i*)(dst + offset + align);
350 int32_t i = (len - align);
351 for (;i > 7; i -= 8, ++avxDst) {
352 *avxDst = _mm256_set1_epi32(val);
356 if (align > len) align -= (align - len);
357 auto tmp = dst + offset;
358 for (; align > 0; --align, ++tmp) *tmp = val;
361 dst += offset + (len - i);
362 while (i-- > 0) *(dst++) = val;
365 while (len--) *dst++ = val;
369 #endif /* _TVG_SW_COMMON_H_ */