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"
23 #include "tvgBezier.h"
25 /************************************************************************/
26 /* Internal Class Implementation */
27 /************************************************************************/
36 static float _lineLength(const Point& pt1, const Point& pt2)
38 /* approximate sqrt(x*x + y*y) using alpha max plus beta min algorithm.
39 With alpha = 1, beta = 3/8, giving results with the largest error less
40 than 7% compared to the exact value. */
41 Point diff = {pt2.x - pt1.x, pt2.y - pt1.y};
42 if (diff.x < 0) diff.x = -diff.x;
43 if (diff.y < 0) diff.y = -diff.y;
44 return (diff.x > diff.y) ? (diff.x + diff.y * 0.375f) : (diff.y + diff.x * 0.375f);
48 static void _lineSplitAt(const Line& cur, float at, Line& left, Line& right)
50 auto len = _lineLength(cur.pt1, cur.pt2);
51 auto dx = ((cur.pt2.x - cur.pt1.x) / len) * at;
52 auto dy = ((cur.pt2.y - cur.pt1.y) / len) * at;
54 left.pt2.x = left.pt1.x + dx;
55 left.pt2.y = left.pt1.y + dy;
61 static void _growOutlineContour(SwOutline& outline, uint32_t n)
63 if (outline.reservedCntrsCnt >= outline.cntrsCnt + n) return;
64 outline.reservedCntrsCnt = outline.cntrsCnt + n;
65 outline.cntrs = static_cast<uint32_t*>(realloc(outline.cntrs, outline.reservedCntrsCnt * sizeof(uint32_t)));
69 static void _growOutlinePoint(SwOutline& outline, uint32_t n)
71 if (outline.reservedPtsCnt >= outline.ptsCnt + n) return;
72 outline.reservedPtsCnt = outline.ptsCnt + n;
73 outline.pts = static_cast<SwPoint*>(realloc(outline.pts, outline.reservedPtsCnt * sizeof(SwPoint)));
74 outline.types = static_cast<uint8_t*>(realloc(outline.types, outline.reservedPtsCnt * sizeof(uint8_t)));
78 static void _outlineEnd(SwOutline& outline)
80 _growOutlineContour(outline, 1);
81 if (outline.ptsCnt > 0) {
82 outline.cntrs[outline.cntrsCnt] = outline.ptsCnt - 1;
88 static void _outlineMoveTo(SwOutline& outline, const Point* to, const Matrix* transform)
90 _growOutlinePoint(outline, 1);
92 outline.pts[outline.ptsCnt] = mathTransform(to, transform);
93 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
95 if (outline.ptsCnt > 0) {
96 _growOutlineContour(outline, 1);
97 outline.cntrs[outline.cntrsCnt] = outline.ptsCnt - 1;
105 static void _outlineLineTo(SwOutline& outline, const Point* to, const Matrix* transform)
107 _growOutlinePoint(outline, 1);
109 outline.pts[outline.ptsCnt] = mathTransform(to, transform);
110 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
115 static void _outlineCubicTo(SwOutline& outline, const Point* ctrl1, const Point* ctrl2, const Point* to, const Matrix* transform)
117 _growOutlinePoint(outline, 3);
119 outline.pts[outline.ptsCnt] = mathTransform(ctrl1, transform);
120 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_CUBIC;
123 outline.pts[outline.ptsCnt] = mathTransform(ctrl2, transform);
124 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_CUBIC;
127 outline.pts[outline.ptsCnt] = mathTransform(to, transform);
128 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
133 static void _outlineClose(SwOutline& outline)
137 if (outline.cntrsCnt > 0) {
138 i = outline.cntrs[outline.cntrsCnt - 1] + 1;
143 //Make sure there is at least one point in the current path
144 if (outline.ptsCnt == i) {
145 outline.opened = true;
150 _growOutlinePoint(outline, 1);
152 outline.pts[outline.ptsCnt] = outline.pts[i];
153 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
156 outline.opened = false;
160 static void _initBBox(SwBBox& bbox)
162 bbox.min.x = bbox.min.y = 0;
163 bbox.max.x = bbox.max.y = 0;
167 static bool _updateBBox(const SwOutline* outline, SwBBox& bbox)
169 if (!outline) return false;
171 auto pt = outline->pts;
173 if (outline->ptsCnt <= 0) {
185 for(uint32_t i = 1; i < outline->ptsCnt; ++i, ++pt) {
186 if (xMin > pt->x) xMin = pt->x;
187 if (xMax < pt->x) xMax = pt->x;
188 if (yMin > pt->y) yMin = pt->y;
189 if (yMax < pt->y) yMax = pt->y;
191 bbox.min.x = xMin >> 6;
192 bbox.max.x = (xMax + 63) >> 6;
193 bbox.min.y = yMin >> 6;
194 bbox.max.y = (yMax + 63) >> 6;
196 if (xMax - xMin < 1 || yMax - yMin < 1) return false;
202 static bool _checkValid(const SwOutline* outline, const SwBBox& bbox, const SwSize& clip)
204 if (outline->ptsCnt == 0 || outline->cntrsCnt <= 0) return false;
207 if (bbox.min.x >= clip.w || bbox.min.y >= clip.h || bbox.max.x <= 0 || bbox.max.y <= 0) return false;
213 static void _dashLineTo(SwDashStroke& dash, const Point* to, const Matrix* transform)
215 _growOutlinePoint(*dash.outline, dash.outline->ptsCnt >> 1);
216 _growOutlineContour(*dash.outline, dash.outline->cntrsCnt >> 1);
218 Line cur = {dash.ptCur, *to};
219 auto len = _lineLength(cur.pt1, cur.pt2);
221 if (len < dash.curLen) {
223 if (!dash.curOpGap) {
224 _outlineMoveTo(*dash.outline, &dash.ptCur, transform);
225 _outlineLineTo(*dash.outline, to, transform);
228 while (len > dash.curLen) {
231 _lineSplitAt(cur, dash.curLen, left, right);;
232 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
233 if (!dash.curOpGap) {
234 _outlineMoveTo(*dash.outline, &left.pt1, transform);
235 _outlineLineTo(*dash.outline, &left.pt2, transform);
237 dash.curLen = dash.pattern[dash.curIdx];
238 dash.curOpGap = !dash.curOpGap;
240 dash.ptCur = cur.pt1;
244 if (!dash.curOpGap) {
245 _outlineMoveTo(*dash.outline, &cur.pt1, transform);
246 _outlineLineTo(*dash.outline, &cur.pt2, transform);
248 if (dash.curLen < 1 && TO_SWCOORD(len) > 1) {
250 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
251 dash.curLen = dash.pattern[dash.curIdx];
252 dash.curOpGap = !dash.curOpGap;
259 static void _dashCubicTo(SwDashStroke& dash, const Point* ctrl1, const Point* ctrl2, const Point* to, const Matrix* transform)
261 _growOutlinePoint(*dash.outline, dash.outline->ptsCnt >> 1);
262 _growOutlineContour(*dash.outline, dash.outline->cntrsCnt >> 1);
264 Bezier cur = {dash.ptCur, *ctrl1, *ctrl2, *to};
265 auto len = bezLength(cur);
267 if (len < dash.curLen) {
269 if (!dash.curOpGap) {
270 _outlineMoveTo(*dash.outline, &dash.ptCur, transform);
271 _outlineCubicTo(*dash.outline, ctrl1, ctrl2, to, transform);
274 while (len > dash.curLen) {
277 bezSplitAt(cur, dash.curLen, left, right);
278 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
279 if (!dash.curOpGap) {
280 _outlineMoveTo(*dash.outline, &left.start, transform);
281 _outlineCubicTo(*dash.outline, &left.ctrl1, &left.ctrl2, &left.end, transform);
283 dash.curLen = dash.pattern[dash.curIdx];
284 dash.curOpGap = !dash.curOpGap;
286 dash.ptCur = right.start;
290 if (!dash.curOpGap) {
291 _outlineMoveTo(*dash.outline, &cur.start, transform);
292 _outlineCubicTo(*dash.outline, &cur.ctrl1, &cur.ctrl2, &cur.end, transform);
294 if (dash.curLen < 1 && TO_SWCOORD(len) > 1) {
296 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
297 dash.curLen = dash.pattern[dash.curIdx];
298 dash.curOpGap = !dash.curOpGap;
305 SwOutline* _genDashOutline(const Shape* sdata, const Matrix* transform)
307 const PathCommand* cmds = nullptr;
308 auto cmdCnt = sdata->pathCommands(&cmds);
310 const Point* pts = nullptr;
311 auto ptsCnt = sdata->pathCoords(&pts);
313 //No actual shape data
314 if (cmdCnt == 0 || ptsCnt == 0) return nullptr;
319 dash.ptStart = {0, 0};
321 dash.curOpGap = false;
323 const float* pattern;
324 dash.cnt = sdata->strokeDash(&pattern);
325 if (dash.cnt == 0) return nullptr;
327 //OPTMIZE ME: Use mempool???
328 dash.pattern = const_cast<float*>(pattern);
329 dash.outline = static_cast<SwOutline*>(calloc(1, sizeof(SwOutline)));
330 dash.outline->opened = true;
333 auto outlinePtsCnt = 0;
334 auto outlineCntrsCnt = 0;
336 for (uint32_t i = 0; i < cmdCnt; ++i) {
337 switch(*(cmds + i)) {
338 case PathCommand::Close: {
342 case PathCommand::MoveTo: {
347 case PathCommand::LineTo: {
351 case PathCommand::CubicTo: {
358 ++outlinePtsCnt; //for close
359 ++outlineCntrsCnt; //for end
361 //Reserve Approximitely 20x...
362 _growOutlinePoint(*dash.outline, outlinePtsCnt * 20);
363 _growOutlineContour(*dash.outline, outlineCntrsCnt * 20);
365 while (cmdCnt-- > 0) {
367 case PathCommand::Close: {
368 _dashLineTo(dash, &dash.ptStart, transform);
371 case PathCommand::MoveTo: {
374 dash.curLen = *dash.pattern;
375 dash.curOpGap = false;
376 dash.ptStart = dash.ptCur = *pts;
380 case PathCommand::LineTo: {
381 _dashLineTo(dash, pts, transform);
385 case PathCommand::CubicTo: {
386 _dashCubicTo(dash, pts, pts + 1, pts + 2, transform);
394 _outlineEnd(*dash.outline);
400 bool _fastTrack(const SwOutline* outline)
402 //Fast Track: Othogonal rectangle?
403 if (outline->ptsCnt != 5) return false;
405 auto pt1 = outline->pts + 0;
406 auto pt2 = outline->pts + 1;
407 auto pt3 = outline->pts + 2;
408 auto pt4 = outline->pts + 3;
410 auto a = SwPoint{pt1->x, pt3->y};
411 auto b = SwPoint{pt3->x, pt1->y};
413 if ((*pt2 == a && *pt4 == b) || (*pt2 == b && *pt4 == a)) return true;
419 /************************************************************************/
420 /* External Class Implementation */
421 /************************************************************************/
423 bool shapePrepare(SwShape* shape, const Shape* sdata, unsigned tid, const SwSize& clip, const Matrix* transform, SwBBox& bbox)
425 if (!shapeGenOutline(shape, sdata, tid, transform)) return false;
427 if (!_updateBBox(shape->outline, shape->bbox)) return false;
429 if (!_checkValid(shape->outline, shape->bbox, clip)) return false;
437 bool shapePrepared(const SwShape* shape)
439 return shape->rle ? true : false;
443 bool shapeGenRle(SwShape* shape, TVG_UNUSED const Shape* sdata, const SwSize& clip, bool antiAlias, bool hasComposite)
445 //FIXME: Should we draw it?
447 //if (shape.outline->opened) return true;
449 //Case A: Fast Track Rectangle Drawing
450 if (!hasComposite && (shape->rect = _fastTrack(shape->outline))) return true;
451 //Case B: Normale Shape RLE Drawing
452 if ((shape->rle = rleRender(shape->rle, shape->outline, shape->bbox, clip, antiAlias))) return true;
458 void shapeDelOutline(SwShape* shape, uint32_t tid)
460 mpoolRetOutline(tid);
461 shape->outline = nullptr;
465 void shapeReset(SwShape* shape)
467 rleReset(shape->rle);
468 rleReset(shape->strokeRle);
470 _initBBox(shape->bbox);
474 bool shapeGenOutline(SwShape* shape, const Shape* sdata, unsigned tid, const Matrix* transform)
476 const PathCommand* cmds = nullptr;
477 auto cmdCnt = sdata->pathCommands(&cmds);
479 const Point* pts = nullptr;
480 auto ptsCnt = sdata->pathCoords(&pts);
482 //No actual shape data
483 if (cmdCnt == 0 || ptsCnt == 0) return false;
486 auto outlinePtsCnt = 0;
487 auto outlineCntrsCnt = 0;
489 for (uint32_t i = 0; i < cmdCnt; ++i) {
490 switch(*(cmds + i)) {
491 case PathCommand::Close: {
495 case PathCommand::MoveTo: {
500 case PathCommand::LineTo: {
504 case PathCommand::CubicTo: {
511 ++outlinePtsCnt; //for close
512 ++outlineCntrsCnt; //for end
514 shape->outline = mpoolReqOutline(tid);
515 auto outline = shape->outline;
516 outline->opened = true;
518 _growOutlinePoint(*outline, outlinePtsCnt);
519 _growOutlineContour(*outline, outlineCntrsCnt);
524 while (cmdCnt-- > 0) {
526 case PathCommand::Close: {
527 _outlineClose(*outline);
531 case PathCommand::MoveTo: {
532 _outlineMoveTo(*outline, pts, transform);
536 case PathCommand::LineTo: {
537 _outlineLineTo(*outline, pts, transform);
541 case PathCommand::CubicTo: {
542 _outlineCubicTo(*outline, pts, pts + 1, pts + 2, transform);
550 _outlineEnd(*outline);
552 if (closed) outline->opened = false;
554 outline->fillRule = sdata->fillRule();
555 shape->outline = outline;
561 void shapeFree(SwShape* shape)
567 rleFree(shape->strokeRle);
568 strokeFree(shape->stroke);
573 void shapeDelStroke(SwShape* shape)
575 if (!shape->stroke) return;
576 rleFree(shape->strokeRle);
577 shape->strokeRle = nullptr;
578 strokeFree(shape->stroke);
579 shape->stroke = nullptr;
583 void shapeResetStroke(SwShape* shape, const Shape* sdata, const Matrix* transform)
585 if (!shape->stroke) shape->stroke = static_cast<SwStroke*>(calloc(1, sizeof(SwStroke)));
586 auto stroke = shape->stroke;
589 strokeReset(stroke, sdata, transform);
590 rleReset(shape->strokeRle);
594 bool shapeGenStrokeRle(SwShape* shape, const Shape* sdata, unsigned tid, const Matrix* transform, const SwSize& clip, SwBBox& bbox)
596 SwOutline* shapeOutline = nullptr;
597 SwOutline* strokeOutline = nullptr;
598 bool freeOutline = false;
602 if (sdata->strokeDash(nullptr) > 0) {
603 shapeOutline = _genDashOutline(sdata, transform);
604 if (!shapeOutline) return false;
606 //Normal Style stroke
608 if (!shape->outline) {
609 if (!shapeGenOutline(shape, sdata, tid, transform)) return false;
611 shapeOutline = shape->outline;
614 if (!strokeParseOutline(shape->stroke, *shapeOutline)) {
619 strokeOutline = strokeExportOutline(shape->stroke, tid);
620 if (!strokeOutline) {
625 _updateBBox(strokeOutline, bbox);
627 if (!_checkValid(strokeOutline, bbox, clip)) {
632 shape->strokeRle = rleRender(shape->strokeRle, strokeOutline, bbox, clip, true);
636 if (shapeOutline->cntrs) free(shapeOutline->cntrs);
637 if (shapeOutline->pts) free(shapeOutline->pts);
638 if (shapeOutline->types) free(shapeOutline->types);
641 mpoolRetStrokeOutline(tid);
647 bool shapeGenFillColors(SwShape* shape, const Fill* fill, const Matrix* transform, SwSurface* surface, uint32_t opacity, bool ctable)
649 return fillGenColorTable(shape->fill, fill, transform, surface, opacity, ctable);
653 bool shapeGenStrokeFillColors(SwShape* shape, const Fill* fill, const Matrix* transform, SwSurface* surface, uint32_t opacity, bool ctable)
655 return fillGenColorTable(shape->stroke->fill, fill, transform, surface, opacity, ctable);
659 void shapeResetFill(SwShape* shape)
662 shape->fill = static_cast<SwFill*>(calloc(1, sizeof(SwFill)));
663 if (!shape->fill) return;
665 fillReset(shape->fill);
669 void shapeResetStrokeFill(SwShape* shape)
671 if (!shape->stroke->fill) {
672 shape->stroke->fill = static_cast<SwFill*>(calloc(1, sizeof(SwFill)));
673 if (!shape->stroke->fill) return;
675 fillReset(shape->stroke->fill);
679 void shapeDelFill(SwShape* shape)
681 if (!shape->fill) return;
682 fillFree(shape->fill);
683 shape->fill = nullptr;
687 void shapeDelStrokeFill(SwShape* shape)
689 if (!shape->stroke->fill) return;
690 fillFree(shape->stroke->fill);
691 shape->stroke->fill = nullptr;