sw_engine: overlaping stroke cap
[platform/core/graphics/tizenvg.git] / src / lib / sw_engine / tvgSwShape.cpp
1 /*
2  * Copyright (c) 2020-2021 Samsung Electronics Co., Ltd. All rights reserved.
3
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:
10
11  * The above copyright notice and this permission notice shall be included in all
12  * copies or substantial portions of the Software.
13
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
20  * SOFTWARE.
21  */
22 #include "tvgSwCommon.h"
23 #include "tvgBezier.h"
24 #include <float.h>
25
26 /************************************************************************/
27 /* Internal Class Implementation                                        */
28 /************************************************************************/
29
30 struct Line
31 {
32     Point pt1;
33     Point pt2;
34 };
35
36
37 static float _lineLength(const Point& pt1, const Point& pt2)
38 {
39     /* approximate sqrt(x*x + y*y) using alpha max plus beta min algorithm.
40        With alpha = 1, beta = 3/8, giving results with the largest error less
41        than 7% compared to the exact value. */
42     Point diff = {pt2.x - pt1.x, pt2.y - pt1.y};
43     if (diff.x < 0) diff.x = -diff.x;
44     if (diff.y < 0) diff.y = -diff.y;
45     return (diff.x > diff.y) ? (diff.x + diff.y * 0.375f) : (diff.y + diff.x * 0.375f);
46 }
47
48
49 static void _lineSplitAt(const Line& cur, float at, Line& left, Line& right)
50 {
51     auto len = _lineLength(cur.pt1, cur.pt2);
52     auto dx = ((cur.pt2.x - cur.pt1.x) / len) * at;
53     auto dy = ((cur.pt2.y - cur.pt1.y) / len) * at;
54     left.pt1 = cur.pt1;
55     left.pt2.x = left.pt1.x + dx;
56     left.pt2.y = left.pt1.y + dy;
57     right.pt1 = left.pt2;
58     right.pt2 = cur.pt2;
59 }
60
61
62 static bool _growOutlineContour(SwOutline& outline, uint32_t n)
63 {
64     if (outline.reservedCntrsCnt >= outline.cntrsCnt + n) return false;
65     outline.reservedCntrsCnt = outline.cntrsCnt + n;
66     outline.cntrs = static_cast<uint16_t*>(realloc(outline.cntrs, outline.reservedCntrsCnt * sizeof(uint16_t)));
67     return true;
68 }
69
70
71 static void _reserveOutlineClose(SwOutline& outline)
72 {
73     //Dash outlines are always opened.
74     //Only normal outlines use this information, it sholud be same to their contour counts.
75     if (outline.closed) free(outline.closed);
76     outline.closed = static_cast<bool*>(calloc(outline.reservedCntrsCnt, sizeof(bool)));
77 }
78
79
80 static void _resetOutlineClose(SwOutline& outline)
81 {
82     memset(outline.closed, 0x0, outline.reservedCntrsCnt * sizeof(bool));
83 }
84
85
86 static void _growOutlinePoint(SwOutline& outline, uint32_t n)
87 {
88     if (outline.reservedPtsCnt >= outline.ptsCnt + n) return;
89     outline.reservedPtsCnt = outline.ptsCnt + n;
90     outline.pts = static_cast<SwPoint*>(realloc(outline.pts, outline.reservedPtsCnt * sizeof(SwPoint)));
91     outline.types = static_cast<uint8_t*>(realloc(outline.types, outline.reservedPtsCnt * sizeof(uint8_t)));
92 }
93
94
95 static void _outlineEnd(SwOutline& outline)
96 {
97     if (outline.ptsCnt == 0) return;
98
99     _growOutlineContour(outline, 1);
100     outline.cntrs[outline.cntrsCnt] = outline.ptsCnt - 1;
101     ++outline.cntrsCnt;
102 }
103
104
105 static void _outlineMoveTo(SwOutline& outline, const Point* to, const Matrix* transform)
106 {
107     _growOutlinePoint(outline, 1);
108
109     outline.pts[outline.ptsCnt] = mathTransform(to, transform);
110     outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
111
112     if (outline.ptsCnt > 0) {
113         _growOutlineContour(outline, 1);
114         outline.cntrs[outline.cntrsCnt] = outline.ptsCnt - 1;
115         ++outline.cntrsCnt;
116     }
117
118     ++outline.ptsCnt;
119 }
120
121
122 static void _outlineLineTo(SwOutline& outline, const Point* to, const Matrix* transform)
123 {
124     _growOutlinePoint(outline, 1);
125
126     outline.pts[outline.ptsCnt] = mathTransform(to, transform);
127     outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
128     ++outline.ptsCnt;
129 }
130
131
132 static void _outlineCubicTo(SwOutline& outline, const Point* ctrl1, const Point* ctrl2, const Point* to, const Matrix* transform)
133 {
134     _growOutlinePoint(outline, 3);
135
136     outline.pts[outline.ptsCnt] = mathTransform(ctrl1, transform);
137     outline.types[outline.ptsCnt] = SW_CURVE_TYPE_CUBIC;
138     ++outline.ptsCnt;
139
140     outline.pts[outline.ptsCnt] = mathTransform(ctrl2, transform);
141     outline.types[outline.ptsCnt] = SW_CURVE_TYPE_CUBIC;
142     ++outline.ptsCnt;
143
144     outline.pts[outline.ptsCnt] = mathTransform(to, transform);
145     outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
146     ++outline.ptsCnt;
147 }
148
149
150 static void _outlineClose(SwOutline& outline)
151 {
152     uint32_t i = 0;
153
154     if (outline.cntrsCnt > 0) {
155         i = outline.cntrs[outline.cntrsCnt - 1] + 1;
156     } else {
157         i = 0;   //First Path
158     }
159
160     //Make sure there is at least one point in the current path
161     if (outline.ptsCnt == i) return;
162
163     //Close the path
164     _growOutlinePoint(outline, 1);
165
166     outline.pts[outline.ptsCnt] = outline.pts[i];
167     outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
168     ++outline.ptsCnt;
169     outline.closed[outline.cntrsCnt] = true;
170 }
171
172
173 static void _dashLineTo(SwDashStroke& dash, const Point* to, const Matrix* transform)
174 {
175     _growOutlinePoint(*dash.outline, dash.outline->ptsCnt >> 1);
176     _growOutlineContour(*dash.outline, dash.outline->cntrsCnt >> 1);
177
178     Line cur = {dash.ptCur, *to};
179     auto len = _lineLength(cur.pt1, cur.pt2);
180
181     if (len < dash.curLen) {
182         dash.curLen -= len;
183         if (!dash.curOpGap) {
184             _outlineMoveTo(*dash.outline, &dash.ptCur, transform);
185             _outlineLineTo(*dash.outline, to, transform);
186         }
187     } else {
188         while (len > dash.curLen) {
189             len -= dash.curLen;
190             Line left, right;
191             _lineSplitAt(cur, dash.curLen, left, right);;
192             dash.curIdx = (dash.curIdx + 1) % dash.cnt;
193             if (!dash.curOpGap) {
194                 _outlineMoveTo(*dash.outline, &left.pt1, transform);
195                 _outlineLineTo(*dash.outline, &left.pt2, transform);
196             }
197             dash.curLen = dash.pattern[dash.curIdx];
198             dash.curOpGap = !dash.curOpGap;
199             cur = right;
200             dash.ptCur = cur.pt1;
201         }
202         //leftovers
203         dash.curLen -= len;
204         if (!dash.curOpGap) {
205             _outlineMoveTo(*dash.outline, &cur.pt1, transform);
206             _outlineLineTo(*dash.outline, &cur.pt2, transform);
207         }
208         if (dash.curLen < 1 && TO_SWCOORD(len) > 1) {
209             //move to next dash
210             dash.curIdx = (dash.curIdx + 1) % dash.cnt;
211             dash.curLen = dash.pattern[dash.curIdx];
212             dash.curOpGap = !dash.curOpGap;
213         }
214     }
215     dash.ptCur = *to;
216 }
217
218
219 static void _dashCubicTo(SwDashStroke& dash, const Point* ctrl1, const Point* ctrl2, const Point* to, const Matrix* transform)
220 {
221     _growOutlinePoint(*dash.outline, dash.outline->ptsCnt >> 1);
222     _growOutlineContour(*dash.outline, dash.outline->cntrsCnt >> 1);
223
224     Bezier cur = {dash.ptCur, *ctrl1, *ctrl2, *to};
225     auto len = bezLength(cur);
226
227     if (len < dash.curLen) {
228         dash.curLen -= len;
229         if (!dash.curOpGap) {
230             _outlineMoveTo(*dash.outline, &dash.ptCur, transform);
231             _outlineCubicTo(*dash.outline, ctrl1, ctrl2, to, transform);
232         }
233     } else {
234         while (len > dash.curLen) {
235             Bezier left, right;
236             len -= dash.curLen;
237             bezSplitAt(cur, dash.curLen, left, right);
238             if (!dash.curOpGap) {
239                 // leftovers from a previous command don't require moveTo
240                 if (dash.pattern[dash.curIdx] - dash.curLen < FLT_EPSILON) {
241                     _outlineMoveTo(*dash.outline, &left.start, transform);
242                 }
243                 _outlineCubicTo(*dash.outline, &left.ctrl1, &left.ctrl2, &left.end, transform);
244             }
245             dash.curIdx = (dash.curIdx + 1) % dash.cnt;
246             dash.curLen = dash.pattern[dash.curIdx];
247             dash.curOpGap = !dash.curOpGap;
248             cur = right;
249             dash.ptCur = right.start;
250         }
251         //leftovers
252         dash.curLen -= len;
253         if (!dash.curOpGap) {
254             _outlineMoveTo(*dash.outline, &cur.start, transform);
255             _outlineCubicTo(*dash.outline, &cur.ctrl1, &cur.ctrl2, &cur.end, transform);
256         }
257         if (dash.curLen < 1 && TO_SWCOORD(len) > 1) {
258             //move to next dash
259             dash.curIdx = (dash.curIdx + 1) % dash.cnt;
260             dash.curLen = dash.pattern[dash.curIdx];
261             dash.curOpGap = !dash.curOpGap;
262         }
263     }
264     dash.ptCur = *to;
265 }
266
267
268 static SwOutline* _genDashOutline(const Shape* sdata, const Matrix* transform)
269 {
270     const PathCommand* cmds = nullptr;
271     auto cmdCnt = sdata->pathCommands(&cmds);
272
273     const Point* pts = nullptr;
274     auto ptsCnt = sdata->pathCoords(&pts);
275
276     //No actual shape data
277     if (cmdCnt == 0 || ptsCnt == 0) return nullptr;
278
279     SwDashStroke dash;
280     dash.curIdx = 0;
281     dash.curLen = 0;
282     dash.ptStart = {0, 0};
283     dash.ptCur = {0, 0};
284     dash.curOpGap = false;
285
286     const float* pattern;
287     dash.cnt = sdata->strokeDash(&pattern);
288     if (dash.cnt == 0) return nullptr;
289
290     //OPTMIZE ME: Use mempool???
291     dash.pattern = const_cast<float*>(pattern);
292     dash.outline = static_cast<SwOutline*>(calloc(1, sizeof(SwOutline)));
293
294     //smart reservation
295     auto outlinePtsCnt = 0;
296     auto outlineCntrsCnt = 0;
297
298     for (uint32_t i = 0; i < cmdCnt; ++i) {
299         switch(*(cmds + i)) {
300             case PathCommand::Close: {
301                 ++outlinePtsCnt;
302                 break;
303             }
304             case PathCommand::MoveTo: {
305                 ++outlineCntrsCnt;
306                 ++outlinePtsCnt;
307                 break;
308             }
309             case PathCommand::LineTo: {
310                 ++outlinePtsCnt;
311                 break;
312             }
313             case PathCommand::CubicTo: {
314                 outlinePtsCnt += 3;
315                 break;
316             }
317         }
318     }
319
320     ++outlinePtsCnt;    //for close
321     ++outlineCntrsCnt;  //for end
322
323     //No idea exact count.... Reserve Approximitely 20x...
324     _growOutlinePoint(*dash.outline, outlinePtsCnt * 20);
325     _growOutlineContour(*dash.outline, outlineCntrsCnt * 20);
326
327     while (cmdCnt-- > 0) {
328         switch(*cmds) {
329             case PathCommand::Close: {
330                 _dashLineTo(dash, &dash.ptStart, transform);
331                 break;
332             }
333             case PathCommand::MoveTo: {
334                 //reset the dash
335                 dash.curIdx = 0;
336                 dash.curLen = *dash.pattern;
337                 dash.curOpGap = false;
338                 dash.ptStart = dash.ptCur = *pts;
339                 ++pts;
340                 break;
341             }
342             case PathCommand::LineTo: {
343                 _dashLineTo(dash, pts, transform);
344                 ++pts;
345                 break;
346             }
347             case PathCommand::CubicTo: {
348                 _dashCubicTo(dash, pts, pts + 1, pts + 2, transform);
349                 pts += 3;
350                 break;
351             }
352         }
353         ++cmds;
354     }
355
356     _outlineEnd(*dash.outline);
357
358     return dash.outline;
359 }
360
361
362 static bool _fastTrack(const SwOutline* outline)
363 {
364     //Fast Track: Othogonal rectangle?
365     if (outline->ptsCnt != 5) return false;
366
367     auto pt1 = outline->pts + 0;
368     auto pt2 = outline->pts + 1;
369     auto pt3 = outline->pts + 2;
370     auto pt4 = outline->pts + 3;
371
372     auto a = SwPoint{pt1->x, pt3->y};
373     auto b = SwPoint{pt3->x, pt1->y};
374
375     if ((*pt2 == a && *pt4 == b) || (*pt2 == b && *pt4 == a)) return true;
376
377     return false;
378 }
379
380
381
382 static bool _genOutline(SwShape* shape, const Shape* sdata, const Matrix* transform, SwMpool* mpool, unsigned tid)
383 {
384     const PathCommand* cmds = nullptr;
385     auto cmdCnt = sdata->pathCommands(&cmds);
386
387     const Point* pts = nullptr;
388     auto ptsCnt = sdata->pathCoords(&pts);
389
390     //No actual shape data
391     if (cmdCnt == 0 || ptsCnt == 0) return false;
392
393     //smart reservation
394     auto outlinePtsCnt = 0;
395     auto outlineCntrsCnt = 0;
396     auto closeCnt = 0;
397
398     for (uint32_t i = 0; i < cmdCnt; ++i) {
399         switch(*(cmds + i)) {
400             case PathCommand::Close: {
401                 ++outlinePtsCnt;
402                 ++closeCnt;
403                 break;
404             }
405             case PathCommand::MoveTo: {
406                 ++outlineCntrsCnt;
407                 ++outlinePtsCnt;
408                 break;
409             }
410             case PathCommand::LineTo: {
411                 ++outlinePtsCnt;
412                 break;
413             }
414             case PathCommand::CubicTo: {
415                 outlinePtsCnt += 3;
416                 break;
417             }
418         }
419     }
420
421     if (static_cast<uint32_t>(outlinePtsCnt - closeCnt) > ptsCnt) {
422         TVGERR("SW_ENGINE", "Wrong a pair of the commands & points - required(%d), current(%d)", outlinePtsCnt - closeCnt, ptsCnt);
423         return false;
424     }
425
426     ++outlinePtsCnt;    //for close
427     ++outlineCntrsCnt;  //for end
428
429     shape->outline = mpoolReqOutline(mpool, tid);
430     auto outline = shape->outline;
431
432     _growOutlinePoint(*outline, outlinePtsCnt);
433
434      if (_growOutlineContour(*outline, outlineCntrsCnt)) {
435         _reserveOutlineClose(*outline);
436     } else {
437         _resetOutlineClose(*outline);
438     }
439
440     //Generate Outlines
441     while (cmdCnt-- > 0) {
442         switch(*cmds) {
443             case PathCommand::Close: {
444                 _outlineClose(*outline);
445                 break;
446             }
447             case PathCommand::MoveTo: {
448                 _outlineMoveTo(*outline, pts, transform);
449                 ++pts;
450                 break;
451             }
452             case PathCommand::LineTo: {
453                 _outlineLineTo(*outline, pts, transform);
454                 ++pts;
455                 break;
456             }
457             case PathCommand::CubicTo: {
458                 _outlineCubicTo(*outline, pts, pts + 1, pts + 2, transform);
459                 pts += 3;
460                 break;
461             }
462         }
463         ++cmds;
464     }
465
466     _outlineEnd(*outline);
467
468     outline->fillRule = sdata->fillRule();
469     shape->outline = outline;
470
471     return true;
472 }
473
474
475 /************************************************************************/
476 /* External Class Implementation                                        */
477 /************************************************************************/
478
479 bool shapePrepare(SwShape* shape, const Shape* sdata, const Matrix* transform,  const SwBBox& clipRegion, SwBBox& renderRegion, SwMpool* mpool, unsigned tid)
480 {
481     if (!_genOutline(shape, sdata, transform, mpool, tid)) return false;
482     if (!mathUpdateOutlineBBox(shape->outline, clipRegion, renderRegion)) return false;
483
484     //Keep it for Rasterization Region
485     shape->bbox = renderRegion;
486
487     //Check valid region
488     if (renderRegion.max.x - renderRegion.min.x < 1 && renderRegion.max.y - renderRegion.min.y < 1) return false;
489
490     //Check boundary
491     if (renderRegion.min.x >= clipRegion.max.x || renderRegion.min.y >= clipRegion.max.y ||
492         renderRegion.max.x <= clipRegion.min.x || renderRegion.max.y <= clipRegion.min.y) return false;
493
494     return true;
495 }
496
497
498 bool shapePrepared(const SwShape* shape)
499 {
500     return shape->rle ? true : false;
501 }
502
503
504 bool shapeGenRle(SwShape* shape, TVG_UNUSED const Shape* sdata, bool antiAlias, bool hasComposite)
505 {
506     //FIXME: Should we draw it?
507     //Case: Stroke Line
508     //if (shape.outline->opened) return true;
509
510     //Case A: Fast Track Rectangle Drawing
511     if (!hasComposite && (shape->rect = _fastTrack(shape->outline))) return true;
512     //Case B: Normale Shape RLE Drawing
513     if ((shape->rle = rleRender(shape->rle, shape->outline, shape->bbox, antiAlias))) return true;
514
515     return false;
516 }
517
518
519 void shapeDelOutline(SwShape* shape, SwMpool* mpool, uint32_t tid)
520 {
521     mpoolRetOutline(mpool, tid);
522     shape->outline = nullptr;
523 }
524
525
526 void shapeReset(SwShape* shape)
527 {
528     rleReset(shape->rle);
529     rleReset(shape->strokeRle);
530     shape->rect = false;
531     shape->bbox.reset();
532 }
533
534
535 void shapeFree(SwShape* shape)
536 {
537     rleFree(shape->rle);
538     shapeDelFill(shape);
539
540     if (shape->stroke) {
541         rleFree(shape->strokeRle);
542         strokeFree(shape->stroke);
543     }
544 }
545
546
547 void shapeDelStroke(SwShape* shape)
548 {
549     if (!shape->stroke) return;
550     rleFree(shape->strokeRle);
551     shape->strokeRle = nullptr;
552     strokeFree(shape->stroke);
553     shape->stroke = nullptr;
554 }
555
556
557 void shapeResetStroke(SwShape* shape, const Shape* sdata, const Matrix* transform)
558 {
559     if (!shape->stroke) shape->stroke = static_cast<SwStroke*>(calloc(1, sizeof(SwStroke)));
560     auto stroke = shape->stroke;
561     if (!stroke) return;
562
563     strokeReset(stroke, sdata, transform);
564     rleReset(shape->strokeRle);
565 }
566
567
568 bool shapeGenStrokeRle(SwShape* shape, const Shape* sdata, const Matrix* transform, const SwBBox& clipRegion, SwBBox& renderRegion, SwMpool* mpool, unsigned tid)
569 {
570     SwOutline* shapeOutline = nullptr;
571     SwOutline* strokeOutline = nullptr;
572     bool freeOutline = false;
573     bool ret = true;
574
575     //Dash Style Stroke
576     if (sdata->strokeDash(nullptr) > 0) {
577         shapeOutline = _genDashOutline(sdata, transform);
578         if (!shapeOutline) return false;
579         freeOutline = true;
580     //Normal Style stroke
581     } else {
582         if (!shape->outline) {
583             if (!_genOutline(shape, sdata, transform, mpool, tid)) return false;
584         }
585         shapeOutline = shape->outline;
586     }
587
588     if (!strokeParseOutline(shape->stroke, *shapeOutline)) {
589         ret = false;
590         goto fail;
591     }
592
593     strokeOutline = strokeExportOutline(shape->stroke, mpool, tid);
594     if (!strokeOutline) {
595         ret = false;
596         goto fail;
597     }
598
599     if (!mathUpdateOutlineBBox(strokeOutline, clipRegion, renderRegion)) {
600         ret = false;
601         goto fail;
602     }
603
604     shape->strokeRle = rleRender(shape->strokeRle, strokeOutline, renderRegion, true);
605
606 fail:
607     if (freeOutline) {
608         if (shapeOutline->cntrs) free(shapeOutline->cntrs);
609         if (shapeOutline->pts) free(shapeOutline->pts);
610         if (shapeOutline->types) free(shapeOutline->types);
611         if (shapeOutline->closed) free(shapeOutline->closed);
612         free(shapeOutline);
613     }
614     mpoolRetStrokeOutline(mpool, tid);
615
616     return ret;
617 }
618
619
620 bool shapeGenFillColors(SwShape* shape, const Fill* fill, const Matrix* transform, SwSurface* surface, uint32_t opacity, bool ctable)
621 {
622     return fillGenColorTable(shape->fill, fill, transform, surface, opacity, ctable);
623 }
624
625
626 bool shapeGenStrokeFillColors(SwShape* shape, const Fill* fill, const Matrix* transform, SwSurface* surface, uint32_t opacity, bool ctable)
627 {
628     return fillGenColorTable(shape->stroke->fill, fill, transform, surface, opacity, ctable);
629 }
630
631
632 void shapeResetFill(SwShape* shape)
633 {
634     if (!shape->fill) {
635         shape->fill = static_cast<SwFill*>(calloc(1, sizeof(SwFill)));
636         if (!shape->fill) return;
637     }
638     fillReset(shape->fill);
639 }
640
641
642 void shapeResetStrokeFill(SwShape* shape)
643 {
644     if (!shape->stroke->fill) {
645         shape->stroke->fill = static_cast<SwFill*>(calloc(1, sizeof(SwFill)));
646         if (!shape->stroke->fill) return;
647     }
648     fillReset(shape->stroke->fill);
649 }
650
651
652 void shapeDelFill(SwShape* shape)
653 {
654     if (!shape->fill) return;
655     fillFree(shape->fill);
656     shape->fill = nullptr;
657 }
658
659
660 void shapeDelStrokeFill(SwShape* shape)
661 {
662     if (!shape->stroke->fill) return;
663     fillFree(shape->stroke->fill);
664     shape->stroke->fill = nullptr;
665 }