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
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use api::{AlphaType, BorderDetails, BorderDisplayItem, BuiltDisplayListIter, PrimitiveFlags};
use api::{ClipId, ColorF, CommonItemProperties, ComplexClipRegion, ComponentTransferFuncType, RasterSpace};
use api::{DisplayItem, DisplayItemRef, ExtendMode, ExternalScrollId, FilterData, SharedFontInstanceMap};
use api::{FilterOp, FilterPrimitive, FontInstanceKey, FontSize, GlyphInstance, GlyphOptions, GradientStop};
use api::{IframeDisplayItem, ImageKey, ImageRendering, ItemRange, ColorDepth, QualitySettings};
use api::{LineOrientation, LineStyle, NinePatchBorderSource, PipelineId, MixBlendMode, StackingContextFlags};
use api::{PropertyBinding, ReferenceFrameKind, ScrollFrameDisplayItem, ScrollSensitivity};
use api::{Shadow, SpaceAndClipInfo, SpatialId, StickyFrameDisplayItem, ImageMask, ItemTag};
use api::{ClipMode, PrimitiveKeyKind, TransformStyle, YuvColorSpace, ColorRange, YuvData, TempFilterData};
use api::{ReferenceTransformBinding, Rotation};
use api::units::*;
use crate::image_tiling::simplify_repeated_primitive;
use crate::clip::{ClipChainId, ClipRegion, ClipItemKey, ClipStore, ClipItemKeyKind};
use crate::clip::{ClipInternData, ClipNodeKind, ClipInstance, SceneClipInstance};
use crate::spatial_tree::{ROOT_SPATIAL_NODE_INDEX, SpatialTree, SpatialNodeIndex};
use crate::frame_builder::{ChasePrimitive, FrameBuilderConfig};
use crate::glyph_rasterizer::FontInstance;
use crate::hit_test::HitTestingScene;
use crate::intern::Interner;
use crate::internal_types::{FastHashMap, LayoutPrimitiveInfo, Filter};
use crate::picture::{Picture3DContext, PictureCompositeMode, PicturePrimitive, PictureOptions};
use crate::picture::{BlitReason, OrderedPictureChild, PrimitiveList};
use crate::prim_store::{PrimitiveInstance, register_prim_chase_id};
use crate::prim_store::{PrimitiveInstanceKind, NinePatchDescriptor, PrimitiveStore};
use crate::prim_store::{InternablePrimitive, SegmentInstanceIndex, PictureIndex};
use crate::prim_store::backdrop::Backdrop;
use crate::prim_store::borders::{ImageBorder, NormalBorderPrim};
use crate::prim_store::gradient::{GradientStopKey, LinearGradient, RadialGradient, RadialGradientParams, ConicGradient, ConicGradientParams};
use crate::prim_store::image::{Image, YuvImage};
use crate::prim_store::line_dec::{LineDecoration, LineDecorationCacheKey, get_line_decoration_size};
use crate::prim_store::picture::{Picture, PictureCompositeKey, PictureKey};
use crate::prim_store::text_run::TextRun;
use crate::render_backend::SceneView;
use crate::resource_cache::ImageRequest;
use crate::scene::{Scene, ScenePipeline, BuiltScene, SceneStats, StackingContextHelpers};
use crate::scene_builder_thread::Interners;
use crate::space::SpaceSnapper;
use crate::spatial_node::{StickyFrameInfo, ScrollFrameKind};
use crate::tile_cache::TileCacheBuilder;
use euclid::approxeq::ApproxEq;
use std::{f32, mem, usize};
use std::collections::vec_deque::VecDeque;
use std::sync::Arc;
use crate::util::{MaxRect, VecHelper};
use crate::filterdata::{SFilterDataComponent, SFilterData, SFilterDataKey};
use smallvec::SmallVec;
/// The offset stack for a given reference frame.
struct ReferenceFrameState {
/// A stack of current offsets from the current reference frame scope.
offsets: Vec<LayoutVector2D>,
}
/// Maps from stacking context layout coordinates into reference frame
/// relative coordinates.
struct ReferenceFrameMapper {
/// A stack of reference frame scopes.
frames: Vec<ReferenceFrameState>,
}
impl ReferenceFrameMapper {
fn new() -> Self {
ReferenceFrameMapper {
frames: vec![
ReferenceFrameState {
offsets: vec![
LayoutVector2D::zero(),
],
}
],
}
}
/// Push a new scope. This resets the current offset to zero, and is
/// used when a new reference frame or iframe is pushed.
fn push_scope(&mut self) {
self.frames.push(ReferenceFrameState {
offsets: vec![
LayoutVector2D::zero(),
],
});
}
/// Pop a reference frame scope off the stack.
fn pop_scope(&mut self) {
self.frames.pop().unwrap();
}
/// Push a new offset for the current scope. This is used when
/// a new stacking context is pushed.
fn push_offset(&mut self, offset: LayoutVector2D) {
let frame = self.frames.last_mut().unwrap();
let current_offset = *frame.offsets.last().unwrap();
frame.offsets.push(current_offset + offset);
}
/// Pop a local stacking context offset from the current scope.
fn pop_offset(&mut self) {
let frame = self.frames.last_mut().unwrap();
frame.offsets.pop().unwrap();
}
/// Retrieve the current offset to allow converting a stacking context
/// relative coordinate to be relative to the owing reference frame.
/// TODO(gw): We could perhaps have separate coordinate spaces for this,
/// however that's going to either mean a lot of changes to
/// public API code, or a lot of changes to internal code.
/// Before doing that, we should revisit how Gecko would
/// prefer to provide coordinates.
/// TODO(gw): For now, this includes only the reference frame relative
/// offset. Soon, we will expand this to include the initial
/// scroll offsets that are now available on scroll nodes. This
/// will allow normalizing the coordinates even between display
/// lists where APZ has scrolled the content.
fn current_offset(&self) -> LayoutVector2D {
*self.frames.last().unwrap().offsets.last().unwrap()
}
}
/// Offsets primitives (and clips) by the external scroll offset
/// supplied to scroll nodes.
pub struct ScrollOffsetMapper {
pub current_spatial_node: SpatialNodeIndex,
pub current_offset: LayoutVector2D,
}
impl ScrollOffsetMapper {
fn new() -> Self {
ScrollOffsetMapper {
current_spatial_node: SpatialNodeIndex::INVALID,
current_offset: LayoutVector2D::zero(),
}
}
/// Return the accumulated external scroll offset for a spatial
/// node. This caches the last result, which is the common case,
/// or defers to the spatial tree to build the value.
fn external_scroll_offset(
&mut self,
spatial_node_index: SpatialNodeIndex,
spatial_tree: &SpatialTree,
) -> LayoutVector2D {
if spatial_node_index != self.current_spatial_node {
self.current_spatial_node = spatial_node_index;
self.current_offset = spatial_tree.external_scroll_offset(spatial_node_index);
}
self.current_offset
}
}
/// A data structure that keeps track of mapping between API Ids for spatials and the indices
/// used internally in the SpatialTree to avoid having to do HashMap lookups for primitives
/// and clips during frame building.
#[derive(Default)]
pub struct NodeIdToIndexMapper {
spatial_node_map: FastHashMap<SpatialId, SpatialNodeIndex>,
}
impl NodeIdToIndexMapper {
fn add_spatial_node(&mut self, id: SpatialId, index: SpatialNodeIndex) {
let _old_value = self.spatial_node_map.insert(id, index);
debug_assert!(_old_value.is_none());
}
fn get_spatial_node_index(&self, id: SpatialId) -> SpatialNodeIndex {
self.spatial_node_map[&id]
}
}
#[derive(Debug, Clone, Default)]
pub struct CompositeOps {
// Requires only a single texture as input (e.g. most filters)
pub filters: Vec<Filter>,
pub filter_datas: Vec<FilterData>,
pub filter_primitives: Vec<FilterPrimitive>,
// Requires two source textures (e.g. mix-blend-mode)
pub mix_blend_mode: Option<MixBlendMode>,
}
impl CompositeOps {
pub fn new(
filters: Vec<Filter>,
filter_datas: Vec<FilterData>,
filter_primitives: Vec<FilterPrimitive>,
mix_blend_mode: Option<MixBlendMode>
) -> Self {
CompositeOps {
filters,
filter_datas,
filter_primitives,
mix_blend_mode,
}
}
pub fn is_empty(&self) -> bool {
self.filters.is_empty() &&
self.filter_primitives.is_empty() &&
self.mix_blend_mode.is_none()
}
/// Returns true if this CompositeOps contains any filters that affect
/// the content (false if no filters, or filters are all no-ops).
fn has_valid_filters(&self) -> bool {
// For each filter, create a new image with that composite mode.
let mut current_filter_data_index = 0;
for filter in &self.filters {
match filter {
Filter::ComponentTransfer => {
let filter_data =
&self.filter_datas[current_filter_data_index];
let filter_data = filter_data.sanitize();
current_filter_data_index = current_filter_data_index + 1;
if filter_data.is_identity() {
continue
} else {
return true;
}
}
_ => {
if filter.is_noop() {
continue;
} else {
return true;
}
}
}
}
if !self.filter_primitives.is_empty() {
return true;
}
false
}
}
/// Represents the current input for a picture chain builder (either a
/// prim list from the stacking context, or a wrapped picture instance).
enum PictureSource {
PrimitiveList {
prim_list: PrimitiveList,
},
WrappedPicture {
instance: PrimitiveInstance,
},
}
/// Helper struct to build picture chains during scene building from
/// a flattened stacking context struct.
struct PictureChainBuilder {
/// The current input source for the next picture
current: PictureSource,
/// Positioning node for this picture chain
spatial_node_index: SpatialNodeIndex,
/// Prim flags for any pictures in this chain
flags: PrimitiveFlags,
}
impl PictureChainBuilder {
/// Create a new picture chain builder, from a primitive list
fn from_prim_list(
prim_list: PrimitiveList,
flags: PrimitiveFlags,
spatial_node_index: SpatialNodeIndex,
) -> Self {
PictureChainBuilder {
current: PictureSource::PrimitiveList {
prim_list,
},
spatial_node_index,
flags,
}
}
/// Create a new picture chain builder, from a picture wrapper instance
fn from_instance(
instance: PrimitiveInstance,
flags: PrimitiveFlags,
spatial_node_index: SpatialNodeIndex,
) -> Self {
PictureChainBuilder {
current: PictureSource::WrappedPicture {
instance,
},
flags,
spatial_node_index,
}
}
/// Wrap the existing content with a new picture with the given parameters
#[must_use]
fn add_picture(
self,
composite_mode: PictureCompositeMode,
context_3d: Picture3DContext<OrderedPictureChild>,
options: PictureOptions,
interners: &mut Interners,
prim_store: &mut PrimitiveStore,
) -> PictureChainBuilder {
let prim_list = match self.current {
PictureSource::PrimitiveList { prim_list } => {
prim_list
}
PictureSource::WrappedPicture { instance } => {
let mut prim_list = PrimitiveList::empty();
prim_list.add_prim(
instance,
LayoutRect::zero(),
self.spatial_node_index,
self.flags,
);
prim_list
}
};
let pic_index = PictureIndex(prim_store.pictures
.alloc()
.init(PicturePrimitive::new_image(
Some(composite_mode.clone()),
context_3d,
true,
self.flags,
prim_list,
self.spatial_node_index,
options,
))
);
let instance = create_prim_instance(
pic_index,
Some(composite_mode).into(),
ClipChainId::NONE,
interners,
);
PictureChainBuilder {
current: PictureSource::WrappedPicture {
instance,
},
spatial_node_index: self.spatial_node_index,
flags: self.flags,
}
}
/// Finish building this picture chain. Set the clip chain on the outermost picture
fn finalize(
self,
clip_chain_id: ClipChainId,
interners: &mut Interners,
prim_store: &mut PrimitiveStore,
) -> PrimitiveInstance {
match self.current {
PictureSource::WrappedPicture { mut instance } => {
instance.clip_set.clip_chain_id = clip_chain_id;
instance
}
PictureSource::PrimitiveList { prim_list } => {
// If no picture was created for this stacking context, create a
// pass-through wrapper now. This is only needed in 1-2 edge cases
// now, and will be removed as a follow up.
let pic_index = PictureIndex(prim_store.pictures
.alloc()
.init(PicturePrimitive::new_image(
None,
Picture3DContext::Out,
true,
self.flags,
prim_list,
self.spatial_node_index,
PictureOptions::default(),
))
);
create_prim_instance(
pic_index,
None.into(),
clip_chain_id,
interners,
)
}
}
}
}
bitflags! {
/// Slice flags
pub struct SliceFlags : u8 {
/// Slice created by a prim that has PrimitiveFlags::IS_SCROLLBAR_CONTAINER
const IS_SCROLLBAR = 1;
}
}
/// A structure that converts a serialized display list into a form that WebRender
/// can use to later build a frame. This structure produces a BuiltScene. Public
/// members are typically those that are destructured into the BuiltScene.
pub struct SceneBuilder<'a> {
/// The scene that we are currently building.
scene: &'a Scene,
/// The map of all font instances.
font_instances: SharedFontInstanceMap,
/// The data structure that converts between ClipId/SpatialId and the various
/// index types that the SpatialTree uses.
id_to_index_mapper: NodeIdToIndexMapper,
/// A stack of stacking context properties.
sc_stack: Vec<FlattenedStackingContext>,
/// Stack of spatial node indices forming containing block for 3d contexts
containing_block_stack: Vec<SpatialNodeIndex>,
/// Stack of requested raster spaces for stacking contexts
raster_space_stack: Vec<RasterSpace>,
/// Maintains state for any currently active shadows
pending_shadow_items: VecDeque<ShadowItem>,
/// The SpatialTree that we are currently building during building.
pub spatial_tree: SpatialTree,
/// The store of primitives.
pub prim_store: PrimitiveStore,
/// Information about all primitives involved in hit testing.
pub hit_testing_scene: HitTestingScene,
/// The store which holds all complex clipping information.
pub clip_store: ClipStore,
/// The configuration to use for the FrameBuilder. We consult this in
/// order to determine the default font.
pub config: FrameBuilderConfig,
/// Reference to the set of data that is interned across display lists.
interners: &'a mut Interners,
/// Helper struct to map stacking context coords <-> reference frame coords.
rf_mapper: ReferenceFrameMapper,
/// Helper struct to map spatial nodes to external scroll offsets.
external_scroll_mapper: ScrollOffsetMapper,
/// The current recursion depth of iframes encountered. Used to restrict picture
/// caching slices to only the top-level content frame.
iframe_size: Vec<LayoutSize>,
/// The current quality / performance settings for this scene.
quality_settings: QualitySettings,
/// Maintains state about the list of tile caches being built for this scene.
tile_cache_builder: TileCacheBuilder,
/// A helper struct to snap local rects in device space. During frame
/// building we may establish new raster roots, however typically that is in
/// cases where we won't be applying snapping (e.g. has perspective), or in
/// edge cases (e.g. SVG filter) where we can accept slightly incorrect
/// behaviour in favour of getting the common case right.
snap_to_device: SpaceSnapper,
}
impl<'a> SceneBuilder<'a> {
pub fn build(
scene: &Scene,
font_instances: SharedFontInstanceMap,
view: &SceneView,
frame_builder_config: &FrameBuilderConfig,
interners: &mut Interners,
stats: &SceneStats,
) -> BuiltScene {
profile_scope!("build_scene");
// We checked that the root pipeline is available on the render backend.
let root_pipeline_id = scene.root_pipeline_id.unwrap();
let root_pipeline = scene.pipelines.get(&root_pipeline_id).unwrap();
let background_color = root_pipeline
.background_color
.and_then(|color| if color.a > 0.0 { Some(color) } else { None });
let device_pixel_scale = view.accumulated_scale_factor_for_snapping();
let spatial_tree = SpatialTree::new();
let snap_to_device = SpaceSnapper::new(
ROOT_SPATIAL_NODE_INDEX,
device_pixel_scale,
);
let mut builder = SceneBuilder {
scene,
spatial_tree,
font_instances,
config: *frame_builder_config,
id_to_index_mapper: NodeIdToIndexMapper::default(),
hit_testing_scene: HitTestingScene::new(&stats.hit_test_stats),
pending_shadow_items: VecDeque::new(),
sc_stack: Vec::new(),
containing_block_stack: Vec::new(),
raster_space_stack: vec![RasterSpace::Screen],
prim_store: PrimitiveStore::new(&stats.prim_store_stats),
clip_store: ClipStore::new(),
interners,
rf_mapper: ReferenceFrameMapper::new(),
external_scroll_mapper: ScrollOffsetMapper::new(),
iframe_size: Vec::new(),
quality_settings: view.quality_settings,
tile_cache_builder: TileCacheBuilder::new(),
snap_to_device,
};
builder.build_all(&root_pipeline);
// Construct the picture cache primitive instance(s) from the tile cache builder
let (tile_cache_config, tile_cache_pictures) = builder.tile_cache_builder.build(
&builder.config,
&mut builder.clip_store,
&mut builder.prim_store,
);
BuiltScene {
has_root_pipeline: scene.has_root_pipeline(),
pipeline_epochs: scene.pipeline_epochs.clone(),
output_rect: view.device_rect.size.into(),
background_color,
hit_testing_scene: Arc::new(builder.hit_testing_scene),
spatial_tree: builder.spatial_tree,
prim_store: builder.prim_store,
clip_store: builder.clip_store,
config: builder.config,
tile_cache_config,
tile_cache_pictures,
}
}
/// Retrieve the current offset to allow converting a stacking context
/// relative coordinate to be relative to the owing reference frame,
/// also considering any external scroll offset on the provided
/// spatial node.
fn current_offset(
&mut self,
spatial_node_index: SpatialNodeIndex,
) -> LayoutVector2D {
// Get the current offset from stacking context <-> reference frame space.
let rf_offset = self.rf_mapper.current_offset();
// Get the external scroll offset, if applicable.
let scroll_offset = self
.external_scroll_mapper
.external_scroll_offset(
spatial_node_index,
&self.spatial_tree,
);
rf_offset + scroll_offset
}
fn build_all(&mut self, root_pipeline: &ScenePipeline) {
enum ContextKind<'a> {
Root,
StackingContext {
sc_info: StackingContextInfo,
},
ReferenceFrame,
Iframe {
parent_traversal: BuiltDisplayListIter<'a>,
}
}
struct BuildContext<'a> {
pipeline_id: PipelineId,
kind: ContextKind<'a>,
}
let root_clip_id = ClipId::root(root_pipeline.pipeline_id);
self.clip_store.register_clip_template(root_clip_id, root_clip_id, &[]);
self.clip_store.push_clip_root(Some(root_clip_id), false);
self.push_root(
root_pipeline.pipeline_id,
&root_pipeline.viewport_size,
);
let mut stack = vec![BuildContext {
pipeline_id: root_pipeline.pipeline_id,
kind: ContextKind::Root,
}];
let mut traversal = root_pipeline.display_list.iter();
'outer: while let Some(bc) = stack.pop() {
loop {
let item = match traversal.next() {
Some(item) => item,
None => break,
};
match item.item() {
DisplayItem::PushStackingContext(ref info) => {
profile_scope!("build_stacking_context");
let spatial_node_index = self.get_space(info.spatial_id);
let mut subtraversal = item.sub_iter();
// Avoid doing unnecessary work for empty stacking contexts.
if subtraversal.current_stacking_context_empty() {
subtraversal.skip_current_stacking_context();
traversal = subtraversal;
continue;
}
let composition_operations = CompositeOps::new(
filter_ops_for_compositing(item.filters()),
filter_datas_for_compositing(item.filter_datas()),
filter_primitives_for_compositing(item.filter_primitives()),
info.stacking_context.mix_blend_mode_for_compositing(),
);
let sc_info = self.push_stacking_context(
composition_operations,
info.stacking_context.transform_style,
info.prim_flags,
spatial_node_index,
info.stacking_context.clip_id,
info.stacking_context.raster_space,
info.stacking_context.flags,
);
self.rf_mapper.push_offset(info.origin.to_vector());
let new_context = BuildContext {
pipeline_id: bc.pipeline_id,
kind: ContextKind::StackingContext {
sc_info,
},
};
stack.push(bc);
stack.push(new_context);
subtraversal.merge_debug_stats_from(&mut traversal);
traversal = subtraversal;
continue 'outer;
}
DisplayItem::PushReferenceFrame(ref info) => {
profile_scope!("build_reference_frame");
let parent_space = self.get_space(info.parent_spatial_id);
let mut subtraversal = item.sub_iter();
let current_offset = self.current_offset(parent_space);
let transform = match info.reference_frame.transform {
ReferenceTransformBinding::Static { binding } => binding,
ReferenceTransformBinding::Computed { scale_from, vertical_flip, rotation } => {
let content_size = &self.iframe_size.last().unwrap();
let mut transform = if let Some(scale_from) = scale_from {
// If we have a 90/270 degree rotation, then scale_from
// and content_size are in different coordinate spaces and
// we need to swap width/height for them to be correct.
match rotation {
Rotation::Degree0 |
Rotation::Degree180 => {
LayoutTransform::scale(
content_size.width / scale_from.width,
content_size.height / scale_from.height,
1.0
)
},
Rotation::Degree90 |
Rotation::Degree270 => {
LayoutTransform::scale(
content_size.height / scale_from.width,
content_size.width / scale_from.height,
1.0
)
}
}
} else {
LayoutTransform::identity()
};
if vertical_flip {
let content_size = &self.iframe_size.last().unwrap();
transform = transform
.then_translate(LayoutVector3D::new(0.0, content_size.height, 0.0))
.pre_scale(1.0, -1.0, 1.0);
}
let rotate = rotation.to_matrix(**content_size);
let transform = transform.then(&rotate);
PropertyBinding::Value(transform)
},
};
self.push_reference_frame(
info.reference_frame.id,
Some(parent_space),
bc.pipeline_id,
info.reference_frame.transform_style,
transform,
info.reference_frame.kind,
current_offset + info.origin.to_vector(),
);
self.rf_mapper.push_scope();
let new_context = BuildContext {
pipeline_id: bc.pipeline_id,
kind: ContextKind::ReferenceFrame,
};
stack.push(bc);
stack.push(new_context);
subtraversal.merge_debug_stats_from(&mut traversal);
traversal = subtraversal;
continue 'outer;
}
DisplayItem::PopReferenceFrame |
DisplayItem::PopStackingContext => break,
DisplayItem::Iframe(ref info) => {
profile_scope!("iframe");
let space = self.get_space(info.space_and_clip.spatial_id);
let (size, subtraversal) = match self.push_iframe(info, space) {
Some(pair) => pair,
None => continue,
};
// If this is a root iframe, force a new tile cache both before and after
// adding primitives for this iframe.
if self.iframe_size.is_empty() {
self.tile_cache_builder.add_tile_cache_barrier();
}
self.rf_mapper.push_scope();
self.iframe_size.push(size);
let new_context = BuildContext {
pipeline_id: info.pipeline_id,
kind: ContextKind::Iframe {
parent_traversal: mem::replace(&mut traversal, subtraversal),
},
};
stack.push(bc);
stack.push(new_context);
continue 'outer;
}
_ => {
self.build_item(item, bc.pipeline_id);
}
};
}
match bc.kind {
ContextKind::Root => {}
ContextKind::StackingContext { sc_info } => {
self.rf_mapper.pop_offset();
self.pop_stacking_context(sc_info);
}
ContextKind::ReferenceFrame => {
self.rf_mapper.pop_scope();
}
ContextKind::Iframe { parent_traversal } => {
self.iframe_size.pop();
self.rf_mapper.pop_scope();
self.clip_store.pop_clip_root();
if self.iframe_size.is_empty() {
self.tile_cache_builder.add_tile_cache_barrier();
}
traversal = parent_traversal;
}
}
// TODO: factor this out to be part of capture
if cfg!(feature = "display_list_stats") {
let stats = traversal.debug_stats();
let total_bytes: usize = stats.iter().map(|(_, stats)| stats.num_bytes).sum();
println!("item, total count, total bytes, % of DL bytes, bytes per item");
for (label, stats) in stats {
println!("{}, {}, {}kb, {}%, {}",
label,
stats.total_count,
stats.num_bytes / 1000,
((stats.num_bytes as f32 / total_bytes.max(1) as f32) * 100.0) as usize,
stats.num_bytes / stats.total_count.max(1));
}
println!();
}
}
self.clip_store.pop_clip_root();
debug_assert!(self.sc_stack.is_empty());
}
fn build_sticky_frame(
&mut self,
info: &StickyFrameDisplayItem,
parent_node_index: SpatialNodeIndex,
) {
let current_offset = self.current_offset(parent_node_index);
let frame_rect = info.bounds.translate(current_offset);
let sticky_frame_info = StickyFrameInfo::new(
frame_rect,
info.margins,
info.vertical_offset_bounds,
info.horizontal_offset_bounds,
info.previously_applied_offset,
);
let index = self.spatial_tree.add_sticky_frame(
parent_node_index,
sticky_frame_info,
info.id.pipeline_id(),
);
self.id_to_index_mapper.add_spatial_node(info.id, index);
}
fn build_scroll_frame(
&mut self,
info: &ScrollFrameDisplayItem,
parent_node_index: SpatialNodeIndex,
pipeline_id: PipelineId,
) {
let current_offset = self.current_offset(parent_node_index);
let clip_region = ClipRegion::create_for_clip_node_with_local_clip(
&info.clip_rect,
¤t_offset,
);
// Just use clip rectangle as the frame rect for this scroll frame.
// This is useful when calculating scroll extents for the
// SpatialNode::scroll(..) API as well as for properly setting sticky
// positioning offsets.
let frame_rect = clip_region.main;
let content_size = info.content_rect.size;
self.add_clip_node(info.clip_id, &info.parent_space_and_clip, clip_region);
self.add_scroll_frame(
info.scroll_frame_id,
parent_node_index,
info.external_id,
pipeline_id,
&frame_rect,
&content_size,
info.scroll_sensitivity,
ScrollFrameKind::Explicit,
info.external_scroll_offset,
);
}
fn push_iframe(
&mut self,
info: &IframeDisplayItem,
spatial_node_index: SpatialNodeIndex,
) -> Option<(LayoutSize, BuiltDisplayListIter<'a>)> {
let iframe_pipeline_id = info.pipeline_id;
let pipeline = match self.scene.pipelines.get(&iframe_pipeline_id) {
Some(pipeline) => pipeline,
None => {
debug_assert!(info.ignore_missing_pipeline);
return None
},
};
let current_offset = self.current_offset(spatial_node_index);
self.add_clip_node(
ClipId::root(iframe_pipeline_id),
&info.space_and_clip,
ClipRegion::create_for_clip_node_with_local_clip(
&info.clip_rect,
¤t_offset,
),
);
self.clip_store.push_clip_root(
Some(ClipId::root(iframe_pipeline_id)),
true,
);
let bounds = self.snap_rect(
&info.bounds.translate(current_offset),
spatial_node_index,
);
let spatial_node_index = self.push_reference_frame(
SpatialId::root_reference_frame(iframe_pipeline_id),
Some(spatial_node_index),
iframe_pipeline_id,
TransformStyle::Flat,
PropertyBinding::Value(LayoutTransform::identity()),
ReferenceFrameKind::Transform,
bounds.origin.to_vector(),
);
let iframe_rect = LayoutRect::new(LayoutPoint::zero(), bounds.size);
let is_root_pipeline = self.iframe_size.is_empty();
self.add_scroll_frame(
SpatialId::root_scroll_node(iframe_pipeline_id),
spatial_node_index,
ExternalScrollId(0, iframe_pipeline_id),
iframe_pipeline_id,
&iframe_rect,
&bounds.size,
ScrollSensitivity::ScriptAndInputEvents,
ScrollFrameKind::PipelineRoot {
is_root_pipeline,
},
LayoutVector2D::zero(),
);
Some((bounds.size, pipeline.display_list.iter()))
}
fn get_space(
&self,
spatial_id: SpatialId,
) -> SpatialNodeIndex {
self.id_to_index_mapper.get_spatial_node_index(spatial_id)
}
fn get_clip_chain(
&mut self,
clip_id: ClipId,
) -> ClipChainId {
self.clip_store.get_or_build_clip_chain_id(clip_id)
}
fn process_common_properties(
&mut self,
common: &CommonItemProperties,
bounds: Option<&LayoutRect>,
) -> (LayoutPrimitiveInfo, LayoutRect, SpatialNodeIndex, ClipChainId) {
let spatial_node_index = self.get_space(common.spatial_id);
let clip_chain_id = self.get_clip_chain(common.clip_id);
let current_offset = self.current_offset(spatial_node_index);
let unsnapped_clip_rect = common.clip_rect.translate(current_offset);
let clip_rect = self.snap_rect(
&unsnapped_clip_rect,
spatial_node_index,
);
let unsnapped_rect = bounds.map(|bounds| {
bounds.translate(current_offset)
});
// If no bounds rect is given, default to clip rect.
let rect = unsnapped_rect.map_or(clip_rect, |bounds| {
self.snap_rect(
&bounds,
spatial_node_index,
)
});
let layout = LayoutPrimitiveInfo {
rect,
clip_rect,
flags: common.flags,
};
(layout, unsnapped_rect.unwrap_or(unsnapped_clip_rect), spatial_node_index, clip_chain_id)
}
fn process_common_properties_with_bounds(
&mut self,
common: &CommonItemProperties,
bounds: &LayoutRect,
) -> (LayoutPrimitiveInfo, LayoutRect, SpatialNodeIndex, ClipChainId) {
self.process_common_properties(
common,
Some(bounds),
)
}
pub fn snap_rect(
&mut self,
rect: &LayoutRect,
target_spatial_node: SpatialNodeIndex,
) -> LayoutRect {
self.snap_to_device.set_target_spatial_node(
target_spatial_node,
&self.spatial_tree
);
self.snap_to_device.snap_rect(rect)
}
fn build_item<'b>(
&'b mut self,
item: DisplayItemRef,
pipeline_id: PipelineId,
) {
match *item.item() {
DisplayItem::Image(ref info) => {
profile_scope!("image");
let (layout, _, spatial_node_index, clip_chain_id) = self.process_common_properties_with_bounds(
&info.common,
&info.bounds,
);
self.add_image(
spatial_node_index,
clip_chain_id,
&layout,
layout.rect.size,
LayoutSize::zero(),
info.image_key,
info.image_rendering,
info.alpha_type,
info.color,
);
}
DisplayItem::RepeatingImage(ref info) => {
profile_scope!("repeating_image");
let (layout, unsnapped_rect, spatial_node_index, clip_chain_id) = self.process_common_properties_with_bounds(
&info.common,
&info.bounds,
);
let stretch_size = process_repeat_size(
&layout.rect,
&unsnapped_rect,
info.stretch_size,
);
self.add_image(
spatial_node_index,
clip_chain_id,
&layout,
stretch_size,
info.tile_spacing,
info.image_key,
info.image_rendering,
info.alpha_type,
info.color,
);
}
DisplayItem::YuvImage(ref info) => {
profile_scope!("yuv_image");
let (layout, _, spatial_node_index, clip_chain_id) = self.process_common_properties_with_bounds(
&info.common,
&info.bounds,
);
self.add_yuv_image(
spatial_node_index,
clip_chain_id,
&layout,
info.yuv_data,
info.color_depth,
info.color_space,
info.color_range,
info.image_rendering,
);
}
DisplayItem::Text(ref info) => {
profile_scope!("text");
// TODO(aosmond): Snapping text primitives does not make much sense, given the
// primitive bounds and clip are supposed to be conservative, not definitive.
// E.g. they should be able to grow and not impact the output. However there
// are subtle interactions between the primitive origin and the glyph offset
// which appear to be significant (presumably due to some sort of accumulated
// error throughout the layers). We should fix this at some point.
let (layout, _, spatial_node_index, clip_chain_id) = self.process_common_properties_with_bounds(
&info.common,
&info.bounds,
);
self.add_text(
spatial_node_index,
clip_chain_id,
&layout,
&info.font_key,
&info.color,
item.glyphs(),
info.glyph_options,
);
}
DisplayItem::Rectangle(ref info) => {
profile_scope!("rect");
let (layout, _, spatial_node_index, clip_chain_id) = self.process_common_properties_with_bounds(
&info.common,
&info.bounds,
);
self.add_primitive(
spatial_node_index,
clip_chain_id,
&layout,
Vec::new(),
PrimitiveKeyKind::Rectangle {
color: info.color.into(),
},
);
}
DisplayItem::HitTest(ref info) => {
profile_scope!("hit_test");
// TODO(gw): We could skip building the clip-chain here completely, as it's not used by
// hit-test items.
let (layout, _, spatial_node_index, _) = self.process_common_properties(
&info.common,
None,
);
// Don't add transparent rectangles to the draw list,
// but do consider them for hit testing. This allows
// specifying invisible hit testing areas.
self.add_primitive_to_hit_testing_list(
&layout,
spatial_node_index,
info.common.clip_id,
info.tag,
);
}
DisplayItem::ClearRectangle(ref info) => {
profile_scope!("clear");
let (layout, _, spatial_node_index, clip_chain_id) = self.process_common_properties_with_bounds(
&info.common,
&info.bounds,
);
self.add_clear_rectangle(
spatial_node_index,
clip_chain_id,
&layout,
);
}
DisplayItem::Line(ref info) => {
profile_scope!("line");
let (layout, _, spatial_node_index, clip_chain_id) = self.process_common_properties_with_bounds(
&info.common,
&info.area,
);
self.add_line(
spatial_node_index,
clip_chain_id,
&layout,
info.wavy_line_thickness,
info.orientation,
info.color,
info.style,
);
}
DisplayItem::Gradient(ref info) => {
profile_scope!("gradient");
let (layout, unsnapped_rect, spatial_node_index, clip_chain_id) = self.process_common_properties_with_bounds(
&info.common,
&info.bounds,
);
let tile_size = process_repeat_size(
&layout.rect,
&unsnapped_rect,
info.tile_size,
);
if let Some(prim_key_kind) = self.create_linear_gradient_prim(
&layout,
info.gradient.start_point,
info.gradient.end_point,
item.gradient_stops(),
info.gradient.extend_mode,
tile_size,
info.tile_spacing,
None,
) {
self.add_nonshadowable_primitive(
spatial_node_index,
clip_chain_id,
&layout,
Vec::new(),
prim_key_kind,
);
}
}
DisplayItem::RadialGradient(ref info) => {
profile_scope!("radial");
let (layout, unsnapped_rect, spatial_node_index, clip_chain_id) = self.process_common_properties_with_bounds(
&info.common,
&info.bounds,
);
let tile_size = process_repeat_size(
&layout.rect,
&unsnapped_rect,
info.tile_size,
);
let prim_key_kind = self.create_radial_gradient_prim(
&layout,
info.gradient.center,
info.gradient.start_offset * info.gradient.radius.width,
info.gradient.end_offset * info.gradient.radius.width,
info.gradient.radius.width / info.gradient.radius.height,
item.gradient_stops(),
info.gradient.extend_mode,
tile_size,
info.tile_spacing,
None,
);
self.add_nonshadowable_primitive(
spatial_node_index,
clip_chain_id,
&layout,
Vec::new(),
prim_key_kind,
);
}
DisplayItem::ConicGradient(ref info) => {
profile_scope!("conic");
let (layout, unsnapped_rect, spatial_node_index, clip_chain_id) = self.process_common_properties_with_bounds(
&info.common,
&info.bounds,
);
let tile_size = process_repeat_size(
&layout.rect,
&unsnapped_rect,
info.tile_size,
);
let prim_key_kind = self.create_conic_gradient_prim(
&layout,
info.gradient.center,
info.gradient.angle,
info.gradient.start_offset,
info.gradient.end_offset,
item.gradient_stops(),
info.gradient.extend_mode,
tile_size,
info.tile_spacing,
None,
);
self.add_nonshadowable_primitive(
spatial_node_index,
clip_chain_id,
&layout,
Vec::new(),
prim_key_kind,
);
}
DisplayItem::BoxShadow(ref info) => {
profile_scope!("box_shadow");
let (layout, _, spatial_node_index, clip_chain_id) = self.process_common_properties_with_bounds(
&info.common,
&info.box_bounds,
);
self.add_box_shadow(
spatial_node_index,
clip_chain_id,
&layout,
&info.offset,
info.color,
info.blur_radius,
info.spread_radius,
info.border_radius,
info.clip_mode,
);
}
DisplayItem::Border(ref info) => {
profile_scope!("border");
let (layout, _, spatial_node_index, clip_chain_id) = self.process_common_properties_with_bounds(
&info.common,
&info.bounds,
);
self.add_border(
spatial_node_index,
clip_chain_id,
&layout,
info,
item.gradient_stops(),
);
}
DisplayItem::ImageMaskClip(ref info) => {
profile_scope!("image_clip");
let parent_space = self.get_space(info.parent_space_and_clip.spatial_id);
let current_offset = self.current_offset(parent_space);
let image_mask = ImageMask {
rect: info.image_mask.rect.translate(current_offset),
..info.image_mask
};
self.add_image_mask_clip_node(
info.id,
&info.parent_space_and_clip,
&image_mask,
);
}
DisplayItem::RoundedRectClip(ref info) => {
profile_scope!("rounded_clip");
let parent_space = self.get_space(info.parent_space_and_clip.spatial_id);
let current_offset = self.current_offset(parent_space);
self.add_rounded_rect_clip_node(
info.id,
&info.parent_space_and_clip,
&info.clip,
current_offset,
);
}
DisplayItem::RectClip(ref info) => {
profile_scope!("rect_clip");
let parent_space = self.get_space(info.parent_space_and_clip.spatial_id);
let current_offset = self.current_offset(parent_space);
let clip_rect = info.clip_rect.translate(current_offset);
self.add_rect_clip_node(
info.id,
&info.parent_space_and_clip,
&clip_rect,
);
}
DisplayItem::Clip(ref info) => {
profile_scope!("clip");
let parent_space = self.get_space(info.parent_space_and_clip.spatial_id);
let current_offset = self.current_offset(parent_space);
let clip_region = ClipRegion::create_for_clip_node(
info.clip_rect,
item.complex_clip().iter(),
¤t_offset,
);
self.add_clip_node(info.id, &info.parent_space_and_clip, clip_region);
}
DisplayItem::ClipChain(ref info) => {
profile_scope!("clip_chain");
let parent = info.parent.map_or(ClipId::root(pipeline_id), |id| ClipId::ClipChain(id));
let mut clips: SmallVec<[SceneClipInstance; 4]> = SmallVec::new();
for clip_item in item.clip_chain_items() {
let template = self.clip_store.get_template(clip_item);
clips.extend_from_slice(&template.clips);
}
self.clip_store.register_clip_template(
ClipId::ClipChain(info.id),
parent,
&clips,
);
},
DisplayItem::ScrollFrame(ref info) => {
profile_scope!("scrollframe");
let parent_space = self.get_space(info.parent_space_and_clip.spatial_id);
self.build_scroll_frame(
info,
parent_space,
pipeline_id,
);
}
DisplayItem::StickyFrame(ref info) => {
profile_scope!("stickyframe");
let parent_space = self.get_space(info.parent_spatial_id);
self.build_sticky_frame(
info,
parent_space,
);
}
DisplayItem::BackdropFilter(ref info) => {
profile_scope!("backdrop");
let (layout, _, spatial_node_index, clip_chain_id) = self.process_common_properties(
&info.common,
None,
);
let filters = filter_ops_for_compositing(item.filters());
let filter_datas = filter_datas_for_compositing(item.filter_datas());
let filter_primitives = filter_primitives_for_compositing(item.filter_primitives());
self.add_backdrop_filter(
spatial_node_index,
clip_chain_id,
&layout,
filters,
filter_datas,
filter_primitives,
);
}
// Do nothing; these are dummy items for the display list parser
DisplayItem::SetGradientStops |
DisplayItem::SetFilterOps |
DisplayItem::SetFilterData |
DisplayItem::SetFilterPrimitives => {}
// Special items that are handled in the parent method
DisplayItem::PushStackingContext(..) |
DisplayItem::PushReferenceFrame(..) |
DisplayItem::PopReferenceFrame |
DisplayItem::PopStackingContext |
DisplayItem::Iframe(_) => {
unreachable!("Handled in `build_all`")
}
DisplayItem::ReuseItems(key) |
DisplayItem::RetainedItems(key) => {
unreachable!("Iterator logic error: {:?}", key);
}
DisplayItem::PushShadow(info) => {
profile_scope!("push_shadow");
let spatial_node_index = self.get_space(info.space_and_clip.spatial_id);
let clip_chain_id = self.get_clip_chain(
info.space_and_clip.clip_id,
);
self.push_shadow(
info.shadow,
spatial_node_index,
clip_chain_id,
info.should_inflate,
);
}
DisplayItem::PopAllShadows => {
profile_scope!("pop_all_shadows");
self.pop_all_shadows();
}
}
}
// Given a list of clip sources, a positioning node and
// a parent clip chain, return a new clip chain entry.
// If the supplied list of clip sources is empty, then
// just return the parent clip chain id directly.
fn build_clip_chain(
&mut self,
clip_items: Vec<ClipItemKey>,
spatial_node_index: SpatialNodeIndex,
parent_clip_chain_id: ClipChainId,
) -> ClipChainId {
if clip_items.is_empty() {
parent_clip_chain_id
} else {
let mut clip_chain_id = parent_clip_chain_id;
for item in clip_items {
// Intern this clip item, and store the handle
// in the clip chain node.
let handle = self.interners
.clip
.intern(&item, || {
ClipInternData {
clip_node_kind: item.kind.node_kind(),
}
});
clip_chain_id = self.clip_store.add_clip_chain_node(
handle,
spatial_node_index,
clip_chain_id,
);
}
clip_chain_id
}
}
/// Create a primitive and add it to the prim store. This method doesn't
/// add the primitive to the draw list, so can be used for creating
/// sub-primitives.
///
/// TODO(djg): Can this inline into `add_interned_prim_to_draw_list`
fn create_primitive<P>(
&mut self,
info: &LayoutPrimitiveInfo,
spatial_node_index: SpatialNodeIndex,
clip_chain_id: ClipChainId,
prim: P,
) -> PrimitiveInstance
where
P: InternablePrimitive,
Interners: AsMut<Interner<P>>,
{
// Build a primitive key.
let prim_key = prim.into_key(info);
let current_offset = self.current_offset(spatial_node_index);
let interner = self.interners.as_mut();
let prim_data_handle = interner
.intern(&prim_key, || ());
let instance_kind = P::make_instance_kind(
prim_key,
prim_data_handle,
&mut self.prim_store,
current_offset,
);
PrimitiveInstance::new(
info.clip_rect,
instance_kind,
clip_chain_id,
)
}
pub fn add_primitive_to_hit_testing_list(
&mut self,
info: &LayoutPrimitiveInfo,
spatial_node_index: SpatialNodeIndex,
clip_id: ClipId,
tag: ItemTag,
) {
self.hit_testing_scene.add_item(
tag,
info,
spatial_node_index,
clip_id,
&self.clip_store,
);
}
/// Add an already created primitive to the draw lists.
pub fn add_primitive_to_draw_list(
&mut self,
prim_instance: PrimitiveInstance,
prim_rect: LayoutRect,
spatial_node_index: SpatialNodeIndex,
flags: PrimitiveFlags,
) {
// Add primitive to the top-most stacking context on the stack.
if prim_instance.is_chased() {
println!("\tadded to stacking context at {}", self.sc_stack.len());
}
// If we have a valid stacking context, the primitive gets added to that.
// Otherwise, it gets added to a top-level picture cache slice.
match self.sc_stack.last_mut() {
Some(stacking_context) => {
stacking_context.prim_list.add_prim(
prim_instance,
prim_rect,
spatial_node_index,
flags,
);
}
None => {
self.tile_cache_builder.add_prim(
prim_instance,
prim_rect,
spatial_node_index,
flags,
&self.spatial_tree,
&self.clip_store,
self.interners,
&self.config,
&self.quality_settings,
);
}
}
}
/// Convenience interface that creates a primitive entry and adds it
/// to the draw list.
fn add_nonshadowable_primitive<P>(
&mut self,
spatial_node_index: SpatialNodeIndex,
clip_chain_id: ClipChainId,
info: &LayoutPrimitiveInfo,
clip_items: Vec<ClipItemKey>,
prim: P,
)
where
P: InternablePrimitive + IsVisible,
Interners: AsMut<Interner<P>>,
{
if prim.is_visible() {
let clip_chain_id = self.build_clip_chain(
clip_items,
spatial_node_index,
clip_chain_id,
);
self.add_prim_to_draw_list(
info,
spatial_node_index,
clip_chain_id,
prim,
);
}
}
pub fn add_primitive<P>(
&mut self,
spatial_node_index: SpatialNodeIndex,
clip_chain_id: ClipChainId,
info: &LayoutPrimitiveInfo,
clip_items: Vec<ClipItemKey>,
prim: P,
)
where
P: InternablePrimitive + IsVisible,
Interners: AsMut<Interner<P>>,
ShadowItem: From<PendingPrimitive<P>>
{
// If a shadow context is not active, then add the primitive
// directly to the parent picture.
if self.pending_shadow_items.is_empty() {
self.add_nonshadowable_primitive(
spatial_node_index,
clip_chain_id,
info,
clip_items,
prim,
);
} else {
debug_assert!(clip_items.is_empty(), "No per-prim clips expected for shadowed primitives");
// There is an active shadow context. Store as a pending primitive
// for processing during pop_all_shadows.
self.pending_shadow_items.push_back(PendingPrimitive {
spatial_node_index,
clip_chain_id,
info: *info,
prim,
}.into());
}
}
fn add_prim_to_draw_list<P>(
&mut self,
info: &LayoutPrimitiveInfo,
spatial_node_index: SpatialNodeIndex,
clip_chain_id: ClipChainId,
prim: P,
)
where
P: InternablePrimitive,
Interners: AsMut<Interner<P>>,
{
let prim_instance = self.create_primitive(
info,
spatial_node_index,
clip_chain_id,
prim,
);
self.register_chase_primitive_by_rect(
&info.rect,
&prim_instance,
);
self.add_primitive_to_draw_list(
prim_instance,
info.rect,
spatial_node_index,
info.flags,
);
}
/// Push a new stacking context. Returns context that must be passed to pop_stacking_context().
fn push_stacking_context(
&mut self,
composite_ops: CompositeOps,
transform_style: TransformStyle,
prim_flags: PrimitiveFlags,
spatial_node_index: SpatialNodeIndex,
clip_id: Option<ClipId>,
requested_raster_space: RasterSpace,
flags: StackingContextFlags,
) -> StackingContextInfo {
profile_scope!("push_stacking_context");
// Push current requested raster space on stack for prims to access
self.raster_space_stack.push(requested_raster_space);
// Get the transform-style of the parent stacking context,
// which determines if we *might* need to draw this on
// an intermediate surface for plane splitting purposes.
let (parent_is_3d, extra_3d_instance) = match self.sc_stack.last_mut() {
Some(ref mut sc) if sc.is_3d() => {
let flat_items_context_3d = match sc.context_3d {
Picture3DContext::In { ancestor_index, .. } => Picture3DContext::In {
root_data: None,
ancestor_index,
},
Picture3DContext::Out => panic!("Unexpected out of 3D context"),
};
// Cut the sequence of flat children before starting a child stacking context,
// so that the relative order between them and our current SC is preserved.
let extra_instance = sc.cut_item_sequence(
&mut self.prim_store,
&mut self.interners,
Some(PictureCompositeMode::Blit(BlitReason::PRESERVE3D)),
flat_items_context_3d,
);
let extra_instance = extra_instance.map(|(_, instance)| {
ExtendedPrimitiveInstance {
instance,
spatial_node_index: sc.spatial_node_index,
flags: sc.prim_flags,
}
});
(true, extra_instance)
},
_ => (false, None),
};
if let Some(instance) = extra_3d_instance {
self.add_primitive_instance_to_3d_root(instance);
}
// If this is preserve-3d *or* the parent is, then this stacking
// context is participating in the 3d rendering context. In that
// case, hoist the picture up to the 3d rendering context
// container, so that it's rendered as a sibling with other
// elements in this context.
let participating_in_3d_context =
composite_ops.is_empty() &&
(parent_is_3d || transform_style == TransformStyle::Preserve3D);
let context_3d = if participating_in_3d_context {
// Get the spatial node index of the containing block, which
// defines the context of backface-visibility.
let ancestor_index = self.containing_block_stack
.last()
.cloned()
.unwrap_or(ROOT_SPATIAL_NODE_INDEX);
Picture3DContext::In {
root_data: if parent_is_3d {
None
} else {
Some(Vec::new())
},
ancestor_index,
}
} else {
Picture3DContext::Out
};
// Force an intermediate surface if the stacking context has a
// complex clip node. In the future, we may decide during
// prepare step to skip the intermediate surface if the
// clip node doesn't affect the stacking context rect.
let mut blit_reason = BlitReason::empty();
if flags.contains(StackingContextFlags::IS_BLEND_CONTAINER) {
blit_reason |= BlitReason::ISOLATE;
}
// If backface visibility is explicitly set, force this stacking
// context to be an off-screen surface. If part of a 3d context
// (common case) it will already be an off-screen surface. If
// the backface-vis is used while outside a 3d rendering context,
// this is an edge case.
if !prim_flags.contains(PrimitiveFlags::IS_BACKFACE_VISIBLE) {
blit_reason |= BlitReason::ISOLATE;
}
// If this stacking context has any complex clips, we need to draw it
// to an off-screen surface.
if let Some(clip_id) = clip_id {
if self.clip_store.has_complex_clips(clip_id) {
blit_reason |= BlitReason::CLIP;
}
}
let is_redundant = FlattenedStackingContext::is_redundant(
flags,
&context_3d,
&composite_ops,
prim_flags,
blit_reason,
self.sc_stack.last(),
);
let mut sc_info = StackingContextInfo {
pop_clip_root: false,
pop_stacking_context: false,
pop_containing_block: false,
};
// If this is not 3d, then it establishes an ancestor root for child 3d contexts.
if !participating_in_3d_context {
sc_info.pop_containing_block = true;
self.containing_block_stack.push(spatial_node_index);
}
// This muct be built before the push_clip_root logic below
let clip_chain_id = match clip_id {
Some(clip_id) => self.clip_store.get_or_build_clip_chain_id(clip_id),
None => ClipChainId::NONE,
};
// If this has a valid clip, it will create a new clip root
if let Some(clip_id) = clip_id {
sc_info.pop_clip_root = true;
// If this stacking context is redundant (prims will be pushed into
// the parent during pop) but it has a valid clip, then we need to
// add that clip to the current clip chain builder, so it's correctly
// applied to any primitives within this redundant stacking context.
// For the normal case, we start a new clip root, knowing that the
// clip on this stacking context will be pushed onto the stack during
// frame building.
if is_redundant {
self.clip_store.push_clip_root(Some(clip_id), true);
} else {
self.clip_store.push_clip_root(None, false);
}
// Push this clip id into the hit-testing scene for child primitives
self.hit_testing_scene.push_clip(clip_id);
}
// If not redundant, create a stacking context to hold primitive clusters
if !is_redundant {
sc_info.pop_stacking_context = true;
// Push the SC onto the stack, so we know how to handle things in
// pop_stacking_context.
self.sc_stack.push(FlattenedStackingContext {
prim_list: PrimitiveList::empty(),
prim_flags,
spatial_node_index,
clip_chain_id,
composite_ops,
blit_reason,
transform_style,
context_3d,
is_redundant,
is_backdrop_root: flags.contains(StackingContextFlags::IS_BACKDROP_ROOT),
});
}
sc_info
}
fn pop_stacking_context(
&mut self,
info: StackingContextInfo,
) {
profile_scope!("pop_stacking_context");
// Pop off current raster space (pushed unconditionally in push_stacking_context)
self.raster_space_stack.pop().unwrap();
// If the stacking context formed a containing block, pop off the stack
if info.pop_containing_block {
self.containing_block_stack.pop().unwrap();
}
// If the stacking context established a clip root, pop off the stack
if info.pop_clip_root {
self.clip_store.pop_clip_root();
self.hit_testing_scene.pop_clip();
}
// If the stacking context was otherwise redundant, early exit
if !info.pop_stacking_context {
return;
}
let stacking_context = self.sc_stack.pop().unwrap();
let parent_is_empty = match self.sc_stack.last() {
Some(parent_sc) => {
assert!(!stacking_context.is_redundant);
parent_sc.prim_list.is_empty()
},
None => true,
};
let mut source = match stacking_context.context_3d {
// TODO(gw): For now, as soon as this picture is in
// a 3D context, we draw it to an intermediate
// surface and apply plane splitting. However,
// there is a large optimization opportunity here.
// During culling, we can check if there is actually
// perspective present, and skip the plane splitting
// completely when that is not the case.
Picture3DContext::In { ancestor_index, .. } => {
let composite_mode = Some(
PictureCompositeMode::Blit(BlitReason::PRESERVE3D | stacking_context.blit_reason)
);
// Add picture for this actual stacking context contents to render into.
let pic_index = PictureIndex(self.prim_store.pictures
.alloc()
.init(PicturePrimitive::new_image(
composite_mode.clone(),
Picture3DContext::In { root_data: None, ancestor_index },
true,
stacking_context.prim_flags,
stacking_context.prim_list,
stacking_context.spatial_node_index,
PictureOptions::default(),
))
);
let instance = create_prim_instance(
pic_index,
composite_mode.into(),
ClipChainId::NONE,
&mut self.interners,
);
PictureChainBuilder::from_instance(
instance,
stacking_context.prim_flags,
stacking_context.spatial_node_index,
)
}
Picture3DContext::Out => {
if stacking_context.blit_reason.is_empty() {
PictureChainBuilder::from_prim_list(
stacking_context.prim_list,
stacking_context.prim_flags,
stacking_context.spatial_node_index,
)
} else {
let composite_mode = Some(
PictureCompositeMode::Blit(stacking_context.blit_reason)
);
// Add picture for this actual stacking context contents to render into.
let pic_index = PictureIndex(self.prim_store.pictures
.alloc()
.init(PicturePrimitive::new_image(
composite_mode.clone(),
Picture3DContext::Out,
true,
stacking_context.prim_flags,
stacking_context.prim_list,
stacking_context.spatial_node_index,
PictureOptions::default(),
))
);
let instance = create_prim_instance(
pic_index,
composite_mode.into(),
ClipChainId::NONE,
&mut self.interners,
);
PictureChainBuilder::from_instance(
instance,
stacking_context.prim_flags,
stacking_context.spatial_node_index,
)
}
}
};
// If establishing a 3d context, the `cur_instance` represents
// a picture with all the *trailing* immediate children elements.
// We append this to the preserve-3D picture set and make a container picture of them.
if let Picture3DContext::In { root_data: Some(mut prims), ancestor_index } = stacking_context.context_3d {
let instance = source.finalize(
ClipChainId::NONE,
&mut self.interners,
&mut self.prim_store,
);
prims.push(ExtendedPrimitiveInstance {
instance,
spatial_node_index: stacking_context.spatial_node_index,
flags: stacking_context.prim_flags,
});
let mut prim_list = PrimitiveList::empty();
for ext_prim in prims.drain(..) {
prim_list.add_prim(
ext_prim.instance,
LayoutRect::zero(),
ext_prim.spatial_node_index,
ext_prim.flags,
);
}
// This is the acttual picture representing our 3D hierarchy root.
let pic_index = PictureIndex(self.prim_store.pictures
.alloc()
.init(PicturePrimitive::new_image(
None,
Picture3DContext::In {
root_data: Some(Vec::new()),
ancestor_index,
},
true,
stacking_context.prim_flags,
prim_list,
stacking_context.spatial_node_index,
PictureOptions::default(),
))
);
let instance = create_prim_instance(
pic_index,
PictureCompositeKey::Identity,
ClipChainId::NONE,
&mut self.interners,
);
source = PictureChainBuilder::from_instance(
instance,
stacking_context.prim_flags,
stacking_context.spatial_node_index,
);
}
let has_filters = stacking_context.composite_ops.has_valid_filters();
source = self.wrap_prim_with_filters(
source,
stacking_context.composite_ops.filters,
stacking_context.composite_ops.filter_primitives,
stacking_context.composite_ops.filter_datas,
true,
);
// Same for mix-blend-mode, except we can skip if this primitive is the first in the parent
// stacking context.
// From https://drafts.fxtf.org/compositing-1/#generalformula, the formula for blending is:
// Cs = (1 - ab) x Cs + ab x Blend(Cb, Cs)
// where
// Cs = Source color
// ab = Backdrop alpha
// Cb = Backdrop color
//
// If we're the first primitive within a stacking context, then we can guarantee that the
// backdrop alpha will be 0, and then the blend equation collapses to just
// Cs = Cs, and the blend mode isn't taken into account at all.
if let (Some(mix_blend_mode), false) = (stacking_context.composite_ops.mix_blend_mode, parent_is_empty) {
let parent_is_isolated = match self.sc_stack.last() {
Some(parent_sc) => parent_sc.blit_reason.contains(BlitReason::ISOLATE),
None => false,
};
if parent_is_isolated {
let composite_mode = PictureCompositeMode::MixBlend(mix_blend_mode);
source = source.add_picture(
composite_mode,
Picture3DContext::Out,
PictureOptions::default(),
&mut self.interners,
&mut self.prim_store,
);
} else {
// If we have a mix-blend-mode, the stacking context needs to be isolated
// to blend correctly as per the CSS spec.
// If not already isolated, we can't correctly blend.
warn!("found a mix-blend-mode outside a blend container, ignoring");
}
}
// Set the stacking context clip on the outermost picture in the chain,
// unless we already set it on the leaf picture.
let cur_instance = source.finalize(
stacking_context.clip_chain_id,
&mut self.interners,
&mut self.prim_store,
);
// The primitive instance for the remainder of flat children of this SC
// if it's a part of 3D hierarchy but not the root of it.
let trailing_children_instance = match self.sc_stack.last_mut() {
// Preserve3D path (only relevant if there are no filters/mix-blend modes)
Some(ref parent_sc) if !has_filters && parent_sc.is_3d() => {
Some(cur_instance)
}
// Regular parenting path
Some(ref mut parent_sc) => {
parent_sc.prim_list.add_prim(
cur_instance,
LayoutRect::zero(),
stacking_context.spatial_node_index,
stacking_context.prim_flags,
);
None
}
// This must be the root stacking context
None => {
self.add_primitive_to_draw_list(
cur_instance,
LayoutRect::zero(),
stacking_context.spatial_node_index,
stacking_context.prim_flags,
);
None
}
};
// finally, if there any outstanding 3D primitive instances,
// find the 3D hierarchy root and add them there.
if let Some(instance) = trailing_children_instance {
self.add_primitive_instance_to_3d_root(ExtendedPrimitiveInstance {
instance,
spatial_node_index: stacking_context.spatial_node_index,
flags: stacking_context.prim_flags,
});
}
assert!(
self.pending_shadow_items.is_empty(),
"Found unpopped shadows when popping stacking context!"
);
}
pub fn push_reference_frame(
&mut self,
reference_frame_id: SpatialId,
parent_index: Option<SpatialNodeIndex>,
pipeline_id: PipelineId,
transform_style: TransformStyle,
source_transform: PropertyBinding<LayoutTransform>,
kind: ReferenceFrameKind,
origin_in_parent_reference_frame: LayoutVector2D,
) -> SpatialNodeIndex {
let index = self.spatial_tree.add_reference_frame(
parent_index,
transform_style,
source_transform,
kind,
origin_in_parent_reference_frame,
pipeline_id,
);
self.id_to_index_mapper.add_spatial_node(reference_frame_id, index);
index
}
pub fn push_root(
&mut self,
pipeline_id: PipelineId,
viewport_size: &LayoutSize,
) {
if let ChasePrimitive::Id(id) = self.config.chase_primitive {
println!("Chasing {:?} by index", id);
register_prim_chase_id(id);
}
let spatial_node_index = self.push_reference_frame(
SpatialId::root_reference_frame(pipeline_id),
None,
pipeline_id,
TransformStyle::Flat,
PropertyBinding::Value(LayoutTransform::identity()),
ReferenceFrameKind::Transform,
LayoutVector2D::zero(),
);
let viewport_rect = self.snap_rect(
&LayoutRect::new(LayoutPoint::zero(), *viewport_size),
spatial_node_index,
);
self.add_scroll_frame(
SpatialId::root_scroll_node(pipeline_id),
spatial_node_index,
ExternalScrollId(0, pipeline_id),
pipeline_id,
&viewport_rect,
&viewport_rect.size,
ScrollSensitivity::ScriptAndInputEvents,
ScrollFrameKind::PipelineRoot {
is_root_pipeline: true,
},
LayoutVector2D::zero(),
);
}
fn add_image_mask_clip_node(
&mut self,
new_node_id: ClipId,
space_and_clip: &SpaceAndClipInfo,
image_mask: &ImageMask,
) {
let spatial_node_index = self.id_to_index_mapper.get_spatial_node_index(space_and_clip.spatial_id);
let snapped_mask_rect = self.snap_rect(
&image_mask.rect,
spatial_node_index,
);
let item = ClipItemKey {
kind: ClipItemKeyKind::image_mask(image_mask, snapped_mask_rect),
};
let handle = self
.interners
.clip
.intern(&item, || {
ClipInternData {
clip_node_kind: ClipNodeKind::Complex,
}
});
let instance = SceneClipInstance {
key: item,
clip: ClipInstance::new(handle, spatial_node_index),
};
self.clip_store.register_clip_template(
new_node_id,
space_and_clip.clip_id,
&[instance],
);
}
/// Add a new rectangle clip, positioned by the spatial node in the `space_and_clip`.
pub fn add_rect_clip_node(
&mut self,
new_node_id: ClipId,
space_and_clip: &SpaceAndClipInfo,
clip_rect: &LayoutRect,
) {
let spatial_node_index = self.id_to_index_mapper.get_spatial_node_index(space_and_clip.spatial_id);
let snapped_clip_rect = self.snap_rect(
clip_rect,
spatial_node_index,
);
let item = ClipItemKey {
kind: ClipItemKeyKind::rectangle(snapped_clip_rect, ClipMode::Clip),
};
let handle = self
.interners
.clip
.intern(&item, || {
ClipInternData {
clip_node_kind: ClipNodeKind::Rectangle,
}
});
let instance = SceneClipInstance {
key: item,
clip: ClipInstance::new(handle, spatial_node_index),
};
self.clip_store.register_clip_template(
new_node_id,
space_and_clip.clip_id,
&[instance],
);
}
pub fn add_rounded_rect_clip_node(
&mut self,
new_node_id: ClipId,
space_and_clip: &SpaceAndClipInfo,
clip: &ComplexClipRegion,
current_offset: LayoutVector2D,
) {
let spatial_node_index = self.id_to_index_mapper.get_spatial_node_index(space_and_clip.spatial_id);
let snapped_region_rect = self.snap_rect(
&clip.rect.translate(current_offset),
spatial_node_index,
);
let item = ClipItemKey {
kind: ClipItemKeyKind::rounded_rect(
snapped_region_rect,
clip.radii,
clip.mode,
),
};
let handle = self
.interners
.clip
.intern(&item, || {
ClipInternData {
clip_node_kind: ClipNodeKind::Complex,
}
});
let instance = SceneClipInstance {
key: item,
clip: ClipInstance::new(handle, spatial_node_index),
};
self.clip_store.register_clip_template(
new_node_id,
space_and_clip.clip_id,
&[instance],
);
}
pub fn add_clip_node<I>(
&mut self,
new_node_id: ClipId,
space_and_clip: &SpaceAndClipInfo,
clip_region: ClipRegion<I>,
)
where
I: IntoIterator<Item = ComplexClipRegion>
{
// Map the ClipId for the positioning node to a spatial node index.
let spatial_node_index = self.id_to_index_mapper.get_spatial_node_index(space_and_clip.spatial_id);
let snapped_clip_rect = self.snap_rect(
&clip_region.main,
spatial_node_index,
);
let mut instances: SmallVec<[SceneClipInstance; 4]> = SmallVec::new();
// Intern each clip item in this clip node, and add the interned
// handle to a clip chain node, parented to form a chain.
// TODO(gw): We could re-structure this to share some of the
// interning and chaining code.
// Build the clip sources from the supplied region.
let item = ClipItemKey {
kind: ClipItemKeyKind::rectangle(snapped_clip_rect, ClipMode::Clip),
};
let handle = self
.interners
.clip
.intern(&item, || {
ClipInternData {
clip_node_kind: ClipNodeKind::Rectangle,
}
});
instances.push(
SceneClipInstance {
key: item,
clip: ClipInstance::new(handle, spatial_node_index),
},
);
for region in clip_region.complex_clips {
let snapped_region_rect = self.snap_rect(®ion.rect, spatial_node_index);
let item = ClipItemKey {
kind: ClipItemKeyKind::rounded_rect(
snapped_region_rect,
region.radii,
region.mode,
),
};
let handle = self
.interners
.clip
.intern(&item, || {
ClipInternData {
clip_node_kind: ClipNodeKind::Complex,
}
});
instances.push(
SceneClipInstance {
key: item,
clip: ClipInstance::new(handle, spatial_node_index),
},
);
}
self.clip_store.register_clip_template(
new_node_id,
space_and_clip.clip_id,
&instances,
);
}
pub fn add_scroll_frame(
&mut self,
new_node_id: SpatialId,
parent_node_index: SpatialNodeIndex,
external_id: ExternalScrollId,
pipeline_id: PipelineId,
frame_rect: &LayoutRect,
content_size: &LayoutSize,
scroll_sensitivity: ScrollSensitivity,
frame_kind: ScrollFrameKind,
external_scroll_offset: LayoutVector2D,
) -> SpatialNodeIndex {
let node_index = self.spatial_tree.add_scroll_frame(
parent_node_index,
external_id,
pipeline_id,
frame_rect,
content_size,
scroll_sensitivity,
frame_kind,
external_scroll_offset,
);
self.id_to_index_mapper.add_spatial_node(new_node_id, node_index);
node_index
}
pub fn push_shadow(
&mut self,
shadow: Shadow,
spatial_node_index: SpatialNodeIndex,
clip_chain_id: ClipChainId,
should_inflate: bool,
) {
// Store this shadow in the pending list, for processing
// during pop_all_shadows.
self.pending_shadow_items.push_back(ShadowItem::Shadow(PendingShadow {
shadow,
spatial_node_index,
clip_chain_id,
should_inflate,
}));
}
pub fn pop_all_shadows(
&mut self,
) {
assert!(!self.pending_shadow_items.is_empty(), "popped shadows, but none were present");
let mut items = mem::replace(&mut self.pending_shadow_items, VecDeque::new());
//
// The pending_shadow_items queue contains a list of shadows and primitives
// that were pushed during the active shadow context. To process these, we:
//
// Iterate the list, popping an item from the front each iteration.
//
// If the item is a shadow:
// - Create a shadow picture primitive.
// - Add *any* primitives that remain in the item list to this shadow.
// If the item is a primitive:
// - Add that primitive as a normal item (if alpha > 0)
//
while let Some(item) = items.pop_front() {
match item {
ShadowItem::Shadow(pending_shadow) => {
// Quote from https://drafts.csswg.org/css-backgrounds-3/#shadow-blur
// "the image that would be generated by applying to the shadow a
// Gaussian blur with a standard deviation equal to half the blur radius."
let std_deviation = pending_shadow.shadow.blur_radius * 0.5;
// Add any primitives that come after this shadow in the item
// list to this shadow.
let mut prim_list = PrimitiveList::empty();
let blur_filter = Filter::Blur(std_deviation, std_deviation);
let blur_is_noop = blur_filter.is_noop();
for item in &items {
let (instance, info, spatial_node_index) = match item {
ShadowItem::Image(ref pending_image) => {
self.create_shadow_prim(
&pending_shadow,
pending_image,
blur_is_noop,
)
}
ShadowItem::LineDecoration(ref pending_line_dec) => {
self.create_shadow_prim(
&pending_shadow,
pending_line_dec,
blur_is_noop,
)
}
ShadowItem::NormalBorder(ref pending_border) => {
self.create_shadow_prim(
&pending_shadow,
pending_border,
blur_is_noop,
)
}
ShadowItem::Primitive(ref pending_primitive) => {
self.create_shadow_prim(
&pending_shadow,
pending_primitive,
blur_is_noop,
)
}
ShadowItem::TextRun(ref pending_text_run) => {
self.create_shadow_prim(
&pending_shadow,
pending_text_run,
blur_is_noop,
)
}
_ => {
continue;
}
};
if blur_is_noop {
self.add_primitive_to_draw_list(
instance,
info.rect,
spatial_node_index,
info.flags,
);
} else {
prim_list.add_prim(
instance,
info.rect,
spatial_node_index,
info.flags,
);
}
}
// No point in adding a shadow here if there were no primitives
// added to the shadow.
if !prim_list.is_empty() {
// Create a picture that the shadow primitives will be added to. If the
// blur radius is 0, the code in Picture::prepare_for_render will
// detect this and mark the picture to be drawn directly into the
// parent picture, which avoids an intermediate surface and blur.
let blur_filter = Filter::Blur(std_deviation, std_deviation);
assert!(!blur_filter.is_noop());
let composite_mode = Some(PictureCompositeMode::Filter(blur_filter));
let composite_mode_key = composite_mode.clone().into();
// Pass through configuration information about whether WR should
// do the bounding rect inflation for text shadows.
let options = PictureOptions {
inflate_if_required: pending_shadow.should_inflate,
};
// Create the primitive to draw the shadow picture into the scene.
let shadow_pic_index = PictureIndex(self.prim_store.pictures
.alloc()
.init(PicturePrimitive::new_image(
composite_mode,
Picture3DContext::Out,
false,
PrimitiveFlags::IS_BACKFACE_VISIBLE,
prim_list,
pending_shadow.spatial_node_index,
options,
))
);
let shadow_pic_key = PictureKey::new(
Picture { composite_mode_key },
);
let shadow_prim_data_handle = self.interners
.picture
.intern(&shadow_pic_key, || ());
let shadow_prim_instance = PrimitiveInstance::new(
LayoutRect::max_rect(),
PrimitiveInstanceKind::Picture {
data_handle: shadow_prim_data_handle,
pic_index: shadow_pic_index,
segment_instance_index: SegmentInstanceIndex::INVALID,
},
pending_shadow.clip_chain_id,
);
// Add the shadow primitive. This must be done before pushing this
// picture on to the shadow stack, to avoid infinite recursion!
self.add_primitive_to_draw_list(
shadow_prim_instance,
LayoutRect::zero(),
pending_shadow.spatial_node_index,
PrimitiveFlags::IS_BACKFACE_VISIBLE,
);
}
}
ShadowItem::Image(pending_image) => {
self.add_shadow_prim_to_draw_list(
pending_image,
)
},
ShadowItem::LineDecoration(pending_line_dec) => {
self.add_shadow_prim_to_draw_list(
pending_line_dec,
)
},
ShadowItem::NormalBorder(pending_border) => {
self.add_shadow_prim_to_draw_list(
pending_border,
)
},
ShadowItem::Primitive(pending_primitive) => {
self.add_shadow_prim_to_draw_list(
pending_primitive,
)
},
ShadowItem::TextRun(pending_text_run) => {
self.add_shadow_prim_to_draw_list(
pending_text_run,
)
},
}
}
debug_assert!(items.is_empty());
self.pending_shadow_items = items;
}
fn create_shadow_prim<P>(
&mut self,
pending_shadow: &PendingShadow,
pending_primitive: &PendingPrimitive<P>,
blur_is_noop: bool,
) -> (PrimitiveInstance, LayoutPrimitiveInfo, SpatialNodeIndex)
where
P: InternablePrimitive + CreateShadow,
Interners: AsMut<Interner<P>>,
{
// Offset the local rect and clip rect by the shadow offset. The pending
// primitive has already been snapped, but we will need to snap the
// shadow after translation. We don't need to worry about the size
// changing because the shadow has the same raster space as the
// primitive, and thus we know the size is already rounded.
let mut info = pending_primitive.info.clone();
info.rect = self.snap_rect(
&info.rect.translate(pending_shadow.shadow.offset),
pending_primitive.spatial_node_index,
);
info.clip_rect = self.snap_rect(
&info.clip_rect.translate(pending_shadow.shadow.offset),
pending_primitive.spatial_node_index,
);
// Construct and add a primitive for the given shadow.
let shadow_prim_instance = self.create_primitive(
&info,
pending_primitive.spatial_node_index,
pending_primitive.clip_chain_id,
pending_primitive.prim.create_shadow(
&pending_shadow.shadow,
blur_is_noop,
self.raster_space_stack.last().cloned().unwrap(),
),
);
(shadow_prim_instance, info, pending_primitive.spatial_node_index)
}
fn add_shadow_prim_to_draw_list<P>(
&mut self,
pending_primitive: PendingPrimitive<P>,
) where
P: InternablePrimitive + IsVisible,
Interners: AsMut<Interner<P>>,
{
// For a normal primitive, if it has alpha > 0, then we add this
// as a normal primitive to the parent picture.
if pending_primitive.prim.is_visible() {
self.add_prim_to_draw_list(
&pending_primitive.info,
pending_primitive.spatial_node_index,
pending_primitive.clip_chain_id,
pending_primitive.prim,
);
}
}
#[cfg(debug_assertions)]
fn register_chase_primitive_by_rect(
&mut self,
rect: &LayoutRect,
prim_instance: &PrimitiveInstance,
) {
if ChasePrimitive::LocalRect(*rect) == self.config.chase_primitive {
println!("Chasing {:?} by local rect", prim_instance.id);
register_prim_chase_id(prim_instance.id);
}
}
#[cfg(not(debug_assertions))]
fn register_chase_primitive_by_rect(
&mut self,
_rect: &LayoutRect,
_prim_instance: &PrimitiveInstance,
) {
}
pub fn add_clear_rectangle(
&mut self,
spatial_node_index: SpatialNodeIndex,
clip_chain_id: ClipChainId,
info: &LayoutPrimitiveInfo,
) {
self.add_primitive(
spatial_node_index,
clip_chain_id,
info,
Vec::new(),
PrimitiveKeyKind::Clear,
);
}
pub fn add_line(
&mut self,
spatial_node_index: SpatialNodeIndex,
clip_chain_id: ClipChainId,
info: &LayoutPrimitiveInfo,
wavy_line_thickness: f32,
orientation: LineOrientation,
color: ColorF,
style: LineStyle,
) {
// For line decorations, we can construct the render task cache key
// here during scene building, since it doesn't depend on device
// pixel ratio or transform.
let mut info = info.clone();
let size = get_line_decoration_size(
&info.rect.size,
orientation,
style,
wavy_line_thickness,
);
let cache_key = size.map(|size| {
// If dotted, adjust the clip rect to ensure we don't draw a final
// partial dot.
if style == LineStyle::Dotted {
let clip_size = match orientation {
LineOrientation::Horizontal => {
LayoutSize::new(
size.width * (info.rect.size.width / size.width).floor(),
info.rect.size.height,
)
}
LineOrientation::Vertical => {
LayoutSize::new(
info.rect.size.width,
size.height * (info.rect.size.height / size.height).floor(),
)
}
};
let clip_rect = LayoutRect::new(
info.rect.origin,
clip_size,
);
info.clip_rect = clip_rect
.intersection(&info.clip_rect)
.unwrap_or_else(LayoutRect::zero);
}
LineDecorationCacheKey {
style,
orientation,
wavy_line_thickness: Au::from_f32_px(wavy_line_thickness),
size: size.to_au(),
}
});
self.add_primitive(
spatial_node_index,
clip_chain_id,
&info,
Vec::new(),
LineDecoration {
cache_key,
color: color.into(),
},
);
}
pub fn add_border(
&mut self,
spatial_node_index: SpatialNodeIndex,
clip_chain_id: ClipChainId,
info: &LayoutPrimitiveInfo,
border_item: &BorderDisplayItem,
gradient_stops: ItemRange<GradientStop>,
) {
match border_item.details {
BorderDetails::NinePatch(ref border) => {
let nine_patch = NinePatchDescriptor {
width: border.width,
height: border.height,
slice: border.slice,
fill: border.fill,
repeat_horizontal: border.repeat_horizontal,
repeat_vertical: border.repeat_vertical,
outset: border.outset.into(),
widths: border_item.widths.into(),
};
match border.source {
NinePatchBorderSource::Image(image_key) => {
let prim = ImageBorder {
request: ImageRequest {
key: image_key,
rendering: ImageRendering::Auto,
tile: None,
},
nine_patch,
};
self.add_nonshadowable_primitive(
spatial_node_index,
clip_chain_id,
info,
Vec::new(),
prim,
);
}
NinePatchBorderSource::Gradient(gradient) => {
let prim = match self.create_linear_gradient_prim(
&info,
gradient.start_point,
gradient.end_point,
gradient_stops,
gradient.extend_mode,
LayoutSize::new(border.height as f32, border.width as f32),
LayoutSize::zero(),
Some(Box::new(nine_patch)),
) {
Some(prim) => prim,
None => return,
};
self.add_nonshadowable_primitive(
spatial_node_index,
clip_chain_id,
info,
Vec::new(),
prim,
);
}
NinePatchBorderSource::RadialGradient(gradient) => {
let prim = self.create_radial_gradient_prim(
&info,
gradient.center,
gradient.start_offset * gradient.radius.width,
gradient.end_offset * gradient.radius.width,
gradient.radius.width / gradient.radius.height,
gradient_stops,
gradient.extend_mode,
LayoutSize::new(border.height as f32, border.width as f32),
LayoutSize::zero(),
Some(Box::new(nine_patch)),
);
self.add_nonshadowable_primitive(
spatial_node_index,
clip_chain_id,
info,
Vec::new(),
prim,
);
}
NinePatchBorderSource::ConicGradient(gradient) => {
let prim = self.create_conic_gradient_prim(
&info,
gradient.center,
gradient.angle,
gradient.start_offset,
gradient.end_offset,
gradient_stops,
gradient.extend_mode,
LayoutSize::new(border.height as f32, border.width as f32),
LayoutSize::zero(),
Some(Box::new(nine_patch)),
);
self.add_nonshadowable_primitive(
spatial_node_index,
clip_chain_id,
info,
Vec::new(),
prim,
);
}
};
}
BorderDetails::Normal(ref border) => {
self.add_normal_border(
info,
border,
border_item.widths,
spatial_node_index,
clip_chain_id,
);
}
}
}
pub fn create_linear_gradient_prim(
&mut self,
info: &LayoutPrimitiveInfo,
start_point: LayoutPoint,
end_point: LayoutPoint,
stops: ItemRange<GradientStop>,
extend_mode: ExtendMode,
stretch_size: LayoutSize,
mut tile_spacing: LayoutSize,
nine_patch: Option<Box<NinePatchDescriptor>>,
) -> Option<LinearGradient> {
let mut prim_rect = info.rect;
simplify_repeated_primitive(&stretch_size, &mut tile_spacing, &mut prim_rect);
let mut max_alpha: f32 = 0.0;
let stops = stops.iter().map(|stop| {
max_alpha = max_alpha.max(stop.color.a);
GradientStopKey {
offset: stop.offset,
color: stop.color.into(),
}
}).collect();
// If all the stops have no alpha, then this
// gradient can't contribute to the scene.
if max_alpha <= 0.0 {
return None;
}
// Try to ensure that if the gradient is specified in reverse, then so long as the stops
// are also supplied in reverse that the rendered result will be equivalent. To do this,
// a reference orientation for the gradient line must be chosen, somewhat arbitrarily, so
// just designate the reference orientation as start < end. Aligned gradient rendering
// manages to produce the same result regardless of orientation, so don't worry about
// reversing in that case.
let reverse_stops = start_point.x > end_point.x ||
(start_point.x == end_point.x && start_point.y > end_point.y);
// To get reftests exactly matching with reverse start/end
// points, it's necessary to reverse the gradient
// line in some cases.
let (sp, ep) = if reverse_stops {
(end_point, start_point)
} else {
(start_point, end_point)
};
Some(LinearGradient {
extend_mode,
start_point: sp.into(),
end_point: ep.into(),
stretch_size: stretch_size.into(),
tile_spacing: tile_spacing.into(),
stops,
reverse_stops,
nine_patch,
})
}
pub fn create_radial_gradient_prim(
&mut self,
info: &LayoutPrimitiveInfo,
center: LayoutPoint,
start_radius: f32,
end_radius: f32,
ratio_xy: f32,
stops: ItemRange<GradientStop>,
extend_mode: ExtendMode,
stretch_size: LayoutSize,
mut tile_spacing: LayoutSize,
nine_patch: Option<Box<NinePatchDescriptor>>,
) -> RadialGradient {
let mut prim_rect = info.rect;
simplify_repeated_primitive(&stretch_size, &mut tile_spacing, &mut prim_rect);
let params = RadialGradientParams {
start_radius,
end_radius,
ratio_xy,
};
let stops = stops.iter().map(|stop| {
GradientStopKey {
offset: stop.offset,
color: stop.color.into(),
}
}).collect();
RadialGradient {
extend_mode,
center: center.into(),
params,
stretch_size: stretch_size.into(),
tile_spacing: tile_spacing.into(),
nine_patch,
stops,
}
}
pub fn create_conic_gradient_prim(
&mut self,
info: &LayoutPrimitiveInfo,
center: LayoutPoint,
angle: f32,
start_offset: f32,
end_offset: f32,
stops: ItemRange<GradientStop>,
extend_mode: ExtendMode,
stretch_size: LayoutSize,
mut tile_spacing: LayoutSize,
nine_patch: Option<Box<NinePatchDescriptor>>,
) -> ConicGradient {
let mut prim_rect = info.rect;
simplify_repeated_primitive(&stretch_size, &mut tile_spacing, &mut prim_rect);
let stops = stops.iter().map(|stop| {
GradientStopKey {
offset: stop.offset,
color: stop.color.into(),
}
}).collect();
ConicGradient {
extend_mode,
center: center.into(),
params: ConicGradientParams { angle, start_offset, end_offset },
stretch_size: stretch_size.into(),
tile_spacing: tile_spacing.into(),
nine_patch,
stops,
}
}
pub fn add_text(
&mut self,
spatial_node_index: SpatialNodeIndex,
clip_chain_id: ClipChainId,
prim_info: &LayoutPrimitiveInfo,
font_instance_key: &FontInstanceKey,
text_color: &ColorF,
glyph_range: ItemRange<GlyphInstance>,
glyph_options: Option<GlyphOptions>,
) {
let offset = self.current_offset(spatial_node_index);
let text_run = {
let instance_map = self.font_instances.lock().unwrap();
let font_instance = match instance_map.get(font_instance_key) {
Some(instance) => instance,
None => {
warn!("Unknown font instance key");
debug!("key={:?}", font_instance_key);
return;
}
};
// Trivial early out checks
if font_instance.size <= FontSize::zero() {
return;
}
// TODO(gw): Use a proper algorithm to select
// whether this item should be rendered with
// subpixel AA!
let mut render_mode = self.config
.default_font_render_mode
.limit_by(font_instance.render_mode);
let mut flags = font_instance.flags;
if let Some(options) = glyph_options {
render_mode = render_mode.limit_by(options.render_mode);
flags |= options.flags;
}
let font = FontInstance::new(
Arc::clone(font_instance),
(*text_color).into(),
render_mode,
flags,
);
// TODO(gw): It'd be nice not to have to allocate here for creating
// the primitive key, when the common case is that the
// hash will match and we won't end up creating a new
// primitive template.
let prim_offset = prim_info.rect.origin.to_vector() - offset;
let glyphs = glyph_range
.iter()
.map(|glyph| {
GlyphInstance {
index: glyph.index,
point: glyph.point - prim_offset,
}
})
.collect();
// Query the current requested raster space (stack handled by push/pop
// stacking context).
let requested_raster_space = self.raster_space_stack
.last()
.cloned()
.unwrap();
TextRun {
glyphs: Arc::new(glyphs),
font,
shadow: false,
requested_raster_space,
}
};
self.add_primitive(
spatial_node_index,
clip_chain_id,
prim_info,
Vec::new(),
text_run,
);
}
pub fn add_image(
&mut self,
spatial_node_index: SpatialNodeIndex,
clip_chain_id: ClipChainId,
info: &LayoutPrimitiveInfo,
stretch_size: LayoutSize,
mut tile_spacing: LayoutSize,
image_key: ImageKey,
image_rendering: ImageRendering,
alpha_type: AlphaType,
color: ColorF,
) {
let mut prim_rect = info.rect;
simplify_repeated_primitive(&stretch_size, &mut tile_spacing, &mut prim_rect);
let info = LayoutPrimitiveInfo {
rect: prim_rect,
.. *info
};
self.add_primitive(
spatial_node_index,
clip_chain_id,
&info,
Vec::new(),
Image {
key: image_key,
tile_spacing: tile_spacing.into(),
stretch_size: stretch_size.into(),
color: color.into(),
image_rendering,
alpha_type,
},
);
}
pub fn add_yuv_image(
&mut self,
spatial_node_index: SpatialNodeIndex,
clip_chain_id: ClipChainId,
info: &LayoutPrimitiveInfo,
yuv_data: YuvData,
color_depth: ColorDepth,
color_space: YuvColorSpace,
color_range: ColorRange,
image_rendering: ImageRendering,
) {
let format = yuv_data.get_format();
let yuv_key = match yuv_data {
YuvData::NV12(plane_0, plane_1) => [plane_0, plane_1, ImageKey::DUMMY],
YuvData::PlanarYCbCr(plane_0, plane_1, plane_2) => [plane_0, plane_1, plane_2],
YuvData::InterleavedYCbCr(plane_0) => [plane_0, ImageKey::DUMMY, ImageKey::DUMMY],
};
self.add_nonshadowable_primitive(
spatial_node_index,
clip_chain_id,
info,
Vec::new(),
YuvImage {
color_depth,
yuv_key,
format,
color_space,
color_range,
image_rendering,
},
);
}
fn add_primitive_instance_to_3d_root(
&mut self,
prim: ExtendedPrimitiveInstance,
) {
// find the 3D root and append to the children list
for sc in self.sc_stack.iter_mut().rev() {
match sc.context_3d {
Picture3DContext::In { root_data: Some(ref mut prims), .. } => {
prims.push(prim);
break;
}
Picture3DContext::In { .. } => {}
Picture3DContext::Out => panic!("Unable to find 3D root"),
}
}
}
pub fn add_backdrop_filter(
&mut self,
spatial_node_index: SpatialNodeIndex,
clip_chain_id: ClipChainId,
info: &LayoutPrimitiveInfo,
filters: Vec<Filter>,
filter_datas: Vec<FilterData>,
filter_primitives: Vec<FilterPrimitive>,
) {
let mut backdrop_pic_index = match self.cut_backdrop_picture() {
// Backdrop contains no content, so no need to add backdrop-filter
None => return,
Some(backdrop_pic_index) => backdrop_pic_index,
};
let backdrop_spatial_node_index = self.prim_store.pictures[backdrop_pic_index.0].spatial_node_index;
let mut instance = self.create_primitive(
info,
// TODO(cbrewster): This is a bit of a hack to help figure out the correct sizing of the backdrop
// region. By makings sure to include this, the clip chain instance computes the correct clip rect,
// but we don't actually apply the filtered backdrop clip yet (this is done to the last instance in
// the filter chain below).
backdrop_spatial_node_index,
clip_chain_id,
Backdrop {
pic_index: backdrop_pic_index,
spatial_node_index,
border_rect: info.rect.into(),
},
);
// We will append the filtered backdrop to the backdrop root, but we need to
// make sure all clips between the current stacking context and backdrop root
// are taken into account. So we wrap the backdrop filter instance with a picture with
// a clip for each stacking context.
for stacking_context in self.sc_stack.iter().rev().take_while(|sc| !sc.is_backdrop_root) {
let clip_chain_id = stacking_context.clip_chain_id;
let prim_flags = stacking_context.prim_flags;
let composite_mode = None;
let mut prim_list = PrimitiveList::empty();
prim_list.add_prim(
instance,
LayoutRect::zero(),
backdrop_spatial_node_index,
prim_flags,
);
backdrop_pic_index = PictureIndex(self.prim_store.pictures
.alloc()
.init(PicturePrimitive::new_image(
composite_mode.clone(),
Picture3DContext::Out,
true,
prim_flags,
prim_list,
backdrop_spatial_node_index,
PictureOptions {
inflate_if_required: false,
},
))
);
instance = create_prim_instance(
backdrop_pic_index,
composite_mode.into(),
clip_chain_id,
&mut self.interners,
);
}
let mut source = PictureChainBuilder::from_instance(
instance,
info.flags,
backdrop_spatial_node_index,
);
source = self.wrap_prim_with_filters(
source,
filters,
filter_primitives,
filter_datas,
false,
);
// Apply filters from all stacking contexts up to, but not including the backdrop root.
// Gecko pushes separate stacking contexts for filters and opacity,
// so we must iterate through multiple stacking contexts to find all effects
// that need to be applied to the filtered backdrop.
let backdrop_root_pos = self.sc_stack.iter().rposition(|sc| sc.is_backdrop_root).expect("no backdrop root?");
for i in ((backdrop_root_pos + 1)..self.sc_stack.len()).rev() {
let stacking_context = &self.sc_stack[i];
let filters = stacking_context.composite_ops.filters.clone();
let filter_primitives = stacking_context.composite_ops.filter_primitives.clone();
let filter_datas = stacking_context.composite_ops.filter_datas.clone();
source = self.wrap_prim_with_filters(
source,
filters,
filter_primitives,
filter_datas,
false,
);
}
let filtered_instance = source.finalize(
clip_chain_id,
&mut self.interners,
&mut self.prim_store,
);
self.sc_stack
.iter_mut()
.rev()
.find(|sc| sc.is_backdrop_root)
.unwrap()
.prim_list
.add_prim(
filtered_instance,
LayoutRect::zero(),
backdrop_spatial_node_index,
info.flags,
);
}
pub fn cut_backdrop_picture(&mut self) -> Option<PictureIndex> {
let mut flattened_items = None;
let mut backdrop_root = None;
let mut spatial_node_index = SpatialNodeIndex::INVALID;
let mut prim_flags = PrimitiveFlags::default();
for sc in self.sc_stack.iter_mut().rev() {
// Add child contents to parent stacking context
if let Some((_, flattened_instance)) = flattened_items.take() {
sc.prim_list.add_prim(
flattened_instance,
LayoutRect::zero(),
spatial_node_index,
prim_flags,
);
}
flattened_items = sc.cut_item_sequence(
&mut self.prim_store,
&mut self.interners,
None,
Picture3DContext::Out,
);
spatial_node_index = sc.spatial_node_index;
prim_flags = sc.prim_flags;
if sc.is_backdrop_root {
backdrop_root = Some(sc);
break;
}
}
let (pic_index, instance) = flattened_items?;
self.prim_store.pictures[pic_index.0].requested_composite_mode = Some(PictureCompositeMode::Blit(BlitReason::BACKDROP));
backdrop_root.expect("no backdrop root found")
.prim_list
.add_prim(
instance,
LayoutRect::zero(),
spatial_node_index,
prim_flags,
);
Some(pic_index)
}
#[must_use]
fn wrap_prim_with_filters(
&mut self,
mut source: PictureChainBuilder,
mut filter_ops: Vec<Filter>,
mut filter_primitives: Vec<FilterPrimitive>,
filter_datas: Vec<FilterData>,
inflate_if_required: bool,
) -> PictureChainBuilder {
// TODO(cbrewster): Currently CSS and SVG filters live side by side in WebRender, but unexpected results will
// happen if they are used simulataneously. Gecko only provides either filter ops or filter primitives.
// At some point, these two should be combined and CSS filters should be expressed in terms of SVG filters.
assert!(filter_ops.is_empty() || filter_primitives.is_empty(),
"Filter ops and filter primitives are not allowed on the same stacking context.");
// For each filter, create a new image with that composite mode.
let mut current_filter_data_index = 0;
for filter in &mut filter_ops {
let composite_mode = match filter {
Filter::ComponentTransfer => {
let filter_data =
&filter_datas[current_filter_data_index];
let filter_data = filter_data.sanitize();
current_filter_data_index = current_filter_data_index + 1;
if filter_data.is_identity() {
continue
} else {
let filter_data_key = SFilterDataKey {
data:
SFilterData {
r_func: SFilterDataComponent::from_functype_values(
filter_data.func_r_type, &filter_data.r_values),
g_func: SFilterDataComponent::from_functype_values(
filter_data.func_g_type, &filter_data.g_values),
b_func: SFilterDataComponent::from_functype_values(
filter_data.func_b_type, &filter_data.b_values),
a_func: SFilterDataComponent::from_functype_values(
filter_data.func_a_type, &filter_data.a_values),
},
};
let handle = self.interners
.filter_data
.intern(&filter_data_key, || ());
PictureCompositeMode::ComponentTransferFilter(handle)
}
}
_ => {
if filter.is_noop() {
continue;
} else {
PictureCompositeMode::Filter(filter.clone())
}
}
};
source = source.add_picture(
composite_mode,
Picture3DContext::Out,
PictureOptions { inflate_if_required },
&mut self.interners,
&mut self.prim_store,
);
}
if !filter_primitives.is_empty() {
let filter_datas = filter_datas.iter()
.map(|filter_data| filter_data.sanitize())
.map(|filter_data| {
SFilterData {
r_func: SFilterDataComponent::from_functype_values(
filter_data.func_r_type, &filter_data.r_values),
g_func: SFilterDataComponent::from_functype_values(
filter_data.func_g_type, &filter_data.g_values),
b_func: SFilterDataComponent::from_functype_values(
filter_data.func_b_type, &filter_data.b_values),
a_func: SFilterDataComponent::from_functype_values(
filter_data.func_a_type, &filter_data.a_values),
}
})
.collect();
// Sanitize filter inputs
for primitive in &mut filter_primitives {
primitive.sanitize();
}
let composite_mode = PictureCompositeMode::SvgFilter(
filter_primitives,
filter_datas,
);
source = source.add_picture(
composite_mode,
Picture3DContext::Out,
PictureOptions { inflate_if_required },
&mut self.interners,
&mut self.prim_store,
);
}
source
}
}
pub trait CreateShadow {
fn create_shadow(
&self,
shadow: &Shadow,
blur_is_noop: bool,
current_raster_space: RasterSpace,
) -> Self;
}
pub trait IsVisible {
fn is_visible(&self) -> bool;
}
/// A primitive instance + some extra information about the primitive. This is
/// stored when constructing 3d rendering contexts, which involve cutting
/// primitive lists.
struct ExtendedPrimitiveInstance {
instance: PrimitiveInstance,
spatial_node_index: SpatialNodeIndex,
flags: PrimitiveFlags,
}
/// Internal tracking information about the currently pushed stacking context.
/// Used to track what operations need to happen when a stacking context is popped.
struct StackingContextInfo {
/// If true, pop an entry from the clip root stack.
pop_clip_root: bool,
/// If true, pop and entry from the containing block stack.
pop_containing_block: bool,
/// If true, pop an entry from the flattened stacking context stack.
pop_stacking_context: bool,
}
/// Properties of a stacking context that are maintained
/// during creation of the scene. These structures are
/// not persisted after the initial scene build.
struct FlattenedStackingContext {
/// The list of primitive instances added to this stacking context.
prim_list: PrimitiveList,
/// Primitive instance flags for compositing this stacking context
prim_flags: PrimitiveFlags,
/// The positioning node for this stacking context
spatial_node_index: SpatialNodeIndex,
/// The clip chain for this stacking context
clip_chain_id: ClipChainId,
/// The list of filters / mix-blend-mode for this
/// stacking context.
composite_ops: CompositeOps,
/// Bitfield of reasons this stacking context needs to
/// be an offscreen surface.
blit_reason: BlitReason,
/// CSS transform-style property.
transform_style: TransformStyle,
/// Defines the relationship to a preserve-3D hiearachy.
context_3d: Picture3DContext<ExtendedPrimitiveInstance>,
/// True if this stacking context is a backdrop root.
is_backdrop_root: bool,
/// True if this stacking context is redundant (i.e. doesn't require a surface)
is_redundant: bool,
}
impl FlattenedStackingContext {
/// Return true if the stacking context has a valid preserve-3d property
pub fn is_3d(&self) -> bool {
self.transform_style == TransformStyle::Preserve3D && self.composite_ops.is_empty()
}
/// Return true if the stacking context isn't needed.
pub fn is_redundant(
sc_flags: StackingContextFlags,
context_3d: &Picture3DContext<ExtendedPrimitiveInstance>,
composite_ops: &CompositeOps,
prim_flags: PrimitiveFlags,
blit_reason: BlitReason,
parent: Option<&FlattenedStackingContext>,
) -> bool {
// If this is a backdrop or blend container, it's needed
if sc_flags.intersects(StackingContextFlags::IS_BACKDROP_ROOT | StackingContextFlags::IS_BLEND_CONTAINER) {
return false;
}
// Any 3d context is required
if let Picture3DContext::In { .. } = context_3d {
return false;
}
// If any filters are present that affect the output
if composite_ops.has_valid_filters() {
return false;
}
// We can skip mix-blend modes if they are the first primitive in a stacking context,
// see pop_stacking_context for a full explanation.
if composite_ops.mix_blend_mode.is_some() {
if let Some(parent) = parent {
if !parent.prim_list.is_empty() {
return false;
}
}
}
// If need to isolate in surface due to clipping / mix-blend-mode
if !blit_reason.is_empty() {
return false;
}
// If this stacking context is a scrollbar, retain it so it can form a picture cache slice
if prim_flags.contains(PrimitiveFlags::IS_SCROLLBAR_CONTAINER) {
return false;
}
// It is redundant!
true
}
/// Cut the sequence of the immediate children recorded so far and generate a picture from them.
pub fn cut_item_sequence(
&mut self,
prim_store: &mut PrimitiveStore,
interners: &mut Interners,
composite_mode: Option<PictureCompositeMode>,
flat_items_context_3d: Picture3DContext<OrderedPictureChild>,
) -> Option<(PictureIndex, PrimitiveInstance)> {
if self.prim_list.is_empty() {
return None
}
let pic_index = PictureIndex(prim_store.pictures
.alloc()
.init(PicturePrimitive::new_image(
composite_mode.clone(),
flat_items_context_3d,
true,
self.prim_flags,
mem::replace(&mut self.prim_list, PrimitiveList::empty()),
self.spatial_node_index,
PictureOptions::default(),
))
);
let prim_instance = create_prim_instance(
pic_index,
composite_mode.into(),
self.clip_chain_id,
interners,
);
Some((pic_index, prim_instance))
}
}
/// A primitive that is added while a shadow context is
/// active is stored as a pending primitive and only
/// added to pictures during pop_all_shadows.
pub struct PendingPrimitive<T> {
spatial_node_index: SpatialNodeIndex,
clip_chain_id: ClipChainId,
info: LayoutPrimitiveInfo,
prim: T,
}
/// As shadows are pushed, they are stored as pending
/// shadows, and handled at once during pop_all_shadows.
pub struct PendingShadow {
shadow: Shadow,
should_inflate: bool,
spatial_node_index: SpatialNodeIndex,
clip_chain_id: ClipChainId,
}
pub enum ShadowItem {
Shadow(PendingShadow),
Image(PendingPrimitive<Image>),
LineDecoration(PendingPrimitive<LineDecoration>),
NormalBorder(PendingPrimitive<NormalBorderPrim>),
Primitive(PendingPrimitive<PrimitiveKeyKind>),
TextRun(PendingPrimitive<TextRun>),
}
impl From<PendingPrimitive<Image>> for ShadowItem {
fn from(image: PendingPrimitive<Image>) -> Self {
ShadowItem::Image(image)
}
}
impl From<PendingPrimitive<LineDecoration>> for ShadowItem {
fn from(line_dec: PendingPrimitive<LineDecoration>) -> Self {
ShadowItem::LineDecoration(line_dec)
}
}
impl From<PendingPrimitive<NormalBorderPrim>> for ShadowItem {
fn from(border: PendingPrimitive<NormalBorderPrim>) -> Self {
ShadowItem::NormalBorder(border)
}
}
impl From<PendingPrimitive<PrimitiveKeyKind>> for ShadowItem {
fn from(container: PendingPrimitive<PrimitiveKeyKind>) -> Self {
ShadowItem::Primitive(container)
}
}
impl From<PendingPrimitive<TextRun>> for ShadowItem {
fn from(text_run: PendingPrimitive<TextRun>) -> Self {
ShadowItem::TextRun(text_run)
}
}
fn create_prim_instance(
pic_index: PictureIndex,
composite_mode_key: PictureCompositeKey,
clip_chain_id: ClipChainId,
interners: &mut Interners,
) -> PrimitiveInstance {
let pic_key = PictureKey::new(
Picture { composite_mode_key },
);
let data_handle = interners
.picture
.intern(&pic_key, || ());
PrimitiveInstance::new(
LayoutRect::max_rect(),
PrimitiveInstanceKind::Picture {
data_handle,
pic_index,
segment_instance_index: SegmentInstanceIndex::INVALID,
},
clip_chain_id,
)
}
fn filter_ops_for_compositing(
input_filters: ItemRange<FilterOp>,
) -> Vec<Filter> {
// TODO(gw): Now that we resolve these later on,
// we could probably make it a bit
// more efficient than cloning these here.
input_filters.iter().map(|filter| filter.into()).collect()
}
fn filter_datas_for_compositing(
input_filter_datas: &[TempFilterData],
) -> Vec<FilterData> {
// TODO(gw): Now that we resolve these later on,
// we could probably make it a bit
// more efficient than cloning these here.
let mut filter_datas = vec![];
for temp_filter_data in input_filter_datas {
let func_types : Vec<ComponentTransferFuncType> = temp_filter_data.func_types.iter().collect();
debug_assert!(func_types.len() == 4);
filter_datas.push( FilterData {
func_r_type: func_types[0],
r_values: temp_filter_data.r_values.iter().collect(),
func_g_type: func_types[1],
g_values: temp_filter_data.g_values.iter().collect(),
func_b_type: func_types[2],
b_values: temp_filter_data.b_values.iter().collect(),
func_a_type: func_types[3],
a_values: temp_filter_data.a_values.iter().collect(),
});
}
filter_datas
}
fn filter_primitives_for_compositing(
input_filter_primitives: ItemRange<FilterPrimitive>,
) -> Vec<FilterPrimitive> {
// Resolve these in the flattener?
// TODO(gw): Now that we resolve these later on,
// we could probably make it a bit
// more efficient than cloning these here.
input_filter_primitives.iter().map(|primitive| primitive).collect()
}
fn process_repeat_size(
snapped_rect: &LayoutRect,
unsnapped_rect: &LayoutRect,
repeat_size: LayoutSize,
) -> LayoutSize {
// FIXME(aosmond): The tile size is calculated based on several parameters
// during display list building. It may produce a slightly different result
// than the bounds due to floating point error accumulation, even though in
// theory they should be the same. We do a fuzzy check here to paper over
// that. It may make more sense to push the original parameters into scene
// building and let it do a saner calculation with more information (e.g.
// the snapped values).
const EPSILON: f32 = 0.001;
LayoutSize::new(
if repeat_size.width.approx_eq_eps(&unsnapped_rect.size.width, &EPSILON) {
snapped_rect.size.width
} else {
repeat_size.width
},
if repeat_size.height.approx_eq_eps(&unsnapped_rect.size.height, &EPSILON) {
snapped_rect.size.height
} else {
repeat_size.height
},
)
}
|