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
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/renderer/render_widget.h"
#include <cmath>
#include <limits>
#include <utility>
#include "base/auto_reset.h"
#include "base/base_switches.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/command_line.h"
#include "base/feature_list.h"
#include "base/logging.h"
#include "base/macros.h"
#include "base/memory/ptr_util.h"
#include "base/memory/singleton.h"
#include "base/metrics/histogram_macros.h"
#include "base/optional.h"
#include "base/stl_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/system/sys_info.h"
#include "base/threading/thread_task_runner_handle.h"
#include "base/trace_event/trace_event.h"
#include "build/build_config.h"
#include "cc/animation/animation_host.h"
#include "cc/base/features.h"
#include "cc/base/switches.h"
#include "cc/input/touch_action.h"
#include "cc/paint/paint_worklet_layer_painter.h"
#include "cc/trees/layer_tree_frame_sink.h"
#include "cc/trees/layer_tree_host.h"
#include "cc/trees/ukm_manager.h"
#include "components/viz/common/display/de_jelly.h"
#include "components/viz/common/features.h"
#include "components/viz/common/frame_sinks/begin_frame_source.h"
#include "components/viz/common/frame_sinks/copy_output_request.h"
#include "components/viz/common/switches.h"
#include "content/common/content_switches_internal.h"
#include "content/common/drag_event_source_info.h"
#include "content/common/drag_messages.h"
#include "content/common/render_message_filter.mojom.h"
#include "content/common/widget_messages.h"
#include "content/public/common/content_client.h"
#include "content/public/common/content_features.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/drop_data.h"
#include "content/public/common/service_names.mojom.h"
#include "content/public/common/untrustworthy_context_menu_params.h"
#include "content/public/common/use_zoom_for_dsf_policy.h"
#include "content/public/renderer/content_renderer_client.h"
#include "content/public/renderer/render_thread.h"
#include "content/renderer/drop_data_builder.h"
#include "content/renderer/frame_swap_message_queue.h"
#include "content/renderer/ime_event_guard.h"
#include "content/renderer/input/main_thread_event_queue.h"
#include "content/renderer/input/widget_input_handler_manager.h"
#include "content/renderer/pepper/pepper_plugin_instance_impl.h"
#include "content/renderer/queue_message_swap_promise.h"
#include "content/renderer/render_frame_impl.h"
#include "content/renderer/render_frame_proxy.h"
#include "content/renderer/render_process.h"
#include "content/renderer/render_thread_impl.h"
#include "content/renderer/render_view_impl.h"
#include "content/renderer/render_widget_screen_metrics_emulator.h"
#include "content/renderer/renderer_blink_platform_impl.h"
#include "gpu/command_buffer/service/gpu_switches.h"
#include "gpu/config/gpu_finch_features.h"
#include "ipc/ipc_message_start.h"
#include "ipc/ipc_sync_message.h"
#include "ipc/ipc_sync_message_filter.h"
#include "media/base/media_switches.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "ppapi/buildflags/buildflags.h"
#include "skia/ext/platform_canvas.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/common/input/web_input_event_attribution.h"
#include "third_party/blink/public/common/input/web_mouse_event.h"
#include "third_party/blink/public/common/page/web_drag_operation.h"
#include "third_party/blink/public/common/switches.h"
#include "third_party/blink/public/platform/file_path_conversion.h"
#include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/public/platform/scheduler/web_render_widget_scheduling_state.h"
#include "third_party/blink/public/platform/scheduler/web_thread_scheduler.h"
#include "third_party/blink/public/platform/scheduler/web_widget_scheduler.h"
#include "third_party/blink/public/platform/web_drag_data.h"
#include "third_party/blink/public/platform/web_rect.h"
#include "third_party/blink/public/platform/web_runtime_features.h"
#include "third_party/blink/public/platform/web_size.h"
#include "third_party/blink/public/platform/web_string.h"
#include "third_party/blink/public/web/web_autofill_client.h"
#include "third_party/blink/public/web/web_device_emulation_params.h"
#include "third_party/blink/public/web/web_frame_widget.h"
#include "third_party/blink/public/web/web_input_method_controller.h"
#include "third_party/blink/public/web/web_local_frame.h"
#include "third_party/blink/public/web/web_node.h"
#include "third_party/blink/public/web/web_page_popup.h"
#include "third_party/blink/public/web/web_popup_menu_info.h"
#include "third_party/blink/public/web/web_range.h"
#include "third_party/blink/public/web/web_settings.h"
#include "third_party/blink/public/web/web_view.h"
#include "third_party/blink/public/web/web_widget.h"
#include "third_party/skia/include/core/SkShader.h"
#include "ui/base/clipboard/clipboard_constants.h"
#include "ui/base/ui_base_features.h"
#include "ui/base/ui_base_switches.h"
#include "ui/events/base_event_utils.h"
#include "ui/gfx/geometry/dip_util.h"
#include "ui/gfx/geometry/point_conversions.h"
#include "ui/gfx/geometry/rect_conversions.h"
#include "ui/gfx/geometry/size_conversions.h"
#include "ui/gfx/skia_util.h"
#include "ui/gfx/switches.h"
#include "ui/gl/gl_switches.h"
#include "ui/native_theme/native_theme_features.h"
#include "ui/native_theme/overlay_scrollbar_constants_aura.h"
#include "ui/surface/transport_dib.h"
#if defined(OS_ANDROID)
#include <android/keycodes.h>
#include "base/time/time.h"
#include "components/viz/common/viz_utils.h"
#endif
#if defined(OS_POSIX)
#include "third_party/skia/include/core/SkMallocPixelRef.h"
#include "third_party/skia/include/core/SkPixelRef.h"
#endif // defined(OS_POSIX)
using blink::WebDeviceEmulationParams;
using blink::WebDragOperation;
using blink::WebDragOperationsMask;
using blink::WebDragData;
using blink::WebFrameWidget;
using blink::WebGestureEvent;
using blink::WebInputEvent;
using blink::WebInputEventResult;
using blink::WebInputMethodController;
using blink::WebLocalFrame;
using blink::WebMouseEvent;
using blink::WebMouseWheelEvent;
using blink::WebNavigationPolicy;
using blink::WebNode;
using blink::WebPagePopup;
using blink::WebRange;
using blink::WebRect;
using blink::WebSize;
using blink::WebString;
using blink::WebTouchEvent;
using blink::WebTouchPoint;
using blink::WebVector;
using blink::WebWidget;
namespace content {
namespace {
RenderWidget::CreateRenderWidgetFunction g_create_render_widget_for_frame =
nullptr;
const base::Feature kUnpremultiplyAndDitherLowBitDepthTiles = {
"UnpremultiplyAndDitherLowBitDepthTiles", base::FEATURE_ENABLED_BY_DEFAULT};
typedef std::map<std::string, ui::TextInputMode> TextInputModeMap;
static const char* kOOPIF = "OOPIF";
static const char* kRenderer = "Renderer";
#if defined(OS_ANDROID)
// With 32 bit pixels, this would mean less than 400kb per buffer. Much less
// than required for, say, nHD.
static const int kSmallScreenPixelThreshold = 1e5;
bool IsSmallScreen(const gfx::Size& size) {
int area = 0;
if (!size.GetCheckedArea().AssignIfValid(&area))
return false;
return area < kSmallScreenPixelThreshold;
}
#endif
class WebWidgetLockTarget : public content::MouseLockDispatcher::LockTarget {
public:
explicit WebWidgetLockTarget(RenderWidget* render_widget)
: render_widget_(render_widget) {}
void OnLockMouseACK(bool succeeded) override {
if (succeeded)
render_widget_->GetWebWidget()->DidAcquirePointerLock();
else
render_widget_->GetWebWidget()->DidNotAcquirePointerLock();
}
void OnMouseLockLost() override {
render_widget_->GetWebWidget()->DidLosePointerLock();
}
bool HandleMouseLockedInputEvent(const blink::WebMouseEvent& event) override {
// The WebWidget handles mouse lock in Blink's handleInputEvent().
return false;
}
private:
// The RenderWidget owns this instance and is guaranteed to outlive it.
RenderWidget* render_widget_;
};
WebDragData DropMetaDataToWebDragData(
const std::vector<DropData::Metadata>& drop_meta_data) {
std::vector<WebDragData::Item> item_list;
for (const auto& meta_data_item : drop_meta_data) {
if (meta_data_item.kind == DropData::Kind::STRING) {
WebDragData::Item item;
item.storage_type = WebDragData::Item::kStorageTypeString;
item.string_type = WebString::FromUTF16(meta_data_item.mime_type);
// Have to pass a dummy URL here instead of an empty URL because the
// DropData received by browser_plugins goes through a round trip:
// DropData::MetaData --> WebDragData-->DropData. In the end, DropData
// will contain an empty URL (which means no URL is dragged) if the URL in
// WebDragData is empty.
if (base::EqualsASCII(meta_data_item.mime_type, ui::kMimeTypeURIList)) {
item.string_data = WebString::FromUTF8("about:dragdrop-placeholder");
}
item_list.push_back(item);
continue;
}
// TODO(hush): crbug.com/584789. Blink needs to support creating a file with
// just the mimetype. This is needed to drag files to WebView on Android
// platform.
if ((meta_data_item.kind == DropData::Kind::FILENAME) &&
!meta_data_item.filename.empty()) {
WebDragData::Item item;
item.storage_type = WebDragData::Item::kStorageTypeFilename;
item.filename_data = blink::FilePathToWebString(meta_data_item.filename);
item_list.push_back(item);
continue;
}
if (meta_data_item.kind == DropData::Kind::FILESYSTEMFILE) {
WebDragData::Item item;
item.storage_type = WebDragData::Item::kStorageTypeFileSystemFile;
item.file_system_url = meta_data_item.file_system_url;
item_list.push_back(item);
continue;
}
}
WebDragData result;
result.SetItems(item_list);
return result;
}
WebDragData DropDataToWebDragData(const DropData& drop_data) {
std::vector<WebDragData::Item> item_list;
// These fields are currently unused when dragging into WebKit.
DCHECK(drop_data.download_metadata.empty());
DCHECK(drop_data.file_contents.empty());
DCHECK(drop_data.file_contents_content_disposition.empty());
if (drop_data.text) {
WebDragData::Item item;
item.storage_type = WebDragData::Item::kStorageTypeString;
item.string_type = WebString::FromUTF8(ui::kMimeTypeText);
item.string_data = WebString::FromUTF16(*drop_data.text);
item_list.push_back(item);
}
if (!drop_data.url.is_empty()) {
WebDragData::Item item;
item.storage_type = WebDragData::Item::kStorageTypeString;
item.string_type = WebString::FromUTF8(ui::kMimeTypeURIList);
item.string_data = WebString::FromUTF8(drop_data.url.spec());
item.title = WebString::FromUTF16(drop_data.url_title);
item_list.push_back(item);
}
if (drop_data.html) {
WebDragData::Item item;
item.storage_type = WebDragData::Item::kStorageTypeString;
item.string_type = WebString::FromUTF8(ui::kMimeTypeHTML);
item.string_data = WebString::FromUTF16(*drop_data.html);
item.base_url = drop_data.html_base_url;
item_list.push_back(item);
}
for (const ui::FileInfo& filename : drop_data.filenames) {
WebDragData::Item item;
item.storage_type = WebDragData::Item::kStorageTypeFilename;
item.filename_data = blink::FilePathToWebString(filename.path);
item.display_name_data =
blink::FilePathToWebString(base::FilePath(filename.display_name));
item_list.push_back(item);
}
for (const DropData::FileSystemFileInfo& file : drop_data.file_system_files) {
WebDragData::Item item;
item.storage_type = WebDragData::Item::kStorageTypeFileSystemFile;
item.file_system_url = file.url;
item.file_system_file_size = file.size;
item.file_system_id = blink::WebString::FromASCII(file.filesystem_id);
item_list.push_back(item);
}
for (const auto& data : drop_data.custom_data) {
WebDragData::Item item;
item.storage_type = WebDragData::Item::kStorageTypeString;
item.string_type = WebString::FromUTF16(data.first);
item.string_data = WebString::FromUTF16(data.second);
item_list.push_back(item);
}
WebDragData result;
result.SetItems(item_list);
result.SetFilesystemId(WebString::FromUTF16(drop_data.filesystem_id));
return result;
}
#if BUILDFLAG(ENABLE_PLUGINS)
blink::WebTextInputType ConvertTextInputType(ui::TextInputType type) {
// Check the type is in the range representable by ui::TextInputType.
DCHECK_LE(type, static_cast<int>(ui::TEXT_INPUT_TYPE_MAX))
<< "blink::WebTextInputType and ui::TextInputType not synchronized";
return static_cast<blink::WebTextInputType>(type);
}
#endif
static bool ComputePreferCompositingToLCDText(
CompositorDependencies* compositor_deps,
float device_scale_factor) {
const base::CommandLine& command_line =
*base::CommandLine::ForCurrentProcess();
if (command_line.HasSwitch(switches::kDisablePreferCompositingToLCDText))
return false;
#if defined(OS_ANDROID) || defined(OS_CHROMEOS)
// On Android, we never have subpixel antialiasing. On Chrome OS we prefer to
// composite all scrollers for better scrolling performance.
return true;
#else
// Prefer compositing if the device scale is high enough that losing subpixel
// antialiasing won't have a noticeable effect on text quality.
// Note: We should keep kHighDPIDeviceScaleFactorThreshold in
// cc/metrics/lcd_text_metrics_reporter.cc the same as the value below.
if (device_scale_factor >= 1.5f)
return true;
if (command_line.HasSwitch(switches::kEnablePreferCompositingToLCDText))
return true;
if (!compositor_deps->IsLcdTextEnabled())
return true;
if (base::FeatureList::IsEnabled(features::kPreferCompositingToLCDText))
return true;
return false;
#endif
}
viz::FrameSinkId GetRemoteFrameSinkId(const blink::WebHitTestResult& result) {
const blink::WebNode& node = result.GetNode();
DCHECK(!node.IsNull());
blink::WebFrame* result_frame = blink::WebFrame::FromFrameOwnerElement(node);
if (!result_frame || !result_frame->IsWebRemoteFrame())
return viz::FrameSinkId();
blink::WebRemoteFrame* remote_frame = result_frame->ToWebRemoteFrame();
if (remote_frame->IsIgnoredForHitTest() || !result.ContentBoxContainsPoint())
return viz::FrameSinkId();
return RenderFrameProxy::FromWebFrame(remote_frame)->frame_sink_id();
}
blink::mojom::DidOverscrollParamsPtr ToOverscrollParams(
const blink::InputHandlerProxy::DidOverscrollParams* params) {
if (!params)
return nullptr;
return blink::mojom::DidOverscrollParams::New(
params->accumulated_overscroll, params->latest_overscroll_delta,
params->current_fling_velocity, params->causal_event_viewport_point,
params->overscroll_behavior);
}
} // namespace
// RenderWidget ---------------------------------------------------------------
// static
void RenderWidget::InstallCreateForFrameHook(
CreateRenderWidgetFunction create_widget) {
g_create_render_widget_for_frame = create_widget;
}
std::unique_ptr<RenderWidget> RenderWidget::CreateForFrame(
int32_t widget_routing_id,
CompositorDependencies* compositor_deps,
bool never_composited) {
if (g_create_render_widget_for_frame) {
return g_create_render_widget_for_frame(widget_routing_id, compositor_deps,
/*hidden=*/true, never_composited);
}
return std::make_unique<RenderWidget>(widget_routing_id, compositor_deps,
/*hidden=*/true, never_composited);
}
RenderWidget* RenderWidget::CreateForPopup(
int32_t widget_routing_id,
CompositorDependencies* compositor_deps,
bool hidden,
bool never_composited) {
return new RenderWidget(widget_routing_id, compositor_deps, hidden,
never_composited);
}
RenderWidget::RenderWidget(int32_t widget_routing_id,
CompositorDependencies* compositor_deps,
bool hidden,
bool never_composited)
: routing_id_(widget_routing_id),
compositor_deps_(compositor_deps),
is_hidden_(hidden),
never_composited_(never_composited),
frame_swap_message_queue_(new FrameSwapMessageQueue(routing_id_)) {
DCHECK_NE(routing_id_, MSG_ROUTING_NONE);
DCHECK(RenderThread::IsMainThread());
DCHECK(compositor_deps_);
}
RenderWidget::~RenderWidget() {
DCHECK(!webwidget_) << "Leaking our WebWidget!";
DCHECK(closing_)
<< " RenderWidget must be destroyed via RenderWidget::Close()";
}
void RenderWidget::InitForPopup(ShowCallback show_callback,
RenderWidget* opener_widget,
blink::WebPagePopup* web_page_popup,
const ScreenInfo& screen_info) {
popup_ = true;
Initialize(std::move(show_callback), web_page_popup, screen_info);
if (opener_widget->device_emulator_) {
opener_widget_screen_origin_ =
opener_widget->device_emulator_->ViewRectOrigin();
opener_original_widget_screen_origin_ =
opener_widget->device_emulator_->original_view_rect().origin();
opener_emulator_scale_ = opener_widget->GetEmulatorScale();
}
}
void RenderWidget::InitForPepperFullscreen(ShowCallback show_callback,
blink::WebWidget* web_widget,
const ScreenInfo& screen_info) {
pepper_fullscreen_ = true;
Initialize(std::move(show_callback), web_widget, screen_info);
}
void RenderWidget::InitForMainFrame(ShowCallback show_callback,
blink::WebFrameWidget* web_frame_widget,
const ScreenInfo& screen_info) {
Initialize(std::move(show_callback), web_frame_widget, screen_info);
}
void RenderWidget::InitForChildLocalRoot(
blink::WebFrameWidget* web_frame_widget,
const ScreenInfo& screen_info) {
for_child_local_root_frame_ = true;
Initialize(base::NullCallback(), web_frame_widget, screen_info);
}
void RenderWidget::CloseForFrame(std::unique_ptr<RenderWidget> widget) {
DCHECK(for_frame());
DCHECK_EQ(widget.get(), this); // This method takes ownership of |this|.
Close(std::move(widget));
}
void RenderWidget::Initialize(ShowCallback show_callback,
WebWidget* web_widget,
const ScreenInfo& screen_info) {
DCHECK_NE(routing_id_, MSG_ROUTING_NONE);
DCHECK(web_widget);
show_callback_ = std::move(show_callback);
#if defined(OS_MACOSX)
text_input_client_observer_ =
std::make_unique<TextInputClientObserver>(for_frame() ? this : nullptr);
#endif
webwidget_mouse_lock_target_.reset(new WebWidgetLockTarget(this));
mouse_lock_dispatcher_.reset(new RenderWidgetMouseLockDispatcher(this));
RenderThread::Get()->AddRoute(routing_id_, this);
webwidget_ = web_widget;
if (auto* scheduler_state = GetWebWidget()->RendererWidgetSchedulingState())
scheduler_state->SetHidden(is_hidden());
InitCompositing(screen_info);
// If the widget is hidden, delay starting the compositor until the user
// shows it. Otherwise start the compositor immediately. If the widget is
// for a provisional frame, this importantly starts the compositor before
// the frame is inserted into the frame tree, which impacts first paint
// metrics.
if (!is_hidden_ && !never_composited_)
web_widget->SetCompositorVisible(true);
// Note that this calls into the WebWidget.
UpdateSurfaceAndScreenInfo(local_surface_id_allocation_from_parent_,
CompositorViewportRect(), screen_info);
}
bool RenderWidget::OnMessageReceived(const IPC::Message& message) {
// The EnableDeviceEmulation message is sent to a provisional RenderWidget
// before the navigation completes. Some investigation into why is done in
// https://chromium-review.googlesource.com/c/chromium/src/+/1853675/5#message-e6edc3fd708d7d267ee981ffe43cae090b37a906
// but it's unclear what would need to be done to delay this until after
// navigation.
bool handled = false;
IPC_BEGIN_MESSAGE_MAP(RenderWidget, message)
IPC_MESSAGE_HANDLER(WidgetMsg_EnableDeviceEmulation,
OnEnableDeviceEmulation)
IPC_END_MESSAGE_MAP()
if (handled)
return true;
// We shouldn't receive IPC messages on provisional frames. It's possible the
// message was destined for a RenderWidget that was destroyed and then
// recreated since it keeps the same routing id. Just drop it here if that
// happened.
if (IsForProvisionalFrame())
return false;
#if defined(OS_MACOSX)
if (IPC_MESSAGE_CLASS(message) == TextInputClientMsgStart)
return text_input_client_observer_->OnMessageReceived(message);
#endif
IPC_BEGIN_MESSAGE_MAP(RenderWidget, message)
IPC_MESSAGE_HANDLER(WidgetMsg_DisableDeviceEmulation,
OnDisableDeviceEmulation)
IPC_MESSAGE_HANDLER(WidgetMsg_ShowContextMenu, OnShowContextMenu)
IPC_MESSAGE_HANDLER(WidgetMsg_Close, OnClose)
IPC_MESSAGE_HANDLER(WidgetMsg_UpdateVisualProperties,
OnUpdateVisualProperties)
IPC_MESSAGE_HANDLER(WidgetMsg_WasHidden, OnWasHidden)
IPC_MESSAGE_HANDLER(WidgetMsg_WasShown, OnWasShown)
IPC_MESSAGE_HANDLER(WidgetMsg_SetActive, OnSetActive)
IPC_MESSAGE_HANDLER(WidgetMsg_SetBounds_ACK, OnRequestSetBoundsAck)
IPC_MESSAGE_HANDLER(WidgetMsg_UpdateScreenRects, OnUpdateScreenRects)
IPC_MESSAGE_HANDLER(WidgetMsg_SetViewportIntersection,
OnSetViewportIntersection)
IPC_MESSAGE_HANDLER(WidgetMsg_WaitForNextFrameForTests,
OnWaitNextFrameForTests)
IPC_MESSAGE_HANDLER(DragMsg_TargetDragEnter, OnDragTargetDragEnter)
IPC_MESSAGE_HANDLER(DragMsg_TargetDrop, OnDragTargetDrop)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
return handled;
}
bool RenderWidget::Send(IPC::Message* message) {
// Provisional frames don't send IPCs until they are swapped in/committed.
CHECK(!IsForProvisionalFrame());
// Don't send any messages during shutdown.
DCHECK(!closing_);
// If given a messsage without a routing ID, then assign our routing ID.
if (message->routing_id() == MSG_ROUTING_NONE)
message->set_routing_id(routing_id_);
return RenderThread::Get()->Send(message);
}
bool RenderWidget::ShouldHandleImeEvents() const {
if (delegate())
return GetWebWidget()->HasFocus();
if (for_child_local_root_frame_) {
// TODO(ekaramad): We track page focus in all RenderViews on the page but
// the RenderWidgets corresponding to child local roots do not get the
// update. For now, this method returns true when the RenderWidget is for a
// child local frame, i.e., IME events will be processed regardless of page
// focus. We should revisit this after page focus for OOPIFs has been fully
// resolved (https://crbug.com/689777).
return true;
}
// Not a frame widget.
return false;
}
void RenderWidget::OnClose() {
DCHECK(popup_ || pepper_fullscreen_);
Close(base::WrapUnique(this));
}
void RenderWidget::OnUpdateVisualProperties(
const VisualProperties& visual_properties_from_browser) {
TRACE_EVENT0("renderer", "RenderWidget::OnUpdateVisualProperties");
// UpdateVisualProperties is used to receive properties from the browser
// process for this RenderWidget. There are roughly 4 types of
// VisualProperties.
// TODO(danakj): Splitting these 4 types of properties apart and making them
// more explicit could be super useful to understanding this code.
// 1. Unique to each RenderWidget. Computed by the RenderWidgetHost and passed
// to the RenderWidget which consumes it here.
// Example: new_size.
// 2. Global properties, which are given to each RenderWidget (to maintain
// the requirement that a RenderWidget is updated atomically). These
// properties are usually the same for every RenderWidget, except when
// device emulation changes them in the main frame RenderWidget only.
// Example: screen_info.
// 3. Computed in the renderer of the main frame RenderWidget (in blink
// usually). Passed down through the waterfall dance to child frame
// RenderWidgets. Here that step is performed by passing the value along
// to all RenderFrameProxy objects that are below this RenderWidgets in the
// frame tree. The main frame (top level) RenderWidget ignores this value
// from its RenderWidgetHost since it is controlled in the renderer. Child
// frame RenderWidgets consume the value from their RenderWidgetHost.
// Example: page_scale_factor.
// 4. Computed independently in the renderer for each RenderWidget (in blink
// usually). Passed down from the parent to the child RenderWidgets through
// the waterfall dance, but the value only travels one step - the child
// frame RenderWidget would compute values for grandchild RenderWidgets
// independently. Here the value is passed to child frame RenderWidgets by
// passing the value along to all RenderFrameProxy objects that are below
// this RenderWidget in the frame tree. Each RenderWidget consumes this
// value when it is received from its RenderWidgetHost.
// Example: compositor_viewport_pixel_rect.
// For each of these properties:
// If the RenderView/WebView also knows these properties, each RenderWidget
// will pass them along to the RenderView as it receives it, even if there
// are multiple RenderWidgets related to the same RenderView.
// However when the main frame in the renderer is the source of truth,
// then child widgets must not clobber that value! In all cases child frames
// do not need to update state in the RenderView when a local main frame is
// present as it always sets the value first.
// TODO(danakj): This does create a race if there are multiple
// UpdateVisualProperties updates flowing through the RenderWidget tree at
// the same time, and it seems that only one RenderWidget for each
// RenderView should be responsible for this update.
//
// This operation is done by going through RenderFrameImpl to pass the value
// to the RenderViewImpl. While this class does not use RenderViewImpl
// directly, it speaks through the RenderFrameImpl::*OnRenderView() methods.
// TODO(danakj): A more explicit API to give values from here to RenderView
// and/or WebView would be nice. Also a more explicit API to give values to
// the RenderFrameProxy in one go, instead of setting each property
// independently, causing an update IPC from the RenderFrameProxy for each
// one.
//
// See also:
// https://docs.google.com/document/d/1G_fR1D_0c1yke8CqDMddoKrDGr3gy5t_ImEH4hKNIII/edit#
VisualProperties visual_properties = visual_properties_from_browser;
// Web tests can override the device scale factor in the renderer.
if (device_scale_factor_for_testing_) {
visual_properties.screen_info.device_scale_factor =
device_scale_factor_for_testing_;
visual_properties.compositor_viewport_pixel_rect =
gfx::Rect(gfx::ScaleToCeiledSize(
visual_properties.new_size,
visual_properties.screen_info.device_scale_factor));
}
// Web tests can override the zoom level in the renderer.
if (zoom_level_for_testing_ != -INFINITY)
visual_properties.zoom_level = zoom_level_for_testing_;
// Inform the rendering thread of the color space indicating the presence of
// HDR capabilities. The HDR bit happens to be globally true/false for all
// browser windows (on Windows OS) and thus would be the same for all
// RenderWidgets, so clobbering each other works out since only the HDR bit is
// used. See https://crbug.com/803451 and
// https://chromium-review.googlesource.com/c/chromium/src/+/852912/15#message-68bbd3e25c3b421a79cd028b2533629527d21fee
//
// The RenderThreadImpl can be null in tests.
{
RenderThreadImpl* render_thread = RenderThreadImpl::current();
if (render_thread)
render_thread->SetRenderingColorSpace(
visual_properties.screen_info.color_space);
}
// TODO(danakj): In order to synchronize updates between local roots, the
// display mode should be propagated to RenderFrameProxies and down through
// their RenderWidgetHosts to child RenderWidgets via the VisualProperties
// waterfall, instead of coming to each RenderWidget independently.
// https://developer.mozilla.org/en-US/docs/Web/CSS/@media/display-mode
GetWebWidget()->SetDisplayMode(visual_properties.display_mode);
if (delegate()) {
if (size_ != visual_properties.new_size) {
// Only hide popups when the size changes. Eg https://crbug.com/761908.
blink::WebView* web_view = GetFrameWidget()->LocalRoot()->View();
web_view->CancelPagePopup();
}
SetAutoResizeMode(visual_properties.auto_resize_enabled,
visual_properties.min_size_for_auto_resize,
visual_properties.max_size_for_auto_resize,
visual_properties.screen_info.device_scale_factor);
browser_controls_params_ = visual_properties.browser_controls_params;
}
if (for_frame()) {
SetZoomLevel(visual_properties.zoom_level);
if (root_widget_window_segments_ !=
visual_properties.root_widget_window_segments) {
root_widget_window_segments_ =
visual_properties.root_widget_window_segments;
blink::WebVector<blink::WebRect> web_segments;
web_segments.reserve(root_widget_window_segments_.size());
for (const auto& segment : root_widget_window_segments_)
web_segments.emplace_back(segment);
GetWebWidget()->SetWindowSegments(std::move(web_segments));
// Propagate changes down to child local root RenderWidgets in other frame
// trees/processes.
for (auto& observer : render_frame_proxies_)
observer.OnRootWindowSegmentsChanged(root_widget_window_segments_);
}
bool capture_sequence_number_changed =
visual_properties.capture_sequence_number !=
last_capture_sequence_number_;
if (capture_sequence_number_changed) {
last_capture_sequence_number_ = visual_properties.capture_sequence_number;
// Propagate changes down to child local root RenderWidgets and
// BrowserPlugins in other frame trees/processes.
for (auto& observer : render_frame_proxies_) {
observer.UpdateCaptureSequenceNumber(
visual_properties.capture_sequence_number);
}
}
}
layer_tree_host_->SetBrowserControlsParams(
visual_properties.browser_controls_params);
if (!auto_resize_mode_) {
if (visual_properties.is_fullscreen_granted != is_fullscreen_granted_) {
is_fullscreen_granted_ = visual_properties.is_fullscreen_granted;
if (is_fullscreen_granted_)
GetWebWidget()->DidEnterFullscreen();
else
GetWebWidget()->DidExitFullscreen();
}
}
gfx::Size old_visible_viewport_size = visible_viewport_size_;
if (device_emulator_) {
DCHECK(!auto_resize_mode_);
DCHECK(!synchronous_resize_mode_for_testing_);
// TODO(danakj): Have RenderWidget grab emulated values from the emulator
// instead of making it call back into RenderWidget, then we can do this
// with a single UpdateSurfaceAndScreenInfo() call. The emulator may
// change the ScreenInfo and then will call back to RenderWidget. Before
// that we keep the current (possibly emulated) ScreenInfo.
UpdateSurfaceAndScreenInfo(
visual_properties.local_surface_id_allocation.value_or(
viz::LocalSurfaceIdAllocation()),
visual_properties.compositor_viewport_pixel_rect, screen_info_);
// This will call back into this class to set the widget size, visible
// viewport size, screen info and screen rects, based on the device
// emulation.
device_emulator_->OnSynchronizeVisualProperties(
visual_properties.screen_info, visual_properties.new_size,
visual_properties.visible_viewport_size);
} else {
// We can ignore browser-initialized resizing during synchronous
// (renderer-controlled) mode, unless it is switching us to/from
// fullsreen mode or changing the device scale factor.
bool ignore_resize_ipc = synchronous_resize_mode_for_testing_;
if (ignore_resize_ipc) {
// TODO(danakj): Does the browser actually change DSF inside a web test??
// TODO(danakj): Isn't the display mode check redundant with the
// fullscreen one?
if (visual_properties.is_fullscreen_granted != is_fullscreen_granted_ ||
visual_properties.screen_info.device_scale_factor !=
screen_info_.device_scale_factor)
ignore_resize_ipc = false;
}
// When controlling the size in the renderer, we should ignore sizes given
// by the browser IPC here.
// TODO(danakj): There are many things also being ignored that aren't the
// widget's size params. It works because tests that use this mode don't
// change those parameters, I guess. But it's more complicated then because
// it looks like they are related to sync resize mode. Let's move them out
// of this block.
if (!ignore_resize_ipc) {
gfx::Rect new_compositor_viewport_pixel_rect =
visual_properties.compositor_viewport_pixel_rect;
if (auto_resize_mode_) {
new_compositor_viewport_pixel_rect = gfx::Rect(gfx::ScaleToCeiledSize(
size_, visual_properties.screen_info.device_scale_factor));
}
UpdateSurfaceAndScreenInfo(
visual_properties.local_surface_id_allocation.value_or(
viz::LocalSurfaceIdAllocation()),
new_compositor_viewport_pixel_rect, visual_properties.screen_info);
if (for_frame()) {
RenderFrameImpl* render_frame =
RenderFrameImpl::FromWebFrame(GetFrameWidget()->LocalRoot());
// This causes compositing state to be modified which dirties the
// document lifecycle. Android Webview relies on the document
// lifecycle being clean after the RenderWidget is initialized, in
// order to send IPCs that query and change compositing state. So
// ResizeWebWidget() must come after this call, as it runs the entire
// document lifecycle.
render_frame->SetPreferCompositingToLCDTextEnabledOnRenderView(
ComputePreferCompositingToLCDText(
compositor_deps_, screen_info_.device_scale_factor));
}
// Store this even when auto-resizing, it is the size of the full viewport
// used for clipping, and this value is propagated down the RenderWidget
// hierarchy via the VisualProperties waterfall.
visible_viewport_size_ = visual_properties.visible_viewport_size;
if (!auto_resize_mode_) {
size_ = visual_properties.new_size;
ResizeWebWidget();
}
}
}
if (!delegate()) {
// The main frame controls the page scale factor, from blink. For other
// frame widgets, the page scale is received from its parent as part of
// the visual properties here. While blink doesn't need to know this
// page scale factor outside the main frame, the compositor does in
// order to produce its output at the correct scale.
layer_tree_host_->SetExternalPageScaleFactor(
visual_properties.page_scale_factor,
visual_properties.is_pinch_gesture_active);
// Store the value to give to any new RenderFrameProxy that is
// registered.
page_scale_factor_from_mainframe_ = visual_properties.page_scale_factor;
// Similarly, only the main frame knows when a pinch gesture is active,
// but this information is needed in subframes so they can throttle
// re-rastering in the same manner as the main frame.
// |is_pinch_gesture_active| follows the same path to the subframe
// compositor(s) as |page_scale_factor|.
is_pinch_gesture_active_from_mainframe_ =
visual_properties.is_pinch_gesture_active;
// Push the page scale factor down to any child RenderWidgets via our
// child proxy frames.
// TODO(danakj): This ends up setting the page scale factor in the
// RenderWidgetHost of the child RenderWidget, so that it can bounce
// the value down to its RenderWidget. Since this is essentially a
// global value per-page, we could instead store it once in the browser
// (such as in RenderViewHost) and distribute it to each frame-hosted
// RenderWidget from there.
for (auto& child_proxy : render_frame_proxies_) {
child_proxy.OnPageScaleFactorChanged(
visual_properties.page_scale_factor,
visual_properties.is_pinch_gesture_active);
}
}
if (old_visible_viewport_size != visible_viewport_size_) {
for (auto& render_frame : render_frames_)
render_frame.ResetHasScrolledFocusedEditableIntoView();
// Propagate changes down to child local root RenderWidgets and
// BrowserPlugins in other frame trees/processes.
for (auto& observer : render_frame_proxies_)
observer.OnVisibleViewportSizeChanged(visible_viewport_size_);
}
// TODO(crbug.com/939118): ScrollFocusedNodeIntoViewForWidget does not work
// when the focused node is inside an OOPIF. This code path where
// scroll_focused_node_into_view is set is used only for WebView, crbug
// 939118 tracks fixing webviews to not use scroll_focused_node_into_view.
if (delegate() && visual_properties.scroll_focused_node_into_view)
delegate()->ScrollFocusedNodeIntoViewForWidget();
AfterUpdateVisualProperties();
}
void RenderWidget::OnEnableDeviceEmulation(
const blink::WebDeviceEmulationParams& params) {
// Device emulation can only be applied to the local main frame render widget.
// TODO(https://crbug.com/1006052): We should move emulation into the browser
// and send consistent ScreenInfo and ScreenRects to all RenderWidgets based
// on emulation.
if (!delegate_)
return;
if (!device_emulator_) {
device_emulator_ = std::make_unique<RenderWidgetScreenMetricsEmulator>(
this, screen_info_, size_, visible_viewport_size_, widget_screen_rect_,
window_screen_rect_);
}
device_emulator_->ChangeEmulationParams(params);
}
void RenderWidget::OnDisableDeviceEmulation() {
// Device emulation can only be applied to the local main frame render widget.
// TODO(https://crbug.com/1006052): We should move emulation into the browser
// and send consistent ScreenInfo and ScreenRects to all RenderWidgets based
// on emulation.
if (!delegate_ || !device_emulator_)
return;
device_emulator_->DisableAndApply();
device_emulator_.reset();
}
float RenderWidget::GetEmulatorScale() const {
if (device_emulator_)
return device_emulator_->scale();
return 1;
}
void RenderWidget::SetAutoResizeMode(bool auto_resize,
const gfx::Size& min_size_before_dsf,
const gfx::Size& max_size_before_dsf,
float device_scale_factor) {
bool was_changed = auto_resize_mode_ != auto_resize;
auto_resize_mode_ = auto_resize;
min_size_for_auto_resize_ = min_size_before_dsf;
max_size_for_auto_resize_ = max_size_before_dsf;
if (auto_resize) {
gfx::Size min_auto_size = min_size_for_auto_resize_;
gfx::Size max_auto_size = max_size_for_auto_resize_;
if (compositor_deps_->IsUseZoomForDSFEnabled()) {
min_auto_size =
gfx::ScaleToCeiledSize(min_auto_size, device_scale_factor);
max_auto_size =
gfx::ScaleToCeiledSize(max_auto_size, device_scale_factor);
}
delegate()->ApplyAutoResizeLimitsForWidget(min_auto_size, max_auto_size);
} else if (was_changed) {
delegate()->DisableAutoResizeForWidget();
}
}
void RenderWidget::SetZoomLevel(double zoom_level) {
RenderFrameImpl* render_frame =
RenderFrameImpl::FromWebFrame(GetFrameWidget()->LocalRoot());
bool zoom_level_changed = render_frame->SetZoomLevelOnRenderView(zoom_level);
if (zoom_level_changed) {
// Hide popups when the zoom changes.
// TODO(danakj): This should go through RenderFrame, and the Delegate path
// should be replaced.
blink::WebView* web_view = GetFrameWidget()->LocalRoot()->View();
web_view->CancelPagePopup();
// Propagate changes down to child local root RenderWidgets and
// BrowserPlugins in other frame trees/processes.
zoom_level_ = zoom_level;
for (auto& observer : render_frame_proxies_)
observer.OnZoomLevelChanged(zoom_level);
}
}
void RenderWidget::OnWasHidden() {
// A provisional frame widget will never be hidden since that would require it
// to be shown first. A frame must be attached to the frame tree before
// changing visibility.
DCHECK(!IsForProvisionalFrame());
TRACE_EVENT0("renderer", "RenderWidget::OnWasHidden");
SetHidden(true);
tab_switch_time_recorder_.TabWasHidden();
for (auto& observer : render_frames_)
observer.WasHidden();
}
void RenderWidget::OnWasShown(
base::TimeTicks show_request_timestamp,
bool was_evicted,
const base::Optional<content::RecordContentToVisibleTimeRequest>&
record_tab_switch_time_request) {
// The frame must be attached to the frame tree (which makes it no longer
// provisional) before changing visibility.
DCHECK(!IsForProvisionalFrame());
TRACE_EVENT_WITH_FLOW0("renderer", "RenderWidget::OnWasShown", routing_id(),
TRACE_EVENT_FLAG_FLOW_IN);
SetHidden(false);
if (record_tab_switch_time_request) {
layer_tree_host_->RequestPresentationTimeForNextFrame(
tab_switch_time_recorder_.TabWasShown(
false /* has_saved_frames */,
record_tab_switch_time_request.value(), show_request_timestamp));
}
for (auto& observer : render_frames_)
observer.WasShown();
if (was_evicted) {
for (auto& observer : render_frame_proxies_)
observer.WasEvicted();
}
}
void RenderWidget::OnRequestSetBoundsAck() {
DCHECK(pending_window_rect_count_);
pending_window_rect_count_--;
}
void RenderWidget::RequestPresentation(PresentationTimeCallback callback) {
layer_tree_host_->RequestPresentationTimeForNextFrame(std::move(callback));
layer_tree_host_->SetNeedsCommitWithForcedRedraw();
}
void RenderWidget::RequestPresentationAfterScrollAnimationEnd(
PresentationTimeCallback callback) {
layer_tree_host_->RequestScrollAnimationEndNotification(
base::BindOnce(&RenderWidget::RequestPresentation,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}
viz::FrameSinkId RenderWidget::GetFrameSinkIdAtPoint(const gfx::PointF& point,
gfx::PointF* local_point) {
blink::WebHitTestResult result = GetHitTestResultAtPoint(point);
blink::WebNode result_node = result.GetNode();
*local_point = gfx::PointF(point);
// TODO(crbug.com/797828): When the node is null the caller may
// need to do extra checks. Like maybe update the layout and then
// call the hit-testing API. Either way it might be better to have
// a DCHECK for the node rather than a null check here.
if (result_node.IsNull()) {
return GetFrameSinkId();
}
viz::FrameSinkId frame_sink_id = GetRemoteFrameSinkId(result);
if (frame_sink_id.is_valid()) {
*local_point = gfx::PointF(result.LocalPointWithoutContentBoxOffset());
if (compositor_deps()->IsUseZoomForDSFEnabled()) {
*local_point = gfx::ConvertPointToDIP(
GetOriginalScreenInfo().device_scale_factor, *local_point);
}
return frame_sink_id;
}
// Return the FrameSinkId for the current widget if the point did not hit
// test to a remote frame, or the point is outside of the remote frame's
// content box, or the remote frame doesn't have a valid FrameSinkId yet.
return GetFrameSinkId();
}
bool RenderWidget::HandleInputEvent(
const blink::WebCoalescedInputEvent& input_event,
HandledEventCallback callback) {
// Temporarily convert between the blink callback and the main thread
// event queue callback. This will go away once the MainThreadEventQueue
// moves into blink.
WebWidget::HandledEventCallback blink_callback = base::BindOnce(
[](HandledEventCallback callback,
blink::mojom::InputEventResultState ack_state,
const ui::LatencyInfo& latency_info,
std::unique_ptr<blink::InputHandlerProxy::DidOverscrollParams>
overscroll_params,
base::Optional<cc::TouchAction> touch_action) {
if (!callback)
return;
std::move(callback).Run(ack_state, latency_info,
ToOverscrollParams(overscroll_params.get()),
touch_action);
},
std::move(callback));
webwidget_->ProcessInputEventSynchronously(input_event,
std::move(blink_callback));
return true;
}
void RenderWidget::SetNeedsMainFrame() {
ScheduleAnimation();
}
scoped_refptr<MainThreadEventQueue> RenderWidget::GetInputEventQueue() {
return input_event_queue_;
}
void RenderWidget::OnCursorVisibilityChange(bool is_visible) {
GetWebWidget()->SetCursorVisibilityState(is_visible);
}
void RenderWidget::OnMouseCaptureLost() {
GetWebWidget()->MouseCaptureLost();
}
void RenderWidget::OnSetEditCommandsForNextKeyEvent(
std::vector<blink::mojom::EditCommandPtr> edit_commands) {
if (auto* frame_widget = GetFrameWidget()) {
for (auto& command : edit_commands) {
frame_widget->AddEditCommandForNextKeyEvent(
WebString::FromUTF8(command->name),
WebString::FromUTF8(command->value));
}
}
}
void RenderWidget::OnSetActive(bool active) {
if (delegate())
delegate()->SetActiveForWidget(active);
}
void RenderWidget::OnSetFocus(bool enable) {
GetWebWidget()->SetFocus(enable);
}
void RenderWidget::FocusChanged(bool enable) {
if (delegate())
delegate()->DidReceiveSetFocusEventForWidget();
for (auto& observer : render_frames_)
observer.RenderWidgetSetFocus(enable);
}
void RenderWidget::DispatchRafAlignedInput(base::TimeTicks frame_time) {
input_event_queue_->DispatchRafAlignedInput(frame_time);
}
void RenderWidget::OnDeferMainFrameUpdatesChanged(bool deferral_state) {
// LayerTreeHost::CreateThreaded() will defer main frame updates immediately
// until it gets a LocalSurfaceIdAllocation. That's before the
// |widget_input_handler_manager_| is created, so it can be null here. We
// detect that by seeing a null LayerTreeHost.
// TODO(schenney): To avoid ping-ponging between defer main frame states
// during initialization, and requiring null checks here, we should probably
// pass the LocalSurfaceIdAllocation to the compositor while it is
// initialized so that it doesn't have to immediately switch into deferred
// mode without being requested to.
if (!layer_tree_host_)
return;
// The input handler wants to know about the mainframe update status to
// enable/disable input and for metrics.
widget_input_handler_manager_->OnDeferMainFrameUpdatesChanged(deferral_state);
}
void RenderWidget::OnDeferCommitsChanged(bool deferral_state) {
// The input handler wants to know about the commit status for metric purposes
// and to enable/disable input.
widget_input_handler_manager_->OnDeferCommitsChanged(deferral_state);
}
void RenderWidget::RequestNewLayerTreeFrameSink(
LayerTreeFrameSinkCallback callback) {
// For widgets that are never visible, we don't start the compositor, so we
// never get a request for a cc::LayerTreeFrameSink.
DCHECK(!never_composited_);
GURL url = GetWebWidget()->GetURLForDebugTrace();
// The |url| is not always available, fallback to a fixed string.
if (url.is_empty())
url = GURL("chrome://gpu/RenderWidget::RequestNewLayerTreeFrameSink");
// TODO(danakj): This may not be accurate, depending on the intent. A child
// local root could be in the same process as the view, so if the client is
// meant to designate the process type, it seems kRenderer would be the
// correct choice. If client is meant to designate the widget type, then
// kOOPIF would denote that it is not for the main frame. However, kRenderer
// would also be used for other widgets such as popups.
const char* client_name = for_child_local_root_frame_ ? kOOPIF : kRenderer;
compositor_deps_->RequestNewLayerTreeFrameSink(
this, frame_swap_message_queue_, std::move(url), std::move(callback),
client_name);
}
void RenderWidget::DidCommitAndDrawCompositorFrame() {
// NOTE: Tests may break if this event is renamed or moved. See
// tab_capture_performancetest.cc.
TRACE_EVENT0("gpu", "RenderWidget::DidCommitAndDrawCompositorFrame");
for (auto& observer : render_frames_)
observer.DidCommitAndDrawCompositorFrame();
}
void RenderWidget::DidCommitCompositorFrame(base::TimeTicks commit_start_time) {
if (delegate())
delegate()->DidCommitCompositorFrameForWidget();
}
void RenderWidget::DidCompletePageScaleAnimation() {
if (delegate())
delegate()->DidCompletePageScaleAnimationForWidget();
}
void RenderWidget::ScheduleAnimation() {
// This call is not needed in single thread mode for tests without a
// scheduler, but they override this method in order to schedule a synchronous
// composite task themselves.
layer_tree_host_->SetNeedsAnimate();
}
void RenderWidget::RecordTimeToFirstActivePaint(base::TimeDelta duration) {
RenderThreadImpl* render_thread_impl = RenderThreadImpl::current();
if (render_thread_impl->NeedsToRecordFirstActivePaint(TTFAP_AFTER_PURGED)) {
UMA_HISTOGRAM_TIMES("PurgeAndSuspend.Experimental.TimeToFirstActivePaint",
duration);
}
if (render_thread_impl->NeedsToRecordFirstActivePaint(
TTFAP_5MIN_AFTER_BACKGROUNDED)) {
UMA_HISTOGRAM_TIMES(
"PurgeAndSuspend.Experimental.TimeToFirstActivePaint."
"AfterBackgrounded.5min",
duration);
}
}
void RenderWidget::DidHandleGestureScrollEvent(
const blink::WebGestureEvent& gesture_event,
const gfx::Vector2dF& unused_delta,
const cc::OverscrollBehavior& overscroll_behavior,
bool event_processed) {
if (!compositor_deps_->IsElasticOverscrollEnabled())
return;
cc::InputHandlerScrollResult scroll_result;
scroll_result.did_scroll = event_processed;
scroll_result.did_overscroll_root = !unused_delta.IsZero();
scroll_result.unused_scroll_delta = unused_delta;
scroll_result.overscroll_behavior = overscroll_behavior;
widget_input_handler_manager_->ObserveGestureEventOnMainThread(gesture_event,
scroll_result);
}
void RenderWidget::DidOverscroll(const gfx::Vector2dF& overscroll_delta,
const gfx::Vector2dF& accumulated_overscroll,
const gfx::PointF& position_in_viewport,
const gfx::Vector2dF& velocity_in_viewport,
cc::OverscrollBehavior overscroll_behavior) {
if (blink::mojom::WidgetInputHandlerHost* host =
widget_input_handler_manager_->GetWidgetInputHandlerHost()) {
host->DidOverscroll(blink::mojom::DidOverscrollParams::New(
accumulated_overscroll, overscroll_delta, velocity_in_viewport,
position_in_viewport, overscroll_behavior));
}
}
void RenderWidget::QueueSyntheticEvent(
std::unique_ptr<blink::WebCoalescedInputEvent> event) {
// TODO(acomminos): If/when we add support for gesture event attribution on
// the impl thread, have the caller provide attribution.
blink::WebInputEventAttribution attribution;
GetInputEventQueue()->HandleEvent(
std::move(event), DISPATCH_TYPE_NON_BLOCKING,
blink::mojom::InputEventResultState::kNotConsumed, attribution,
HandledEventCallback());
}
void RenderWidget::GetWidgetInputHandler(
blink::CrossVariantMojoReceiver<
blink::mojom::WidgetInputHandlerInterfaceBase> widget_input_receiver,
blink::CrossVariantMojoRemote<
blink::mojom::WidgetInputHandlerHostInterfaceBase>
widget_input_host_remote) {
widget_input_handler_manager_->AddInterface(
std::move(widget_input_receiver), std::move(widget_input_host_remote));
}
bool RenderWidget::HasCurrentImeGuard(bool request_to_show_virtual_keyboard) {
if (!ime_event_guard_)
return false;
if (request_to_show_virtual_keyboard)
ime_event_guard_->set_show_virtual_keyboard(true);
return true;
}
void RenderWidget::SendCompositionRangeChanged(
const gfx::Range& range,
const std::vector<gfx::Rect>& character_bounds) {
if (blink::mojom::WidgetInputHandlerHost* host =
widget_input_handler_manager_->GetWidgetInputHandlerHost()) {
host->ImeCompositionRangeChanged(range, character_bounds);
}
}
bool RenderWidget::CanComposeInline() {
#if BUILDFLAG(ENABLE_PLUGINS)
if (auto* plugin = GetFocusedPepperPluginInsideWidget())
return plugin->IsPluginAcceptingCompositionEvents();
#endif
return true;
}
bool RenderWidget::ShouldDispatchImeEventsToPepper() {
#if BUILDFLAG(ENABLE_PLUGINS)
return GetFocusedPepperPluginInsideWidget();
#else
return false;
#endif
}
blink::WebTextInputType RenderWidget::GetPepperTextInputType() {
#if BUILDFLAG(ENABLE_PLUGINS)
return ConvertTextInputType(
GetFocusedPepperPluginInsideWidget()->text_input_type());
#else
NOTREACHED();
return blink::WebTextInputType::kWebTextInputTypeNone;
#endif
}
gfx::Rect RenderWidget::GetPepperCaretBounds() {
#if BUILDFLAG(ENABLE_PLUGINS)
blink::WebRect caret(GetFocusedPepperPluginInsideWidget()->GetCaretBounds());
ConvertViewportToWindow(&caret);
return caret;
#else
NOTREACHED();
return gfx::Rect();
#endif
}
void RenderWidget::UpdateTextInputState() {
GetWebWidget()->UpdateTextInputState();
}
bool RenderWidget::WillHandleGestureEvent(const blink::WebGestureEvent& event) {
possible_drag_event_info_.event_source =
ui::DragDropTypes::DRAG_EVENT_SOURCE_TOUCH;
possible_drag_event_info_.event_location =
gfx::ToFlooredPoint(event.PositionInScreen());
return false;
}
bool RenderWidget::WillHandleMouseEvent(const blink::WebMouseEvent& event) {
for (auto& observer : render_frames_)
observer.RenderWidgetWillHandleMouseEvent();
possible_drag_event_info_.event_source =
ui::DragDropTypes::DRAG_EVENT_SOURCE_MOUSE;
possible_drag_event_info_.event_location =
gfx::Point(event.PositionInScreen().x(), event.PositionInScreen().y());
return mouse_lock_dispatcher()->WillHandleMouseEvent(event);
}
void RenderWidget::ResizeWebWidget() {
// In auto resize mode, blink controls sizes and RenderWidget should not be
// passing values back in.
DCHECK(!auto_resize_mode_);
// The widget size given to blink is scaled by the (non-emulated,
// see https://crbug.com/819903) device scale factor (if UseZoomForDSF is
// enabled).
gfx::Size size_for_blink;
if (!compositor_deps_->IsUseZoomForDSFEnabled()) {
size_for_blink = size_;
} else {
size_for_blink = gfx::ScaleToCeiledSize(
size_, GetOriginalScreenInfo().device_scale_factor);
}
// The |visible_viewport_size| given to blink is scaled by the (non-emulated,
// see https://crbug.com/819903) device scale factor (if UseZoomForDSF is
// enabled).
gfx::Size visible_viewport_size_for_blink;
if (!compositor_deps_->IsUseZoomForDSFEnabled()) {
visible_viewport_size_for_blink = visible_viewport_size_;
} else {
visible_viewport_size_for_blink = gfx::ScaleToCeiledSize(
visible_viewport_size_, GetOriginalScreenInfo().device_scale_factor);
}
if (delegate()) {
// When associated with a RenderView, the RenderView is in control of the
// main frame's size, because it includes other factors for top and bottom
// controls.
delegate()->ResizeWebWidgetForWidget(size_for_blink,
visible_viewport_size_for_blink,
browser_controls_params_);
} else {
// Child frames set the |visible_viewport_size| on the RenderView/WebView to
// limit the size blink tries to composite when the widget is not visible,
// such as when it is scrolled out of the main frame's view.
if (for_frame()) {
RenderFrameImpl* render_frame =
RenderFrameImpl::FromWebFrame(GetFrameWidget()->LocalRoot());
render_frame->SetVisibleViewportSizeForChildLocalRootOnRenderView(
visible_viewport_size_for_blink);
}
// For child frame widgets, popups, and pepper, the RenderWidget is in
// control of the WebWidget's size.
GetWebWidget()->Resize(size_for_blink);
}
}
gfx::Rect RenderWidget::CompositorViewportRect() const {
return layer_tree_host_->device_viewport_rect();
}
void RenderWidget::SetScreenInfoAndSize(
const ScreenInfo& screen_info,
const gfx::Size& widget_size,
const gfx::Size& visible_viewport_size) {
// Emulation only happens on the main frame.
DCHECK(delegate());
DCHECK(for_frame());
// Emulation happens on regular main frames which don't use auto-resize mode.
DCHECK(!auto_resize_mode_);
UpdateSurfaceAndScreenInfo(local_surface_id_allocation_from_parent_,
CompositorViewportRect(), screen_info);
RenderFrameImpl* render_frame =
RenderFrameImpl::FromWebFrame(GetFrameWidget()->LocalRoot());
// UpdateSurfaceAndScreenInfo() changes properties including the device scale
// factor, which changes PreferCompositingToLCDText decisions.
// TODO(danakj): Do this in UpdateSurfaceAndScreenInfo? But requires a Resize
// to happen after (see comment on
// SetPreferCompositingToLCDTextEnabledOnRenderView).
//
// This causes compositing state to be modified which dirties the document
// lifecycle. Android Webview relies on the document lifecycle being clean
// after the RenderWidget is initialized, in order to send IPCs that query
// and change compositing state. So ResizeWebWidget() must come after this
// call, as it runs the entire document lifecycle.
render_frame->SetPreferCompositingToLCDTextEnabledOnRenderView(
ComputePreferCompositingToLCDText(compositor_deps_,
screen_info_.device_scale_factor));
visible_viewport_size_ = visible_viewport_size;
size_ = widget_size;
ResizeWebWidget();
}
void RenderWidget::SetScreenMetricsEmulationParameters(
bool enabled,
const blink::WebDeviceEmulationParams& params) {
// This is only supported in RenderView, which has an delegate().
DCHECK(delegate());
delegate()->SetScreenMetricsEmulationParametersForWidget(enabled, params);
}
void RenderWidget::SetScreenRects(const gfx::Rect& widget_screen_rect,
const gfx::Rect& window_screen_rect) {
widget_screen_rect_ = widget_screen_rect;
window_screen_rect_ = window_screen_rect;
}
///////////////////////////////////////////////////////////////////////////////
// WebWidgetClient
void RenderWidget::DidMeaningfulLayout(blink::WebMeaningfulLayout layout_type) {
for (auto& observer : render_frames_)
observer.DidMeaningfulLayout(layout_type);
}
// static
std::unique_ptr<cc::SwapPromise> RenderWidget::QueueMessageImpl(
std::unique_ptr<IPC::Message> msg,
FrameSwapMessageQueue* frame_swap_message_queue,
scoped_refptr<IPC::SyncMessageFilter> sync_message_filter,
int source_frame_number) {
bool first_message_for_frame = false;
frame_swap_message_queue->QueueMessageForFrame(
source_frame_number, std::move(msg), &first_message_for_frame);
if (!first_message_for_frame)
return nullptr;
return std::make_unique<QueueMessageSwapPromise>(
sync_message_filter, frame_swap_message_queue, source_frame_number);
}
void RenderWidget::SetHandlingInputEvent(bool handling_input_event) {
GetWebWidget()->SetHandlingInputEvent(handling_input_event);
}
void RenderWidget::QueueMessage(std::unique_ptr<IPC::Message> msg) {
// RenderThreadImpl::current() is NULL in some tests.
if (!RenderThreadImpl::current()) {
Send(msg.release());
return;
}
std::unique_ptr<cc::SwapPromise> swap_promise =
QueueMessageImpl(std::move(msg), frame_swap_message_queue_.get(),
RenderThreadImpl::current()->sync_message_filter(),
layer_tree_host_->SourceFrameNumber());
if (swap_promise) {
layer_tree_host_->QueueSwapPromise(std::move(swap_promise));
// Request a main frame if one is not already in progress. This might either
// A) request a commit ahead of time or B) request a commit which is not
// needed because there are not pending updates. If B) then the frame will
// be aborted early and the swap promises will be broken (see
// EarlyOut_NoUpdates).
layer_tree_host_->SetNeedsAnimateIfNotInsideMainFrame();
}
}
// We are supposed to get a single call to Show for a newly created RenderWidget
// that was created via RenderWidget::CreateWebView. So, we wait until this
// point to dispatch the ShowWidget message.
//
// This method provides us with the information about how to display the newly
// created RenderWidget (i.e., as a blocked popup or as a new tab).
//
void RenderWidget::Show(WebNavigationPolicy policy) {
if (!show_callback_) {
if (delegate()) {
// When SupportsMultipleWindows is disabled, popups are reusing
// the view's RenderWidget. In some scenarios, this makes blink to call
// Show() twice. But otherwise, if it is enabled, we should not visit
// Show() more than once.
DCHECK(!delegate()->SupportsMultipleWindowsForWidget());
return;
} else {
NOTREACHED() << "received extraneous Show call";
}
}
DCHECK(routing_id_ != MSG_ROUTING_NONE);
// The opener is responsible for actually showing this widget.
std::move(show_callback_).Run(this, policy, initial_rect_);
// NOTE: initial_rect_ may still have its default values at this point, but
// that's okay. It'll be ignored if as_popup is false, or the browser
// process will impose a default position otherwise.
SetPendingWindowRect(initial_rect_);
}
void RenderWidget::InitCompositing(const ScreenInfo& screen_info) {
TRACE_EVENT0("blink", "RenderWidget::InitializeLayerTreeView");
layer_tree_host_ = webwidget_->InitializeCompositing(
compositor_deps_->GetTaskGraphRunner(),
GenerateLayerTreeSettings(compositor_deps_, for_child_local_root_frame_,
screen_info.rect.size(),
screen_info.device_scale_factor),
compositor_deps_->CreateUkmRecorderFactory());
DCHECK(layer_tree_host_);
blink::scheduler::WebThreadScheduler* main_thread_scheduler =
compositor_deps_->GetWebMainThreadScheduler();
widget_scheduler_ = main_thread_scheduler->CreateWidgetScheduler();
blink::scheduler::WebThreadScheduler* compositor_thread_scheduler =
blink::scheduler::WebThreadScheduler::CompositorThreadScheduler();
scoped_refptr<base::SingleThreadTaskRunner> compositor_input_task_runner;
// Use the compositor thread task runner unless this is a popup or other such
// non-frame widgets. The |compositor_thread_scheduler| can be null in tests
// without a compositor thread.
if (for_frame() && compositor_thread_scheduler) {
compositor_input_task_runner =
compositor_thread_scheduler->DefaultTaskRunner();
}
input_event_queue_ = base::MakeRefCounted<MainThreadEventQueue>(
this, widget_scheduler_->InputTaskRunner(), main_thread_scheduler,
/*allow_raf_aligned_input=*/!never_composited_);
// We only use an external input handler for frame RenderWidgets because only
// frames use the compositor for input handling. Other kinds of RenderWidgets
// (e.g. popups, plugins) must forward their input directly through
// RenderWidgetInputHandler into Blink.
bool uses_input_handler = for_frame();
widget_input_handler_manager_ = WidgetInputHandlerManager::Create(
weak_ptr_factory_.GetWeakPtr(), std::move(compositor_input_task_runner),
main_thread_scheduler, uses_input_handler);
const base::CommandLine& command_line =
*base::CommandLine::ForCurrentProcess();
if (command_line.HasSwitch(blink::switches::kAllowPreCommitInput))
widget_input_handler_manager_->AllowPreCommitInput();
}
// static
void RenderWidget::DoDeferredClose(int widget_routing_id) {
// DoDeferredClose() was a posted task, which means the RenderWidget may have
// been destroyed in the meantime. So break the dependency on RenderWidget
// here, by making this method static and going to RenderThread directly to
// send.
RenderThread::Get()->Send(new WidgetHostMsg_Close(widget_routing_id));
}
void RenderWidget::ClosePopupWidgetSoon() {
// Only should be called for popup widgets.
DCHECK(!for_child_local_root_frame_);
DCHECK(!delegate_);
CloseWidgetSoon();
}
void RenderWidget::CloseWidgetSoon() {
DCHECK(RenderThread::IsMainThread());
// If a page calls window.close() twice, we'll end up here twice, but that's
// OK. It is safe to send multiple Close messages.
//
// Ask the RenderWidgetHost to initiate close. We could be called from deep
// in Javascript. If we ask the RenderWidgetHost to close now, the window
// could be closed before the JS finishes executing, thanks to nested message
// loops running and handling the resuliting Close IPC. So instead, post a
// message back to the message loop, which won't run until the JS is
// complete, and then the Close request can be sent.
compositor_deps_->GetCleanupTaskRunner()->PostTask(
FROM_HERE, base::BindOnce(&RenderWidget::DoDeferredClose, routing_id_));
}
void RenderWidget::Close(std::unique_ptr<RenderWidget> widget) {
// At the end of this method, |widget| which points to this is deleted.
DCHECK_EQ(widget.get(), this);
DCHECK(RenderThread::IsMainThread());
DCHECK(!closing_);
closing_ = true;
// Browser correspondence is no longer needed at this point.
if (routing_id_ != MSG_ROUTING_NONE) {
RenderThread::Get()->RemoveRoute(routing_id_);
}
// The |input_event_queue_| is refcounted and will live while an event is
// being handled. This drops the connection back to this RenderWidget which
// is being destroyed.
input_event_queue_->ClearClient();
// The |widget_input_handler_manager_| needs to outlive the LayerTreeHost,
// which is destroyed asynchronously by Close(). We pass ownership of it to
// Close() for it to destroy the LayerTreeHost and
// |widget_input_handler_manager_| together on the cleanup TaskRunner.
webwidget_->Close(
compositor_deps_->GetCleanupTaskRunner(),
base::BindOnce([](scoped_refptr<WidgetInputHandlerManager> manager) {},
std::move(widget_input_handler_manager_)));
webwidget_ = nullptr;
// |layer_tree_host_| is valid only when |webwidget_| is valid. Close may
// use the WebWidgetClient while unloading the Frame so we clear this
// after.
layer_tree_host_ = nullptr;
// Note the ACK is a control message going to the RenderProcessHost.
RenderThread::Get()->Send(new WidgetHostMsg_Close_ACK(routing_id()));
}
blink::WebFrameWidget* RenderWidget::GetFrameWidget() const {
// TODO(danakj): Remove this check and don't call this method for non-frames.
if (!for_frame())
return nullptr;
return static_cast<blink::WebFrameWidget*>(webwidget_);
}
bool RenderWidget::IsForProvisionalFrame() const {
if (!for_frame())
return false;
// No widget here means the main frame is remote and there is no
// provisional frame at the moment.
if (!webwidget_)
return false;
auto* frame_widget = static_cast<blink::WebFrameWidget*>(webwidget_);
return frame_widget->LocalRoot()->IsProvisional();
}
void RenderWidget::ScreenRectToEmulated(gfx::Rect* screen_rect) const {
screen_rect->set_x(
opener_widget_screen_origin_.x() +
(screen_rect->x() - opener_original_widget_screen_origin_.x()) /
opener_emulator_scale_);
screen_rect->set_y(
opener_widget_screen_origin_.y() +
(screen_rect->y() - opener_original_widget_screen_origin_.y()) /
opener_emulator_scale_);
}
void RenderWidget::EmulatedToScreenRect(gfx::Rect* screen_rect) const {
screen_rect->set_x(opener_original_widget_screen_origin_.x() +
(screen_rect->x() - opener_widget_screen_origin_.x()) *
opener_emulator_scale_);
screen_rect->set_y(opener_original_widget_screen_origin_.y() +
(screen_rect->y() - opener_widget_screen_origin_.y()) *
opener_emulator_scale_);
}
blink::WebScreenInfo RenderWidget::GetScreenInfo() {
const ScreenInfo& info = screen_info_;
blink::WebScreenInfo web_screen_info;
web_screen_info.device_scale_factor = info.device_scale_factor;
web_screen_info.color_space = info.color_space;
web_screen_info.depth = info.depth;
web_screen_info.depth_per_component = info.depth_per_component;
web_screen_info.is_monochrome = info.is_monochrome;
web_screen_info.rect = blink::WebRect(info.rect);
web_screen_info.available_rect = blink::WebRect(info.available_rect);
switch (info.orientation_type) {
case SCREEN_ORIENTATION_VALUES_PORTRAIT_PRIMARY:
web_screen_info.orientation_type =
blink::kWebScreenOrientationPortraitPrimary;
break;
case SCREEN_ORIENTATION_VALUES_PORTRAIT_SECONDARY:
web_screen_info.orientation_type =
blink::kWebScreenOrientationPortraitSecondary;
break;
case SCREEN_ORIENTATION_VALUES_LANDSCAPE_PRIMARY:
web_screen_info.orientation_type =
blink::kWebScreenOrientationLandscapePrimary;
break;
case SCREEN_ORIENTATION_VALUES_LANDSCAPE_SECONDARY:
web_screen_info.orientation_type =
blink::kWebScreenOrientationLandscapeSecondary;
break;
default:
web_screen_info.orientation_type = blink::kWebScreenOrientationUndefined;
break;
}
web_screen_info.orientation_angle = info.orientation_angle;
return web_screen_info;
}
WebRect RenderWidget::WindowRect() {
gfx::Rect rect;
if (pending_window_rect_count_) {
// NOTE(mbelshe): If there is a pending_window_rect_, then getting
// the RootWindowRect is probably going to return wrong results since the
// browser may not have processed the Move yet. There isn't really anything
// good to do in this case, and it shouldn't happen - since this size is
// only really needed for windowToScreen, which is only used for Popups.
rect = pending_window_rect_;
} else {
rect = window_screen_rect_;
}
// Popup widgets aren't emulated, but the WindowRect (aka WindowScreenRect)
// given to them should be.
if (opener_emulator_scale_) {
DCHECK(popup_);
ScreenRectToEmulated(&rect);
}
return rect;
}
WebRect RenderWidget::ViewRect() {
gfx::Rect rect = widget_screen_rect_;
// Popup widgets aren't emulated, but the ViewRect (aka WidgetScreenRect)
// given to them should be.
if (opener_emulator_scale_) {
DCHECK(popup_);
ScreenRectToEmulated(&rect);
}
return rect;
}
void RenderWidget::SetWindowRect(const WebRect& rect_in_screen) {
// This path is for the renderer to change the on-screen position/size of
// the widget by changing its window rect. This is not possible for
// RenderWidgets whose position/size are controlled by layout from another
// frame tree (ie. child local root frames), as the window rect can only be
// set by the browser.
if (for_child_local_root_frame_)
return;
gfx::Rect window_rect = rect_in_screen;
// Popups aren't emulated, but the WidgetScreenRect and WindowScreenRect
// given to them are. When they set the WindowScreenRect it is based on those
// emulated values, so we reverse the emulation.
if (opener_emulator_scale_) {
DCHECK(popup_);
EmulatedToScreenRect(&window_rect);
}
if (synchronous_resize_mode_for_testing_) {
// This is a web-test-only path. At one point, it was planned to be
// removed. See https://crbug.com/309760.
SetWindowRectSynchronously(window_rect);
return;
}
if (show_callback_) {
// The widget is not shown yet. Delay the |window_rect| being sent to the
// browser until Show() is called so it can be sent with that IPC, once the
// browser is ready for the info.
initial_rect_ = window_rect;
} else {
Send(new WidgetHostMsg_RequestSetBounds(routing_id_, window_rect));
SetPendingWindowRect(window_rect);
}
}
void RenderWidget::SetPendingWindowRect(const WebRect& rect) {
pending_window_rect_ = rect;
pending_window_rect_count_++;
// Popups don't get size updates back from the browser so just store the set
// values.
if (!for_frame()) {
window_screen_rect_ = rect;
widget_screen_rect_ = rect;
}
}
void RenderWidget::OnShowContextMenu(ui::MenuSourceType source_type,
const gfx::Point& location) {
has_host_context_menu_location_ = true;
host_context_menu_location_ = location;
GetWebWidget()->ShowContextMenu(
static_cast<blink::WebMenuSourceType>(source_type));
has_host_context_menu_location_ = false;
}
void RenderWidget::OnImeSetComposition(
const base::string16& text,
const std::vector<ui::ImeTextSpan>& ime_text_spans,
const gfx::Range& replacement_range,
int selection_start,
int selection_end) {
if (!ShouldHandleImeEvents())
return;
#if BUILDFLAG(ENABLE_PLUGINS)
if (auto* plugin = GetFocusedPepperPluginInsideWidget()) {
plugin->render_frame()->OnImeSetComposition(text, ime_text_spans,
selection_start, selection_end);
return;
}
#endif
ImeEventGuard guard(weak_ptr_factory_.GetWeakPtr());
blink::WebInputMethodController* controller = GetInputMethodController();
if (!controller ||
!controller->SetComposition(
WebString::FromUTF16(text),
WebVector<ui::ImeTextSpan>(ime_text_spans),
replacement_range.IsValid()
? WebRange(replacement_range.start(), replacement_range.length())
: WebRange(),
selection_start, selection_end)) {
// If we failed to set the composition text, then we need to let the browser
// process to cancel the input method's ongoing composition session, to make
// sure we are in a consistent state.
if (blink::mojom::WidgetInputHandlerHost* host =
widget_input_handler_manager_->GetWidgetInputHandlerHost()) {
host->ImeCancelComposition();
}
}
UpdateCompositionInfo();
}
void RenderWidget::OnImeCommitText(
const base::string16& text,
const std::vector<ui::ImeTextSpan>& ime_text_spans,
const gfx::Range& replacement_range,
int relative_cursor_pos) {
if (!ShouldHandleImeEvents())
return;
#if BUILDFLAG(ENABLE_PLUGINS)
if (auto* plugin = GetFocusedPepperPluginInsideWidget()) {
plugin->render_frame()->OnImeCommitText(text, replacement_range,
relative_cursor_pos);
return;
}
#endif
ImeEventGuard guard(weak_ptr_factory_.GetWeakPtr());
GetWebWidget()->SetHandlingInputEvent(true);
if (auto* controller = GetInputMethodController()) {
controller->CommitText(
WebString::FromUTF16(text), WebVector<ui::ImeTextSpan>(ime_text_spans),
replacement_range.IsValid()
? WebRange(replacement_range.start(), replacement_range.length())
: WebRange(),
relative_cursor_pos);
}
GetWebWidget()->SetHandlingInputEvent(false);
UpdateCompositionInfo();
}
void RenderWidget::OnImeFinishComposingText(bool keep_selection) {
if (!ShouldHandleImeEvents())
return;
#if BUILDFLAG(ENABLE_PLUGINS)
if (auto* plugin = GetFocusedPepperPluginInsideWidget()) {
plugin->render_frame()->OnImeFinishComposingText(keep_selection);
return;
}
#endif
ImeEventGuard guard(weak_ptr_factory_.GetWeakPtr());
GetWebWidget()->SetHandlingInputEvent(true);
if (auto* controller = GetInputMethodController()) {
controller->FinishComposingText(
keep_selection ? WebInputMethodController::kKeepSelection
: WebInputMethodController::kDoNotKeepSelection);
}
GetWebWidget()->SetHandlingInputEvent(false);
UpdateCompositionInfo();
}
void RenderWidget::OnRequestTextInputStateUpdate() {
GetWebWidget()->ForceTextInputStateUpdate();
}
void RenderWidget::OnRequestCompositionUpdates(bool immediate_request,
bool monitor_updates) {
GetWebWidget()->RequestCompositionUpdates(immediate_request, monitor_updates);
}
void RenderWidget::UpdateSurfaceAndScreenInfo(
const viz::LocalSurfaceIdAllocation& new_local_surface_id_allocation,
const gfx::Rect& compositor_viewport_pixel_rect,
const ScreenInfo& new_screen_info) {
// Same logic is used in RenderWidgetHostImpl::SynchronizeVisualProperties to
// detect if there is a screen orientation change.
bool orientation_changed =
screen_info_.orientation_angle != new_screen_info.orientation_angle ||
screen_info_.orientation_type != new_screen_info.orientation_type;
ScreenInfo previous_original_screen_info = GetOriginalScreenInfo();
local_surface_id_allocation_from_parent_ = new_local_surface_id_allocation;
screen_info_ = new_screen_info;
// Note carefully that the DSF specified in |new_screen_info| is not the
// DSF used by the compositor during device emulation!
layer_tree_host_->SetViewportRectAndScale(
compositor_viewport_pixel_rect,
GetOriginalScreenInfo().device_scale_factor,
local_surface_id_allocation_from_parent_);
// The ViewportVisibleRect derives from the LayerTreeView's viewport size,
// which is set above.
layer_tree_host_->SetViewportVisibleRect(ViewportVisibleRect());
layer_tree_host_->SetRasterColorSpace(
screen_info_.color_space.GetRasterColorSpace());
if (orientation_changed)
OnOrientationChange();
if (for_frame()) {
RenderFrameImpl* render_frame =
RenderFrameImpl::FromWebFrame(GetFrameWidget()->LocalRoot());
// TODO(danakj): RenderWidget knows the DSF and could avoid calling into
// blink when it hasn't changed, but it sets an initial |screen_info_|
// during construction, so it is hard to tell if the value is not the
// default value once we get to OnSynchronizeVisualProperties. Thus we
// call into blink unconditionally and let it early out if it's already
// set.
render_frame->SetDeviceScaleFactorOnRenderView(
compositor_deps_->IsUseZoomForDSFEnabled(),
screen_info_.device_scale_factor);
// When the device scale changes, the size and position of the popup would
// need to be adjusted, which we can't do. Just close the popup, which is
// also consistent with page zoom and resize behavior.
if (previous_original_screen_info.device_scale_factor !=
screen_info_.device_scale_factor) {
blink::WebView* web_view = GetFrameWidget()->LocalRoot()->View();
web_view->CancelPagePopup();
}
}
// Propagate changes down to child local root RenderWidgets and BrowserPlugins
// in other frame trees/processes.
if (previous_original_screen_info != GetOriginalScreenInfo()) {
for (auto& observer : render_frame_proxies_)
observer.OnScreenInfoChanged(GetOriginalScreenInfo());
}
}
void RenderWidget::SetWindowRectSynchronously(
const gfx::Rect& new_window_rect) {
// This method is only call in tests, and it applies the |new_window_rect| to
// all three of:
// a) widget size (in |size_|)
// b) blink viewport (in |visible_viewport_size_|)
// c) compositor viewport (in cc::LayerTreeHost)
// Normally the browser controls these three things independently, but this is
// used in tests to control the size from the renderer.
// We are resizing the window from the renderer, so allocate a new
// viz::LocalSurfaceId to avoid surface invariants violations in tests.
layer_tree_host_->RequestNewLocalSurfaceId();
gfx::Rect compositor_viewport_pixel_rect(gfx::ScaleToCeiledSize(
new_window_rect.size(), screen_info_.device_scale_factor));
UpdateSurfaceAndScreenInfo(local_surface_id_allocation_from_parent_,
compositor_viewport_pixel_rect, screen_info_);
visible_viewport_size_ = new_window_rect.size();
size_ = new_window_rect.size();
ResizeWebWidget();
widget_screen_rect_ = new_window_rect;
window_screen_rect_ = new_window_rect;
if (show_callback_) {
// Tests may call here directly to control the window rect. If
// Show() did not happen yet, the rect is stored to be passed to the
// browser when the RenderWidget requests Show().
initial_rect_ = new_window_rect;
}
}
void RenderWidget::OnUpdateScreenRects(const gfx::Rect& widget_screen_rect,
const gfx::Rect& window_screen_rect) {
if (device_emulator_) {
device_emulator_->OnUpdateScreenRects(widget_screen_rect,
window_screen_rect);
} else {
SetScreenRects(widget_screen_rect, window_screen_rect);
}
Send(new WidgetHostMsg_UpdateScreenRects_ACK(routing_id()));
}
void RenderWidget::OnSetViewportIntersection(
const blink::ViewportIntersectionState& intersection_state) {
if (auto* frame_widget = GetFrameWidget()) {
compositor_visible_rect_ = intersection_state.compositor_visible_rect;
frame_widget->SetRemoteViewportIntersection(intersection_state);
layer_tree_host_->SetViewportVisibleRect(ViewportVisibleRect());
}
}
void RenderWidget::OnDragTargetDragEnter(
const std::vector<DropData::Metadata>& drop_meta_data,
const gfx::PointF& client_point,
const gfx::PointF& screen_point,
WebDragOperationsMask ops,
int key_modifiers) {
blink::WebFrameWidget* frame_widget = GetFrameWidget();
if (!frame_widget)
return;
WebDragOperation operation = frame_widget->DragTargetDragEnter(
DropMetaDataToWebDragData(drop_meta_data), client_point, screen_point,
ops, key_modifiers);
Send(new DragHostMsg_UpdateDragCursor(routing_id(), operation));
}
void RenderWidget::OnDragTargetDrop(const DropData& drop_data,
const gfx::PointF& client_point,
const gfx::PointF& screen_point,
int key_modifiers) {
blink::WebFrameWidget* frame_widget = GetFrameWidget();
if (!frame_widget)
return;
frame_widget->DragTargetDrop(DropDataToWebDragData(drop_data),
ConvertWindowPointToViewport(client_point),
screen_point, key_modifiers);
}
void RenderWidget::ConvertViewportToWindow(blink::WebRect* rect) {
if (compositor_deps_->IsUseZoomForDSFEnabled()) {
float reverse = 1 / GetOriginalScreenInfo().device_scale_factor;
// TODO(oshima): We may need to allow pixel precision here as the the
// anchor element can be placed at half pixel.
gfx::Rect window_rect = gfx::ScaleToEnclosedRect(gfx::Rect(*rect), reverse);
rect->x = window_rect.x();
rect->y = window_rect.y();
rect->width = window_rect.width();
rect->height = window_rect.height();
}
}
void RenderWidget::ConvertViewportToWindow(blink::WebFloatRect* rect) {
if (compositor_deps_->IsUseZoomForDSFEnabled()) {
rect->x /= GetOriginalScreenInfo().device_scale_factor;
rect->y /= GetOriginalScreenInfo().device_scale_factor;
rect->width /= GetOriginalScreenInfo().device_scale_factor;
rect->height /= GetOriginalScreenInfo().device_scale_factor;
}
}
void RenderWidget::ConvertWindowToViewport(blink::WebFloatRect* rect) {
if (compositor_deps_->IsUseZoomForDSFEnabled()) {
rect->x *= GetOriginalScreenInfo().device_scale_factor;
rect->y *= GetOriginalScreenInfo().device_scale_factor;
rect->width *= GetOriginalScreenInfo().device_scale_factor;
rect->height *= GetOriginalScreenInfo().device_scale_factor;
}
}
void RenderWidget::OnOrientationChange() {
if (auto* frame_widget = GetFrameWidget()) {
// LocalRoot() might return null for provisional main frames. In this case,
// the frame hasn't committed a navigation and is not swapped into the tree
// yet, so it doesn't make sense to send orientation change events to it.
//
// TODO(https://crbug.com/578349): This check should be cleaned up
// once provisional frames are gone.
if (frame_widget->LocalRoot())
frame_widget->LocalRoot()->SendOrientationChangeEvent();
}
}
void RenderWidget::SetHidden(bool hidden) {
// A provisional frame widget will never be shown or hidden, as the frame must
// be attached to the frame tree before changing visibility.
DCHECK(!IsForProvisionalFrame());
if (is_hidden_ == hidden)
return;
// The status has changed. Tell the RenderThread about it and ensure
// throttled acks are released in case frame production ceases.
is_hidden_ = hidden;
if (auto* scheduler_state = GetWebWidget()->RendererWidgetSchedulingState())
scheduler_state->SetHidden(hidden);
// If the renderer was hidden, resolve any pending synthetic gestures so they
// aren't blocked waiting for a compositor frame to be generated.
if (is_hidden_)
widget_input_handler_manager_->InvokeInputProcessedCallback();
if (!never_composited_)
webwidget_->SetCompositorVisible(!is_hidden_);
}
void RenderWidget::OnImeEventGuardStart(ImeEventGuard* guard) {
if (!ime_event_guard_)
ime_event_guard_ = guard;
}
void RenderWidget::OnImeEventGuardFinish(ImeEventGuard* guard) {
if (ime_event_guard_ != guard)
return;
ime_event_guard_ = nullptr;
// While handling an ime event, text input state and selection bounds updates
// are ignored. These must explicitly be updated once finished handling the
// ime event.
UpdateSelectionBounds();
#if defined(OS_ANDROID)
if (guard->show_virtual_keyboard())
GetWebWidget()->ShowVirtualKeyboard();
else
UpdateTextInputState();
#endif
}
void RenderWidget::UpdateSelectionBounds() {
GetWebWidget()->UpdateSelectionBounds();
}
void RenderWidget::UpdateCompositionInfo() {
GetWebWidget()->UpdateCompositionInfo();
}
void RenderWidget::DidAutoResize(const gfx::Size& new_size) {
WebRect new_size_in_window(0, 0, new_size.width(), new_size.height());
ConvertViewportToWindow(&new_size_in_window);
if (size_.width() != new_size_in_window.width ||
size_.height() != new_size_in_window.height) {
size_ = gfx::Size(new_size_in_window.width, new_size_in_window.height);
if (synchronous_resize_mode_for_testing_) {
gfx::Rect new_pos(WindowRect().x, WindowRect().y, size_.width(),
size_.height());
widget_screen_rect_ = new_pos;
window_screen_rect_ = new_pos;
}
// TODO(ccameron): Note that this destroys any information differentiating
// |size_| from the compositor's viewport size. Also note that the
// calculation of |new_compositor_viewport_pixel_rect| does not appear to
// take into account device emulation.
layer_tree_host_->RequestNewLocalSurfaceId();
gfx::Rect new_compositor_viewport_pixel_rect = gfx::Rect(
gfx::ScaleToCeiledSize(size_, screen_info_.device_scale_factor));
UpdateSurfaceAndScreenInfo(local_surface_id_allocation_from_parent_,
new_compositor_viewport_pixel_rect,
screen_info_);
}
}
// static
cc::LayerTreeSettings RenderWidget::GenerateLayerTreeSettings(
CompositorDependencies* compositor_deps,
bool is_for_subframe,
const gfx::Size& initial_screen_size,
float initial_device_scale_factor) {
const bool is_threaded = !compositor_deps->IsSingleThreaded();
const base::CommandLine& cmd = *base::CommandLine::ForCurrentProcess();
cc::LayerTreeSettings settings;
settings.enable_synchronized_scrolling =
base::FeatureList::IsEnabled(features::kSynchronizedScrolling);
settings.use_zoom_for_dsf = compositor_deps->IsUseZoomForDSFEnabled();
settings.percent_based_scrolling =
base::FeatureList::IsEnabled(features::kPercentBasedScrolling);
settings.compositor_threaded_scrollbar_scrolling =
base::FeatureList::IsEnabled(
features::kCompositorThreadedScrollbarScrolling);
settings.resource_settings.use_r16_texture =
base::FeatureList::IsEnabled(media::kUseR16Texture);
settings.commit_to_active_tree = !is_threaded;
settings.is_layer_tree_for_subframe = is_for_subframe;
settings.main_frame_before_activation_enabled =
cmd.HasSwitch(cc::switches::kEnableMainFrameBeforeActivation);
// Checkerimaging is not supported for synchronous single-threaded mode, which
// is what the renderer uses if its not threaded.
settings.enable_checker_imaging =
!cmd.HasSwitch(cc::switches::kDisableCheckerImaging) && is_threaded;
#if defined(OS_ANDROID)
// WebView should always raster in the default color space.
// Synchronous compositing indicates WebView.
if (!compositor_deps->UsingSynchronousCompositing())
settings.prefer_raster_in_srgb = features::IsDynamicColorGamutEnabled();
// We can use a more aggressive limit on Android since decodes tend to take
// longer on these devices.
settings.min_image_bytes_to_checker = 512 * 1024; // 512kB
// Re-rasterization of checker-imaged content with software raster can be too
// costly on Android.
settings.only_checker_images_with_gpu_raster = true;
#endif
auto switch_value_as_int = [](const base::CommandLine& command_line,
const std::string& switch_string, int min_value,
int max_value, int* result) {
std::string string_value = command_line.GetSwitchValueASCII(switch_string);
int int_value;
if (base::StringToInt(string_value, &int_value) && int_value >= min_value &&
int_value <= max_value) {
*result = int_value;
return true;
} else {
DLOG(WARNING) << "Failed to parse switch " << switch_string << ": "
<< string_value;
return false;
}
};
int default_tile_size = 256;
#if defined(OS_ANDROID)
const gfx::Size screen_size =
gfx::ScaleToFlooredSize(initial_screen_size, initial_device_scale_factor);
int display_width = screen_size.width();
int display_height = screen_size.height();
int numTiles = (display_width * display_height) / (256 * 256);
if (numTiles > 16)
default_tile_size = 384;
if (numTiles >= 40)
default_tile_size = 512;
// Adjust for some resolutions that barely straddle an extra
// tile when in portrait mode. This helps worst case scroll/raster
// by not needing a full extra tile for each row.
constexpr int tolerance = 10; // To avoid rounding errors.
int portrait_width = std::min(display_width, display_height);
if (default_tile_size == 256 && std::abs(portrait_width - 768) < tolerance)
default_tile_size += 32;
if (default_tile_size == 384 && std::abs(portrait_width - 1200) < tolerance)
default_tile_size += 32;
#elif defined(OS_CHROMEOS) || defined(OS_MACOSX)
// Use 512 for high DPI (dsf=2.0f) devices.
if (initial_device_scale_factor >= 2.0f)
default_tile_size = 512;
#endif
// TODO(danakj): This should not be a setting O_O; it should change when the
// device scale factor on LayerTreeHost changes.
settings.default_tile_size = gfx::Size(default_tile_size, default_tile_size);
if (cmd.HasSwitch(blink::switches::kDefaultTileWidth)) {
int tile_width = 0;
switch_value_as_int(cmd, blink::switches::kDefaultTileWidth, 1,
std::numeric_limits<int>::max(), &tile_width);
settings.default_tile_size.set_width(tile_width);
}
if (cmd.HasSwitch(blink::switches::kDefaultTileHeight)) {
int tile_height = 0;
switch_value_as_int(cmd, blink::switches::kDefaultTileHeight, 1,
std::numeric_limits<int>::max(), &tile_height);
settings.default_tile_size.set_height(tile_height);
}
if (cmd.HasSwitch(blink::switches::kMinHeightForGpuRasterTile)) {
int min_height_for_gpu_raster_tile = 0;
switch_value_as_int(cmd, blink::switches::kMinHeightForGpuRasterTile, 1,
std::numeric_limits<int>::max(),
&min_height_for_gpu_raster_tile);
settings.min_height_for_gpu_raster_tile = min_height_for_gpu_raster_tile;
}
int max_untiled_layer_width = settings.max_untiled_layer_size.width();
if (cmd.HasSwitch(blink::switches::kMaxUntiledLayerWidth)) {
switch_value_as_int(cmd, blink::switches::kMaxUntiledLayerWidth, 1,
std::numeric_limits<int>::max(),
&max_untiled_layer_width);
}
int max_untiled_layer_height = settings.max_untiled_layer_size.height();
if (cmd.HasSwitch(blink::switches::kMaxUntiledLayerHeight)) {
switch_value_as_int(cmd, blink::switches::kMaxUntiledLayerHeight, 1,
std::numeric_limits<int>::max(),
&max_untiled_layer_height);
}
settings.max_untiled_layer_size =
gfx::Size(max_untiled_layer_width, max_untiled_layer_height);
settings.gpu_rasterization_msaa_sample_count =
compositor_deps->GetGpuRasterizationMSAASampleCount();
settings.can_use_lcd_text = compositor_deps->IsLcdTextEnabled();
settings.use_zero_copy = compositor_deps->IsZeroCopyEnabled();
settings.use_partial_raster = compositor_deps->IsPartialRasterEnabled();
settings.enable_elastic_overscroll =
compositor_deps->IsElasticOverscrollEnabled();
settings.resource_settings.use_gpu_memory_buffer_resources =
compositor_deps->IsGpuMemoryBufferCompositorResourcesEnabled();
settings.use_painted_device_scale_factor =
compositor_deps->IsUseZoomForDSFEnabled();
// Build LayerTreeSettings from command line args.
if (cmd.HasSwitch(cc::switches::kBrowserControlsShowThreshold)) {
std::string top_threshold_str =
cmd.GetSwitchValueASCII(cc::switches::kBrowserControlsShowThreshold);
double show_threshold;
if (base::StringToDouble(top_threshold_str, &show_threshold) &&
show_threshold >= 0.f && show_threshold <= 1.f)
settings.top_controls_show_threshold = show_threshold;
}
if (cmd.HasSwitch(cc::switches::kBrowserControlsHideThreshold)) {
std::string top_threshold_str =
cmd.GetSwitchValueASCII(cc::switches::kBrowserControlsHideThreshold);
double hide_threshold;
if (base::StringToDouble(top_threshold_str, &hide_threshold) &&
hide_threshold >= 0.f && hide_threshold <= 1.f)
settings.top_controls_hide_threshold = hide_threshold;
}
// Blink sends cc a layer list and property trees.
settings.use_layer_lists = true;
// Blink currently doesn't support setting fractional scroll offsets so CC
// must send integer values. We plan to eventually make Blink use fractional
// offsets internally: https://crbug.com/414283.
settings.commit_fractional_scroll_deltas =
blink::WebRuntimeFeatures::IsFractionalScrollOffsetsEnabled();
settings.enable_smooth_scroll = compositor_deps->IsScrollAnimatorEnabled();
// The means the renderer compositor has 2 possible modes:
// - Threaded compositing with a scheduler.
// - Single threaded compositing without a scheduler (for web tests only).
// Using the scheduler in web tests introduces additional composite steps
// that create flakiness.
settings.single_thread_proxy_scheduler = false;
// These flags should be mirrored by UI versions in ui/compositor/.
if (cmd.HasSwitch(cc::switches::kShowCompositedLayerBorders))
settings.initial_debug_state.show_debug_borders.set();
settings.initial_debug_state.show_fps_counter =
cmd.HasSwitch(cc::switches::kShowFPSCounter);
settings.initial_debug_state.show_layer_animation_bounds_rects =
cmd.HasSwitch(cc::switches::kShowLayerAnimationBounds);
settings.initial_debug_state.show_paint_rects =
cmd.HasSwitch(blink::switches::kShowPaintRects);
settings.initial_debug_state.show_layout_shift_regions =
cmd.HasSwitch(blink::switches::kShowLayoutShiftRegions);
settings.initial_debug_state.show_property_changed_rects =
cmd.HasSwitch(cc::switches::kShowPropertyChangedRects);
settings.initial_debug_state.show_surface_damage_rects =
cmd.HasSwitch(cc::switches::kShowSurfaceDamageRects);
settings.initial_debug_state.show_screen_space_rects =
cmd.HasSwitch(cc::switches::kShowScreenSpaceRects);
settings.initial_debug_state.highlight_non_lcd_text_layers =
cmd.HasSwitch(cc::switches::kHighlightNonLCDTextLayers);
settings.initial_debug_state.SetRecordRenderingStats(
cmd.HasSwitch(cc::switches::kEnableGpuBenchmarking));
if (cmd.HasSwitch(cc::switches::kSlowDownRasterScaleFactor)) {
const int kMinSlowDownScaleFactor = 0;
const int kMaxSlowDownScaleFactor = INT_MAX;
switch_value_as_int(
cmd, cc::switches::kSlowDownRasterScaleFactor, kMinSlowDownScaleFactor,
kMaxSlowDownScaleFactor,
&settings.initial_debug_state.slow_down_raster_scale_factor);
}
// This is default overlay scrollbar settings for Android and DevTools mobile
// emulator. Aura Overlay Scrollbar will override below.
settings.scrollbar_animator = cc::LayerTreeSettings::ANDROID_OVERLAY;
settings.solid_color_scrollbar_color = SkColorSetARGB(128, 128, 128, 128);
settings.scrollbar_fade_delay = base::TimeDelta::FromMilliseconds(300);
settings.scrollbar_fade_duration = base::TimeDelta::FromMilliseconds(300);
if (cmd.HasSwitch(cc::switches::kCCScrollAnimationDurationForTesting)) {
const int kMinScrollAnimationDuration = 0;
const int kMaxScrollAnimationDuration = INT_MAX;
int duration;
if (switch_value_as_int(cmd,
cc::switches::kCCScrollAnimationDurationForTesting,
kMinScrollAnimationDuration,
kMaxScrollAnimationDuration, &duration)) {
settings.scroll_animation_duration_for_testing =
base::TimeDelta::FromSeconds(duration);
}
}
#if defined(OS_ANDROID)
bool using_synchronous_compositor =
compositor_deps->UsingSynchronousCompositing();
bool using_low_memory_policy =
base::SysInfo::IsLowEndDevice() && !IsSmallScreen(screen_size);
settings.use_stream_video_draw_quad = true;
settings.using_synchronous_renderer_compositor = using_synchronous_compositor;
if (using_synchronous_compositor) {
// Android WebView uses system scrollbars, so make ours invisible.
// http://crbug.com/677348: This can't be done using hide_scrollbars
// setting because supporting -webkit custom scrollbars is still desired
// on sublayers.
settings.scrollbar_animator = cc::LayerTreeSettings::NO_ANIMATOR;
settings.solid_color_scrollbar_color = SK_ColorTRANSPARENT;
settings.enable_early_damage_check =
cmd.HasSwitch(cc::switches::kCheckDamageEarly);
}
// Android WebView handles root layer flings itself.
settings.ignore_root_layer_flings = using_synchronous_compositor;
// Memory policy on Android WebView does not depend on whether device is
// low end, so always use default policy.
if (using_low_memory_policy && !using_synchronous_compositor) {
// On low-end we want to be very carefull about killing other
// apps. So initially we use 50% more memory to avoid flickering
// or raster-on-demand.
settings.max_memory_for_prepaint_percentage = 67;
} else {
// On other devices we have increased memory excessively to avoid
// raster-on-demand already, so now we reserve 50% _only_ to avoid
// raster-on-demand, and use 50% of the memory otherwise.
settings.max_memory_for_prepaint_percentage = 50;
}
// TODO(danakj): Only do this on low end devices.
settings.create_low_res_tiling = true;
#else // defined(OS_ANDROID)
bool using_synchronous_compositor = false; // Only for Android WebView.
// On desktop, we never use the low memory policy unless we are simulating
// low-end mode via a switch.
bool using_low_memory_policy =
cmd.HasSwitch(switches::kEnableLowEndDeviceMode);
if (ui::IsOverlayScrollbarEnabled()) {
settings.scrollbar_animator = cc::LayerTreeSettings::AURA_OVERLAY;
settings.scrollbar_fade_delay = ui::kOverlayScrollbarFadeDelay;
settings.scrollbar_fade_duration = ui::kOverlayScrollbarFadeDuration;
settings.scrollbar_thinning_duration =
ui::kOverlayScrollbarThinningDuration;
settings.scrollbar_flash_after_any_scroll_update =
ui::OverlayScrollbarFlashAfterAnyScrollUpdate();
settings.scrollbar_flash_when_mouse_enter =
ui::OverlayScrollbarFlashWhenMouseEnter();
}
// On desktop, if there's over 4GB of memory on the machine, increase the
// working set size to 256MB for both gpu and software.
const int kImageDecodeMemoryThresholdMB = 4 * 1024;
if (base::SysInfo::AmountOfPhysicalMemoryMB() >=
kImageDecodeMemoryThresholdMB) {
settings.decoded_image_working_set_budget_bytes = 256 * 1024 * 1024;
} else {
// This is the default, but recorded here as well.
settings.decoded_image_working_set_budget_bytes = 128 * 1024 * 1024;
}
#endif // defined(OS_ANDROID)
if (using_low_memory_policy) {
// RGBA_4444 textures are only enabled:
// - If the user hasn't explicitly disabled them
// - If system ram is <= 512MB (1GB devices are sometimes low-end).
// - If we are not running in a WebView, where 4444 isn't supported.
// - If we are not using vulkan, since some GPU drivers don't support
// using RGBA4444 as color buffer.
// TODO(penghuang): query supported formats from GPU process.
if (!cmd.HasSwitch(blink::switches::kDisableRGBA4444Textures) &&
base::SysInfo::AmountOfPhysicalMemoryMB() <= 512 &&
!using_synchronous_compositor &&
!base::FeatureList::IsEnabled(features::kVulkan)) {
settings.use_rgba_4444 = viz::RGBA_4444;
// If we are going to unpremultiply and dither these tiles, we need to
// allocate an additional RGBA_8888 intermediate for each tile
// rasterization when rastering to RGBA_4444 to allow for dithering.
// Setting a reasonable sized max tile size allows this intermediate to
// be consistently reused.
if (base::FeatureList::IsEnabled(
kUnpremultiplyAndDitherLowBitDepthTiles)) {
settings.max_gpu_raster_tile_size = gfx::Size(512, 256);
settings.unpremultiply_and_dither_low_bit_depth_tiles = true;
}
}
}
if (cmd.HasSwitch(blink::switches::kEnableLowResTiling))
settings.create_low_res_tiling = true;
if (cmd.HasSwitch(blink::switches::kDisableLowResTiling))
settings.create_low_res_tiling = false;
if (cmd.HasSwitch(blink::switches::kEnableRGBA4444Textures) &&
!cmd.HasSwitch(blink::switches::kDisableRGBA4444Textures)) {
settings.use_rgba_4444 = true;
}
settings.max_staging_buffer_usage_in_bytes = 32 * 1024 * 1024; // 32MB
// Use 1/4th of staging buffers on low-end devices.
if (base::SysInfo::IsLowEndDevice())
settings.max_staging_buffer_usage_in_bytes /= 4;
cc::ManagedMemoryPolicy defaults = settings.memory_policy;
settings.memory_policy = GetGpuMemoryPolicy(defaults, initial_screen_size,
initial_device_scale_factor);
settings.disallow_non_exact_resource_reuse =
cmd.HasSwitch(switches::kDisallowNonExactResourceReuse);
#if defined(OS_ANDROID)
// TODO(crbug.com/746931): This feature appears to be causing visual
// corruption on certain android devices. Will investigate and re-enable.
settings.disallow_non_exact_resource_reuse = true;
#endif
settings.enable_impl_latency_recovery =
features::IsImplLatencyRecoveryEnabled();
settings.enable_main_latency_recovery =
features::IsMainLatencyRecoveryEnabled();
if (cmd.HasSwitch(switches::kRunAllCompositorStagesBeforeDraw)) {
settings.wait_for_all_pipeline_stages_before_draw = true;
settings.enable_impl_latency_recovery = false;
settings.enable_main_latency_recovery = false;
}
settings.enable_image_animation_resync =
!cmd.HasSwitch(blink::switches::kDisableImageAnimationResync);
settings.send_compositor_frame_ack = false;
// Renderer can de-jelly, browser UI can not. We do not know whether we are
// going to apply de-jelly until we draw a frame in the Viz process. Because
// of this, all changes in the renderer are based on whether de-jelly may be
// active (viz::DeJellyEnabled) vs whether it is currently active
// (viz::DeJellyActive).
settings.allow_de_jelly_effect = viz::DeJellyEnabled();
// Disable occlusion if de-jelly effect is enabled.
settings.enable_occlusion &= !settings.allow_de_jelly_effect;
settings.enable_transform_interop =
base::FeatureList::IsEnabled(blink::features::kTransformInterop);
return settings;
}
// static
cc::ManagedMemoryPolicy RenderWidget::GetGpuMemoryPolicy(
const cc::ManagedMemoryPolicy& default_policy,
const gfx::Size& initial_screen_size,
float initial_device_scale_factor) {
cc::ManagedMemoryPolicy actual = default_policy;
actual.bytes_limit_when_visible = 0;
// If the value was overridden on the command line, use the specified value.
static bool client_hard_limit_bytes_overridden =
base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kForceGpuMemAvailableMb);
if (client_hard_limit_bytes_overridden) {
if (base::StringToSizeT(
base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
switches::kForceGpuMemAvailableMb),
&actual.bytes_limit_when_visible))
actual.bytes_limit_when_visible *= 1024 * 1024;
return actual;
}
#if defined(OS_ANDROID)
// We can't query available GPU memory from the system on Android.
// Physical memory is also mis-reported sometimes (eg. Nexus 10 reports
// 1262MB when it actually has 2GB, while Razr M has 1GB but only reports
// 128MB java heap size). First we estimate physical memory using both.
size_t dalvik_mb = base::SysInfo::DalvikHeapSizeMB();
size_t physical_mb = base::SysInfo::AmountOfPhysicalMemoryMB();
size_t physical_memory_mb = 0;
if (base::SysInfo::IsLowEndDevice()) {
// TODO(crbug.com/742534): The code below appears to no longer work.
// |dalvik_mb| no longer follows the expected heuristic pattern, causing us
// to over-estimate memory on low-end devices. This entire section probably
// needs to be re-written, but for now we can address the low-end Android
// issues by ignoring |dalvik_mb|.
physical_memory_mb = physical_mb;
} else if (dalvik_mb >= 256) {
physical_memory_mb = dalvik_mb * 4;
} else {
physical_memory_mb = std::max(dalvik_mb * 4, (physical_mb * 4) / 3);
}
// Now we take a default of 1/8th of memory on high-memory devices,
// and gradually scale that back for low-memory devices (to be nicer
// to other apps so they don't get killed). Examples:
// Nexus 4/10(2GB) 256MB (normally 128MB)
// Droid Razr M(1GB) 114MB (normally 57MB)
// Galaxy Nexus(1GB) 100MB (normally 50MB)
// Xoom(1GB) 100MB (normally 50MB)
// Nexus S(low-end) 8MB (normally 8MB)
// Note that the compositor now uses only some of this memory for
// pre-painting and uses the rest only for 'emergencies'.
if (actual.bytes_limit_when_visible == 0) {
// NOTE: Non-low-end devices use only 50% of these limits,
// except during 'emergencies' where 100% can be used.
if (physical_memory_mb >= 1536)
actual.bytes_limit_when_visible = physical_memory_mb / 8; // >192MB
else if (physical_memory_mb >= 1152)
actual.bytes_limit_when_visible = physical_memory_mb / 8; // >144MB
else if (physical_memory_mb >= 768)
actual.bytes_limit_when_visible = physical_memory_mb / 10; // >76MB
else if (physical_memory_mb >= 513)
actual.bytes_limit_when_visible = physical_memory_mb / 12; // <64MB
else
// Devices with this little RAM have very little headroom so we hardcode
// the limit rather than relying on the heuristics above. (They also use
// 4444 textures so we can use a lower limit.)
actual.bytes_limit_when_visible = 8;
actual.bytes_limit_when_visible =
actual.bytes_limit_when_visible * 1024 * 1024;
// Clamp the observed value to a specific range on Android.
actual.bytes_limit_when_visible = std::max(
actual.bytes_limit_when_visible, static_cast<size_t>(8 * 1024 * 1024));
actual.bytes_limit_when_visible =
std::min(actual.bytes_limit_when_visible,
static_cast<size_t>(256 * 1024 * 1024));
}
actual.priority_cutoff_when_visible =
gpu::MemoryAllocation::CUTOFF_ALLOW_EVERYTHING;
#else
// Ignore what the system said and give all clients the same maximum
// allocation on desktop platforms.
actual.bytes_limit_when_visible = 512 * 1024 * 1024;
actual.priority_cutoff_when_visible =
gpu::MemoryAllocation::CUTOFF_ALLOW_NICE_TO_HAVE;
// For large monitors (4k), double the tile memory to avoid frequent out of
// memory problems. 4k could mean a screen width of anywhere from 3840 to 4096
// (see https://en.wikipedia.org/wiki/4K_resolution). We use 3500 as a proxy
// for "large enough".
static const int kLargeDisplayThreshold = 3500;
int display_width =
std::round(initial_screen_size.width() * initial_device_scale_factor);
if (display_width >= kLargeDisplayThreshold)
actual.bytes_limit_when_visible *= 2;
#endif
return actual;
}
void RenderWidget::SetHasPointerRawUpdateEventHandlers(bool has_handlers) {
input_event_queue_->HasPointerRawUpdateEventHandlers(has_handlers);
}
void RenderWidget::SetNeedsLowLatencyInput(bool needs_low_latency) {
input_event_queue_->SetNeedsLowLatency(needs_low_latency);
}
void RenderWidget::SetNeedsUnbufferedInputForDebugger(bool unbuffered) {
input_event_queue_->SetNeedsUnbufferedInputForDebugger(unbuffered);
}
void RenderWidget::SetPageScaleStateAndLimits(float page_scale_factor,
bool is_pinch_gesture_active,
float minimum,
float maximum) {
layer_tree_host_->SetPageScaleFactorAndLimits(page_scale_factor, minimum,
maximum);
// Only continue if this is a mainframe, or something's actually changed.
if (!delegate() ||
(page_scale_factor == page_scale_factor_from_mainframe_ &&
is_pinch_gesture_active == is_pinch_gesture_active_from_mainframe_)) {
return;
}
DCHECK(!IsForProvisionalFrame());
// The page scale is controlled by the WebView for the local main frame of
// the Page. So this is called from blink for the RenderWidget of that
// local main frame. We forward the value on to each child RenderWidget (each
// of which will be via proxy child frame). These will each in turn forward
// the message to their child RenderWidgets (through their proxy child
// frames).
for (auto& observer : render_frame_proxies_) {
observer.OnPageScaleFactorChanged(page_scale_factor,
is_pinch_gesture_active);
}
// Store the value to give to any new RenderFrameProxy that is registered.
page_scale_factor_from_mainframe_ = page_scale_factor;
is_pinch_gesture_active_from_mainframe_ = is_pinch_gesture_active;
}
void RenderWidget::RequestDecode(const cc::PaintImage& image,
base::OnceCallback<void(bool)> callback) {
layer_tree_host_->QueueImageDecode(image, std::move(callback));
}
viz::FrameSinkId RenderWidget::GetFrameSinkId() {
return viz::FrameSinkId(RenderThread::Get()->GetClientId(), routing_id());
}
void RenderWidget::RequestUnbufferedInputEvents() {
input_event_queue_->RequestUnbufferedInputEvents();
}
void RenderWidget::SetTouchAction(cc::TouchAction touch_action) {
widget_input_handler_manager_->ProcessTouchAction(touch_action);
}
void RenderWidget::RegisterRenderFrameProxy(RenderFrameProxy* proxy) {
render_frame_proxies_.AddObserver(proxy);
// These properties are propagated down the RenderWidget tree through
// the RenderFrameProxy (see explanation in OnUpdateVisualProperties()).
// When a new RenderFrameProxy is added, we propagate them immediately.
proxy->OnPageScaleFactorChanged(page_scale_factor_from_mainframe_,
is_pinch_gesture_active_from_mainframe_);
proxy->OnScreenInfoChanged(GetOriginalScreenInfo());
proxy->OnZoomLevelChanged(zoom_level_);
proxy->OnVisibleViewportSizeChanged(visible_viewport_size_);
proxy->OnRootWindowSegmentsChanged(root_widget_window_segments_);
}
void RenderWidget::UnregisterRenderFrameProxy(RenderFrameProxy* proxy) {
render_frame_proxies_.RemoveObserver(proxy);
}
void RenderWidget::RegisterRenderFrame(RenderFrameImpl* frame) {
render_frames_.AddObserver(frame);
}
void RenderWidget::UnregisterRenderFrame(RenderFrameImpl* frame) {
render_frames_.RemoveObserver(frame);
}
void RenderWidget::OnWaitNextFrameForTests(
int main_frame_thread_observer_routing_id) {
// Sends an ACK to the browser process during the next compositor frame.
QueueMessage(std::make_unique<WidgetHostMsg_WaitForNextFrameForTests_ACK>(
main_frame_thread_observer_routing_id));
}
const ScreenInfo& RenderWidget::GetOriginalScreenInfo() const {
if (device_emulator_)
return device_emulator_->original_screen_info();
return screen_info_;
}
gfx::PointF RenderWidget::ConvertWindowPointToViewport(
const gfx::PointF& point) {
blink::WebFloatRect point_in_viewport(point.x(), point.y(), 0, 0);
ConvertWindowToViewport(&point_in_viewport);
return gfx::PointF(point_in_viewport.x, point_in_viewport.y);
}
gfx::Point RenderWidget::ConvertWindowPointToViewport(const gfx::Point& point) {
return gfx::ToRoundedPoint(ConvertWindowPointToViewport(gfx::PointF(point)));
}
bool RenderWidget::RequestPointerLock(
WebLocalFrame* requester_frame,
blink::WebWidgetClient::PointerLockCallback callback,
bool request_unadjusted_movement) {
return mouse_lock_dispatcher_->LockMouse(webwidget_mouse_lock_target_.get(),
requester_frame, std::move(callback),
request_unadjusted_movement);
}
bool RenderWidget::RequestPointerLockChange(
blink::WebLocalFrame* requester_frame,
blink::WebWidgetClient::PointerLockCallback callback,
bool request_unadjusted_movement) {
return mouse_lock_dispatcher_->ChangeMouseLock(
webwidget_mouse_lock_target_.get(), requester_frame, std::move(callback),
request_unadjusted_movement);
}
void RenderWidget::RequestPointerUnlock() {
mouse_lock_dispatcher_->UnlockMouse(webwidget_mouse_lock_target_.get());
}
bool RenderWidget::IsPointerLocked() {
return mouse_lock_dispatcher_->IsMouseLockedTo(
webwidget_mouse_lock_target_.get());
}
void RenderWidget::StartDragging(network::mojom::ReferrerPolicy policy,
const WebDragData& data,
WebDragOperationsMask mask,
const SkBitmap& drag_image,
const gfx::Point& web_image_offset) {
blink::WebRect offset_in_window(web_image_offset.x(), web_image_offset.y(), 0,
0);
ConvertViewportToWindow(&offset_in_window);
DropData drop_data(DropDataBuilder::Build(data));
drop_data.referrer_policy = policy;
gfx::Vector2d image_offset(offset_in_window.x, offset_in_window.y);
Send(new DragHostMsg_StartDragging(routing_id(), drop_data, mask, drag_image,
image_offset, possible_drag_event_info_));
}
void RenderWidget::DidNavigate(ukm::SourceId source_id, const GURL& url) {
// The input handler wants to know about navigation so that it can
// suppress input until the newly navigated page has a committed frame.
// It also resets the state for UMA reporting of input arrival with respect
// to document lifecycle.
DCHECK(widget_input_handler_manager_);
widget_input_handler_manager_->DidNavigate();
// Update the URL and the document source id used to key UKM metrics in the
// compositor. Note that the metrics for all frames are keyed to the main
// frame's URL.
layer_tree_host_->SetSourceURL(source_id, url);
}
blink::WebInputMethodController* RenderWidget::GetInputMethodController()
const {
if (auto* frame_widget = GetFrameWidget())
return frame_widget->GetActiveWebInputMethodController();
return nullptr;
}
blink::mojom::WidgetInputHandlerHost* RenderWidget::GetInputHandlerHost() {
return widget_input_handler_manager_->GetWidgetInputHandlerHost();
}
void RenderWidget::SetMouseCapture(bool capture) {
if (blink::mojom::WidgetInputHandlerHost* host =
widget_input_handler_manager_->GetWidgetInputHandlerHost()) {
host->SetMouseCapture(capture);
}
}
void RenderWidget::UseSynchronousResizeModeForTesting(bool enable) {
synchronous_resize_mode_for_testing_ = enable;
}
blink::WebHitTestResult RenderWidget::GetHitTestResultAtPoint(
const gfx::PointF& point) {
gfx::PointF point_in_pixel(point);
if (compositor_deps()->IsUseZoomForDSFEnabled()) {
point_in_pixel = gfx::ConvertPointToPixel(
GetOriginalScreenInfo().device_scale_factor, point_in_pixel);
}
return GetWebWidget()->HitTestResultAt(point_in_pixel);
}
void RenderWidget::SetDeviceScaleFactorForTesting(float factor) {
DCHECK_GE(factor, 0.f);
// Receiving a 0 is used to reset between tests, it removes the override in
// order to listen to the browser for the next test.
if (!factor) {
device_scale_factor_for_testing_ = 0;
return;
}
// We are changing the device scale factor from the renderer, so allocate a
// new viz::LocalSurfaceId to avoid surface invariants violations in tests.
layer_tree_host_->RequestNewLocalSurfaceId();
ScreenInfo info = screen_info_;
info.device_scale_factor = factor;
gfx::Size viewport_pixel_size = gfx::ScaleToCeiledSize(size_, factor);
UpdateSurfaceAndScreenInfo(local_surface_id_allocation_from_parent_,
gfx::Rect(viewport_pixel_size), info);
if (!auto_resize_mode_)
ResizeWebWidget(); // This picks up the new device scale factor in |info|.
RenderFrameImpl* render_frame =
RenderFrameImpl::FromWebFrame(GetFrameWidget()->LocalRoot());
render_frame->SetPreferCompositingToLCDTextEnabledOnRenderView(
ComputePreferCompositingToLCDText(compositor_deps_,
screen_info_.device_scale_factor));
// Make sure to override any future OnSynchronizeVisualProperties IPCs.
device_scale_factor_for_testing_ = factor;
}
void RenderWidget::SetZoomLevelForTesting(double zoom_level) {
DCHECK_NE(zoom_level, -INFINITY);
SetZoomLevel(zoom_level);
// Make sure to override any future OnSynchronizeVisualProperties IPCs.
zoom_level_for_testing_ = zoom_level;
}
void RenderWidget::ResetZoomLevelForTesting() {
zoom_level_for_testing_ = -INFINITY;
SetZoomLevel(0);
}
void RenderWidget::SetDeviceColorSpaceForTesting(
const gfx::ColorSpace& color_space) {
// We are changing the device color space from the renderer, so allocate a
// new viz::LocalSurfaceId to avoid surface invariants violations in tests.
layer_tree_host_->RequestNewLocalSurfaceId();
ScreenInfo info = screen_info_;
info.color_space = color_space;
UpdateSurfaceAndScreenInfo(local_surface_id_allocation_from_parent_,
CompositorViewportRect(), info);
}
void RenderWidget::SetWindowRectSynchronouslyForTesting(
const gfx::Rect& new_window_rect) {
SetWindowRectSynchronously(new_window_rect);
}
void RenderWidget::EnableAutoResizeForTesting(const gfx::Size& min_size,
const gfx::Size& max_size) {
SetAutoResizeMode(true, min_size, max_size, screen_info_.device_scale_factor);
}
void RenderWidget::DisableAutoResizeForTesting(const gfx::Size& new_size) {
if (!auto_resize_mode_)
return;
SetAutoResizeMode(false, gfx::Size(), gfx::Size(),
screen_info_.device_scale_factor);
// The |new_size| is empty when resetting auto resize in between tests. In
// this case the current size should just be preserved.
if (!new_size.IsEmpty()) {
size_ = new_size;
ResizeWebWidget();
}
}
blink::WebLocalFrame* RenderWidget::GetFocusedWebLocalFrameInWidget() const {
if (auto* frame_widget = GetFrameWidget())
return frame_widget->FocusedWebLocalFrameInWidget();
return nullptr;
}
#if BUILDFLAG(ENABLE_PLUGINS)
PepperPluginInstanceImpl* RenderWidget::GetFocusedPepperPluginInsideWidget() {
blink::WebFrameWidget* frame_widget = GetFrameWidget();
if (!frame_widget)
return nullptr;
// Focused pepper instance might not always be in the focused frame. For
// instance if a pepper instance and its embedder frame are focused an then
// another frame takes focus using javascript, the embedder frame will no
// longer be focused while the pepper instance is (the embedder frame's
// |focused_pepper_plugin_| is not nullptr). Especially, if the pepper plugin
// is fullscreen, clicking into the pepper will not refocus the embedder
// frame. This is why we have to traverse the whole frame tree to find the
// focused plugin.
blink::WebFrame* current_frame = frame_widget->LocalRoot();
while (current_frame) {
RenderFrameImpl* render_frame =
current_frame->IsWebLocalFrame()
? RenderFrameImpl::FromWebFrame(current_frame)
: nullptr;
if (render_frame && render_frame->focused_pepper_plugin())
return render_frame->focused_pepper_plugin();
current_frame = current_frame->TraverseNext();
}
return nullptr;
}
#endif
gfx::Rect RenderWidget::ViewportVisibleRect() {
if (for_child_local_root_frame_)
return compositor_visible_rect_;
return CompositorViewportRect();
}
base::WeakPtr<RenderWidget> RenderWidget::AsWeakPtr() {
return weak_ptr_factory_.GetWeakPtr();
}
} // namespace content
|