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 #include "tvgSwCommon.h"
23 #include "tvgBezier.h"
26 /************************************************************************/
27 /* Internal Class Implementation */
28 /************************************************************************/
37 static float _lineLength(const Point& pt1, const Point& pt2)
39 /* approximate sqrt(x*x + y*y) using alpha max plus beta min algorithm.
40 With alpha = 1, beta = 3/8, giving results with the largest error less
41 than 7% compared to the exact value. */
42 Point diff = {pt2.x - pt1.x, pt2.y - pt1.y};
43 if (diff.x < 0) diff.x = -diff.x;
44 if (diff.y < 0) diff.y = -diff.y;
45 return (diff.x > diff.y) ? (diff.x + diff.y * 0.375f) : (diff.y + diff.x * 0.375f);
49 static void _lineSplitAt(const Line& cur, float at, Line& left, Line& right)
51 auto len = _lineLength(cur.pt1, cur.pt2);
52 auto dx = ((cur.pt2.x - cur.pt1.x) / len) * at;
53 auto dy = ((cur.pt2.y - cur.pt1.y) / len) * at;
55 left.pt2.x = left.pt1.x + dx;
56 left.pt2.y = left.pt1.y + dy;
62 static bool _growOutlineContour(SwOutline& outline, uint32_t n)
64 if (outline.reservedCntrsCnt >= outline.cntrsCnt + n) return false;
65 outline.reservedCntrsCnt = outline.cntrsCnt + n;
66 outline.cntrs = static_cast<uint16_t*>(realloc(outline.cntrs, outline.reservedCntrsCnt * sizeof(uint16_t)));
71 static void _reserveOutlineClose(SwOutline& outline)
73 //Dash outlines are always opened.
74 //Only normal outlines use this information, it sholud be same to their contour counts.
75 if (outline.closed) free(outline.closed);
76 outline.closed = static_cast<bool*>(calloc(outline.reservedCntrsCnt, sizeof(bool)));
80 static void _resetOutlineClose(SwOutline& outline)
82 memset(outline.closed, 0x0, outline.reservedCntrsCnt * sizeof(bool));
86 static void _growOutlinePoint(SwOutline& outline, uint32_t n)
88 if (outline.reservedPtsCnt >= outline.ptsCnt + n) return;
89 outline.reservedPtsCnt = outline.ptsCnt + n;
90 outline.pts = static_cast<SwPoint*>(realloc(outline.pts, outline.reservedPtsCnt * sizeof(SwPoint)));
91 outline.types = static_cast<uint8_t*>(realloc(outline.types, outline.reservedPtsCnt * sizeof(uint8_t)));
95 static void _outlineEnd(SwOutline& outline)
97 if (outline.ptsCnt == 0) return;
99 _growOutlineContour(outline, 1);
100 outline.cntrs[outline.cntrsCnt] = outline.ptsCnt - 1;
105 static void _outlineMoveTo(SwOutline& outline, const Point* to, const Matrix* transform)
107 _growOutlinePoint(outline, 1);
109 outline.pts[outline.ptsCnt] = mathTransform(to, transform);
110 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
112 if (outline.ptsCnt > 0) {
113 _growOutlineContour(outline, 1);
114 outline.cntrs[outline.cntrsCnt] = outline.ptsCnt - 1;
122 static void _outlineLineTo(SwOutline& outline, const Point* to, const Matrix* transform)
124 _growOutlinePoint(outline, 1);
126 outline.pts[outline.ptsCnt] = mathTransform(to, transform);
127 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
132 static void _outlineCubicTo(SwOutline& outline, const Point* ctrl1, const Point* ctrl2, const Point* to, const Matrix* transform)
134 _growOutlinePoint(outline, 3);
136 outline.pts[outline.ptsCnt] = mathTransform(ctrl1, transform);
137 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_CUBIC;
140 outline.pts[outline.ptsCnt] = mathTransform(ctrl2, transform);
141 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_CUBIC;
144 outline.pts[outline.ptsCnt] = mathTransform(to, transform);
145 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
150 static void _outlineClose(SwOutline& outline)
154 if (outline.cntrsCnt > 0) {
155 i = outline.cntrs[outline.cntrsCnt - 1] + 1;
160 //Make sure there is at least one point in the current path
161 if (outline.ptsCnt == i) return;
164 _growOutlinePoint(outline, 1);
166 outline.pts[outline.ptsCnt] = outline.pts[i];
167 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
169 outline.closed[outline.cntrsCnt] = true;
173 static void _dashLineTo(SwDashStroke& dash, const Point* to, const Matrix* transform)
175 _growOutlinePoint(*dash.outline, dash.outline->ptsCnt >> 1);
176 _growOutlineContour(*dash.outline, dash.outline->cntrsCnt >> 1);
178 Line cur = {dash.ptCur, *to};
179 auto len = _lineLength(cur.pt1, cur.pt2);
181 if (len < dash.curLen) {
183 if (!dash.curOpGap) {
184 _outlineMoveTo(*dash.outline, &dash.ptCur, transform);
185 _outlineLineTo(*dash.outline, to, transform);
188 while (len > dash.curLen) {
191 _lineSplitAt(cur, dash.curLen, left, right);;
192 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
193 if (!dash.curOpGap) {
194 _outlineMoveTo(*dash.outline, &left.pt1, transform);
195 _outlineLineTo(*dash.outline, &left.pt2, transform);
197 dash.curLen = dash.pattern[dash.curIdx];
198 dash.curOpGap = !dash.curOpGap;
200 dash.ptCur = cur.pt1;
204 if (!dash.curOpGap) {
205 _outlineMoveTo(*dash.outline, &cur.pt1, transform);
206 _outlineLineTo(*dash.outline, &cur.pt2, transform);
208 if (dash.curLen < 1 && TO_SWCOORD(len) > 1) {
210 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
211 dash.curLen = dash.pattern[dash.curIdx];
212 dash.curOpGap = !dash.curOpGap;
219 static void _dashCubicTo(SwDashStroke& dash, const Point* ctrl1, const Point* ctrl2, const Point* to, const Matrix* transform)
221 _growOutlinePoint(*dash.outline, dash.outline->ptsCnt >> 1);
222 _growOutlineContour(*dash.outline, dash.outline->cntrsCnt >> 1);
224 Bezier cur = {dash.ptCur, *ctrl1, *ctrl2, *to};
225 auto len = bezLength(cur);
227 if (len < dash.curLen) {
229 if (!dash.curOpGap) {
230 _outlineMoveTo(*dash.outline, &dash.ptCur, transform);
231 _outlineCubicTo(*dash.outline, ctrl1, ctrl2, to, transform);
234 while (len > dash.curLen) {
237 bezSplitAt(cur, dash.curLen, left, right);
238 if (!dash.curOpGap) {
239 // leftovers from a previous command don't require moveTo
240 if (dash.pattern[dash.curIdx] - dash.curLen < FLT_EPSILON) {
241 _outlineMoveTo(*dash.outline, &left.start, transform);
243 _outlineCubicTo(*dash.outline, &left.ctrl1, &left.ctrl2, &left.end, transform);
245 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
246 dash.curLen = dash.pattern[dash.curIdx];
247 dash.curOpGap = !dash.curOpGap;
249 dash.ptCur = right.start;
253 if (!dash.curOpGap) {
254 _outlineMoveTo(*dash.outline, &cur.start, transform);
255 _outlineCubicTo(*dash.outline, &cur.ctrl1, &cur.ctrl2, &cur.end, transform);
257 if (dash.curLen < 1 && TO_SWCOORD(len) > 1) {
259 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
260 dash.curLen = dash.pattern[dash.curIdx];
261 dash.curOpGap = !dash.curOpGap;
268 static SwOutline* _genDashOutline(const Shape* sdata, const Matrix* transform)
270 const PathCommand* cmds = nullptr;
271 auto cmdCnt = sdata->pathCommands(&cmds);
273 const Point* pts = nullptr;
274 auto ptsCnt = sdata->pathCoords(&pts);
276 //No actual shape data
277 if (cmdCnt == 0 || ptsCnt == 0) return nullptr;
282 dash.ptStart = {0, 0};
284 dash.curOpGap = false;
286 const float* pattern;
287 dash.cnt = sdata->strokeDash(&pattern);
288 if (dash.cnt == 0) return nullptr;
290 //OPTMIZE ME: Use mempool???
291 dash.pattern = const_cast<float*>(pattern);
292 dash.outline = static_cast<SwOutline*>(calloc(1, sizeof(SwOutline)));
295 auto outlinePtsCnt = 0;
296 auto outlineCntrsCnt = 0;
298 for (uint32_t i = 0; i < cmdCnt; ++i) {
299 switch(*(cmds + i)) {
300 case PathCommand::Close: {
304 case PathCommand::MoveTo: {
309 case PathCommand::LineTo: {
313 case PathCommand::CubicTo: {
320 ++outlinePtsCnt; //for close
321 ++outlineCntrsCnt; //for end
323 //No idea exact count.... Reserve Approximitely 20x...
324 _growOutlinePoint(*dash.outline, outlinePtsCnt * 20);
325 _growOutlineContour(*dash.outline, outlineCntrsCnt * 20);
327 while (cmdCnt-- > 0) {
329 case PathCommand::Close: {
330 _dashLineTo(dash, &dash.ptStart, transform);
333 case PathCommand::MoveTo: {
336 dash.curLen = *dash.pattern;
337 dash.curOpGap = false;
338 dash.ptStart = dash.ptCur = *pts;
342 case PathCommand::LineTo: {
343 _dashLineTo(dash, pts, transform);
347 case PathCommand::CubicTo: {
348 _dashCubicTo(dash, pts, pts + 1, pts + 2, transform);
356 _outlineEnd(*dash.outline);
362 static bool _fastTrack(const SwOutline* outline)
364 //Fast Track: Othogonal rectangle?
365 if (outline->ptsCnt != 5) return false;
367 auto pt1 = outline->pts + 0;
368 auto pt2 = outline->pts + 1;
369 auto pt3 = outline->pts + 2;
370 auto pt4 = outline->pts + 3;
372 auto a = SwPoint{pt1->x, pt3->y};
373 auto b = SwPoint{pt3->x, pt1->y};
375 if ((*pt2 == a && *pt4 == b) || (*pt2 == b && *pt4 == a)) return true;
382 static bool _genOutline(SwShape* shape, const Shape* sdata, const Matrix* transform, SwMpool* mpool, unsigned tid)
384 const PathCommand* cmds = nullptr;
385 auto cmdCnt = sdata->pathCommands(&cmds);
387 const Point* pts = nullptr;
388 auto ptsCnt = sdata->pathCoords(&pts);
390 //No actual shape data
391 if (cmdCnt == 0 || ptsCnt == 0) return false;
394 auto outlinePtsCnt = 0;
395 auto outlineCntrsCnt = 0;
398 for (uint32_t i = 0; i < cmdCnt; ++i) {
399 switch(*(cmds + i)) {
400 case PathCommand::Close: {
405 case PathCommand::MoveTo: {
410 case PathCommand::LineTo: {
414 case PathCommand::CubicTo: {
421 if (static_cast<uint32_t>(outlinePtsCnt - closeCnt) > ptsCnt) {
422 TVGERR("SW_ENGINE", "Wrong a pair of the commands & points - required(%d), current(%d)", outlinePtsCnt - closeCnt, ptsCnt);
426 ++outlinePtsCnt; //for close
427 ++outlineCntrsCnt; //for end
429 shape->outline = mpoolReqOutline(mpool, tid);
430 auto outline = shape->outline;
432 _growOutlinePoint(*outline, outlinePtsCnt);
434 if (_growOutlineContour(*outline, outlineCntrsCnt)) {
435 _reserveOutlineClose(*outline);
437 _resetOutlineClose(*outline);
441 while (cmdCnt-- > 0) {
443 case PathCommand::Close: {
444 _outlineClose(*outline);
447 case PathCommand::MoveTo: {
448 _outlineMoveTo(*outline, pts, transform);
452 case PathCommand::LineTo: {
453 _outlineLineTo(*outline, pts, transform);
457 case PathCommand::CubicTo: {
458 _outlineCubicTo(*outline, pts, pts + 1, pts + 2, transform);
466 _outlineEnd(*outline);
468 outline->fillRule = sdata->fillRule();
469 shape->outline = outline;
475 /************************************************************************/
476 /* External Class Implementation */
477 /************************************************************************/
479 bool shapePrepare(SwShape* shape, const Shape* sdata, const Matrix* transform, const SwBBox& clipRegion, SwBBox& renderRegion, SwMpool* mpool, unsigned tid)
481 if (!_genOutline(shape, sdata, transform, mpool, tid)) return false;
482 if (!mathUpdateOutlineBBox(shape->outline, clipRegion, renderRegion)) return false;
484 //Keep it for Rasterization Region
485 shape->bbox = renderRegion;
488 if (renderRegion.max.x - renderRegion.min.x < 1 && renderRegion.max.y - renderRegion.min.y < 1) return false;
491 if (renderRegion.min.x >= clipRegion.max.x || renderRegion.min.y >= clipRegion.max.y ||
492 renderRegion.max.x <= clipRegion.min.x || renderRegion.max.y <= clipRegion.min.y) return false;
498 bool shapePrepared(const SwShape* shape)
500 return shape->rle ? true : false;
504 bool shapeGenRle(SwShape* shape, TVG_UNUSED const Shape* sdata, bool antiAlias, bool hasComposite)
506 //FIXME: Should we draw it?
508 //if (shape.outline->opened) return true;
510 //Case A: Fast Track Rectangle Drawing
511 if (!hasComposite && (shape->rect = _fastTrack(shape->outline))) return true;
512 //Case B: Normale Shape RLE Drawing
513 if ((shape->rle = rleRender(shape->rle, shape->outline, shape->bbox, antiAlias))) return true;
519 void shapeDelOutline(SwShape* shape, SwMpool* mpool, uint32_t tid)
521 mpoolRetOutline(mpool, tid);
522 shape->outline = nullptr;
526 void shapeReset(SwShape* shape)
528 rleReset(shape->rle);
529 rleReset(shape->strokeRle);
535 void shapeFree(SwShape* shape)
541 rleFree(shape->strokeRle);
542 strokeFree(shape->stroke);
547 void shapeDelStroke(SwShape* shape)
549 if (!shape->stroke) return;
550 rleFree(shape->strokeRle);
551 shape->strokeRle = nullptr;
552 strokeFree(shape->stroke);
553 shape->stroke = nullptr;
557 void shapeResetStroke(SwShape* shape, const Shape* sdata, const Matrix* transform)
559 if (!shape->stroke) shape->stroke = static_cast<SwStroke*>(calloc(1, sizeof(SwStroke)));
560 auto stroke = shape->stroke;
563 strokeReset(stroke, sdata, transform);
564 rleReset(shape->strokeRle);
568 bool shapeGenStrokeRle(SwShape* shape, const Shape* sdata, const Matrix* transform, const SwBBox& clipRegion, SwBBox& renderRegion, SwMpool* mpool, unsigned tid)
570 SwOutline* shapeOutline = nullptr;
571 SwOutline* strokeOutline = nullptr;
572 bool freeOutline = false;
576 if (sdata->strokeDash(nullptr) > 0) {
577 shapeOutline = _genDashOutline(sdata, transform);
578 if (!shapeOutline) return false;
580 //Normal Style stroke
582 if (!shape->outline) {
583 if (!_genOutline(shape, sdata, transform, mpool, tid)) return false;
585 shapeOutline = shape->outline;
588 if (!strokeParseOutline(shape->stroke, *shapeOutline)) {
593 strokeOutline = strokeExportOutline(shape->stroke, mpool, tid);
594 if (!strokeOutline) {
599 if (!mathUpdateOutlineBBox(strokeOutline, clipRegion, renderRegion)) {
604 shape->strokeRle = rleRender(shape->strokeRle, strokeOutline, renderRegion, true);
608 if (shapeOutline->cntrs) free(shapeOutline->cntrs);
609 if (shapeOutline->pts) free(shapeOutline->pts);
610 if (shapeOutline->types) free(shapeOutline->types);
611 if (shapeOutline->closed) free(shapeOutline->closed);
614 mpoolRetStrokeOutline(mpool, tid);
620 bool shapeGenFillColors(SwShape* shape, const Fill* fill, const Matrix* transform, SwSurface* surface, uint32_t opacity, bool ctable)
622 return fillGenColorTable(shape->fill, fill, transform, surface, opacity, ctable);
626 bool shapeGenStrokeFillColors(SwShape* shape, const Fill* fill, const Matrix* transform, SwSurface* surface, uint32_t opacity, bool ctable)
628 return fillGenColorTable(shape->stroke->fill, fill, transform, surface, opacity, ctable);
632 void shapeResetFill(SwShape* shape)
635 shape->fill = static_cast<SwFill*>(calloc(1, sizeof(SwFill)));
636 if (!shape->fill) return;
638 fillReset(shape->fill);
642 void shapeResetStrokeFill(SwShape* shape)
644 if (!shape->stroke->fill) {
645 shape->stroke->fill = static_cast<SwFill*>(calloc(1, sizeof(SwFill)));
646 if (!shape->stroke->fill) return;
648 fillReset(shape->stroke->fill);
652 void shapeDelFill(SwShape* shape)
654 if (!shape->fill) return;
655 fillFree(shape->fill);
656 shape->fill = nullptr;
660 void shapeDelStrokeFill(SwShape* shape)
662 if (!shape->stroke->fill) return;
663 fillFree(shape->stroke->fill);
664 shape->stroke->fill = nullptr;