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<uint32_t*>(realloc(outline.cntrs, outline.reservedCntrsCnt * sizeof(uint32_t)));
70 static void _growOutlineClose(SwOutline& outline)
72 //Dash outlines are always opened.
73 //Only normal outlines use this information, it sholud be same to their contour counts.
74 outline.closed = static_cast<bool*>(realloc(outline.closed, outline.reservedCntrsCnt * sizeof(bool)));
78 static void _growOutlinePoint(SwOutline& outline, uint32_t n)
80 if (outline.reservedPtsCnt >= outline.ptsCnt + n) return;
81 outline.reservedPtsCnt = outline.ptsCnt + n;
82 outline.pts = static_cast<SwPoint*>(realloc(outline.pts, outline.reservedPtsCnt * sizeof(SwPoint)));
83 outline.types = static_cast<uint8_t*>(realloc(outline.types, outline.reservedPtsCnt * sizeof(uint8_t)));
87 static void _outlineEnd(SwOutline& outline)
89 _growOutlineContour(outline, 1);
92 outline.closed[outline.cntrsCnt] = false;
95 if (outline.ptsCnt > 0) {
96 outline.cntrs[outline.cntrsCnt] = outline.ptsCnt - 1;
102 static void _outlineMoveTo(SwOutline& outline, const Point* to, const Matrix* transform)
104 _growOutlinePoint(outline, 1);
106 outline.pts[outline.ptsCnt] = mathTransform(to, transform);
107 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
109 if (outline.ptsCnt > 0) {
110 _growOutlineContour(outline, 1);
111 outline.cntrs[outline.cntrsCnt] = outline.ptsCnt - 1;
119 static void _outlineLineTo(SwOutline& outline, const Point* to, const Matrix* transform)
121 _growOutlinePoint(outline, 1);
123 outline.pts[outline.ptsCnt] = mathTransform(to, transform);
124 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
129 static void _outlineCubicTo(SwOutline& outline, const Point* ctrl1, const Point* ctrl2, const Point* to, const Matrix* transform)
131 _growOutlinePoint(outline, 3);
133 outline.pts[outline.ptsCnt] = mathTransform(ctrl1, transform);
134 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_CUBIC;
137 outline.pts[outline.ptsCnt] = mathTransform(ctrl2, transform);
138 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_CUBIC;
141 outline.pts[outline.ptsCnt] = mathTransform(to, transform);
142 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
147 static void _outlineClose(SwOutline& outline)
151 if (outline.cntrsCnt > 0) {
152 i = outline.cntrs[outline.cntrsCnt - 1] + 1;
157 //Make sure there is at least one point in the current path
158 if (outline.ptsCnt == i) {
159 outline.closed[outline.cntrsCnt] = false;
164 _growOutlinePoint(outline, 1);
166 outline.pts[outline.ptsCnt] = outline.pts[i];
167 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
170 outline.closed[outline.cntrsCnt] = true;
174 static void _dashLineTo(SwDashStroke& dash, const Point* to, const Matrix* transform)
176 _growOutlinePoint(*dash.outline, dash.outline->ptsCnt >> 1);
177 _growOutlineContour(*dash.outline, dash.outline->cntrsCnt >> 1);
179 Line cur = {dash.ptCur, *to};
180 auto len = _lineLength(cur.pt1, cur.pt2);
182 if (len < dash.curLen) {
184 if (!dash.curOpGap) {
185 _outlineMoveTo(*dash.outline, &dash.ptCur, transform);
186 _outlineLineTo(*dash.outline, to, transform);
189 while (len > dash.curLen) {
192 _lineSplitAt(cur, dash.curLen, left, right);;
193 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
194 if (!dash.curOpGap) {
195 _outlineMoveTo(*dash.outline, &left.pt1, transform);
196 _outlineLineTo(*dash.outline, &left.pt2, transform);
198 dash.curLen = dash.pattern[dash.curIdx];
199 dash.curOpGap = !dash.curOpGap;
201 dash.ptCur = cur.pt1;
205 if (!dash.curOpGap) {
206 _outlineMoveTo(*dash.outline, &cur.pt1, transform);
207 _outlineLineTo(*dash.outline, &cur.pt2, transform);
209 if (dash.curLen < 1 && TO_SWCOORD(len) > 1) {
211 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
212 dash.curLen = dash.pattern[dash.curIdx];
213 dash.curOpGap = !dash.curOpGap;
220 static void _dashCubicTo(SwDashStroke& dash, const Point* ctrl1, const Point* ctrl2, const Point* to, const Matrix* transform)
222 _growOutlinePoint(*dash.outline, dash.outline->ptsCnt >> 1);
223 _growOutlineContour(*dash.outline, dash.outline->cntrsCnt >> 1);
225 Bezier cur = {dash.ptCur, *ctrl1, *ctrl2, *to};
226 auto len = bezLength(cur);
228 if (len < dash.curLen) {
230 if (!dash.curOpGap) {
231 _outlineMoveTo(*dash.outline, &dash.ptCur, transform);
232 _outlineCubicTo(*dash.outline, ctrl1, ctrl2, to, transform);
235 while (len > dash.curLen) {
238 bezSplitAt(cur, dash.curLen, left, right);
239 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
240 if (!dash.curOpGap) {
241 _outlineMoveTo(*dash.outline, &left.start, transform);
242 _outlineCubicTo(*dash.outline, &left.ctrl1, &left.ctrl2, &left.end, transform);
244 dash.curLen = dash.pattern[dash.curIdx];
245 dash.curOpGap = !dash.curOpGap;
247 dash.ptCur = right.start;
251 if (!dash.curOpGap) {
252 _outlineMoveTo(*dash.outline, &cur.start, transform);
253 _outlineCubicTo(*dash.outline, &cur.ctrl1, &cur.ctrl2, &cur.end, transform);
255 if (dash.curLen < 1 && TO_SWCOORD(len) > 1) {
257 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
258 dash.curLen = dash.pattern[dash.curIdx];
259 dash.curOpGap = !dash.curOpGap;
266 static SwOutline* _genDashOutline(const Shape* sdata, const Matrix* transform)
268 const PathCommand* cmds = nullptr;
269 auto cmdCnt = sdata->pathCommands(&cmds);
271 const Point* pts = nullptr;
272 auto ptsCnt = sdata->pathCoords(&pts);
274 //No actual shape data
275 if (cmdCnt == 0 || ptsCnt == 0) return nullptr;
280 dash.ptStart = {0, 0};
282 dash.curOpGap = false;
284 const float* pattern;
285 dash.cnt = sdata->strokeDash(&pattern);
286 if (dash.cnt == 0) return nullptr;
288 //OPTMIZE ME: Use mempool???
289 dash.pattern = const_cast<float*>(pattern);
290 dash.outline = static_cast<SwOutline*>(calloc(1, sizeof(SwOutline)));
293 auto outlinePtsCnt = 0;
294 auto outlineCntrsCnt = 0;
296 for (uint32_t i = 0; i < cmdCnt; ++i) {
297 switch(*(cmds + i)) {
298 case PathCommand::Close: {
302 case PathCommand::MoveTo: {
307 case PathCommand::LineTo: {
311 case PathCommand::CubicTo: {
318 ++outlinePtsCnt; //for close
319 ++outlineCntrsCnt; //for end
321 //No idea exact count.... Reserve Approximitely 20x...
322 _growOutlinePoint(*dash.outline, outlinePtsCnt * 20);
323 _growOutlineContour(*dash.outline, outlineCntrsCnt * 20);
325 while (cmdCnt-- > 0) {
327 case PathCommand::Close: {
328 _dashLineTo(dash, &dash.ptStart, transform);
331 case PathCommand::MoveTo: {
334 dash.curLen = *dash.pattern;
335 dash.curOpGap = false;
336 dash.ptStart = dash.ptCur = *pts;
340 case PathCommand::LineTo: {
341 _dashLineTo(dash, pts, transform);
345 case PathCommand::CubicTo: {
346 _dashCubicTo(dash, pts, pts + 1, pts + 2, transform);
354 _outlineEnd(*dash.outline);
360 static bool _fastTrack(const SwOutline* outline)
362 //Fast Track: Othogonal rectangle?
363 if (outline->ptsCnt != 5) return false;
365 auto pt1 = outline->pts + 0;
366 auto pt2 = outline->pts + 1;
367 auto pt3 = outline->pts + 2;
368 auto pt4 = outline->pts + 3;
370 auto a = SwPoint{pt1->x, pt3->y};
371 auto b = SwPoint{pt3->x, pt1->y};
373 if ((*pt2 == a && *pt4 == b) || (*pt2 == b && *pt4 == a)) return true;
380 static bool _genOutline(SwShape* shape, const Shape* sdata, const Matrix* transform, SwMpool* mpool, unsigned tid)
382 const PathCommand* cmds = nullptr;
383 auto cmdCnt = sdata->pathCommands(&cmds);
385 const Point* pts = nullptr;
386 auto ptsCnt = sdata->pathCoords(&pts);
388 //No actual shape data
389 if (cmdCnt == 0 || ptsCnt == 0) return false;
392 auto outlinePtsCnt = 0;
393 auto outlineCntrsCnt = 0;
395 for (uint32_t i = 0; i < cmdCnt; ++i) {
396 switch(*(cmds + i)) {
397 case PathCommand::Close: {
401 case PathCommand::MoveTo: {
406 case PathCommand::LineTo: {
410 case PathCommand::CubicTo: {
417 ++outlinePtsCnt; //for close
418 ++outlineCntrsCnt; //for end
420 shape->outline = mpoolReqOutline(mpool, tid);
421 auto outline = shape->outline;
423 _growOutlinePoint(*outline, outlinePtsCnt);
425 if (_growOutlineContour(*outline, outlineCntrsCnt)) {
426 _growOutlineClose(*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;