sw_engine shape: prevent crash when wrong pair of commands & points.
[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     auto closeCnt = 0;
393
394     for (uint32_t i = 0; i < cmdCnt; ++i) {
395         switch(*(cmds + i)) {
396             case PathCommand::Close: {
397                 ++outlinePtsCnt;
398                 ++closeCnt;
399                 break;
400             }
401             case PathCommand::MoveTo: {
402                 ++outlineCntrsCnt;
403                 ++outlinePtsCnt;
404                 break;
405             }
406             case PathCommand::LineTo: {
407                 ++outlinePtsCnt;
408                 break;
409             }
410             case PathCommand::CubicTo: {
411                 outlinePtsCnt += 3;
412                 break;
413             }
414         }
415     }
416
417     if (static_cast<uint32_t>(outlinePtsCnt - closeCnt) > ptsCnt) {
418         TVGERR("SW_ENGINE", "Wrong a pair of the commands & points - required(%d), current(%d)", outlinePtsCnt - closeCnt, ptsCnt);
419         return false;
420     }
421
422     ++outlinePtsCnt;    //for close
423     ++outlineCntrsCnt;  //for end
424
425     shape->outline = mpoolReqOutline(mpool, tid);
426     auto outline = shape->outline;
427
428     _growOutlinePoint(*outline, outlinePtsCnt);
429
430      if (_growOutlineContour(*outline, outlineCntrsCnt)) {
431         _reserveOutlineClose(*outline);
432     } else {
433         _resetOutlineClose(*outline);
434     }
435
436     //Generate Outlines
437     while (cmdCnt-- > 0) {
438         switch(*cmds) {
439             case PathCommand::Close: {
440                 _outlineClose(*outline);
441                 break;
442             }
443             case PathCommand::MoveTo: {
444                 _outlineMoveTo(*outline, pts, transform);
445                 ++pts;
446                 break;
447             }
448             case PathCommand::LineTo: {
449                 _outlineLineTo(*outline, pts, transform);
450                 ++pts;
451                 break;
452             }
453             case PathCommand::CubicTo: {
454                 _outlineCubicTo(*outline, pts, pts + 1, pts + 2, transform);
455                 pts += 3;
456                 break;
457             }
458         }
459         ++cmds;
460     }
461
462     _outlineEnd(*outline);
463
464     outline->fillRule = sdata->fillRule();
465     shape->outline = outline;
466
467     return true;
468 }
469
470
471 /************************************************************************/
472 /* External Class Implementation                                        */
473 /************************************************************************/
474
475 bool shapePrepare(SwShape* shape, const Shape* sdata, const Matrix* transform,  const SwBBox& clipRegion, SwBBox& renderRegion, SwMpool* mpool, unsigned tid)
476 {
477     if (!_genOutline(shape, sdata, transform, mpool, tid)) return false;
478     if (!mathUpdateOutlineBBox(shape->outline, clipRegion, renderRegion)) return false;
479
480     //Keep it for Rasterization Region
481     shape->bbox = renderRegion;
482
483     //Check valid region
484     if (renderRegion.max.x - renderRegion.min.x < 1 && renderRegion.max.y - renderRegion.min.y < 1) return false;
485
486     //Check boundary
487     if (renderRegion.min.x >= clipRegion.max.x || renderRegion.min.y >= clipRegion.max.y ||
488         renderRegion.max.x <= clipRegion.min.x || renderRegion.max.y <= clipRegion.min.y) return false;
489
490     return true;
491 }
492
493
494 bool shapePrepared(const SwShape* shape)
495 {
496     return shape->rle ? true : false;
497 }
498
499
500 bool shapeGenRle(SwShape* shape, TVG_UNUSED const Shape* sdata, bool antiAlias, bool hasComposite)
501 {
502     //FIXME: Should we draw it?
503     //Case: Stroke Line
504     //if (shape.outline->opened) return true;
505
506     //Case A: Fast Track Rectangle Drawing
507     if (!hasComposite && (shape->rect = _fastTrack(shape->outline))) return true;
508     //Case B: Normale Shape RLE Drawing
509     if ((shape->rle = rleRender(shape->rle, shape->outline, shape->bbox, antiAlias))) return true;
510
511     return false;
512 }
513
514
515 void shapeDelOutline(SwShape* shape, SwMpool* mpool, uint32_t tid)
516 {
517     mpoolRetOutline(mpool, tid);
518     shape->outline = nullptr;
519 }
520
521
522 void shapeReset(SwShape* shape)
523 {
524     rleReset(shape->rle);
525     rleReset(shape->strokeRle);
526     shape->rect = false;
527     shape->bbox.reset();
528 }
529
530
531 void shapeFree(SwShape* shape)
532 {
533     rleFree(shape->rle);
534     shapeDelFill(shape);
535
536     if (shape->stroke) {
537         rleFree(shape->strokeRle);
538         strokeFree(shape->stroke);
539     }
540 }
541
542
543 void shapeDelStroke(SwShape* shape)
544 {
545     if (!shape->stroke) return;
546     rleFree(shape->strokeRle);
547     shape->strokeRle = nullptr;
548     strokeFree(shape->stroke);
549     shape->stroke = nullptr;
550 }
551
552
553 void shapeResetStroke(SwShape* shape, const Shape* sdata, const Matrix* transform)
554 {
555     if (!shape->stroke) shape->stroke = static_cast<SwStroke*>(calloc(1, sizeof(SwStroke)));
556     auto stroke = shape->stroke;
557     if (!stroke) return;
558
559     strokeReset(stroke, sdata, transform);
560     rleReset(shape->strokeRle);
561 }
562
563
564 bool shapeGenStrokeRle(SwShape* shape, const Shape* sdata, const Matrix* transform, const SwBBox& clipRegion, SwBBox& renderRegion, SwMpool* mpool, unsigned tid)
565 {
566     SwOutline* shapeOutline = nullptr;
567     SwOutline* strokeOutline = nullptr;
568     bool freeOutline = false;
569     bool ret = true;
570
571     //Dash Style Stroke
572     if (sdata->strokeDash(nullptr) > 0) {
573         shapeOutline = _genDashOutline(sdata, transform);
574         if (!shapeOutline) return false;
575         freeOutline = true;
576     //Normal Style stroke
577     } else {
578         if (!shape->outline) {
579             if (!_genOutline(shape, sdata, transform, mpool, tid)) return false;
580         }
581         shapeOutline = shape->outline;
582     }
583
584     if (!strokeParseOutline(shape->stroke, *shapeOutline)) {
585         ret = false;
586         goto fail;
587     }
588
589     strokeOutline = strokeExportOutline(shape->stroke, mpool, tid);
590     if (!strokeOutline) {
591         ret = false;
592         goto fail;
593     }
594
595     if (!mathUpdateOutlineBBox(strokeOutline, clipRegion, renderRegion)) {
596         ret = false;
597         goto fail;
598     }
599
600     shape->strokeRle = rleRender(shape->strokeRle, strokeOutline, renderRegion, true);
601
602 fail:
603     if (freeOutline) {
604         if (shapeOutline->cntrs) free(shapeOutline->cntrs);
605         if (shapeOutline->pts) free(shapeOutline->pts);
606         if (shapeOutline->types) free(shapeOutline->types);
607         if (shapeOutline->closed) free(shapeOutline->closed);
608         free(shapeOutline);
609     }
610     mpoolRetStrokeOutline(mpool, tid);
611
612     return ret;
613 }
614
615
616 bool shapeGenFillColors(SwShape* shape, const Fill* fill, const Matrix* transform, SwSurface* surface, uint32_t opacity, bool ctable)
617 {
618     return fillGenColorTable(shape->fill, fill, transform, surface, opacity, ctable);
619 }
620
621
622 bool shapeGenStrokeFillColors(SwShape* shape, const Fill* fill, const Matrix* transform, SwSurface* surface, uint32_t opacity, bool ctable)
623 {
624     return fillGenColorTable(shape->stroke->fill, fill, transform, surface, opacity, ctable);
625 }
626
627
628 void shapeResetFill(SwShape* shape)
629 {
630     if (!shape->fill) {
631         shape->fill = static_cast<SwFill*>(calloc(1, sizeof(SwFill)));
632         if (!shape->fill) return;
633     }
634     fillReset(shape->fill);
635 }
636
637
638 void shapeResetStrokeFill(SwShape* shape)
639 {
640     if (!shape->stroke->fill) {
641         shape->stroke->fill = static_cast<SwFill*>(calloc(1, sizeof(SwFill)));
642         if (!shape->stroke->fill) return;
643     }
644     fillReset(shape->stroke->fill);
645 }
646
647
648 void shapeDelFill(SwShape* shape)
649 {
650     if (!shape->fill) return;
651     fillFree(shape->fill);
652     shape->fill = nullptr;
653 }
654
655
656 void shapeDelStrokeFill(SwShape* shape)
657 {
658     if (!shape->stroke->fill) return;
659     fillFree(shape->stroke->fill);
660     shape->stroke->fill = nullptr;
661 }