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