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