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);
116 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
118 if (outline.ptsCnt > 0) {
119 _growOutlineContour(outline, 1);
120 outline.cntrs[outline.cntrsCnt] = outline.ptsCnt - 1;
128 static void _outlineLineTo(SwOutline& outline, const Point* to, const Matrix* transform)
130 _growOutlinePoint(outline, 1);
132 outline.pts[outline.ptsCnt] = _transform(to, transform);
133 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
138 static void _outlineCubicTo(SwOutline& outline, const Point* ctrl1, const Point* ctrl2, const Point* to, const Matrix* transform)
140 _growOutlinePoint(outline, 3);
142 outline.pts[outline.ptsCnt] = _transform(ctrl1, transform);
143 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_CUBIC;
146 outline.pts[outline.ptsCnt] = _transform(ctrl2, transform);
147 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_CUBIC;
150 outline.pts[outline.ptsCnt] = _transform(to, transform);
151 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
156 static void _outlineClose(SwOutline& outline)
160 if (outline.cntrsCnt > 0) {
161 i = outline.cntrs[outline.cntrsCnt - 1] + 1;
166 //Make sure there is at least one point in the current path
167 if (outline.ptsCnt == i) {
168 outline.opened = true;
173 _growOutlinePoint(outline, 1);
175 outline.pts[outline.ptsCnt] = outline.pts[i];
176 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
179 outline.opened = false;
183 static void _initBBox(SwBBox& bbox)
185 bbox.min.x = bbox.min.y = 0;
186 bbox.max.x = bbox.max.y = 0;
190 static bool _updateBBox(SwOutline* outline, SwBBox& bbox)
192 if (!outline) return false;
194 auto pt = outline->pts;
196 if (outline->ptsCnt <= 0) {
208 for(uint32_t i = 1; i < outline->ptsCnt; ++i, ++pt) {
209 if (xMin > pt->x) xMin = pt->x;
210 if (xMax < pt->x) xMax = pt->x;
211 if (yMin > pt->y) yMin = pt->y;
212 if (yMax < pt->y) yMax = pt->y;
214 bbox.min.x = xMin >> 6;
215 bbox.max.x = (xMax + 63) >> 6;
216 bbox.min.y = yMin >> 6;
217 bbox.max.y = (yMax + 63) >> 6;
219 if (xMax - xMin < 1 && yMax - yMin < 1) return false;
225 static bool _checkValid(const SwOutline* outline, const SwBBox& bbox, const SwSize& clip)
227 if (outline->ptsCnt == 0 || outline->cntrsCnt <= 0) return false;
230 if (bbox.min.x >= clip.w || bbox.min.y >= clip.h || bbox.max.x <= 0 || bbox.max.y <= 0) return false;
236 static void _dashLineTo(SwDashStroke& dash, const Point* to, const Matrix* transform)
238 _growOutlinePoint(*dash.outline, dash.outline->ptsCnt >> 1);
239 _growOutlineContour(*dash.outline, dash.outline->cntrsCnt >> 1);
241 Line cur = {dash.ptCur, *to};
242 auto len = _lineLength(cur.pt1, cur.pt2);
244 if (len < dash.curLen) {
246 if (!dash.curOpGap) {
247 _outlineMoveTo(*dash.outline, &dash.ptCur, transform);
248 _outlineLineTo(*dash.outline, to, transform);
251 while (len > dash.curLen) {
254 _lineSplitAt(cur, dash.curLen, left, right);;
255 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
256 if (!dash.curOpGap) {
257 _outlineMoveTo(*dash.outline, &left.pt1, transform);
258 _outlineLineTo(*dash.outline, &left.pt2, transform);
260 dash.curLen = dash.pattern[dash.curIdx];
261 dash.curOpGap = !dash.curOpGap;
263 dash.ptCur = cur.pt1;
267 if (!dash.curOpGap) {
268 _outlineMoveTo(*dash.outline, &cur.pt1, transform);
269 _outlineLineTo(*dash.outline, &cur.pt2, transform);
271 if (dash.curLen < 1 && TO_SWCOORD(len) > 1) {
273 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
274 dash.curLen = dash.pattern[dash.curIdx];
275 dash.curOpGap = !dash.curOpGap;
282 static void _dashCubicTo(SwDashStroke& dash, const Point* ctrl1, const Point* ctrl2, const Point* to, const Matrix* transform)
284 _growOutlinePoint(*dash.outline, dash.outline->ptsCnt >> 1);
285 _growOutlineContour(*dash.outline, dash.outline->cntrsCnt >> 1);
287 Bezier cur = {dash.ptCur, *ctrl1, *ctrl2, *to};
288 auto len = bezLength(cur);
290 if (len < dash.curLen) {
292 if (!dash.curOpGap) {
293 _outlineMoveTo(*dash.outline, &dash.ptCur, transform);
294 _outlineCubicTo(*dash.outline, ctrl1, ctrl2, to, transform);
297 while (len > dash.curLen) {
300 bezSplitAt(cur, dash.curLen, left, right);
301 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
302 if (!dash.curOpGap) {
303 _outlineMoveTo(*dash.outline, &left.start, transform);
304 _outlineCubicTo(*dash.outline, &left.ctrl1, &left.ctrl2, &left.end, transform);
306 dash.curLen = dash.pattern[dash.curIdx];
307 dash.curOpGap = !dash.curOpGap;
309 dash.ptCur = right.start;
313 if (!dash.curOpGap) {
314 _outlineMoveTo(*dash.outline, &cur.start, transform);
315 _outlineCubicTo(*dash.outline, &cur.ctrl1, &cur.ctrl2, &cur.end, transform);
317 if (dash.curLen < 1 && TO_SWCOORD(len) > 1) {
319 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
320 dash.curLen = dash.pattern[dash.curIdx];
321 dash.curOpGap = !dash.curOpGap;
328 SwOutline* _genDashOutline(const Shape* sdata, const Matrix* transform)
330 const PathCommand* cmds = nullptr;
331 auto cmdCnt = sdata->pathCommands(&cmds);
333 const Point* pts = nullptr;
334 auto ptsCnt = sdata->pathCoords(&pts);
336 //No actual shape data
337 if (cmdCnt == 0 || ptsCnt == 0) return nullptr;
342 dash.ptStart = {0, 0};
344 dash.curOpGap = false;
346 const float* pattern;
347 dash.cnt = sdata->strokeDash(&pattern);
348 if (dash.cnt == 0) return nullptr;
350 //Is it safe to mutual exclusive?
351 dash.pattern = const_cast<float*>(pattern);
352 dash.outline = static_cast<SwOutline*>(calloc(1, sizeof(SwOutline)));
353 dash.outline->opened = true;
356 auto outlinePtsCnt = 0;
357 auto outlineCntrsCnt = 0;
359 for (uint32_t i = 0; i < cmdCnt; ++i) {
360 switch(*(cmds + i)) {
361 case PathCommand::Close: {
365 case PathCommand::MoveTo: {
370 case PathCommand::LineTo: {
374 case PathCommand::CubicTo: {
381 ++outlinePtsCnt; //for close
382 ++outlineCntrsCnt; //for end
384 //Reserve Approximitely 20x...
385 _growOutlinePoint(*dash.outline, outlinePtsCnt * 20);
386 _growOutlineContour(*dash.outline, outlineCntrsCnt * 20);
388 while (cmdCnt-- > 0) {
390 case PathCommand::Close: {
391 _dashLineTo(dash, &dash.ptStart, transform);
394 case PathCommand::MoveTo: {
397 dash.curLen = *dash.pattern;
398 dash.curOpGap = false;
399 dash.ptStart = dash.ptCur = *pts;
403 case PathCommand::LineTo: {
404 _dashLineTo(dash, pts, transform);
408 case PathCommand::CubicTo: {
409 _dashCubicTo(dash, pts, pts + 1, pts + 2, transform);
417 _outlineEnd(*dash.outline);
423 bool _fastTrack(const SwOutline* outline)
425 //Fast Track: Othogonal rectangle?
426 if (outline->ptsCnt != 5) return false;
428 auto pt1 = outline->pts + 0;
429 auto pt2 = outline->pts + 1;
430 auto pt3 = outline->pts + 2;
431 auto pt4 = outline->pts + 3;
433 auto a = SwPoint{pt1->x, pt3->y};
434 auto b = SwPoint{pt3->x, pt1->y};
436 if ((*pt2 == a && *pt4 == b) || (*pt2 == b && *pt4 == a)) return true;
442 /************************************************************************/
443 /* External Class Implementation */
444 /************************************************************************/
446 bool shapePrepare(SwShape* shape, const Shape* sdata, const SwSize& clip, const Matrix* transform)
448 if (!shapeGenOutline(shape, sdata, transform)) return false;
450 if (!_updateBBox(shape->outline, shape->bbox)) return false;
452 if (!_checkValid(shape->outline, shape->bbox, clip)) return false;
458 bool shapePrepared(SwShape* shape)
460 return shape->rle ? true : false;
464 bool shapeGenRle(SwShape* shape, TVG_UNUSED const Shape* sdata, const SwSize& clip, bool antiAlias, bool hasComposite)
466 //FIXME: Should we draw it?
468 //if (shape.outline->opened) return true;
470 //Case A: Fast Track Rectangle Drawing
471 if (!hasComposite && (shape->rect = _fastTrack(shape->outline))) return true;
472 //Case B: Normale Shape RLE Drawing
473 if ((shape->rle = rleRender(shape->outline, shape->bbox, clip, antiAlias))) return true;
479 void shapeDelOutline(SwShape* shape)
481 auto outline = shape->outline;
482 _delOutline(outline);
483 shape->outline = nullptr;
487 void shapeReset(SwShape* shape)
489 shapeDelOutline(shape);
491 shape->rle = nullptr;
493 _initBBox(shape->bbox);
497 bool shapeGenOutline(SwShape* shape, const Shape* sdata, const Matrix* transform)
499 const PathCommand* cmds = nullptr;
500 auto cmdCnt = sdata->pathCommands(&cmds);
502 const Point* pts = nullptr;
503 auto ptsCnt = sdata->pathCoords(&pts);
505 //No actual shape data
506 if (cmdCnt == 0 || ptsCnt == 0) return false;
509 auto outlinePtsCnt = 0;
510 auto outlineCntrsCnt = 0;
512 for (uint32_t i = 0; i < cmdCnt; ++i) {
513 switch(*(cmds + i)) {
514 case PathCommand::Close: {
518 case PathCommand::MoveTo: {
523 case PathCommand::LineTo: {
527 case PathCommand::CubicTo: {
534 ++outlinePtsCnt; //for close
535 ++outlineCntrsCnt; //for end
537 auto outline = shape->outline;
538 if (!outline) outline = static_cast<SwOutline*>(calloc(1, sizeof(SwOutline)));
539 outline->opened = true;
541 _growOutlinePoint(*outline, outlinePtsCnt);
542 _growOutlineContour(*outline, outlineCntrsCnt);
547 while (cmdCnt-- > 0) {
549 case PathCommand::Close: {
550 _outlineClose(*outline);
554 case PathCommand::MoveTo: {
555 _outlineMoveTo(*outline, pts, transform);
559 case PathCommand::LineTo: {
560 _outlineLineTo(*outline, pts, transform);
564 case PathCommand::CubicTo: {
565 _outlineCubicTo(*outline, pts, pts + 1, pts + 2, transform);
573 _outlineEnd(*outline);
575 if (closed) outline->opened = false;
577 outline->fillRule = sdata->fillRule();
578 shape->outline = outline;
584 void shapeFree(SwShape* shape)
586 shapeDelOutline(shape);
591 rleFree(shape->strokeRle);
592 strokeFree(shape->stroke);
597 void shapeDelStroke(SwShape* shape)
599 if (!shape->stroke) return;
600 rleFree(shape->strokeRle);
601 shape->strokeRle = nullptr;
602 strokeFree(shape->stroke);
603 shape->stroke = nullptr;
607 void shapeResetStroke(SwShape* shape, const Shape* sdata, const Matrix* transform)
609 if (!shape->stroke) shape->stroke = static_cast<SwStroke*>(calloc(1, sizeof(SwStroke)));
610 auto stroke = shape->stroke;
613 strokeReset(stroke, sdata, transform);
615 rleFree(shape->strokeRle);
616 shape->strokeRle = nullptr;
620 bool shapeGenStrokeRle(SwShape* shape, const Shape* sdata, const Matrix* transform, const SwSize& clip)
622 SwOutline* shapeOutline = nullptr;
623 SwOutline* strokeOutline = nullptr;
624 bool freeOutline = false;
628 if (sdata->strokeDash(nullptr) > 0) {
629 shapeOutline = _genDashOutline(sdata, transform);
630 if (!shapeOutline) return false;
632 //Normal Style stroke
634 if (!shape->outline) {
635 if (!shapeGenOutline(shape, sdata, transform)) return false;
637 shapeOutline = shape->outline;
640 if (!strokeParseOutline(shape->stroke, *shapeOutline)) {
645 strokeOutline = strokeExportOutline(shape->stroke);
646 if (!strokeOutline) {
652 _updateBBox(strokeOutline, bbox);
654 if (!_checkValid(strokeOutline, bbox, clip)) {
659 shape->strokeRle = rleRender(strokeOutline, bbox, clip, true);
662 if (freeOutline) _delOutline(shapeOutline);
663 _delOutline(strokeOutline);
669 bool shapeGenFillColors(SwShape* shape, const Fill* fill, const Matrix* transform, SwSurface* surface, bool ctable)
671 return fillGenColorTable(shape->fill, fill, transform, surface, ctable);
675 void shapeResetFill(SwShape* shape)
678 shape->fill = static_cast<SwFill*>(calloc(1, sizeof(SwFill)));
679 if (!shape->fill) return;
681 fillReset(shape->fill);
685 void shapeDelFill(SwShape* shape)
687 if (!shape->fill) return;
688 fillFree(shape->fill);
689 shape->fill = nullptr;