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