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 #ifndef _TVG_SW_SHAPE_H_
23 #define _TVG_SW_SHAPE_H_
24
25 #include "tvgSwCommon.h"
26
27 /************************************************************************/
28 /* Internal Class Implementation                                        */
29 /************************************************************************/
30
31 struct Line
32 {
33     Point pt1;
34     Point pt2;
35 };
36
37
38 static SwPoint _transform(const Point* to, const Matrix* transform)
39 {
40     if (!transform) return {TO_SWCOORD(to->x), TO_SWCOORD(to->y)};
41
42     auto tx = round(to->x * transform->e11 + to->y * transform->e12 + transform->e13);
43     auto ty = round(to->x * transform->e21 + to->y * transform->e22 + transform->e23);
44
45     return {TO_SWCOORD(tx), TO_SWCOORD(ty)};
46 }
47
48
49 static float _lineLength(const Point& pt1, const Point& pt2)
50 {
51     /* approximate sqrt(x*x + y*y) using alpha max plus beta min algorithm.
52        With alpha = 1, beta = 3/8, giving results with the largest error less
53        than 7% compared to the exact value. */
54     Point diff = {pt2.x - pt1.x, pt2.y - pt1.y};
55     if (diff.x < 0) diff.x = -diff.x;
56     if (diff.y < 0) diff.y = -diff.y;
57     return (diff.x > diff.y) ? (diff.x + diff.y * 0.375f) : (diff.y + diff.x * 0.375f);
58 }
59
60
61 static void _lineSplitAt(const Line& cur, float at, Line& left, Line& right)
62 {
63     auto len = _lineLength(cur.pt1, cur.pt2);
64     auto dx = ((cur.pt2.x - cur.pt1.x) / len) * at;
65     auto dy = ((cur.pt2.y - cur.pt1.y) / len) * at;
66     left.pt1 = cur.pt1;
67     left.pt2.x = left.pt1.x + dx;
68     left.pt2.y = left.pt1.y + dy;
69     right.pt1 = left.pt2;
70     right.pt2 = cur.pt2;
71 }
72
73
74 static void _growOutlineContour(SwOutline& outline, uint32_t n)
75 {
76     if (outline.reservedCntrsCnt >= outline.cntrsCnt + n) return;
77     outline.reservedCntrsCnt = outline.cntrsCnt + n;
78     outline.cntrs = static_cast<uint32_t*>(realloc(outline.cntrs, outline.reservedCntrsCnt * sizeof(uint32_t)));
79 }
80
81
82 static void _growOutlinePoint(SwOutline& outline, uint32_t n)
83 {
84     if (outline.reservedPtsCnt >= outline.ptsCnt + n) return;
85     outline.reservedPtsCnt = outline.ptsCnt + n;
86     outline.pts = static_cast<SwPoint*>(realloc(outline.pts, outline.reservedPtsCnt * sizeof(SwPoint)));
87     outline.types = static_cast<uint8_t*>(realloc(outline.types, outline.reservedPtsCnt * sizeof(uint8_t)));
88 }
89
90
91 static void _delOutline(SwOutline* outline)
92 {
93     if (!outline) return;
94
95     if (outline->cntrs) free(outline->cntrs);
96     if (outline->pts) free(outline->pts);
97     if (outline->types) free(outline->types);
98     free(outline);
99 }
100
101
102 static void _outlineEnd(SwOutline& outline)
103 {
104     _growOutlineContour(outline, 1);
105     if (outline.ptsCnt > 0) {
106         outline.cntrs[outline.cntrsCnt] = outline.ptsCnt - 1;
107         ++outline.cntrsCnt;
108     }
109 }
110
111
112 static void _outlineMoveTo(SwOutline& outline, const Point* to, const Matrix* transform)
113 {
114     _growOutlinePoint(outline, 1);
115
116     outline.pts[outline.ptsCnt] = _transform(to, transform);
117
118     outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
119
120     if (outline.ptsCnt > 0) {
121         _growOutlineContour(outline, 1);
122         outline.cntrs[outline.cntrsCnt] = outline.ptsCnt - 1;
123         ++outline.cntrsCnt;
124     }
125
126     ++outline.ptsCnt;
127 }
128
129
130 static void _outlineLineTo(SwOutline& outline, const Point* to, const Matrix* transform)
131 {
132     _growOutlinePoint(outline, 1);
133
134     outline.pts[outline.ptsCnt] = _transform(to, transform);
135     outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
136     ++outline.ptsCnt;
137 }
138
139
140 static void _outlineCubicTo(SwOutline& outline, const Point* ctrl1, const Point* ctrl2, const Point* to, const Matrix* transform)
141 {
142     _growOutlinePoint(outline, 3);
143
144     outline.pts[outline.ptsCnt] = _transform(ctrl1, transform);
145     outline.types[outline.ptsCnt] = SW_CURVE_TYPE_CUBIC;
146     ++outline.ptsCnt;
147
148     outline.pts[outline.ptsCnt] = _transform(ctrl2, transform);
149     outline.types[outline.ptsCnt] = SW_CURVE_TYPE_CUBIC;
150     ++outline.ptsCnt;
151
152     outline.pts[outline.ptsCnt] = _transform(to, transform);
153     outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
154     ++outline.ptsCnt;
155 }
156
157
158 static void _outlineClose(SwOutline& outline)
159 {
160     uint32_t i = 0;
161
162     if (outline.cntrsCnt > 0) {
163         i = outline.cntrs[outline.cntrsCnt - 1] + 1;
164     } else {
165         i = 0;   //First Path
166     }
167
168     //Make sure there is at least one point in the current path
169     if (outline.ptsCnt == i) {
170         outline.opened = true;
171         return;
172     }
173
174     //Close the path
175     _growOutlinePoint(outline, 1);
176
177     outline.pts[outline.ptsCnt] = outline.pts[i];
178     outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
179     ++outline.ptsCnt;
180
181     outline.opened = false;
182 }
183
184
185 static void _initBBox(SwBBox& bbox)
186 {
187     bbox.min.x = bbox.min.y = 0;
188     bbox.max.x = bbox.max.y = 0;
189 }
190
191
192 static bool _updateBBox(SwOutline* outline, SwBBox& bbox)
193 {
194     if (!outline) return false;
195
196     auto pt = outline->pts;
197
198     if (outline->ptsCnt <= 0) {
199         _initBBox(bbox);
200         return false;
201     }
202
203     auto xMin = pt->x;
204     auto xMax = pt->x;
205     auto yMin = pt->y;
206     auto yMax = pt->y;
207
208     ++pt;
209
210     for(uint32_t i = 1; i < outline->ptsCnt; ++i, ++pt) {
211         if (xMin > pt->x) xMin = pt->x;
212         if (xMax < pt->x) xMax = pt->x;
213         if (yMin > pt->y) yMin = pt->y;
214         if (yMax < pt->y) yMax = pt->y;
215     }
216     bbox.min.x = xMin >> 6;
217     bbox.max.x = (xMax + 63) >> 6;
218     bbox.min.y = yMin >> 6;
219     bbox.max.y = (yMax + 63) >> 6;
220
221     if (xMax - xMin < 1 && yMax - yMin < 1) return false;
222
223     return true;
224 }
225
226
227 static bool _checkValid(const SwOutline* outline, const SwBBox& bbox, const SwSize& clip)
228 {
229     if (outline->ptsCnt == 0 || outline->cntrsCnt <= 0) return false;
230
231     //Check boundary
232     if (bbox.min.x >= clip.w || bbox.min.y >= clip.h || bbox.max.x <= 0 || bbox.max.y <= 0) return false;
233
234     return true;
235 }
236
237
238 static void _dashLineTo(SwDashStroke& dash, const Point* to, const Matrix* transform)
239 {
240     _growOutlinePoint(*dash.outline, dash.outline->ptsCnt >> 1);
241     _growOutlineContour(*dash.outline, dash.outline->cntrsCnt >> 1);
242
243     Line cur = {dash.ptCur, *to};
244     auto len = _lineLength(cur.pt1, cur.pt2);
245
246     if (len < dash.curLen) {
247         dash.curLen -= len;
248         if (!dash.curOpGap) {
249             _outlineMoveTo(*dash.outline, &dash.ptCur, transform);
250             _outlineLineTo(*dash.outline, to, transform);
251         }
252     } else {
253         while (len > dash.curLen) {
254             len -= dash.curLen;
255             Line left, right;
256             _lineSplitAt(cur, dash.curLen, left, right);;
257             dash.curIdx = (dash.curIdx + 1) % dash.cnt;
258             if (!dash.curOpGap) {
259                 _outlineMoveTo(*dash.outline, &left.pt1, transform);
260                 _outlineLineTo(*dash.outline, &left.pt2, transform);
261             }
262             dash.curLen = dash.pattern[dash.curIdx];
263             dash.curOpGap = !dash.curOpGap;
264             cur = right;
265             dash.ptCur = cur.pt1;
266         }
267         //leftovers
268         dash.curLen -= len;
269         if (!dash.curOpGap) {
270             _outlineMoveTo(*dash.outline, &cur.pt1, transform);
271             _outlineLineTo(*dash.outline, &cur.pt2, transform);
272         }
273         if (dash.curLen < 1) {
274             //move to next dash
275             dash.curIdx = (dash.curIdx + 1) % dash.cnt;
276             dash.curLen = dash.pattern[dash.curIdx];
277             dash.curOpGap = !dash.curOpGap;
278         }
279     }
280     dash.ptCur = *to;
281 }
282
283
284 static void _dashCubicTo(SwDashStroke& dash, const Point* ctrl1, const Point* ctrl2, const Point* to, const Matrix* transform)
285 {
286     _growOutlinePoint(*dash.outline, dash.outline->ptsCnt >> 1);
287     _growOutlineContour(*dash.outline, dash.outline->cntrsCnt >> 1);
288
289     Bezier cur = { dash.ptCur, *ctrl1, *ctrl2, *to};
290     auto len = bezLength(cur);
291
292     if (len < dash.curLen) {
293         dash.curLen -= len;
294         if (!dash.curOpGap) {
295             _outlineMoveTo(*dash.outline, &dash.ptCur, transform);
296             _outlineCubicTo(*dash.outline, ctrl1, ctrl2, to, transform);
297         }
298     } else {
299         while (len > dash.curLen) {
300             Bezier left, right;
301             len -= dash.curLen;
302             bezSplitAt(cur, dash.curLen, left, right);
303             dash.curIdx = (dash.curIdx + 1) % dash.cnt;
304             if (!dash.curOpGap) {
305                 _outlineMoveTo(*dash.outline, &left.start, transform);
306                 _outlineCubicTo(*dash.outline, &left.ctrl1, &left.ctrl2, &left.end, transform);
307             }
308             dash.curLen = dash.pattern[dash.curIdx];
309             dash.curOpGap = !dash.curOpGap;
310             cur = right;
311             dash.ptCur = right.start;
312         }
313         //leftovers
314         dash.curLen -= len;
315         if (!dash.curOpGap) {
316             _outlineMoveTo(*dash.outline, &cur.start, transform);
317             _outlineCubicTo(*dash.outline, &cur.ctrl1, &cur.ctrl2, &cur.end, transform);
318         }
319         if (dash.curLen < 1) {
320             //move to next dash
321             dash.curIdx = (dash.curIdx + 1) % dash.cnt;
322             dash.curLen = dash.pattern[dash.curIdx];
323             dash.curOpGap = !dash.curOpGap;
324         }
325     }
326     dash.ptCur = *to;
327 }
328
329
330 SwOutline* _genDashOutline(const Shape* sdata, const Matrix* transform)
331 {
332     const PathCommand* cmds = nullptr;
333     auto cmdCnt = sdata->pathCommands(&cmds);
334
335     const Point* pts = nullptr;
336     auto ptsCnt = sdata->pathCoords(&pts);
337
338     //No actual shape data
339     if (cmdCnt == 0 || ptsCnt == 0) return nullptr;
340
341     SwDashStroke dash;
342     dash.curIdx = 0;
343     dash.curLen = 0;
344     dash.ptStart = {0, 0};
345     dash.ptCur = {0, 0};
346     dash.curOpGap = false;
347
348     const float* pattern;
349     dash.cnt = sdata->strokeDash(&pattern);
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 shapeGenRle(SwShape* shape, TVG_UNUSED const Shape* sdata, const SwSize& clip, bool antiAlias)
463 {
464     //FIXME: Should we draw it?
465     //Case: Stroke Line
466     //if (shape.outline->opened) return true;
467
468     //Case A: Fast Track Rectangle Drawing
469     if ((shape->rect = _fastTrack(shape->outline))) return true;
470     //Case B: Normale Shape RLE Drawing
471     if ((shape->rle = rleRender(shape->outline, shape->bbox, clip, antiAlias))) return true;
472
473     return false;
474 }
475
476
477 void shapeDelOutline(SwShape* shape)
478 {
479     auto outline = shape->outline;
480     _delOutline(outline);
481     shape->outline = nullptr;
482 }
483
484
485 void shapeReset(SwShape* shape)
486 {
487     shapeDelOutline(shape);
488     rleFree(shape->rle);
489     shape->rle = nullptr;
490     shape->rect = false;
491     _initBBox(shape->bbox);
492 }
493
494
495 bool shapeGenOutline(SwShape* shape, const Shape* sdata, const Matrix* transform)
496 {
497     const PathCommand* cmds = nullptr;
498     auto cmdCnt = sdata->pathCommands(&cmds);
499
500     const Point* pts = nullptr;
501     auto ptsCnt = sdata->pathCoords(&pts);
502
503     //No actual shape data
504     if (cmdCnt == 0 || ptsCnt == 0) return false;
505
506     //smart reservation
507     auto outlinePtsCnt = 0;
508     auto outlineCntrsCnt = 0;
509
510     for (uint32_t i = 0; i < cmdCnt; ++i) {
511         switch(*(cmds + i)) {
512             case PathCommand::Close: {
513                 ++outlinePtsCnt;
514                 break;
515             }
516             case PathCommand::MoveTo: {
517                 ++outlineCntrsCnt;
518                 ++outlinePtsCnt;
519                 break;
520             }
521             case PathCommand::LineTo: {
522                 ++outlinePtsCnt;
523                 break;
524             }
525             case PathCommand::CubicTo: {
526                 outlinePtsCnt += 3;
527                 break;
528             }
529         }
530     }
531
532     ++outlinePtsCnt;    //for close
533     ++outlineCntrsCnt;  //for end
534
535     auto outline = shape->outline;
536     if (!outline) outline = static_cast<SwOutline*>(calloc(1, sizeof(SwOutline)));
537     outline->opened = true;
538
539     _growOutlinePoint(*outline, outlinePtsCnt);
540     _growOutlineContour(*outline, outlineCntrsCnt);
541
542     auto closed = false;
543
544     //Generate Outlines
545     while (cmdCnt-- > 0) {
546         switch(*cmds) {
547             case PathCommand::Close: {
548                 _outlineClose(*outline);
549                 closed = true;
550                 break;
551             }
552             case PathCommand::MoveTo: {
553                 _outlineMoveTo(*outline, pts, transform);
554                 ++pts;
555                 break;
556             }
557             case PathCommand::LineTo: {
558                 _outlineLineTo(*outline, pts, transform);
559                 ++pts;
560                 break;
561             }
562             case PathCommand::CubicTo: {
563                 _outlineCubicTo(*outline, pts, pts + 1, pts + 2, transform);
564                 pts += 3;
565                 break;
566             }
567         }
568         ++cmds;
569     }
570
571     _outlineEnd(*outline);
572
573     if (closed) outline->opened = false;
574
575     //FIXME:
576     //outline->flags = SwOutline::FillRule::Winding;
577
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
624     //Dash Style Stroke
625     if (sdata->strokeDash(nullptr) > 0) {
626         shapeOutline = _genDashOutline(sdata, transform);
627         if (!shapeOutline) return false;
628     //Normal Style stroke
629     } else {
630         if (!shape->outline) {
631             if (!shapeGenOutline(shape, sdata, transform)) return false;
632         }
633         shapeOutline = shape->outline;
634     }
635
636     if (!strokeParseOutline(shape->stroke, *shapeOutline)) return false;
637
638     auto strokeOutline = strokeExportOutline(shape->stroke);
639     if (!strokeOutline) return false;
640
641     SwBBox bbox;
642     _updateBBox(strokeOutline, bbox);
643
644     if (!_checkValid(strokeOutline, bbox, clip)) return false;
645
646     shape->strokeRle = rleRender(strokeOutline, bbox, clip, true);
647
648     _delOutline(strokeOutline);
649
650     return true;
651 }
652
653
654 bool shapeGenFillColors(SwShape* shape, const Fill* fill, const Matrix* transform, SwSurface* surface, bool ctable)
655 {
656     return fillGenColorTable(shape->fill, fill, transform, surface, ctable);
657 }
658
659
660 void shapeResetFill(SwShape* shape)
661 {
662     if (!shape->fill) {
663         shape->fill = static_cast<SwFill*>(calloc(1, sizeof(SwFill)));
664         if (!shape->fill) return;
665     }
666     fillReset(shape->fill);
667 }
668
669
670 void shapeDelFill(SwShape* shape)
671 {
672     if (!shape->fill) return;
673     fillFree(shape->fill);
674     shape->fill = nullptr;
675 }
676
677
678 #endif /* _TVG_SW_SHAPE_H_ */