1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
|
// Copyright 2011 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 "cc/layer_tree_host_common.h"
#include "cc/content_layer.h"
#include "cc/content_layer_client.h"
#include "cc/layer.h"
#include "cc/layer_animation_controller.h"
#include "cc/layer_impl.h"
#include "cc/layer_sorter.h"
#include "cc/math_util.h"
#include "cc/proxy.h"
#include "cc/single_thread_proxy.h"
#include "cc/test/animation_test_common.h"
#include "cc/test/geometry_test_utils.h"
#include "cc/thread.h"
#include "ui/gfx/size_conversions.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include <public/WebTransformationMatrix.h>
using namespace WebKitTests;
using WebKit::WebTransformationMatrix;
namespace cc {
namespace {
template<typename LayerType>
void setLayerPropertiesForTestingInternal(LayerType* layer, const WebTransformationMatrix& transform, const WebTransformationMatrix& sublayerTransform, const gfx::PointF& anchor, const gfx::PointF& position, const gfx::Size& bounds, bool preserves3D)
{
layer->setTransform(transform);
layer->setSublayerTransform(sublayerTransform);
layer->setAnchorPoint(anchor);
layer->setPosition(position);
layer->setBounds(bounds);
layer->setPreserves3D(preserves3D);
}
void setLayerPropertiesForTesting(Layer* layer, const WebTransformationMatrix& transform, const WebTransformationMatrix& sublayerTransform, const gfx::PointF& anchor, const gfx::PointF& position, const gfx::Size& bounds, bool preserves3D)
{
setLayerPropertiesForTestingInternal<Layer>(layer, transform, sublayerTransform, anchor, position, bounds, preserves3D);
layer->setAutomaticallyComputeRasterScale(true);
}
void setLayerPropertiesForTesting(LayerImpl* layer, const WebTransformationMatrix& transform, const WebTransformationMatrix& sublayerTransform, const gfx::PointF& anchor, const gfx::PointF& position, const gfx::Size& bounds, bool preserves3D)
{
setLayerPropertiesForTestingInternal<LayerImpl>(layer, transform, sublayerTransform, anchor, position, bounds, preserves3D);
layer->setContentBounds(bounds);
}
void executeCalculateDrawTransformsAndVisibility(Layer* rootLayer, float deviceScaleFactor = 1, float pageScaleFactor = 1)
{
WebTransformationMatrix identityMatrix;
std::vector<scoped_refptr<Layer> > dummyRenderSurfaceLayerList;
int dummyMaxTextureSize = 512;
gfx::Size deviceViewportSize = gfx::Size(rootLayer->bounds().width() * deviceScaleFactor, rootLayer->bounds().height() * deviceScaleFactor);
// We are probably not testing what is intended if the rootLayer bounds are empty.
DCHECK(!rootLayer->bounds().IsEmpty());
LayerTreeHostCommon::calculateDrawTransforms(rootLayer, deviceViewportSize, deviceScaleFactor, pageScaleFactor, dummyMaxTextureSize, dummyRenderSurfaceLayerList);
}
void executeCalculateDrawTransformsAndVisibility(LayerImpl* rootLayer, float deviceScaleFactor = 1, float pageScaleFactor = 1)
{
// Note: this version skips layer sorting.
WebTransformationMatrix identityMatrix;
std::vector<LayerImpl*> dummyRenderSurfaceLayerList;
int dummyMaxTextureSize = 512;
gfx::Size deviceViewportSize = gfx::Size(rootLayer->bounds().width() * deviceScaleFactor, rootLayer->bounds().height() * deviceScaleFactor);
// We are probably not testing what is intended if the rootLayer bounds are empty.
DCHECK(!rootLayer->bounds().IsEmpty());
LayerTreeHostCommon::calculateDrawTransforms(rootLayer, deviceViewportSize, deviceScaleFactor, pageScaleFactor, 0, dummyMaxTextureSize, dummyRenderSurfaceLayerList);
}
WebTransformationMatrix remove3DComponentOfMatrix(const WebTransformationMatrix& mat)
{
WebTransformationMatrix ret = mat;
ret.setM13(0);
ret.setM23(0);
ret.setM31(0);
ret.setM32(0);
ret.setM33(1);
ret.setM34(0);
ret.setM43(0);
return ret;
}
scoped_ptr<LayerImpl> createTreeForFixedPositionTests()
{
scoped_ptr<LayerImpl> root = LayerImpl::create(1);
scoped_ptr<LayerImpl> child = LayerImpl::create(2);
scoped_ptr<LayerImpl> grandChild = LayerImpl::create(3);
scoped_ptr<LayerImpl> greatGrandChild = LayerImpl::create(4);
WebTransformationMatrix IdentityMatrix;
gfx::PointF anchor(0, 0);
gfx::PointF position(0, 0);
gfx::Size bounds(100, 100);
setLayerPropertiesForTesting(root.get(), IdentityMatrix, IdentityMatrix, anchor, position, bounds, false);
setLayerPropertiesForTesting(child.get(), IdentityMatrix, IdentityMatrix, anchor, position, bounds, false);
setLayerPropertiesForTesting(grandChild.get(), IdentityMatrix, IdentityMatrix, anchor, position, bounds, false);
setLayerPropertiesForTesting(greatGrandChild.get(), IdentityMatrix, IdentityMatrix, anchor, position, bounds, false);
grandChild->addChild(greatGrandChild.Pass());
child->addChild(grandChild.Pass());
root->addChild(child.Pass());
return root.Pass();
}
class LayerWithForcedDrawsContent : public Layer {
public:
LayerWithForcedDrawsContent()
: Layer()
{
}
virtual bool drawsContent() const OVERRIDE { return true; }
private:
virtual ~LayerWithForcedDrawsContent()
{
}
};
class MockContentLayerClient : public ContentLayerClient {
public:
MockContentLayerClient() { }
virtual ~MockContentLayerClient() { }
virtual void paintContents(SkCanvas*, const gfx::Rect& clip, gfx::RectF& opaque) OVERRIDE { }
};
scoped_refptr<ContentLayer> createDrawableContentLayer(ContentLayerClient* delegate)
{
scoped_refptr<ContentLayer> toReturn = ContentLayer::create(delegate);
toReturn->setIsDrawable(true);
return toReturn;
}
#define EXPECT_CONTENTS_SCALE_EQ(expected, layer) \
do { \
EXPECT_FLOAT_EQ(expected, layer->contentsScaleX()); \
EXPECT_FLOAT_EQ(expected, layer->contentsScaleY()); \
} while (false)
TEST(LayerTreeHostCommonTest, verifyTransformsForNoOpLayer)
{
// Sanity check: For layers positioned at zero, with zero size,
// and with identity transforms, then the drawTransform,
// screenSpaceTransform, and the hierarchy passed on to children
// layers should also be identity transforms.
scoped_refptr<Layer> parent = Layer::create();
scoped_refptr<Layer> child = Layer::create();
scoped_refptr<Layer> grandChild = Layer::create();
parent->addChild(child);
child->addChild(grandChild);
WebTransformationMatrix identityMatrix;
setLayerPropertiesForTesting(parent.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(100, 100), false);
setLayerPropertiesForTesting(child.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(0, 0), false);
setLayerPropertiesForTesting(grandChild.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(0, 0), false);
executeCalculateDrawTransformsAndVisibility(parent.get());
EXPECT_TRANSFORMATION_MATRIX_EQ(identityMatrix, child->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(identityMatrix, child->screenSpaceTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(identityMatrix, grandChild->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(identityMatrix, grandChild->screenSpaceTransform());
}
TEST(LayerTreeHostCommonTest, verifyTransformsForSingleLayer)
{
WebTransformationMatrix identityMatrix;
scoped_refptr<Layer> layer = Layer::create();
scoped_refptr<Layer> root = Layer::create();
setLayerPropertiesForTesting(root.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(1, 2), false);
root->addChild(layer);
// Case 1: setting the sublayer transform should not affect this layer's draw transform or screen-space transform.
WebTransformationMatrix arbitraryTranslation;
arbitraryTranslation.translate(10, 20);
setLayerPropertiesForTesting(layer.get(), identityMatrix, arbitraryTranslation, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(100, 100), false);
executeCalculateDrawTransformsAndVisibility(root.get());
WebTransformationMatrix expectedDrawTransform = identityMatrix;
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedDrawTransform, layer->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(identityMatrix, layer->screenSpaceTransform());
// Case 2: Setting the bounds of the layer should not affect either the draw transform or the screenspace transform.
WebTransformationMatrix translationToCenter;
translationToCenter.translate(5, 6);
setLayerPropertiesForTesting(layer.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(10, 12), false);
executeCalculateDrawTransformsAndVisibility(root.get());
EXPECT_TRANSFORMATION_MATRIX_EQ(identityMatrix, layer->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(identityMatrix, layer->screenSpaceTransform());
// Case 3: The anchor point by itself (without a layer transform) should have no effect on the transforms.
setLayerPropertiesForTesting(layer.get(), identityMatrix, identityMatrix, gfx::PointF(0.25, 0.25), gfx::PointF(0, 0), gfx::Size(10, 12), false);
executeCalculateDrawTransformsAndVisibility(root.get());
EXPECT_TRANSFORMATION_MATRIX_EQ(identityMatrix, layer->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(identityMatrix, layer->screenSpaceTransform());
// Case 4: A change in actual position affects both the draw transform and screen space transform.
WebTransformationMatrix positionTransform;
positionTransform.translate(0, 1.2);
setLayerPropertiesForTesting(layer.get(), identityMatrix, identityMatrix, gfx::PointF(0.25, 0.25), gfx::PointF(0, 1.2f), gfx::Size(10, 12), false);
executeCalculateDrawTransformsAndVisibility(root.get());
EXPECT_TRANSFORMATION_MATRIX_EQ(positionTransform, layer->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(positionTransform, layer->screenSpaceTransform());
// Case 5: In the correct sequence of transforms, the layer transform should pre-multiply the translationToCenter. This is easily tested by
// using a scale transform, because scale and translation are not commutative.
WebTransformationMatrix layerTransform;
layerTransform.scale3d(2, 2, 1);
setLayerPropertiesForTesting(layer.get(), layerTransform, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(10, 12), false);
executeCalculateDrawTransformsAndVisibility(root.get());
EXPECT_TRANSFORMATION_MATRIX_EQ(layerTransform, layer->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(layerTransform, layer->screenSpaceTransform());
// Case 6: The layer transform should occur with respect to the anchor point.
WebTransformationMatrix translationToAnchor;
translationToAnchor.translate(5, 0);
WebTransformationMatrix expectedResult = translationToAnchor * layerTransform * translationToAnchor.inverse();
setLayerPropertiesForTesting(layer.get(), layerTransform, identityMatrix, gfx::PointF(0.5, 0), gfx::PointF(0, 0), gfx::Size(10, 12), false);
executeCalculateDrawTransformsAndVisibility(root.get());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedResult, layer->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedResult, layer->screenSpaceTransform());
// Case 7: Verify that position pre-multiplies the layer transform.
// The current implementation of calculateDrawTransforms does this implicitly, but it is
// still worth testing to detect accidental regressions.
expectedResult = positionTransform * translationToAnchor * layerTransform * translationToAnchor.inverse();
setLayerPropertiesForTesting(layer.get(), layerTransform, identityMatrix, gfx::PointF(0.5, 0), gfx::PointF(0, 1.2f), gfx::Size(10, 12), false);
executeCalculateDrawTransformsAndVisibility(root.get());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedResult, layer->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedResult, layer->screenSpaceTransform());
}
TEST(LayerTreeHostCommonTest, verifyTransformsForSimpleHierarchy)
{
WebTransformationMatrix identityMatrix;
scoped_refptr<Layer> root = Layer::create();
scoped_refptr<Layer> parent = Layer::create();
scoped_refptr<Layer> child = Layer::create();
scoped_refptr<Layer> grandChild = Layer::create();
root->addChild(parent);
parent->addChild(child);
child->addChild(grandChild);
// One-time setup of root layer
setLayerPropertiesForTesting(root.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(1, 2), false);
// Case 1: parent's anchorPoint should not affect child or grandChild.
setLayerPropertiesForTesting(parent.get(), identityMatrix, identityMatrix, gfx::PointF(0.25, 0.25), gfx::PointF(0, 0), gfx::Size(10, 12), false);
setLayerPropertiesForTesting(child.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(16, 18), false);
setLayerPropertiesForTesting(grandChild.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(76, 78), false);
executeCalculateDrawTransformsAndVisibility(root.get());
EXPECT_TRANSFORMATION_MATRIX_EQ(identityMatrix, child->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(identityMatrix, child->screenSpaceTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(identityMatrix, grandChild->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(identityMatrix, grandChild->screenSpaceTransform());
// Case 2: parent's position affects child and grandChild.
WebTransformationMatrix parentPositionTransform;
parentPositionTransform.translate(0, 1.2);
setLayerPropertiesForTesting(parent.get(), identityMatrix, identityMatrix, gfx::PointF(0.25, 0.25), gfx::PointF(0, 1.2f), gfx::Size(10, 12), false);
setLayerPropertiesForTesting(child.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(16, 18), false);
setLayerPropertiesForTesting(grandChild.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(76, 78), false);
executeCalculateDrawTransformsAndVisibility(root.get());
EXPECT_TRANSFORMATION_MATRIX_EQ(parentPositionTransform, child->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(parentPositionTransform, child->screenSpaceTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(parentPositionTransform, grandChild->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(parentPositionTransform, grandChild->screenSpaceTransform());
// Case 3: parent's local transform affects child and grandchild
WebTransformationMatrix parentLayerTransform;
parentLayerTransform.scale3d(2, 2, 1);
WebTransformationMatrix parentTranslationToAnchor;
parentTranslationToAnchor.translate(2.5, 3);
WebTransformationMatrix parentCompositeTransform = parentTranslationToAnchor * parentLayerTransform * parentTranslationToAnchor.inverse();
setLayerPropertiesForTesting(parent.get(), parentLayerTransform, identityMatrix, gfx::PointF(0.25, 0.25), gfx::PointF(0, 0), gfx::Size(10, 12), false);
setLayerPropertiesForTesting(child.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(16, 18), false);
setLayerPropertiesForTesting(grandChild.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(76, 78), false);
executeCalculateDrawTransformsAndVisibility(root.get());
EXPECT_TRANSFORMATION_MATRIX_EQ(parentCompositeTransform, child->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(parentCompositeTransform, child->screenSpaceTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(parentCompositeTransform, grandChild->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(parentCompositeTransform, grandChild->screenSpaceTransform());
// Case 4: parent's sublayerMatrix affects child and grandchild
// scaling is used here again so that the correct sequence of transforms is properly tested.
// Note that preserves3D is false, but the sublayer matrix should retain its 3D properties when given to child.
// But then, the child also does not preserve3D. When it gives its hierarchy to the grandChild, it should be flattened to 2D.
WebTransformationMatrix parentSublayerMatrix;
parentSublayerMatrix.scale3d(10, 10, 3.3);
WebTransformationMatrix parentTranslationToCenter;
parentTranslationToCenter.translate(5, 6);
// Sublayer matrix is applied to the center of the parent layer.
parentCompositeTransform = parentTranslationToAnchor * parentLayerTransform * parentTranslationToAnchor.inverse()
* parentTranslationToCenter * parentSublayerMatrix * parentTranslationToCenter.inverse();
WebTransformationMatrix flattenedCompositeTransform = remove3DComponentOfMatrix(parentCompositeTransform);
setLayerPropertiesForTesting(parent.get(), parentLayerTransform, parentSublayerMatrix, gfx::PointF(0.25, 0.25), gfx::PointF(0, 0), gfx::Size(10, 12), false);
setLayerPropertiesForTesting(child.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(16, 18), false);
setLayerPropertiesForTesting(grandChild.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(76, 78), false);
executeCalculateDrawTransformsAndVisibility(root.get());
EXPECT_TRANSFORMATION_MATRIX_EQ(parentCompositeTransform, child->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(parentCompositeTransform, child->screenSpaceTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(flattenedCompositeTransform, grandChild->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(flattenedCompositeTransform, grandChild->screenSpaceTransform());
// Case 5: same as Case 4, except that child does preserve 3D, so the grandChild should receive the non-flattened composite transform.
//
setLayerPropertiesForTesting(parent.get(), parentLayerTransform, parentSublayerMatrix, gfx::PointF(0.25, 0.25), gfx::PointF(0, 0), gfx::Size(10, 12), false);
setLayerPropertiesForTesting(child.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(16, 18), true);
setLayerPropertiesForTesting(grandChild.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(76, 78), false);
executeCalculateDrawTransformsAndVisibility(root.get());
EXPECT_TRANSFORMATION_MATRIX_EQ(parentCompositeTransform, child->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(parentCompositeTransform, child->screenSpaceTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(parentCompositeTransform, grandChild->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(parentCompositeTransform, grandChild->screenSpaceTransform());
}
TEST(LayerTreeHostCommonTest, verifyTransformsForSingleRenderSurface)
{
scoped_refptr<Layer> root = Layer::create();
scoped_refptr<Layer> parent = Layer::create();
scoped_refptr<Layer> child = Layer::create();
scoped_refptr<LayerWithForcedDrawsContent> grandChild = make_scoped_refptr(new LayerWithForcedDrawsContent());
root->addChild(parent);
parent->addChild(child);
child->addChild(grandChild);
// One-time setup of root layer
WebTransformationMatrix identityMatrix;
setLayerPropertiesForTesting(root.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(1, 2), false);
// Child is set up so that a new render surface should be created.
child->setOpacity(0.5);
WebTransformationMatrix parentLayerTransform;
parentLayerTransform.scale3d(1, 0.9, 1);
WebTransformationMatrix parentTranslationToAnchor;
parentTranslationToAnchor.translate(25, 30);
WebTransformationMatrix parentSublayerMatrix;
parentSublayerMatrix.scale3d(0.9, 1, 3.3);
WebTransformationMatrix parentTranslationToCenter;
parentTranslationToCenter.translate(50, 60);
WebTransformationMatrix parentCompositeTransform = parentTranslationToAnchor * parentLayerTransform * parentTranslationToAnchor.inverse()
* parentTranslationToCenter * parentSublayerMatrix * parentTranslationToCenter.inverse();
gfx::Vector2dF parentCompositeScale = MathUtil::computeTransform2dScaleComponents(parentCompositeTransform);
WebTransformationMatrix surfaceSublayerTransform;
surfaceSublayerTransform.scaleNonUniform(parentCompositeScale.x(), parentCompositeScale.y());
WebTransformationMatrix surfaceSublayerCompositeTransform = parentCompositeTransform * surfaceSublayerTransform.inverse();
// Child's render surface should not exist yet.
ASSERT_FALSE(child->renderSurface());
setLayerPropertiesForTesting(parent.get(), parentLayerTransform, parentSublayerMatrix, gfx::PointF(0.25, 0.25), gfx::PointF(0, 0), gfx::Size(100, 120), false);
setLayerPropertiesForTesting(child.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(16, 18), false);
setLayerPropertiesForTesting(grandChild.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(8, 10), false);
executeCalculateDrawTransformsAndVisibility(root.get());
// Render surface should have been created now.
ASSERT_TRUE(child->renderSurface());
ASSERT_EQ(child, child->renderTarget());
// The child layer's draw transform should refer to its new render surface.
// The screen-space transform, however, should still refer to the root.
EXPECT_TRANSFORMATION_MATRIX_EQ(surfaceSublayerTransform, child->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(parentCompositeTransform, child->screenSpaceTransform());
// Because the grandChild is the only drawable content, the child's renderSurface will tighten its bounds to the grandChild.
// The scale at which the surface's subtree is drawn must be removed from the composite transform.
EXPECT_TRANSFORMATION_MATRIX_EQ(surfaceSublayerCompositeTransform, child->renderTarget()->renderSurface()->drawTransform());
// The screen space is the same as the target since the child surface draws into the root.
EXPECT_TRANSFORMATION_MATRIX_EQ(surfaceSublayerCompositeTransform, child->renderTarget()->renderSurface()->screenSpaceTransform());
}
TEST(LayerTreeHostCommonTest, verifyTransformsForReplica)
{
scoped_refptr<Layer> root = Layer::create();
scoped_refptr<Layer> parent = Layer::create();
scoped_refptr<Layer> child = Layer::create();
scoped_refptr<Layer> childReplica = Layer::create();
scoped_refptr<LayerWithForcedDrawsContent> grandChild = make_scoped_refptr(new LayerWithForcedDrawsContent());
root->addChild(parent);
parent->addChild(child);
child->addChild(grandChild);
child->setReplicaLayer(childReplica.get());
// One-time setup of root layer
WebTransformationMatrix identityMatrix;
setLayerPropertiesForTesting(root.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(1, 2), false);
// Child is set up so that a new render surface should be created.
child->setOpacity(0.5);
WebTransformationMatrix parentLayerTransform;
parentLayerTransform.scale3d(2, 2, 1);
WebTransformationMatrix parentTranslationToAnchor;
parentTranslationToAnchor.translate(2.5, 3);
WebTransformationMatrix parentSublayerMatrix;
parentSublayerMatrix.scale3d(10, 10, 3.3);
WebTransformationMatrix parentTranslationToCenter;
parentTranslationToCenter.translate(5, 6);
WebTransformationMatrix parentCompositeTransform = parentTranslationToAnchor * parentLayerTransform * parentTranslationToAnchor.inverse()
* parentTranslationToCenter * parentSublayerMatrix * parentTranslationToCenter.inverse();
WebTransformationMatrix childTranslationToCenter;
childTranslationToCenter.translate(8, 9);
WebTransformationMatrix replicaLayerTransform;
replicaLayerTransform.scale3d(3, 3, 1);
gfx::Vector2dF parentCompositeScale = MathUtil::computeTransform2dScaleComponents(parentCompositeTransform);
WebTransformationMatrix surfaceSublayerTransform;
surfaceSublayerTransform.scaleNonUniform(parentCompositeScale.x(), parentCompositeScale.y());
WebTransformationMatrix replicaCompositeTransform = parentCompositeTransform * replicaLayerTransform * surfaceSublayerTransform.inverse();
// Child's render surface should not exist yet.
ASSERT_FALSE(child->renderSurface());
setLayerPropertiesForTesting(parent.get(), parentLayerTransform, parentSublayerMatrix, gfx::PointF(0.25, 0.25), gfx::PointF(0, 0), gfx::Size(10, 12), false);
setLayerPropertiesForTesting(child.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(16, 18), false);
setLayerPropertiesForTesting(grandChild.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(-0.5, -0.5), gfx::Size(1, 1), false);
setLayerPropertiesForTesting(childReplica.get(), replicaLayerTransform, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(0, 0), false);
executeCalculateDrawTransformsAndVisibility(root.get());
// Render surface should have been created now.
ASSERT_TRUE(child->renderSurface());
ASSERT_EQ(child, child->renderTarget());
EXPECT_TRANSFORMATION_MATRIX_EQ(replicaCompositeTransform, child->renderTarget()->renderSurface()->replicaDrawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(replicaCompositeTransform, child->renderTarget()->renderSurface()->replicaScreenSpaceTransform());
}
TEST(LayerTreeHostCommonTest, verifyTransformsForRenderSurfaceHierarchy)
{
// This test creates a more complex tree and verifies it all at once. This covers the following cases:
// - layers that are described w.r.t. a render surface: should have draw transforms described w.r.t. that surface
// - A render surface described w.r.t. an ancestor render surface: should have a draw transform described w.r.t. that ancestor surface
// - Replicas of a render surface are described w.r.t. the replica's transform around its anchor, along with the surface itself.
// - Sanity check on recursion: verify transforms of layers described w.r.t. a render surface that is described w.r.t. an ancestor render surface.
// - verifying that each layer has a reference to the correct renderSurface and renderTarget values.
scoped_refptr<Layer> root = Layer::create();
scoped_refptr<Layer> parent = Layer::create();
scoped_refptr<Layer> renderSurface1 = Layer::create();
scoped_refptr<Layer> renderSurface2 = Layer::create();
scoped_refptr<Layer> childOfRoot = Layer::create();
scoped_refptr<Layer> childOfRS1 = Layer::create();
scoped_refptr<Layer> childOfRS2 = Layer::create();
scoped_refptr<Layer> replicaOfRS1 = Layer::create();
scoped_refptr<Layer> replicaOfRS2 = Layer::create();
scoped_refptr<Layer> grandChildOfRoot = Layer::create();
scoped_refptr<LayerWithForcedDrawsContent> grandChildOfRS1 = make_scoped_refptr(new LayerWithForcedDrawsContent());
scoped_refptr<LayerWithForcedDrawsContent> grandChildOfRS2 = make_scoped_refptr(new LayerWithForcedDrawsContent());
root->addChild(parent);
parent->addChild(renderSurface1);
parent->addChild(childOfRoot);
renderSurface1->addChild(childOfRS1);
renderSurface1->addChild(renderSurface2);
renderSurface2->addChild(childOfRS2);
childOfRoot->addChild(grandChildOfRoot);
childOfRS1->addChild(grandChildOfRS1);
childOfRS2->addChild(grandChildOfRS2);
renderSurface1->setReplicaLayer(replicaOfRS1.get());
renderSurface2->setReplicaLayer(replicaOfRS2.get());
// In combination with descendantDrawsContent, opacity != 1 forces the layer to have a new renderSurface.
renderSurface1->setOpacity(0.5);
renderSurface2->setOpacity(0.33f);
// One-time setup of root layer
WebTransformationMatrix identityMatrix;
setLayerPropertiesForTesting(root.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(1, 2), false);
// All layers in the tree are initialized with an anchor at .25 and a size of (10,10).
// matrix "A" is the composite layer transform used in all layers, centered about the anchor point
// matrix "B" is the sublayer transform used in all layers, centered about the center position of the layer.
// matrix "R" is the composite replica transform used in all replica layers.
//
// x component tests that layerTransform and sublayerTransform are done in the right order (translation and scale are noncommutative).
// y component has a translation by 1 for every ancestor, which indicates the "depth" of the layer in the hierarchy.
WebTransformationMatrix translationToAnchor;
translationToAnchor.translate(2.5, 0);
WebTransformationMatrix translationToCenter;
translationToCenter.translate(5, 5);
WebTransformationMatrix layerTransform;
layerTransform.translate(1, 1);
WebTransformationMatrix sublayerTransform;
sublayerTransform.scale3d(10, 1, 1);
WebTransformationMatrix replicaLayerTransform;
replicaLayerTransform.scale3d(-2, 5, 1);
WebTransformationMatrix A = translationToAnchor * layerTransform * translationToAnchor.inverse();
WebTransformationMatrix B = translationToCenter * sublayerTransform * translationToCenter.inverse();
WebTransformationMatrix R = A * translationToAnchor * replicaLayerTransform * translationToAnchor.inverse();
gfx::Vector2dF surface1ParentTransformScale = MathUtil::computeTransform2dScaleComponents(A * B);
WebTransformationMatrix surface1SublayerTransform;
surface1SublayerTransform.scaleNonUniform(surface1ParentTransformScale.x(), surface1ParentTransformScale.y());
// SS1 = transform given to the subtree of renderSurface1
WebTransformationMatrix SS1 = surface1SublayerTransform;
// S1 = transform to move from renderSurface1 pixels to the layer space of the owning layer
WebTransformationMatrix S1 = surface1SublayerTransform.inverse();
gfx::Vector2dF surface2ParentTransformScale = MathUtil::computeTransform2dScaleComponents(SS1 * A * B);
WebTransformationMatrix surface2SublayerTransform;
surface2SublayerTransform.scaleNonUniform(surface2ParentTransformScale.x(), surface2ParentTransformScale.y());
// SS2 = transform given to the subtree of renderSurface2
WebTransformationMatrix SS2 = surface2SublayerTransform;
// S2 = transform to move from renderSurface2 pixels to the layer space of the owning layer
WebTransformationMatrix S2 = surface2SublayerTransform.inverse();
setLayerPropertiesForTesting(parent.get(), layerTransform, sublayerTransform, gfx::PointF(0.25, 0), gfx::PointF(0, 0), gfx::Size(10, 10), false);
setLayerPropertiesForTesting(renderSurface1.get(), layerTransform, sublayerTransform, gfx::PointF(0.25, 0), gfx::PointF(0, 0), gfx::Size(10, 10), false);
setLayerPropertiesForTesting(renderSurface2.get(), layerTransform, sublayerTransform, gfx::PointF(0.25, 0), gfx::PointF(0, 0), gfx::Size(10, 10), false);
setLayerPropertiesForTesting(childOfRoot.get(), layerTransform, sublayerTransform, gfx::PointF(0.25, 0), gfx::PointF(0, 0), gfx::Size(10, 10), false);
setLayerPropertiesForTesting(childOfRS1.get(), layerTransform, sublayerTransform, gfx::PointF(0.25, 0), gfx::PointF(0, 0), gfx::Size(10, 10), false);
setLayerPropertiesForTesting(childOfRS2.get(), layerTransform, sublayerTransform, gfx::PointF(0.25, 0), gfx::PointF(0, 0), gfx::Size(10, 10), false);
setLayerPropertiesForTesting(grandChildOfRoot.get(), layerTransform, sublayerTransform, gfx::PointF(0.25, 0), gfx::PointF(0, 0), gfx::Size(10, 10), false);
setLayerPropertiesForTesting(grandChildOfRS1.get(), layerTransform, sublayerTransform, gfx::PointF(0.25, 0), gfx::PointF(0, 0), gfx::Size(10, 10), false);
setLayerPropertiesForTesting(grandChildOfRS2.get(), layerTransform, sublayerTransform, gfx::PointF(0.25, 0), gfx::PointF(0, 0), gfx::Size(10, 10), false);
setLayerPropertiesForTesting(replicaOfRS1.get(), replicaLayerTransform, sublayerTransform, gfx::PointF(0.25, 0), gfx::PointF(0, 0), gfx::Size(), false);
setLayerPropertiesForTesting(replicaOfRS2.get(), replicaLayerTransform, sublayerTransform, gfx::PointF(0.25, 0), gfx::PointF(0, 0), gfx::Size(), false);
executeCalculateDrawTransformsAndVisibility(root.get());
// Only layers that are associated with render surfaces should have an actual renderSurface() value.
//
ASSERT_TRUE(root->renderSurface());
ASSERT_FALSE(childOfRoot->renderSurface());
ASSERT_FALSE(grandChildOfRoot->renderSurface());
ASSERT_TRUE(renderSurface1->renderSurface());
ASSERT_FALSE(childOfRS1->renderSurface());
ASSERT_FALSE(grandChildOfRS1->renderSurface());
ASSERT_TRUE(renderSurface2->renderSurface());
ASSERT_FALSE(childOfRS2->renderSurface());
ASSERT_FALSE(grandChildOfRS2->renderSurface());
// Verify all renderTarget accessors
//
EXPECT_EQ(root, parent->renderTarget());
EXPECT_EQ(root, childOfRoot->renderTarget());
EXPECT_EQ(root, grandChildOfRoot->renderTarget());
EXPECT_EQ(renderSurface1, renderSurface1->renderTarget());
EXPECT_EQ(renderSurface1, childOfRS1->renderTarget());
EXPECT_EQ(renderSurface1, grandChildOfRS1->renderTarget());
EXPECT_EQ(renderSurface2, renderSurface2->renderTarget());
EXPECT_EQ(renderSurface2, childOfRS2->renderTarget());
EXPECT_EQ(renderSurface2, grandChildOfRS2->renderTarget());
// Verify layer draw transforms
// note that draw transforms are described with respect to the nearest ancestor render surface
// but screen space transforms are described with respect to the root.
//
EXPECT_TRANSFORMATION_MATRIX_EQ(A, parent->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(A * B * A, childOfRoot->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(A * B * A * B * A, grandChildOfRoot->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(SS1, renderSurface1->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(SS1 * B * A, childOfRS1->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(SS1 * B * A * B * A, grandChildOfRS1->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(SS2, renderSurface2->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(SS2 * B * A, childOfRS2->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(SS2 * B * A * B * A, grandChildOfRS2->drawTransform());
// Verify layer screen-space transforms
//
EXPECT_TRANSFORMATION_MATRIX_EQ(A, parent->screenSpaceTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(A * B * A, childOfRoot->screenSpaceTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(A * B * A * B * A, grandChildOfRoot->screenSpaceTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(A * B * A, renderSurface1->screenSpaceTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(A * B * A * B * A, childOfRS1->screenSpaceTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(A * B * A * B * A * B * A, grandChildOfRS1->screenSpaceTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(A * B * A * B * A, renderSurface2->screenSpaceTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(A * B * A * B * A * B * A, childOfRS2->screenSpaceTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(A * B * A * B * A * B * A * B * A, grandChildOfRS2->screenSpaceTransform());
// Verify render surface transforms.
//
// Draw transform of render surface 1 is described with respect to root.
EXPECT_TRANSFORMATION_MATRIX_EQ(A * B * A * S1, renderSurface1->renderSurface()->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(A * B * R * S1, renderSurface1->renderSurface()->replicaDrawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(A * B * A * S1, renderSurface1->renderSurface()->screenSpaceTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(A * B * R * S1, renderSurface1->renderSurface()->replicaScreenSpaceTransform());
// Draw transform of render surface 2 is described with respect to render surface 1.
EXPECT_TRANSFORMATION_MATRIX_EQ(SS1 * B * A * S2, renderSurface2->renderSurface()->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(SS1 * B * R * S2, renderSurface2->renderSurface()->replicaDrawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(A * B * A * B * A * S2, renderSurface2->renderSurface()->screenSpaceTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(A * B * A * B * R * S2, renderSurface2->renderSurface()->replicaScreenSpaceTransform());
// Sanity check. If these fail there is probably a bug in the test itself.
// It is expected that we correctly set up transforms so that the y-component of the screen-space transform
// encodes the "depth" of the layer in the tree.
EXPECT_FLOAT_EQ(1, parent->screenSpaceTransform().m42());
EXPECT_FLOAT_EQ(2, childOfRoot->screenSpaceTransform().m42());
EXPECT_FLOAT_EQ(3, grandChildOfRoot->screenSpaceTransform().m42());
EXPECT_FLOAT_EQ(2, renderSurface1->screenSpaceTransform().m42());
EXPECT_FLOAT_EQ(3, childOfRS1->screenSpaceTransform().m42());
EXPECT_FLOAT_EQ(4, grandChildOfRS1->screenSpaceTransform().m42());
EXPECT_FLOAT_EQ(3, renderSurface2->screenSpaceTransform().m42());
EXPECT_FLOAT_EQ(4, childOfRS2->screenSpaceTransform().m42());
EXPECT_FLOAT_EQ(5, grandChildOfRS2->screenSpaceTransform().m42());
}
TEST(LayerTreeHostCommonTest, verifyTransformsForFlatteningLayer)
{
// For layers that flatten their subtree, there should be an orthographic projection
// (for x and y values) in the middle of the transform sequence. Note that the way the
// code is currently implemented, it is not expected to use a canonical orthographic
// projection.
scoped_refptr<Layer> root = Layer::create();
scoped_refptr<Layer> child = Layer::create();
scoped_refptr<LayerWithForcedDrawsContent> grandChild = make_scoped_refptr(new LayerWithForcedDrawsContent());
WebTransformationMatrix rotationAboutYAxis;
rotationAboutYAxis.rotate3d(0, 30, 0);
const WebTransformationMatrix identityMatrix;
setLayerPropertiesForTesting(root.get(), identityMatrix, identityMatrix, gfx::PointF(), gfx::PointF(), gfx::Size(100, 100), false);
setLayerPropertiesForTesting(child.get(), rotationAboutYAxis, identityMatrix, gfx::PointF(), gfx::PointF(), gfx::Size(10, 10), false);
setLayerPropertiesForTesting(grandChild.get(), rotationAboutYAxis, identityMatrix, gfx::PointF(), gfx::PointF(), gfx::Size(10, 10), false);
root->addChild(child);
child->addChild(grandChild);
child->setForceRenderSurface(true);
// No layers in this test should preserve 3d.
ASSERT_FALSE(root->preserves3D());
ASSERT_FALSE(child->preserves3D());
ASSERT_FALSE(grandChild->preserves3D());
WebTransformationMatrix expectedChildDrawTransform = rotationAboutYAxis;
WebTransformationMatrix expectedChildScreenSpaceTransform = rotationAboutYAxis;
WebTransformationMatrix expectedGrandChildDrawTransform = rotationAboutYAxis; // draws onto child's renderSurface
WebTransformationMatrix expectedGrandChildScreenSpaceTransform = rotationAboutYAxis.to2dTransform() * rotationAboutYAxis;
executeCalculateDrawTransformsAndVisibility(root.get());
// The child's drawTransform should have been taken by its surface.
ASSERT_TRUE(child->renderSurface());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedChildDrawTransform, child->renderSurface()->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedChildScreenSpaceTransform, child->renderSurface()->screenSpaceTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(identityMatrix, child->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedChildScreenSpaceTransform, child->screenSpaceTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedGrandChildDrawTransform, grandChild->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedGrandChildScreenSpaceTransform, grandChild->screenSpaceTransform());
}
TEST(LayerTreeHostCommonTest, verifyTransformsForDegenerateIntermediateLayer)
{
// A layer that is empty in one axis, but not the other, was accidentally skipping a necessary translation.
// Without that translation, the coordinate space of the layer's drawTransform is incorrect.
//
// Normally this isn't a problem, because the layer wouldn't be drawn anyway, but if that layer becomes a renderSurface, then
// its drawTransform is implicitly inherited by the rest of the subtree, which then is positioned incorrectly as a result.
scoped_refptr<Layer> root = Layer::create();
scoped_refptr<Layer> child = Layer::create();
scoped_refptr<LayerWithForcedDrawsContent> grandChild = make_scoped_refptr(new LayerWithForcedDrawsContent());
// The child height is zero, but has non-zero width that should be accounted for while computing drawTransforms.
const WebTransformationMatrix identityMatrix;
setLayerPropertiesForTesting(root.get(), identityMatrix, identityMatrix, gfx::PointF(), gfx::PointF(), gfx::Size(100, 100), false);
setLayerPropertiesForTesting(child.get(), identityMatrix, identityMatrix, gfx::PointF(), gfx::PointF(), gfx::Size(10, 0), false);
setLayerPropertiesForTesting(grandChild.get(), identityMatrix, identityMatrix, gfx::PointF(), gfx::PointF(), gfx::Size(10, 10), false);
root->addChild(child);
child->addChild(grandChild);
child->setForceRenderSurface(true);
executeCalculateDrawTransformsAndVisibility(root.get());
ASSERT_TRUE(child->renderSurface());
EXPECT_TRANSFORMATION_MATRIX_EQ(identityMatrix, child->renderSurface()->drawTransform()); // This is the real test, the rest are sanity checks.
EXPECT_TRANSFORMATION_MATRIX_EQ(identityMatrix, child->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(identityMatrix, grandChild->drawTransform());
}
TEST(LayerTreeHostCommonTest, verifyRenderSurfaceListForRenderSurfaceWithClippedLayer)
{
scoped_refptr<Layer> parent = Layer::create();
scoped_refptr<Layer> renderSurface1 = Layer::create();
scoped_refptr<LayerWithForcedDrawsContent> child = make_scoped_refptr(new LayerWithForcedDrawsContent());
const WebTransformationMatrix identityMatrix;
setLayerPropertiesForTesting(parent.get(), identityMatrix, identityMatrix, gfx::PointF(), gfx::PointF(), gfx::Size(10, 10), false);
setLayerPropertiesForTesting(renderSurface1.get(), identityMatrix, identityMatrix, gfx::PointF(), gfx::PointF(), gfx::Size(10, 10), false);
setLayerPropertiesForTesting(child.get(), identityMatrix, identityMatrix, gfx::PointF(), gfx::PointF(30, 30), gfx::Size(10, 10), false);
parent->addChild(renderSurface1);
parent->setMasksToBounds(true);
renderSurface1->addChild(child);
renderSurface1->setForceRenderSurface(true);
std::vector<scoped_refptr<Layer> > renderSurfaceLayerList;
int dummyMaxTextureSize = 512;
LayerTreeHostCommon::calculateDrawTransforms(parent.get(), parent->bounds(), 1, 1, dummyMaxTextureSize, renderSurfaceLayerList);
// The child layer's content is entirely outside the parent's clip rect, so the intermediate
// render surface should not be listed here, even if it was forced to be created. Render surfaces without children or visible
// content are unexpected at draw time (e.g. we might try to create a content texture of size 0).
ASSERT_TRUE(parent->renderSurface());
ASSERT_FALSE(renderSurface1->renderSurface());
EXPECT_EQ(1U, renderSurfaceLayerList.size());
}
TEST(LayerTreeHostCommonTest, verifyRenderSurfaceListForTransparentChild)
{
scoped_refptr<Layer> parent = Layer::create();
scoped_refptr<Layer> renderSurface1 = Layer::create();
scoped_refptr<LayerWithForcedDrawsContent> child = make_scoped_refptr(new LayerWithForcedDrawsContent());
const WebTransformationMatrix identityMatrix;
setLayerPropertiesForTesting(renderSurface1.get(), identityMatrix, identityMatrix, gfx::PointF(), gfx::PointF(), gfx::Size(10, 10), false);
setLayerPropertiesForTesting(child.get(), identityMatrix, identityMatrix, gfx::PointF(), gfx::PointF(), gfx::Size(10, 10), false);
parent->addChild(renderSurface1);
renderSurface1->addChild(child);
renderSurface1->setForceRenderSurface(true);
renderSurface1->setOpacity(0);
std::vector<scoped_refptr<Layer> > renderSurfaceLayerList;
int dummyMaxTextureSize = 512;
LayerTreeHostCommon::calculateDrawTransforms(parent.get(), parent->bounds(), 1, 1, dummyMaxTextureSize, renderSurfaceLayerList);
// Since the layer is transparent, renderSurface1->renderSurface() should not have gotten added anywhere.
// Also, the drawable content rect should not have been extended by the children.
ASSERT_TRUE(parent->renderSurface());
EXPECT_EQ(0U, parent->renderSurface()->layerList().size());
EXPECT_EQ(1U, renderSurfaceLayerList.size());
EXPECT_EQ(parent->id(), renderSurfaceLayerList[0]->id());
EXPECT_EQ(gfx::Rect(), parent->drawableContentRect());
}
TEST(LayerTreeHostCommonTest, verifyForceRenderSurface)
{
scoped_refptr<Layer> parent = Layer::create();
scoped_refptr<Layer> renderSurface1 = Layer::create();
scoped_refptr<LayerWithForcedDrawsContent> child = make_scoped_refptr(new LayerWithForcedDrawsContent());
renderSurface1->setForceRenderSurface(true);
const WebTransformationMatrix identityMatrix;
setLayerPropertiesForTesting(parent.get(), identityMatrix, identityMatrix, gfx::PointF(), gfx::PointF(), gfx::Size(10, 10), false);
setLayerPropertiesForTesting(renderSurface1.get(), identityMatrix, identityMatrix, gfx::PointF(), gfx::PointF(), gfx::Size(10, 10), false);
setLayerPropertiesForTesting(child.get(), identityMatrix, identityMatrix, gfx::PointF(), gfx::PointF(), gfx::Size(10, 10), false);
parent->addChild(renderSurface1);
renderSurface1->addChild(child);
// Sanity check before the actual test
EXPECT_FALSE(parent->renderSurface());
EXPECT_FALSE(renderSurface1->renderSurface());
std::vector<scoped_refptr<Layer> > renderSurfaceLayerList;
int dummyMaxTextureSize = 512;
LayerTreeHostCommon::calculateDrawTransforms(parent.get(), parent->bounds(), 1, 1, dummyMaxTextureSize, renderSurfaceLayerList);
// The root layer always creates a renderSurface
EXPECT_TRUE(parent->renderSurface());
EXPECT_TRUE(renderSurface1->renderSurface());
EXPECT_EQ(2U, renderSurfaceLayerList.size());
renderSurfaceLayerList.clear();
renderSurface1->setForceRenderSurface(false);
LayerTreeHostCommon::calculateDrawTransforms(parent.get(), parent->bounds(), 1, 1, dummyMaxTextureSize, renderSurfaceLayerList);
EXPECT_TRUE(parent->renderSurface());
EXPECT_FALSE(renderSurface1->renderSurface());
EXPECT_EQ(1U, renderSurfaceLayerList.size());
}
TEST(LayerTreeHostCommonTest, verifyScrollCompensationForFixedPositionLayerWithDirectContainer)
{
// This test checks for correct scroll compensation when the fixed-position container
// is the direct parent of the fixed-position layer.
scoped_ptr<LayerImpl> root = createTreeForFixedPositionTests();
LayerImpl* child = root->children()[0];
LayerImpl* grandChild = child->children()[0];
child->setIsContainerForFixedPositionLayers(true);
grandChild->setFixedToContainerLayer(true);
// Case 1: scrollDelta of 0, 0
child->setScrollDelta(gfx::Vector2d(0, 0));
executeCalculateDrawTransformsAndVisibility(root.get());
WebTransformationMatrix expectedChildTransform;
WebTransformationMatrix expectedGrandChildTransform = expectedChildTransform;
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedChildTransform, child->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedGrandChildTransform, grandChild->drawTransform());
// Case 2: scrollDelta of 10, 10
child->setScrollDelta(gfx::Vector2d(10, 10));
executeCalculateDrawTransformsAndVisibility(root.get());
// Here the child is affected by scrollDelta, but the fixed position grandChild should not be affected.
expectedChildTransform.makeIdentity();
expectedChildTransform.translate(-10, -10);
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedChildTransform, child->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedGrandChildTransform, grandChild->drawTransform());
}
TEST(LayerTreeHostCommonTest, verifyScrollCompensationForFixedPositionLayerWithTransformedDirectContainer)
{
// This test checks for correct scroll compensation when the fixed-position container
// is the direct parent of the fixed-position layer, but that container is transformed.
// In this case, the fixed position element inherits the container's transform,
// but the scrollDelta that has to be undone should not be affected by that transform.
//
// Transforms are in general non-commutative; using something like a non-uniform scale
// helps to verify that translations and non-uniform scales are applied in the correct
// order.
scoped_ptr<LayerImpl> root = createTreeForFixedPositionTests();
LayerImpl* child = root->children()[0];
LayerImpl* grandChild = child->children()[0];
// This scale will cause child and grandChild to be effectively 200 x 800 with respect to the renderTarget.
WebTransformationMatrix nonUniformScale;
nonUniformScale.scaleNonUniform(2, 8);
child->setTransform(nonUniformScale);
child->setIsContainerForFixedPositionLayers(true);
grandChild->setFixedToContainerLayer(true);
// Case 1: scrollDelta of 0, 0
child->setScrollDelta(gfx::Vector2d(0, 0));
executeCalculateDrawTransformsAndVisibility(root.get());
WebTransformationMatrix expectedChildTransform;
expectedChildTransform.multiply(nonUniformScale);
WebTransformationMatrix expectedGrandChildTransform = expectedChildTransform;
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedChildTransform, child->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedGrandChildTransform, grandChild->drawTransform());
// Case 2: scrollDelta of 10, 20
child->setScrollDelta(gfx::Vector2d(10, 20));
executeCalculateDrawTransformsAndVisibility(root.get());
// The child should be affected by scrollDelta, but the fixed position grandChild should not be affected.
expectedChildTransform.makeIdentity();
expectedChildTransform.translate(-10, -20); // scrollDelta
expectedChildTransform.multiply(nonUniformScale);
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedChildTransform, child->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedGrandChildTransform, grandChild->drawTransform());
}
TEST(LayerTreeHostCommonTest, verifyScrollCompensationForFixedPositionLayerWithDistantContainer)
{
// This test checks for correct scroll compensation when the fixed-position container
// is NOT the direct parent of the fixed-position layer.
scoped_ptr<LayerImpl> root = createTreeForFixedPositionTests();
LayerImpl* child = root->children()[0];
LayerImpl* grandChild = child->children()[0];
LayerImpl* greatGrandChild = grandChild->children()[0];
child->setIsContainerForFixedPositionLayers(true);
grandChild->setPosition(gfx::PointF(8, 6));
greatGrandChild->setFixedToContainerLayer(true);
// Case 1: scrollDelta of 0, 0
child->setScrollDelta(gfx::Vector2d(0, 0));
executeCalculateDrawTransformsAndVisibility(root.get());
WebTransformationMatrix expectedChildTransform;
WebTransformationMatrix expectedGrandChildTransform;
expectedGrandChildTransform.translate(8, 6);
WebTransformationMatrix expectedGreatGrandChildTransform = expectedGrandChildTransform;
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedChildTransform, child->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedGrandChildTransform, grandChild->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedGreatGrandChildTransform, greatGrandChild->drawTransform());
// Case 2: scrollDelta of 10, 10
child->setScrollDelta(gfx::Vector2d(10, 10));
executeCalculateDrawTransformsAndVisibility(root.get());
// Here the child and grandChild are affected by scrollDelta, but the fixed position greatGrandChild should not be affected.
expectedChildTransform.makeIdentity();
expectedChildTransform.translate(-10, -10);
expectedGrandChildTransform.makeIdentity();
expectedGrandChildTransform.translate(-2, -4);
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedChildTransform, child->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedGrandChildTransform, grandChild->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedGreatGrandChildTransform, greatGrandChild->drawTransform());
}
TEST(LayerTreeHostCommonTest, verifyScrollCompensationForFixedPositionLayerWithDistantContainerAndTransforms)
{
// This test checks for correct scroll compensation when the fixed-position container
// is NOT the direct parent of the fixed-position layer, and the hierarchy has various
// transforms that have to be processed in the correct order.
scoped_ptr<LayerImpl> root = createTreeForFixedPositionTests();
LayerImpl* child = root->children()[0];
LayerImpl* grandChild = child->children()[0];
LayerImpl* greatGrandChild = grandChild->children()[0];
WebTransformationMatrix rotationAboutZ;
rotationAboutZ.rotate3d(0, 0, 90);
child->setIsContainerForFixedPositionLayers(true);
child->setTransform(rotationAboutZ);
grandChild->setPosition(gfx::PointF(8, 6));
grandChild->setTransform(rotationAboutZ);
greatGrandChild->setFixedToContainerLayer(true); // greatGrandChild is positioned upside-down with respect to the renderTarget.
// Case 1: scrollDelta of 0, 0
child->setScrollDelta(gfx::Vector2d(0, 0));
executeCalculateDrawTransformsAndVisibility(root.get());
WebTransformationMatrix expectedChildTransform;
expectedChildTransform.multiply(rotationAboutZ);
WebTransformationMatrix expectedGrandChildTransform;
expectedGrandChildTransform.multiply(rotationAboutZ); // child's local transform is inherited
expectedGrandChildTransform.translate(8, 6); // translation because of position occurs before layer's local transform.
expectedGrandChildTransform.multiply(rotationAboutZ); // grandChild's local transform
WebTransformationMatrix expectedGreatGrandChildTransform = expectedGrandChildTransform;
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedChildTransform, child->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedGrandChildTransform, grandChild->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedGreatGrandChildTransform, greatGrandChild->drawTransform());
// Case 2: scrollDelta of 10, 20
child->setScrollDelta(gfx::Vector2d(10, 20));
executeCalculateDrawTransformsAndVisibility(root.get());
// Here the child and grandChild are affected by scrollDelta, but the fixed position greatGrandChild should not be affected.
expectedChildTransform.makeIdentity();
expectedChildTransform.translate(-10, -20); // scrollDelta
expectedChildTransform.multiply(rotationAboutZ);
expectedGrandChildTransform.makeIdentity();
expectedGrandChildTransform.translate(-10, -20); // child's scrollDelta is inherited
expectedGrandChildTransform.multiply(rotationAboutZ); // child's local transform is inherited
expectedGrandChildTransform.translate(8, 6); // translation because of position occurs before layer's local transform.
expectedGrandChildTransform.multiply(rotationAboutZ); // grandChild's local transform
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedChildTransform, child->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedGrandChildTransform, grandChild->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedGreatGrandChildTransform, greatGrandChild->drawTransform());
}
TEST(LayerTreeHostCommonTest, verifyScrollCompensationForFixedPositionLayerWithMultipleScrollDeltas)
{
// This test checks for correct scroll compensation when the fixed-position container
// has multiple ancestors that have nonzero scrollDelta before reaching the space where the layer is fixed.
// In this test, each scrollDelta occurs in a different space because of each layer's local transform.
// This test checks for correct scroll compensation when the fixed-position container
// is NOT the direct parent of the fixed-position layer, and the hierarchy has various
// transforms that have to be processed in the correct order.
scoped_ptr<LayerImpl> root = createTreeForFixedPositionTests();
LayerImpl* child = root->children()[0];
LayerImpl* grandChild = child->children()[0];
LayerImpl* greatGrandChild = grandChild->children()[0];
WebTransformationMatrix rotationAboutZ;
rotationAboutZ.rotate3d(0, 0, 90);
child->setIsContainerForFixedPositionLayers(true);
child->setTransform(rotationAboutZ);
grandChild->setPosition(gfx::PointF(8, 6));
grandChild->setTransform(rotationAboutZ);
greatGrandChild->setFixedToContainerLayer(true); // greatGrandChild is positioned upside-down with respect to the renderTarget.
// Case 1: scrollDelta of 0, 0
child->setScrollDelta(gfx::Vector2d(0, 0));
executeCalculateDrawTransformsAndVisibility(root.get());
WebTransformationMatrix expectedChildTransform;
expectedChildTransform.multiply(rotationAboutZ);
WebTransformationMatrix expectedGrandChildTransform;
expectedGrandChildTransform.multiply(rotationAboutZ); // child's local transform is inherited
expectedGrandChildTransform.translate(8, 6); // translation because of position occurs before layer's local transform.
expectedGrandChildTransform.multiply(rotationAboutZ); // grandChild's local transform
WebTransformationMatrix expectedGreatGrandChildTransform = expectedGrandChildTransform;
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedChildTransform, child->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedGrandChildTransform, grandChild->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedGreatGrandChildTransform, greatGrandChild->drawTransform());
// Case 2: scrollDelta of 10, 20
child->setScrollDelta(gfx::Vector2d(10, 0));
grandChild->setScrollDelta(gfx::Vector2d(5, 0));
executeCalculateDrawTransformsAndVisibility(root.get());
// Here the child and grandChild are affected by scrollDelta, but the fixed position greatGrandChild should not be affected.
expectedChildTransform.makeIdentity();
expectedChildTransform.translate(-10, 0); // scrollDelta
expectedChildTransform.multiply(rotationAboutZ);
expectedGrandChildTransform.makeIdentity();
expectedGrandChildTransform.translate(-10, 0); // child's scrollDelta is inherited
expectedGrandChildTransform.multiply(rotationAboutZ); // child's local transform is inherited
expectedGrandChildTransform.translate(-5, 0); // grandChild's scrollDelta
expectedGrandChildTransform.translate(8, 6); // translation because of position occurs before layer's local transform.
expectedGrandChildTransform.multiply(rotationAboutZ); // grandChild's local transform
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedChildTransform, child->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedGrandChildTransform, grandChild->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedGreatGrandChildTransform, greatGrandChild->drawTransform());
}
TEST(LayerTreeHostCommonTest, verifyScrollCompensationForFixedPositionLayerWithIntermediateSurfaceAndTransforms)
{
// This test checks for correct scroll compensation when the fixed-position container
// contributes to a different renderSurface than the fixed-position layer. In this
// case, the surface drawTransforms also have to be accounted for when checking the
// scrollDelta.
scoped_ptr<LayerImpl> root = createTreeForFixedPositionTests();
LayerImpl* child = root->children()[0];
LayerImpl* grandChild = child->children()[0];
LayerImpl* greatGrandChild = grandChild->children()[0];
child->setIsContainerForFixedPositionLayers(true);
grandChild->setPosition(gfx::PointF(8, 6));
grandChild->setForceRenderSurface(true);
greatGrandChild->setFixedToContainerLayer(true);
greatGrandChild->setDrawsContent(true);
WebTransformationMatrix rotationAboutZ;
rotationAboutZ.rotate3d(0, 0, 90);
grandChild->setTransform(rotationAboutZ);
// Case 1: scrollDelta of 0, 0
child->setScrollDelta(gfx::Vector2d(0, 0));
executeCalculateDrawTransformsAndVisibility(root.get());
WebTransformationMatrix expectedChildTransform;
WebTransformationMatrix expectedSurfaceDrawTransform;
expectedSurfaceDrawTransform.translate(8, 6);
expectedSurfaceDrawTransform.multiply(rotationAboutZ);
WebTransformationMatrix expectedGrandChildTransform;
WebTransformationMatrix expectedGreatGrandChildTransform;
ASSERT_TRUE(grandChild->renderSurface());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedChildTransform, child->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedSurfaceDrawTransform, grandChild->renderSurface()->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedGrandChildTransform, grandChild->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedGreatGrandChildTransform, greatGrandChild->drawTransform());
// Case 2: scrollDelta of 10, 30
child->setScrollDelta(gfx::Vector2d(10, 30));
executeCalculateDrawTransformsAndVisibility(root.get());
// Here the grandChild remains unchanged, because it scrolls along with the
// renderSurface, and the translation is actually in the renderSurface. But, the fixed
// position greatGrandChild is more awkward: its actually being drawn with respect to
// the renderSurface, but it needs to remain fixed with resepct to a container beyond
// that surface. So, the net result is that, unlike previous tests where the fixed
// position layer's transform remains unchanged, here the fixed position layer's
// transform explicitly contains the translation that cancels out the scroll.
expectedChildTransform.makeIdentity();
expectedChildTransform.translate(-10, -30); // scrollDelta
expectedSurfaceDrawTransform.makeIdentity();
expectedSurfaceDrawTransform.translate(-10, -30); // scrollDelta
expectedSurfaceDrawTransform.translate(8, 6);
expectedSurfaceDrawTransform.multiply(rotationAboutZ);
// The rotation and its inverse are needed to place the scrollDelta compensation in
// the correct space. This test will fail if the rotation/inverse are backwards, too,
// so it requires perfect order of operations.
expectedGreatGrandChildTransform.makeIdentity();
expectedGreatGrandChildTransform.multiply(rotationAboutZ.inverse());
expectedGreatGrandChildTransform.translate(10, 30); // explicit canceling out the scrollDelta that gets embedded in the fixed position layer's surface.
expectedGreatGrandChildTransform.multiply(rotationAboutZ);
ASSERT_TRUE(grandChild->renderSurface());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedChildTransform, child->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedSurfaceDrawTransform, grandChild->renderSurface()->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedGrandChildTransform, grandChild->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedGreatGrandChildTransform, greatGrandChild->drawTransform());
}
TEST(LayerTreeHostCommonTest, verifyScrollCompensationForFixedPositionLayerWithMultipleIntermediateSurfaces)
{
// This test checks for correct scroll compensation when the fixed-position container
// contributes to a different renderSurface than the fixed-position layer, with
// additional renderSurfaces in-between. This checks that the conversion to ancestor
// surfaces is accumulated properly in the final matrix transform.
scoped_ptr<LayerImpl> root = createTreeForFixedPositionTests();
LayerImpl* child = root->children()[0];
LayerImpl* grandChild = child->children()[0];
LayerImpl* greatGrandChild = grandChild->children()[0];
// Add one more layer to the test tree for this scenario.
{
WebTransformationMatrix identity;
scoped_ptr<LayerImpl> fixedPositionChild = LayerImpl::create(5);
setLayerPropertiesForTesting(fixedPositionChild.get(), identity, identity, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(100, 100), false);
greatGrandChild->addChild(fixedPositionChild.Pass());
}
LayerImpl* fixedPositionChild = greatGrandChild->children()[0];
// Actually set up the scenario here.
child->setIsContainerForFixedPositionLayers(true);
grandChild->setPosition(gfx::PointF(8, 6));
grandChild->setForceRenderSurface(true);
greatGrandChild->setPosition(gfx::PointF(40, 60));
greatGrandChild->setForceRenderSurface(true);
fixedPositionChild->setFixedToContainerLayer(true);
fixedPositionChild->setDrawsContent(true);
// The additional rotations, which are non-commutative with translations, help to
// verify that we have correct order-of-operations in the final scroll compensation.
// Note that rotating about the center of the layer ensures we do not accidentally
// clip away layers that we want to test.
WebTransformationMatrix rotationAboutZ;
rotationAboutZ.translate(50, 50);
rotationAboutZ.rotate3d(0, 0, 90);
rotationAboutZ.translate(-50, -50);
grandChild->setTransform(rotationAboutZ);
greatGrandChild->setTransform(rotationAboutZ);
// Case 1: scrollDelta of 0, 0
child->setScrollDelta(gfx::Vector2d(0, 0));
executeCalculateDrawTransformsAndVisibility(root.get());
WebTransformationMatrix expectedChildTransform;
WebTransformationMatrix expectedGrandChildSurfaceDrawTransform;
expectedGrandChildSurfaceDrawTransform.translate(8, 6);
expectedGrandChildSurfaceDrawTransform.multiply(rotationAboutZ);
WebTransformationMatrix expectedGrandChildTransform;
WebTransformationMatrix expectedGreatGrandChildSurfaceDrawTransform;
expectedGreatGrandChildSurfaceDrawTransform.translate(40, 60);
expectedGreatGrandChildSurfaceDrawTransform.multiply(rotationAboutZ);
WebTransformationMatrix expectedGreatGrandChildTransform;
WebTransformationMatrix expectedFixedPositionChildTransform;
ASSERT_TRUE(grandChild->renderSurface());
ASSERT_TRUE(greatGrandChild->renderSurface());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedChildTransform, child->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedGrandChildSurfaceDrawTransform, grandChild->renderSurface()->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedGrandChildTransform, grandChild->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedGreatGrandChildSurfaceDrawTransform, greatGrandChild->renderSurface()->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedGreatGrandChildTransform, greatGrandChild->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedFixedPositionChildTransform, fixedPositionChild->drawTransform());
// Case 2: scrollDelta of 10, 30
child->setScrollDelta(gfx::Vector2d(10, 30));
executeCalculateDrawTransformsAndVisibility(root.get());
expectedChildTransform.makeIdentity();
expectedChildTransform.translate(-10, -30); // scrollDelta
expectedGrandChildSurfaceDrawTransform.makeIdentity();
expectedGrandChildSurfaceDrawTransform.translate(-10, -30); // scrollDelta
expectedGrandChildSurfaceDrawTransform.translate(8, 6);
expectedGrandChildSurfaceDrawTransform.multiply(rotationAboutZ);
// grandChild, greatGrandChild, and greatGrandChild's surface are not expected to
// change, since they are all not fixed, and they are all drawn with respect to
// grandChild's surface that already has the scrollDelta accounted for.
// But the great-great grandchild, "fixedPositionChild", should have a transform that explicitly cancels out the scrollDelta.
// The expected transform is:
// compoundDrawTransform.inverse() * translate(positive scrollDelta) * compoundOriginTransform
WebTransformationMatrix compoundDrawTransform; // transform from greatGrandChildSurface's origin to the root surface.
compoundDrawTransform.translate(8, 6); // origin translation of grandChild
compoundDrawTransform.multiply(rotationAboutZ); // rotation of grandChild
compoundDrawTransform.translate(40, 60); // origin translation of greatGrandChild
compoundDrawTransform.multiply(rotationAboutZ); // rotation of greatGrandChild
expectedFixedPositionChildTransform.makeIdentity();
expectedFixedPositionChildTransform.multiply(compoundDrawTransform.inverse());
expectedFixedPositionChildTransform.translate(10, 30); // explicit canceling out the scrollDelta that gets embedded in the fixed position layer's surface.
expectedFixedPositionChildTransform.multiply(compoundDrawTransform);
ASSERT_TRUE(grandChild->renderSurface());
ASSERT_TRUE(greatGrandChild->renderSurface());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedChildTransform, child->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedGrandChildSurfaceDrawTransform, grandChild->renderSurface()->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedGrandChildTransform, grandChild->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedGreatGrandChildSurfaceDrawTransform, greatGrandChild->renderSurface()->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedGreatGrandChildTransform, greatGrandChild->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedFixedPositionChildTransform, fixedPositionChild->drawTransform());
}
TEST(LayerTreeHostCommonTest, verifyScrollCompensationForFixedPositionLayerWithContainerLayerThatHasSurface)
{
// This test checks for correct scroll compensation when the fixed-position container
// itself has a renderSurface. In this case, the container layer should be treated
// like a layer that contributes to a renderTarget, and that renderTarget
// is completely irrelevant; it should not affect the scroll compensation.
scoped_ptr<LayerImpl> root = createTreeForFixedPositionTests();
LayerImpl* child = root->children()[0];
LayerImpl* grandChild = child->children()[0];
child->setIsContainerForFixedPositionLayers(true);
child->setForceRenderSurface(true);
grandChild->setFixedToContainerLayer(true);
grandChild->setDrawsContent(true);
// Case 1: scrollDelta of 0, 0
child->setScrollDelta(gfx::Vector2d(0, 0));
executeCalculateDrawTransformsAndVisibility(root.get());
WebTransformationMatrix expectedSurfaceDrawTransform;
expectedSurfaceDrawTransform.translate(0, 0);
WebTransformationMatrix expectedChildTransform;
WebTransformationMatrix expectedGrandChildTransform;
ASSERT_TRUE(child->renderSurface());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedSurfaceDrawTransform, child->renderSurface()->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedChildTransform, child->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedGrandChildTransform, grandChild->drawTransform());
// Case 2: scrollDelta of 10, 10
child->setScrollDelta(gfx::Vector2d(10, 10));
executeCalculateDrawTransformsAndVisibility(root.get());
// The surface is translated by scrollDelta, the child transform doesn't change
// because it scrolls along with the surface, but the fixed position grandChild
// needs to compensate for the scroll translation.
expectedSurfaceDrawTransform.makeIdentity();
expectedSurfaceDrawTransform.translate(-10, -10);
expectedGrandChildTransform.makeIdentity();
expectedGrandChildTransform.translate(10, 10);
ASSERT_TRUE(child->renderSurface());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedSurfaceDrawTransform, child->renderSurface()->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedChildTransform, child->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedGrandChildTransform, grandChild->drawTransform());
}
TEST(LayerTreeHostCommonTest, verifyScrollCompensationForFixedPositionLayerThatIsAlsoFixedPositionContainer)
{
// This test checks the scenario where a fixed-position layer also happens to be a
// container itself for a descendant fixed position layer. In particular, the layer
// should not accidentally be fixed to itself.
scoped_ptr<LayerImpl> root = createTreeForFixedPositionTests();
LayerImpl* child = root->children()[0];
LayerImpl* grandChild = child->children()[0];
child->setIsContainerForFixedPositionLayers(true);
grandChild->setFixedToContainerLayer(true);
// This should not confuse the grandChild. If correct, the grandChild would still be considered fixed to its container (i.e. "child").
grandChild->setIsContainerForFixedPositionLayers(true);
// Case 1: scrollDelta of 0, 0
child->setScrollDelta(gfx::Vector2d(0, 0));
executeCalculateDrawTransformsAndVisibility(root.get());
WebTransformationMatrix expectedChildTransform;
WebTransformationMatrix expectedGrandChildTransform;
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedChildTransform, child->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedGrandChildTransform, grandChild->drawTransform());
// Case 2: scrollDelta of 10, 10
child->setScrollDelta(gfx::Vector2d(10, 10));
executeCalculateDrawTransformsAndVisibility(root.get());
// Here the child is affected by scrollDelta, but the fixed position grandChild should not be affected.
expectedChildTransform.makeIdentity();
expectedChildTransform.translate(-10, -10);
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedChildTransform, child->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedGrandChildTransform, grandChild->drawTransform());
}
TEST(LayerTreeHostCommonTest, verifyScrollCompensationForFixedPositionLayerThatHasNoContainer)
{
// This test checks scroll compensation when a fixed-position layer does not find any
// ancestor that is a "containerForFixedPositionLayers". In this situation, the layer should
// be fixed to the viewport -- not the rootLayer, which may have transforms of its own.
scoped_ptr<LayerImpl> root = createTreeForFixedPositionTests();
LayerImpl* child = root->children()[0];
LayerImpl* grandChild = child->children()[0];
WebTransformationMatrix rotationByZ;
rotationByZ.rotate3d(0, 0, 90);
root->setTransform(rotationByZ);
grandChild->setFixedToContainerLayer(true);
// Case 1: root scrollDelta of 0, 0
root->setScrollDelta(gfx::Vector2d(0, 0));
executeCalculateDrawTransformsAndVisibility(root.get());
WebTransformationMatrix identityMatrix;
EXPECT_TRANSFORMATION_MATRIX_EQ(identityMatrix, child->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(identityMatrix, grandChild->drawTransform());
// Case 2: root scrollDelta of 10, 10
root->setScrollDelta(gfx::Vector2d(10, 20));
executeCalculateDrawTransformsAndVisibility(root.get());
// The child is affected by scrollDelta, but it is already implcitly accounted for by
// the child's target surface (i.e. the root renderSurface). The grandChild is not
// affected by the scrollDelta, so its drawTransform needs to explicitly
// inverse-compensate for the scroll that's embedded in the target surface.
WebTransformationMatrix expectedGrandChildTransform;
expectedGrandChildTransform.multiply(rotationByZ.inverse());
expectedGrandChildTransform.translate(10, 20); // explicit canceling out the scrollDelta that gets embedded in the fixed position layer's surface.
expectedGrandChildTransform.multiply(rotationByZ);
EXPECT_TRANSFORMATION_MATRIX_EQ(identityMatrix, child->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedGrandChildTransform, grandChild->drawTransform());
}
TEST(LayerTreeHostCommonTest, verifyClipRectCullsRenderSurfaces)
{
// The entire subtree of layers that are outside the clipRect should be culled away,
// and should not affect the renderSurfaceLayerList.
//
// The test tree is set up as follows:
// - all layers except the leafNodes are forced to be a new renderSurface that have something to draw.
// - parent is a large container layer.
// - child has masksToBounds=true to cause clipping.
// - grandChild is positioned outside of the child's bounds
// - greatGrandChild is also kept outside child's bounds.
//
// In this configuration, grandChild and greatGrandChild are completely outside the
// clipRect, and they should never get scheduled on the list of renderSurfaces.
//
const WebTransformationMatrix identityMatrix;
scoped_refptr<Layer> parent = Layer::create();
scoped_refptr<Layer> child = Layer::create();
scoped_refptr<Layer> grandChild = Layer::create();
scoped_refptr<Layer> greatGrandChild = Layer::create();
scoped_refptr<LayerWithForcedDrawsContent> leafNode1 = make_scoped_refptr(new LayerWithForcedDrawsContent());
scoped_refptr<LayerWithForcedDrawsContent> leafNode2 = make_scoped_refptr(new LayerWithForcedDrawsContent());
parent->addChild(child);
child->addChild(grandChild);
grandChild->addChild(greatGrandChild);
// leafNode1 ensures that parent and child are kept on the renderSurfaceLayerList,
// even though grandChild and greatGrandChild should be clipped.
child->addChild(leafNode1);
greatGrandChild->addChild(leafNode2);
setLayerPropertiesForTesting(parent.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(500, 500), false);
setLayerPropertiesForTesting(child.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(20, 20), false);
setLayerPropertiesForTesting(grandChild.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(45, 45), gfx::Size(10, 10), false);
setLayerPropertiesForTesting(greatGrandChild.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(10, 10), false);
setLayerPropertiesForTesting(leafNode1.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(500, 500), false);
setLayerPropertiesForTesting(leafNode2.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(20, 20), false);
child->setMasksToBounds(true);
child->setOpacity(0.4f);
grandChild->setOpacity(0.5);
greatGrandChild->setOpacity(0.4f);
std::vector<scoped_refptr<Layer> > renderSurfaceLayerList;
int dummyMaxTextureSize = 512;
LayerTreeHostCommon::calculateDrawTransforms(parent.get(), parent->bounds(), 1, 1, dummyMaxTextureSize, renderSurfaceLayerList);
ASSERT_EQ(2U, renderSurfaceLayerList.size());
EXPECT_EQ(parent->id(), renderSurfaceLayerList[0]->id());
EXPECT_EQ(child->id(), renderSurfaceLayerList[1]->id());
}
TEST(LayerTreeHostCommonTest, verifyClipRectCullsSurfaceWithoutVisibleContent)
{
// When a renderSurface has a clipRect, it is used to clip the contentRect
// of the surface. When the renderSurface is animating its transforms, then
// the contentRect's position in the clipRect is not defined on the main
// thread, and its contentRect should not be clipped.
// The test tree is set up as follows:
// - parent is a container layer that masksToBounds=true to cause clipping.
// - child is a renderSurface, which has a clipRect set to the bounds of the parent.
// - grandChild is a renderSurface, and the only visible content in child. It is positioned outside of the clipRect from parent.
// In this configuration, grandChild should be outside the clipped
// contentRect of the child, making grandChild not appear in the
// renderSurfaceLayerList. However, when we place an animation on the child,
// this clipping should be avoided and we should keep the grandChild
// in the renderSurfaceLayerList.
const WebTransformationMatrix identityMatrix;
scoped_refptr<Layer> parent = Layer::create();
scoped_refptr<Layer> child = Layer::create();
scoped_refptr<Layer> grandChild = Layer::create();
scoped_refptr<LayerWithForcedDrawsContent> leafNode = make_scoped_refptr(new LayerWithForcedDrawsContent());
parent->addChild(child);
child->addChild(grandChild);
grandChild->addChild(leafNode);
setLayerPropertiesForTesting(parent.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(100, 100), false);
setLayerPropertiesForTesting(child.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(20, 20), false);
setLayerPropertiesForTesting(grandChild.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(200, 200), gfx::Size(10, 10), false);
setLayerPropertiesForTesting(leafNode.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(10, 10), false);
parent->setMasksToBounds(true);
child->setOpacity(0.4f);
grandChild->setOpacity(0.4f);
std::vector<scoped_refptr<Layer> > renderSurfaceLayerList;
int dummyMaxTextureSize = 512;
LayerTreeHostCommon::calculateDrawTransforms(parent.get(), parent->bounds(), 1, 1, dummyMaxTextureSize, renderSurfaceLayerList);
// Without an animation, we should cull child and grandChild from the renderSurfaceLayerList.
ASSERT_EQ(1U, renderSurfaceLayerList.size());
EXPECT_EQ(parent->id(), renderSurfaceLayerList[0]->id());
// Now put an animating transform on child.
addAnimatedTransformToController(*child->layerAnimationController(), 10, 30, 0);
parent->clearRenderSurface();
child->clearRenderSurface();
grandChild->clearRenderSurface();
renderSurfaceLayerList.clear();
LayerTreeHostCommon::calculateDrawTransforms(parent.get(), parent->bounds(), 1, 1, dummyMaxTextureSize, renderSurfaceLayerList);
// With an animating transform, we should keep child and grandChild in the renderSurfaceLayerList.
ASSERT_EQ(3U, renderSurfaceLayerList.size());
EXPECT_EQ(parent->id(), renderSurfaceLayerList[0]->id());
EXPECT_EQ(child->id(), renderSurfaceLayerList[1]->id());
EXPECT_EQ(grandChild->id(), renderSurfaceLayerList[2]->id());
}
TEST(LayerTreeHostCommonTest, verifyDrawableContentRectForLayers)
{
// Verify that layers get the appropriate drawableContentRect when their parent masksToBounds is true.
//
// grandChild1 - completely inside the region; drawableContentRect should be the layer rect expressed in target space.
// grandChild2 - partially clipped but NOT masksToBounds; the clipRect will be the intersection of layerBounds and the mask region.
// grandChild3 - partially clipped and masksToBounds; the drawableContentRect will still be the intersection of layerBounds and the mask region.
// grandChild4 - outside parent's clipRect; the drawableContentRect should be empty.
//
const WebTransformationMatrix identityMatrix;
scoped_refptr<Layer> parent = Layer::create();
scoped_refptr<Layer> child = Layer::create();
scoped_refptr<Layer> grandChild1 = Layer::create();
scoped_refptr<Layer> grandChild2 = Layer::create();
scoped_refptr<Layer> grandChild3 = Layer::create();
scoped_refptr<Layer> grandChild4 = Layer::create();
parent->addChild(child);
child->addChild(grandChild1);
child->addChild(grandChild2);
child->addChild(grandChild3);
child->addChild(grandChild4);
setLayerPropertiesForTesting(parent.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(500, 500), false);
setLayerPropertiesForTesting(child.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(20, 20), false);
setLayerPropertiesForTesting(grandChild1.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(5, 5), gfx::Size(10, 10), false);
setLayerPropertiesForTesting(grandChild2.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(15, 15), gfx::Size(10, 10), false);
setLayerPropertiesForTesting(grandChild3.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(15, 15), gfx::Size(10, 10), false);
setLayerPropertiesForTesting(grandChild4.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(45, 45), gfx::Size(10, 10), false);
child->setMasksToBounds(true);
grandChild3->setMasksToBounds(true);
// Force everyone to be a render surface.
child->setOpacity(0.4f);
grandChild1->setOpacity(0.5);
grandChild2->setOpacity(0.5);
grandChild3->setOpacity(0.5);
grandChild4->setOpacity(0.5);
std::vector<scoped_refptr<Layer> > renderSurfaceLayerList;
int dummyMaxTextureSize = 512;
LayerTreeHostCommon::calculateDrawTransforms(parent.get(), parent->bounds(), 1, 1, dummyMaxTextureSize, renderSurfaceLayerList);
EXPECT_RECT_EQ(gfx::Rect(gfx::Point(5, 5), gfx::Size(10, 10)), grandChild1->drawableContentRect());
EXPECT_RECT_EQ(gfx::Rect(gfx::Point(15, 15), gfx::Size(5, 5)), grandChild3->drawableContentRect());
EXPECT_RECT_EQ(gfx::Rect(gfx::Point(15, 15), gfx::Size(5, 5)), grandChild3->drawableContentRect());
EXPECT_TRUE(grandChild4->drawableContentRect().IsEmpty());
}
TEST(LayerTreeHostCommonTest, verifyClipRectIsPropagatedCorrectlyToSurfaces)
{
// Verify that renderSurfaces (and their layers) get the appropriate clipRects when their parent masksToBounds is true.
//
// Layers that own renderSurfaces (at least for now) do not inherit any clipping;
// instead the surface will enforce the clip for the entire subtree. They may still
// have a clipRect of their own layer bounds, however, if masksToBounds was true.
//
const WebTransformationMatrix identityMatrix;
scoped_refptr<Layer> parent = Layer::create();
scoped_refptr<Layer> child = Layer::create();
scoped_refptr<Layer> grandChild1 = Layer::create();
scoped_refptr<Layer> grandChild2 = Layer::create();
scoped_refptr<Layer> grandChild3 = Layer::create();
scoped_refptr<Layer> grandChild4 = Layer::create();
scoped_refptr<LayerWithForcedDrawsContent> leafNode1 = make_scoped_refptr(new LayerWithForcedDrawsContent());
scoped_refptr<LayerWithForcedDrawsContent> leafNode2 = make_scoped_refptr(new LayerWithForcedDrawsContent());
scoped_refptr<LayerWithForcedDrawsContent> leafNode3 = make_scoped_refptr(new LayerWithForcedDrawsContent());
scoped_refptr<LayerWithForcedDrawsContent> leafNode4 = make_scoped_refptr(new LayerWithForcedDrawsContent());
parent->addChild(child);
child->addChild(grandChild1);
child->addChild(grandChild2);
child->addChild(grandChild3);
child->addChild(grandChild4);
// the leaf nodes ensure that these grandChildren become renderSurfaces for this test.
grandChild1->addChild(leafNode1);
grandChild2->addChild(leafNode2);
grandChild3->addChild(leafNode3);
grandChild4->addChild(leafNode4);
setLayerPropertiesForTesting(parent.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(500, 500), false);
setLayerPropertiesForTesting(child.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(20, 20), false);
setLayerPropertiesForTesting(grandChild1.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(5, 5), gfx::Size(10, 10), false);
setLayerPropertiesForTesting(grandChild2.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(15, 15), gfx::Size(10, 10), false);
setLayerPropertiesForTesting(grandChild3.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(15, 15), gfx::Size(10, 10), false);
setLayerPropertiesForTesting(grandChild4.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(45, 45), gfx::Size(10, 10), false);
setLayerPropertiesForTesting(leafNode1.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(10, 10), false);
setLayerPropertiesForTesting(leafNode2.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(10, 10), false);
setLayerPropertiesForTesting(leafNode3.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(10, 10), false);
setLayerPropertiesForTesting(leafNode4.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(10, 10), false);
child->setMasksToBounds(true);
grandChild3->setMasksToBounds(true);
grandChild4->setMasksToBounds(true);
// Force everyone to be a render surface.
child->setOpacity(0.4f);
grandChild1->setOpacity(0.5);
grandChild2->setOpacity(0.5);
grandChild3->setOpacity(0.5);
grandChild4->setOpacity(0.5);
std::vector<scoped_refptr<Layer> > renderSurfaceLayerList;
int dummyMaxTextureSize = 512;
LayerTreeHostCommon::calculateDrawTransforms(parent.get(), parent->bounds(), 1, 1, dummyMaxTextureSize, renderSurfaceLayerList);
ASSERT_TRUE(grandChild1->renderSurface());
ASSERT_TRUE(grandChild2->renderSurface());
ASSERT_TRUE(grandChild3->renderSurface());
EXPECT_FALSE(grandChild4->renderSurface()); // Because grandChild4 is entirely clipped, it is expected to not have a renderSurface.
// Surfaces are clipped by their parent, but un-affected by the owning layer's masksToBounds.
EXPECT_RECT_EQ(gfx::Rect(gfx::Point(0, 0), gfx::Size(20, 20)), grandChild1->renderSurface()->clipRect());
EXPECT_RECT_EQ(gfx::Rect(gfx::Point(0, 0), gfx::Size(20, 20)), grandChild2->renderSurface()->clipRect());
EXPECT_RECT_EQ(gfx::Rect(gfx::Point(0, 0), gfx::Size(20, 20)), grandChild3->renderSurface()->clipRect());
}
TEST(LayerTreeHostCommonTest, verifyAnimationsForRenderSurfaceHierarchy)
{
scoped_refptr<Layer> parent = Layer::create();
scoped_refptr<Layer> renderSurface1 = Layer::create();
scoped_refptr<Layer> renderSurface2 = Layer::create();
scoped_refptr<Layer> childOfRoot = Layer::create();
scoped_refptr<Layer> childOfRS1 = Layer::create();
scoped_refptr<Layer> childOfRS2 = Layer::create();
scoped_refptr<Layer> grandChildOfRoot = Layer::create();
scoped_refptr<LayerWithForcedDrawsContent> grandChildOfRS1 = make_scoped_refptr(new LayerWithForcedDrawsContent());
scoped_refptr<LayerWithForcedDrawsContent> grandChildOfRS2 = make_scoped_refptr(new LayerWithForcedDrawsContent());
parent->addChild(renderSurface1);
parent->addChild(childOfRoot);
renderSurface1->addChild(childOfRS1);
renderSurface1->addChild(renderSurface2);
renderSurface2->addChild(childOfRS2);
childOfRoot->addChild(grandChildOfRoot);
childOfRS1->addChild(grandChildOfRS1);
childOfRS2->addChild(grandChildOfRS2);
// Make our render surfaces.
renderSurface1->setForceRenderSurface(true);
renderSurface2->setForceRenderSurface(true);
// Put an animated opacity on the render surface.
addOpacityTransitionToController(*renderSurface1->layerAnimationController(), 10, 1, 0, false);
// Also put an animated opacity on a layer without descendants.
addOpacityTransitionToController(*grandChildOfRoot->layerAnimationController(), 10, 1, 0, false);
WebTransformationMatrix layerTransform;
layerTransform.translate(1, 1);
WebTransformationMatrix sublayerTransform;
sublayerTransform.scale3d(10, 1, 1);
// Put a transform animation on the render surface.
addAnimatedTransformToController(*renderSurface2->layerAnimationController(), 10, 30, 0);
// Also put transform animations on grandChildOfRoot, and grandChildOfRS2
addAnimatedTransformToController(*grandChildOfRoot->layerAnimationController(), 10, 30, 0);
addAnimatedTransformToController(*grandChildOfRS2->layerAnimationController(), 10, 30, 0);
setLayerPropertiesForTesting(parent.get(), layerTransform, sublayerTransform, gfx::PointF(0.25, 0), gfx::PointF(2.5, 0), gfx::Size(10, 10), false);
setLayerPropertiesForTesting(renderSurface1.get(), layerTransform, sublayerTransform, gfx::PointF(0.25, 0), gfx::PointF(2.5, 0), gfx::Size(10, 10), false);
setLayerPropertiesForTesting(renderSurface2.get(), layerTransform, sublayerTransform, gfx::PointF(0.25, 0), gfx::PointF(2.5, 0), gfx::Size(10, 10), false);
setLayerPropertiesForTesting(childOfRoot.get(), layerTransform, sublayerTransform, gfx::PointF(0.25, 0), gfx::PointF(2.5, 0), gfx::Size(10, 10), false);
setLayerPropertiesForTesting(childOfRS1.get(), layerTransform, sublayerTransform, gfx::PointF(0.25, 0), gfx::PointF(2.5, 0), gfx::Size(10, 10), false);
setLayerPropertiesForTesting(childOfRS2.get(), layerTransform, sublayerTransform, gfx::PointF(0.25, 0), gfx::PointF(2.5, 0), gfx::Size(10, 10), false);
setLayerPropertiesForTesting(grandChildOfRoot.get(), layerTransform, sublayerTransform, gfx::PointF(0.25, 0), gfx::PointF(2.5, 0), gfx::Size(10, 10), false);
setLayerPropertiesForTesting(grandChildOfRS1.get(), layerTransform, sublayerTransform, gfx::PointF(0.25, 0), gfx::PointF(2.5, 0), gfx::Size(10, 10), false);
setLayerPropertiesForTesting(grandChildOfRS2.get(), layerTransform, sublayerTransform, gfx::PointF(0.25, 0), gfx::PointF(2.5, 0), gfx::Size(10, 10), false);
executeCalculateDrawTransformsAndVisibility(parent.get());
// Only layers that are associated with render surfaces should have an actual renderSurface() value.
//
ASSERT_TRUE(parent->renderSurface());
ASSERT_FALSE(childOfRoot->renderSurface());
ASSERT_FALSE(grandChildOfRoot->renderSurface());
ASSERT_TRUE(renderSurface1->renderSurface());
ASSERT_FALSE(childOfRS1->renderSurface());
ASSERT_FALSE(grandChildOfRS1->renderSurface());
ASSERT_TRUE(renderSurface2->renderSurface());
ASSERT_FALSE(childOfRS2->renderSurface());
ASSERT_FALSE(grandChildOfRS2->renderSurface());
// Verify all renderTarget accessors
//
EXPECT_EQ(parent, parent->renderTarget());
EXPECT_EQ(parent, childOfRoot->renderTarget());
EXPECT_EQ(parent, grandChildOfRoot->renderTarget());
EXPECT_EQ(renderSurface1, renderSurface1->renderTarget());
EXPECT_EQ(renderSurface1, childOfRS1->renderTarget());
EXPECT_EQ(renderSurface1, grandChildOfRS1->renderTarget());
EXPECT_EQ(renderSurface2, renderSurface2->renderTarget());
EXPECT_EQ(renderSurface2, childOfRS2->renderTarget());
EXPECT_EQ(renderSurface2, grandChildOfRS2->renderTarget());
// Verify drawOpacityIsAnimating values
//
EXPECT_FALSE(parent->drawOpacityIsAnimating());
EXPECT_FALSE(childOfRoot->drawOpacityIsAnimating());
EXPECT_TRUE(grandChildOfRoot->drawOpacityIsAnimating());
EXPECT_FALSE(renderSurface1->drawOpacityIsAnimating());
EXPECT_TRUE(renderSurface1->renderSurface()->drawOpacityIsAnimating());
EXPECT_FALSE(childOfRS1->drawOpacityIsAnimating());
EXPECT_FALSE(grandChildOfRS1->drawOpacityIsAnimating());
EXPECT_FALSE(renderSurface2->drawOpacityIsAnimating());
EXPECT_FALSE(renderSurface2->renderSurface()->drawOpacityIsAnimating());
EXPECT_FALSE(childOfRS2->drawOpacityIsAnimating());
EXPECT_FALSE(grandChildOfRS2->drawOpacityIsAnimating());
// Verify drawTransformsAnimatingInTarget values
//
EXPECT_FALSE(parent->drawTransformIsAnimating());
EXPECT_FALSE(childOfRoot->drawTransformIsAnimating());
EXPECT_TRUE(grandChildOfRoot->drawTransformIsAnimating());
EXPECT_FALSE(renderSurface1->drawTransformIsAnimating());
EXPECT_FALSE(renderSurface1->renderSurface()->targetSurfaceTransformsAreAnimating());
EXPECT_FALSE(childOfRS1->drawTransformIsAnimating());
EXPECT_FALSE(grandChildOfRS1->drawTransformIsAnimating());
EXPECT_FALSE(renderSurface2->drawTransformIsAnimating());
EXPECT_TRUE(renderSurface2->renderSurface()->targetSurfaceTransformsAreAnimating());
EXPECT_FALSE(childOfRS2->drawTransformIsAnimating());
EXPECT_TRUE(grandChildOfRS2->drawTransformIsAnimating());
// Verify drawTransformsAnimatingInScreen values
//
EXPECT_FALSE(parent->screenSpaceTransformIsAnimating());
EXPECT_FALSE(childOfRoot->screenSpaceTransformIsAnimating());
EXPECT_TRUE(grandChildOfRoot->screenSpaceTransformIsAnimating());
EXPECT_FALSE(renderSurface1->screenSpaceTransformIsAnimating());
EXPECT_FALSE(renderSurface1->renderSurface()->screenSpaceTransformsAreAnimating());
EXPECT_FALSE(childOfRS1->screenSpaceTransformIsAnimating());
EXPECT_FALSE(grandChildOfRS1->screenSpaceTransformIsAnimating());
EXPECT_TRUE(renderSurface2->screenSpaceTransformIsAnimating());
EXPECT_TRUE(renderSurface2->renderSurface()->screenSpaceTransformsAreAnimating());
EXPECT_TRUE(childOfRS2->screenSpaceTransformIsAnimating());
EXPECT_TRUE(grandChildOfRS2->screenSpaceTransformIsAnimating());
// Sanity check. If these fail there is probably a bug in the test itself.
// It is expected that we correctly set up transforms so that the y-component of the screen-space transform
// encodes the "depth" of the layer in the tree.
EXPECT_FLOAT_EQ(1, parent->screenSpaceTransform().m42());
EXPECT_FLOAT_EQ(2, childOfRoot->screenSpaceTransform().m42());
EXPECT_FLOAT_EQ(3, grandChildOfRoot->screenSpaceTransform().m42());
EXPECT_FLOAT_EQ(2, renderSurface1->screenSpaceTransform().m42());
EXPECT_FLOAT_EQ(3, childOfRS1->screenSpaceTransform().m42());
EXPECT_FLOAT_EQ(4, grandChildOfRS1->screenSpaceTransform().m42());
EXPECT_FLOAT_EQ(3, renderSurface2->screenSpaceTransform().m42());
EXPECT_FLOAT_EQ(4, childOfRS2->screenSpaceTransform().m42());
EXPECT_FLOAT_EQ(5, grandChildOfRS2->screenSpaceTransform().m42());
}
TEST(LayerTreeHostCommonTest, verifyVisibleRectForIdentityTransform)
{
// Test the calculateVisibleRect() function works correctly for identity transforms.
gfx::Rect targetSurfaceRect = gfx::Rect(gfx::Point(0, 0), gfx::Size(100, 100));
WebTransformationMatrix layerToSurfaceTransform;
// Case 1: Layer is contained within the surface.
gfx::Rect layerContentRect = gfx::Rect(gfx::Point(10, 10), gfx::Size(30, 30));
gfx::Rect expected = gfx::Rect(gfx::Point(10, 10), gfx::Size(30, 30));
gfx::Rect actual = LayerTreeHostCommon::calculateVisibleRect(targetSurfaceRect, layerContentRect, layerToSurfaceTransform);
EXPECT_RECT_EQ(expected, actual);
// Case 2: Layer is outside the surface rect.
layerContentRect = gfx::Rect(gfx::Point(120, 120), gfx::Size(30, 30));
actual = LayerTreeHostCommon::calculateVisibleRect(targetSurfaceRect, layerContentRect, layerToSurfaceTransform);
EXPECT_TRUE(actual.IsEmpty());
// Case 3: Layer is partially overlapping the surface rect.
layerContentRect = gfx::Rect(gfx::Point(80, 80), gfx::Size(30, 30));
expected = gfx::Rect(gfx::Point(80, 80), gfx::Size(20, 20));
actual = LayerTreeHostCommon::calculateVisibleRect(targetSurfaceRect, layerContentRect, layerToSurfaceTransform);
EXPECT_RECT_EQ(expected, actual);
}
TEST(LayerTreeHostCommonTest, verifyVisibleRectForTranslations)
{
// Test the calculateVisibleRect() function works correctly for scaling transforms.
gfx::Rect targetSurfaceRect = gfx::Rect(gfx::Point(0, 0), gfx::Size(100, 100));
gfx::Rect layerContentRect = gfx::Rect(gfx::Point(0, 0), gfx::Size(30, 30));
WebTransformationMatrix layerToSurfaceTransform;
// Case 1: Layer is contained within the surface.
layerToSurfaceTransform.makeIdentity();
layerToSurfaceTransform.translate(10, 10);
gfx::Rect expected = gfx::Rect(gfx::Point(0, 0), gfx::Size(30, 30));
gfx::Rect actual = LayerTreeHostCommon::calculateVisibleRect(targetSurfaceRect, layerContentRect, layerToSurfaceTransform);
EXPECT_RECT_EQ(expected, actual);
// Case 2: Layer is outside the surface rect.
layerToSurfaceTransform.makeIdentity();
layerToSurfaceTransform.translate(120, 120);
actual = LayerTreeHostCommon::calculateVisibleRect(targetSurfaceRect, layerContentRect, layerToSurfaceTransform);
EXPECT_TRUE(actual.IsEmpty());
// Case 3: Layer is partially overlapping the surface rect.
layerToSurfaceTransform.makeIdentity();
layerToSurfaceTransform.translate(80, 80);
expected = gfx::Rect(gfx::Point(0, 0), gfx::Size(20, 20));
actual = LayerTreeHostCommon::calculateVisibleRect(targetSurfaceRect, layerContentRect, layerToSurfaceTransform);
EXPECT_RECT_EQ(expected, actual);
}
TEST(LayerTreeHostCommonTest, verifyVisibleRectFor2DRotations)
{
// Test the calculateVisibleRect() function works correctly for rotations about z-axis (i.e. 2D rotations).
// Remember that calculateVisibleRect() should return the visible rect in the layer's space.
gfx::Rect targetSurfaceRect = gfx::Rect(gfx::Point(0, 0), gfx::Size(100, 100));
gfx::Rect layerContentRect = gfx::Rect(gfx::Point(0, 0), gfx::Size(30, 30));
WebTransformationMatrix layerToSurfaceTransform;
// Case 1: Layer is contained within the surface.
layerToSurfaceTransform.makeIdentity();
layerToSurfaceTransform.translate(50, 50);
layerToSurfaceTransform.rotate(45);
gfx::Rect expected = gfx::Rect(gfx::Point(0, 0), gfx::Size(30, 30));
gfx::Rect actual = LayerTreeHostCommon::calculateVisibleRect(targetSurfaceRect, layerContentRect, layerToSurfaceTransform);
EXPECT_RECT_EQ(expected, actual);
// Case 2: Layer is outside the surface rect.
layerToSurfaceTransform.makeIdentity();
layerToSurfaceTransform.translate(-50, 0);
layerToSurfaceTransform.rotate(45);
actual = LayerTreeHostCommon::calculateVisibleRect(targetSurfaceRect, layerContentRect, layerToSurfaceTransform);
EXPECT_TRUE(actual.IsEmpty());
// Case 3: The layer is rotated about its top-left corner. In surface space, the layer
// is oriented diagonally, with the left half outside of the renderSurface. In
// this case, the visible rect should still be the entire layer (remember the
// visible rect is computed in layer space); both the top-left and
// bottom-right corners of the layer are still visible.
layerToSurfaceTransform.makeIdentity();
layerToSurfaceTransform.rotate(45);
expected = gfx::Rect(gfx::Point(0, 0), gfx::Size(30, 30));
actual = LayerTreeHostCommon::calculateVisibleRect(targetSurfaceRect, layerContentRect, layerToSurfaceTransform);
EXPECT_RECT_EQ(expected, actual);
// Case 4: The layer is rotated about its top-left corner, and translated upwards. In
// surface space, the layer is oriented diagonally, with only the top corner
// of the surface overlapping the layer. In layer space, the render surface
// overlaps the right side of the layer. The visible rect should be the
// layer's right half.
layerToSurfaceTransform.makeIdentity();
layerToSurfaceTransform.translate(0, -sqrt(2.0) * 15);
layerToSurfaceTransform.rotate(45);
expected = gfx::Rect(gfx::Point(15, 0), gfx::Size(15, 30)); // right half of layer bounds.
actual = LayerTreeHostCommon::calculateVisibleRect(targetSurfaceRect, layerContentRect, layerToSurfaceTransform);
EXPECT_RECT_EQ(expected, actual);
}
TEST(LayerTreeHostCommonTest, verifyVisibleRectFor3dOrthographicTransform)
{
// Test that the calculateVisibleRect() function works correctly for 3d transforms.
gfx::Rect targetSurfaceRect = gfx::Rect(gfx::Point(0, 0), gfx::Size(100, 100));
gfx::Rect layerContentRect = gfx::Rect(gfx::Point(0, 0), gfx::Size(100, 100));
WebTransformationMatrix layerToSurfaceTransform;
// Case 1: Orthographic projection of a layer rotated about y-axis by 45 degrees, should be fully contained in the renderSurface.
layerToSurfaceTransform.makeIdentity();
layerToSurfaceTransform.rotate3d(0, 45, 0);
gfx::Rect expected = gfx::Rect(gfx::Point(0, 0), gfx::Size(100, 100));
gfx::Rect actual = LayerTreeHostCommon::calculateVisibleRect(targetSurfaceRect, layerContentRect, layerToSurfaceTransform);
EXPECT_RECT_EQ(expected, actual);
// Case 2: Orthographic projection of a layer rotated about y-axis by 45 degrees, but
// shifted to the side so only the right-half the layer would be visible on
// the surface.
double halfWidthOfRotatedLayer = (100 / sqrt(2.0)) * 0.5; // 100 is the un-rotated layer width; divided by sqrt(2) is the rotated width.
layerToSurfaceTransform.makeIdentity();
layerToSurfaceTransform.translate(-halfWidthOfRotatedLayer, 0);
layerToSurfaceTransform.rotate3d(0, 45, 0); // rotates about the left edge of the layer
expected = gfx::Rect(gfx::Point(50, 0), gfx::Size(50, 100)); // right half of the layer.
actual = LayerTreeHostCommon::calculateVisibleRect(targetSurfaceRect, layerContentRect, layerToSurfaceTransform);
EXPECT_RECT_EQ(expected, actual);
}
TEST(LayerTreeHostCommonTest, verifyVisibleRectFor3dPerspectiveTransform)
{
// Test the calculateVisibleRect() function works correctly when the layer has a
// perspective projection onto the target surface.
gfx::Rect targetSurfaceRect = gfx::Rect(gfx::Point(0, 0), gfx::Size(100, 100));
gfx::Rect layerContentRect = gfx::Rect(gfx::Point(-50, -50), gfx::Size(200, 200));
WebTransformationMatrix layerToSurfaceTransform;
// Case 1: Even though the layer is twice as large as the surface, due to perspective
// foreshortening, the layer will fit fully in the surface when its translated
// more than the perspective amount.
layerToSurfaceTransform.makeIdentity();
// The following sequence of transforms applies the perspective about the center of the surface.
layerToSurfaceTransform.translate(50, 50);
layerToSurfaceTransform.applyPerspective(9);
layerToSurfaceTransform.translate(-50, -50);
// This translate places the layer in front of the surface's projection plane.
layerToSurfaceTransform.translate3d(0, 0, -27);
gfx::Rect expected = gfx::Rect(gfx::Point(-50, -50), gfx::Size(200, 200));
gfx::Rect actual = LayerTreeHostCommon::calculateVisibleRect(targetSurfaceRect, layerContentRect, layerToSurfaceTransform);
EXPECT_RECT_EQ(expected, actual);
// Case 2: same projection as before, except that the layer is also translated to the
// side, so that only the right half of the layer should be visible.
//
// Explanation of expected result:
// The perspective ratio is (z distance between layer and camera origin) / (z distance between projection plane and camera origin) == ((-27 - 9) / 9)
// Then, by similar triangles, if we want to move a layer by translating -50 units in projected surface units (so that only half of it is
// visible), then we would need to translate by (-36 / 9) * -50 == -200 in the layer's units.
//
layerToSurfaceTransform.translate3d(-200, 0, 0);
expected = gfx::Rect(gfx::Point(50, -50), gfx::Size(100, 200)); // The right half of the layer's bounding rect.
actual = LayerTreeHostCommon::calculateVisibleRect(targetSurfaceRect, layerContentRect, layerToSurfaceTransform);
EXPECT_RECT_EQ(expected, actual);
}
TEST(LayerTreeHostCommonTest, verifyVisibleRectFor3dOrthographicIsNotClippedBehindSurface)
{
// There is currently no explicit concept of an orthographic projection plane in our
// code (nor in the CSS spec to my knowledge). Therefore, layers that are technically
// behind the surface in an orthographic world should not be clipped when they are
// flattened to the surface.
gfx::Rect targetSurfaceRect = gfx::Rect(gfx::Point(0, 0), gfx::Size(100, 100));
gfx::Rect layerContentRect = gfx::Rect(gfx::Point(0, 0), gfx::Size(100, 100));
WebTransformationMatrix layerToSurfaceTransform;
// This sequence of transforms effectively rotates the layer about the y-axis at the
// center of the layer.
layerToSurfaceTransform.makeIdentity();
layerToSurfaceTransform.translate(50, 0);
layerToSurfaceTransform.rotate3d(0, 45, 0);
layerToSurfaceTransform.translate(-50, 0);
gfx::Rect expected = gfx::Rect(gfx::Point(0, 0), gfx::Size(100, 100));
gfx::Rect actual = LayerTreeHostCommon::calculateVisibleRect(targetSurfaceRect, layerContentRect, layerToSurfaceTransform);
EXPECT_RECT_EQ(expected, actual);
}
TEST(LayerTreeHostCommonTest, verifyVisibleRectFor3dPerspectiveWhenClippedByW)
{
// Test the calculateVisibleRect() function works correctly when projecting a surface
// onto a layer, but the layer is partially behind the camera (not just behind the
// projection plane). In this case, the cartesian coordinates may seem to be valid,
// but actually they are not. The visibleRect needs to be properly clipped by the
// w = 0 plane in homogeneous coordinates before converting to cartesian coordinates.
gfx::Rect targetSurfaceRect = gfx::Rect(gfx::Point(-50, -50), gfx::Size(100, 100));
gfx::Rect layerContentRect = gfx::Rect(gfx::Point(-10, -1), gfx::Size(20, 2));
WebTransformationMatrix layerToSurfaceTransform;
// The layer is positioned so that the right half of the layer should be in front of
// the camera, while the other half is behind the surface's projection plane. The
// following sequence of transforms applies the perspective and rotation about the
// center of the layer.
layerToSurfaceTransform.makeIdentity();
layerToSurfaceTransform.applyPerspective(1);
layerToSurfaceTransform.translate3d(-2, 0, 1);
layerToSurfaceTransform.rotate3d(0, 45, 0);
// Sanity check that this transform does indeed cause w < 0 when applying the
// transform, otherwise this code is not testing the intended scenario.
bool clipped = false;
MathUtil::mapQuad(layerToSurfaceTransform, gfx::QuadF(gfx::RectF(layerContentRect)), clipped);
ASSERT_TRUE(clipped);
int expectedXPosition = 0;
int expectedWidth = 10;
gfx::Rect actual = LayerTreeHostCommon::calculateVisibleRect(targetSurfaceRect, layerContentRect, layerToSurfaceTransform);
EXPECT_EQ(expectedXPosition, actual.x());
EXPECT_EQ(expectedWidth, actual.width());
}
TEST(LayerTreeHostCommonTest, verifyVisibleRectForPerspectiveUnprojection)
{
// To determine visibleRect in layer space, there needs to be an un-projection from
// surface space to layer space. When the original transform was a perspective
// projection that was clipped, it returns a rect that encloses the clipped bounds.
// Un-projecting this new rect may require clipping again.
// This sequence of transforms causes one corner of the layer to protrude across the w = 0 plane, and should be clipped.
gfx::Rect targetSurfaceRect = gfx::Rect(gfx::Point(-50, -50), gfx::Size(100, 100));
gfx::Rect layerContentRect = gfx::Rect(gfx::Point(-10, -10), gfx::Size(20, 20));
WebTransformationMatrix layerToSurfaceTransform;
layerToSurfaceTransform.makeIdentity();
layerToSurfaceTransform.applyPerspective(1);
layerToSurfaceTransform.translate3d(0, 0, -5);
layerToSurfaceTransform.rotate3d(0, 45, 0);
layerToSurfaceTransform.rotate3d(80, 0, 0);
// Sanity check that un-projection does indeed cause w < 0, otherwise this code is not
// testing the intended scenario.
bool clipped = false;
gfx::RectF clippedRect = MathUtil::mapClippedRect(layerToSurfaceTransform, layerContentRect);
MathUtil::projectQuad(layerToSurfaceTransform.inverse(), gfx::QuadF(clippedRect), clipped);
ASSERT_TRUE(clipped);
// Only the corner of the layer is not visible on the surface because of being
// clipped. But, the net result of rounding visible region to an axis-aligned rect is
// that the entire layer should still be considered visible.
gfx::Rect expected = gfx::Rect(gfx::Point(-10, -10), gfx::Size(20, 20));
gfx::Rect actual = LayerTreeHostCommon::calculateVisibleRect(targetSurfaceRect, layerContentRect, layerToSurfaceTransform);
EXPECT_RECT_EQ(expected, actual);
}
TEST(LayerTreeHostCommonTest, verifyDrawableAndVisibleContentRectsForSimpleLayers)
{
scoped_refptr<Layer> root = Layer::create();
scoped_refptr<LayerWithForcedDrawsContent> child1 = make_scoped_refptr(new LayerWithForcedDrawsContent());
scoped_refptr<LayerWithForcedDrawsContent> child2 = make_scoped_refptr(new LayerWithForcedDrawsContent());
scoped_refptr<LayerWithForcedDrawsContent> child3 = make_scoped_refptr(new LayerWithForcedDrawsContent());
root->addChild(child1);
root->addChild(child2);
root->addChild(child3);
WebTransformationMatrix identityMatrix;
setLayerPropertiesForTesting(root.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(100, 100), false);
setLayerPropertiesForTesting(child1.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(50, 50), false);
setLayerPropertiesForTesting(child2.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(75, 75), gfx::Size(50, 50), false);
setLayerPropertiesForTesting(child3.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(125, 125), gfx::Size(50, 50), false);
executeCalculateDrawTransformsAndVisibility(root.get());
EXPECT_RECT_EQ(gfx::Rect(0, 0, 100, 100), root->renderSurface()->drawableContentRect());
EXPECT_RECT_EQ(gfx::Rect(0, 0, 100, 100), root->drawableContentRect());
// Layers that do not draw content should have empty visibleContentRects.
EXPECT_RECT_EQ(gfx::Rect(0, 0, 0, 0), root->visibleContentRect());
// layer visibleContentRects are clipped by their targetSurface
EXPECT_RECT_EQ(gfx::Rect(0, 0, 50, 50), child1->visibleContentRect());
EXPECT_RECT_EQ(gfx::Rect(0, 0, 25, 25), child2->visibleContentRect());
EXPECT_TRUE(child3->visibleContentRect().IsEmpty());
// layer drawableContentRects are not clipped.
EXPECT_RECT_EQ(gfx::Rect(0, 0, 50, 50), child1->drawableContentRect());
EXPECT_RECT_EQ(gfx::Rect(75, 75, 50, 50), child2->drawableContentRect());
EXPECT_RECT_EQ(gfx::Rect(125, 125, 50, 50), child3->drawableContentRect());
}
TEST(LayerTreeHostCommonTest, verifyDrawableAndVisibleContentRectsForLayersClippedByLayer)
{
scoped_refptr<Layer> root = Layer::create();
scoped_refptr<Layer> child = Layer::create();
scoped_refptr<LayerWithForcedDrawsContent> grandChild1 = make_scoped_refptr(new LayerWithForcedDrawsContent());
scoped_refptr<LayerWithForcedDrawsContent> grandChild2 = make_scoped_refptr(new LayerWithForcedDrawsContent());
scoped_refptr<LayerWithForcedDrawsContent> grandChild3 = make_scoped_refptr(new LayerWithForcedDrawsContent());
root->addChild(child);
child->addChild(grandChild1);
child->addChild(grandChild2);
child->addChild(grandChild3);
WebTransformationMatrix identityMatrix;
setLayerPropertiesForTesting(root.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(100, 100), false);
setLayerPropertiesForTesting(child.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(100, 100), false);
setLayerPropertiesForTesting(grandChild1.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(5, 5), gfx::Size(50, 50), false);
setLayerPropertiesForTesting(grandChild2.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(75, 75), gfx::Size(50, 50), false);
setLayerPropertiesForTesting(grandChild3.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(125, 125), gfx::Size(50, 50), false);
child->setMasksToBounds(true);
executeCalculateDrawTransformsAndVisibility(root.get());
ASSERT_FALSE(child->renderSurface());
EXPECT_RECT_EQ(gfx::Rect(0, 0, 100, 100), root->renderSurface()->drawableContentRect());
EXPECT_RECT_EQ(gfx::Rect(0, 0, 100, 100), root->drawableContentRect());
// Layers that do not draw content should have empty visibleContentRects.
EXPECT_RECT_EQ(gfx::Rect(0, 0, 0, 0), root->visibleContentRect());
EXPECT_RECT_EQ(gfx::Rect(0, 0, 0, 0), child->visibleContentRect());
// All grandchild visibleContentRects should be clipped by child.
EXPECT_RECT_EQ(gfx::Rect(0, 0, 50, 50), grandChild1->visibleContentRect());
EXPECT_RECT_EQ(gfx::Rect(0, 0, 25, 25), grandChild2->visibleContentRect());
EXPECT_TRUE(grandChild3->visibleContentRect().IsEmpty());
// All grandchild drawableContentRects should also be clipped by child.
EXPECT_RECT_EQ(gfx::Rect(5, 5, 50, 50), grandChild1->drawableContentRect());
EXPECT_RECT_EQ(gfx::Rect(75, 75, 25, 25), grandChild2->drawableContentRect());
EXPECT_TRUE(grandChild3->drawableContentRect().IsEmpty());
}
TEST(LayerTreeHostCommonTest, verifyDrawableAndVisibleContentRectsForLayersInUnclippedRenderSurface)
{
scoped_refptr<Layer> root = Layer::create();
scoped_refptr<Layer> renderSurface1 = Layer::create();
scoped_refptr<LayerWithForcedDrawsContent> child1 = make_scoped_refptr(new LayerWithForcedDrawsContent());
scoped_refptr<LayerWithForcedDrawsContent> child2 = make_scoped_refptr(new LayerWithForcedDrawsContent());
scoped_refptr<LayerWithForcedDrawsContent> child3 = make_scoped_refptr(new LayerWithForcedDrawsContent());
root->addChild(renderSurface1);
renderSurface1->addChild(child1);
renderSurface1->addChild(child2);
renderSurface1->addChild(child3);
WebTransformationMatrix identityMatrix;
setLayerPropertiesForTesting(root.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(100, 100), false);
setLayerPropertiesForTesting(renderSurface1.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(3, 4), false);
setLayerPropertiesForTesting(child1.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(5, 5), gfx::Size(50, 50), false);
setLayerPropertiesForTesting(child2.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(75, 75), gfx::Size(50, 50), false);
setLayerPropertiesForTesting(child3.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(125, 125), gfx::Size(50, 50), false);
renderSurface1->setForceRenderSurface(true);
executeCalculateDrawTransformsAndVisibility(root.get());
ASSERT_TRUE(renderSurface1->renderSurface());
EXPECT_RECT_EQ(gfx::Rect(0, 0, 100, 100), root->renderSurface()->drawableContentRect());
EXPECT_RECT_EQ(gfx::Rect(0, 0, 100, 100), root->drawableContentRect());
// Layers that do not draw content should have empty visibleContentRects.
EXPECT_RECT_EQ(gfx::Rect(0, 0, 0, 0), root->visibleContentRect());
EXPECT_RECT_EQ(gfx::Rect(0, 0, 0, 0), renderSurface1->visibleContentRect());
// An unclipped surface grows its drawableContentRect to include all drawable regions of the subtree.
EXPECT_RECT_EQ(gfx::Rect(5, 5, 170, 170), renderSurface1->renderSurface()->drawableContentRect());
// All layers that draw content into the unclipped surface are also unclipped.
EXPECT_RECT_EQ(gfx::Rect(0, 0, 50, 50), child1->visibleContentRect());
EXPECT_RECT_EQ(gfx::Rect(0, 0, 50, 50), child2->visibleContentRect());
EXPECT_RECT_EQ(gfx::Rect(0, 0, 50, 50), child3->visibleContentRect());
EXPECT_RECT_EQ(gfx::Rect(5, 5, 50, 50), child1->drawableContentRect());
EXPECT_RECT_EQ(gfx::Rect(75, 75, 50, 50), child2->drawableContentRect());
EXPECT_RECT_EQ(gfx::Rect(125, 125, 50, 50), child3->drawableContentRect());
}
TEST(LayerTreeHostCommonTest, verifyDrawableAndVisibleContentRectsForLayersInClippedRenderSurface)
{
scoped_refptr<Layer> root = Layer::create();
scoped_refptr<Layer> renderSurface1 = Layer::create();
scoped_refptr<LayerWithForcedDrawsContent> child1 = make_scoped_refptr(new LayerWithForcedDrawsContent());
scoped_refptr<LayerWithForcedDrawsContent> child2 = make_scoped_refptr(new LayerWithForcedDrawsContent());
scoped_refptr<LayerWithForcedDrawsContent> child3 = make_scoped_refptr(new LayerWithForcedDrawsContent());
root->addChild(renderSurface1);
renderSurface1->addChild(child1);
renderSurface1->addChild(child2);
renderSurface1->addChild(child3);
WebTransformationMatrix identityMatrix;
setLayerPropertiesForTesting(root.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(100, 100), false);
setLayerPropertiesForTesting(renderSurface1.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(3, 4), false);
setLayerPropertiesForTesting(child1.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(5, 5), gfx::Size(50, 50), false);
setLayerPropertiesForTesting(child2.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(75, 75), gfx::Size(50, 50), false);
setLayerPropertiesForTesting(child3.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(125, 125), gfx::Size(50, 50), false);
root->setMasksToBounds(true);
renderSurface1->setForceRenderSurface(true);
executeCalculateDrawTransformsAndVisibility(root.get());
ASSERT_TRUE(renderSurface1->renderSurface());
EXPECT_RECT_EQ(gfx::Rect(0, 0, 100, 100), root->renderSurface()->drawableContentRect());
EXPECT_RECT_EQ(gfx::Rect(0, 0, 100, 100), root->drawableContentRect());
// Layers that do not draw content should have empty visibleContentRects.
EXPECT_RECT_EQ(gfx::Rect(0, 0, 0, 0), root->visibleContentRect());
EXPECT_RECT_EQ(gfx::Rect(0, 0, 0, 0), renderSurface1->visibleContentRect());
// A clipped surface grows its drawableContentRect to include all drawable regions of the subtree,
// but also gets clamped by the ancestor's clip.
EXPECT_RECT_EQ(gfx::Rect(5, 5, 95, 95), renderSurface1->renderSurface()->drawableContentRect());
// All layers that draw content into the surface have their visibleContentRect clipped by the surface clipRect.
EXPECT_RECT_EQ(gfx::Rect(0, 0, 50, 50), child1->visibleContentRect());
EXPECT_RECT_EQ(gfx::Rect(0, 0, 25, 25), child2->visibleContentRect());
EXPECT_TRUE(child3->visibleContentRect().IsEmpty());
// But the drawableContentRects are unclipped.
EXPECT_RECT_EQ(gfx::Rect(5, 5, 50, 50), child1->drawableContentRect());
EXPECT_RECT_EQ(gfx::Rect(75, 75, 50, 50), child2->drawableContentRect());
EXPECT_RECT_EQ(gfx::Rect(125, 125, 50, 50), child3->drawableContentRect());
}
TEST(LayerTreeHostCommonTest, verifyDrawableAndVisibleContentRectsForSurfaceHierarchy)
{
// Check that clipping does not propagate down surfaces.
scoped_refptr<Layer> root = Layer::create();
scoped_refptr<Layer> renderSurface1 = Layer::create();
scoped_refptr<Layer> renderSurface2 = Layer::create();
scoped_refptr<LayerWithForcedDrawsContent> child1 = make_scoped_refptr(new LayerWithForcedDrawsContent());
scoped_refptr<LayerWithForcedDrawsContent> child2 = make_scoped_refptr(new LayerWithForcedDrawsContent());
scoped_refptr<LayerWithForcedDrawsContent> child3 = make_scoped_refptr(new LayerWithForcedDrawsContent());
root->addChild(renderSurface1);
renderSurface1->addChild(renderSurface2);
renderSurface2->addChild(child1);
renderSurface2->addChild(child2);
renderSurface2->addChild(child3);
WebTransformationMatrix identityMatrix;
setLayerPropertiesForTesting(root.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(100, 100), false);
setLayerPropertiesForTesting(renderSurface1.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(3, 4), false);
setLayerPropertiesForTesting(renderSurface2.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(7, 13), false);
setLayerPropertiesForTesting(child1.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(5, 5), gfx::Size(50, 50), false);
setLayerPropertiesForTesting(child2.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(75, 75), gfx::Size(50, 50), false);
setLayerPropertiesForTesting(child3.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(125, 125), gfx::Size(50, 50), false);
root->setMasksToBounds(true);
renderSurface1->setForceRenderSurface(true);
renderSurface2->setForceRenderSurface(true);
executeCalculateDrawTransformsAndVisibility(root.get());
ASSERT_TRUE(renderSurface1->renderSurface());
ASSERT_TRUE(renderSurface2->renderSurface());
EXPECT_RECT_EQ(gfx::Rect(0, 0, 100, 100), root->renderSurface()->drawableContentRect());
EXPECT_RECT_EQ(gfx::Rect(0, 0, 100, 100), root->drawableContentRect());
// Layers that do not draw content should have empty visibleContentRects.
EXPECT_RECT_EQ(gfx::Rect(0, 0, 0, 0), root->visibleContentRect());
EXPECT_RECT_EQ(gfx::Rect(0, 0, 0, 0), renderSurface1->visibleContentRect());
EXPECT_RECT_EQ(gfx::Rect(0, 0, 0, 0), renderSurface2->visibleContentRect());
// A clipped surface grows its drawableContentRect to include all drawable regions of the subtree,
// but also gets clamped by the ancestor's clip.
EXPECT_RECT_EQ(gfx::Rect(5, 5, 95, 95), renderSurface1->renderSurface()->drawableContentRect());
// renderSurface1 lives in the "unclipped universe" of renderSurface1, and is only
// implicitly clipped by renderSurface1's contentRect. So, renderSurface2 grows to
// enclose all drawable content of its subtree.
EXPECT_RECT_EQ(gfx::Rect(5, 5, 170, 170), renderSurface2->renderSurface()->drawableContentRect());
// All layers that draw content into renderSurface2 think they are unclipped.
EXPECT_RECT_EQ(gfx::Rect(0, 0, 50, 50), child1->visibleContentRect());
EXPECT_RECT_EQ(gfx::Rect(0, 0, 50, 50), child2->visibleContentRect());
EXPECT_RECT_EQ(gfx::Rect(0, 0, 50, 50), child3->visibleContentRect());
// drawableContentRects are also unclipped.
EXPECT_RECT_EQ(gfx::Rect(5, 5, 50, 50), child1->drawableContentRect());
EXPECT_RECT_EQ(gfx::Rect(75, 75, 50, 50), child2->drawableContentRect());
EXPECT_RECT_EQ(gfx::Rect(125, 125, 50, 50), child3->drawableContentRect());
}
TEST(LayerTreeHostCommonTest, verifyDrawableAndVisibleContentRectsWithTransformOnUnclippedSurface)
{
// Layers that have non-axis aligned bounds (due to transforms) have an expanded,
// axis-aligned drawableContentRect and visibleContentRect.
scoped_refptr<Layer> root = Layer::create();
scoped_refptr<Layer> renderSurface1 = Layer::create();
scoped_refptr<LayerWithForcedDrawsContent> child1 = make_scoped_refptr(new LayerWithForcedDrawsContent());
root->addChild(renderSurface1);
renderSurface1->addChild(child1);
WebTransformationMatrix identityMatrix;
WebTransformationMatrix childRotation;
childRotation.rotate(45);
setLayerPropertiesForTesting(root.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(100, 100), false);
setLayerPropertiesForTesting(renderSurface1.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(3, 4), false);
setLayerPropertiesForTesting(child1.get(), childRotation, identityMatrix, gfx::PointF(0.5, 0.5), gfx::PointF(25, 25), gfx::Size(50, 50), false);
renderSurface1->setForceRenderSurface(true);
executeCalculateDrawTransformsAndVisibility(root.get());
ASSERT_TRUE(renderSurface1->renderSurface());
EXPECT_RECT_EQ(gfx::Rect(0, 0, 100, 100), root->renderSurface()->drawableContentRect());
EXPECT_RECT_EQ(gfx::Rect(0, 0, 100, 100), root->drawableContentRect());
// Layers that do not draw content should have empty visibleContentRects.
EXPECT_RECT_EQ(gfx::Rect(0, 0, 0, 0), root->visibleContentRect());
EXPECT_RECT_EQ(gfx::Rect(0, 0, 0, 0), renderSurface1->visibleContentRect());
// The unclipped surface grows its drawableContentRect to include all drawable regions of the subtree.
int diagonalRadius = ceil(sqrt(2.0) * 25);
gfx::Rect expectedSurfaceDrawableContent = gfx::Rect(50 - diagonalRadius, 50 - diagonalRadius, diagonalRadius * 2, diagonalRadius * 2);
EXPECT_RECT_EQ(expectedSurfaceDrawableContent, renderSurface1->renderSurface()->drawableContentRect());
// All layers that draw content into the unclipped surface are also unclipped.
EXPECT_RECT_EQ(gfx::Rect(0, 0, 50, 50), child1->visibleContentRect());
EXPECT_RECT_EQ(expectedSurfaceDrawableContent, child1->drawableContentRect());
}
TEST(LayerTreeHostCommonTest, verifyDrawableAndVisibleContentRectsWithTransformOnClippedSurface)
{
// Layers that have non-axis aligned bounds (due to transforms) have an expanded,
// axis-aligned drawableContentRect and visibleContentRect.
scoped_refptr<Layer> root = Layer::create();
scoped_refptr<Layer> renderSurface1 = Layer::create();
scoped_refptr<LayerWithForcedDrawsContent> child1 = make_scoped_refptr(new LayerWithForcedDrawsContent());
root->addChild(renderSurface1);
renderSurface1->addChild(child1);
WebTransformationMatrix identityMatrix;
WebTransformationMatrix childRotation;
childRotation.rotate(45);
setLayerPropertiesForTesting(root.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(50, 50), false);
setLayerPropertiesForTesting(renderSurface1.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(3, 4), false);
setLayerPropertiesForTesting(child1.get(), childRotation, identityMatrix, gfx::PointF(0.5, 0.5), gfx::PointF(25, 25), gfx::Size(50, 50), false);
root->setMasksToBounds(true);
renderSurface1->setForceRenderSurface(true);
executeCalculateDrawTransformsAndVisibility(root.get());
ASSERT_TRUE(renderSurface1->renderSurface());
// The clipped surface clamps the drawableContentRect that encloses the rotated layer.
int diagonalRadius = ceil(sqrt(2.0) * 25);
gfx::Rect unclippedSurfaceContent = gfx::Rect(50 - diagonalRadius, 50 - diagonalRadius, diagonalRadius * 2, diagonalRadius * 2);
gfx::Rect expectedSurfaceDrawableContent = gfx::IntersectRects(unclippedSurfaceContent, gfx::Rect(0, 0, 50, 50));
EXPECT_RECT_EQ(expectedSurfaceDrawableContent, renderSurface1->renderSurface()->drawableContentRect());
// On the clipped surface, only a quarter of the child1 is visible, but when rotating
// it back to child1's content space, the actual enclosing rect ends up covering the
// full left half of child1.
EXPECT_RECT_EQ(gfx::Rect(0, 0, 26, 50), child1->visibleContentRect());
// The child's drawableContentRect is unclipped.
EXPECT_RECT_EQ(unclippedSurfaceContent, child1->drawableContentRect());
}
TEST(LayerTreeHostCommonTest, verifyDrawableAndVisibleContentRectsInHighDPI)
{
MockContentLayerClient client;
scoped_refptr<Layer> root = Layer::create();
scoped_refptr<ContentLayer> renderSurface1 = createDrawableContentLayer(&client);
scoped_refptr<ContentLayer> renderSurface2 = createDrawableContentLayer(&client);
scoped_refptr<ContentLayer> child1 = createDrawableContentLayer(&client);
scoped_refptr<ContentLayer> child2 = createDrawableContentLayer(&client);
scoped_refptr<ContentLayer> child3 = createDrawableContentLayer(&client);
root->addChild(renderSurface1);
renderSurface1->addChild(renderSurface2);
renderSurface2->addChild(child1);
renderSurface2->addChild(child2);
renderSurface2->addChild(child3);
WebTransformationMatrix identityMatrix;
setLayerPropertiesForTesting(root.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(100, 100), false);
setLayerPropertiesForTesting(renderSurface1.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(5, 5), gfx::Size(3, 4), false);
setLayerPropertiesForTesting(renderSurface2.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(5, 5), gfx::Size(7, 13), false);
setLayerPropertiesForTesting(child1.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(5, 5), gfx::Size(50, 50), false);
setLayerPropertiesForTesting(child2.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(75, 75), gfx::Size(50, 50), false);
setLayerPropertiesForTesting(child3.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(125, 125), gfx::Size(50, 50), false);
const double deviceScaleFactor = 2;
root->setContentsScale(deviceScaleFactor);
renderSurface1->setContentsScale(deviceScaleFactor);
renderSurface2->setContentsScale(deviceScaleFactor);
child1->setContentsScale(deviceScaleFactor);
child2->setContentsScale(deviceScaleFactor);
child3->setContentsScale(deviceScaleFactor);
root->setMasksToBounds(true);
renderSurface1->setForceRenderSurface(true);
renderSurface2->setForceRenderSurface(true);
executeCalculateDrawTransformsAndVisibility(root.get(), deviceScaleFactor);
ASSERT_TRUE(renderSurface1->renderSurface());
ASSERT_TRUE(renderSurface2->renderSurface());
// DrawableContentRects for all layers and surfaces are scaled by deviceScaleFactor.
EXPECT_RECT_EQ(gfx::Rect(0, 0, 200, 200), root->renderSurface()->drawableContentRect());
EXPECT_RECT_EQ(gfx::Rect(0, 0, 200, 200), root->drawableContentRect());
EXPECT_RECT_EQ(gfx::Rect(10, 10, 190, 190), renderSurface1->renderSurface()->drawableContentRect());
// renderSurface2 lives in the "unclipped universe" of renderSurface1, and
// is only implicitly clipped by renderSurface1.
EXPECT_RECT_EQ(gfx::Rect(10, 10, 350, 350), renderSurface2->renderSurface()->drawableContentRect());
EXPECT_RECT_EQ(gfx::Rect(10, 10, 100, 100), child1->drawableContentRect());
EXPECT_RECT_EQ(gfx::Rect(150, 150, 100, 100), child2->drawableContentRect());
EXPECT_RECT_EQ(gfx::Rect(250, 250, 100, 100), child3->drawableContentRect());
// The root layer does not actually draw content of its own.
EXPECT_RECT_EQ(gfx::Rect(0, 0, 0, 0), root->visibleContentRect());
// All layer visibleContentRects are expressed in content space of each
// layer, so they are also scaled by the deviceScaleFactor.
EXPECT_RECT_EQ(gfx::Rect(0, 0, 6, 8), renderSurface1->visibleContentRect());
EXPECT_RECT_EQ(gfx::Rect(0, 0, 14, 26), renderSurface2->visibleContentRect());
EXPECT_RECT_EQ(gfx::Rect(0, 0, 100, 100), child1->visibleContentRect());
EXPECT_RECT_EQ(gfx::Rect(0, 0, 100, 100), child2->visibleContentRect());
EXPECT_RECT_EQ(gfx::Rect(0, 0, 100, 100), child3->visibleContentRect());
}
TEST(LayerTreeHostCommonTest, verifyBackFaceCullingWithoutPreserves3d)
{
// Verify the behavior of back-face culling when there are no preserve-3d layers. Note
// that 3d transforms still apply in this case, but they are "flattened" to each
// parent layer according to current W3C spec.
const WebTransformationMatrix identityMatrix;
scoped_refptr<Layer> parent = Layer::create();
scoped_refptr<LayerWithForcedDrawsContent> frontFacingChild = make_scoped_refptr(new LayerWithForcedDrawsContent());
scoped_refptr<LayerWithForcedDrawsContent> backFacingChild = make_scoped_refptr(new LayerWithForcedDrawsContent());
scoped_refptr<LayerWithForcedDrawsContent> frontFacingSurface = make_scoped_refptr(new LayerWithForcedDrawsContent());
scoped_refptr<LayerWithForcedDrawsContent> backFacingSurface = make_scoped_refptr(new LayerWithForcedDrawsContent());
scoped_refptr<LayerWithForcedDrawsContent> frontFacingChildOfFrontFacingSurface = make_scoped_refptr(new LayerWithForcedDrawsContent());
scoped_refptr<LayerWithForcedDrawsContent> backFacingChildOfFrontFacingSurface = make_scoped_refptr(new LayerWithForcedDrawsContent());
scoped_refptr<LayerWithForcedDrawsContent> frontFacingChildOfBackFacingSurface = make_scoped_refptr(new LayerWithForcedDrawsContent());
scoped_refptr<LayerWithForcedDrawsContent> backFacingChildOfBackFacingSurface = make_scoped_refptr(new LayerWithForcedDrawsContent());
parent->addChild(frontFacingChild);
parent->addChild(backFacingChild);
parent->addChild(frontFacingSurface);
parent->addChild(backFacingSurface);
frontFacingSurface->addChild(frontFacingChildOfFrontFacingSurface);
frontFacingSurface->addChild(backFacingChildOfFrontFacingSurface);
backFacingSurface->addChild(frontFacingChildOfBackFacingSurface);
backFacingSurface->addChild(backFacingChildOfBackFacingSurface);
// Nothing is double-sided
frontFacingChild->setDoubleSided(false);
backFacingChild->setDoubleSided(false);
frontFacingSurface->setDoubleSided(false);
backFacingSurface->setDoubleSided(false);
frontFacingChildOfFrontFacingSurface->setDoubleSided(false);
backFacingChildOfFrontFacingSurface->setDoubleSided(false);
frontFacingChildOfBackFacingSurface->setDoubleSided(false);
backFacingChildOfBackFacingSurface->setDoubleSided(false);
WebTransformationMatrix backfaceMatrix;
backfaceMatrix.translate(50, 50);
backfaceMatrix.rotate3d(0, 1, 0, 180);
backfaceMatrix.translate(-50, -50);
// Having a descendant and opacity will force these to have render surfaces.
frontFacingSurface->setOpacity(0.5);
backFacingSurface->setOpacity(0.5);
// Nothing preserves 3d. According to current W3C CSS Transforms spec, these layers
// should blindly use their own local transforms to determine back-face culling.
setLayerPropertiesForTesting(parent.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(100, 100), false);
setLayerPropertiesForTesting(frontFacingChild.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(100, 100), false);
setLayerPropertiesForTesting(backFacingChild.get(), backfaceMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(100, 100), false);
setLayerPropertiesForTesting(frontFacingSurface.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(100, 100), false);
setLayerPropertiesForTesting(backFacingSurface.get(), backfaceMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(100, 100), false);
setLayerPropertiesForTesting(frontFacingChildOfFrontFacingSurface.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(100, 100), false);
setLayerPropertiesForTesting(backFacingChildOfFrontFacingSurface.get(), backfaceMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(100, 100), false);
setLayerPropertiesForTesting(frontFacingChildOfBackFacingSurface.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(100, 100), false);
setLayerPropertiesForTesting(backFacingChildOfBackFacingSurface.get(), backfaceMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(100, 100), false);
std::vector<scoped_refptr<Layer> > renderSurfaceLayerList;
int dummyMaxTextureSize = 512;
LayerTreeHostCommon::calculateDrawTransforms(parent.get(), parent->bounds(), 1, 1, dummyMaxTextureSize, renderSurfaceLayerList);
// Verify which renderSurfaces were created.
EXPECT_FALSE(frontFacingChild->renderSurface());
EXPECT_FALSE(backFacingChild->renderSurface());
EXPECT_TRUE(frontFacingSurface->renderSurface());
EXPECT_TRUE(backFacingSurface->renderSurface());
EXPECT_FALSE(frontFacingChildOfFrontFacingSurface->renderSurface());
EXPECT_FALSE(backFacingChildOfFrontFacingSurface->renderSurface());
EXPECT_FALSE(frontFacingChildOfBackFacingSurface->renderSurface());
EXPECT_FALSE(backFacingChildOfBackFacingSurface->renderSurface());
// Verify the renderSurfaceLayerList.
ASSERT_EQ(3u, renderSurfaceLayerList.size());
EXPECT_EQ(parent->id(), renderSurfaceLayerList[0]->id());
EXPECT_EQ(frontFacingSurface->id(), renderSurfaceLayerList[1]->id());
// Even though the back facing surface LAYER gets culled, the other descendants should still be added, so the SURFACE should not be culled.
EXPECT_EQ(backFacingSurface->id(), renderSurfaceLayerList[2]->id());
// Verify root surface's layerList.
ASSERT_EQ(3u, renderSurfaceLayerList[0]->renderSurface()->layerList().size());
EXPECT_EQ(frontFacingChild->id(), renderSurfaceLayerList[0]->renderSurface()->layerList()[0]->id());
EXPECT_EQ(frontFacingSurface->id(), renderSurfaceLayerList[0]->renderSurface()->layerList()[1]->id());
EXPECT_EQ(backFacingSurface->id(), renderSurfaceLayerList[0]->renderSurface()->layerList()[2]->id());
// Verify frontFacingSurface's layerList.
ASSERT_EQ(2u, renderSurfaceLayerList[1]->renderSurface()->layerList().size());
EXPECT_EQ(frontFacingSurface->id(), renderSurfaceLayerList[1]->renderSurface()->layerList()[0]->id());
EXPECT_EQ(frontFacingChildOfFrontFacingSurface->id(), renderSurfaceLayerList[1]->renderSurface()->layerList()[1]->id());
// Verify backFacingSurface's layerList; its own layer should be culled from the surface list.
ASSERT_EQ(1u, renderSurfaceLayerList[2]->renderSurface()->layerList().size());
EXPECT_EQ(frontFacingChildOfBackFacingSurface->id(), renderSurfaceLayerList[2]->renderSurface()->layerList()[0]->id());
}
TEST(LayerTreeHostCommonTest, verifyBackFaceCullingWithPreserves3d)
{
// Verify the behavior of back-face culling when preserves-3d transform style is used.
const WebTransformationMatrix identityMatrix;
scoped_refptr<Layer> parent = Layer::create();
scoped_refptr<LayerWithForcedDrawsContent> frontFacingChild = make_scoped_refptr(new LayerWithForcedDrawsContent());
scoped_refptr<LayerWithForcedDrawsContent> backFacingChild = make_scoped_refptr(new LayerWithForcedDrawsContent());
scoped_refptr<LayerWithForcedDrawsContent> frontFacingSurface = make_scoped_refptr(new LayerWithForcedDrawsContent());
scoped_refptr<LayerWithForcedDrawsContent> backFacingSurface = make_scoped_refptr(new LayerWithForcedDrawsContent());
scoped_refptr<LayerWithForcedDrawsContent> frontFacingChildOfFrontFacingSurface = make_scoped_refptr(new LayerWithForcedDrawsContent());
scoped_refptr<LayerWithForcedDrawsContent> backFacingChildOfFrontFacingSurface = make_scoped_refptr(new LayerWithForcedDrawsContent());
scoped_refptr<LayerWithForcedDrawsContent> frontFacingChildOfBackFacingSurface = make_scoped_refptr(new LayerWithForcedDrawsContent());
scoped_refptr<LayerWithForcedDrawsContent> backFacingChildOfBackFacingSurface = make_scoped_refptr(new LayerWithForcedDrawsContent());
scoped_refptr<LayerWithForcedDrawsContent> dummyReplicaLayer1 = make_scoped_refptr(new LayerWithForcedDrawsContent());
scoped_refptr<LayerWithForcedDrawsContent> dummyReplicaLayer2 = make_scoped_refptr(new LayerWithForcedDrawsContent());
parent->addChild(frontFacingChild);
parent->addChild(backFacingChild);
parent->addChild(frontFacingSurface);
parent->addChild(backFacingSurface);
frontFacingSurface->addChild(frontFacingChildOfFrontFacingSurface);
frontFacingSurface->addChild(backFacingChildOfFrontFacingSurface);
backFacingSurface->addChild(frontFacingChildOfBackFacingSurface);
backFacingSurface->addChild(backFacingChildOfBackFacingSurface);
// Nothing is double-sided
frontFacingChild->setDoubleSided(false);
backFacingChild->setDoubleSided(false);
frontFacingSurface->setDoubleSided(false);
backFacingSurface->setDoubleSided(false);
frontFacingChildOfFrontFacingSurface->setDoubleSided(false);
backFacingChildOfFrontFacingSurface->setDoubleSided(false);
frontFacingChildOfBackFacingSurface->setDoubleSided(false);
backFacingChildOfBackFacingSurface->setDoubleSided(false);
WebTransformationMatrix backfaceMatrix;
backfaceMatrix.translate(50, 50);
backfaceMatrix.rotate3d(0, 1, 0, 180);
backfaceMatrix.translate(-50, -50);
// Opacity will not force creation of renderSurfaces in this case because of the
// preserve-3d transform style. Instead, an example of when a surface would be
// created with preserve-3d is when there is a replica layer.
frontFacingSurface->setReplicaLayer(dummyReplicaLayer1.get());
backFacingSurface->setReplicaLayer(dummyReplicaLayer2.get());
// Each surface creates its own new 3d rendering context (as defined by W3C spec).
// According to current W3C CSS Transforms spec, layers in a 3d rendering context
// should use the transform with respect to that context. This 3d rendering context
// occurs when (a) parent's transform style is flat and (b) the layer's transform
// style is preserve-3d.
setLayerPropertiesForTesting(parent.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(100, 100), false); // parent transform style is flat.
setLayerPropertiesForTesting(frontFacingChild.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(100, 100), false);
setLayerPropertiesForTesting(backFacingChild.get(), backfaceMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(100, 100), false);
setLayerPropertiesForTesting(frontFacingSurface.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(100, 100), true); // surface transform style is preserve-3d.
setLayerPropertiesForTesting(backFacingSurface.get(), backfaceMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(100, 100), true); // surface transform style is preserve-3d.
setLayerPropertiesForTesting(frontFacingChildOfFrontFacingSurface.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(100, 100), false);
setLayerPropertiesForTesting(backFacingChildOfFrontFacingSurface.get(), backfaceMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(100, 100), false);
setLayerPropertiesForTesting(frontFacingChildOfBackFacingSurface.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(100, 100), false);
setLayerPropertiesForTesting(backFacingChildOfBackFacingSurface.get(), backfaceMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(100, 100), false);
std::vector<scoped_refptr<Layer> > renderSurfaceLayerList;
int dummyMaxTextureSize = 512;
LayerTreeHostCommon::calculateDrawTransforms(parent.get(), parent->bounds(), 1, 1, dummyMaxTextureSize, renderSurfaceLayerList);
// Verify which renderSurfaces were created.
EXPECT_FALSE(frontFacingChild->renderSurface());
EXPECT_FALSE(backFacingChild->renderSurface());
EXPECT_TRUE(frontFacingSurface->renderSurface());
EXPECT_FALSE(backFacingSurface->renderSurface());
EXPECT_FALSE(frontFacingChildOfFrontFacingSurface->renderSurface());
EXPECT_FALSE(backFacingChildOfFrontFacingSurface->renderSurface());
EXPECT_FALSE(frontFacingChildOfBackFacingSurface->renderSurface());
EXPECT_FALSE(backFacingChildOfBackFacingSurface->renderSurface());
// Verify the renderSurfaceLayerList. The back-facing surface should be culled.
ASSERT_EQ(2u, renderSurfaceLayerList.size());
EXPECT_EQ(parent->id(), renderSurfaceLayerList[0]->id());
EXPECT_EQ(frontFacingSurface->id(), renderSurfaceLayerList[1]->id());
// Verify root surface's layerList.
ASSERT_EQ(2u, renderSurfaceLayerList[0]->renderSurface()->layerList().size());
EXPECT_EQ(frontFacingChild->id(), renderSurfaceLayerList[0]->renderSurface()->layerList()[0]->id());
EXPECT_EQ(frontFacingSurface->id(), renderSurfaceLayerList[0]->renderSurface()->layerList()[1]->id());
// Verify frontFacingSurface's layerList.
ASSERT_EQ(2u, renderSurfaceLayerList[1]->renderSurface()->layerList().size());
EXPECT_EQ(frontFacingSurface->id(), renderSurfaceLayerList[1]->renderSurface()->layerList()[0]->id());
EXPECT_EQ(frontFacingChildOfFrontFacingSurface->id(), renderSurfaceLayerList[1]->renderSurface()->layerList()[1]->id());
}
TEST(LayerTreeHostCommonTest, verifyBackFaceCullingWithAnimatingTransforms)
{
// Verify that layers are appropriately culled when their back face is showing and
// they are not double sided, while animations are going on.
//
// Layers that are animating do not get culled on the main thread, as their transforms should be
// treated as "unknown" so we can not be sure that their back face is really showing.
//
const WebTransformationMatrix identityMatrix;
scoped_refptr<Layer> parent = Layer::create();
scoped_refptr<LayerWithForcedDrawsContent> child = make_scoped_refptr(new LayerWithForcedDrawsContent());
scoped_refptr<LayerWithForcedDrawsContent> animatingSurface = make_scoped_refptr(new LayerWithForcedDrawsContent());
scoped_refptr<LayerWithForcedDrawsContent> childOfAnimatingSurface = make_scoped_refptr(new LayerWithForcedDrawsContent());
scoped_refptr<LayerWithForcedDrawsContent> animatingChild = make_scoped_refptr(new LayerWithForcedDrawsContent());
scoped_refptr<LayerWithForcedDrawsContent> child2 = make_scoped_refptr(new LayerWithForcedDrawsContent());
parent->addChild(child);
parent->addChild(animatingSurface);
animatingSurface->addChild(childOfAnimatingSurface);
parent->addChild(animatingChild);
parent->addChild(child2);
// Nothing is double-sided
child->setDoubleSided(false);
child2->setDoubleSided(false);
animatingSurface->setDoubleSided(false);
childOfAnimatingSurface->setDoubleSided(false);
animatingChild->setDoubleSided(false);
WebTransformationMatrix backfaceMatrix;
backfaceMatrix.translate(50, 50);
backfaceMatrix.rotate3d(0, 1, 0, 180);
backfaceMatrix.translate(-50, -50);
// Make our render surface.
animatingSurface->setForceRenderSurface(true);
// Animate the transform on the render surface.
addAnimatedTransformToController(*animatingSurface->layerAnimationController(), 10, 30, 0);
// This is just an animating layer, not a surface.
addAnimatedTransformToController(*animatingChild->layerAnimationController(), 10, 30, 0);
setLayerPropertiesForTesting(parent.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(100, 100), false);
setLayerPropertiesForTesting(child.get(), backfaceMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(100, 100), false);
setLayerPropertiesForTesting(animatingSurface.get(), backfaceMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(100, 100), false);
setLayerPropertiesForTesting(childOfAnimatingSurface.get(), backfaceMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(100, 100), false);
setLayerPropertiesForTesting(animatingChild.get(), backfaceMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(100, 100), false);
setLayerPropertiesForTesting(child2.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(100, 100), false);
std::vector<scoped_refptr<Layer> > renderSurfaceLayerList;
int dummyMaxTextureSize = 512;
LayerTreeHostCommon::calculateDrawTransforms(parent.get(), parent->bounds(), 1, 1, dummyMaxTextureSize, renderSurfaceLayerList);
EXPECT_FALSE(child->renderSurface());
EXPECT_TRUE(animatingSurface->renderSurface());
EXPECT_FALSE(childOfAnimatingSurface->renderSurface());
EXPECT_FALSE(animatingChild->renderSurface());
EXPECT_FALSE(child2->renderSurface());
// Verify that the animatingChild and childOfAnimatingSurface were not culled, but that child was.
ASSERT_EQ(2u, renderSurfaceLayerList.size());
EXPECT_EQ(parent->id(), renderSurfaceLayerList[0]->id());
EXPECT_EQ(animatingSurface->id(), renderSurfaceLayerList[1]->id());
// The non-animating child be culled from the layer list for the parent render surface.
ASSERT_EQ(3u, renderSurfaceLayerList[0]->renderSurface()->layerList().size());
EXPECT_EQ(animatingSurface->id(), renderSurfaceLayerList[0]->renderSurface()->layerList()[0]->id());
EXPECT_EQ(animatingChild->id(), renderSurfaceLayerList[0]->renderSurface()->layerList()[1]->id());
EXPECT_EQ(child2->id(), renderSurfaceLayerList[0]->renderSurface()->layerList()[2]->id());
ASSERT_EQ(2u, renderSurfaceLayerList[1]->renderSurface()->layerList().size());
EXPECT_EQ(animatingSurface->id(), renderSurfaceLayerList[1]->renderSurface()->layerList()[0]->id());
EXPECT_EQ(childOfAnimatingSurface->id(), renderSurfaceLayerList[1]->renderSurface()->layerList()[1]->id());
EXPECT_FALSE(child2->visibleContentRect().IsEmpty());
// The animating layers should have a visibleContentRect that represents the area of the front face that is within the viewport.
EXPECT_EQ(animatingChild->visibleContentRect(), gfx::Rect(gfx::Point(), animatingChild->contentBounds()));
EXPECT_EQ(animatingSurface->visibleContentRect(), gfx::Rect(gfx::Point(), animatingSurface->contentBounds()));
// And layers in the subtree of the animating layer should have valid visibleContentRects also.
EXPECT_EQ(childOfAnimatingSurface->visibleContentRect(), gfx::Rect(gfx::Point(), childOfAnimatingSurface->contentBounds()));
}
TEST(LayerTreeHostCommonTest, verifyBackFaceCullingWithPreserves3dForFlatteningSurface)
{
// Verify the behavior of back-face culling for a renderSurface that is created
// when it flattens its subtree, and its parent has preserves-3d.
const WebTransformationMatrix identityMatrix;
scoped_refptr<Layer> parent = Layer::create();
scoped_refptr<LayerWithForcedDrawsContent> frontFacingSurface = make_scoped_refptr(new LayerWithForcedDrawsContent());
scoped_refptr<LayerWithForcedDrawsContent> backFacingSurface = make_scoped_refptr(new LayerWithForcedDrawsContent());
scoped_refptr<LayerWithForcedDrawsContent> child1 = make_scoped_refptr(new LayerWithForcedDrawsContent());
scoped_refptr<LayerWithForcedDrawsContent> child2 = make_scoped_refptr(new LayerWithForcedDrawsContent());
parent->addChild(frontFacingSurface);
parent->addChild(backFacingSurface);
frontFacingSurface->addChild(child1);
backFacingSurface->addChild(child2);
// RenderSurfaces are not double-sided
frontFacingSurface->setDoubleSided(false);
backFacingSurface->setDoubleSided(false);
WebTransformationMatrix backfaceMatrix;
backfaceMatrix.translate(50, 50);
backfaceMatrix.rotate3d(0, 1, 0, 180);
backfaceMatrix.translate(-50, -50);
setLayerPropertiesForTesting(parent.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(100, 100), true); // parent transform style is preserve3d.
setLayerPropertiesForTesting(frontFacingSurface.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(100, 100), false); // surface transform style is flat.
setLayerPropertiesForTesting(backFacingSurface.get(), backfaceMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(100, 100), false); // surface transform style is flat.
setLayerPropertiesForTesting(child1.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(100, 100), false);
setLayerPropertiesForTesting(child2.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(100, 100), false);
std::vector<scoped_refptr<Layer> > renderSurfaceLayerList;
int dummyMaxTextureSize = 512;
LayerTreeHostCommon::calculateDrawTransforms(parent.get(), parent->bounds(), 1, 1, dummyMaxTextureSize, renderSurfaceLayerList);
// Verify which renderSurfaces were created.
EXPECT_TRUE(frontFacingSurface->renderSurface());
EXPECT_FALSE(backFacingSurface->renderSurface()); // because it should be culled
EXPECT_FALSE(child1->renderSurface());
EXPECT_FALSE(child2->renderSurface());
// Verify the renderSurfaceLayerList. The back-facing surface should be culled.
ASSERT_EQ(2u, renderSurfaceLayerList.size());
EXPECT_EQ(parent->id(), renderSurfaceLayerList[0]->id());
EXPECT_EQ(frontFacingSurface->id(), renderSurfaceLayerList[1]->id());
// Verify root surface's layerList.
ASSERT_EQ(1u, renderSurfaceLayerList[0]->renderSurface()->layerList().size());
EXPECT_EQ(frontFacingSurface->id(), renderSurfaceLayerList[0]->renderSurface()->layerList()[0]->id());
// Verify frontFacingSurface's layerList.
ASSERT_EQ(2u, renderSurfaceLayerList[1]->renderSurface()->layerList().size());
EXPECT_EQ(frontFacingSurface->id(), renderSurfaceLayerList[1]->renderSurface()->layerList()[0]->id());
EXPECT_EQ(child1->id(), renderSurfaceLayerList[1]->renderSurface()->layerList()[1]->id());
}
TEST(LayerTreeHostCommonTest, verifyHitTestingForEmptyLayerList)
{
// Hit testing on an empty renderSurfaceLayerList should return a null pointer.
std::vector<LayerImpl*> renderSurfaceLayerList;
gfx::Point testPoint(0, 0);
LayerImpl* resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPoint(testPoint, renderSurfaceLayerList);
EXPECT_FALSE(resultLayer);
testPoint = gfx::Point(10, 20);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPoint(testPoint, renderSurfaceLayerList);
EXPECT_FALSE(resultLayer);
}
TEST(LayerTreeHostCommonTest, verifyHitTestingForSingleLayer)
{
scoped_ptr<LayerImpl> root = LayerImpl::create(12345);
WebTransformationMatrix identityMatrix;
gfx::PointF anchor(0, 0);
gfx::PointF position(0, 0);
gfx::Size bounds(100, 100);
setLayerPropertiesForTesting(root.get(), identityMatrix, identityMatrix, anchor, position, bounds, false);
root->setDrawsContent(true);
std::vector<LayerImpl*> renderSurfaceLayerList;
int dummyMaxTextureSize = 512;
LayerTreeHostCommon::calculateDrawTransforms(root.get(), root->bounds(), 1, 1, 0, dummyMaxTextureSize, renderSurfaceLayerList);
// Sanity check the scenario we just created.
ASSERT_EQ(1u, renderSurfaceLayerList.size());
ASSERT_EQ(1u, root->renderSurface()->layerList().size());
// Hit testing for a point outside the layer should return a null pointer.
gfx::Point testPoint(101, 101);
LayerImpl* resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPoint(testPoint, renderSurfaceLayerList);
EXPECT_FALSE(resultLayer);
testPoint = gfx::Point(-1, -1);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPoint(testPoint, renderSurfaceLayerList);
EXPECT_FALSE(resultLayer);
// Hit testing for a point inside should return the root layer.
testPoint = gfx::Point(1, 1);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPoint(testPoint, renderSurfaceLayerList);
ASSERT_TRUE(resultLayer);
EXPECT_EQ(12345, resultLayer->id());
testPoint = gfx::Point(99, 99);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPoint(testPoint, renderSurfaceLayerList);
ASSERT_TRUE(resultLayer);
EXPECT_EQ(12345, resultLayer->id());
}
TEST(LayerTreeHostCommonTest, verifyHitTestingForUninvertibleTransform)
{
scoped_ptr<LayerImpl> root = LayerImpl::create(12345);
WebTransformationMatrix uninvertibleTransform;
uninvertibleTransform.setM11(0);
uninvertibleTransform.setM22(0);
uninvertibleTransform.setM33(0);
uninvertibleTransform.setM44(0);
ASSERT_FALSE(uninvertibleTransform.isInvertible());
WebTransformationMatrix identityMatrix;
gfx::PointF anchor(0, 0);
gfx::PointF position(0, 0);
gfx::Size bounds(100, 100);
setLayerPropertiesForTesting(root.get(), uninvertibleTransform, identityMatrix, anchor, position, bounds, false);
root->setDrawsContent(true);
std::vector<LayerImpl*> renderSurfaceLayerList;
int dummyMaxTextureSize = 512;
LayerTreeHostCommon::calculateDrawTransforms(root.get(), root->bounds(), 1, 1, 0, dummyMaxTextureSize, renderSurfaceLayerList);
// Sanity check the scenario we just created.
ASSERT_EQ(1u, renderSurfaceLayerList.size());
ASSERT_EQ(1u, root->renderSurface()->layerList().size());
ASSERT_FALSE(root->screenSpaceTransform().isInvertible());
// Hit testing any point should not hit the layer. If the invertible matrix is
// accidentally ignored and treated like an identity, then the hit testing will
// incorrectly hit the layer when it shouldn't.
gfx::Point testPoint(1, 1);
LayerImpl* resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPoint(testPoint, renderSurfaceLayerList);
EXPECT_FALSE(resultLayer);
testPoint = gfx::Point(10, 10);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPoint(testPoint, renderSurfaceLayerList);
EXPECT_FALSE(resultLayer);
testPoint = gfx::Point(10, 30);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPoint(testPoint, renderSurfaceLayerList);
EXPECT_FALSE(resultLayer);
testPoint = gfx::Point(50, 50);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPoint(testPoint, renderSurfaceLayerList);
EXPECT_FALSE(resultLayer);
testPoint = gfx::Point(67, 48);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPoint(testPoint, renderSurfaceLayerList);
EXPECT_FALSE(resultLayer);
testPoint = gfx::Point(99, 99);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPoint(testPoint, renderSurfaceLayerList);
EXPECT_FALSE(resultLayer);
testPoint = gfx::Point(-1, -1);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPoint(testPoint, renderSurfaceLayerList);
EXPECT_FALSE(resultLayer);
}
TEST(LayerTreeHostCommonTest, verifyHitTestingForSinglePositionedLayer)
{
scoped_ptr<LayerImpl> root = LayerImpl::create(12345);
WebTransformationMatrix identityMatrix;
gfx::PointF anchor(0, 0);
gfx::PointF position(50, 50); // this layer is positioned, and hit testing should correctly know where the layer is located.
gfx::Size bounds(100, 100);
setLayerPropertiesForTesting(root.get(), identityMatrix, identityMatrix, anchor, position, bounds, false);
root->setDrawsContent(true);
std::vector<LayerImpl*> renderSurfaceLayerList;
int dummyMaxTextureSize = 512;
LayerTreeHostCommon::calculateDrawTransforms(root.get(), root->bounds(), 1, 1, 0, dummyMaxTextureSize, renderSurfaceLayerList);
// Sanity check the scenario we just created.
ASSERT_EQ(1u, renderSurfaceLayerList.size());
ASSERT_EQ(1u, root->renderSurface()->layerList().size());
// Hit testing for a point outside the layer should return a null pointer.
gfx::Point testPoint(49, 49);
LayerImpl* resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPoint(testPoint, renderSurfaceLayerList);
EXPECT_FALSE(resultLayer);
// Even though the layer exists at (101, 101), it should not be visible there since the root renderSurface would clamp it.
testPoint = gfx::Point(101, 101);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPoint(testPoint, renderSurfaceLayerList);
EXPECT_FALSE(resultLayer);
// Hit testing for a point inside should return the root layer.
testPoint = gfx::Point(51, 51);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPoint(testPoint, renderSurfaceLayerList);
ASSERT_TRUE(resultLayer);
EXPECT_EQ(12345, resultLayer->id());
testPoint = gfx::Point(99, 99);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPoint(testPoint, renderSurfaceLayerList);
ASSERT_TRUE(resultLayer);
EXPECT_EQ(12345, resultLayer->id());
}
TEST(LayerTreeHostCommonTest, verifyHitTestingForSingleRotatedLayer)
{
scoped_ptr<LayerImpl> root = LayerImpl::create(12345);
WebTransformationMatrix identityMatrix;
WebTransformationMatrix rotation45DegreesAboutCenter;
rotation45DegreesAboutCenter.translate(50, 50);
rotation45DegreesAboutCenter.rotate3d(0, 0, 45);
rotation45DegreesAboutCenter.translate(-50, -50);
gfx::PointF anchor(0, 0);
gfx::PointF position(0, 0);
gfx::Size bounds(100, 100);
setLayerPropertiesForTesting(root.get(), rotation45DegreesAboutCenter, identityMatrix, anchor, position, bounds, false);
root->setDrawsContent(true);
std::vector<LayerImpl*> renderSurfaceLayerList;
int dummyMaxTextureSize = 512;
LayerTreeHostCommon::calculateDrawTransforms(root.get(), root->bounds(), 1, 1, 0, dummyMaxTextureSize, renderSurfaceLayerList);
// Sanity check the scenario we just created.
ASSERT_EQ(1u, renderSurfaceLayerList.size());
ASSERT_EQ(1u, root->renderSurface()->layerList().size());
// Hit testing for points outside the layer.
// These corners would have been inside the un-transformed layer, but they should not hit the correctly transformed layer.
gfx::Point testPoint(99, 99);
LayerImpl* resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPoint(testPoint, renderSurfaceLayerList);
EXPECT_FALSE(resultLayer);
testPoint = gfx::Point(1, 1);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPoint(testPoint, renderSurfaceLayerList);
EXPECT_FALSE(resultLayer);
// Hit testing for a point inside should return the root layer.
testPoint = gfx::Point(1, 50);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPoint(testPoint, renderSurfaceLayerList);
ASSERT_TRUE(resultLayer);
EXPECT_EQ(12345, resultLayer->id());
// Hit testing the corners that would overlap the unclipped layer, but are outside the clipped region.
testPoint = gfx::Point(50, -1);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPoint(testPoint, renderSurfaceLayerList);
ASSERT_FALSE(resultLayer);
testPoint = gfx::Point(-1, 50);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPoint(testPoint, renderSurfaceLayerList);
ASSERT_FALSE(resultLayer);
}
TEST(LayerTreeHostCommonTest, verifyHitTestingForSinglePerspectiveLayer)
{
scoped_ptr<LayerImpl> root = LayerImpl::create(12345);
WebTransformationMatrix identityMatrix;
// perspectiveProjectionAboutCenter * translationByZ is designed so that the 100 x 100 layer becomes 50 x 50, and remains centered at (50, 50).
WebTransformationMatrix perspectiveProjectionAboutCenter;
perspectiveProjectionAboutCenter.translate(50, 50);
perspectiveProjectionAboutCenter.applyPerspective(1);
perspectiveProjectionAboutCenter.translate(-50, -50);
WebTransformationMatrix translationByZ;
translationByZ.translate3d(0, 0, -1);
gfx::PointF anchor(0, 0);
gfx::PointF position(0, 0);
gfx::Size bounds(100, 100);
setLayerPropertiesForTesting(root.get(), perspectiveProjectionAboutCenter * translationByZ, identityMatrix, anchor, position, bounds, false);
root->setDrawsContent(true);
std::vector<LayerImpl*> renderSurfaceLayerList;
int dummyMaxTextureSize = 512;
LayerTreeHostCommon::calculateDrawTransforms(root.get(), root->bounds(), 1, 1, 0, dummyMaxTextureSize, renderSurfaceLayerList);
// Sanity check the scenario we just created.
ASSERT_EQ(1u, renderSurfaceLayerList.size());
ASSERT_EQ(1u, root->renderSurface()->layerList().size());
// Hit testing for points outside the layer.
// These corners would have been inside the un-transformed layer, but they should not hit the correctly transformed layer.
gfx::Point testPoint(24, 24);
LayerImpl* resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPoint(testPoint, renderSurfaceLayerList);
EXPECT_FALSE(resultLayer);
testPoint = gfx::Point(76, 76);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPoint(testPoint, renderSurfaceLayerList);
EXPECT_FALSE(resultLayer);
// Hit testing for a point inside should return the root layer.
testPoint = gfx::Point(26, 26);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPoint(testPoint, renderSurfaceLayerList);
ASSERT_TRUE(resultLayer);
EXPECT_EQ(12345, resultLayer->id());
testPoint = gfx::Point(74, 74);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPoint(testPoint, renderSurfaceLayerList);
ASSERT_TRUE(resultLayer);
EXPECT_EQ(12345, resultLayer->id());
}
TEST(LayerTreeHostCommonTest, verifyHitTestingForSingleLayerWithScaledContents)
{
// A layer's visibleContentRect is actually in the layer's content space. The
// screenSpaceTransform converts from the layer's origin space to screen space. This
// test makes sure that hit testing works correctly accounts for the contents scale.
// A contentsScale that is not 1 effectively forces a non-identity transform between
// layer's content space and layer's origin space. The hit testing code must take this into account.
//
// To test this, the layer is positioned at (25, 25), and is size (50, 50). If
// contentsScale is ignored, then hit testing will mis-interpret the visibleContentRect
// as being larger than the actual bounds of the layer.
//
scoped_ptr<LayerImpl> root = LayerImpl::create(1);
WebTransformationMatrix identityMatrix;
gfx::PointF anchor(0, 0);
setLayerPropertiesForTesting(root.get(), identityMatrix, identityMatrix, anchor, gfx::PointF(0, 0), gfx::Size(100, 100), false);
{
gfx::PointF position(25, 25);
gfx::Size bounds(50, 50);
scoped_ptr<LayerImpl> testLayer = LayerImpl::create(12345);
setLayerPropertiesForTesting(testLayer.get(), identityMatrix, identityMatrix, anchor, position, bounds, false);
// override contentBounds and contentsScale
testLayer->setContentBounds(gfx::Size(100, 100));
testLayer->setContentsScale(2, 2);
testLayer->setDrawsContent(true);
root->addChild(testLayer.Pass());
}
std::vector<LayerImpl*> renderSurfaceLayerList;
int dummyMaxTextureSize = 512;
LayerTreeHostCommon::calculateDrawTransforms(root.get(), root->bounds(), 1, 1, 0, dummyMaxTextureSize, renderSurfaceLayerList);
// Sanity check the scenario we just created.
// The visibleContentRect for testLayer is actually 100x100, even though its layout size is 50x50, positioned at 25x25.
LayerImpl* testLayer = root->children()[0];
EXPECT_RECT_EQ(gfx::Rect(gfx::Point(), gfx::Size(100, 100)), testLayer->visibleContentRect());
ASSERT_EQ(1u, renderSurfaceLayerList.size());
ASSERT_EQ(1u, root->renderSurface()->layerList().size());
// Hit testing for a point outside the layer should return a null pointer (the root layer does not draw content, so it will not be hit tested either).
gfx::Point testPoint(101, 101);
LayerImpl* resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPoint(testPoint, renderSurfaceLayerList);
EXPECT_FALSE(resultLayer);
testPoint = gfx::Point(24, 24);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPoint(testPoint, renderSurfaceLayerList);
EXPECT_FALSE(resultLayer);
testPoint = gfx::Point(76, 76);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPoint(testPoint, renderSurfaceLayerList);
EXPECT_FALSE(resultLayer);
// Hit testing for a point inside should return the test layer.
testPoint = gfx::Point(26, 26);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPoint(testPoint, renderSurfaceLayerList);
ASSERT_TRUE(resultLayer);
EXPECT_EQ(12345, resultLayer->id());
testPoint = gfx::Point(74, 74);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPoint(testPoint, renderSurfaceLayerList);
ASSERT_TRUE(resultLayer);
EXPECT_EQ(12345, resultLayer->id());
}
TEST(LayerTreeHostCommonTest, verifyHitTestingForSimpleClippedLayer)
{
// Test that hit-testing will only work for the visible portion of a layer, and not
// the entire layer bounds. Here we just test the simple axis-aligned case.
WebTransformationMatrix identityMatrix;
gfx::PointF anchor(0, 0);
scoped_ptr<LayerImpl> root = LayerImpl::create(1);
setLayerPropertiesForTesting(root.get(), identityMatrix, identityMatrix, anchor, gfx::PointF(0, 0), gfx::Size(100, 100), false);
{
scoped_ptr<LayerImpl> clippingLayer = LayerImpl::create(123);
gfx::PointF position(25, 25); // this layer is positioned, and hit testing should correctly know where the layer is located.
gfx::Size bounds(50, 50);
setLayerPropertiesForTesting(clippingLayer.get(), identityMatrix, identityMatrix, anchor, position, bounds, false);
clippingLayer->setMasksToBounds(true);
scoped_ptr<LayerImpl> child = LayerImpl::create(456);
position = gfx::PointF(-50, -50);
bounds = gfx::Size(300, 300);
setLayerPropertiesForTesting(child.get(), identityMatrix, identityMatrix, anchor, position, bounds, false);
child->setDrawsContent(true);
clippingLayer->addChild(child.Pass());
root->addChild(clippingLayer.Pass());
}
std::vector<LayerImpl*> renderSurfaceLayerList;
int dummyMaxTextureSize = 512;
LayerTreeHostCommon::calculateDrawTransforms(root.get(), root->bounds(), 1, 1, 0, dummyMaxTextureSize, renderSurfaceLayerList);
// Sanity check the scenario we just created.
ASSERT_EQ(1u, renderSurfaceLayerList.size());
ASSERT_EQ(1u, root->renderSurface()->layerList().size());
ASSERT_EQ(456, root->renderSurface()->layerList()[0]->id());
// Hit testing for a point outside the layer should return a null pointer.
// Despite the child layer being very large, it should be clipped to the root layer's bounds.
gfx::Point testPoint(24, 24);
LayerImpl* resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPoint(testPoint, renderSurfaceLayerList);
EXPECT_FALSE(resultLayer);
// Even though the layer exists at (101, 101), it should not be visible there since the clippingLayer would clamp it.
testPoint = gfx::Point(76, 76);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPoint(testPoint, renderSurfaceLayerList);
EXPECT_FALSE(resultLayer);
// Hit testing for a point inside should return the child layer.
testPoint = gfx::Point(26, 26);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPoint(testPoint, renderSurfaceLayerList);
ASSERT_TRUE(resultLayer);
EXPECT_EQ(456, resultLayer->id());
testPoint = gfx::Point(74, 74);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPoint(testPoint, renderSurfaceLayerList);
ASSERT_TRUE(resultLayer);
EXPECT_EQ(456, resultLayer->id());
}
TEST(LayerTreeHostCommonTest, verifyHitTestingForMultiClippedRotatedLayer)
{
// This test checks whether hit testing correctly avoids hit testing with multiple
// ancestors that clip in non axis-aligned ways. To pass this test, the hit testing
// algorithm needs to recognize that multiple parent layers may clip the layer, and
// should not actually hit those clipped areas.
//
// The child and grandChild layers are both initialized to clip the rotatedLeaf. The
// child layer is rotated about the top-left corner, so that the root + child clips
// combined create a triangle. The rotatedLeaf will only be visible where it overlaps
// this triangle.
//
scoped_ptr<LayerImpl> root = LayerImpl::create(123);
WebTransformationMatrix identityMatrix;
gfx::PointF anchor(0, 0);
gfx::PointF position(0, 0);
gfx::Size bounds(100, 100);
setLayerPropertiesForTesting(root.get(), identityMatrix, identityMatrix, anchor, position, bounds, false);
root->setMasksToBounds(true);
{
scoped_ptr<LayerImpl> child = LayerImpl::create(456);
scoped_ptr<LayerImpl> grandChild = LayerImpl::create(789);
scoped_ptr<LayerImpl> rotatedLeaf = LayerImpl::create(2468);
position = gfx::PointF(10, 10);
bounds = gfx::Size(80, 80);
setLayerPropertiesForTesting(child.get(), identityMatrix, identityMatrix, anchor, position, bounds, false);
child->setMasksToBounds(true);
WebTransformationMatrix rotation45DegreesAboutCorner;
rotation45DegreesAboutCorner.rotate3d(0, 0, 45);
position = gfx::PointF(0, 0); // remember, positioned with respect to its parent which is already at 10, 10
bounds = gfx::Size(200, 200); // to ensure it covers at least sqrt(2) * 100.
setLayerPropertiesForTesting(grandChild.get(), rotation45DegreesAboutCorner, identityMatrix, anchor, position, bounds, false);
grandChild->setMasksToBounds(true);
// Rotates about the center of the layer
WebTransformationMatrix rotatedLeafTransform;
rotatedLeafTransform.translate(-10, -10); // cancel out the grandParent's position
rotatedLeafTransform.rotate3d(0, 0, -45); // cancel out the corner 45-degree rotation of the parent.
rotatedLeafTransform.translate(50, 50);
rotatedLeafTransform.rotate3d(0, 0, 45);
rotatedLeafTransform.translate(-50, -50);
position = gfx::PointF(0, 0);
bounds = gfx::Size(100, 100);
setLayerPropertiesForTesting(rotatedLeaf.get(), rotatedLeafTransform, identityMatrix, anchor, position, bounds, false);
rotatedLeaf->setDrawsContent(true);
grandChild->addChild(rotatedLeaf.Pass());
child->addChild(grandChild.Pass());
root->addChild(child.Pass());
}
std::vector<LayerImpl*> renderSurfaceLayerList;
int dummyMaxTextureSize = 512;
LayerTreeHostCommon::calculateDrawTransforms(root.get(), root->bounds(), 1, 1, 0, dummyMaxTextureSize, renderSurfaceLayerList);
// Sanity check the scenario we just created.
// The grandChild is expected to create a renderSurface because it masksToBounds and is not axis aligned.
ASSERT_EQ(2u, renderSurfaceLayerList.size());
ASSERT_EQ(1u, renderSurfaceLayerList[0]->renderSurface()->layerList().size());
ASSERT_EQ(789, renderSurfaceLayerList[0]->renderSurface()->layerList()[0]->id()); // grandChild's surface.
ASSERT_EQ(1u, renderSurfaceLayerList[1]->renderSurface()->layerList().size());
ASSERT_EQ(2468, renderSurfaceLayerList[1]->renderSurface()->layerList()[0]->id());
// (11, 89) is close to the the bottom left corner within the clip, but it is not inside the layer.
gfx::Point testPoint(11, 89);
LayerImpl* resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPoint(testPoint, renderSurfaceLayerList);
EXPECT_FALSE(resultLayer);
// Closer inwards from the bottom left will overlap the layer.
testPoint = gfx::Point(25, 75);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPoint(testPoint, renderSurfaceLayerList);
ASSERT_TRUE(resultLayer);
EXPECT_EQ(2468, resultLayer->id());
// (4, 50) is inside the unclipped layer, but that corner of the layer should be
// clipped away by the grandParent and should not get hit. If hit testing blindly uses
// visibleContentRect without considering how parent may clip the layer, then hit
// testing would accidentally think that the point successfully hits the layer.
testPoint = gfx::Point(4, 50);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPoint(testPoint, renderSurfaceLayerList);
EXPECT_FALSE(resultLayer);
// (11, 50) is inside the layer and within the clipped area.
testPoint = gfx::Point(11, 50);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPoint(testPoint, renderSurfaceLayerList);
ASSERT_TRUE(resultLayer);
EXPECT_EQ(2468, resultLayer->id());
// Around the middle, just to the right and up, would have hit the layer except that
// that area should be clipped away by the parent.
testPoint = gfx::Point(51, 51);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPoint(testPoint, renderSurfaceLayerList);
EXPECT_FALSE(resultLayer);
// Around the middle, just to the left and down, should successfully hit the layer.
testPoint = gfx::Point(49, 51);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPoint(testPoint, renderSurfaceLayerList);
ASSERT_TRUE(resultLayer);
EXPECT_EQ(2468, resultLayer->id());
}
TEST(LayerTreeHostCommonTest, verifyHitTestingForNonClippingIntermediateLayer)
{
// This test checks that hit testing code does not accidentally clip to layer
// bounds for a layer that actually does not clip.
WebTransformationMatrix identityMatrix;
gfx::PointF anchor(0, 0);
scoped_ptr<LayerImpl> root = LayerImpl::create(1);
setLayerPropertiesForTesting(root.get(), identityMatrix, identityMatrix, anchor, gfx::PointF(0, 0), gfx::Size(100, 100), false);
{
scoped_ptr<LayerImpl> intermediateLayer = LayerImpl::create(123);
gfx::PointF position(10, 10); // this layer is positioned, and hit testing should correctly know where the layer is located.
gfx::Size bounds(50, 50);
setLayerPropertiesForTesting(intermediateLayer.get(), identityMatrix, identityMatrix, anchor, position, bounds, false);
// Sanity check the intermediate layer should not clip.
ASSERT_FALSE(intermediateLayer->masksToBounds());
ASSERT_FALSE(intermediateLayer->maskLayer());
// The child of the intermediateLayer is translated so that it does not overlap intermediateLayer at all.
// If child is incorrectly clipped, we would not be able to hit it successfully.
scoped_ptr<LayerImpl> child = LayerImpl::create(456);
position = gfx::PointF(60, 60); // 70, 70 in screen space
bounds = gfx::Size(20, 20);
setLayerPropertiesForTesting(child.get(), identityMatrix, identityMatrix, anchor, position, bounds, false);
child->setDrawsContent(true);
intermediateLayer->addChild(child.Pass());
root->addChild(intermediateLayer.Pass());
}
std::vector<LayerImpl*> renderSurfaceLayerList;
int dummyMaxTextureSize = 512;
LayerTreeHostCommon::calculateDrawTransforms(root.get(), root->bounds(), 1, 1, 0, dummyMaxTextureSize, renderSurfaceLayerList);
// Sanity check the scenario we just created.
ASSERT_EQ(1u, renderSurfaceLayerList.size());
ASSERT_EQ(1u, root->renderSurface()->layerList().size());
ASSERT_EQ(456, root->renderSurface()->layerList()[0]->id());
// Hit testing for a point outside the layer should return a null pointer.
gfx::Point testPoint(69, 69);
LayerImpl* resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPoint(testPoint, renderSurfaceLayerList);
EXPECT_FALSE(resultLayer);
testPoint = gfx::Point(91, 91);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPoint(testPoint, renderSurfaceLayerList);
EXPECT_FALSE(resultLayer);
// Hit testing for a point inside should return the child layer.
testPoint = gfx::Point(71, 71);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPoint(testPoint, renderSurfaceLayerList);
ASSERT_TRUE(resultLayer);
EXPECT_EQ(456, resultLayer->id());
testPoint = gfx::Point(89, 89);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPoint(testPoint, renderSurfaceLayerList);
ASSERT_TRUE(resultLayer);
EXPECT_EQ(456, resultLayer->id());
}
TEST(LayerTreeHostCommonTest, verifyHitTestingForMultipleLayers)
{
scoped_ptr<LayerImpl> root = LayerImpl::create(1);
WebTransformationMatrix identityMatrix;
gfx::PointF anchor(0, 0);
gfx::PointF position(0, 0);
gfx::Size bounds(100, 100);
setLayerPropertiesForTesting(root.get(), identityMatrix, identityMatrix, anchor, position, bounds, false);
root->setDrawsContent(true);
{
// child 1 and child2 are initialized to overlap between x=50 and x=60.
// grandChild is set to overlap both child1 and child2 between y=50 and y=60.
// The expected stacking order is:
// (front) child2, (second) grandChild, (third) child1, and (back) the root layer behind all other layers.
scoped_ptr<LayerImpl> child1 = LayerImpl::create(2);
scoped_ptr<LayerImpl> child2 = LayerImpl::create(3);
scoped_ptr<LayerImpl> grandChild1 = LayerImpl::create(4);
position = gfx::PointF(10, 10);
bounds = gfx::Size(50, 50);
setLayerPropertiesForTesting(child1.get(), identityMatrix, identityMatrix, anchor, position, bounds, false);
child1->setDrawsContent(true);
position = gfx::PointF(50, 10);
bounds = gfx::Size(50, 50);
setLayerPropertiesForTesting(child2.get(), identityMatrix, identityMatrix, anchor, position, bounds, false);
child2->setDrawsContent(true);
// Remember that grandChild is positioned with respect to its parent (i.e. child1).
// In screen space, the intended position is (10, 50), with size 100 x 50.
position = gfx::PointF(0, 40);
bounds = gfx::Size(100, 50);
setLayerPropertiesForTesting(grandChild1.get(), identityMatrix, identityMatrix, anchor, position, bounds, false);
grandChild1->setDrawsContent(true);
child1->addChild(grandChild1.Pass());
root->addChild(child1.Pass());
root->addChild(child2.Pass());
}
LayerImpl* child1 = root->children()[0];
LayerImpl* child2 = root->children()[1];
LayerImpl* grandChild1 = child1->children()[0];
std::vector<LayerImpl*> renderSurfaceLayerList;
int dummyMaxTextureSize = 512;
LayerTreeHostCommon::calculateDrawTransforms(root.get(), root->bounds(), 1, 1, 0, dummyMaxTextureSize, renderSurfaceLayerList);
// Sanity check the scenario we just created.
ASSERT_TRUE(child1);
ASSERT_TRUE(child2);
ASSERT_TRUE(grandChild1);
ASSERT_EQ(1u, renderSurfaceLayerList.size());
ASSERT_EQ(4u, root->renderSurface()->layerList().size());
ASSERT_EQ(1, root->renderSurface()->layerList()[0]->id()); // root layer
ASSERT_EQ(2, root->renderSurface()->layerList()[1]->id()); // child1
ASSERT_EQ(4, root->renderSurface()->layerList()[2]->id()); // grandChild1
ASSERT_EQ(3, root->renderSurface()->layerList()[3]->id()); // child2
// Nothing overlaps the rootLayer at (1, 1), so hit testing there should find the root layer.
gfx::Point testPoint = gfx::Point(1, 1);
LayerImpl* resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPoint(testPoint, renderSurfaceLayerList);
ASSERT_TRUE(resultLayer);
EXPECT_EQ(1, resultLayer->id());
// At (15, 15), child1 and root are the only layers. child1 is expected to be on top.
testPoint = gfx::Point(15, 15);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPoint(testPoint, renderSurfaceLayerList);
ASSERT_TRUE(resultLayer);
EXPECT_EQ(2, resultLayer->id());
// At (51, 20), child1 and child2 overlap. child2 is expected to be on top.
testPoint = gfx::Point(51, 20);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPoint(testPoint, renderSurfaceLayerList);
ASSERT_TRUE(resultLayer);
EXPECT_EQ(3, resultLayer->id());
// At (80, 51), child2 and grandChild1 overlap. child2 is expected to be on top.
testPoint = gfx::Point(80, 51);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPoint(testPoint, renderSurfaceLayerList);
ASSERT_TRUE(resultLayer);
EXPECT_EQ(3, resultLayer->id());
// At (51, 51), all layers overlap each other. child2 is expected to be on top of all other layers.
testPoint = gfx::Point(51, 51);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPoint(testPoint, renderSurfaceLayerList);
ASSERT_TRUE(resultLayer);
EXPECT_EQ(3, resultLayer->id());
// At (20, 51), child1 and grandChild1 overlap. grandChild1 is expected to be on top.
testPoint = gfx::Point(20, 51);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPoint(testPoint, renderSurfaceLayerList);
ASSERT_TRUE(resultLayer);
EXPECT_EQ(4, resultLayer->id());
}
TEST(LayerTreeHostCommonTest, verifyHitTestingForMultipleLayerLists)
{
//
// The geometry is set up similarly to the previous case, but
// all layers are forced to be renderSurfaces now.
//
scoped_ptr<LayerImpl> root = LayerImpl::create(1);
WebTransformationMatrix identityMatrix;
gfx::PointF anchor(0, 0);
gfx::PointF position(0, 0);
gfx::Size bounds(100, 100);
setLayerPropertiesForTesting(root.get(), identityMatrix, identityMatrix, anchor, position, bounds, false);
root->setDrawsContent(true);
{
// child 1 and child2 are initialized to overlap between x=50 and x=60.
// grandChild is set to overlap both child1 and child2 between y=50 and y=60.
// The expected stacking order is:
// (front) child2, (second) grandChild, (third) child1, and (back) the root layer behind all other layers.
scoped_ptr<LayerImpl> child1 = LayerImpl::create(2);
scoped_ptr<LayerImpl> child2 = LayerImpl::create(3);
scoped_ptr<LayerImpl> grandChild1 = LayerImpl::create(4);
position = gfx::PointF(10, 10);
bounds = gfx::Size(50, 50);
setLayerPropertiesForTesting(child1.get(), identityMatrix, identityMatrix, anchor, position, bounds, false);
child1->setDrawsContent(true);
child1->setForceRenderSurface(true);
position = gfx::PointF(50, 10);
bounds = gfx::Size(50, 50);
setLayerPropertiesForTesting(child2.get(), identityMatrix, identityMatrix, anchor, position, bounds, false);
child2->setDrawsContent(true);
child2->setForceRenderSurface(true);
// Remember that grandChild is positioned with respect to its parent (i.e. child1).
// In screen space, the intended position is (10, 50), with size 100 x 50.
position = gfx::PointF(0, 40);
bounds = gfx::Size(100, 50);
setLayerPropertiesForTesting(grandChild1.get(), identityMatrix, identityMatrix, anchor, position, bounds, false);
grandChild1->setDrawsContent(true);
grandChild1->setForceRenderSurface(true);
child1->addChild(grandChild1.Pass());
root->addChild(child1.Pass());
root->addChild(child2.Pass());
}
LayerImpl* child1 = root->children()[0];
LayerImpl* child2 = root->children()[1];
LayerImpl* grandChild1 = child1->children()[0];
std::vector<LayerImpl*> renderSurfaceLayerList;
int dummyMaxTextureSize = 512;
LayerTreeHostCommon::calculateDrawTransforms(root.get(), root->bounds(), 1, 1, 0, dummyMaxTextureSize, renderSurfaceLayerList);
// Sanity check the scenario we just created.
ASSERT_TRUE(child1);
ASSERT_TRUE(child2);
ASSERT_TRUE(grandChild1);
ASSERT_TRUE(child1->renderSurface());
ASSERT_TRUE(child2->renderSurface());
ASSERT_TRUE(grandChild1->renderSurface());
ASSERT_EQ(4u, renderSurfaceLayerList.size());
ASSERT_EQ(3u, root->renderSurface()->layerList().size()); // The root surface has the root layer, and child1's and child2's renderSurfaces.
ASSERT_EQ(2u, child1->renderSurface()->layerList().size()); // The child1 surface has the child1 layer and grandChild1's renderSurface.
ASSERT_EQ(1u, child2->renderSurface()->layerList().size());
ASSERT_EQ(1u, grandChild1->renderSurface()->layerList().size());
ASSERT_EQ(1, renderSurfaceLayerList[0]->id()); // root layer
ASSERT_EQ(2, renderSurfaceLayerList[1]->id()); // child1
ASSERT_EQ(4, renderSurfaceLayerList[2]->id()); // grandChild1
ASSERT_EQ(3, renderSurfaceLayerList[3]->id()); // child2
// Nothing overlaps the rootLayer at (1, 1), so hit testing there should find the root layer.
gfx::Point testPoint = gfx::Point(1, 1);
LayerImpl* resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPoint(testPoint, renderSurfaceLayerList);
ASSERT_TRUE(resultLayer);
EXPECT_EQ(1, resultLayer->id());
// At (15, 15), child1 and root are the only layers. child1 is expected to be on top.
testPoint = gfx::Point(15, 15);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPoint(testPoint, renderSurfaceLayerList);
ASSERT_TRUE(resultLayer);
EXPECT_EQ(2, resultLayer->id());
// At (51, 20), child1 and child2 overlap. child2 is expected to be on top.
testPoint = gfx::Point(51, 20);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPoint(testPoint, renderSurfaceLayerList);
ASSERT_TRUE(resultLayer);
EXPECT_EQ(3, resultLayer->id());
// At (80, 51), child2 and grandChild1 overlap. child2 is expected to be on top.
testPoint = gfx::Point(80, 51);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPoint(testPoint, renderSurfaceLayerList);
ASSERT_TRUE(resultLayer);
EXPECT_EQ(3, resultLayer->id());
// At (51, 51), all layers overlap each other. child2 is expected to be on top of all other layers.
testPoint = gfx::Point(51, 51);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPoint(testPoint, renderSurfaceLayerList);
ASSERT_TRUE(resultLayer);
EXPECT_EQ(3, resultLayer->id());
// At (20, 51), child1 and grandChild1 overlap. grandChild1 is expected to be on top.
testPoint = gfx::Point(20, 51);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPoint(testPoint, renderSurfaceLayerList);
ASSERT_TRUE(resultLayer);
EXPECT_EQ(4, resultLayer->id());
}
TEST(LayerTreeHostCommonTest, verifyHitCheckingTouchHandlerRegionsForEmptyLayerList)
{
// Hit checking on an empty renderSurfaceLayerList should return a null pointer.
std::vector<LayerImpl*> renderSurfaceLayerList;
gfx::Point testPoint(0, 0);
LayerImpl* resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPointInTouchHandlerRegion(testPoint, renderSurfaceLayerList);
EXPECT_FALSE(resultLayer);
testPoint = gfx::Point(10, 20);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPointInTouchHandlerRegion(testPoint, renderSurfaceLayerList);
EXPECT_FALSE(resultLayer);
}
TEST(LayerTreeHostCommonTest, verifyHitCheckingTouchHandlerRegionsForSingleLayer)
{
scoped_ptr<LayerImpl> root = LayerImpl::create(12345);
WebTransformationMatrix identityMatrix;
Region touchHandlerRegion(gfx::Rect(10, 10, 50, 50));
gfx::PointF anchor(0, 0);
gfx::PointF position(0, 0);
gfx::Size bounds(100, 100);
setLayerPropertiesForTesting(root.get(), identityMatrix, identityMatrix, anchor, position, bounds, false);
root->setDrawsContent(true);
std::vector<LayerImpl*> renderSurfaceLayerList;
int dummyMaxTextureSize = 512;
LayerTreeHostCommon::calculateDrawTransforms(root.get(), root->bounds(), 1, 1, 0, dummyMaxTextureSize, renderSurfaceLayerList);
// Sanity check the scenario we just created.
ASSERT_EQ(1u, renderSurfaceLayerList.size());
ASSERT_EQ(1u, root->renderSurface()->layerList().size());
// Hit checking for any point should return a null pointer for a layer without any touch event handler regions.
gfx::Point testPoint(11, 11);
LayerImpl* resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPointInTouchHandlerRegion(testPoint, renderSurfaceLayerList);
EXPECT_FALSE(resultLayer);
root->setTouchEventHandlerRegion(touchHandlerRegion);
// Hit checking for a point outside the layer should return a null pointer.
testPoint = gfx::Point(101, 101);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPointInTouchHandlerRegion(testPoint, renderSurfaceLayerList);
EXPECT_FALSE(resultLayer);
testPoint = gfx::Point(-1, -1);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPointInTouchHandlerRegion(testPoint, renderSurfaceLayerList);
EXPECT_FALSE(resultLayer);
// Hit checking for a point inside the layer, but outside the touch handler region should return a null pointer.
testPoint = gfx::Point(1, 1);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPointInTouchHandlerRegion(testPoint, renderSurfaceLayerList);
EXPECT_FALSE(resultLayer);
testPoint = gfx::Point(99, 99);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPointInTouchHandlerRegion(testPoint, renderSurfaceLayerList);
EXPECT_FALSE(resultLayer);
// Hit checking for a point inside the touch event handler region should return the root layer.
testPoint = gfx::Point(11, 11);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPointInTouchHandlerRegion(testPoint, renderSurfaceLayerList);
ASSERT_TRUE(resultLayer);
EXPECT_EQ(12345, resultLayer->id());
testPoint = gfx::Point(59, 59);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPointInTouchHandlerRegion(testPoint, renderSurfaceLayerList);
ASSERT_TRUE(resultLayer);
EXPECT_EQ(12345, resultLayer->id());
}
TEST(LayerTreeHostCommonTest, verifyHitCheckingTouchHandlerRegionsForUninvertibleTransform)
{
scoped_ptr<LayerImpl> root = LayerImpl::create(12345);
WebTransformationMatrix uninvertibleTransform;
uninvertibleTransform.setM11(0);
uninvertibleTransform.setM22(0);
uninvertibleTransform.setM33(0);
uninvertibleTransform.setM44(0);
ASSERT_FALSE(uninvertibleTransform.isInvertible());
WebTransformationMatrix identityMatrix;
Region touchHandlerRegion(gfx::Rect(10, 10, 50, 50));
gfx::PointF anchor(0, 0);
gfx::PointF position(0, 0);
gfx::Size bounds(100, 100);
setLayerPropertiesForTesting(root.get(), uninvertibleTransform, identityMatrix, anchor, position, bounds, false);
root->setDrawsContent(true);
root->setTouchEventHandlerRegion(touchHandlerRegion);
std::vector<LayerImpl*> renderSurfaceLayerList;
int dummyMaxTextureSize = 512;
LayerTreeHostCommon::calculateDrawTransforms(root.get(), root->bounds(), 1, 1, 0, dummyMaxTextureSize, renderSurfaceLayerList);
// Sanity check the scenario we just created.
ASSERT_EQ(1u, renderSurfaceLayerList.size());
ASSERT_EQ(1u, root->renderSurface()->layerList().size());
ASSERT_FALSE(root->screenSpaceTransform().isInvertible());
// Hit checking any point should not hit the touch handler region on the layer. If the invertible matrix is
// accidentally ignored and treated like an identity, then the hit testing will
// incorrectly hit the layer when it shouldn't.
gfx::Point testPoint(1, 1);
LayerImpl* resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPointInTouchHandlerRegion(testPoint, renderSurfaceLayerList);
EXPECT_FALSE(resultLayer);
testPoint = gfx::Point(10, 10);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPointInTouchHandlerRegion(testPoint, renderSurfaceLayerList);
EXPECT_FALSE(resultLayer);
testPoint = gfx::Point(10, 30);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPointInTouchHandlerRegion(testPoint, renderSurfaceLayerList);
EXPECT_FALSE(resultLayer);
testPoint = gfx::Point(50, 50);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPointInTouchHandlerRegion(testPoint, renderSurfaceLayerList);
EXPECT_FALSE(resultLayer);
testPoint = gfx::Point(67, 48);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPointInTouchHandlerRegion(testPoint, renderSurfaceLayerList);
EXPECT_FALSE(resultLayer);
testPoint = gfx::Point(99, 99);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPointInTouchHandlerRegion(testPoint, renderSurfaceLayerList);
EXPECT_FALSE(resultLayer);
testPoint = gfx::Point(-1, -1);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPointInTouchHandlerRegion(testPoint, renderSurfaceLayerList);
EXPECT_FALSE(resultLayer);
}
TEST(LayerTreeHostCommonTest, verifyHitCheckingTouchHandlerRegionsForSinglePositionedLayer)
{
scoped_ptr<LayerImpl> root = LayerImpl::create(12345);
WebTransformationMatrix identityMatrix;
Region touchHandlerRegion(gfx::Rect(10, 10, 50, 50));
gfx::PointF anchor(0, 0);
gfx::PointF position(50, 50); // this layer is positioned, and hit testing should correctly know where the layer is located.
gfx::Size bounds(100, 100);
setLayerPropertiesForTesting(root.get(), identityMatrix, identityMatrix, anchor, position, bounds, false);
root->setDrawsContent(true);
root->setTouchEventHandlerRegion(touchHandlerRegion);
std::vector<LayerImpl*> renderSurfaceLayerList;
int dummyMaxTextureSize = 512;
LayerTreeHostCommon::calculateDrawTransforms(root.get(), root->bounds(), 1, 1, 0, dummyMaxTextureSize, renderSurfaceLayerList);
// Sanity check the scenario we just created.
ASSERT_EQ(1u, renderSurfaceLayerList.size());
ASSERT_EQ(1u, root->renderSurface()->layerList().size());
// Hit checking for a point outside the layer should return a null pointer.
gfx::Point testPoint(49, 49);
LayerImpl* resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPointInTouchHandlerRegion(testPoint, renderSurfaceLayerList);
EXPECT_FALSE(resultLayer);
// Even though the layer has a touch handler region containing (101, 101), it should not be visible there since the root renderSurface would clamp it.
testPoint = gfx::Point(101, 101);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPointInTouchHandlerRegion(testPoint, renderSurfaceLayerList);
EXPECT_FALSE(resultLayer);
// Hit checking for a point inside the layer, but outside the touch handler region should return a null pointer.
testPoint = gfx::Point(51, 51);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPointInTouchHandlerRegion(testPoint, renderSurfaceLayerList);
EXPECT_FALSE(resultLayer);
// Hit checking for a point inside the touch event handler region should return the root layer.
testPoint = gfx::Point(61, 61);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPointInTouchHandlerRegion(testPoint, renderSurfaceLayerList);
ASSERT_TRUE(resultLayer);
EXPECT_EQ(12345, resultLayer->id());
testPoint = gfx::Point(99, 99);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPointInTouchHandlerRegion(testPoint, renderSurfaceLayerList);
ASSERT_TRUE(resultLayer);
EXPECT_EQ(12345, resultLayer->id());
}
TEST(LayerTreeHostCommonTest, verifyHitCheckingTouchHandlerRegionsForSingleLayerWithScaledContents)
{
// A layer's visibleContentRect is actually in the layer's content space. The
// screenSpaceTransform converts from the layer's origin space to screen space. This
// test makes sure that hit testing works correctly accounts for the contents scale.
// A contentsScale that is not 1 effectively forces a non-identity transform between
// layer's content space and layer's origin space. The hit testing code must take this into account.
//
// To test this, the layer is positioned at (25, 25), and is size (50, 50). If
// contentsScale is ignored, then hit checking will mis-interpret the visibleContentRect
// as being larger than the actual bounds of the layer.
//
scoped_ptr<LayerImpl> root = LayerImpl::create(1);
WebTransformationMatrix identityMatrix;
gfx::PointF anchor(0, 0);
setLayerPropertiesForTesting(root.get(), identityMatrix, identityMatrix, anchor, gfx::PointF(0, 0), gfx::Size(100, 100), false);
{
Region touchHandlerRegion(gfx::Rect(10, 10, 30, 30));
gfx::PointF position(25, 25);
gfx::Size bounds(50, 50);
scoped_ptr<LayerImpl> testLayer = LayerImpl::create(12345);
setLayerPropertiesForTesting(testLayer.get(), identityMatrix, identityMatrix, anchor, position, bounds, false);
// override contentBounds and contentsScale
testLayer->setContentBounds(gfx::Size(100, 100));
testLayer->setContentsScale(2, 2);
testLayer->setDrawsContent(true);
testLayer->setTouchEventHandlerRegion(touchHandlerRegion);
root->addChild(testLayer.Pass());
}
std::vector<LayerImpl*> renderSurfaceLayerList;
int dummyMaxTextureSize = 512;
LayerTreeHostCommon::calculateDrawTransforms(root.get(), root->bounds(), 1, 1, 0, dummyMaxTextureSize, renderSurfaceLayerList);
// Sanity check the scenario we just created.
// The visibleContentRect for testLayer is actually 100x100, even though its layout size is 50x50, positioned at 25x25.
LayerImpl* testLayer = root->children()[0];
EXPECT_RECT_EQ(gfx::Rect(gfx::Point(), gfx::Size(100, 100)), testLayer->visibleContentRect());
ASSERT_EQ(1u, renderSurfaceLayerList.size());
ASSERT_EQ(1u, root->renderSurface()->layerList().size());
// Hit checking for a point outside the layer should return a null pointer (the root layer does not draw content, so it will not be tested either).
gfx::Point testPoint(76, 76);
LayerImpl* resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPointInTouchHandlerRegion(testPoint, renderSurfaceLayerList);
EXPECT_FALSE(resultLayer);
// Hit checking for a point inside the layer, but outside the touch handler region should return a null pointer.
testPoint = gfx::Point(26, 26);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPointInTouchHandlerRegion(testPoint, renderSurfaceLayerList);
EXPECT_FALSE(resultLayer);
testPoint = gfx::Point(34, 34);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPointInTouchHandlerRegion(testPoint, renderSurfaceLayerList);
EXPECT_FALSE(resultLayer);
testPoint = gfx::Point(65, 65);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPointInTouchHandlerRegion(testPoint, renderSurfaceLayerList);
EXPECT_FALSE(resultLayer);
testPoint = gfx::Point(74, 74);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPointInTouchHandlerRegion(testPoint, renderSurfaceLayerList);
EXPECT_FALSE(resultLayer);
// Hit checking for a point inside the touch event handler region should return the root layer.
testPoint = gfx::Point(35, 35);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPointInTouchHandlerRegion(testPoint, renderSurfaceLayerList);
ASSERT_TRUE(resultLayer);
EXPECT_EQ(12345, resultLayer->id());
testPoint = gfx::Point(64, 64);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPointInTouchHandlerRegion(testPoint, renderSurfaceLayerList);
ASSERT_TRUE(resultLayer);
EXPECT_EQ(12345, resultLayer->id());
}
TEST(LayerTreeHostCommonTest, verifyHitCheckingTouchHandlerRegionsForSingleLayerWithDeviceScale)
{
// The layer's deviceScalefactor and pageScaleFactor should scale the contentRect and we should
// be able to hit the touch handler region by scaling the points accordingly.
scoped_ptr<LayerImpl> root = LayerImpl::create(1);
WebTransformationMatrix identityMatrix;
gfx::PointF anchor(0, 0);
// Set the bounds of the root layer big enough to fit the child when scaled.
setLayerPropertiesForTesting(root.get(), identityMatrix, identityMatrix, anchor, gfx::PointF(0, 0), gfx::Size(100, 100), false);
{
Region touchHandlerRegion(gfx::Rect(10, 10, 30, 30));
gfx::PointF position(25, 25);
gfx::Size bounds(50, 50);
scoped_ptr<LayerImpl> testLayer = LayerImpl::create(12345);
setLayerPropertiesForTesting(testLayer.get(), identityMatrix, identityMatrix, anchor, position, bounds, false);
testLayer->setDrawsContent(true);
testLayer->setTouchEventHandlerRegion(touchHandlerRegion);
root->addChild(testLayer.Pass());
}
std::vector<LayerImpl*> renderSurfaceLayerList;
int dummyMaxTextureSize = 512;
float deviceScaleFactor = 3.0f;
float pageScaleFactor = 5.0f;
WebTransformationMatrix pageScaleTransform;
pageScaleTransform.scale(pageScaleFactor);
root->setImplTransform(pageScaleTransform); // Applying the pageScaleFactor through implTransform.
gfx::Size scaledBoundsForRoot = gfx::ToCeiledSize(gfx::ScaleSize(root->bounds(), deviceScaleFactor * pageScaleFactor));
LayerTreeHostCommon::calculateDrawTransforms(root.get(), scaledBoundsForRoot, deviceScaleFactor, 1, 0, dummyMaxTextureSize, renderSurfaceLayerList);
// Sanity check the scenario we just created.
// The visibleContentRect for testLayer is actually 100x100, even though its layout size is 50x50, positioned at 25x25.
LayerImpl* testLayer = root->children()[0];
ASSERT_EQ(1u, renderSurfaceLayerList.size());
ASSERT_EQ(1u, root->renderSurface()->layerList().size());
// Check whether the child layer fits into the root after scaled.
EXPECT_RECT_EQ(gfx::Rect(testLayer->contentBounds()), testLayer->visibleContentRect());;
// Hit checking for a point outside the layer should return a null pointer (the root layer does not draw content, so it will not be tested either).
gfx::PointF testPoint(76, 76);
testPoint = gfx::ScalePoint(testPoint, deviceScaleFactor * pageScaleFactor);
LayerImpl* resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPointInTouchHandlerRegion(testPoint, renderSurfaceLayerList);
EXPECT_FALSE(resultLayer);
// Hit checking for a point inside the layer, but outside the touch handler region should return a null pointer.
testPoint = gfx::Point(26, 26);
testPoint = gfx::ScalePoint(testPoint, deviceScaleFactor * pageScaleFactor);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPointInTouchHandlerRegion(testPoint, renderSurfaceLayerList);
EXPECT_FALSE(resultLayer);
testPoint = gfx::Point(34, 34);
testPoint = gfx::ScalePoint(testPoint, deviceScaleFactor * pageScaleFactor);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPointInTouchHandlerRegion(testPoint, renderSurfaceLayerList);
EXPECT_FALSE(resultLayer);
testPoint = gfx::Point(65, 65);
testPoint = gfx::ScalePoint(testPoint, deviceScaleFactor * pageScaleFactor);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPointInTouchHandlerRegion(testPoint, renderSurfaceLayerList);
EXPECT_FALSE(resultLayer);
testPoint = gfx::Point(74, 74);
testPoint = gfx::ScalePoint(testPoint, deviceScaleFactor * pageScaleFactor);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPointInTouchHandlerRegion(testPoint, renderSurfaceLayerList);
EXPECT_FALSE(resultLayer);
// Hit checking for a point inside the touch event handler region should return the root layer.
testPoint = gfx::Point(35, 35);
testPoint = gfx::ScalePoint(testPoint, deviceScaleFactor * pageScaleFactor);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPointInTouchHandlerRegion(testPoint, renderSurfaceLayerList);
ASSERT_TRUE(resultLayer);
EXPECT_EQ(12345, resultLayer->id());
testPoint = gfx::Point(64, 64);
testPoint = gfx::ScalePoint(testPoint, deviceScaleFactor * pageScaleFactor);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPointInTouchHandlerRegion(testPoint, renderSurfaceLayerList);
ASSERT_TRUE(resultLayer);
EXPECT_EQ(12345, resultLayer->id());
}
TEST(LayerTreeHostCommonTest, verifyHitCheckingTouchHandlerRegionsForSimpleClippedLayer)
{
// Test that hit-checking will only work for the visible portion of a layer, and not
// the entire layer bounds. Here we just test the simple axis-aligned case.
WebTransformationMatrix identityMatrix;
gfx::PointF anchor(0, 0);
scoped_ptr<LayerImpl> root = LayerImpl::create(1);
setLayerPropertiesForTesting(root.get(), identityMatrix, identityMatrix, anchor, gfx::PointF(0, 0), gfx::Size(100, 100), false);
{
scoped_ptr<LayerImpl> clippingLayer = LayerImpl::create(123);
gfx::PointF position(25, 25); // this layer is positioned, and hit testing should correctly know where the layer is located.
gfx::Size bounds(50, 50);
setLayerPropertiesForTesting(clippingLayer.get(), identityMatrix, identityMatrix, anchor, position, bounds, false);
clippingLayer->setMasksToBounds(true);
scoped_ptr<LayerImpl> child = LayerImpl::create(456);
Region touchHandlerRegion(gfx::Rect(10, 10, 50, 50));
position = gfx::PointF(-50, -50);
bounds = gfx::Size(300, 300);
setLayerPropertiesForTesting(child.get(), identityMatrix, identityMatrix, anchor, position, bounds, false);
child->setDrawsContent(true);
child->setTouchEventHandlerRegion(touchHandlerRegion);
clippingLayer->addChild(child.Pass());
root->addChild(clippingLayer.Pass());
}
std::vector<LayerImpl*> renderSurfaceLayerList;
int dummyMaxTextureSize = 512;
LayerTreeHostCommon::calculateDrawTransforms(root.get(), root->bounds(), 1, 1, 0, dummyMaxTextureSize, renderSurfaceLayerList);
// Sanity check the scenario we just created.
ASSERT_EQ(1u, renderSurfaceLayerList.size());
ASSERT_EQ(1u, root->renderSurface()->layerList().size());
ASSERT_EQ(456, root->renderSurface()->layerList()[0]->id());
// Hit checking for a point outside the layer should return a null pointer.
// Despite the child layer being very large, it should be clipped to the root layer's bounds.
gfx::Point testPoint(24, 24);
LayerImpl* resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPointInTouchHandlerRegion(testPoint, renderSurfaceLayerList);
EXPECT_FALSE(resultLayer);
// Hit checking for a point inside the layer, but outside the touch handler region should return a null pointer.
testPoint = gfx::Point(35, 35);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPointInTouchHandlerRegion(testPoint, renderSurfaceLayerList);
EXPECT_FALSE(resultLayer);
testPoint = gfx::Point(74, 74);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPointInTouchHandlerRegion(testPoint, renderSurfaceLayerList);
EXPECT_FALSE(resultLayer);
// Hit checking for a point inside the touch event handler region should return the root layer.
testPoint = gfx::Point(25, 25);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPointInTouchHandlerRegion(testPoint, renderSurfaceLayerList);
ASSERT_TRUE(resultLayer);
EXPECT_EQ(456, resultLayer->id());
testPoint = gfx::Point(34, 34);
resultLayer = LayerTreeHostCommon::findLayerThatIsHitByPointInTouchHandlerRegion(testPoint, renderSurfaceLayerList);
ASSERT_TRUE(resultLayer);
EXPECT_EQ(456, resultLayer->id());
}
class NoScaleContentLayer : public ContentLayer
{
public:
static scoped_refptr<NoScaleContentLayer> create(ContentLayerClient* client) { return make_scoped_refptr(new NoScaleContentLayer(client)); }
virtual gfx::Size contentBounds() const OVERRIDE { return bounds(); }
virtual float contentsScaleX() const OVERRIDE { return 1.0; }
virtual float contentsScaleY() const OVERRIDE { return 1.0; }
protected:
explicit NoScaleContentLayer(ContentLayerClient* client) : ContentLayer(client) { }
virtual ~NoScaleContentLayer() { }
};
scoped_refptr<NoScaleContentLayer> createNoScaleDrawableContentLayer(ContentLayerClient* delegate)
{
scoped_refptr<NoScaleContentLayer> toReturn = NoScaleContentLayer::create(delegate);
toReturn->setIsDrawable(true);
return toReturn;
}
TEST(LayerTreeHostCommonTest, verifyLayerTransformsInHighDPI)
{
// Verify draw and screen space transforms of layers not in a surface.
MockContentLayerClient delegate;
WebTransformationMatrix identityMatrix;
scoped_refptr<ContentLayer> parent = createDrawableContentLayer(&delegate);
setLayerPropertiesForTesting(parent.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(100, 100), true);
scoped_refptr<ContentLayer> child = createDrawableContentLayer(&delegate);
setLayerPropertiesForTesting(child.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(2, 2), gfx::Size(10, 10), true);
scoped_refptr<ContentLayer> childEmpty = createDrawableContentLayer(&delegate);
setLayerPropertiesForTesting(childEmpty.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(2, 2), gfx::Size(0, 0), true);
scoped_refptr<NoScaleContentLayer> childNoScale = createNoScaleDrawableContentLayer(&delegate);
setLayerPropertiesForTesting(childNoScale.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(2, 2), gfx::Size(10, 10), true);
parent->addChild(child);
parent->addChild(childEmpty);
parent->addChild(childNoScale);
std::vector<scoped_refptr<Layer> > renderSurfaceLayerList;
int dummyMaxTextureSize = 512;
const double deviceScaleFactor = 2.5;
const double pageScaleFactor = 1;
LayerTreeHostCommon::calculateDrawTransforms(parent.get(), parent->bounds(), deviceScaleFactor, pageScaleFactor, dummyMaxTextureSize, renderSurfaceLayerList);
EXPECT_CONTENTS_SCALE_EQ(deviceScaleFactor * pageScaleFactor, parent);
EXPECT_CONTENTS_SCALE_EQ(deviceScaleFactor * pageScaleFactor, child);
EXPECT_CONTENTS_SCALE_EQ(deviceScaleFactor * pageScaleFactor, childEmpty);
EXPECT_CONTENTS_SCALE_EQ(1, childNoScale);
EXPECT_EQ(1u, renderSurfaceLayerList.size());
// Verify parent transforms
WebTransformationMatrix expectedParentTransform;
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedParentTransform, parent->screenSpaceTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedParentTransform, parent->drawTransform());
// Verify results of transformed parent rects
gfx::RectF parentContentBounds(gfx::PointF(), gfx::SizeF(parent->contentBounds()));
gfx::RectF parentDrawRect = MathUtil::mapClippedRect(parent->drawTransform(), parentContentBounds);
gfx::RectF parentScreenSpaceRect = MathUtil::mapClippedRect(parent->screenSpaceTransform(), parentContentBounds);
gfx::RectF expectedParentDrawRect(gfx::PointF(), parent->bounds());
expectedParentDrawRect.Scale(deviceScaleFactor);
EXPECT_FLOAT_RECT_EQ(expectedParentDrawRect, parentDrawRect);
EXPECT_FLOAT_RECT_EQ(expectedParentDrawRect, parentScreenSpaceRect);
// Verify child and childEmpty transforms. They should match.
WebTransformationMatrix expectedChildTransform;
expectedChildTransform.translate(deviceScaleFactor * child->position().x(), deviceScaleFactor * child->position().y());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedChildTransform, child->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedChildTransform, child->screenSpaceTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedChildTransform, childEmpty->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedChildTransform, childEmpty->screenSpaceTransform());
// Verify results of transformed child and childEmpty rects. They should match.
gfx::RectF childContentBounds(gfx::PointF(), gfx::SizeF(child->contentBounds()));
gfx::RectF childDrawRect = MathUtil::mapClippedRect(child->drawTransform(), childContentBounds);
gfx::RectF childScreenSpaceRect = MathUtil::mapClippedRect(child->screenSpaceTransform(), childContentBounds);
gfx::RectF childEmptyDrawRect = MathUtil::mapClippedRect(childEmpty->drawTransform(), childContentBounds);
gfx::RectF childEmptyScreenSpaceRect = MathUtil::mapClippedRect(childEmpty->screenSpaceTransform(), childContentBounds);
gfx::RectF expectedChildDrawRect(child->position(), child->bounds());
expectedChildDrawRect.Scale(deviceScaleFactor);
EXPECT_FLOAT_RECT_EQ(expectedChildDrawRect, childDrawRect);
EXPECT_FLOAT_RECT_EQ(expectedChildDrawRect, childScreenSpaceRect);
EXPECT_FLOAT_RECT_EQ(expectedChildDrawRect, childEmptyDrawRect);
EXPECT_FLOAT_RECT_EQ(expectedChildDrawRect, childEmptyScreenSpaceRect);
// Verify childNoScale transforms
WebTransformationMatrix expectedChildNoScaleTransform = child->drawTransform();
// All transforms operate on content rects. The child's content rect
// incorporates device scale, but the childNoScale does not; add it here.
expectedChildNoScaleTransform.scale(deviceScaleFactor);
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedChildNoScaleTransform, childNoScale->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedChildNoScaleTransform, childNoScale->screenSpaceTransform());
}
TEST(LayerTreeHostCommonTest, verifyLayerTransformsInHighDPIAccurateScaleZeroChildPosition)
{
// Verify draw and screen space transforms of layers not in a surface.
MockContentLayerClient delegate;
WebTransformationMatrix identityMatrix;
scoped_refptr<ContentLayer> parent = createDrawableContentLayer(&delegate);
setLayerPropertiesForTesting(parent.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(133, 133), true);
scoped_refptr<ContentLayer> child = createDrawableContentLayer(&delegate);
setLayerPropertiesForTesting(child.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(13, 13), true);
scoped_refptr<NoScaleContentLayer> childNoScale = createNoScaleDrawableContentLayer(&delegate);
setLayerPropertiesForTesting(childNoScale.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(13, 13), true);
parent->addChild(child);
parent->addChild(childNoScale);
std::vector<scoped_refptr<Layer> > renderSurfaceLayerList;
int dummyMaxTextureSize = 512;
const float deviceScaleFactor = 1.7f;
const float pageScaleFactor = 1;
LayerTreeHostCommon::calculateDrawTransforms(parent.get(), parent->bounds(), deviceScaleFactor, pageScaleFactor, dummyMaxTextureSize, renderSurfaceLayerList);
EXPECT_CONTENTS_SCALE_EQ(deviceScaleFactor * pageScaleFactor, parent);
EXPECT_CONTENTS_SCALE_EQ(deviceScaleFactor * pageScaleFactor, child);
EXPECT_CONTENTS_SCALE_EQ(1, childNoScale);
EXPECT_EQ(1u, renderSurfaceLayerList.size());
// Verify parent transforms
WebTransformationMatrix expectedParentTransform;
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedParentTransform, parent->screenSpaceTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedParentTransform, parent->drawTransform());
// Verify results of transformed parent rects
gfx::RectF parentContentBounds(gfx::PointF(), gfx::SizeF(parent->contentBounds()));
gfx::RectF parentDrawRect = MathUtil::mapClippedRect(parent->drawTransform(), parentContentBounds);
gfx::RectF parentScreenSpaceRect = MathUtil::mapClippedRect(parent->screenSpaceTransform(), parentContentBounds);
gfx::RectF expectedParentDrawRect(gfx::PointF(), parent->bounds());
expectedParentDrawRect.Scale(deviceScaleFactor);
expectedParentDrawRect.set_width(ceil(expectedParentDrawRect.width()));
expectedParentDrawRect.set_height(ceil(expectedParentDrawRect.height()));
EXPECT_FLOAT_RECT_EQ(expectedParentDrawRect, parentDrawRect);
EXPECT_FLOAT_RECT_EQ(expectedParentDrawRect, parentScreenSpaceRect);
// Verify child transforms
WebTransformationMatrix expectedChildTransform;
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedChildTransform, child->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedChildTransform, child->screenSpaceTransform());
// Verify results of transformed child rects
gfx::RectF childContentBounds(gfx::PointF(), gfx::SizeF(child->contentBounds()));
gfx::RectF childDrawRect = MathUtil::mapClippedRect(child->drawTransform(), childContentBounds);
gfx::RectF childScreenSpaceRect = MathUtil::mapClippedRect(child->screenSpaceTransform(), childContentBounds);
gfx::RectF expectedChildDrawRect(gfx::PointF(), child->bounds());
expectedChildDrawRect.Scale(deviceScaleFactor);
expectedChildDrawRect.set_width(ceil(expectedChildDrawRect.width()));
expectedChildDrawRect.set_height(ceil(expectedChildDrawRect.height()));
EXPECT_FLOAT_RECT_EQ(expectedChildDrawRect, childDrawRect);
EXPECT_FLOAT_RECT_EQ(expectedChildDrawRect, childScreenSpaceRect);
// Verify childNoScale transforms
WebTransformationMatrix expectedChildNoScaleTransform = child->drawTransform();
// All transforms operate on content rects. The child's content rect
// incorporates device scale, but the childNoScale does not; add it here.
expectedChildNoScaleTransform.scale(deviceScaleFactor);
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedChildNoScaleTransform, childNoScale->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedChildNoScaleTransform, childNoScale->screenSpaceTransform());
}
TEST(LayerTreeHostCommonTest, verifyContentsScale)
{
MockContentLayerClient delegate;
WebTransformationMatrix identityMatrix;
WebTransformationMatrix parentScaleMatrix;
const double initialParentScale = 1.75;
parentScaleMatrix.scale(initialParentScale);
WebTransformationMatrix childScaleMatrix;
const double initialChildScale = 1.25;
childScaleMatrix.scale(initialChildScale);
float fixedRasterScale = 2.5;
scoped_refptr<ContentLayer> parent = createDrawableContentLayer(&delegate);
setLayerPropertiesForTesting(parent.get(), parentScaleMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(100, 100), true);
scoped_refptr<ContentLayer> childScale = createDrawableContentLayer(&delegate);
setLayerPropertiesForTesting(childScale.get(), childScaleMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(2, 2), gfx::Size(10, 10), true);
scoped_refptr<ContentLayer> childEmpty = createDrawableContentLayer(&delegate);
setLayerPropertiesForTesting(childEmpty.get(), childScaleMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(2, 2), gfx::Size(0, 0), true);
scoped_refptr<NoScaleContentLayer> childNoScale = createNoScaleDrawableContentLayer(&delegate);
setLayerPropertiesForTesting(childNoScale.get(), childScaleMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(12, 12), gfx::Size(10, 10), true);
scoped_refptr<ContentLayer> childNoAutoScale = createDrawableContentLayer(&delegate);
setLayerPropertiesForTesting(childNoAutoScale.get(), childScaleMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(22, 22), gfx::Size(10, 10), true);
childNoAutoScale->setAutomaticallyComputeRasterScale(false);
childNoAutoScale->setRasterScale(fixedRasterScale);
// FIXME: Remove this when pageScaleFactor is applied in the compositor.
// Page scale should not apply to the parent.
parent->setBoundsContainPageScale(true);
parent->addChild(childScale);
parent->addChild(childEmpty);
parent->addChild(childNoScale);
parent->addChild(childNoAutoScale);
std::vector<scoped_refptr<Layer> > renderSurfaceLayerList;
int dummyMaxTextureSize = 512;
double deviceScaleFactor = 2.5;
double pageScaleFactor = 1.5;
// FIXME: Remove this when pageScaleFactor is applied in the compositor.
WebTransformationMatrix pageScaleMatrix;
pageScaleMatrix.scale(pageScaleFactor);
parent->setSublayerTransform(pageScaleMatrix);
LayerTreeHostCommon::calculateDrawTransforms(parent.get(), parent->bounds(), deviceScaleFactor, pageScaleFactor, dummyMaxTextureSize, renderSurfaceLayerList);
EXPECT_CONTENTS_SCALE_EQ(deviceScaleFactor * initialParentScale, parent);
EXPECT_CONTENTS_SCALE_EQ(deviceScaleFactor * pageScaleFactor * initialParentScale * initialChildScale, childScale);
EXPECT_CONTENTS_SCALE_EQ(deviceScaleFactor * pageScaleFactor * initialParentScale * initialChildScale, childEmpty);
EXPECT_CONTENTS_SCALE_EQ(1, childNoScale);
EXPECT_CONTENTS_SCALE_EQ(deviceScaleFactor * pageScaleFactor * fixedRasterScale, childNoAutoScale);
// The parent is scaled up and shouldn't need to scale during draw. The child that can scale its contents should
// also not need to scale during draw. This shouldn't change if the child has empty bounds. The other children should.
EXPECT_FLOAT_EQ(1, parent->drawTransform().m11());
EXPECT_FLOAT_EQ(1, parent->drawTransform().m22());
EXPECT_FLOAT_EQ(1, childScale->drawTransform().m11());
EXPECT_FLOAT_EQ(1, childScale->drawTransform().m22());
EXPECT_FLOAT_EQ(1, childEmpty->drawTransform().m11());
EXPECT_FLOAT_EQ(1, childEmpty->drawTransform().m22());
EXPECT_FLOAT_EQ(deviceScaleFactor * pageScaleFactor * initialParentScale * initialChildScale, childNoScale->drawTransform().m11());
EXPECT_FLOAT_EQ(deviceScaleFactor * pageScaleFactor * initialParentScale * initialChildScale, childNoScale->drawTransform().m22());
EXPECT_FLOAT_EQ(initialParentScale * initialChildScale / fixedRasterScale, childNoAutoScale->drawTransform().m11());
EXPECT_FLOAT_EQ(initialParentScale * initialChildScale / fixedRasterScale, childNoAutoScale->drawTransform().m22());
// If the transform changes, we expect the contentsScale to remain unchanged.
childScale->setTransform(identityMatrix);
childEmpty->setTransform(identityMatrix);
renderSurfaceLayerList.clear();
LayerTreeHostCommon::calculateDrawTransforms(parent.get(), parent->bounds(), deviceScaleFactor, pageScaleFactor, dummyMaxTextureSize, renderSurfaceLayerList);
EXPECT_CONTENTS_SCALE_EQ(deviceScaleFactor * initialParentScale, parent);
EXPECT_CONTENTS_SCALE_EQ(deviceScaleFactor * pageScaleFactor * initialParentScale * initialChildScale, childScale);
EXPECT_CONTENTS_SCALE_EQ(deviceScaleFactor * pageScaleFactor * initialParentScale * initialChildScale, childEmpty);
EXPECT_CONTENTS_SCALE_EQ(1, childNoScale);
// But if the deviceScaleFactor or pageScaleFactor changes, then it should be updated, but using the initial transform.
deviceScaleFactor = 2.25;
pageScaleFactor = 1.25;
// FIXME: Remove this when pageScaleFactor is applied in the compositor.
pageScaleMatrix = identityMatrix;
pageScaleMatrix.scale(pageScaleFactor);
parent->setSublayerTransform(pageScaleMatrix);
renderSurfaceLayerList.clear();
LayerTreeHostCommon::calculateDrawTransforms(parent.get(), parent->bounds(), deviceScaleFactor, pageScaleFactor, dummyMaxTextureSize, renderSurfaceLayerList);
EXPECT_CONTENTS_SCALE_EQ(deviceScaleFactor * initialParentScale, parent);
EXPECT_CONTENTS_SCALE_EQ(deviceScaleFactor * pageScaleFactor * initialParentScale * initialChildScale, childScale);
EXPECT_CONTENTS_SCALE_EQ(deviceScaleFactor * pageScaleFactor * initialParentScale * initialChildScale, childEmpty);
EXPECT_CONTENTS_SCALE_EQ(1, childNoScale);
EXPECT_CONTENTS_SCALE_EQ(deviceScaleFactor * pageScaleFactor * fixedRasterScale, childNoAutoScale);
}
TEST(LayerTreeHostCommonTest, verifySmallContentsScale)
{
MockContentLayerClient delegate;
WebTransformationMatrix identityMatrix;
WebTransformationMatrix parentScaleMatrix;
const double initialParentScale = 1.75;
parentScaleMatrix.scale(initialParentScale);
WebTransformationMatrix childScaleMatrix;
const double initialChildScale = 0.25;
childScaleMatrix.scale(initialChildScale);
scoped_refptr<ContentLayer> parent = createDrawableContentLayer(&delegate);
setLayerPropertiesForTesting(parent.get(), parentScaleMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(100, 100), true);
scoped_refptr<ContentLayer> childScale = createDrawableContentLayer(&delegate);
setLayerPropertiesForTesting(childScale.get(), childScaleMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(2, 2), gfx::Size(10, 10), true);
// FIXME: Remove this when pageScaleFactor is applied in the compositor.
// Page scale should not apply to the parent.
parent->setBoundsContainPageScale(true);
parent->addChild(childScale);
std::vector<scoped_refptr<Layer> > renderSurfaceLayerList;
int dummyMaxTextureSize = 512;
double deviceScaleFactor = 2.5;
double pageScaleFactor = 0.01;
// FIXME: Remove this when pageScaleFactor is applied in the compositor.
WebTransformationMatrix pageScaleMatrix;
pageScaleMatrix.scale(pageScaleFactor);
parent->setSublayerTransform(pageScaleMatrix);
LayerTreeHostCommon::calculateDrawTransforms(parent.get(), parent->bounds(), deviceScaleFactor, pageScaleFactor, dummyMaxTextureSize, renderSurfaceLayerList);
EXPECT_CONTENTS_SCALE_EQ(deviceScaleFactor * initialParentScale, parent);
// The child's scale is < 1, so we should not save and use that scale factor.
EXPECT_CONTENTS_SCALE_EQ(deviceScaleFactor * pageScaleFactor * 1, childScale);
// When chilld's total scale becomes >= 1, we should save and use that scale factor.
childScaleMatrix.makeIdentity();
const double finalChildScale = 0.75;
childScaleMatrix.scale(finalChildScale);
childScale->setTransform(childScaleMatrix);
renderSurfaceLayerList.clear();
LayerTreeHostCommon::calculateDrawTransforms(parent.get(), parent->bounds(), deviceScaleFactor, pageScaleFactor, dummyMaxTextureSize, renderSurfaceLayerList);
EXPECT_CONTENTS_SCALE_EQ(deviceScaleFactor * initialParentScale, parent);
EXPECT_CONTENTS_SCALE_EQ(deviceScaleFactor * pageScaleFactor * initialParentScale * finalChildScale, childScale);
}
TEST(LayerTreeHostCommonTest, verifyContentsScaleForSurfaces)
{
MockContentLayerClient delegate;
WebTransformationMatrix identityMatrix;
WebTransformationMatrix parentScaleMatrix;
const double initialParentScale = 2;
parentScaleMatrix.scale(initialParentScale);
WebTransformationMatrix childScaleMatrix;
const double initialChildScale = 3;
childScaleMatrix.scale(initialChildScale);
float fixedRasterScale = 4;
scoped_refptr<ContentLayer> parent = createDrawableContentLayer(&delegate);
setLayerPropertiesForTesting(parent.get(), parentScaleMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(100, 100), true);
scoped_refptr<ContentLayer> surfaceScale = createDrawableContentLayer(&delegate);
setLayerPropertiesForTesting(surfaceScale.get(), childScaleMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(2, 2), gfx::Size(10, 10), true);
scoped_refptr<ContentLayer> surfaceScaleChildScale = createDrawableContentLayer(&delegate);
setLayerPropertiesForTesting(surfaceScaleChildScale.get(), childScaleMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(10, 10), true);
scoped_refptr<NoScaleContentLayer> surfaceScaleChildNoScale = createNoScaleDrawableContentLayer(&delegate);
setLayerPropertiesForTesting(surfaceScaleChildNoScale.get(), childScaleMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(10, 10), true);
scoped_refptr<NoScaleContentLayer> surfaceNoScale = createNoScaleDrawableContentLayer(&delegate);
setLayerPropertiesForTesting(surfaceNoScale.get(), childScaleMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(12, 12), gfx::Size(10, 10), true);
scoped_refptr<ContentLayer> surfaceNoScaleChildScale = createDrawableContentLayer(&delegate);
setLayerPropertiesForTesting(surfaceNoScaleChildScale.get(), childScaleMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(10, 10), true);
scoped_refptr<NoScaleContentLayer> surfaceNoScaleChildNoScale = createNoScaleDrawableContentLayer(&delegate);
setLayerPropertiesForTesting(surfaceNoScaleChildNoScale.get(), childScaleMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(10, 10), true);
scoped_refptr<ContentLayer> surfaceNoAutoScale = createDrawableContentLayer(&delegate);
setLayerPropertiesForTesting(surfaceNoAutoScale.get(), childScaleMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(22, 22), gfx::Size(10, 10), true);
surfaceNoAutoScale->setAutomaticallyComputeRasterScale(false);
surfaceNoAutoScale->setRasterScale(fixedRasterScale);
scoped_refptr<ContentLayer> surfaceNoAutoScaleChildScale = createDrawableContentLayer(&delegate);
setLayerPropertiesForTesting(surfaceNoAutoScaleChildScale.get(), childScaleMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(10, 10), true);
scoped_refptr<NoScaleContentLayer> surfaceNoAutoScaleChildNoScale = createNoScaleDrawableContentLayer(&delegate);
setLayerPropertiesForTesting(surfaceNoAutoScaleChildNoScale.get(), childScaleMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(10, 10), true);
// FIXME: Remove this when pageScaleFactor is applied in the compositor.
// Page scale should not apply to the parent.
parent->setBoundsContainPageScale(true);
parent->addChild(surfaceScale);
parent->addChild(surfaceNoScale);
parent->addChild(surfaceNoAutoScale);
surfaceScale->setForceRenderSurface(true);
surfaceScale->addChild(surfaceScaleChildScale);
surfaceScale->addChild(surfaceScaleChildNoScale);
surfaceNoScale->setForceRenderSurface(true);
surfaceNoScale->addChild(surfaceNoScaleChildScale);
surfaceNoScale->addChild(surfaceNoScaleChildNoScale);
surfaceNoAutoScale->setForceRenderSurface(true);
surfaceNoAutoScale->addChild(surfaceNoAutoScaleChildScale);
surfaceNoAutoScale->addChild(surfaceNoAutoScaleChildNoScale);
std::vector<scoped_refptr<Layer> > renderSurfaceLayerList;
int dummyMaxTextureSize = 512;
double deviceScaleFactor = 5;
double pageScaleFactor = 7;
// FIXME: Remove this when pageScaleFactor is applied in the compositor.
WebTransformationMatrix pageScaleMatrix;
pageScaleMatrix.scale(pageScaleFactor);
parent->setSublayerTransform(pageScaleMatrix);
LayerTreeHostCommon::calculateDrawTransforms(parent.get(), parent->bounds(), deviceScaleFactor, pageScaleFactor, dummyMaxTextureSize, renderSurfaceLayerList);
EXPECT_CONTENTS_SCALE_EQ(deviceScaleFactor * initialParentScale, parent);
EXPECT_CONTENTS_SCALE_EQ(deviceScaleFactor * pageScaleFactor * initialParentScale * initialChildScale, surfaceScale);
EXPECT_CONTENTS_SCALE_EQ(1, surfaceNoScale);
EXPECT_CONTENTS_SCALE_EQ(deviceScaleFactor * pageScaleFactor * fixedRasterScale, surfaceNoAutoScale);
EXPECT_CONTENTS_SCALE_EQ(deviceScaleFactor * pageScaleFactor * initialParentScale * initialChildScale * initialChildScale, surfaceScaleChildScale);
EXPECT_CONTENTS_SCALE_EQ(1, surfaceScaleChildNoScale);
EXPECT_CONTENTS_SCALE_EQ(deviceScaleFactor * pageScaleFactor * initialParentScale * initialChildScale * initialChildScale, surfaceNoScaleChildScale);
EXPECT_CONTENTS_SCALE_EQ(1, surfaceNoScaleChildNoScale);
EXPECT_CONTENTS_SCALE_EQ(deviceScaleFactor * pageScaleFactor * initialParentScale * initialChildScale * initialChildScale, surfaceNoAutoScaleChildScale);
EXPECT_CONTENTS_SCALE_EQ(1, surfaceNoAutoScaleChildNoScale);
// The parent is scaled up and shouldn't need to scale during draw.
EXPECT_FLOAT_EQ(1, parent->drawTransform().m11());
EXPECT_FLOAT_EQ(1, parent->drawTransform().m22());
// RenderSurfaces should always be 1:1 with their target.
EXPECT_FLOAT_EQ(1, surfaceScale->renderSurface()->drawTransform().m11());
EXPECT_FLOAT_EQ(1, surfaceScale->renderSurface()->drawTransform().m22());
// The surfaceScale can apply contents scale so the layer shouldn't need to scale during draw.
EXPECT_FLOAT_EQ(1, surfaceScale->drawTransform().m11());
EXPECT_FLOAT_EQ(1, surfaceScale->drawTransform().m22());
// The surfaceScaleChildScale can apply contents scale so it shouldn't need to scale during draw.
EXPECT_FLOAT_EQ(1, surfaceScaleChildScale->drawTransform().m11());
EXPECT_FLOAT_EQ(1, surfaceScaleChildScale->drawTransform().m22());
// The surfaceScaleChildNoScale can not apply contents scale, so it needs to be scaled during draw.
EXPECT_FLOAT_EQ(deviceScaleFactor * pageScaleFactor * initialParentScale * initialChildScale * initialChildScale, surfaceScaleChildNoScale->drawTransform().m11());
EXPECT_FLOAT_EQ(deviceScaleFactor * pageScaleFactor * initialParentScale * initialChildScale * initialChildScale, surfaceScaleChildNoScale->drawTransform().m22());
// RenderSurfaces should always be 1:1 with their target.
EXPECT_FLOAT_EQ(1, surfaceNoScale->renderSurface()->drawTransform().m11());
EXPECT_FLOAT_EQ(1, surfaceNoScale->renderSurface()->drawTransform().m22());
// The surfaceNoScale layer can not apply contents scale, so it needs to be scaled during draw.
EXPECT_FLOAT_EQ(deviceScaleFactor * pageScaleFactor * initialParentScale * initialChildScale, surfaceNoScale->drawTransform().m11());
EXPECT_FLOAT_EQ(deviceScaleFactor * pageScaleFactor * initialParentScale * initialChildScale, surfaceNoScale->drawTransform().m22());
// The surfaceScaleChildScale can apply contents scale so it shouldn't need to scale during draw.
EXPECT_FLOAT_EQ(1, surfaceNoScaleChildScale->drawTransform().m11());
EXPECT_FLOAT_EQ(1, surfaceNoScaleChildScale->drawTransform().m22());
// The surfaceScaleChildNoScale can not apply contents scale, so it needs to be scaled during draw.
EXPECT_FLOAT_EQ(deviceScaleFactor * pageScaleFactor * initialParentScale * initialChildScale * initialChildScale, surfaceNoScaleChildNoScale->drawTransform().m11());
EXPECT_FLOAT_EQ(deviceScaleFactor * pageScaleFactor * initialParentScale * initialChildScale * initialChildScale, surfaceNoScaleChildNoScale->drawTransform().m22());
// RenderSurfaces should always be 1:1 with their target.
EXPECT_FLOAT_EQ(1, surfaceNoAutoScale->renderSurface()->drawTransform().m11());
EXPECT_FLOAT_EQ(1, surfaceNoAutoScale->renderSurface()->drawTransform().m22());
// The surfaceNoAutoScale layer has a fixed contents scale, so it needs to be scaled during draw.
EXPECT_FLOAT_EQ(deviceScaleFactor * pageScaleFactor * initialParentScale * initialChildScale / (deviceScaleFactor * pageScaleFactor * fixedRasterScale), surfaceNoAutoScale->drawTransform().m11());
EXPECT_FLOAT_EQ(deviceScaleFactor * pageScaleFactor * initialParentScale * initialChildScale / (deviceScaleFactor * pageScaleFactor * fixedRasterScale), surfaceNoAutoScale->drawTransform().m22());
// The surfaceScaleChildScale can apply contents scale so it shouldn't need to scale during draw.
EXPECT_FLOAT_EQ(1, surfaceNoAutoScaleChildScale->drawTransform().m11());
EXPECT_FLOAT_EQ(1, surfaceNoAutoScaleChildScale->drawTransform().m22());
// The surfaceScaleChildNoScale can not apply contents scale, so it needs to be scaled during draw.
EXPECT_FLOAT_EQ(deviceScaleFactor * pageScaleFactor * initialParentScale * initialChildScale * initialChildScale, surfaceNoAutoScaleChildNoScale->drawTransform().m11());
EXPECT_FLOAT_EQ(deviceScaleFactor * pageScaleFactor * initialParentScale * initialChildScale * initialChildScale, surfaceNoAutoScaleChildNoScale->drawTransform().m22());
}
TEST(LayerTreeHostCommonTest, verifyContentsScaleForAnimatingLayer)
{
MockContentLayerClient delegate;
WebTransformationMatrix identityMatrix;
WebTransformationMatrix parentScaleMatrix;
const double initialParentScale = 1.75;
parentScaleMatrix.scale(initialParentScale);
WebTransformationMatrix childScaleMatrix;
const double initialChildScale = 1.25;
childScaleMatrix.scale(initialChildScale);
scoped_refptr<ContentLayer> parent = createDrawableContentLayer(&delegate);
setLayerPropertiesForTesting(parent.get(), parentScaleMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(100, 100), true);
scoped_refptr<ContentLayer> childScale = createDrawableContentLayer(&delegate);
setLayerPropertiesForTesting(childScale.get(), childScaleMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(2, 2), gfx::Size(10, 10), true);
parent->addChild(childScale);
// Now put an animating transform on child.
int animationId = addAnimatedTransformToController(*childScale->layerAnimationController(), 10, 30, 0);
std::vector<scoped_refptr<Layer> > renderSurfaceLayerList;
int dummyMaxTextureSize = 512;
LayerTreeHostCommon::calculateDrawTransforms(parent.get(), parent->bounds(), 1, 1, dummyMaxTextureSize, renderSurfaceLayerList);
EXPECT_CONTENTS_SCALE_EQ(initialParentScale, parent);
// The layers with animating transforms should not compute a contentsScale other than 1 until they finish animating.
EXPECT_CONTENTS_SCALE_EQ(1, childScale);
// Remove the animation, now it can save a raster scale.
childScale->layerAnimationController()->removeAnimation(animationId);
LayerTreeHostCommon::calculateDrawTransforms(parent.get(), parent->bounds(), 1, 1, dummyMaxTextureSize, renderSurfaceLayerList);
EXPECT_CONTENTS_SCALE_EQ(initialParentScale, parent);
// The layers with animating transforms should not compute a contentsScale other than 1 until they finish animating.
EXPECT_CONTENTS_SCALE_EQ(initialParentScale * initialChildScale, childScale);
}
TEST(LayerTreeHostCommonTest, verifyRenderSurfaceTransformsInHighDPI)
{
MockContentLayerClient delegate;
WebTransformationMatrix identityMatrix;
scoped_refptr<ContentLayer> parent = createDrawableContentLayer(&delegate);
setLayerPropertiesForTesting(parent.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(30, 30), true);
scoped_refptr<ContentLayer> child = createDrawableContentLayer(&delegate);
setLayerPropertiesForTesting(child.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(2, 2), gfx::Size(10, 10), true);
WebTransformationMatrix replicaTransform;
replicaTransform.scaleNonUniform(1, -1);
scoped_refptr<ContentLayer> replica = createDrawableContentLayer(&delegate);
setLayerPropertiesForTesting(replica.get(), replicaTransform, identityMatrix, gfx::PointF(0, 0), gfx::PointF(2, 2), gfx::Size(10, 10), true);
// This layer should end up in the same surface as child, with the same draw
// and screen space transforms.
scoped_refptr<ContentLayer> duplicateChildNonOwner = createDrawableContentLayer(&delegate);
setLayerPropertiesForTesting(duplicateChildNonOwner.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(10, 10), true);
parent->addChild(child);
child->addChild(duplicateChildNonOwner);
child->setReplicaLayer(replica.get());
std::vector<scoped_refptr<Layer> > renderSurfaceLayerList;
int dummyMaxTextureSize = 512;
const double deviceScaleFactor = 1.5;
parent->setContentsScale(deviceScaleFactor);
child->setContentsScale(deviceScaleFactor);
duplicateChildNonOwner->setContentsScale(deviceScaleFactor);
replica->setContentsScale(deviceScaleFactor);
LayerTreeHostCommon::calculateDrawTransforms(parent.get(), parent->bounds(), deviceScaleFactor, 1, dummyMaxTextureSize, renderSurfaceLayerList);
// We should have two render surfaces. The root's render surface and child's
// render surface (it needs one because it has a replica layer).
EXPECT_EQ(2u, renderSurfaceLayerList.size());
WebTransformationMatrix expectedParentTransform;
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedParentTransform, parent->screenSpaceTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedParentTransform, parent->drawTransform());
WebTransformationMatrix expectedDrawTransform;
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedDrawTransform, child->drawTransform());
WebTransformationMatrix expectedScreenSpaceTransform;
expectedScreenSpaceTransform.translate(deviceScaleFactor * child->position().x(), deviceScaleFactor * child->position().y());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedScreenSpaceTransform, child->screenSpaceTransform());
WebTransformationMatrix expectedDuplicateChildDrawTransform = child->drawTransform();
EXPECT_TRANSFORMATION_MATRIX_EQ(child->drawTransform(), duplicateChildNonOwner->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(child->screenSpaceTransform(), duplicateChildNonOwner->screenSpaceTransform());
EXPECT_RECT_EQ(child->drawableContentRect(), duplicateChildNonOwner->drawableContentRect());
EXPECT_EQ(child->contentBounds(), duplicateChildNonOwner->contentBounds());
WebTransformationMatrix expectedRenderSurfaceDrawTransform;
expectedRenderSurfaceDrawTransform.translate(deviceScaleFactor * child->position().x(), deviceScaleFactor * child->position().y());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedRenderSurfaceDrawTransform, child->renderSurface()->drawTransform());
WebTransformationMatrix expectedSurfaceDrawTransform;
expectedSurfaceDrawTransform.translate(deviceScaleFactor * 2, deviceScaleFactor * 2);
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedSurfaceDrawTransform, child->renderSurface()->drawTransform());
WebTransformationMatrix expectedSurfaceScreenSpaceTransform;
expectedSurfaceScreenSpaceTransform.translate(deviceScaleFactor * 2, deviceScaleFactor * 2);
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedSurfaceScreenSpaceTransform, child->renderSurface()->screenSpaceTransform());
WebTransformationMatrix expectedReplicaDrawTransform;
expectedReplicaDrawTransform.setM22(-1);
expectedReplicaDrawTransform.setM41(6);
expectedReplicaDrawTransform.setM42(6);
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedReplicaDrawTransform, child->renderSurface()->replicaDrawTransform());
WebTransformationMatrix expectedReplicaScreenSpaceTransform;
expectedReplicaScreenSpaceTransform.setM22(-1);
expectedReplicaScreenSpaceTransform.setM41(6);
expectedReplicaScreenSpaceTransform.setM42(6);
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedReplicaScreenSpaceTransform, child->renderSurface()->replicaScreenSpaceTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedReplicaScreenSpaceTransform, child->renderSurface()->replicaScreenSpaceTransform());
}
TEST(LayerTreeHostCommonTest, verifyRenderSurfaceTransformsInHighDPIAccurateScaleZeroPosition)
{
MockContentLayerClient delegate;
WebTransformationMatrix identityMatrix;
scoped_refptr<ContentLayer> parent = createDrawableContentLayer(&delegate);
setLayerPropertiesForTesting(parent.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(33, 31), true);
scoped_refptr<ContentLayer> child = createDrawableContentLayer(&delegate);
setLayerPropertiesForTesting(child.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(13, 11), true);
WebTransformationMatrix replicaTransform;
replicaTransform.scaleNonUniform(1, -1);
scoped_refptr<ContentLayer> replica = createDrawableContentLayer(&delegate);
setLayerPropertiesForTesting(replica.get(), replicaTransform, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(13, 11), true);
// This layer should end up in the same surface as child, with the same draw
// and screen space transforms.
scoped_refptr<ContentLayer> duplicateChildNonOwner = createDrawableContentLayer(&delegate);
setLayerPropertiesForTesting(duplicateChildNonOwner.get(), identityMatrix, identityMatrix, gfx::PointF(0, 0), gfx::PointF(0, 0), gfx::Size(13, 11), true);
parent->addChild(child);
child->addChild(duplicateChildNonOwner);
child->setReplicaLayer(replica.get());
std::vector<scoped_refptr<Layer> > renderSurfaceLayerList;
int dummyMaxTextureSize = 512;
const float deviceScaleFactor = 1.7f;
parent->setContentsScale(deviceScaleFactor);
child->setContentsScale(deviceScaleFactor);
duplicateChildNonOwner->setContentsScale(deviceScaleFactor);
replica->setContentsScale(deviceScaleFactor);
LayerTreeHostCommon::calculateDrawTransforms(parent.get(), parent->bounds(), deviceScaleFactor, 1, dummyMaxTextureSize, renderSurfaceLayerList);
// We should have two render surfaces. The root's render surface and child's
// render surface (it needs one because it has a replica layer).
EXPECT_EQ(2u, renderSurfaceLayerList.size());
WebTransformationMatrix identityTransform;
EXPECT_TRANSFORMATION_MATRIX_EQ(identityTransform, parent->screenSpaceTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(identityTransform, parent->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(identityTransform, child->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(identityTransform, child->screenSpaceTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(identityTransform, duplicateChildNonOwner->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(identityTransform, duplicateChildNonOwner->screenSpaceTransform());
EXPECT_RECT_EQ(child->drawableContentRect(), duplicateChildNonOwner->drawableContentRect());
EXPECT_EQ(child->contentBounds(), duplicateChildNonOwner->contentBounds());
EXPECT_TRANSFORMATION_MATRIX_EQ(identityTransform, child->renderSurface()->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(identityTransform, child->renderSurface()->drawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(identityTransform, child->renderSurface()->screenSpaceTransform());
WebTransformationMatrix expectedReplicaDrawTransform;
expectedReplicaDrawTransform.setM22(-1);
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedReplicaDrawTransform, child->renderSurface()->replicaDrawTransform());
WebTransformationMatrix expectedReplicaScreenSpaceTransform;
expectedReplicaScreenSpaceTransform.setM22(-1);
EXPECT_TRANSFORMATION_MATRIX_EQ(expectedReplicaScreenSpaceTransform, child->renderSurface()->replicaScreenSpaceTransform());
}
TEST(LayerTreeHostCommonTest, verifySubtreeSearch)
{
scoped_refptr<Layer> root = Layer::create();
scoped_refptr<Layer> child = Layer::create();
scoped_refptr<Layer> grandChild = Layer::create();
scoped_refptr<Layer> maskLayer = Layer::create();
scoped_refptr<Layer> replicaLayer = Layer::create();
grandChild->setReplicaLayer(replicaLayer.get());
child->addChild(grandChild.get());
child->setMaskLayer(maskLayer.get());
root->addChild(child.get());
int nonexistentId = -1;
EXPECT_EQ(root, LayerTreeHostCommon::findLayerInSubtree(root.get(), root->id()));
EXPECT_EQ(child, LayerTreeHostCommon::findLayerInSubtree(root.get(), child->id()));
EXPECT_EQ(grandChild, LayerTreeHostCommon::findLayerInSubtree(root.get(), grandChild->id()));
EXPECT_EQ(maskLayer, LayerTreeHostCommon::findLayerInSubtree(root.get(), maskLayer->id()));
EXPECT_EQ(replicaLayer, LayerTreeHostCommon::findLayerInSubtree(root.get(), replicaLayer->id()));
EXPECT_EQ(0, LayerTreeHostCommon::findLayerInSubtree(root.get(), nonexistentId));
}
} // namespace
} // namespace cc
|