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"
27 /************************************************************************/
28 /* Internal Class Implementation */
29 /************************************************************************/
38 static float _lineLength(const Point& pt1, const Point& pt2)
40 /* approximate sqrt(x*x + y*y) using alpha max plus beta min algorithm.
41 With alpha = 1, beta = 3/8, giving results with the largest error less
42 than 7% compared to the exact value. */
43 Point diff = {pt2.x - pt1.x, pt2.y - pt1.y};
44 if (diff.x < 0) diff.x = -diff.x;
45 if (diff.y < 0) diff.y = -diff.y;
46 return (diff.x > diff.y) ? (diff.x + diff.y * 0.375f) : (diff.y + diff.x * 0.375f);
50 static void _lineSplitAt(const Line& cur, float at, Line& left, Line& right)
52 auto len = _lineLength(cur.pt1, cur.pt2);
53 auto dx = ((cur.pt2.x - cur.pt1.x) / len) * at;
54 auto dy = ((cur.pt2.y - cur.pt1.y) / len) * at;
56 left.pt2.x = left.pt1.x + dx;
57 left.pt2.y = left.pt1.y + dy;
63 static bool _growOutlineContour(SwOutline& outline, uint32_t n)
65 if (outline.reservedCntrsCnt >= outline.cntrsCnt + n) return false;
66 outline.reservedCntrsCnt = outline.cntrsCnt + n;
67 outline.cntrs = static_cast<uint16_t*>(realloc(outline.cntrs, outline.reservedCntrsCnt * sizeof(uint16_t)));
72 static void _reserveOutlineClose(SwOutline& outline)
74 //Dash outlines are always opened.
75 //Only normal outlines use this information, it sholud be same to their contour counts.
76 if (outline.closed) free(outline.closed);
77 outline.closed = static_cast<bool*>(calloc(outline.reservedCntrsCnt, sizeof(bool)));
81 static void _resetOutlineClose(SwOutline& outline)
83 memset(outline.closed, 0x0, outline.reservedCntrsCnt * sizeof(bool));
87 static void _growOutlinePoint(SwOutline& outline, uint32_t n)
89 if (outline.reservedPtsCnt >= outline.ptsCnt + n) return;
90 outline.reservedPtsCnt = outline.ptsCnt + n;
91 outline.pts = static_cast<SwPoint*>(realloc(outline.pts, outline.reservedPtsCnt * sizeof(SwPoint)));
92 outline.types = static_cast<uint8_t*>(realloc(outline.types, outline.reservedPtsCnt * sizeof(uint8_t)));
96 static void _outlineEnd(SwOutline& outline)
98 if (outline.ptsCnt == 0) return;
100 _growOutlineContour(outline, 1);
101 outline.cntrs[outline.cntrsCnt] = outline.ptsCnt - 1;
106 static void _outlineMoveTo(SwOutline& outline, const Point* to, const Matrix* transform)
108 _growOutlinePoint(outline, 1);
110 outline.pts[outline.ptsCnt] = mathTransform(to, transform);
111 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
113 if (outline.ptsCnt > 0) {
114 _growOutlineContour(outline, 1);
115 outline.cntrs[outline.cntrsCnt] = outline.ptsCnt - 1;
123 static void _outlineLineTo(SwOutline& outline, const Point* to, const Matrix* transform)
125 _growOutlinePoint(outline, 1);
127 outline.pts[outline.ptsCnt] = mathTransform(to, transform);
128 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
133 static void _outlineCubicTo(SwOutline& outline, const Point* ctrl1, const Point* ctrl2, const Point* to, const Matrix* transform)
135 _growOutlinePoint(outline, 3);
137 outline.pts[outline.ptsCnt] = mathTransform(ctrl1, transform);
138 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_CUBIC;
141 outline.pts[outline.ptsCnt] = mathTransform(ctrl2, transform);
142 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_CUBIC;
145 outline.pts[outline.ptsCnt] = mathTransform(to, transform);
146 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
151 static void _outlineClose(SwOutline& outline)
155 if (outline.cntrsCnt > 0) {
156 i = outline.cntrs[outline.cntrsCnt - 1] + 1;
161 //Make sure there is at least one point in the current path
162 if (outline.ptsCnt == i) return;
165 _growOutlinePoint(outline, 1);
167 outline.pts[outline.ptsCnt] = outline.pts[i];
168 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
170 outline.closed[outline.cntrsCnt] = true;
174 static void _dashLineTo(SwDashStroke& dash, const Point* to, const Matrix* transform)
176 _growOutlinePoint(*dash.outline, dash.outline->ptsCnt >> 1);
177 _growOutlineContour(*dash.outline, dash.outline->cntrsCnt >> 1);
179 Line cur = {dash.ptCur, *to};
180 auto len = _lineLength(cur.pt1, cur.pt2);
182 if (len < dash.curLen) {
184 if (!dash.curOpGap) {
185 _outlineMoveTo(*dash.outline, &dash.ptCur, transform);
186 _outlineLineTo(*dash.outline, to, transform);
189 while (len > dash.curLen) {
192 _lineSplitAt(cur, dash.curLen, left, right);;
193 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
194 if (!dash.curOpGap) {
195 _outlineMoveTo(*dash.outline, &left.pt1, transform);
196 _outlineLineTo(*dash.outline, &left.pt2, transform);
198 dash.curLen = dash.pattern[dash.curIdx];
199 dash.curOpGap = !dash.curOpGap;
201 dash.ptCur = cur.pt1;
205 if (!dash.curOpGap) {
206 _outlineMoveTo(*dash.outline, &cur.pt1, transform);
207 _outlineLineTo(*dash.outline, &cur.pt2, transform);
209 if (dash.curLen < 1 && TO_SWCOORD(len) > 1) {
211 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
212 dash.curLen = dash.pattern[dash.curIdx];
213 dash.curOpGap = !dash.curOpGap;
220 static void _dashCubicTo(SwDashStroke& dash, const Point* ctrl1, const Point* ctrl2, const Point* to, const Matrix* transform)
222 _growOutlinePoint(*dash.outline, dash.outline->ptsCnt >> 1);
223 _growOutlineContour(*dash.outline, dash.outline->cntrsCnt >> 1);
225 Bezier cur = {dash.ptCur, *ctrl1, *ctrl2, *to};
226 auto len = bezLength(cur);
228 if (len < dash.curLen) {
230 if (!dash.curOpGap) {
231 _outlineMoveTo(*dash.outline, &dash.ptCur, transform);
232 _outlineCubicTo(*dash.outline, ctrl1, ctrl2, to, transform);
235 while (len > dash.curLen) {
238 bezSplitAt(cur, dash.curLen, left, right);
239 if (!dash.curOpGap) {
240 // leftovers from a previous command don't require moveTo
241 if (dash.pattern[dash.curIdx] - dash.curLen < FLT_EPSILON) {
242 _outlineMoveTo(*dash.outline, &left.start, transform);
244 _outlineCubicTo(*dash.outline, &left.ctrl1, &left.ctrl2, &left.end, transform);
246 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
247 dash.curLen = dash.pattern[dash.curIdx];
248 dash.curOpGap = !dash.curOpGap;
250 dash.ptCur = right.start;
254 if (!dash.curOpGap) {
255 _outlineMoveTo(*dash.outline, &cur.start, transform);
256 _outlineCubicTo(*dash.outline, &cur.ctrl1, &cur.ctrl2, &cur.end, transform);
258 if (dash.curLen < 1 && TO_SWCOORD(len) > 1) {
260 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
261 dash.curLen = dash.pattern[dash.curIdx];
262 dash.curOpGap = !dash.curOpGap;
269 static SwOutline* _genDashOutline(const Shape* sdata, const Matrix* transform)
271 const PathCommand* cmds = nullptr;
272 auto cmdCnt = sdata->pathCommands(&cmds);
274 const Point* pts = nullptr;
275 auto ptsCnt = sdata->pathCoords(&pts);
277 //No actual shape data
278 if (cmdCnt == 0 || ptsCnt == 0) return nullptr;
283 dash.ptStart = {0, 0};
285 dash.curOpGap = false;
287 const float* pattern;
288 dash.cnt = sdata->strokeDash(&pattern);
289 if (dash.cnt == 0) return nullptr;
291 //OPTMIZE ME: Use mempool???
292 dash.pattern = const_cast<float*>(pattern);
293 dash.outline = static_cast<SwOutline*>(calloc(1, sizeof(SwOutline)));
296 auto outlinePtsCnt = 0;
297 auto outlineCntrsCnt = 0;
299 for (uint32_t i = 0; i < cmdCnt; ++i) {
300 switch(*(cmds + i)) {
301 case PathCommand::Close: {
305 case PathCommand::MoveTo: {
310 case PathCommand::LineTo: {
314 case PathCommand::CubicTo: {
321 ++outlinePtsCnt; //for close
322 ++outlineCntrsCnt; //for end
324 //No idea exact count.... Reserve Approximitely 20x...
325 _growOutlinePoint(*dash.outline, outlinePtsCnt * 20);
326 _growOutlineContour(*dash.outline, outlineCntrsCnt * 20);
328 while (cmdCnt-- > 0) {
330 case PathCommand::Close: {
331 _dashLineTo(dash, &dash.ptStart, transform);
334 case PathCommand::MoveTo: {
337 dash.curLen = *dash.pattern;
338 dash.curOpGap = false;
339 dash.ptStart = dash.ptCur = *pts;
343 case PathCommand::LineTo: {
344 _dashLineTo(dash, pts, transform);
348 case PathCommand::CubicTo: {
349 _dashCubicTo(dash, pts, pts + 1, pts + 2, transform);
357 _outlineEnd(*dash.outline);
363 static bool _fastTrack(const SwOutline* outline, SwBBox& bbox)
365 //Fast Track: Othogonal rectangle?
366 if (outline->ptsCnt != 5) return false;
368 /* NOTICE: If the antialiased pixels matter, we can turn off the fast track
369 in case the pixels have the pixel fraction. */
371 auto pt1 = outline->pts + 0;
372 auto pt2 = outline->pts + 1;
373 auto pt3 = outline->pts + 2;
374 auto pt4 = outline->pts + 3;
376 auto a = SwPoint{pt1->x, pt3->y};
377 auto b = SwPoint{pt3->x, pt1->y};
380 if ((*pt2 == a && *pt4 == b) || (*pt2 == b && *pt4 == a)) {
381 //Since no antialiasing is applied in the Fast Track case,
382 //the rasterization region has to be rearranged.
383 //https://github.com/Samsung/thorvg/issues/916
384 auto corner1 = outline->pts;
385 auto corner3 = outline->pts + 2;
387 auto xMin = corner1->x;
388 auto xMax = corner3->x;
394 auto yMin = corner1->y;
395 auto yMax = corner3->y;
401 bbox.min.x = static_cast<SwCoord>(round(xMin / 64.0f));
402 bbox.max.x = static_cast<SwCoord>(round(xMax / 64.0f));
403 bbox.min.y = static_cast<SwCoord>(round(yMin / 64.0f));
404 bbox.max.y = static_cast<SwCoord>(round(yMax / 64.0f));
414 static bool _genOutline(SwShape* shape, const Shape* sdata, const Matrix* transform, SwMpool* mpool, unsigned tid)
416 const PathCommand* cmds = nullptr;
417 auto cmdCnt = sdata->pathCommands(&cmds);
419 const Point* pts = nullptr;
420 auto ptsCnt = sdata->pathCoords(&pts);
422 //No actual shape data
423 if (cmdCnt == 0 || ptsCnt == 0) return false;
426 auto outlinePtsCnt = 0;
427 auto outlineCntrsCnt = 0;
430 for (uint32_t i = 0; i < cmdCnt; ++i) {
431 switch(*(cmds + i)) {
432 case PathCommand::Close: {
437 case PathCommand::MoveTo: {
442 case PathCommand::LineTo: {
446 case PathCommand::CubicTo: {
453 if (static_cast<uint32_t>(outlinePtsCnt - closeCnt) > ptsCnt) {
454 TVGERR("SW_ENGINE", "Wrong a pair of the commands & points - required(%d), current(%d)", outlinePtsCnt - closeCnt, ptsCnt);
458 ++outlinePtsCnt; //for close
459 ++outlineCntrsCnt; //for end
461 shape->outline = mpoolReqOutline(mpool, tid);
462 auto outline = shape->outline;
464 _growOutlinePoint(*outline, outlinePtsCnt);
466 if (_growOutlineContour(*outline, outlineCntrsCnt)) {
467 _reserveOutlineClose(*outline);
469 _resetOutlineClose(*outline);
473 while (cmdCnt-- > 0) {
475 case PathCommand::Close: {
476 _outlineClose(*outline);
479 case PathCommand::MoveTo: {
480 _outlineMoveTo(*outline, pts, transform);
484 case PathCommand::LineTo: {
485 _outlineLineTo(*outline, pts, transform);
489 case PathCommand::CubicTo: {
490 _outlineCubicTo(*outline, pts, pts + 1, pts + 2, transform);
498 _outlineEnd(*outline);
500 outline->fillRule = sdata->fillRule();
501 shape->outline = outline;
507 /************************************************************************/
508 /* External Class Implementation */
509 /************************************************************************/
511 bool shapePrepare(SwShape* shape, const Shape* sdata, const Matrix* transform, const SwBBox& clipRegion, SwBBox& renderRegion, SwMpool* mpool, unsigned tid)
513 if (!_genOutline(shape, sdata, transform, mpool, tid)) return false;
514 if (!mathUpdateOutlineBBox(shape->outline, clipRegion, renderRegion)) return false;
516 //Keep it for Rasterization Region
517 shape->bbox = renderRegion;
520 if (renderRegion.max.x - renderRegion.min.x < 1 && renderRegion.max.y - renderRegion.min.y < 1) return false;
523 if (renderRegion.min.x >= clipRegion.max.x || renderRegion.min.y >= clipRegion.max.y ||
524 renderRegion.max.x <= clipRegion.min.x || renderRegion.max.y <= clipRegion.min.y) return false;
530 bool shapePrepared(const SwShape* shape)
532 return shape->rle ? true : false;
536 bool shapeGenRle(SwShape* shape, TVG_UNUSED const Shape* sdata, bool antiAlias, bool hasComposite)
538 //FIXME: Should we draw it?
540 //if (shape.outline->opened) return true;
542 //Case A: Fast Track Rectangle Drawing
543 if (!hasComposite && (shape->rect = _fastTrack(shape->outline, shape->bbox))) return true;
545 //Case B: Normal Shape RLE Drawing
546 if ((shape->rle = rleRender(shape->rle, shape->outline, shape->bbox, antiAlias))) return true;
552 void shapeDelOutline(SwShape* shape, SwMpool* mpool, uint32_t tid)
554 mpoolRetOutline(mpool, tid);
555 shape->outline = nullptr;
559 void shapeReset(SwShape* shape)
561 rleReset(shape->rle);
562 rleReset(shape->strokeRle);
568 void shapeFree(SwShape* shape)
574 rleFree(shape->strokeRle);
575 strokeFree(shape->stroke);
580 void shapeDelStroke(SwShape* shape)
582 if (!shape->stroke) return;
583 rleFree(shape->strokeRle);
584 shape->strokeRle = nullptr;
585 strokeFree(shape->stroke);
586 shape->stroke = nullptr;
590 void shapeResetStroke(SwShape* shape, const Shape* sdata, const Matrix* transform)
592 if (!shape->stroke) shape->stroke = static_cast<SwStroke*>(calloc(1, sizeof(SwStroke)));
593 auto stroke = shape->stroke;
596 strokeReset(stroke, sdata, transform);
597 rleReset(shape->strokeRle);
601 bool shapeGenStrokeRle(SwShape* shape, const Shape* sdata, const Matrix* transform, const SwBBox& clipRegion, SwBBox& renderRegion, SwMpool* mpool, unsigned tid)
603 SwOutline* shapeOutline = nullptr;
604 SwOutline* strokeOutline = nullptr;
605 bool freeOutline = false;
609 if (sdata->strokeDash(nullptr) > 0) {
610 shapeOutline = _genDashOutline(sdata, transform);
611 if (!shapeOutline) return false;
613 //Normal Style stroke
615 if (!shape->outline) {
616 if (!_genOutline(shape, sdata, transform, mpool, tid)) return false;
618 shapeOutline = shape->outline;
621 if (!strokeParseOutline(shape->stroke, *shapeOutline)) {
626 strokeOutline = strokeExportOutline(shape->stroke, mpool, tid);
627 if (!strokeOutline) {
632 if (!mathUpdateOutlineBBox(strokeOutline, clipRegion, renderRegion)) {
637 shape->strokeRle = rleRender(shape->strokeRle, strokeOutline, renderRegion, true);
641 if (shapeOutline->cntrs) free(shapeOutline->cntrs);
642 if (shapeOutline->pts) free(shapeOutline->pts);
643 if (shapeOutline->types) free(shapeOutline->types);
644 if (shapeOutline->closed) free(shapeOutline->closed);
647 mpoolRetStrokeOutline(mpool, tid);
653 bool shapeGenFillColors(SwShape* shape, const Fill* fill, const Matrix* transform, SwSurface* surface, uint32_t opacity, bool ctable)
655 return fillGenColorTable(shape->fill, fill, transform, surface, opacity, ctable);
659 bool shapeGenStrokeFillColors(SwShape* shape, const Fill* fill, const Matrix* transform, SwSurface* surface, uint32_t opacity, bool ctable)
661 return fillGenColorTable(shape->stroke->fill, fill, transform, surface, opacity, ctable);
665 void shapeResetFill(SwShape* shape)
668 shape->fill = static_cast<SwFill*>(calloc(1, sizeof(SwFill)));
669 if (!shape->fill) return;
671 fillReset(shape->fill);
675 void shapeResetStrokeFill(SwShape* shape)
677 if (!shape->stroke->fill) {
678 shape->stroke->fill = static_cast<SwFill*>(calloc(1, sizeof(SwFill)));
679 if (!shape->stroke->fill) return;
681 fillReset(shape->stroke->fill);
685 void shapeDelFill(SwShape* shape)
687 if (!shape->fill) return;
688 fillFree(shape->fill);
689 shape->fill = nullptr;
693 void shapeDelStrokeFill(SwShape* shape)
695 if (!shape->stroke->fill) return;
696 fillFree(shape->stroke->fill);
697 shape->stroke->fill = nullptr;