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);
348 //Is it safe to mutual exclusive?
349 dash.pattern = const_cast<float*>(pattern);
350 dash.outline = static_cast<SwOutline*>(calloc(1, sizeof(SwOutline)));
351 dash.outline->opened = true;
354 auto outlinePtsCnt = 0;
355 auto outlineCntrsCnt = 0;
357 for (uint32_t i = 0; i < cmdCnt; ++i) {
358 switch(*(cmds + i)) {
359 case PathCommand::Close: {
363 case PathCommand::MoveTo: {
368 case PathCommand::LineTo: {
372 case PathCommand::CubicTo: {
379 ++outlinePtsCnt; //for close
380 ++outlineCntrsCnt; //for end
382 //Reserve Approximitely 20x...
383 _growOutlinePoint(*dash.outline, outlinePtsCnt * 20);
384 _growOutlineContour(*dash.outline, outlineCntrsCnt * 20);
386 while (cmdCnt-- > 0) {
388 case PathCommand::Close: {
389 _dashLineTo(dash, &dash.ptStart, transform);
392 case PathCommand::MoveTo: {
395 dash.curLen = *dash.pattern;
396 dash.curOpGap = false;
397 dash.ptStart = dash.ptCur = *pts;
401 case PathCommand::LineTo: {
402 _dashLineTo(dash, pts, transform);
406 case PathCommand::CubicTo: {
407 _dashCubicTo(dash, pts, pts + 1, pts + 2, transform);
415 _outlineEnd(*dash.outline);
421 bool _fastTrack(const SwOutline* outline)
423 //Fast Track: Othogonal rectangle?
424 if (outline->ptsCnt != 5) return false;
426 auto pt1 = outline->pts + 0;
427 auto pt2 = outline->pts + 1;
428 auto pt3 = outline->pts + 2;
429 auto pt4 = outline->pts + 3;
431 auto min1 = pt1->y < pt3->y ? pt1 : pt3;
432 auto min2 = pt2->y < pt4->y ? pt2 : pt4;
433 if (min1->y != min2->y) return false;
435 SwCoord len1 = pow(pt1->x - pt3->x, 2) + pow(pt1->y - pt3->y, 2);
436 SwCoord len2 = pow(pt2->x - pt4->x, 2) + pow(pt2->y - pt4->y, 2);
437 if (len1 == len2) return true;
443 /************************************************************************/
444 /* External Class Implementation */
445 /************************************************************************/
447 bool shapePrepare(SwShape* shape, const Shape* sdata, const SwSize& clip, const Matrix* transform)
449 if (!shapeGenOutline(shape, sdata, transform)) return false;
451 if (!_updateBBox(shape->outline, shape->bbox)) return false;
453 if (!_checkValid(shape->outline, shape->bbox, clip)) return false;
459 bool shapeGenRle(SwShape* shape, TVG_UNUSED const Shape* sdata, const SwSize& clip, bool antiAlias)
461 //FIXME: Should we draw it?
463 //if (shape.outline->opened) return true;
465 //Case A: Fast Track Rectangle Drawing
466 if ((shape->rect = _fastTrack(shape->outline))) return true;
467 //Case B: Normale Shape RLE Drawing
468 if ((shape->rle = rleRender(shape->outline, shape->bbox, clip, antiAlias))) return true;
474 void shapeDelOutline(SwShape* shape)
476 auto outline = shape->outline;
477 _delOutline(outline);
478 shape->outline = nullptr;
482 void shapeReset(SwShape* shape)
484 shapeDelOutline(shape);
486 shape->rle = nullptr;
488 _initBBox(shape->bbox);
492 bool shapeGenOutline(SwShape* shape, const Shape* sdata, const Matrix* transform)
494 const PathCommand* cmds = nullptr;
495 auto cmdCnt = sdata->pathCommands(&cmds);
497 const Point* pts = nullptr;
498 auto ptsCnt = sdata->pathCoords(&pts);
500 //No actual shape data
501 if (cmdCnt == 0 || ptsCnt == 0) return false;
504 auto outlinePtsCnt = 0;
505 auto outlineCntrsCnt = 0;
507 for (uint32_t i = 0; i < cmdCnt; ++i) {
508 switch(*(cmds + i)) {
509 case PathCommand::Close: {
513 case PathCommand::MoveTo: {
518 case PathCommand::LineTo: {
522 case PathCommand::CubicTo: {
529 ++outlinePtsCnt; //for close
530 ++outlineCntrsCnt; //for end
532 auto outline = shape->outline;
533 if (!outline) outline = static_cast<SwOutline*>(calloc(1, sizeof(SwOutline)));
534 outline->opened = true;
536 _growOutlinePoint(*outline, outlinePtsCnt);
537 _growOutlineContour(*outline, outlineCntrsCnt);
542 while (cmdCnt-- > 0) {
544 case PathCommand::Close: {
545 _outlineClose(*outline);
549 case PathCommand::MoveTo: {
550 _outlineMoveTo(*outline, pts, transform);
554 case PathCommand::LineTo: {
555 _outlineLineTo(*outline, pts, transform);
559 case PathCommand::CubicTo: {
560 _outlineCubicTo(*outline, pts, pts + 1, pts + 2, transform);
568 _outlineEnd(*outline);
570 if (closed) outline->opened = false;
573 //outline->flags = SwOutline::FillRule::Winding;
575 shape->outline = outline;
581 void shapeFree(SwShape* shape)
583 shapeDelOutline(shape);
588 rleFree(shape->strokeRle);
589 strokeFree(shape->stroke);
594 void shapeDelStroke(SwShape* shape)
596 if (!shape->stroke) return;
597 rleFree(shape->strokeRle);
598 shape->strokeRle = nullptr;
599 strokeFree(shape->stroke);
600 shape->stroke = nullptr;
604 void shapeResetStroke(SwShape* shape, const Shape* sdata, const Matrix* transform)
606 if (!shape->stroke) shape->stroke = static_cast<SwStroke*>(calloc(1, sizeof(SwStroke)));
607 auto stroke = shape->stroke;
610 strokeReset(stroke, sdata, transform);
612 rleFree(shape->strokeRle);
613 shape->strokeRle = nullptr;
617 bool shapeGenStrokeRle(SwShape* shape, const Shape* sdata, const Matrix* transform, const SwSize& clip)
619 SwOutline* shapeOutline = nullptr;
622 if (sdata->strokeDash(nullptr) > 0) {
623 shapeOutline = _genDashOutline(sdata, transform);
624 if (!shapeOutline) return false;
625 //Normal Style stroke
627 if (!shape->outline) {
628 if (!shapeGenOutline(shape, sdata, transform)) return false;
630 shapeOutline = shape->outline;
633 if (!strokeParseOutline(shape->stroke, *shapeOutline)) return false;
635 auto strokeOutline = strokeExportOutline(shape->stroke);
636 if (!strokeOutline) return false;
639 _updateBBox(strokeOutline, bbox);
641 if (!_checkValid(strokeOutline, bbox, clip)) return false;
643 shape->strokeRle = rleRender(strokeOutline, bbox, clip, true);
645 _delOutline(strokeOutline);
651 bool shapeGenFillColors(SwShape* shape, const Fill* fill, const Matrix* transform, SwSurface* surface, bool ctable)
653 return fillGenColorTable(shape->fill, fill, transform, surface, ctable);
657 void shapeResetFill(SwShape* shape)
660 shape->fill = static_cast<SwFill*>(calloc(1, sizeof(SwFill)));
661 if (!shape->fill) return;
663 fillReset(shape->fill);
667 void shapeDelFill(SwShape* shape)
669 if (!shape->fill) return;
670 fillFree(shape->fill);
671 shape->fill = nullptr;