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 _outlineEnd(SwOutline& outline)
92 _growOutlineContour(outline, 1);
93 if (outline.ptsCnt > 0) {
94 outline.cntrs[outline.cntrsCnt] = outline.ptsCnt - 1;
100 static void _outlineMoveTo(SwOutline& outline, const Point* to, const Matrix* transform)
102 _growOutlinePoint(outline, 1);
104 outline.pts[outline.ptsCnt] = _transform(to, transform);
105 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
107 if (outline.ptsCnt > 0) {
108 _growOutlineContour(outline, 1);
109 outline.cntrs[outline.cntrsCnt] = outline.ptsCnt - 1;
117 static void _outlineLineTo(SwOutline& outline, const Point* to, const Matrix* transform)
119 _growOutlinePoint(outline, 1);
121 outline.pts[outline.ptsCnt] = _transform(to, transform);
122 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
127 static void _outlineCubicTo(SwOutline& outline, const Point* ctrl1, const Point* ctrl2, const Point* to, const Matrix* transform)
129 _growOutlinePoint(outline, 3);
131 outline.pts[outline.ptsCnt] = _transform(ctrl1, transform);
132 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_CUBIC;
135 outline.pts[outline.ptsCnt] = _transform(ctrl2, transform);
136 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_CUBIC;
139 outline.pts[outline.ptsCnt] = _transform(to, transform);
140 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
145 static void _outlineClose(SwOutline& outline)
149 if (outline.cntrsCnt > 0) {
150 i = outline.cntrs[outline.cntrsCnt - 1] + 1;
155 //Make sure there is at least one point in the current path
156 if (outline.ptsCnt == i) {
157 outline.opened = true;
162 _growOutlinePoint(outline, 1);
164 outline.pts[outline.ptsCnt] = outline.pts[i];
165 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
168 outline.opened = false;
172 static void _initBBox(SwBBox& bbox)
174 bbox.min.x = bbox.min.y = 0;
175 bbox.max.x = bbox.max.y = 0;
179 static bool _updateBBox(SwOutline* outline, SwBBox& bbox)
181 if (!outline) return false;
183 auto pt = outline->pts;
185 if (outline->ptsCnt <= 0) {
197 for(uint32_t i = 1; i < outline->ptsCnt; ++i, ++pt) {
198 if (xMin > pt->x) xMin = pt->x;
199 if (xMax < pt->x) xMax = pt->x;
200 if (yMin > pt->y) yMin = pt->y;
201 if (yMax < pt->y) yMax = pt->y;
203 bbox.min.x = xMin >> 6;
204 bbox.max.x = (xMax + 63) >> 6;
205 bbox.min.y = yMin >> 6;
206 bbox.max.y = (yMax + 63) >> 6;
208 if (xMax - xMin < 1 && yMax - yMin < 1) return false;
214 static bool _checkValid(const SwOutline* outline, const SwBBox& bbox, const SwSize& clip)
216 if (outline->ptsCnt == 0 || outline->cntrsCnt <= 0) return false;
219 if (bbox.min.x >= clip.w || bbox.min.y >= clip.h || bbox.max.x <= 0 || bbox.max.y <= 0) return false;
225 static void _dashLineTo(SwDashStroke& dash, const Point* to, const Matrix* transform)
227 _growOutlinePoint(*dash.outline, dash.outline->ptsCnt >> 1);
228 _growOutlineContour(*dash.outline, dash.outline->cntrsCnt >> 1);
230 Line cur = {dash.ptCur, *to};
231 auto len = _lineLength(cur.pt1, cur.pt2);
233 if (len < dash.curLen) {
235 if (!dash.curOpGap) {
236 _outlineMoveTo(*dash.outline, &dash.ptCur, transform);
237 _outlineLineTo(*dash.outline, to, transform);
240 while (len > dash.curLen) {
243 _lineSplitAt(cur, dash.curLen, left, right);;
244 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
245 if (!dash.curOpGap) {
246 _outlineMoveTo(*dash.outline, &left.pt1, transform);
247 _outlineLineTo(*dash.outline, &left.pt2, transform);
249 dash.curLen = dash.pattern[dash.curIdx];
250 dash.curOpGap = !dash.curOpGap;
252 dash.ptCur = cur.pt1;
256 if (!dash.curOpGap) {
257 _outlineMoveTo(*dash.outline, &cur.pt1, transform);
258 _outlineLineTo(*dash.outline, &cur.pt2, transform);
260 if (dash.curLen < 1 && TO_SWCOORD(len) > 1) {
262 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
263 dash.curLen = dash.pattern[dash.curIdx];
264 dash.curOpGap = !dash.curOpGap;
271 static void _dashCubicTo(SwDashStroke& dash, const Point* ctrl1, const Point* ctrl2, const Point* to, const Matrix* transform)
273 _growOutlinePoint(*dash.outline, dash.outline->ptsCnt >> 1);
274 _growOutlineContour(*dash.outline, dash.outline->cntrsCnt >> 1);
276 Bezier cur = {dash.ptCur, *ctrl1, *ctrl2, *to};
277 auto len = bezLength(cur);
279 if (len < dash.curLen) {
281 if (!dash.curOpGap) {
282 _outlineMoveTo(*dash.outline, &dash.ptCur, transform);
283 _outlineCubicTo(*dash.outline, ctrl1, ctrl2, to, transform);
286 while (len > dash.curLen) {
289 bezSplitAt(cur, dash.curLen, left, right);
290 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
291 if (!dash.curOpGap) {
292 _outlineMoveTo(*dash.outline, &left.start, transform);
293 _outlineCubicTo(*dash.outline, &left.ctrl1, &left.ctrl2, &left.end, transform);
295 dash.curLen = dash.pattern[dash.curIdx];
296 dash.curOpGap = !dash.curOpGap;
298 dash.ptCur = right.start;
302 if (!dash.curOpGap) {
303 _outlineMoveTo(*dash.outline, &cur.start, transform);
304 _outlineCubicTo(*dash.outline, &cur.ctrl1, &cur.ctrl2, &cur.end, transform);
306 if (dash.curLen < 1 && TO_SWCOORD(len) > 1) {
308 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
309 dash.curLen = dash.pattern[dash.curIdx];
310 dash.curOpGap = !dash.curOpGap;
317 SwOutline* _genDashOutline(const Shape* sdata, const Matrix* transform)
319 const PathCommand* cmds = nullptr;
320 auto cmdCnt = sdata->pathCommands(&cmds);
322 const Point* pts = nullptr;
323 auto ptsCnt = sdata->pathCoords(&pts);
325 //No actual shape data
326 if (cmdCnt == 0 || ptsCnt == 0) return nullptr;
331 dash.ptStart = {0, 0};
333 dash.curOpGap = false;
335 const float* pattern;
336 dash.cnt = sdata->strokeDash(&pattern);
337 if (dash.cnt == 0) return nullptr;
339 //Is it safe to mutual exclusive?
340 dash.pattern = const_cast<float*>(pattern);
341 dash.outline = static_cast<SwOutline*>(calloc(1, sizeof(SwOutline)));
342 dash.outline->opened = true;
345 auto outlinePtsCnt = 0;
346 auto outlineCntrsCnt = 0;
348 for (uint32_t i = 0; i < cmdCnt; ++i) {
349 switch(*(cmds + i)) {
350 case PathCommand::Close: {
354 case PathCommand::MoveTo: {
359 case PathCommand::LineTo: {
363 case PathCommand::CubicTo: {
370 ++outlinePtsCnt; //for close
371 ++outlineCntrsCnt; //for end
373 //Reserve Approximitely 20x...
374 _growOutlinePoint(*dash.outline, outlinePtsCnt * 20);
375 _growOutlineContour(*dash.outline, outlineCntrsCnt * 20);
377 while (cmdCnt-- > 0) {
379 case PathCommand::Close: {
380 _dashLineTo(dash, &dash.ptStart, transform);
383 case PathCommand::MoveTo: {
386 dash.curLen = *dash.pattern;
387 dash.curOpGap = false;
388 dash.ptStart = dash.ptCur = *pts;
392 case PathCommand::LineTo: {
393 _dashLineTo(dash, pts, transform);
397 case PathCommand::CubicTo: {
398 _dashCubicTo(dash, pts, pts + 1, pts + 2, transform);
406 _outlineEnd(*dash.outline);
412 bool _fastTrack(const SwOutline* outline)
414 //Fast Track: Othogonal rectangle?
415 if (outline->ptsCnt != 5) return false;
417 auto pt1 = outline->pts + 0;
418 auto pt2 = outline->pts + 1;
419 auto pt3 = outline->pts + 2;
420 auto pt4 = outline->pts + 3;
422 auto a = SwPoint{pt1->x, pt3->y};
423 auto b = SwPoint{pt3->x, pt1->y};
425 if ((*pt2 == a && *pt4 == b) || (*pt2 == b && *pt4 == a)) return true;
431 /************************************************************************/
432 /* External Class Implementation */
433 /************************************************************************/
435 bool shapePrepare(SwShape* shape, const Shape* sdata, unsigned tid, const SwSize& clip, const Matrix* transform)
437 if (!shapeGenOutline(shape, sdata, tid, transform)) return false;
439 if (!_updateBBox(shape->outline, shape->bbox)) return false;
441 if (!_checkValid(shape->outline, shape->bbox, clip)) return false;
447 bool shapePrepared(SwShape* shape)
449 return shape->rle ? true : false;
453 bool shapeGenRle(SwShape* shape, TVG_UNUSED const Shape* sdata, const SwSize& clip, bool antiAlias, bool hasComposite)
455 //FIXME: Should we draw it?
457 //if (shape.outline->opened) return true;
459 //Case A: Fast Track Rectangle Drawing
460 if (!hasComposite && (shape->rect = _fastTrack(shape->outline))) return true;
461 //Case B: Normale Shape RLE Drawing
462 if ((shape->rle = rleRender(shape->outline, shape->bbox, clip, antiAlias))) return true;
468 void shapeDelOutline(SwShape* shape, uint32_t tid)
470 mpoolRetOutline(tid);
471 shape->outline = nullptr;
475 void shapeReset(SwShape* shape)
478 shape->rle = nullptr;
480 _initBBox(shape->bbox);
484 bool shapeGenOutline(SwShape* shape, const Shape* sdata, unsigned tid, const Matrix* transform)
486 const PathCommand* cmds = nullptr;
487 auto cmdCnt = sdata->pathCommands(&cmds);
489 const Point* pts = nullptr;
490 auto ptsCnt = sdata->pathCoords(&pts);
492 //No actual shape data
493 if (cmdCnt == 0 || ptsCnt == 0) return false;
496 auto outlinePtsCnt = 0;
497 auto outlineCntrsCnt = 0;
499 for (uint32_t i = 0; i < cmdCnt; ++i) {
500 switch(*(cmds + i)) {
501 case PathCommand::Close: {
505 case PathCommand::MoveTo: {
510 case PathCommand::LineTo: {
514 case PathCommand::CubicTo: {
521 ++outlinePtsCnt; //for close
522 ++outlineCntrsCnt; //for end
524 shape->outline = mpoolReqOutline(tid);
525 auto outline = shape->outline;
526 outline->opened = true;
528 _growOutlinePoint(*outline, outlinePtsCnt);
529 _growOutlineContour(*outline, outlineCntrsCnt);
534 while (cmdCnt-- > 0) {
536 case PathCommand::Close: {
537 _outlineClose(*outline);
541 case PathCommand::MoveTo: {
542 _outlineMoveTo(*outline, pts, transform);
546 case PathCommand::LineTo: {
547 _outlineLineTo(*outline, pts, transform);
551 case PathCommand::CubicTo: {
552 _outlineCubicTo(*outline, pts, pts + 1, pts + 2, transform);
560 _outlineEnd(*outline);
562 if (closed) outline->opened = false;
564 outline->fillRule = sdata->fillRule();
565 shape->outline = outline;
571 void shapeFree(SwShape* shape)
577 rleFree(shape->strokeRle);
578 strokeFree(shape->stroke);
583 void shapeDelStroke(SwShape* shape)
585 if (!shape->stroke) return;
586 rleFree(shape->strokeRle);
587 shape->strokeRle = nullptr;
588 strokeFree(shape->stroke);
589 shape->stroke = nullptr;
593 void shapeResetStroke(SwShape* shape, const Shape* sdata, const Matrix* transform)
595 if (!shape->stroke) shape->stroke = static_cast<SwStroke*>(calloc(1, sizeof(SwStroke)));
596 auto stroke = shape->stroke;
599 strokeReset(stroke, sdata, transform);
601 rleFree(shape->strokeRle);
602 shape->strokeRle = nullptr;
606 bool shapeGenStrokeRle(SwShape* shape, const Shape* sdata, unsigned tid, const Matrix* transform, const SwSize& clip)
608 SwOutline* shapeOutline = nullptr;
609 SwOutline* strokeOutline = nullptr;
610 bool freeOutline = false;
614 if (sdata->strokeDash(nullptr) > 0) {
615 shapeOutline = _genDashOutline(sdata, transform);
616 if (!shapeOutline) return false;
618 //Normal Style stroke
620 if (!shape->outline) {
621 if (!shapeGenOutline(shape, sdata, tid, transform)) return false;
623 shapeOutline = shape->outline;
626 if (!strokeParseOutline(shape->stroke, *shapeOutline)) {
631 strokeOutline = strokeExportOutline(shape->stroke, tid);
632 if (!strokeOutline) {
638 _updateBBox(strokeOutline, bbox);
640 if (!_checkValid(strokeOutline, bbox, clip)) {
645 shape->strokeRle = rleRender(strokeOutline, bbox, clip, true);
649 if (shapeOutline->cntrs) free(shapeOutline->cntrs);
650 if (shapeOutline->pts) free(shapeOutline->pts);
651 if (shapeOutline->types) free(shapeOutline->types);
653 mpoolRetStrokeOutline(tid);
659 bool shapeGenFillColors(SwShape* shape, const Fill* fill, const Matrix* transform, SwSurface* surface, bool ctable)
661 return fillGenColorTable(shape->fill, fill, transform, surface, 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 shapeDelFill(SwShape* shape)
677 if (!shape->fill) return;
678 fillFree(shape->fill);
679 shape->fill = nullptr;