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