sw_engine: threading optimization
[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 Matrix* transform)
235 {
236     if (!transform) return;
237
238     assert(outline);
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->e11 + dy * transform->e12 + transform->e31;
244         auto ty = dx * transform->e21 + dy * transform->e22 + transform->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* sdata)
344 {
345     assert(sdata);
346
347     const PathCommand* cmds = nullptr;
348     auto cmdCnt = sdata->pathCommands(&cmds);
349
350     const Point* pts = nullptr;
351     auto ptsCnt = sdata->pathCoords(&pts);
352
353     //No actual shape data
354     if (cmdCnt == 0 || ptsCnt == 0) return nullptr;
355
356     SwDashStroke dash;
357     dash.curIdx = 0;
358     dash.curLen = 0;
359     dash.ptStart = {0, 0};
360     dash.ptCur = {0, 0};
361     dash.curOpGap = false;
362
363     const float* pattern;
364     dash.cnt = sdata->strokeDash(&pattern);
365     assert(dash.cnt > 0 && pattern);
366
367     //Is it safe to mutual exclusive?
368     dash.pattern = const_cast<float*>(pattern);
369     dash.outline = static_cast<SwOutline*>(calloc(1, sizeof(SwOutline)));
370     assert(dash.outline);
371     dash.outline->opened = true;
372
373     //smart reservation
374     auto outlinePtsCnt = 0;
375     auto outlineCntrsCnt = 0;
376
377     for (uint32_t i = 0; i < cmdCnt; ++i) {
378         switch(*(cmds + i)) {
379             case PathCommand::Close: {
380                 ++outlinePtsCnt;
381                 break;
382             }
383             case PathCommand::MoveTo: {
384                 ++outlineCntrsCnt;
385                 ++outlinePtsCnt;
386                 break;
387             }
388             case PathCommand::LineTo: {
389                 ++outlinePtsCnt;
390                 break;
391             }
392             case PathCommand::CubicTo: {
393                 outlinePtsCnt += 3;
394                 break;
395             }
396         }
397     }
398
399     ++outlinePtsCnt;    //for close
400     ++outlineCntrsCnt;  //for end
401
402     //Reserve Approximitely 20x...
403     _growOutlinePoint(*dash.outline, outlinePtsCnt * 20);
404     _growOutlineContour(*dash.outline, outlineCntrsCnt * 20);
405
406     while (cmdCnt-- > 0) {
407         switch(*cmds) {
408             case PathCommand::Close: {
409                 _dashLineTo(dash, &dash.ptStart);
410                 break;
411             }
412             case PathCommand::MoveTo: {
413                 //reset the dash
414                 dash.curIdx = 0;
415                 dash.curLen = *dash.pattern;
416                 dash.curOpGap = false;
417                 dash.ptStart = dash.ptCur = *pts;
418                 ++pts;
419                 break;
420             }
421             case PathCommand::LineTo: {
422                 _dashLineTo(dash, pts);
423                 ++pts;
424                 break;
425             }
426             case PathCommand::CubicTo: {
427                 _dashCubicTo(dash, pts, pts + 1, pts + 2);
428                 pts += 3;
429                 break;
430             }
431         }
432         ++cmds;
433     }
434
435     _outlineEnd(*dash.outline);
436
437     return dash.outline;
438 }
439
440
441 /************************************************************************/
442 /* External Class Implementation                                        */
443 /************************************************************************/
444
445 bool shapeGenRle(SwShape& shape, const Shape* sdata, const SwSize& clip, const Matrix* transform)
446 {
447     if (!shapeGenOutline(shape, sdata)) return false;
448
449     _transformOutline(shape.outline, transform);
450
451     if (!_updateBBox(shape.outline, shape.bbox)) goto end;
452
453     if (!_checkValid(shape.outline, shape.bbox, clip)) goto end;
454
455     shape.rle = rleRender(shape.outline, shape.bbox, clip);
456 end:
457     if (shape.rle) return true;
458     return false;
459 }
460
461
462 void shapeDelOutline(SwShape& shape)
463 {
464     auto outline = shape.outline;
465     _delOutline(outline);
466     shape.outline = nullptr;
467 }
468
469
470 void shapeReset(SwShape& shape)
471 {
472     shapeDelOutline(shape);
473     rleFree(shape.rle);
474     shape.rle = nullptr;
475     _initBBox(shape.bbox);
476 }
477
478
479 bool shapeGenOutline(SwShape& shape, const Shape* sdata)
480 {
481     assert(sdata);
482
483     const PathCommand* cmds = nullptr;
484     auto cmdCnt = sdata->pathCommands(&cmds);
485
486     const Point* pts = nullptr;
487     auto ptsCnt = sdata->pathCoords(&pts);
488
489     //No actual shape data
490     if (cmdCnt == 0 || ptsCnt == 0) return false;
491
492     //smart reservation
493     auto outlinePtsCnt = 0;
494     auto outlineCntrsCnt = 0;
495
496     for (uint32_t i = 0; i < cmdCnt; ++i) {
497         switch(*(cmds + i)) {
498             case PathCommand::Close: {
499                 ++outlinePtsCnt;
500                 break;
501             }
502             case PathCommand::MoveTo: {
503                 ++outlineCntrsCnt;
504                 ++outlinePtsCnt;
505                 break;
506             }
507             case PathCommand::LineTo: {
508                 ++outlinePtsCnt;
509                 break;
510             }
511             case PathCommand::CubicTo: {
512                 outlinePtsCnt += 3;
513                 break;
514             }
515         }
516     }
517
518     ++outlinePtsCnt;    //for close
519     ++outlineCntrsCnt;  //for end
520
521     auto outline = shape.outline;
522     if (!outline) outline = static_cast<SwOutline*>(calloc(1, sizeof(SwOutline)));
523     assert(outline);
524     outline->opened = true;
525
526     _growOutlinePoint(*outline, outlinePtsCnt);
527     _growOutlineContour(*outline, outlineCntrsCnt);
528
529     //Generate Outlines
530     while (cmdCnt-- > 0) {
531         switch(*cmds) {
532             case PathCommand::Close: {
533                 _outlineClose(*outline);
534                 break;
535             }
536             case PathCommand::MoveTo: {
537                 _outlineMoveTo(*outline, pts);
538                 ++pts;
539                 break;
540             }
541             case PathCommand::LineTo: {
542                 _outlineLineTo(*outline, pts);
543                 ++pts;
544                 break;
545             }
546             case PathCommand::CubicTo: {
547                 _outlineCubicTo(*outline, pts, pts + 1, pts + 2);
548                 pts += 3;
549                 break;
550             }
551         }
552         ++cmds;
553     }
554
555     _outlineEnd(*outline);
556
557     //FIXME:
558     //outline->flags = SwOutline::FillRule::Winding;
559
560     shape.outline = outline;
561
562     return true;
563 }
564
565
566 void shapeFree(SwShape& shape)
567 {
568     shapeDelOutline(shape);
569     rleFree(shape.rle);
570
571     if (shape.stroke) {
572         rleFree(shape.strokeRle);
573         strokeFree(shape.stroke);
574     }
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     assert(sdata);
604
605     SwOutline* shapeOutline = nullptr;
606
607     //Dash Style Stroke
608     if (sdata->strokeDash(nullptr) > 0) {
609         shapeOutline = _genDashOutline(sdata);
610         if (!shapeOutline) return false;
611
612     //Normal Style stroke
613     } else {
614         if (!shape.outline) {
615             if (!shapeGenOutline(shape, sdata)) return false;
616         }
617         shapeOutline = shape.outline;
618     }
619
620     if (!strokeParseOutline(*shape.stroke, *shapeOutline)) return false;
621
622     auto strokeOutline = strokeExportOutline(*shape.stroke);
623     if (!strokeOutline) return false;
624
625     SwBBox bbox;
626     _updateBBox(strokeOutline, bbox);
627
628     if (!_checkValid(strokeOutline, bbox, clip)) return false;
629
630     shape.strokeRle = rleRender(strokeOutline, bbox, clip);
631
632     _delOutline(strokeOutline);
633
634     return true;
635 }
636
637
638 bool shapeGenFillColors(SwShape& shape, const Fill* fill, const Matrix* transform, bool ctable)
639 {
640     return fillGenColorTable(shape.fill, fill, transform, ctable);
641 }
642
643
644 void shapeResetFill(SwShape& shape)
645 {
646     if (!shape.fill) shape.fill = static_cast<SwFill*>(calloc(1, sizeof(SwFill)));
647     assert(shape.fill);
648
649     fillReset(shape.fill);
650 }
651
652
653 void shapeDelFill(SwShape& shape)
654 {
655     if (!shape.fill) return;
656     fillFree(shape.fill);
657     shape.fill = nullptr;
658 }
659
660
661 #endif /* _TVG_SW_SHAPE_H_ */