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
23 #include "tvgSwCommon.h"
24 #include "tvgBezier.h"
26 /************************************************************************/
27 /* Internal Class Implementation */
28 /************************************************************************/
37 static SwPoint _transform(const Point* to, const Matrix* transform)
39 if (!transform) return {TO_SWCOORD(to->x), TO_SWCOORD(to->y)};
41 auto tx = round(to->x * transform->e11 + to->y * transform->e12 + transform->e13);
42 auto ty = round(to->x * transform->e21 + to->y * transform->e22 + transform->e23);
44 return {TO_SWCOORD(tx), TO_SWCOORD(ty)};
48 static float _lineLength(const Point& pt1, const Point& pt2)
50 /* approximate sqrt(x*x + y*y) using alpha max plus beta min algorithm.
51 With alpha = 1, beta = 3/8, giving results with the largest error less
52 than 7% compared to the exact value. */
53 Point diff = {pt2.x - pt1.x, pt2.y - pt1.y};
54 if (diff.x < 0) diff.x = -diff.x;
55 if (diff.y < 0) diff.y = -diff.y;
56 return (diff.x > diff.y) ? (diff.x + diff.y * 0.375f) : (diff.y + diff.x * 0.375f);
60 static void _lineSplitAt(const Line& cur, float at, Line& left, Line& right)
62 auto len = _lineLength(cur.pt1, cur.pt2);
63 auto dx = ((cur.pt2.x - cur.pt1.x) / len) * at;
64 auto dy = ((cur.pt2.y - cur.pt1.y) / len) * at;
66 left.pt2.x = left.pt1.x + dx;
67 left.pt2.y = left.pt1.y + dy;
73 static void _growOutlineContour(SwOutline& outline, uint32_t n)
75 if (outline.reservedCntrsCnt >= outline.cntrsCnt + n) return;
76 outline.reservedCntrsCnt = outline.cntrsCnt + n;
77 outline.cntrs = static_cast<uint32_t*>(realloc(outline.cntrs, outline.reservedCntrsCnt * sizeof(uint32_t)));
81 static void _growOutlinePoint(SwOutline& outline, uint32_t n)
83 if (outline.reservedPtsCnt >= outline.ptsCnt + n) return;
84 outline.reservedPtsCnt = outline.ptsCnt + n;
85 outline.pts = static_cast<SwPoint*>(realloc(outline.pts, outline.reservedPtsCnt * sizeof(SwPoint)));
86 outline.types = static_cast<uint8_t*>(realloc(outline.types, outline.reservedPtsCnt * sizeof(uint8_t)));
90 static void _delOutline(SwOutline* outline)
94 if (outline->cntrs) free(outline->cntrs);
95 if (outline->pts) free(outline->pts);
96 if (outline->types) free(outline->types);
101 static void _outlineEnd(SwOutline& outline)
103 _growOutlineContour(outline, 1);
104 if (outline.ptsCnt > 0) {
105 outline.cntrs[outline.cntrsCnt] = outline.ptsCnt - 1;
111 static void _outlineMoveTo(SwOutline& outline, const Point* to, const Matrix* transform)
113 _growOutlinePoint(outline, 1);
115 outline.pts[outline.ptsCnt] = _transform(to, transform);
117 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
119 if (outline.ptsCnt > 0) {
120 _growOutlineContour(outline, 1);
121 outline.cntrs[outline.cntrsCnt] = outline.ptsCnt - 1;
129 static void _outlineLineTo(SwOutline& outline, const Point* to, const Matrix* transform)
131 _growOutlinePoint(outline, 1);
133 outline.pts[outline.ptsCnt] = _transform(to, transform);
134 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
139 static void _outlineCubicTo(SwOutline& outline, const Point* ctrl1, const Point* ctrl2, const Point* to, const Matrix* transform)
141 _growOutlinePoint(outline, 3);
143 outline.pts[outline.ptsCnt] = _transform(ctrl1, transform);
144 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_CUBIC;
147 outline.pts[outline.ptsCnt] = _transform(ctrl2, transform);
148 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_CUBIC;
151 outline.pts[outline.ptsCnt] = _transform(to, transform);
152 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
157 static void _outlineClose(SwOutline& outline)
161 if (outline.cntrsCnt > 0) {
162 i = outline.cntrs[outline.cntrsCnt - 1] + 1;
167 //Make sure there is at least one point in the current path
168 if (outline.ptsCnt == i) {
169 outline.opened = true;
174 _growOutlinePoint(outline, 1);
176 outline.pts[outline.ptsCnt] = outline.pts[i];
177 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
180 outline.opened = false;
184 static void _initBBox(SwBBox& bbox)
186 bbox.min.x = bbox.min.y = 0;
187 bbox.max.x = bbox.max.y = 0;
191 static bool _updateBBox(SwOutline* outline, SwBBox& bbox)
193 if (!outline) return false;
195 auto pt = outline->pts;
197 if (outline->ptsCnt <= 0) {
209 for(uint32_t i = 1; i < outline->ptsCnt; ++i, ++pt) {
210 if (xMin > pt->x) xMin = pt->x;
211 if (xMax < pt->x) xMax = pt->x;
212 if (yMin > pt->y) yMin = pt->y;
213 if (yMax < pt->y) yMax = pt->y;
215 bbox.min.x = xMin >> 6;
216 bbox.max.x = (xMax + 63) >> 6;
217 bbox.min.y = yMin >> 6;
218 bbox.max.y = (yMax + 63) >> 6;
220 if (xMax - xMin < 1 && yMax - yMin < 1) return false;
226 static bool _checkValid(const SwOutline* outline, const SwBBox& bbox, const SwSize& clip)
228 if (outline->ptsCnt == 0 || outline->cntrsCnt <= 0) return false;
231 if (bbox.min.x >= clip.w || bbox.min.y >= clip.h || bbox.max.x <= 0 || bbox.max.y <= 0) return false;
237 static void _dashLineTo(SwDashStroke& dash, const Point* to, const Matrix* transform)
239 _growOutlinePoint(*dash.outline, dash.outline->ptsCnt >> 1);
240 _growOutlineContour(*dash.outline, dash.outline->cntrsCnt >> 1);
242 Line cur = {dash.ptCur, *to};
243 auto len = _lineLength(cur.pt1, cur.pt2);
245 if (len < dash.curLen) {
247 if (!dash.curOpGap) {
248 _outlineMoveTo(*dash.outline, &dash.ptCur, transform);
249 _outlineLineTo(*dash.outline, to, transform);
252 while (len > dash.curLen) {
255 _lineSplitAt(cur, dash.curLen, left, right);;
256 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
257 if (!dash.curOpGap) {
258 _outlineMoveTo(*dash.outline, &left.pt1, transform);
259 _outlineLineTo(*dash.outline, &left.pt2, transform);
261 dash.curLen = dash.pattern[dash.curIdx];
262 dash.curOpGap = !dash.curOpGap;
264 dash.ptCur = cur.pt1;
268 if (!dash.curOpGap) {
269 _outlineMoveTo(*dash.outline, &cur.pt1, transform);
270 _outlineLineTo(*dash.outline, &cur.pt2, transform);
272 if (dash.curLen < 1) {
274 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
275 dash.curLen = dash.pattern[dash.curIdx];
276 dash.curOpGap = !dash.curOpGap;
283 static void _dashCubicTo(SwDashStroke& dash, const Point* ctrl1, const Point* ctrl2, const Point* to, const Matrix* transform)
285 _growOutlinePoint(*dash.outline, dash.outline->ptsCnt >> 1);
286 _growOutlineContour(*dash.outline, dash.outline->cntrsCnt >> 1);
288 Bezier cur = { dash.ptCur, *ctrl1, *ctrl2, *to};
289 auto len = bezLength(cur);
291 if (len < dash.curLen) {
293 if (!dash.curOpGap) {
294 _outlineMoveTo(*dash.outline, &dash.ptCur, transform);
295 _outlineCubicTo(*dash.outline, ctrl1, ctrl2, to, transform);
298 while (len > dash.curLen) {
301 bezSplitAt(cur, dash.curLen, left, right);
302 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
303 if (!dash.curOpGap) {
304 _outlineMoveTo(*dash.outline, &left.start, transform);
305 _outlineCubicTo(*dash.outline, &left.ctrl1, &left.ctrl2, &left.end, transform);
307 dash.curLen = dash.pattern[dash.curIdx];
308 dash.curOpGap = !dash.curOpGap;
310 dash.ptCur = right.start;
314 if (!dash.curOpGap) {
315 _outlineMoveTo(*dash.outline, &cur.start, transform);
316 _outlineCubicTo(*dash.outline, &cur.ctrl1, &cur.ctrl2, &cur.end, transform);
318 if (dash.curLen < 1) {
320 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
321 dash.curLen = dash.pattern[dash.curIdx];
322 dash.curOpGap = !dash.curOpGap;
329 SwOutline* _genDashOutline(const Shape* sdata, const Matrix* transform)
331 const PathCommand* cmds = nullptr;
332 auto cmdCnt = sdata->pathCommands(&cmds);
334 const Point* pts = nullptr;
335 auto ptsCnt = sdata->pathCoords(&pts);
337 //No actual shape data
338 if (cmdCnt == 0 || ptsCnt == 0) return nullptr;
343 dash.ptStart = {0, 0};
345 dash.curOpGap = false;
347 const float* pattern;
348 dash.cnt = sdata->strokeDash(&pattern);
349 if (dash.cnt == 0) return nullptr;
351 //Is it safe to mutual exclusive?
352 dash.pattern = const_cast<float*>(pattern);
353 dash.outline = static_cast<SwOutline*>(calloc(1, sizeof(SwOutline)));
354 dash.outline->opened = true;
357 auto outlinePtsCnt = 0;
358 auto outlineCntrsCnt = 0;
360 for (uint32_t i = 0; i < cmdCnt; ++i) {
361 switch(*(cmds + i)) {
362 case PathCommand::Close: {
366 case PathCommand::MoveTo: {
371 case PathCommand::LineTo: {
375 case PathCommand::CubicTo: {
382 ++outlinePtsCnt; //for close
383 ++outlineCntrsCnt; //for end
385 //Reserve Approximitely 20x...
386 _growOutlinePoint(*dash.outline, outlinePtsCnt * 20);
387 _growOutlineContour(*dash.outline, outlineCntrsCnt * 20);
389 while (cmdCnt-- > 0) {
391 case PathCommand::Close: {
392 _dashLineTo(dash, &dash.ptStart, transform);
395 case PathCommand::MoveTo: {
398 dash.curLen = *dash.pattern;
399 dash.curOpGap = false;
400 dash.ptStart = dash.ptCur = *pts;
404 case PathCommand::LineTo: {
405 _dashLineTo(dash, pts, transform);
409 case PathCommand::CubicTo: {
410 _dashCubicTo(dash, pts, pts + 1, pts + 2, transform);
418 _outlineEnd(*dash.outline);
424 bool _fastTrack(const SwOutline* outline)
426 //Fast Track: Othogonal rectangle?
427 if (outline->ptsCnt != 5) return false;
429 auto pt1 = outline->pts + 0;
430 auto pt2 = outline->pts + 1;
431 auto pt3 = outline->pts + 2;
432 auto pt4 = outline->pts + 3;
434 auto min1 = pt1->y < pt3->y ? pt1 : pt3;
435 auto min2 = pt2->y < pt4->y ? pt2 : pt4;
436 if (min1->y != min2->y) return false;
438 SwCoord len1 = pow(pt1->x - pt3->x, 2) + pow(pt1->y - pt3->y, 2);
439 SwCoord len2 = pow(pt2->x - pt4->x, 2) + pow(pt2->y - pt4->y, 2);
440 if (len1 == len2) return true;
446 /************************************************************************/
447 /* External Class Implementation */
448 /************************************************************************/
450 bool shapePrepare(SwShape* shape, const Shape* sdata, const SwSize& clip, const Matrix* transform)
452 if (!shapeGenOutline(shape, sdata, transform)) return false;
454 if (!_updateBBox(shape->outline, shape->bbox)) return false;
456 if (!_checkValid(shape->outline, shape->bbox, clip)) return false;
462 bool shapePrepared(SwShape* shape)
464 return shape->rle ? true : false;
468 bool shapeGenRle(SwShape* shape, TVG_UNUSED const Shape* sdata, const SwSize& clip, bool antiAlias, bool hasComposite)
470 //FIXME: Should we draw it?
472 //if (shape.outline->opened) return true;
474 //Case A: Fast Track Rectangle Drawing
475 if (!hasComposite && (shape->rect = _fastTrack(shape->outline))) return true;
476 //Case B: Normale Shape RLE Drawing
477 if ((shape->rle = rleRender(shape->outline, shape->bbox, clip, antiAlias))) return true;
483 void shapeDelOutline(SwShape* shape)
485 auto outline = shape->outline;
486 _delOutline(outline);
487 shape->outline = nullptr;
491 void shapeReset(SwShape* shape)
493 shapeDelOutline(shape);
495 shape->rle = nullptr;
497 _initBBox(shape->bbox);
501 bool shapeGenOutline(SwShape* shape, const Shape* sdata, const Matrix* transform)
503 const PathCommand* cmds = nullptr;
504 auto cmdCnt = sdata->pathCommands(&cmds);
506 const Point* pts = nullptr;
507 auto ptsCnt = sdata->pathCoords(&pts);
509 //No actual shape data
510 if (cmdCnt == 0 || ptsCnt == 0) return false;
513 auto outlinePtsCnt = 0;
514 auto outlineCntrsCnt = 0;
516 for (uint32_t i = 0; i < cmdCnt; ++i) {
517 switch(*(cmds + i)) {
518 case PathCommand::Close: {
522 case PathCommand::MoveTo: {
527 case PathCommand::LineTo: {
531 case PathCommand::CubicTo: {
538 ++outlinePtsCnt; //for close
539 ++outlineCntrsCnt; //for end
541 auto outline = shape->outline;
542 if (!outline) outline = static_cast<SwOutline*>(calloc(1, sizeof(SwOutline)));
543 outline->opened = true;
545 _growOutlinePoint(*outline, outlinePtsCnt);
546 _growOutlineContour(*outline, outlineCntrsCnt);
551 while (cmdCnt-- > 0) {
553 case PathCommand::Close: {
554 _outlineClose(*outline);
558 case PathCommand::MoveTo: {
559 _outlineMoveTo(*outline, pts, transform);
563 case PathCommand::LineTo: {
564 _outlineLineTo(*outline, pts, transform);
568 case PathCommand::CubicTo: {
569 _outlineCubicTo(*outline, pts, pts + 1, pts + 2, transform);
577 _outlineEnd(*outline);
579 if (closed) outline->opened = false;
581 outline->fillRule = sdata->fillRule();
582 shape->outline = outline;
588 void shapeFree(SwShape* shape)
590 shapeDelOutline(shape);
595 rleFree(shape->strokeRle);
596 strokeFree(shape->stroke);
601 void shapeDelStroke(SwShape* shape)
603 if (!shape->stroke) return;
604 rleFree(shape->strokeRle);
605 shape->strokeRle = nullptr;
606 strokeFree(shape->stroke);
607 shape->stroke = nullptr;
611 void shapeResetStroke(SwShape* shape, const Shape* sdata, const Matrix* transform)
613 if (!shape->stroke) shape->stroke = static_cast<SwStroke*>(calloc(1, sizeof(SwStroke)));
614 auto stroke = shape->stroke;
617 strokeReset(stroke, sdata, transform);
619 rleFree(shape->strokeRle);
620 shape->strokeRle = nullptr;
624 bool shapeGenStrokeRle(SwShape* shape, const Shape* sdata, const Matrix* transform, const SwSize& clip)
626 SwOutline* shapeOutline = nullptr;
627 SwOutline* strokeOutline = nullptr;
628 bool freeOutline = false;
632 if (sdata->strokeDash(nullptr) > 0) {
633 shapeOutline = _genDashOutline(sdata, transform);
634 if (!shapeOutline) return false;
636 //Normal Style stroke
638 if (!shape->outline) {
639 if (!shapeGenOutline(shape, sdata, transform)) return false;
641 shapeOutline = shape->outline;
644 if (!strokeParseOutline(shape->stroke, *shapeOutline)) {
649 strokeOutline = strokeExportOutline(shape->stroke);
650 if (!strokeOutline) {
656 _updateBBox(strokeOutline, bbox);
658 if (!_checkValid(strokeOutline, bbox, clip)) {
663 shape->strokeRle = rleRender(strokeOutline, bbox, clip, true);
666 if (freeOutline) _delOutline(shapeOutline);
667 _delOutline(strokeOutline);
673 bool shapeGenFillColors(SwShape* shape, const Fill* fill, const Matrix* transform, SwSurface* surface, bool ctable)
675 return fillGenColorTable(shape->fill, fill, transform, surface, ctable);
679 void shapeResetFill(SwShape* shape)
682 shape->fill = static_cast<SwFill*>(calloc(1, sizeof(SwFill)));
683 if (!shape->fill) return;
685 fillReset(shape->fill);
689 void shapeDelFill(SwShape* shape)
691 if (!shape->fill) return;
692 fillFree(shape->fill);
693 shape->fill = nullptr;