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