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
|
// Copyright 2011 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/390223051): Remove C-library calls to fix the errors.
#pragma allow_unsafe_libc_calls
#endif
#include "cc/scheduler/scheduler.h"
#include <stddef.h>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "base/auto_reset.h"
#include "base/check_op.h"
#include "base/functional/bind.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/notreached.h"
#include "base/numerics/safe_conversions.h"
#include "base/run_loop.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/test_mock_time_task_runner.h"
#include "base/time/time.h"
#include "base/time/time_override.h"
#include "base/trace_event/trace_event.h"
#include "cc/base/features.h"
#include "cc/metrics/begin_main_frame_metrics.h"
#include "cc/metrics/event_metrics.h"
#include "cc/metrics/frame_sequence_tracker_collection.h"
#include "cc/test/fake_compositor_frame_reporting_controller.h"
#include "cc/test/scheduler_test_common.h"
#include "components/viz/common/frame_sinks/begin_frame_args.h"
#include "components/viz/test/begin_frame_args_test.h"
#include "components/viz/test/fake_delay_based_time_source.h"
#include "components/viz/test/fake_external_begin_frame_source.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#define EXPECT_ACTIONS(...) \
EXPECT_THAT(client_->Actions(), ::testing::ElementsAre(__VA_ARGS__))
#define EXPECT_NO_ACTION() EXPECT_THAT(client_->Actions(), ::testing::IsEmpty())
#define EXPECT_SCOPED(statements) \
{ \
SCOPED_TRACE(""); \
statements; \
}
namespace cc {
namespace {
base::TimeDelta kSlowDuration = base::Seconds(1);
base::TimeDelta kFastDuration = base::Milliseconds(1);
class FakeSchedulerClient : public SchedulerClient,
public viz::FakeExternalBeginFrameSource::Client {
public:
FakeSchedulerClient() { Reset(); }
void Reset() {
actions_.clear();
will_begin_impl_frame_causes_redraw_ = false;
will_begin_impl_frame_requests_one_begin_impl_frame_ = false;
invalidate_needs_redraw_ = true;
draw_will_happen_ = true;
swap_will_happen_if_draw_happens_ = true;
num_draws_ = 0;
last_begin_main_frame_args_ = viz::BeginFrameArgs();
last_begin_frame_ack_ = viz::BeginFrameAck();
last_frame_skipped_reason_.reset();
}
void set_scheduler(TestScheduler* scheduler) { scheduler_ = scheduler; }
bool needs_begin_frames() { return scheduler_->begin_frames_expected(); }
int num_draws() const { return num_draws_; }
bool invalidate_needs_redraw() const { return invalidate_needs_redraw_; }
const std::vector<std::string> Actions() const {
return std::vector<std::string>(actions_.begin(), actions_.end());
}
base::TimeTicks posted_begin_impl_frame_deadline() const {
return posted_begin_impl_frame_deadline_;
}
base::TimeDelta frame_interval() const { return frame_interval_; }
int ActionIndex(const char* action) const {
for (size_t i = 0; i < actions_.size(); i++)
if (!strcmp(actions_[i], action))
return base::checked_cast<int>(i);
return -1;
}
bool HasAction(const char* action) const { return ActionIndex(action) >= 0; }
void SetWillBeginImplFrameRequestsOneBeginImplFrame(bool request) {
will_begin_impl_frame_requests_one_begin_impl_frame_ = request;
}
void SetWillBeginImplFrameCausesRedraw(bool causes_redraw) {
will_begin_impl_frame_causes_redraw_ = causes_redraw;
}
void SetInvalidateNeedsRedraw(bool needs_redraw) {
invalidate_needs_redraw_ = needs_redraw;
}
void SetDrawWillHappen(bool draw_will_happen) {
draw_will_happen_ = draw_will_happen;
}
void SetSwapWillHappenIfDrawHappens(bool swap_will_happen_if_draw_happens) {
swap_will_happen_if_draw_happens_ = swap_will_happen_if_draw_happens;
}
void SetAutomaticSubmitCompositorFrameAck(bool automatic_ack) {
automatic_ack_ = automatic_ack;
}
void SetWillBeginImplFrameMightHaveDamage(bool might_have_damage) {
will_begin_impl_frame_might_have_damage_ = might_have_damage;
}
// SchedulerClient implementation.
bool WillBeginImplFrame(const viz::BeginFrameArgs& args) override {
EXPECT_FALSE(inside_begin_impl_frame_);
EXPECT_FALSE(inside_action_);
base::AutoReset<bool> mark_inside(&inside_action_, true);
inside_begin_impl_frame_ = true;
PushAction("WillBeginImplFrame");
if (will_begin_impl_frame_requests_one_begin_impl_frame_)
scheduler_->SetNeedsOneBeginImplFrame();
if (will_begin_impl_frame_causes_redraw_)
scheduler_->SetNeedsRedraw();
return will_begin_impl_frame_might_have_damage_;
}
void DidFinishImplFrame(
const viz::BeginFrameArgs& last_activated_args) override {
EXPECT_TRUE(inside_begin_impl_frame_);
EXPECT_FALSE(inside_action_);
base::AutoReset<bool> mark_inside(&inside_action_, true);
inside_begin_impl_frame_ = false;
}
void DidNotProduceFrame(const viz::BeginFrameAck& ack,
FrameSkippedReason reason) override {
EXPECT_FALSE(inside_action_);
base::AutoReset<bool> mark_inside(&inside_action_, true);
last_begin_frame_ack_ = ack;
last_frame_skipped_reason_ = reason;
}
void WillNotReceiveBeginFrame() override {}
void ScheduledActionSendBeginMainFrame(
const viz::BeginFrameArgs& args) override {
EXPECT_FALSE(inside_action_);
base::AutoReset<bool> mark_inside(&inside_action_, true);
PushAction("ScheduledActionSendBeginMainFrame");
last_begin_main_frame_args_ = args;
}
void FrameIntervalUpdated(base::TimeDelta interval) override {
frame_interval_ = interval;
}
void OnBeginImplFrameDeadline() override {}
const viz::BeginFrameArgs& last_begin_main_frame_args() {
return last_begin_main_frame_args_;
}
const viz::BeginFrameAck& last_begin_frame_ack() {
return last_begin_frame_ack_;
}
FrameSkippedReason last_frame_skipped_reason() const {
return last_frame_skipped_reason_.value();
}
DrawResult ScheduledActionDrawIfPossible() override {
EXPECT_FALSE(inside_action_);
base::AutoReset<bool> mark_inside(&inside_action_, true);
PushAction("ScheduledActionDrawIfPossible");
num_draws_++;
if (!draw_will_happen_)
return DrawResult::kAbortedCheckerboardAnimations;
if (swap_will_happen_if_draw_happens_) {
last_begin_frame_ack_ = scheduler_->CurrentBeginFrameAckForActiveTree();
SubmitInfo submit_info;
scheduler_->DidSubmitCompositorFrame(submit_info);
if (automatic_ack_)
scheduler_->DidReceiveCompositorFrameAck();
}
return DrawResult::kSuccess;
}
DrawResult ScheduledActionDrawForced() override {
EXPECT_FALSE(inside_action_);
base::AutoReset<bool> mark_inside(&inside_action_, true);
PushAction("ScheduledActionDrawForced");
last_begin_frame_ack_ = scheduler_->CurrentBeginFrameAckForActiveTree();
return DrawResult::kSuccess;
}
void ScheduledActionCommit() override {
EXPECT_FALSE(inside_action_);
base::AutoReset<bool> mark_inside(&inside_action_, true);
PushAction("ScheduledActionCommit");
}
void ScheduledActionPostCommit() override {
EXPECT_FALSE(inside_action_);
base::AutoReset<bool> mark_inside(&inside_action_, true);
PushAction("ScheduledActionPostCommit");
}
void ScheduledActionActivateSyncTree() override {
EXPECT_FALSE(inside_action_);
base::AutoReset<bool> mark_inside(&inside_action_, true);
PushAction("ScheduledActionActivateSyncTree");
}
void ScheduledActionBeginLayerTreeFrameSinkCreation() override {
EXPECT_FALSE(inside_action_);
base::AutoReset<bool> mark_inside(&inside_action_, true);
PushAction("ScheduledActionBeginLayerTreeFrameSinkCreation");
}
void ScheduledActionPrepareTiles() override {
EXPECT_FALSE(inside_action_);
base::AutoReset<bool> mark_inside(&inside_action_, true);
PushAction("ScheduledActionPrepareTiles");
scheduler_->DidPrepareTiles();
}
void ScheduledActionInvalidateLayerTreeFrameSink(bool needs_redraw) override {
EXPECT_FALSE(inside_action_);
base::AutoReset<bool> mark_inside(&inside_action_, true);
invalidate_needs_redraw_ = needs_redraw;
actions_.push_back("ScheduledActionInvalidateLayerTreeFrameSink");
}
void ScheduledActionPerformImplSideInvalidation() override {
EXPECT_FALSE(inside_action_);
base::AutoReset<bool> mark_inside(&inside_action_, true);
PushAction("ScheduledActionPerformImplSideInvalidation");
}
void SendBeginMainFrameNotExpectedSoon() override {
EXPECT_FALSE(inside_action_);
base::AutoReset<bool> mark_inside(&inside_action_, true);
PushAction("SendBeginMainFrameNotExpectedSoon");
}
void ScheduledActionBeginMainFrameNotExpectedUntil(
base::TimeTicks time) override {
EXPECT_FALSE(inside_action_);
base::AutoReset<bool> mark_inside(&inside_action_, true);
PushAction("ScheduledActionBeginMainFrameNotExpectedUntil");
}
bool IsInsideBeginImplFrame() const { return inside_begin_impl_frame_; }
base::RepeatingCallback<bool(void)> InsideBeginImplFrame(bool state) {
return base::BindRepeating(
&FakeSchedulerClient::InsideBeginImplFrameCallback,
base::Unretained(this), state);
}
bool IsCurrentFrame(int last_frame_number) const {
return scheduler_->current_frame_number() == last_frame_number;
}
base::RepeatingCallback<bool(void)> FrameHasNotAdvancedCallback() {
return base::BindRepeating(&FakeSchedulerClient::IsCurrentFrame,
base::Unretained(this),
scheduler_->current_frame_number());
}
void PushAction(const char* description) {
actions_.push_back(description);
}
// FakeExternalBeginFrameSource::Client implementation.
void OnAddObserver(viz::BeginFrameObserver* obs) override {
PushAction("AddObserver(this)");
}
void OnRemoveObserver(viz::BeginFrameObserver* obs) override {
PushAction("RemoveObserver(this)");
}
protected:
bool InsideBeginImplFrameCallback(bool state) {
return inside_begin_impl_frame_ == state;
}
bool inside_action_ = false;
bool inside_begin_impl_frame_ = false;
bool will_begin_impl_frame_causes_redraw_;
bool will_begin_impl_frame_requests_one_begin_impl_frame_;
bool invalidate_needs_redraw_ = true;
bool draw_will_happen_;
bool swap_will_happen_if_draw_happens_;
bool automatic_ack_ = true;
bool will_begin_impl_frame_might_have_damage_ = true;
int num_draws_;
viz::BeginFrameArgs last_begin_main_frame_args_;
viz::BeginFrameAck last_begin_frame_ack_;
base::TimeTicks posted_begin_impl_frame_deadline_;
std::vector<const char*> actions_;
raw_ptr<TestScheduler> scheduler_ = nullptr;
base::TimeDelta frame_interval_;
std::optional<FrameSkippedReason> last_frame_skipped_reason_;
};
enum BeginFrameSourceType {
EXTERNAL_BFS,
UNTHROTTLED_BFS,
THROTTLED_BFS,
};
class SchedulerTestTaskRunner : public base::TestMockTimeTaskRunner {
public:
SchedulerTestTaskRunner()
: base::TestMockTimeTaskRunner(
base::TestMockTimeTaskRunner::Type::kStandalone) {
AdvanceMockTickClock(base::Microseconds(110000));
}
void RunUntilTime(base::TimeTicks end_time) {
FastForwardBy(end_time - NowTicks());
}
// Runs all tasks posted before this call.
void RunPendingTasks() {
base::circular_deque<base::TestPendingTask> tasks = TakePendingTasks();
while (!tasks.empty()) {
base::TestPendingTask task = std::move(tasks.front());
tasks.pop_front();
// Set clock to the beginning of task and run it.
AdvanceMockTickClock(task.GetTimeToRun() - NowTicks());
std::move(task.task).Run();
}
}
// Runs tasks while condition is met.
// Condition is being checked when task exists and before it gets selected.
void RunTasksWhile(base::RepeatingCallback<bool()> condition) {
run_condition_ = condition;
FastForwardUntilNoTasksRemain();
run_condition_ = std::nullopt;
// We've moved all the pending tasks away to break the execution loop,
// now we should restore them.
while (!tasks_to_requeue_.empty()) {
base::TestPendingTask task = std::move(tasks_to_requeue_.front());
tasks_to_requeue_.pop_front();
PostDelayedTask(task.location, std::move(task.task),
task.GetTimeToRun() - NowTicks());
}
}
protected:
void OnBeforeSelectingTask() override {
// Avoid potential infinite loops.
ASSERT_LT(++task_count_, 100u);
if (run_condition_ && HasPendingTask() && !run_condition_->Run()) {
// Execution will not continue because we move all the pending tasks away.
tasks_to_requeue_ = TakePendingTasks();
}
}
private:
~SchedulerTestTaskRunner() override = default; // Ref-counted.
size_t task_count_ = 0u;
std::optional<base::RepeatingCallback<bool()>> run_condition_;
base::circular_deque<base::TestPendingTask> tasks_to_requeue_;
};
class SchedulerTest : public testing::Test {
public:
SchedulerTest()
: task_runner_(base::MakeRefCounted<SchedulerTestTaskRunner>()),
fake_external_begin_frame_source_(nullptr),
tracker_collection_(false) {}
~SchedulerTest() override { client_->set_scheduler(nullptr); }
protected:
TestScheduler* CreateScheduler(BeginFrameSourceType bfs_type) {
viz::BeginFrameSource* frame_source = nullptr;
unthrottled_frame_source_ =
std::make_unique<viz::BackToBackBeginFrameSource>(
std::make_unique<viz::FakeDelayBasedTimeSource>(
task_runner_->GetMockTickClock(), task_runner_.get()));
fake_external_begin_frame_source_ =
std::make_unique<viz::FakeExternalBeginFrameSource>(1.0, false);
fake_external_begin_frame_source_->SetClient(client_.get());
synthetic_frame_source_ = std::make_unique<viz::DelayBasedBeginFrameSource>(
std::make_unique<viz::FakeDelayBasedTimeSource>(
task_runner_->GetMockTickClock(), task_runner_.get()),
viz::BeginFrameSource::kNotRestartableId);
switch (bfs_type) {
case EXTERNAL_BFS:
frame_source = fake_external_begin_frame_source_.get();
break;
case UNTHROTTLED_BFS:
frame_source = unthrottled_frame_source_.get();
break;
case THROTTLED_BFS:
frame_source = synthetic_frame_source_.get();
break;
}
DCHECK(frame_source);
std::unique_ptr<FakeCompositorTimingHistory>
fake_compositor_timing_history = FakeCompositorTimingHistory::Create(
scheduler_settings_.using_synchronous_renderer_compositor);
fake_compositor_timing_history_ = fake_compositor_timing_history.get();
reporting_controller =
std::make_unique<FakeCompositorFrameReportingController>();
reporting_controller->SetFrameSorter(&frame_sorter);
reporting_controller->SetFrameSequenceTrackerCollection(
&tracker_collection_);
scheduler_ = std::make_unique<TestScheduler>(
task_runner_->GetMockTickClock(), client_.get(), scheduler_settings_, 0,
task_runner_.get(), std::move(fake_compositor_timing_history),
reporting_controller.get());
client_->set_scheduler(scheduler_.get());
scheduler_->SetBeginFrameSource(frame_source);
return scheduler_.get();
}
void SetUpScheduler(BeginFrameSourceType bfs_type,
std::unique_ptr<FakeSchedulerClient> client) {
client_ = std::move(client);
CreateScheduler(bfs_type);
EXPECT_SCOPED(InitializeLayerTreeFrameSinkAndFirstCommit());
}
void SetUpScheduler(BeginFrameSourceType bfs_type) {
SetUpScheduler(bfs_type, std::make_unique<FakeSchedulerClient>());
}
void SetUpSchedulerWithNoLayerTreeFrameSink(BeginFrameSourceType bfs_type) {
client_ = std::make_unique<FakeSchedulerClient>();
CreateScheduler(bfs_type);
}
// As this function contains EXPECT macros, to allow debugging it should be
// called inside EXPECT_SCOPED like so;
// EXPECT_SCOPED(
// client.InitializeLayerTreeFrameSinkAndFirstCommit(scheduler));
void InitializeLayerTreeFrameSinkAndFirstCommit() {
TRACE_EVENT0(
"cc", "SchedulerUnitTest::InitializeLayerTreeFrameSinkAndFirstCommit");
DCHECK(scheduler_);
// Check the client doesn't have any actions queued when calling this
// function.
EXPECT_NO_ACTION();
EXPECT_FALSE(scheduler_->begin_frames_expected());
// Start the initial LayerTreeFrameSink creation.
scheduler_->SetVisible(true);
scheduler_->SetCanDraw(true);
EXPECT_ACTIONS("ScheduledActionBeginLayerTreeFrameSinkCreation");
client_->Reset();
// We don't see anything happening until the first impl frame.
scheduler_->DidCreateAndInitializeLayerTreeFrameSink();
scheduler_->SetNeedsBeginMainFrame();
EXPECT_TRUE(scheduler_->begin_frames_expected());
EXPECT_FALSE(client_->IsInsideBeginImplFrame());
client_->Reset();
{
SCOPED_TRACE("Do first frame to commit after initialize.");
AdvanceFrame();
task_runner_->AdvanceMockTickClock(base::Milliseconds(1));
scheduler_->NotifyBeginMainFrameStarted(task_runner_->NowTicks());
scheduler_->NotifyReadyToCommit(nullptr);
scheduler_->NotifyReadyToActivate();
scheduler_->NotifyReadyToDraw();
EXPECT_FALSE(scheduler_->CommitPending());
if (scheduler_settings_.using_synchronous_renderer_compositor) {
scheduler_->SetNeedsRedraw();
bool resourceless_software_draw = false;
bool skip_draw = false;
scheduler_->OnDrawForLayerTreeFrameSink(resourceless_software_draw,
skip_draw);
} else {
// Run the posted deadline task.
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
task_runner_->RunTasksWhile(client_->InsideBeginImplFrame(true));
}
EXPECT_FALSE(client_->IsInsideBeginImplFrame());
}
client_->Reset();
{
SCOPED_TRACE(
"Run second frame so Scheduler calls SetNeedsBeginFrame(false).");
AdvanceFrame();
if (!scheduler_settings_.using_synchronous_renderer_compositor) {
// Run the posted deadline task.
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
task_runner_->RunTasksWhile(client_->InsideBeginImplFrame(true));
}
EXPECT_FALSE(client_->IsInsideBeginImplFrame());
}
EXPECT_FALSE(scheduler_->begin_frames_expected());
if (scheduler_->begin_frame_source() ==
fake_external_begin_frame_source_.get()) {
// Expect the last viz::BeginFrameAck to be for last BeginFrame, which
// didn't cause damage.
uint64_t last_begin_frame_number =
fake_external_begin_frame_source_->next_begin_frame_number() - 1;
bool has_damage = false;
EXPECT_EQ(
viz::BeginFrameAck(fake_external_begin_frame_source_->source_id(),
last_begin_frame_number, has_damage),
client_->last_begin_frame_ack());
}
client_->Reset();
}
// As this function contains EXPECT macros, to allow debugging it should be
// called inside EXPECT_SCOPED like so;
// EXPECT_SCOPED(client.AdvanceFrame());
void AdvanceFrame(bool animate_only = false) {
TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("cc.debug.scheduler.frames"),
"FakeSchedulerClient::AdvanceFrame");
// Send the next BeginFrame message if using an external source, otherwise
// it will be already in the task queue.
if (scheduler_->begin_frame_source() ==
fake_external_begin_frame_source_.get()) {
EXPECT_TRUE(scheduler_->begin_frames_expected());
// Run the deadline first if we're inside the previous frame.
if (client_->IsInsideBeginImplFrame())
task_runner_->RunPendingTasks();
SendNextBeginFrame(animate_only);
} else {
task_runner_->RunTasksWhile(client_->FrameHasNotAdvancedCallback());
}
}
viz::BeginFrameArgs SendNextBeginFrame(bool animate_only = false) {
DCHECK_EQ(scheduler_->begin_frame_source(),
fake_external_begin_frame_source_.get());
// Creep the time forward so that any viz::BeginFrameArgs is not equal to
// the last one otherwise we violate the viz::BeginFrameSource contract.
task_runner_->AdvanceMockTickClock(viz::BeginFrameArgs::DefaultInterval());
viz::BeginFrameArgs args =
fake_external_begin_frame_source_->CreateBeginFrameArgs(
BEGINFRAME_FROM_HERE, task_runner_->GetMockTickClock());
args.animate_only = animate_only;
fake_external_begin_frame_source_->TestOnBeginFrame(args);
return args;
}
viz::FakeExternalBeginFrameSource* fake_external_begin_frame_source() const {
return fake_external_begin_frame_source_.get();
}
void SetShouldDeferInvalidationForMainFrame(bool defer) {
// Set the CompositorTimingHistory so that main thread is identified to be
// fast or slow.
base::TimeDelta delta;
if (!defer)
delta = base::Seconds(1);
fake_compositor_timing_history_
->SetBeginMainFrameStartToReadyToCommitDurationEstimate(delta);
fake_compositor_timing_history_
->SetBeginMainFrameQueueDurationCriticalEstimate(delta);
fake_compositor_timing_history_
->SetBeginMainFrameQueueDurationNotCriticalEstimate(delta);
fake_compositor_timing_history_->SetDrawDurationEstimate(base::TimeDelta());
}
void SendTestBeginFrameAfterInterval(base::TimeDelta interval,
uint64_t source_id,
uint64_t sequence_number) {
scheduler_->SetNeedsRedraw();
task_runner_->AdvanceMockTickClock(interval);
viz::BeginFrameArgs args = viz::BeginFrameArgs::Create(
BEGINFRAME_FROM_HERE, source_id, sequence_number,
task_runner_->NowTicks(), task_runner_->NowTicks() + interval, interval,
viz::BeginFrameArgs::NORMAL);
fake_external_begin_frame_source_->TestOnBeginFrame(args);
EXPECT_EQ(client_->frame_interval(), interval);
}
void AdvanceAndMissOneFrame();
void CheckMainFrameNotSkippedAfterLateCommit();
void ImplFrameNotSkippedAfterLateAck();
void BeginFramesNotFromClient(BeginFrameSourceType bfs_type);
void BeginFramesNotFromClient_IsDrawThrottled(BeginFrameSourceType bfs_type);
bool BeginMainFrameOnCriticalPath(TreePriority tree_priority,
ScrollHandlerState scroll_handler_state,
base::TimeDelta durations);
scoped_refptr<SchedulerTestTaskRunner> task_runner_;
std::unique_ptr<viz::FakeExternalBeginFrameSource>
fake_external_begin_frame_source_;
std::unique_ptr<viz::SyntheticBeginFrameSource> synthetic_frame_source_;
std::unique_ptr<viz::SyntheticBeginFrameSource> unthrottled_frame_source_;
SchedulerSettings scheduler_settings_;
std::unique_ptr<FakeSchedulerClient> client_;
std::unique_ptr<TestScheduler> scheduler_;
raw_ptr<FakeCompositorTimingHistory> fake_compositor_timing_history_;
FrameSequenceTrackerCollection tracker_collection_;
FrameSorter frame_sorter;
// Since CFRC destructor cleans up the FrameSorter's
// registered observers (in this case FSTC)
// it needs to be declared last so that it will be
// cleaned up first.
std::unique_ptr<CompositorFrameReportingController> reporting_controller;
};
TEST_F(SchedulerTest, InitializeLayerTreeFrameSinkDoesNotBeginImplFrame) {
SetUpSchedulerWithNoLayerTreeFrameSink(EXTERNAL_BFS);
scheduler_->SetVisible(true);
scheduler_->SetCanDraw(true);
EXPECT_ACTIONS("ScheduledActionBeginLayerTreeFrameSinkCreation");
client_->Reset();
scheduler_->DidCreateAndInitializeLayerTreeFrameSink();
EXPECT_NO_ACTION();
}
TEST_F(SchedulerTest, Stop) {
SetUpScheduler(EXTERNAL_BFS);
scheduler_->SetNeedsBeginMainFrame();
EXPECT_ACTIONS("AddObserver(this)");
client_->Reset();
// No scheduled actions are performed after Stop. WillBeginImplFrame is only
// a notification and not an action performed by the scheduler.
scheduler_->Stop();
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame");
client_->Reset();
}
TEST_F(SchedulerTest, VideoNeedsBeginFrames) {
SetUpScheduler(EXTERNAL_BFS);
scheduler_->SetVideoNeedsBeginFrames(true);
EXPECT_ACTIONS("AddObserver(this)");
EXPECT_TRUE(scheduler_->begin_frames_expected());
client_->Reset();
EXPECT_SCOPED(AdvanceFrame());
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
// WillBeginImplFrame is responsible for sending BeginFrames to video.
EXPECT_ACTIONS("WillBeginImplFrame");
client_->Reset();
EXPECT_SCOPED(AdvanceFrame());
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
EXPECT_ACTIONS("WillBeginImplFrame");
client_->Reset();
scheduler_->SetVideoNeedsBeginFrames(false);
EXPECT_NO_ACTION();
client_->Reset();
task_runner_->RunTasksWhile(client_->InsideBeginImplFrame(true));
EXPECT_FALSE(client_->IsInsideBeginImplFrame());
EXPECT_ACTIONS("RemoveObserver(this)");
EXPECT_FALSE(scheduler_->begin_frames_expected());
}
TEST_F(SchedulerTest, RequestCommit) {
SetUpScheduler(EXTERNAL_BFS);
// SetNeedsBeginMainFrame should begin the frame on the next BeginImplFrame.
scheduler_->SetNeedsBeginMainFrame();
EXPECT_ACTIONS("AddObserver(this)");
client_->Reset();
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame", "ScheduledActionSendBeginMainFrame");
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
EXPECT_TRUE(scheduler_->begin_frames_expected());
client_->Reset();
// If we don't swap on the deadline, we wait for the next BeginFrame.
task_runner_->RunPendingTasks(); // Run posted deadline.
EXPECT_NO_ACTION();
EXPECT_FALSE(client_->IsInsideBeginImplFrame());
EXPECT_TRUE(scheduler_->begin_frames_expected());
client_->Reset();
// NotifyReadyToCommit should trigger the commit.
scheduler_->NotifyBeginMainFrameStarted(task_runner_->NowTicks());
scheduler_->NotifyReadyToCommit(nullptr);
EXPECT_ACTIONS("ScheduledActionCommit", "ScheduledActionPostCommit");
EXPECT_TRUE(scheduler_->begin_frames_expected());
client_->Reset();
// NotifyReadyToActivate should trigger the activation.
scheduler_->NotifyReadyToActivate();
EXPECT_ACTIONS("ScheduledActionActivateSyncTree");
EXPECT_TRUE(scheduler_->begin_frames_expected());
client_->Reset();
// BeginImplFrame should prepare the draw.
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame");
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
EXPECT_TRUE(scheduler_->begin_frames_expected());
client_->Reset();
// BeginImplFrame deadline should draw.
task_runner_->RunPendingTasks(); // Run posted deadline.
EXPECT_ACTIONS("ScheduledActionDrawIfPossible");
EXPECT_FALSE(client_->IsInsideBeginImplFrame());
EXPECT_TRUE(scheduler_->begin_frames_expected());
client_->Reset();
// The following BeginImplFrame deadline should SetNeedsBeginFrame(false)
// to avoid excessive toggles.
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame");
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
client_->Reset();
task_runner_->RunPendingTasks(); // Run posted deadline.
EXPECT_ACTIONS("RemoveObserver(this)");
client_->Reset();
}
TEST_F(SchedulerTest, RequestCommitAfterSetDeferBeginMainFrame) {
SetUpScheduler(EXTERNAL_BFS);
scheduler_->SetDeferBeginMainFrame(true);
scheduler_->SetNeedsBeginMainFrame();
EXPECT_NO_ACTION();
client_->Reset();
task_runner_->RunPendingTasks();
// There are no pending tasks or actions.
EXPECT_NO_ACTION();
EXPECT_FALSE(scheduler_->begin_frames_expected());
client_->Reset();
scheduler_->SetDeferBeginMainFrame(false);
EXPECT_ACTIONS("AddObserver(this)");
// Start new BeginMainFrame after defer commit is off.
client_->Reset();
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame", "ScheduledActionSendBeginMainFrame");
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
}
TEST_F(SchedulerTest, DeferBeginMainFrameWithRedraw) {
SetUpScheduler(EXTERNAL_BFS);
scheduler_->SetDeferBeginMainFrame(true);
scheduler_->SetNeedsBeginMainFrame();
EXPECT_NO_ACTION();
// The SetNeedsRedraw will override the SetDeferBeginMainFrame(true), to
// allow a begin frame to be needed.
client_->Reset();
scheduler_->SetNeedsRedraw();
EXPECT_ACTIONS("AddObserver(this)");
client_->Reset();
AdvanceFrame();
// BeginMainFrame is not sent during the defer commit is on.
EXPECT_ACTIONS("WillBeginImplFrame");
client_->Reset();
task_runner_->RunPendingTasks(); // Run posted deadline.
EXPECT_ACTIONS("ScheduledActionDrawIfPossible");
EXPECT_FALSE(client_->IsInsideBeginImplFrame());
EXPECT_TRUE(scheduler_->begin_frames_expected());
client_->Reset();
AdvanceFrame();
EXPECT_ACTIONS("WillBeginImplFrame");
}
TEST_F(SchedulerTest, RequestCommitAfterBeginMainFrameSent) {
SetUpScheduler(EXTERNAL_BFS);
// SetNeedsBeginMainFrame should begin the frame.
scheduler_->SetNeedsBeginMainFrame();
EXPECT_ACTIONS("AddObserver(this)");
client_->Reset();
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame", "ScheduledActionSendBeginMainFrame");
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
EXPECT_TRUE(scheduler_->begin_frames_expected());
client_->Reset();
// Now SetNeedsBeginMainFrame again. Calling here means we need a second
// commit.
scheduler_->SetNeedsBeginMainFrame();
EXPECT_NO_ACTION();
client_->Reset();
// Finish the first commit.
scheduler_->NotifyBeginMainFrameStarted(task_runner_->NowTicks());
scheduler_->NotifyReadyToCommit(nullptr);
EXPECT_ACTIONS("ScheduledActionCommit", "ScheduledActionPostCommit");
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
client_->Reset();
// Activate it.
scheduler_->NotifyReadyToActivate();
EXPECT_ACTIONS("ScheduledActionActivateSyncTree");
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
client_->Reset();
task_runner_->RunPendingTasks(); // Run posted deadline.
EXPECT_ACTIONS("ScheduledActionDrawIfPossible");
EXPECT_FALSE(client_->IsInsideBeginImplFrame());
// Because we just swapped, the Scheduler should also request the next
// BeginImplFrame from the LayerTreeFrameSink.
EXPECT_TRUE(scheduler_->begin_frames_expected());
client_->Reset();
// Since another commit is needed, the next BeginImplFrame should initiate
// the second commit.
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame", "ScheduledActionSendBeginMainFrame");
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
client_->Reset();
// Finishing the commit before the deadline should post a new deadline task
// to trigger the deadline early.
scheduler_->NotifyBeginMainFrameStarted(task_runner_->NowTicks());
scheduler_->NotifyReadyToCommit(nullptr);
EXPECT_ACTIONS("ScheduledActionCommit", "ScheduledActionPostCommit");
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
client_->Reset();
scheduler_->NotifyReadyToActivate();
EXPECT_ACTIONS("ScheduledActionActivateSyncTree");
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
client_->Reset();
task_runner_->RunPendingTasks(); // Run posted deadline.
EXPECT_ACTIONS("ScheduledActionDrawIfPossible");
EXPECT_FALSE(client_->IsInsideBeginImplFrame());
EXPECT_TRUE(scheduler_->begin_frames_expected());
client_->Reset();
// On the next BeginImplFrame, verify we go back to a quiescent state and
// no longer request BeginImplFrames.
EXPECT_SCOPED(AdvanceFrame());
task_runner_->RunPendingTasks(); // Run posted deadline.
EXPECT_FALSE(scheduler_->begin_frames_expected());
client_->Reset();
}
class SchedulerClientThatsetNeedsDrawInsideDraw : public FakeSchedulerClient {
public:
SchedulerClientThatsetNeedsDrawInsideDraw()
: FakeSchedulerClient(), request_redraws_(false) {}
void SetRequestRedrawsInsideDraw(bool enable) { request_redraws_ = enable; }
DrawResult ScheduledActionDrawIfPossible() override {
// Only SetNeedsRedraw the first time this is called
if (request_redraws_) {
scheduler_->SetNeedsRedraw();
}
return FakeSchedulerClient::ScheduledActionDrawIfPossible();
}
DrawResult ScheduledActionDrawForced() override { NOTREACHED(); }
private:
bool request_redraws_;
};
// Tests for two different situations:
// 1. the scheduler dropping SetNeedsRedraw requests that happen inside
// a ScheduledActionDraw
// 2. the scheduler drawing twice inside a single tick
TEST_F(SchedulerTest, RequestRedrawInsideDraw) {
SchedulerClientThatsetNeedsDrawInsideDraw* client =
new SchedulerClientThatsetNeedsDrawInsideDraw;
SetUpScheduler(EXTERNAL_BFS, base::WrapUnique(client));
client->SetRequestRedrawsInsideDraw(true);
scheduler_->SetNeedsRedraw();
EXPECT_TRUE(scheduler_->RedrawPending());
EXPECT_TRUE(client->needs_begin_frames());
EXPECT_EQ(0, client->num_draws());
EXPECT_SCOPED(AdvanceFrame());
task_runner_->RunPendingTasks(); // Run posted deadline.
EXPECT_EQ(1, client->num_draws());
EXPECT_TRUE(scheduler_->RedrawPending());
EXPECT_TRUE(client->needs_begin_frames());
client->SetRequestRedrawsInsideDraw(false);
EXPECT_SCOPED(AdvanceFrame());
task_runner_->RunPendingTasks(); // Run posted deadline.
EXPECT_EQ(2, client_->num_draws());
EXPECT_FALSE(scheduler_->RedrawPending());
EXPECT_TRUE(client->needs_begin_frames());
// We stop requesting BeginImplFrames after a BeginImplFrame where we don't
// swap.
EXPECT_SCOPED(AdvanceFrame());
task_runner_->RunPendingTasks(); // Run posted deadline.
EXPECT_EQ(2, client->num_draws());
EXPECT_FALSE(scheduler_->RedrawPending());
EXPECT_FALSE(client->needs_begin_frames());
}
// Test that requesting redraw inside a failed draw doesn't lose the request.
TEST_F(SchedulerTest, RequestRedrawInsideFailedDraw) {
SchedulerClientThatsetNeedsDrawInsideDraw* client =
new SchedulerClientThatsetNeedsDrawInsideDraw;
SetUpScheduler(EXTERNAL_BFS, base::WrapUnique(client));
client->SetRequestRedrawsInsideDraw(true);
client->SetDrawWillHappen(false);
scheduler_->SetNeedsRedraw();
EXPECT_TRUE(scheduler_->RedrawPending());
EXPECT_TRUE(client->needs_begin_frames());
EXPECT_EQ(0, client->num_draws());
// Fail the draw.
EXPECT_SCOPED(AdvanceFrame());
task_runner_->RunPendingTasks(); // Run posted deadline.
EXPECT_EQ(1, client->num_draws());
// We have a commit pending and the draw failed, and we didn't lose the redraw
// request.
EXPECT_TRUE(scheduler_->CommitPending());
EXPECT_TRUE(scheduler_->RedrawPending());
EXPECT_TRUE(client->needs_begin_frames());
client->SetRequestRedrawsInsideDraw(false);
// Fail the draw again.
EXPECT_SCOPED(AdvanceFrame());
task_runner_->RunPendingTasks(); // Run posted deadline.
EXPECT_EQ(2, client->num_draws());
EXPECT_TRUE(scheduler_->CommitPending());
EXPECT_TRUE(scheduler_->RedrawPending());
EXPECT_TRUE(client->needs_begin_frames());
// Draw successfully.
client->SetDrawWillHappen(true);
EXPECT_SCOPED(AdvanceFrame());
task_runner_->RunPendingTasks(); // Run posted deadline.
EXPECT_EQ(3, client->num_draws());
EXPECT_TRUE(scheduler_->CommitPending());
EXPECT_FALSE(scheduler_->RedrawPending());
EXPECT_TRUE(client->needs_begin_frames());
}
class SchedulerClientThatSetNeedsBeginMainFrameInsideDraw
: public FakeSchedulerClient {
public:
SchedulerClientThatSetNeedsBeginMainFrameInsideDraw()
: set_needs_commit_on_next_draw_(false) {}
DrawResult ScheduledActionDrawIfPossible() override {
// Only SetNeedsBeginMainFrame the first time this is called
if (set_needs_commit_on_next_draw_) {
scheduler_->SetNeedsBeginMainFrame();
set_needs_commit_on_next_draw_ = false;
}
return FakeSchedulerClient::ScheduledActionDrawIfPossible();
}
DrawResult ScheduledActionDrawForced() override { NOTREACHED(); }
void SetNeedsBeginMainFrameOnNextDraw() {
set_needs_commit_on_next_draw_ = true;
}
private:
bool set_needs_commit_on_next_draw_;
};
// Tests for the scheduler infinite-looping on SetNeedsBeginMainFrame requests
// that happen inside a ScheduledActionDraw
TEST_F(SchedulerTest, RequestCommitInsideDraw) {
SchedulerClientThatSetNeedsBeginMainFrameInsideDraw* client =
new SchedulerClientThatSetNeedsBeginMainFrameInsideDraw;
SetUpScheduler(EXTERNAL_BFS, base::WrapUnique(client));
EXPECT_FALSE(client->needs_begin_frames());
scheduler_->SetNeedsRedraw();
EXPECT_TRUE(scheduler_->RedrawPending());
EXPECT_EQ(0, client->num_draws());
EXPECT_TRUE(client->needs_begin_frames());
client->SetNeedsBeginMainFrameOnNextDraw();
EXPECT_SCOPED(AdvanceFrame());
client->SetNeedsBeginMainFrameOnNextDraw();
task_runner_->RunPendingTasks(); // Run posted deadline.
EXPECT_EQ(1, client->num_draws());
EXPECT_TRUE(scheduler_->CommitPending());
EXPECT_TRUE(client->needs_begin_frames());
scheduler_->NotifyBeginMainFrameStarted(task_runner_->NowTicks());
scheduler_->NotifyReadyToCommit(nullptr);
scheduler_->NotifyReadyToActivate();
EXPECT_SCOPED(AdvanceFrame());
task_runner_->RunPendingTasks(); // Run posted deadline.
EXPECT_EQ(2, client->num_draws());
EXPECT_FALSE(scheduler_->RedrawPending());
EXPECT_FALSE(scheduler_->CommitPending());
EXPECT_TRUE(client->needs_begin_frames());
// We stop requesting BeginImplFrames after a BeginImplFrame where we don't
// swap.
EXPECT_SCOPED(AdvanceFrame());
task_runner_->RunPendingTasks(); // Run posted deadline.
EXPECT_EQ(2, client->num_draws());
EXPECT_FALSE(scheduler_->RedrawPending());
EXPECT_FALSE(scheduler_->CommitPending());
EXPECT_FALSE(client->needs_begin_frames());
}
// Tests that when a draw fails then the pending commit should not be dropped.
TEST_F(SchedulerTest, RequestCommitInsideFailedDraw) {
SchedulerClientThatsetNeedsDrawInsideDraw* client =
new SchedulerClientThatsetNeedsDrawInsideDraw;
SetUpScheduler(EXTERNAL_BFS, base::WrapUnique(client));
client->SetDrawWillHappen(false);
scheduler_->SetNeedsRedraw();
EXPECT_TRUE(scheduler_->RedrawPending());
EXPECT_TRUE(client->needs_begin_frames());
EXPECT_EQ(0, client->num_draws());
// Fail the draw.
EXPECT_SCOPED(AdvanceFrame());
task_runner_->RunPendingTasks(); // Run posted deadline.
EXPECT_EQ(1, client->num_draws());
// We have a commit pending and the draw failed, and we didn't lose the commit
// request.
EXPECT_TRUE(scheduler_->CommitPending());
EXPECT_TRUE(scheduler_->RedrawPending());
EXPECT_TRUE(client->needs_begin_frames());
// Fail the draw again.
EXPECT_SCOPED(AdvanceFrame());
task_runner_->RunPendingTasks(); // Run posted deadline.
EXPECT_EQ(2, client->num_draws());
EXPECT_TRUE(scheduler_->CommitPending());
EXPECT_TRUE(scheduler_->RedrawPending());
EXPECT_TRUE(client->needs_begin_frames());
// Draw successfully.
client->SetDrawWillHappen(true);
EXPECT_SCOPED(AdvanceFrame());
task_runner_->RunPendingTasks(); // Run posted deadline.
EXPECT_EQ(3, client->num_draws());
EXPECT_TRUE(scheduler_->CommitPending());
EXPECT_FALSE(scheduler_->RedrawPending());
EXPECT_TRUE(client->needs_begin_frames());
}
TEST_F(SchedulerTest, NoSwapWhenDrawFails) {
SchedulerClientThatSetNeedsBeginMainFrameInsideDraw* client =
new SchedulerClientThatSetNeedsBeginMainFrameInsideDraw;
SetUpScheduler(EXTERNAL_BFS, base::WrapUnique(client));
scheduler_->SetNeedsRedraw();
EXPECT_TRUE(scheduler_->RedrawPending());
EXPECT_TRUE(client->needs_begin_frames());
EXPECT_EQ(0, client->num_draws());
// Draw successfully, this starts a new frame.
client->SetNeedsBeginMainFrameOnNextDraw();
EXPECT_SCOPED(AdvanceFrame());
task_runner_->RunPendingTasks(); // Run posted deadline.
EXPECT_EQ(1, client->num_draws());
scheduler_->SetNeedsRedraw();
EXPECT_TRUE(scheduler_->RedrawPending());
EXPECT_TRUE(client->needs_begin_frames());
// Fail to draw, this should not start a frame.
client->SetDrawWillHappen(false);
client->SetNeedsBeginMainFrameOnNextDraw();
EXPECT_SCOPED(AdvanceFrame());
task_runner_->RunPendingTasks(); // Run posted deadline.
EXPECT_EQ(2, client->num_draws());
}
class SchedulerClientNeedsPrepareTilesInDraw : public FakeSchedulerClient {
public:
DrawResult ScheduledActionDrawIfPossible() override {
scheduler_->SetNeedsPrepareTiles();
return FakeSchedulerClient::ScheduledActionDrawIfPossible();
}
};
// Test prepare tiles is independant of draws.
TEST_F(SchedulerTest, PrepareTiles) {
SchedulerClientNeedsPrepareTilesInDraw* client =
new SchedulerClientNeedsPrepareTilesInDraw;
SetUpScheduler(EXTERNAL_BFS, base::WrapUnique(client));
// Request both draw and prepare tiles. PrepareTiles shouldn't
// be trigged until BeginImplFrame.
client->Reset();
scheduler_->SetNeedsPrepareTiles();
scheduler_->SetNeedsRedraw();
EXPECT_TRUE(scheduler_->RedrawPending());
EXPECT_TRUE(scheduler_->PrepareTilesPending());
EXPECT_TRUE(client->needs_begin_frames());
EXPECT_EQ(0, client->num_draws());
EXPECT_FALSE(client->HasAction("ScheduledActionPrepareTiles"));
EXPECT_FALSE(client->HasAction("ScheduledActionDrawIfPossible"));
// We have no immediate actions to perform, so the BeginImplFrame should post
// the deadline task.
client->Reset();
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame");
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
// On the deadline, the actions should have occured in the right order.
client->Reset();
task_runner_->RunPendingTasks(); // Run posted deadline.
EXPECT_EQ(1, client->num_draws());
EXPECT_TRUE(client->HasAction("ScheduledActionDrawIfPossible"));
EXPECT_TRUE(client->HasAction("ScheduledActionPrepareTiles"));
EXPECT_LT(client->ActionIndex("ScheduledActionDrawIfPossible"),
client->ActionIndex("ScheduledActionPrepareTiles"));
EXPECT_FALSE(scheduler_->RedrawPending());
EXPECT_FALSE(scheduler_->PrepareTilesPending());
EXPECT_FALSE(client_->IsInsideBeginImplFrame());
// Request a draw. We don't need a PrepareTiles yet.
client->Reset();
scheduler_->SetNeedsRedraw();
EXPECT_TRUE(scheduler_->RedrawPending());
EXPECT_FALSE(scheduler_->PrepareTilesPending());
EXPECT_TRUE(client->needs_begin_frames());
EXPECT_EQ(0, client->num_draws());
// We have no immediate actions to perform, so the BeginImplFrame should post
// the deadline task.
client->Reset();
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame");
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
// Draw. The draw will trigger SetNeedsPrepareTiles, and
// then the PrepareTiles action will be triggered after the Draw.
// Afterwards, neither a draw nor PrepareTiles are pending.
client->Reset();
task_runner_->RunPendingTasks(); // Run posted deadline.
EXPECT_EQ(1, client->num_draws());
EXPECT_TRUE(client->HasAction("ScheduledActionDrawIfPossible"));
EXPECT_TRUE(client->HasAction("ScheduledActionPrepareTiles"));
EXPECT_LT(client->ActionIndex("ScheduledActionDrawIfPossible"),
client->ActionIndex("ScheduledActionPrepareTiles"));
EXPECT_FALSE(scheduler_->RedrawPending());
EXPECT_FALSE(scheduler_->PrepareTilesPending());
EXPECT_FALSE(client_->IsInsideBeginImplFrame());
// We need a BeginImplFrame where we don't swap to go idle.
client->Reset();
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame");
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
client->Reset();
task_runner_->RunPendingTasks(); // Run posted deadline.
EXPECT_ACTIONS("RemoveObserver(this)");
EXPECT_FALSE(client_->IsInsideBeginImplFrame());
EXPECT_EQ(0, client->num_draws());
// Now trigger a PrepareTiles outside of a draw. We will then need
// a begin-frame for the PrepareTiles, but we don't need a draw.
client->Reset();
EXPECT_FALSE(client->needs_begin_frames());
scheduler_->SetNeedsPrepareTiles();
EXPECT_TRUE(client->needs_begin_frames());
EXPECT_TRUE(scheduler_->PrepareTilesPending());
EXPECT_FALSE(scheduler_->RedrawPending());
// BeginImplFrame. There will be no draw, only PrepareTiles.
client->Reset();
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame");
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
client->Reset();
task_runner_->RunPendingTasks(); // Run posted deadline.
EXPECT_EQ(0, client->num_draws());
EXPECT_FALSE(client->HasAction("ScheduledActionDrawIfPossible"));
EXPECT_TRUE(client->HasAction("ScheduledActionPrepareTiles"));
EXPECT_FALSE(client_->IsInsideBeginImplFrame());
}
// Test that PrepareTiles only happens once per frame. If an external caller
// initiates it, then the state machine should not PrepareTiles on that frame.
TEST_F(SchedulerTest, PrepareTilesOncePerFrame) {
SetUpScheduler(EXTERNAL_BFS);
// If DidPrepareTiles during a frame, then PrepareTiles should not occur
// again.
scheduler_->SetNeedsPrepareTiles();
scheduler_->SetNeedsRedraw();
client_->Reset();
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame");
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
EXPECT_TRUE(scheduler_->PrepareTilesPending());
scheduler_->DidPrepareTiles(); // An explicit PrepareTiles.
EXPECT_FALSE(scheduler_->PrepareTilesPending());
client_->Reset();
task_runner_->RunPendingTasks(); // Run posted deadline.
EXPECT_EQ(1, client_->num_draws());
EXPECT_TRUE(client_->HasAction("ScheduledActionDrawIfPossible"));
EXPECT_FALSE(client_->HasAction("ScheduledActionPrepareTiles"));
EXPECT_FALSE(scheduler_->RedrawPending());
EXPECT_FALSE(scheduler_->PrepareTilesPending());
EXPECT_FALSE(client_->IsInsideBeginImplFrame());
// Next frame without DidPrepareTiles should PrepareTiles with draw.
scheduler_->SetNeedsPrepareTiles();
scheduler_->SetNeedsRedraw();
client_->Reset();
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame");
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
client_->Reset();
task_runner_->RunPendingTasks(); // Run posted deadline.
EXPECT_EQ(1, client_->num_draws());
EXPECT_TRUE(client_->HasAction("ScheduledActionDrawIfPossible"));
EXPECT_TRUE(client_->HasAction("ScheduledActionPrepareTiles"));
EXPECT_LT(client_->ActionIndex("ScheduledActionDrawIfPossible"),
client_->ActionIndex("ScheduledActionPrepareTiles"));
EXPECT_FALSE(scheduler_->RedrawPending());
EXPECT_FALSE(scheduler_->PrepareTilesPending());
EXPECT_FALSE(client_->IsInsideBeginImplFrame());
// If we get another DidPrepareTiles within the same frame, we should
// not PrepareTiles on the next frame.
scheduler_->DidPrepareTiles(); // An explicit PrepareTiles.
scheduler_->SetNeedsPrepareTiles();
scheduler_->SetNeedsRedraw();
client_->Reset();
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame");
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
EXPECT_TRUE(scheduler_->PrepareTilesPending());
client_->Reset();
task_runner_->RunPendingTasks(); // Run posted deadline.
EXPECT_EQ(1, client_->num_draws());
EXPECT_TRUE(client_->HasAction("ScheduledActionDrawIfPossible"));
EXPECT_FALSE(client_->HasAction("ScheduledActionPrepareTiles"));
EXPECT_FALSE(scheduler_->RedrawPending());
EXPECT_FALSE(client_->IsInsideBeginImplFrame());
// If we get another DidPrepareTiles, we should not PrepareTiles on the next
// frame. This verifies we don't alternate calling PrepareTiles once and
// twice.
EXPECT_TRUE(scheduler_->PrepareTilesPending());
scheduler_->DidPrepareTiles(); // An explicit PrepareTiles.
EXPECT_FALSE(scheduler_->PrepareTilesPending());
scheduler_->SetNeedsPrepareTiles();
scheduler_->SetNeedsRedraw();
client_->Reset();
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame");
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
EXPECT_TRUE(scheduler_->PrepareTilesPending());
client_->Reset();
task_runner_->RunPendingTasks(); // Run posted deadline.
EXPECT_EQ(1, client_->num_draws());
EXPECT_TRUE(client_->HasAction("ScheduledActionDrawIfPossible"));
EXPECT_FALSE(client_->HasAction("ScheduledActionPrepareTiles"));
EXPECT_FALSE(scheduler_->RedrawPending());
EXPECT_FALSE(client_->IsInsideBeginImplFrame());
// Next frame without DidPrepareTiles should PrepareTiles with draw.
scheduler_->SetNeedsPrepareTiles();
scheduler_->SetNeedsRedraw();
client_->Reset();
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame");
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
client_->Reset();
task_runner_->RunPendingTasks(); // Run posted deadline.
EXPECT_EQ(1, client_->num_draws());
EXPECT_TRUE(client_->HasAction("ScheduledActionDrawIfPossible"));
EXPECT_TRUE(client_->HasAction("ScheduledActionPrepareTiles"));
EXPECT_LT(client_->ActionIndex("ScheduledActionDrawIfPossible"),
client_->ActionIndex("ScheduledActionPrepareTiles"));
EXPECT_FALSE(scheduler_->RedrawPending());
EXPECT_FALSE(scheduler_->PrepareTilesPending());
EXPECT_FALSE(client_->IsInsideBeginImplFrame());
}
TEST_F(SchedulerTest, DidPrepareTilesPreventsPrepareTilesForOneFrame) {
std::unique_ptr<SchedulerClientNeedsPrepareTilesInDraw> client =
base::WrapUnique(new SchedulerClientNeedsPrepareTilesInDraw);
SetUpScheduler(EXTERNAL_BFS, std::move(client));
client_->Reset();
scheduler_->SetNeedsRedraw();
EXPECT_ACTIONS("AddObserver(this)");
client_->Reset();
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame");
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
client_->Reset();
task_runner_->RunPendingTasks(); // Run posted deadline.
EXPECT_FALSE(client_->IsInsideBeginImplFrame());
EXPECT_ACTIONS("ScheduledActionDrawIfPossible",
"ScheduledActionPrepareTiles");
// We don't want to hinder scheduled prepare tiles for more than one frame
// even if we call unscheduled prepare tiles many times.
for (int i = 0; i < 10; i++) {
scheduler_->DidPrepareTiles();
}
client_->Reset();
scheduler_->SetNeedsRedraw();
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame");
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
// No scheduled prepare tiles because we've already counted a prepare tiles in
// between frames.
client_->Reset();
task_runner_->RunPendingTasks(); // Run posted deadline.
EXPECT_FALSE(client_->IsInsideBeginImplFrame());
EXPECT_ACTIONS("ScheduledActionDrawIfPossible");
client_->Reset();
scheduler_->SetNeedsRedraw();
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame");
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
// Resume scheduled prepare tiles.
client_->Reset();
task_runner_->RunPendingTasks(); // Run posted deadline.
EXPECT_FALSE(client_->IsInsideBeginImplFrame());
EXPECT_ACTIONS("ScheduledActionDrawIfPossible",
"ScheduledActionPrepareTiles");
}
TEST_F(SchedulerTest, TriggerBeginFrameDeadlineEarly) {
SchedulerClientNeedsPrepareTilesInDraw* client =
new SchedulerClientNeedsPrepareTilesInDraw;
SetUpScheduler(EXTERNAL_BFS, base::WrapUnique(client));
scheduler_->SetNeedsRedraw();
EXPECT_SCOPED(AdvanceFrame());
// The deadline should be zero since there is no work other than drawing
// pending.
EXPECT_EQ(base::TimeTicks(), client->posted_begin_impl_frame_deadline());
}
TEST_F(SchedulerTest, WaitForReadyToDrawDoNotPostDeadline) {
SchedulerClientNeedsPrepareTilesInDraw* client =
new SchedulerClientNeedsPrepareTilesInDraw;
scheduler_settings_.commit_to_active_tree = true;
SetUpScheduler(EXTERNAL_BFS, base::WrapUnique(client));
// SetNeedsBeginMainFrame should begin the frame on the next BeginImplFrame.
scheduler_->SetNeedsBeginMainFrame();
EXPECT_ACTIONS("AddObserver(this)");
client_->Reset();
// Begin new frame.
EXPECT_SCOPED(AdvanceFrame());
scheduler_->NotifyBeginMainFrameStarted(task_runner_->NowTicks());
EXPECT_ACTIONS("WillBeginImplFrame", "ScheduledActionSendBeginMainFrame");
client_->Reset();
scheduler_->NotifyReadyToCommit(nullptr);
EXPECT_ACTIONS("ScheduledActionCommit", "ScheduledActionPostCommit");
client_->Reset();
scheduler_->NotifyReadyToActivate();
EXPECT_ACTIONS("ScheduledActionActivateSyncTree");
// Scheduler won't post deadline in the mode.
client_->Reset();
task_runner_->RunPendingTasks(); // Try to run posted deadline.
// There is no posted deadline.
EXPECT_NO_ACTION();
// Scheduler received ready to draw signal, and posted deadline.
scheduler_->NotifyReadyToDraw();
client_->Reset();
task_runner_->RunPendingTasks(); // Run posted deadline.
EXPECT_EQ(1, client_->num_draws());
EXPECT_TRUE(client_->HasAction("ScheduledActionDrawIfPossible"));
}
TEST_F(SchedulerTest, WaitForReadyToDrawCancelledWhenLostLayerTreeFrameSink) {
SchedulerClientNeedsPrepareTilesInDraw* client =
new SchedulerClientNeedsPrepareTilesInDraw;
scheduler_settings_.commit_to_active_tree = true;
SetUpScheduler(EXTERNAL_BFS, base::WrapUnique(client));
// SetNeedsBeginMainFrame should begin the frame on the next BeginImplFrame.
scheduler_->SetNeedsBeginMainFrame();
EXPECT_ACTIONS("AddObserver(this)");
client_->Reset();
// Begin new frame.
EXPECT_SCOPED(AdvanceFrame());
scheduler_->NotifyBeginMainFrameStarted(task_runner_->NowTicks());
EXPECT_ACTIONS("WillBeginImplFrame", "ScheduledActionSendBeginMainFrame");
client_->Reset();
scheduler_->NotifyReadyToCommit(nullptr);
EXPECT_ACTIONS("ScheduledActionCommit", "ScheduledActionPostCommit");
client_->Reset();
scheduler_->NotifyReadyToActivate();
EXPECT_ACTIONS("ScheduledActionActivateSyncTree");
// Scheduler won't post deadline in the mode.
client_->Reset();
task_runner_->RunPendingTasks(); // Try to run posted deadline.
// There is no posted deadline.
EXPECT_NO_ACTION();
// Scheduler loses LayerTreeFrameSink, and stops waiting for ready to draw
// signal.
client_->Reset();
scheduler_->DidLoseLayerTreeFrameSink();
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
task_runner_->RunPendingTasks(); // Run posted deadline.
EXPECT_ACTIONS("ScheduledActionBeginLayerTreeFrameSinkCreation",
"RemoveObserver(this)");
}
void SchedulerTest::AdvanceAndMissOneFrame() {
// Impl thread hits deadline before commit finishes.
scheduler_->SetNeedsBeginMainFrame();
EXPECT_FALSE(scheduler_->MainThreadMissedLastDeadline());
EXPECT_SCOPED(AdvanceFrame());
EXPECT_FALSE(scheduler_->MainThreadMissedLastDeadline());
task_runner_->RunTasksWhile(client_->InsideBeginImplFrame(true));
EXPECT_TRUE(scheduler_->MainThreadMissedLastDeadline());
scheduler_->NotifyBeginMainFrameStarted(task_runner_->NowTicks());
scheduler_->NotifyReadyToCommit(nullptr);
scheduler_->NotifyReadyToActivate();
EXPECT_ACTIONS("AddObserver(this)", "WillBeginImplFrame",
"ScheduledActionSendBeginMainFrame", "ScheduledActionCommit",
"ScheduledActionPostCommit",
"ScheduledActionActivateSyncTree");
EXPECT_TRUE(scheduler_->MainThreadMissedLastDeadline());
client_->Reset();
}
void SchedulerTest::CheckMainFrameNotSkippedAfterLateCommit() {
AdvanceAndMissOneFrame();
scheduler_->SetNeedsBeginMainFrame();
EXPECT_TRUE(scheduler_->MainThreadMissedLastDeadline());
EXPECT_SCOPED(AdvanceFrame());
EXPECT_TRUE(scheduler_->MainThreadMissedLastDeadline());
task_runner_->RunTasksWhile(client_->InsideBeginImplFrame(true));
EXPECT_TRUE(scheduler_->MainThreadMissedLastDeadline());
EXPECT_TRUE(client_->HasAction("WillBeginImplFrame"));
EXPECT_TRUE(client_->HasAction("ScheduledActionSendBeginMainFrame"));
}
TEST_F(SchedulerTest, MainFrameNotSkippedAfterLateBeginFrame) {
// If a begin frame is delivered extremely late (because the browser has
// some contention), make sure that the main frame is not skipped even
// if it can activate before the deadline.
SetUpScheduler(EXTERNAL_BFS);
fake_compositor_timing_history_->SetAllEstimatesTo(kFastDuration);
AdvanceAndMissOneFrame();
EXPECT_TRUE(scheduler_->MainThreadMissedLastDeadline());
scheduler_->SetNeedsBeginMainFrame();
// Advance frame and create a begin frame.
task_runner_->AdvanceMockTickClock(viz::BeginFrameArgs::DefaultInterval());
viz::BeginFrameArgs args =
fake_external_begin_frame_source_->CreateBeginFrameArgs(
BEGINFRAME_FROM_HERE, task_runner_->GetMockTickClock());
// Deliver this begin frame super late.
task_runner_->AdvanceMockTickClock(viz::BeginFrameArgs::DefaultInterval() *
100);
fake_external_begin_frame_source_->TestOnBeginFrame(args);
task_runner_->RunTasksWhile(client_->InsideBeginImplFrame(true));
EXPECT_EQ(true, scheduler_->MainThreadMissedLastDeadline());
EXPECT_ACTIONS("WillBeginImplFrame", "ScheduledActionSendBeginMainFrame",
"ScheduledActionDrawIfPossible");
}
TEST_F(SchedulerTest, FrameIntervalUpdated) {
// Verify that the SchedulerClient gets updates when the begin frame interval
// changes.
SetUpScheduler(EXTERNAL_BFS);
constexpr uint64_t kSourceId = viz::BeginFrameArgs::kStartingSourceId;
uint64_t sequence_number = viz::BeginFrameArgs::kStartingFrameNumber;
base::TimeDelta interval =
base::Microseconds(base::Time::kMicrosecondsPerSecond / 120.0);
// Send BeginFrameArgs with 120hz refresh rate and confirm client gets update.
scheduler_->SetNeedsRedraw();
task_runner_->AdvanceMockTickClock(interval);
viz::BeginFrameArgs args1 = viz::BeginFrameArgs::Create(
BEGINFRAME_FROM_HERE, kSourceId, sequence_number++,
task_runner_->NowTicks(), task_runner_->NowTicks() + interval, interval,
viz::BeginFrameArgs::NORMAL);
fake_external_begin_frame_source_->TestOnBeginFrame(args1);
EXPECT_EQ(client_->frame_interval(), interval);
// Send another BeginFrameArgs with 120hz refresh rate that arrives late. Even
// though the interval between begin frames arriving is bigger than |interval|
// the client only hears the interval specified in BeginFrameArgs.
scheduler_->SetNeedsRedraw();
const base::TimeDelta late_delta = base::Milliseconds(4);
task_runner_->AdvanceMockTickClock(interval + late_delta);
viz::BeginFrameArgs args2 = viz::BeginFrameArgs::Create(
BEGINFRAME_FROM_HERE, kSourceId, sequence_number++, args1.deadline,
args1.deadline + interval, interval, viz::BeginFrameArgs::NORMAL);
fake_external_begin_frame_source_->TestOnBeginFrame(args2);
EXPECT_EQ(client_->frame_interval(), interval);
// Change the interval for 90hz refresh rate.
interval = base::Microseconds(base::Time::kMicrosecondsPerSecond / 90.0);
// Send BeginFrameArgs with 90hz refresh rate and confirm client gets update.
scheduler_->SetNeedsRedraw();
task_runner_->AdvanceMockTickClock(args2.deadline - task_runner_->NowTicks());
viz::BeginFrameArgs args3 = viz::BeginFrameArgs::Create(
BEGINFRAME_FROM_HERE, kSourceId, sequence_number++, args2.deadline,
args2.deadline + interval, interval, viz::BeginFrameArgs::NORMAL);
fake_external_begin_frame_source_->TestOnBeginFrame(args3);
EXPECT_EQ(client_->frame_interval(), interval);
// Send BeginFrameArgs with zero interval. This isn't a valid interval and
// client shouldn't find out about it.
scheduler_->SetNeedsRedraw();
task_runner_->AdvanceMockTickClock(interval);
viz::BeginFrameArgs args4 = viz::BeginFrameArgs::Create(
BEGINFRAME_FROM_HERE, kSourceId, sequence_number++, args3.deadline,
args3.deadline + interval, base::TimeDelta(),
viz::BeginFrameArgs::NORMAL);
fake_external_begin_frame_source_->TestOnBeginFrame(args4);
EXPECT_EQ(client_->frame_interval(), interval);
}
TEST_F(SchedulerTest, BeginMainFrameThrottling) {
// Verify that the SchedulerClient gets updates when the begin frame interval
// changes.
SetUpScheduler(EXTERNAL_BFS);
constexpr uint64_t kSourceId = viz::BeginFrameArgs::kStartingSourceId;
uint64_t sequence_number = viz::BeginFrameArgs::kStartingFrameNumber;
// No throttling at 60Hz.
base::TimeDelta interval = base::Hertz(60);
scheduler_->SetNeedsRedraw();
task_runner_->AdvanceMockTickClock(interval);
viz::BeginFrameArgs args = viz::BeginFrameArgs::Create(
BEGINFRAME_FROM_HERE, kSourceId, sequence_number++,
task_runner_->NowTicks(), task_runner_->NowTicks() + interval, interval,
viz::BeginFrameArgs::NORMAL);
fake_external_begin_frame_source_->TestOnBeginFrame(args);
EXPECT_EQ(client_->frame_interval(), interval);
EXPECT_TRUE(
scheduler_->state_machine().MainFrameThrottledInterval().is_zero());
{
base::test::ScopedFeatureList feature_list;
feature_list.InitAndDisableFeature(features::kThrottleMainFrameTo60Hz);
// No throttling when the feature is disabled.
interval = base::Hertz(240);
scheduler_->SetNeedsRedraw();
task_runner_->AdvanceMockTickClock(interval);
args = viz::BeginFrameArgs::Create(
BEGINFRAME_FROM_HERE, kSourceId, sequence_number++,
task_runner_->NowTicks(), task_runner_->NowTicks() + interval, interval,
viz::BeginFrameArgs::NORMAL);
fake_external_begin_frame_source_->TestOnBeginFrame(args);
EXPECT_EQ(client_->frame_interval(), interval);
EXPECT_TRUE(
scheduler_->state_machine().MainFrameThrottledInterval().is_zero());
}
// Enable the feature for the rest of the test.
base::test::ScopedFeatureList feature_list{
features::kThrottleMainFrameTo60Hz};
// Throttling at 120fps.
interval = base::Hertz(120);
scheduler_->SetNeedsRedraw();
task_runner_->AdvanceMockTickClock(interval);
args = viz::BeginFrameArgs::Create(
BEGINFRAME_FROM_HERE, kSourceId, sequence_number++,
task_runner_->NowTicks(), task_runner_->NowTicks() + interval, interval,
viz::BeginFrameArgs::NORMAL);
fake_external_begin_frame_source_->TestOnBeginFrame(args);
EXPECT_EQ(client_->frame_interval(), interval);
constexpr float kSlackFactor = .9;
EXPECT_NEAR(scheduler_->state_machine()
.MainFrameThrottledInterval()
.InMillisecondsF(),
(base::Hertz(60) * kSlackFactor).InMillisecondsF(), 1e-2);
// Not at 90Hz.
interval = base::Hertz(90);
scheduler_->SetNeedsRedraw();
task_runner_->AdvanceMockTickClock(interval);
args = viz::BeginFrameArgs::Create(
BEGINFRAME_FROM_HERE, kSourceId, sequence_number++,
task_runner_->NowTicks(), task_runner_->NowTicks() + interval, interval,
viz::BeginFrameArgs::NORMAL);
fake_external_begin_frame_source_->TestOnBeginFrame(args);
EXPECT_EQ(client_->frame_interval(), interval);
EXPECT_TRUE(
scheduler_->state_machine().MainFrameThrottledInterval().is_zero());
}
TEST_F(SchedulerTest, MainFrameNotSkippedAfterLateCommit) {
SetUpScheduler(EXTERNAL_BFS);
fake_compositor_timing_history_->SetAllEstimatesTo(kFastDuration);
EXPECT_SCOPED(CheckMainFrameNotSkippedAfterLateCommit());
}
// Response times of BeginMainFrame's without the critical path flag set
// should not affect whether we recover latency or not.
TEST_F(
SchedulerTest,
MainFrameNotSkippedAfterLateCommit_LongMainFrameQueueDurationNotCritical) {
SetUpScheduler(EXTERNAL_BFS);
fake_compositor_timing_history_->SetAllEstimatesTo(kFastDuration);
fake_compositor_timing_history_
->SetBeginMainFrameQueueDurationNotCriticalEstimate(kSlowDuration);
EXPECT_SCOPED(CheckMainFrameNotSkippedAfterLateCommit());
}
// Response times of BeginMainFrame's with the critical path flag set
// should affect whether we recover latency or not.
TEST_F(SchedulerTest,
MainFrameNotSkippedAfterLateCommit_LongMainFrameQueueDurationCritical) {
SetUpScheduler(EXTERNAL_BFS);
fake_compositor_timing_history_->SetAllEstimatesTo(kFastDuration);
fake_compositor_timing_history_
->SetBeginMainFrameQueueDurationCriticalEstimate(kSlowDuration);
fake_compositor_timing_history_
->SetBeginMainFrameQueueDurationNotCriticalEstimate(kSlowDuration);
EXPECT_SCOPED(CheckMainFrameNotSkippedAfterLateCommit());
}
TEST_F(SchedulerTest,
MainFrameNotSkippedAfterLateCommitInPreferImplLatencyMode) {
SetUpScheduler(EXTERNAL_BFS);
scheduler_->SetTreePrioritiesAndScrollState(
SMOOTHNESS_TAKES_PRIORITY,
ScrollHandlerState::SCROLL_DOES_NOT_AFFECT_SCROLL_HANDLER);
fake_compositor_timing_history_->SetAllEstimatesTo(kFastDuration);
EXPECT_SCOPED(CheckMainFrameNotSkippedAfterLateCommit());
}
TEST_F(SchedulerTest,
MainFrameNotSkippedAfterLateCommit_CommitEstimateTooLong) {
SetUpScheduler(EXTERNAL_BFS);
fake_compositor_timing_history_->SetAllEstimatesTo(kFastDuration);
fake_compositor_timing_history_
->SetBeginMainFrameStartToReadyToCommitDurationEstimate(kSlowDuration);
EXPECT_SCOPED(CheckMainFrameNotSkippedAfterLateCommit());
}
TEST_F(SchedulerTest,
MainFrameNotSkippedAfterLateCommit_ReadyToActivateEstimateTooLong) {
SetUpScheduler(EXTERNAL_BFS);
fake_compositor_timing_history_->SetAllEstimatesTo(kFastDuration);
fake_compositor_timing_history_->SetCommitToReadyToActivateDurationEstimate(
kSlowDuration);
EXPECT_SCOPED(CheckMainFrameNotSkippedAfterLateCommit());
}
TEST_F(SchedulerTest,
MainFrameNotSkippedAfterLateCommit_ActivateEstimateTooLong) {
SetUpScheduler(EXTERNAL_BFS);
fake_compositor_timing_history_->SetAllEstimatesTo(kFastDuration);
fake_compositor_timing_history_->SetActivateDurationEstimate(kSlowDuration);
EXPECT_SCOPED(CheckMainFrameNotSkippedAfterLateCommit());
}
TEST_F(SchedulerTest, MainFrameNotSkippedAfterLateCommit_DrawEstimateTooLong) {
SetUpScheduler(EXTERNAL_BFS);
fake_compositor_timing_history_->SetAllEstimatesTo(kFastDuration);
fake_compositor_timing_history_->SetDrawDurationEstimate(kSlowDuration);
EXPECT_SCOPED(CheckMainFrameNotSkippedAfterLateCommit());
}
// If the BeginMainFrame aborts, it doesn't actually insert a frame into the
// queue, which means there is no latency to recover.
TEST_F(SchedulerTest, MainFrameNotSkippedAfterLateBeginMainFrameAbort) {
SetUpScheduler(EXTERNAL_BFS);
// Use fast estimates so we think we can recover latency if needed.
fake_compositor_timing_history_->SetAllEstimatesTo(kFastDuration);
// Impl thread hits deadline before BeginMainFrame aborts.
scheduler_->SetNeedsBeginMainFrame();
EXPECT_FALSE(scheduler_->MainThreadMissedLastDeadline());
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("AddObserver(this)", "WillBeginImplFrame",
"ScheduledActionSendBeginMainFrame");
EXPECT_FALSE(scheduler_->MainThreadMissedLastDeadline());
task_runner_->RunTasksWhile(client_->InsideBeginImplFrame(true));
EXPECT_TRUE(scheduler_->MainThreadMissedLastDeadline());
scheduler_->NotifyBeginMainFrameStarted(task_runner_->NowTicks());
EXPECT_TRUE(scheduler_->MainThreadMissedLastDeadline());
// After aborting the frame, make sure we don't skip the
// next BeginMainFrame.
client_->Reset();
scheduler_->BeginMainFrameAborted(CommitEarlyOutReason::kFinishedNoUpdates);
EXPECT_FALSE(scheduler_->MainThreadMissedLastDeadline());
scheduler_->SetNeedsBeginMainFrame();
EXPECT_FALSE(scheduler_->MainThreadMissedLastDeadline());
EXPECT_SCOPED(AdvanceFrame());
EXPECT_TRUE(client_->HasAction("WillBeginImplFrame"));
EXPECT_TRUE(client_->HasAction("ScheduledActionSendBeginMainFrame"));
EXPECT_FALSE(scheduler_->MainThreadMissedLastDeadline());
task_runner_->RunTasksWhile(client_->InsideBeginImplFrame(true));
EXPECT_TRUE(scheduler_->MainThreadMissedLastDeadline());
}
// If the BeginMainFrame aborts, it doesn't actually insert a frame into the
// queue, which means there is no latency to recover.
TEST_F(SchedulerTest, MainFrameNotSkippedAfterCanDrawChanges) {
SetUpScheduler(EXTERNAL_BFS);
// Use fast estimates so we think we can recover latency if needed.
fake_compositor_timing_history_->SetAllEstimatesTo(kFastDuration);
// Impl thread hits deadline before BeginMainFrame aborts.
scheduler_->SetNeedsBeginMainFrame();
EXPECT_FALSE(scheduler_->MainThreadMissedLastDeadline());
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("AddObserver(this)", "WillBeginImplFrame",
"ScheduledActionSendBeginMainFrame");
EXPECT_FALSE(scheduler_->MainThreadMissedLastDeadline());
task_runner_->RunTasksWhile(client_->InsideBeginImplFrame(true));
EXPECT_TRUE(scheduler_->MainThreadMissedLastDeadline());
scheduler_->NotifyBeginMainFrameStarted(task_runner_->NowTicks());
EXPECT_TRUE(scheduler_->MainThreadMissedLastDeadline());
// Make us abort the upcoming draw.
client_->Reset();
scheduler_->NotifyReadyToCommit(nullptr);
scheduler_->NotifyReadyToActivate();
EXPECT_ACTIONS("ScheduledActionCommit", "ScheduledActionPostCommit",
"ScheduledActionActivateSyncTree");
EXPECT_TRUE(scheduler_->MainThreadMissedLastDeadline());
scheduler_->SetCanDraw(false);
EXPECT_FALSE(scheduler_->MainThreadMissedLastDeadline());
// Make CanDraw true after activation.
client_->Reset();
scheduler_->SetCanDraw(true);
EXPECT_NO_ACTION();
EXPECT_FALSE(scheduler_->MainThreadMissedLastDeadline());
// Make sure we don't skip the next BeginMainFrame.
client_->Reset();
scheduler_->SetNeedsBeginMainFrame();
EXPECT_FALSE(scheduler_->MainThreadMissedLastDeadline());
EXPECT_SCOPED(AdvanceFrame());
EXPECT_TRUE(client_->HasAction("WillBeginImplFrame"));
EXPECT_TRUE(client_->HasAction("ScheduledActionSendBeginMainFrame"));
EXPECT_FALSE(scheduler_->MainThreadMissedLastDeadline());
task_runner_->RunTasksWhile(client_->InsideBeginImplFrame(true));
EXPECT_TRUE(scheduler_->MainThreadMissedLastDeadline());
}
TEST_F(SchedulerTest, MainFrameNotSkippedWhenNoTimingHistory) {
SetUpScheduler(EXTERNAL_BFS);
// Use fast estimates so we think we can recover latency if needed.
fake_compositor_timing_history_->SetAllEstimatesTo(kFastDuration);
// Impl thread hits deadline before BeginMainFrame commits.
scheduler_->SetNeedsBeginMainFrame();
EXPECT_FALSE(scheduler_->MainThreadMissedLastDeadline());
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("AddObserver(this)", "WillBeginImplFrame",
"ScheduledActionSendBeginMainFrame");
EXPECT_FALSE(scheduler_->MainThreadMissedLastDeadline());
task_runner_->RunTasksWhile(client_->InsideBeginImplFrame(true));
EXPECT_TRUE(scheduler_->MainThreadMissedLastDeadline());
scheduler_->NotifyBeginMainFrameStarted(task_runner_->NowTicks());
EXPECT_TRUE(scheduler_->MainThreadMissedLastDeadline());
// Commit after the deadline.
client_->Reset();
scheduler_->NotifyReadyToCommit(nullptr);
scheduler_->NotifyReadyToActivate();
EXPECT_ACTIONS("ScheduledActionCommit", "ScheduledActionPostCommit",
"ScheduledActionActivateSyncTree");
EXPECT_TRUE(scheduler_->MainThreadMissedLastDeadline());
// Clear the timing history. Make sure we don't skip the main frame until we
// have history from at least one frame.
client_->Reset();
scheduler_->SetNeedsBeginMainFrame();
scheduler_->ClearHistory();
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame", "ScheduledActionSendBeginMainFrame");
}
void SchedulerTest::ImplFrameNotSkippedAfterLateAck() {
// To get into a high latency state, this test disables automatic swap acks.
client_->SetAutomaticSubmitCompositorFrameAck(false);
// Draw and swap for first BeginFrame
client_->Reset();
scheduler_->SetNeedsBeginMainFrame();
EXPECT_FALSE(scheduler_->MainThreadMissedLastDeadline());
SendNextBeginFrame();
EXPECT_ACTIONS("AddObserver(this)", "WillBeginImplFrame",
"ScheduledActionSendBeginMainFrame");
client_->Reset();
scheduler_->NotifyBeginMainFrameStarted(task_runner_->NowTicks());
scheduler_->NotifyReadyToCommit(nullptr);
scheduler_->NotifyReadyToActivate();
EXPECT_FALSE(scheduler_->MainThreadMissedLastDeadline());
task_runner_->RunTasksWhile(client_->InsideBeginImplFrame(true));
EXPECT_ACTIONS("ScheduledActionCommit", "ScheduledActionPostCommit",
"ScheduledActionActivateSyncTree",
"ScheduledActionDrawIfPossible");
// Verify impl thread consistently operates in high latency mode
// without skipping any frames.
for (int i = 0; i < 10; i++) {
// Not calling scheduler_->DidReceiveCompositorFrameAck() until after next
// frame
// puts the impl thread in high latency mode.
client_->Reset();
scheduler_->SetNeedsBeginMainFrame();
EXPECT_FALSE(scheduler_->MainThreadMissedLastDeadline());
SendNextBeginFrame();
EXPECT_ACTIONS("WillBeginImplFrame");
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
EXPECT_FALSE(scheduler_->MainThreadMissedLastDeadline());
client_->Reset();
scheduler_->DidReceiveCompositorFrameAck();
scheduler_->NotifyBeginMainFrameStarted(task_runner_->NowTicks());
scheduler_->NotifyReadyToCommit(nullptr);
scheduler_->NotifyReadyToActivate();
task_runner_->RunTasksWhile(client_->InsideBeginImplFrame(true));
// Verify that we don't skip the actions of the BeginImplFrame
EXPECT_ACTIONS("ScheduledActionSendBeginMainFrame", "ScheduledActionCommit",
"ScheduledActionPostCommit",
"ScheduledActionActivateSyncTree",
"ScheduledActionDrawIfPossible");
}
}
TEST_F(SchedulerTest,
ImplFrameNotSkippedAfterLateAck_MainFrameQueueDurationCriticalTooLong) {
SetUpScheduler(EXTERNAL_BFS);
fake_compositor_timing_history_->SetAllEstimatesTo(kFastDuration);
fake_compositor_timing_history_
->SetBeginMainFrameQueueDurationCriticalEstimate(kSlowDuration);
fake_compositor_timing_history_
->SetBeginMainFrameQueueDurationNotCriticalEstimate(kSlowDuration);
EXPECT_SCOPED(ImplFrameNotSkippedAfterLateAck());
}
TEST_F(SchedulerTest, ImplFrameNotSkippedAfterLateAck_CommitEstimateTooLong) {
SetUpScheduler(EXTERNAL_BFS);
fake_compositor_timing_history_->SetAllEstimatesTo(kFastDuration);
fake_compositor_timing_history_
->SetBeginMainFrameStartToReadyToCommitDurationEstimate(kSlowDuration);
EXPECT_SCOPED(ImplFrameNotSkippedAfterLateAck());
}
TEST_F(SchedulerTest,
ImplFrameNotSkippedAfterLateAck_ReadyToActivateEstimateTooLong) {
SetUpScheduler(EXTERNAL_BFS);
fake_compositor_timing_history_->SetAllEstimatesTo(kFastDuration);
fake_compositor_timing_history_->SetCommitToReadyToActivateDurationEstimate(
kSlowDuration);
EXPECT_SCOPED(ImplFrameNotSkippedAfterLateAck());
}
TEST_F(SchedulerTest, ImplFrameNotSkippedAfterLateAck_ActivateEstimateTooLong) {
SetUpScheduler(EXTERNAL_BFS);
fake_compositor_timing_history_->SetAllEstimatesTo(kFastDuration);
fake_compositor_timing_history_->SetActivateDurationEstimate(kSlowDuration);
EXPECT_SCOPED(ImplFrameNotSkippedAfterLateAck());
}
TEST_F(SchedulerTest, ImplFrameNotSkippedAfterLateAck_DrawEstimateTooLong) {
SetUpScheduler(EXTERNAL_BFS);
fake_compositor_timing_history_->SetAllEstimatesTo(kFastDuration);
fake_compositor_timing_history_->SetDrawDurationEstimate(kSlowDuration);
EXPECT_SCOPED(ImplFrameNotSkippedAfterLateAck());
}
void SchedulerTest::BeginFramesNotFromClient(BeginFrameSourceType bfs_type) {
SetUpScheduler(bfs_type);
// SetNeedsBeginMainFrame should begin the frame on the next BeginImplFrame
// without calling SetNeedsBeginFrame.
scheduler_->SetNeedsBeginMainFrame();
EXPECT_NO_ACTION();
client_->Reset();
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame", "ScheduledActionSendBeginMainFrame");
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
client_->Reset();
// Can't run the deadline task because it can race with begin frame for the
// SyntheticBFS case.
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame");
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
client_->Reset();
// NotifyReadyToCommit should trigger the commit.
scheduler_->NotifyBeginMainFrameStarted(task_runner_->NowTicks());
scheduler_->NotifyReadyToCommit(nullptr);
EXPECT_ACTIONS("ScheduledActionCommit", "ScheduledActionPostCommit");
client_->Reset();
// NotifyReadyToActivate should trigger the activation.
scheduler_->NotifyReadyToActivate();
EXPECT_ACTIONS("ScheduledActionActivateSyncTree");
client_->Reset();
// BeginImplFrame deadline should draw. The following BeginImplFrame deadline
// should SetNeedsBeginFrame(false) to avoid excessive toggles.
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("ScheduledActionDrawIfPossible", "WillBeginImplFrame");
client_->Reset();
// Make sure SetNeedsBeginFrame isn't called on the client
// when the BeginFrame is no longer needed.
task_runner_->RunPendingTasks(); // Run posted deadline.
EXPECT_NO_ACTION();
client_->Reset();
}
TEST_F(SchedulerTest, SyntheticBeginFrames) {
BeginFramesNotFromClient(THROTTLED_BFS);
}
TEST_F(SchedulerTest, UnthrottledBeginFrames) {
BeginFramesNotFromClient(UNTHROTTLED_BFS);
}
void SchedulerTest::BeginFramesNotFromClient_IsDrawThrottled(
BeginFrameSourceType bfs_type) {
SetUpScheduler(bfs_type);
// Set the draw duration estimate to zero so that deadlines are accurate.
fake_compositor_timing_history_->SetDrawDurationEstimate(base::TimeDelta());
// To test swap ack throttling, this test disables automatic swap acks.
client_->SetAutomaticSubmitCompositorFrameAck(false);
// SetNeedsBeginMainFrame should begin the frame on the next BeginImplFrame.
client_->Reset();
scheduler_->SetNeedsBeginMainFrame();
EXPECT_NO_ACTION();
client_->Reset();
// Trigger the first BeginImplFrame and BeginMainFrame
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame", "ScheduledActionSendBeginMainFrame");
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
client_->Reset();
// NotifyReadyToCommit should trigger the pending commit.
scheduler_->NotifyBeginMainFrameStarted(task_runner_->NowTicks());
scheduler_->NotifyReadyToCommit(nullptr);
EXPECT_ACTIONS("ScheduledActionCommit", "ScheduledActionPostCommit");
client_->Reset();
// NotifyReadyToActivate should trigger the activation and draw.
scheduler_->NotifyReadyToActivate();
EXPECT_ACTIONS("ScheduledActionActivateSyncTree");
client_->Reset();
// Swapping will put us into a swap throttled state.
// Run posted deadline.
task_runner_->RunTasksWhile(client_->InsideBeginImplFrame(true));
EXPECT_ACTIONS("ScheduledActionDrawIfPossible");
EXPECT_FALSE(client_->IsInsideBeginImplFrame());
client_->Reset();
// While swap throttled, BeginFrames should trigger BeginImplFrames,
// but not a BeginMainFrame or draw.
scheduler_->SetNeedsBeginMainFrame();
scheduler_->SetNeedsRedraw();
EXPECT_SCOPED(AdvanceFrame()); // Run posted BeginFrame.
EXPECT_ACTIONS("WillBeginImplFrame");
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
client_->Reset();
base::TimeTicks before_deadline, after_deadline;
// The deadline is set to the regular deadline.
before_deadline = task_runner_->NowTicks();
task_runner_->RunTasksWhile(client_->InsideBeginImplFrame(true));
after_deadline = task_runner_->NowTicks();
// We can't do an equality comparison here because the scheduler uses a fudge
// factor that's an internal implementation detail.
EXPECT_GT(after_deadline, before_deadline);
EXPECT_LT(after_deadline,
before_deadline + viz::BeginFrameArgs::DefaultInterval());
EXPECT_FALSE(client_->IsInsideBeginImplFrame());
client_->Reset();
EXPECT_SCOPED(AdvanceFrame()); // Run posted BeginFrame.
EXPECT_ACTIONS("WillBeginImplFrame");
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
client_->Reset();
// Take us out of a swap throttled state.
scheduler_->DidReceiveCompositorFrameAck();
EXPECT_ACTIONS("ScheduledActionSendBeginMainFrame");
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
client_->Reset();
// The deadline is set to the regular deadline.
before_deadline = task_runner_->NowTicks();
task_runner_->RunTasksWhile(client_->InsideBeginImplFrame(true));
after_deadline = task_runner_->NowTicks();
// We can't do an equality comparison here because the scheduler uses a fudge
// factor that's an internal implementation detail.
EXPECT_GT(after_deadline, before_deadline);
EXPECT_LT(after_deadline,
before_deadline + viz::BeginFrameArgs::DefaultInterval());
EXPECT_FALSE(client_->IsInsideBeginImplFrame());
client_->Reset();
}
TEST_F(SchedulerTest, SyntheticBeginFrames_IsDrawThrottled) {
BeginFramesNotFromClient_IsDrawThrottled(THROTTLED_BFS);
}
TEST_F(SchedulerTest, UnthrottledBeginFrames_IsDrawThrottled) {
BeginFramesNotFromClient_IsDrawThrottled(UNTHROTTLED_BFS);
}
TEST_F(SchedulerTest,
DidLoseLayerTreeFrameSinkAfterLayerTreeFrameSinkIsInitialized) {
SetUpSchedulerWithNoLayerTreeFrameSink(EXTERNAL_BFS);
scheduler_->SetVisible(true);
scheduler_->SetCanDraw(true);
EXPECT_ACTIONS("ScheduledActionBeginLayerTreeFrameSinkCreation");
client_->Reset();
scheduler_->DidCreateAndInitializeLayerTreeFrameSink();
EXPECT_NO_ACTION();
scheduler_->DidLoseLayerTreeFrameSink();
EXPECT_ACTIONS("ScheduledActionBeginLayerTreeFrameSinkCreation");
}
TEST_F(SchedulerTest, DidLoseLayerTreeFrameSinkAfterBeginFrameStarted) {
SetUpScheduler(EXTERNAL_BFS);
// SetNeedsBeginMainFrame should begin the frame.
scheduler_->SetNeedsBeginMainFrame();
EXPECT_ACTIONS("AddObserver(this)");
client_->Reset();
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame", "ScheduledActionSendBeginMainFrame");
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
client_->Reset();
scheduler_->DidLoseLayerTreeFrameSink();
// RemoveObserver(this) is not called until the end of the frame.
EXPECT_NO_ACTION();
client_->Reset();
scheduler_->NotifyBeginMainFrameStarted(task_runner_->NowTicks());
scheduler_->NotifyReadyToCommit(nullptr);
EXPECT_ACTIONS("ScheduledActionCommit", "ScheduledActionPostCommit",
"ScheduledActionActivateSyncTree");
client_->Reset();
task_runner_->RunTasksWhile(client_->InsideBeginImplFrame(true));
EXPECT_ACTIONS("ScheduledActionBeginLayerTreeFrameSinkCreation",
"RemoveObserver(this)");
}
TEST_F(SchedulerTest,
DidLoseLayerTreeFrameSinkAfterBeginFrameStartedWithHighLatency) {
SetUpScheduler(EXTERNAL_BFS);
// SetNeedsBeginMainFrame should begin the frame.
scheduler_->SetNeedsBeginMainFrame();
EXPECT_ACTIONS("AddObserver(this)");
client_->Reset();
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame", "ScheduledActionSendBeginMainFrame");
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
client_->Reset();
scheduler_->DidLoseLayerTreeFrameSink();
// Do nothing when impl frame is in deadine pending state.
EXPECT_NO_ACTION();
client_->Reset();
// Run posted deadline.
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
task_runner_->RunTasksWhile(client_->InsideBeginImplFrame(true));
// OnBeginImplFrameDeadline didn't schedule LayerTreeFrameSink creation
// because
// main frame is not yet completed.
EXPECT_ACTIONS("RemoveObserver(this)");
EXPECT_FALSE(client_->IsInsideBeginImplFrame());
// BeginImplFrame is not started.
client_->Reset();
task_runner_->RunUntilTime(task_runner_->NowTicks() + base::Milliseconds(10));
EXPECT_NO_ACTION();
EXPECT_FALSE(client_->IsInsideBeginImplFrame());
client_->Reset();
scheduler_->NotifyBeginMainFrameStarted(task_runner_->NowTicks());
scheduler_->NotifyReadyToCommit(nullptr);
EXPECT_ACTIONS("ScheduledActionCommit", "ScheduledActionPostCommit",
"ScheduledActionActivateSyncTree",
"ScheduledActionBeginLayerTreeFrameSinkCreation");
}
TEST_F(SchedulerTest, DidLoseLayerTreeFrameSinkAfterReadyToCommit) {
SetUpScheduler(EXTERNAL_BFS);
// SetNeedsBeginMainFrame should begin the frame.
scheduler_->SetNeedsBeginMainFrame();
EXPECT_ACTIONS("AddObserver(this)");
client_->Reset();
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame", "ScheduledActionSendBeginMainFrame");
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
client_->Reset();
scheduler_->NotifyBeginMainFrameStarted(task_runner_->NowTicks());
scheduler_->NotifyReadyToCommit(nullptr);
EXPECT_ACTIONS("ScheduledActionCommit", "ScheduledActionPostCommit");
client_->Reset();
scheduler_->DidLoseLayerTreeFrameSink();
// Sync tree should be forced to activate.
EXPECT_ACTIONS("ScheduledActionActivateSyncTree");
// RemoveObserver(this) is not called until the end of the frame.
client_->Reset();
task_runner_->RunTasksWhile(client_->InsideBeginImplFrame(true));
EXPECT_ACTIONS("ScheduledActionBeginLayerTreeFrameSinkCreation",
"RemoveObserver(this)");
}
TEST_F(SchedulerTest, DidLoseLayerTreeFrameSinkAfterSetNeedsPrepareTiles) {
SetUpScheduler(EXTERNAL_BFS);
scheduler_->SetNeedsPrepareTiles();
scheduler_->SetNeedsRedraw();
EXPECT_ACTIONS("AddObserver(this)");
client_->Reset();
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame");
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
client_->Reset();
scheduler_->DidLoseLayerTreeFrameSink();
// RemoveObserver(this) is not called until the end of the frame.
EXPECT_NO_ACTION();
client_->Reset();
task_runner_->RunTasksWhile(client_->InsideBeginImplFrame(true));
EXPECT_ACTIONS("ScheduledActionPrepareTiles",
"ScheduledActionBeginLayerTreeFrameSinkCreation",
"RemoveObserver(this)");
}
TEST_F(SchedulerTest, DidLoseLayerTreeFrameSinkWithDelayBasedBeginFrameSource) {
SetUpScheduler(THROTTLED_BFS);
// SetNeedsBeginMainFrame should begin the frame on the next BeginImplFrame.
EXPECT_FALSE(scheduler_->begin_frames_expected());
scheduler_->SetNeedsBeginMainFrame();
EXPECT_TRUE(scheduler_->begin_frames_expected());
client_->Reset();
AdvanceFrame();
EXPECT_ACTIONS("WillBeginImplFrame", "ScheduledActionSendBeginMainFrame");
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
EXPECT_TRUE(scheduler_->begin_frames_expected());
// NotifyReadyToCommit should trigger the commit.
client_->Reset();
scheduler_->NotifyBeginMainFrameStarted(task_runner_->NowTicks());
scheduler_->NotifyReadyToCommit(nullptr);
EXPECT_ACTIONS("ScheduledActionCommit", "ScheduledActionPostCommit");
EXPECT_TRUE(scheduler_->begin_frames_expected());
// NotifyReadyToActivate should trigger the activation.
client_->Reset();
scheduler_->NotifyReadyToActivate();
EXPECT_ACTIONS("ScheduledActionActivateSyncTree");
EXPECT_TRUE(scheduler_->begin_frames_expected());
client_->Reset();
scheduler_->DidLoseLayerTreeFrameSink();
// RemoveObserver(this) is not called until the end of the frame.
EXPECT_NO_ACTION();
EXPECT_TRUE(scheduler_->begin_frames_expected());
client_->Reset();
task_runner_->RunTasksWhile(client_->InsideBeginImplFrame(true));
EXPECT_ACTIONS("ScheduledActionBeginLayerTreeFrameSinkCreation");
EXPECT_FALSE(scheduler_->begin_frames_expected());
}
TEST_F(SchedulerTest, DidLoseLayerTreeFrameSinkWhenIdle) {
SetUpScheduler(EXTERNAL_BFS);
// SetNeedsBeginMainFrame should begin the frame.
scheduler_->SetNeedsBeginMainFrame();
EXPECT_ACTIONS("AddObserver(this)");
client_->Reset();
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame", "ScheduledActionSendBeginMainFrame");
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
client_->Reset();
scheduler_->NotifyBeginMainFrameStarted(task_runner_->NowTicks());
scheduler_->NotifyReadyToCommit(nullptr);
EXPECT_ACTIONS("ScheduledActionCommit", "ScheduledActionPostCommit");
client_->Reset();
scheduler_->NotifyReadyToActivate();
EXPECT_ACTIONS("ScheduledActionActivateSyncTree");
client_->Reset();
task_runner_->RunTasksWhile(client_->InsideBeginImplFrame(true));
EXPECT_ACTIONS("ScheduledActionDrawIfPossible");
// Idle time between BeginFrames.
client_->Reset();
scheduler_->DidLoseLayerTreeFrameSink();
EXPECT_ACTIONS("ScheduledActionBeginLayerTreeFrameSinkCreation",
"RemoveObserver(this)");
}
TEST_F(SchedulerTest, ScheduledActionActivateAfterBecomingInvisible) {
SetUpScheduler(EXTERNAL_BFS);
// SetNeedsBeginMainFrame should begin the frame.
scheduler_->SetNeedsBeginMainFrame();
EXPECT_ACTIONS("AddObserver(this)");
client_->Reset();
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame", "ScheduledActionSendBeginMainFrame");
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
client_->Reset();
scheduler_->NotifyBeginMainFrameStarted(task_runner_->NowTicks());
scheduler_->NotifyReadyToCommit(nullptr);
EXPECT_ACTIONS("ScheduledActionCommit", "ScheduledActionPostCommit");
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
client_->Reset();
scheduler_->SetVisible(false);
task_runner_->RunPendingTasks(); // Run posted deadline.
// Sync tree should be forced to activate.
EXPECT_ACTIONS("ScheduledActionActivateSyncTree", "RemoveObserver(this)");
}
TEST_F(SchedulerTest, ScheduledActionActivateAfterBeginFrameSourcePaused) {
SetUpScheduler(EXTERNAL_BFS);
// SetNeedsBeginMainFrame should begin the frame.
scheduler_->SetNeedsBeginMainFrame();
EXPECT_ACTIONS("AddObserver(this)");
client_->Reset();
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame", "ScheduledActionSendBeginMainFrame");
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
client_->Reset();
scheduler_->NotifyBeginMainFrameStarted(task_runner_->NowTicks());
scheduler_->NotifyReadyToCommit(nullptr);
EXPECT_ACTIONS("ScheduledActionCommit", "ScheduledActionPostCommit");
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
client_->Reset();
fake_external_begin_frame_source_->SetPaused(true);
task_runner_->RunPendingTasks(); // Run posted deadline.
// Sync tree should be forced to activate.
// Pausing the begin frame source aborts the draw. Then
// ProactiveBeginFrameWanted is no longer true, so the scheduler stops
// listening for begin frames.
EXPECT_ACTIONS("ScheduledActionActivateSyncTree", "RemoveObserver(this)");
}
// Tests to ensure frame sources can be successfully changed while drawing.
TEST_F(SchedulerTest, SwitchFrameSourceToUnthrottled) {
SetUpScheduler(EXTERNAL_BFS);
// SetNeedsRedraw should begin the frame on the next BeginImplFrame.
scheduler_->SetNeedsRedraw();
EXPECT_ACTIONS("AddObserver(this)");
client_->Reset();
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame");
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
EXPECT_TRUE(scheduler_->begin_frames_expected());
client_->Reset();
task_runner_->RunPendingTasks(); // Run posted deadline.
EXPECT_ACTIONS("ScheduledActionDrawIfPossible");
scheduler_->SetNeedsRedraw();
// Switch to an unthrottled frame source.
scheduler_->SetBeginFrameSource(unthrottled_frame_source_.get());
client_->Reset();
// Unthrottled frame source will immediately begin a new frame.
task_runner_->RunPendingTasks(); // Run posted BeginFrame.
EXPECT_ACTIONS("WillBeginImplFrame");
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
client_->Reset();
// If we don't swap on the deadline, we wait for the next BeginFrame.
task_runner_->RunPendingTasks(); // Run posted deadline.
EXPECT_ACTIONS("ScheduledActionDrawIfPossible");
EXPECT_FALSE(client_->IsInsideBeginImplFrame());
client_->Reset();
}
// Tests to ensure frame sources can be successfully changed while a frame
// deadline is pending.
TEST_F(SchedulerTest, SwitchFrameSourceToUnthrottledBeforeDeadline) {
SetUpScheduler(EXTERNAL_BFS);
// SetNeedsRedraw should begin the frame on the next BeginImplFrame.
scheduler_->SetNeedsRedraw();
EXPECT_ACTIONS("AddObserver(this)");
client_->Reset();
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame");
// Switch to an unthrottled frame source before the frame deadline is hit.
scheduler_->SetBeginFrameSource(unthrottled_frame_source_.get());
client_->Reset();
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
EXPECT_TRUE(scheduler_->begin_frames_expected());
client_->Reset();
task_runner_->RunPendingTasks(); // Run posted deadline.
EXPECT_ACTIONS("ScheduledActionDrawIfPossible",
// Unthrottled frame source will immediately begin a new frame.
"WillBeginImplFrame");
scheduler_->SetNeedsRedraw();
client_->Reset();
task_runner_->RunPendingTasks(); // Run posted deadline.
EXPECT_ACTIONS("ScheduledActionDrawIfPossible");
EXPECT_FALSE(client_->IsInsideBeginImplFrame());
client_->Reset();
}
// Tests to ensure that the active frame source can successfully be changed from
// unthrottled to throttled.
TEST_F(SchedulerTest, SwitchFrameSourceToThrottled) {
SetUpScheduler(UNTHROTTLED_BFS);
scheduler_->SetNeedsRedraw();
EXPECT_NO_ACTION();
client_->Reset();
task_runner_->RunPendingTasks(); // Run posted BeginFrame.
EXPECT_ACTIONS("WillBeginImplFrame");
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
client_->Reset();
task_runner_->RunPendingTasks(); // Run posted deadline.
EXPECT_ACTIONS("ScheduledActionDrawIfPossible");
EXPECT_FALSE(client_->IsInsideBeginImplFrame());
client_->Reset();
// Switch to a throttled frame source.
scheduler_->SetBeginFrameSource(fake_external_begin_frame_source_.get());
client_->Reset();
// SetNeedsRedraw should begin the frame on the next BeginImplFrame.
scheduler_->SetNeedsRedraw();
task_runner_->RunPendingTasks();
EXPECT_NO_ACTION();
client_->Reset();
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame");
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
EXPECT_TRUE(scheduler_->begin_frames_expected());
client_->Reset();
task_runner_->RunPendingTasks(); // Run posted deadline.
EXPECT_ACTIONS("ScheduledActionDrawIfPossible");
}
TEST_F(SchedulerTest, SwitchFrameSourceToNullInsideDeadline) {
SetUpScheduler(EXTERNAL_BFS);
scheduler_->SetNeedsRedraw();
EXPECT_ACTIONS("AddObserver(this)");
client_->Reset();
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame");
client_->Reset();
// Switch to a null frame source.
scheduler_->SetBeginFrameSource(nullptr);
EXPECT_ACTIONS("RemoveObserver(this)");
client_->Reset();
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
task_runner_->RunPendingTasks(); // Run posted deadline.
EXPECT_ACTIONS("ScheduledActionDrawIfPossible");
EXPECT_FALSE(scheduler_->begin_frames_expected());
EXPECT_FALSE(client_->IsInsideBeginImplFrame());
client_->Reset();
// AdvanceFrame helper can't be used here because there's no deadline posted.
scheduler_->SetNeedsRedraw();
EXPECT_FALSE(client_->IsInsideBeginImplFrame());
EXPECT_NO_ACTION();
client_->Reset();
scheduler_->SetNeedsBeginMainFrame();
EXPECT_FALSE(client_->IsInsideBeginImplFrame());
EXPECT_NO_ACTION();
client_->Reset();
// Switch back to the same source, make sure frames continue to be produced.
scheduler_->SetBeginFrameSource(fake_external_begin_frame_source_.get());
EXPECT_ACTIONS("AddObserver(this)");
EXPECT_FALSE(client_->IsInsideBeginImplFrame());
client_->Reset();
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame", "ScheduledActionSendBeginMainFrame");
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
client_->Reset();
task_runner_->RunPendingTasks();
EXPECT_ACTIONS("ScheduledActionDrawIfPossible");
}
// This test maskes sure that switching a frame source when not observing
// such as when not visible also works.
TEST_F(SchedulerTest, SwitchFrameSourceWhenNotObserving) {
SetUpScheduler(EXTERNAL_BFS);
// SetNeedsBeginMainFrame should begin the frame on the next BeginImplFrame.
scheduler_->SetNeedsBeginMainFrame();
EXPECT_ACTIONS("AddObserver(this)");
client_->Reset();
// Begin new frame.
EXPECT_SCOPED(AdvanceFrame());
scheduler_->NotifyBeginMainFrameStarted(task_runner_->NowTicks());
EXPECT_ACTIONS("WillBeginImplFrame", "ScheduledActionSendBeginMainFrame");
client_->Reset();
scheduler_->NotifyReadyToCommit(nullptr);
EXPECT_ACTIONS("ScheduledActionCommit", "ScheduledActionPostCommit");
client_->Reset();
scheduler_->NotifyReadyToActivate();
EXPECT_ACTIONS("ScheduledActionActivateSyncTree");
// Scheduler loses LayerTreeFrameSink, and stops waiting for ready to draw
// signal.
client_->Reset();
scheduler_->DidLoseLayerTreeFrameSink();
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
task_runner_->RunPendingTasks();
EXPECT_ACTIONS("ScheduledActionBeginLayerTreeFrameSinkCreation",
"RemoveObserver(this)");
// Changing begin frame source doesn't do anything.
// The unthrottled source doesn't print Add/RemoveObserver like the fake one.
client_->Reset();
scheduler_->SetBeginFrameSource(unthrottled_frame_source_.get());
EXPECT_NO_ACTION();
client_->Reset();
scheduler_->DidCreateAndInitializeLayerTreeFrameSink();
EXPECT_NO_ACTION();
client_->Reset();
scheduler_->SetNeedsBeginMainFrame();
EXPECT_NO_ACTION();
client_->Reset();
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame", "ScheduledActionSendBeginMainFrame");
}
// Tests to ensure that we send a ScheduledActionBeginMainFrameNotExpectedUntil
// when expected.
TEST_F(SchedulerTest, ScheduledActionBeginMainFrameNotExpectedUntil) {
SetUpScheduler(EXTERNAL_BFS);
scheduler_->SetNeedsRedraw();
EXPECT_ACTIONS("AddObserver(this)");
client_->Reset();
EXPECT_SCOPED(AdvanceFrame());
scheduler_->SetMainThreadWantsBeginMainFrameNotExpected(true);
task_runner_->RunPendingTasks();
EXPECT_ACTIONS("WillBeginImplFrame",
"ScheduledActionBeginMainFrameNotExpectedUntil",
"ScheduledActionDrawIfPossible");
}
// Tests to ensure that BeginMainFrameNotExpectedUntil is only sent once within
// the same frame.
TEST_F(SchedulerTest,
ScheduledActionBeginMainFrameNotExpectedUntilSentOnlyOncePerFrame) {
SetUpScheduler(EXTERNAL_BFS);
scheduler_->SetNeedsRedraw();
EXPECT_ACTIONS("AddObserver(this)");
client_->Reset();
EXPECT_SCOPED(AdvanceFrame());
scheduler_->SetMainThreadWantsBeginMainFrameNotExpected(true);
task_runner_->RunPendingTasks();
EXPECT_ACTIONS("WillBeginImplFrame",
"ScheduledActionBeginMainFrameNotExpectedUntil",
"ScheduledActionDrawIfPossible");
client_->Reset();
scheduler_->SetMainThreadWantsBeginMainFrameNotExpected(false);
task_runner_->RunPendingTasks();
EXPECT_NO_ACTION();
scheduler_->SetMainThreadWantsBeginMainFrameNotExpected(true);
task_runner_->RunPendingTasks();
EXPECT_NO_ACTION();
}
// Tests to ensure that we send a BeginMainFrameNotExpectedSoon when expected.
TEST_F(SchedulerTest, SendBeginMainFrameNotExpectedSoon_Requested) {
SetUpScheduler(EXTERNAL_BFS);
// SetNeedsBeginMainFrame should begin the frame on the next BeginImplFrame.
scheduler_->SetNeedsBeginMainFrame();
EXPECT_ACTIONS("AddObserver(this)");
client_->Reset();
// Trigger a frame draw.
EXPECT_SCOPED(AdvanceFrame());
scheduler_->NotifyBeginMainFrameStarted(task_runner_->NowTicks());
scheduler_->NotifyReadyToCommit(nullptr);
scheduler_->NotifyReadyToActivate();
task_runner_->RunPendingTasks();
EXPECT_ACTIONS("WillBeginImplFrame", "ScheduledActionSendBeginMainFrame",
"ScheduledActionCommit", "ScheduledActionPostCommit",
"ScheduledActionActivateSyncTree",
"ScheduledActionDrawIfPossible");
client_->Reset();
scheduler_->SetMainThreadWantsBeginMainFrameNotExpected(true);
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame",
"ScheduledActionBeginMainFrameNotExpectedUntil");
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
client_->Reset();
// The BeginImplFrame deadline should SetNeedsBeginFrame(false) and send a
// SendBeginMainFrameNotExpectedSoon.
task_runner_->RunPendingTasks(); // Run posted deadline.
EXPECT_ACTIONS("SendBeginMainFrameNotExpectedSoon", "RemoveObserver(this)");
client_->Reset();
}
// Tests to ensure that we dont't send a BeginMainFrameNotExpectedSoon when
// possible but not requested.
TEST_F(SchedulerTest, SendBeginMainFrameNotExpectedSoon_Unrequested) {
SetUpScheduler(EXTERNAL_BFS);
// SetNeedsBeginMainFrame should begin the frame on the next BeginImplFrame.
scheduler_->SetNeedsBeginMainFrame();
EXPECT_ACTIONS("AddObserver(this)");
client_->Reset();
// Trigger a frame draw.
EXPECT_SCOPED(AdvanceFrame());
scheduler_->NotifyBeginMainFrameStarted(task_runner_->NowTicks());
scheduler_->NotifyReadyToCommit(nullptr);
scheduler_->NotifyReadyToActivate();
task_runner_->RunPendingTasks();
EXPECT_ACTIONS("WillBeginImplFrame", "ScheduledActionSendBeginMainFrame",
"ScheduledActionCommit", "ScheduledActionPostCommit",
"ScheduledActionActivateSyncTree",
"ScheduledActionDrawIfPossible");
client_->Reset();
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame");
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
client_->Reset();
// The BeginImplFrame deadline should SetNeedsBeginFrame(false), but doesn't
// send a SendBeginMainFrameNotExpectedSoon as it's not been requested by the
// main thread.
task_runner_->RunPendingTasks(); // Run posted deadline.
EXPECT_ACTIONS("RemoveObserver(this)");
client_->Reset();
scheduler_->SetMainThreadWantsBeginMainFrameNotExpected(true);
EXPECT_ACTIONS("SendBeginMainFrameNotExpectedSoon");
}
// Tests to ensure that we send a BeginMainFrameNotExpectedSoon only once per
// frame.
TEST_F(SchedulerTest, SendBeginMainFrameNotExpectedSoonOnlyOncePerFrame) {
SetUpScheduler(EXTERNAL_BFS);
// SetNeedsBeginMainFrame should begin the frame on the next BeginImplFrame.
scheduler_->SetNeedsBeginMainFrame();
EXPECT_ACTIONS("AddObserver(this)");
client_->Reset();
// Trigger a frame draw.
EXPECT_SCOPED(AdvanceFrame());
scheduler_->NotifyBeginMainFrameStarted(task_runner_->NowTicks());
scheduler_->NotifyReadyToCommit(nullptr);
scheduler_->NotifyReadyToActivate();
task_runner_->RunPendingTasks();
EXPECT_ACTIONS("WillBeginImplFrame", "ScheduledActionSendBeginMainFrame",
"ScheduledActionCommit", "ScheduledActionPostCommit",
"ScheduledActionActivateSyncTree",
"ScheduledActionDrawIfPossible");
client_->Reset();
scheduler_->SetMainThreadWantsBeginMainFrameNotExpected(true);
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame",
"ScheduledActionBeginMainFrameNotExpectedUntil");
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
client_->Reset();
task_runner_->RunPendingTasks(); // Run posted deadline.
EXPECT_ACTIONS("SendBeginMainFrameNotExpectedSoon", "RemoveObserver(this)");
client_->Reset();
scheduler_->SetMainThreadWantsBeginMainFrameNotExpected(false);
EXPECT_NO_ACTION();
scheduler_->SetMainThreadWantsBeginMainFrameNotExpected(true);
EXPECT_NO_ACTION();
}
// Tests to ensure that we send a BeginMainFrameNotExpectedSoon in situations
// where the client doesn't want messages when we first stopped observing
// BeginFrames but later does.
TEST_F(SchedulerTest, SendBeginMainFrameNotExpectedSoon_AlreadyIdle) {
SetUpScheduler(EXTERNAL_BFS);
// SetNeedsBeginMainFrame should begin the frame on the next BeginImplFrame.
scheduler_->SetNeedsBeginMainFrame();
EXPECT_ACTIONS("AddObserver(this)");
client_->Reset();
// Trigger a frame draw.
EXPECT_SCOPED(AdvanceFrame());
scheduler_->NotifyBeginMainFrameStarted(task_runner_->NowTicks());
scheduler_->NotifyReadyToCommit(nullptr);
scheduler_->NotifyReadyToActivate();
task_runner_->RunPendingTasks();
EXPECT_ACTIONS("WillBeginImplFrame", "ScheduledActionSendBeginMainFrame",
"ScheduledActionCommit", "ScheduledActionPostCommit",
"ScheduledActionActivateSyncTree",
"ScheduledActionDrawIfPossible");
client_->Reset();
EXPECT_SCOPED(AdvanceFrame());
task_runner_->RunPendingTasks(); // Run posted deadline.
EXPECT_ACTIONS("WillBeginImplFrame", "RemoveObserver(this)");
client_->Reset();
scheduler_->SetMainThreadWantsBeginMainFrameNotExpected(true);
EXPECT_ACTIONS("SendBeginMainFrameNotExpectedSoon");
}
// This tests to ensure BeginMainFrameNotExpectedSoon is sent during idle
// periods if (1) it initially wasn't sent because the message wasn't needed at
// the time, and (2) the BeginMainFrameNotExpectedUntil was already sent in the
// frame (crbug.com/893653).
TEST_F(SchedulerTest, SendBeginMainFrameNotExpectedSoonDuringIdleIfNeeded) {
SetUpScheduler(EXTERNAL_BFS);
scheduler_->SetNeedsRedraw();
EXPECT_ACTIONS("AddObserver(this)");
client_->Reset();
EXPECT_SCOPED(AdvanceFrame());
task_runner_->RunPendingTasks();
EXPECT_ACTIONS("WillBeginImplFrame", "ScheduledActionDrawIfPossible");
client_->Reset();
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame");
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
client_->Reset();
// Toggle WantsBeginMainFrameNotExpected while inside BeginImplFrame. This
// causes the BeginMainFrameNotExpectedUntil message to get sent and the
// BeginMainFrameNotExpectedSoon message to be withheld.
scheduler_->SetMainThreadWantsBeginMainFrameNotExpected(true);
EXPECT_ACTIONS("ScheduledActionBeginMainFrameNotExpectedUntil");
client_->Reset();
scheduler_->SetMainThreadWantsBeginMainFrameNotExpected(false);
task_runner_->RunPendingTasks();
EXPECT_ACTIONS("RemoveObserver(this)");
EXPECT_FALSE(client_->IsInsideBeginImplFrame());
client_->Reset();
scheduler_->SetMainThreadWantsBeginMainFrameNotExpected(true);
EXPECT_ACTIONS("SendBeginMainFrameNotExpectedSoon");
}
// This tests to ensure BeginMainFrameNotExpectedSoon is sent during idle
// periods if (1) it initially wasn't sent because the message wasn't needed at
// the time, and (2) |scheduler_|.visible() is false.
TEST_F(SchedulerTest,
ScheduledActionBeginMainFrameNotSoonSentDuringIdleIfNeededNotVisible) {
SetUpScheduler(EXTERNAL_BFS);
scheduler_->SetNeedsRedraw();
EXPECT_ACTIONS("AddObserver(this)");
client_->Reset();
EXPECT_SCOPED(AdvanceFrame());
task_runner_->RunPendingTasks();
EXPECT_ACTIONS("WillBeginImplFrame", "ScheduledActionDrawIfPossible");
client_->Reset();
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame");
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
client_->Reset();
scheduler_->SetVisible(false);
task_runner_->RunPendingTasks();
EXPECT_ACTIONS("RemoveObserver(this)");
EXPECT_FALSE(client_->IsInsideBeginImplFrame());
// The scheduler won't send BeginMainFrameNotExpectedUntil messages while not
// visible, but it needs to send a BeginMainFrameNotExpectedSoon to let the
// client know it's gone idle.
client_->Reset();
scheduler_->SetMainThreadWantsBeginMainFrameNotExpected(true);
EXPECT_ACTIONS("SendBeginMainFrameNotExpectedSoon");
}
TEST_F(SchedulerTest, SynchronousCompositorAnimation) {
scheduler_settings_.using_synchronous_renderer_compositor = true;
SetUpScheduler(EXTERNAL_BFS);
scheduler_->SetNeedsOneBeginImplFrame();
EXPECT_ACTIONS("AddObserver(this)");
client_->Reset();
// Testing the case where animation ticks a fling scroll.
client_->SetWillBeginImplFrameCausesRedraw(true);
// The animation isn't done so it'll cause another tick in the future.
client_->SetWillBeginImplFrameRequestsOneBeginImplFrame(true);
// Next vsync.
AdvanceFrame();
EXPECT_ACTIONS("WillBeginImplFrame",
"ScheduledActionInvalidateLayerTreeFrameSink");
// Finish frame is deferred until next draw since there was invalidation.
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
client_->Reset();
// Android onDraw. This doesn't consume the single begin frame request.
scheduler_->SetNeedsRedraw();
bool resourceless_software_draw = false;
bool skip_draw = false;
scheduler_->OnDrawForLayerTreeFrameSink(resourceless_software_draw,
skip_draw);
EXPECT_ACTIONS("ScheduledActionDrawIfPossible");
EXPECT_FALSE(client_->IsInsideBeginImplFrame());
client_->Reset();
// The animation inside of WillBeginImplFrame changes stuff on the screen, but
// ends here, so does not cause another frame to happen.
client_->SetWillBeginImplFrameCausesRedraw(true);
// Next vsync.
AdvanceFrame();
EXPECT_ACTIONS("WillBeginImplFrame",
"ScheduledActionInvalidateLayerTreeFrameSink");
// Finish frame is deferred until next draw since there was invalidation.
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
client_->Reset();
// Android onDraw.
scheduler_->SetNeedsRedraw();
scheduler_->OnDrawForLayerTreeFrameSink(resourceless_software_draw,
skip_draw);
EXPECT_ACTIONS("ScheduledActionDrawIfPossible");
EXPECT_FALSE(client_->IsInsideBeginImplFrame());
client_->Reset();
// Idle on next vsync, as the animation has completed.
AdvanceFrame();
EXPECT_ACTIONS("WillBeginImplFrame", "RemoveObserver(this)");
EXPECT_FALSE(client_->IsInsideBeginImplFrame());
client_->Reset();
}
TEST_F(SchedulerTest, SynchronousCompositorOnDrawDuringIdle) {
scheduler_settings_.using_synchronous_renderer_compositor = true;
SetUpScheduler(EXTERNAL_BFS);
scheduler_->SetNeedsRedraw();
bool resourceless_software_draw = false;
bool skip_draw = false;
scheduler_->OnDrawForLayerTreeFrameSink(resourceless_software_draw,
skip_draw);
EXPECT_ACTIONS("AddObserver(this)", "ScheduledActionDrawIfPossible");
EXPECT_FALSE(client_->IsInsideBeginImplFrame());
client_->Reset();
// Idle on next vsync.
AdvanceFrame();
EXPECT_ACTIONS("WillBeginImplFrame", "RemoveObserver(this)");
EXPECT_FALSE(client_->IsInsideBeginImplFrame());
client_->Reset();
}
TEST_F(SchedulerTest, InvalidateLayerTreeFrameSinkWhenCannotDraw) {
scheduler_settings_.using_synchronous_renderer_compositor = true;
SetUpScheduler(EXTERNAL_BFS);
scheduler_->SetCanDraw(false);
scheduler_->SetNeedsRedraw();
EXPECT_ACTIONS("AddObserver(this)");
client_->Reset();
// Do not invalidate in next BeginFrame.
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame");
// Redraw is not cleared.
EXPECT_TRUE(scheduler_->RedrawPending());
scheduler_->SetCanDraw(true);
client_->Reset();
// Do invalidate in next BeginFrame.
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame",
"ScheduledActionInvalidateLayerTreeFrameSink");
client_->Reset();
bool resourceless_software_draw = false;
bool skip_draw = false;
scheduler_->OnDrawForLayerTreeFrameSink(resourceless_software_draw,
skip_draw);
EXPECT_ACTIONS("ScheduledActionDrawIfPossible");
EXPECT_FALSE(scheduler_->RedrawPending());
}
TEST_F(SchedulerTest, NeedsPrepareTilesInvalidates) {
// This is to test that SetNeedsPrepareTiles causes invalidates even if
// CanDraw is false.
scheduler_settings_.using_synchronous_renderer_compositor = true;
SetUpScheduler(EXTERNAL_BFS);
scheduler_->SetCanDraw(false);
scheduler_->SetNeedsPrepareTiles();
EXPECT_ACTIONS("AddObserver(this)");
client_->Reset();
// Do not invalidate in next BeginFrame.
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame",
"ScheduledActionInvalidateLayerTreeFrameSink");
client_->Reset();
}
TEST_F(SchedulerTest, SetNeedsOneBeginImplFrame) {
SetUpScheduler(EXTERNAL_BFS);
EXPECT_FALSE(scheduler_->begin_frames_expected());
// Request a frame, should kick the source.
scheduler_->SetNeedsOneBeginImplFrame();
EXPECT_ACTIONS("AddObserver(this)");
client_->Reset();
// The incoming WillBeginImplFrame will request another one.
client_->SetWillBeginImplFrameRequestsOneBeginImplFrame(true);
// Next vsync, the first requested frame happens.
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame");
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
client_->Reset();
// We don't request another frame here.
// Next vsync, the second requested frame happens (the one requested inside
// the previous frame's begin impl frame step).
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame");
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
client_->Reset();
// End that frame's deadline.
task_runner_->RunTasksWhile(client_->InsideBeginImplFrame(true));
EXPECT_FALSE(client_->IsInsideBeginImplFrame());
// Scheduler shuts down the source now that no begin frame is requested.
EXPECT_ACTIONS("RemoveObserver(this)");
}
TEST_F(SchedulerTest, AbortEarlyIfNoDamage) {
SetUpScheduler(EXTERNAL_BFS);
// WillBeginImplFrame will return false, so draws should never be scheduled
// and client_->num_draws() should stay at 0.
client_->SetWillBeginImplFrameMightHaveDamage(false);
scheduler_->SetNeedsRedraw();
EXPECT_EQ(0, client_->num_draws());
EXPECT_ACTIONS("AddObserver(this)");
EXPECT_SCOPED(AdvanceFrame());
task_runner_->RunPendingTasks(); // Run posted deadline.
// Should not try to schedule a draw. (ScheduledActionDrawIfPossible should
// not appear.)
// When the frame is aborted, the scheduler does not ask for a proactive begin
// frame, so stop listening for begin frames.
EXPECT_ACTIONS("AddObserver(this)", "WillBeginImplFrame",
"RemoveObserver(this)");
EXPECT_EQ(0, client_->num_draws());
scheduler_->SetNeedsRedraw();
EXPECT_SCOPED(AdvanceFrame());
task_runner_->RunPendingTasks(); // Run posted deadline.
EXPECT_EQ(0, client_->num_draws());
}
TEST_F(SchedulerTest, SkipDraw) {
scheduler_settings_.using_synchronous_renderer_compositor = true;
SetUpScheduler(EXTERNAL_BFS);
scheduler_->SetNeedsOneBeginImplFrame();
EXPECT_ACTIONS("AddObserver(this)");
client_->Reset();
client_->SetWillBeginImplFrameCausesRedraw(true);
// Next vsync.
AdvanceFrame();
EXPECT_ACTIONS("WillBeginImplFrame",
"ScheduledActionInvalidateLayerTreeFrameSink");
EXPECT_TRUE(client_->invalidate_needs_redraw());
client_->Reset();
// Android onDraw. This doesn't consume the single begin frame request.
scheduler_->SetNeedsPrepareTiles();
bool resourceless_software_draw = false;
bool skip_draw = false;
scheduler_->OnDrawForLayerTreeFrameSink(resourceless_software_draw,
skip_draw);
EXPECT_ACTIONS("ScheduledActionDrawIfPossible",
"ScheduledActionPrepareTiles");
client_->Reset();
// Next vsync.
scheduler_->SetNeedsPrepareTiles();
AdvanceFrame();
EXPECT_ACTIONS("WillBeginImplFrame",
"ScheduledActionInvalidateLayerTreeFrameSink");
EXPECT_FALSE(client_->invalidate_needs_redraw());
client_->Reset();
// Android onDraw.
scheduler_->SetNeedsRedraw();
scheduler_->SetNeedsPrepareTiles();
client_->SetInvalidateNeedsRedraw(false);
skip_draw = true;
scheduler_->OnDrawForLayerTreeFrameSink(resourceless_software_draw,
skip_draw);
EXPECT_ACTIONS("ScheduledActionPrepareTiles");
client_->Reset();
}
TEST_F(SchedulerTest, SynchronousCompositorCommitAndVerifyBeginFrameAcks) {
scheduler_settings_.using_synchronous_renderer_compositor = true;
SetUpScheduler(EXTERNAL_BFS);
scheduler_->SetNeedsBeginMainFrame();
EXPECT_ACTIONS("AddObserver(this)");
client_->Reset();
// Next vsync.
viz::BeginFrameArgs args = SendNextBeginFrame();
EXPECT_ACTIONS("WillBeginImplFrame", "ScheduledActionSendBeginMainFrame");
EXPECT_FALSE(client_->IsInsideBeginImplFrame());
bool has_damage = false;
EXPECT_EQ(viz::BeginFrameAck(args, has_damage),
client_->last_begin_frame_ack());
client_->Reset();
scheduler_->NotifyBeginMainFrameStarted(task_runner_->NowTicks());
EXPECT_NO_ACTION();
// Next vsync.
args = SendNextBeginFrame();
EXPECT_ACTIONS("WillBeginImplFrame");
EXPECT_FALSE(client_->IsInsideBeginImplFrame());
has_damage = false;
EXPECT_EQ(viz::BeginFrameAck(args, has_damage),
client_->last_begin_frame_ack());
client_->Reset();
scheduler_->NotifyReadyToCommit(nullptr);
EXPECT_ACTIONS("ScheduledActionCommit", "ScheduledActionPostCommit");
client_->Reset();
scheduler_->NotifyReadyToActivate();
EXPECT_ACTIONS("ScheduledActionActivateSyncTree");
client_->Reset();
// Next vsync.
args = SendNextBeginFrame();
EXPECT_ACTIONS("WillBeginImplFrame",
"ScheduledActionInvalidateLayerTreeFrameSink");
// Finish frame is deferred until next draw since there was invalidation.
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
client_->Reset();
// Android onDraw.
scheduler_->SetNeedsRedraw();
bool resourceless_software_draw = false;
bool skip_draw = false;
scheduler_->OnDrawForLayerTreeFrameSink(resourceless_software_draw,
skip_draw);
EXPECT_ACTIONS("ScheduledActionDrawIfPossible");
EXPECT_FALSE(client_->IsInsideBeginImplFrame());
client_->Reset();
// Idle on next vsync.
args = SendNextBeginFrame();
EXPECT_ACTIONS("WillBeginImplFrame", "RemoveObserver(this)");
EXPECT_FALSE(client_->IsInsideBeginImplFrame());
has_damage = false;
EXPECT_EQ(viz::BeginFrameAck(args, has_damage),
client_->last_begin_frame_ack());
client_->Reset();
}
class SchedulerClientSetNeedsPrepareTilesOnDraw : public FakeSchedulerClient {
public:
SchedulerClientSetNeedsPrepareTilesOnDraw() : FakeSchedulerClient() {}
protected:
DrawResult ScheduledActionDrawIfPossible() override {
scheduler_->SetNeedsPrepareTiles();
return FakeSchedulerClient::ScheduledActionDrawIfPossible();
}
};
TEST_F(SchedulerTest, SynchronousCompositorPrepareTilesOnDraw) {
scheduler_settings_.using_synchronous_renderer_compositor = true;
std::unique_ptr<FakeSchedulerClient> client =
base::WrapUnique(new SchedulerClientSetNeedsPrepareTilesOnDraw);
SetUpScheduler(EXTERNAL_BFS, std::move(client));
scheduler_->SetNeedsRedraw();
EXPECT_ACTIONS("AddObserver(this)");
client_->Reset();
// Next vsync.
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame",
"ScheduledActionInvalidateLayerTreeFrameSink");
client_->Reset();
// Android onDraw.
scheduler_->SetNeedsRedraw();
bool resourceless_software_draw = false;
bool skip_draw = false;
scheduler_->OnDrawForLayerTreeFrameSink(resourceless_software_draw,
skip_draw);
EXPECT_ACTIONS("ScheduledActionDrawIfPossible",
"ScheduledActionPrepareTiles");
EXPECT_FALSE(client_->IsInsideBeginImplFrame());
EXPECT_FALSE(scheduler_->PrepareTilesPending());
client_->Reset();
// Android onDraw.
scheduler_->SetNeedsRedraw();
scheduler_->OnDrawForLayerTreeFrameSink(resourceless_software_draw,
skip_draw);
EXPECT_ACTIONS("ScheduledActionDrawIfPossible",
"ScheduledActionPrepareTiles");
EXPECT_FALSE(client_->IsInsideBeginImplFrame());
EXPECT_FALSE(scheduler_->PrepareTilesPending());
client_->Reset();
// Next vsync.
EXPECT_SCOPED(AdvanceFrame());
EXPECT_FALSE(scheduler_->PrepareTilesPending());
EXPECT_ACTIONS("WillBeginImplFrame", "RemoveObserver(this)");
EXPECT_FALSE(scheduler_->begin_frames_expected());
client_->Reset();
}
// Synchronous compositor does not require the active tree to be drawn at least
// once before the next activation. This test verifies two commit-activate
// cycles without draw work correctly.
TEST_F(SchedulerTest, SynchronousCompositorAllowsActivateBeforeDraw) {
scheduler_settings_.using_synchronous_renderer_compositor = true;
std::unique_ptr<FakeSchedulerClient> client =
base::WrapUnique(new SchedulerClientSetNeedsPrepareTilesOnDraw);
SetUpScheduler(EXTERNAL_BFS, std::move(client));
scheduler_->SetNeedsRedraw();
EXPECT_ACTIONS("AddObserver(this)");
client_->Reset();
// Next vsync.
scheduler_->SetNeedsBeginMainFrame();
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame", "ScheduledActionSendBeginMainFrame",
"ScheduledActionInvalidateLayerTreeFrameSink");
client_->Reset();
// Commit and activate.
scheduler_->NotifyBeginMainFrameStarted(task_runner_->NowTicks());
scheduler_->NotifyReadyToCommit(nullptr);
EXPECT_ACTIONS("ScheduledActionCommit", "ScheduledActionPostCommit");
client_->Reset();
scheduler_->NotifyReadyToActivate();
EXPECT_ACTIONS("ScheduledActionActivateSyncTree");
client_->Reset();
// No Draw.
// Next vsync.
scheduler_->SetNeedsBeginMainFrame();
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame", "ScheduledActionSendBeginMainFrame",
"ScheduledActionInvalidateLayerTreeFrameSink");
client_->Reset();
// Commit and activate.
scheduler_->NotifyBeginMainFrameStarted(task_runner_->NowTicks());
scheduler_->NotifyReadyToCommit(nullptr);
EXPECT_ACTIONS("ScheduledActionCommit", "ScheduledActionPostCommit");
client_->Reset();
scheduler_->NotifyReadyToActivate();
EXPECT_ACTIONS("ScheduledActionActivateSyncTree");
client_->Reset();
}
TEST_F(SchedulerTest, SetNeedsRedrawFromWillBeginImplFrame) {
client_ = std::make_unique<FakeSchedulerClient>();
CreateScheduler(EXTERNAL_BFS);
scheduler_->SetVisible(true);
scheduler_->SetCanDraw(true);
client_->SetWillBeginImplFrameCausesRedraw(true);
scheduler_->DidCreateAndInitializeLayerTreeFrameSink();
scheduler_->SetNeedsBeginMainFrame();
AdvanceFrame();
EXPECT_ACTIONS("ScheduledActionBeginLayerTreeFrameSinkCreation",
"AddObserver(this)", "WillBeginImplFrame",
"ScheduledActionSendBeginMainFrame");
EXPECT_TRUE(scheduler_->RedrawPending());
// WillBeginFrame calls Scheduler::SetNeedsRedraw, which could try to run
// another action. If none of the EXPECT_FALSE(inside_action_)s in
// FakeSchedulerClient fail, we know we didn't re-enter the scheduler.
}
TEST_F(SchedulerTest, SynchronousCompositorSendBeginMainFrameWhileIdle) {
scheduler_settings_.using_synchronous_renderer_compositor = true;
SetUpScheduler(EXTERNAL_BFS);
scheduler_->SetNeedsRedraw();
EXPECT_ACTIONS("AddObserver(this)");
client_->Reset();
// Next vsync.
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame",
"ScheduledActionInvalidateLayerTreeFrameSink");
client_->Reset();
// Android onDraw.
scheduler_->SetNeedsRedraw();
bool resourceless_software_draw = false;
bool skip_draw = false;
scheduler_->OnDrawForLayerTreeFrameSink(resourceless_software_draw,
skip_draw);
EXPECT_ACTIONS("ScheduledActionDrawIfPossible");
EXPECT_FALSE(client_->IsInsideBeginImplFrame());
EXPECT_FALSE(scheduler_->PrepareTilesPending());
client_->Reset();
// Simulate SetNeedsBeginMainFrame due to input event.
scheduler_->SetNeedsBeginMainFrame();
EXPECT_ACTIONS("ScheduledActionSendBeginMainFrame");
client_->Reset();
scheduler_->NotifyBeginMainFrameStarted(task_runner_->NowTicks());
scheduler_->NotifyReadyToCommit(nullptr);
EXPECT_ACTIONS("ScheduledActionCommit", "ScheduledActionPostCommit");
client_->Reset();
scheduler_->NotifyReadyToActivate();
EXPECT_ACTIONS("ScheduledActionActivateSyncTree");
client_->Reset();
// Next vsync.
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame",
"ScheduledActionInvalidateLayerTreeFrameSink");
client_->Reset();
// Android onDraw.
scheduler_->SetNeedsRedraw();
scheduler_->OnDrawForLayerTreeFrameSink(resourceless_software_draw,
skip_draw);
EXPECT_ACTIONS("ScheduledActionDrawIfPossible");
EXPECT_FALSE(client_->IsInsideBeginImplFrame());
EXPECT_FALSE(scheduler_->PrepareTilesPending());
client_->Reset();
// Simulate SetNeedsBeginMainFrame due to input event.
scheduler_->SetNeedsBeginMainFrame();
EXPECT_ACTIONS("ScheduledActionSendBeginMainFrame");
client_->Reset();
}
TEST_F(SchedulerTest, SynchronousCompositorResourcelessOnDrawWhenInvisible) {
scheduler_settings_.using_synchronous_renderer_compositor = true;
SetUpScheduler(EXTERNAL_BFS);
scheduler_->SetVisible(false);
scheduler_->SetNeedsRedraw();
bool resourceless_software_draw = true;
bool skip_draw = false;
scheduler_->OnDrawForLayerTreeFrameSink(resourceless_software_draw,
skip_draw);
// SynchronousCompositor has to draw regardless of visibility.
EXPECT_ACTIONS("ScheduledActionDrawIfPossible");
EXPECT_FALSE(client_->IsInsideBeginImplFrame());
client_->Reset();
}
TEST_F(SchedulerTest, AuthoritativeVSyncInterval) {
SetUpScheduler(THROTTLED_BFS);
base::TimeDelta initial_interval = scheduler_->BeginImplFrameInterval();
base::TimeDelta authoritative_interval = base::Milliseconds(33);
scheduler_->SetNeedsBeginMainFrame();
EXPECT_SCOPED(AdvanceFrame());
EXPECT_EQ(initial_interval, scheduler_->BeginImplFrameInterval());
scheduler_->NotifyBeginMainFrameStarted(task_runner_->NowTicks());
scheduler_->NotifyReadyToCommit(nullptr);
scheduler_->NotifyReadyToActivate();
task_runner_->RunTasksWhile(client_->InsideBeginImplFrame(true));
// Test changing the interval on the frame source external to the scheduler.
synthetic_frame_source_->OnUpdateVSyncParameters(task_runner_->NowTicks(),
authoritative_interval);
EXPECT_SCOPED(AdvanceFrame());
// At the next BeginFrame, authoritative interval is used instead of previous
// interval.
EXPECT_NE(initial_interval, scheduler_->BeginImplFrameInterval());
EXPECT_EQ(authoritative_interval, scheduler_->BeginImplFrameInterval());
}
TEST_F(SchedulerTest, ImplLatencyTakesPriority) {
SetUpScheduler(THROTTLED_BFS);
scheduler_->SetTreePrioritiesAndScrollState(
SMOOTHNESS_TAKES_PRIORITY,
ScrollHandlerState::SCROLL_DOES_NOT_AFFECT_SCROLL_HANDLER);
scheduler_->SetCriticalBeginMainFrameToActivateIsFast(true);
EXPECT_TRUE(scheduler_->ImplLatencyTakesPriority());
scheduler_->SetCriticalBeginMainFrameToActivateIsFast(false);
EXPECT_TRUE(scheduler_->ImplLatencyTakesPriority());
scheduler_->SetTreePrioritiesAndScrollState(
SMOOTHNESS_TAKES_PRIORITY,
ScrollHandlerState::SCROLL_AFFECTS_SCROLL_HANDLER);
scheduler_->SetCriticalBeginMainFrameToActivateIsFast(true);
EXPECT_FALSE(scheduler_->ImplLatencyTakesPriority());
scheduler_->SetCriticalBeginMainFrameToActivateIsFast(false);
EXPECT_TRUE(scheduler_->ImplLatencyTakesPriority());
scheduler_->SetTreePrioritiesAndScrollState(
SAME_PRIORITY_FOR_BOTH_TREES,
ScrollHandlerState::SCROLL_DOES_NOT_AFFECT_SCROLL_HANDLER);
scheduler_->SetCriticalBeginMainFrameToActivateIsFast(true);
EXPECT_FALSE(scheduler_->ImplLatencyTakesPriority());
scheduler_->SetCriticalBeginMainFrameToActivateIsFast(false);
EXPECT_FALSE(scheduler_->ImplLatencyTakesPriority());
scheduler_->SetTreePrioritiesAndScrollState(
SAME_PRIORITY_FOR_BOTH_TREES,
ScrollHandlerState::SCROLL_AFFECTS_SCROLL_HANDLER);
scheduler_->SetCriticalBeginMainFrameToActivateIsFast(true);
EXPECT_FALSE(scheduler_->ImplLatencyTakesPriority());
scheduler_->SetCriticalBeginMainFrameToActivateIsFast(false);
EXPECT_FALSE(scheduler_->ImplLatencyTakesPriority());
}
TEST_F(SchedulerTest, NoLayerTreeFrameSinkCreationWhileCommitPending) {
SetUpScheduler(THROTTLED_BFS);
// SetNeedsBeginMainFrame should begin the frame.
scheduler_->SetNeedsBeginMainFrame();
client_->Reset();
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame", "ScheduledActionSendBeginMainFrame");
// Lose the LayerTreeFrameSink and trigger the deadline.
client_->Reset();
scheduler_->DidLoseLayerTreeFrameSink();
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
EXPECT_NO_ACTION();
// The scheduler should not trigger the LayerTreeFrameSink creation till the
// commit is aborted.
task_runner_->RunTasksWhile(client_->InsideBeginImplFrame(true));
EXPECT_FALSE(client_->IsInsideBeginImplFrame());
EXPECT_NO_ACTION();
// Abort the commit.
client_->Reset();
scheduler_->NotifyBeginMainFrameStarted(task_runner_->NowTicks());
scheduler_->BeginMainFrameAborted(
CommitEarlyOutReason::kAbortedDeferredCommit);
EXPECT_ACTIONS("ScheduledActionBeginLayerTreeFrameSinkCreation");
}
TEST_F(SchedulerTest, ImplSideInvalidationInsideImplFrame) {
SetUpScheduler(EXTERNAL_BFS);
// Request an impl-side invalidation. Ensure that it runs before the deadline.
bool needs_first_draw_on_activation = true;
scheduler_->SetNeedsImplSideInvalidation(needs_first_draw_on_activation);
client_->Reset();
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame",
"ScheduledActionPerformImplSideInvalidation");
}
TEST_F(SchedulerTest, ImplSideInvalidationsMergedWithCommit) {
SetUpScheduler(EXTERNAL_BFS);
// Request a main frame and invalidation, the only action run should be
// sending the main frame.
SetShouldDeferInvalidationForMainFrame(true);
scheduler_->SetNeedsBeginMainFrame();
bool needs_first_draw_on_activation = true;
scheduler_->SetNeedsImplSideInvalidation(needs_first_draw_on_activation);
client_->Reset();
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame", "ScheduledActionSendBeginMainFrame");
// Respond with a commit. The scheduler should only perform the commit
// actions since the impl-side invalidation request will be merged with the
// commit.
client_->Reset();
scheduler_->NotifyBeginMainFrameStarted(task_runner_->NowTicks());
scheduler_->NotifyReadyToCommit(nullptr);
EXPECT_ACTIONS("ScheduledActionCommit", "ScheduledActionPostCommit");
EXPECT_FALSE(scheduler_->needs_impl_side_invalidation());
}
TEST_F(SchedulerTest, AbortedCommitsTriggerImplSideInvalidations) {
SetUpScheduler(EXTERNAL_BFS);
// Request a main frame and invalidation, with a fast main thread so we wait
// for it to respond.
SetShouldDeferInvalidationForMainFrame(true);
scheduler_->SetNeedsBeginMainFrame();
bool needs_first_draw_on_activation = true;
scheduler_->SetNeedsImplSideInvalidation(needs_first_draw_on_activation);
client_->Reset();
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame", "ScheduledActionSendBeginMainFrame");
// Abort the main frame and request another one, the impl-side invalidations
// should not be blocked on the main frame.
client_->Reset();
scheduler_->SetNeedsBeginMainFrame();
scheduler_->NotifyBeginMainFrameStarted(task_runner_->NowTicks());
scheduler_->BeginMainFrameAborted(CommitEarlyOutReason::kFinishedNoUpdates);
EXPECT_ACTIONS("ScheduledActionPerformImplSideInvalidation");
}
TEST_F(SchedulerTest, InvalidationNotBlockedOnMainFrame) {
SetUpScheduler(EXTERNAL_BFS);
// Request a main frame and invalidation, with a slow main thread so the
// invalidation is not blocked on a commit.
SetShouldDeferInvalidationForMainFrame(false);
scheduler_->SetNeedsBeginMainFrame();
bool needs_first_draw_on_activation = true;
scheduler_->SetNeedsImplSideInvalidation(needs_first_draw_on_activation);
client_->Reset();
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame", "ScheduledActionSendBeginMainFrame",
"ScheduledActionPerformImplSideInvalidation");
}
// The three letters appeneded to each version of this test mean the following:s
// tree_priority: B = both trees same priority; A = active tree priority;
// scroll_handler_state: H = affects scroll handler; N = does not affect scroll
// handler;
// durations: F = fast durations; S = slow durations
bool SchedulerTest::BeginMainFrameOnCriticalPath(
TreePriority tree_priority,
ScrollHandlerState scroll_handler_state,
base::TimeDelta durations) {
SetUpScheduler(EXTERNAL_BFS);
fake_compositor_timing_history_->SetAllEstimatesTo(durations);
client_->Reset();
scheduler_->SetTreePrioritiesAndScrollState(tree_priority,
scroll_handler_state);
scheduler_->SetNeedsBeginMainFrame();
EXPECT_FALSE(client_->last_begin_main_frame_args().IsValid());
EXPECT_SCOPED(AdvanceFrame());
EXPECT_TRUE(client_->last_begin_main_frame_args().IsValid());
return client_->last_begin_main_frame_args().on_critical_path;
}
TEST_F(SchedulerTest, BeginMainFrameOnCriticalPath_BNF) {
EXPECT_TRUE(BeginMainFrameOnCriticalPath(
SAME_PRIORITY_FOR_BOTH_TREES,
ScrollHandlerState::SCROLL_DOES_NOT_AFFECT_SCROLL_HANDLER,
kFastDuration));
}
TEST_F(SchedulerTest, BeginMainFrameOnCriticalPath_BNS) {
EXPECT_TRUE(BeginMainFrameOnCriticalPath(
SAME_PRIORITY_FOR_BOTH_TREES,
ScrollHandlerState::SCROLL_DOES_NOT_AFFECT_SCROLL_HANDLER,
kSlowDuration));
}
TEST_F(SchedulerTest, BeginMainFrameOnCriticalPath_BHF) {
EXPECT_TRUE(BeginMainFrameOnCriticalPath(
SAME_PRIORITY_FOR_BOTH_TREES,
ScrollHandlerState::SCROLL_AFFECTS_SCROLL_HANDLER, kFastDuration));
}
TEST_F(SchedulerTest, BeginMainFrameOnCriticalPath_BHS) {
EXPECT_TRUE(BeginMainFrameOnCriticalPath(
SAME_PRIORITY_FOR_BOTH_TREES,
ScrollHandlerState::SCROLL_AFFECTS_SCROLL_HANDLER, kSlowDuration));
}
TEST_F(SchedulerTest, BeginMainFrameOnCriticalPath_ANF) {
EXPECT_FALSE(BeginMainFrameOnCriticalPath(
SMOOTHNESS_TAKES_PRIORITY,
ScrollHandlerState::SCROLL_DOES_NOT_AFFECT_SCROLL_HANDLER,
kFastDuration));
}
TEST_F(SchedulerTest, BeginMainFrameOnCriticalPath_ANS) {
EXPECT_FALSE(BeginMainFrameOnCriticalPath(
SMOOTHNESS_TAKES_PRIORITY,
ScrollHandlerState::SCROLL_DOES_NOT_AFFECT_SCROLL_HANDLER,
kSlowDuration));
}
TEST_F(SchedulerTest, BeginMainFrameOnCriticalPath_AHF) {
EXPECT_TRUE(BeginMainFrameOnCriticalPath(
SMOOTHNESS_TAKES_PRIORITY,
ScrollHandlerState::SCROLL_AFFECTS_SCROLL_HANDLER, kFastDuration));
}
TEST_F(SchedulerTest, BeginMainFrameOnCriticalPath_AHS) {
EXPECT_FALSE(BeginMainFrameOnCriticalPath(
SMOOTHNESS_TAKES_PRIORITY,
ScrollHandlerState::SCROLL_AFFECTS_SCROLL_HANDLER, kSlowDuration));
}
TEST_F(SchedulerTest, BeginFrameAckForFinishedImplFrame) {
// Sets up scheduler and sends two BeginFrames, both finished.
SetUpScheduler(EXTERNAL_BFS);
// Run a successful redraw and verify that a new ack is sent.
scheduler_->SetNeedsRedraw();
client_->Reset();
viz::BeginFrameArgs args = SendNextBeginFrame();
EXPECT_ACTIONS("WillBeginImplFrame");
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
EXPECT_TRUE(scheduler_->begin_frames_expected());
client_->Reset();
task_runner_->RunPendingTasks(); // Run posted deadline.
EXPECT_ACTIONS("ScheduledActionDrawIfPossible");
EXPECT_FALSE(client_->IsInsideBeginImplFrame());
EXPECT_TRUE(scheduler_->begin_frames_expected());
// Successful draw caused damage.
bool has_damage = true;
EXPECT_EQ(viz::BeginFrameAck(args, has_damage),
client_->last_begin_frame_ack());
client_->Reset();
// Request another redraw, but fail it. Verify that a new ack is sent.
scheduler_->SetNeedsRedraw();
client_->Reset();
args = SendNextBeginFrame();
EXPECT_ACTIONS("WillBeginImplFrame");
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
EXPECT_TRUE(scheduler_->begin_frames_expected());
client_->Reset();
client_->SetDrawWillHappen(false);
task_runner_->RunPendingTasks(); // Run posted deadline.
EXPECT_ACTIONS("ScheduledActionDrawIfPossible",
// Failed draw triggers SendBeginMainFrame.
"ScheduledActionSendBeginMainFrame");
EXPECT_FALSE(client_->IsInsideBeginImplFrame());
EXPECT_TRUE(scheduler_->begin_frames_expected());
// Failed draw: no damage.
has_damage = false;
EXPECT_EQ(viz::BeginFrameAck(args, has_damage),
client_->last_begin_frame_ack());
// The begin-main-frame sent has not been acknowledged yet (either by
// doing a commit, or aborting the draw from the main-thread).
EXPECT_EQ(FrameSkippedReason::kWaitingOnMain,
client_->last_frame_skipped_reason());
client_->Reset();
// Start another frame, and end the frame without drawing.
args = SendNextBeginFrame();
EXPECT_ACTIONS("WillBeginImplFrame");
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
EXPECT_TRUE(scheduler_->begin_frames_expected());
client_->Reset();
client_->SetDrawWillHappen(false);
client_->SetSwapWillHappenIfDrawHappens(false);
task_runner_->RunPendingTasks(); // Run posted deadline.
EXPECT_ACTIONS("ScheduledActionDrawIfPossible");
// Draw with no damage.
has_damage = false;
EXPECT_EQ(viz::BeginFrameAck(args, has_damage),
client_->last_begin_frame_ack());
// The main thread still has not responded to the begin-main-frame.
EXPECT_EQ(FrameSkippedReason::kWaitingOnMain,
client_->last_frame_skipped_reason());
client_->Reset();
// Allow the commit now. NotifyReadyToCommit should trigger the commit.
scheduler_->NotifyBeginMainFrameStarted(task_runner_->NowTicks());
scheduler_->NotifyReadyToCommit(nullptr);
EXPECT_ACTIONS("ScheduledActionCommit", "ScheduledActionPostCommit");
EXPECT_TRUE(scheduler_->begin_frames_expected());
client_->Reset();
// Start another frame. For this frame, draw succeeds, but 'swap' does not
// happen (i.e. no frame is submitted).
args = SendNextBeginFrame();
EXPECT_ACTIONS("WillBeginImplFrame");
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
EXPECT_TRUE(scheduler_->begin_frames_expected());
client_->Reset();
client_->SetDrawWillHappen(true);
client_->SetSwapWillHappenIfDrawHappens(false);
task_runner_->RunPendingTasks(); // Run posted deadline.
// Draw with no damage.
has_damage = false;
EXPECT_EQ(viz::BeginFrameAck(args, has_damage),
client_->last_begin_frame_ack());
// The pending tree is not activated yet so the frame is still waiting on
// Main thread update
EXPECT_EQ(FrameSkippedReason::kWaitingOnMain,
client_->last_frame_skipped_reason());
}
TEST_F(SchedulerTest, BeginFrameAckForBeginFrameBeforeLastDeadline) {
SetUpScheduler(EXTERNAL_BFS);
// Request tile preparation to schedule a proactive BeginFrame.
scheduler_->SetNeedsPrepareTiles();
client_->Reset();
SendNextBeginFrame();
EXPECT_ACTIONS("WillBeginImplFrame");
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
// Until tiles were prepared, further proactive BeginFrames are expected.
EXPECT_TRUE(scheduler_->begin_frames_expected());
client_->Reset();
// Send the next BeginFrame before the previous one's deadline was executed.
// This should post the previous BeginFrame's deadline, during which tiles
// will be prepared. As a result of that, no further BeginFrames will be
// needed, and the new BeginFrame should be dropped.
viz::BeginFrameArgs args = SendNextBeginFrame();
task_runner_->RunPendingTasks(); // Run posted deadline.
EXPECT_ACTIONS("ScheduledActionPrepareTiles", "RemoveObserver(this)");
EXPECT_FALSE(client_->IsInsideBeginImplFrame());
EXPECT_FALSE(scheduler_->begin_frames_expected());
// Latest ack should be for the dropped BeginFrame.
bool has_damage = false;
EXPECT_EQ(viz::BeginFrameAck(args, has_damage),
client_->last_begin_frame_ack());
client_->Reset();
}
TEST_F(SchedulerTest, BeginFrameAckForDroppedBeginFrame) {
SetUpScheduler(EXTERNAL_BFS);
// Request a single BeginFrame.
scheduler_->SetNeedsOneBeginImplFrame();
EXPECT_TRUE(scheduler_->begin_frames_expected());
client_->Reset();
// First BeginFrame is handled by StateMachine.
viz::BeginFrameArgs first_args = SendNextBeginFrame();
EXPECT_ACTIONS("WillBeginImplFrame");
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
// State machine is no longer interested in BeginFrames, but scheduler is
// still observing the source.
EXPECT_TRUE(scheduler_->begin_frames_expected());
EXPECT_FALSE(scheduler_->BeginFrameNeeded());
client_->Reset();
// Send the next BeginFrame before the previous one's deadline was executed.
// The BeginFrame should be dropped immediately, since the state machine is
// not expecting any BeginFrames.
viz::BeginFrameArgs second_args = SendNextBeginFrame();
EXPECT_NO_ACTION();
// Latest ack should be for the dropped BeginFrame.
bool has_damage = false;
EXPECT_EQ(viz::BeginFrameAck(second_args, has_damage),
client_->last_begin_frame_ack());
client_->Reset();
task_runner_->RunPendingTasks(); // Run deadline of prior BeginFrame.
EXPECT_ACTIONS("RemoveObserver(this)");
// We'd expect an out-of-order ack for the prior BeginFrame.
has_damage = false;
EXPECT_EQ(viz::BeginFrameAck(first_args, has_damage),
client_->last_begin_frame_ack());
client_->Reset();
}
TEST_F(SchedulerTest, BeginFrameAckForLateMissedBeginFrame) {
SetUpScheduler(EXTERNAL_BFS);
scheduler_->SetNeedsRedraw();
client_->Reset();
// Send a missed BeginFrame with a passed deadline.
task_runner_->AdvanceMockTickClock(viz::BeginFrameArgs::DefaultInterval());
viz::BeginFrameArgs args =
fake_external_begin_frame_source_->CreateBeginFrameArgs(
BEGINFRAME_FROM_HERE, task_runner_->GetMockTickClock());
args.type = viz::BeginFrameArgs::MISSED;
task_runner_->AdvanceMockTickClock(viz::BeginFrameArgs::DefaultInterval());
EXPECT_GT(task_runner_->NowTicks(), args.deadline);
fake_external_begin_frame_source_->TestOnBeginFrame(args);
task_runner_->RunPendingTasks();
EXPECT_NO_ACTION();
EXPECT_FALSE(client_->IsInsideBeginImplFrame());
// Latest ack should be for the missed BeginFrame that was too late: no
// damage.
bool has_damage = false;
EXPECT_EQ(viz::BeginFrameAck(args, has_damage),
client_->last_begin_frame_ack());
client_->Reset();
}
TEST_F(SchedulerTest, CriticalBeginMainFrameToActivateIsFast) {
SetUpScheduler(EXTERNAL_BFS);
scheduler_->SetNeedsRedraw();
base::TimeDelta estimate_duration = base::Milliseconds(1);
fake_compositor_timing_history_->SetAllEstimatesTo(estimate_duration);
// If we have a scroll handler but the critical main frame is slow, we should
// still prioritize impl thread latency.
scheduler_->SetTreePrioritiesAndScrollState(
SMOOTHNESS_TAKES_PRIORITY,
ScrollHandlerState::SCROLL_AFFECTS_SCROLL_HANDLER);
scheduler_->SetNeedsRedraw();
// An interval of 2ms makes sure that the main frame is considered slow.
base::TimeDelta interval = base::Milliseconds(2);
task_runner_->AdvanceMockTickClock(interval);
viz::BeginFrameArgs args = viz::BeginFrameArgs::Create(
BEGINFRAME_FROM_HERE, 0u, 1u, task_runner_->NowTicks(),
task_runner_->NowTicks() + interval, interval,
viz::BeginFrameArgs::NORMAL);
fake_external_begin_frame_source_->TestOnBeginFrame(args);
EXPECT_TRUE(scheduler_->ImplLatencyTakesPriority());
task_runner_->RunPendingTasks(); // Run posted deadline to finish the frame.
ASSERT_FALSE(client_->IsInsideBeginImplFrame());
// Set an interval of 10ms. The bmf_to_activate_interval should be 1*4 = 4ms,
// to account for queue + main_frame + pending_tree + activation durations.
// With a draw time of 1ms and fudge factor of 1ms, the interval available for
// the main frame to be activated is 8ms, so it should be considered fast.
scheduler_->SetNeedsRedraw();
interval = base::Milliseconds(10);
task_runner_->AdvanceMockTickClock(interval);
args = viz::BeginFrameArgs::Create(BEGINFRAME_FROM_HERE, 0u, 2u,
task_runner_->NowTicks(),
task_runner_->NowTicks() + interval,
interval, viz::BeginFrameArgs::NORMAL);
fake_external_begin_frame_source_->TestOnBeginFrame(args);
EXPECT_FALSE(scheduler_->ImplLatencyTakesPriority());
task_runner_->RunPendingTasks(); // Run posted deadline to finish the frame.
ASSERT_FALSE(client_->IsInsideBeginImplFrame());
// Increase the draw duration to decrease the time available for the main
// frame. This should prioritize the impl thread.
scheduler_->SetNeedsRedraw();
fake_compositor_timing_history_->SetDrawDurationEstimate(
base::Milliseconds(7));
task_runner_->AdvanceMockTickClock(interval);
args = viz::BeginFrameArgs::Create(BEGINFRAME_FROM_HERE, 0u, 3u,
task_runner_->NowTicks(),
task_runner_->NowTicks() + interval,
interval, viz::BeginFrameArgs::NORMAL);
fake_external_begin_frame_source_->TestOnBeginFrame(args);
EXPECT_TRUE(scheduler_->ImplLatencyTakesPriority());
}
TEST_F(SchedulerTest, WaitForAllPipelineStagesUsesMissedBeginFrames) {
scheduler_settings_.wait_for_all_pipeline_stages_before_draw = true;
client_ = std::make_unique<FakeSchedulerClient>();
CreateScheduler(EXTERNAL_BFS);
// Initialize frame sink so that Scheduler and state machine need BeginFrames.
scheduler_->SetVisible(true);
scheduler_->SetCanDraw(true);
EXPECT_ACTIONS("ScheduledActionBeginLayerTreeFrameSinkCreation");
client_->Reset();
scheduler_->DidCreateAndInitializeLayerTreeFrameSink();
scheduler_->SetNeedsBeginMainFrame();
EXPECT_TRUE(scheduler_->begin_frames_expected());
EXPECT_FALSE(client_->IsInsideBeginImplFrame());
client_->Reset();
// Uses MISSED BeginFrames even after the deadline has passed.
base::TimeDelta interval = base::Milliseconds(16);
task_runner_->AdvanceMockTickClock(interval);
base::TimeTicks timestamp = task_runner_->NowTicks();
// Deadline should have passed after this.
task_runner_->AdvanceMockTickClock(interval * 2);
viz::BeginFrameArgs args = viz::BeginFrameArgs::Create(
BEGINFRAME_FROM_HERE, 0u, 1u, timestamp, timestamp + interval, interval,
viz::BeginFrameArgs::MISSED);
fake_external_begin_frame_source_->TestOnBeginFrame(args);
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
}
TEST_F(SchedulerTest, WaitForAllPipelineStagesAlwaysObservesBeginFrames) {
scheduler_settings_.wait_for_all_pipeline_stages_before_draw = true;
client_ = std::make_unique<FakeSchedulerClient>();
CreateScheduler(EXTERNAL_BFS);
// Initialize frame sink, request a main frame but defer commits, so that
// state machine is idle.
scheduler_->SetVisible(true);
scheduler_->SetCanDraw(true);
EXPECT_ACTIONS("ScheduledActionBeginLayerTreeFrameSinkCreation");
client_->Reset();
scheduler_->DidCreateAndInitializeLayerTreeFrameSink();
scheduler_->SetDeferBeginMainFrame(true);
scheduler_->SetNeedsBeginMainFrame();
EXPECT_TRUE(scheduler_->begin_frames_expected());
EXPECT_FALSE(client_->IsInsideBeginImplFrame());
client_->Reset();
// In full-pipe mode, the SchedulerStateMachine always wants BeginFrames, even
// if it is otherwise idle.
EXPECT_TRUE(scheduler_->begin_frames_expected());
EXPECT_TRUE(scheduler_->BeginFrameNeeded());
// Scheduler begins a frame even if otherwise idle.
base::TimeDelta interval = base::Milliseconds(16);
task_runner_->AdvanceMockTickClock(interval);
base::TimeTicks timestamp = task_runner_->NowTicks();
viz::BeginFrameArgs args = viz::BeginFrameArgs::Create(
BEGINFRAME_FROM_HERE, 0u, 1u, timestamp, timestamp + interval, interval,
viz::BeginFrameArgs::NORMAL);
fake_external_begin_frame_source_->TestOnBeginFrame(args);
EXPECT_ACTIONS("WillBeginImplFrame");
EXPECT_TRUE(client_->IsInsideBeginImplFrame());
client_->Reset();
// BeginFrame deadline is not blocked because commits are deferred.
task_runner_->RunPendingTasks();
EXPECT_ACTIONS();
EXPECT_TRUE(!client_->IsInsideBeginImplFrame());
client_->Reset();
}
TEST_F(SchedulerTest, CriticalBeginMainFrameIsFast_CommitEstimateSlow) {
SetUpScheduler(EXTERNAL_BFS);
scheduler_->SetNeedsBeginMainFrame();
fake_compositor_timing_history_->SetAllEstimatesTo(kFastDuration);
fake_compositor_timing_history_->SetCommitDurationEstimate(kSlowDuration);
EXPECT_SCOPED(AdvanceFrame());
EXPECT_FALSE(scheduler_->state_machine()
.critical_begin_main_frame_to_activate_is_fast());
}
TEST_F(SchedulerTest, CriticalBeginMainFrameIsFast_CommitEstimateFast) {
SetUpScheduler(EXTERNAL_BFS);
scheduler_->SetNeedsBeginMainFrame();
fake_compositor_timing_history_->SetAllEstimatesTo(kFastDuration);
EXPECT_SCOPED(AdvanceFrame());
EXPECT_TRUE(scheduler_->state_machine()
.critical_begin_main_frame_to_activate_is_fast());
}
TEST_F(SchedulerTest, ShouldDeferInvalidation_AllEstimatesFast) {
SetUpScheduler(EXTERNAL_BFS);
scheduler_->SetNeedsBeginMainFrame();
fake_compositor_timing_history_->SetAllEstimatesTo(kFastDuration);
EXPECT_SCOPED(AdvanceFrame());
EXPECT_TRUE(scheduler_->state_machine()
.should_defer_invalidation_for_fast_main_frame());
}
TEST_F(SchedulerTest, ShouldDeferInvalidation_BMFStartToReadyToCommitSlow) {
SetUpScheduler(EXTERNAL_BFS);
scheduler_->SetNeedsBeginMainFrame();
fake_compositor_timing_history_->SetAllEstimatesTo(kFastDuration);
fake_compositor_timing_history_
->SetBeginMainFrameStartToReadyToCommitDurationEstimate(kSlowDuration);
EXPECT_SCOPED(AdvanceFrame());
EXPECT_FALSE(scheduler_->state_machine()
.should_defer_invalidation_for_fast_main_frame());
}
TEST_F(SchedulerTest, ShouldDeferInvalidation_BMFQueueDurationCriticalSlow) {
SetUpScheduler(EXTERNAL_BFS);
scheduler_->SetNeedsBeginMainFrame();
fake_compositor_timing_history_->SetAllEstimatesTo(kFastDuration);
fake_compositor_timing_history_
->SetBeginMainFrameQueueDurationCriticalEstimate(kSlowDuration);
EXPECT_SCOPED(AdvanceFrame());
EXPECT_FALSE(scheduler_->state_machine()
.should_defer_invalidation_for_fast_main_frame());
}
TEST_F(SchedulerTest, ShouldDeferInvalidation_BMFQueueDurationNotCriticalSlow) {
SetUpScheduler(EXTERNAL_BFS);
scheduler_->SetNeedsBeginMainFrame();
scheduler_->SetTreePrioritiesAndScrollState(
TreePriority::SMOOTHNESS_TAKES_PRIORITY,
ScrollHandlerState::SCROLL_DOES_NOT_AFFECT_SCROLL_HANDLER);
fake_compositor_timing_history_->SetAllEstimatesTo(kFastDuration);
fake_compositor_timing_history_
->SetBeginMainFrameQueueDurationNotCriticalEstimate(kSlowDuration);
EXPECT_SCOPED(AdvanceFrame());
EXPECT_FALSE(scheduler_->state_machine()
.should_defer_invalidation_for_fast_main_frame());
}
TEST_F(SchedulerTest, SlowMainThreadButEstimatedFastTriggersInvalidations) {
SetUpScheduler(EXTERNAL_BFS);
scheduler_->SetNeedsBeginMainFrame();
scheduler_->SetNeedsImplSideInvalidation(true);
fake_compositor_timing_history_->SetAllEstimatesTo(kFastDuration);
// Main thread is estimated fast, invalidation will be deferred.
client_->Reset();
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame", "ScheduledActionSendBeginMainFrame");
// Draw deadline.
client_->Reset();
task_runner_->RunPendingTasks();
EXPECT_ACTIONS();
// Next frame. The invalidation should not be throttled.
client_->Reset();
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame",
"ScheduledActionPerformImplSideInvalidation");
}
TEST_F(SchedulerTest,
SlowMainThreadRasterButEstimatedFastDoesNotTriggersInvalidations) {
SetUpScheduler(EXTERNAL_BFS);
scheduler_->SetNeedsBeginMainFrame();
scheduler_->SetNeedsImplSideInvalidation(true);
fake_compositor_timing_history_->SetAllEstimatesTo(kFastDuration);
// Main thread is estimated fast, invalidation will be deferred.
client_->Reset();
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame", "ScheduledActionSendBeginMainFrame");
// Commit before deadline but not ready to activate.
client_->Reset();
scheduler_->NotifyBeginMainFrameStarted(task_runner_->NowTicks());
scheduler_->NotifyReadyToCommit(nullptr);
EXPECT_ACTIONS("ScheduledActionCommit", "ScheduledActionPostCommit");
// Draw deadline.
client_->Reset();
task_runner_->RunPendingTasks();
EXPECT_ACTIONS();
// Next frame. The invalidation should still be throttled.
client_->Reset();
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame");
}
TEST_F(SchedulerTest, SynchronousCompositorImplSideInvalidation) {
// Synchronous compositor doesn't have a deadline and our heuristics can't
// work. We should never be prioritizing impl-side invalidations over main
// frames.
scheduler_settings_.using_synchronous_renderer_compositor = true;
SetUpScheduler(EXTERNAL_BFS);
fake_compositor_timing_history_->SetAllEstimatesTo(kSlowDuration);
scheduler_->SetNeedsBeginMainFrame();
const bool needs_first_draw_on_activation = true;
scheduler_->SetNeedsImplSideInvalidation(needs_first_draw_on_activation);
client_->Reset();
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame", "ScheduledActionSendBeginMainFrame");
}
TEST_F(SchedulerTest, NoInvalidationForAnimateOnlyFrames) {
SetUpScheduler(EXTERNAL_BFS);
fake_compositor_timing_history_->SetAllEstimatesTo(kFastDuration);
client_->Reset();
scheduler_->SetNeedsImplSideInvalidation(true);
bool animate_only = true;
EXPECT_SCOPED(AdvanceFrame(animate_only));
EXPECT_ACTIONS("AddObserver(this)", "WillBeginImplFrame");
client_->Reset();
task_runner_->RunTasksWhile(client_->InsideBeginImplFrame(true));
EXPECT_ACTIONS();
// Now send a frame which requires full updates.
animate_only = false;
EXPECT_SCOPED(AdvanceFrame(animate_only));
EXPECT_ACTIONS("WillBeginImplFrame",
"ScheduledActionPerformImplSideInvalidation");
client_->Reset();
task_runner_->RunTasksWhile(client_->InsideBeginImplFrame(true));
EXPECT_ACTIONS();
}
TEST_F(SchedulerTest, SendEarlyDidNotProduceFrameIfIdle) {
SetUpScheduler(EXTERNAL_BFS);
scheduler_->SetNeedsBeginMainFrame();
client_->Reset();
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame", "ScheduledActionSendBeginMainFrame");
auto begin_main_frame_args = client_->last_begin_main_frame_args();
EXPECT_NE(client_->last_begin_frame_ack().frame_id.sequence_number,
begin_main_frame_args.frame_id.sequence_number);
client_->Reset();
scheduler_->NotifyBeginMainFrameStarted(task_runner_->NowTicks());
// Request a new commit before finishing the current one to simulate behavior
// seen in certain OOPIF renderers.
scheduler_->SetNeedsBeginMainFrame();
scheduler_->BeginMainFrameAborted(CommitEarlyOutReason::kFinishedNoUpdates);
EXPECT_EQ(client_->last_begin_frame_ack().frame_id.sequence_number,
begin_main_frame_args.frame_id.sequence_number);
}
TEST_F(SchedulerTest,
HighImplLatencyModePrioritizesMainFramesOverImplInvalidation) {
SetUpScheduler(EXTERNAL_BFS);
fake_compositor_timing_history_->SetAllEstimatesTo(kFastDuration);
// Place the impl thread in high latency mode.
scheduler_->SetNeedsImplSideInvalidation(true);
client_->Reset();
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame",
"ScheduledActionPerformImplSideInvalidation");
// Request a main frame and start the next impl frame. Since we have an impl
// side pending tree, we will activate and draw it. This finishes the impl
// frame before the main thread can respond causing the scheduler to
// incorrectly assume the main thread is slow.
client_->Reset();
EXPECT_SCOPED(AdvanceFrame());
EXPECT_ACTIONS("WillBeginImplFrame");
client_->Reset();
scheduler_->SetNeedsBeginMainFrame();
EXPECT_ACTIONS("ScheduledActionSendBeginMainFrame");
fake_compositor_timing_history_->SetBeginMainFrameSentTime(
task_runner_->NowTicks() + base::Milliseconds(8));
client_->Reset();
scheduler_->NotifyReadyToActivate();
task_runner_->RunTasksWhile(client_->InsideBeginImplFrame(true));
EXPECT_ACTIONS("ScheduledActionActivateSyncTree",
"ScheduledActionDrawIfPossible");
// Start a new frame. We should not assume the main thread is slow.
client_->Reset();
EXPECT_SCOPED(AdvanceFrame());
scheduler_->SetNeedsImplSideInvalidation(true);
// No invalidation should be performed since we are waiting for the main
// thread to respond and merge with the commit.
EXPECT_ACTIONS("WillBeginImplFrame");
}
TEST_F(SchedulerTest, ProactiveThrottling) {
// Verify that the SchedulerClient gets updates when the begin frame interval
// changes.
SetUpScheduler(EXTERNAL_BFS);
constexpr uint64_t kSourceId = viz::BeginFrameArgs::kStartingSourceId;
uint64_t sequence_number = viz::BeginFrameArgs::kStartingFrameNumber;
// Enable the proactive throttling feature.
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeatureWithParameters(
features::kRenderThrottleFrameRate,
{{"render-throttled-frame-interval-hz", "30"}});
base::TimeDelta throttled_interval =
base::Hertz(features::kRenderThrottledFrameIntervalHz.Get());
// No throttling by default.
base::TimeDelta interval = base::Hertz(120);
EXPECT_TRUE(
scheduler_->state_machine().MainFrameThrottledInterval().is_zero());
SendTestBeginFrameAfterInterval(interval, kSourceId, sequence_number++);
EXPECT_TRUE(
scheduler_->state_machine().MainFrameThrottledInterval().is_zero());
scheduler_->SetShouldThrottleFrameRate(true);
// Throttling at 60fps.
interval = base::Hertz(60);
SendTestBeginFrameAfterInterval(interval, kSourceId, sequence_number++);
EXPECT_EQ(scheduler_->state_machine().MainFrameThrottledInterval(),
throttled_interval);
// Not at 10fps.
interval = base::Hertz(10);
SendTestBeginFrameAfterInterval(interval, kSourceId, sequence_number++);
EXPECT_TRUE(
scheduler_->state_machine().MainFrameThrottledInterval().is_zero());
// Not throttling after stopping the throttle.
scheduler_->SetShouldThrottleFrameRate(false);
interval = base::Hertz(60);
SendTestBeginFrameAfterInterval(interval, kSourceId, sequence_number++);
EXPECT_TRUE(
scheduler_->state_machine().MainFrameThrottledInterval().is_zero());
}
// Tests that `SetShouldWarmUp()` will start initial `LayerTreeFrameSink`
// creation even if invisible.
TEST_F(SchedulerTest, SetShouldWarmUpWillStartLayerTreeFrameSinkCreation) {
SetUpSchedulerWithNoLayerTreeFrameSink(EXTERNAL_BFS);
scheduler_->SetVisible(false);
scheduler_->SetShouldWarmUp();
EXPECT_ACTIONS("ScheduledActionBeginLayerTreeFrameSinkCreation");
client_->Reset();
scheduler_->DidCreateAndInitializeLayerTreeFrameSink();
EXPECT_NO_ACTION();
}
} // namespace
} // namespace cc
|