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
22 #include "tvgSwCommon.h"
24 /************************************************************************/
25 /* Internal Class Implementation */
26 /************************************************************************/
35 static SwPoint _transform(const Point* to, const Matrix* transform)
37 if (!transform) return {TO_SWCOORD(to->x), TO_SWCOORD(to->y)};
39 auto tx = round(to->x * transform->e11 + to->y * transform->e12 + transform->e13);
40 auto ty = round(to->x * transform->e21 + to->y * transform->e22 + transform->e23);
42 return {TO_SWCOORD(tx), TO_SWCOORD(ty)};
46 static float _lineLength(const Point& pt1, const Point& pt2)
48 /* approximate sqrt(x*x + y*y) using alpha max plus beta min algorithm.
49 With alpha = 1, beta = 3/8, giving results with the largest error less
50 than 7% compared to the exact value. */
51 Point diff = {pt2.x - pt1.x, pt2.y - pt1.y};
52 if (diff.x < 0) diff.x = -diff.x;
53 if (diff.y < 0) diff.y = -diff.y;
54 return (diff.x > diff.y) ? (diff.x + diff.y * 0.375f) : (diff.y + diff.x * 0.375f);
58 static void _lineSplitAt(const Line& cur, float at, Line& left, Line& right)
60 auto len = _lineLength(cur.pt1, cur.pt2);
61 auto dx = ((cur.pt2.x - cur.pt1.x) / len) * at;
62 auto dy = ((cur.pt2.y - cur.pt1.y) / len) * at;
64 left.pt2.x = left.pt1.x + dx;
65 left.pt2.y = left.pt1.y + dy;
71 static void _growOutlineContour(SwOutline& outline, uint32_t n)
73 if (outline.reservedCntrsCnt >= outline.cntrsCnt + n) return;
74 outline.reservedCntrsCnt = outline.cntrsCnt + n;
75 outline.cntrs = static_cast<uint32_t*>(realloc(outline.cntrs, outline.reservedCntrsCnt * sizeof(uint32_t)));
79 static void _growOutlinePoint(SwOutline& outline, uint32_t n)
81 if (outline.reservedPtsCnt >= outline.ptsCnt + n) return;
82 outline.reservedPtsCnt = outline.ptsCnt + n;
83 outline.pts = static_cast<SwPoint*>(realloc(outline.pts, outline.reservedPtsCnt * sizeof(SwPoint)));
84 outline.types = static_cast<uint8_t*>(realloc(outline.types, outline.reservedPtsCnt * sizeof(uint8_t)));
88 static void _delOutline(SwOutline* outline)
92 if (outline->cntrs) free(outline->cntrs);
93 if (outline->pts) free(outline->pts);
94 if (outline->types) free(outline->types);
99 static void _outlineEnd(SwOutline& outline)
101 _growOutlineContour(outline, 1);
102 if (outline.ptsCnt > 0) {
103 outline.cntrs[outline.cntrsCnt] = outline.ptsCnt - 1;
109 static void _outlineMoveTo(SwOutline& outline, const Point* to, const Matrix* transform)
111 _growOutlinePoint(outline, 1);
113 outline.pts[outline.ptsCnt] = _transform(to, transform);
115 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
117 if (outline.ptsCnt > 0) {
118 _growOutlineContour(outline, 1);
119 outline.cntrs[outline.cntrsCnt] = outline.ptsCnt - 1;
127 static void _outlineLineTo(SwOutline& outline, const Point* to, const Matrix* transform)
129 _growOutlinePoint(outline, 1);
131 outline.pts[outline.ptsCnt] = _transform(to, transform);
132 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
137 static void _outlineCubicTo(SwOutline& outline, const Point* ctrl1, const Point* ctrl2, const Point* to, const Matrix* transform)
139 _growOutlinePoint(outline, 3);
141 outline.pts[outline.ptsCnt] = _transform(ctrl1, transform);
142 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_CUBIC;
145 outline.pts[outline.ptsCnt] = _transform(ctrl2, transform);
146 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_CUBIC;
149 outline.pts[outline.ptsCnt] = _transform(to, transform);
150 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
155 static void _outlineClose(SwOutline& outline)
159 if (outline.cntrsCnt > 0) {
160 i = outline.cntrs[outline.cntrsCnt - 1] + 1;
165 //Make sure there is at least one point in the current path
166 if (outline.ptsCnt == i) {
167 outline.opened = true;
172 _growOutlinePoint(outline, 1);
174 outline.pts[outline.ptsCnt] = outline.pts[i];
175 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
178 outline.opened = false;
182 static void _initBBox(SwBBox& bbox)
184 bbox.min.x = bbox.min.y = 0;
185 bbox.max.x = bbox.max.y = 0;
189 static bool _updateBBox(SwOutline* outline, SwBBox& bbox)
191 if (!outline) return false;
193 auto pt = outline->pts;
195 if (outline->ptsCnt <= 0) {
207 for(uint32_t i = 1; i < outline->ptsCnt; ++i, ++pt) {
208 if (xMin > pt->x) xMin = pt->x;
209 if (xMax < pt->x) xMax = pt->x;
210 if (yMin > pt->y) yMin = pt->y;
211 if (yMax < pt->y) yMax = pt->y;
213 bbox.min.x = xMin >> 6;
214 bbox.max.x = (xMax + 63) >> 6;
215 bbox.min.y = yMin >> 6;
216 bbox.max.y = (yMax + 63) >> 6;
218 if (xMax - xMin < 1 && yMax - yMin < 1) return false;
224 static bool _checkValid(const SwOutline* outline, const SwBBox& bbox, const SwSize& clip)
226 if (outline->ptsCnt == 0 || outline->cntrsCnt <= 0) return false;
229 if (bbox.min.x >= clip.w || bbox.min.y >= clip.h || bbox.max.x <= 0 || bbox.max.y <= 0) return false;
235 static void _dashLineTo(SwDashStroke& dash, const Point* to, const Matrix* transform)
237 _growOutlinePoint(*dash.outline, dash.outline->ptsCnt >> 1);
238 _growOutlineContour(*dash.outline, dash.outline->cntrsCnt >> 1);
240 Line cur = {dash.ptCur, *to};
241 auto len = _lineLength(cur.pt1, cur.pt2);
243 if (len < dash.curLen) {
245 if (!dash.curOpGap) {
246 _outlineMoveTo(*dash.outline, &dash.ptCur, transform);
247 _outlineLineTo(*dash.outline, to, transform);
250 while (len > dash.curLen) {
253 _lineSplitAt(cur, dash.curLen, left, right);;
254 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
255 if (!dash.curOpGap) {
256 _outlineMoveTo(*dash.outline, &left.pt1, transform);
257 _outlineLineTo(*dash.outline, &left.pt2, transform);
259 dash.curLen = dash.pattern[dash.curIdx];
260 dash.curOpGap = !dash.curOpGap;
262 dash.ptCur = cur.pt1;
266 if (!dash.curOpGap) {
267 _outlineMoveTo(*dash.outline, &cur.pt1, transform);
268 _outlineLineTo(*dash.outline, &cur.pt2, transform);
270 if (dash.curLen < 1) {
272 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
273 dash.curLen = dash.pattern[dash.curIdx];
274 dash.curOpGap = !dash.curOpGap;
281 static void _dashCubicTo(SwDashStroke& dash, const Point* ctrl1, const Point* ctrl2, const Point* to, const Matrix* transform)
283 _growOutlinePoint(*dash.outline, dash.outline->ptsCnt >> 1);
284 _growOutlineContour(*dash.outline, dash.outline->cntrsCnt >> 1);
286 Bezier cur = { dash.ptCur, *ctrl1, *ctrl2, *to};
287 auto len = bezLength(cur);
289 if (len < dash.curLen) {
291 if (!dash.curOpGap) {
292 _outlineMoveTo(*dash.outline, &dash.ptCur, transform);
293 _outlineCubicTo(*dash.outline, ctrl1, ctrl2, to, transform);
296 while (len > dash.curLen) {
299 bezSplitAt(cur, dash.curLen, left, right);
300 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
301 if (!dash.curOpGap) {
302 _outlineMoveTo(*dash.outline, &left.start, transform);
303 _outlineCubicTo(*dash.outline, &left.ctrl1, &left.ctrl2, &left.end, transform);
305 dash.curLen = dash.pattern[dash.curIdx];
306 dash.curOpGap = !dash.curOpGap;
308 dash.ptCur = right.start;
312 if (!dash.curOpGap) {
313 _outlineMoveTo(*dash.outline, &cur.start, transform);
314 _outlineCubicTo(*dash.outline, &cur.ctrl1, &cur.ctrl2, &cur.end, transform);
316 if (dash.curLen < 1) {
318 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
319 dash.curLen = dash.pattern[dash.curIdx];
320 dash.curOpGap = !dash.curOpGap;
327 SwOutline* _genDashOutline(const Shape* sdata, const Matrix* transform)
329 const PathCommand* cmds = nullptr;
330 auto cmdCnt = sdata->pathCommands(&cmds);
332 const Point* pts = nullptr;
333 auto ptsCnt = sdata->pathCoords(&pts);
335 //No actual shape data
336 if (cmdCnt == 0 || ptsCnt == 0) return nullptr;
341 dash.ptStart = {0, 0};
343 dash.curOpGap = false;
345 const float* pattern;
346 dash.cnt = sdata->strokeDash(&pattern);
347 if (dash.cnt == 0) return nullptr;
349 //Is it safe to mutual exclusive?
350 dash.pattern = const_cast<float*>(pattern);
351 dash.outline = static_cast<SwOutline*>(calloc(1, sizeof(SwOutline)));
352 dash.outline->opened = true;
355 auto outlinePtsCnt = 0;
356 auto outlineCntrsCnt = 0;
358 for (uint32_t i = 0; i < cmdCnt; ++i) {
359 switch(*(cmds + i)) {
360 case PathCommand::Close: {
364 case PathCommand::MoveTo: {
369 case PathCommand::LineTo: {
373 case PathCommand::CubicTo: {
380 ++outlinePtsCnt; //for close
381 ++outlineCntrsCnt; //for end
383 //Reserve Approximitely 20x...
384 _growOutlinePoint(*dash.outline, outlinePtsCnt * 20);
385 _growOutlineContour(*dash.outline, outlineCntrsCnt * 20);
387 while (cmdCnt-- > 0) {
389 case PathCommand::Close: {
390 _dashLineTo(dash, &dash.ptStart, transform);
393 case PathCommand::MoveTo: {
396 dash.curLen = *dash.pattern;
397 dash.curOpGap = false;
398 dash.ptStart = dash.ptCur = *pts;
402 case PathCommand::LineTo: {
403 _dashLineTo(dash, pts, transform);
407 case PathCommand::CubicTo: {
408 _dashCubicTo(dash, pts, pts + 1, pts + 2, transform);
416 _outlineEnd(*dash.outline);
422 bool _fastTrack(const SwOutline* outline)
424 //Fast Track: Othogonal rectangle?
425 if (outline->ptsCnt != 5) return false;
427 auto pt1 = outline->pts + 0;
428 auto pt2 = outline->pts + 1;
429 auto pt3 = outline->pts + 2;
430 auto pt4 = outline->pts + 3;
432 auto min1 = pt1->y < pt3->y ? pt1 : pt3;
433 auto min2 = pt2->y < pt4->y ? pt2 : pt4;
434 if (min1->y != min2->y) return false;
436 SwCoord len1 = pow(pt1->x - pt3->x, 2) + pow(pt1->y - pt3->y, 2);
437 SwCoord len2 = pow(pt2->x - pt4->x, 2) + pow(pt2->y - pt4->y, 2);
438 if (len1 == len2) return true;
444 /************************************************************************/
445 /* External Class Implementation */
446 /************************************************************************/
448 bool shapePrepare(SwShape* shape, const Shape* sdata, const SwSize& clip, const Matrix* transform)
450 if (!shapeGenOutline(shape, sdata, transform)) return false;
452 if (!_updateBBox(shape->outline, shape->bbox)) return false;
454 if (!_checkValid(shape->outline, shape->bbox, clip)) return false;
460 bool shapeGenRle(SwShape* shape, TVG_UNUSED const Shape* sdata, const SwSize& clip, bool antiAlias)
462 //FIXME: Should we draw it?
464 //if (shape.outline->opened) return true;
466 //Case A: Fast Track Rectangle Drawing
467 if ((shape->rect = _fastTrack(shape->outline))) return true;
468 //Case B: Normale Shape RLE Drawing
469 if ((shape->rle = rleRender(shape->outline, shape->bbox, clip, antiAlias))) return true;
475 void shapeDelOutline(SwShape* shape)
477 auto outline = shape->outline;
478 _delOutline(outline);
479 shape->outline = nullptr;
483 void shapeReset(SwShape* shape)
485 shapeDelOutline(shape);
487 shape->rle = nullptr;
489 _initBBox(shape->bbox);
493 bool shapeGenOutline(SwShape* shape, const Shape* sdata, const Matrix* transform)
495 const PathCommand* cmds = nullptr;
496 auto cmdCnt = sdata->pathCommands(&cmds);
498 const Point* pts = nullptr;
499 auto ptsCnt = sdata->pathCoords(&pts);
501 //No actual shape data
502 if (cmdCnt == 0 || ptsCnt == 0) return false;
505 auto outlinePtsCnt = 0;
506 auto outlineCntrsCnt = 0;
508 for (uint32_t i = 0; i < cmdCnt; ++i) {
509 switch(*(cmds + i)) {
510 case PathCommand::Close: {
514 case PathCommand::MoveTo: {
519 case PathCommand::LineTo: {
523 case PathCommand::CubicTo: {
530 ++outlinePtsCnt; //for close
531 ++outlineCntrsCnt; //for end
533 auto outline = shape->outline;
534 if (!outline) outline = static_cast<SwOutline*>(calloc(1, sizeof(SwOutline)));
535 outline->opened = true;
537 _growOutlinePoint(*outline, outlinePtsCnt);
538 _growOutlineContour(*outline, outlineCntrsCnt);
543 while (cmdCnt-- > 0) {
545 case PathCommand::Close: {
546 _outlineClose(*outline);
550 case PathCommand::MoveTo: {
551 _outlineMoveTo(*outline, pts, transform);
555 case PathCommand::LineTo: {
556 _outlineLineTo(*outline, pts, transform);
560 case PathCommand::CubicTo: {
561 _outlineCubicTo(*outline, pts, pts + 1, pts + 2, transform);
569 _outlineEnd(*outline);
571 if (closed) outline->opened = false;
574 //outline->flags = SwOutline::FillRule::Winding;
576 shape->outline = outline;
582 void shapeFree(SwShape* shape)
584 shapeDelOutline(shape);
589 rleFree(shape->strokeRle);
590 strokeFree(shape->stroke);
595 void shapeDelStroke(SwShape* shape)
597 if (!shape->stroke) return;
598 rleFree(shape->strokeRle);
599 shape->strokeRle = nullptr;
600 strokeFree(shape->stroke);
601 shape->stroke = nullptr;
605 void shapeResetStroke(SwShape* shape, const Shape* sdata, const Matrix* transform)
607 if (!shape->stroke) shape->stroke = static_cast<SwStroke*>(calloc(1, sizeof(SwStroke)));
608 auto stroke = shape->stroke;
611 strokeReset(stroke, sdata, transform);
613 rleFree(shape->strokeRle);
614 shape->strokeRle = nullptr;
618 bool shapeGenStrokeRle(SwShape* shape, const Shape* sdata, const Matrix* transform, const SwSize& clip)
620 SwOutline* shapeOutline = nullptr;
623 if (sdata->strokeDash(nullptr) > 0) {
624 shapeOutline = _genDashOutline(sdata, transform);
625 if (!shapeOutline) return false;
626 //Normal Style stroke
628 if (!shape->outline) {
629 if (!shapeGenOutline(shape, sdata, transform)) return false;
631 shapeOutline = shape->outline;
634 if (!strokeParseOutline(shape->stroke, *shapeOutline)) return false;
636 auto strokeOutline = strokeExportOutline(shape->stroke);
637 if (!strokeOutline) return false;
640 _updateBBox(strokeOutline, bbox);
642 if (!_checkValid(strokeOutline, bbox, clip)) return false;
644 shape->strokeRle = rleRender(strokeOutline, bbox, clip, true);
646 _delOutline(strokeOutline);
652 bool shapeGenFillColors(SwShape* shape, const Fill* fill, const Matrix* transform, SwSurface* surface, bool ctable)
654 return fillGenColorTable(shape->fill, fill, transform, surface, ctable);
658 void shapeResetFill(SwShape* shape)
661 shape->fill = static_cast<SwFill*>(calloc(1, sizeof(SwFill)));
662 if (!shape->fill) return;
664 fillReset(shape->fill);
668 void shapeDelFill(SwShape* shape)
670 if (!shape->fill) return;
671 fillFree(shape->fill);
672 shape->fill = nullptr;