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"
30 static double timeStamp()
33 gettimeofday(&tv, NULL);
34 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_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)
45 using SwCoord = signed long;
46 using SwFixed = signed long long;
52 SwPoint& operator+=(const SwPoint& rhs)
59 SwPoint operator+(const SwPoint& rhs) const
61 return {x + rhs.x, y + rhs.y};
64 SwPoint operator-(const SwPoint& rhs) const
66 return {x - rhs.x, y - rhs.y};
69 bool operator==(const SwPoint& rhs) const
71 return (x == rhs.x && y == rhs.y);
74 bool operator!=(const SwPoint& rhs) const
76 return (x != rhs.x || y != rhs.y);
81 if (x == 0 && y == 0) return true;
88 if (abs(x) < 2 && abs(y) < 2) return true;
101 SwPoint* pts; //the outline's points
102 uint16_t ptsCnt; //number of points in the glyph
103 uint16_t reservedPtsCnt;
104 uint16_t* cntrs; //the contour end points
105 uint16_t cntrsCnt; //number of contours in glyph
106 uint16_t reservedCntrsCnt;
107 uint8_t* types; //curve type
108 bool* closed; //opened or closed path?
132 min.x = min.y = max.x = max.y = 0;
145 float a11, a12, shiftX;
146 float a21, a22, shiftY;
162 struct SwStrokeBorder
168 int32_t start; //index of current sub-path start point
169 bool movable; //true: for ends of lineto borders
178 SwFixed subPathAngle;
179 SwPoint ptStartSubPath;
180 SwFixed subPathLineLength;
185 StrokeJoin joinSaved;
186 SwFill* fill = nullptr;
188 SwStrokeBorder borders[2];
194 bool handleWideStrokes;
211 SwOutline* outline = nullptr;
212 SwStroke* stroke = nullptr;
213 SwFill* fill = nullptr;
214 SwRleData* rle = nullptr;
215 SwRleData* strokeRle = nullptr;
216 SwBBox bbox; //Keep it boundary without stroke region. Using for optimal filling.
218 bool fastTrack = false; //Fast Track: axis-aligned rectangle without any clips?
223 SwOutline* outline = nullptr;
224 SwRleData* rle = nullptr;
225 uint32_t* data = nullptr;
226 uint32_t w, h, stride;
227 int32_t ox = 0; //offset x
228 int32_t oy = 0; //offset y
231 bool direct = false; //draw image directly (with offset)
232 bool scaled = false; //draw scaled image
237 uint32_t (*join)(uint8_t r, uint8_t g, uint8_t b, uint8_t a);
238 uint32_t (*alpha)(uint32_t rgba);
239 uint32_t (*ialpha)(uint32_t rgba);
244 struct SwSurface : Surface
246 SwBlender blender; //mandatory
247 SwCompositor* compositor = nullptr; //compositor (optional)
250 struct SwCompositor : Compositor
252 SwSurface* recoverSfc; //Recover surface when composition is started
253 SwCompositor* recoverCmp; //Recover compositor when composition is done
262 SwOutline* strokeOutline;
266 static inline SwCoord TO_SWCOORD(float val)
268 return SwCoord(val * 64.0f);
271 static inline uint32_t ALPHA_BLEND(uint32_t c, uint32_t a)
273 return (((((c >> 8) & 0x00ff00ff) * a + 0x00ff00ff) & 0xff00ff00) +
274 ((((c & 0x00ff00ff) * a + 0x00ff00ff) >> 8) & 0x00ff00ff));
277 static inline uint32_t COLOR_INTERPOLATE(uint32_t c1, uint32_t a1, uint32_t c2, uint32_t a2)
279 auto t = (((c1 & 0xff00ff) * a1 + (c2 & 0xff00ff) * a2) >> 8) & 0xff00ff;
280 c1 = (((c1 >> 8) & 0xff00ff) * a1 + ((c2 >> 8) & 0xff00ff) * a2) & 0xff00ff00;
284 static inline SwCoord HALF_STROKE(float width)
286 return TO_SWCOORD(width * 0.5f);
289 int64_t mathMultiply(int64_t a, int64_t b);
290 int64_t mathDivide(int64_t a, int64_t b);
291 int64_t mathMulDiv(int64_t a, int64_t b, int64_t c);
292 void mathRotate(SwPoint& pt, SwFixed angle);
293 SwFixed mathTan(SwFixed angle);
294 SwFixed mathAtan(const SwPoint& pt);
295 SwFixed mathCos(SwFixed angle);
296 SwFixed mathSin(SwFixed angle);
297 void mathSplitCubic(SwPoint* base);
298 SwFixed mathDiff(SwFixed angle1, SwFixed angle2);
299 SwFixed mathLength(const SwPoint& pt);
300 bool mathSmallCubic(const SwPoint* base, SwFixed& angleIn, SwFixed& angleMid, SwFixed& angleOut);
301 SwFixed mathMean(SwFixed angle1, SwFixed angle2);
302 SwPoint mathTransform(const Point* to, const Matrix* transform);
303 bool mathUpdateOutlineBBox(const SwOutline* outline, const SwBBox& clipRegion, SwBBox& renderRegion, bool fastTrack);
304 bool mathClipBBox(const SwBBox& clipper, SwBBox& clipee);
306 void shapeReset(SwShape* shape);
307 bool shapePrepare(SwShape* shape, const Shape* sdata, const Matrix* transform, const SwBBox& clipRegion, SwBBox& renderRegion, SwMpool* mpool, unsigned tid, bool hasComposite);
308 bool shapePrepared(const SwShape* shape);
309 bool shapeGenRle(SwShape* shape, const Shape* sdata, bool antiAlias);
310 void shapeDelOutline(SwShape* shape, SwMpool* mpool, uint32_t tid);
311 void shapeResetStroke(SwShape* shape, const Shape* sdata, const Matrix* transform);
312 bool shapeGenStrokeRle(SwShape* shape, const Shape* sdata, const Matrix* transform, const SwBBox& clipRegion, SwBBox& renderRegion, SwMpool* mpool, unsigned tid);
313 void shapeFree(SwShape* shape);
314 void shapeDelStroke(SwShape* shape);
315 bool shapeGenFillColors(SwShape* shape, const Fill* fill, const Matrix* transform, SwSurface* surface, uint32_t opacity, bool ctable);
316 bool shapeGenStrokeFillColors(SwShape* shape, const Fill* fill, const Matrix* transform, SwSurface* surface, uint32_t opacity, bool ctable);
317 void shapeResetFill(SwShape* shape);
318 void shapeResetStrokeFill(SwShape* shape);
319 void shapeDelFill(SwShape* shape);
320 void shapeDelStrokeFill(SwShape* shape);
322 void strokeReset(SwStroke* stroke, const Shape* shape, const Matrix* transform);
323 bool strokeParseOutline(SwStroke* stroke, const SwOutline& outline);
324 SwOutline* strokeExportOutline(SwStroke* stroke, SwMpool* mpool, unsigned tid);
325 void strokeFree(SwStroke* stroke);
327 bool imagePrepare(SwImage* image, const Matrix* transform, const SwBBox& clipRegion, SwBBox& renderRegion, SwMpool* mpool, unsigned tid);
328 bool imageGenRle(SwImage* image, TVG_UNUSED const Picture* pdata, const SwBBox& renderRegion, bool antiAlias);
329 void imageDelOutline(SwImage* image, SwMpool* mpool, uint32_t tid);
330 void imageReset(SwImage* image);
331 void imageFree(SwImage* image);
333 bool fillGenColorTable(SwFill* fill, const Fill* fdata, const Matrix* transform, SwSurface* surface, uint32_t opacity, bool ctable);
334 void fillReset(SwFill* fill);
335 void fillFree(SwFill* fill);
336 void fillFetchLinear(const SwFill* fill, uint32_t* dst, uint32_t y, uint32_t x, uint32_t len);
337 void fillFetchRadial(const SwFill* fill, uint32_t* dst, uint32_t y, uint32_t x, uint32_t len);
339 SwRleData* rleRender(SwRleData* rle, const SwOutline* outline, const SwBBox& renderRegion, bool antiAlias);
340 void rleFree(SwRleData* rle);
341 void rleReset(SwRleData* rle);
342 void rleClipPath(SwRleData *rle, const SwRleData *clip);
343 void rleClipRect(SwRleData *rle, const SwBBox* clip);
345 SwMpool* mpoolInit(uint32_t threads);
346 bool mpoolTerm(SwMpool* mpool);
347 bool mpoolClear(SwMpool* mpool);
348 SwOutline* mpoolReqOutline(SwMpool* mpool, unsigned idx);
349 void mpoolRetOutline(SwMpool* mpool, unsigned idx);
350 SwOutline* mpoolReqStrokeOutline(SwMpool* mpool, unsigned idx);
351 void mpoolRetStrokeOutline(SwMpool* mpool, unsigned idx);
353 bool rasterCompositor(SwSurface* surface);
354 bool rasterGradientShape(SwSurface* surface, SwShape* shape, unsigned id);
355 bool rasterShape(SwSurface* surface, SwShape* shape, uint8_t r, uint8_t g, uint8_t b, uint8_t a);
356 bool rasterImage(SwSurface* surface, SwImage* image, const Matrix* transform, const SwBBox& bbox, uint32_t opacity);
357 bool rasterStroke(SwSurface* surface, SwShape* shape, uint8_t r, uint8_t g, uint8_t b, uint8_t a);
358 bool rasterGradientStroke(SwSurface* surface, SwShape* shape, unsigned id);
359 bool rasterClear(SwSurface* surface);
360 void rasterRGBA32(uint32_t *dst, uint32_t val, uint32_t offset, int32_t len);
361 void rasterUnpremultiply(SwSurface* surface);
363 #endif /* _TVG_SW_COMMON_H_ */