summaryrefslogtreecommitdiff
path: root/Source/WebKit/chromium/tests/TiledLayerChromiumTest.cpp
blob: e39fe7e569c7ed2addd5acc12a438a2118053930 (plain)
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
/*
 * Copyright (C) 2011 Google Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1.  Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 * 2.  Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"

#include "TiledLayerChromium.h"

#include "BitmapCanvasLayerTextureUpdater.h"
#include "CCAnimationTestCommon.h"
#include "CCLayerTreeTestCommon.h"
#include "CCTiledLayerTestCommon.h"
#include "FakeCCGraphicsContext.h"
#include "FakeCCLayerTreeHostClient.h"
#include "LayerPainterChromium.h"
#include "WebCompositor.h"
#include "cc/CCOverdrawMetrics.h"
#include "cc/CCRenderingStats.h"
#include "cc/CCSingleThreadProxy.h" // For DebugScopedSetImplThread
#include <gtest/gtest.h>
#include <public/WebTransformationMatrix.h>

using namespace WebCore;
using namespace WebKitTests;
using namespace WTF;
using WebKit::WebTransformationMatrix;

#define EXPECT_EQ_RECT(a, b) \
    EXPECT_EQ(a.x(), b.x()); \
    EXPECT_EQ(a.y(), b.y()); \
    EXPECT_EQ(a.width(), b.width()); \
    EXPECT_EQ(a.height(), b.height());

namespace {

class TestCCOcclusionTracker : public CCOcclusionTracker {
public:
    TestCCOcclusionTracker()
        : CCOcclusionTracker(IntRect(0, 0, 1000, 1000), true)
        , m_scissorRectInScreen(IntRect(0, 0, 1000, 1000))
    {
        // Pretend we have visited a render surface.
        m_stack.append(StackObject());
    }

    void setOcclusion(const Region& occlusion) { m_stack.last().occlusionInScreen = occlusion; }

protected:
    virtual IntRect layerScissorRectInTargetSurface(const LayerChromium* layer) const { return m_scissorRectInScreen; }

private:
    IntRect m_scissorRectInScreen;
};

class TiledLayerChromiumTest : public testing::Test {
public:
    TiledLayerChromiumTest()
        : m_context(WebKit::createFakeCCGraphicsContext())
    {
        DebugScopedSetImplThread implThread;
        m_resourceProvider = CCResourceProvider::create(m_context.get());
    }

    virtual ~TiledLayerChromiumTest()
    {
        DebugScopedSetImplThread implThread;
        m_resourceProvider.clear();
    }

    void updateTextures(int count = 500)
    {
        m_updater.update(m_resourceProvider.get(), &m_copier, &m_uploader, count);
    }
public:
    OwnPtr<CCGraphicsContext> m_context;
    OwnPtr<CCResourceProvider> m_resourceProvider;
    CCTextureUpdater m_updater;
    CCRenderingStats m_stats;
    FakeTextureCopier m_copier;
    FakeTextureUploader m_uploader;
    CCPriorityCalculator m_priorityCalculator;
};



TEST_F(TiledLayerChromiumTest, pushDirtyTiles)
{
    OwnPtr<CCPrioritizedTextureManager> textureManager = CCPrioritizedTextureManager::create(4*1024*1024, 1024, CCRenderer::ContentPool);
    RefPtr<FakeTiledLayerChromium> layer = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
    DebugScopedSetImplThread implThread;
    OwnPtr<FakeCCTiledLayerImpl> layerImpl(adoptPtr(new FakeCCTiledLayerImpl(1)));

    // The tile size is 100x100, so this invalidates and then paints two tiles.
    layer->setBounds(IntSize(100, 200));
    layer->setVisibleContentRect(IntRect(0, 0, 100, 200));
    layer->invalidateContentRect(IntRect(0, 0, 100, 200));

    layer->setTexturePriorities(m_priorityCalculator);
    textureManager->prioritizeTextures();

    layer->updateContentRect(m_updater, IntRect(0, 0, 100, 200), 0, m_stats);
    updateTextures();
    layer->pushPropertiesTo(layerImpl.get());

    // We should have both tiles on the impl side.
    EXPECT_TRUE(layerImpl->hasTileAt(0, 0));
    EXPECT_TRUE(layerImpl->hasTileAt(0, 1));

    // Invalidates both tiles...
    layer->invalidateContentRect(IntRect(0, 0, 100, 200));
    // ....but then only update one of them.
    layer->setTexturePriorities(m_priorityCalculator);
    textureManager->prioritizeTextures();
    layer->updateContentRect(m_updater, IntRect(0, 0, 100, 100), 0, m_stats);
    layer->pushPropertiesTo(layerImpl.get());

    // We should only have the first tile since the other tile was invalidated but not painted.
    EXPECT_TRUE(layerImpl->hasTileAt(0, 0));
    EXPECT_FALSE(layerImpl->hasTileAt(0, 1));
}

TEST_F(TiledLayerChromiumTest, pushOccludedDirtyTiles)
{
    OwnPtr<CCPrioritizedTextureManager> textureManager = CCPrioritizedTextureManager::create(4*1024*1024, 1024, CCRenderer::ContentPool);
    RefPtr<FakeTiledLayerChromium> layer = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
    DebugScopedSetImplThread implThread;
    OwnPtr<FakeCCTiledLayerImpl> layerImpl(adoptPtr(new FakeCCTiledLayerImpl(1)));
    TestCCOcclusionTracker occluded;

    // The tile size is 100x100, so this invalidates and then paints two tiles.
    layer->setBounds(IntSize(100, 200));
    layer->setVisibleContentRect(IntRect(0, 0, 100, 200));
    layer->invalidateContentRect(IntRect(0, 0, 100, 200));

    layer->setTexturePriorities(m_priorityCalculator);
    textureManager->prioritizeTextures();

    layer->updateContentRect(m_updater, IntRect(0, 0, 100, 200), &occluded, m_stats);
    updateTextures();
    layer->pushPropertiesTo(layerImpl.get());

    EXPECT_NEAR(occluded.overdrawMetrics().pixelsUploadedOpaque(), 0, 1);
    EXPECT_NEAR(occluded.overdrawMetrics().pixelsUploadedTranslucent(), 20000, 1);
    EXPECT_EQ(0, occluded.overdrawMetrics().tilesCulledForUpload());

    // We should have both tiles on the impl side.
    EXPECT_TRUE(layerImpl->hasTileAt(0, 0));
    EXPECT_TRUE(layerImpl->hasTileAt(0, 1));

    // Invalidates part of the top tile...
    layer->invalidateContentRect(IntRect(0, 0, 50, 50));
    // ....but the area is occluded.
    occluded.setOcclusion(IntRect(0, 0, 50, 50));
    layer->updateContentRect(m_updater, IntRect(0, 0, 100, 100), &occluded, m_stats);
    updateTextures();
    layer->pushPropertiesTo(layerImpl.get());

    EXPECT_NEAR(occluded.overdrawMetrics().pixelsUploadedOpaque(), 0, 1);
    EXPECT_NEAR(occluded.overdrawMetrics().pixelsUploadedTranslucent(), 20000 + 2500, 1);
    EXPECT_EQ(0, occluded.overdrawMetrics().tilesCulledForUpload());

    // We should still have both tiles, as part of the top tile is still unoccluded.
    EXPECT_TRUE(layerImpl->hasTileAt(0, 0));
    EXPECT_TRUE(layerImpl->hasTileAt(0, 1));
}

TEST_F(TiledLayerChromiumTest, pushDeletedTiles)
{
    OwnPtr<CCPrioritizedTextureManager> textureManager = CCPrioritizedTextureManager::create(4*1024*1024, 1024, CCRenderer::ContentPool);
    RefPtr<FakeTiledLayerChromium> layer = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
    DebugScopedSetImplThread implThread;
    OwnPtr<FakeCCTiledLayerImpl> layerImpl(adoptPtr(new FakeCCTiledLayerImpl(1)));

    // The tile size is 100x100, so this invalidates and then paints two tiles.
    layer->setBounds(IntSize(100, 200));
    layer->setVisibleContentRect(IntRect(0, 0, 100, 200));
    layer->invalidateContentRect(IntRect(0, 0, 100, 200));

    layer->setTexturePriorities(m_priorityCalculator);
    textureManager->prioritizeTextures();

    layer->updateContentRect(m_updater, IntRect(0, 0, 100, 200), 0, m_stats);
    updateTextures();
    layer->pushPropertiesTo(layerImpl.get());

    // We should have both tiles on the impl side.
    EXPECT_TRUE(layerImpl->hasTileAt(0, 0));
    EXPECT_TRUE(layerImpl->hasTileAt(0, 1));

    textureManager->clearPriorities();
    textureManager->clearAllMemory(m_resourceProvider.get());
    textureManager->setMaxMemoryLimitBytes(4*1024*1024);

    // This should drop the tiles on the impl thread.
    layer->pushPropertiesTo(layerImpl.get());

    // We should now have no textures on the impl thread.
    EXPECT_FALSE(layerImpl->hasTileAt(0, 0));
    EXPECT_FALSE(layerImpl->hasTileAt(0, 1));

    // This should recreate and update the deleted textures.
    layer->setTexturePriorities(m_priorityCalculator);
    textureManager->prioritizeTextures();
    layer->updateContentRect(m_updater, IntRect(0, 0, 100, 100), 0, m_stats);
    updateTextures();
    layer->pushPropertiesTo(layerImpl.get());

    // We should only have the first tile since the other tile was invalidated but not painted.
    EXPECT_TRUE(layerImpl->hasTileAt(0, 0));
    EXPECT_FALSE(layerImpl->hasTileAt(0, 1));
}

TEST_F(TiledLayerChromiumTest, pushIdlePaintTiles)
{
    OwnPtr<CCPrioritizedTextureManager> textureManager = CCPrioritizedTextureManager::create(4*1024*1024, 1024, CCRenderer::ContentPool);
    RefPtr<FakeTiledLayerChromium> layer = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
    DebugScopedSetImplThread implThread;
    OwnPtr<FakeCCTiledLayerImpl> layerImpl(adoptPtr(new FakeCCTiledLayerImpl(1)));

    // The tile size is 100x100. Setup 5x5 tiles with one visible tile in the center.
    IntSize contentBounds(500, 500);
    IntRect contentRect(IntPoint::zero(), contentBounds);
    IntRect visibleRect(200, 200, 100, 100);

    // This invalidates 25 tiles and then paints one visible tile.
    layer->setBounds(contentBounds);
    layer->setVisibleContentRect(visibleRect);
    layer->invalidateContentRect(contentRect);

    layer->setTexturePriorities(m_priorityCalculator);
    textureManager->prioritizeTextures();

    layer->updateContentRect(m_updater, visibleRect, 0, m_stats);
    updateTextures();

    // We should need idle-painting for 3x3 tiles in the center.
    EXPECT_TRUE(layer->needsIdlePaint(visibleRect));

    layer->pushPropertiesTo(layerImpl.get());

    // We should have one tile on the impl side.
    EXPECT_TRUE(layerImpl->hasTileAt(2, 2));

    // For the next four updates, we should detect we still need idle painting.
    for (int i = 0; i < 4; i++) {
        layer->setTexturePriorities(m_priorityCalculator);
        textureManager->prioritizeTextures();

        layer->updateContentRect(m_updater, visibleRect, 0, m_stats);
        EXPECT_TRUE(layer->needsIdlePaint(visibleRect));
        updateTextures();
        layer->pushPropertiesTo(layerImpl.get());
    }

    // After four passes of idle painting, we should be finished painting
    // EXPECT_FALSE(layer->needsIdlePaint(visibleRect));

    // We should have one tile surrounding the visible tile on all sides, but no other tiles.
    IntRect idlePaintTiles(1, 1, 3, 3);
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++) {
            if (idlePaintTiles.contains(i, j))
                EXPECT_TRUE(layerImpl->hasTileAt(i, j));
            else
                EXPECT_FALSE(layerImpl->hasTileAt(i, j));
        }
    }
}

TEST_F(TiledLayerChromiumTest, pushTilesAfterIdlePaintFailed)
{
    // Start with 2mb of memory, but the test is going to try to use just more than 1mb, so we reduce to 1mb later.
    OwnPtr<CCPrioritizedTextureManager> textureManager = CCPrioritizedTextureManager::create(2*1024*1024, 1024, CCRenderer::ContentPool);
    DebugScopedSetImplThread implThread;
    RefPtr<FakeTiledLayerChromium> layer1 = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
    OwnPtr<FakeCCTiledLayerImpl> layerImpl1(adoptPtr(new FakeCCTiledLayerImpl(1)));
    RefPtr<FakeTiledLayerChromium> layer2 = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
    OwnPtr<FakeCCTiledLayerImpl> layerImpl2(adoptPtr(new FakeCCTiledLayerImpl(2)));

    // For this test we have two layers. layer1 exhausts most texture memory, leaving room for 2 more tiles from
    // layer2, but not all three tiles. First we paint layer1, and one tile from layer2. Then when we idle paint
    // layer2, we will fail on the third tile of layer2, and this should not leave the second tile in a bad state.

    // This requires 4*30000 bytes of memory.
    IntRect layer2Rect(0, 0, 100, 300);
    layer2->setBounds(layer2Rect.size());
    layer2->setVisibleContentRect(layer2Rect);
    layer2->invalidateContentRect(layer2Rect);

    // This uses 960000 bytes, leaving 88576 bytes of memory left, which is enough for 2 tiles only in the other layer.
    IntRect layerRect(IntPoint::zero(), IntSize(100, 2400));
    layer1->setBounds(layerRect.size());
    layer1->setVisibleContentRect(layerRect);
    layer1->invalidateContentRect(layerRect);

    // Paint a single tile in layer2 so that it will idle paint.
    layer2->setTexturePriorities(m_priorityCalculator);
    layer1->setTexturePriorities(m_priorityCalculator);
    textureManager->prioritizeTextures();
    layer1->updateContentRect(m_updater, layerRect, 0, m_stats);
    layer2->updateContentRect(m_updater, IntRect(0, 0, 100, 100), 0, m_stats);

    // We should need idle-painting for both remaining tiles in layer2.
    EXPECT_TRUE(layer2->needsIdlePaint(layer2Rect));

    // Commit the frame over to impl.
    updateTextures();
    layer1->pushPropertiesTo(layerImpl1.get());
    layer2->pushPropertiesTo(layerImpl2.get());

    // Reduce our memory limits to 1mb.
    textureManager->setMaxMemoryLimitBytes(1024 * 1024);

    // Now idle paint layer2. We are going to run out of memory though!
    layer2->setTexturePriorities(m_priorityCalculator);
    layer1->setTexturePriorities(m_priorityCalculator);
    textureManager->prioritizeTextures();
    layer2->updateContentRect(m_updater, IntRect(0, 0, 100, 100), 0, m_stats);

    // Oh well, commit the frame and push.
    updateTextures();
    layer1->pushPropertiesTo(layerImpl1.get());
    layer2->pushPropertiesTo(layerImpl2.get());

    // Sanity check, we should have textures for the big layer.
    EXPECT_TRUE(layerImpl1->hasTextureIdForTileAt(0, 0));

    // We should only have the first tile from layer2 since it failed to idle update.
    EXPECT_TRUE(layerImpl2->hasTileAt(0, 0));
    EXPECT_TRUE(layerImpl2->hasTextureIdForTileAt(0, 0));
    EXPECT_FALSE(layerImpl2->hasTileAt(0, 1));
    EXPECT_FALSE(layerImpl2->hasTileAt(0, 2));

    // Now if layer2 becomes fully visible, we should be able to paint it and push valid textures.
    layer2->setTexturePriorities(m_priorityCalculator);
    layer1->setTexturePriorities(m_priorityCalculator);
    textureManager->prioritizeTextures();
    layer2->updateContentRect(m_updater, layer2Rect, 0, m_stats);
    layer1->updateContentRect(m_updater, layerRect, 0, m_stats);

    updateTextures();
    layer1->pushPropertiesTo(layerImpl1.get());
    layer2->pushPropertiesTo(layerImpl2.get());

    EXPECT_TRUE(layerImpl2->hasTileAt(0, 0));
    EXPECT_TRUE(layerImpl2->hasTileAt(0, 1));
    EXPECT_TRUE(layerImpl2->hasTileAt(0, 2));
    EXPECT_TRUE(layerImpl2->hasTextureIdForTileAt(0, 0));
    EXPECT_TRUE(layerImpl2->hasTextureIdForTileAt(0, 1));
    EXPECT_TRUE(layerImpl2->hasTextureIdForTileAt(0, 2));
}

TEST_F(TiledLayerChromiumTest, pushIdlePaintedOccludedTiles)
{
    OwnPtr<CCPrioritizedTextureManager> textureManager = CCPrioritizedTextureManager::create(4*1024*1024, 1024, CCRenderer::ContentPool);
    RefPtr<FakeTiledLayerChromium> layer = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
    DebugScopedSetImplThread implThread;
    OwnPtr<FakeCCTiledLayerImpl> layerImpl(adoptPtr(new FakeCCTiledLayerImpl(1)));
    TestCCOcclusionTracker occluded;

    // The tile size is 100x100, so this invalidates one occluded tile, culls it during paint, but prepaints it.
    occluded.setOcclusion(IntRect(0, 0, 100, 100));

    layer->setBounds(IntSize(100, 100));
    layer->setVisibleContentRect(IntRect(0, 0, 100, 100));
    layer->invalidateContentRect(IntRect(0, 0, 100, 100));

    layer->setTexturePriorities(m_priorityCalculator);
    textureManager->prioritizeTextures();
    layer->updateContentRect(m_updater, IntRect(0, 0, 100, 100), &occluded, m_stats);
    updateTextures();
    layer->pushPropertiesTo(layerImpl.get());

    // We should have the prepainted tile on the impl side.
    EXPECT_TRUE(layerImpl->hasTileAt(0, 0));
}

TEST_F(TiledLayerChromiumTest, pushTilesMarkedDirtyDuringPaint)
{
    OwnPtr<CCPrioritizedTextureManager> textureManager = CCPrioritizedTextureManager::create(4*1024*1024, 1024, CCRenderer::ContentPool);
    RefPtr<FakeTiledLayerChromium> layer = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
    DebugScopedSetImplThread implThread;
    OwnPtr<FakeCCTiledLayerImpl> layerImpl(adoptPtr(new FakeCCTiledLayerImpl(1)));

    // The tile size is 100x100, so this invalidates and then paints two tiles.
    // However, during the paint, we invalidate one of the tiles. This should
    // not prevent the tile from being pushed.
    layer->setBounds(IntSize(100, 200));
    layer->invalidateContentRect(IntRect(0, 0, 100, 200));
    layer->setVisibleContentRect(IntRect(0, 0, 100, 200));
    layer->fakeLayerTextureUpdater()->setRectToInvalidate(IntRect(0, 50, 100, 50), layer.get());

    layer->setTexturePriorities(m_priorityCalculator);
    textureManager->prioritizeTextures();
    layer->updateContentRect(m_updater, IntRect(0, 0, 100, 200), 0, m_stats);
    updateTextures();
    layer->pushPropertiesTo(layerImpl.get());

    // We should have both tiles on the impl side.
    EXPECT_TRUE(layerImpl->hasTileAt(0, 0));
    EXPECT_TRUE(layerImpl->hasTileAt(0, 1));
}

TEST_F(TiledLayerChromiumTest, pushTilesLayerMarkedDirtyDuringPaintOnNextLayer)
{
    OwnPtr<CCPrioritizedTextureManager> textureManager = CCPrioritizedTextureManager::create(4*1024*1024, 1024, CCRenderer::ContentPool);
    RefPtr<FakeTiledLayerChromium> layer1 = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
    RefPtr<FakeTiledLayerChromium> layer2 = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
    DebugScopedSetImplThread implThread;
    OwnPtr<FakeCCTiledLayerImpl> layer1Impl(adoptPtr(new FakeCCTiledLayerImpl(1)));
    OwnPtr<FakeCCTiledLayerImpl> layer2Impl(adoptPtr(new FakeCCTiledLayerImpl(2)));

    layer1->setBounds(IntSize(100, 200));
    layer1->setVisibleContentRect(IntRect(0, 0, 100, 200));
    layer1->invalidateContentRect(IntRect(0, 0, 100, 200));

    layer2->setBounds(IntSize(100, 200));
    layer2->setVisibleContentRect(IntRect(0, 0, 100, 200));
    layer2->invalidateContentRect(IntRect(0, 0, 100, 200));

    layer1->setTexturePriorities(m_priorityCalculator);
    layer2->setTexturePriorities(m_priorityCalculator);
    textureManager->prioritizeTextures();

    layer1->updateContentRect(m_updater, IntRect(0, 0, 100, 200), 0, m_stats);

    // Invalidate a tile on layer1
    layer2->fakeLayerTextureUpdater()->setRectToInvalidate(IntRect(0, 50, 100, 50), layer1.get());
    layer2->updateContentRect(m_updater, IntRect(0, 0, 100, 200), 0, m_stats);

    updateTextures();
    layer1->pushPropertiesTo(layer1Impl.get());
    layer2->pushPropertiesTo(layer2Impl.get());

    // We should have both tiles on the impl side for all layers.
    EXPECT_TRUE(layer1Impl->hasTileAt(0, 0));
    EXPECT_TRUE(layer1Impl->hasTileAt(0, 1));
    EXPECT_TRUE(layer2Impl->hasTileAt(0, 0));
    EXPECT_TRUE(layer2Impl->hasTileAt(0, 1));
}

TEST_F(TiledLayerChromiumTest, pushTilesLayerMarkedDirtyDuringPaintOnPreviousLayer)
{
    OwnPtr<CCPrioritizedTextureManager> textureManager = CCPrioritizedTextureManager::create(4*1024*1024, 1024, CCRenderer::ContentPool);
    RefPtr<FakeTiledLayerChromium> layer1 = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
    RefPtr<FakeTiledLayerChromium> layer2 = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
    DebugScopedSetImplThread implThread;
    OwnPtr<FakeCCTiledLayerImpl> layer1Impl(adoptPtr(new FakeCCTiledLayerImpl(1)));
    OwnPtr<FakeCCTiledLayerImpl> layer2Impl(adoptPtr(new FakeCCTiledLayerImpl(2)));

    layer1->setBounds(IntSize(100, 200));
    layer1->setVisibleContentRect(IntRect(0, 0, 100, 200));
    layer1->invalidateContentRect(IntRect(0, 0, 100, 200));

    layer2->setBounds(IntSize(100, 200));
    layer2->setVisibleContentRect(IntRect(0, 0, 100, 200));
    layer2->invalidateContentRect(IntRect(0, 0, 100, 200));

    layer1->setTexturePriorities(m_priorityCalculator);
    layer2->setTexturePriorities(m_priorityCalculator);
    textureManager->prioritizeTextures();

    // Invalidate a tile on layer2
    layer1->fakeLayerTextureUpdater()->setRectToInvalidate(IntRect(0, 50, 100, 50), layer2.get());
    layer1->updateContentRect(m_updater, IntRect(0, 0, 100, 200), 0, m_stats);
    layer2->updateContentRect(m_updater, IntRect(0, 0, 100, 200), 0, m_stats);
    updateTextures();
    layer1->pushPropertiesTo(layer1Impl.get());
    layer2->pushPropertiesTo(layer2Impl.get());

    // We should have both tiles on the impl side for all layers.
    EXPECT_TRUE(layer1Impl->hasTileAt(0, 0));
    EXPECT_TRUE(layer1Impl->hasTileAt(0, 1));
    EXPECT_TRUE(layer2Impl->hasTileAt(0, 0));
    EXPECT_TRUE(layer2Impl->hasTileAt(0, 1));
}

TEST_F(TiledLayerChromiumTest, idlePaintOutOfMemory)
{
    // The tile size is 100x100. Setup 3x3 tiles with one 1x1 visible tile in the center.
    IntSize contentBounds(300, 300);
    IntRect contentRect(IntPoint::zero(), contentBounds);
    IntRect visibleRect(100, 100, 100, 100);

    // We have enough memory for only the visible rect, so we will run out of memory in first idle paint.
    int memoryLimit = 4 * 100 * 100; // 1 tiles, 4 bytes per pixel.

    OwnPtr<CCPrioritizedTextureManager> textureManager = CCPrioritizedTextureManager::create(memoryLimit, 1024, CCRenderer::ContentPool);
    RefPtr<FakeTiledLayerChromium> layer = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
    DebugScopedSetImplThread implThread;
    OwnPtr<FakeCCTiledLayerImpl> layerImpl(adoptPtr(new FakeCCTiledLayerImpl(1)));

    // Invalidates 9 tiles and then paints one visible tile.
    layer->setBounds(contentBounds);
    layer->setVisibleContentRect(visibleRect);
    layer->invalidateContentRect(contentRect);

    layer->setTexturePriorities(m_priorityCalculator);
    textureManager->prioritizeTextures();
    layer->updateContentRect(m_updater, visibleRect, 0, m_stats);

    // Idle-painting should see no more priority tiles for painting.
    EXPECT_FALSE(layer->needsIdlePaint(visibleRect));

    updateTextures();
    layer->pushPropertiesTo(layerImpl.get());

    // We should have one tile on the impl side.
    EXPECT_TRUE(layerImpl->hasTileAt(1, 1));

    layer->setTexturePriorities(m_priorityCalculator);
    textureManager->prioritizeTextures();
    layer->updateContentRect(m_updater, visibleRect, 0, m_stats);

    // We shouldn't signal we need another idle paint.
    EXPECT_FALSE(layer->needsIdlePaint(visibleRect));
}

TEST_F(TiledLayerChromiumTest, idlePaintZeroSizedLayer)
{
    OwnPtr<CCPrioritizedTextureManager> textureManager = CCPrioritizedTextureManager::create(20000, 1024, CCRenderer::ContentPool);
    RefPtr<FakeTiledLayerChromium> layer = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
    DebugScopedSetImplThread implThread;
    OwnPtr<FakeCCTiledLayerImpl> layerImpl(adoptPtr(new FakeCCTiledLayerImpl(1)));

    bool animating[2] = {false, true};
    for (int i = 0; i < 2; i++) {
        // Pretend the layer is animating.
        layer->setDrawTransformIsAnimating(animating[i]);

        // The layer's bounds are empty.
        IntRect contentRect;

        layer->setBounds(contentRect.size());
        layer->setVisibleContentRect(contentRect);
        layer->invalidateContentRect(contentRect);

        layer->setTexturePriorities(m_priorityCalculator);
        textureManager->prioritizeTextures();

        // Empty layers don't paint or idle-paint.
        layer->updateContentRect(m_updater, contentRect, 0, m_stats);

        // Empty layers don't have tiles.
        EXPECT_EQ(0u, layer->numPaintedTiles());

        // Empty layers don't need prepaint.
        EXPECT_FALSE(layer->needsIdlePaint(contentRect));

        layer->pushPropertiesTo(layerImpl.get());

        // Empty layers don't have tiles.
        EXPECT_FALSE(layerImpl->hasTileAt(0, 0));
    }
}

TEST_F(TiledLayerChromiumTest, idlePaintNonVisibleLayers)
{
    IntSize contentBounds(100, 100);
    IntRect contentRect(IntPoint::zero(), contentBounds);

    OwnPtr<CCPrioritizedTextureManager> textureManager = CCPrioritizedTextureManager::create(20000, 1024, CCRenderer::ContentPool);
    RefPtr<FakeTiledLayerChromium> layer = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
    DebugScopedSetImplThread implThread;
    OwnPtr<FakeCCTiledLayerImpl> layerImpl(adoptPtr(new FakeCCTiledLayerImpl(1)));

    // Invalidate the layer but make none of it visible, so nothing paints.
    IntRect visibleRect;

    layer->setBounds(contentBounds);
    layer->setVisibleContentRect(visibleRect);
    layer->invalidateContentRect(contentRect);

    for (int i = 0; i < 2; i++) {
        // Paint / idle-paint.
        layer->setTexturePriorities(m_priorityCalculator);
        textureManager->prioritizeTextures();
        layer->updateContentRect(m_updater, visibleRect, 0, m_stats);

        // Non-visible layers don't need idle paint.
        EXPECT_FALSE(layer->needsIdlePaint(visibleRect));

        layer->pushPropertiesTo(layerImpl.get());

        // We should not have any tiles pushed since the layer is not visible.
        EXPECT_FALSE(layerImpl->hasTileAt(0, 0));
    }
}

static void testHaveOuterTiles(FakeCCTiledLayerImpl* layerImpl, int width, int height, int have)
{
    for (int i = 0; i < width; ++i) {
        for (int j = 0; j < height; ++j) {
            bool hasTile = i < have || j < have || i >= width - have || j >= height - have;
            EXPECT_EQ(hasTile, layerImpl->hasTileAt(i, j));
        }
    }
}

TEST_F(TiledLayerChromiumTest, idlePaintNonVisibleAnimatingLayers)
{
    OwnPtr<CCPrioritizedTextureManager> textureManager = CCPrioritizedTextureManager::create(8000*8000*8, 1024, CCRenderer::ContentPool);
    DebugScopedSetImplThread implThread;

    int tileWidth = FakeTiledLayerChromium::tileSize().width();
    int tileHeight = FakeTiledLayerChromium::tileSize().height();
    int width[] = { 1, 2, 3, 4, 9, 10, 0 };
    int height[] = { 1, 2, 3, 4, 9, 10, 0 };

    for (int j = 0; height[j]; ++j) {
        for (int i = 0; width[i]; ++i) {
            RefPtr<FakeTiledLayerChromium> layer = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
            OwnPtr<FakeCCTiledLayerImpl> layerImpl(adoptPtr(new FakeCCTiledLayerImpl(1)));

            // Pretend the layer is animating.
            layer->setDrawTransformIsAnimating(true);

            IntSize contentBounds(width[i] * tileWidth, height[j] * tileHeight);
            IntRect contentRect(IntPoint::zero(), contentBounds);
            IntRect visibleRect;

            layer->setBounds(contentBounds);
            layer->setVisibleContentRect(visibleRect);
            layer->invalidateContentRect(contentRect);

            layer->setTexturePriorities(m_priorityCalculator);
            textureManager->prioritizeTextures();

            // If idlePaintRect gives back a non-empty result then we should paint it. Otherwise,
            // we shoud paint nothing.
            bool shouldPrepaint = !layer->idlePaintRect(visibleRect).isEmpty();

            // Normally we don't allow non-visible layers to pre-paint, but if they are animating then we should.
            EXPECT_EQ(shouldPrepaint, layer->needsIdlePaint(visibleRect));

            // If the layer is to be prepainted at all, then after four updates we should have the outer row/columns painted.
            for (int k = 0; k < 4; ++k) {
                layer->setTexturePriorities(m_priorityCalculator);
                textureManager->prioritizeTextures();

                layer->updateContentRect(m_updater, visibleRect, 0, m_stats);
                updateTextures();
                layer->pushPropertiesTo(layerImpl.get());
            }

            testHaveOuterTiles(layerImpl.get(), width[i], height[j], shouldPrepaint ? 1 : 0);

            // We don't currently idle paint past the outermost tiles.
            EXPECT_FALSE(layer->needsIdlePaint(visibleRect));
            for (int k = 0; k < 4; ++k) {
                layer->setTexturePriorities(m_priorityCalculator);
                textureManager->prioritizeTextures();

                layer->updateContentRect(m_updater, visibleRect, 0, m_stats);
                updateTextures();
                layer->pushPropertiesTo(layerImpl.get());
            }

            testHaveOuterTiles(layerImpl.get(), width[i], height[j], shouldPrepaint ? 1 : 0);
        }
    }
}

TEST_F(TiledLayerChromiumTest, invalidateFromPrepare)
{
    OwnPtr<CCPrioritizedTextureManager> textureManager = CCPrioritizedTextureManager::create(4*1024*1024, 1024, CCRenderer::ContentPool);
    RefPtr<FakeTiledLayerChromium> layer = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
    DebugScopedSetImplThread implThread;
    OwnPtr<FakeCCTiledLayerImpl> layerImpl(adoptPtr(new FakeCCTiledLayerImpl(1)));

    // The tile size is 100x100, so this invalidates and then paints two tiles.
    layer->setBounds(IntSize(100, 200));
    layer->setVisibleContentRect(IntRect(0, 0, 100, 200));
    layer->invalidateContentRect(IntRect(0, 0, 100, 200));
    layer->setTexturePriorities(m_priorityCalculator);
    textureManager->prioritizeTextures();
    layer->updateContentRect(m_updater, IntRect(0, 0, 100, 200), 0, m_stats);
    updateTextures(1000);
    layer->pushPropertiesTo(layerImpl.get());

    // We should have both tiles on the impl side.
    EXPECT_TRUE(layerImpl->hasTileAt(0, 0));
    EXPECT_TRUE(layerImpl->hasTileAt(0, 1));

    layer->fakeLayerTextureUpdater()->clearPrepareCount();
    // Invoke updateContentRect again. As the layer is valid updateContentRect shouldn't be invoked on
    // the LayerTextureUpdater.
    layer->updateContentRect(m_updater, IntRect(0, 0, 100, 200), 0, m_stats);
    updateTextures(1000);
    EXPECT_EQ(0, layer->fakeLayerTextureUpdater()->prepareCount());

    layer->invalidateContentRect(IntRect(0, 0, 50, 50));
    // setRectToInvalidate triggers invalidateContentRect() being invoked from updateContentRect.
    layer->fakeLayerTextureUpdater()->setRectToInvalidate(IntRect(25, 25, 50, 50), layer.get());
    layer->fakeLayerTextureUpdater()->clearPrepareCount();
    layer->updateContentRect(m_updater, IntRect(0, 0, 100, 200), 0, m_stats);
    updateTextures(1000);
    EXPECT_EQ(1, layer->fakeLayerTextureUpdater()->prepareCount());
    layer->fakeLayerTextureUpdater()->clearPrepareCount();
    // The layer should still be invalid as updateContentRect invoked invalidate.
    layer->updateContentRect(m_updater, IntRect(0, 0, 100, 200), 0, m_stats);
    updateTextures(1000);
    EXPECT_EQ(1, layer->fakeLayerTextureUpdater()->prepareCount());
}

TEST_F(TiledLayerChromiumTest, verifyUpdateRectWhenContentBoundsAreScaled)
{
    // The updateRect (that indicates what was actually painted) should be in
    // layer space, not the content space.

    OwnPtr<CCPrioritizedTextureManager> textureManager = CCPrioritizedTextureManager::create(4*1024*1024, 1024, CCRenderer::ContentPool);
    RefPtr<FakeTiledLayerWithScaledBounds> layer = adoptRef(new FakeTiledLayerWithScaledBounds(textureManager.get()));
    DebugScopedSetImplThread implThread;

    IntRect layerBounds(0, 0, 300, 200);
    IntRect contentBounds(0, 0, 200, 250);

    layer->setBounds(layerBounds.size());
    layer->setContentBounds(contentBounds.size());
    layer->setVisibleContentRect(contentBounds);

    // On first update, the updateRect includes all tiles, even beyond the boundaries of the layer.
    // However, it should still be in layer space, not content space.
    layer->invalidateContentRect(contentBounds);

    layer->setTexturePriorities(m_priorityCalculator);
    textureManager->prioritizeTextures();
    layer->updateContentRect(m_updater, contentBounds, 0, m_stats);
    EXPECT_FLOAT_RECT_EQ(FloatRect(0, 0, 300, 300 * 0.8), layer->updateRect());
    updateTextures();

    // After the tiles are updated once, another invalidate only needs to update the bounds of the layer.
    layer->setTexturePriorities(m_priorityCalculator);
    textureManager->prioritizeTextures();
    layer->invalidateContentRect(contentBounds);
    layer->updateContentRect(m_updater, contentBounds, 0, m_stats);
    EXPECT_FLOAT_RECT_EQ(FloatRect(layerBounds), layer->updateRect());
    updateTextures();

    // Partial re-paint should also be represented by the updateRect in layer space, not content space.
    IntRect partialDamage(30, 100, 10, 10);
    layer->invalidateContentRect(partialDamage);
    layer->setTexturePriorities(m_priorityCalculator);
    textureManager->prioritizeTextures();
    layer->updateContentRect(m_updater, contentBounds, 0, m_stats);
    EXPECT_FLOAT_RECT_EQ(FloatRect(45, 80, 15, 8), layer->updateRect());
}

TEST_F(TiledLayerChromiumTest, verifyInvalidationWhenContentsScaleChanges)
{
    OwnPtr<CCPrioritizedTextureManager> textureManager = CCPrioritizedTextureManager::create(4*1024*1024, 1024, CCRenderer::ContentPool);
    RefPtr<FakeTiledLayerChromium> layer = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
    DebugScopedSetImplThread implThread;
    OwnPtr<FakeCCTiledLayerImpl> layerImpl(adoptPtr(new FakeCCTiledLayerImpl(1)));

    // Create a layer with one tile.
    layer->setBounds(IntSize(100, 100));
    layer->setVisibleContentRect(IntRect(0, 0, 100, 100));

    // Invalidate the entire layer.
    layer->setNeedsDisplay();
    EXPECT_FLOAT_RECT_EQ(FloatRect(0, 0, 100, 100), layer->lastNeedsDisplayRect());

    // Push the tiles to the impl side and check that there is exactly one.
    layer->setTexturePriorities(m_priorityCalculator);
    textureManager->prioritizeTextures();
    layer->updateContentRect(m_updater, IntRect(0, 0, 100, 100), 0, m_stats);
    updateTextures();
    layer->pushPropertiesTo(layerImpl.get());
    EXPECT_TRUE(layerImpl->hasTileAt(0, 0));
    EXPECT_FALSE(layerImpl->hasTileAt(0, 1));
    EXPECT_FALSE(layerImpl->hasTileAt(1, 0));
    EXPECT_FALSE(layerImpl->hasTileAt(1, 1));

    // Change the contents scale and verify that the content rectangle requiring painting
    // is not scaled.
    layer->setContentsScale(2);
    layer->setVisibleContentRect(IntRect(0, 0, 200, 200));
    EXPECT_FLOAT_RECT_EQ(FloatRect(0, 0, 100, 100), layer->lastNeedsDisplayRect());

    // The impl side should get 2x2 tiles now.
    layer->setTexturePriorities(m_priorityCalculator);
    textureManager->prioritizeTextures();
    layer->updateContentRect(m_updater, IntRect(0, 0, 200, 200), 0, m_stats);
    updateTextures();
    layer->pushPropertiesTo(layerImpl.get());
    EXPECT_TRUE(layerImpl->hasTileAt(0, 0));
    EXPECT_TRUE(layerImpl->hasTileAt(0, 1));
    EXPECT_TRUE(layerImpl->hasTileAt(1, 0));
    EXPECT_TRUE(layerImpl->hasTileAt(1, 1));

    // Invalidate the entire layer again, but do not paint. All tiles should be gone now from the
    // impl side.
    layer->setNeedsDisplay();
    layer->setTexturePriorities(m_priorityCalculator);
    layer->updateContentRect(m_updater, IntRect(1, 0, 0, 1), 0, m_stats);
    textureManager->prioritizeTextures();

    layer->pushPropertiesTo(layerImpl.get());
    EXPECT_FALSE(layerImpl->hasTileAt(0, 0));
    EXPECT_FALSE(layerImpl->hasTileAt(0, 1));
    EXPECT_FALSE(layerImpl->hasTileAt(1, 0));
    EXPECT_FALSE(layerImpl->hasTileAt(1, 1));
}

TEST_F(TiledLayerChromiumTest, skipsDrawGetsReset)
{
    // Initialize without threading support.
    WebKit::WebCompositor::initialize(0);
    FakeCCLayerTreeHostClient fakeCCLayerTreeHostClient;
    OwnPtr<CCLayerTreeHost> ccLayerTreeHost = CCLayerTreeHost::create(&fakeCCLayerTreeHostClient, CCLayerTreeSettings());
    ASSERT_TRUE(ccLayerTreeHost->initializeLayerRendererIfNeeded());

    // Create two 300 x 300 tiled layers.
    IntSize contentBounds(300, 300);
    IntRect contentRect(IntPoint::zero(), contentBounds);

    // We have enough memory for only one of the two layers.
    int memoryLimit = 4 * 300 * 300; // 4 bytes per pixel.

    RefPtr<FakeTiledLayerChromium> rootLayer = adoptRef(new FakeTiledLayerChromium(ccLayerTreeHost->contentsTextureManager()));
    RefPtr<FakeTiledLayerChromium> childLayer = adoptRef(new FakeTiledLayerChromium(ccLayerTreeHost->contentsTextureManager()));
    rootLayer->addChild(childLayer);

    rootLayer->setBounds(contentBounds);
    rootLayer->setVisibleContentRect(contentRect);
    rootLayer->setPosition(FloatPoint(0, 0));
    childLayer->setBounds(contentBounds);
    childLayer->setVisibleContentRect(contentRect);
    childLayer->setPosition(FloatPoint(0, 0));
    rootLayer->invalidateContentRect(contentRect);
    childLayer->invalidateContentRect(contentRect);

    ccLayerTreeHost->setRootLayer(rootLayer);
    ccLayerTreeHost->setViewportSize(IntSize(300, 300));

    ccLayerTreeHost->updateLayers(m_updater, memoryLimit);

    // We'll skip the root layer.
    EXPECT_TRUE(rootLayer->skipsDraw());
    EXPECT_FALSE(childLayer->skipsDraw());

    ccLayerTreeHost->commitComplete();

    // Remove the child layer.
    rootLayer->removeAllChildren();

    ccLayerTreeHost->updateLayers(m_updater, memoryLimit);
    EXPECT_FALSE(rootLayer->skipsDraw());

    ccLayerTreeHost->contentsTextureManager()->clearAllMemory(m_resourceProvider.get());
    ccLayerTreeHost->setRootLayer(0);
    ccLayerTreeHost.clear();
    WebKit::WebCompositor::shutdown();
}

TEST_F(TiledLayerChromiumTest, resizeToSmaller)
{
    OwnPtr<CCPrioritizedTextureManager> textureManager = CCPrioritizedTextureManager::create(60*1024*1024, 1024, CCRenderer::ContentPool);
    RefPtr<FakeTiledLayerChromium> layer = adoptRef(new FakeTiledLayerChromium(textureManager.get()));

    layer->setBounds(IntSize(700, 700));
    layer->setVisibleContentRect(IntRect(0, 0, 700, 700));
    layer->invalidateContentRect(IntRect(0, 0, 700, 700));

    layer->setTexturePriorities(m_priorityCalculator);
    textureManager->prioritizeTextures();
    layer->updateContentRect(m_updater, IntRect(0, 0, 700, 700), 0, m_stats);

    layer->setBounds(IntSize(200, 200));
    layer->invalidateContentRect(IntRect(0, 0, 200, 200));
}

TEST_F(TiledLayerChromiumTest, hugeLayerUpdateCrash)
{
    OwnPtr<CCPrioritizedTextureManager> textureManager = CCPrioritizedTextureManager::create(60*1024*1024, 1024, CCRenderer::ContentPool);
    RefPtr<FakeTiledLayerChromium> layer = adoptRef(new FakeTiledLayerChromium(textureManager.get()));

    int size = 1 << 30;
    layer->setBounds(IntSize(size, size));
    layer->setVisibleContentRect(IntRect(0, 0, 700, 700));
    layer->invalidateContentRect(IntRect(0, 0, size, size));

    // Ensure no crash for bounds where size * size would overflow an int.
    layer->setTexturePriorities(m_priorityCalculator);
    textureManager->prioritizeTextures();
    layer->updateContentRect(m_updater, IntRect(0, 0, 700, 700), 0, m_stats);
}

TEST_F(TiledLayerChromiumTest, partialUpdates)
{
    // Initialize without threading support.
    WebKit::WebCompositor::initialize(0);

    CCLayerTreeSettings settings;
    settings.maxPartialTextureUpdates = 4;

    FakeCCLayerTreeHostClient fakeCCLayerTreeHostClient;
    OwnPtr<CCLayerTreeHost> ccLayerTreeHost = CCLayerTreeHost::create(&fakeCCLayerTreeHostClient, settings);
    ASSERT_TRUE(ccLayerTreeHost->initializeLayerRendererIfNeeded());

    // Create one 300 x 200 tiled layer with 3 x 2 tiles.
    IntSize contentBounds(300, 200);
    IntRect contentRect(IntPoint::zero(), contentBounds);

    RefPtr<FakeTiledLayerChromium> layer = adoptRef(new FakeTiledLayerChromium(ccLayerTreeHost->contentsTextureManager()));
    layer->setBounds(contentBounds);
    layer->setPosition(FloatPoint(0, 0));
    layer->setVisibleContentRect(contentRect);
    layer->invalidateContentRect(contentRect);

    ccLayerTreeHost->setRootLayer(layer);
    ccLayerTreeHost->setViewportSize(IntSize(300, 200));

    // Full update of all 6 tiles.
    ccLayerTreeHost->updateLayers(m_updater, std::numeric_limits<size_t>::max());
    {
        DebugScopedSetImplThread implThread;
        OwnPtr<FakeCCTiledLayerImpl> layerImpl(adoptPtr(new FakeCCTiledLayerImpl(1)));
        updateTextures(4);
        EXPECT_EQ(4, layer->fakeLayerTextureUpdater()->updateCount());
        EXPECT_TRUE(m_updater.hasMoreUpdates());
        layer->fakeLayerTextureUpdater()->clearUpdateCount();
        updateTextures(4);
        EXPECT_EQ(2, layer->fakeLayerTextureUpdater()->updateCount());
        EXPECT_FALSE(m_updater.hasMoreUpdates());
        layer->fakeLayerTextureUpdater()->clearUpdateCount();
        layer->pushPropertiesTo(layerImpl.get());
    }
    ccLayerTreeHost->commitComplete();

    // Full update of 3 tiles and partial update of 3 tiles.
    layer->invalidateContentRect(IntRect(0, 0, 300, 150));
    ccLayerTreeHost->updateLayers(m_updater, std::numeric_limits<size_t>::max());
    {
        DebugScopedSetImplThread implThread;
        OwnPtr<FakeCCTiledLayerImpl> layerImpl(adoptPtr(new FakeCCTiledLayerImpl(1)));
        updateTextures(4);
        EXPECT_EQ(3, layer->fakeLayerTextureUpdater()->updateCount());
        EXPECT_TRUE(m_updater.hasMoreUpdates());
        layer->fakeLayerTextureUpdater()->clearUpdateCount();
        updateTextures(4);
        EXPECT_EQ(3, layer->fakeLayerTextureUpdater()->updateCount());
        EXPECT_FALSE(m_updater.hasMoreUpdates());
        layer->fakeLayerTextureUpdater()->clearUpdateCount();
        layer->pushPropertiesTo(layerImpl.get());
    }
    ccLayerTreeHost->commitComplete();

    // Partial update of 6 tiles.
    layer->invalidateContentRect(IntRect(50, 50, 200, 100));
    {
        DebugScopedSetImplThread implThread;
        OwnPtr<FakeCCTiledLayerImpl> layerImpl(adoptPtr(new FakeCCTiledLayerImpl(1)));
        ccLayerTreeHost->updateLayers(m_updater, std::numeric_limits<size_t>::max());
        updateTextures(4);
        EXPECT_EQ(2, layer->fakeLayerTextureUpdater()->updateCount());
        EXPECT_TRUE(m_updater.hasMoreUpdates());
        layer->fakeLayerTextureUpdater()->clearUpdateCount();
        updateTextures(4);
        EXPECT_EQ(4, layer->fakeLayerTextureUpdater()->updateCount());
        EXPECT_FALSE(m_updater.hasMoreUpdates());
        layer->fakeLayerTextureUpdater()->clearUpdateCount();
        layer->pushPropertiesTo(layerImpl.get());
    }
    ccLayerTreeHost->commitComplete();

    // Checkerboard all tiles.
    layer->invalidateContentRect(IntRect(0, 0, 300, 200));
    {
        DebugScopedSetImplThread implThread;
        OwnPtr<FakeCCTiledLayerImpl> layerImpl(adoptPtr(new FakeCCTiledLayerImpl(1)));
        layer->pushPropertiesTo(layerImpl.get());
    }
    ccLayerTreeHost->commitComplete();

    // Partial update of 6 checkerboard tiles.
    layer->invalidateContentRect(IntRect(50, 50, 200, 100));
    {
        DebugScopedSetImplThread implThread;
        OwnPtr<FakeCCTiledLayerImpl> layerImpl(adoptPtr(new FakeCCTiledLayerImpl(1)));
        ccLayerTreeHost->updateLayers(m_updater, std::numeric_limits<size_t>::max());
        updateTextures(4);
        EXPECT_EQ(4, layer->fakeLayerTextureUpdater()->updateCount());
        EXPECT_TRUE(m_updater.hasMoreUpdates());
        layer->fakeLayerTextureUpdater()->clearUpdateCount();
        updateTextures(4);
        EXPECT_EQ(2, layer->fakeLayerTextureUpdater()->updateCount());
        EXPECT_FALSE(m_updater.hasMoreUpdates());
        layer->fakeLayerTextureUpdater()->clearUpdateCount();
        layer->pushPropertiesTo(layerImpl.get());
    }
    ccLayerTreeHost->commitComplete();

    // Partial update of 4 tiles.
    layer->invalidateContentRect(IntRect(50, 50, 100, 100));
    {
        DebugScopedSetImplThread implThread;
        OwnPtr<FakeCCTiledLayerImpl> layerImpl(adoptPtr(new FakeCCTiledLayerImpl(1)));
        ccLayerTreeHost->updateLayers(m_updater, std::numeric_limits<size_t>::max());
        updateTextures(4);
        EXPECT_EQ(4, layer->fakeLayerTextureUpdater()->updateCount());
        EXPECT_FALSE(m_updater.hasMoreUpdates());
        layer->fakeLayerTextureUpdater()->clearUpdateCount();
        layer->pushPropertiesTo(layerImpl.get());
    }
    ccLayerTreeHost->commitComplete();

    {
        DebugScopedSetImplThread implThread;
        ccLayerTreeHost->contentsTextureManager()->clearAllMemory(m_resourceProvider.get());
    }
    ccLayerTreeHost->setRootLayer(0);
    ccLayerTreeHost.clear();
    WebKit::WebCompositor::shutdown();
}

TEST_F(TiledLayerChromiumTest, tilesPaintedWithoutOcclusion)
{
    OwnPtr<CCPrioritizedTextureManager> textureManager = CCPrioritizedTextureManager::create(4*1024*1024, 1024, CCRenderer::ContentPool);
    RefPtr<FakeTiledLayerChromium> layer = adoptRef(new FakeTiledLayerChromium(textureManager.get()));

    // The tile size is 100x100, so this invalidates and then paints two tiles.
    layer->setBounds(IntSize(100, 200));
    layer->setVisibleContentRect(IntRect(0, 0, 100, 200));
    layer->invalidateContentRect(IntRect(0, 0, 100, 200));

    layer->setTexturePriorities(m_priorityCalculator);
    textureManager->prioritizeTextures();
    layer->updateContentRect(m_updater, IntRect(0, 0, 100, 200), 0, m_stats);
    EXPECT_EQ(2, layer->fakeLayerTextureUpdater()->prepareRectCount());
}

TEST_F(TiledLayerChromiumTest, tilesPaintedWithOcclusion)
{
    OwnPtr<CCPrioritizedTextureManager> textureManager = CCPrioritizedTextureManager::create(4*1024*1024, 1024, CCRenderer::ContentPool);
    RefPtr<FakeTiledLayerChromium> layer = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
    TestCCOcclusionTracker occluded;

    // The tile size is 100x100.

    layer->setBounds(IntSize(600, 600));

    occluded.setOcclusion(IntRect(200, 200, 300, 100));
    layer->setVisibleContentRect(IntRect(IntPoint(), layer->contentBounds()));
    layer->invalidateContentRect(IntRect(0, 0, 600, 600));

    layer->setTexturePriorities(m_priorityCalculator);
    textureManager->prioritizeTextures();
    layer->updateContentRect(m_updater, IntRect(0, 0, 600, 600), &occluded, m_stats);
    EXPECT_EQ(36-3, layer->fakeLayerTextureUpdater()->prepareRectCount());

    EXPECT_NEAR(occluded.overdrawMetrics().pixelsUploadedOpaque(), 0, 1);
    EXPECT_NEAR(occluded.overdrawMetrics().pixelsUploadedTranslucent(), 330000, 1);
    EXPECT_EQ(3, occluded.overdrawMetrics().tilesCulledForUpload());

    layer->fakeLayerTextureUpdater()->clearPrepareRectCount();

    occluded.setOcclusion(IntRect(250, 200, 300, 100));
    layer->invalidateContentRect(IntRect(0, 0, 600, 600));
    layer->updateContentRect(m_updater, IntRect(0, 0, 600, 600), &occluded, m_stats);
    EXPECT_EQ(36-2, layer->fakeLayerTextureUpdater()->prepareRectCount());

    EXPECT_NEAR(occluded.overdrawMetrics().pixelsUploadedOpaque(), 0, 1);
    EXPECT_NEAR(occluded.overdrawMetrics().pixelsUploadedTranslucent(), 330000 + 340000, 1);
    EXPECT_EQ(3 + 2, occluded.overdrawMetrics().tilesCulledForUpload());

    layer->fakeLayerTextureUpdater()->clearPrepareRectCount();

    occluded.setOcclusion(IntRect(250, 250, 300, 100));
    layer->invalidateContentRect(IntRect(0, 0, 600, 600));
    layer->updateContentRect(m_updater, IntRect(0, 0, 600, 600), &occluded, m_stats);
    EXPECT_EQ(36, layer->fakeLayerTextureUpdater()->prepareRectCount());

    EXPECT_NEAR(occluded.overdrawMetrics().pixelsUploadedOpaque(), 0, 1);
    EXPECT_NEAR(occluded.overdrawMetrics().pixelsUploadedTranslucent(), 330000 + 340000 + 360000, 1);
    EXPECT_EQ(3 + 2, occluded.overdrawMetrics().tilesCulledForUpload());
}

TEST_F(TiledLayerChromiumTest, tilesPaintedWithOcclusionAndVisiblityConstraints)
{
    OwnPtr<CCPrioritizedTextureManager> textureManager = CCPrioritizedTextureManager::create(4*1024*1024, 1024, CCRenderer::ContentPool);
    RefPtr<FakeTiledLayerChromium> layer = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
    TestCCOcclusionTracker occluded;

    // The tile size is 100x100.

    layer->setBounds(IntSize(600, 600));

    // The partially occluded tiles (by the 150 occlusion height) are visible beyond the occlusion, so not culled.
    occluded.setOcclusion(IntRect(200, 200, 300, 150));
    layer->setVisibleContentRect(IntRect(0, 0, 600, 360));
    layer->invalidateContentRect(IntRect(0, 0, 600, 600));

    layer->setTexturePriorities(m_priorityCalculator);
    textureManager->prioritizeTextures();
    layer->updateContentRect(m_updater, IntRect(0, 0, 600, 360), &occluded, m_stats);
    EXPECT_EQ(24-3, layer->fakeLayerTextureUpdater()->prepareRectCount());

    EXPECT_NEAR(occluded.overdrawMetrics().pixelsUploadedOpaque(), 0, 1);
    EXPECT_NEAR(occluded.overdrawMetrics().pixelsUploadedTranslucent(), 210000, 1);
    EXPECT_EQ(3, occluded.overdrawMetrics().tilesCulledForUpload());

    layer->fakeLayerTextureUpdater()->clearPrepareRectCount();

    // Now the visible region stops at the edge of the occlusion so the partly visible tiles become fully occluded.
    occluded.setOcclusion(IntRect(200, 200, 300, 150));
    layer->setVisibleContentRect(IntRect(0, 0, 600, 350));
    layer->invalidateContentRect(IntRect(0, 0, 600, 600));
    layer->setTexturePriorities(m_priorityCalculator);
    textureManager->prioritizeTextures();
    layer->updateContentRect(m_updater, IntRect(0, 0, 600, 350), &occluded, m_stats);
    EXPECT_EQ(24-6, layer->fakeLayerTextureUpdater()->prepareRectCount());

    EXPECT_NEAR(occluded.overdrawMetrics().pixelsUploadedOpaque(), 0, 1);
    EXPECT_NEAR(occluded.overdrawMetrics().pixelsUploadedTranslucent(), 210000 + 180000, 1);
    EXPECT_EQ(3 + 6, occluded.overdrawMetrics().tilesCulledForUpload());

    layer->fakeLayerTextureUpdater()->clearPrepareRectCount();

    // Now the visible region is even smaller than the occlusion, it should have the same result.
    occluded.setOcclusion(IntRect(200, 200, 300, 150));
    layer->setVisibleContentRect(IntRect(0, 0, 600, 340));
    layer->invalidateContentRect(IntRect(0, 0, 600, 600));
    layer->setTexturePriorities(m_priorityCalculator);
    textureManager->prioritizeTextures();
    layer->updateContentRect(m_updater, IntRect(0, 0, 600, 340), &occluded, m_stats);
    EXPECT_EQ(24-6, layer->fakeLayerTextureUpdater()->prepareRectCount());

    EXPECT_NEAR(occluded.overdrawMetrics().pixelsUploadedOpaque(), 0, 1);
    EXPECT_NEAR(occluded.overdrawMetrics().pixelsUploadedTranslucent(), 210000 + 180000 + 180000, 1);
    EXPECT_EQ(3 + 6 + 6, occluded.overdrawMetrics().tilesCulledForUpload());

}

TEST_F(TiledLayerChromiumTest, tilesNotPaintedWithoutInvalidation)
{
    OwnPtr<CCPrioritizedTextureManager> textureManager = CCPrioritizedTextureManager::create(4*1024*1024, 1024, CCRenderer::ContentPool);
    RefPtr<FakeTiledLayerChromium> layer = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
    TestCCOcclusionTracker occluded;

    // The tile size is 100x100.

    layer->setBounds(IntSize(600, 600));

    occluded.setOcclusion(IntRect(200, 200, 300, 100));
    layer->setVisibleContentRect(IntRect(0, 0, 600, 600));
    layer->invalidateContentRect(IntRect(0, 0, 600, 600));
    layer->setTexturePriorities(m_priorityCalculator);
    textureManager->prioritizeTextures();
    layer->updateContentRect(m_updater, IntRect(0, 0, 600, 600), &occluded, m_stats);
    EXPECT_EQ(36-3, layer->fakeLayerTextureUpdater()->prepareRectCount());
    {
        DebugScopedSetImplThread implThread;
        updateTextures();
    }

    EXPECT_NEAR(occluded.overdrawMetrics().pixelsUploadedOpaque(), 0, 1);
    EXPECT_NEAR(occluded.overdrawMetrics().pixelsUploadedTranslucent(), 330000, 1);
    EXPECT_EQ(3, occluded.overdrawMetrics().tilesCulledForUpload());

    layer->fakeLayerTextureUpdater()->clearPrepareRectCount();

    // Repaint without marking it dirty. The 3 culled tiles will be pre-painted now.
    layer->updateContentRect(m_updater, IntRect(0, 0, 600, 600), &occluded, m_stats);
    EXPECT_EQ(3, layer->fakeLayerTextureUpdater()->prepareRectCount());

    EXPECT_NEAR(occluded.overdrawMetrics().pixelsUploadedOpaque(), 0, 1);
    EXPECT_NEAR(occluded.overdrawMetrics().pixelsUploadedTranslucent(), 330000, 1);
    EXPECT_EQ(6, occluded.overdrawMetrics().tilesCulledForUpload());
}

TEST_F(TiledLayerChromiumTest, tilesPaintedWithOcclusionAndTransforms)
{
    OwnPtr<CCPrioritizedTextureManager> textureManager = CCPrioritizedTextureManager::create(4*1024*1024, 1024, CCRenderer::ContentPool);
    RefPtr<FakeTiledLayerChromium> layer = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
    TestCCOcclusionTracker occluded;

    // The tile size is 100x100.

    // This makes sure the painting works when the occluded region (in screen space)
    // is transformed differently than the layer.
    layer->setBounds(IntSize(600, 600));
    WebTransformationMatrix screenTransform;
    screenTransform.scale(0.5);
    layer->setScreenSpaceTransform(screenTransform);
    layer->setDrawTransform(screenTransform);

    occluded.setOcclusion(IntRect(100, 100, 150, 50));
    layer->setVisibleContentRect(IntRect(IntPoint(), layer->contentBounds()));
    layer->invalidateContentRect(IntRect(0, 0, 600, 600));
    layer->setTexturePriorities(m_priorityCalculator);
    textureManager->prioritizeTextures();
    layer->updateContentRect(m_updater, IntRect(0, 0, 600, 600), &occluded, m_stats);
    EXPECT_EQ(36-3, layer->fakeLayerTextureUpdater()->prepareRectCount());

    EXPECT_NEAR(occluded.overdrawMetrics().pixelsUploadedOpaque(), 0, 1);
    EXPECT_NEAR(occluded.overdrawMetrics().pixelsUploadedTranslucent(), 330000, 1);
    EXPECT_EQ(3, occluded.overdrawMetrics().tilesCulledForUpload());
}

TEST_F(TiledLayerChromiumTest, tilesPaintedWithOcclusionAndScaling)
{
    OwnPtr<CCPrioritizedTextureManager> textureManager = CCPrioritizedTextureManager::create(4*1024*1024, 1024, CCRenderer::ContentPool);
    RefPtr<FakeTiledLayerChromium> layer = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
    TestCCOcclusionTracker occluded;

    // The tile size is 100x100.

    // This makes sure the painting works when the content space is scaled to
    // a different layer space. In this case tiles are scaled to be 200x200
    // pixels, which means none should be occluded.
    layer->setContentsScale(0.5);
    layer->setBounds(IntSize(600, 600));
    WebTransformationMatrix drawTransform;
    drawTransform.scale(1 / layer->contentsScale());
    layer->setDrawTransform(drawTransform);
    layer->setScreenSpaceTransform(drawTransform);

    occluded.setOcclusion(IntRect(200, 200, 300, 100));
    layer->setVisibleContentRect(IntRect(IntPoint(), layer->contentBounds()));
    layer->invalidateContentRect(IntRect(0, 0, 600, 600));
    layer->setTexturePriorities(m_priorityCalculator);
    textureManager->prioritizeTextures();
    layer->updateContentRect(m_updater, IntRect(0, 0, 600, 600), &occluded, m_stats);
    // The content is half the size of the layer (so the number of tiles is fewer).
    // In this case, the content is 300x300, and since the tile size is 100, the
    // number of tiles 3x3.
    EXPECT_EQ(9, layer->fakeLayerTextureUpdater()->prepareRectCount());

    EXPECT_NEAR(occluded.overdrawMetrics().pixelsUploadedOpaque(), 0, 1);
    EXPECT_NEAR(occluded.overdrawMetrics().pixelsUploadedTranslucent(), 90000, 1);
    EXPECT_EQ(0, occluded.overdrawMetrics().tilesCulledForUpload());

    layer->fakeLayerTextureUpdater()->clearPrepareRectCount();

    // This makes sure the painting works when the content space is scaled to
    // a different layer space. In this case the occluded region catches the
    // blown up tiles.
    occluded.setOcclusion(IntRect(200, 200, 300, 200));
    layer->setVisibleContentRect(IntRect(IntPoint(), layer->contentBounds()));
    layer->invalidateContentRect(IntRect(0, 0, 600, 600));
    layer->setTexturePriorities(m_priorityCalculator);
    textureManager->prioritizeTextures();
    layer->updateContentRect(m_updater, IntRect(0, 0, 600, 600), &occluded, m_stats);
    EXPECT_EQ(9-1, layer->fakeLayerTextureUpdater()->prepareRectCount());

    EXPECT_NEAR(occluded.overdrawMetrics().pixelsUploadedOpaque(), 0, 1);
    EXPECT_NEAR(occluded.overdrawMetrics().pixelsUploadedTranslucent(), 90000 + 80000, 1);
    EXPECT_EQ(1, occluded.overdrawMetrics().tilesCulledForUpload());

    layer->fakeLayerTextureUpdater()->clearPrepareRectCount();

    // This makes sure content scaling and transforms work together.
    WebTransformationMatrix screenTransform;
    screenTransform.scale(0.5);
    layer->setScreenSpaceTransform(screenTransform);
    layer->setDrawTransform(screenTransform);

    occluded.setOcclusion(IntRect(100, 100, 150, 100));
    layer->setVisibleContentRect(IntRect(IntPoint(), layer->contentBounds()));
    layer->invalidateContentRect(IntRect(0, 0, 600, 600));
    layer->setTexturePriorities(m_priorityCalculator);
    textureManager->prioritizeTextures();
    layer->updateContentRect(m_updater, IntRect(0, 0, 600, 600), &occluded, m_stats);
    EXPECT_EQ(9-1, layer->fakeLayerTextureUpdater()->prepareRectCount());

    EXPECT_NEAR(occluded.overdrawMetrics().pixelsUploadedOpaque(), 0, 1);
    EXPECT_NEAR(occluded.overdrawMetrics().pixelsUploadedTranslucent(), 90000 + 80000 + 80000, 1);
    EXPECT_EQ(1 + 1, occluded.overdrawMetrics().tilesCulledForUpload());
}

TEST_F(TiledLayerChromiumTest, visibleContentOpaqueRegion)
{
    OwnPtr<CCPrioritizedTextureManager> textureManager = CCPrioritizedTextureManager::create(4*1024*1024, 1024, CCRenderer::ContentPool);
    RefPtr<FakeTiledLayerChromium> layer = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
    TestCCOcclusionTracker occluded;
    DebugScopedSetImplThread implThread;

    // The tile size is 100x100, so this invalidates and then paints two tiles in various ways.

    IntRect opaquePaintRect;
    Region opaqueContents;

    IntRect contentBounds = IntRect(0, 0, 100, 200);
    IntRect visibleBounds = IntRect(0, 0, 100, 150);

    layer->setBounds(contentBounds.size());
    layer->setVisibleContentRect(visibleBounds);
    layer->setDrawOpacity(1);

    layer->setTexturePriorities(m_priorityCalculator);
    textureManager->prioritizeTextures();

    // If the layer doesn't paint opaque content, then the visibleContentOpaqueRegion should be empty.
    layer->fakeLayerTextureUpdater()->setOpaquePaintRect(IntRect());
    layer->invalidateContentRect(contentBounds);
    layer->updateContentRect(m_updater, contentBounds, &occluded, m_stats);
    opaqueContents = layer->visibleContentOpaqueRegion();
    EXPECT_TRUE(opaqueContents.isEmpty());

    EXPECT_NEAR(occluded.overdrawMetrics().pixelsPainted(), 20000, 1);
    EXPECT_NEAR(occluded.overdrawMetrics().pixelsUploadedOpaque(), 0, 1);
    EXPECT_NEAR(occluded.overdrawMetrics().pixelsUploadedTranslucent(), 20000, 1);
    EXPECT_EQ(0, occluded.overdrawMetrics().tilesCulledForUpload());

    // visibleContentOpaqueRegion should match the visible part of what is painted opaque.
    opaquePaintRect = IntRect(10, 10, 90, 190);
    layer->fakeLayerTextureUpdater()->setOpaquePaintRect(opaquePaintRect);
    layer->invalidateContentRect(contentBounds);
    layer->updateContentRect(m_updater, contentBounds, &occluded, m_stats);
    updateTextures();
    opaqueContents = layer->visibleContentOpaqueRegion();
    EXPECT_EQ_RECT(intersection(opaquePaintRect, visibleBounds), opaqueContents.bounds());
    EXPECT_EQ(1u, opaqueContents.rects().size());

    EXPECT_NEAR(occluded.overdrawMetrics().pixelsPainted(), 20000 * 2, 1);
    EXPECT_NEAR(occluded.overdrawMetrics().pixelsUploadedOpaque(), 17100, 1);
    EXPECT_NEAR(occluded.overdrawMetrics().pixelsUploadedTranslucent(), 20000 + 20000 - 17100, 1);
    EXPECT_EQ(0, occluded.overdrawMetrics().tilesCulledForUpload());

    // If we paint again without invalidating, the same stuff should be opaque.
    layer->fakeLayerTextureUpdater()->setOpaquePaintRect(IntRect());
    layer->updateContentRect(m_updater, contentBounds, &occluded, m_stats);
    updateTextures();
    opaqueContents = layer->visibleContentOpaqueRegion();
    EXPECT_EQ_RECT(intersection(opaquePaintRect, visibleBounds), opaqueContents.bounds());
    EXPECT_EQ(1u, opaqueContents.rects().size());

    EXPECT_NEAR(occluded.overdrawMetrics().pixelsPainted(), 20000 * 2, 1);
    EXPECT_NEAR(occluded.overdrawMetrics().pixelsUploadedOpaque(), 17100, 1);
    EXPECT_NEAR(occluded.overdrawMetrics().pixelsUploadedTranslucent(), 20000 + 20000 - 17100, 1);
    EXPECT_EQ(0, occluded.overdrawMetrics().tilesCulledForUpload());

    // If we repaint a non-opaque part of the tile, then it shouldn't lose its opaque-ness. And other tiles should
    // not be affected.
    layer->fakeLayerTextureUpdater()->setOpaquePaintRect(IntRect());
    layer->invalidateContentRect(IntRect(0, 0, 1, 1));
    layer->updateContentRect(m_updater, contentBounds, &occluded, m_stats);
    updateTextures();
    opaqueContents = layer->visibleContentOpaqueRegion();
    EXPECT_EQ_RECT(intersection(opaquePaintRect, visibleBounds), opaqueContents.bounds());
    EXPECT_EQ(1u, opaqueContents.rects().size());

    EXPECT_NEAR(occluded.overdrawMetrics().pixelsPainted(), 20000 * 2 + 1, 1);
    EXPECT_NEAR(occluded.overdrawMetrics().pixelsUploadedOpaque(), 17100, 1);
    EXPECT_NEAR(occluded.overdrawMetrics().pixelsUploadedTranslucent(), 20000 + 20000 - 17100 + 1, 1);
    EXPECT_EQ(0, occluded.overdrawMetrics().tilesCulledForUpload());

    // If we repaint an opaque part of the tile, then it should lose its opaque-ness. But other tiles should still
    // not be affected.
    layer->fakeLayerTextureUpdater()->setOpaquePaintRect(IntRect());
    layer->invalidateContentRect(IntRect(10, 10, 1, 1));
    layer->updateContentRect(m_updater, contentBounds, &occluded, m_stats);
    updateTextures();
    opaqueContents = layer->visibleContentOpaqueRegion();
    EXPECT_EQ_RECT(intersection(IntRect(10, 100, 90, 100), visibleBounds), opaqueContents.bounds());
    EXPECT_EQ(1u, opaqueContents.rects().size());

    EXPECT_NEAR(occluded.overdrawMetrics().pixelsPainted(), 20000 * 2 + 1  + 1, 1);
    EXPECT_NEAR(occluded.overdrawMetrics().pixelsUploadedOpaque(), 17100, 1);
    EXPECT_NEAR(occluded.overdrawMetrics().pixelsUploadedTranslucent(), 20000 + 20000 - 17100 + 1 + 1, 1);
    EXPECT_EQ(0, occluded.overdrawMetrics().tilesCulledForUpload());
}

TEST_F(TiledLayerChromiumTest, pixelsPaintedMetrics)
{
    OwnPtr<CCPrioritizedTextureManager> textureManager = CCPrioritizedTextureManager::create(4*1024*1024, 1024, CCRenderer::ContentPool);
    RefPtr<FakeTiledLayerChromium> layer = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
    TestCCOcclusionTracker occluded;
    DebugScopedSetImplThread implThread;

    // The tile size is 100x100, so this invalidates and then paints two tiles in various ways.

    IntRect opaquePaintRect;
    Region opaqueContents;

    IntRect contentBounds = IntRect(0, 0, 100, 300);
    IntRect visibleBounds = IntRect(0, 0, 100, 300);

    layer->setBounds(contentBounds.size());
    layer->setVisibleContentRect(visibleBounds);
    layer->setDrawOpacity(1);

    layer->setTexturePriorities(m_priorityCalculator);
    textureManager->prioritizeTextures();

    // Invalidates and paints the whole layer.
    layer->fakeLayerTextureUpdater()->setOpaquePaintRect(IntRect());
    layer->invalidateContentRect(contentBounds);
    layer->updateContentRect(m_updater, contentBounds, &occluded, m_stats);
    updateTextures();
    opaqueContents = layer->visibleContentOpaqueRegion();
    EXPECT_TRUE(opaqueContents.isEmpty());

    EXPECT_NEAR(occluded.overdrawMetrics().pixelsPainted(), 30000, 1);
    EXPECT_NEAR(occluded.overdrawMetrics().pixelsUploadedOpaque(), 0, 1);
    EXPECT_NEAR(occluded.overdrawMetrics().pixelsUploadedTranslucent(), 30000, 1);
    EXPECT_EQ(0, occluded.overdrawMetrics().tilesCulledForUpload());

    // Invalidates an area on the top and bottom tile, which will cause us to paint the tile in the middle,
    // even though it is not dirty and will not be uploaded.
    layer->fakeLayerTextureUpdater()->setOpaquePaintRect(IntRect());
    layer->invalidateContentRect(IntRect(0, 0, 1, 1));
    layer->invalidateContentRect(IntRect(50, 200, 10, 10));
    layer->updateContentRect(m_updater, contentBounds, &occluded, m_stats);
    updateTextures();
    opaqueContents = layer->visibleContentOpaqueRegion();
    EXPECT_TRUE(opaqueContents.isEmpty());

    // The middle tile was painted even though not invalidated.
    EXPECT_NEAR(occluded.overdrawMetrics().pixelsPainted(), 30000 + 60 * 210, 1);
    // The pixels uploaded will not include the non-invalidated tile in the middle.
    EXPECT_NEAR(occluded.overdrawMetrics().pixelsUploadedOpaque(), 0, 1);
    EXPECT_NEAR(occluded.overdrawMetrics().pixelsUploadedTranslucent(), 30000 + 1 + 100, 1);
    EXPECT_EQ(0, occluded.overdrawMetrics().tilesCulledForUpload());
}

TEST_F(TiledLayerChromiumTest, dontAllocateContentsWhenTargetSurfaceCantBeAllocated)
{
    // Initialize without threading support.
    WebKit::WebCompositor::initialize(0);

    // Tile size is 100x100.
    IntRect rootRect(0, 0, 300, 200);
    IntRect childRect(0, 0, 300, 100);
    IntRect child2Rect(0, 100, 300, 100);

    CCLayerTreeSettings settings;
    FakeCCLayerTreeHostClient fakeCCLayerTreeHostClient;
    OwnPtr<CCLayerTreeHost> ccLayerTreeHost = CCLayerTreeHost::create(&fakeCCLayerTreeHostClient, settings);
    ASSERT_TRUE(ccLayerTreeHost->initializeLayerRendererIfNeeded());

    RefPtr<FakeTiledLayerChromium> root = adoptRef(new FakeTiledLayerChromium(ccLayerTreeHost->contentsTextureManager()));
    RefPtr<LayerChromium> surface = LayerChromium::create();
    RefPtr<FakeTiledLayerChromium> child = adoptRef(new FakeTiledLayerChromium(ccLayerTreeHost->contentsTextureManager()));
    RefPtr<FakeTiledLayerChromium> child2 = adoptRef(new FakeTiledLayerChromium(ccLayerTreeHost->contentsTextureManager()));

    root->setBounds(rootRect.size());
    root->setAnchorPoint(FloatPoint());
    root->setVisibleContentRect(rootRect);
    root->addChild(surface);

    surface->setForceRenderSurface(true);
    surface->setAnchorPoint(FloatPoint());
    surface->setOpacity(0.5);
    surface->addChild(child);
    surface->addChild(child2);

    child->setBounds(childRect.size());
    child->setAnchorPoint(FloatPoint());
    child->setPosition(childRect.location());
    child->setVisibleContentRect(childRect);

    child2->setBounds(child2Rect.size());
    child2->setAnchorPoint(FloatPoint());
    child2->setPosition(child2Rect.location());
    child2->setVisibleContentRect(child2Rect);

    ccLayerTreeHost->setRootLayer(root);
    ccLayerTreeHost->setViewportSize(rootRect.size());

    // With a huge memory limit, all layers should update and push their textures.
    root->invalidateContentRect(rootRect);
    child->invalidateContentRect(childRect);
    child2->invalidateContentRect(child2Rect);
    ccLayerTreeHost->updateLayers(m_updater, std::numeric_limits<size_t>::max());
    {
        DebugScopedSetImplThread implThread;
        updateTextures(1000);
        EXPECT_EQ(6, root->fakeLayerTextureUpdater()->updateCount());
        EXPECT_EQ(3, child->fakeLayerTextureUpdater()->updateCount());
        EXPECT_EQ(3, child2->fakeLayerTextureUpdater()->updateCount());
        EXPECT_FALSE(m_updater.hasMoreUpdates());

        root->fakeLayerTextureUpdater()->clearUpdateCount();
        child->fakeLayerTextureUpdater()->clearUpdateCount();
        child2->fakeLayerTextureUpdater()->clearUpdateCount();

        OwnPtr<FakeCCTiledLayerImpl> rootImpl(adoptPtr(new FakeCCTiledLayerImpl(root->id())));
        OwnPtr<FakeCCTiledLayerImpl> childImpl(adoptPtr(new FakeCCTiledLayerImpl(child->id())));
        OwnPtr<FakeCCTiledLayerImpl> child2Impl(adoptPtr(new FakeCCTiledLayerImpl(child2->id())));
        root->pushPropertiesTo(rootImpl.get());
        child->pushPropertiesTo(childImpl.get());
        child2->pushPropertiesTo(child2Impl.get());

        for (unsigned i = 0; i < 3; ++i) {
            for (unsigned j = 0; j < 2; ++j)
                EXPECT_TRUE(rootImpl->hasTextureIdForTileAt(i, j));
            EXPECT_TRUE(childImpl->hasTextureIdForTileAt(i, 0));
            EXPECT_TRUE(child2Impl->hasTextureIdForTileAt(i, 0));
        }
    }
    ccLayerTreeHost->commitComplete();

    // With a memory limit that includes only the root layer (3x2 tiles) and half the surface that
    // the child layers draw into, the child layers will not be allocated. If the surface isn't
    // accounted for, then one of the children would fit within the memory limit.
    root->invalidateContentRect(rootRect);
    child->invalidateContentRect(childRect);
    child2->invalidateContentRect(child2Rect);
    ccLayerTreeHost->updateLayers(m_updater, (3 * 2 + 3 * 1) * (100 * 100) * 4);
    {
        DebugScopedSetImplThread implThread;
        updateTextures(1000);
        EXPECT_EQ(6, root->fakeLayerTextureUpdater()->updateCount());
        EXPECT_EQ(0, child->fakeLayerTextureUpdater()->updateCount());
        EXPECT_EQ(0, child2->fakeLayerTextureUpdater()->updateCount());
        EXPECT_FALSE(m_updater.hasMoreUpdates());

        root->fakeLayerTextureUpdater()->clearUpdateCount();
        child->fakeLayerTextureUpdater()->clearUpdateCount();
        child2->fakeLayerTextureUpdater()->clearUpdateCount();

        OwnPtr<FakeCCTiledLayerImpl> rootImpl(adoptPtr(new FakeCCTiledLayerImpl(root->id())));
        OwnPtr<FakeCCTiledLayerImpl> childImpl(adoptPtr(new FakeCCTiledLayerImpl(child->id())));
        OwnPtr<FakeCCTiledLayerImpl> child2Impl(adoptPtr(new FakeCCTiledLayerImpl(child2->id())));
        root->pushPropertiesTo(rootImpl.get());
        child->pushPropertiesTo(childImpl.get());
        child2->pushPropertiesTo(child2Impl.get());

        for (unsigned i = 0; i < 3; ++i) {
            for (unsigned j = 0; j < 2; ++j)
                EXPECT_TRUE(rootImpl->hasTextureIdForTileAt(i, j));
            EXPECT_FALSE(childImpl->hasTextureIdForTileAt(i, 0));
            EXPECT_FALSE(child2Impl->hasTextureIdForTileAt(i, 0));
        }
    }
    ccLayerTreeHost->commitComplete();

    // With a memory limit that includes only half the root layer, no contents will be
    // allocated. If render surface memory wasn't accounted for, there is enough space
    // for one of the children layers, but they draw into a surface that can't be
    // allocated.
    root->invalidateContentRect(rootRect);
    child->invalidateContentRect(childRect);
    child2->invalidateContentRect(child2Rect);
    ccLayerTreeHost->updateLayers(m_updater, (3 * 1) * (100 * 100) * 4);
    {
        DebugScopedSetImplThread implThread;
        updateTextures(1000);
        EXPECT_EQ(0, root->fakeLayerTextureUpdater()->updateCount());
        EXPECT_EQ(0, child->fakeLayerTextureUpdater()->updateCount());
        EXPECT_EQ(0, child2->fakeLayerTextureUpdater()->updateCount());
        EXPECT_FALSE(m_updater.hasMoreUpdates());

        root->fakeLayerTextureUpdater()->clearUpdateCount();
        child->fakeLayerTextureUpdater()->clearUpdateCount();
        child2->fakeLayerTextureUpdater()->clearUpdateCount();

        OwnPtr<FakeCCTiledLayerImpl> rootImpl(adoptPtr(new FakeCCTiledLayerImpl(root->id())));
        OwnPtr<FakeCCTiledLayerImpl> childImpl(adoptPtr(new FakeCCTiledLayerImpl(child->id())));
        OwnPtr<FakeCCTiledLayerImpl> child2Impl(adoptPtr(new FakeCCTiledLayerImpl(child2->id())));
        root->pushPropertiesTo(rootImpl.get());
        child->pushPropertiesTo(childImpl.get());
        child2->pushPropertiesTo(child2Impl.get());

        for (unsigned i = 0; i < 3; ++i) {
            for (unsigned j = 0; j < 2; ++j)
                EXPECT_FALSE(rootImpl->hasTextureIdForTileAt(i, j));
            EXPECT_FALSE(childImpl->hasTextureIdForTileAt(i, 0));
            EXPECT_FALSE(child2Impl->hasTextureIdForTileAt(i, 0));
        }
    }
    ccLayerTreeHost->commitComplete();

    {
        DebugScopedSetImplThread implThread;
        ccLayerTreeHost->contentsTextureManager()->clearAllMemory(m_resourceProvider.get());
    }
    ccLayerTreeHost->setRootLayer(0);
    ccLayerTreeHost.clear();
    WebKit::WebCompositor::shutdown();
}

class TrackingLayerPainter : public LayerPainterChromium {
public:
    static PassOwnPtr<TrackingLayerPainter> create() { return adoptPtr(new TrackingLayerPainter()); }

    virtual void paint(SkCanvas*, const IntRect& contentRect, FloatRect&) OVERRIDE
    {
        m_paintedRect = contentRect;
    }

    const IntRect& paintedRect() const { return m_paintedRect; }
    void resetPaintedRect() { m_paintedRect = IntRect(); }

private:
    TrackingLayerPainter() { }

    IntRect m_paintedRect;
};

class UpdateTrackingTiledLayerChromium : public FakeTiledLayerChromium {
public:
    explicit UpdateTrackingTiledLayerChromium(WebCore::CCPrioritizedTextureManager* manager)
        : FakeTiledLayerChromium(manager)
    {
        OwnPtr<TrackingLayerPainter> trackingLayerPainter(TrackingLayerPainter::create());
        m_trackingLayerPainter = trackingLayerPainter.get();
        m_layerTextureUpdater = BitmapCanvasLayerTextureUpdater::create(trackingLayerPainter.release());
    }
    virtual ~UpdateTrackingTiledLayerChromium() { }

    TrackingLayerPainter* trackingLayerPainter() const { return m_trackingLayerPainter; }

protected:
    virtual WebCore::LayerTextureUpdater* textureUpdater() const OVERRIDE { return m_layerTextureUpdater.get(); }

private:
    TrackingLayerPainter* m_trackingLayerPainter;
    RefPtr<BitmapCanvasLayerTextureUpdater> m_layerTextureUpdater;
};

TEST_F(TiledLayerChromiumTest, nonIntegerContentsScaleIsNotDistortedDuringPaint)
{
    OwnPtr<CCPrioritizedTextureManager> textureManager(CCPrioritizedTextureManager::create(4000000, 4000000, CCRenderer::ContentPool));

    RefPtr<UpdateTrackingTiledLayerChromium> layer = adoptRef(new UpdateTrackingTiledLayerChromium(textureManager.get()));

    IntRect layerRect(0, 0, 30, 31);
    layer->setPosition(layerRect.location());
    layer->setBounds(layerRect.size());
    layer->setContentsScale(1.5);

    IntRect contentRect(0, 0, 45, 47);
    EXPECT_EQ(contentRect.size(), layer->contentBounds());
    layer->setVisibleContentRect(contentRect);

    layer->setTexturePriorities(m_priorityCalculator);
    textureManager->prioritizeTextures();

    // Update the whole tile.
    layer->updateContentRect(m_updater, contentRect, 0, m_stats);
    layer->trackingLayerPainter()->resetPaintedRect();

    EXPECT_INT_RECT_EQ(IntRect(), layer->trackingLayerPainter()->paintedRect());

    {
        DebugScopedSetImplThread implThread;
        updateTextures();
    }

    // Invalidate the entire layer in content space. When painting, the rect given to webkit should match the layer's bounds.
    layer->invalidateContentRect(contentRect);
    layer->updateContentRect(m_updater, contentRect, 0, m_stats);

    EXPECT_INT_RECT_EQ(layerRect, layer->trackingLayerPainter()->paintedRect());
}

TEST_F(TiledLayerChromiumTest, nonIntegerContentsScaleIsNotDistortedDuringInvalidation)
{
    OwnPtr<CCPrioritizedTextureManager> textureManager(CCPrioritizedTextureManager::create(4000000, 4000000, CCRenderer::ContentPool));

    RefPtr<UpdateTrackingTiledLayerChromium> layer = adoptRef(new UpdateTrackingTiledLayerChromium(textureManager.get()));

    IntRect layerRect(0, 0, 30, 31);
    layer->setPosition(layerRect.location());
    layer->setBounds(layerRect.size());
    layer->setContentsScale(1.3f);

    IntRect contentRect(IntPoint(), layer->contentBounds());
    layer->setVisibleContentRect(contentRect);

    layer->setTexturePriorities(m_priorityCalculator);
    textureManager->prioritizeTextures();

    // Update the whole tile.
    layer->updateContentRect(m_updater, contentRect, 0, m_stats);
    layer->trackingLayerPainter()->resetPaintedRect();

    EXPECT_INT_RECT_EQ(IntRect(), layer->trackingLayerPainter()->paintedRect());

    {
        DebugScopedSetImplThread implThread;
        updateTextures();
    }

    // Invalidate the entire layer in layer space. When painting, the rect given to webkit should match the layer's bounds.
    layer->setNeedsDisplayRect(layerRect);
    layer->updateContentRect(m_updater, contentRect, 0, m_stats);

    EXPECT_INT_RECT_EQ(layerRect, layer->trackingLayerPainter()->paintedRect());
}

} // namespace