2 * Copyright (c) 2020-2021 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 bool _growOutlineContour(SwOutline& outline, uint32_t n)
63 if (outline.reservedCntrsCnt >= outline.cntrsCnt + n) return false;
64 outline.reservedCntrsCnt = outline.cntrsCnt + n;
65 outline.cntrs = static_cast<uint16_t*>(realloc(outline.cntrs, outline.reservedCntrsCnt * sizeof(uint16_t)));
70 static void _reserveOutlineClose(SwOutline& outline)
72 //Dash outlines are always opened.
73 //Only normal outlines use this information, it sholud be same to their contour counts.
74 if (outline.closed) free(outline.closed);
75 outline.closed = static_cast<bool*>(calloc(outline.reservedCntrsCnt, sizeof(bool)));
79 static void _resetOutlineClose(SwOutline& outline)
81 memset(outline.closed, 0x0, outline.reservedCntrsCnt * sizeof(bool));
85 static void _growOutlinePoint(SwOutline& outline, uint32_t n)
87 if (outline.reservedPtsCnt >= outline.ptsCnt + n) return;
88 outline.reservedPtsCnt = outline.ptsCnt + n;
89 outline.pts = static_cast<SwPoint*>(realloc(outline.pts, outline.reservedPtsCnt * sizeof(SwPoint)));
90 outline.types = static_cast<uint8_t*>(realloc(outline.types, outline.reservedPtsCnt * sizeof(uint8_t)));
94 static void _outlineEnd(SwOutline& outline)
96 if (outline.ptsCnt == 0) return;
98 _growOutlineContour(outline, 1);
99 outline.cntrs[outline.cntrsCnt] = outline.ptsCnt - 1;
104 static void _outlineMoveTo(SwOutline& outline, const Point* to, const Matrix* transform)
106 _growOutlinePoint(outline, 1);
108 outline.pts[outline.ptsCnt] = mathTransform(to, transform);
109 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
111 if (outline.ptsCnt > 0) {
112 _growOutlineContour(outline, 1);
113 outline.cntrs[outline.cntrsCnt] = outline.ptsCnt - 1;
121 static void _outlineLineTo(SwOutline& outline, const Point* to, const Matrix* transform)
123 _growOutlinePoint(outline, 1);
125 outline.pts[outline.ptsCnt] = mathTransform(to, transform);
126 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
131 static void _outlineCubicTo(SwOutline& outline, const Point* ctrl1, const Point* ctrl2, const Point* to, const Matrix* transform)
133 _growOutlinePoint(outline, 3);
135 outline.pts[outline.ptsCnt] = mathTransform(ctrl1, transform);
136 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_CUBIC;
139 outline.pts[outline.ptsCnt] = mathTransform(ctrl2, transform);
140 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_CUBIC;
143 outline.pts[outline.ptsCnt] = mathTransform(to, transform);
144 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
149 static void _outlineClose(SwOutline& outline)
153 if (outline.cntrsCnt > 0) {
154 i = outline.cntrs[outline.cntrsCnt - 1] + 1;
159 //Make sure there is at least one point in the current path
160 if (outline.ptsCnt == i) return;
163 _growOutlinePoint(outline, 1);
165 outline.pts[outline.ptsCnt] = outline.pts[i];
166 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
168 outline.closed[outline.cntrsCnt] = true;
172 static void _dashLineTo(SwDashStroke& dash, const Point* to, const Matrix* transform)
174 _growOutlinePoint(*dash.outline, dash.outline->ptsCnt >> 1);
175 _growOutlineContour(*dash.outline, dash.outline->cntrsCnt >> 1);
177 Line cur = {dash.ptCur, *to};
178 auto len = _lineLength(cur.pt1, cur.pt2);
180 if (len < dash.curLen) {
182 if (!dash.curOpGap) {
183 _outlineMoveTo(*dash.outline, &dash.ptCur, transform);
184 _outlineLineTo(*dash.outline, to, transform);
187 while (len > dash.curLen) {
190 _lineSplitAt(cur, dash.curLen, left, right);;
191 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
192 if (!dash.curOpGap) {
193 _outlineMoveTo(*dash.outline, &left.pt1, transform);
194 _outlineLineTo(*dash.outline, &left.pt2, transform);
196 dash.curLen = dash.pattern[dash.curIdx];
197 dash.curOpGap = !dash.curOpGap;
199 dash.ptCur = cur.pt1;
203 if (!dash.curOpGap) {
204 _outlineMoveTo(*dash.outline, &cur.pt1, transform);
205 _outlineLineTo(*dash.outline, &cur.pt2, transform);
207 if (dash.curLen < 1 && TO_SWCOORD(len) > 1) {
209 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
210 dash.curLen = dash.pattern[dash.curIdx];
211 dash.curOpGap = !dash.curOpGap;
218 static void _dashCubicTo(SwDashStroke& dash, const Point* ctrl1, const Point* ctrl2, const Point* to, const Matrix* transform)
220 _growOutlinePoint(*dash.outline, dash.outline->ptsCnt >> 1);
221 _growOutlineContour(*dash.outline, dash.outline->cntrsCnt >> 1);
223 Bezier cur = {dash.ptCur, *ctrl1, *ctrl2, *to};
224 auto len = bezLength(cur);
226 if (len < dash.curLen) {
228 if (!dash.curOpGap) {
229 _outlineMoveTo(*dash.outline, &dash.ptCur, transform);
230 _outlineCubicTo(*dash.outline, ctrl1, ctrl2, to, transform);
233 while (len > dash.curLen) {
236 bezSplitAt(cur, dash.curLen, left, right);
237 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
238 if (!dash.curOpGap) {
239 _outlineMoveTo(*dash.outline, &left.start, transform);
240 _outlineCubicTo(*dash.outline, &left.ctrl1, &left.ctrl2, &left.end, transform);
242 dash.curLen = dash.pattern[dash.curIdx];
243 dash.curOpGap = !dash.curOpGap;
245 dash.ptCur = right.start;
249 if (!dash.curOpGap) {
250 _outlineMoveTo(*dash.outline, &cur.start, transform);
251 _outlineCubicTo(*dash.outline, &cur.ctrl1, &cur.ctrl2, &cur.end, transform);
253 if (dash.curLen < 1 && TO_SWCOORD(len) > 1) {
255 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
256 dash.curLen = dash.pattern[dash.curIdx];
257 dash.curOpGap = !dash.curOpGap;
264 static SwOutline* _genDashOutline(const Shape* sdata, const Matrix* transform)
266 const PathCommand* cmds = nullptr;
267 auto cmdCnt = sdata->pathCommands(&cmds);
269 const Point* pts = nullptr;
270 auto ptsCnt = sdata->pathCoords(&pts);
272 //No actual shape data
273 if (cmdCnt == 0 || ptsCnt == 0) return nullptr;
278 dash.ptStart = {0, 0};
280 dash.curOpGap = false;
282 const float* pattern;
283 dash.cnt = sdata->strokeDash(&pattern);
284 if (dash.cnt == 0) return nullptr;
286 //OPTMIZE ME: Use mempool???
287 dash.pattern = const_cast<float*>(pattern);
288 dash.outline = static_cast<SwOutline*>(calloc(1, sizeof(SwOutline)));
291 auto outlinePtsCnt = 0;
292 auto outlineCntrsCnt = 0;
294 for (uint32_t i = 0; i < cmdCnt; ++i) {
295 switch(*(cmds + i)) {
296 case PathCommand::Close: {
300 case PathCommand::MoveTo: {
305 case PathCommand::LineTo: {
309 case PathCommand::CubicTo: {
316 ++outlinePtsCnt; //for close
317 ++outlineCntrsCnt; //for end
319 //No idea exact count.... Reserve Approximitely 20x...
320 _growOutlinePoint(*dash.outline, outlinePtsCnt * 20);
321 _growOutlineContour(*dash.outline, outlineCntrsCnt * 20);
323 while (cmdCnt-- > 0) {
325 case PathCommand::Close: {
326 _dashLineTo(dash, &dash.ptStart, transform);
329 case PathCommand::MoveTo: {
332 dash.curLen = *dash.pattern;
333 dash.curOpGap = false;
334 dash.ptStart = dash.ptCur = *pts;
338 case PathCommand::LineTo: {
339 _dashLineTo(dash, pts, transform);
343 case PathCommand::CubicTo: {
344 _dashCubicTo(dash, pts, pts + 1, pts + 2, transform);
352 _outlineEnd(*dash.outline);
358 static bool _fastTrack(const SwOutline* outline)
360 //Fast Track: Othogonal rectangle?
361 if (outline->ptsCnt != 5) return false;
363 auto pt1 = outline->pts + 0;
364 auto pt2 = outline->pts + 1;
365 auto pt3 = outline->pts + 2;
366 auto pt4 = outline->pts + 3;
368 auto a = SwPoint{pt1->x, pt3->y};
369 auto b = SwPoint{pt3->x, pt1->y};
371 if ((*pt2 == a && *pt4 == b) || (*pt2 == b && *pt4 == a)) return true;
378 static bool _genOutline(SwShape* shape, const Shape* sdata, const Matrix* transform, SwMpool* mpool, unsigned tid)
380 const PathCommand* cmds = nullptr;
381 auto cmdCnt = sdata->pathCommands(&cmds);
383 const Point* pts = nullptr;
384 auto ptsCnt = sdata->pathCoords(&pts);
386 //No actual shape data
387 if (cmdCnt == 0 || ptsCnt == 0) return false;
390 auto outlinePtsCnt = 0;
391 auto outlineCntrsCnt = 0;
393 for (uint32_t i = 0; i < cmdCnt; ++i) {
394 switch(*(cmds + i)) {
395 case PathCommand::Close: {
399 case PathCommand::MoveTo: {
404 case PathCommand::LineTo: {
408 case PathCommand::CubicTo: {
415 ++outlinePtsCnt; //for close
416 ++outlineCntrsCnt; //for end
418 shape->outline = mpoolReqOutline(mpool, tid);
419 auto outline = shape->outline;
421 _growOutlinePoint(*outline, outlinePtsCnt);
423 if (_growOutlineContour(*outline, outlineCntrsCnt)) {
424 _reserveOutlineClose(*outline);
426 _resetOutlineClose(*outline);
430 while (cmdCnt-- > 0) {
432 case PathCommand::Close: {
433 _outlineClose(*outline);
436 case PathCommand::MoveTo: {
437 _outlineMoveTo(*outline, pts, transform);
441 case PathCommand::LineTo: {
442 _outlineLineTo(*outline, pts, transform);
446 case PathCommand::CubicTo: {
447 _outlineCubicTo(*outline, pts, pts + 1, pts + 2, transform);
455 _outlineEnd(*outline);
457 outline->fillRule = sdata->fillRule();
458 shape->outline = outline;
464 /************************************************************************/
465 /* External Class Implementation */
466 /************************************************************************/
468 bool shapePrepare(SwShape* shape, const Shape* sdata, const Matrix* transform, const SwBBox& clipRegion, SwBBox& renderRegion, SwMpool* mpool, unsigned tid)
470 if (!_genOutline(shape, sdata, transform, mpool, tid)) return false;
471 if (!mathUpdateOutlineBBox(shape->outline, clipRegion, renderRegion)) return false;
473 //Keep it for Rasterization Region
474 shape->bbox = renderRegion;
477 if (renderRegion.max.x - renderRegion.min.x < 1 && renderRegion.max.y - renderRegion.min.y < 1) return false;
480 if (renderRegion.min.x >= clipRegion.max.x || renderRegion.min.y >= clipRegion.max.y ||
481 renderRegion.max.x <= clipRegion.min.x || renderRegion.max.y <= clipRegion.min.y) return false;
487 bool shapePrepared(const SwShape* shape)
489 return shape->rle ? true : false;
493 bool shapeGenRle(SwShape* shape, TVG_UNUSED const Shape* sdata, bool antiAlias, bool hasComposite)
495 //FIXME: Should we draw it?
497 //if (shape.outline->opened) return true;
499 //Case A: Fast Track Rectangle Drawing
500 if (!hasComposite && (shape->rect = _fastTrack(shape->outline))) return true;
501 //Case B: Normale Shape RLE Drawing
502 if ((shape->rle = rleRender(shape->rle, shape->outline, shape->bbox, antiAlias))) return true;
508 void shapeDelOutline(SwShape* shape, SwMpool* mpool, uint32_t tid)
510 mpoolRetOutline(mpool, tid);
511 shape->outline = nullptr;
515 void shapeReset(SwShape* shape)
517 rleReset(shape->rle);
518 rleReset(shape->strokeRle);
524 void shapeFree(SwShape* shape)
530 rleFree(shape->strokeRle);
531 strokeFree(shape->stroke);
536 void shapeDelStroke(SwShape* shape)
538 if (!shape->stroke) return;
539 rleFree(shape->strokeRle);
540 shape->strokeRle = nullptr;
541 strokeFree(shape->stroke);
542 shape->stroke = nullptr;
546 void shapeResetStroke(SwShape* shape, const Shape* sdata, const Matrix* transform)
548 if (!shape->stroke) shape->stroke = static_cast<SwStroke*>(calloc(1, sizeof(SwStroke)));
549 auto stroke = shape->stroke;
552 strokeReset(stroke, sdata, transform);
553 rleReset(shape->strokeRle);
557 bool shapeGenStrokeRle(SwShape* shape, const Shape* sdata, const Matrix* transform, const SwBBox& clipRegion, SwBBox& renderRegion, SwMpool* mpool, unsigned tid)
559 SwOutline* shapeOutline = nullptr;
560 SwOutline* strokeOutline = nullptr;
561 bool freeOutline = false;
565 if (sdata->strokeDash(nullptr) > 0) {
566 shapeOutline = _genDashOutline(sdata, transform);
567 if (!shapeOutline) return false;
569 //Normal Style stroke
571 if (!shape->outline) {
572 if (!_genOutline(shape, sdata, transform, mpool, tid)) return false;
574 shapeOutline = shape->outline;
577 if (!strokeParseOutline(shape->stroke, *shapeOutline)) {
582 strokeOutline = strokeExportOutline(shape->stroke, mpool, tid);
583 if (!strokeOutline) {
588 if (!mathUpdateOutlineBBox(strokeOutline, clipRegion, renderRegion)) {
593 shape->strokeRle = rleRender(shape->strokeRle, strokeOutline, renderRegion, true);
597 if (shapeOutline->cntrs) free(shapeOutline->cntrs);
598 if (shapeOutline->pts) free(shapeOutline->pts);
599 if (shapeOutline->types) free(shapeOutline->types);
600 if (shapeOutline->closed) free(shapeOutline->closed);
603 mpoolRetStrokeOutline(mpool, tid);
609 bool shapeGenFillColors(SwShape* shape, const Fill* fill, const Matrix* transform, SwSurface* surface, uint32_t opacity, bool ctable)
611 return fillGenColorTable(shape->fill, fill, transform, surface, opacity, ctable);
615 bool shapeGenStrokeFillColors(SwShape* shape, const Fill* fill, const Matrix* transform, SwSurface* surface, uint32_t opacity, bool ctable)
617 return fillGenColorTable(shape->stroke->fill, fill, transform, surface, opacity, ctable);
621 void shapeResetFill(SwShape* shape)
624 shape->fill = static_cast<SwFill*>(calloc(1, sizeof(SwFill)));
625 if (!shape->fill) return;
627 fillReset(shape->fill);
631 void shapeResetStrokeFill(SwShape* shape)
633 if (!shape->stroke->fill) {
634 shape->stroke->fill = static_cast<SwFill*>(calloc(1, sizeof(SwFill)));
635 if (!shape->stroke->fill) return;
637 fillReset(shape->stroke->fill);
641 void shapeDelFill(SwShape* shape)
643 if (!shape->fill) return;
644 fillFree(shape->fill);
645 shape->fill = nullptr;
649 void shapeDelStrokeFill(SwShape* shape)
651 if (!shape->stroke->fill) return;
652 fillFree(shape->stroke->fill);
653 shape->stroke->fill = nullptr;