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