sw_engine: fix a regression bug.
[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         if (xMin > pt->x) xMin = pt->x;
205         if (xMax < pt->x) xMax = pt->x;
206         if (yMin > pt->y) yMin = pt->y;
207         if (yMax < pt->y) yMax = pt->y;
208     }
209     bbox.min.x = xMin >> 6;
210     bbox.max.x = (xMax + 63) >> 6;
211     bbox.min.y = yMin >> 6;
212     bbox.max.y = (yMax + 63) >> 6;
213
214     if (xMax - xMin < 1 && yMax - yMin < 1) return false;
215
216     return true;
217 }
218
219
220 static bool _checkValid(const SwOutline* outline, const SwBBox& bbox, const SwSize& clip)
221 {
222     assert(outline);
223
224     if (outline->ptsCnt == 0 || outline->cntrsCnt <= 0) return false;
225
226     //Check boundary
227     if (bbox.min.x >= clip.w || bbox.min.y >= clip.h || bbox.max.x <= 0 || bbox.max.y <= 0) return false;
228
229     return true;
230 }
231
232
233 static void _transformOutline(SwOutline* outline, const Matrix* transform)
234 {
235     if (!transform) return;
236
237     assert(outline);
238
239     for(uint32_t i = 0; i < outline->ptsCnt; ++i) {
240         auto dx = static_cast<float>(outline->pts[i].x >> 6);
241         auto dy = static_cast<float>(outline->pts[i].y >> 6);
242         auto tx = dx * transform->e11 + dy * transform->e12 + transform->e31;
243         auto ty = dx * transform->e21 + dy * transform->e22 + transform->e32;
244         auto pt = Point{tx, ty};
245         outline->pts[i] = TO_SWPOINT(&pt);
246     }
247 }
248
249
250 static void _dashLineTo(SwDashStroke& dash, const Point* to)
251 {
252     _growOutlinePoint(*dash.outline, dash.outline->ptsCnt >> 1);
253     _growOutlineContour(*dash.outline, dash.outline->cntrsCnt >> 1);
254
255     Line cur = {dash.ptCur, *to};
256     auto len = _lineLength(cur.pt1, cur.pt2);
257
258     if (len < dash.curLen) {
259         dash.curLen -= len;
260         if (!dash.curOpGap) {
261             _outlineMoveTo(*dash.outline, &dash.ptCur);
262             _outlineLineTo(*dash.outline, to);
263         }
264     } else {
265         while (len > dash.curLen) {
266             len -= dash.curLen;
267             Line left, right;
268             _lineSplitAt(cur, dash.curLen, left, right);;
269             dash.curIdx = (dash.curIdx + 1) % dash.cnt;
270             if (!dash.curOpGap) {
271                 _outlineMoveTo(*dash.outline, &left.pt1);
272                 _outlineLineTo(*dash.outline, &left.pt2);
273             }
274             dash.curLen = dash.pattern[dash.curIdx];
275             dash.curOpGap = !dash.curOpGap;
276             cur = right;
277             dash.ptCur = cur.pt1;
278         }
279         //leftovers
280         dash.curLen -= len;
281         if (!dash.curOpGap) {    
282             _outlineMoveTo(*dash.outline, &cur.pt1);
283             _outlineLineTo(*dash.outline, &cur.pt2);
284         }
285         if (dash.curLen < 1) {
286             //move to next dash
287             dash.curIdx = (dash.curIdx + 1) % dash.cnt;
288             dash.curLen = dash.pattern[dash.curIdx];
289             dash.curOpGap = !dash.curOpGap;
290         }
291     }
292     dash.ptCur = *to;
293 }
294
295
296 static void _dashCubicTo(SwDashStroke& dash, const Point* ctrl1, const Point* ctrl2, const Point* to)
297 {
298     _growOutlinePoint(*dash.outline, dash.outline->ptsCnt >> 1);
299     _growOutlineContour(*dash.outline, dash.outline->cntrsCnt >> 1);
300
301     Bezier cur = { dash.ptCur, *ctrl1, *ctrl2, *to};
302     auto len = bezLength(cur);
303
304     if (len < dash.curLen) {
305         dash.curLen -= len;
306         if (!dash.curOpGap) {
307             _outlineMoveTo(*dash.outline, &dash.ptCur);
308             _outlineCubicTo(*dash.outline, ctrl1, ctrl2, to);
309         }
310     } else {
311         while (len > dash.curLen) {
312             Bezier left, right;
313             len -= dash.curLen;
314             bezSplitAt(cur, dash.curLen, left, right);
315             dash.curIdx = (dash.curIdx + 1) % dash.cnt;
316             if (!dash.curOpGap) {
317                 _outlineMoveTo(*dash.outline, &left.start);
318                 _outlineCubicTo(*dash.outline, &left.ctrl1, &left.ctrl2, &left.end);
319             }
320             dash.curLen = dash.pattern[dash.curIdx];
321             dash.curOpGap = !dash.curOpGap;
322             cur = right;
323             dash.ptCur = right.start;
324         }
325         //leftovers
326         dash.curLen -= len;
327         if (!dash.curOpGap) {
328             _outlineMoveTo(*dash.outline, &cur.start);
329             _outlineCubicTo(*dash.outline, &cur.ctrl1, &cur.ctrl2, &cur.end);
330         }
331         if (dash.curLen < 1) {
332             //move to next dash
333             dash.curIdx = (dash.curIdx + 1) % dash.cnt;
334             dash.curLen = dash.pattern[dash.curIdx];
335             dash.curOpGap = !dash.curOpGap;
336         }
337     }
338     dash.ptCur = *to;
339 }
340
341
342 SwOutline* _genDashOutline(const Shape* sdata)
343 {
344     assert(sdata);
345
346     const PathCommand* cmds = nullptr;
347     auto cmdCnt = sdata->pathCommands(&cmds);
348
349     const Point* pts = nullptr;
350     auto ptsCnt = sdata->pathCoords(&pts);
351
352     //No actual shape data
353     if (cmdCnt == 0 || ptsCnt == 0) return nullptr;
354
355     SwDashStroke dash;
356     dash.curIdx = 0;
357     dash.curLen = 0;
358     dash.ptStart = {0, 0};
359     dash.ptCur = {0, 0};
360     dash.curOpGap = false;
361
362     const float* pattern;
363     dash.cnt = sdata->strokeDash(&pattern);
364     assert(dash.cnt > 0 && pattern);
365
366     //Is it safe to mutual exclusive?
367     dash.pattern = const_cast<float*>(pattern);
368     dash.outline = static_cast<SwOutline*>(calloc(1, sizeof(SwOutline)));
369     assert(dash.outline);
370     dash.outline->opened = true;
371
372     //smart reservation
373     auto outlinePtsCnt = 0;
374     auto outlineCntrsCnt = 0;
375
376     for (uint32_t i = 0; i < cmdCnt; ++i) {
377         switch(*(cmds + i)) {
378             case PathCommand::Close: {
379                 ++outlinePtsCnt;
380                 break;
381             }
382             case PathCommand::MoveTo: {
383                 ++outlineCntrsCnt;
384                 ++outlinePtsCnt;
385                 break;
386             }
387             case PathCommand::LineTo: {
388                 ++outlinePtsCnt;
389                 break;
390             }
391             case PathCommand::CubicTo: {
392                 outlinePtsCnt += 3;
393                 break;
394             }
395         }
396     }
397
398     ++outlinePtsCnt;    //for close
399     ++outlineCntrsCnt;  //for end
400
401     //Reserve Approximitely 20x...
402     _growOutlinePoint(*dash.outline, outlinePtsCnt * 20);
403     _growOutlineContour(*dash.outline, outlineCntrsCnt * 20);
404
405     while (cmdCnt-- > 0) {
406         switch(*cmds) {
407             case PathCommand::Close: {
408                 _dashLineTo(dash, &dash.ptStart);
409                 break;
410             }
411             case PathCommand::MoveTo: {
412                 //reset the dash
413                 dash.curIdx = 0;
414                 dash.curLen = *dash.pattern;
415                 dash.curOpGap = false;
416                 dash.ptStart = dash.ptCur = *pts;
417                 ++pts;
418                 break;
419             }
420             case PathCommand::LineTo: {
421                 _dashLineTo(dash, pts);
422                 ++pts;
423                 break;
424             }
425             case PathCommand::CubicTo: {
426                 _dashCubicTo(dash, pts, pts + 1, pts + 2);
427                 pts += 3;
428                 break;
429             }
430         }
431         ++cmds;
432     }
433
434     _outlineEnd(*dash.outline);
435
436     return dash.outline;
437 }
438
439
440 /************************************************************************/
441 /* External Class Implementation                                        */
442 /************************************************************************/
443
444 bool shapeGenRle(SwShape& shape, const Shape* sdata, const SwSize& clip, const Matrix* transform)
445 {
446     if (!shapeGenOutline(shape, sdata)) return false;
447
448     _transformOutline(shape.outline, transform);
449
450     if (!_updateBBox(shape.outline, shape.bbox)) goto end;
451
452     if (!_checkValid(shape.outline, shape.bbox, clip)) goto end;
453
454     //Case: Stroke Line
455     if (shape.outline->opened) return true;
456
457     shape.rle = rleRender(shape.outline, shape.bbox, clip);
458 end:
459     if (shape.rle) return true;
460     return false;
461 }
462
463
464 void shapeDelOutline(SwShape& shape)
465 {
466     auto outline = shape.outline;
467     _delOutline(outline);
468     shape.outline = nullptr;
469 }
470
471
472 void shapeReset(SwShape& shape)
473 {
474     shapeDelOutline(shape);
475     rleFree(shape.rle);
476     shape.rle = nullptr;
477     _initBBox(shape.bbox);
478 }
479
480
481 bool shapeGenOutline(SwShape& shape, const Shape* sdata)
482 {
483     assert(sdata);
484
485     const PathCommand* cmds = nullptr;
486     auto cmdCnt = sdata->pathCommands(&cmds);
487
488     const Point* pts = nullptr;
489     auto ptsCnt = sdata->pathCoords(&pts);
490
491     //No actual shape data
492     if (cmdCnt == 0 || ptsCnt == 0) return false;
493
494     //smart reservation
495     auto outlinePtsCnt = 0;
496     auto outlineCntrsCnt = 0;
497
498     for (uint32_t i = 0; i < cmdCnt; ++i) {
499         switch(*(cmds + i)) {
500             case PathCommand::Close: {
501                 ++outlinePtsCnt;
502                 break;
503             }
504             case PathCommand::MoveTo: {
505                 ++outlineCntrsCnt;
506                 ++outlinePtsCnt;
507                 break;
508             }
509             case PathCommand::LineTo: {
510                 ++outlinePtsCnt;
511                 break;
512             }
513             case PathCommand::CubicTo: {
514                 outlinePtsCnt += 3;
515                 break;
516             }
517         }
518     }
519
520     ++outlinePtsCnt;    //for close
521     ++outlineCntrsCnt;  //for end
522
523     auto outline = shape.outline;
524     if (!outline) outline = static_cast<SwOutline*>(calloc(1, sizeof(SwOutline)));
525     assert(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);
543                 ++pts;
544                 break;
545             }
546             case PathCommand::LineTo: {
547                 _outlineLineTo(*outline, pts);
548                 ++pts;
549                 break;
550             }
551             case PathCommand::CubicTo: {
552                 _outlineCubicTo(*outline, pts, pts + 1, pts + 2);
553                 pts += 3;
554                 break;
555             }
556         }
557         ++cmds;
558     }
559
560     _outlineEnd(*outline);
561
562     if (closed) outline->opened = false;
563
564     //FIXME:
565     //outline->flags = SwOutline::FillRule::Winding;
566
567     shape.outline = outline;
568
569     return true;
570 }
571
572
573 void shapeFree(SwShape& shape)
574 {
575     shapeDelOutline(shape);
576     rleFree(shape.rle);
577     shapeDelFill(shape);
578
579     if (shape.stroke) {
580         rleFree(shape.strokeRle);
581         strokeFree(shape.stroke);
582     }
583 }
584
585
586 void shapeDelStroke(SwShape& shape)
587 {
588     if (!shape.stroke) return;
589     rleFree(shape.strokeRle);
590     shape.strokeRle = nullptr;
591     strokeFree(shape.stroke);
592     shape.stroke = nullptr;
593 }
594
595
596 void shapeResetStroke(SwShape& shape, const Shape* sdata)
597 {
598     if (!shape.stroke) shape.stroke = static_cast<SwStroke*>(calloc(1, sizeof(SwStroke)));
599     auto stroke = shape.stroke;
600     assert(stroke);
601
602     strokeReset(*stroke, sdata);
603
604     rleFree(shape.strokeRle);
605     shape.strokeRle = nullptr;
606 }
607
608
609 bool shapeGenStrokeRle(SwShape& shape, const Shape* sdata, const SwSize& clip)
610 {
611     assert(sdata);
612
613     SwOutline* shapeOutline = nullptr;
614
615     //Dash Style Stroke
616     if (sdata->strokeDash(nullptr) > 0) {
617         shapeOutline = _genDashOutline(sdata);
618         if (!shapeOutline) return false;
619
620     //Normal Style stroke
621     } else {
622         if (!shape.outline) {
623             if (!shapeGenOutline(shape, sdata)) return false;
624         }
625         shapeOutline = shape.outline;
626     }
627
628     if (!strokeParseOutline(*shape.stroke, *shapeOutline)) return false;
629
630     auto strokeOutline = strokeExportOutline(*shape.stroke);
631     if (!strokeOutline) return false;
632
633     SwBBox bbox;
634     _updateBBox(strokeOutline, bbox);
635
636     if (!_checkValid(strokeOutline, bbox, clip)) return false;
637
638     shape.strokeRle = rleRender(strokeOutline, bbox, clip);
639
640     _delOutline(strokeOutline);
641
642     return true;
643 }
644
645
646 bool shapeGenFillColors(SwShape& shape, const Fill* fill, const Matrix* transform, bool ctable)
647 {
648     return fillGenColorTable(shape.fill, fill, transform, ctable);
649 }
650
651
652 void shapeResetFill(SwShape& shape)
653 {
654     if (!shape.fill) shape.fill = static_cast<SwFill*>(calloc(1, sizeof(SwFill)));
655     assert(shape.fill);
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 }
667
668
669 #endif /* _TVG_SW_SHAPE_H_ */