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