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
cc / trees / damage_tracker_unittest.cc [blame]
// Copyright 2011 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "cc/trees/damage_tracker.h"
#include <stddef.h>
#include <limits>
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/unguessable_token.h"
#include "cc/base/math_util.h"
#include "cc/layers/layer_impl.h"
#include "cc/layers/view_transition_content_layer_impl.h"
#include "cc/paint/filter_operation.h"
#include "cc/paint/filter_operations.h"
#include "cc/test/fake_picture_layer_impl.h"
#include "cc/test/fake_raster_source.h"
#include "cc/test/layer_tree_impl_test_base.h"
#include "cc/test/property_tree_test_utils.h"
#include "cc/trees/effect_node.h"
#include "cc/trees/layer_tree_impl.h"
#include "cc/trees/single_thread_proxy.h"
#include "cc/trees/transform_node.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/geometry/quad_f.h"
#include "ui/gfx/geometry/rect_conversions.h"
#include "ui/gfx/geometry/vector2d_f.h"
namespace cc {
namespace {
class TestLayerImpl : public LayerImpl {
public:
static std::unique_ptr<TestLayerImpl> Create(LayerTreeImpl* tree_impl,
int id) {
return base::WrapUnique(new TestLayerImpl(tree_impl, id));
}
void AddDamageRect(const gfx::Rect& damage_rect);
void SetDamageReasons(DamageReasonSet reasons);
// LayerImpl overrides.
gfx::Rect GetDamageRect() const override;
DamageReasonSet GetDamageReasons() const override;
void ResetChangeTracking() override;
private:
TestLayerImpl(LayerTreeImpl* tree_impl, int id);
gfx::Rect damage_rect_;
DamageReasonSet damage_reasons_;
};
TestLayerImpl::TestLayerImpl(LayerTreeImpl* tree_impl, int id)
: LayerImpl(tree_impl, id) {}
void TestLayerImpl::AddDamageRect(const gfx::Rect& damage_rect) {
damage_rect_.Union(damage_rect);
}
void TestLayerImpl::SetDamageReasons(DamageReasonSet reasons) {
damage_reasons_ = reasons;
}
gfx::Rect TestLayerImpl::GetDamageRect() const {
return damage_rect_;
}
DamageReasonSet TestLayerImpl::GetDamageReasons() const {
return damage_reasons_;
}
void TestLayerImpl::ResetChangeTracking() {
LayerImpl::ResetChangeTracking();
damage_rect_.SetRect(0, 0, 0, 0);
}
class TestViewTransitionContentLayerImpl
: public ViewTransitionContentLayerImpl {
public:
static std::unique_ptr<TestViewTransitionContentLayerImpl> Create(
LayerTreeImpl* tree_impl,
int id,
const viz::ViewTransitionElementResourceId& resource_id,
bool is_live_content_layer) {
return base::WrapUnique(new TestViewTransitionContentLayerImpl(
tree_impl, id, resource_id, is_live_content_layer));
}
void AddDamageRect(const gfx::Rect& damage_rect);
// LayerImpl overrides.
gfx::Rect GetDamageRect() const override;
void ResetChangeTracking() override;
private:
TestViewTransitionContentLayerImpl(
LayerTreeImpl* tree_impl,
int id,
const viz::ViewTransitionElementResourceId& resource_id,
bool is_live_content_layer);
gfx::Rect damage_rect_;
};
TestViewTransitionContentLayerImpl::TestViewTransitionContentLayerImpl(
LayerTreeImpl* tree_impl,
int id,
const viz::ViewTransitionElementResourceId& resource_id,
bool is_live_content_layer)
: ViewTransitionContentLayerImpl(tree_impl,
id,
resource_id,
is_live_content_layer,
gfx::RectF()) {}
void TestViewTransitionContentLayerImpl::AddDamageRect(
const gfx::Rect& damage_rect) {
damage_rect_.Union(damage_rect);
}
gfx::Rect TestViewTransitionContentLayerImpl::GetDamageRect() const {
return damage_rect_;
}
void TestViewTransitionContentLayerImpl::ResetChangeTracking() {
LayerImpl::ResetChangeTracking();
damage_rect_.SetRect(0, 0, 0, 0);
}
void ClearDamageForAllSurfaces(LayerImpl* root) {
for (auto* layer : *root->layer_tree_impl()) {
if (GetRenderSurface(layer))
GetRenderSurface(layer)->damage_tracker()->DidDrawDamagedArea();
layer->ResetChangeTracking();
}
}
void SetCopyRequest(LayerImpl* root) {
auto* root_node =
root->layer_tree_impl()->property_trees()->effect_tree_mutable().Node(
root->effect_tree_index());
root_node->has_copy_request = true;
root->layer_tree_impl()
->property_trees()
->effect_tree_mutable()
.set_needs_update(true);
}
class DamageTrackerTest : public LayerTreeImplTestBase, public testing::Test {
public:
DamageTrackerTest()
: LayerTreeImplTestBase(CommitToActiveTreeLayerListSettings()) {}
LayerImpl* CreateTestTreeWithOneSurface(int number_of_children) {
ClearLayersAndProperties();
LayerImpl* root = root_layer();
root->SetBounds(gfx::Size(500, 500));
root->layer_tree_impl()->SetDeviceViewportRect(gfx::Rect(root->bounds()));
root->SetDrawsContent(true);
SetupRootProperties(root);
child_layers_.resize(number_of_children);
for (int i = 0; i < number_of_children; ++i) {
auto* child = AddLayerInActiveTree<TestLayerImpl>();
child->SetBounds(gfx::Size(30, 30));
child->SetDrawsContent(true);
child_layers_[i] = child;
CopyProperties(root, child);
child->SetOffsetToTransformParent(gfx::Vector2dF(100.f, 100.f));
}
SetElementIdsForTesting();
return root;
}
LayerImpl* CreateTestTreeWithTwoSurfaces() {
// This test tree has two render surfaces: one for the root, and one for
// child1. Additionally, the root has a second child layer, and child1 has
// two children of its own.
ClearLayersAndProperties();
LayerImpl* root = root_layer();
root->SetBounds(gfx::Size(500, 500));
root->layer_tree_impl()->SetDeviceViewportRect(gfx::Rect(root->bounds()));
root->SetDrawsContent(true);
SetupRootProperties(root);
child1_ = AddLayerInActiveTree<TestLayerImpl>();
grand_child1_ = AddLayerInActiveTree<TestLayerImpl>();
grand_child2_ = AddLayerInActiveTree<TestLayerImpl>();
child2_ = AddLayerInActiveTree<TestLayerImpl>();
SetElementIdsForTesting();
child1_->SetBounds(gfx::Size(30, 30));
// With a child that draws_content, opacity will cause the layer to create
// its own RenderSurface. This layer does not draw, but is intended to
// create its own RenderSurface.
child1_->SetDrawsContent(false);
CopyProperties(root, child1_);
CreateTransformNode(child1_).post_translation =
gfx::Vector2dF(100.f, 100.f);
CreateEffectNode(child1_).render_surface_reason =
RenderSurfaceReason::kTest;
grand_child1_->SetBounds(gfx::Size(6, 8));
grand_child1_->SetDrawsContent(true);
CopyProperties(child1_, grand_child1_);
grand_child1_->SetOffsetToTransformParent(gfx::Vector2dF(200.f, 200.f));
grand_child2_->SetBounds(gfx::Size(6, 8));
grand_child2_->SetDrawsContent(true);
CopyProperties(child1_, grand_child2_);
grand_child2_->SetOffsetToTransformParent(gfx::Vector2dF(190.f, 190.f));
child2_->SetBounds(gfx::Size(18, 18));
child2_->SetDrawsContent(true);
CopyProperties(root, child2_);
child2_->SetOffsetToTransformParent(gfx::Vector2dF(11.f, 11.f));
return root;
}
LayerImpl* CreateTestTreeWithFourSurfaces() {
// This test tree has four render surfaces: one each for the root, child1_,
// grand_child3_ and grand_child4_. child1_ has four children of its own.
// Additionally, the root has a second child layer.
// child1_ has a screen space rect 270,270 36x38, from
// - grand_child1_ 300,300, 6x8
// - grand_child2_ 290,290, 6x8
// - grand_child3_ 270,270, 6x8
// - grand_child4_ 280,280, 15x16
// child2_ has a screen space rect 11,11 18x18
ClearLayersAndProperties();
LayerImpl* root = root_layer();
root->SetBounds(gfx::Size(500, 500));
root->layer_tree_impl()->SetDeviceViewportRect(gfx::Rect(root->bounds()));
root->SetDrawsContent(true);
SetupRootProperties(root);
child1_ = AddLayerInActiveTree<TestLayerImpl>();
grand_child1_ = AddLayerInActiveTree<TestLayerImpl>();
grand_child2_ = AddLayerInActiveTree<TestLayerImpl>();
grand_child3_ = AddLayerInActiveTree<TestLayerImpl>();
grand_child4_ = AddLayerInActiveTree<TestLayerImpl>();
child2_ = AddLayerInActiveTree<TestLayerImpl>();
SetElementIdsForTesting();
child1_->SetBounds(gfx::Size(30, 30));
// With a child that draws_content, opacity will cause the layer to create
// its own RenderSurface. This layer does not draw, but is intended to
// create its own RenderSurface.
child1_->SetDrawsContent(false);
CopyProperties(root, child1_);
CreateTransformNode(child1_).post_translation =
gfx::Vector2dF(100.f, 100.f);
CreateEffectNode(child1_).render_surface_reason =
RenderSurfaceReason::kTest;
grand_child1_->SetBounds(gfx::Size(6, 8));
grand_child1_->SetDrawsContent(true);
CopyProperties(child1_, grand_child1_);
grand_child1_->SetOffsetToTransformParent(gfx::Vector2dF(200.f, 200.f));
grand_child2_->SetBounds(gfx::Size(6, 8));
grand_child2_->SetDrawsContent(true);
CopyProperties(child1_, grand_child2_);
grand_child2_->SetOffsetToTransformParent(gfx::Vector2dF(190.f, 190.f));
grand_child3_->SetBounds(gfx::Size(6, 8));
grand_child3_->SetDrawsContent(true);
CopyProperties(child1_, grand_child3_);
CreateEffectNode(grand_child3_).render_surface_reason =
RenderSurfaceReason::kTest;
CreateTransformNode(grand_child3_).post_translation =
gfx::Vector2dF(170.f, 170.f);
grand_child4_->SetBounds(gfx::Size(15, 16));
grand_child4_->SetDrawsContent(true);
CopyProperties(child1_, grand_child4_);
CreateEffectNode(grand_child4_).render_surface_reason =
RenderSurfaceReason::kTest;
CreateTransformNode(grand_child4_).post_translation =
gfx::Vector2dF(180.f, 180.f);
child2_->SetBounds(gfx::Size(18, 18));
child2_->SetDrawsContent(true);
CopyProperties(root, child2_);
child2_->SetOffsetToTransformParent(gfx::Vector2dF(11.f, 11.f));
return root;
}
LayerImpl* CreateAndSetUpTestTreeWithOneSurface(int number_of_children = 1) {
LayerImpl* root = CreateTestTreeWithOneSurface(number_of_children);
// Setup includes going past the first frame which always damages
// everything, so that we can actually perform specific tests.
EmulateDrawingOneFrame(root);
return root;
}
LayerImpl* CreateAndSetUpTestTreeWithTwoSurfaces() {
LayerImpl* root = CreateTestTreeWithTwoSurfaces();
// Setup includes going past the first frame which always damages
// everything, so that we can actually perform specific tests.
EmulateDrawingOneFrame(root);
return root;
}
LayerImpl* CreateAndSetUpTestTreeWithTwoSurfacesDrawingFullyVisible() {
LayerImpl* root = CreateTestTreeWithTwoSurfaces();
// Make sure render surface takes content outside visible rect into
// consideration.
root->layer_tree_impl()
->property_trees()
->effect_tree_mutable()
.Node(child1_->effect_tree_index())
->backdrop_filters.Append(
FilterOperation::CreateZoomFilter(2.f /* zoom */, 0 /* inset */));
// Setup includes going past the first frame which always damages
// everything, so that we can actually perform specific tests.
EmulateDrawingOneFrame(root);
return root;
}
LayerImpl* CreateAndSetUpTestTreeWithFourSurfaces() {
LayerImpl* root = CreateTestTreeWithFourSurfaces();
// Setup includes going past the first frame which always damages
// everything, so that we can actually perform specific tests.
EmulateDrawingOneFrame(root);
return root;
}
void EmulateDrawingOneFrame(LayerImpl* root,
float device_scale_factor = 1.f) {
// This emulates only steps that are relevant to testing the damage tracker:
// 1. computing the render passes and layerlists
// 2. updating all damage trackers in the correct order
// 3. resetting all update_rects and property_changed flags for all layers
// and surfaces.
root->layer_tree_impl()->SetDeviceScaleFactor(device_scale_factor);
root->layer_tree_impl()->set_needs_update_draw_properties();
UpdateDrawProperties(root->layer_tree_impl());
DamageTracker::UpdateDamageTracking(root->layer_tree_impl());
root->layer_tree_impl()->ResetAllChangeTracking();
}
protected:
void ClearLayersAndProperties() {
host_impl()->active_tree()->DetachLayersKeepingRootLayerForTesting();
host_impl()->active_tree()->property_trees()->clear();
child_layers_.clear();
child1_ = nullptr;
child2_ = nullptr;
grand_child1_ = nullptr;
grand_child2_ = nullptr;
grand_child3_ = nullptr;
grand_child4_ = nullptr;
}
// Stores result of CreateTestTreeWithOneSurface().
std::vector<raw_ptr<TestLayerImpl, VectorExperimental>> child_layers_;
// Store result of CreateTestTreeWithTwoSurfaces().
raw_ptr<TestLayerImpl> child1_ = nullptr;
raw_ptr<TestLayerImpl> child2_ = nullptr;
raw_ptr<TestLayerImpl, DanglingUntriaged> grand_child1_ = nullptr;
raw_ptr<TestLayerImpl, DanglingUntriaged> grand_child2_ = nullptr;
raw_ptr<TestLayerImpl> grand_child3_ = nullptr;
raw_ptr<TestLayerImpl> grand_child4_ = nullptr;
};
TEST_F(DamageTrackerTest, SanityCheckTestTreeWithOneSurface) {
// Sanity check that the simple test tree will actually produce the expected
// render surfaces.
LayerImpl* root = CreateAndSetUpTestTreeWithOneSurface();
LayerImpl* child = child_layers_[0];
EXPECT_EQ(2, GetRenderSurface(root)->num_contributors());
EXPECT_TRUE(root->contributes_to_drawn_render_surface());
EXPECT_TRUE(child->contributes_to_drawn_render_surface());
gfx::Rect root_damage_rect;
EXPECT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&root_damage_rect));
EXPECT_EQ(gfx::Rect(500, 500).ToString(), root_damage_rect.ToString());
EXPECT_TRUE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
}
TEST_F(DamageTrackerTest, SanityCheckTestTreeWithTwoSurfaces) {
// Sanity check that the complex test tree will actually produce the expected
// render surfaces.
LayerImpl* root = CreateAndSetUpTestTreeWithTwoSurfaces();
gfx::Rect child_damage_rect;
EXPECT_TRUE(GetRenderSurface(child1_)->damage_tracker()->GetDamageRectIfValid(
&child_damage_rect));
gfx::Rect root_damage_rect;
EXPECT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&root_damage_rect));
EXPECT_NE(GetRenderSurface(child1_), GetRenderSurface(root));
EXPECT_EQ(GetRenderSurface(child2_), GetRenderSurface(root));
EXPECT_EQ(3, GetRenderSurface(root)->num_contributors());
EXPECT_EQ(2, GetRenderSurface(child1_)->num_contributors());
// The render surface for child1_ only has a content_rect that encloses
// grand_child1_ and grand_child2_, because child1_ does not draw content.
EXPECT_EQ(gfx::Rect(190, 190, 16, 18).ToString(),
child_damage_rect.ToString());
EXPECT_EQ(gfx::Rect(500, 500).ToString(), root_damage_rect.ToString());
EXPECT_TRUE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
EXPECT_TRUE(GetRenderSurface(child1_)
->damage_tracker()
->has_damage_from_contributing_content());
}
TEST_F(DamageTrackerTest, VerifyDamageForUpdateRects) {
LayerImpl* root = CreateAndSetUpTestTreeWithOneSurface();
LayerImpl* child = child_layers_[0];
// CASE 1: Setting the update rect should cause the corresponding damage to
// the surface.
ClearDamageForAllSurfaces(root);
child->UnionUpdateRect(gfx::Rect(10, 11, 12, 13));
EmulateDrawingOneFrame(root);
// Damage position on the surface should be: position of update_rect (10, 11)
// relative to the child (100, 100).
gfx::Rect root_damage_rect;
EXPECT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&root_damage_rect));
EXPECT_EQ(gfx::Rect(110, 111, 12, 13).ToString(),
root_damage_rect.ToString());
// CASE 2: The same update rect twice in a row still produces the same
// damage.
ClearDamageForAllSurfaces(root);
child->UnionUpdateRect(gfx::Rect(10, 11, 12, 13));
EmulateDrawingOneFrame(root);
EXPECT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&root_damage_rect));
EXPECT_EQ(gfx::Rect(110, 111, 12, 13).ToString(),
root_damage_rect.ToString());
// CASE 3: Setting a different update rect should cause damage on the new
// update region, but no additional exposed old region.
ClearDamageForAllSurfaces(root);
child->UnionUpdateRect(gfx::Rect(20, 25, 1, 2));
EmulateDrawingOneFrame(root);
// Damage position on the surface should be: position of update_rect (20, 25)
// relative to the child (100, 100).
EXPECT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&root_damage_rect));
EXPECT_EQ(gfx::Rect(120, 125, 1, 2).ToString(), root_damage_rect.ToString());
EXPECT_TRUE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
}
TEST_F(DamageTrackerTest, VerifyDamageForLayerDamageRects) {
LayerImpl* root = CreateAndSetUpTestTreeWithOneSurface();
TestLayerImpl* child = child_layers_[0];
// CASE 1: Adding the layer damage rect should cause the corresponding damage
// to the surface.
ClearDamageForAllSurfaces(root);
child->AddDamageRect(gfx::Rect(10, 11, 12, 13));
child->SetDamageReasons({DamageReason::kAnimatedImage});
EmulateDrawingOneFrame(root);
// Damage position on the surface should be: position of layer damage_rect
// (10, 11) relative to the child (100, 100).
gfx::Rect root_damage_rect;
EXPECT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&root_damage_rect));
EXPECT_EQ(true, root_damage_rect.Contains(gfx::Rect(110, 111, 12, 13)));
EXPECT_EQ(GetRenderSurface(root)->damage_tracker()->GetDamageReasons(),
DamageReasonSet{DamageReason::kAnimatedImage});
// CASE 2: The same layer damage rect twice in a row still produces the same
// damage.
ClearDamageForAllSurfaces(root);
child->AddDamageRect(gfx::Rect(10, 11, 12, 13));
child->SetDamageReasons({DamageReason::kScrollbarFadeOutAnimation});
EmulateDrawingOneFrame(root);
EXPECT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&root_damage_rect));
EXPECT_EQ(true, root_damage_rect.Contains(gfx::Rect(110, 111, 12, 13)));
EXPECT_EQ(GetRenderSurface(root)->damage_tracker()->GetDamageReasons(),
DamageReasonSet{DamageReason::kScrollbarFadeOutAnimation});
// CASE 3: Adding a different layer damage rect should cause damage on the
// new damaged region, but no additional exposed old region.
ClearDamageForAllSurfaces(root);
child->AddDamageRect(gfx::Rect(20, 25, 1, 2));
child->SetDamageReasons({DamageReason::kAnimatedImage});
EmulateDrawingOneFrame(root);
// Damage position on the surface should be: position of layer damage_rect
// (20, 25) relative to the child (100, 100).
EXPECT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&root_damage_rect));
EXPECT_EQ(true, root_damage_rect.Contains(gfx::Rect(120, 125, 1, 2)));
EXPECT_EQ(GetRenderSurface(root)->damage_tracker()->GetDamageReasons(),
DamageReasonSet{DamageReason::kAnimatedImage});
// CASE 4: Adding multiple layer damage rects should cause a unified
// damage on root damage rect.
ClearDamageForAllSurfaces(root);
child->AddDamageRect(gfx::Rect(20, 25, 1, 2));
child->AddDamageRect(gfx::Rect(10, 15, 3, 4));
child->SetDamageReasons(
{DamageReason::kAnimatedImage, DamageReason::kUntracked});
EmulateDrawingOneFrame(root);
// Damage position on the surface should be: position of layer damage_rect
// (20, 25) relative to the child (100, 100).
EXPECT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&root_damage_rect));
EXPECT_EQ(true, root_damage_rect.Contains(gfx::Rect(120, 125, 1, 2)));
EXPECT_EQ(true, root_damage_rect.Contains(gfx::Rect(110, 115, 3, 4)));
EXPECT_TRUE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
EXPECT_EQ(GetRenderSurface(root)->damage_tracker()->GetDamageReasons(),
(DamageReasonSet{DamageReason::kAnimatedImage,
DamageReason::kUntracked}));
}
TEST_F(DamageTrackerTest, VerifyDamageForLayerUpdateAndDamageRects) {
LayerImpl* root = CreateAndSetUpTestTreeWithOneSurface();
TestLayerImpl* child = child_layers_[0];
// CASE 1: Adding the layer damage rect and update rect should cause the
// corresponding damage to the surface.
ClearDamageForAllSurfaces(root);
child->AddDamageRect(gfx::Rect(5, 6, 12, 13));
child->UnionUpdateRect(gfx::Rect(15, 16, 14, 10));
child->SetDamageReasons({DamageReason::kAnimatedImage});
EmulateDrawingOneFrame(root);
// Damage position on the surface should be: position of unified layer
// damage_rect and update rect (5, 6)
// relative to the child (100, 100).
gfx::Rect root_damage_rect;
EXPECT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&root_damage_rect));
EXPECT_EQ(true, root_damage_rect.Contains(gfx::Rect(105, 106, 24, 20)));
EXPECT_EQ(GetRenderSurface(root)->damage_tracker()->GetDamageReasons(),
DamageReasonSet{DamageReason::kAnimatedImage});
// CASE 2: The same layer damage rect and update rect twice in a row still
// produces the same damage.
ClearDamageForAllSurfaces(root);
child->AddDamageRect(gfx::Rect(10, 11, 12, 13));
child->UnionUpdateRect(gfx::Rect(10, 11, 14, 15));
child->SetDamageReasons({DamageReason::kAnimatedImage});
EmulateDrawingOneFrame(root);
EXPECT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&root_damage_rect));
EXPECT_EQ(true, root_damage_rect.Contains(gfx::Rect(110, 111, 14, 15)));
EXPECT_EQ(GetRenderSurface(root)->damage_tracker()->GetDamageReasons(),
DamageReasonSet{DamageReason::kAnimatedImage});
// CASE 3: Adding a different layer damage rect and update rect should cause
// damage on the new damaged region, but no additional exposed old region.
ClearDamageForAllSurfaces(root);
child->AddDamageRect(gfx::Rect(20, 25, 2, 3));
child->UnionUpdateRect(gfx::Rect(5, 10, 7, 8));
child->SetDamageReasons({DamageReason::kAnimatedImage});
EmulateDrawingOneFrame(root);
// Damage position on the surface should be: position of unified layer damage
// rect and update rect (5, 10) relative to the child (100, 100).
EXPECT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&root_damage_rect));
EXPECT_EQ(true, root_damage_rect.Contains(gfx::Rect(105, 110, 17, 18)));
EXPECT_TRUE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
EXPECT_EQ(GetRenderSurface(root)->damage_tracker()->GetDamageReasons(),
DamageReasonSet{DamageReason::kAnimatedImage});
}
TEST_F(DamageTrackerTest, VerifyDamageForPropertyChanges) {
LayerImpl* root = CreateAndSetUpTestTreeWithOneSurface();
TestLayerImpl* child = child_layers_[0];
// CASE 1: The layer's property changed flag takes priority over update rect.
//
CreateEffectNode(child).render_surface_reason = RenderSurfaceReason::kTest;
EmulateDrawingOneFrame(root);
ClearDamageForAllSurfaces(root);
child->UnionUpdateRect(gfx::Rect(10, 11, 12, 13));
root->layer_tree_impl()->SetOpacityMutated(child->element_id(), 0.5f);
child->SetDamageReasons({DamageReason::kAnimatedImage});
EmulateDrawingOneFrame(root);
ASSERT_EQ(2, GetRenderSurface(root)->num_contributors());
EXPECT_EQ(GetRenderSurface(root)->damage_tracker()->GetDamageReasons(),
DamageReasonSet{DamageReason::kAnimatedImage});
// Damage should be the entire child layer in target_surface space.
gfx::Rect expected_rect = gfx::Rect(100, 100, 30, 30);
gfx::Rect root_damage_rect;
EXPECT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&root_damage_rect));
EXPECT_EQ(expected_rect.ToString(), root_damage_rect.ToString());
EXPECT_TRUE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
EXPECT_TRUE(GetRenderSurface(child)
->damage_tracker()
->has_damage_from_contributing_content());
// CASE 2: If a layer moves due to property change, it damages both the new
// location and the old (exposed) location. The old location is the
// entire old layer, not just the update_rect.
// Cycle one frame of no change, just to sanity check that the next rect is
// not because of the old damage state.
ClearDamageForAllSurfaces(root);
EmulateDrawingOneFrame(root);
EXPECT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&root_damage_rect));
EXPECT_TRUE(root_damage_rect.IsEmpty());
EXPECT_EQ(GetRenderSurface(root)->damage_tracker()->GetDamageReasons(),
DamageReasonSet{});
// Then, test the actual layer movement.
ClearDamageForAllSurfaces(root);
CreateTransformNode(child).post_translation =
child->offset_to_transform_parent();
child->SetOffsetToTransformParent(gfx::Vector2dF());
gfx::Transform translation;
translation.Translate(100.f, 130.f);
root->layer_tree_impl()->SetTransformMutated(child->element_id(),
translation);
child->SetDamageReasons({DamageReason::kAnimatedImage});
EmulateDrawingOneFrame(root);
// Expect damage to be the combination of the previous one and the new one.
expected_rect.Union(gfx::Rect(200, 230, 30, 30));
EXPECT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&root_damage_rect));
EXPECT_EQ(expected_rect, root_damage_rect);
EXPECT_TRUE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
// TODO(crbug.com/40646366): Transform from browser animation should not be
// considered as damage from contributing layer since it is applied to the
// whole layer which has a render surface.
EXPECT_TRUE(GetRenderSurface(child)
->damage_tracker()
->has_damage_from_contributing_content());
// SurfacePropertyChanged causes an extra kUntracked to be added. Verify
// kAnimatedImage is not dropped.
EXPECT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageReasons().Has(
DamageReason::kAnimatedImage));
}
TEST_F(DamageTrackerTest,
VerifyDamageForPropertyChangesFromContributingContents) {
LayerImpl* root = CreateAndSetUpTestTreeWithTwoSurfaces();
// CASE 1: child1_'s opacity changed.
ClearDamageForAllSurfaces(root);
root->layer_tree_impl()->SetOpacityMutated(child1_->element_id(), 0.5f);
EmulateDrawingOneFrame(root);
EXPECT_TRUE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
EXPECT_FALSE(GetRenderSurface(child1_)
->damage_tracker()
->has_damage_from_contributing_content());
// CASE 2: layer2_'s opacity changed.
CreateEffectNode(child2_).render_surface_reason = RenderSurfaceReason::kTest;
EmulateDrawingOneFrame(root);
ClearDamageForAllSurfaces(root);
root->layer_tree_impl()->SetOpacityMutated(child2_->element_id(), 0.5f);
EmulateDrawingOneFrame(root);
EXPECT_TRUE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
EXPECT_FALSE(GetRenderSurface(child1_)
->damage_tracker()
->has_damage_from_contributing_content());
// CASE 3: grand_child1_'s opacity changed.
CreateEffectNode(grand_child1_).render_surface_reason =
RenderSurfaceReason::kTest;
EmulateDrawingOneFrame(root);
ClearDamageForAllSurfaces(root);
root->layer_tree_impl()->SetOpacityMutated(grand_child1_->element_id(), 0.5f);
EmulateDrawingOneFrame(root);
EXPECT_TRUE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
EXPECT_TRUE(GetRenderSurface(child1_)
->damage_tracker()
->has_damage_from_contributing_content());
}
// Regression test for http://crbug.com/923794
TEST_F(DamageTrackerTest, EffectPropertyChangeNoSurface) {
LayerImpl* root = CreateAndSetUpTestTreeWithOneSurface();
LayerImpl* child = child_layers_[0];
// Create a separate effect node for the child, but no render surface.
auto& effect_node = CreateEffectNode(child);
effect_node.opacity = 0.5;
effect_node.has_potential_opacity_animation = true;
EmulateDrawingOneFrame(root);
EXPECT_EQ(root->transform_tree_index(), child->transform_tree_index());
EXPECT_NE(root->effect_tree_index(), child->effect_tree_index());
// Change the child's opacity, which should damage its target.
ClearDamageForAllSurfaces(root);
root->layer_tree_impl()->SetOpacityMutated(child->element_id(), 0.4f);
EmulateDrawingOneFrame(root);
EXPECT_TRUE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
}
// Regression test for http://crbug.com/923794
TEST_F(DamageTrackerTest, TransformPropertyChangeNoSurface) {
LayerImpl* root = CreateAndSetUpTestTreeWithOneSurface();
LayerImpl* child = child_layers_[0];
// Create a separate transform node for the child, but no render surface.
gfx::Transform trans1;
trans1.Scale(2, 1);
CreateTransformNode(child).local = trans1;
EmulateDrawingOneFrame(root);
EXPECT_NE(root->transform_tree_index(), child->transform_tree_index());
EXPECT_EQ(root->effect_tree_index(), child->effect_tree_index());
// Change the child's transform , which should damage its target.
ClearDamageForAllSurfaces(root);
gfx::Transform trans2;
trans2.Scale(1, 2);
root->layer_tree_impl()->SetTransformMutated(child->element_id(), trans2);
EmulateDrawingOneFrame(root);
EXPECT_TRUE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
}
TEST_F(DamageTrackerTest,
VerifyDamageForUpdateAndDamageRectsFromContributingContents) {
LayerImpl* root = CreateAndSetUpTestTreeWithTwoSurfaces();
// CASE 1: Adding child1_'s damage rect and update rect should cause the
// corresponding damage to the surface.
child1_->SetDrawsContent(true);
ClearDamageForAllSurfaces(root);
child1_->AddDamageRect(gfx::Rect(105, 106, 12, 15));
child1_->UnionUpdateRect(gfx::Rect(115, 116, 12, 15));
EmulateDrawingOneFrame(root);
EXPECT_TRUE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
EXPECT_TRUE(GetRenderSurface(child1_)
->damage_tracker()
->has_damage_from_contributing_content());
// CASE 2: Adding child2_'s damage rect and update rect should cause the
// corresponding damage to the surface.
ClearDamageForAllSurfaces(root);
child2_->AddDamageRect(gfx::Rect(11, 11, 12, 15));
child2_->UnionUpdateRect(gfx::Rect(12, 12, 12, 15));
EmulateDrawingOneFrame(root);
EXPECT_TRUE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
EXPECT_FALSE(GetRenderSurface(child1_)
->damage_tracker()
->has_damage_from_contributing_content());
// CASE 3: Adding grand_child1_'s damage rect and update rect should cause
// the corresponding damage to the surface.
ClearDamageForAllSurfaces(root);
grand_child1_->AddDamageRect(gfx::Rect(1, 0, 2, 5));
grand_child1_->UnionUpdateRect(gfx::Rect(2, 1, 2, 5));
EmulateDrawingOneFrame(root);
EXPECT_TRUE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
EXPECT_TRUE(GetRenderSurface(child1_)
->damage_tracker()
->has_damage_from_contributing_content());
}
TEST_F(DamageTrackerTest, VerifyDamageWhenSurfaceRemoved) {
LayerImpl* root = CreateAndSetUpTestTreeWithTwoSurfaces();
LayerImpl* surface = child1_;
LayerImpl* child = grand_child1_;
child->SetDrawsContent(true);
EmulateDrawingOneFrame(root);
ClearDamageForAllSurfaces(root);
SetRenderSurfaceReason(surface, RenderSurfaceReason::kNone);
child->SetDrawsContent(false);
EmulateDrawingOneFrame(root);
gfx::Rect root_damage_rect;
EXPECT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&root_damage_rect));
EXPECT_EQ(gfx::Rect(290, 290, 16, 18).ToString(),
root_damage_rect.ToString());
EXPECT_TRUE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
}
TEST_F(DamageTrackerTest, VerifyDamageForTransformedLayer) {
// If a layer is transformed, the damage rect should still enclose the entire
// transformed layer.
LayerImpl* root = CreateAndSetUpTestTreeWithOneSurface();
LayerImpl* child = child_layers_[0];
gfx::Transform rotation;
rotation.Rotate(45.0);
ClearDamageForAllSurfaces(root);
auto& transform_node = CreateTransformNode(child);
transform_node.origin = gfx::Point3F(child->bounds().width() * 0.5f,
child->bounds().height() * 0.5f, 0.f);
transform_node.post_translation = gfx::Vector2dF(85.f, 85.f);
child->SetOffsetToTransformParent(gfx::Vector2dF());
CreateEffectNode(child).render_surface_reason = RenderSurfaceReason::kTest;
child->NoteLayerPropertyChanged();
EmulateDrawingOneFrame(root);
// Sanity check that the layer actually moved to (85, 85), damaging its old
// location and new location.
gfx::Rect root_damage_rect;
EXPECT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&root_damage_rect));
EXPECT_EQ(gfx::Rect(85, 85, 45, 45).ToString(), root_damage_rect.ToString());
EXPECT_TRUE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
// Layer's layer_property_changed_not_from_property_trees_ should be
// considered as damage to render surface.
EXPECT_TRUE(GetRenderSurface(child)
->damage_tracker()
->has_damage_from_contributing_content());
// With the anchor on the layer's center, now we can test the rotation more
// intuitively, since it applies about the layer's anchor.
ClearDamageForAllSurfaces(root);
root->layer_tree_impl()->SetTransformMutated(child->element_id(), rotation);
EmulateDrawingOneFrame(root);
// Since the child layer is square, rotation by 45 degrees about the center
// should increase the size of the expected rect by sqrt(2), centered around
// (100, 100). The old exposed region should be fully contained in the new
// region.
float expected_width = 30.f * sqrt(2.f);
float expected_position = 100.f - 0.5f * expected_width;
gfx::Rect expected_rect = gfx::ToEnclosingRect(gfx::RectF(
expected_position, expected_position, expected_width, expected_width));
EXPECT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&root_damage_rect));
EXPECT_EQ(expected_rect.ToString(), root_damage_rect.ToString());
EXPECT_TRUE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
// Transform from browser animation should not be considered as damage from
// contributing layer since it is applied to the whole layer which has a
// render surface.
EXPECT_FALSE(GetRenderSurface(child)
->damage_tracker()
->has_damage_from_contributing_content());
}
TEST_F(DamageTrackerTest, VerifyDamageForPerspectiveClippedLayer) {
// If a layer has a perspective transform that causes w < 0, then not
// clipping the layer can cause an invalid damage rect. This test checks that
// the w < 0 case is tracked properly.
//
// The transform is constructed so that if w < 0 clipping is not performed,
// the incorrect rect will be very small, specifically: position (500.972504,
// 498.544617) and size 0.056610 x 2.910767. Instead, the correctly
// transformed rect should actually be very huge (i.e. in theory, -infinity
// on the left), and positioned so that the right-most bound rect will be
// approximately 501 units in root surface space.
//
LayerImpl* root = CreateAndSetUpTestTreeWithOneSurface();
LayerImpl* child = child_layers_[0];
gfx::Transform transform;
transform.Translate3d(550.0, 500.0, 0.0);
transform.ApplyPerspectiveDepth(1.0);
transform.RotateAboutYAxis(45.0);
transform.Translate3d(-50.0, -50.0, 0.0);
// Set up the child
child->SetBounds(gfx::Size(100, 100));
CreateTransformNode(child).local = transform;
child->SetOffsetToTransformParent(gfx::Vector2dF());
EmulateDrawingOneFrame(root);
// Sanity check that the child layer's bounds would actually get clipped by
// w < 0, otherwise this test is not actually testing the intended scenario.
gfx::RectF test_rect(gfx::SizeF(child->bounds()));
bool clipped = false;
MathUtil::MapQuad(transform, gfx::QuadF(test_rect), &clipped);
EXPECT_TRUE(clipped);
// Damage the child without moving it.
CreateEffectNode(child).render_surface_reason = RenderSurfaceReason::kTest;
EmulateDrawingOneFrame(root);
ClearDamageForAllSurfaces(root);
root->layer_tree_impl()->SetOpacityMutated(child->element_id(), 0.5f);
EmulateDrawingOneFrame(root);
// The expected damage should cover the entire root surface (500x500), but we
// don't care whether the damage rect was clamped or is larger than the
// surface for this test.
gfx::Rect root_damage_rect;
EXPECT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&root_damage_rect));
gfx::Rect damage_we_care_about = gfx::Rect(gfx::Size(500, 500));
EXPECT_TRUE(root_damage_rect.Contains(damage_we_care_about));
EXPECT_TRUE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
EXPECT_FALSE(GetRenderSurface(child)
->damage_tracker()
->has_damage_from_contributing_content());
}
TEST_F(DamageTrackerTest, VerifyDamageForBlurredSurface) {
LayerImpl* root = CreateAndSetUpTestTreeWithTwoSurfaces();
LayerImpl* surface = child1_;
LayerImpl* child = grand_child1_;
FilterOperations filters;
filters.Append(FilterOperation::CreateBlurFilter(5.f));
// TODO(crbug.com/40646366): Setting the filter on an existing render surface
// should not damage the conrresponding render surface.
ClearDamageForAllSurfaces(root);
SetFilter(surface, filters);
EmulateDrawingOneFrame(root);
EXPECT_TRUE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
EXPECT_FALSE(GetRenderSurface(surface)
->damage_tracker()
->has_damage_from_contributing_content());
// Setting the update rect should cause the corresponding damage to the
// surface, blurred based on the size of the blur filter.
ClearDamageForAllSurfaces(root);
child->UnionUpdateRect(gfx::Rect(1, 2, 3, 4));
EmulateDrawingOneFrame(root);
// Damage position on the surface should be: position of update_rect (1, 2)
// relative to the child (300, 300), but expanded by the blur outsets
// (15, since the blur radius is 5).
gfx::Rect root_damage_rect;
EXPECT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&root_damage_rect));
EXPECT_EQ(gfx::Rect(286, 287, 33, 34), root_damage_rect);
EXPECT_TRUE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
EXPECT_TRUE(GetRenderSurface(surface)
->damage_tracker()
->has_damage_from_contributing_content());
}
TEST_F(DamageTrackerTest, VerifyDamageForImageFilter) {
LayerImpl* root = CreateAndSetUpTestTreeWithOneSurface();
LayerImpl* child = child_layers_[0];
gfx::Rect root_damage_rect, child_damage_rect;
// Allow us to set damage on child too.
child->SetDrawsContent(true);
FilterOperations filters;
filters.Append(FilterOperation::CreateReferenceFilter(
sk_make_sp<BlurPaintFilter>(2, 2, SkTileMode::kDecal, nullptr)));
// Setting the filter will damage the whole surface.
CreateTransformNode(child).post_translation =
child->offset_to_transform_parent();
child->SetOffsetToTransformParent(gfx::Vector2dF());
CreateEffectNode(child).render_surface_reason = RenderSurfaceReason::kTest;
EmulateDrawingOneFrame(root);
ClearDamageForAllSurfaces(root);
child->layer_tree_impl()->SetFilterMutated(child->element_id(), filters);
EmulateDrawingOneFrame(root);
EXPECT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&root_damage_rect));
EXPECT_TRUE(GetRenderSurface(child)->damage_tracker()->GetDamageRectIfValid(
&child_damage_rect));
// gfx::Rect(100, 100, 30, 30), expanded by 6px for the 2px blur filter.
EXPECT_EQ(gfx::Rect(94, 94, 42, 42), root_damage_rect);
// gfx::Rect(0, 0, 30, 30), expanded by 6px for the 2px blur filter.
EXPECT_EQ(gfx::Rect(-6, -6, 42, 42), child_damage_rect);
EXPECT_TRUE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
EXPECT_FALSE(GetRenderSurface(child)
->damage_tracker()
->has_damage_from_contributing_content());
// CASE 1: Setting the update rect should damage the whole surface (for now)
ClearDamageForAllSurfaces(root);
child->UnionUpdateRect(gfx::Rect(1, 1));
EmulateDrawingOneFrame(root);
EXPECT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&root_damage_rect));
EXPECT_TRUE(GetRenderSurface(child)->damage_tracker()->GetDamageRectIfValid(
&child_damage_rect));
// gfx::Rect(100, 100, 1, 1), expanded by 6px for the 2px blur filter.
EXPECT_EQ(gfx::Rect(94, 94, 13, 13), root_damage_rect);
// gfx::Rect(0, 0, 1, 1), expanded by 6px for the 2px blur filter.
EXPECT_EQ(gfx::Rect(-6, -6, 13, 13), child_damage_rect);
EXPECT_TRUE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
EXPECT_TRUE(GetRenderSurface(child)
->damage_tracker()
->has_damage_from_contributing_content());
// CASE 2: No changes, so should not damage the surface.
ClearDamageForAllSurfaces(root);
EmulateDrawingOneFrame(root);
EXPECT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&root_damage_rect));
EXPECT_TRUE(GetRenderSurface(child)->damage_tracker()->GetDamageRectIfValid(
&child_damage_rect));
// Should not be expanded by the blur filter.
EXPECT_EQ(gfx::Rect(), root_damage_rect);
EXPECT_EQ(gfx::Rect(), child_damage_rect);
EXPECT_FALSE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
EXPECT_FALSE(GetRenderSurface(child)
->damage_tracker()
->has_damage_from_contributing_content());
}
TEST_F(DamageTrackerTest, VerifyDamageForTransformedImageFilter) {
LayerImpl* root = CreateAndSetUpTestTreeWithOneSurface();
LayerImpl* child = child_layers_[0];
gfx::Rect root_damage_rect, child_damage_rect;
// Allow us to set damage on child too.
child->SetDrawsContent(true);
FilterOperations filters;
filters.Append(FilterOperation::CreateReferenceFilter(
sk_make_sp<BlurPaintFilter>(2, 2, SkTileMode::kDecal, nullptr)));
// Setting the filter will damage the whole surface.
gfx::Transform transform;
transform.RotateAboutYAxis(60);
ClearDamageForAllSurfaces(root);
auto& transform_node = CreateTransformNode(child);
transform_node.local = transform;
transform_node.post_translation = child->offset_to_transform_parent();
child->SetOffsetToTransformParent(gfx::Vector2dF());
auto& effect_node = CreateEffectNode(child);
effect_node.render_surface_reason = RenderSurfaceReason::kFilter;
effect_node.has_potential_filter_animation = true;
EmulateDrawingOneFrame(root);
child->layer_tree_impl()->SetFilterMutated(child->element_id(), filters);
EmulateDrawingOneFrame(root);
EXPECT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&root_damage_rect));
EXPECT_TRUE(GetRenderSurface(child)->damage_tracker()->GetDamageRectIfValid(
&child_damage_rect));
// Blur outset is 6px for a 2px blur.
int blur_outset = 6;
int rotated_outset_left = blur_outset / 2;
int expected_rotated_width = (30 + 2 * blur_outset) / 2;
gfx::Rect expected_root_damage(100 - rotated_outset_left, 100 - blur_outset,
expected_rotated_width, 30 + 2 * blur_outset);
expected_root_damage.Union(gfx::Rect(100, 100, 30, 30));
EXPECT_EQ(expected_root_damage, root_damage_rect);
EXPECT_EQ(gfx::Rect(-blur_outset, -blur_outset, 30 + 2 * blur_outset,
30 + 2 * blur_outset),
child_damage_rect);
// Setting the update rect should damage the whole surface (for now)
ClearDamageForAllSurfaces(root);
child->UnionUpdateRect(gfx::Rect(30, 30));
EmulateDrawingOneFrame(root);
EXPECT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&root_damage_rect));
EXPECT_TRUE(GetRenderSurface(child)->damage_tracker()->GetDamageRectIfValid(
&child_damage_rect));
int expect_width = 30 + 2 * blur_outset;
int expect_height = 30 + 2 * blur_outset;
EXPECT_EQ(gfx::Rect(100 - blur_outset / 2, 100 - blur_outset,
expect_width / 2, expect_height),
root_damage_rect);
EXPECT_EQ(gfx::Rect(-blur_outset, -blur_outset, expect_width, expect_height),
child_damage_rect);
}
TEST_F(DamageTrackerTest, VerifyDamageForHighDPIImageFilter) {
LayerImpl* root = CreateAndSetUpTestTreeWithOneSurface();
LayerImpl* child = child_layers_[0];
gfx::Rect root_damage_rect, child_damage_rect;
ClearDamageForAllSurfaces(root);
int device_scale_factor = 2;
EmulateDrawingOneFrame(root, device_scale_factor);
// Allow us to set damage on child too.
child->SetDrawsContent(true);
FilterOperations filters;
filters.Append(FilterOperation::CreateBlurFilter(3.f));
// Setting the filter and creating a new render surface will damage the whole
// surface.
ClearDamageForAllSurfaces(root);
CreateTransformNode(child).post_translation =
child->offset_to_transform_parent();
child->SetOffsetToTransformParent(gfx::Vector2dF());
CreateEffectNode(child).render_surface_reason = RenderSurfaceReason::kTest;
ClearDamageForAllSurfaces(root);
child->layer_tree_impl()->SetFilterMutated(child->element_id(), filters);
EmulateDrawingOneFrame(root, device_scale_factor);
EXPECT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&root_damage_rect));
EXPECT_TRUE(GetRenderSurface(child)->damage_tracker()->GetDamageRectIfValid(
&child_damage_rect));
// Blur outset is 9px for a 3px blur, scaled up by DSF.
int blur_outset = 9 * device_scale_factor;
gfx::Rect expected_child_damage_rect(60, 60);
expected_child_damage_rect.Inset(-blur_outset);
gfx::Rect expected_root_damage_rect(child_damage_rect);
expected_root_damage_rect.Offset(200, 200);
EXPECT_EQ(expected_root_damage_rect, root_damage_rect);
EXPECT_EQ(expected_child_damage_rect, child_damage_rect);
// Setting the update rect should damage only the affected area (original,
// outset by 3 * blur sigma * DSF).
ClearDamageForAllSurfaces(root);
child->UnionUpdateRect(gfx::Rect(30, 30));
EmulateDrawingOneFrame(root, device_scale_factor);
EXPECT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&root_damage_rect));
EXPECT_TRUE(GetRenderSurface(child)->damage_tracker()->GetDamageRectIfValid(
&child_damage_rect));
EXPECT_EQ(expected_root_damage_rect, root_damage_rect);
EXPECT_EQ(expected_child_damage_rect, child_damage_rect);
}
TEST_F(DamageTrackerTest, VerifyDamageForAddingAndRemovingLayer) {
LayerImpl* root = CreateAndSetUpTestTreeWithOneSurface();
LayerImpl* child1 = child_layers_[0];
// CASE 1: Adding a new layer should cause the appropriate damage.
//
ClearDamageForAllSurfaces(root);
LayerImpl* child2 = AddLayerInActiveTree<LayerImpl>();
child2->SetBounds(gfx::Size(6, 8));
child2->SetDrawsContent(true);
CopyProperties(root, child2);
child2->SetOffsetToTransformParent(gfx::Vector2dF(400.f, 380.f));
EmulateDrawingOneFrame(root);
// Sanity check - all 3 layers should be on the same render surface; render
// surfaces are tested elsewhere.
ASSERT_EQ(3, GetRenderSurface(root)->num_contributors());
gfx::Rect root_damage_rect;
EXPECT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&root_damage_rect));
EXPECT_EQ(gfx::Rect(400, 380, 6, 8).ToString(), root_damage_rect.ToString());
EXPECT_TRUE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
// CASE 2: If the layer is removed, its entire old layer becomes exposed, not
// just the last update rect.
// Advance one frame without damage so that we know the damage rect is not
// leftover from the previous case.
ClearDamageForAllSurfaces(root);
EmulateDrawingOneFrame(root);
EXPECT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&root_damage_rect));
EXPECT_TRUE(root_damage_rect.IsEmpty());
// Then, test removing child1_.
{
OwnedLayerImplList layers =
host_impl()->active_tree()->DetachLayersKeepingRootLayerForTesting();
ASSERT_EQ(3u, layers.size());
ASSERT_EQ(child1, layers[1].get());
host_impl()->active_tree()->AddLayer(std::move(layers[2]));
}
EmulateDrawingOneFrame(root);
EXPECT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&root_damage_rect));
EXPECT_EQ(gfx::Rect(100, 100, 30, 30).ToString(),
root_damage_rect.ToString());
EXPECT_TRUE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
}
TEST_F(DamageTrackerTest, VerifyDamageForNewUnchangedLayer) {
// If child2_ is added to the layer tree, but it doesn't have any explicit
// damage of its own, it should still indeed damage the target surface.
LayerImpl* root = CreateAndSetUpTestTreeWithOneSurface();
ClearDamageForAllSurfaces(root);
LayerImpl* child2 = AddLayerInActiveTree<LayerImpl>();
child2->SetBounds(gfx::Size(6, 8));
child2->SetDrawsContent(true);
CopyProperties(root, child2);
child2->SetOffsetToTransformParent(gfx::Vector2dF(400.f, 380.f));
host_impl()->active_tree()->ResetAllChangeTracking();
// Sanity check the initial conditions of the test, if these asserts
// trigger, it means the test no longer actually covers the intended
// scenario.
ASSERT_FALSE(child2->LayerPropertyChanged());
ASSERT_TRUE(child2->update_rect().IsEmpty());
EmulateDrawingOneFrame(root);
// Sanity check - all 3 layers should be on the same render surface; render
// surfaces are tested elsewhere.
ASSERT_EQ(3, GetRenderSurface(root)->num_contributors());
gfx::Rect root_damage_rect;
EXPECT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&root_damage_rect));
EXPECT_EQ(gfx::Rect(400, 380, 6, 8).ToString(), root_damage_rect.ToString());
EXPECT_TRUE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
}
TEST_F(DamageTrackerTest, VerifyDamageForMultipleLayers) {
LayerImpl* root = CreateAndSetUpTestTreeWithOneSurface();
LayerImpl* child1 = child_layers_[0];
// In this test we don't want the above tree manipulation to be considered
// part of the same frame.
ClearDamageForAllSurfaces(root);
LayerImpl* child2 = AddLayerInActiveTree<LayerImpl>();
child2->SetBounds(gfx::Size(6, 8));
child2->SetDrawsContent(true);
CopyProperties(root, child2);
child2->SetOffsetToTransformParent(gfx::Vector2dF(400.f, 380.f));
EmulateDrawingOneFrame(root);
// Damaging two layers simultaneously should cause combined damage.
// - child1_ update rect in surface space: gfx::Rect(100, 100, 1, 2);
// - child2_ update rect in surface space: gfx::Rect(400, 380, 3, 4);
ClearDamageForAllSurfaces(root);
child1->UnionUpdateRect(gfx::Rect(1, 2));
child2->UnionUpdateRect(gfx::Rect(3, 4));
EmulateDrawingOneFrame(root);
gfx::Rect root_damage_rect;
EXPECT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&root_damage_rect));
EXPECT_EQ(gfx::Rect(100, 100, 303, 284).ToString(),
root_damage_rect.ToString());
EXPECT_TRUE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
}
TEST_F(DamageTrackerTest, VerifyDamageForNestedSurfaces) {
LayerImpl* root = CreateAndSetUpTestTreeWithTwoSurfaces();
CreateEffectNode(child2_).render_surface_reason = RenderSurfaceReason::kTest;
CreateEffectNode(grand_child1_).render_surface_reason =
RenderSurfaceReason::kTest;
EmulateDrawingOneFrame(root);
gfx::Rect child_damage_rect;
gfx::Rect root_damage_rect;
// CASE 1: Damage to a descendant surface should propagate properly to
// ancestor surface.
ClearDamageForAllSurfaces(root);
root->layer_tree_impl()->SetOpacityMutated(grand_child1_->element_id(), 0.5f);
EmulateDrawingOneFrame(root);
EXPECT_TRUE(GetRenderSurface(child1_)->damage_tracker()->GetDamageRectIfValid(
&child_damage_rect));
EXPECT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&root_damage_rect));
EXPECT_EQ(gfx::Rect(200, 200, 6, 8).ToString(), child_damage_rect.ToString());
EXPECT_EQ(gfx::Rect(300, 300, 6, 8).ToString(), root_damage_rect.ToString());
EXPECT_TRUE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
EXPECT_TRUE(GetRenderSurface(child1_)
->damage_tracker()
->has_damage_from_contributing_content());
EXPECT_FALSE(GetRenderSurface(child2_)
->damage_tracker()
->has_damage_from_contributing_content());
EXPECT_FALSE(GetRenderSurface(grand_child1_)
->damage_tracker()
->has_damage_from_contributing_content());
// CASE 2: Same as previous case, but with additional damage elsewhere that
// should be properly unioned.
// - child1_ surface damage in root surface space:
// gfx::Rect(300, 300, 6, 8);
// - child2_ damage in root surface space:
// gfx::Rect(11, 11, 18, 18);
ClearDamageForAllSurfaces(root);
root->layer_tree_impl()->SetOpacityMutated(grand_child1_->element_id(), 0.7f);
root->layer_tree_impl()->SetOpacityMutated(child2_->element_id(), 0.7f);
EmulateDrawingOneFrame(root);
EXPECT_TRUE(GetRenderSurface(child1_)->damage_tracker()->GetDamageRectIfValid(
&child_damage_rect));
EXPECT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&root_damage_rect));
EXPECT_EQ(gfx::Rect(200, 200, 6, 8).ToString(), child_damage_rect.ToString());
EXPECT_EQ(gfx::Rect(11, 11, 295, 297).ToString(),
root_damage_rect.ToString());
EXPECT_TRUE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
EXPECT_TRUE(GetRenderSurface(child1_)
->damage_tracker()
->has_damage_from_contributing_content());
EXPECT_FALSE(GetRenderSurface(child2_)
->damage_tracker()
->has_damage_from_contributing_content());
EXPECT_FALSE(GetRenderSurface(grand_child1_)
->damage_tracker()
->has_damage_from_contributing_content());
}
TEST_F(DamageTrackerTest, VerifyDamageForSurfaceChangeFromDescendantLayer) {
// If descendant layer changes and affects the content bounds of the render
// surface, then the entire descendant surface should be damaged, and it
// should damage its ancestor surface with the old and new surface regions.
// This is a tricky case, since only the first grand_child changes, but the
// entire surface should be marked dirty.
LayerImpl* root = CreateAndSetUpTestTreeWithTwoSurfaces();
gfx::Rect child_damage_rect;
gfx::Rect root_damage_rect;
ClearDamageForAllSurfaces(root);
grand_child1_->SetOffsetToTransformParent(gfx::Vector2dF(195.f, 205.f));
EmulateDrawingOneFrame(root);
EXPECT_TRUE(GetRenderSurface(child1_)->damage_tracker()->GetDamageRectIfValid(
&child_damage_rect));
EXPECT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&root_damage_rect));
// The new surface bounds should be damaged entirely, even though only one of
// the layers changed.
EXPECT_EQ(gfx::Rect(190, 190, 11, 23).ToString(),
child_damage_rect.ToString());
// Damage to the root surface should be the union of child1_'s *entire* render
// surface (in target space), and its old exposed area (also in target
// space).
EXPECT_EQ(gfx::Rect(290, 290, 16, 23).ToString(),
root_damage_rect.ToString());
EXPECT_TRUE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
EXPECT_TRUE(GetRenderSurface(child1_)
->damage_tracker()
->has_damage_from_contributing_content());
}
TEST_F(DamageTrackerTest, VerifyDamageForSurfaceChangeFromDescendantSurface) {
// CASE 1: If descendant surface changes position, the ancestor surface should
// be damaged with the old and new descendant surface regions.
LayerImpl* root = CreateAndSetUpTestTreeWithTwoSurfaces();
child1_->SetDrawsContent(true);
EmulateDrawingOneFrame(root);
gfx::Rect child_damage_rect;
gfx::Rect root_damage_rect;
ClearDamageForAllSurfaces(root);
SetPostTranslation(child1_.get(), gfx::Vector2dF(105.f, 107.f));
child1_->NoteLayerPropertyChanged();
EmulateDrawingOneFrame(root);
EXPECT_TRUE(GetRenderSurface(child1_)->damage_tracker()->GetDamageRectIfValid(
&child_damage_rect));
EXPECT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&root_damage_rect));
// Damage to the root surface should be the union of child1_'s *entire* render
// surface (in target space), and its old exposed area (also in target
// space).
EXPECT_EQ(gfx::UnionRects(gfx::Rect(100, 100, 206, 208),
gfx::Rect(105, 107, 206, 208))
.ToString(),
root_damage_rect.ToString());
// The child surface should also be damaged.
EXPECT_EQ(gfx::Rect(0, 0, 206, 208).ToString(), child_damage_rect.ToString());
EXPECT_TRUE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
EXPECT_TRUE(GetRenderSurface(child1_)
->damage_tracker()
->has_damage_from_contributing_content());
// CASE 2: Change render surface content rect should make
// |has_damage_from_contributing_content| true.
ClearDamageForAllSurfaces(root);
CreateEffectNode(child2_).render_surface_reason = RenderSurfaceReason::kTest;
EmulateDrawingOneFrame(root);
// Surface property changed only from descendant.
child2_->SetBounds(gfx::Size(120, 140));
EmulateDrawingOneFrame(root);
EXPECT_TRUE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
EXPECT_TRUE(GetRenderSurface(child2_)
->damage_tracker()
->has_damage_from_contributing_content());
// Surface property changed from both parent and descendant.
child2_->SetBounds(gfx::Size(220, 240));
root->layer_tree_impl()->SetOpacityMutated(child2_->element_id(), 0.5f);
EmulateDrawingOneFrame(root);
EXPECT_TRUE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
EXPECT_TRUE(GetRenderSurface(child2_)
->damage_tracker()
->has_damage_from_contributing_content());
}
TEST_F(DamageTrackerTest, VerifyDamageForSurfaceChangeFromViewTransitionLayer) {
ClearLayersAndProperties();
blink::ViewTransitionToken transition_token;
LayerImpl* root = root_layer();
root->SetBounds(gfx::Size(500, 500));
root->layer_tree_impl()->SetDeviceViewportRect(gfx::Rect(root->bounds()));
root->SetDrawsContent(true);
SetupRootProperties(root);
LayerImpl* child1 = AddLayerInActiveTree<TestLayerImpl>();
LayerImpl* grand_child1 = AddLayerInActiveTree<TestLayerImpl>();
LayerImpl* child2 = AddLayerInActiveTree<TestViewTransitionContentLayerImpl>(
viz::ViewTransitionElementResourceId(transition_token, 3), false);
// child 1 of the root - live render surface.
child1->SetBounds(gfx::Size(80, 80));
child1->SetDrawsContent(true);
CopyProperties(root, child1);
CreateTransformNode(child1).post_translation = gfx::Vector2dF(100.f, 100.f);
CreateEffectNode(child1).render_surface_reason = RenderSurfaceReason::kTest;
// grandchild 1 - child of the child1
grand_child1->SetBounds(gfx::Size(10, 20));
grand_child1->SetDrawsContent(true);
CopyProperties(child1, grand_child1);
grand_child1->SetOffsetToTransformParent(gfx::Vector2dF(30.f, 30.f));
// child2 of the root - Shared element layer
child2->SetBounds(gfx::Size(160, 160));
child2->SetDrawsContent(true);
CopyProperties(root, child2);
child2->SetOffsetToTransformParent(gfx::Vector2dF(100.f, 100.f));
SetElementIdsForTesting();
EmulateDrawingOneFrame(root);
// Assign the same element resource id to child 1.
GetRenderSurface(child1)
->OwningEffectNodeMutableForTest()
->view_transition_element_resource_id =
child2->ViewTransitionResourceId();
EmulateDrawingOneFrame(root);
gfx::Rect child1_damage_rect;
gfx::Rect root_damage_rect;
// Next frame
ClearDamageForAllSurfaces(root);
grand_child1->NoteLayerPropertyChanged();
EmulateDrawingOneFrame(root);
EXPECT_TRUE(GetRenderSurface(child1)->damage_tracker()->GetDamageRectIfValid(
&child1_damage_rect));
EXPECT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&root_damage_rect));
EXPECT_EQ(gfx::Rect(30, 30, 10, 20).ToString(),
child1_damage_rect.ToString());
// The damage from the shared content render surface should contributes to
// the view transition layer's parent surface.
EXPECT_EQ(gfx::Rect(130, 130, 50, 70).ToString(),
root_damage_rect.ToString());
EXPECT_TRUE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
EXPECT_TRUE(GetRenderSurface(child1)
->damage_tracker()
->has_damage_from_contributing_content());
}
TEST_F(DamageTrackerTest, VerifyDamageForSurfaceChangeFromAncestorLayer) {
// An ancestor/owning layer changes that affects the position/transform of
// the render surface. Note that in this case, the layer_property_changed flag
// already propagates to the subtree (tested in LayerImpltest), which damages
// the entire child1_ surface, but the damage tracker still needs the correct
// logic to compute the exposed region on the root surface.
// TODO(shawnsingh): the expectations of this test case should change when we
// add support for a unique scissor_rect per RenderSurface. In that case, the
// child1_ surface should be completely unchanged, since we are only
// transforming it, while the root surface would be damaged appropriately.
LayerImpl* root = CreateAndSetUpTestTreeWithTwoSurfaces();
gfx::Rect child_damage_rect;
gfx::Rect root_damage_rect;
ClearDamageForAllSurfaces(root);
gfx::Transform translation;
translation.Translate(-50.f, -50.f);
root->layer_tree_impl()->SetTransformMutated(child1_->element_id(),
translation);
EmulateDrawingOneFrame(root);
EXPECT_TRUE(GetRenderSurface(child1_)->damage_tracker()->GetDamageRectIfValid(
&child_damage_rect));
EXPECT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&root_damage_rect));
// The new surface bounds should be damaged entirely.
EXPECT_EQ(gfx::Rect(190, 190, 16, 18).ToString(),
child_damage_rect.ToString());
// The entire child1_ surface and the old exposed child1_ surface should
// damage the root surface.
// - old child1_ surface in target space: gfx::Rect(290, 290, 16, 18)
// - new child1_ surface in target space: gfx::Rect(240, 240, 16, 18)
EXPECT_EQ(gfx::Rect(240, 240, 66, 68).ToString(),
root_damage_rect.ToString());
EXPECT_TRUE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
EXPECT_FALSE(GetRenderSurface(child1_)
->damage_tracker()
->has_damage_from_contributing_content());
}
TEST_F(DamageTrackerTest, VerifyDamageForAddingAndRemovingRenderSurfaces) {
LayerImpl* root = CreateAndSetUpTestTreeWithTwoSurfaces();
gfx::Rect child_damage_rect;
gfx::Rect root_damage_rect;
// CASE 1: If a descendant surface disappears, its entire old area becomes
// exposed.
ClearDamageForAllSurfaces(root);
SetRenderSurfaceReason(child1_.get(), RenderSurfaceReason::kNone);
EmulateDrawingOneFrame(root);
// Sanity check that there is only one surface now.
ASSERT_EQ(GetRenderSurface(child1_), GetRenderSurface(root));
ASSERT_EQ(4, GetRenderSurface(root)->num_contributors());
EXPECT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&root_damage_rect));
EXPECT_EQ(gfx::Rect(290, 290, 16, 18).ToString(),
root_damage_rect.ToString());
EXPECT_TRUE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
// CASE 2: If a descendant surface appears, its entire old area becomes
// exposed.
// Cycle one frame of no change, just to sanity check that the next rect is
// not because of the old damage state.
ClearDamageForAllSurfaces(root);
EmulateDrawingOneFrame(root);
EXPECT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&root_damage_rect));
EXPECT_TRUE(root_damage_rect.IsEmpty());
// Then change the tree so that the render surface is added back.
ClearDamageForAllSurfaces(root);
SetRenderSurfaceReason(child1_.get(), RenderSurfaceReason::kTest);
EmulateDrawingOneFrame(root);
// Sanity check that there is a new surface now.
ASSERT_TRUE(GetRenderSurface(child1_));
EXPECT_EQ(3, GetRenderSurface(root)->num_contributors());
EXPECT_EQ(2, GetRenderSurface(child1_)->num_contributors());
EXPECT_TRUE(GetRenderSurface(child1_)->damage_tracker()->GetDamageRectIfValid(
&child_damage_rect));
EXPECT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&root_damage_rect));
EXPECT_EQ(gfx::Rect(190, 190, 16, 18).ToString(),
child_damage_rect.ToString());
EXPECT_EQ(gfx::Rect(290, 290, 16, 18).ToString(),
root_damage_rect.ToString());
EXPECT_TRUE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
}
TEST_F(DamageTrackerTest, VerifyNoDamageWhenNothingChanged) {
LayerImpl* root = CreateAndSetUpTestTreeWithTwoSurfaces();
gfx::Rect child_damage_rect;
gfx::Rect root_damage_rect;
// CASE 1: If nothing changes, the damage rect should be empty.
//
ClearDamageForAllSurfaces(root);
EmulateDrawingOneFrame(root);
EXPECT_TRUE(GetRenderSurface(child1_)->damage_tracker()->GetDamageRectIfValid(
&child_damage_rect));
EXPECT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&root_damage_rect));
EXPECT_TRUE(child_damage_rect.IsEmpty());
EXPECT_TRUE(root_damage_rect.IsEmpty());
EXPECT_FALSE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
// CASE 2: If nothing changes twice in a row, the damage rect should still be
// empty.
//
ClearDamageForAllSurfaces(root);
EmulateDrawingOneFrame(root);
EXPECT_TRUE(GetRenderSurface(child1_)->damage_tracker()->GetDamageRectIfValid(
&child_damage_rect));
EXPECT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&root_damage_rect));
EXPECT_TRUE(child_damage_rect.IsEmpty());
EXPECT_TRUE(root_damage_rect.IsEmpty());
EXPECT_FALSE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
}
TEST_F(DamageTrackerTest, VerifyNoDamageForUpdateRectThatDoesNotDrawContent) {
LayerImpl* root = CreateAndSetUpTestTreeWithTwoSurfaces();
gfx::Rect child_damage_rect;
gfx::Rect root_damage_rect;
// In our specific tree, the update rect of child1_ should not cause any
// damage to any surface because it does not actually draw content.
ClearDamageForAllSurfaces(root);
child1_->UnionUpdateRect(gfx::Rect(1, 2));
EmulateDrawingOneFrame(root);
EXPECT_TRUE(GetRenderSurface(child1_)->damage_tracker()->GetDamageRectIfValid(
&child_damage_rect));
EXPECT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&root_damage_rect));
EXPECT_TRUE(child_damage_rect.IsEmpty());
EXPECT_TRUE(root_damage_rect.IsEmpty());
EXPECT_FALSE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
}
TEST_F(DamageTrackerTest, VerifyDamageForMask) {
LayerImpl* root = CreateAndSetUpTestTreeWithOneSurface();
LayerImpl* child = child_layers_[0];
// In the current implementation of the damage tracker, changes to mask
// layers should damage the entire corresponding surface.
ClearDamageForAllSurfaces(root);
CreateTransformNode(child).post_translation =
child->offset_to_transform_parent();
child->SetOffsetToTransformParent(gfx::Vector2dF());
// Set up the mask layer.
CreateEffectNode(child);
auto* mask_layer = AddLayerInActiveTree<FakePictureLayerImpl>();
SetupMaskProperties(child, mask_layer);
Region empty_invalidation;
mask_layer->UpdateRasterSource(
FakeRasterSource::CreateFilled(child->bounds()), &empty_invalidation);
// Add opacity and a grand_child so that the render surface persists even
// after we remove the mask.
LayerImpl* grand_child = AddLayerInActiveTree<LayerImpl>();
grand_child->SetBounds(gfx::Size(2, 2));
grand_child->SetDrawsContent(true);
CopyProperties(child, grand_child);
grand_child->SetOffsetToTransformParent(gfx::Vector2dF(2.f, 2.f));
EmulateDrawingOneFrame(root);
EXPECT_EQ(2, GetRenderSurface(root)->num_contributors());
EXPECT_TRUE(root->contributes_to_drawn_render_surface());
EXPECT_EQ(3, GetRenderSurface(child)->num_contributors());
EXPECT_TRUE(child->contributes_to_drawn_render_surface());
EXPECT_EQ(GetRenderSurface(child), GetRenderSurface(mask_layer));
EXPECT_TRUE(mask_layer->contributes_to_drawn_render_surface());
// CASE 1: the update_rect on a mask layer should damage the rect.
ClearDamageForAllSurfaces(root);
mask_layer->UnionUpdateRect(gfx::Rect(1, 2, 3, 4));
EmulateDrawingOneFrame(root);
gfx::Rect child_damage_rect;
EXPECT_TRUE(GetRenderSurface(child)->damage_tracker()->GetDamageRectIfValid(
&child_damage_rect));
EXPECT_EQ(gfx::Rect(1, 2, 3, 4), child_damage_rect);
EXPECT_TRUE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
EXPECT_TRUE(GetRenderSurface(child)
->damage_tracker()
->has_damage_from_contributing_content());
// CASE 2: a property change on the mask layer should damage the entire
// target surface.
// Advance one frame without damage so that we know the damage rect is not
// leftover from the previous case.
ClearDamageForAllSurfaces(root);
EmulateDrawingOneFrame(root);
EXPECT_TRUE(GetRenderSurface(child)->damage_tracker()->GetDamageRectIfValid(
&child_damage_rect));
EXPECT_TRUE(child_damage_rect.IsEmpty());
// Then test the property change.
ClearDamageForAllSurfaces(root);
mask_layer->NoteLayerPropertyChanged();
EmulateDrawingOneFrame(root);
EXPECT_TRUE(GetRenderSurface(child)->damage_tracker()->GetDamageRectIfValid(
&child_damage_rect));
EXPECT_EQ(gfx::Rect(30, 30), child_damage_rect);
EXPECT_TRUE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
EXPECT_TRUE(GetRenderSurface(child)
->damage_tracker()
->has_damage_from_contributing_content());
// CASE 3: removing the mask also damages the entire target surface.
//
// Advance one frame without damage so that we know the damage rect is not
// leftover from the previous case.
ClearDamageForAllSurfaces(root);
EmulateDrawingOneFrame(root);
EXPECT_TRUE(GetRenderSurface(child)->damage_tracker()->GetDamageRectIfValid(
&child_damage_rect));
EXPECT_TRUE(child_damage_rect.IsEmpty());
// Then test mask removal.
ClearDamageForAllSurfaces(root);
auto layers =
root->layer_tree_impl()->DetachLayersKeepingRootLayerForTesting();
ASSERT_EQ(layers[1].get(), child);
root->layer_tree_impl()->AddLayer(std::move(layers[1]));
ASSERT_EQ(layers[2].get(), mask_layer);
ASSERT_EQ(layers[3].get(), grand_child);
root->layer_tree_impl()->AddLayer(std::move(layers[3]));
CopyProperties(root, child);
CreateEffectNode(child).render_surface_reason = RenderSurfaceReason::kTest;
CopyProperties(child, grand_child);
EmulateDrawingOneFrame(root);
// Sanity check that a render surface still exists.
ASSERT_TRUE(GetRenderSurface(child));
EXPECT_TRUE(GetRenderSurface(child)->damage_tracker()->GetDamageRectIfValid(
&child_damage_rect));
EXPECT_EQ(gfx::Rect(30, 30), child_damage_rect);
EXPECT_TRUE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
}
TEST_F(DamageTrackerTest, DamageWhenAddedExternally) {
LayerImpl* root = CreateAndSetUpTestTreeWithOneSurface();
LayerImpl* child = child_layers_[0];
// Case 1: This test ensures that when the tracker is given damage, that
// it is included with any other partial damage.
//
ClearDamageForAllSurfaces(root);
child->UnionUpdateRect(gfx::Rect(10, 11, 12, 13));
GetRenderSurface(root)->damage_tracker()->AddDamageNextUpdate(
gfx::Rect(15, 16, 32, 33));
EmulateDrawingOneFrame(root);
gfx::Rect root_damage_rect;
EXPECT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&root_damage_rect));
EXPECT_EQ(gfx::UnionRects(gfx::Rect(15, 16, 32, 33),
gfx::Rect(100 + 10, 100 + 11, 12, 13)).ToString(),
root_damage_rect.ToString());
EXPECT_TRUE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
// Case 2: An additional sanity check that adding damage works even when
// nothing on the layer tree changed.
//
ClearDamageForAllSurfaces(root);
GetRenderSurface(root)->damage_tracker()->AddDamageNextUpdate(
gfx::Rect(30, 31, 14, 15));
EmulateDrawingOneFrame(root);
EXPECT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&root_damage_rect));
EXPECT_EQ(gfx::Rect(30, 31, 14, 15).ToString(), root_damage_rect.ToString());
EXPECT_FALSE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
}
TEST_F(DamageTrackerTest, VerifyDamageWithNoContributingLayers) {
LayerImpl* root = root_layer();
ClearDamageForAllSurfaces(root);
LayerImpl* empty_surface = AddLayerInActiveTree<LayerImpl>();
CopyProperties(root, empty_surface);
CreateEffectNode(empty_surface).render_surface_reason =
RenderSurfaceReason::kTest;
EmulateDrawingOneFrame(root);
DCHECK_EQ(GetRenderSurface(empty_surface), empty_surface->render_target());
const RenderSurfaceImpl* target_surface = GetRenderSurface(empty_surface);
gfx::Rect damage_rect;
EXPECT_TRUE(
target_surface->damage_tracker()->GetDamageRectIfValid(&damage_rect));
EXPECT_TRUE(damage_rect.IsEmpty());
EXPECT_FALSE(
target_surface->damage_tracker()->has_damage_from_contributing_content());
}
TEST_F(DamageTrackerTest, VerifyDamageAccumulatesUntilReset) {
// If damage is not cleared, it should accumulate.
LayerImpl* root = CreateAndSetUpTestTreeWithOneSurface();
LayerImpl* child = child_layers_[0];
ClearDamageForAllSurfaces(root);
child->UnionUpdateRect(gfx::Rect(10.f, 11.f, 1.f, 2.f));
EmulateDrawingOneFrame(root);
// Sanity check damage after the first frame; this isnt the actual test yet.
gfx::Rect root_damage_rect;
EXPECT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&root_damage_rect));
EXPECT_EQ(gfx::Rect(110, 111, 1, 2).ToString(), root_damage_rect.ToString());
EXPECT_TRUE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
// New damage, without having cleared the previous damage, should be unioned
// to the previous one.
child->UnionUpdateRect(gfx::Rect(20, 25, 1, 2));
EmulateDrawingOneFrame(root);
EXPECT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&root_damage_rect));
EXPECT_EQ(gfx::Rect(110, 111, 11, 16).ToString(),
root_damage_rect.ToString());
EXPECT_TRUE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
// If we notify the damage tracker that we drew the damaged area, then damage
// should be emptied.
GetRenderSurface(root)->damage_tracker()->DidDrawDamagedArea();
EXPECT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&root_damage_rect));
EXPECT_TRUE(root_damage_rect.IsEmpty());
EXPECT_FALSE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
// Damage should remain empty even after one frame, since there's yet no new
// damage.
EmulateDrawingOneFrame(root);
EXPECT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&root_damage_rect));
EXPECT_TRUE(root_damage_rect.IsEmpty());
EXPECT_FALSE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
}
TEST_F(DamageTrackerTest, HugeDamageRect) {
// This number is so large that we start losing floating point accuracy.
const int kBigNumber = 900000000;
// Walk over a range to find floating point inaccuracy boundaries that move
// toward the wrong direction.
const int kRange = 5000;
for (int i = 0; i < kRange; ++i) {
LayerImpl* root = CreateAndSetUpTestTreeWithOneSurface();
LayerImpl* child = child_layers_[0];
// Set copy request to damage the entire layer.
SetCopyRequest(root);
gfx::Transform transform;
transform.Translate(-kBigNumber, -kBigNumber);
// The child layer covers (0, 0, i, i) of the viewport,
// but has a huge negative position.
child->SetBounds(gfx::Size(kBigNumber + i, kBigNumber + i));
auto& transform_node = CreateTransformNode(child);
transform_node.local = transform;
transform_node.post_translation = child->offset_to_transform_parent();
child->SetOffsetToTransformParent(gfx::Vector2dF());
float device_scale_factor = 1.f;
// Visible rects computed from combining clips in target space and root
// space don't match because of the loss in floating point accuracy. So, we
// skip verify_clip_tree_calculations.
EmulateDrawingOneFrame(root, device_scale_factor);
// The expected damage should cover the visible part of the child layer,
// which is (0, 0, i, i) in the viewport.
gfx::Rect root_damage_rect;
EXPECT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&root_damage_rect));
gfx::Rect damage_we_care_about = gfx::Rect(i, i);
EXPECT_LE(damage_we_care_about.right(), root_damage_rect.right());
EXPECT_LE(damage_we_care_about.bottom(), root_damage_rect.bottom());
EXPECT_TRUE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
}
}
TEST_F(DamageTrackerTest, DamageRectTooBig) {
LayerImpl* root = CreateAndSetUpTestTreeWithOneSurface(2);
LayerImpl* child1 = child_layers_[0];
LayerImpl* child2 = child_layers_[1];
// Set copy request to damage the entire layer.
SetCopyRequest(root);
// Really far left.
child1->SetOffsetToTransformParent(gfx::Vector2dF(
static_cast<float>(std::numeric_limits<int>::min() + 100), 0));
child1->SetBounds(gfx::Size(1, 1));
// Really far right.
child2->SetOffsetToTransformParent(gfx::Vector2dF(
static_cast<float>(std::numeric_limits<int>::max() - 100), 0));
child2->SetBounds(gfx::Size(1, 1));
EmulateDrawingOneFrame(root, 1.f);
// The expected damage would be too large to store in a gfx::Rect, so we
// should damage everything (ie, we don't have a valid rect).
gfx::Rect damage_rect;
EXPECT_FALSE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&damage_rect));
EXPECT_EQ(GetRenderSurface(root)->content_rect(),
GetRenderSurface(root)->GetDamageRect());
EXPECT_TRUE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
}
TEST_F(DamageTrackerTest, DamageRectTooBigWithFilter) {
LayerImpl* root = CreateAndSetUpTestTreeWithOneSurface(2);
LayerImpl* child1 = child_layers_[0];
LayerImpl* child2 = child_layers_[1];
// Set copy request to damage the entire layer.
SetCopyRequest(root);
FilterOperations filters;
filters.Append(FilterOperation::CreateBlurFilter(5.f));
root->SetDrawsContent(true);
SetBackdropFilter(root, filters);
// Really far left.
child1->SetOffsetToTransformParent(gfx::Vector2dF(
static_cast<float>(std::numeric_limits<int>::min() + 100), 0));
child1->SetBounds(gfx::Size(1, 1));
// Really far right.
child2->SetOffsetToTransformParent(gfx::Vector2dF(
static_cast<float>(std::numeric_limits<int>::max() - 100), 0));
child2->SetBounds(gfx::Size(1, 1));
float device_scale_factor = 1.f;
EmulateDrawingOneFrame(root, device_scale_factor);
// The expected damage would be too large to store in a gfx::Rect, so we
// should damage everything (ie, we don't have a valid rect).
gfx::Rect damage_rect;
EXPECT_FALSE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&damage_rect));
EXPECT_EQ(GetRenderSurface(root)->content_rect(),
GetRenderSurface(root)->GetDamageRect());
EXPECT_TRUE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
}
TEST_F(DamageTrackerTest, DamageRectTooBigInRenderSurface) {
LayerImpl* root = CreateAndSetUpTestTreeWithTwoSurfacesDrawingFullyVisible();
// Set copy request to damage the entire layer.
SetCopyRequest(root);
// Really far left.
grand_child1_->SetOffsetToTransformParent(gfx::Vector2dF(
static_cast<float>(std::numeric_limits<int>::min() + 500), 0));
grand_child1_->SetBounds(gfx::Size(1, 1));
grand_child1_->SetDrawsContent(true);
// Really far right.
grand_child2_->SetOffsetToTransformParent(gfx::Vector2dF(
static_cast<float>(std::numeric_limits<int>::max() - 500), 0));
grand_child2_->SetBounds(gfx::Size(1, 1));
grand_child2_->SetDrawsContent(true);
UpdateDrawProperties(host_impl()->active_tree());
// Avoid the descendant-only property change path that skips unioning damage
// from descendant layers.
GetRenderSurface(child1_)->NoteAncestorPropertyChanged();
DamageTracker::UpdateDamageTracking(host_impl()->active_tree());
// The expected damage would be too large to store in a gfx::Rect, so we
// should damage everything on child1_.
gfx::Rect damage_rect;
EXPECT_FALSE(
GetRenderSurface(child1_)->damage_tracker()->GetDamageRectIfValid(
&damage_rect));
EXPECT_EQ(GetRenderSurface(child1_)->content_rect(),
GetRenderSurface(child1_)->GetDamageRect());
// However, the root should just use the child1_ render surface's content rect
// as damage.
ASSERT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&damage_rect));
EXPECT_TRUE(damage_rect.Contains(GetRenderSurface(root)->content_rect()));
EXPECT_TRUE(damage_rect.Contains(
gfx::ToEnclosingRect(GetRenderSurface(child1_)->DrawableContentRect())));
EXPECT_EQ(damage_rect, GetRenderSurface(root)->GetDamageRect());
EXPECT_TRUE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
EXPECT_TRUE(GetRenderSurface(child1_)
->damage_tracker()
->has_damage_from_contributing_content());
// Add new damage, without changing properties, which goes down a different
// path in the damage tracker.
root->layer_tree_impl()->ResetAllChangeTracking();
grand_child1_->AddDamageRect(gfx::Rect(grand_child1_->bounds()));
grand_child2_->AddDamageRect(gfx::Rect(grand_child1_->bounds()));
// Recompute all damage / properties.
UpdateDrawProperties(host_impl()->active_tree());
DamageTracker::UpdateDamageTracking(host_impl()->active_tree());
// Child1 should still not have a valid rect, since the union of the damage of
// its children is not representable by a single rect.
EXPECT_FALSE(
GetRenderSurface(child1_)->damage_tracker()->GetDamageRectIfValid(
&damage_rect));
EXPECT_EQ(GetRenderSurface(child1_)->content_rect(),
GetRenderSurface(child1_)->GetDamageRect());
// Root should have valid damage and contain both its content rect and the
// drawable content rect of child1_.
ASSERT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&damage_rect));
EXPECT_TRUE(damage_rect.Contains(GetRenderSurface(root)->content_rect()));
EXPECT_TRUE(damage_rect.Contains(
gfx::ToEnclosingRect(GetRenderSurface(child1_)->DrawableContentRect())));
EXPECT_EQ(damage_rect, GetRenderSurface(root)->GetDamageRect());
EXPECT_TRUE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
EXPECT_TRUE(GetRenderSurface(child1_)
->damage_tracker()
->has_damage_from_contributing_content());
}
TEST_F(DamageTrackerTest, DamageRectTooBigInRenderSurfaceWithFilter) {
LayerImpl* root = CreateAndSetUpTestTreeWithTwoSurfaces();
// Set copy request to damage the entire layer.
SetCopyRequest(root);
// Set up a moving pixels filter on the child.
FilterOperations filters;
filters.Append(FilterOperation::CreateBlurFilter(5.f));
child1_->SetDrawsContent(true);
SetBackdropFilter(child1_.get(), filters);
// Really far left.
grand_child1_->SetOffsetToTransformParent(gfx::Vector2dF(
static_cast<float>(std::numeric_limits<int>::min() + 500), 0));
grand_child1_->SetBounds(gfx::Size(1, 1));
grand_child1_->SetDrawsContent(true);
// Really far right.
grand_child2_->SetOffsetToTransformParent(gfx::Vector2dF(
static_cast<float>(std::numeric_limits<int>::max() - 500), 0));
grand_child2_->SetBounds(gfx::Size(1, 1));
grand_child2_->SetDrawsContent(true);
UpdateDrawProperties(host_impl()->active_tree());
// Avoid the descendant-only property change path that skips unioning damage
// from descendant layers.
GetRenderSurface(child1_)->NoteAncestorPropertyChanged();
DamageTracker::UpdateDamageTracking(host_impl()->active_tree());
// The expected damage would be too large to store in a gfx::Rect, so we
// should damage everything on child1_.
gfx::Rect damage_rect;
EXPECT_FALSE(
GetRenderSurface(child1_)->damage_tracker()->GetDamageRectIfValid(
&damage_rect));
EXPECT_EQ(GetRenderSurface(child1_)->content_rect(),
GetRenderSurface(child1_)->GetDamageRect());
// However, the root should just use the child1_ render surface's content rect
// as damage.
ASSERT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&damage_rect));
EXPECT_TRUE(damage_rect.Contains(GetRenderSurface(root)->content_rect()));
EXPECT_TRUE(damage_rect.Contains(
gfx::ToEnclosingRect(GetRenderSurface(child1_)->DrawableContentRect())));
EXPECT_EQ(damage_rect, GetRenderSurface(root)->GetDamageRect());
EXPECT_TRUE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
EXPECT_TRUE(GetRenderSurface(child1_)
->damage_tracker()
->has_damage_from_contributing_content());
// Add new damage, without changing properties, which goes down a different
// path in the damage tracker.
root->layer_tree_impl()->ResetAllChangeTracking();
grand_child1_->AddDamageRect(gfx::Rect(grand_child1_->bounds()));
grand_child2_->AddDamageRect(gfx::Rect(grand_child1_->bounds()));
// Recompute all damage / properties.
UpdateDrawProperties(host_impl()->active_tree());
DamageTracker::UpdateDamageTracking(host_impl()->active_tree());
// Child1 should still not have a valid rect, since the union of the damage of
// its children is not representable by a single rect.
EXPECT_FALSE(
GetRenderSurface(child1_)->damage_tracker()->GetDamageRectIfValid(
&damage_rect));
EXPECT_EQ(GetRenderSurface(child1_)->content_rect(),
GetRenderSurface(child1_)->GetDamageRect());
// Root should have valid damage and contain both its content rect and the
// drawable content rect of child1_.
ASSERT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&damage_rect));
EXPECT_TRUE(damage_rect.Contains(GetRenderSurface(root)->content_rect()));
EXPECT_TRUE(damage_rect.Contains(
gfx::ToEnclosingRect(GetRenderSurface(child1_)->DrawableContentRect())));
EXPECT_EQ(damage_rect, GetRenderSurface(root)->GetDamageRect());
EXPECT_TRUE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
EXPECT_TRUE(GetRenderSurface(child1_)
->damage_tracker()
->has_damage_from_contributing_content());
}
TEST_F(DamageTrackerTest, CanUseCachedBackdropFilterResultTest) {
LayerImpl* root = CreateAndSetUpTestTreeWithFourSurfaces();
// child1_ has a screen space rect of 270,270 36x38, from
// - grand_child1_ 300,300, 6x8
// - grand_child2_ 290,290, 6x8
// - grand_child3_ 270,270, 6x8
// - grand_child4_ 280,280, 15x16
// child2_ has a screen space rect 11,11 18x18
ClearDamageForAllSurfaces(root);
// Add a backdrop blur filter onto child1_
FilterOperations filters;
filters.Append(FilterOperation::CreateBlurFilter(2.f));
SetBackdropFilter(child1_.get(), filters);
child1_->NoteLayerPropertyChanged();
// intersects_damage_under_ is false by default.
EXPECT_TRUE(GetRenderSurface(child1_)->intersects_damage_under());
EmulateDrawingOneFrame(root);
// child1_'s render target has changed its surface property.
EXPECT_TRUE(GetRenderSurface(child1_)->intersects_damage_under());
// Let run for one update and there should be no damage left.
EmulateDrawingOneFrame(root);
EXPECT_FALSE(GetRenderSurface(child1_)->intersects_damage_under());
// CASE 1.1: Setting a non-intersecting update rect on the root
// doesn't invalidate child1_'s cached backdrop-filtered result.
// Damage rect at 0,0 20x20 doesn't intersect 270,270 36x38.
root->UnionUpdateRect(gfx::Rect(0, 0, 20, 20));
EmulateDrawingOneFrame(root);
EXPECT_FALSE(GetRenderSurface(child1_)->intersects_damage_under());
// CASE 1.2: Setting an intersecting update rect on the root invalidates
// child1_'s cached backdrop-filtered result.
// Damage rect at 260,260 20x20 intersects 270,270 36x38.
ClearDamageForAllSurfaces(root);
root->UnionUpdateRect(gfx::Rect(260, 260, 20, 20));
EmulateDrawingOneFrame(root);
EXPECT_TRUE(GetRenderSurface(child1_)->intersects_damage_under());
// CASE 1.3: Damage on layers above the surface with the backdrop filter
// doesn't invalidate cached backdrop-filtered result. Move child2_ to overlap
// with child1_.
ClearDamageForAllSurfaces(root);
child2_->SetOffsetToTransformParent(gfx::Vector2dF(180.f, 180.f));
EmulateDrawingOneFrame(root);
EXPECT_FALSE(GetRenderSurface(grand_child1_)->intersects_damage_under());
// CASE 2: Adding or removing a backdrop filter would invalidate cached
// backdrop-filtered result of the corresponding render surfaces.
ClearDamageForAllSurfaces(root);
// Remove the backdrop filter on child1_
SetBackdropFilter(child1_.get(), FilterOperations());
grand_child1_->NoteLayerPropertyChanged();
// Add a backdrop blur filtre to grand_child4_
SetBackdropFilter(grand_child4_.get(), filters);
grand_child4_->NoteLayerPropertyChanged();
EmulateDrawingOneFrame(root);
EXPECT_TRUE(GetRenderSurface(grand_child4_)->intersects_damage_under());
EXPECT_TRUE(GetRenderSurface(grand_child1_)->intersects_damage_under());
// Let run for one update and there should be no damage left.
EmulateDrawingOneFrame(root);
EXPECT_FALSE(GetRenderSurface(grand_child4_)->intersects_damage_under());
// CASE 3.1: Adding a non-intersecting damage rect to a sibling layer under
// the render surface with the backdrop filter doesn't invalidate cached
// backdrop-filtered result. Damage rect on grand_child1_ at 302,302 1x1
// doesn't intersect 280,280 15x16.
ClearDamageForAllSurfaces(root);
grand_child1_->AddDamageRect(gfx::Rect(2, 2, 1.f, 1.f));
EmulateDrawingOneFrame(root);
EXPECT_FALSE(GetRenderSurface(grand_child4_)->intersects_damage_under());
// CASE 3.2: Adding an intersecting damage rect to a sibling layer under the
// render surface with the backdrop filter invalidates cached
// backdrop-filtered result. Damage rect on grand_child2_ at 290,290 1x1
// intersects 280,280 15x16.
ClearDamageForAllSurfaces(root);
grand_child2_->AddDamageRect(gfx::Rect(0, 0, 1.f, 1.f));
EmulateDrawingOneFrame(root);
EXPECT_TRUE(GetRenderSurface(grand_child4_)->intersects_damage_under());
// CASE 4.1: Non-intersecting damage rect on a sibling surface under the
// render surface with the backdrop filter doesn't invalidate cached
// backdrop-filtered result.
ClearDamageForAllSurfaces(root);
grand_child3_->AddDamageRect(gfx::Rect(0, 0, 1.f, 1.f));
EmulateDrawingOneFrame(root);
gfx::Rect damage_rect;
EXPECT_TRUE(GetRenderSurface(grand_child3_)
->damage_tracker()
->GetDamageRectIfValid(&damage_rect));
EXPECT_EQ(gfx::Rect(170, 170, 1.f, 1.f), damage_rect);
// Damage rect at 170,170 1x1 in render target local space doesn't intersect
// 180,180 15x16.
EXPECT_FALSE(GetRenderSurface(grand_child4_)->intersects_damage_under());
// CASE 4.2: Intersecting damage rect on a sibling surface under the render
// surface with the backdrop filter invalidates cached backdrop-filtered
// result.
ClearDamageForAllSurfaces(root);
grand_child3_->SetBounds(gfx::Size(11.f, 11.f));
EmulateDrawingOneFrame(root);
EXPECT_TRUE(GetRenderSurface(grand_child3_)
->damage_tracker()
->GetDamageRectIfValid(&damage_rect));
EXPECT_EQ(gfx::Rect(170, 170, 11.f, 11.f), damage_rect);
// Damage rect at 170,170 11x11 in render target local space intersects
// 180,180 15x16
EXPECT_TRUE(GetRenderSurface(grand_child4_)->intersects_damage_under());
// CASE 5.1: Removing a non-intersecting sibling layer under the render
// surface with the backdrop filter doesn't invalidate cached
// backdrop-filtered result. Removing grand_child1_ at 300,300 6x8 which
// doesn't intersect 280,280 15x16.
ClearDamageForAllSurfaces(root);
OwnedLayerImplList layers =
host_impl()->active_tree()->DetachLayersKeepingRootLayerForTesting();
ASSERT_EQ(7u, layers.size());
ASSERT_EQ(child1_, layers[1].get());
ASSERT_EQ(grand_child1_, layers[2].get());
ASSERT_EQ(grand_child2_, layers[3].get());
ASSERT_EQ(grand_child3_, layers[4].get());
ASSERT_EQ(grand_child4_, layers[5].get());
ASSERT_EQ(child2_, layers[6].get());
host_impl()->active_tree()->AddLayer(std::move(layers[1]));
host_impl()->active_tree()->AddLayer(std::move(layers[3]));
host_impl()->active_tree()->AddLayer(std::move(layers[4]));
host_impl()->active_tree()->AddLayer(std::move(layers[5]));
host_impl()->active_tree()->AddLayer(std::move(layers[6]));
EmulateDrawingOneFrame(root);
EXPECT_FALSE(GetRenderSurface(grand_child4_)->intersects_damage_under());
// CASE 5.2: Removing an intersecting sibling layer under the render surface
// with the backdrop filter invalidates cached backdrop-filtered result.
// Removing grand_child2_ at 290,290 6x8 which intersects 280,280 15x16.
ClearDamageForAllSurfaces(root);
layers = host_impl()->active_tree()->DetachLayersKeepingRootLayerForTesting();
ASSERT_EQ(6u, layers.size());
ASSERT_EQ(child1_, layers[1].get());
ASSERT_EQ(grand_child2_, layers[2].get());
ASSERT_EQ(grand_child3_, layers[3].get());
ASSERT_EQ(grand_child4_, layers[4].get());
ASSERT_EQ(child2_, layers[5].get());
host_impl()->active_tree()->AddLayer(std::move(layers[1]));
host_impl()->active_tree()->AddLayer(std::move(layers[3]));
host_impl()->active_tree()->AddLayer(std::move(layers[4]));
host_impl()->active_tree()->AddLayer(std::move(layers[5]));
EmulateDrawingOneFrame(root);
EXPECT_TRUE(GetRenderSurface(grand_child4_)->intersects_damage_under());
// Let run for one update and there should be no damage left.
ClearDamageForAllSurfaces(root);
EmulateDrawingOneFrame(root);
EXPECT_FALSE(GetRenderSurface(grand_child4_)->intersects_damage_under());
// CASE 6: Removing a intersecting sibling surface under the render
// surface with the backdrop filter invalidate cached backdrop-filtered
// result.
ClearDamageForAllSurfaces(root);
SetRenderSurfaceReason(grand_child3_.get(), RenderSurfaceReason::kNone);
grand_child3_->SetDrawsContent(false);
EmulateDrawingOneFrame(root);
EXPECT_TRUE(GetRenderSurface(grand_child4_)->intersects_damage_under());
}
TEST_F(DamageTrackerTest, DamageRectOnlyVisibleContentsMoveToOutside) {
LayerImpl* root = CreateAndSetUpTestTreeWithOneSurface(2);
ClearDamageForAllSurfaces(root);
LayerImpl* child1 = child_layers_[0];
LayerImpl* child2 = child_layers_[1];
gfx::Rect origin_damage = child1->visible_drawable_content_rect();
origin_damage.Union(child2->visible_drawable_content_rect());
// Really far left.
child1->SetOffsetToTransformParent(gfx::Vector2dF(
static_cast<float>(std::numeric_limits<int>::min() + 100), 0));
child1->SetBounds(gfx::Size(1, 1));
// Really far right.
child2->SetOffsetToTransformParent(gfx::Vector2dF(
static_cast<float>(std::numeric_limits<int>::max() - 100), 0));
child2->SetBounds(gfx::Size(1, 1));
EmulateDrawingOneFrame(root, 1.f);
// Above damages should be excludebe because they're outside of
// the root surface.
gfx::Rect damage_rect;
EXPECT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&damage_rect));
EXPECT_EQ(origin_damage, damage_rect);
EXPECT_TRUE(GetRenderSurface(root)->content_rect().Contains(damage_rect));
EXPECT_TRUE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
}
TEST_F(DamageTrackerTest, DamageRectOnlyVisibleContentsLargeTwoContents) {
LayerImpl* root = CreateAndSetUpTestTreeWithOneSurface(2);
ClearDamageForAllSurfaces(root);
LayerImpl* child1 = child_layers_[0];
LayerImpl* child2 = child_layers_[1];
gfx::Rect expected_damage = child1->visible_drawable_content_rect();
expected_damage.Union(child2->visible_drawable_content_rect());
expected_damage.set_x(0);
expected_damage.set_width(GetRenderSurface(root)->content_rect().width());
// Really far left.
child1->SetOffsetToTransformParent(gfx::Vector2dF(
static_cast<float>(std::numeric_limits<int>::min() + 100), 100));
child1->SetBounds(
gfx::Size(std::numeric_limits<int>::max(), child1->bounds().height()));
// Really far right.
child2->SetOffsetToTransformParent(gfx::Vector2dF(100, 100));
child2->SetBounds(
gfx::Size(std::numeric_limits<int>::max(), child2->bounds().height()));
EmulateDrawingOneFrame(root, 1.f);
// Above damages should be excluded because they're outside of
// the root surface.
gfx::Rect damage_rect;
EXPECT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&damage_rect));
EXPECT_EQ(expected_damage, damage_rect);
EXPECT_TRUE(GetRenderSurface(root)->content_rect().Contains(damage_rect));
EXPECT_TRUE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
}
TEST_F(DamageTrackerTest,
DamageRectOnlyVisibleContentsHugeContentPartiallyVisible) {
LayerImpl* root = CreateAndSetUpTestTreeWithOneSurface(1);
int content_width = GetRenderSurface(root)->content_rect().width();
ClearDamageForAllSurfaces(root);
LayerImpl* child1 = child_layers_[0];
int y = child1->offset_to_transform_parent().y();
int offset = 100;
int expected_width = offset + child1->bounds().width();
// Huge content that exceeds on both side.
child1->SetOffsetToTransformParent(
gfx::Vector2dF(std::numeric_limits<int>::min() + offset, y));
child1->SetBounds(
gfx::Size(std::numeric_limits<int>::max(), child1->bounds().height()));
EmulateDrawingOneFrame(root);
gfx::Rect expected_damage_rect1(0, y, expected_width,
child1->bounds().height());
// Above damages should be excludebe because they're outside of
// the root surface.
gfx::Rect damage_rect;
EXPECT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&damage_rect));
EXPECT_EQ(expected_damage_rect1, damage_rect);
EXPECT_TRUE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
ClearDamageForAllSurfaces(root);
// Now move the huge layer to the right, keeping offset visible.
child1->SetOffsetToTransformParent(gfx::Vector2dF(content_width - offset, y));
child1->NoteLayerPropertyChanged();
EmulateDrawingOneFrame(root);
// The damaged rect should be "letter boxed" region.
gfx::Rect expected_damage_rect2(0, y, content_width,
child1->bounds().height());
EXPECT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&damage_rect));
EXPECT_EQ(expected_damage_rect2, damage_rect);
EXPECT_TRUE(GetRenderSurface(root)
->damage_tracker()
->has_damage_from_contributing_content());
}
TEST_F(DamageTrackerTest, VerifyDamageExpansionWithBackdropBlurFilters) {
LayerImpl* root = CreateAndSetUpTestTreeWithTwoSurfaces();
// Allow us to set damage on child1_.
child1_->SetDrawsContent(true);
FilterOperations filters;
filters.Append(FilterOperation::CreateBlurFilter(2.f));
// Setting the filter will damage the whole surface.
ClearDamageForAllSurfaces(root);
SetBackdropFilter(child1_.get(), filters);
child1_->NoteLayerPropertyChanged();
EmulateDrawingOneFrame(root);
ClearDamageForAllSurfaces(root);
root->UnionUpdateRect(gfx::Rect(297, 297, 2, 2));
EmulateDrawingOneFrame(root);
// child1_'s render surface has a size of 206x208 due to the contributions
// from grand_child1_ and grand_child2_. The blur filter on child1_ intersects
// the damage from root and expands it to (100,100 206x208).
gfx::Rect expected_damage_rect = gfx::Rect(100, 100, 206, 208);
gfx::Rect root_damage_rect;
EXPECT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&root_damage_rect));
EXPECT_EQ(expected_damage_rect, root_damage_rect);
ClearDamageForAllSurfaces(root);
gfx::Rect damage_rect(97, 97, 2, 2);
root->UnionUpdateRect(damage_rect);
EmulateDrawingOneFrame(root);
// The blur filter on child1_ doesn't intersect the damage from root so the
// damage remains unchanged.
EXPECT_TRUE(GetRenderSurface(root)->damage_tracker()->GetDamageRectIfValid(
&root_damage_rect));
EXPECT_EQ(damage_rect, root_damage_rect);
}
} // namespace
} // namespace cc