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"
27 /************************************************************************/
28 /* Internal Class Implementation */
29 /************************************************************************/
38 static float _lineLength(const Point& pt1, const Point& pt2)
40 /* approximate sqrt(x*x + y*y) using alpha max plus beta min algorithm.
41 With alpha = 1, beta = 3/8, giving results with the largest error less
42 than 7% compared to the exact value. */
43 Point diff = {pt2.x - pt1.x, pt2.y - pt1.y};
44 if (diff.x < 0) diff.x = -diff.x;
45 if (diff.y < 0) diff.y = -diff.y;
46 return (diff.x > diff.y) ? (diff.x + diff.y * 0.375f) : (diff.y + diff.x * 0.375f);
50 static void _lineSplitAt(const Line& cur, float at, Line& left, Line& right)
52 auto len = _lineLength(cur.pt1, cur.pt2);
53 auto dx = ((cur.pt2.x - cur.pt1.x) / len) * at;
54 auto dy = ((cur.pt2.y - cur.pt1.y) / len) * at;
56 left.pt2.x = left.pt1.x + dx;
57 left.pt2.y = left.pt1.y + dy;
63 static bool _growOutlineContour(SwOutline& outline, uint32_t n)
65 if (outline.reservedCntrsCnt >= outline.cntrsCnt + n) return false;
66 outline.reservedCntrsCnt = outline.cntrsCnt + n;
67 outline.cntrs = static_cast<uint16_t*>(realloc(outline.cntrs, outline.reservedCntrsCnt * sizeof(uint16_t)));
72 static void _reserveOutlineClose(SwOutline& outline)
74 //Dash outlines are always opened.
75 //Only normal outlines use this information, it sholud be same to their contour counts.
76 if (outline.closed) free(outline.closed);
77 outline.closed = static_cast<bool*>(calloc(outline.reservedCntrsCnt, sizeof(bool)));
81 static void _resetOutlineClose(SwOutline& outline)
83 memset(outline.closed, 0x0, outline.reservedCntrsCnt * sizeof(bool));
87 static void _growOutlinePoint(SwOutline& outline, uint32_t n)
89 if (outline.reservedPtsCnt >= outline.ptsCnt + n) return;
90 outline.reservedPtsCnt = outline.ptsCnt + n;
91 outline.pts = static_cast<SwPoint*>(realloc(outline.pts, outline.reservedPtsCnt * sizeof(SwPoint)));
92 outline.types = static_cast<uint8_t*>(realloc(outline.types, outline.reservedPtsCnt * sizeof(uint8_t)));
96 static void _outlineEnd(SwOutline& outline)
98 if (outline.ptsCnt == 0) return;
100 _growOutlineContour(outline, 1);
101 outline.cntrs[outline.cntrsCnt] = outline.ptsCnt - 1;
106 static void _outlineMoveTo(SwOutline& outline, const Point* to, const Matrix* transform)
108 _growOutlinePoint(outline, 1);
110 outline.pts[outline.ptsCnt] = mathTransform(to, transform);
111 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
113 if (outline.ptsCnt > 0) {
114 _growOutlineContour(outline, 1);
115 outline.cntrs[outline.cntrsCnt] = outline.ptsCnt - 1;
123 static void _outlineLineTo(SwOutline& outline, const Point* to, const Matrix* transform)
125 _growOutlinePoint(outline, 1);
127 outline.pts[outline.ptsCnt] = mathTransform(to, transform);
128 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
133 static void _outlineCubicTo(SwOutline& outline, const Point* ctrl1, const Point* ctrl2, const Point* to, const Matrix* transform)
135 _growOutlinePoint(outline, 3);
137 outline.pts[outline.ptsCnt] = mathTransform(ctrl1, transform);
138 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_CUBIC;
141 outline.pts[outline.ptsCnt] = mathTransform(ctrl2, transform);
142 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_CUBIC;
145 outline.pts[outline.ptsCnt] = mathTransform(to, transform);
146 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
151 static void _outlineClose(SwOutline& outline)
155 if (outline.cntrsCnt > 0) {
156 i = outline.cntrs[outline.cntrsCnt - 1] + 1;
161 //Make sure there is at least one point in the current path
162 if (outline.ptsCnt == i) return;
165 _growOutlinePoint(outline, 1);
167 outline.pts[outline.ptsCnt] = outline.pts[i];
168 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 if (!dash.curOpGap) {
240 // leftovers from a previous command don't require moveTo
241 if (dash.pattern[dash.curIdx] - dash.curLen < FLT_EPSILON) {
242 _outlineMoveTo(*dash.outline, &left.start, transform);
244 _outlineCubicTo(*dash.outline, &left.ctrl1, &left.ctrl2, &left.end, transform);
246 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
247 dash.curLen = dash.pattern[dash.curIdx];
248 dash.curOpGap = !dash.curOpGap;
250 dash.ptCur = right.start;
254 if (!dash.curOpGap) {
255 _outlineMoveTo(*dash.outline, &cur.start, transform);
256 _outlineCubicTo(*dash.outline, &cur.ctrl1, &cur.ctrl2, &cur.end, transform);
258 if (dash.curLen < 1 && TO_SWCOORD(len) > 1) {
260 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
261 dash.curLen = dash.pattern[dash.curIdx];
262 dash.curOpGap = !dash.curOpGap;
269 static SwOutline* _genDashOutline(const Shape* sdata, const Matrix* transform)
271 const PathCommand* cmds = nullptr;
272 auto cmdCnt = sdata->pathCommands(&cmds);
274 const Point* pts = nullptr;
275 auto ptsCnt = sdata->pathCoords(&pts);
277 //No actual shape data
278 if (cmdCnt == 0 || ptsCnt == 0) return nullptr;
283 dash.ptStart = {0, 0};
285 dash.curOpGap = false;
287 const float* pattern;
288 dash.cnt = sdata->strokeDash(&pattern);
289 if (dash.cnt == 0) return nullptr;
291 //OPTMIZE ME: Use mempool???
292 dash.pattern = const_cast<float*>(pattern);
293 dash.outline = static_cast<SwOutline*>(calloc(1, sizeof(SwOutline)));
296 auto outlinePtsCnt = 0;
297 auto outlineCntrsCnt = 0;
299 for (uint32_t i = 0; i < cmdCnt; ++i) {
300 switch(*(cmds + i)) {
301 case PathCommand::Close: {
305 case PathCommand::MoveTo: {
310 case PathCommand::LineTo: {
314 case PathCommand::CubicTo: {
321 ++outlinePtsCnt; //for close
322 ++outlineCntrsCnt; //for end
324 //No idea exact count.... Reserve Approximitely 20x...
325 _growOutlinePoint(*dash.outline, outlinePtsCnt * 20);
326 _growOutlineContour(*dash.outline, outlineCntrsCnt * 20);
328 while (cmdCnt-- > 0) {
330 case PathCommand::Close: {
331 _dashLineTo(dash, &dash.ptStart, transform);
334 case PathCommand::MoveTo: {
337 dash.curLen = *dash.pattern;
338 dash.curOpGap = false;
339 dash.ptStart = dash.ptCur = *pts;
343 case PathCommand::LineTo: {
344 _dashLineTo(dash, pts, transform);
348 case PathCommand::CubicTo: {
349 _dashCubicTo(dash, pts, pts + 1, pts + 2, transform);
357 _outlineEnd(*dash.outline);
363 static bool _axisAlignedRect(const SwOutline* outline)
365 //Fast Track: axis-aligned rectangle?
366 if (outline->ptsCnt != 5) return false;
368 auto pt1 = outline->pts + 0;
369 auto pt2 = outline->pts + 1;
370 auto pt3 = outline->pts + 2;
371 auto pt4 = outline->pts + 3;
373 auto a = SwPoint{pt1->x, pt3->y};
374 auto b = SwPoint{pt3->x, pt1->y};
376 if ((*pt2 == a && *pt4 == b) || (*pt2 == b && *pt4 == a)) return true;
383 static bool _genOutline(SwShape* shape, const Shape* sdata, const Matrix* transform, SwMpool* mpool, unsigned tid, bool hasComposite)
385 const PathCommand* cmds = nullptr;
386 auto cmdCnt = sdata->pathCommands(&cmds);
388 const Point* pts = nullptr;
389 auto ptsCnt = sdata->pathCoords(&pts);
391 //No actual shape data
392 if (cmdCnt == 0 || ptsCnt == 0) return false;
395 auto outlinePtsCnt = 0;
396 auto outlineCntrsCnt = 0;
399 for (uint32_t i = 0; i < cmdCnt; ++i) {
400 switch(*(cmds + i)) {
401 case PathCommand::Close: {
406 case PathCommand::MoveTo: {
411 case PathCommand::LineTo: {
415 case PathCommand::CubicTo: {
422 if (static_cast<uint32_t>(outlinePtsCnt - closeCnt) > ptsCnt) {
423 TVGERR("SW_ENGINE", "Wrong a pair of the commands & points - required(%d), current(%d)", outlinePtsCnt - closeCnt, ptsCnt);
427 ++outlinePtsCnt; //for close
428 ++outlineCntrsCnt; //for end
430 shape->outline = mpoolReqOutline(mpool, tid);
431 auto outline = shape->outline;
433 _growOutlinePoint(*outline, outlinePtsCnt);
435 if (_growOutlineContour(*outline, outlineCntrsCnt)) {
436 _reserveOutlineClose(*outline);
438 _resetOutlineClose(*outline);
442 while (cmdCnt-- > 0) {
444 case PathCommand::Close: {
445 _outlineClose(*outline);
448 case PathCommand::MoveTo: {
449 _outlineMoveTo(*outline, pts, transform);
453 case PathCommand::LineTo: {
454 _outlineLineTo(*outline, pts, transform);
458 case PathCommand::CubicTo: {
459 _outlineCubicTo(*outline, pts, pts + 1, pts + 2, transform);
467 _outlineEnd(*outline);
469 outline->fillRule = sdata->fillRule();
470 shape->outline = outline;
472 shape->fastTrack = (!hasComposite && _axisAlignedRect(shape->outline));
477 /************************************************************************/
478 /* External Class Implementation */
479 /************************************************************************/
481 bool shapePrepare(SwShape* shape, const Shape* sdata, const Matrix* transform, const SwBBox& clipRegion, SwBBox& renderRegion, SwMpool* mpool, unsigned tid, bool hasComposite)
483 if (!_genOutline(shape, sdata, transform, mpool, tid, hasComposite)) return false;
484 if (!mathUpdateOutlineBBox(shape->outline, clipRegion, renderRegion, shape->fastTrack)) return false;
486 //Keep it for Rasterization Region
487 shape->bbox = renderRegion;
490 if (renderRegion.max.x - renderRegion.min.x < 1 && renderRegion.max.y - renderRegion.min.y < 1) return false;
493 if (renderRegion.min.x >= clipRegion.max.x || renderRegion.min.y >= clipRegion.max.y ||
494 renderRegion.max.x <= clipRegion.min.x || renderRegion.max.y <= clipRegion.min.y) return false;
500 bool shapePrepared(const SwShape* shape)
502 return shape->rle ? true : false;
506 bool shapeGenRle(SwShape* shape, TVG_UNUSED const Shape* sdata, bool antiAlias)
508 //FIXME: Should we draw it?
510 //if (shape.outline->opened) return true;
512 //Case A: Fast Track Rectangle Drawing
513 if (shape->fastTrack) return true;
515 //Case B: Normal Shape RLE Drawing
516 if ((shape->rle = rleRender(shape->rle, shape->outline, shape->bbox, antiAlias))) return true;
522 void shapeDelOutline(SwShape* shape, SwMpool* mpool, uint32_t tid)
524 mpoolRetOutline(mpool, tid);
525 shape->outline = nullptr;
529 void shapeReset(SwShape* shape)
531 rleReset(shape->rle);
532 rleReset(shape->strokeRle);
533 shape->fastTrack = false;
538 void shapeFree(SwShape* shape)
544 rleFree(shape->strokeRle);
545 strokeFree(shape->stroke);
550 void shapeDelStroke(SwShape* shape)
552 if (!shape->stroke) return;
553 rleFree(shape->strokeRle);
554 shape->strokeRle = nullptr;
555 strokeFree(shape->stroke);
556 shape->stroke = nullptr;
560 void shapeResetStroke(SwShape* shape, const Shape* sdata, const Matrix* transform)
562 if (!shape->stroke) shape->stroke = static_cast<SwStroke*>(calloc(1, sizeof(SwStroke)));
563 auto stroke = shape->stroke;
566 strokeReset(stroke, sdata, transform);
567 rleReset(shape->strokeRle);
571 bool shapeGenStrokeRle(SwShape* shape, const Shape* sdata, const Matrix* transform, const SwBBox& clipRegion, SwBBox& renderRegion, SwMpool* mpool, unsigned tid)
573 SwOutline* shapeOutline = nullptr;
574 SwOutline* strokeOutline = nullptr;
575 bool freeOutline = false;
579 if (sdata->strokeDash(nullptr) > 0) {
580 shapeOutline = _genDashOutline(sdata, transform);
581 if (!shapeOutline) return false;
583 //Normal Style stroke
585 if (!shape->outline) {
586 if (!_genOutline(shape, sdata, transform, mpool, tid, false)) return false;
588 shapeOutline = shape->outline;
591 if (!strokeParseOutline(shape->stroke, *shapeOutline)) {
596 strokeOutline = strokeExportOutline(shape->stroke, mpool, tid);
598 if (!mathUpdateOutlineBBox(strokeOutline, clipRegion, renderRegion, false)) {
603 shape->strokeRle = rleRender(shape->strokeRle, strokeOutline, renderRegion, true);
607 if (shapeOutline->cntrs) free(shapeOutline->cntrs);
608 if (shapeOutline->pts) free(shapeOutline->pts);
609 if (shapeOutline->types) free(shapeOutline->types);
610 if (shapeOutline->closed) free(shapeOutline->closed);
613 mpoolRetStrokeOutline(mpool, tid);
619 bool shapeGenFillColors(SwShape* shape, const Fill* fill, const Matrix* transform, SwSurface* surface, uint32_t opacity, bool ctable)
621 return fillGenColorTable(shape->fill, fill, transform, surface, opacity, ctable);
625 bool shapeGenStrokeFillColors(SwShape* shape, const Fill* fill, const Matrix* transform, SwSurface* surface, uint32_t opacity, bool ctable)
627 return fillGenColorTable(shape->stroke->fill, fill, transform, surface, opacity, ctable);
631 void shapeResetFill(SwShape* shape)
634 shape->fill = static_cast<SwFill*>(calloc(1, sizeof(SwFill)));
635 if (!shape->fill) return;
637 fillReset(shape->fill);
641 void shapeResetStrokeFill(SwShape* shape)
643 if (!shape->stroke->fill) {
644 shape->stroke->fill = static_cast<SwFill*>(calloc(1, sizeof(SwFill)));
645 if (!shape->stroke->fill) return;
647 fillReset(shape->stroke->fill);
651 void shapeDelFill(SwShape* shape)
653 if (!shape->fill) return;
654 fillFree(shape->fill);
655 shape->fill = nullptr;
659 void shapeDelStrokeFill(SwShape* shape)
661 if (!shape->stroke->fill) return;
662 fillFree(shape->stroke->fill);
663 shape->stroke->fill = nullptr;