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 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/viz/service/display/skia_renderer.h"
#include <algorithm>
#include <array>
#include <limits>
#include <optional>
#include <string>
#include <utility>
#include <variant>
#include "base/auto_reset.h"
#include "base/command_line.h"
#include "base/debug/crash_logging.h"
#include "base/debug/dump_without_crashing.h"
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/metrics/histogram_macros.h"
#include "base/numerics/angle_conversions.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/task/bind_post_task.h"
#include "base/task/single_thread_task_runner.h"
#include "base/trace_event/trace_event.h"
#include "base/trace_event/traced_value.h"
#include "build/build_config.h"
#include "cc/base/math_util.h"
#include "cc/debug/debug_colors.h"
#include "cc/paint/render_surface_filters.h"
#include "cc/paint/tone_map_util.h"
#include "components/viz/common/display/renderer_settings.h"
#include "components/viz/common/features.h"
#include "components/viz/common/frame_sinks/copy_output_request.h"
#include "components/viz/common/frame_sinks/copy_output_util.h"
#include "components/viz/common/quads/aggregated_render_pass.h"
#include "components/viz/common/quads/aggregated_render_pass_draw_quad.h"
#include "components/viz/common/quads/compositor_render_pass_draw_quad.h"
#include "components/viz/common/quads/debug_border_draw_quad.h"
#include "components/viz/common/quads/picture_draw_quad.h"
#include "components/viz/common/quads/solid_color_draw_quad.h"
#include "components/viz/common/quads/texture_draw_quad.h"
#include "components/viz/common/quads/tile_draw_quad.h"
#include "components/viz/common/resources/platform_color.h"
#include "components/viz/common/resources/shared_image_format_utils.h"
#include "components/viz/common/skia_helper.h"
#include "components/viz/common/viz_utils.h"
#include "components/viz/service/debugger/viz_debugger.h"
#include "components/viz/service/display/delegated_ink_handler.h"
#include "components/viz/service/display/delegated_ink_point_renderer_skia.h"
#include "components/viz/service/display/display_resource_provider.h"
#include "components/viz/service/display/display_resource_provider_skia.h"
#include "components/viz/service/display/output_surface.h"
#include "components/viz/service/display/output_surface_frame.h"
#include "components/viz/service/display/renderer_utils.h"
#include "components/viz/service/display/resource_fence.h"
#include "components/viz/service/display/skia_output_surface.h"
#include "gpu/command_buffer/client/shared_image_interface.h"
#include "gpu/command_buffer/common/shared_image_usage.h"
#include "gpu/command_buffer/common/swap_buffers_complete_params.h"
#include "gpu/command_buffer/common/sync_token.h"
#include "gpu/config/gpu_finch_features.h"
#include "media/base/media_switches.h"
#include "skia/ext/opacity_filter_canvas.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkColor.h"
#include "third_party/skia/include/core/SkColorFilter.h"
#include "third_party/skia/include/core/SkMatrix.h"
#include "third_party/skia/include/core/SkPath.h"
#include "third_party/skia/include/core/SkPixelRef.h"
#include "third_party/skia/include/core/SkShader.h"
#include "third_party/skia/include/core/SkString.h"
#include "third_party/skia/include/effects/SkColorMatrix.h"
#include "third_party/skia/include/effects/SkGradientShader.h"
#include "third_party/skia/include/effects/SkImageFilters.h"
#include "third_party/skia/include/effects/SkOverdrawColorFilter.h"
#include "third_party/skia/include/effects/SkRuntimeEffect.h"
#include "third_party/skia/include/effects/SkShaderMaskFilter.h"
#include "third_party/skia/include/gpu/ganesh/GrBackendSurface.h"
#include "third_party/skia/include/gpu/ganesh/GrDirectContext.h"
#include "third_party/skia/include/private/chromium/GrDeferredDisplayList.h"
#include "third_party/skia/modules/skcms/skcms.h"
#include "third_party/skia/src/core/SkCanvasPriv.h"
#include "ui/base/ui_base_features.h"
#include "ui/gfx/buffer_format_util.h"
#include "ui/gfx/color_space.h"
#include "ui/gfx/color_transform.h"
#include "ui/gfx/geometry/axis_transform2d.h"
#include "ui/gfx/geometry/linear_gradient.h"
#include "ui/gfx/geometry/point_conversions.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/rect_conversions.h"
#include "ui/gfx/geometry/rect_f.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/geometry/size_conversions.h"
#include "ui/gfx/geometry/size_f.h"
#include "ui/gfx/geometry/skia_conversions.h"
#include "ui/gfx/geometry/transform.h"
#include "ui/gfx/geometry/transform_util.h"
#include "ui/gfx/gpu_fence_handle.h"
#include "ui/gfx/hdr_metadata.h"
#include "ui/gfx/swap_result.h"
#if BUILDFLAG(IS_ANDROID)
#include "components/viz/service/display/overlay_processor_surface_control.h"
#endif
namespace viz {
namespace {
// Feature to temporarily create a dump in the case where we try and sample from
// a render pass that has never been drawn to.
// See: crbug.com/344458294, crbug.com/345673794
// TODO(crbug.com/347909405): Remove this
BASE_FEATURE(kDumpWithoutCrashingOnMissingRenderPassBacking,
"DumpWithoutCrashingOnMissingRenderPassBacking",
base::FEATURE_ENABLED_BY_DEFAULT);
#if BUILDFLAG(IS_WIN)
// Use BufferQueue for the primary plane instead of a DXGI swap chain or DComp
// surface.
BASE_FEATURE(kBufferQueue, "BufferQueue", base::FEATURE_DISABLED_BY_DEFAULT);
#endif
// Smallest unit that impacts anti-aliasing output. We use this to determine
// when an exterior edge (with AA) has been clipped (no AA). The specific value
// was chosen to match that used by gl_renderer.
static const float kAAEpsilon = 1.0f / 1024.0f;
// The gfx::QuadF draw_region passed to DoDrawQuad, converted to Skia types
struct SkDrawRegion {
SkDrawRegion() = default;
explicit SkDrawRegion(const gfx::QuadF& draw_region);
std::array<SkPoint, 4> points;
};
SkDrawRegion::SkDrawRegion(const gfx::QuadF& draw_region) {
points[0] = gfx::PointFToSkPoint(draw_region.p1());
points[1] = gfx::PointFToSkPoint(draw_region.p2());
points[2] = gfx::PointFToSkPoint(draw_region.p3());
points[3] = gfx::PointFToSkPoint(draw_region.p4());
}
bool IsTextureResource(DisplayResourceProviderSkia* resource_provider,
ResourceId resource_id) {
return !resource_provider->IsResourceSoftwareBacked(resource_id);
}
unsigned GetCornerAAFlags(const DrawQuad* quad,
const SkPoint& vertex,
unsigned edge_mask) {
// Returns mask of SkCanvas::QuadAAFlags, with bits set for each edge of the
// shared quad state's quad_layer_rect that vertex is touching.
unsigned mask = SkCanvas::kNone_QuadAAFlags;
if (std::abs(vertex.x()) < kAAEpsilon)
mask |= SkCanvas::kLeft_QuadAAFlag;
if (std::abs(vertex.x() - quad->shared_quad_state->quad_layer_rect.width()) <
kAAEpsilon)
mask |= SkCanvas::kRight_QuadAAFlag;
if (std::abs(vertex.y()) < kAAEpsilon)
mask |= SkCanvas::kTop_QuadAAFlag;
if (std::abs(vertex.y() - quad->shared_quad_state->quad_layer_rect.height()) <
kAAEpsilon)
mask |= SkCanvas::kBottom_QuadAAFlag;
// & with the overall edge_mask to take into account edges that were clipped
// by the visible rect.
return mask & edge_mask;
}
// This is slightly different than Transform::IsPositiveScaleAndTranslation()
// in that it also allows zero scales. This is because in the common
// orthographic case the z scale is 0.
bool Is2dScaleTranslateTransform(const gfx::Transform& transform) {
return transform.IsScaleOrTranslation() && transform.rc(0, 0) >= 0.0f &&
transform.rc(1, 1) >= 0.0f && transform.rc(2, 2) >= 0.0f;
}
bool IsExteriorEdge(unsigned corner_mask1, unsigned corner_mask2) {
return (corner_mask1 & corner_mask2) != 0;
}
unsigned GetRectilinearEdgeFlags(const DrawQuad* quad) {
// In the normal case, turn on AA for edges that represent the outside of
// the layer, and that aren't clipped by the visible rect.
unsigned mask = SkCanvas::kNone_QuadAAFlags;
if (quad->IsLeftEdge() &&
std::abs(quad->rect.x() - quad->visible_rect.x()) < kAAEpsilon)
mask |= SkCanvas::kLeft_QuadAAFlag;
if (quad->IsTopEdge() &&
std::abs(quad->rect.y() - quad->visible_rect.y()) < kAAEpsilon)
mask |= SkCanvas::kTop_QuadAAFlag;
if (quad->IsRightEdge() &&
std::abs(quad->rect.right() - quad->visible_rect.right()) < kAAEpsilon)
mask |= SkCanvas::kRight_QuadAAFlag;
if (quad->IsBottomEdge() &&
std::abs(quad->rect.bottom() - quad->visible_rect.bottom()) < kAAEpsilon)
mask |= SkCanvas::kBottom_QuadAAFlag;
return mask;
}
// This also modifies draw_region to clean up any degeneracies
void GetClippedEdgeFlags(const DrawQuad* quad,
unsigned* edge_mask,
SkDrawRegion* draw_region) {
// Instead of trying to rotate vertices of draw_region to align with Skia's
// edge label conventions, turn on an edge's label if it is aligned to any
// exterior edge.
unsigned p0Mask = GetCornerAAFlags(quad, draw_region->points[0], *edge_mask);
unsigned p1Mask = GetCornerAAFlags(quad, draw_region->points[1], *edge_mask);
unsigned p2Mask = GetCornerAAFlags(quad, draw_region->points[2], *edge_mask);
unsigned p3Mask = GetCornerAAFlags(quad, draw_region->points[3], *edge_mask);
unsigned mask = SkCanvas::kNone_QuadAAFlags;
// The "top" is p0 to p1
if (IsExteriorEdge(p0Mask, p1Mask))
mask |= SkCanvas::kTop_QuadAAFlag;
// The "right" is p1 to p2
if (IsExteriorEdge(p1Mask, p2Mask))
mask |= SkCanvas::kRight_QuadAAFlag;
// The "bottom" is p2 to p3
if (IsExteriorEdge(p2Mask, p3Mask))
mask |= SkCanvas::kBottom_QuadAAFlag;
// The "left" is p3 to p0
if (IsExteriorEdge(p3Mask, p0Mask))
mask |= SkCanvas::kLeft_QuadAAFlag;
// If the clipped draw_region has adjacent non-AA edges that touch the
// exterior edge (which should be AA'ed), move the degenerate vertex to the
// appropriate index so that Skia knows to construct a coverage ramp at that
// corner. This is not an ideal solution, but is the best hint we can give,
// given our limited information post-BSP splitting.
if (draw_region->points[2] == draw_region->points[3]) {
// The BSP splitting always creates degenerate quads with the duplicate
// vertex in the last two indices.
if (p0Mask && !(mask & SkCanvas::kLeft_QuadAAFlag) &&
!(mask & SkCanvas::kTop_QuadAAFlag)) {
// Rewrite draw_region from p0,p1,p2,p2 to p0,p1,p2,p0; top edge stays off
// right edge is preserved, bottom edge turns off, left edge turns on
draw_region->points[3] = draw_region->points[0];
mask = SkCanvas::kLeft_QuadAAFlag | (mask & SkCanvas::kRight_QuadAAFlag);
} else if (p1Mask && !(mask & SkCanvas::kTop_QuadAAFlag) &&
!(mask & SkCanvas::kRight_QuadAAFlag)) {
// Rewrite draw_region to p0,p1,p1,p2; top edge stays off, right edge
// turns on, bottom edge turns off, left edge is preserved
draw_region->points[2] = draw_region->points[1];
mask = SkCanvas::kRight_QuadAAFlag | (mask & SkCanvas::kLeft_QuadAAFlag);
}
// p2 could follow the same process, but if its adjacent edges are AA
// (skipping the degenerate edge to p3), it's actually already in the
// desired vertex ordering; and since p3 is in the same location, it's
// equivalent to p2 so it doesn't need checking either.
} // Else not degenerate, so can't to correct non-AA corners touching AA edge
*edge_mask = mask;
}
bool IsAAForcedOff(const DrawQuad* quad) {
switch (quad->material) {
case DrawQuad::Material::kPictureContent:
return PictureDrawQuad::MaterialCast(quad)->force_anti_aliasing_off;
case DrawQuad::Material::kCompositorRenderPass:
// We should not have compositor render passes here.
NOTREACHED();
case DrawQuad::Material::kAggregatedRenderPass:
return AggregatedRenderPassDrawQuad::MaterialCast(quad)
->force_anti_aliasing_off;
case DrawQuad::Material::kSolidColor:
return SolidColorDrawQuad::MaterialCast(quad)->force_anti_aliasing_off;
case DrawQuad::Material::kTiledContent:
return TileDrawQuad::MaterialCast(quad)->force_anti_aliasing_off;
case DrawQuad::Material::kTextureContent:
// This is done to match the behaviour of GLRenderer and we can revisit it
// later.
return true;
default:
return false;
}
}
bool UseNearestNeighborSampling(const DrawQuad* quad) {
switch (quad->material) {
case DrawQuad::Material::kPictureContent:
return PictureDrawQuad::MaterialCast(quad)->nearest_neighbor;
case DrawQuad::Material::kTextureContent:
return TextureDrawQuad::MaterialCast(quad)->nearest_neighbor;
case DrawQuad::Material::kTiledContent:
return TileDrawQuad::MaterialCast(quad)->nearest_neighbor;
default:
// Other quad types do not expose nearest_neighbor.
return false;
}
}
SkSamplingOptions GetSampling(const DrawQuad* quad) {
if (UseNearestNeighborSampling(quad))
return SkSamplingOptions(SkFilterMode::kNearest);
// Default to bilinear if the quad doesn't specify nearest_neighbor.
// TODO(penghuang): figure out how to set correct filter quality for YUV and
// video stream quads.
return SkSamplingOptions(SkFilterMode::kLinear);
}
// Returns kFast if sampling outside of vis_tex_coords due to AA or bilerp will
// not go outside of the content area, or if the content area is the full image
// (in which case hardware clamping handles it automatically). Different quad
// types have different rules for the content area within the image.
SkCanvas::SrcRectConstraint GetTextureConstraint(
const SkImage* image,
const gfx::RectF& vis_tex_coords,
const gfx::RectF& valid_texel_bounds) {
bool fills_left = valid_texel_bounds.x() <= 0.f;
bool fills_right = valid_texel_bounds.right() >= image->width();
bool fills_top = valid_texel_bounds.y() <= 0.f;
bool fills_bottom = valid_texel_bounds.bottom() >= image->height();
if (fills_left && fills_right && fills_top && fills_bottom) {
// The entire image is contained in the content area, so hardware clamping
// ensures only content texels are sampled
return SkCanvas::kFast_SrcRectConstraint;
}
gfx::RectF safe_texels = valid_texel_bounds;
safe_texels.Inset(0.5f);
// Check each axis independently; tile quads may only need clamping on one
// side (e.g. right or bottom) and this logic doesn't fully match a simple
// contains() check.
if ((!fills_left && vis_tex_coords.x() < safe_texels.x()) ||
(!fills_right && vis_tex_coords.right() > safe_texels.right())) {
return SkCanvas::kStrict_SrcRectConstraint;
}
if ((!fills_top && vis_tex_coords.y() < safe_texels.y()) ||
(!fills_bottom && vis_tex_coords.bottom() > safe_texels.bottom())) {
return SkCanvas::kStrict_SrcRectConstraint;
}
// The texture coordinates are far enough from the content area that even with
// bilerp and AA, it won't sample outside the content area
return SkCanvas::kFast_SrcRectConstraint;
}
// Return a color filter that multiplies the incoming color by the fixed alpha
sk_sp<SkColorFilter> MakeOpacityFilter(float alpha, sk_sp<SkColorFilter> in) {
SkColor4f alpha_as_color = {1.0, 1.0, 1.0, alpha};
// MakeModeFilter treats fixed color as src, and input color as dst.
// kDstIn is (srcAlpha * dstColor, srcAlpha * dstAlpha) so this makes the
// output color equal to input color * alpha.
// TODO(michaelludwig): Update to pass alpha_as_color as-is once
// skbug.com/13637 is resolved (adds Blend + SkColor4f variation).
sk_sp<SkColorFilter> opacity =
SkColorFilters::Blend(alpha_as_color.toSkColor(), SkBlendMode::kDstIn);
// Opaque (alpha = 1.0) and kDstIn returns nullptr to signal a no-op, so that
// case should just return 'in'.
if (opacity) {
return opacity->makeComposed(std::move(in));
} else {
return in;
}
}
// Porter-Duff blend mode utility functions, where the final color is
// represented as a weighted sum of the incoming src and existing dst color.
// See [https://skia.org/user/api/SkBlendMode_Reference]
bool IsPorterDuffBlendMode(SkBlendMode blendMode) {
return blendMode <= SkBlendMode::kLastCoeffMode;
}
// Returns true if drawing transparent black with |blendMode| would modify the
// destination buffer. If false is returned, the draw would have no discernible
// effect on the pixel color so the entire draw can be skipped.
bool TransparentBlackAffectsOutput(SkBlendMode blendMode) {
SkBlendModeCoeff src, dst;
if (!SkBlendMode_AsCoeff(blendMode, &src, &dst)) {
// An advanced blend mode that can't be represented as coefficients, so
// assume it modifies the output.
return true;
}
// True when the dst coefficient is not equal to 1 (when src = (0,0,0,0))
return dst != SkBlendModeCoeff::kOne && dst != SkBlendModeCoeff::kISA &&
dst != SkBlendModeCoeff::kISC;
}
// Returns true if src content drawn with |blendMode| into a RenderPass would
// produce the exact same image as the original src content.
bool RenderPassPreservesContent(SkBlendMode blendMode) {
SkBlendModeCoeff src, dst;
if (!SkBlendMode_AsCoeff(blendMode, &src, &dst)) {
return false;
}
// True when src coefficient is equal to 1 (when dst = (0,0,0,0))
return src == SkBlendModeCoeff::kOne || src == SkBlendModeCoeff::kIDA ||
src == SkBlendModeCoeff::kIDC;
}
// Returns true if src content draw with |blendMode| into an empty RenderPass
// would produce a transparent black image.
bool RenderPassRemainsTransparent(SkBlendMode blendMode) {
SkBlendModeCoeff src, dst;
if (!SkBlendMode_AsCoeff(blendMode, &src, &dst)) {
return false;
}
// True when src coefficient is equal to 0 (when dst = (0,0,0,0))
return src == SkBlendModeCoeff::kZero || src == SkBlendModeCoeff::kDA ||
src == SkBlendModeCoeff::kDC;
}
#if BUILDFLAG(ENABLE_VULKAN) && BUILDFLAG(IS_CHROMEOS) && \
BUILDFLAG(USE_V4L2_CODEC)
constexpr size_t kMaxProtectedContentWidth = 3840;
constexpr size_t kMaxProtectedContentHeight = 2160;
#endif
} // namespace
// A helper class to emit Viz debugger messages that has access to SkiaRenderer
// internals.
class SkiaRenderer::VizDebuggerLog {
public:
static void DebugLogDumpRenderPassBackings(
const base::flat_map<AggregatedRenderPassId, RenderPassBacking>&
render_pass_backings) {
bool enabled;
DBG_CONNECTED_OR_TRACING(enabled);
if (enabled) {
DBG_LOG("renderer.skia.render_pass_backings",
"render_pass_backings_ = [");
for (auto& [render_pass_id, backing] : render_pass_backings) {
base::trace_event::TracedValueJSON value;
base::trace_event::TracedValue::Dictionary(
{
{"size", backing.size.ToString()},
{"generate_mipmap", backing.generate_mipmap},
{"color_space", backing.color_space.ToString()},
{"alpha_type",
backing.alpha_type == RenderPassAlphaType::kPremul ? "premul"
: "opaque"},
{"format", backing.format.ToString()},
{"mailbox", backing.mailbox.ToDebugString()},
{"is_root", backing.is_root},
{"is_scanout", backing.is_scanout},
{"scanout_dcomp_surface", backing.scanout_dcomp_surface},
{"drawn_rect", backing.drawn_rect.ToString()},
})
.WriteToValue(&value);
DBG_LOG("renderer.skia.render_pass_backings", "%" PRIu64 ": %s",
render_pass_id.value(), value.ToFormattedJSON().c_str());
}
DBG_LOG("renderer.skia.render_pass_backings", "]");
}
}
static void DebugLogNewRenderPassBacking(
const AggregatedRenderPassId& render_pass_id,
const RenderPassRequirements& requirements) {
bool enabled;
DBG_CONNECTED_OR_TRACING(enabled);
if (enabled) {
base::trace_event::TracedValueJSON value;
base::trace_event::TracedValue::Dictionary(
{
{"size", requirements.size.ToString()},
{"generate_mipmap", requirements.generate_mipmap},
{"format", requirements.format.ToString()},
{"color_space", requirements.color_space.ToString()},
{"alpha_type", static_cast<int>(requirements.alpha_type)},
{"is_scanout", requirements.is_scanout},
{"scanout_dcomp_surface", requirements.scanout_dcomp_surface},
})
.WriteToValue(&value);
DBG_LOG("renderer.skia.render_pass_backings",
"allocate backing for render_pass %" PRIu64 ", %s",
render_pass_id.value(), value.ToFormattedJSON().c_str());
}
}
};
SkiaRenderer::RenderPassBacking::RenderPassBacking() = default;
SkiaRenderer::RenderPassBacking::RenderPassBacking(
const SkiaRenderer::RenderPassBacking&) = default;
SkiaRenderer::RenderPassBacking& SkiaRenderer::RenderPassBacking::operator=(
const SkiaRenderer::RenderPassBacking&) = default;
SkiaRenderer::RenderPassBacking::RenderPassBacking(
gfx::Size size,
bool generate_mipmap,
gfx::ColorSpace color_space,
RenderPassAlphaType alpha_type,
SharedImageFormat format,
gpu::Mailbox mailbox,
bool is_root,
bool is_scanout,
bool scanout_dcomp_surface)
: size(size),
generate_mipmap(generate_mipmap),
color_space(color_space),
alpha_type(alpha_type),
format(format),
mailbox(mailbox),
is_root(is_root),
is_scanout(is_scanout),
scanout_dcomp_surface(scanout_dcomp_surface) {}
// chrome style prevents this from going in skia_renderer.h, but since it
// uses std::optional, the style also requires it to have a declared ctor
SkiaRenderer::BatchedQuadState::BatchedQuadState() = default;
// State calculated from a DrawQuad and current renderer state, that is common
// to all DrawQuad rendering.
struct SkiaRenderer::DrawQuadParams {
DrawQuadParams() = default;
DrawQuadParams(const gfx::Transform& cdt,
const gfx::RectF& rect,
const gfx::RectF& visible_rect,
unsigned aa_flags,
SkBlendMode blend_mode,
float opacity,
const SkSamplingOptions& sampling,
const gfx::QuadF* draw_region);
// target_to_device_transform * quad_to_target_transform normally, or
// quad_to_target_transform if the remaining device transform is held in the
// DrawRPDQParams for a bypass quad.
gfx::Transform content_device_transform;
// The DrawQuad's rect.
gfx::RectF rect;
// The DrawQuad's visible_rect, possibly explicitly clipped by the scissor
gfx::RectF visible_rect;
// Initialized to the visible_rect, relevant quad types should updated based
// on their specialized properties.
gfx::RectF vis_tex_coords;
// SkCanvas::QuadAAFlags, already taking into account settings
// (but not certain quad type's force_antialias_off bit)
unsigned aa_flags;
// Final blend mode to use, respecting quad settings + opacity optimizations
SkBlendMode blend_mode;
// Final opacity of quad
float opacity;
// Resolved sampling from quad settings
SkSamplingOptions sampling;
// Optional restricted draw geometry, will point to a length 4 SkPoint array
// with its points in CW order matching Skia's vertex/edge expectations.
std::optional<SkDrawRegion> draw_region;
// Optional mask filter info that may contain rounded corner clip and/or a
// gradient mask to apply. If present, rounded corner clip will have been
// transformed to device space and ShouldApplyRoundedCorner returns true. If
// present, gradient mask will have been transformed to device space and
// ShouldApplyGradientMask returns true.
std::optional<gfx::MaskFilterInfo> mask_filter_info;
// Optional device space clip to apply. If present, it is equal to the current
// |scissor_rect_| of the renderer.
std::optional<gfx::Rect> scissor_rect;
SkPaint paint(sk_sp<SkColorFilter> color_filter) const {
SkPaint p;
if (color_filter) {
p.setColorFilter(color_filter);
}
p.setBlendMode(blend_mode);
p.setAlphaf(opacity);
p.setAntiAlias(aa_flags != SkCanvas::kNone_QuadAAFlags);
return p;
}
SkPath draw_region_in_path() const {
if (draw_region) {
return SkPath::Polygon(draw_region->points, /*isClosed=*/true);
}
return SkPath();
}
void ApplyScissor(const SkiaRenderer* renderer,
const DrawQuad* quad,
const std::optional<gfx::Rect>& scissor_to_apply);
};
SkiaRenderer::DrawQuadParams::DrawQuadParams(const gfx::Transform& cdt,
const gfx::RectF& rect,
const gfx::RectF& visible_rect,
unsigned aa_flags,
SkBlendMode blend_mode,
float opacity,
const SkSamplingOptions& sampling,
const gfx::QuadF* draw_region)
: content_device_transform(cdt),
rect(rect),
visible_rect(visible_rect),
vis_tex_coords(visible_rect),
aa_flags(aa_flags),
blend_mode(blend_mode),
opacity(opacity),
sampling(sampling) {
if (draw_region) {
this->draw_region.emplace(*draw_region);
}
}
#if BUILDFLAG(IS_APPLE) || BUILDFLAG(IS_OZONE) || BUILDFLAG(IS_WIN)
struct SkiaRenderer::RenderPassOverlayParams {
AggregatedRenderPassId render_pass_id;
RenderPassBacking render_pass_backing;
AggregatedRenderPassDrawQuad rpdq;
SharedQuadState shared_quad_state;
cc::FilterOperations filters;
cc::FilterOperations backdrop_filters;
// Represents the number of |OverlayLock|s (i.e. number of distinct frames)
// that reference this.
int ref_count = 0;
};
#endif
enum class SkiaRenderer::BypassMode {
// The RenderPass's contents' blendmode would have made a transparent black
// image and the RenderPass's own blend mode does not effect transparent black
kSkip,
// The renderPass's contents' creates a transparent image, but the
// RenderPass's own blend mode must still process the transparent pixels (e.g.
// certain filters affect transparent black).
kDrawTransparentQuad,
// Can draw the bypass quad with the modified parameters
kDrawBypassQuad
};
// Scoped helper class for building SkImage from resource id.
class SkiaRenderer::ScopedSkImageBuilder {
public:
ScopedSkImageBuilder(SkiaRenderer* skia_renderer,
ResourceId resource_id,
bool maybe_concurrent_reads,
SkAlphaType alpha_type = kPremul_SkAlphaType,
sk_sp<SkColorSpace> override_color_space = nullptr,
bool raw_draw_if_possible = false,
bool force_rgbx = false);
ScopedSkImageBuilder(const ScopedSkImageBuilder&) = delete;
ScopedSkImageBuilder& operator=(const ScopedSkImageBuilder&) = delete;
~ScopedSkImageBuilder() = default;
const SkImage* sk_image() const { return sk_image_.get(); }
const cc::PaintOpBuffer* paint_op_buffer() const { return paint_op_buffer_; }
const std::optional<SkColor4f>& clear_color() const { return clear_color_; }
private:
sk_sp<SkImage> sk_image_;
raw_ptr<const cc::PaintOpBuffer> paint_op_buffer_ = nullptr;
std::optional<SkColor4f> clear_color_;
};
SkiaRenderer::ScopedSkImageBuilder::ScopedSkImageBuilder(
SkiaRenderer* skia_renderer,
ResourceId resource_id,
bool maybe_concurrent_reads,
SkAlphaType alpha_type,
sk_sp<SkColorSpace> override_color_space,
bool raw_draw_if_possible,
bool force_rgbx) {
if (!resource_id)
return;
auto* resource_provider = skia_renderer->resource_provider();
DCHECK(IsTextureResource(resource_provider, resource_id));
auto* image_context = skia_renderer->lock_set_for_external_use_.LockResource(
resource_id, maybe_concurrent_reads, raw_draw_if_possible);
// |ImageContext::image| provides thread safety: (a) this ImageContext is
// only accessed by GPU thread after |image| is set and (b) the fields of
// ImageContext that are accessed by both compositor and GPU thread are no
// longer modified after |image| is set.
if (!image_context->has_image()) {
image_context->set_alpha_type(alpha_type);
}
// We need the original TransferableResource.color_space for YUV => RGB
// conversion.
skia_renderer->skia_output_surface_->MakePromiseSkImage(
image_context, resource_provider->GetColorSpace(resource_id), force_rgbx);
paint_op_buffer_ = image_context->paint_op_buffer();
clear_color_ = image_context->clear_color();
sk_image_ = image_context->image();
LOG_IF(ERROR, !image_context->has_image() && !paint_op_buffer_)
<< "Failed to create the promise sk image or get paint ops.";
if (sk_image_ && override_color_space) {
sk_image_ = sk_image_->reinterpretColorSpace(override_color_space);
}
}
// Parameters needed to draw a CompositorRenderPassDrawQuad.
struct SkiaRenderer::DrawRPDQParams {
struct BypassGeometry {
// The additional matrix to concatenate to the SkCanvas after image filters
// have been configured so that the DrawQuadParams geometry is properly
// mapped (i.e. when set, |visible_rect| and |draw_region| must be
// pre-transformed by this before |content_device_transform|).
SkMatrix transform;
// Clipping in bypassed render pass coordinate space. This can come from
// RenderPassDrawQuad::visible_rect and the bypassing quad's clip_rect.
gfx::RectF clip_rect;
};
class MaskShader {
public:
MaskShader(ResourceId mask_resource_id, SkMatrix mask_to_quad_matrix)
: mask_resource_id_(mask_resource_id),
mask_to_quad_matrix_(mask_to_quad_matrix) {
CHECK(!mask_resource_id_.is_null());
}
// Get the resolved mask image and calculated transform matrix baked into an
// SkShader
sk_sp<SkShader> GetOrCreateSkShader(SkiaRenderer* renderer) const;
private:
ResourceId mask_resource_id_;
// Map mask rect to full quad rect so that mask coordinates don't change
// with clipping.
SkMatrix mask_to_quad_matrix_;
mutable sk_sp<SkShader> sk_shader_ = nullptr;
};
DrawRPDQParams() : filter_bounds(SkRect::MakeEmpty()) {}
explicit DrawRPDQParams(const gfx::RectF& visible_rect)
: rpdq_visible_rect(gfx::RectFToSkRect(visible_rect)),
filter_bounds(rpdq_visible_rect) {}
// Root of the calculated image filter DAG to be applied to the render pass.
sk_sp<SkImageFilter> image_filter = nullptr;
// If |image_filter| can be represented as a single color filter, this will
// be that filter. |image_filter| will still be non-null.
sk_sp<SkColorFilter> color_filter = nullptr;
// Root of the calculated backdrop filter DAG to be applied to the render pass
// Backdrop filtered content must be clipped to |backdrop_filter_bounds| and
// the DrawQuad's rect (or draw_region if BSP-clipped).
sk_sp<SkImageFilter> backdrop_filter = nullptr;
// If present, the mask image, which can be applied using SkCanvas::clipShader
// in the RPDQ's coord space.
std::optional<MaskShader> mask_shader;
// Backdrop border box for the render pass, to clip backdrop-filtered content
// (but not the rest of the RPDQ itself).
std::optional<SkPath> backdrop_filter_bounds;
// Original render pass's visible rect, which will be intersected with
// |backdrop_filter_bounds| to determine the extent of backdrop content.
// It is preserved here as the original DrawQuad's |visible_rect| may be
// adjusted as part of bypassing render passes.
SkRect rpdq_visible_rect;
// The content space bounds that includes any filtered extents. If empty,
// the draw can be skipped. It may represent fractional pixel coverage.
SkRect filter_bounds;
// Multiplier used for downscaling backdrop filter.
float backdrop_filter_quality = 1.0f;
// Geometry from the bypassed RenderPassDrawQuad.
std::optional<BypassGeometry> bypass_geometry;
// True when there is an |image_filter| and it's not equivalent to
// |color_filter|.
bool has_complex_image_filter() const {
return image_filter && !color_filter;
}
// True if the RenderPass's output rect would clip the visible contents that
// are bypassing the renderpass' offscreen buffer.
bool needs_bypass_clip(const gfx::RectF& content_rect) const {
if (!bypass_geometry) {
return false;
}
SkRect content_bounds =
bypass_geometry->transform.mapRect(gfx::RectFToSkRect(content_rect));
return !bypass_geometry->clip_rect.Contains(
gfx::SkRectToRectF(content_bounds));
}
// Sets a clip on the canvas to restrict the size of the Skia layer that holds
// the backdrop filtered content to the size of the DrawQuad. When possible
// this is an exact clip to reduce operations performed within the backdrop
// layer; otherwise it's conservative to constrain size without impacting
// visuals.
void SetBackdropFilterClip(SkCanvas* canvas,
const DrawQuadParams* params) const;
// Erases backdrop filtered content outside of the DrawQuad and backdrop
// filter bounds rrect within the backdrop layer. This is a no-op if exact
// clipping was used in SetBackdropFilterClip to achieve the same effect.
// Otherwise, this is necessary to limit the backdrop content without
// impacting the DrawQuad or regular filter output.
void ClearOutsideBackdropBounds(SkCanvas* canvas,
const DrawQuadParams* params) const;
};
sk_sp<SkShader> SkiaRenderer::DrawRPDQParams::MaskShader::GetOrCreateSkShader(
SkiaRenderer* renderer) const {
if (sk_shader_) {
return sk_shader_;
}
ScopedSkImageBuilder mask_image_builder(renderer, mask_resource_id_,
/*maybe_concurrent_reads=*/false);
const SkImage* mask_image = mask_image_builder.sk_image();
CHECK(mask_image);
sk_shader_ = mask_image->makeShader(
SkTileMode::kClamp, SkTileMode::kClamp,
SkSamplingOptions(SkFilterMode::kLinear, SkMipmapMode::kNone),
&mask_to_quad_matrix_);
return sk_shader_;
}
void SkiaRenderer::DrawRPDQParams::SetBackdropFilterClip(
SkCanvas* canvas,
const DrawQuadParams* params) const {
if (!backdrop_filter) {
return; // No clipping necessary
}
const bool aa = params->aa_flags != SkCanvas::kNone_QuadAAFlags;
// filter_bounds is either a conservative bounding box that will not restrict
// the output of any forward-filters applied to the RPDQ, or it is exactly
// the content bounds that the backdrop filter should be limited to.
canvas->clipRect(filter_bounds, aa);
}
void SkiaRenderer::DrawRPDQParams::ClearOutsideBackdropBounds(
SkCanvas* canvas,
const DrawQuadParams* params) const {
if (!backdrop_filter) {
return; // Nothing to clear within the layer
}
// Must erase pixels not in the intersection of the backdrop_filter_bounds,
// visible_rect, and any draw_region. This is the union of the inverse fills
// of those shapes, which can be accomplished most efficiently by clipping
// the shape with the kDifference op and then clearing the canvas, per shape
const bool aa = params->aa_flags != SkCanvas::kNone_QuadAAFlags;
if (backdrop_filter_bounds) {
canvas->save();
canvas->clipPath(*backdrop_filter_bounds, SkClipOp::kDifference, aa);
canvas->clear(SK_ColorTRANSPARENT);
canvas->restore();
}
if (params->draw_region) {
canvas->save();
if (bypass_geometry) {
// If there's a bypass geometry, the draw_region is relative to that
// coordinate space.
canvas->concat(bypass_geometry->transform);
}
canvas->clipPath(params->draw_region_in_path(), SkClipOp::kDifference, aa);
canvas->clear(SK_ColorTRANSPARENT);
canvas->restore();
} else {
// NOTE: Use |rpdq_visible_rect| and not params->visible_rect because it's
// the render pass's extent that constraints backdrop filter content, but
// if we bypassed the RPDQ, |params->visible_rect| can be smaller and
// reflect the bounds of inner content pre-expansion by a backdrop filter.
if (!rpdq_visible_rect.contains(filter_bounds) &&
(!backdrop_filter_bounds ||
!rpdq_visible_rect.contains(backdrop_filter_bounds->getBounds()))) {
// If the |draw_region| is defined, it's already a subset of |rect|, so
// we don't have to clear both. Similarly, if |filter_bounds| is contained
// within the quad, the clip set in BackdropFilterClip() discards anything
// that would be cleared here. And if |backdrop_filter_bounds| is
// contained within the quad, the first clear was sufficient. Otherwise,
// there is some excess backdrop content that must still be erased.
canvas->save();
canvas->clipRect(rpdq_visible_rect, SkClipOp::kDifference, aa);
canvas->clear(SK_ColorTRANSPARENT);
canvas->restore();
}
}
}
// A read lock based fence that is signaled after gpu commands are completed
// meaning the resource has been read.
// TODO(fangzhoug): Move this out of this file s.t. it can be referenced in
// display_resource_provider_skia_unittest.cc.
class SkiaRenderer::FrameResourceGpuCommandsCompletedFence
: public ResourceFence {
public:
explicit FrameResourceGpuCommandsCompletedFence(
DisplayResourceProviderSkia* resource_provider)
: ResourceFence(resource_provider) {}
FrameResourceGpuCommandsCompletedFence() = delete;
FrameResourceGpuCommandsCompletedFence(
const FrameResourceGpuCommandsCompletedFence&) = delete;
FrameResourceGpuCommandsCompletedFence& operator=(
const FrameResourceGpuCommandsCompletedFence&) = delete;
// ResourceFence implementation.
bool HasPassed() override { return passed_; }
gfx::GpuFenceHandle GetGpuFenceHandle() override { NOTREACHED(); }
void Signal() {
passed_ = true;
FencePassed();
}
private:
~FrameResourceGpuCommandsCompletedFence() override = default;
bool passed_ = false;
};
// FrameResourceFence that gets a ReleaseFence which is later set to returned
// resources.
// TODO(fangzhoug): Move this out of this file s.t. it can be referenced in
// display_resource_provider_skia_unittest.cc.
class SkiaRenderer::FrameResourceReleaseFence : public ResourceFence {
public:
explicit FrameResourceReleaseFence(
DisplayResourceProviderSkia* resource_provider)
: ResourceFence(resource_provider) {}
FrameResourceReleaseFence() = delete;
FrameResourceReleaseFence(const FrameResourceReleaseFence&) = delete;
FrameResourceReleaseFence& operator=(const FrameResourceReleaseFence&) =
delete;
// ResourceFence implementation:
bool HasPassed() override { return release_fence_.has_value(); }
gfx::GpuFenceHandle GetGpuFenceHandle() override {
return HasPassed() ? release_fence_.value().Clone() : gfx::GpuFenceHandle();
}
void SetReleaseFenceCallback(gfx::GpuFenceHandle release_fence) {
release_fence_ = std::move(release_fence);
FencePassed();
}
private:
~FrameResourceReleaseFence() override = default;
// This is made optional so that the value is set after
// SetReleaseFenceCallback is called. Otherwise, there is no way to know if
// the fence has been set and a null handle is a "valid" handle.
std::optional<gfx::GpuFenceHandle> release_fence_;
};
SkiaRenderer::SkiaRenderer(const RendererSettings* settings,
const DebugRendererSettings* debug_settings,
OutputSurface* output_surface,
DisplayResourceProviderSkia* resource_provider,
OverlayProcessorInterface* overlay_processor,
SkiaOutputSurface* skia_output_surface)
: DirectRenderer(settings,
debug_settings,
output_surface,
resource_provider,
overlay_processor),
skia_output_surface_(skia_output_surface),
lock_set_for_external_use_(resource_provider, skia_output_surface_),
is_using_raw_draw_(features::IsUsingRawDraw()) {
DCHECK(skia_output_surface_);
// There can be different synchronization types requested for different
// resources. Some of them may require SyncToken, others - ReadLockFence, and
// others may need ReleaseFence. SyncTokens are set when the output surface
// is flushed and external resources are released. However, other resources
// require additional setup, which helps to handle that.
current_gpu_commands_completed_fence_ =
base::MakeRefCounted<FrameResourceGpuCommandsCompletedFence>(
resource_provider);
current_release_fence_ =
base::MakeRefCounted<FrameResourceReleaseFence>(resource_provider);
this->resource_provider()->SetGpuCommandsCompletedFence(
current_gpu_commands_completed_fence_.get());
this->resource_provider()->SetReleaseFence(current_release_fence_.get());
#if BUILDFLAG(IS_WIN)
// Windows does not normally use buffer queue because swap chains and DComp
// surfaces internally manage buffers and cross-frame damage. It instead lets
// the renderer allocate the root surface like a normal render pass backing.
// It's possible to use BufferQueue with DComp textures, so we can optionally
// enable it behind a feature flag.
const bool want_buffer_queue =
base::FeatureList::IsEnabled(kBufferQueue) &&
output_surface_->capabilities().dc_support_level >=
OutputSurface::DCSupportLevel::kDCompTexture;
#else
const bool want_buffer_queue = true;
#endif
if (want_buffer_queue &&
output_surface->capabilities().renderer_allocates_images) {
// When using dynamic frame buffer allocation we'll start with 0 buffers and
// let EnsureMinNumberOfBuffers() increase it later.
size_t number_of_buffers =
output_surface->capabilities().supports_dynamic_frame_buffer_allocation
? 0
: output_surface->capabilities().number_of_buffers;
buffer_queue_ = std::make_unique<BufferQueue>(
skia_output_surface_, skia_output_surface_->GetSurfaceHandle(),
number_of_buffers);
}
#if BUILDFLAG(ENABLE_VULKAN) && BUILDFLAG(IS_CHROMEOS) && \
BUILDFLAG(USE_V4L2_CODEC)
protected_buffer_queue_ = std::make_unique<BufferQueue>(
skia_output_surface_, skia_output_surface_->GetSurfaceHandle(), 3,
/*is_protected=*/true);
#endif
}
SkiaRenderer::~SkiaRenderer() = default;
bool SkiaRenderer::CanPartialSwap() {
return output_surface_->capabilities().supports_post_sub_buffer;
}
void SkiaRenderer::BeginDrawingFrame() {
TRACE_EVENT0("viz", "SkiaRenderer::BeginDrawingFrame");
DCHECK(!current_gpu_commands_completed_fence_->was_set());
DCHECK(!current_release_fence_->was_set());
}
void SkiaRenderer::FinishDrawingFrame() {
TRACE_EVENT0("viz", "SkiaRenderer::FinishDrawingFrame");
current_canvas_ = nullptr;
swap_buffer_rect_ = current_frame()->root_damage_rect;
VizDebuggerLog::DebugLogDumpRenderPassBackings(render_pass_backings_);
#if BUILDFLAG(IS_OZONE)
MaybeScheduleBackgroundImage(current_frame()->overlay_list);
#endif // BUILDFLAG(IS_OZONE)
// TODO(weiliangc): Remove this once OverlayProcessor schedules overlays.
if (current_frame()->output_surface_plane) {
CHECK(output_surface_->capabilities().renderer_allocates_images);
auto& surface_plane = current_frame()->output_surface_plane.value();
auto root_pass_backing =
render_pass_backings_.find(current_frame()->root_render_pass->id);
// The root pass backing should always exist.
DCHECK(root_pass_backing != render_pass_backings_.end());
OverlayCandidate surface_candidate;
surface_candidate.mailbox = root_pass_backing->second.mailbox;
surface_candidate.is_root_render_pass = true;
#if BUILDFLAG(IS_APPLE) || BUILDFLAG(IS_WIN)
surface_candidate.transform = gfx::Transform();
#else
surface_candidate.transform = surface_plane.transform;
#endif
surface_candidate.display_rect = surface_plane.display_rect;
surface_candidate.uv_rect = surface_plane.uv_rect;
surface_candidate.resource_size_in_pixels = surface_plane.resource_size;
surface_candidate.format = surface_plane.format;
surface_candidate.color_space = surface_plane.color_space;
if (current_frame()->display_color_spaces.SupportsHDR() &&
current_frame()->root_render_pass->content_color_usage ==
gfx::ContentColorUsage::kHDR) {
surface_candidate.hdr_metadata.extended_range.emplace();
// TODO(crbug.com/40263227): Track the actual brightness of the
// content. For now, assume that all HDR content is 1,000 nits.
surface_candidate.hdr_metadata.extended_range->desired_headroom =
gfx::HdrMetadataExtendedRange::kDefaultHdrHeadroom;
}
surface_candidate.is_opaque = !surface_plane.enable_blending;
surface_candidate.opacity = surface_plane.opacity;
surface_candidate.priority_hint = surface_plane.priority_hint;
surface_candidate.rounded_corners = surface_plane.rounded_corners;
surface_candidate.damage_rect =
use_partial_swap_ ? gfx::RectF(swap_buffer_rect_)
: gfx::RectF(surface_plane.resource_size);
#if BUILDFLAG(IS_WIN)
surface_candidate.layer_id = gfx::OverlayLayerId::MakeVizInternalRenderPass(
current_frame()->root_render_pass->id);
#endif
#if BUILDFLAG(IS_OZONE)
// Ozone DRM needs the primary plane as the first overlay when overlay
// testing.
const auto insert_positon = current_frame()->overlay_list.begin();
current_frame()->overlay_list.insert(insert_positon, surface_candidate);
#elif BUILDFLAG(IS_MAC)
// Mac doesn't use the plane_z_order field and it needs to have primary
// plane last in the list of overlays.
current_frame()->overlay_list.push_back(surface_candidate);
#elif BUILDFLAG(IS_ANDROID)
// Android respects plane_z_order and order in the list shouldn't matter,
// but it surfaces the bug when the planes are not hidden properly. As we
// use only underlays, we should keep primary plane first so it would hide
// planes that are not supposed to be visible.
const auto insert_positon = current_frame()->overlay_list.begin();
current_frame()->overlay_list.insert(insert_positon, surface_candidate);
#else
// Other platforms respect plane_z_order so the list order doesn't matter.
current_frame()->overlay_list.push_back(surface_candidate);
#endif
} else {
if (buffer_queue_) {
// If there's no primary plane on these platforms it mean's we're
// delegating to the system compositor, and don't need the buffers
// anymore. On Mac the primary plane buffers are marked as purgeable so
// the OS can decide if they should be destroyed or not.
#if BUILDFLAG(IS_WIN)
buffer_queue_->DestroyBuffers();
#elif BUILDFLAG(IS_APPLE)
buffer_queue_->SetBuffersPurgeable();
#endif
}
}
ScheduleOverlays();
debug_tint_modulate_count_++;
}
#if BUILDFLAG(IS_CHROMEOS) && BUILDFLAG(ENABLE_VULKAN) && \
BUILDFLAG(USE_V4L2_CODEC)
// Simple scheme for de-allocating protected buffers: if we go one SwapBuffer
// cycle without needing a protected shared image, we can delete the protected
// buffer queue.
gpu::Mailbox SkiaRenderer::GetProtectedSharedImage(bool is_10bit) {
is_protected_pool_idle_ = false;
protected_buffer_queue_->Reshape(
gfx::Size(kMaxProtectedContentWidth, kMaxProtectedContentHeight),
gfx::ColorSpace::CreateSRGB(), RenderPassAlphaType::kPremul,
(is_10bit &&
base::FeatureList::IsEnabled(media::kEnableArmHwdrm10bitOverlays))
? SinglePlaneFormat::kBGRA_1010102
: SinglePlaneFormat::kBGRA_8888);
return protected_buffer_queue_->GetCurrentBuffer();
}
void SkiaRenderer::MaybeFreeProtectedPool() {
if (is_protected_pool_idle_ && protected_buffer_queue_) {
protected_buffer_queue_->DestroyBuffers();
skia_output_surface_->CleanupImageProcessor();
} else {
is_protected_pool_idle_ = true;
}
}
#endif
void SkiaRenderer::SwapBuffers(SwapFrameData swap_frame_data) {
DCHECK(visible_);
DCHECK(output_surface_->capabilities().supports_viewporter ||
viewport_size_for_swap_buffers() == surface_size_for_swap_buffers());
TRACE_EVENT0("viz,benchmark", "SkiaRenderer::SwapBuffers");
OutputSurfaceFrame output_frame;
output_frame.latency_info = std::move(swap_frame_data.latency_info);
output_frame.choreographer_vsync_id = swap_frame_data.choreographer_vsync_id;
output_frame.size = viewport_size_for_swap_buffers();
output_frame.data.seq = swap_frame_data.seq;
output_frame.data.swap_trace_id = swap_frame_data.swap_trace_id;
if (use_partial_swap_) {
swap_buffer_rect_.Intersect(gfx::Rect(surface_size_for_swap_buffers()));
output_frame.sub_buffer_rect = swap_buffer_rect_;
}
if (delegated_ink_handler_ && !UsingSkiaForDelegatedInk()) {
output_frame.delegated_ink_metadata =
delegated_ink_handler_->TakeMetadata();
}
output_frame.data.display_hdr_headroom = swap_frame_data.display_hdr_headroom;
#if BUILDFLAG(IS_APPLE)
output_frame.data.ca_layer_error_code = swap_frame_data.ca_layer_error_code;
#endif
#if BUILDFLAG(IS_MAC)
output_frame.data.is_handling_interaction =
swap_frame_data.is_handling_interaction;
output_frame.data.is_handling_animation =
swap_frame_data.is_handling_animation;
#endif
if (buffer_queue_) {
gfx::Rect damage_rect = output_frame.sub_buffer_rect.value_or(
gfx::Rect(surface_size_for_swap_buffers()));
buffer_queue_->SwapBuffers(damage_rect);
}
skia_output_surface_->SwapBuffers(std::move(output_frame));
swap_buffer_rect_ = gfx::Rect();
FlushOutputSurface();
#if BUILDFLAG(IS_APPLE) || BUILDFLAG(IS_OZONE) || BUILDFLAG(IS_WIN)
// Delete render pass overlay backings from the previous frame that will not
// be used again.
for (auto& overlay : available_render_pass_overlay_backings_) {
skia_output_surface_->DestroySharedImage(
overlay.render_pass_backing.mailbox);
}
available_render_pass_overlay_backings_.clear();
#endif // BUILDFLAG(IS_APPLE) || BUILDFLAG(IS_OZONE) || BUILDFLAG(IS_WIN)
#if BUILDFLAG(ENABLE_VULKAN) && BUILDFLAG(IS_CHROMEOS) && \
BUILDFLAG(USE_V4L2_CODEC)
if (protected_buffer_queue_) {
// Note that we still call BufferQueue::SwapBuffers() even when we suspect
// our buffer queue is idle because there might still be in-flight frames
// that need to be managed.
protected_buffer_queue_->SwapBuffers(
gfx::Rect(kMaxProtectedContentWidth, kMaxProtectedContentHeight));
}
MaybeFreeProtectedPool();
#endif
}
void SkiaRenderer::SwapBuffersSkipped() {
gfx::Rect root_pass_damage_rect = gfx::Rect(surface_size_for_swap_buffers());
if (use_partial_swap_)
root_pass_damage_rect.Intersect(swap_buffer_rect_);
if (overlay_processor_) {
overlay_processor_->OnSwapBuffersComplete(gfx::SwapResult::SWAP_SKIPPED);
}
pending_overlay_locks_.pop_back();
skia_output_surface_->SwapBuffersSkipped(root_pass_damage_rect);
if (buffer_queue_) {
buffer_queue_->SwapBuffersSkipped(root_pass_damage_rect);
}
swap_buffer_rect_ = gfx::Rect();
FlushOutputSurface();
}
void SkiaRenderer::SwapBuffersComplete(
const gpu::SwapBuffersCompleteParams& params,
gfx::GpuFenceHandle release_fence) {
auto& read_lock_release_fence_overlay_locks =
read_lock_release_fence_overlay_locks_.emplace_back();
auto read_fence_lock_iter = committed_overlay_locks_.end();
if (overlay_processor_) {
overlay_processor_->OnSwapBuffersComplete(params.swap_response.result);
}
// SWAP_SKIPPED is the only case where presentation is skipped. Even if
// presentation wasn't successful for some other results the GPU thread
// still ran presentation logic.
bool did_present =
params.swap_response.result != gfx::SwapResult::SWAP_SKIPPED;
if (buffer_queue_) {
if (params.swap_response.result ==
gfx::SwapResult::SWAP_NAK_RECREATE_BUFFERS) {
buffer_queue_->RecreateBuffers();
}
buffer_queue_->SwapBuffersComplete(did_present);
}
#if BUILDFLAG(ENABLE_VULKAN) && BUILDFLAG(IS_CHROMEOS) && \
BUILDFLAG(USE_V4L2_CODEC)
if (protected_buffer_queue_) {
protected_buffer_queue_->SwapBuffersComplete(did_present);
}
#endif
if (!did_present) {
CHECK(release_fence.is_null());
// Current pending overlays aren't going to be presented so unlock them
// immediately. `committed_overlay_locks_` remains unchanged.
DisplayResourceProvider::ScopedBatchReturnResources returner(
resource_provider_.get(), /*allow_access_to_gpu_thread=*/true);
pending_overlay_locks_.pop_front();
return;
}
if (!release_fence.is_null()) {
// Set release fences to return overlay resources for last frame.
for (auto& lock : committed_overlay_locks_) {
lock.SetReleaseFence(release_fence.Clone());
}
// Find all locks that have a read-lock fence associated with them and move
// them to the back of locks. If we have a release fence, it's not safe to
// release them here. Release them later in BuffersPresented().
read_fence_lock_iter = std::partition(
committed_overlay_locks_.begin(), committed_overlay_locks_.end(),
[](auto& lock) { return !lock.HasReadLockFence(); });
read_lock_release_fence_overlay_locks.insert(
read_lock_release_fence_overlay_locks.end(),
std::make_move_iterator(read_fence_lock_iter),
std::make_move_iterator(committed_overlay_locks_.end()));
}
#if BUILDFLAG(IS_APPLE)
// On macOS, we don't want to release |committed_overlay_locks_| right away
// because CoreAnimation can hold the overlay images for potentially several
// frames. We depend on the output device to signal the return of overlays via
// |DidReceiveReleasedOverlays| to know when it's safe to release the locks.
for (auto lock_iter = committed_overlay_locks_.begin();
lock_iter != read_fence_lock_iter; ++lock_iter) {
awaiting_release_overlay_locks_.insert(std::move(*lock_iter));
}
#endif // BUILDFLAG(IS_APPLE)
// Current pending locks should have been committed by the next time
// SwapBuffers() is completed.
{
DisplayResourceProvider::ScopedBatchReturnResources returner(
resource_provider_.get(), /*allow_access_to_gpu_thread=*/true);
committed_overlay_locks_.clear();
}
if (delegated_ink_handler_ && UsingSkiaForDelegatedInk()) {
delegated_ink_handler_->GetInkRenderer()->ReportPointsDrawn();
}
std::swap(committed_overlay_locks_, pending_overlay_locks_.front());
pending_overlay_locks_.pop_front();
}
void SkiaRenderer::BuffersPresented() {
read_lock_release_fence_overlay_locks_.pop_front();
}
void SkiaRenderer::DidReceiveReleasedOverlays(
const std::vector<gpu::Mailbox>& released_overlays) {
#if BUILDFLAG(IS_APPLE)
DisplayResourceProvider::ScopedBatchReturnResources returner(
resource_provider_.get(), /*allow_access_to_gpu_thread=*/true);
for (const auto& mailbox : released_overlays) {
auto iter = awaiting_release_overlay_locks_.find(mailbox);
if (iter == awaiting_release_overlay_locks_.end()) {
// The released overlay should always be found as awaiting to be released.
DLOG(FATAL) << "Got an unexpected mailbox";
continue;
}
awaiting_release_overlay_locks_.erase(iter);
}
#else
// Only macOS has the requirement of polling the OS compositor to check if the
// overlay images have been released.
NOTREACHED();
#endif
}
void SkiaRenderer::EnsureScissorTestDisabled() {
scissor_rect_.reset();
}
void SkiaRenderer::SetScissorTestRect(const gfx::Rect& scissor_rect) {
scissor_rect_ = std::optional<gfx::Rect>(scissor_rect);
}
void SkiaRenderer::ClearCanvas(SkColor4f color) {
if (!current_canvas_)
return;
if (scissor_rect_.has_value()) {
// Limit the clear with the scissor rect.
SkAutoCanvasRestore autoRestore(current_canvas_, true /* do_save */);
current_canvas_->clipRect(gfx::RectToSkRect(scissor_rect_.value()));
current_canvas_->clear(color);
} else {
current_canvas_->clear(color);
}
}
void SkiaRenderer::ClearFramebuffer() {
if (current_frame()->current_render_pass->has_transparent_background) {
ClearCanvas(SkColors::kTransparent);
} else {
#if DCHECK_IS_ON() && !BUILDFLAG(IS_LINUX)
// On DEBUG builds, opaque render passes are cleared to blue
// to easily see regions that were not drawn on the screen.
// ClearCavas() call causes slight pixel difference, so linux-ref and
// linux-blink-ref bots cannot share the same baseline for webtest.
// So remove this ClearCanvas() call for dcheck on build for now.
// TODO(crbug.com/40227119): add it back.
ClearCanvas(SkColors::kBlue);
#endif
}
}
bool SkiaRenderer::NeedsLayerForColorConversion(
const AggregatedRenderPass* render_pass) {
if (!render_pass->ShouldDrawWithBlending()) {
// We don't need to be in a color space suitable for blending if we're not
// doing any blending.
return false;
}
const auto it = render_pass_backings_.find(render_pass->id);
if (it == render_pass_backings_.end()) {
// The render pass backing must be owned by the |render_pass_backings_|, the
// output surface. If it is owned by the latter, then we know we're drawing
// the root render pass.
CHECK(!output_surface_->capabilities().renderer_allocates_images);
}
// If the color space of the render pass backing is suitable for blending, we
// don't need to do any color conversion.
const gfx::ColorSpace& pass_color_space =
it != render_pass_backings_.end()
? it->second.color_space
: RenderPassColorSpace(current_frame()->root_render_pass);
if (pass_color_space.IsSuitableForBlending()) {
return false;
}
CHECK(pass_color_space.IsHDR());
return true;
}
gfx::ColorSpace SkiaRenderer::CurrentDrawLayerColorSpace() const {
if (hdr_color_conversion_layer_reset_) {
// A color conversion layer allows us to draw everything in extended sRGB.
return gfx::ColorSpace::CreateExtendedSRGB();
}
// `NeedsLayerForColorConversion` can return false when no quads in a render
// pass require blending. To correctly handle color conversion, the
// destination color space (the result of this function) must match the actual
// render pass backing. We thus cannot unconditionally use the compositing
// color space because it may not be what SCANOUT render pass backings use.
const auto it =
render_pass_backings_.find(current_frame()->current_render_pass->id);
if (it != render_pass_backings_.end()) {
return it->second.color_space;
}
// If there is no render pass backing, we must be drawing the root pass or
// drawing a render pass overlay backing.
return RenderPassColorSpace(current_frame()->root_render_pass);
}
void SkiaRenderer::BeginDrawingRenderPass(
const AggregatedRenderPass* render_pass,
bool needs_clear,
const gfx::Rect& render_pass_update_rect,
const gfx::Size& viewport_size) {
TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("viz.quads"),
"SkiaRenderer::BeginDrawingRenderPass");
// The root render pass will be either bound to the buffer allocated by the
// SkiaOutputSurface, or if the renderer allocates images then the root render
// pass buffer will be bound to the backing allocated in
// AllocateRenderPassResourceIfNeeded().
const bool is_root = render_pass == current_frame()->root_render_pass;
if (is_root && !output_surface_->capabilities().renderer_allocates_images) {
current_canvas_ = skia_output_surface_->BeginPaintCurrentFrame();
} else {
auto iter = render_pass_backings_.find(render_pass->id);
// This function is called after AllocateRenderPassResourceIfNeeded, so
// there should be backing ready.
CHECK(render_pass_backings_.end() != iter);
const RenderPassBacking& backing = iter->second;
current_canvas_ = skia_output_surface_->BeginPaintRenderPass(
render_pass->id, backing.size, backing.format, backing.alpha_type,
backing.generate_mipmap ? skgpu::Mipmapped::kYes
: skgpu::Mipmapped::kNo,
backing.scanout_dcomp_surface, RenderPassBackingSkColorSpace(backing),
/*is_overlay=*/backing.is_scanout, backing.mailbox);
}
if (is_root && debug_settings_->show_overdraw_feedback) {
current_canvas_ = skia_output_surface_->RecordOverdrawForCurrentPaint();
}
if (render_pass_update_rect == gfx::Rect(viewport_size)) {
EnsureScissorTestDisabled();
} else {
SetScissorTestRect(render_pass_update_rect);
}
if (NeedsLayerForColorConversion(render_pass)) {
CHECK(!hdr_color_conversion_layer_reset_.has_value());
hdr_color_conversion_layer_reset_.emplace(current_canvas_.get(),
/*do_save=*/true);
const SkRect layer_bounds = gfx::RectToSkRect(render_pass_update_rect);
if (scissor_rect_.has_value()) {
// Set the clip rect so when we pop the layer, we only touch pixels within
// the update rect.
current_canvas_->clipRect(layer_bounds);
}
SkPaint no_blend;
no_blend.setBlendMode(SkBlendMode::kSrc);
const gfx::ColorSpace blend_color_space =
gfx::ColorSpace::CreateExtendedSRGB();
CHECK(blend_color_space.IsSuitableForBlending());
sk_sp<const SkColorSpace> color_space = blend_color_space.ToSkColorSpace();
current_canvas_->saveLayer(
SkCanvas::SaveLayerRec(&layer_bounds, &no_blend,
/*backdrop=*/nullptr, color_space.get(),
SkCanvas::SaveLayerFlagsSet::kF16ColorType));
}
if (needs_clear) {
ClearFramebuffer();
}
current_render_pass_update_rect_ = render_pass_update_rect;
}
void SkiaRenderer::DoDrawQuad(const DrawQuad* quad,
const gfx::QuadF* draw_region) {
if (!current_canvas_)
return;
TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("viz.quads"),
"SkiaRenderer::DoDrawQuad");
DrawQuadParams params =
CalculateDrawQuadParams(current_frame()->target_to_device_transform,
scissor_rect_, quad, draw_region);
// The outer DrawQuad will never have RPDQ params
DrawQuadInternal(quad, /* rpdq */ nullptr, ¶ms);
}
void SkiaRenderer::DrawQuadInternal(const DrawQuad* quad,
const DrawRPDQParams* rpdq_params,
DrawQuadParams* params) {
if (MustFlushBatchedQuads(quad, rpdq_params, *params)) {
FlushBatchedQuads();
}
if (OverlayCandidate::RequiresOverlay(quad)) {
// We cannot composite this quad properly, replace it with a fallback
// solid color quad.
if (!batched_quads_.empty()) {
FlushBatchedQuads();
}
#if DCHECK_IS_ON()
DrawColoredQuad(SkColors::kMagenta, rpdq_params, params);
#else
DrawColoredQuad(SkColors::kBlack, rpdq_params, params);
#endif
return;
}
switch (quad->material) {
case DrawQuad::Material::kAggregatedRenderPass:
DrawRenderPassQuad(AggregatedRenderPassDrawQuad::MaterialCast(quad),
rpdq_params, params);
break;
case DrawQuad::Material::kDebugBorder:
// DebugBorders draw directly into the device space, so are not compatible
// with image filters, so should never have been promoted as a bypass quad
DCHECK(rpdq_params == nullptr);
DrawDebugBorderQuad(DebugBorderDrawQuad::MaterialCast(quad), params);
break;
case DrawQuad::Material::kPictureContent:
// PictureQuads represent a collection of drawn elements that are
// dynamically rasterized by Skia; bypassing a RenderPass to redraw the
// N elements doesn't make much sense.
DCHECK(rpdq_params == nullptr);
DrawPictureQuad(PictureDrawQuad::MaterialCast(quad), params);
break;
case DrawQuad::Material::kCompositorRenderPass:
// RenderPassDrawQuads should be converted to
// AggregatedRenderPassDrawQuads at this point.
NOTREACHED();
case DrawQuad::Material::kSolidColor:
DrawSolidColorQuad(SolidColorDrawQuad::MaterialCast(quad), rpdq_params,
params);
break;
case DrawQuad::Material::kTextureContent:
DrawTextureQuad(TextureDrawQuad::MaterialCast(quad), rpdq_params, params);
break;
case DrawQuad::Material::kTiledContent:
DrawTileDrawQuad(TileDrawQuad::MaterialCast(quad), rpdq_params, params);
break;
case DrawQuad::Material::kSharedElement:
NOTREACHED();
case DrawQuad::Material::kInvalid:
NOTREACHED();
case DrawQuad::Material::kVideoHole:
// VideoHoleDrawQuad should only be used by Cast, and should
// have been replaced by cast-specific OverlayProcessor before
// reach here. In non-cast build, an untrusted render could send such
// Quad and the quad would then reach here unexpectedly. Therefore
// we should skip NOTREACHED() so an untrusted render is not capable
// of causing a crash.
DrawUnsupportedQuad(quad, rpdq_params, params);
break;
default:
// If we've reached here, it's a new quad type that needs a
// dedicated implementation
NOTREACHED();
}
}
void SkiaRenderer::PrepareCanvas(
const std::optional<gfx::Rect>& scissor_rect,
const std::optional<gfx::MaskFilterInfo>& mask_filter_info,
const gfx::Transform* cdt) {
// Scissor is applied in the device space (CTM == I) and since no changes
// to the canvas persist, CTM should already be the identity
DCHECK(current_canvas_->getTotalMatrix() == SkMatrix::I());
if (scissor_rect.has_value()) {
current_canvas_->clipRect(gfx::RectToSkRect(*scissor_rect));
}
if (mask_filter_info.has_value()) {
current_canvas_->clipRRect(
static_cast<SkRRect>(mask_filter_info->rounded_corner_bounds()),
/*doAntiAlias=*/true);
if (mask_filter_info->HasGradientMask())
PrepareGradient(mask_filter_info);
}
if (cdt) {
SkMatrix m = gfx::TransformToFlattenedSkMatrix(*cdt);
current_canvas_->concat(m);
}
}
#define MaskColor(a) SkColorSetARGB(a, a, a, a);
void SkiaRenderer::PrepareGradient(
const std::optional<gfx::MaskFilterInfo>& mask_filter_info) {
if (!mask_filter_info || !mask_filter_info->HasGradientMask())
return;
const gfx::RectF rect = mask_filter_info->bounds();
const std::optional<gfx::LinearGradient>& gradient_mask =
mask_filter_info->gradient_mask();
int16_t angle = gradient_mask->angle() % 360;
if (angle < 0) angle += 360;
SkPoint start_end[2];
float rad_angle = base::DegToRad(static_cast<float>(angle));
float s = std::sin(rad_angle);
float c = std::cos(rad_angle);
if (angle % 180 > 90) {
float start_x = rect.width() * c * c;
float start_y = rect.height() - (rect.width() * s * c);
float end_x = rect.height() * s * c;
float end_y = rect.height() * c * c;
if (angle < 180) {
start_end[0] = {start_x, start_y};
start_end[1] = {end_x, end_y};
} else {
start_end[0] = {end_x, end_y};
start_end[1] = {start_x, start_y};
}
} else {
float start_x = -rect.height() * s * c;
float start_y = rect.height() * s * s;
float end_x = rect.width() * c * c;
float end_y = -rect.width() * s * c;
if (angle < 180) {
start_end[0] = {start_x, start_y};
start_end[1] = {end_x, end_y};
} else {
start_end[0] = {end_x, end_y};
start_end[1] = {start_x, start_y};
}
}
std::array<SkScalar, gfx::LinearGradient::kMaxStepSize> positions;
std::array<SkColor, gfx::LinearGradient::kMaxStepSize> gradient_colors;
size_t i = 0;
for (; i < gradient_mask->step_count(); ++i) {
positions[i] = gradient_mask->steps()[i].fraction;
gradient_colors[i] = MaskColor(gradient_mask->steps()[i].alpha);
}
SkPoint::Offset(start_end, /*count=*/2, rect.x(), rect.y());
sk_sp<SkShader> gradient = SkGradientShader::MakeLinear(
start_end, gradient_colors.data(), positions.data(), /*count=*/i,
SkTileMode::kClamp);
current_canvas_->clipShader(std::move(gradient));
}
void SkiaRenderer::PrepareCanvasForRPDQ(const DrawRPDQParams& rpdq_params,
DrawQuadParams* params) {
// Clip before the saveLayer() so that Skia only filters the backdrop that is
// necessary for the |backdrop_filter_bounds| (otherwise it will fill the
// quad's SharedQuadState's |clip_rect|).
rpdq_params.SetBackdropFilterClip(current_canvas_, params);
SkPaint layer_paint = params->paint(nullptr /* color_filter */);
// The layer always consumes the opacity, but its blend mode depends on if
// it was initialized with backdrop content or not.
params->opacity = 1.f;
if (rpdq_params.backdrop_filter) {
layer_paint.setBlendMode(SkBlendMode::kSrcOver);
} else {
params->blend_mode = SkBlendMode::kSrcOver;
}
if (rpdq_params.color_filter) {
layer_paint.setColorFilter(rpdq_params.color_filter);
} else if (rpdq_params.image_filter) {
layer_paint.setImageFilter(rpdq_params.image_filter);
}
// Here |bounds| represents the extent of content to be drawn into the saved
// layer, so it's either |params->visible_rect| or the bypass geometry's
// |clip_rect|
SkRect bounds =
gfx::RectFToSkRect(rpdq_params.bypass_geometry.has_value()
? rpdq_params.bypass_geometry->clip_rect
: params->visible_rect);
current_canvas_->saveLayer(SkCanvasPriv::ScaledBackdropLayer(
&bounds, &layer_paint, rpdq_params.backdrop_filter.get(),
rpdq_params.backdrop_filter_quality, 0));
// If we have backdrop filtered content (and not transparent black like with
// regular render passes), we have to clear out the parts of the layer that
// shouldn't show the backdrop.
rpdq_params.ClearOutsideBackdropBounds(current_canvas_, params);
}
void SkiaRenderer::PreparePaintOrCanvasForRPDQ(
const DrawRPDQParams& rpdq_params,
DrawQuadParams* params,
SkPaint* paint) {
// When the draw call accepts an SkPaint, some of the rpdq effects are more
// efficient to store on the paint instead of making an explicit layer in
// the canvas. But there are several requirements in order for the order of
// operations to be consistent with what RenderPasses require:
// 1. Backdrop filtering always requires a layer.
// 2. The content bypassing the renderpass needs to be clipped before the
// image filter is evaluated.
bool needs_bypass_clip = rpdq_params.needs_bypass_clip(params->visible_rect);
bool needs_save_layer = false;
if (rpdq_params.backdrop_filter)
needs_save_layer = true;
else if (rpdq_params.has_complex_image_filter())
needs_save_layer = needs_bypass_clip;
if (rpdq_params.mask_shader) {
// Apply the mask image using clipShader(), this works the same regardless
// of if we need a saveLayer for image filtering since the clip is applied
// at the end automatically.
current_canvas_->clipShader(
rpdq_params.mask_shader->GetOrCreateSkShader(this));
}
if (needs_save_layer) {
PrepareCanvasForRPDQ(rpdq_params, params);
// Sync the content's paint with the updated |params|
paint->setAlphaf(params->opacity);
paint->setBlendMode(params->blend_mode);
} else {
// At this point, the image filter and/or color filter are on the paint.
DCHECK(!rpdq_params.backdrop_filter);
if (rpdq_params.color_filter) {
// Use the color filter directly, instead of the image filter.
if (paint->getColorFilter()) {
paint->setColorFilter(
rpdq_params.color_filter->makeComposed(paint->refColorFilter()));
} else {
paint->setColorFilter(rpdq_params.color_filter);
}
DCHECK(paint->getColorFilter());
} else if (rpdq_params.image_filter) {
// Store the image filter on the paint.
if (params->opacity != 1.f) {
// Apply opacity as the last step of image filter so it is uniform
// across any overlapping content produced by the image filters.
paint->setImageFilter(SkImageFilters::ColorFilter(
MakeOpacityFilter(params->opacity, nullptr),
rpdq_params.image_filter));
paint->setAlphaf(1.f);
params->opacity = 1.f;
} else {
paint->setImageFilter(rpdq_params.image_filter);
}
}
}
// Whether or not we saved a layer, clip the bypassed RenderPass's content
if (needs_bypass_clip) {
current_canvas_->clipRect(
gfx::RectFToSkRect(rpdq_params.bypass_geometry->clip_rect),
params->aa_flags != SkCanvas::kNone_QuadAAFlags);
}
}
void SkiaRenderer::PrepareColorOrCanvasForRPDQ(
const DrawRPDQParams& rpdq_params,
DrawQuadParams* params,
SkColor4f* content_color) {
// When the draw call only takes a color and not an SkPaint, rpdq params
// with just a color filter can be handled directly. Otherwise, the rpdq
// params must use a layer on the canvas.
bool needs_save_layer =
rpdq_params.has_complex_image_filter() || rpdq_params.backdrop_filter;
if (rpdq_params.mask_shader) {
current_canvas_->clipShader(
rpdq_params.mask_shader->GetOrCreateSkShader(this));
}
if (needs_save_layer) {
PrepareCanvasForRPDQ(rpdq_params, params);
} else if (rpdq_params.color_filter) {
// At this point, the RPDQ effect is at most a color filter, so it can
// modify |content_color| directly.
SkColorSpace* cs = nullptr;
*content_color =
rpdq_params.color_filter->filterColor4f(*content_color, cs, cs);
}
// Even if the color filter image filter was applied to the content color
// directly (so no explicit save layer), the draw may need to be clipped to
// the output rect of the renderpass it is bypassing.
if (rpdq_params.needs_bypass_clip(params->visible_rect)) {
current_canvas_->clipRect(
gfx::RectFToSkRect(rpdq_params.bypass_geometry->clip_rect),
params->aa_flags != SkCanvas::kNone_QuadAAFlags);
}
}
SkiaRenderer::DrawQuadParams SkiaRenderer::CalculateDrawQuadParams(
const gfx::AxisTransform2d& target_to_device,
const std::optional<gfx::Rect>& scissor_rect,
const DrawQuad* quad,
const gfx::QuadF* draw_region) const {
DrawQuadParams params(
quad->shared_quad_state->quad_to_target_transform, gfx::RectF(quad->rect),
gfx::RectF(quad->visible_rect), SkCanvas::kNone_QuadAAFlags,
quad->shared_quad_state->blend_mode, quad->shared_quad_state->opacity,
GetSampling(quad), draw_region);
params.content_device_transform.PostConcat(target_to_device);
params.content_device_transform.Flatten();
// Respect per-quad setting overrides as highest priority setting
if (!IsAAForcedOff(quad)) {
if (settings_->force_antialiasing) {
// This setting makes the entire draw AA, so don't bother checking edges
params.aa_flags = SkCanvas::kAll_QuadAAFlags;
} else if (settings_->allow_antialiasing) {
params.aa_flags = GetRectilinearEdgeFlags(quad);
if (draw_region && params.aa_flags != SkCanvas::kNone_QuadAAFlags) {
// Turn off interior edges' AA from the BSP splitting
GetClippedEdgeFlags(quad, ¶ms.aa_flags, &*params.draw_region);
}
}
}
if (!quad->ShouldDrawWithBlending()) {
// The quad layer is src-over with 1.0 opacity and its needs_blending flag
// has been set to false. However, even if the layer's opacity is 1.0, the
// contents may not be (e.g. png or a color with alpha).
if (quad->shared_quad_state->are_contents_opaque) {
// Visually, this is the same as kSrc but Skia is faster with SrcOver
params.blend_mode = SkBlendMode::kSrcOver;
} else {
// Replaces dst contents with the new color (e.g. no blending); this is
// just as fast as srcover when there's no AA, but is slow when coverage
// must be taken into account.
params.blend_mode = SkBlendMode::kSrc;
}
params.opacity = 1.f;
}
params.ApplyScissor(this, quad, scissor_rect);
// Determine final rounded rect clip geometry. We transform it from target
// space to window space to make batching and canvas preparation easier
// (otherwise we'd have to separate those two matrices in the CDT).
if (ShouldApplyRoundedCorner(quad) || ShouldApplyGradientMask(quad)) {
params.mask_filter_info.emplace(quad->shared_quad_state->mask_filter_info);
// Transform by the window and projection matrix to go from target to
// device space, which should always be a scale+translate.
params.mask_filter_info->ApplyTransform(target_to_device);
}
return params;
}
void SkiaRenderer::DrawQuadParams::ApplyScissor(
const SkiaRenderer* renderer,
const DrawQuad* quad,
const std::optional<gfx::Rect>& scissor_to_apply) {
// No scissor should have been set before calling ApplyScissor
DCHECK(!scissor_rect.has_value());
if (!scissor_to_apply) {
// No scissor at all, which matches the DCHECK'ed state above
return;
}
// Assume at start that the scissor will be applied through the canvas clip,
// so that this can simply return when it detects the scissor cannot be
// applied explicitly to |visible_rect|.
scissor_rect = *scissor_to_apply;
// PICTURE_CONTENT is not like the others, since it is executing a list of
// draw calls into the canvas.
if (quad->material == DrawQuad::Material::kPictureContent)
return;
// DebugBorderDrawQuads draw a path so they must be explicitly clipped.
if (quad->material == DrawQuad::Material::kDebugBorder)
return;
// Intersection with scissor and a quadrilateral is not necessarily a quad,
// so don't complicate things
if (draw_region.has_value())
return;
if (!Is2dScaleTranslateTransform(content_device_transform)) {
return;
}
// State check: should not have a CompositorRenderPassDrawQuad if we got here.
DCHECK_NE(quad->material, DrawQuad::Material::kCompositorRenderPass);
if (const auto* quad_pass =
quad->DynamicCast<AggregatedRenderPassDrawQuad>()) {
// If the renderpass has filters, the filters may modify the effective
// geometry beyond the quad's visible_rect, so it's not safe to pre-clip.
// Note: no need to check against the backdrop filters, as they are always
// restricted to the visible rect of a quad.
auto pass_id = quad_pass->render_pass_id;
if (const auto* filters = renderer->FiltersForPass(pass_id);
filters && filters->HasFilterThatMovesPixels()) {
return;
}
}
// If the intersection of the scissor and the quad's visible_rect results in
// subpixel device-space geometry, do not drop the scissor. Otherwise Skia
// sees an unclipped anti-aliased hairline and uses different AA methods that
// would cause the rasterized result to extend beyond the scissor.
gfx::RectF device_bounds = content_device_transform.MapRect(visible_rect);
device_bounds.Intersect(gfx::RectF(*scissor_rect));
if (device_bounds.width() < 1.0f || device_bounds.height() < 1.0f) {
return;
}
// The explicit scissor is applied in the quad's local space. If the transform
// does not leave sufficient precision to round-trip the scissor rect to-from
// device->local->device space, the explicitly "clipped" geometry does not
// necessarily respect the original scissor.
std::optional<gfx::RectF> local_scissor =
content_device_transform.InverseMapRect(gfx::RectF(*scissor_rect));
if (!local_scissor) {
return;
}
gfx::RectF remapped_scissor =
content_device_transform.MapRect(*local_scissor);
if (gfx::ToRoundedRect(remapped_scissor) != *scissor_rect) {
return;
}
// At this point, we've determined that we can transform the scissor rect into
// the quad's local space and adjust |vis_rect|, such that when it's mapped to
// device space, it will be contained in in the original scissor.
// Applying the scissor explicitly means avoiding a clipRect() call and
// allows more quads to be batched together in a DrawEdgeAAImageSet call
float x_epsilon = kAAEpsilon / content_device_transform.rc(0, 0);
float y_epsilon = kAAEpsilon / content_device_transform.rc(1, 1);
// The scissor is a non-AA clip, so unset the bit flag for clipped edges.
if (local_scissor->x() - visible_rect.x() >= x_epsilon)
aa_flags &= ~SkCanvas::kLeft_QuadAAFlag;
if (local_scissor->y() - visible_rect.y() >= y_epsilon)
aa_flags &= ~SkCanvas::kTop_QuadAAFlag;
if (visible_rect.right() - local_scissor->right() >= x_epsilon)
aa_flags &= ~SkCanvas::kRight_QuadAAFlag;
if (visible_rect.bottom() - local_scissor->bottom() >= y_epsilon)
aa_flags &= ~SkCanvas::kBottom_QuadAAFlag;
visible_rect.Intersect(*local_scissor);
vis_tex_coords = visible_rect;
scissor_rect.reset();
}
std::optional<const DrawQuad*> SkiaRenderer::CanPassBeDrawnDirectly(
const AggregatedRenderPass* pass,
const RenderPassRequirements& requirements) {
// If render pass bypassing is disabled for testing
if (settings_->disable_render_pass_bypassing)
return std::nullopt;
// A pass that is functionally empty is bypass-able with a transparent quad.
if (pass->quad_list.empty() || pass->output_rect.IsEmpty()) {
return std::optional(nullptr);
}
// Only supports bypassing render passes with a single child quad and simple
// content.
if (pass->quad_list.size() != 1) {
return std::nullopt;
}
// If it there are supposed to be mipmaps, the renderpass must exist
if (pass->generate_mipmap)
return std::nullopt;
// Force passes whose backings can be directly scanned out from being a
// bypass quad. This logic should mirror
// |GetRenderPassBackingForDirectScanout|.
#if BUILDFLAG(IS_WIN)
if (requirements.is_scanout) {
return std::nullopt;
}
#else
// This platform doesn't support direct scanout, so we don't expect any
// scanout render pass backings.
CHECK(!requirements.is_scanout);
#endif
const DrawQuad* quad = *pass->quad_list.BackToFrontBegin();
// For simplicity in debug border and picture quad draw implementations, don't
// bypass a render pass containing those. Their draw functions do not take a
// DrawRPDQParams.
if (quad->material == DrawQuad::Material::kDebugBorder ||
quad->material == DrawQuad::Material::kPictureContent)
return std::nullopt;
// TODO(penghuang): support composite TileDrawQuad in a sub render pass for
// raw draw directly.
if (is_using_raw_draw_ && quad->material == DrawQuad::Material::kTiledContent)
return std::nullopt;
// If the quad specifies nearest-neighbor scaling then there could be two
// scaling operations at different quality levels. This requires drawing to an
// intermediate render pass. See https://crbug.com/1155338.
if (UseNearestNeighborSampling(quad))
return std::nullopt;
// In order to concatenate the bypass'ed quads transform with RP itself, it
// needs to be invertible.
// TODO(michaelludwig) - See crbug.com/1175981 and crbug.com/1186657;
// We can't use gfx::Transform.IsInvertible() since that checks the 4x4 matrix
// and the rest of skia_renderer->Skia flattens to a 3x3 matrix, which can
// change invertibility.
SkMatrix flattened = gfx::TransformToFlattenedSkMatrix(
quad->shared_quad_state->quad_to_target_transform);
if (!flattened.invert(nullptr))
return std::nullopt;
// A renderpass normally draws its content into a transparent destination,
// using the quad's blend mode, then that result is later drawn into the
// real dst with the RP's blend mode. In order to bypass the RP and draw
// correctly, CalculateBypassParams must be able to reason about the quad's
// blend mode.
if (!IsPorterDuffBlendMode(quad->shared_quad_state->blend_mode))
return std::nullopt;
// All Porter-Duff blending with transparent black should fall into one of
// these two categories:
DCHECK(RenderPassPreservesContent(quad->shared_quad_state->blend_mode) ||
RenderPassRemainsTransparent(quad->shared_quad_state->blend_mode));
// The content must not have any rrect clipping, since skia_renderer applies
// the rrect in device space, and in this case, the bypass quad's device space
// is the RP's buffer.
// TODO(michaelludwig) - If this becomes a bottleneck, we can track the
// bypass rrect separately and update PrepareCanvasForRDQP to apply the
// additional clip.
if (ShouldApplyRoundedCorner(quad))
return std::nullopt;
if (ShouldApplyGradientMask(quad))
return std::nullopt;
if (const auto* render_pass_quad =
quad->DynamicCast<AggregatedRenderPassDrawQuad>()) {
if (render_pass_quad->mask_resource_id()) {
return std::nullopt;
}
// Only allow merging render passes containing RenderPassDrawQuads if they
// have 2D scale/translate transform. This allows merging clip rects into
// a single intermediate coordinate space.
if (!Is2dScaleTranslateTransform(
render_pass_quad->shared_quad_state->quad_to_target_transform)) {
return std::nullopt;
}
const auto nested_render_pass_id = render_pass_quad->render_pass_id;
auto it =
std::ranges::find_if(*current_frame()->render_passes_in_draw_order,
[&nested_render_pass_id](const auto& render_pass) {
return render_pass->id == nested_render_pass_id;
});
CHECK(it != current_frame()->render_passes_in_draw_order->end());
const auto& nested_render_pass = *it;
if (!nested_render_pass->filters.IsEmpty() ||
!nested_render_pass->backdrop_filters.IsEmpty()) {
return std::nullopt;
}
}
// The quad type knows how to apply RPDQ filters, and the quad settings can
// be merged into the RPDQs settings in CalculateBypassParams.
return quad;
}
SkiaRenderer::BypassMode SkiaRenderer::CalculateBypassParams(
const DrawQuad* bypass_quad,
DrawRPDQParams* rpdq_params,
DrawQuadParams* params) const {
// `bypass_quad` is nullptr if the render pass being bypassed is functionally
// empty. We want to draw a transparent quad in this case to support backdrop
// filters that can expand the bounding box.
if (!bypass_quad) {
return BypassMode::kDrawTransparentQuad;
}
// Depending on bypass_quad's blend mode, its content may be irrelevant
if (RenderPassRemainsTransparent(
bypass_quad->shared_quad_state->blend_mode)) {
// NOTE: this uses the pass's blend mode since this refers to the final draw
// of the render pass itself
if (TransparentBlackAffectsOutput(params->blend_mode)) {
return BypassMode::kDrawTransparentQuad;
} else {
return BypassMode::kSkip;
}
}
// If we made it here, the bypass blend mode would have just preserved the
// bypass quad's content, so we can draw it directly using the render pass's
// blend mode instead.
DCHECK(
RenderPassPreservesContent(bypass_quad->shared_quad_state->blend_mode));
// The bypass quad will be drawn directly, so update |params| and
// |rpdq_params| to reflect the change of coordinate system and merge settings
// between the inner and outer quads.
SkMatrix bypass_to_rpdq = gfx::TransformToFlattenedSkMatrix(
bypass_quad->shared_quad_state->quad_to_target_transform);
if (params->draw_region) {
SkMatrix rpdq_to_bypass;
bool inverted = bypass_to_rpdq.invert(&rpdq_to_bypass);
// Invertibility was a requirement for being bypassable.
DCHECK(inverted);
// The draw region was determined by the RPDQ's geometry, so map the
// quadrilateral to the bypass'ed quad's coordinate space so that BSP
// splitting is still respected.
rpdq_to_bypass.mapPoints(params->draw_region->points);
}
std::optional<gfx::RectF> bypassed_quad_clip_rect;
if (rpdq_params->bypass_geometry) {
// If BypassGeometry is already populated then this is part of a chain of
// bypassed render passes. This merges any additional transform and clip
// into the first bypassed render pass coordinate space. This is always
// possible as RenderPassDrawQuads are only bypassed if they have 2D
// scale/translation transform.
auto& bypass_geometry = rpdq_params->bypass_geometry.value();
gfx::Transform bypass_transform =
gfx::SkMatrixToTransform(bypass_geometry.transform);
DCHECK(Is2dScaleTranslateTransform(bypass_transform));
// `bypass_geometry.clip_rect` is in the first bypassed render pass
// coordinate space. The last CalculateBypassParams() updated
// `params->visible_rect` so it is in the current bypassed render pass
// coordinate space. Transform that into the first bypassed render pass
// coordinate space and intersect with existing clip there. That way there
// is a single intermediate clip entirely in the original bypassed render
// pass coordinate space.
gfx::RectF bypass_visible_rect =
bypass_transform.MapRect(params->visible_rect);
bypass_geometry.clip_rect.Intersect(bypass_visible_rect);
if (bypass_quad->shared_quad_state->clip_rect) {
// The bypass_quad clip_rect needs to be transformed from current bypassed
// render pass coordinate space into the first bypassed render pass
// coordinate space.
bypassed_quad_clip_rect = bypass_transform.MapRect(
gfx::RectF(*bypass_quad->shared_quad_state->clip_rect));
}
// Update transform so it maps from `bypass_quad` to the first bypassed
// render pass coordinate space.
bypass_geometry.transform.preConcat(bypass_to_rpdq);
} else {
// BypassGeometry holds the RenderPassDrawQuad visible_rect, which is in the
// bypassed render pass coordinate space, along with the transform from
// `bypass_quad` to bypassed render pass coordinate space.
rpdq_params->bypass_geometry =
DrawRPDQParams::BypassGeometry{bypass_to_rpdq, params->visible_rect};
if (bypass_quad->shared_quad_state->clip_rect) {
// The bypass_quad clip_rect is in the same coordinate space as the RPDQ +
// bypassed render pass aka the same as bypass_geometry.
bypassed_quad_clip_rect =
gfx::RectF(*bypass_quad->shared_quad_state->clip_rect);
}
}
if (bypassed_quad_clip_rect) {
// If bypassed_quad clip_rect isn't empty then normally it would be added
// to scissor_rect in SetScissorStateForQuad(). That never happens when the
// render pass is bypassed so add it here.
rpdq_params->bypass_geometry->clip_rect.Intersect(*bypassed_quad_clip_rect);
}
// Compute draw params for `bypass_quad` to update some of the original draw
// params. Both transform and scissor_rect are already accounted for in
// BypassGeometry so pass identify and empty for those.
DrawQuadParams bypass_quad_params = CalculateDrawQuadParams(
/*target_to_device=*/gfx::AxisTransform2d(),
/*scissor_rect=*/std::nullopt, bypass_quad,
/*draw_region=*/nullptr);
// NOTE: params |content_device_transform| remains that of the RPDQ to prepare
// the canvas' CTM to match what any image filters require. The above
// BypassGeometry::transform is then applied when drawing so that these
// updated coordinates are correctly transformed to device space.
params->visible_rect = bypass_quad_params.visible_rect;
params->vis_tex_coords = bypass_quad_params.vis_tex_coords;
// Combine anti-aliasing policy (use AND so that any draw_region clipping
// is preserved).
params->aa_flags &= bypass_quad_params.aa_flags;
// Blending will use the top-level RPDQ blend mode, but factor in the
// content's opacity as well, since that would have normally been baked into
// the RP's buffer.
params->opacity *= bypass_quad_params.opacity;
// Take the highest quality filter, since this single draw will reflect the
// filtering decisions made both when drawing into the RP and when drawing the
// RP results itself. The ord() lambda simulates this notion of "highest" when
// we used to use FilterQuality.
auto ord = [](const SkSamplingOptions& sampling) {
if (sampling.useCubic) {
return 3;
} else if (sampling.mipmap != SkMipmapMode::kNone) {
return 2;
}
return sampling.filter == SkFilterMode::kLinear ? 1 : 0;
};
if (ord(bypass_quad_params.sampling) > ord(params->sampling)) {
params->sampling = bypass_quad_params.sampling;
}
// Rounded corner bounds are in device space, which gets tricky when bypassing
// the device that the RP would have represented
DCHECK(!bypass_quad_params.mask_filter_info.has_value());
return BypassMode::kDrawBypassQuad;
}
SkCanvas::ImageSetEntry SkiaRenderer::MakeEntry(
const SkImage* image,
int matrix_index,
const DrawQuadParams& params) const {
return SkCanvas::ImageSetEntry(
{sk_ref_sp(image), gfx::RectFToSkRect(params.vis_tex_coords),
gfx::RectFToSkRect(params.visible_rect), matrix_index, params.opacity,
params.aa_flags, params.draw_region.has_value()});
}
SkCanvas::SrcRectConstraint SkiaRenderer::ResolveTextureConstraints(
const SkImage* image,
const gfx::RectF& valid_texel_bounds,
DrawQuadParams* params) const {
if (params->aa_flags == SkCanvas::kNone_QuadAAFlags &&
params->sampling == SkSamplingOptions()) {
// Non-AA and no bilinear filtering so rendering won't filter outside the
// provided texture coordinates.
return SkCanvas::kFast_SrcRectConstraint;
}
// Resolve texture coordinates against the valid content area of the image
SkCanvas::SrcRectConstraint constraint =
GetTextureConstraint(image, params->vis_tex_coords, valid_texel_bounds);
// Skia clamps to the provided texture coordinates, not the content_area. If
// there is a mismatch, have to update the draw params to account for the new
// constraint
if (constraint == SkCanvas::kFast_SrcRectConstraint ||
valid_texel_bounds == params->vis_tex_coords) {
return constraint;
}
// To get |valid_texel_bounds| as the constraint, it must be sent as the tex
// coords. To draw the right shape, store |visible_rect| as the |draw_region|
// and change the visible rect so that the mapping from |visible_rect| to
// |valid_texel_bounds| causes |draw_region| to map to original
// |vis_tex_coords|
if (!params->draw_region) {
params->draw_region.emplace(gfx::QuadF(params->visible_rect));
}
// Preserve the src-to-dst transformation for the padded texture coords
SkMatrix src_to_dst =
SkMatrix::RectToRect(gfx::RectFToSkRect(params->vis_tex_coords),
gfx::RectFToSkRect(params->visible_rect));
params->visible_rect = gfx::SkRectToRectF(
src_to_dst.mapRect(gfx::RectFToSkRect(valid_texel_bounds)));
params->vis_tex_coords = valid_texel_bounds;
return SkCanvas::kStrict_SrcRectConstraint;
}
bool SkiaRenderer::MustFlushBatchedQuads(const DrawQuad* new_quad,
const DrawRPDQParams* rpdq_params,
const DrawQuadParams& params) const {
if (batched_quads_.empty())
return false;
// If |new_quad| is the bypass quad for a renderpass with filters, it must be
// drawn by itself, regardless of if it could otherwise would've been batched.
if (rpdq_params)
return true;
DCHECK_NE(new_quad->material, DrawQuad::Material::kCompositorRenderPass);
if (new_quad->material != DrawQuad::Material::kAggregatedRenderPass &&
new_quad->material != DrawQuad::Material::kTextureContent &&
new_quad->material != DrawQuad::Material::kTiledContent)
return true;
if (batched_quad_state_.blend_mode != params.blend_mode ||
batched_quad_state_.sampling != params.sampling)
return true;
if (batched_quad_state_.scissor_rect != params.scissor_rect) {
return true;
}
if (batched_quad_state_.mask_filter_info != params.mask_filter_info) {
return true;
}
return false;
}
void SkiaRenderer::AddQuadToBatch(const SkImage* image,
const gfx::RectF& valid_texel_bounds,
DrawQuadParams* params) {
SkCanvas::SrcRectConstraint constraint =
ResolveTextureConstraints(image, valid_texel_bounds, params);
// Last check for flushing the batch, since constraint can't be known until
// the last minute.
if (!batched_quads_.empty() && batched_quad_state_.constraint != constraint) {
FlushBatchedQuads();
}
// Configure batch state if it's the first
if (batched_quads_.empty()) {
batched_quad_state_.scissor_rect = params->scissor_rect;
batched_quad_state_.mask_filter_info = params->mask_filter_info;
batched_quad_state_.blend_mode = params->blend_mode;
batched_quad_state_.sampling = params->sampling;
batched_quad_state_.constraint = constraint;
}
DCHECK(batched_quad_state_.constraint == constraint);
// Add entry, with optional clip quad and shared transform
if (params->draw_region) {
for (const auto& point : params->draw_region->points) {
batched_draw_regions_.push_back(point);
}
}
SkMatrix m =
gfx::TransformToFlattenedSkMatrix(params->content_device_transform);
if (batched_cdt_matrices_.empty() || batched_cdt_matrices_.back() != m) {
batched_cdt_matrices_.push_back(m);
}
int matrix_index = batched_cdt_matrices_.size() - 1;
batched_quads_.push_back(MakeEntry(image, matrix_index, *params));
}
void SkiaRenderer::FlushBatchedQuads() {
TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("viz.quads"),
"SkiaRenderer::FlushBatchedQuads");
SkAutoCanvasRestore acr(current_canvas_, true /* do_save */);
PrepareCanvas(batched_quad_state_.scissor_rect,
batched_quad_state_.mask_filter_info, nullptr);
SkPaint paint;
sk_sp<SkColorFilter> color_filter = GetContentColorFilter();
if (color_filter)
paint.setColorFilter(color_filter);
paint.setBlendMode(batched_quad_state_.blend_mode);
current_canvas_->experimental_DrawEdgeAAImageSet(
&batched_quads_.front(), batched_quads_.size(),
batched_draw_regions_.data(), &batched_cdt_matrices_.front(),
batched_quad_state_.sampling, &paint, batched_quad_state_.constraint);
batched_quads_.clear();
batched_draw_regions_.clear();
batched_cdt_matrices_.clear();
}
void SkiaRenderer::DrawColoredQuad(SkColor4f color,
const DrawRPDQParams* rpdq_params,
DrawQuadParams* params) {
TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("viz.quads"),
"SkiaRenderer::DrawColoredQuad");
DCHECK(batched_quads_.empty());
SkAutoCanvasRestore acr(current_canvas_, true /* do_save */);
PrepareCanvas(params->scissor_rect, params->mask_filter_info,
¶ms->content_device_transform);
if (rpdq_params) {
// This will modify the provided content color as needed for the RP effects,
// or it will make an explicit save layer on the current canvas
PrepareColorOrCanvasForRPDQ(*rpdq_params, params, &color);
if (rpdq_params->bypass_geometry) {
// Concatenate the bypass'ed quad's transform after all the RPDQ state
// has been pushed to the canvas.
current_canvas_->concat(rpdq_params->bypass_geometry->transform);
}
}
sk_sp<SkColorFilter> content_color_filter = GetContentColorFilter();
if (content_color_filter) {
SkColorSpace* color_space = current_canvas_->imageInfo().colorSpace();
color = content_color_filter->filterColor4f(
color, SkColorSpace::MakeSRGB().get(), color_space);
// DrawEdgeAAQuad lacks color filter support via SkPaint, so we apply the
// color filter to the quad color directly. When applying a color filter
// via drawRect or drawImage, Skia will first transform the src into the
// dst color space before applying the color filter. Thus, here we need to
// apply the color filter in the dst color space and then convert back to
// the src color space. More formally:
// (C * Xfrm * CF * Xfrm_inv)[in Viz] * Xfrm[in Skia] = C * Xfrm * CF
if (color_space && !color_space->isSRGB()) {
SkPaint paint;
paint.setColor(color, color_space);
color = paint.getColor4f();
}
}
// PrepareCanvasForRPDQ will have updated params->opacity and blend_mode to
// account for the layer applying those effects. We need to truncate to an
// integral value of [0, 255] to match the explicit floor workaround in
// blink::ConversionContext::StartEffect.
color.fA = floor(params->opacity * color.fA * 255.f) / 255.f;
const SkPoint* draw_region =
params->draw_region ? params->draw_region->points.data() : nullptr;
current_canvas_->experimental_DrawEdgeAAQuad(
gfx::RectFToSkRect(params->visible_rect), draw_region,
static_cast<SkCanvas::QuadAAFlags>(params->aa_flags), color,
params->blend_mode);
}
void SkiaRenderer::DrawSingleImage(const SkImage* image,
const gfx::RectF& valid_texel_bounds,
const DrawRPDQParams* rpdq_params,
SkPaint* paint,
DrawQuadParams* params) {
DCHECK(batched_quads_.empty());
TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("viz.quads"),
"SkiaRenderer::DrawSingleImage");
SkAutoCanvasRestore acr(current_canvas_, true /* do_save */);
PrepareCanvas(params->scissor_rect, params->mask_filter_info,
¶ms->content_device_transform);
int matrix_index = -1;
const SkMatrix* bypass_transform = nullptr;
if (rpdq_params) {
// This will modify the provided content paint as needed for the RP effects,
// or it will make an explicit save layer on the current canvas
PreparePaintOrCanvasForRPDQ(*rpdq_params, params, paint);
if (rpdq_params->bypass_geometry) {
// Incorporate the bypass transform, but unlike for solid color quads, do
// not modify the SkCanvas's CTM. This is because the RPDQ's filters may
// have been optimally placed on the SkPaint of the draw, which means the
// canvas' transform must match that of the RenderPass. The pre-CTM matrix
// of the image set entry can be used instead to modify the drawn geometry
// without impacting the filter's coordinate space.
bypass_transform = &rpdq_params->bypass_geometry->transform;
matrix_index = 0;
}
}
// At this point, the params' opacity should be handled by |paint| (either
// as its alpha or in a color filter), or in an image filter from the RPDQ,
// or in the saved layer for the RPDQ. Set opacity to 1 to ensure the image
// set entry does not doubly-apply the opacity then.
params->opacity = 1.f;
SkCanvas::SrcRectConstraint constraint =
ResolveTextureConstraints(image, valid_texel_bounds, params);
// Use -1 for matrix index since the cdt is set on the canvas.
SkCanvas::ImageSetEntry entry = MakeEntry(image, matrix_index, *params);
const SkPoint* draw_region =
params->draw_region ? params->draw_region->points.data() : nullptr;
current_canvas_->experimental_DrawEdgeAAImageSet(
&entry, 1, draw_region, bypass_transform, params->sampling, paint,
constraint);
}
void SkiaRenderer::DrawPaintOpBuffer(
const cc::PaintOpBuffer* buffer,
const std::optional<SkColor4f>& clear_color,
const TileDrawQuad* quad,
const DrawQuadParams* params) {
TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("viz.quads"),
"SkiaRenderer::DrawPaintOpBuffer");
if (!batched_quads_.empty())
FlushBatchedQuads();
SkAutoCanvasRestore auto_canvas_restore(current_canvas_, true /* do_save */);
PrepareCanvas(params->scissor_rect, params->mask_filter_info,
¶ms->content_device_transform);
auto visible_rect = gfx::RectFToSkRect(params->visible_rect);
current_canvas_->clipRect(visible_rect);
if (params->draw_region) {
bool aa = params->aa_flags != SkCanvas::kNone_QuadAAFlags;
current_canvas_->clipPath(params->draw_region_in_path(), aa);
}
if (quad->ShouldDrawWithBlending()) {
auto paint = params->paint(nullptr);
// TODO(penghuang): saveLayer() is expensive, try to avoid it as much as
// possible.
current_canvas_->saveLayer(&visible_rect, &paint);
}
if (clear_color)
current_canvas_->drawColor(*clear_color);
float scale_x = params->rect.width() / quad->tex_coord_rect.width();
float scale_y = params->rect.height() / quad->tex_coord_rect.height();
float offset_x =
params->visible_rect.x() - params->vis_tex_coords.x() * scale_x;
float offset_y =
params->visible_rect.y() - params->vis_tex_coords.y() * scale_y;
current_canvas_->translate(offset_x, offset_y);
current_canvas_->scale(scale_x, scale_y);
cc::PlaybackParams playback_params(nullptr, SkM44());
buffer->Playback(current_canvas_, playback_params);
}
void SkiaRenderer::DrawDebugBorderQuad(const DebugBorderDrawQuad* quad,
DrawQuadParams* params) {
TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("viz.quads"),
"SkiaRenderer::DrawDebugBorderQuad");
DCHECK(batched_quads_.empty());
SkAutoCanvasRestore acr(current_canvas_, true /* do_save */);
// We need to apply the matrix manually to have pixel-sized stroke width.
PrepareCanvas(params->scissor_rect, params->mask_filter_info, nullptr);
SkMatrix cdt =
gfx::TransformToFlattenedSkMatrix(params->content_device_transform);
SkPath path = params->draw_region
? params->draw_region_in_path()
: SkPath::Rect(gfx::RectFToSkRect(params->visible_rect));
path.transform(cdt);
SkPaint paint = params->paint(nullptr /* color_filter */);
paint.setColor(quad->color); // Must correct alpha afterwards
paint.setAlphaf(params->opacity * paint.getAlphaf());
paint.setStyle(SkPaint::kStroke_Style);
paint.setStrokeJoin(SkPaint::kMiter_Join);
paint.setStrokeWidth(quad->width);
current_canvas_->drawPath(path, paint);
}
void SkiaRenderer::DrawPictureQuad(const PictureDrawQuad* quad,
DrawQuadParams* params) {
DCHECK(batched_quads_.empty());
TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("viz.quads"),
"SkiaRenderer::DrawPictureQuad");
// If the layer is transparent or needs a non-SrcOver blend mode, saveLayer
// must be used so that the display list is drawn into a transient image and
// then blended as a single layer at the end.
const bool needs_transparency =
params->opacity < 1.f || params->blend_mode != SkBlendMode::kSrcOver;
const bool disable_image_filtering = params->sampling == SkSamplingOptions();
SkAutoCanvasRestore acr(current_canvas_, true /* do_save */);
PrepareCanvas(params->scissor_rect,
params->mask_filter_info,
¶ms->content_device_transform);
// Unlike other quads which draw visible_rect or draw_region as their geometry
// these represent the valid windows of content to show for the display list,
// so they need to be used as a clip in Skia.
SkRect visible_rect = gfx::RectFToSkRect(params->visible_rect);
SkPaint paint = params->paint(GetContentColorFilter());
if (params->draw_region) {
current_canvas_->clipPath(params->draw_region_in_path(),
paint.isAntiAlias());
} else {
current_canvas_->clipRect(visible_rect, paint.isAntiAlias());
}
if (needs_transparency) {
// Use the DrawQuadParams' paint for the layer, since that will affect the
// final draw of the backing image.
current_canvas_->saveLayer(&visible_rect, &paint);
}
SkCanvas* raster_canvas = current_canvas_;
std::optional<skia::OpacityFilterCanvas> opacity_canvas;
if (disable_image_filtering) {
// TODO(vmpstr): Fold this canvas into playback and have raster source
// accept a set of settings on playback that will determine which canvas to
// apply. (http://crbug.com/594679)
// saveLayer applies the opacity, this filter is only used for quality
// overriding in the display list, hence the fixed 1.f for alpha.
opacity_canvas.emplace(raster_canvas, 1.f, disable_image_filtering);
raster_canvas = &*opacity_canvas;
}
// Treat all subnormal values as zero for performance.
cc::ScopedSubnormalFloatDisabler disabler;
raster_canvas->concat(SkMatrix::RectToRect(
gfx::RectFToSkRect(quad->tex_coord_rect), gfx::RectToSkRect(quad->rect)));
raster_canvas->translate(-quad->content_rect.x(), -quad->content_rect.y());
raster_canvas->clipRect(gfx::RectToSkRect(quad->content_rect));
raster_canvas->scale(quad->contents_scale, quad->contents_scale);
quad->display_item_list->Raster(raster_canvas, /*image_provider=*/nullptr,
&quad->raster_inducing_scroll_offsets);
}
void SkiaRenderer::DrawSolidColorQuad(const SolidColorDrawQuad* quad,
const DrawRPDQParams* rpdq_params,
DrawQuadParams* params) {
DrawColoredQuad(quad->color, rpdq_params, params);
}
void SkiaRenderer::DrawTextureQuad(const TextureDrawQuad* quad,
const DrawRPDQParams* rpdq_params,
DrawQuadParams* params) {
TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("viz.quads"),
"SkiaRenderer::DrawTextureQuad");
// Sometimes we use different color space for overlays to make sure we stay on
// hardware path for power efficiency even if it's slightly incorrect. To
// avoid color changes during promotion we use the same color space for
// compositing.
std::optional<gfx::ColorSpace> overlay_color_space;
#if BUILDFLAG(IS_ANDROID)
if (resource_provider()->IsOverlayCandidate(quad->resource_id)) {
overlay_color_space =
OverlayProcessorSurfaceControl::GetOverrideColorSpace();
}
#endif
// We need only RGB portion of the color space, YUV conversion handled in
// skia.
const gfx::ColorSpace src_color_space =
overlay_color_space.value_or(resource_provider()
->GetColorSpace(quad->resource_id)
.GetAsFullRangeRGB());
const gfx::HDRMetadata& src_hdr_metadata =
resource_provider()->GetHDRMetadata(quad->resource_id);
const bool needs_tone_map = [&]() {
if (quad->is_video_frame && src_color_space.IsHDR()) {
return true;
}
if (src_color_space.IsToneMappedByDefault()) {
return true;
}
if (gfx::HdrMetadataAgtm::IsEnabled() &&
src_hdr_metadata.agtm.has_value()) {
return true;
}
return false;
}();
sk_sp<SkColorSpace> override_color_space;
if (overlay_color_space) {
override_color_space = overlay_color_space->ToSkColorSpace();
}
ScopedSkImageBuilder builder(
this, quad->resource_id, /*maybe_concurrent_reads=*/true,
resource_provider_->GetAlphaType(quad->resource_id), override_color_space,
false, quad->force_rgbx);
const SkImage* image = builder.sk_image();
if (!image)
return;
gfx::RectF uv_rect = gfx::ScaleRect(
gfx::BoundingRect(quad->uv_top_left, quad->uv_bottom_right),
image->width(), image->height());
params->vis_tex_coords = cc::MathUtil::ScaleRectProportional(
uv_rect, gfx::RectF(quad->rect), params->visible_rect);
gfx::RectF valid_texel_bounds = gfx::RectF(image->width(), image->height());
// For video frames, `valid_texel_bounds` is VideoFrame::visible_rect which is
// passed here via `uv_rect`.
if (quad->is_video_frame) {
valid_texel_bounds = uv_rect;
}
// There are three scenarios where a texture quad cannot be put into a batch:
// 1. It needs to be blended with a constant background color.
// 2. The quad contains video which might need special white level adjustment.
const bool blend_background =
quad->background_color != SkColors::kTransparent && !image->isOpaque();
if (!blend_background && !needs_tone_map && !rpdq_params) {
// This is a simple texture draw and can go into the batching system
DCHECK(!MustFlushBatchedQuads(quad, rpdq_params, *params));
AddQuadToBatch(image, valid_texel_bounds, params);
return;
}
// This needs a color filter for background blending and/or a mask filter
// to simulate the vertex opacity, which requires configuring a full SkPaint
// and is incompatible with anything batched, but since MustFlushBatchedQuads
// was optimistic for TextureQuad's, we're responsible for flushing now.
if (!batched_quads_.empty())
FlushBatchedQuads();
SkPaint paint = params->paint(GetContentColorFilter());
float quad_alpha;
if (rpdq_params) {
quad_alpha = 1.f;
} else {
// We will entirely handle the quad's opacity with the mask or color filter
quad_alpha = params->opacity;
params->opacity = 1.f;
}
// Auto-restore canvas state after applying clipShader and draw.
SkAutoCanvasRestore acr(current_canvas_, /*do_save=*/true);
if (needs_tone_map) {
// Use the current SDR slider white level for PQ HDR videos on
// Windows, so that they look similar when rendered by the
// compositor and when rendered as an overlay (HDR10 MPO).
// https://crbug.com/1492817
auto hdr_metadata = src_hdr_metadata;
if (quad->is_video_frame &&
src_color_space.GetTransferID() == gfx::ColorSpace::TransferID::PQ &&
base::FeatureList::IsEnabled(
features::kUseDisplaySDRMaxLuminanceNits)) {
hdr_metadata =
gfx::HDRMetadata::PopulateUnspecifiedWithDefaults(src_hdr_metadata);
hdr_metadata.ndwl = gfx::HdrMetadataNdwl(
current_frame()->display_color_spaces.GetSDRMaxLuminanceNits());
}
cc::ToneMapUtil::AddGlobalToneMapFilterToPaint(
paint, image, hdr_metadata,
quad->dynamic_range_limit.ComputeHdrHeadroom(
current_frame()
->display_color_spaces.GetHDRMaxLuminanceRelative()));
}
// From gl_renderer, the final src color will be
// (textureColor + backgroundColor * (1 - textureAlpha))
if (blend_background) {
// Add a color filter that does DstOver blending between texture and the
// background color. Then, modulate by quad's opacity *after* blending.
// TODO(crbug.com/40219248) remove toSkColor and make all SkColor4f
sk_sp<SkColorFilter> cf = SkColorFilters::Blend(
quad->background_color.toSkColor(), SkBlendMode::kDstOver);
if (quad_alpha < 1.f) {
cf = MakeOpacityFilter(quad_alpha, std::move(cf));
quad_alpha = 1.f;
DCHECK(cf);
}
// |cf| could be null if alpha in |quad->background_color| is 0.
if (cf)
paint.setColorFilter(cf->makeComposed(paint.refColorFilter()));
}
if (!rpdq_params) {
// Reset the paint's alpha, since it started as params.opacity and that
// is now applied outside of the paint's alpha.
paint.setAlphaf(quad_alpha);
}
DrawSingleImage(image, valid_texel_bounds, rpdq_params, &paint, params);
}
void SkiaRenderer::DrawTileDrawQuad(const TileDrawQuad* quad,
const DrawRPDQParams* rpdq_params,
DrawQuadParams* params) {
TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("viz.quads"),
"SkiaRenderer::DrawTileDrawQuad");
DCHECK(!MustFlushBatchedQuads(quad, rpdq_params, *params));
// |resource_provider()| can be NULL in resourceless software draws, which
// should never produce tile quads in the first place.
DCHECK(resource_provider());
// If quad->ShouldDrawWithBlending() is true, we need to raster tile paint ops
// to an offscreen texture first, and then blend it with content behind the
// tile. Since a tile could be used cross frames, so it would better to not
// use raw draw.
bool raw_draw_if_possible =
is_using_raw_draw_ && !quad->ShouldDrawWithBlending();
ScopedSkImageBuilder builder(
this, quad->resource_id, /*maybe_concurrent_reads=*/false,
kPremul_SkAlphaType,
/*override_color_space=*/nullptr, raw_draw_if_possible);
params->vis_tex_coords = cc::MathUtil::ScaleRectProportional(
quad->tex_coord_rect, gfx::RectF(quad->rect), params->visible_rect);
bool using_raw_draw = builder.paint_op_buffer();
if (is_using_raw_draw_) {
UMA_HISTOGRAM_BOOLEAN(
"Compositing.SkiaRenderer.DrawTileDrawQuad.UsingRawDraw",
using_raw_draw);
}
if (using_raw_draw) {
DCHECK(!rpdq_params);
DrawPaintOpBuffer(builder.paint_op_buffer(), builder.clear_color(), quad,
params);
return;
}
const SkImage* image = builder.sk_image();
if (!image)
return;
// When a tile is at the right or bottom edge of the entire tiled area, its
// images won't be fully filled so use the unclipped texture coords. On
// interior tiles or left/top tiles, the image has been filled with
// overlapping content so the entire image is valid for sampling.
gfx::RectF valid_texel_bounds(gfx::SizeF(
resource_provider()->GetResourceBackedSize(quad->resource_id)));
if (quad->IsRightEdge()) {
// Restrict the width to match far side of texture coords
valid_texel_bounds.set_width(quad->tex_coord_rect.right());
}
if (quad->IsBottomEdge()) {
// Restrict the height to match far side of texture coords
valid_texel_bounds.set_height(quad->tex_coord_rect.bottom());
}
if (rpdq_params) {
SkPaint paint = params->paint(GetContentColorFilter());
DrawSingleImage(image, valid_texel_bounds, rpdq_params, &paint, params);
} else {
AddQuadToBatch(image, valid_texel_bounds, params);
}
}
void SkiaRenderer::DrawUnsupportedQuad(const DrawQuad* quad,
const DrawRPDQParams* rpdq_params,
DrawQuadParams* params) {
#ifdef NDEBUG
DrawColoredQuad(SkColors::kWhite, rpdq_params, params);
#else
DrawColoredQuad(SkColors::kMagenta, rpdq_params, params);
#endif
}
void SkiaRenderer::ScheduleOverlays() {
DCHECK(!current_gpu_commands_completed_fence_->was_set());
DCHECK(!current_release_fence_->was_set());
// Always add an empty set of locks to be used in either SwapBuffersSkipped()
// or SwapBuffersComplete().
pending_overlay_locks_.emplace_back();
[[maybe_unused]] auto& locks = pending_overlay_locks_.back();
if (current_frame()->overlay_list.empty())
return;
std::vector<gpu::SyncToken> sync_tokens;
#if !BUILDFLAG(IS_WIN)
DCHECK(output_surface_->capabilities().supports_surfaceless);
#endif
for (auto& overlay : current_frame()->overlay_list) {
if (overlay.is_root_render_pass) {
continue;
}
#if BUILDFLAG(ENABLE_VULKAN) && BUILDFLAG(IS_CHROMEOS) && \
BUILDFLAG(USE_V4L2_CODEC)
if (overlay.needs_detiling) {
if (!std::holds_alternative<gfx::OverlayTransform>(overlay.transform)) {
LOG(ERROR) << "Unsupported transform on tiled protected content.";
continue;
}
locks.emplace_back(resource_provider(), overlay.resource_id);
auto& lock = locks.back();
bool is_10bit = overlay.format == MultiPlaneFormat::kP010;
gpu::Mailbox detiled_image = GetProtectedSharedImage(is_10bit);
skia_output_surface_->DetileOverlay(
overlay.mailbox, overlay.resource_size_in_pixels, lock.sync_token(),
detiled_image, overlay.display_rect, overlay.uv_rect,
std::get<gfx::OverlayTransform>(overlay.transform), is_10bit);
overlay.uv_rect = gfx::RectF(
static_cast<float>(overlay.display_rect.width()) /
static_cast<float>(kMaxProtectedContentWidth),
static_cast<float>(overlay.display_rect.height() /
static_cast<float>(kMaxProtectedContentHeight)));
overlay.mailbox = detiled_image;
overlay.format = (is_10bit && base::FeatureList::IsEnabled(
media::kEnableArmHwdrm10bitOverlays))
? SinglePlaneFormat::kBGRA_1010102
: SinglePlaneFormat::kBGRA_8888;
overlay.transform = gfx::OVERLAY_TRANSFORM_NONE;
continue;
}
#endif
#if BUILDFLAG(IS_APPLE) || BUILDFLAG(IS_OZONE) || BUILDFLAG(IS_WIN)
if (overlay.rpdq) {
// Try and use the render pass backing image directly as an overlay.
if (auto backing = GetRenderPassBackingForDirectScanout(
overlay.rpdq->render_pass_id);
backing) {
DBG_LOG("delegated.overlay.log",
"Pass %" PRIu64 ": RPDQ overlay can scanout directly",
overlay.rpdq->render_pass_id.value());
// SkiaRenderer might've allocated a larger backing than our render
// pass' requested size.
overlay.uv_rect =
gfx::MapRect(overlay.rpdq->tex_coord_rect,
gfx::RectF(backing->size), gfx::RectF(1, 1));
if (overlay.rpdq->visible_rect != overlay.rpdq->rect) {
// The overlay's |display_rect| is the quad's |visible_rect|, so we
// need to adjust our |uv_rect| to account for that.
overlay.uv_rect =
gfx::MapRect(gfx::RectF(overlay.rpdq->visible_rect),
gfx::RectF(overlay.rpdq->rect), overlay.uv_rect);
}
overlay.mailbox = backing->mailbox;
overlay.resource_size_in_pixels = backing->size;
// We do not add the overlay mailbox to |locks| since we're using a
// backing from |render_pass_backings_| directly. We can expect the
// synchronization to be handled externally.
} else {
PrepareRenderPassOverlay(&overlay);
if (!overlay.mailbox.IsZero()) {
locks.emplace_back(this, overlay.mailbox);
}
}
// The overlay will be sent to GPU the thread, so set rpdq to nullptr to
// avoid being accessed on the GPU thread.
overlay.rpdq = nullptr;
continue;
}
#else
DCHECK(!overlay.rpdq);
#endif
if (overlay.is_solid_color) {
DCHECK(overlay.color);
DCHECK(!overlay.resource_id);
// All other platforms must support solid color overlays
DCHECK(output_surface_->capabilities()
.supports_non_backed_solid_color_overlays ||
output_surface_->capabilities().supports_single_pixel_buffer);
continue;
}
// Resources will be unlocked after the next SwapBuffers() is completed.
locks.emplace_back(resource_provider(), overlay.resource_id);
auto& lock = locks.back();
// Sync tokens ensure the texture to be overlaid is available before
// scheduling it for display.
if (lock.sync_token().HasData())
sync_tokens.push_back(lock.sync_token());
overlay.mailbox = lock.mailbox();
DCHECK(!overlay.mailbox.IsZero());
}
DCHECK(!current_gpu_commands_completed_fence_->was_set());
DCHECK(!current_release_fence_->was_set());
skia_output_surface_->ScheduleOverlays(
std::move(current_frame()->overlay_list), std::move(sync_tokens));
}
namespace {
SkColorMatrix ToColorMatrix(const SkM44& mat) {
std::array<float, 20> values;
values.fill(0.0f);
for (uint32_t r = 0; r < 4; r++) {
for (uint32_t c = 0; c < 4; c++) {
values[r * 5 + c] = mat.rc(r, c);
}
}
SkColorMatrix mat_out;
mat_out.setRowMajor(values.data());
return mat_out;
}
} // namespace
sk_sp<SkColorFilter> SkiaRenderer::GetContentColorFilter() {
sk_sp<SkColorFilter> color_transform = nullptr;
bool is_root =
current_frame()->current_render_pass == current_frame()->root_render_pass;
if (is_root && output_surface_->color_matrix() != SkM44()) {
color_transform =
SkColorFilters::Matrix(ToColorMatrix(output_surface_->color_matrix()));
}
sk_sp<SkColorFilter> tint_transform = nullptr;
if (is_root && debug_settings_->tint_composited_content) {
if (debug_settings_->tint_composited_content_modulate) {
// Integer counter causes modulation through rgb dimming variations.
std::array<float, 3> rgb;
uint32_t ci = debug_tint_modulate_count_ % 7u;
for (int rc = 0; rc < 3; rc++) {
rgb[rc] = (ci & (1u << rc)) ? 0.7f : 1.0f;
}
SkColorMatrix color_mat;
color_mat.setScale(rgb[0], rgb[1], rgb[2]);
tint_transform = SkColorFilters::Matrix(color_mat);
} else {
SkM44 mat44 = SkM44::ColMajor(
cc::DebugColors::TintCompositedContentColorTransformMatrix().data());
tint_transform = SkColorFilters::Matrix(ToColorMatrix(mat44));
}
}
if (color_transform) {
return tint_transform ? color_transform->makeComposed(tint_transform)
: color_transform;
} else {
return tint_transform;
}
}
SkiaRenderer::DrawRPDQParams SkiaRenderer::CalculateRPDQParams(
const gfx::AxisTransform2d& target_to_device,
const AggregatedRenderPassDrawQuad* quad,
const DrawQuadParams* params) {
DrawRPDQParams rpdq_params(params->visible_rect);
if (!quad->mask_resource_id().is_null()) {
// Scale normalized uv rect into absolute texel coordinates.
SkRect mask_rect = gfx::RectFToSkRect(
gfx::ScaleRect(quad->mask_uv_rect, quad->mask_texture_size.width(),
quad->mask_texture_size.height()));
SkMatrix mask_to_quad_matrix =
SkMatrix::RectToRect(mask_rect, gfx::RectToSkRect(quad->rect));
rpdq_params.mask_shader.emplace(quad->mask_resource_id(),
mask_to_quad_matrix);
}
const cc::FilterOperations* filters = FiltersForPass(quad->render_pass_id);
const cc::FilterOperations* backdrop_filters =
BackdropFiltersForPass(quad->render_pass_id);
// Early out if there are no filters to convert to SkImageFilters
if (!filters && !backdrop_filters) {
return rpdq_params;
}
// Calculate local matrix that's shared by filters and backdrop_filters. This
// local matrix represents the UI display scale that's already been applied to
// the DrawQuads but not any geometric properties of the filters.
SkMatrix local_matrix;
local_matrix.setTranslate(quad->filters_origin.x(), quad->filters_origin.y());
local_matrix.postScale(quad->filters_scale.x(), quad->filters_scale.y());
auto to_sk_image_filter =
[](sk_sp<cc::PaintFilter> paint_filter,
const SkMatrix& local_matrix) -> sk_sp<SkImageFilter> {
if (paint_filter && paint_filter->cached_sk_filter_) {
return paint_filter->cached_sk_filter_->makeWithLocalMatrix(local_matrix);
} else {
return nullptr;
}
};
// Convert CC image filters into a SkImageFilter root node
if (filters) {
DCHECK(!filters->IsEmpty());
auto paint_filter = cc::RenderSurfaceFilters::BuildImageFilter(*filters);
rpdq_params.image_filter =
to_sk_image_filter(std::move(paint_filter), local_matrix);
if (rpdq_params.image_filter) {
// Update the filter bounds to account for how the image filters
// grow or move the area touched by the base quad.
rpdq_params.filter_bounds = rpdq_params.image_filter->computeFastBounds(
rpdq_params.filter_bounds);
// Attempt to simplify the image filter to a color filter, which enables
// the RPDQ effects to be applied more efficiently.
SkColorFilter* color_filter_ptr = nullptr;
if (rpdq_params.image_filter->asAColorFilter(&color_filter_ptr)) {
// asAColorFilter already ref'ed the filter when true is returned,
// reset() does not add a ref itself, so everything is okay.
rpdq_params.color_filter.reset(color_filter_ptr);
}
}
}
// Convert CC image filters for the backdrop into a SkImageFilter root node
if (backdrop_filters) {
DCHECK(!backdrop_filters->IsEmpty());
rpdq_params.backdrop_filter_quality = quad->backdrop_filter_quality;
// quad->rect represents the layer's bounds *after* any display scale has
// been applied to it. The ZOOM FilterOperation uses the layer's bounds as
// its "lens" bounds. All image filters operate with a local matrix to
// match the display scale. We must undo the local matrix's effect on
// quad->rect to get the input bounds for ZOOM. Otherwise its lens would be
// doubly-scaled while none of the other filter operations would align.
SkMatrix inv_local_matrix;
if (local_matrix.invert(&inv_local_matrix)) {
SkIRect filter_rect =
inv_local_matrix.mapRect(gfx::RectToSkRect(quad->rect)).roundOut();
auto bg_paint_filter = cc::RenderSurfaceFilters::BuildImageFilter(
*backdrop_filters, gfx::SkIRectToRect(filter_rect));
rpdq_params.backdrop_filter =
to_sk_image_filter(std::move(bg_paint_filter), local_matrix);
}
}
// Determine the clipping to apply to the backdrop filter. Skia normally
// fills layers with the backdrop content, whereas viz wants the backdrop
// content restricted to the intersection of the DrawQuad and any defined
// |backdrop_filter_bounds|.
if (rpdq_params.backdrop_filter) {
SkRect backdrop_rect = gfx::RectFToSkRect(params->visible_rect);
// Pass bounds do not match the display scale; they will be scaled and
// converted into an SkPath in |backdrop_filter_bounds| if defined.
std::optional<SkPath> pass_bounds =
BackdropFilterBoundsForPass(quad->render_pass_id);
std::optional<SkPath> backdrop_filter_bounds;
if (pass_bounds) {
SkRRect backdrop_filter_bounds_as_rrect;
SkRect backdrop_filter_bounds_as_rect;
SkRRect transformed_filter_bounds;
const bool is_rect = pass_bounds->isRect(&backdrop_filter_bounds_as_rect);
if (is_rect || pass_bounds->isRRect(&backdrop_filter_bounds_as_rrect)) {
if (is_rect) {
backdrop_filter_bounds_as_rrect =
SkRRect::MakeRect(backdrop_filter_bounds_as_rect);
}
// Scale by the filter's scale, but don't apply filter origin
SkRRect result;
if (!backdrop_filter_bounds_as_rrect.transform(local_matrix, &result) ||
!backdrop_rect.intersect(result.rect())) {
// No visible backdrop filter
rpdq_params.backdrop_filter = nullptr;
return rpdq_params;
} else {
transformed_filter_bounds = result;
backdrop_filter_bounds = SkPath::RRect(result);
}
if (transformed_filter_bounds.contains(rpdq_params.filter_bounds)) {
// The backdrop filter bounds are a no-op since the quad rect or
// region fully limits the backdrop filter.
backdrop_filter_bounds.reset();
} else {
// The backdrop filter bounds might have an effect, but a simple case
// to check for is if the backdrop rounded corners are identical to
// the quad's rounded corner mask info. In that case, the prior
// contains() check would be false, but we can still discard these
// bounds since the final mask clip will achieve the same visual
// effect.
if (params->mask_filter_info) {
SkMatrix m = gfx::TransformToFlattenedSkMatrix(
params->content_device_transform);
if (transformed_filter_bounds.transform(m, &result) &&
SkRRect(params->mask_filter_info->rounded_corner_bounds()) ==
result) {
backdrop_filter_bounds.reset();
}
}
}
} else {
SkPath transformed_path;
transformed_path.addPath(*pass_bounds, local_matrix);
if (!backdrop_rect.intersect(transformed_path.getBounds())) {
rpdq_params.backdrop_filter = nullptr;
return rpdq_params;
}
backdrop_filter_bounds = transformed_path;
}
}
// Besides ensuring the output of the backdrop filter doesn't go beyond its
// bounds, it should not read pixels outside of its bounds to prevent color
// bleeding. If it's a pixel-moving filter, we compose a kMirror-tiling Crop
// image filter to enforce this requirement. Mirror tiling avoids jarring
// discontinuities and flickering when content moves in and out of the
// background. See https://github.com/w3c/fxtf-drafts/issues/374.
// NOTE: The above comment refers to the intended ideal behavior. Originally
// the edge mode was kClamp and a feature controls the active mode.
SkIRect sk_crop_rect = backdrop_rect.roundOut();
SkIRect sk_src_rect = rpdq_params.backdrop_filter->filterBounds(
sk_crop_rect, SkMatrix::I(), SkImageFilter::kReverse_MapDirection,
/*inputRect=*/nullptr);
if (!sk_crop_rect.contains(sk_src_rect)) {
SkTileMode sk_tile_mode =
base::FeatureList::IsEnabled(features::kBackdropFilterMirrorEdgeMode)
? SkTileMode::kMirror
: SkTileMode::kClamp;
rpdq_params.backdrop_filter = SkImageFilters::Compose(
/*outer=*/std::move(rpdq_params.backdrop_filter),
/*inner=*/SkImageFilters::Crop(backdrop_rect, sk_tile_mode, nullptr));
}
// Update |filter_bounds| to include content produced by the backdrop. Under
// most circumstances this will be a no-op since content is restricted to
// underneath the RPDQ's draw region, but if a backdrop filter is combined
// with some pixel-moving filters, that may not remain the case and this
// ensures |filter_bounds| will contain all possible output.
rpdq_params.filter_bounds.join(backdrop_rect);
rpdq_params.backdrop_filter_bounds = backdrop_filter_bounds;
}
return rpdq_params;
}
void SkiaRenderer::DrawRenderPassQuad(
const AggregatedRenderPassDrawQuad* quad,
const DrawRPDQParams* bypassed_rpdq_params,
DrawQuadParams* params) {
TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("viz.quads"),
"SkiaRenderer::DrawRenderPassQuad");
DrawRPDQParams rpdq_params =
bypassed_rpdq_params
? *bypassed_rpdq_params
: CalculateRPDQParams(current_frame()->target_to_device_transform,
quad, params);
// |filter_bounds| is the content space bounds that includes any filtered
// extents. If empty, the draw can be skipped.
if (rpdq_params.filter_bounds.isEmpty()) {
return;
}
auto bypass = render_pass_bypass_quads_.find(quad->render_pass_id);
// When Render Pass has a single quad inside we would draw that directly.
if (bypass != render_pass_bypass_quads_.end()) {
BypassMode mode =
CalculateBypassParams(bypass->second, &rpdq_params, params);
if (mode == BypassMode::kDrawTransparentQuad) {
// The RPDQ is masquerading as a solid color quad, which do not support
// batching.
if (!batched_quads_.empty())
FlushBatchedQuads();
DrawColoredQuad(SkColors::kTransparent, &rpdq_params, params);
} else if (mode == BypassMode::kDrawBypassQuad) {
DrawQuadInternal(bypass->second, &rpdq_params, params);
} // else mode == kSkip
return;
}
// A real render pass that was turned into an image
auto iter = render_pass_backings_.find(quad->render_pass_id);
if (iter == render_pass_backings_.end()) {
if (base::FeatureList::IsEnabled(
kDumpWithoutCrashingOnMissingRenderPassBacking)) {
// This can happen if we previously skipped drawing a render pass (and
// allocating its backing) due to an empty update rect.
LOG(ERROR) << "Could not find render pass id # " << quad->render_pass_id
<< " in the render pass overlay backings";
SCOPED_CRASH_KEY_STRING32(
"missing rp backing", "0-seen before?",
base::NumberToString(
seen_render_pass_ids_.contains(quad->render_pass_id)));
// This is derived from |DirectRenderer::ShouldSkipQuad|.
gfx::Rect visible_rect = quad->visible_rect;
SCOPED_CRASH_KEY_STRING32("missing rp backing", "1-visible rect",
visible_rect.ToString());
auto filter_it = render_pass_filters_.find(quad->render_pass_id);
if (filter_it != render_pass_filters_.end()) {
visible_rect =
GetExpandedRectForPixelMovingFilters(*quad, *filter_it->second);
}
SCOPED_CRASH_KEY_STRING32("missing rp backing", "2-filter expansion",
filter_it != render_pass_filters_.end()
? visible_rect.ToString()
: "no filter expansion");
const gfx::QuadF target_quad =
quad->shared_quad_state->quad_to_target_transform.MapQuad(
gfx::QuadF(gfx::RectF(visible_rect)));
SCOPED_CRASH_KEY_STRING256("missing rp backing", "3-rpdq in draw",
target_quad.IsRectilinear()
? target_quad.BoundingBox().ToString()
: target_quad.ToString());
gfx::Rect draw_rect_in_draw_space = OutputSurfaceRectInDrawSpace();
SCOPED_CRASH_KEY_STRING32("missing rp backing", "4-output surface",
draw_rect_in_draw_space.ToString());
if (scissor_rect_) {
draw_rect_in_draw_space = scissor_rect_.value();
draw_rect_in_draw_space.Offset(
current_frame()
->current_render_pass->output_rect.OffsetFromOrigin());
}
SCOPED_CRASH_KEY_STRING32(
"missing rp backing", "5-with scissor?",
scissor_rect_ ? draw_rect_in_draw_space.ToString() : "no scissor");
if (quad->shared_quad_state->clip_rect) {
draw_rect_in_draw_space.Intersect(*quad->shared_quad_state->clip_rect);
}
SCOPED_CRASH_KEY_STRING32("missing rp backing", "6-with quad clip?",
quad->shared_quad_state->clip_rect
? draw_rect_in_draw_space.ToString()
: "no quad clip");
const bool intersects =
target_quad.IntersectsRect(gfx::RectF(draw_rect_in_draw_space));
SCOPED_CRASH_KEY_STRING32("missing rp backing", "7-intersects?",
base::NumberToString(intersects));
SCOPED_CRASH_KEY_STRING32(
"missing rp backing", "8-quad-pass-id",
base::NumberToString(quad->render_pass_id.value()));
std::vector<std::string> pass_ids;
for (const auto& pass : *current_frame()->render_passes_in_draw_order) {
pass_ids.push_back(base::NumberToString(pass->id.value()));
}
SCOPED_CRASH_KEY_STRING256("missing rp backing", "9-frame-pass-ids",
base::JoinString(pass_ids, ","));
pass_ids.clear();
for (const auto& [id, pass] : render_pass_backings_) {
pass_ids.push_back(base::NumberToString(id.value()));
}
SCOPED_CRASH_KEY_STRING256("missing rp backing", "10-backing-pass-ids",
base::JoinString(pass_ids, ","));
auto it =
std::ranges::find(*current_frame()->render_passes_in_draw_order,
quad->render_pass_id, &AggregatedRenderPass::id);
SCOPED_CRASH_KEY_STRING256(
"missing rp backing", "11-rp transform",
it != current_frame()->render_passes_in_draw_order->end()
? it->get()->transform_to_root_target.ToString()
: "missing pass in frame");
SCOPED_CRASH_KEY_STRING256(
"missing rp backing", "12-rpdq transform",
quad->shared_quad_state->quad_to_target_transform.ToString());
// Collect a dump so we can investigate the root cause, but fallback to a
// solid color to avoid disrupting the user.
base::debug::DumpWithoutCrashing();
}
// The fallback is a solid color quad, which do not support batching.
if (!batched_quads_.empty()) {
FlushBatchedQuads();
}
#if DCHECK_IS_ON()
DrawColoredQuad(SkColors::kRed, &rpdq_params, params);
#else
DrawColoredQuad(SkColors::kWhite, &rpdq_params, params);
#endif
return;
}
// This function is called after AllocateRenderPassResourceIfNeeded, so
// there should be backing ready.
RenderPassBacking& backing = iter->second;
sk_sp<SkImage> content_image =
skia_output_surface_->MakePromiseSkImageFromRenderPass(
quad->render_pass_id, backing.size, backing.format,
backing.generate_mipmap, RenderPassBackingSkColorSpace(backing),
backing.mailbox);
DLOG_IF(ERROR, !content_image)
<< "MakePromiseSkImageFromRenderPass() failed for render pass";
if (!content_image)
return;
if (backing.generate_mipmap)
params->sampling =
SkSamplingOptions(SkFilterMode::kLinear, SkMipmapMode::kLinear);
params->vis_tex_coords = cc::MathUtil::ScaleRectProportional(
quad->tex_coord_rect, gfx::RectF(quad->rect), params->visible_rect);
gfx::RectF valid_texel_bounds(content_image->width(),
content_image->height());
// When the RPDQ was needed because of a copy request, it may not require any
// advanced filtering/effects at which point it's basically a tiled quad.
if (!rpdq_params.image_filter && !rpdq_params.backdrop_filter &&
!rpdq_params.mask_shader && !rpdq_params.bypass_geometry) {
DCHECK(!MustFlushBatchedQuads(quad, nullptr, *params));
AddQuadToBatch(content_image.get(), valid_texel_bounds, params);
return;
}
// The paint is complex enough that it has to be drawn on its own, and since
// MustFlushBatchedQuads() was optimistic, we manage the flush here.
if (!batched_quads_.empty())
FlushBatchedQuads();
SkPaint paint = params->paint(GetContentColorFilter());
DrawSingleImage(content_image.get(), valid_texel_bounds, &rpdq_params, &paint,
params);
}
void SkiaRenderer::CopyDrawnRenderPass(
const copy_output::RenderPassGeometry& geometry,
std::unique_ptr<CopyOutputRequest> request) {
// TODO(weiliangc): Make copy request work. (crbug.com/644851)
TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("viz.quads"),
"SkiaRenderer::CopyDrawnRenderPass");
// Root framebuffer uses a zero-mailbox in SkiaOutputSurface.
gpu::Mailbox mailbox;
const auto* const render_pass = current_frame()->current_render_pass.get();
AggregatedRenderPassId render_pass_id = render_pass->id;
auto it = render_pass_backings_.find(render_pass_id);
if (it != render_pass_backings_.end()) {
mailbox = it->second.mailbox;
}
skia_output_surface_->CopyOutput(geometry, RenderPassColorSpace(render_pass),
std::move(request), mailbox);
}
void SkiaRenderer::DidChangeVisibility() {
if (visible_)
output_surface_->EnsureBackbuffer();
else
output_surface_->DiscardBackbuffer();
}
void SkiaRenderer::FinishDrawingRenderPass() {
TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("viz.quads"),
"SkiaRenderer::FinishDrawingRenderPass");
if (!current_canvas_)
return;
if (!batched_quads_.empty())
FlushBatchedQuads();
bool is_root_render_pass =
current_frame()->current_render_pass == current_frame()->root_render_pass;
// Drawing the delegated ink trail must happen after the final
// FlushBatchedQuads() call so that the trail can always be on top of
// everything else that has already been drawn on the page.
if (UsingSkiaForDelegatedInk()) {
if (const auto pass_id = delegated_ink_handler_->GetInkRenderer()
->GetLatestMetadataRenderPassId();
current_frame()->current_render_pass->id == pass_id) {
gfx::Transform root_target_to_render_pass_draw_transform;
if (current_frame()
->current_render_pass->transform_to_root_target.GetInverse(
&root_target_to_render_pass_draw_transform)) {
DrawDelegatedInkTrail(root_target_to_render_pass_draw_transform);
}
}
}
// Pops a layer that is pushed at the start of |BeginDrawingRenderPass|. This
// applies color space conversion for HDR passes, if present.
hdr_color_conversion_layer_reset_.reset();
current_canvas_ = nullptr;
// Non-root render passes that are scheduled as overlays will be painted in
// PrepareRenderPassOverlay().
bool is_overlay =
skia_output_surface_->capabilities().renderer_allocates_images &&
is_root_render_pass;
EndPaint(current_render_pass_update_rect_, /*failed=*/false, is_overlay);
// Defer flushing drawing task for root render pass, to avoid extra
// MakeCurrent() call. It is expensive on GL.
// TODO(crbug.com/40154045): Consider deferring drawing tasks for
// all render passes.
if (is_root_render_pass)
return;
FlushOutputSurface();
}
void SkiaRenderer::UpdateRenderPassTextures(
const AggregatedRenderPassList& render_passes_in_draw_order,
const base::flat_map<AggregatedRenderPassId, RenderPassRequirements>&
render_passes_in_frame) {
const auto& root_pass_id = render_passes_in_draw_order.back()->id;
std::vector<AggregatedRenderPassId> passes_to_delete;
for (const auto& [backing_id, backing] : render_pass_backings_) {
// Buffer queue's root manages the root pass backing and its bookkeeping
// separately from other render pass backings.
if (buffer_queue_) {
// If a root backing exists but its id does not match the current root
// render pass id, then it must be an old backing that should be deleted.
// Otherwise we should not delete a root backing in case it is scheduled
// this frame but not drawn (e.g. in the case of overlay-only damage).
if (backing.is_root) {
if (backing_id != root_pass_id) {
passes_to_delete.push_back(backing_id);
}
continue;
}
}
auto render_pass_it = render_passes_in_frame.find(backing_id);
if (render_pass_it == render_passes_in_frame.end()) {
passes_to_delete.push_back(backing_id);
DBG_LOG("renderer.skia.render_pass_backings",
"render_pass %" PRIu64 " is no longer in frame",
backing_id.value());
continue;
}
const RenderPassRequirements& requirements = render_pass_it->second;
const bool size_is_exact_match = backing.size == requirements.size;
const bool size_is_sufficient =
backing.size.width() >= requirements.size.width() &&
backing.size.height() >= requirements.size.height();
bool size_appropriate =
backing.is_root ? size_is_exact_match : size_is_sufficient;
bool mipmap_appropriate =
!requirements.generate_mipmap || backing.generate_mipmap;
bool no_change_in_format = requirements.format == backing.format;
bool no_change_in_alpha_type =
requirements.alpha_type == backing.alpha_type;
bool no_change_in_color_space =
requirements.color_space == backing.color_space;
bool scanout_appropriate =
requirements.is_scanout == backing.is_scanout &&
requirements.scanout_dcomp_surface == backing.scanout_dcomp_surface;
if (!size_appropriate || !mipmap_appropriate || !no_change_in_format ||
!no_change_in_alpha_type || !no_change_in_color_space ||
!scanout_appropriate) {
passes_to_delete.push_back(backing_id);
DBG_LOG("renderer.skia.render_pass_backings",
"render_pass %" PRIu64
" allocation part not appropriate:%s%s%s%s%s%s",
backing_id.value(), !size_appropriate ? " size" : "",
!mipmap_appropriate ? " mipmap" : "",
!no_change_in_format ? " format" : "",
!no_change_in_alpha_type ? " alpha_type" : "",
!no_change_in_color_space ? " color_space" : "",
!scanout_appropriate ? " scanout" : "");
}
}
// Delete RenderPass backings from the previous frame that will not be used
// again.
for (size_t i = 0; i < passes_to_delete.size(); ++i) {
auto it = render_pass_backings_.find(passes_to_delete[i]);
auto& backing = it->second;
// Root render pass backings managed by |buffer_queue_| are not managed by
// DisplayResourceProvider, so we should not destroy them here. This
// reallocation is done in Reshape before drawing the frame
if (!(buffer_queue_ && backing.is_root)) {
skia_output_surface_->DestroySharedImage(backing.mailbox);
}
render_pass_backings_.erase(it);
}
if (!passes_to_delete.empty()) {
skia_output_surface_->RemoveRenderPassResource(std::move(passes_to_delete));
}
}
void SkiaRenderer::AllocateRenderPassResourceIfNeeded(
const AggregatedRenderPassId& render_pass_id,
const RenderPassRequirements& requirements) {
const bool is_root = render_pass_id == current_frame()->root_render_pass->id;
// Root render pass backings managed by |buffer_queue_| are not managed by
// DisplayResourceProvider, so we should not allocate them here.
if (buffer_queue_ && is_root) {
auto& root_pass_backing = render_pass_backings_[render_pass_id];
root_pass_backing.is_root = true;
root_pass_backing.mailbox = buffer_queue_->GetCurrentBuffer();
root_pass_backing.generate_mipmap = requirements.generate_mipmap;
root_pass_backing.size = requirements.size;
root_pass_backing.format = requirements.format;
root_pass_backing.alpha_type = requirements.alpha_type;
root_pass_backing.color_space = requirements.color_space;
root_pass_backing.is_scanout = true;
root_pass_backing.scanout_dcomp_surface = false;
return;
}
auto it = render_pass_backings_.find(render_pass_id);
if (it != render_pass_backings_.end()) {
DCHECK(gfx::Rect(it->second.size).Contains(gfx::Rect(requirements.size)));
// A root backing should not be used for other render passes. If the root
// pass id has changed, then it's old backing should have been deleted
// already in UpdateRenderPassTextures().
DCHECK(!(buffer_queue_ && it->second.is_root));
return;
}
gpu::SharedImageUsageSet usage = gpu::SHARED_IMAGE_USAGE_DISPLAY_READ |
gpu::SHARED_IMAGE_USAGE_DISPLAY_WRITE;
if (requirements.generate_mipmap) {
DCHECK(!requirements.is_scanout);
usage |= gpu::SHARED_IMAGE_USAGE_MIPMAP;
}
if (requirements.is_scanout &&
!settings_->force_non_scanout_backing_for_pixel_tests) {
usage |= gpu::SHARED_IMAGE_USAGE_SCANOUT;
#if BUILDFLAG(IS_WIN)
// DComp surfaces do not support RGB10A2 so we must fall back to swap
// chains. If this happens with video overlays, this can result in the video
// overlay and its parent surface having unsynchronized updates.
//
// TODO(tangm): We should clean this up by either avoiding HDR or using
// RGBAF16 surfaces in this case.
const bool dcomp_surface_unsupported_format =
requirements.format == SinglePlaneFormat::kRGBA_1010102;
if (requirements.scanout_dcomp_surface &&
!dcomp_surface_unsupported_format) {
usage |= gpu::SHARED_IMAGE_USAGE_SCANOUT_DCOMP_SURFACE;
// DComp surfaces are write-only, viz cannot sample them.
usage.RemoveAll(gpu::SHARED_IMAGE_USAGE_DISPLAY_READ);
} else {
usage |= gpu::SHARED_IMAGE_USAGE_SCANOUT_DXGI_SWAP_CHAIN;
}
#else
DCHECK(!requirements.scanout_dcomp_surface);
#endif
} else {
DCHECK(!requirements.scanout_dcomp_surface);
}
auto mailbox = skia_output_surface_->CreateSharedImage(
requirements.format, requirements.size, requirements.color_space,
requirements.alpha_type, usage, "RenderPassBacking",
gpu::kNullSurfaceHandle);
VizDebuggerLog::DebugLogNewRenderPassBacking(render_pass_id, requirements);
render_pass_backings_.emplace(
render_pass_id,
RenderPassBacking(requirements.size, requirements.generate_mipmap,
requirements.color_space, requirements.alpha_type,
requirements.format, mailbox, is_root,
requirements.is_scanout,
requirements.scanout_dcomp_surface));
if (base::FeatureList::IsEnabled(
kDumpWithoutCrashingOnMissingRenderPassBacking)) {
seen_render_pass_ids_.insert(render_pass_id);
}
}
void SkiaRenderer::FlushOutputSurface() {
auto sync_token = skia_output_surface_->Flush();
lock_set_for_external_use_.UnlockResources(sync_token);
}
#if BUILDFLAG(IS_APPLE) || BUILDFLAG(IS_OZONE) || BUILDFLAG(IS_WIN)
bool SkiaRenderer::CanSkipRenderPassOverlay(
AggregatedRenderPassId render_pass_id,
const AggregatedRenderPassDrawQuad* rpdq,
RenderPassOverlayParams** output_render_pass_overlay) {
// The render pass draw quad can be skipped if (1) the render pass has no
// damage and is skipped in DirectRender and (2) the parameters of drawing the
// render pass has not changed.
// Check if the render pass has been re-drawn.
if (skipped_render_pass_ids_.count(render_pass_id) == 0)
return false;
// Every time a new render_pass_overlay is allocated, it's added to the back
// of the list. In order to get the render_pass_overlay of the previous frame,
// loop through the list in a reverse order since there might be multiple
// render pass overlays with the same render pass id.
RenderPassOverlayParams* overlay_found = nullptr;
bool found_in_available_backings = false;
for (auto rit = in_flight_render_pass_overlay_backings_.rbegin();
rit != in_flight_render_pass_overlay_backings_.rend(); ++rit) {
if (rit->render_pass_id == render_pass_id) {
overlay_found = &*rit;
break;
}
}
// The backing of the previous frame might be complete and moved to
// available_render_pass_overlay_backings_ when this frame starts.
std::vector<RenderPassOverlayParams>::iterator it_to_delete;
if (!overlay_found) {
int index = 0;
for (auto rit = available_render_pass_overlay_backings_.rbegin();
rit != available_render_pass_overlay_backings_.rend();
++rit, ++index) {
if (rit->render_pass_id == render_pass_id) {
found_in_available_backings = true;
// Cannot use reverse_iterator. Convert it to const_iterator.
it_to_delete =
available_render_pass_overlay_backings_.begin() +
(available_render_pass_overlay_backings_.size() - index - 1);
overlay_found = &*rit;
break;
}
}
}
if (!overlay_found) {
return false;
}
// Compare RenderPassDrawQuads of the previous frame and the current frame.
const cc::FilterOperations* filters = FiltersForPass(render_pass_id);
const cc::FilterOperations* backdrop_filters =
BackdropFiltersForPass(render_pass_id);
overlay_found->rpdq.shared_quad_state = &(overlay_found->shared_quad_state);
bool no_change_in_rpdq = overlay_found->rpdq.Equals(*rpdq);
bool no_change_in_filters =
filters ? (overlay_found->filters == *filters)
: (overlay_found->filters == cc::FilterOperations());
bool no_change_in_backdrop_filters =
backdrop_filters
? (overlay_found->backdrop_filters == *backdrop_filters)
: (overlay_found->backdrop_filters == cc::FilterOperations());
if (no_change_in_rpdq && no_change_in_filters &&
no_change_in_backdrop_filters) {
if (found_in_available_backings) {
in_flight_render_pass_overlay_backings_.push_back(*overlay_found);
available_render_pass_overlay_backings_.erase(it_to_delete);
*output_render_pass_overlay =
&in_flight_render_pass_overlay_backings_.back();
} else {
*output_render_pass_overlay = overlay_found;
}
return true;
} else {
return false;
}
}
std::optional<SkiaRenderer::RenderPassBacking>
SkiaRenderer::GetRenderPassBackingForDirectScanout(
const AggregatedRenderPassId& render_pass_id) const {
#if BUILDFLAG(IS_WIN)
if (auto backing_it = render_pass_backings_.find(render_pass_id);
backing_it != render_pass_backings_.end()) {
if (backing_it->second.is_scanout) {
if (DCHECK_IS_ON()) {
auto pass_it =
std::ranges::find(*current_frame()->render_passes_in_draw_order,
backing_it->first, &AggregatedRenderPass::id);
CHECK(pass_it != current_frame()->render_passes_in_draw_order->end());
DCHECK(!pass_it->get()->generate_mipmap);
DCHECK(pass_it->get()->filters.IsEmpty());
DCHECK(pass_it->get()->backdrop_filters.IsEmpty());
DCHECK(!(pass_it->get()->will_backing_be_read_by_viz &&
backing_it->second.scanout_dcomp_surface));
}
return std::make_optional(backing_it->second);
}
}
#else
// Non-Win backends need BufferQueue support on render pass backings. Any new
// implementation should also modify |CanPassBeDrawnDirectly| to avoid the
// bypass quad case for direct scanout backings.
#endif
return std::nullopt;
}
SkiaRenderer::RenderPassOverlayParams*
SkiaRenderer::GetOrCreateRenderPassOverlayBacking(
AggregatedRenderPassId render_pass_id,
const AggregatedRenderPassDrawQuad* rpdq,
SharedImageFormat buffer_format,
gfx::ColorSpace color_space,
const gfx::Size& buffer_size) {
RenderPassOverlayParams overlay_params;
auto it = std::ranges::find_if(available_render_pass_overlay_backings_,
[&buffer_format, &buffer_size, &color_space](
const RenderPassOverlayParams& overlay) {
auto& backing = overlay.render_pass_backing;
return backing.format == buffer_format &&
backing.size == buffer_size &&
backing.color_space == color_space;
});
if (it == available_render_pass_overlay_backings_.end()) {
// Allocate the image for render pass overlay if there is no existing
// available one.
auto kOverlayUsage = gpu::SHARED_IMAGE_USAGE_SCANOUT |
gpu::SHARED_IMAGE_USAGE_DISPLAY_READ |
gpu::SHARED_IMAGE_USAGE_DISPLAY_WRITE;
auto mailbox = skia_output_surface_->CreateSharedImage(
buffer_format, buffer_size, color_space, RenderPassAlphaType::kPremul,
kOverlayUsage, "RenderPassOverlay", gpu::kNullSurfaceHandle);
overlay_params.render_pass_backing = {
buffer_size,
/*generate_mipmap=*/false,
color_space,
RenderPassAlphaType::kPremul,
buffer_format,
mailbox,
/*is_root=*/false,
/*is_scanout=*/true,
/*scanout_dcomp_surface=*/false,
};
} else {
overlay_params = *it;
available_render_pass_overlay_backings_.erase(it);
}
// Add current rpdq to RenderPassOverlayParams.
overlay_params.render_pass_id = render_pass_id;
overlay_params.shared_quad_state.SetAll(*rpdq->shared_quad_state);
overlay_params.rpdq.SetAll(*rpdq);
if (const cc::FilterOperations* filters = FiltersForPass(render_pass_id);
filters) {
overlay_params.filters = *filters;
}
if (const cc::FilterOperations* backdrop_filters =
BackdropFiltersForPass(render_pass_id);
backdrop_filters) {
overlay_params.backdrop_filters = *backdrop_filters;
}
in_flight_render_pass_overlay_backings_.push_back(overlay_params);
return &in_flight_render_pass_overlay_backings_.back();
}
void SkiaRenderer::PrepareRenderPassOverlay(
OverlayProcessorInterface::PlatformOverlayCandidate* overlay) {
DCHECK(!current_canvas_);
DCHECK(batched_quads_.empty());
DCHECK(overlay->rpdq);
auto* const quad = overlay->rpdq.get();
// The |current_render_pass| could be used for calculating destination
// color space or clipping rect for backdrop filters. However
// the |current_render_pass| is nullptr during ScheduleOverlays(), since all
// overlay quads should be in the |root_render_pass|, before they are promoted
// to overlays, so set the |root_render_pass| to the |current_render_pass|.
base::AutoReset<raw_ptr<const AggregatedRenderPass>>
auto_reset_current_render_pass(¤t_frame()->current_render_pass,
current_frame()->root_render_pass);
auto* shared_quad_state =
const_cast<SharedQuadState*>(quad->shared_quad_state);
std::optional<gfx::Transform> quad_to_target_transform_inverse;
if (shared_quad_state->quad_to_target_transform.IsInvertible()) {
quad_to_target_transform_inverse.emplace();
// Flatten before inverting, since we're interested in how points
// with z=0 in local space map to the clip rect, not in how the clip
// rect at z=0 in device space maps to some other z in local space.
gfx::Transform flat_quad_to_target_transform(
shared_quad_state->quad_to_target_transform);
flat_quad_to_target_transform.Flatten();
quad_to_target_transform_inverse =
flat_quad_to_target_transform.GetCheckedInverse();
}
// The |clip_rect| is in the target coordinate space with all transforms
// (translation, scaling, rotation, etc), so remove them since we are
// later updating the quad-to-target transform to be the identity. These
// properties must stay in sync with the transform in case they are used
// in Calculate[RPDQ|DrawQuad]Params(), and so we can restrict the overlay
// backing size to just what is visible.
std::optional<base::AutoReset<std::optional<gfx::Rect>>> auto_reset_clip_rect;
if (quad_to_target_transform_inverse) {
// |output_rect| is also in the original target space and is the default
// clip that we should include since we have to manually apply that as
// well for overlay sizing.
gfx::RectF clip_rect(quad->shared_quad_state->clip_rect.value_or(
current_frame()->current_render_pass->output_rect));
// NOTE: If the quad to target transform has rotation, skew, or perspective,
// the modified clip rect might be expanded to ensure the quad image covers
// the original area when transformed by the overlay.
clip_rect = quad_to_target_transform_inverse->MapRect(clip_rect);
auto_reset_clip_rect.emplace(&shared_quad_state->clip_rect,
gfx::ToEnclosedRect(clip_rect));
} else {
// If we can't position the clip rect into render pass space, we shouldn't
// use it when rendering.
auto_reset_clip_rect.emplace(&shared_quad_state->clip_rect, std::nullopt);
}
// The |mask_filter_info| is in the device coordinate and with all transforms
// (translation, scaling, rotation, etc), so remove them.
std::optional<base::AutoReset<gfx::MaskFilterInfo>> auto_reset_mask_info;
if (!shared_quad_state->mask_filter_info.IsEmpty()) {
if (quad_to_target_transform_inverse) {
// Instantiate the auto reset with the original value so that
// ApplyTransform can modify it in place, but it will still be reset to
// the original value.
auto_reset_mask_info.emplace(&shared_quad_state->mask_filter_info,
shared_quad_state->mask_filter_info);
// NOTE: ApplyTransform only supports scale+translate transformations, if
// the quad has rotation, skew, or perspective, the mask filter will be
// discarded automatically since it can't be represented in the quad's
// local coordinate space.
shared_quad_state->mask_filter_info.ApplyTransform(
*quad_to_target_transform_inverse);
} else {
// If we can't position the mask info into the render pass space, we
// shouldn't use it when rendering.
auto_reset_mask_info.emplace(&shared_quad_state->mask_filter_info,
gfx::MaskFilterInfo());
}
}
const auto& viewport_size = current_frame()->device_viewport_size;
gfx::AxisTransform2d target_to_device = gfx::OrthoProjectionTransform(
/*left=*/0, /*right=*/viewport_size.width(), /*bottom=*/0,
/*top=*/viewport_size.height());
target_to_device.PostConcat(
gfx::WindowTransform(/*x=*/0, /*y=*/0, /*width=*/viewport_size.width(),
/*height=*/viewport_size.height()));
DrawQuadParams params;
DrawRPDQParams rpdq_params{gfx::RectF()};
{
// Reset |quad_to_target_transform|, so the quad will be rendered at the
// origin (0,0) without all transforms (translation, scaling, rotation, etc)
// and then we will use OS compositor to do those transforms.
base::AutoReset<gfx::Transform> auto_reset_transform(
&shared_quad_state->quad_to_target_transform, gfx::Transform());
// Use nullptr scissor, so we can always render the whole render pass in an
// overlay backing.
// TODO(penghuang): reusing overlay backing from previous frame to avoid
// reproducing the overlay backing if the render pass content quad
// properties and content are not changed.
params = CalculateDrawQuadParams(target_to_device,
/*scissor_rect=*/std::nullopt, quad,
/*draw_region=*/nullptr);
rpdq_params = CalculateRPDQParams(target_to_device, quad, ¶ms);
}
gfx::Rect filter_bounds =
gfx::SkIRectToRect(rpdq_params.filter_bounds.roundOut());
// Apply the clip that is normally handled indirectly via SkCanvas::clipRect.
// Since |shared_quad_state->quad_to_target_transform| and |clip_rect| were
// modified above, it is in the same coordinate space as |filter_bounds| now.
if (shared_quad_state->clip_rect) {
filter_bounds.Intersect(*shared_quad_state->clip_rect);
}
// If empty, the draw can be skipped
if (filter_bounds.IsEmpty()) {
return;
}
SharedImageFormat si_format;
gfx::ColorSpace color_space;
RenderPassBacking* src_quad_backing = nullptr;
auto bypass = render_pass_bypass_quads_.find(quad->render_pass_id);
BypassMode bypass_mode = BypassMode::kSkip;
// When Render Pass has a single quad inside we would draw that directly.
if (bypass != render_pass_bypass_quads_.end()) {
bypass_mode = CalculateBypassParams(bypass->second, &rpdq_params, ¶ms);
if (bypass_mode == BypassMode::kSkip) {
return;
}
// For bypassed render pass, we use the same format and color space for the
// framebuffer.
si_format = reshape_si_format();
color_space = reshape_color_space();
} else {
// A real render pass that was turned into an image
auto it = render_pass_backings_.find(quad->render_pass_id);
CHECK(render_pass_backings_.end() != it);
// This function is called after AllocateRenderPassResourceIfNeeded, so
// there should be backing ready.
src_quad_backing = &it->second;
si_format = src_quad_backing->format;
color_space = src_quad_backing->color_space;
}
// Adjust the overlay |buffer_size| to reduce memory fragmentation. It also
// increases buffer reusing possibilities.
constexpr int kBufferMultiple = 64;
gfx::Size buffer_size(
cc::MathUtil::CheckedRoundUp(filter_bounds.width(), kBufferMultiple),
cc::MathUtil::CheckedRoundUp(filter_bounds.height(), kBufferMultiple));
RenderPassOverlayParams* overlay_params = nullptr;
bool can_skip_render_pass =
CanSkipRenderPassOverlay(quad->render_pass_id, quad, &overlay_params);
if (!can_skip_render_pass) {
overlay_params = GetOrCreateRenderPassOverlayBacking(
quad->render_pass_id, quad, si_format, color_space, buffer_size);
}
DCHECK(overlay_params);
const RenderPassBacking& dst_overlay_backing =
overlay_params->render_pass_backing;
overlay->mailbox = dst_overlay_backing.mailbox;
overlay->resource_size_in_pixels = dst_overlay_backing.size;
if (!can_skip_render_pass) {
current_canvas_ = skia_output_surface_->BeginPaintRenderPass(
quad->render_pass_id, dst_overlay_backing.size,
dst_overlay_backing.format, dst_overlay_backing.alpha_type,
skgpu::Mipmapped::kNo, dst_overlay_backing.scanout_dcomp_surface,
RenderPassBackingSkColorSpace(dst_overlay_backing),
/*is_overlay=*/true, overlay->mailbox);
if (!current_canvas_) {
DLOG(ERROR)
<< "BeginPaintRenderPass() in PrepareRenderPassOverlay() failed.";
return;
}
current_canvas_->clear(SK_ColorTRANSPARENT);
// Adjust the |content_device_transform| to make sure filter extends are
// drawn inside of the buffer.
params.content_device_transform.Translate(-filter_bounds.x(),
-filter_bounds.y());
// Also adjust the |rounded_corner_bounds| to the new location.
if (params.mask_filter_info) {
params.mask_filter_info->ApplyTransform(params.content_device_transform);
}
// When Render Pass has a single quad inside we would draw that directly.
if (bypass != render_pass_bypass_quads_.end()) {
if (bypass_mode == BypassMode::kDrawTransparentQuad) {
DrawColoredQuad(SkColors::kTransparent, &rpdq_params, ¶ms);
} else if (bypass_mode == BypassMode::kDrawBypassQuad) {
DrawQuadInternal(bypass->second, &rpdq_params, ¶ms);
} else {
NOTREACHED();
}
} else {
DCHECK(src_quad_backing);
auto content_image =
skia_output_surface_->MakePromiseSkImageFromRenderPass(
quad->render_pass_id, src_quad_backing->size,
src_quad_backing->format, src_quad_backing->generate_mipmap,
RenderPassBackingSkColorSpace(*src_quad_backing),
src_quad_backing->mailbox);
if (!content_image) {
DLOG(ERROR) << "MakePromiseSkImageFromRenderPass() in "
"PrepareRenderPassOverlay() failed.";
EndPaint(gfx::Rect(dst_overlay_backing.size), /*failed=*/true,
/*is_overlay=*/true);
return;
}
if (src_quad_backing->generate_mipmap) {
params.sampling =
SkSamplingOptions(SkFilterMode::kLinear, SkMipmapMode::kLinear);
}
params.vis_tex_coords = cc::MathUtil::ScaleRectProportional(
quad->tex_coord_rect, gfx::RectF(quad->rect), params.visible_rect);
gfx::RectF valid_texel_bounds(content_image->width(),
content_image->height());
SkPaint paint = params.paint(GetContentColorFilter());
DrawSingleImage(content_image.get(), valid_texel_bounds, &rpdq_params,
&paint, ¶ms);
}
current_canvas_ = nullptr;
EndPaint(gfx::Rect(dst_overlay_backing.size), /*failed=*/false,
/*is_overlay=*/true);
}
#if BUILDFLAG(IS_APPLE)
// Adjust |bounds_rect| to contain the whole buffer and at the right location.
overlay->display_rect.set_origin(gfx::PointF(filter_bounds.origin()));
overlay->display_rect.set_size(gfx::SizeF(buffer_size));
#else // BUILDFLAG(IS_OZONE) || BUILDFLAG(IS_WIN)
// TODO(fangzhoug): Merge Ozone and Apple code paths of delegated compositing.
// Set |uv_rect| to reflect rounding up from |filter_bounds| to |buffer_size|.
overlay->uv_rect = gfx::RectF(filter_bounds.size());
overlay->uv_rect.InvScale(buffer_size.width(), buffer_size.height());
if (std::holds_alternative<gfx::OverlayTransform>(overlay->transform)) {
// When using an OverlayTransform, the transform should be baked into the
// display_rect.
overlay->display_rect =
quad->shared_quad_state->quad_to_target_transform.MapRect(
gfx::RectF(filter_bounds));
// Apply all clipping because we may not always delegate quads that extend
// beyond window bounds.
gfx::Rect apply_clip = gfx::Rect(current_frame()->device_viewport_size);
if (overlay->clip_rect.has_value()) {
apply_clip.Intersect(overlay->clip_rect.value());
}
OverlayCandidate::ApplyClip(*overlay, gfx::RectF(apply_clip));
overlay->clip_rect = std::nullopt;
} else {
overlay->display_rect = gfx::RectF(filter_bounds);
}
// Fill in |format| and |color_space| information based on selected backing.
overlay->color_space = color_space;
overlay->format = si_format;
#endif // BUILDFLAG(IS_APPLE)
}
#endif // BUILDFLAG(IS_APPLE) || BUILDFLAG(IS_OZONE) || BUILDFLAG(IS_WIN)
void SkiaRenderer::EndPaint(const gfx::Rect& update_rect,
bool failed,
bool is_overlay) {
base::OnceClosure on_finished_callback;
base::OnceCallback<void(gfx::GpuFenceHandle)> on_return_release_fence_cb;
// If SkiaRenderer has not failed, prepare callbacks and pass them to
// SkiaOutputSurface.
if (!failed) {
// Signal |current_frame_resource_fence_| when the root render pass is
// finished.
if (current_gpu_commands_completed_fence_->was_set()) {
on_finished_callback = base::BindPostTask(
base::SingleThreadTaskRunner::GetCurrentDefault(),
base::BindOnce(&FrameResourceGpuCommandsCompletedFence::Signal,
std::move(current_gpu_commands_completed_fence_)));
current_gpu_commands_completed_fence_ =
base::MakeRefCounted<FrameResourceGpuCommandsCompletedFence>(
resource_provider());
resource_provider()->SetGpuCommandsCompletedFence(
current_gpu_commands_completed_fence_.get());
}
// Return a release fence to the |current_release_fence_|
// when the root render pass is finished.
if (current_release_fence_->was_set()) {
on_return_release_fence_cb = base::BindPostTask(
base::SingleThreadTaskRunner::GetCurrentDefault(),
base::BindOnce(&FrameResourceReleaseFence::SetReleaseFenceCallback,
std::move(current_release_fence_)));
current_release_fence_ =
base::MakeRefCounted<FrameResourceReleaseFence>(resource_provider());
resource_provider()->SetReleaseFence(current_release_fence_.get());
}
}
skia_output_surface_->EndPaint(std::move(on_finished_callback),
std::move(on_return_release_fence_cb),
update_rect, is_overlay);
}
bool SkiaRenderer::IsRenderPassResourceAllocated(
const AggregatedRenderPassId& render_pass_id) const {
auto it = render_pass_backings_.find(render_pass_id);
return it != render_pass_backings_.end();
}
gfx::Size SkiaRenderer::GetRenderPassBackingPixelSize(
const AggregatedRenderPassId& render_pass_id) {
auto it = render_pass_backings_.find(render_pass_id);
CHECK(it != render_pass_backings_.end());
return it->second.size;
}
gfx::Rect SkiaRenderer::GetRenderPassBackingDrawnRect(
const AggregatedRenderPassId& render_pass_id) const {
if (auto it = render_pass_backings_.find(render_pass_id);
it != render_pass_backings_.end()) {
return it->second.drawn_rect;
} else {
// DirectRenderer can call this before it has allocated a render pass
// backing if this is the first contiguous frame we're seeing
// |render_pass_id|. This can happen because it calculates the render pass
// scissor rect before it actually allocates the backing.
return gfx::Rect();
}
}
void SkiaRenderer::SetRenderPassBackingDrawnRect(
const AggregatedRenderPassId& render_pass_id,
const gfx::Rect& drawn_rect) {
auto it = render_pass_backings_.find(render_pass_id);
CHECK(it != render_pass_backings_.end());
it->second.drawn_rect = drawn_rect;
return;
}
void SkiaRenderer::SetDelegatedInkPointRendererSkiaForTest(
std::unique_ptr<DelegatedInkPointRendererSkia> renderer) {
DCHECK(!delegated_ink_handler_);
delegated_ink_handler_ = std::make_unique<DelegatedInkHandler>(
output_surface_->capabilities().supports_delegated_ink);
delegated_ink_handler_->SetDelegatedInkPointRendererForTest(
std::move(renderer));
}
void SkiaRenderer::DrawDelegatedInkTrail(
const gfx::Transform& root_target_to_render_pass_transform) {
if (!delegated_ink_handler_ || !delegated_ink_handler_->GetInkRenderer())
return;
delegated_ink_handler_->GetInkRenderer()->DrawDelegatedInkTrail(
current_canvas_, root_target_to_render_pass_transform);
}
DelegatedInkPointRendererBase* SkiaRenderer::GetDelegatedInkPointRenderer(
bool create_if_necessary) {
if (!delegated_ink_handler_ && !create_if_necessary)
return nullptr;
if (!delegated_ink_handler_) {
delegated_ink_handler_ = std::make_unique<DelegatedInkHandler>(
output_surface_->capabilities().supports_delegated_ink);
}
return delegated_ink_handler_->GetInkRenderer();
}
bool SkiaRenderer::SupportsBGRA() const {
return skia_output_surface_->SupportsBGRA();
}
void SkiaRenderer::SetDelegatedInkMetadata(
std::unique_ptr<gfx::DelegatedInkMetadata> metadata) {
if (!delegated_ink_handler_) {
delegated_ink_handler_ = std::make_unique<DelegatedInkHandler>(
output_surface_->capabilities().supports_delegated_ink);
}
delegated_ink_handler_->SetDelegatedInkMetadata(std::move(metadata));
if (!UsingSkiaForDelegatedInk()) {
overlay_processor_->SetFrameHasDelegatedInk();
}
}
bool SkiaRenderer::UsingSkiaForDelegatedInk() const {
return delegated_ink_handler_ && delegated_ink_handler_->GetInkRenderer();
}
gfx::Rect SkiaRenderer::GetCurrentFramebufferDamage() const {
if (buffer_queue_) {
return buffer_queue_->CurrentBufferDamage();
} else {
return skia_output_surface_->GetCurrentFramebufferDamage();
}
}
void SkiaRenderer::Reshape(const OutputSurface::ReshapeParams& reshape_params) {
if (buffer_queue_) {
#if BUILDFLAG(IS_CHROMEOS)
// CrOS assumes that we (almost) never reallocate buffers, so we force
// |kPremul| to never trigger a reallocation due to root opacity changes.
const RenderPassAlphaType alpha_type = RenderPassAlphaType::kPremul;
#else
const RenderPassAlphaType alpha_type = reshape_params.alpha_type;
#endif
buffer_queue_->Reshape(reshape_params.size, reshape_params.color_space,
alpha_type, reshape_params.format);
}
// Even if we have our own BufferQueue, we still need to forward the Reshape()
// call down to the OutputPresenter.
skia_output_surface_->Reshape(reshape_params);
}
void SkiaRenderer::EnsureMinNumberOfBuffers(int n) {
CHECK(buffer_queue_);
buffer_queue_->EnsureMinNumberOfBuffers(n);
}
gpu::Mailbox SkiaRenderer::GetPrimaryPlaneOverlayTestingMailbox() {
#if BUILDFLAG(IS_WIN)
// Windows dcomp uses a swap chain for primary plane instead of BufferQueue.
return gpu::Mailbox();
#else
// For the purpose of testing the overlay configuration, the mailbox for ANY
// buffer from BufferQueue is good enough because they're all created with
// identical properties.
// At the time we're testing overlays we don't yet know which mailbox will be
// presented this frame so we'll just use the last swapped buffer. (We might
// present a new frame's mailbox, or if we empty-swap we'll present the
// previous frame's mailbox.)
CHECK(buffer_queue_);
return buffer_queue_->GetLastSwappedBuffer();
#endif
}
#if BUILDFLAG(IS_OZONE)
DBG_FLAG_FBOOL("delegated.overlay.background_candidate.colored",
toggle_background_overlay_color) // False by default.
void SkiaRenderer::MaybeScheduleBackgroundImage(
OverlayProcessorInterface::CandidateList& overlay_list) {
if (!output_surface_->capabilities().needs_background_image) {
return;
}
DCHECK(output_surface_->capabilities()
.supports_non_backed_solid_color_overlays ||
output_surface_->capabilities().supports_single_pixel_buffer);
OverlayCandidate background_candidate;
background_candidate.color_space = reshape_color_space();
background_candidate.display_rect =
gfx::RectF(gfx::SizeF(viewport_size_for_swap_buffers()));
background_candidate.color = toggle_background_overlay_color()
? SkColors::kRed
: SkColors::kTransparent;
background_candidate.plane_z_order = INT32_MIN;
// Mark the candidate as transparent. Otherwise it can cause visual
// artifacts, especially if the contents of viewport are opaque but have
// efects that makes portions of the content as transparent (like rounded
// corners). See b/334959107.
background_candidate.opacity =
toggle_background_overlay_color() ? 1.0f : 0.0f;
// ScheduleOverlays() will convert this to a buffer-backed solid color overlay
// if necessary.
background_candidate.is_solid_color = true;
if (overlay_processor_) {
background_candidate.damage_rect =
overlay_processor_->GetUnassignedDamage();
DBG_DRAW_RECT("damage_not_assigned", background_candidate.damage_rect);
}
overlay_list.push_back(background_candidate);
}
#endif // BUILDFLAG(IS_OZONE)
#if BUILDFLAG(IS_APPLE) || BUILDFLAG(IS_OZONE) || BUILDFLAG(IS_WIN)
SkiaRenderer::ScopedInFlightRenderPassOverlayBackingRef::
ScopedInFlightRenderPassOverlayBackingRef(SkiaRenderer* renderer,
const gpu::Mailbox& mailbox)
: renderer_(renderer), mailbox_(mailbox) {
CHECK(renderer_);
CHECK(!mailbox_.IsZero());
auto it =
std::ranges::find(renderer_->in_flight_render_pass_overlay_backings_,
mailbox_, [](const RenderPassOverlayParams& overlay) {
return overlay.render_pass_backing.mailbox;
});
CHECK(it != renderer_->in_flight_render_pass_overlay_backings_.end());
it->ref_count++;
}
void SkiaRenderer::ScopedInFlightRenderPassOverlayBackingRef::Reset() {
if (!renderer_ && mailbox_.IsZero()) {
return;
}
auto it =
std::ranges::find(renderer_->in_flight_render_pass_overlay_backings_,
mailbox_, [](const RenderPassOverlayParams& overlay) {
return overlay.render_pass_backing.mailbox;
});
CHECK(it != renderer_->in_flight_render_pass_overlay_backings_.end());
// Render pass overlay backings can be reused across multiple frames so we
// only want to mark them as available when we're releasing lock holding the
// last reference.
CHECK_GT(it->ref_count, 0);
it->ref_count--;
if (it->ref_count == 0) {
renderer_->available_render_pass_overlay_backings_.push_back(*it);
renderer_->in_flight_render_pass_overlay_backings_.erase(it);
}
}
SkiaRenderer::ScopedInFlightRenderPassOverlayBackingRef::
~ScopedInFlightRenderPassOverlayBackingRef() {
Reset();
}
SkiaRenderer::ScopedInFlightRenderPassOverlayBackingRef::
ScopedInFlightRenderPassOverlayBackingRef(
SkiaRenderer::ScopedInFlightRenderPassOverlayBackingRef&& other) {
*this = std::move(other);
}
SkiaRenderer::ScopedInFlightRenderPassOverlayBackingRef&
SkiaRenderer::ScopedInFlightRenderPassOverlayBackingRef::
ScopedInFlightRenderPassOverlayBackingRef::operator=(
SkiaRenderer::ScopedInFlightRenderPassOverlayBackingRef&& other) {
Reset();
// This is an RAII type so we depend on the move operators to clear the
// |other| binding so we don't to unintentional work in the dtor. We need to
// manually implement the move operators since neither |raw_ptr| nor
// |gpu::Mailbox| guarantees a move operation that default initializes the
// original binding.
renderer_ = other.renderer_;
mailbox_ = other.mailbox_;
other.renderer_ = nullptr;
other.mailbox_ = gpu::Mailbox();
return *this;
}
#endif // BUILDFLAG(IS_APPLE) || BUILDFLAG(IS_OZONE) || BUILDFLAG(IS_WIN)
SkiaRenderer::OverlayLock::OverlayLock(
DisplayResourceProvider* resource_provider,
ResourceId resource_id) {
resource_lock.emplace(resource_provider, resource_id);
}
SkiaRenderer::OverlayLock::~OverlayLock() = default;
SkiaRenderer::OverlayLock::OverlayLock(SkiaRenderer::OverlayLock&& other) {
resource_lock = std::move(other.resource_lock);
#if BUILDFLAG(IS_APPLE) || BUILDFLAG(IS_OZONE) || BUILDFLAG(IS_WIN)
render_pass_lock = std::move(other.render_pass_lock);
#endif // BUILDFLAG(IS_APPLE) || BUILDFLAG(IS_OZONE) || BUILDFLAG(IS_WIN)
}
SkiaRenderer::OverlayLock& SkiaRenderer::OverlayLock::OverlayLock::operator=(
SkiaRenderer::OverlayLock&& other) {
resource_lock = std::move(other.resource_lock);
#if BUILDFLAG(IS_APPLE) || BUILDFLAG(IS_OZONE) || BUILDFLAG(IS_WIN)
render_pass_lock = std::move(other.render_pass_lock);
#endif // BUILDFLAG(IS_APPLE) || BUILDFLAG(IS_OZONE) || BUILDFLAG(IS_WIN)
return *this;
}
#if BUILDFLAG(IS_APPLE) || BUILDFLAG(IS_OZONE) || BUILDFLAG(IS_WIN)
SkiaRenderer::OverlayLock::OverlayLock(SkiaRenderer* renderer,
const gpu::Mailbox& mailbox) {
render_pass_lock.emplace(renderer, mailbox);
}
#endif // BUILDFLAG(IS_APPLE) || BUILDFLAG(IS_OZONE) || BUILDFLAG(IS_WIN)
#if BUILDFLAG(IS_APPLE)
std::size_t SkiaRenderer::OverlayLockHash::operator()(
const OverlayLock& o) const {
return std::hash<gpu::Mailbox>{}(o.mailbox());
}
std::size_t SkiaRenderer::OverlayLockHash::operator()(
const gpu::Mailbox& m) const {
return std::hash<gpu::Mailbox>{}(m);
}
bool SkiaRenderer::OverlayLockKeyEqual::operator()(
const OverlayLock& lhs,
const OverlayLock& rhs) const {
return lhs.mailbox() == rhs.mailbox();
}
bool SkiaRenderer::OverlayLockKeyEqual::operator()(
const OverlayLock& lhs,
const gpu::Mailbox& rhs) const {
return lhs.mailbox() == rhs;
}
#endif
} // namespace viz
|