1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
|
/*
* Copyright 2015 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "GrTessellator.h"
#include "GrDefaultGeoProcFactory.h"
#include "GrPathUtils.h"
#include "SkArenaAlloc.h"
#include "SkGeometry.h"
#include "SkPath.h"
#include "SkPointPriv.h"
#include "SkTDPQueue.h"
#include <algorithm>
#include <stdio.h>
/*
* There are six stages to the basic algorithm:
*
* 1) Linearize the path contours into piecewise linear segments (path_to_contours()).
* 2) Build a mesh of edges connecting the vertices (build_edges()).
* 3) Sort the vertices in Y (and secondarily in X) (merge_sort()).
* 4) Simplify the mesh by inserting new vertices at intersecting edges (simplify()).
* 5) Tessellate the simplified mesh into monotone polygons (tessellate()).
* 6) Triangulate the monotone polygons directly into a vertex buffer (polys_to_triangles()).
*
* For screenspace antialiasing, the algorithm is modified as follows:
*
* Run steps 1-5 above to produce polygons.
* 5b) Apply fill rules to extract boundary contours from the polygons (extract_boundaries()).
* 5c) Simplify boundaries to remove "pointy" vertices that cause inversions (simplify_boundary()).
* 5d) Displace edges by half a pixel inward and outward along their normals. Intersect to find
* new vertices, and set zero alpha on the exterior and one alpha on the interior. Build a new
* antialiased mesh from those vertices (stroke_boundary()).
* Run steps 3-6 above on the new mesh, and produce antialiased triangles.
*
* The vertex sorting in step (3) is a merge sort, since it plays well with the linked list
* of vertices (and the necessity of inserting new vertices on intersection).
*
* Stages (4) and (5) use an active edge list -- a list of all edges for which the
* sweep line has crossed the top vertex, but not the bottom vertex. It's sorted
* left-to-right based on the point where both edges are active (when both top vertices
* have been seen, so the "lower" top vertex of the two). If the top vertices are equal
* (shared), it's sorted based on the last point where both edges are active, so the
* "upper" bottom vertex.
*
* The most complex step is the simplification (4). It's based on the Bentley-Ottman
* line-sweep algorithm, but due to floating point inaccuracy, the intersection points are
* not exact and may violate the mesh topology or active edge list ordering. We
* accommodate this by adjusting the topology of the mesh and AEL to match the intersection
* points. This occurs in two ways:
*
* A) Intersections may cause a shortened edge to no longer be ordered with respect to its
* neighbouring edges at the top or bottom vertex. This is handled by merging the
* edges (merge_collinear_edges()).
* B) Intersections may cause an edge to violate the left-to-right ordering of the
* active edge list. This is handled by detecting potential violations and rewinding
* the active edge list to the vertex before they occur (rewind() during merging,
* rewind_if_necessary() during splitting).
*
* The tessellation steps (5) and (6) are based on "Triangulating Simple Polygons and
* Equivalent Problems" (Fournier and Montuno); also a line-sweep algorithm. Note that it
* currently uses a linked list for the active edge list, rather than a 2-3 tree as the
* paper describes. The 2-3 tree gives O(lg N) lookups, but insertion and removal also
* become O(lg N). In all the test cases, it was found that the cost of frequent O(lg N)
* insertions and removals was greater than the cost of infrequent O(N) lookups with the
* linked list implementation. With the latter, all removals are O(1), and most insertions
* are O(1), since we know the adjacent edge in the active edge list based on the topology.
* Only type 2 vertices (see paper) require the O(N) lookups, and these are much less
* frequent. There may be other data structures worth investigating, however.
*
* Note that the orientation of the line sweep algorithms is determined by the aspect ratio of the
* path bounds. When the path is taller than it is wide, we sort vertices based on increasing Y
* coordinate, and secondarily by increasing X coordinate. When the path is wider than it is tall,
* we sort by increasing X coordinate, but secondarily by *decreasing* Y coordinate. This is so
* that the "left" and "right" orientation in the code remains correct (edges to the left are
* increasing in Y; edges to the right are decreasing in Y). That is, the setting rotates 90
* degrees counterclockwise, rather that transposing.
*/
#define LOGGING_ENABLED 0
#if LOGGING_ENABLED
#define LOG printf
#else
#define LOG(...)
#endif
namespace {
const int kArenaChunkSize = 16 * 1024;
#ifndef GR_TESSELLATOR_LEGACY_INVERSION_HANDLING
const float kCosMiterAngle = 0.97f; // Corresponds to an angle of ~14 degrees.
#endif
struct Vertex;
struct Edge;
struct Event;
struct Poly;
template <class T, T* T::*Prev, T* T::*Next>
void list_insert(T* t, T* prev, T* next, T** head, T** tail) {
t->*Prev = prev;
t->*Next = next;
if (prev) {
prev->*Next = t;
} else if (head) {
*head = t;
}
if (next) {
next->*Prev = t;
} else if (tail) {
*tail = t;
}
}
template <class T, T* T::*Prev, T* T::*Next>
void list_remove(T* t, T** head, T** tail) {
if (t->*Prev) {
t->*Prev->*Next = t->*Next;
} else if (head) {
*head = t->*Next;
}
if (t->*Next) {
t->*Next->*Prev = t->*Prev;
} else if (tail) {
*tail = t->*Prev;
}
t->*Prev = t->*Next = nullptr;
}
/**
* Vertices are used in three ways: first, the path contours are converted into a
* circularly-linked list of Vertices for each contour. After edge construction, the same Vertices
* are re-ordered by the merge sort according to the sweep_lt comparator (usually, increasing
* in Y) using the same fPrev/fNext pointers that were used for the contours, to avoid
* reallocation. Finally, MonotonePolys are built containing a circularly-linked list of
* Vertices. (Currently, those Vertices are newly-allocated for the MonotonePolys, since
* an individual Vertex from the path mesh may belong to multiple
* MonotonePolys, so the original Vertices cannot be re-used.
*/
struct Vertex {
Vertex(const SkPoint& point, uint8_t alpha)
: fPoint(point), fPrev(nullptr), fNext(nullptr)
, fFirstEdgeAbove(nullptr), fLastEdgeAbove(nullptr)
, fFirstEdgeBelow(nullptr), fLastEdgeBelow(nullptr)
, fLeftEnclosingEdge(nullptr), fRightEnclosingEdge(nullptr)
, fPartner(nullptr)
, fAlpha(alpha)
#if LOGGING_ENABLED
, fID (-1.0f)
#endif
{}
SkPoint fPoint; // Vertex position
Vertex* fPrev; // Linked list of contours, then Y-sorted vertices.
Vertex* fNext; // "
Edge* fFirstEdgeAbove; // Linked list of edges above this vertex.
Edge* fLastEdgeAbove; // "
Edge* fFirstEdgeBelow; // Linked list of edges below this vertex.
Edge* fLastEdgeBelow; // "
Edge* fLeftEnclosingEdge; // Nearest edge in the AEL left of this vertex.
Edge* fRightEnclosingEdge; // Nearest edge in the AEL right of this vertex.
Vertex* fPartner; // Corresponding inner or outer vertex (for AA).
uint8_t fAlpha;
#if LOGGING_ENABLED
float fID; // Identifier used for logging.
#endif
};
/***************************************************************************************/
struct AAParams {
bool fTweakAlpha;
GrColor fColor;
};
typedef bool (*CompareFunc)(const SkPoint& a, const SkPoint& b);
bool sweep_lt_horiz(const SkPoint& a, const SkPoint& b) {
return a.fX < b.fX || (a.fX == b.fX && a.fY > b.fY);
}
bool sweep_lt_vert(const SkPoint& a, const SkPoint& b) {
return a.fY < b.fY || (a.fY == b.fY && a.fX < b.fX);
}
struct Comparator {
enum class Direction { kVertical, kHorizontal };
Comparator(Direction direction) : fDirection(direction) {}
bool sweep_lt(const SkPoint& a, const SkPoint& b) const {
return fDirection == Direction::kHorizontal ? sweep_lt_horiz(a, b) : sweep_lt_vert(a, b);
}
Direction fDirection;
};
inline void* emit_vertex(Vertex* v, const AAParams* aaParams, void* data) {
if (!aaParams) {
SkPoint* d = static_cast<SkPoint*>(data);
*d++ = v->fPoint;
return d;
}
if (aaParams->fTweakAlpha) {
auto d = static_cast<GrDefaultGeoProcFactory::PositionColorAttr*>(data);
d->fPosition = v->fPoint;
d->fColor = SkAlphaMulQ(aaParams->fColor, SkAlpha255To256(v->fAlpha));
d++;
return d;
}
auto d = static_cast<GrDefaultGeoProcFactory::PositionColorCoverageAttr*>(data);
d->fPosition = v->fPoint;
d->fColor = aaParams->fColor;
d->fCoverage = GrNormalizeByteToFloat(v->fAlpha);
d++;
return d;
}
void* emit_triangle(Vertex* v0, Vertex* v1, Vertex* v2, const AAParams* aaParams, void* data) {
LOG("emit_triangle %g (%g, %g) %d\n", v0->fID, v0->fPoint.fX, v0->fPoint.fY, v0->fAlpha);
LOG(" %g (%g, %g) %d\n", v1->fID, v1->fPoint.fX, v1->fPoint.fY, v1->fAlpha);
LOG(" %g (%g, %g) %d\n", v2->fID, v2->fPoint.fX, v2->fPoint.fY, v2->fAlpha);
#if TESSELLATOR_WIREFRAME
data = emit_vertex(v0, aaParams, data);
data = emit_vertex(v1, aaParams, data);
data = emit_vertex(v1, aaParams, data);
data = emit_vertex(v2, aaParams, data);
data = emit_vertex(v2, aaParams, data);
data = emit_vertex(v0, aaParams, data);
#else
data = emit_vertex(v0, aaParams, data);
data = emit_vertex(v1, aaParams, data);
data = emit_vertex(v2, aaParams, data);
#endif
return data;
}
struct VertexList {
VertexList() : fHead(nullptr), fTail(nullptr) {}
VertexList(Vertex* head, Vertex* tail) : fHead(head), fTail(tail) {}
Vertex* fHead;
Vertex* fTail;
void insert(Vertex* v, Vertex* prev, Vertex* next) {
list_insert<Vertex, &Vertex::fPrev, &Vertex::fNext>(v, prev, next, &fHead, &fTail);
}
void append(Vertex* v) {
insert(v, fTail, nullptr);
}
void append(const VertexList& list) {
if (!list.fHead) {
return;
}
if (fTail) {
fTail->fNext = list.fHead;
list.fHead->fPrev = fTail;
} else {
fHead = list.fHead;
}
fTail = list.fTail;
}
void prepend(Vertex* v) {
insert(v, nullptr, fHead);
}
void remove(Vertex* v) {
list_remove<Vertex, &Vertex::fPrev, &Vertex::fNext>(v, &fHead, &fTail);
}
void close() {
if (fHead && fTail) {
fTail->fNext = fHead;
fHead->fPrev = fTail;
}
}
};
// Round to nearest quarter-pixel. This is used for screenspace tessellation.
inline void round(SkPoint* p) {
p->fX = SkScalarRoundToScalar(p->fX * SkFloatToScalar(4.0f)) * SkFloatToScalar(0.25f);
p->fY = SkScalarRoundToScalar(p->fY * SkFloatToScalar(4.0f)) * SkFloatToScalar(0.25f);
}
inline SkScalar double_to_clamped_scalar(double d) {
return SkDoubleToScalar(std::min((double) SK_ScalarMax, std::max(d, (double) -SK_ScalarMax)));
}
// A line equation in implicit form. fA * x + fB * y + fC = 0, for all points (x, y) on the line.
struct Line {
Line(double a, double b, double c) : fA(a), fB(b), fC(c) {}
Line(Vertex* p, Vertex* q) : Line(p->fPoint, q->fPoint) {}
Line(const SkPoint& p, const SkPoint& q)
: fA(static_cast<double>(q.fY) - p.fY) // a = dY
, fB(static_cast<double>(p.fX) - q.fX) // b = -dX
, fC(static_cast<double>(p.fY) * q.fX - // c = cross(q, p)
static_cast<double>(p.fX) * q.fY) {}
double dist(const SkPoint& p) const {
return fA * p.fX + fB * p.fY + fC;
}
Line operator*(double v) const {
return Line(fA * v, fB * v, fC * v);
}
double magSq() const {
return fA * fA + fB * fB;
}
void normalize() {
double len = sqrt(this->magSq());
if (len == 0.0) {
return;
}
double scale = 1.0f / len;
fA *= scale;
fB *= scale;
fC *= scale;
}
bool nearParallel(const Line& o) const {
return fabs(o.fA - fA) < 0.00001 && fabs(o.fB - fB) < 0.00001;
}
// Compute the intersection of two (infinite) Lines.
bool intersect(const Line& other, SkPoint* point) const {
double denom = fA * other.fB - fB * other.fA;
if (denom == 0.0) {
return false;
}
double scale = 1.0 / denom;
point->fX = double_to_clamped_scalar((fB * other.fC - other.fB * fC) * scale);
point->fY = double_to_clamped_scalar((other.fA * fC - fA * other.fC) * scale);
round(point);
return true;
}
double fA, fB, fC;
};
/**
* An Edge joins a top Vertex to a bottom Vertex. Edge ordering for the list of "edges above" and
* "edge below" a vertex as well as for the active edge list is handled by isLeftOf()/isRightOf().
* Note that an Edge will give occasionally dist() != 0 for its own endpoints (because floating
* point). For speed, that case is only tested by the callers that require it (e.g.,
* rewind_if_necessary()). Edges also handle checking for intersection with other edges.
* Currently, this converts the edges to the parametric form, in order to avoid doing a division
* until an intersection has been confirmed. This is slightly slower in the "found" case, but
* a lot faster in the "not found" case.
*
* The coefficients of the line equation stored in double precision to avoid catastrphic
* cancellation in the isLeftOf() and isRightOf() checks. Using doubles ensures that the result is
* correct in float, since it's a polynomial of degree 2. The intersect() function, being
* degree 5, is still subject to catastrophic cancellation. We deal with that by assuming its
* output may be incorrect, and adjusting the mesh topology to match (see comment at the top of
* this file).
*/
struct Edge {
enum class Type { kInner, kOuter, kConnector };
Edge(Vertex* top, Vertex* bottom, int winding, Type type)
: fWinding(winding)
, fTop(top)
, fBottom(bottom)
, fType(type)
, fLeft(nullptr)
, fRight(nullptr)
, fPrevEdgeAbove(nullptr)
, fNextEdgeAbove(nullptr)
, fPrevEdgeBelow(nullptr)
, fNextEdgeBelow(nullptr)
, fLeftPoly(nullptr)
, fRightPoly(nullptr)
, fEvent(nullptr)
, fLeftPolyPrev(nullptr)
, fLeftPolyNext(nullptr)
, fRightPolyPrev(nullptr)
, fRightPolyNext(nullptr)
, fOverlap(false)
, fUsedInLeftPoly(false)
, fUsedInRightPoly(false)
, fLine(top, bottom) {
}
int fWinding; // 1 == edge goes downward; -1 = edge goes upward.
Vertex* fTop; // The top vertex in vertex-sort-order (sweep_lt).
Vertex* fBottom; // The bottom vertex in vertex-sort-order.
Type fType;
Edge* fLeft; // The linked list of edges in the active edge list.
Edge* fRight; // "
Edge* fPrevEdgeAbove; // The linked list of edges in the bottom Vertex's "edges above".
Edge* fNextEdgeAbove; // "
Edge* fPrevEdgeBelow; // The linked list of edges in the top Vertex's "edges below".
Edge* fNextEdgeBelow; // "
Poly* fLeftPoly; // The Poly to the left of this edge, if any.
Poly* fRightPoly; // The Poly to the right of this edge, if any.
Event* fEvent;
Edge* fLeftPolyPrev;
Edge* fLeftPolyNext;
Edge* fRightPolyPrev;
Edge* fRightPolyNext;
bool fOverlap; // True if there's an overlap region adjacent to this edge.
bool fUsedInLeftPoly;
bool fUsedInRightPoly;
Line fLine;
double dist(const SkPoint& p) const {
return fLine.dist(p);
}
bool isRightOf(Vertex* v) const {
return fLine.dist(v->fPoint) < 0.0;
}
bool isLeftOf(Vertex* v) const {
return fLine.dist(v->fPoint) > 0.0;
}
void recompute() {
fLine = Line(fTop, fBottom);
}
bool intersect(const Edge& other, SkPoint* p, uint8_t* alpha = nullptr) const {
LOG("intersecting %g -> %g with %g -> %g\n",
fTop->fID, fBottom->fID,
other.fTop->fID, other.fBottom->fID);
if (fTop == other.fTop || fBottom == other.fBottom) {
return false;
}
double denom = fLine.fA * other.fLine.fB - fLine.fB * other.fLine.fA;
if (denom == 0.0) {
return false;
}
double dx = static_cast<double>(other.fTop->fPoint.fX) - fTop->fPoint.fX;
double dy = static_cast<double>(other.fTop->fPoint.fY) - fTop->fPoint.fY;
double sNumer = dy * other.fLine.fB + dx * other.fLine.fA;
double tNumer = dy * fLine.fB + dx * fLine.fA;
// If (sNumer / denom) or (tNumer / denom) is not in [0..1], exit early.
// This saves us doing the divide below unless absolutely necessary.
if (denom > 0.0 ? (sNumer < 0.0 || sNumer > denom || tNumer < 0.0 || tNumer > denom)
: (sNumer > 0.0 || sNumer < denom || tNumer > 0.0 || tNumer < denom)) {
return false;
}
double s = sNumer / denom;
SkASSERT(s >= 0.0 && s <= 1.0);
p->fX = SkDoubleToScalar(fTop->fPoint.fX - s * fLine.fB);
p->fY = SkDoubleToScalar(fTop->fPoint.fY + s * fLine.fA);
if (alpha) {
if (fType == Type::kConnector) {
*alpha = (1.0 - s) * fTop->fAlpha + s * fBottom->fAlpha;
} else if (other.fType == Type::kConnector) {
double t = tNumer / denom;
*alpha = (1.0 - t) * other.fTop->fAlpha + t * other.fBottom->fAlpha;
} else if (fType == Type::kOuter && other.fType == Type::kOuter) {
*alpha = 0;
} else {
*alpha = 255;
}
}
return true;
}
};
struct EdgeList {
EdgeList() : fHead(nullptr), fTail(nullptr) {}
Edge* fHead;
Edge* fTail;
void insert(Edge* edge, Edge* prev, Edge* next) {
list_insert<Edge, &Edge::fLeft, &Edge::fRight>(edge, prev, next, &fHead, &fTail);
}
void append(Edge* e) {
insert(e, fTail, nullptr);
}
void remove(Edge* edge) {
list_remove<Edge, &Edge::fLeft, &Edge::fRight>(edge, &fHead, &fTail);
}
void removeAll() {
while (fHead) {
this->remove(fHead);
}
}
void close() {
if (fHead && fTail) {
fTail->fRight = fHead;
fHead->fLeft = fTail;
}
}
bool contains(Edge* edge) const {
return edge->fLeft || edge->fRight || fHead == edge;
}
};
#ifndef GR_TESSELLATOR_LEGACY_INVERSION_HANDLING
struct Event {
Event(Edge* edge, const SkPoint& point, uint8_t alpha)
: fEdge(edge), fPoint(point), fAlpha(alpha), fPrev(nullptr), fNext(nullptr) {
}
Edge* fEdge;
SkPoint fPoint;
uint8_t fAlpha;
Event* fPrev;
Event* fNext;
void apply(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc);
};
bool compare(Event* const& e1, Event* const& e2) {
return e1->fAlpha > e2->fAlpha;
}
struct EventList : public SkTDPQueue<Event*, &compare> {};
void create_event(Edge* e, EventList* events, SkArenaAlloc& alloc) {
Edge bisector1(e->fTop, e->fTop->fPartner, 1, Edge::Type::kConnector);
Edge bisector2(e->fBottom, e->fBottom->fPartner, 1, Edge::Type::kConnector);
SkPoint p;
uint8_t alpha;
if (bisector1.intersect(bisector2, &p, &alpha)) {
LOG("found overlap edge %g -> %g, will collapse to %g,%g alpha %d\n",
e->fTop->fID, e->fBottom->fID, p.fX, p.fY, alpha);
e->fEvent = alloc.make<Event>(e, p, alpha);
events->insert(e->fEvent);
}
}
#endif
/***************************************************************************************/
struct Poly {
Poly(Vertex* v, int winding)
: fFirstVertex(v)
, fWinding(winding)
, fHead(nullptr)
, fTail(nullptr)
, fNext(nullptr)
, fPartner(nullptr)
, fCount(0)
{
#if LOGGING_ENABLED
static int gID = 0;
fID = gID++;
LOG("*** created Poly %d\n", fID);
#endif
}
typedef enum { kLeft_Side, kRight_Side } Side;
struct MonotonePoly {
MonotonePoly(Edge* edge, Side side)
: fSide(side)
, fFirstEdge(nullptr)
, fLastEdge(nullptr)
, fPrev(nullptr)
, fNext(nullptr) {
this->addEdge(edge);
}
Side fSide;
Edge* fFirstEdge;
Edge* fLastEdge;
MonotonePoly* fPrev;
MonotonePoly* fNext;
void addEdge(Edge* edge) {
if (fSide == kRight_Side) {
SkASSERT(!edge->fUsedInRightPoly);
list_insert<Edge, &Edge::fRightPolyPrev, &Edge::fRightPolyNext>(
edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge);
edge->fUsedInRightPoly = true;
} else {
SkASSERT(!edge->fUsedInLeftPoly);
list_insert<Edge, &Edge::fLeftPolyPrev, &Edge::fLeftPolyNext>(
edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge);
edge->fUsedInLeftPoly = true;
}
}
void* emit(const AAParams* aaParams, void* data) {
Edge* e = fFirstEdge;
VertexList vertices;
vertices.append(e->fTop);
int count = 1;
while (e != nullptr) {
if (kRight_Side == fSide) {
vertices.append(e->fBottom);
e = e->fRightPolyNext;
} else {
vertices.prepend(e->fBottom);
e = e->fLeftPolyNext;
}
count++;
}
Vertex* first = vertices.fHead;
Vertex* v = first->fNext;
while (v != vertices.fTail) {
SkASSERT(v && v->fPrev && v->fNext);
Vertex* prev = v->fPrev;
Vertex* curr = v;
Vertex* next = v->fNext;
if (count == 3) {
return emit_triangle(prev, curr, next, aaParams, data);
}
double ax = static_cast<double>(curr->fPoint.fX) - prev->fPoint.fX;
double ay = static_cast<double>(curr->fPoint.fY) - prev->fPoint.fY;
double bx = static_cast<double>(next->fPoint.fX) - curr->fPoint.fX;
double by = static_cast<double>(next->fPoint.fY) - curr->fPoint.fY;
if (ax * by - ay * bx >= 0.0) {
data = emit_triangle(prev, curr, next, aaParams, data);
v->fPrev->fNext = v->fNext;
v->fNext->fPrev = v->fPrev;
count--;
if (v->fPrev == first) {
v = v->fNext;
} else {
v = v->fPrev;
}
} else {
v = v->fNext;
}
}
return data;
}
};
Poly* addEdge(Edge* e, Side side, SkArenaAlloc& alloc) {
LOG("addEdge (%g -> %g) to poly %d, %s side\n",
e->fTop->fID, e->fBottom->fID, fID, side == kLeft_Side ? "left" : "right");
Poly* partner = fPartner;
Poly* poly = this;
if (side == kRight_Side) {
if (e->fUsedInRightPoly) {
return this;
}
} else {
if (e->fUsedInLeftPoly) {
return this;
}
}
if (partner) {
fPartner = partner->fPartner = nullptr;
}
if (!fTail) {
fHead = fTail = alloc.make<MonotonePoly>(e, side);
fCount += 2;
} else if (e->fBottom == fTail->fLastEdge->fBottom) {
return poly;
} else if (side == fTail->fSide) {
fTail->addEdge(e);
fCount++;
} else {
e = alloc.make<Edge>(fTail->fLastEdge->fBottom, e->fBottom, 1, Edge::Type::kInner);
fTail->addEdge(e);
fCount++;
if (partner) {
partner->addEdge(e, side, alloc);
poly = partner;
} else {
MonotonePoly* m = alloc.make<MonotonePoly>(e, side);
m->fPrev = fTail;
fTail->fNext = m;
fTail = m;
}
}
return poly;
}
void* emit(const AAParams* aaParams, void *data) {
if (fCount < 3) {
return data;
}
LOG("emit() %d, size %d\n", fID, fCount);
for (MonotonePoly* m = fHead; m != nullptr; m = m->fNext) {
data = m->emit(aaParams, data);
}
return data;
}
Vertex* lastVertex() const { return fTail ? fTail->fLastEdge->fBottom : fFirstVertex; }
Vertex* fFirstVertex;
int fWinding;
MonotonePoly* fHead;
MonotonePoly* fTail;
Poly* fNext;
Poly* fPartner;
int fCount;
#if LOGGING_ENABLED
int fID;
#endif
};
/***************************************************************************************/
bool coincident(const SkPoint& a, const SkPoint& b) {
return a == b;
}
Poly* new_poly(Poly** head, Vertex* v, int winding, SkArenaAlloc& alloc) {
Poly* poly = alloc.make<Poly>(v, winding);
poly->fNext = *head;
*head = poly;
return poly;
}
void append_point_to_contour(const SkPoint& p, VertexList* contour, SkArenaAlloc& alloc) {
Vertex* v = alloc.make<Vertex>(p, 255);
#if LOGGING_ENABLED
static float gID = 0.0f;
v->fID = gID++;
#endif
contour->append(v);
}
SkScalar quad_error_at(const SkPoint pts[3], SkScalar t, SkScalar u) {
SkQuadCoeff quad(pts);
SkPoint p0 = to_point(quad.eval(t - 0.5f * u));
SkPoint mid = to_point(quad.eval(t));
SkPoint p1 = to_point(quad.eval(t + 0.5f * u));
if (!p0.isFinite() || !mid.isFinite() || !p1.isFinite()) {
return 0;
}
return SkPointPriv::DistanceToLineSegmentBetweenSqd(mid, p0, p1);
}
void append_quadratic_to_contour(const SkPoint pts[3], SkScalar toleranceSqd, VertexList* contour,
SkArenaAlloc& alloc) {
SkQuadCoeff quad(pts);
Sk2s aa = quad.fA * quad.fA;
SkScalar denom = 2.0f * (aa[0] + aa[1]);
Sk2s ab = quad.fA * quad.fB;
SkScalar t = denom ? (-ab[0] - ab[1]) / denom : 0.0f;
int nPoints = 1;
SkScalar u = 1.0f;
// Test possible subdivision values only at the point of maximum curvature.
// If it passes the flatness metric there, it'll pass everywhere.
while (nPoints < GrPathUtils::kMaxPointsPerCurve) {
u = 1.0f / nPoints;
if (quad_error_at(pts, t, u) < toleranceSqd) {
break;
}
nPoints++;
}
for (int j = 1; j <= nPoints; j++) {
append_point_to_contour(to_point(quad.eval(j * u)), contour, alloc);
}
}
void generate_cubic_points(const SkPoint& p0,
const SkPoint& p1,
const SkPoint& p2,
const SkPoint& p3,
SkScalar tolSqd,
VertexList* contour,
int pointsLeft,
SkArenaAlloc& alloc) {
SkScalar d1 = SkPointPriv::DistanceToLineSegmentBetweenSqd(p1, p0, p3);
SkScalar d2 = SkPointPriv::DistanceToLineSegmentBetweenSqd(p2, p0, p3);
if (pointsLeft < 2 || (d1 < tolSqd && d2 < tolSqd) ||
!SkScalarIsFinite(d1) || !SkScalarIsFinite(d2)) {
append_point_to_contour(p3, contour, alloc);
return;
}
const SkPoint q[] = {
{ SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) },
{ SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) },
{ SkScalarAve(p2.fX, p3.fX), SkScalarAve(p2.fY, p3.fY) }
};
const SkPoint r[] = {
{ SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) },
{ SkScalarAve(q[1].fX, q[2].fX), SkScalarAve(q[1].fY, q[2].fY) }
};
const SkPoint s = { SkScalarAve(r[0].fX, r[1].fX), SkScalarAve(r[0].fY, r[1].fY) };
pointsLeft >>= 1;
generate_cubic_points(p0, q[0], r[0], s, tolSqd, contour, pointsLeft, alloc);
generate_cubic_points(s, r[1], q[2], p3, tolSqd, contour, pointsLeft, alloc);
}
// Stage 1: convert the input path to a set of linear contours (linked list of Vertices).
void path_to_contours(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
VertexList* contours, SkArenaAlloc& alloc, bool *isLinear) {
SkScalar toleranceSqd = tolerance * tolerance;
SkPoint pts[4];
*isLinear = true;
VertexList* contour = contours;
SkPath::Iter iter(path, false);
if (path.isInverseFillType()) {
SkPoint quad[4];
clipBounds.toQuad(quad);
for (int i = 3; i >= 0; i--) {
append_point_to_contour(quad[i], contours, alloc);
}
contour++;
}
SkAutoConicToQuads converter;
SkPath::Verb verb;
while ((verb = iter.next(pts, false)) != SkPath::kDone_Verb) {
switch (verb) {
case SkPath::kConic_Verb: {
SkScalar weight = iter.conicWeight();
const SkPoint* quadPts = converter.computeQuads(pts, weight, toleranceSqd);
for (int i = 0; i < converter.countQuads(); ++i) {
append_quadratic_to_contour(quadPts, toleranceSqd, contour, alloc);
quadPts += 2;
}
*isLinear = false;
break;
}
case SkPath::kMove_Verb:
if (contour->fHead) {
contour++;
}
append_point_to_contour(pts[0], contour, alloc);
break;
case SkPath::kLine_Verb: {
append_point_to_contour(pts[1], contour, alloc);
break;
}
case SkPath::kQuad_Verb: {
append_quadratic_to_contour(pts, toleranceSqd, contour, alloc);
*isLinear = false;
break;
}
case SkPath::kCubic_Verb: {
int pointsLeft = GrPathUtils::cubicPointCount(pts, tolerance);
generate_cubic_points(pts[0], pts[1], pts[2], pts[3], toleranceSqd, contour,
pointsLeft, alloc);
*isLinear = false;
break;
}
case SkPath::kClose_Verb:
case SkPath::kDone_Verb:
break;
}
}
}
inline bool apply_fill_type(SkPath::FillType fillType, int winding) {
switch (fillType) {
case SkPath::kWinding_FillType:
return winding != 0;
case SkPath::kEvenOdd_FillType:
return (winding & 1) != 0;
case SkPath::kInverseWinding_FillType:
return winding == 1;
case SkPath::kInverseEvenOdd_FillType:
return (winding & 1) == 1;
default:
SkASSERT(false);
return false;
}
}
inline bool apply_fill_type(SkPath::FillType fillType, Poly* poly) {
return poly && apply_fill_type(fillType, poly->fWinding);
}
Edge* new_edge(Vertex* prev, Vertex* next, Edge::Type type, Comparator& c, SkArenaAlloc& alloc) {
int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1;
Vertex* top = winding < 0 ? next : prev;
Vertex* bottom = winding < 0 ? prev : next;
return alloc.make<Edge>(top, bottom, winding, type);
}
void remove_edge(Edge* edge, EdgeList* edges) {
LOG("removing edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID);
SkASSERT(edges->contains(edge));
edges->remove(edge);
}
void insert_edge(Edge* edge, Edge* prev, EdgeList* edges) {
LOG("inserting edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID);
SkASSERT(!edges->contains(edge));
Edge* next = prev ? prev->fRight : edges->fHead;
edges->insert(edge, prev, next);
}
void find_enclosing_edges(Vertex* v, EdgeList* edges, Edge** left, Edge** right) {
if (v->fFirstEdgeAbove && v->fLastEdgeAbove) {
*left = v->fFirstEdgeAbove->fLeft;
*right = v->fLastEdgeAbove->fRight;
return;
}
Edge* next = nullptr;
Edge* prev;
for (prev = edges->fTail; prev != nullptr; prev = prev->fLeft) {
if (prev->isLeftOf(v)) {
break;
}
next = prev;
}
*left = prev;
*right = next;
}
void insert_edge_above(Edge* edge, Vertex* v, Comparator& c) {
if (edge->fTop->fPoint == edge->fBottom->fPoint ||
c.sweep_lt(edge->fBottom->fPoint, edge->fTop->fPoint)) {
return;
}
LOG("insert edge (%g -> %g) above vertex %g\n", edge->fTop->fID, edge->fBottom->fID, v->fID);
Edge* prev = nullptr;
Edge* next;
for (next = v->fFirstEdgeAbove; next; next = next->fNextEdgeAbove) {
if (next->isRightOf(edge->fTop)) {
break;
}
prev = next;
}
list_insert<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>(
edge, prev, next, &v->fFirstEdgeAbove, &v->fLastEdgeAbove);
}
void insert_edge_below(Edge* edge, Vertex* v, Comparator& c) {
if (edge->fTop->fPoint == edge->fBottom->fPoint ||
c.sweep_lt(edge->fBottom->fPoint, edge->fTop->fPoint)) {
return;
}
LOG("insert edge (%g -> %g) below vertex %g\n", edge->fTop->fID, edge->fBottom->fID, v->fID);
Edge* prev = nullptr;
Edge* next;
for (next = v->fFirstEdgeBelow; next; next = next->fNextEdgeBelow) {
if (next->isRightOf(edge->fBottom)) {
break;
}
prev = next;
}
list_insert<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>(
edge, prev, next, &v->fFirstEdgeBelow, &v->fLastEdgeBelow);
}
void remove_edge_above(Edge* edge) {
LOG("removing edge (%g -> %g) above vertex %g\n", edge->fTop->fID, edge->fBottom->fID,
edge->fBottom->fID);
list_remove<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>(
edge, &edge->fBottom->fFirstEdgeAbove, &edge->fBottom->fLastEdgeAbove);
}
void remove_edge_below(Edge* edge) {
LOG("removing edge (%g -> %g) below vertex %g\n", edge->fTop->fID, edge->fBottom->fID,
edge->fTop->fID);
list_remove<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>(
edge, &edge->fTop->fFirstEdgeBelow, &edge->fTop->fLastEdgeBelow);
}
void disconnect(Edge* edge)
{
remove_edge_above(edge);
remove_edge_below(edge);
}
void merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Vertex** current, Comparator& c);
void rewind(EdgeList* activeEdges, Vertex** current, Vertex* dst, Comparator& c) {
if (!current || *current == dst || c.sweep_lt((*current)->fPoint, dst->fPoint)) {
return;
}
Vertex* v = *current;
LOG("rewinding active edges from vertex %g to vertex %g\n", v->fID, dst->fID);
while (v != dst) {
v = v->fPrev;
for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
remove_edge(e, activeEdges);
}
Edge* leftEdge = v->fLeftEnclosingEdge;
for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
insert_edge(e, leftEdge, activeEdges);
leftEdge = e;
}
}
*current = v;
}
void rewind_if_necessary(Edge* edge, EdgeList* activeEdges, Vertex** current, Comparator& c) {
if (!activeEdges || !current) {
return;
}
Vertex* top = edge->fTop;
Vertex* bottom = edge->fBottom;
if (edge->fLeft) {
Vertex* leftTop = edge->fLeft->fTop;
Vertex* leftBottom = edge->fLeft->fBottom;
if (c.sweep_lt(leftTop->fPoint, top->fPoint) && !edge->fLeft->isLeftOf(top)) {
rewind(activeEdges, current, leftTop, c);
} else if (c.sweep_lt(top->fPoint, leftTop->fPoint) && !edge->isRightOf(leftTop)) {
rewind(activeEdges, current, top, c);
} else if (c.sweep_lt(bottom->fPoint, leftBottom->fPoint) &&
!edge->fLeft->isLeftOf(bottom)) {
rewind(activeEdges, current, leftTop, c);
} else if (c.sweep_lt(leftBottom->fPoint, bottom->fPoint) && !edge->isRightOf(leftBottom)) {
rewind(activeEdges, current, top, c);
}
}
if (edge->fRight) {
Vertex* rightTop = edge->fRight->fTop;
Vertex* rightBottom = edge->fRight->fBottom;
if (c.sweep_lt(rightTop->fPoint, top->fPoint) && !edge->fRight->isRightOf(top)) {
rewind(activeEdges, current, rightTop, c);
} else if (c.sweep_lt(top->fPoint, rightTop->fPoint) && !edge->isLeftOf(rightTop)) {
rewind(activeEdges, current, top, c);
} else if (c.sweep_lt(bottom->fPoint, rightBottom->fPoint) &&
!edge->fRight->isRightOf(bottom)) {
rewind(activeEdges, current, rightTop, c);
} else if (c.sweep_lt(rightBottom->fPoint, bottom->fPoint) &&
!edge->isLeftOf(rightBottom)) {
rewind(activeEdges, current, top, c);
}
}
}
void set_top(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current, Comparator& c) {
remove_edge_below(edge);
edge->fTop = v;
edge->recompute();
insert_edge_below(edge, v, c);
rewind_if_necessary(edge, activeEdges, current, c);
merge_collinear_edges(edge, activeEdges, current, c);
}
void set_bottom(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current, Comparator& c) {
remove_edge_above(edge);
edge->fBottom = v;
edge->recompute();
insert_edge_above(edge, v, c);
rewind_if_necessary(edge, activeEdges, current, c);
merge_collinear_edges(edge, activeEdges, current, c);
}
void merge_edges_above(Edge* edge, Edge* other, EdgeList* activeEdges, Vertex** current,
Comparator& c) {
if (coincident(edge->fTop->fPoint, other->fTop->fPoint)) {
LOG("merging coincident above edges (%g, %g) -> (%g, %g)\n",
edge->fTop->fPoint.fX, edge->fTop->fPoint.fY,
edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY);
rewind(activeEdges, current, edge->fTop, c);
other->fWinding += edge->fWinding;
disconnect(edge);
} else if (c.sweep_lt(edge->fTop->fPoint, other->fTop->fPoint)) {
rewind(activeEdges, current, edge->fTop, c);
other->fWinding += edge->fWinding;
set_bottom(edge, other->fTop, activeEdges, current, c);
} else {
rewind(activeEdges, current, other->fTop, c);
edge->fWinding += other->fWinding;
set_bottom(other, edge->fTop, activeEdges, current, c);
}
}
void merge_edges_below(Edge* edge, Edge* other, EdgeList* activeEdges, Vertex** current,
Comparator& c) {
if (coincident(edge->fBottom->fPoint, other->fBottom->fPoint)) {
LOG("merging coincident below edges (%g, %g) -> (%g, %g)\n",
edge->fTop->fPoint.fX, edge->fTop->fPoint.fY,
edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY);
rewind(activeEdges, current, edge->fTop, c);
other->fWinding += edge->fWinding;
disconnect(edge);
} else if (c.sweep_lt(edge->fBottom->fPoint, other->fBottom->fPoint)) {
rewind(activeEdges, current, other->fTop, c);
edge->fWinding += other->fWinding;
set_top(other, edge->fBottom, activeEdges, current, c);
} else {
rewind(activeEdges, current, edge->fTop, c);
other->fWinding += edge->fWinding;
set_top(edge, other->fBottom, activeEdges, current, c);
}
}
void merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Vertex** current, Comparator& c) {
for (;;) {
if (edge->fPrevEdgeAbove && (edge->fTop == edge->fPrevEdgeAbove->fTop ||
!edge->fPrevEdgeAbove->isLeftOf(edge->fTop))) {
merge_edges_above(edge, edge->fPrevEdgeAbove, activeEdges, current, c);
} else if (edge->fNextEdgeAbove && (edge->fTop == edge->fNextEdgeAbove->fTop ||
!edge->isLeftOf(edge->fNextEdgeAbove->fTop))) {
merge_edges_above(edge, edge->fNextEdgeAbove, activeEdges, current, c);
} else if (edge->fPrevEdgeBelow && (edge->fBottom == edge->fPrevEdgeBelow->fBottom ||
!edge->fPrevEdgeBelow->isLeftOf(edge->fBottom))) {
merge_edges_below(edge, edge->fPrevEdgeBelow, activeEdges, current, c);
} else if (edge->fNextEdgeBelow && (edge->fBottom == edge->fNextEdgeBelow->fBottom ||
!edge->isLeftOf(edge->fNextEdgeBelow->fBottom))) {
merge_edges_below(edge, edge->fNextEdgeBelow, activeEdges, current, c);
} else {
break;
}
}
SkASSERT(!edge->fPrevEdgeAbove || edge->fPrevEdgeAbove->isLeftOf(edge->fTop));
SkASSERT(!edge->fPrevEdgeBelow || edge->fPrevEdgeBelow->isLeftOf(edge->fBottom));
SkASSERT(!edge->fNextEdgeAbove || edge->fNextEdgeAbove->isRightOf(edge->fTop));
SkASSERT(!edge->fNextEdgeBelow || edge->fNextEdgeBelow->isRightOf(edge->fBottom));
}
void split_edge(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current, Comparator& c,
SkArenaAlloc& alloc) {
if (v == edge->fTop || v == edge->fBottom) {
return;
}
LOG("splitting edge (%g -> %g) at vertex %g (%g, %g)\n",
edge->fTop->fID, edge->fBottom->fID,
v->fID, v->fPoint.fX, v->fPoint.fY);
Vertex* top;
Vertex* bottom;
if (c.sweep_lt(v->fPoint, edge->fTop->fPoint)) {
top = v;
bottom = edge->fTop;
set_top(edge, v, activeEdges, current, c);
} else if (c.sweep_lt(edge->fBottom->fPoint, v->fPoint)) {
top = edge->fBottom;
bottom = v;
set_bottom(edge, v, activeEdges, current, c);
} else {
top = v;
bottom = edge->fBottom;
set_bottom(edge, v, activeEdges, current, c);
}
Edge* newEdge = alloc.make<Edge>(top, bottom, edge->fWinding, edge->fType);
insert_edge_below(newEdge, top, c);
insert_edge_above(newEdge, bottom, c);
merge_collinear_edges(newEdge, activeEdges, current, c);
}
Edge* connect(Vertex* prev, Vertex* next, Edge::Type type, Comparator& c, SkArenaAlloc& alloc,
int winding_scale = 1) {
if (!prev || !next || prev->fPoint == next->fPoint) {
return nullptr;
}
Edge* edge = new_edge(prev, next, type, c, alloc);
insert_edge_below(edge, edge->fTop, c);
insert_edge_above(edge, edge->fBottom, c);
edge->fWinding *= winding_scale;
merge_collinear_edges(edge, nullptr, nullptr, c);
return edge;
}
void merge_vertices(Vertex* src, Vertex* dst, VertexList* mesh, Comparator& c,
SkArenaAlloc& alloc) {
LOG("found coincident verts at %g, %g; merging %g into %g\n", src->fPoint.fX, src->fPoint.fY,
src->fID, dst->fID);
dst->fAlpha = SkTMax(src->fAlpha, dst->fAlpha);
if (src->fPartner) {
src->fPartner->fPartner = dst;
}
for (Edge* edge = src->fFirstEdgeAbove; edge;) {
Edge* next = edge->fNextEdgeAbove;
set_bottom(edge, dst, nullptr, nullptr, c);
edge = next;
}
for (Edge* edge = src->fFirstEdgeBelow; edge;) {
Edge* next = edge->fNextEdgeBelow;
set_top(edge, dst, nullptr, nullptr, c);
edge = next;
}
mesh->remove(src);
}
#ifdef GR_TESSELLATOR_LEGACY_INVERSION_HANDLING
uint8_t max_edge_alpha(Edge* a, Edge* b) {
if (a->fType == Edge::Type::kInner || b->fType == Edge::Type::kInner) {
return 255;
} else if (a->fType == Edge::Type::kOuter && b->fType == Edge::Type::kOuter) {
return 0;
} else {
return SkTMax(SkTMax(a->fTop->fAlpha, a->fBottom->fAlpha),
SkTMax(b->fTop->fAlpha, b->fBottom->fAlpha));
}
}
#endif
bool out_of_range_and_collinear(const SkPoint& p, Edge* edge, Comparator& c) {
if (c.sweep_lt(p, edge->fTop->fPoint) &&
!Line(p, edge->fBottom->fPoint).dist(edge->fTop->fPoint)) {
return true;
} else if (c.sweep_lt(edge->fBottom->fPoint, p) &&
!Line(edge->fTop->fPoint, p).dist(edge->fBottom->fPoint)) {
return true;
}
return false;
}
Vertex* create_sorted_vertex(const SkPoint& p, uint8_t alpha, VertexList* mesh,
Vertex* reference, Comparator& c, SkArenaAlloc& alloc) {
Vertex* prevV = reference;
while (prevV && c.sweep_lt(p, prevV->fPoint)) {
prevV = prevV->fPrev;
}
Vertex* nextV = prevV ? prevV->fNext : mesh->fHead;
while (nextV && c.sweep_lt(nextV->fPoint, p)) {
prevV = nextV;
nextV = nextV->fNext;
}
Vertex* v;
if (prevV && coincident(prevV->fPoint, p)) {
v = prevV;
} else if (nextV && coincident(nextV->fPoint, p)) {
v = nextV;
} else {
v = alloc.make<Vertex>(p, alpha);
#if LOGGING_ENABLED
if (!prevV) {
v->fID = mesh->fHead->fID - 1.0f;
} else if (!nextV) {
v->fID = mesh->fTail->fID + 1.0f;
} else {
v->fID = (prevV->fID + nextV->fID) * 0.5f;
}
#endif
mesh->insert(v, prevV, nextV);
}
return v;
}
bool check_for_intersection(Edge* edge, Edge* other, EdgeList* activeEdges, Vertex** current,
VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
if (!edge || !other) {
return false;
}
SkPoint p;
uint8_t alpha;
if (edge->intersect(*other, &p, &alpha) && p.isFinite()) {
// Ignore any out-of-range intersections which are also collinear,
// since the resulting edges would cancel each other out by merging.
if (out_of_range_and_collinear(p, edge, c) ||
out_of_range_and_collinear(p, other, c)) {
return false;
}
Vertex* v;
LOG("found intersection, pt is %g, %g\n", p.fX, p.fY);
Vertex* top = *current;
// If the intersection point is above the current vertex, rewind to the vertex above the
// intersection.
while (top && c.sweep_lt(p, top->fPoint)) {
top = top->fPrev;
}
if (p == edge->fTop->fPoint) {
v = edge->fTop;
} else if (p == edge->fBottom->fPoint) {
v = edge->fBottom;
} else if (p == other->fTop->fPoint) {
v = other->fTop;
} else if (p == other->fBottom->fPoint) {
v = other->fBottom;
} else {
v = create_sorted_vertex(p, alpha, mesh, top, c, alloc);
#ifndef GR_TESSELLATOR_LEGACY_INVERSION_HANDLING
if (edge->fTop->fPartner) {
Line line1 = edge->fLine;
Line line2 = other->fLine;
int dir = edge->fType == Edge::Type::kOuter ? -1 : 1;
line1.fC += sqrt(edge->fLine.magSq()) * (edge->fWinding > 0 ? 1 : -1) * dir;
line2.fC += sqrt(other->fLine.magSq()) * (other->fWinding > 0 ? 1 : -1) * dir;
SkPoint p;
if (line1.intersect(line2, &p)) {
LOG("synthesizing partner (%g,%g) for intersection vertex %g\n",
p.fX, p.fY, v->fID);
v->fPartner = alloc.make<Vertex>(p, 255 - v->fAlpha);
}
}
#endif
}
rewind(activeEdges, current, top ? top : v, c);
split_edge(edge, v, activeEdges, current, c, alloc);
split_edge(other, v, activeEdges, current, c, alloc);
v->fAlpha = SkTMax(v->fAlpha, alpha);
return true;
}
return false;
}
void sanitize_contours(VertexList* contours, int contourCnt, bool approximate) {
for (VertexList* contour = contours; contourCnt > 0; --contourCnt, ++contour) {
SkASSERT(contour->fHead);
Vertex* prev = contour->fTail;
if (approximate) {
round(&prev->fPoint);
}
for (Vertex* v = contour->fHead; v;) {
if (approximate) {
round(&v->fPoint);
}
Vertex* next = v->fNext;
if (coincident(prev->fPoint, v->fPoint)) {
LOG("vertex %g,%g coincident; removing\n", v->fPoint.fX, v->fPoint.fY);
contour->remove(v);
} else if (!v->fPoint.isFinite()) {
LOG("vertex %g,%g non-finite; removing\n", v->fPoint.fX, v->fPoint.fY);
contour->remove(v);
}
prev = v;
v = next;
}
}
}
bool merge_coincident_vertices(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
if (!mesh->fHead) {
return false;
}
bool merged = false;
for (Vertex* v = mesh->fHead->fNext; v;) {
Vertex* next = v->fNext;
if (c.sweep_lt(v->fPoint, v->fPrev->fPoint)) {
v->fPoint = v->fPrev->fPoint;
}
if (coincident(v->fPrev->fPoint, v->fPoint)) {
merge_vertices(v, v->fPrev, mesh, c, alloc);
merged = true;
}
v = next;
}
return merged;
}
// Stage 2: convert the contours to a mesh of edges connecting the vertices.
void build_edges(VertexList* contours, int contourCnt, VertexList* mesh, Comparator& c,
SkArenaAlloc& alloc) {
for (VertexList* contour = contours; contourCnt > 0; --contourCnt, ++contour) {
Vertex* prev = contour->fTail;
for (Vertex* v = contour->fHead; v;) {
Vertex* next = v->fNext;
connect(prev, v, Edge::Type::kInner, c, alloc);
mesh->append(v);
prev = v;
v = next;
}
}
}
void connect_partners(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
for (Vertex* outer = mesh->fHead; outer; outer = outer->fNext) {
if (Vertex* inner = outer->fPartner) {
if ((inner->fPrev || inner->fNext) && (outer->fPrev || outer->fNext)) {
// Connector edges get zero winding, since they're only structural (i.e., to ensure
// no 0-0-0 alpha triangles are produced), and shouldn't affect the poly winding
// number.
connect(outer, inner, Edge::Type::kConnector, c, alloc, 0);
inner->fPartner = outer->fPartner = nullptr;
}
}
}
}
template <CompareFunc sweep_lt>
void sorted_merge(VertexList* front, VertexList* back, VertexList* result) {
Vertex* a = front->fHead;
Vertex* b = back->fHead;
while (a && b) {
if (sweep_lt(a->fPoint, b->fPoint)) {
front->remove(a);
result->append(a);
a = front->fHead;
} else {
back->remove(b);
result->append(b);
b = back->fHead;
}
}
result->append(*front);
result->append(*back);
}
void sorted_merge(VertexList* front, VertexList* back, VertexList* result, Comparator& c) {
if (c.fDirection == Comparator::Direction::kHorizontal) {
sorted_merge<sweep_lt_horiz>(front, back, result);
} else {
sorted_merge<sweep_lt_vert>(front, back, result);
}
#if LOGGING_ENABLED
float id = 0.0f;
for (Vertex* v = result->fHead; v; v = v->fNext) {
v->fID = id++;
}
#endif
}
// Stage 3: sort the vertices by increasing sweep direction.
template <CompareFunc sweep_lt>
void merge_sort(VertexList* vertices) {
Vertex* slow = vertices->fHead;
if (!slow) {
return;
}
Vertex* fast = slow->fNext;
if (!fast) {
return;
}
do {
fast = fast->fNext;
if (fast) {
fast = fast->fNext;
slow = slow->fNext;
}
} while (fast);
VertexList front(vertices->fHead, slow);
VertexList back(slow->fNext, vertices->fTail);
front.fTail->fNext = back.fHead->fPrev = nullptr;
merge_sort<sweep_lt>(&front);
merge_sort<sweep_lt>(&back);
vertices->fHead = vertices->fTail = nullptr;
sorted_merge<sweep_lt>(&front, &back, vertices);
}
void dump_mesh(const VertexList& mesh) {
#if LOGGING_ENABLED
for (Vertex* v = mesh.fHead; v; v = v->fNext) {
LOG("vertex %g (%g, %g) alpha %d", v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha);
if (Vertex* p = v->fPartner) {
LOG(", partner %g (%g, %g) alpha %d\n", p->fID, p->fPoint.fX, p->fPoint.fY, p->fAlpha);
} else {
LOG(", null partner\n");
}
for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
LOG(" edge %g -> %g, winding %d\n", e->fTop->fID, e->fBottom->fID, e->fWinding);
}
for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
LOG(" edge %g -> %g, winding %d\n", e->fTop->fID, e->fBottom->fID, e->fWinding);
}
}
#endif
}
// Stage 4: Simplify the mesh by inserting new vertices at intersecting edges.
bool simplify(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
LOG("simplifying complex polygons\n");
EdgeList activeEdges;
bool found = false;
for (Vertex* v = mesh->fHead; v != nullptr; v = v->fNext) {
if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) {
continue;
}
Edge* leftEnclosingEdge;
Edge* rightEnclosingEdge;
bool restartChecks;
do {
LOG("\nvertex %g: (%g,%g), alpha %d\n", v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha);
restartChecks = false;
find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
if (rightEnclosingEdge && !rightEnclosingEdge->isRightOf(v)) {
split_edge(rightEnclosingEdge, v, &activeEdges, &v, c, alloc);
restartChecks = true;
continue;
}
SkASSERT(!rightEnclosingEdge || rightEnclosingEdge->isRightOf(v));
v->fLeftEnclosingEdge = leftEnclosingEdge;
v->fRightEnclosingEdge = rightEnclosingEdge;
if (v->fFirstEdgeBelow) {
for (Edge* edge = v->fFirstEdgeBelow; edge; edge = edge->fNextEdgeBelow) {
if (check_for_intersection(edge, leftEnclosingEdge, &activeEdges, &v, mesh, c,
alloc)) {
restartChecks = true;
break;
}
if (check_for_intersection(edge, rightEnclosingEdge, &activeEdges, &v, mesh, c,
alloc)) {
restartChecks = true;
break;
}
}
} else {
if (check_for_intersection(leftEnclosingEdge, rightEnclosingEdge,
&activeEdges, &v, mesh, c, alloc)) {
restartChecks = true;
}
}
found = found || restartChecks;
} while (restartChecks);
#ifdef GR_TESSELLATOR_LEGACY_INVERSION_HANDLING
if (v->fAlpha == 0) {
if ((leftEnclosingEdge && leftEnclosingEdge->fWinding < 0) &&
(rightEnclosingEdge && rightEnclosingEdge->fWinding > 0)) {
v->fAlpha = max_edge_alpha(leftEnclosingEdge, rightEnclosingEdge);
}
}
#endif
for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
remove_edge(e, &activeEdges);
}
Edge* leftEdge = leftEnclosingEdge;
for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
insert_edge(e, leftEdge, &activeEdges);
leftEdge = e;
}
}
SkASSERT(!activeEdges.fHead && !activeEdges.fTail);
return found;
}
#ifdef GR_TESSELLATOR_LEGACY_INVERSION_HANDLING
// This is a stripped-down version of simplify() (the Bentley-Ottmann algorithm) that
// early-returns true on the first found intersection, false if none.
bool is_complex(const VertexList& vertices) {
LOG("testing polygon complexity\n");
EdgeList activeEdges;
for (Vertex* v = vertices.fHead; v != nullptr; v = v->fNext) {
if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) {
continue;
}
Edge* leftEnclosingEdge;
Edge* rightEnclosingEdge;
find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
SkPoint dummy;
if (v->fFirstEdgeBelow) {
for (Edge* edge = v->fFirstEdgeBelow; edge; edge = edge->fNextEdgeBelow) {
if (edge && leftEnclosingEdge && edge->intersect(*leftEnclosingEdge, &dummy)) {
activeEdges.removeAll();
return true;
}
if (edge && rightEnclosingEdge && edge->intersect(*rightEnclosingEdge, &dummy)) {
activeEdges.removeAll();
return true;
}
}
} else if (leftEnclosingEdge && rightEnclosingEdge &&
leftEnclosingEdge->intersect(*rightEnclosingEdge, &dummy)) {
activeEdges.removeAll();
return true;
}
for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
remove_edge(e, &activeEdges);
}
Edge* leftEdge = leftEnclosingEdge;
for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
insert_edge(e, leftEdge, &activeEdges);
leftEdge = e;
}
}
activeEdges.removeAll();
return false;
}
#endif
// Stage 5: Tessellate the simplified mesh into monotone polygons.
Poly* tessellate(const VertexList& vertices, SkArenaAlloc& alloc) {
LOG("\ntessellating simple polygons\n");
EdgeList activeEdges;
Poly* polys = nullptr;
for (Vertex* v = vertices.fHead; v != nullptr; v = v->fNext) {
if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) {
continue;
}
#if LOGGING_ENABLED
LOG("\nvertex %g: (%g,%g), alpha %d\n", v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha);
#endif
Edge* leftEnclosingEdge;
Edge* rightEnclosingEdge;
find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
Poly* leftPoly;
Poly* rightPoly;
if (v->fFirstEdgeAbove) {
leftPoly = v->fFirstEdgeAbove->fLeftPoly;
rightPoly = v->fLastEdgeAbove->fRightPoly;
} else {
leftPoly = leftEnclosingEdge ? leftEnclosingEdge->fRightPoly : nullptr;
rightPoly = rightEnclosingEdge ? rightEnclosingEdge->fLeftPoly : nullptr;
}
#if LOGGING_ENABLED
LOG("edges above:\n");
for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID,
e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1);
}
LOG("edges below:\n");
for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID,
e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1);
}
#endif
if (v->fFirstEdgeAbove) {
if (leftPoly) {
leftPoly = leftPoly->addEdge(v->fFirstEdgeAbove, Poly::kRight_Side, alloc);
}
if (rightPoly) {
rightPoly = rightPoly->addEdge(v->fLastEdgeAbove, Poly::kLeft_Side, alloc);
}
for (Edge* e = v->fFirstEdgeAbove; e != v->fLastEdgeAbove; e = e->fNextEdgeAbove) {
Edge* rightEdge = e->fNextEdgeAbove;
remove_edge(e, &activeEdges);
if (e->fRightPoly) {
e->fRightPoly->addEdge(e, Poly::kLeft_Side, alloc);
}
if (rightEdge->fLeftPoly && rightEdge->fLeftPoly != e->fRightPoly) {
rightEdge->fLeftPoly->addEdge(e, Poly::kRight_Side, alloc);
}
}
remove_edge(v->fLastEdgeAbove, &activeEdges);
if (!v->fFirstEdgeBelow) {
if (leftPoly && rightPoly && leftPoly != rightPoly) {
SkASSERT(leftPoly->fPartner == nullptr && rightPoly->fPartner == nullptr);
rightPoly->fPartner = leftPoly;
leftPoly->fPartner = rightPoly;
}
}
}
if (v->fFirstEdgeBelow) {
if (!v->fFirstEdgeAbove) {
if (leftPoly && rightPoly) {
if (leftPoly == rightPoly) {
if (leftPoly->fTail && leftPoly->fTail->fSide == Poly::kLeft_Side) {
leftPoly = new_poly(&polys, leftPoly->lastVertex(),
leftPoly->fWinding, alloc);
leftEnclosingEdge->fRightPoly = leftPoly;
} else {
rightPoly = new_poly(&polys, rightPoly->lastVertex(),
rightPoly->fWinding, alloc);
rightEnclosingEdge->fLeftPoly = rightPoly;
}
}
Edge* join = alloc.make<Edge>(leftPoly->lastVertex(), v, 1, Edge::Type::kInner);
leftPoly = leftPoly->addEdge(join, Poly::kRight_Side, alloc);
rightPoly = rightPoly->addEdge(join, Poly::kLeft_Side, alloc);
}
}
Edge* leftEdge = v->fFirstEdgeBelow;
leftEdge->fLeftPoly = leftPoly;
insert_edge(leftEdge, leftEnclosingEdge, &activeEdges);
for (Edge* rightEdge = leftEdge->fNextEdgeBelow; rightEdge;
rightEdge = rightEdge->fNextEdgeBelow) {
insert_edge(rightEdge, leftEdge, &activeEdges);
int winding = leftEdge->fLeftPoly ? leftEdge->fLeftPoly->fWinding : 0;
winding += leftEdge->fWinding;
if (winding != 0) {
Poly* poly = new_poly(&polys, v, winding, alloc);
leftEdge->fRightPoly = rightEdge->fLeftPoly = poly;
}
leftEdge = rightEdge;
}
v->fLastEdgeBelow->fRightPoly = rightPoly;
}
#if LOGGING_ENABLED
LOG("\nactive edges:\n");
for (Edge* e = activeEdges.fHead; e != nullptr; e = e->fRight) {
LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID,
e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1);
}
#endif
}
return polys;
}
void remove_non_boundary_edges(const VertexList& mesh, SkPath::FillType fillType,
SkArenaAlloc& alloc) {
LOG("removing non-boundary edges\n");
EdgeList activeEdges;
for (Vertex* v = mesh.fHead; v != nullptr; v = v->fNext) {
if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) {
continue;
}
Edge* leftEnclosingEdge;
Edge* rightEnclosingEdge;
find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
bool prevFilled = leftEnclosingEdge &&
apply_fill_type(fillType, leftEnclosingEdge->fWinding);
for (Edge* e = v->fFirstEdgeAbove; e;) {
Edge* next = e->fNextEdgeAbove;
remove_edge(e, &activeEdges);
bool filled = apply_fill_type(fillType, e->fWinding);
if (filled == prevFilled) {
disconnect(e);
}
prevFilled = filled;
e = next;
}
Edge* prev = leftEnclosingEdge;
for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
if (prev) {
e->fWinding += prev->fWinding;
}
insert_edge(e, prev, &activeEdges);
prev = e;
}
}
}
// Note: this is the normal to the edge, but not necessarily unit length.
void get_edge_normal(const Edge* e, SkVector* normal) {
#ifdef GR_TESSELLATOR_LEGACY_INVERSION_HANDLING
normal->set(SkDoubleToScalar(e->fLine.fA) * e->fWinding,
SkDoubleToScalar(e->fLine.fB) * e->fWinding);
#else
normal->set(SkDoubleToScalar(e->fLine.fA),
SkDoubleToScalar(e->fLine.fB));
#endif
}
#ifndef GR_TESSELLATOR_LEGACY_INVERSION_HANDLING
void reconnect(Edge* edge, Vertex* src, Vertex* dst, Comparator& c) {
disconnect(edge);
if (src == edge->fTop) {
edge->fTop = dst;
} else {
SkASSERT(src == edge->fBottom);
edge->fBottom = dst;
}
if (edge->fEvent) {
edge->fEvent->fEdge = nullptr;
}
if (edge->fTop == edge->fBottom) {
return;
}
if (c.sweep_lt(edge->fBottom->fPoint, edge->fTop->fPoint)) {
SkTSwap(edge->fTop, edge->fBottom);
edge->fWinding *= -1;
}
edge->recompute();
insert_edge_below(edge, edge->fTop, c);
insert_edge_above(edge, edge->fBottom, c);
merge_collinear_edges(edge, nullptr, nullptr, c);
}
#endif
// Stage 5c: detect and remove "pointy" vertices whose edge normals point in opposite directions
// and whose adjacent vertices are less than a quarter pixel from an edge. These are guaranteed to
// invert on stroking.
void simplify_boundary(EdgeList* boundary, Comparator& c, SkArenaAlloc& alloc) {
Edge* prevEdge = boundary->fTail;
SkVector prevNormal;
get_edge_normal(prevEdge, &prevNormal);
for (Edge* e = boundary->fHead; e != nullptr;) {
Vertex* prev = prevEdge->fWinding == 1 ? prevEdge->fTop : prevEdge->fBottom;
Vertex* next = e->fWinding == 1 ? e->fBottom : e->fTop;
double dist = e->dist(prev->fPoint);
SkVector normal;
get_edge_normal(e, &normal);
#ifdef GR_TESSELLATOR_LEGACY_INVERSION_HANDLING
double denom = 0.0625f * e->fLine.magSq();
#else
double denom = 0.0625f;
#endif
if (prevNormal.dot(normal) < 0.0 && (dist * dist) <= denom) {
Edge* join = new_edge(prev, next, Edge::Type::kInner, c, alloc);
#ifndef GR_TESSELLATOR_LEGACY_INVERSION_HANDLING
if (prev->fPoint != next->fPoint) {
join->fLine.normalize();
join->fLine = join->fLine * join->fWinding;
}
#endif
insert_edge(join, e, boundary);
remove_edge(prevEdge, boundary);
remove_edge(e, boundary);
if (join->fLeft && join->fRight) {
prevEdge = join->fLeft;
e = join;
} else {
prevEdge = boundary->fTail;
e = boundary->fHead; // join->fLeft ? join->fLeft : join;
}
get_edge_normal(prevEdge, &prevNormal);
} else {
prevEdge = e;
prevNormal = normal;
e = e->fRight;
}
}
}
#ifdef GR_TESSELLATOR_LEGACY_INVERSION_HANDLING
void fix_inversions(Vertex* prev, Vertex* next, Edge* prevBisector, Edge* nextBisector,
Edge* prevEdge, Comparator& c) {
if (!prev || !next) {
return;
}
int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1;
SkPoint p;
uint8_t alpha;
if (winding != prevEdge->fWinding && prevBisector->intersect(*nextBisector, &p, &alpha)) {
prev->fPoint = next->fPoint = p;
prev->fAlpha = next->fAlpha = alpha;
}
}
#else
void reconnect_all_overlap_edges(Vertex* src, Vertex* dst, Edge* current, Comparator& c) {
if (src->fPartner) {
src->fPartner->fPartner = dst;
}
for (Edge* e = src->fFirstEdgeAbove; e; ) {
Edge* next = e->fNextEdgeAbove;
if (e->fOverlap && e != current) {
reconnect(e, src, dst, c);
}
e = next;
}
for (Edge* e = src->fFirstEdgeBelow; e; ) {
Edge* next = e->fNextEdgeBelow;
if (e->fOverlap && e != current) {
reconnect(e, src, dst, c);
}
e = next;
}
}
void Event::apply(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
if (!fEdge) {
return;
}
Vertex* top = fEdge->fTop;
Vertex* bottom = fEdge->fBottom;
Vertex* dest = create_sorted_vertex(fPoint, fAlpha, mesh, fEdge->fTop, c, alloc);
LOG("collapsing edge %g -> %g to %g (%g, %g) alpha %d\n",
top->fID, bottom->fID, dest->fID, fPoint.fX, fPoint.fY, fAlpha);
reconnect_all_overlap_edges(top, dest, fEdge, c);
reconnect_all_overlap_edges(bottom, dest, fEdge, c);
// Since the destination has multiple partners, give it none.
dest->fPartner = nullptr;
disconnect(fEdge);
// If top still has some connected edges, set its partner to dest.
top->fPartner = top->fFirstEdgeAbove || top->fFirstEdgeBelow ? dest : nullptr;
// If bottom still has some connected edges, set its partner to dest.
bottom->fPartner = bottom->fFirstEdgeAbove || bottom->fFirstEdgeBelow ? dest : nullptr;
}
bool is_overlap_edge(Edge* e) {
if (e->fType == Edge::Type::kOuter) {
return e->fWinding != 0 && e->fWinding != 1;
} else if (e->fType == Edge::Type::kInner) {
return e->fWinding != 0 && e->fWinding != -2;
} else {
return false;
}
}
// This is a stripped-down version of tessellate() which computes edges which
// join two filled regions, which represent overlap regions, and collapses them.
bool collapse_overlap_regions(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
LOG("\nfinding overlap regions\n");
EdgeList activeEdges;
EventList events;
for (Vertex* v = mesh->fHead; v != nullptr; v = v->fNext) {
if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) {
continue;
}
Edge* leftEnclosingEdge;
Edge* rightEnclosingEdge;
find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
for (Edge* e = v->fLastEdgeAbove; e; e = e->fPrevEdgeAbove) {
Edge* prev = e->fPrevEdgeAbove ? e->fPrevEdgeAbove : leftEnclosingEdge;
remove_edge(e, &activeEdges);
if (prev) {
e->fWinding -= prev->fWinding;
}
}
Edge* prev = leftEnclosingEdge;
for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
if (prev) {
e->fWinding += prev->fWinding;
e->fOverlap = e->fOverlap || is_overlap_edge(prev);
}
e->fOverlap = e->fOverlap || is_overlap_edge(e);
if (e->fOverlap) {
create_event(e, &events, alloc);
}
insert_edge(e, prev, &activeEdges);
prev = e;
}
}
LOG("\ncollapsing overlap regions\n");
if (events.count() == 0) {
return false;
}
while (events.count() > 0) {
Event* event = events.peek();
events.pop();
event->apply(mesh, c, alloc);
}
return true;
}
bool inversion(Vertex* prev, Vertex* next, Edge* origEdge, Comparator& c) {
if (!prev || !next) {
return true;
}
int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1;
return winding != origEdge->fWinding;
}
#endif
// Stage 5d: Displace edges by half a pixel inward and outward along their normals. Intersect to
// find new vertices, and set zero alpha on the exterior and one alpha on the interior. Build a
// new antialiased mesh from those vertices.
#ifdef GR_TESSELLATOR_LEGACY_INVERSION_HANDLING
void stroke_boundary(EdgeList* boundary, VertexList* innerMesh, VertexList* outerMesh,
Comparator& c, SkArenaAlloc& alloc) {
// A boundary with fewer than 3 edges is degenerate.
if (!boundary->fHead || !boundary->fHead->fRight || !boundary->fHead->fRight->fRight) {
return;
}
Edge* prevEdge = boundary->fTail;
float radius = 0.5f;
double offset = radius * sqrt(prevEdge->fLine.magSq()) * prevEdge->fWinding;
Line prevInner(prevEdge->fLine);
prevInner.fC -= offset;
Line prevOuter(prevEdge->fLine);
prevOuter.fC += offset;
VertexList innerVertices;
VertexList outerVertices;
Edge* prevBisector = nullptr;
for (Edge* e = boundary->fHead; e != nullptr; e = e->fRight) {
double offset = radius * sqrt(e->fLine.magSq()) * e->fWinding;
Line inner(e->fLine);
inner.fC -= offset;
Line outer(e->fLine);
outer.fC += offset;
SkPoint innerPoint, outerPoint;
if (prevInner.intersect(inner, &innerPoint) &&
prevOuter.intersect(outer, &outerPoint)) {
Vertex* innerVertex = alloc.make<Vertex>(innerPoint, 255);
Vertex* outerVertex = alloc.make<Vertex>(outerPoint, 0);
Edge* bisector = new_edge(outerVertex, innerVertex, Edge::Type::kConnector, c, alloc);
fix_inversions(innerVertices.fTail, innerVertex, prevBisector, bisector, prevEdge, c);
fix_inversions(outerVertices.fTail, outerVertex, prevBisector, bisector, prevEdge, c);
innerVertex->fPartner = outerVertex;
outerVertex->fPartner = innerVertex;
innerVertices.append(innerVertex);
outerVertices.append(outerVertex);
prevBisector = bisector;
}
prevInner = inner;
prevOuter = outer;
prevEdge = e;
}
Vertex* innerVertex = innerVertices.fHead;
Vertex* outerVertex = outerVertices.fHead;
if (!innerVertex || !outerVertex) {
return;
}
Edge* bisector = new_edge(outerVertices.fHead, innerVertices.fHead, Edge::Type::kConnector, c,
alloc);
fix_inversions(innerVertices.fTail, innerVertices.fHead, prevBisector, bisector, prevEdge, c);
fix_inversions(outerVertices.fTail, outerVertices.fHead, prevBisector, bisector, prevEdge, c);
Vertex* prevInnerVertex = innerVertices.fTail;
Vertex* prevOuterVertex = outerVertices.fTail;
while (innerVertex && outerVertex) {
// Connect vertices into a quad mesh. Outer edges get default (1) winding.
// Inner edges get -2 winding. This ensures that the interior is always filled
// (-1 winding number for normal cases, 3 for thin features where the interior inverts).
connect(prevOuterVertex, outerVertex, Edge::Type::kOuter, c, alloc);
connect(prevInnerVertex, innerVertex, Edge::Type::kInner, c, alloc, -2);
prevInnerVertex = innerVertex;
prevOuterVertex = outerVertex;
innerVertex = innerVertex->fNext;
outerVertex = outerVertex->fNext;
}
innerMesh->append(innerVertices);
outerMesh->append(outerVertices);
}
#else
void stroke_boundary(EdgeList* boundary, VertexList* innerMesh, VertexList* outerMesh,
Comparator& c, SkArenaAlloc& alloc) {
LOG("\nstroking boundary\n");
// A boundary with fewer than 3 edges is degenerate.
if (!boundary->fHead || !boundary->fHead->fRight || !boundary->fHead->fRight->fRight) {
return;
}
Edge* prevEdge = boundary->fTail;
Vertex* prevV = prevEdge->fWinding > 0 ? prevEdge->fTop : prevEdge->fBottom;
SkVector prevNormal;
get_edge_normal(prevEdge, &prevNormal);
double radius = 0.5;
Line prevInner(prevEdge->fLine);
prevInner.fC -= radius;
Line prevOuter(prevEdge->fLine);
prevOuter.fC += radius;
VertexList innerVertices;
VertexList outerVertices;
bool innerInversion = true;
bool outerInversion = true;
for (Edge* e = boundary->fHead; e != nullptr; e = e->fRight) {
Vertex* v = e->fWinding > 0 ? e->fTop : e->fBottom;
SkVector normal;
get_edge_normal(e, &normal);
Line inner(e->fLine);
inner.fC -= radius;
Line outer(e->fLine);
outer.fC += radius;
SkPoint innerPoint, outerPoint;
LOG("stroking vertex %g (%g, %g)\n", v->fID, v->fPoint.fX, v->fPoint.fY);
if (!prevEdge->fLine.nearParallel(e->fLine) && prevInner.intersect(inner, &innerPoint) &&
prevOuter.intersect(outer, &outerPoint)) {
float cosAngle = normal.dot(prevNormal);
if (cosAngle < -kCosMiterAngle) {
Vertex* nextV = e->fWinding > 0 ? e->fBottom : e->fTop;
// This is a pointy vertex whose angle is smaller than the threshold; miter it.
Line bisector(innerPoint, outerPoint);
Line tangent(v->fPoint, v->fPoint + SkPoint::Make(bisector.fA, bisector.fB));
if (tangent.fA == 0 && tangent.fB == 0) {
continue;
}
tangent.normalize();
Line innerTangent(tangent);
Line outerTangent(tangent);
innerTangent.fC -= 0.5;
outerTangent.fC += 0.5;
SkPoint innerPoint1, innerPoint2, outerPoint1, outerPoint2;
if (prevNormal.cross(normal) > 0) {
// Miter inner points
if (!innerTangent.intersect(prevInner, &innerPoint1) ||
!innerTangent.intersect(inner, &innerPoint2) ||
!outerTangent.intersect(bisector, &outerPoint)) {
continue;
}
Line prevTangent(prevV->fPoint,
prevV->fPoint + SkVector::Make(prevOuter.fA, prevOuter.fB));
Line nextTangent(nextV->fPoint,
nextV->fPoint + SkVector::Make(outer.fA, outer.fB));
if (prevTangent.dist(outerPoint) > 0) {
bisector.intersect(prevTangent, &outerPoint);
}
if (nextTangent.dist(outerPoint) < 0) {
bisector.intersect(nextTangent, &outerPoint);
}
outerPoint1 = outerPoint2 = outerPoint;
} else {
// Miter outer points
if (!outerTangent.intersect(prevOuter, &outerPoint1) ||
!outerTangent.intersect(outer, &outerPoint2)) {
continue;
}
Line prevTangent(prevV->fPoint,
prevV->fPoint + SkVector::Make(prevInner.fA, prevInner.fB));
Line nextTangent(nextV->fPoint,
nextV->fPoint + SkVector::Make(inner.fA, inner.fB));
if (prevTangent.dist(innerPoint) > 0) {
bisector.intersect(prevTangent, &innerPoint);
}
if (nextTangent.dist(innerPoint) < 0) {
bisector.intersect(nextTangent, &innerPoint);
}
innerPoint1 = innerPoint2 = innerPoint;
}
LOG("inner (%g, %g), (%g, %g), ",
innerPoint1.fX, innerPoint1.fY, innerPoint2.fX, innerPoint2.fY);
LOG("outer (%g, %g), (%g, %g)\n",
outerPoint1.fX, outerPoint1.fY, outerPoint2.fX, outerPoint2.fY);
Vertex* innerVertex1 = alloc.make<Vertex>(innerPoint1, 255);
Vertex* innerVertex2 = alloc.make<Vertex>(innerPoint2, 255);
Vertex* outerVertex1 = alloc.make<Vertex>(outerPoint1, 0);
Vertex* outerVertex2 = alloc.make<Vertex>(outerPoint2, 0);
innerVertex1->fPartner = outerVertex1;
innerVertex2->fPartner = outerVertex2;
outerVertex1->fPartner = innerVertex1;
outerVertex2->fPartner = innerVertex2;
if (!inversion(innerVertices.fTail, innerVertex1, prevEdge, c)) {
innerInversion = false;
}
if (!inversion(outerVertices.fTail, outerVertex1, prevEdge, c)) {
outerInversion = false;
}
innerVertices.append(innerVertex1);
innerVertices.append(innerVertex2);
outerVertices.append(outerVertex1);
outerVertices.append(outerVertex2);
} else {
LOG("inner (%g, %g), ", innerPoint.fX, innerPoint.fY);
LOG("outer (%g, %g)\n", outerPoint.fX, outerPoint.fY);
Vertex* innerVertex = alloc.make<Vertex>(innerPoint, 255);
Vertex* outerVertex = alloc.make<Vertex>(outerPoint, 0);
innerVertex->fPartner = outerVertex;
outerVertex->fPartner = innerVertex;
if (!inversion(innerVertices.fTail, innerVertex, prevEdge, c)) {
innerInversion = false;
}
if (!inversion(outerVertices.fTail, outerVertex, prevEdge, c)) {
outerInversion = false;
}
innerVertices.append(innerVertex);
outerVertices.append(outerVertex);
}
}
prevInner = inner;
prevOuter = outer;
prevV = v;
prevEdge = e;
prevNormal = normal;
}
if (!inversion(innerVertices.fTail, innerVertices.fHead, prevEdge, c)) {
innerInversion = false;
}
if (!inversion(outerVertices.fTail, outerVertices.fHead, prevEdge, c)) {
outerInversion = false;
}
// Outer edges get 1 winding, and inner edges get -2 winding. This ensures that the interior
// is always filled (1 + -2 = -1 for normal cases, 1 + 2 = 3 for thin features where the
// interior inverts).
// For total inversion cases, the shape has now reversed handedness, so invert the winding
// so it will be detected during collapse_overlap_regions().
int innerWinding = innerInversion ? 2 : -2;
int outerWinding = outerInversion ? -1 : 1;
for (Vertex* v = innerVertices.fHead; v && v->fNext; v = v->fNext) {
connect(v, v->fNext, Edge::Type::kInner, c, alloc, innerWinding);
}
connect(innerVertices.fTail, innerVertices.fHead, Edge::Type::kInner, c, alloc, innerWinding);
for (Vertex* v = outerVertices.fHead; v && v->fNext; v = v->fNext) {
connect(v, v->fNext, Edge::Type::kOuter, c, alloc, outerWinding);
}
connect(outerVertices.fTail, outerVertices.fHead, Edge::Type::kOuter, c, alloc, outerWinding);
innerMesh->append(innerVertices);
outerMesh->append(outerVertices);
}
#endif
void extract_boundary(EdgeList* boundary, Edge* e, SkPath::FillType fillType, SkArenaAlloc& alloc) {
LOG("\nextracting boundary\n");
bool down = apply_fill_type(fillType, e->fWinding);
while (e) {
e->fWinding = down ? 1 : -1;
Edge* next;
#ifndef GR_TESSELLATOR_LEGACY_INVERSION_HANDLING
e->fLine.normalize();
e->fLine = e->fLine * e->fWinding;
#endif
boundary->append(e);
if (down) {
// Find outgoing edge, in clockwise order.
if ((next = e->fNextEdgeAbove)) {
down = false;
} else if ((next = e->fBottom->fLastEdgeBelow)) {
down = true;
} else if ((next = e->fPrevEdgeAbove)) {
down = false;
}
} else {
// Find outgoing edge, in counter-clockwise order.
if ((next = e->fPrevEdgeBelow)) {
down = true;
} else if ((next = e->fTop->fFirstEdgeAbove)) {
down = false;
} else if ((next = e->fNextEdgeBelow)) {
down = true;
}
}
disconnect(e);
e = next;
}
}
// Stage 5b: Extract boundaries from mesh, simplify and stroke them into a new mesh.
void extract_boundaries(const VertexList& inMesh, VertexList* innerVertices,
VertexList* outerVertices, SkPath::FillType fillType,
Comparator& c, SkArenaAlloc& alloc) {
remove_non_boundary_edges(inMesh, fillType, alloc);
for (Vertex* v = inMesh.fHead; v; v = v->fNext) {
while (v->fFirstEdgeBelow) {
EdgeList boundary;
extract_boundary(&boundary, v->fFirstEdgeBelow, fillType, alloc);
simplify_boundary(&boundary, c, alloc);
stroke_boundary(&boundary, innerVertices, outerVertices, c, alloc);
}
}
}
// This is a driver function that calls stages 2-5 in turn.
void contours_to_mesh(VertexList* contours, int contourCnt, bool antialias,
VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
#if LOGGING_ENABLED
for (int i = 0; i < contourCnt; ++i) {
Vertex* v = contours[i].fHead;
SkASSERT(v);
LOG("path.moveTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
for (v = v->fNext; v; v = v->fNext) {
LOG("path.lineTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
}
}
#endif
sanitize_contours(contours, contourCnt, antialias);
build_edges(contours, contourCnt, mesh, c, alloc);
}
void sort_mesh(VertexList* vertices, Comparator& c, SkArenaAlloc& alloc) {
if (!vertices || !vertices->fHead) {
return;
}
// Sort vertices in Y (secondarily in X).
if (c.fDirection == Comparator::Direction::kHorizontal) {
merge_sort<sweep_lt_horiz>(vertices);
} else {
merge_sort<sweep_lt_vert>(vertices);
}
#if LOGGING_ENABLED
for (Vertex* v = vertices->fHead; v != nullptr; v = v->fNext) {
static float gID = 0.0f;
v->fID = gID++;
}
#endif
}
Poly* contours_to_polys(VertexList* contours, int contourCnt, SkPath::FillType fillType,
const SkRect& pathBounds, bool antialias, VertexList* outerMesh,
SkArenaAlloc& alloc) {
Comparator c(pathBounds.width() > pathBounds.height() ? Comparator::Direction::kHorizontal
: Comparator::Direction::kVertical);
VertexList mesh;
contours_to_mesh(contours, contourCnt, antialias, &mesh, c, alloc);
sort_mesh(&mesh, c, alloc);
merge_coincident_vertices(&mesh, c, alloc);
simplify(&mesh, c, alloc);
if (antialias) {
VertexList innerMesh;
extract_boundaries(mesh, &innerMesh, outerMesh, fillType, c, alloc);
sort_mesh(&innerMesh, c, alloc);
sort_mesh(outerMesh, c, alloc);
#ifdef GR_TESSELLATOR_LEGACY_INVERSION_HANDLING
if (is_complex(innerMesh) || is_complex(*outerMesh)) {
#else
merge_coincident_vertices(&innerMesh, c, alloc);
bool was_complex = merge_coincident_vertices(outerMesh, c, alloc);
was_complex = simplify(&innerMesh, c, alloc) || was_complex;
was_complex = simplify(outerMesh, c, alloc) || was_complex;
LOG("\ninner mesh before:\n");
dump_mesh(innerMesh);
LOG("\nouter mesh before:\n");
dump_mesh(*outerMesh);
was_complex = collapse_overlap_regions(&innerMesh, c, alloc) || was_complex;
was_complex = collapse_overlap_regions(outerMesh, c, alloc) || was_complex;
if (was_complex) {
#endif
LOG("found complex mesh; taking slow path\n");
VertexList aaMesh;
LOG("\ninner mesh after:\n");
dump_mesh(innerMesh);
LOG("\nouter mesh after:\n");
dump_mesh(*outerMesh);
connect_partners(outerMesh, c, alloc);
connect_partners(&innerMesh, c, alloc);
sorted_merge(&innerMesh, outerMesh, &aaMesh, c);
merge_coincident_vertices(&aaMesh, c, alloc);
simplify(&aaMesh, c, alloc);
dump_mesh(aaMesh);
outerMesh->fHead = outerMesh->fTail = nullptr;
return tessellate(aaMesh, alloc);
} else {
LOG("no complex polygons; taking fast path\n");
#ifdef GR_TESSELLATOR_LEGACY_INVERSION_HANDLING
merge_coincident_vertices(&innerMesh, c, alloc);
#endif
return tessellate(innerMesh, alloc);
}
} else {
return tessellate(mesh, alloc);
}
}
// Stage 6: Triangulate the monotone polygons into a vertex buffer.
void* polys_to_triangles(Poly* polys, SkPath::FillType fillType, const AAParams* aaParams,
void* data) {
for (Poly* poly = polys; poly; poly = poly->fNext) {
if (apply_fill_type(fillType, poly)) {
data = poly->emit(aaParams, data);
}
}
return data;
}
Poly* path_to_polys(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
int contourCnt, SkArenaAlloc& alloc, bool antialias, bool* isLinear,
VertexList* outerMesh) {
SkPath::FillType fillType = path.getFillType();
if (SkPath::IsInverseFillType(fillType)) {
contourCnt++;
}
std::unique_ptr<VertexList[]> contours(new VertexList[contourCnt]);
path_to_contours(path, tolerance, clipBounds, contours.get(), alloc, isLinear);
return contours_to_polys(contours.get(), contourCnt, path.getFillType(), path.getBounds(),
antialias, outerMesh, alloc);
}
int get_contour_count(const SkPath& path, SkScalar tolerance) {
int contourCnt;
int maxPts = GrPathUtils::worstCasePointCount(path, &contourCnt, tolerance);
if (maxPts <= 0) {
return 0;
}
if (maxPts > ((int)SK_MaxU16 + 1)) {
SkDebugf("Path not rendered, too many verts (%d)\n", maxPts);
return 0;
}
return contourCnt;
}
int count_points(Poly* polys, SkPath::FillType fillType) {
int count = 0;
for (Poly* poly = polys; poly; poly = poly->fNext) {
if (apply_fill_type(fillType, poly) && poly->fCount >= 3) {
count += (poly->fCount - 2) * (TESSELLATOR_WIREFRAME ? 6 : 3);
}
}
return count;
}
int count_outer_mesh_points(const VertexList& outerMesh) {
int count = 0;
for (Vertex* v = outerMesh.fHead; v; v = v->fNext) {
for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
count += TESSELLATOR_WIREFRAME ? 12 : 6;
}
}
return count;
}
void* outer_mesh_to_triangles(const VertexList& outerMesh, const AAParams* aaParams, void* data) {
for (Vertex* v = outerMesh.fHead; v; v = v->fNext) {
for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
Vertex* v0 = e->fTop;
Vertex* v1 = e->fBottom;
Vertex* v2 = e->fBottom->fPartner;
Vertex* v3 = e->fTop->fPartner;
data = emit_triangle(v0, v1, v2, aaParams, data);
data = emit_triangle(v0, v2, v3, aaParams, data);
}
}
return data;
}
} // namespace
namespace GrTessellator {
// Stage 6: Triangulate the monotone polygons into a vertex buffer.
int PathToTriangles(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
VertexAllocator* vertexAllocator, bool antialias, const GrColor& color,
bool canTweakAlphaForCoverage, bool* isLinear) {
int contourCnt = get_contour_count(path, tolerance);
if (contourCnt <= 0) {
*isLinear = true;
return 0;
}
SkArenaAlloc alloc(kArenaChunkSize);
VertexList outerMesh;
Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc, antialias,
isLinear, &outerMesh);
SkPath::FillType fillType = antialias ? SkPath::kWinding_FillType : path.getFillType();
int count = count_points(polys, fillType);
if (antialias) {
count += count_outer_mesh_points(outerMesh);
}
if (0 == count) {
return 0;
}
void* verts = vertexAllocator->lock(count);
if (!verts) {
SkDebugf("Could not allocate vertices\n");
return 0;
}
LOG("emitting %d verts\n", count);
AAParams aaParams;
aaParams.fTweakAlpha = canTweakAlphaForCoverage;
aaParams.fColor = color;
void* end = polys_to_triangles(polys, fillType, antialias ? &aaParams : nullptr, verts);
end = outer_mesh_to_triangles(outerMesh, &aaParams, end);
int actualCount = static_cast<int>((static_cast<uint8_t*>(end) - static_cast<uint8_t*>(verts))
/ vertexAllocator->stride());
SkASSERT(actualCount <= count);
vertexAllocator->unlock(actualCount);
return actualCount;
}
int PathToVertices(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
GrTessellator::WindingVertex** verts) {
int contourCnt = get_contour_count(path, tolerance);
if (contourCnt <= 0) {
return 0;
}
SkArenaAlloc alloc(kArenaChunkSize);
bool isLinear;
Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc, false, &isLinear,
nullptr);
SkPath::FillType fillType = path.getFillType();
int count = count_points(polys, fillType);
if (0 == count) {
*verts = nullptr;
return 0;
}
*verts = new GrTessellator::WindingVertex[count];
GrTessellator::WindingVertex* vertsEnd = *verts;
SkPoint* points = new SkPoint[count];
SkPoint* pointsEnd = points;
for (Poly* poly = polys; poly; poly = poly->fNext) {
if (apply_fill_type(fillType, poly)) {
SkPoint* start = pointsEnd;
pointsEnd = static_cast<SkPoint*>(poly->emit(nullptr, pointsEnd));
while (start != pointsEnd) {
vertsEnd->fPos = *start;
vertsEnd->fWinding = poly->fWinding;
++start;
++vertsEnd;
}
}
}
int actualCount = static_cast<int>(vertsEnd - *verts);
SkASSERT(actualCount <= count);
SkASSERT(pointsEnd - points == actualCount);
delete[] points;
return actualCount;
}
} // namespace
|