sw_engine: fix memory leak.
[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     if (dash.cnt == 0) return nullptr;
348
349     //Is it safe to mutual exclusive?
350     dash.pattern = const_cast<float*>(pattern);
351     dash.outline = static_cast<SwOutline*>(calloc(1, sizeof(SwOutline)));
352     dash.outline->opened = true;
353
354     //smart reservation
355     auto outlinePtsCnt = 0;
356     auto outlineCntrsCnt = 0;
357
358     for (uint32_t i = 0; i < cmdCnt; ++i) {
359         switch(*(cmds + i)) {
360             case PathCommand::Close: {
361                 ++outlinePtsCnt;
362                 break;
363             }
364             case PathCommand::MoveTo: {
365                 ++outlineCntrsCnt;
366                 ++outlinePtsCnt;
367                 break;
368             }
369             case PathCommand::LineTo: {
370                 ++outlinePtsCnt;
371                 break;
372             }
373             case PathCommand::CubicTo: {
374                 outlinePtsCnt += 3;
375                 break;
376             }
377         }
378     }
379
380     ++outlinePtsCnt;    //for close
381     ++outlineCntrsCnt;  //for end
382
383     //Reserve Approximitely 20x...
384     _growOutlinePoint(*dash.outline, outlinePtsCnt * 20);
385     _growOutlineContour(*dash.outline, outlineCntrsCnt * 20);
386
387     while (cmdCnt-- > 0) {
388         switch(*cmds) {
389             case PathCommand::Close: {
390                 _dashLineTo(dash, &dash.ptStart, transform);
391                 break;
392             }
393             case PathCommand::MoveTo: {
394                 //reset the dash
395                 dash.curIdx = 0;
396                 dash.curLen = *dash.pattern;
397                 dash.curOpGap = false;
398                 dash.ptStart = dash.ptCur = *pts;
399                 ++pts;
400                 break;
401             }
402             case PathCommand::LineTo: {
403                 _dashLineTo(dash, pts, transform);
404                 ++pts;
405                 break;
406             }
407             case PathCommand::CubicTo: {
408                 _dashCubicTo(dash, pts, pts + 1, pts + 2, transform);
409                 pts += 3;
410                 break;
411             }
412         }
413         ++cmds;
414     }
415
416     _outlineEnd(*dash.outline);
417
418     return dash.outline;
419 }
420
421
422 bool _fastTrack(const SwOutline* outline)
423 {
424     //Fast Track: Othogonal rectangle?
425     if (outline->ptsCnt != 5) return false;
426
427     auto pt1 = outline->pts + 0;
428     auto pt2 = outline->pts + 1;
429     auto pt3 = outline->pts + 2;
430     auto pt4 = outline->pts + 3;
431
432     auto min1 = pt1->y < pt3->y ? pt1 : pt3;
433     auto min2 = pt2->y < pt4->y ? pt2 : pt4;
434     if (min1->y != min2->y) return false;
435
436     SwCoord len1 = pow(pt1->x - pt3->x, 2) + pow(pt1->y - pt3->y, 2);
437     SwCoord len2 = pow(pt2->x - pt4->x, 2) + pow(pt2->y - pt4->y, 2);
438     if (len1 == len2) return true;
439
440     return false;
441 }
442
443
444 /************************************************************************/
445 /* External Class Implementation                                        */
446 /************************************************************************/
447
448 bool shapePrepare(SwShape* shape, const Shape* sdata, const SwSize& clip, const Matrix* transform)
449 {
450     if (!shapeGenOutline(shape, sdata, transform)) return false;
451
452     if (!_updateBBox(shape->outline, shape->bbox)) return false;
453
454     if (!_checkValid(shape->outline, shape->bbox, clip)) return false;
455
456     return true;
457 }
458
459
460 bool shapeGenRle(SwShape* shape, TVG_UNUSED const Shape* sdata, const SwSize& clip, bool antiAlias)
461 {
462     //FIXME: Should we draw it?
463     //Case: Stroke Line
464     //if (shape.outline->opened) return true;
465
466     //Case A: Fast Track Rectangle Drawing
467     if ((shape->rect = _fastTrack(shape->outline))) return true;
468     //Case B: Normale Shape RLE Drawing
469     if ((shape->rle = rleRender(shape->outline, shape->bbox, clip, antiAlias))) return true;
470
471     return false;
472 }
473
474
475 void shapeDelOutline(SwShape* shape)
476 {
477     auto outline = shape->outline;
478     _delOutline(outline);
479     shape->outline = nullptr;
480 }
481
482
483 void shapeReset(SwShape* shape)
484 {
485     shapeDelOutline(shape);
486     rleFree(shape->rle);
487     shape->rle = nullptr;
488     shape->rect = false;
489     _initBBox(shape->bbox);
490 }
491
492
493 bool shapeGenOutline(SwShape* shape, const Shape* sdata, const Matrix* transform)
494 {
495     const PathCommand* cmds = nullptr;
496     auto cmdCnt = sdata->pathCommands(&cmds);
497
498     const Point* pts = nullptr;
499     auto ptsCnt = sdata->pathCoords(&pts);
500
501     //No actual shape data
502     if (cmdCnt == 0 || ptsCnt == 0) return false;
503
504     //smart reservation
505     auto outlinePtsCnt = 0;
506     auto outlineCntrsCnt = 0;
507
508     for (uint32_t i = 0; i < cmdCnt; ++i) {
509         switch(*(cmds + i)) {
510             case PathCommand::Close: {
511                 ++outlinePtsCnt;
512                 break;
513             }
514             case PathCommand::MoveTo: {
515                 ++outlineCntrsCnt;
516                 ++outlinePtsCnt;
517                 break;
518             }
519             case PathCommand::LineTo: {
520                 ++outlinePtsCnt;
521                 break;
522             }
523             case PathCommand::CubicTo: {
524                 outlinePtsCnt += 3;
525                 break;
526             }
527         }
528     }
529
530     ++outlinePtsCnt;    //for close
531     ++outlineCntrsCnt;  //for end
532
533     auto outline = shape->outline;
534     if (!outline) outline = static_cast<SwOutline*>(calloc(1, sizeof(SwOutline)));
535     outline->opened = true;
536
537     _growOutlinePoint(*outline, outlinePtsCnt);
538     _growOutlineContour(*outline, outlineCntrsCnt);
539
540     auto closed = false;
541
542     //Generate Outlines
543     while (cmdCnt-- > 0) {
544         switch(*cmds) {
545             case PathCommand::Close: {
546                 _outlineClose(*outline);
547                 closed = true;
548                 break;
549             }
550             case PathCommand::MoveTo: {
551                 _outlineMoveTo(*outline, pts, transform);
552                 ++pts;
553                 break;
554             }
555             case PathCommand::LineTo: {
556                 _outlineLineTo(*outline, pts, transform);
557                 ++pts;
558                 break;
559             }
560             case PathCommand::CubicTo: {
561                 _outlineCubicTo(*outline, pts, pts + 1, pts + 2, transform);
562                 pts += 3;
563                 break;
564             }
565         }
566         ++cmds;
567     }
568
569     _outlineEnd(*outline);
570
571     if (closed) outline->opened = false;
572
573     //FIXME:
574     //outline->flags = SwOutline::FillRule::Winding;
575
576     shape->outline = outline;
577
578     return true;
579 }
580
581
582 void shapeFree(SwShape* shape)
583 {
584     shapeDelOutline(shape);
585     rleFree(shape->rle);
586     shapeDelFill(shape);
587
588     if (shape->stroke) {
589         rleFree(shape->strokeRle);
590         strokeFree(shape->stroke);
591     }
592 }
593
594
595 void shapeDelStroke(SwShape* shape)
596 {
597     if (!shape->stroke) return;
598     rleFree(shape->strokeRle);
599     shape->strokeRle = nullptr;
600     strokeFree(shape->stroke);
601     shape->stroke = nullptr;
602 }
603
604
605 void shapeResetStroke(SwShape* shape, const Shape* sdata, const Matrix* transform)
606 {
607     if (!shape->stroke) shape->stroke = static_cast<SwStroke*>(calloc(1, sizeof(SwStroke)));
608     auto stroke = shape->stroke;
609     if (!stroke) return;
610
611     strokeReset(stroke, sdata, transform);
612
613     rleFree(shape->strokeRle);
614     shape->strokeRle = nullptr;
615 }
616
617
618 bool shapeGenStrokeRle(SwShape* shape, const Shape* sdata, const Matrix* transform, const SwSize& clip)
619 {
620     SwOutline* shapeOutline = nullptr;
621     bool freeOutline = false;
622
623     //Dash Style Stroke
624     if (sdata->strokeDash(nullptr) > 0) {
625         shapeOutline = _genDashOutline(sdata, transform);
626         if (!shapeOutline) return false;
627         freeOutline = true;
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     if (freeOutline) _delOutline(shapeOutline);
649     _delOutline(strokeOutline);
650
651     return true;
652 }
653
654
655 bool shapeGenFillColors(SwShape* shape, const Fill* fill, const Matrix* transform, SwSurface* surface, bool ctable)
656 {
657     return fillGenColorTable(shape->fill, fill, transform, surface, ctable);
658 }
659
660
661 void shapeResetFill(SwShape* shape)
662 {
663     if (!shape->fill) {
664         shape->fill = static_cast<SwFill*>(calloc(1, sizeof(SwFill)));
665         if (!shape->fill) return;
666     }
667     fillReset(shape->fill);
668 }
669
670
671 void shapeDelFill(SwShape* shape)
672 {
673     if (!shape->fill) return;
674     fillFree(shape->fill);
675     shape->fill = nullptr;
676 }