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