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