Home | History | Annotate | Download | only in resources
      1 // Copyright 2013 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "base/memory/scoped_ptr.h"
      6 #include "cc/test/fake_picture_pile_impl.h"
      7 #include "cc/test/fake_rendering_stats_instrumentation.h"
      8 #include "cc/test/skia_common.h"
      9 #include "skia/ext/lazy_pixel_ref.h"
     10 #include "skia/ext/refptr.h"
     11 #include "testing/gtest/include/gtest/gtest.h"
     12 #include "third_party/skia/include/core/SkPixelRef.h"
     13 #include "third_party/skia/include/core/SkShader.h"
     14 #include "ui/gfx/rect.h"
     15 #include "ui/gfx/size_conversions.h"
     16 
     17 namespace cc {
     18 namespace {
     19 
     20 TEST(PicturePileImplTest, AnalyzeIsSolidUnscaled) {
     21   gfx::Size tile_size(100, 100);
     22   gfx::Size layer_bounds(400, 400);
     23 
     24   scoped_refptr<FakePicturePileImpl> pile =
     25       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
     26 
     27   SkColor solid_color = SkColorSetARGB(255, 12, 23, 34);
     28   SkPaint solid_paint;
     29   solid_paint.setColor(solid_color);
     30 
     31   SkColor non_solid_color = SkColorSetARGB(128, 45, 56, 67);
     32   SkPaint non_solid_paint;
     33   non_solid_paint.setColor(non_solid_color);
     34 
     35   pile->add_draw_rect_with_paint(gfx::Rect(0, 0, 400, 400), solid_paint);
     36   pile->RerecordPile();
     37 
     38   // Ensure everything is solid
     39   for (int y = 0; y <= 300; y += 100) {
     40     for (int x = 0; x <= 300; x += 100) {
     41       PicturePileImpl::Analysis analysis;
     42       gfx::Rect rect(x, y, 100, 100);
     43       pile->AnalyzeInRect(rect, 1.0, &analysis);
     44       EXPECT_TRUE(analysis.is_solid_color) << rect.ToString();
     45       EXPECT_EQ(analysis.solid_color, solid_color) << rect.ToString();
     46     }
     47   }
     48 
     49   // One pixel non solid
     50   pile->add_draw_rect_with_paint(gfx::Rect(50, 50, 1, 1), non_solid_paint);
     51   pile->RerecordPile();
     52 
     53   PicturePileImpl::Analysis analysis;
     54   pile->AnalyzeInRect(gfx::Rect(0, 0, 100, 100), 1.0, &analysis);
     55   EXPECT_FALSE(analysis.is_solid_color);
     56 
     57   pile->AnalyzeInRect(gfx::Rect(100, 0, 100, 100), 1.0, &analysis);
     58   EXPECT_TRUE(analysis.is_solid_color);
     59   EXPECT_EQ(analysis.solid_color, solid_color);
     60 
     61   // Boundaries should be clipped
     62   analysis.is_solid_color = false;
     63   pile->AnalyzeInRect(gfx::Rect(350, 0, 100, 100), 1.0, &analysis);
     64   EXPECT_TRUE(analysis.is_solid_color);
     65   EXPECT_EQ(analysis.solid_color, solid_color);
     66 
     67   analysis.is_solid_color = false;
     68   pile->AnalyzeInRect(gfx::Rect(0, 350, 100, 100), 1.0, &analysis);
     69   EXPECT_TRUE(analysis.is_solid_color);
     70   EXPECT_EQ(analysis.solid_color, solid_color);
     71 
     72   analysis.is_solid_color = false;
     73   pile->AnalyzeInRect(gfx::Rect(350, 350, 100, 100), 1.0, &analysis);
     74   EXPECT_TRUE(analysis.is_solid_color);
     75   EXPECT_EQ(analysis.solid_color, solid_color);
     76 }
     77 
     78 TEST(PicturePileImplTest, AnalyzeIsSolidScaled) {
     79   gfx::Size tile_size(100, 100);
     80   gfx::Size layer_bounds(400, 400);
     81 
     82   scoped_refptr<FakePicturePileImpl> pile =
     83       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
     84 
     85   SkColor solid_color = SkColorSetARGB(255, 12, 23, 34);
     86   SkPaint solid_paint;
     87   solid_paint.setColor(solid_color);
     88 
     89   SkColor non_solid_color = SkColorSetARGB(128, 45, 56, 67);
     90   SkPaint non_solid_paint;
     91   non_solid_paint.setColor(non_solid_color);
     92 
     93   pile->add_draw_rect_with_paint(gfx::Rect(0, 0, 400, 400), solid_paint);
     94   pile->RerecordPile();
     95 
     96   // Ensure everything is solid
     97   for (int y = 0; y <= 30; y += 10) {
     98     for (int x = 0; x <= 30; x += 10) {
     99       PicturePileImpl::Analysis analysis;
    100       gfx::Rect rect(x, y, 10, 10);
    101       pile->AnalyzeInRect(rect, 0.1f, &analysis);
    102       EXPECT_TRUE(analysis.is_solid_color) << rect.ToString();
    103       EXPECT_EQ(analysis.solid_color, solid_color) << rect.ToString();
    104     }
    105   }
    106 
    107   // One pixel non solid
    108   pile->add_draw_rect_with_paint(gfx::Rect(50, 50, 1, 1), non_solid_paint);
    109   pile->RerecordPile();
    110 
    111   PicturePileImpl::Analysis analysis;
    112   pile->AnalyzeInRect(gfx::Rect(0, 0, 10, 10), 0.1f, &analysis);
    113   EXPECT_FALSE(analysis.is_solid_color);
    114 
    115   pile->AnalyzeInRect(gfx::Rect(10, 0, 10, 10), 0.1f, &analysis);
    116   EXPECT_TRUE(analysis.is_solid_color);
    117   EXPECT_EQ(analysis.solid_color, solid_color);
    118 
    119   // Boundaries should be clipped
    120   analysis.is_solid_color = false;
    121   pile->AnalyzeInRect(gfx::Rect(35, 0, 10, 10), 0.1f, &analysis);
    122   EXPECT_TRUE(analysis.is_solid_color);
    123   EXPECT_EQ(analysis.solid_color, solid_color);
    124 
    125   analysis.is_solid_color = false;
    126   pile->AnalyzeInRect(gfx::Rect(0, 35, 10, 10), 0.1f, &analysis);
    127   EXPECT_TRUE(analysis.is_solid_color);
    128   EXPECT_EQ(analysis.solid_color, solid_color);
    129 
    130   analysis.is_solid_color = false;
    131   pile->AnalyzeInRect(gfx::Rect(35, 35, 10, 10), 0.1f, &analysis);
    132   EXPECT_TRUE(analysis.is_solid_color);
    133   EXPECT_EQ(analysis.solid_color, solid_color);
    134 }
    135 
    136 TEST(PicturePileImplTest, AnalyzeIsSolidEmpty) {
    137   gfx::Size tile_size(100, 100);
    138   gfx::Size layer_bounds(400, 400);
    139 
    140   scoped_refptr<FakePicturePileImpl> pile =
    141       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
    142   PicturePileImpl::Analysis analysis;
    143   EXPECT_FALSE(analysis.is_solid_color);
    144 
    145   pile->AnalyzeInRect(gfx::Rect(0, 0, 400, 400), 1.f, &analysis);
    146 
    147   EXPECT_TRUE(analysis.is_solid_color);
    148   EXPECT_EQ(analysis.solid_color, SkColorSetARGB(0, 0, 0, 0));
    149 }
    150 
    151 TEST(PicturePileImplTest, PixelRefIteratorEmpty) {
    152   gfx::Size tile_size(128, 128);
    153   gfx::Size layer_bounds(256, 256);
    154 
    155   // Create a filled pile with no recording.
    156   scoped_refptr<FakePicturePileImpl> pile =
    157       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
    158 
    159   // Tile sized iterators.
    160   {
    161     PicturePileImpl::PixelRefIterator iterator(
    162         gfx::Rect(0, 0, 128, 128), 1.0, pile.get());
    163     EXPECT_FALSE(iterator);
    164   }
    165   {
    166     PicturePileImpl::PixelRefIterator iterator(
    167         gfx::Rect(0, 0, 256, 256), 2.0, pile.get());
    168     EXPECT_FALSE(iterator);
    169   }
    170   {
    171     PicturePileImpl::PixelRefIterator iterator(
    172         gfx::Rect(0, 0, 64, 64), 0.5, pile.get());
    173     EXPECT_FALSE(iterator);
    174   }
    175   // Shifted tile sized iterators.
    176   {
    177     PicturePileImpl::PixelRefIterator iterator(
    178         gfx::Rect(140, 140, 128, 128), 1.0, pile.get());
    179     EXPECT_FALSE(iterator);
    180   }
    181   {
    182     PicturePileImpl::PixelRefIterator iterator(
    183         gfx::Rect(280, 280, 256, 256), 2.0, pile.get());
    184     EXPECT_FALSE(iterator);
    185   }
    186   {
    187     PicturePileImpl::PixelRefIterator iterator(
    188         gfx::Rect(70, 70, 64, 64), 0.5, pile.get());
    189     EXPECT_FALSE(iterator);
    190   }
    191   // Layer sized iterators.
    192   {
    193     PicturePileImpl::PixelRefIterator iterator(
    194         gfx::Rect(0, 0, 256, 256), 1.0, pile.get());
    195     EXPECT_FALSE(iterator);
    196   }
    197   {
    198     PicturePileImpl::PixelRefIterator iterator(
    199         gfx::Rect(0, 0, 512, 512), 2.0, pile.get());
    200     EXPECT_FALSE(iterator);
    201   }
    202   {
    203     PicturePileImpl::PixelRefIterator iterator(
    204         gfx::Rect(0, 0, 128, 128), 0.5, pile.get());
    205     EXPECT_FALSE(iterator);
    206   }
    207 }
    208 
    209 TEST(PicturePileImplTest, PixelRefIteratorNoLazyRefs) {
    210   gfx::Size tile_size(128, 128);
    211   gfx::Size layer_bounds(256, 256);
    212 
    213   scoped_refptr<FakePicturePileImpl> pile =
    214       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
    215 
    216   SkPaint simple_paint;
    217   simple_paint.setColor(SkColorSetARGB(255, 12, 23, 34));
    218 
    219   SkBitmap non_lazy_bitmap;
    220   CreateBitmap(gfx::Size(128, 128), "notlazy", &non_lazy_bitmap);
    221 
    222   pile->add_draw_rect_with_paint(gfx::Rect(0, 0, 256, 256), simple_paint);
    223   pile->add_draw_rect_with_paint(gfx::Rect(128, 128, 512, 512), simple_paint);
    224   pile->add_draw_rect_with_paint(gfx::Rect(512, 0, 256, 256), simple_paint);
    225   pile->add_draw_rect_with_paint(gfx::Rect(0, 512, 256, 256), simple_paint);
    226   pile->add_draw_bitmap(non_lazy_bitmap, gfx::Point(128, 0));
    227   pile->add_draw_bitmap(non_lazy_bitmap, gfx::Point(0, 128));
    228   pile->add_draw_bitmap(non_lazy_bitmap, gfx::Point(150, 150));
    229 
    230   pile->RerecordPile();
    231 
    232   // Tile sized iterators.
    233   {
    234     PicturePileImpl::PixelRefIterator iterator(
    235         gfx::Rect(0, 0, 128, 128), 1.0, pile.get());
    236     EXPECT_FALSE(iterator);
    237   }
    238   {
    239     PicturePileImpl::PixelRefIterator iterator(
    240         gfx::Rect(0, 0, 256, 256), 2.0, pile.get());
    241     EXPECT_FALSE(iterator);
    242   }
    243   {
    244     PicturePileImpl::PixelRefIterator iterator(
    245         gfx::Rect(0, 0, 64, 64), 0.5, pile.get());
    246     EXPECT_FALSE(iterator);
    247   }
    248   // Shifted tile sized iterators.
    249   {
    250     PicturePileImpl::PixelRefIterator iterator(
    251         gfx::Rect(140, 140, 128, 128), 1.0, pile.get());
    252     EXPECT_FALSE(iterator);
    253   }
    254   {
    255     PicturePileImpl::PixelRefIterator iterator(
    256         gfx::Rect(280, 280, 256, 256), 2.0, pile.get());
    257     EXPECT_FALSE(iterator);
    258   }
    259   {
    260     PicturePileImpl::PixelRefIterator iterator(
    261         gfx::Rect(70, 70, 64, 64), 0.5, pile.get());
    262     EXPECT_FALSE(iterator);
    263   }
    264   // Layer sized iterators.
    265   {
    266     PicturePileImpl::PixelRefIterator iterator(
    267         gfx::Rect(0, 0, 256, 256), 1.0, pile.get());
    268     EXPECT_FALSE(iterator);
    269   }
    270   {
    271     PicturePileImpl::PixelRefIterator iterator(
    272         gfx::Rect(0, 0, 512, 512), 2.0, pile.get());
    273     EXPECT_FALSE(iterator);
    274   }
    275   {
    276     PicturePileImpl::PixelRefIterator iterator(
    277         gfx::Rect(0, 0, 128, 128), 0.5, pile.get());
    278     EXPECT_FALSE(iterator);
    279   }
    280 }
    281 
    282 TEST(PicturePileImplTest, PixelRefIteratorLazyRefs) {
    283   gfx::Size tile_size(128, 128);
    284   gfx::Size layer_bounds(256, 256);
    285 
    286   scoped_refptr<FakePicturePileImpl> pile =
    287       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
    288 
    289   SkBitmap lazy_bitmap[2][2];
    290   CreateBitmap(gfx::Size(32, 32), "lazy", &lazy_bitmap[0][0]);
    291   CreateBitmap(gfx::Size(32, 32), "lazy", &lazy_bitmap[1][0]);
    292   CreateBitmap(gfx::Size(32, 32), "lazy", &lazy_bitmap[1][1]);
    293 
    294   // Lazy pixel refs are found in the following cells:
    295   // |---|---|
    296   // | x |   |
    297   // |---|---|
    298   // | x | x |
    299   // |---|---|
    300   pile->add_draw_bitmap(lazy_bitmap[0][0], gfx::Point(0, 0));
    301   pile->add_draw_bitmap(lazy_bitmap[1][0], gfx::Point(0, 130));
    302   pile->add_draw_bitmap(lazy_bitmap[1][1], gfx::Point(140, 140));
    303 
    304   pile->RerecordPile();
    305 
    306   // Tile sized iterators. These should find only one pixel ref.
    307   {
    308     PicturePileImpl::PixelRefIterator iterator(
    309         gfx::Rect(0, 0, 128, 128), 1.0, pile.get());
    310     EXPECT_TRUE(iterator);
    311     EXPECT_TRUE(*iterator == lazy_bitmap[0][0].pixelRef());
    312     EXPECT_FALSE(++iterator);
    313   }
    314   {
    315     PicturePileImpl::PixelRefIterator iterator(
    316         gfx::Rect(0, 0, 256, 256), 2.0, pile.get());
    317     EXPECT_TRUE(iterator);
    318     EXPECT_TRUE(*iterator == lazy_bitmap[0][0].pixelRef());
    319     EXPECT_FALSE(++iterator);
    320   }
    321   {
    322     PicturePileImpl::PixelRefIterator iterator(
    323         gfx::Rect(0, 0, 64, 64), 0.5, pile.get());
    324     EXPECT_TRUE(iterator);
    325     EXPECT_TRUE(*iterator == lazy_bitmap[0][0].pixelRef());
    326     EXPECT_FALSE(++iterator);
    327   }
    328   // Shifted tile sized iterators. These should find only one pixel ref.
    329   {
    330     PicturePileImpl::PixelRefIterator iterator(
    331         gfx::Rect(140, 140, 128, 128), 1.0, pile.get());
    332     EXPECT_TRUE(iterator);
    333     EXPECT_TRUE(*iterator == lazy_bitmap[1][1].pixelRef());
    334     EXPECT_FALSE(++iterator);
    335   }
    336   {
    337     PicturePileImpl::PixelRefIterator iterator(
    338         gfx::Rect(280, 280, 256, 256), 2.0, pile.get());
    339     EXPECT_TRUE(iterator);
    340     EXPECT_TRUE(*iterator == lazy_bitmap[1][1].pixelRef());
    341     EXPECT_FALSE(++iterator);
    342   }
    343   {
    344     PicturePileImpl::PixelRefIterator iterator(
    345         gfx::Rect(70, 70, 64, 64), 0.5, pile.get());
    346     EXPECT_TRUE(iterator);
    347     EXPECT_TRUE(*iterator == lazy_bitmap[1][1].pixelRef());
    348     EXPECT_FALSE(++iterator);
    349   }
    350   // Ensure there's no lazy pixel refs in the empty cell
    351   {
    352     PicturePileImpl::PixelRefIterator iterator(
    353         gfx::Rect(140, 0, 128, 128), 1.0, pile.get());
    354     EXPECT_FALSE(iterator);
    355   }
    356   // Layer sized iterators. These should find all 3 pixel refs.
    357   {
    358     PicturePileImpl::PixelRefIterator iterator(
    359         gfx::Rect(0, 0, 256, 256), 1.0, pile.get());
    360     EXPECT_TRUE(iterator);
    361     EXPECT_TRUE(*iterator == lazy_bitmap[0][0].pixelRef());
    362     EXPECT_TRUE(++iterator);
    363     EXPECT_TRUE(*iterator == lazy_bitmap[1][0].pixelRef());
    364     EXPECT_TRUE(++iterator);
    365     EXPECT_TRUE(*iterator == lazy_bitmap[1][1].pixelRef());
    366     EXPECT_FALSE(++iterator);
    367   }
    368   {
    369     PicturePileImpl::PixelRefIterator iterator(
    370         gfx::Rect(0, 0, 512, 512), 2.0, pile.get());
    371     EXPECT_TRUE(iterator);
    372     EXPECT_TRUE(*iterator == lazy_bitmap[0][0].pixelRef());
    373     EXPECT_TRUE(++iterator);
    374     EXPECT_TRUE(*iterator == lazy_bitmap[1][0].pixelRef());
    375     EXPECT_TRUE(++iterator);
    376     EXPECT_TRUE(*iterator == lazy_bitmap[1][1].pixelRef());
    377     EXPECT_FALSE(++iterator);
    378   }
    379   {
    380     PicturePileImpl::PixelRefIterator iterator(
    381         gfx::Rect(0, 0, 128, 128), 0.5, pile.get());
    382     EXPECT_TRUE(iterator);
    383     EXPECT_TRUE(*iterator == lazy_bitmap[0][0].pixelRef());
    384     EXPECT_TRUE(++iterator);
    385     EXPECT_TRUE(*iterator == lazy_bitmap[1][0].pixelRef());
    386     EXPECT_TRUE(++iterator);
    387     EXPECT_TRUE(*iterator == lazy_bitmap[1][1].pixelRef());
    388     EXPECT_FALSE(++iterator);
    389   }
    390 }
    391 
    392 TEST(PicturePileImplTest, PixelRefIteratorLazyRefsOneTile) {
    393   gfx::Size tile_size(256, 256);
    394   gfx::Size layer_bounds(512, 512);
    395 
    396   scoped_refptr<FakePicturePileImpl> pile =
    397       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
    398 
    399   SkBitmap lazy_bitmap[2][2];
    400   CreateBitmap(gfx::Size(32, 32), "lazy", &lazy_bitmap[0][0]);
    401   CreateBitmap(gfx::Size(32, 32), "lazy", &lazy_bitmap[0][1]);
    402   CreateBitmap(gfx::Size(32, 32), "lazy", &lazy_bitmap[1][1]);
    403 
    404   // Lazy pixel refs are found in the following cells:
    405   // |---|---|
    406   // | x | x |
    407   // |---|---|
    408   // |   | x |
    409   // |---|---|
    410   pile->add_draw_bitmap(lazy_bitmap[0][0], gfx::Point(0, 0));
    411   pile->add_draw_bitmap(lazy_bitmap[0][1], gfx::Point(260, 0));
    412   pile->add_draw_bitmap(lazy_bitmap[1][1], gfx::Point(260, 260));
    413 
    414   pile->RerecordPile();
    415 
    416   // Tile sized iterators. These should find only one pixel ref.
    417   {
    418     PicturePileImpl::PixelRefIterator iterator(
    419         gfx::Rect(0, 0, 256, 256), 1.0, pile.get());
    420     EXPECT_TRUE(iterator);
    421     EXPECT_TRUE(*iterator == lazy_bitmap[0][0].pixelRef());
    422     EXPECT_FALSE(++iterator);
    423   }
    424   {
    425     PicturePileImpl::PixelRefIterator iterator(
    426         gfx::Rect(0, 0, 512, 512), 2.0, pile.get());
    427     EXPECT_TRUE(iterator);
    428     EXPECT_TRUE(*iterator == lazy_bitmap[0][0].pixelRef());
    429     EXPECT_FALSE(++iterator);
    430   }
    431   {
    432     PicturePileImpl::PixelRefIterator iterator(
    433         gfx::Rect(0, 0, 128, 128), 0.5, pile.get());
    434     EXPECT_TRUE(iterator);
    435     EXPECT_TRUE(*iterator == lazy_bitmap[0][0].pixelRef());
    436     EXPECT_FALSE(++iterator);
    437   }
    438   // Shifted tile sized iterators. These should find only one pixel ref.
    439   {
    440     PicturePileImpl::PixelRefIterator iterator(
    441         gfx::Rect(260, 260, 256, 256), 1.0, pile.get());
    442     EXPECT_TRUE(iterator);
    443     EXPECT_TRUE(*iterator == lazy_bitmap[1][1].pixelRef());
    444     EXPECT_FALSE(++iterator);
    445   }
    446   {
    447     PicturePileImpl::PixelRefIterator iterator(
    448         gfx::Rect(520, 520, 512, 512), 2.0, pile.get());
    449     EXPECT_TRUE(iterator);
    450     EXPECT_TRUE(*iterator == lazy_bitmap[1][1].pixelRef());
    451     EXPECT_FALSE(++iterator);
    452   }
    453   {
    454     PicturePileImpl::PixelRefIterator iterator(
    455         gfx::Rect(130, 130, 128, 128), 0.5, pile.get());
    456     EXPECT_TRUE(iterator);
    457     EXPECT_TRUE(*iterator == lazy_bitmap[1][1].pixelRef());
    458     EXPECT_FALSE(++iterator);
    459   }
    460   // Ensure there's no lazy pixel refs in the empty cell
    461   {
    462     PicturePileImpl::PixelRefIterator iterator(
    463         gfx::Rect(0, 256, 256, 256), 1.0, pile.get());
    464     EXPECT_FALSE(iterator);
    465   }
    466   // Layer sized iterators. These should find three pixel ref.
    467   {
    468     PicturePileImpl::PixelRefIterator iterator(
    469         gfx::Rect(0, 0, 512, 512), 1.0, pile.get());
    470     EXPECT_TRUE(iterator);
    471     EXPECT_TRUE(*iterator == lazy_bitmap[0][0].pixelRef());
    472     EXPECT_TRUE(++iterator);
    473     EXPECT_TRUE(*iterator == lazy_bitmap[0][1].pixelRef());
    474     EXPECT_TRUE(++iterator);
    475     EXPECT_TRUE(*iterator == lazy_bitmap[1][1].pixelRef());
    476     EXPECT_FALSE(++iterator);
    477   }
    478   {
    479     PicturePileImpl::PixelRefIterator iterator(
    480         gfx::Rect(0, 0, 1024, 1024), 2.0, pile.get());
    481     EXPECT_TRUE(iterator);
    482     EXPECT_TRUE(*iterator == lazy_bitmap[0][0].pixelRef());
    483     EXPECT_TRUE(++iterator);
    484     EXPECT_TRUE(*iterator == lazy_bitmap[0][1].pixelRef());
    485     EXPECT_TRUE(++iterator);
    486     EXPECT_TRUE(*iterator == lazy_bitmap[1][1].pixelRef());
    487     EXPECT_FALSE(++iterator);
    488   }
    489   {
    490     PicturePileImpl::PixelRefIterator iterator(
    491         gfx::Rect(0, 0, 256, 256), 0.5, pile.get());
    492     EXPECT_TRUE(iterator);
    493     EXPECT_TRUE(*iterator == lazy_bitmap[0][0].pixelRef());
    494     EXPECT_TRUE(++iterator);
    495     EXPECT_TRUE(*iterator == lazy_bitmap[0][1].pixelRef());
    496     EXPECT_TRUE(++iterator);
    497     EXPECT_TRUE(*iterator == lazy_bitmap[1][1].pixelRef());
    498     EXPECT_FALSE(++iterator);
    499   }
    500 
    501   // Copy test.
    502   PicturePileImpl::PixelRefIterator iterator(
    503       gfx::Rect(0, 0, 512, 512), 1.0, pile.get());
    504   EXPECT_TRUE(iterator);
    505   EXPECT_TRUE(*iterator == lazy_bitmap[0][0].pixelRef());
    506   EXPECT_TRUE(++iterator);
    507   EXPECT_TRUE(*iterator == lazy_bitmap[0][1].pixelRef());
    508 
    509   // copy now points to the same spot as iterator,
    510   // but both can be incremented independently.
    511   PicturePileImpl::PixelRefIterator copy = iterator;
    512   EXPECT_TRUE(++iterator);
    513   EXPECT_TRUE(*iterator == lazy_bitmap[1][1].pixelRef());
    514   EXPECT_FALSE(++iterator);
    515 
    516   EXPECT_TRUE(copy);
    517   EXPECT_TRUE(*copy == lazy_bitmap[0][1].pixelRef());
    518   EXPECT_TRUE(++copy);
    519   EXPECT_TRUE(*copy == lazy_bitmap[1][1].pixelRef());
    520   EXPECT_FALSE(++copy);
    521 }
    522 
    523 TEST(PicturePileImplTest, PixelRefIteratorLazyRefsBaseNonLazy) {
    524   gfx::Size tile_size(256, 256);
    525   gfx::Size layer_bounds(512, 512);
    526 
    527   scoped_refptr<FakePicturePileImpl> pile =
    528       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
    529 
    530   SkBitmap non_lazy_bitmap;
    531   CreateBitmap(gfx::Size(512, 512), "notlazy", &non_lazy_bitmap);
    532 
    533   SkBitmap lazy_bitmap[2][2];
    534   CreateBitmap(gfx::Size(128, 128), "lazy", &lazy_bitmap[0][0]);
    535   CreateBitmap(gfx::Size(128, 128), "lazy", &lazy_bitmap[0][1]);
    536   CreateBitmap(gfx::Size(128, 128), "lazy", &lazy_bitmap[1][1]);
    537 
    538   // One large non-lazy bitmap covers the whole grid.
    539   // Lazy pixel refs are found in the following cells:
    540   // |---|---|
    541   // | x | x |
    542   // |---|---|
    543   // |   | x |
    544   // |---|---|
    545   pile->add_draw_bitmap(non_lazy_bitmap, gfx::Point(0, 0));
    546   pile->add_draw_bitmap(lazy_bitmap[0][0], gfx::Point(0, 0));
    547   pile->add_draw_bitmap(lazy_bitmap[0][1], gfx::Point(260, 0));
    548   pile->add_draw_bitmap(lazy_bitmap[1][1], gfx::Point(260, 260));
    549 
    550   pile->RerecordPile();
    551 
    552   // Tile sized iterators. These should find only one pixel ref.
    553   {
    554     PicturePileImpl::PixelRefIterator iterator(
    555         gfx::Rect(0, 0, 256, 256), 1.0, pile.get());
    556     EXPECT_TRUE(iterator);
    557     EXPECT_TRUE(*iterator == lazy_bitmap[0][0].pixelRef());
    558     EXPECT_FALSE(++iterator);
    559   }
    560   {
    561     PicturePileImpl::PixelRefIterator iterator(
    562         gfx::Rect(0, 0, 512, 512), 2.0, pile.get());
    563     EXPECT_TRUE(iterator);
    564     EXPECT_TRUE(*iterator == lazy_bitmap[0][0].pixelRef());
    565     EXPECT_FALSE(++iterator);
    566   }
    567   {
    568     PicturePileImpl::PixelRefIterator iterator(
    569         gfx::Rect(0, 0, 128, 128), 0.5, pile.get());
    570     EXPECT_TRUE(iterator);
    571     EXPECT_TRUE(*iterator == lazy_bitmap[0][0].pixelRef());
    572     EXPECT_FALSE(++iterator);
    573   }
    574   // Shifted tile sized iterators. These should find only one pixel ref.
    575   {
    576     PicturePileImpl::PixelRefIterator iterator(
    577         gfx::Rect(260, 260, 256, 256), 1.0, pile.get());
    578     EXPECT_TRUE(iterator);
    579     EXPECT_TRUE(*iterator == lazy_bitmap[1][1].pixelRef());
    580     EXPECT_FALSE(++iterator);
    581   }
    582   {
    583     PicturePileImpl::PixelRefIterator iterator(
    584         gfx::Rect(520, 520, 512, 512), 2.0, pile.get());
    585     EXPECT_TRUE(iterator);
    586     EXPECT_TRUE(*iterator == lazy_bitmap[1][1].pixelRef());
    587     EXPECT_FALSE(++iterator);
    588   }
    589   {
    590     PicturePileImpl::PixelRefIterator iterator(
    591         gfx::Rect(130, 130, 128, 128), 0.5, pile.get());
    592     EXPECT_TRUE(iterator);
    593     EXPECT_TRUE(*iterator == lazy_bitmap[1][1].pixelRef());
    594     EXPECT_FALSE(++iterator);
    595   }
    596   // Ensure there's no lazy pixel refs in the empty cell
    597   {
    598     PicturePileImpl::PixelRefIterator iterator(
    599         gfx::Rect(0, 256, 256, 256), 1.0, pile.get());
    600     EXPECT_FALSE(iterator);
    601   }
    602   // Layer sized iterators. These should find three pixel ref.
    603   {
    604     PicturePileImpl::PixelRefIterator iterator(
    605         gfx::Rect(0, 0, 512, 512), 1.0, pile.get());
    606     EXPECT_TRUE(iterator);
    607     EXPECT_TRUE(*iterator == lazy_bitmap[0][0].pixelRef());
    608     EXPECT_TRUE(++iterator);
    609     EXPECT_TRUE(*iterator == lazy_bitmap[0][1].pixelRef());
    610     EXPECT_TRUE(++iterator);
    611     EXPECT_TRUE(*iterator == lazy_bitmap[1][1].pixelRef());
    612     EXPECT_FALSE(++iterator);
    613   }
    614   {
    615     PicturePileImpl::PixelRefIterator iterator(
    616         gfx::Rect(0, 0, 1024, 1024), 2.0, pile.get());
    617     EXPECT_TRUE(iterator);
    618     EXPECT_TRUE(*iterator == lazy_bitmap[0][0].pixelRef());
    619     EXPECT_TRUE(++iterator);
    620     EXPECT_TRUE(*iterator == lazy_bitmap[0][1].pixelRef());
    621     EXPECT_TRUE(++iterator);
    622     EXPECT_TRUE(*iterator == lazy_bitmap[1][1].pixelRef());
    623     EXPECT_FALSE(++iterator);
    624   }
    625   {
    626     PicturePileImpl::PixelRefIterator iterator(
    627         gfx::Rect(0, 0, 256, 256), 0.5, pile.get());
    628     EXPECT_TRUE(iterator);
    629     EXPECT_TRUE(*iterator == lazy_bitmap[0][0].pixelRef());
    630     EXPECT_TRUE(++iterator);
    631     EXPECT_TRUE(*iterator == lazy_bitmap[0][1].pixelRef());
    632     EXPECT_TRUE(++iterator);
    633     EXPECT_TRUE(*iterator == lazy_bitmap[1][1].pixelRef());
    634     EXPECT_FALSE(++iterator);
    635   }
    636 }
    637 
    638 TEST(PicturePileImplTest, PixelRefIteratorMultiplePictures) {
    639   gfx::Size tile_size(256, 256);
    640   gfx::Size layer_bounds(256, 256);
    641 
    642   SkTileGridPicture::TileGridInfo tile_grid_info;
    643   tile_grid_info.fTileInterval = SkISize::Make(256, 256);
    644   tile_grid_info.fMargin.setEmpty();
    645   tile_grid_info.fOffset.setZero();
    646 
    647   scoped_refptr<FakePicturePileImpl> pile =
    648       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
    649 
    650   SkBitmap lazy_bitmap[2][2];
    651   CreateBitmap(gfx::Size(32, 32), "lazy", &lazy_bitmap[0][0]);
    652   CreateBitmap(gfx::Size(32, 32), "lazy", &lazy_bitmap[0][1]);
    653   CreateBitmap(gfx::Size(32, 32), "lazy", &lazy_bitmap[1][1]);
    654   SkBitmap non_lazy_bitmap;
    655   CreateBitmap(gfx::Size(256, 256), "notlazy", &non_lazy_bitmap);
    656 
    657   // Each bitmap goes into its own picture, the final layout
    658   // has lazy pixel refs in the following regions:
    659   // ||=======||
    660   // ||x|   |x||
    661   // ||--   --||
    662   // ||     |x||
    663   // ||=======||
    664   pile->add_draw_bitmap(non_lazy_bitmap, gfx::Point(0, 0));
    665   pile->RerecordPile();
    666 
    667   FakeContentLayerClient content_layer_clients[2][2];
    668   FakeRenderingStatsInstrumentation stats_instrumentation;
    669   scoped_refptr<Picture> pictures[2][2];
    670   for (int y = 0; y < 2; ++y) {
    671     for (int x = 0; x < 2; ++x) {
    672       if (x == 0 && y == 1)
    673         continue;
    674       SkPaint paint;
    675       content_layer_clients[y][x].add_draw_bitmap(
    676           lazy_bitmap[y][x],
    677           gfx::Point(x * 128 + 10, y * 128 + 10), paint);
    678       pictures[y][x] = Picture::Create(
    679           gfx::Rect(x * 128 + 10, y * 128 + 10, 64, 64));
    680       pictures[y][x]->Record(
    681           &content_layer_clients[y][x],
    682           tile_grid_info,
    683           &stats_instrumentation);
    684       pictures[y][x]->GatherPixelRefs(tile_grid_info, &stats_instrumentation);
    685       pile->AddPictureToRecording(0, 0, pictures[y][x]);
    686     }
    687   }
    688 
    689   // These should find only one pixel ref.
    690   {
    691     PicturePileImpl::PixelRefIterator iterator(
    692         gfx::Rect(0, 0, 128, 128), 1.0, pile.get());
    693     EXPECT_TRUE(iterator);
    694     EXPECT_TRUE(*iterator == lazy_bitmap[0][0].pixelRef());
    695     EXPECT_FALSE(++iterator);
    696   }
    697   {
    698     PicturePileImpl::PixelRefIterator iterator(
    699         gfx::Rect(128, 0, 128, 128), 1.0, pile.get());
    700     EXPECT_TRUE(iterator);
    701     EXPECT_TRUE(*iterator == lazy_bitmap[0][1].pixelRef());
    702     EXPECT_FALSE(++iterator);
    703   }
    704   {
    705     PicturePileImpl::PixelRefIterator iterator(
    706         gfx::Rect(128, 128, 128, 128), 1.0, pile.get());
    707     EXPECT_TRUE(iterator);
    708     EXPECT_TRUE(*iterator == lazy_bitmap[1][1].pixelRef());
    709     EXPECT_FALSE(++iterator);
    710   }
    711   // This one should not find any refs
    712   {
    713     PicturePileImpl::PixelRefIterator iterator(
    714         gfx::Rect(0, 128, 128, 128), 1.0, pile.get());
    715     EXPECT_FALSE(iterator);
    716   }
    717 }
    718 
    719 TEST(PicturePileImpl, RasterContentsOpaque) {
    720   gfx::Size tile_size(1000, 1000);
    721   gfx::Size layer_bounds(3, 5);
    722   float contents_scale = 1.5f;
    723 
    724   scoped_refptr<FakePicturePileImpl> pile =
    725       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
    726   // Because the caller sets content opaque, it also promises that it
    727   // has at least filled in layer_bounds opaquely.
    728   SkPaint red_paint;
    729   red_paint.setColor(SK_ColorRED);
    730   pile->add_draw_rect_with_paint(gfx::Rect(layer_bounds), red_paint);
    731 
    732   pile->SetMinContentsScale(contents_scale);
    733   pile->set_background_color(SK_ColorRED);
    734   pile->set_contents_opaque(true);
    735   pile->RerecordPile();
    736 
    737   gfx::Size content_bounds(
    738       gfx::ToCeiledSize(gfx::ScaleSize(layer_bounds, contents_scale)));
    739 
    740   // Simulate a canvas rect larger than the content bounds.  Every pixel
    741   // up to one pixel outside the content bounds is guaranteed to be opaque.
    742   // Outside of that is undefined.
    743   gfx::Rect canvas_rect(content_bounds);
    744   canvas_rect.Inset(0, 0, -1, -1);
    745 
    746   SkBitmap bitmap;
    747   bitmap.setConfig(SkBitmap::kARGB_8888_Config,
    748                    canvas_rect.width(),
    749                    canvas_rect.height());
    750   bitmap.allocPixels();
    751   SkCanvas canvas(bitmap);
    752 
    753   PicturePileImpl::RasterStats raster_stats;
    754   pile->RasterToBitmap(
    755       &canvas, canvas_rect, contents_scale, &raster_stats);
    756 
    757   SkColor* pixels = reinterpret_cast<SkColor*>(bitmap.getPixels());
    758   int num_pixels = bitmap.width() * bitmap.height();
    759   for (int i = 0; i < num_pixels; ++i) {
    760     EXPECT_EQ(SkColorGetA(pixels[i]), 255u);
    761   }
    762 }
    763 
    764 TEST(PicturePileImpl, RasterContentsTransparent) {
    765   gfx::Size tile_size(1000, 1000);
    766   gfx::Size layer_bounds(5, 3);
    767   float contents_scale = 0.5f;
    768 
    769   scoped_refptr<FakePicturePileImpl> pile =
    770       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
    771   pile->set_background_color(SK_ColorTRANSPARENT);
    772   pile->set_contents_opaque(false);
    773   pile->SetMinContentsScale(contents_scale);
    774   pile->RerecordPile();
    775 
    776   gfx::Size content_bounds(
    777       gfx::ToCeiledSize(gfx::ScaleSize(layer_bounds, contents_scale)));
    778 
    779   gfx::Rect canvas_rect(content_bounds);
    780   canvas_rect.Inset(0, 0, -1, -1);
    781 
    782   SkBitmap bitmap;
    783   bitmap.setConfig(SkBitmap::kARGB_8888_Config,
    784                    canvas_rect.width(),
    785                    canvas_rect.height());
    786   bitmap.allocPixels();
    787   SkCanvas canvas(bitmap);
    788 
    789   PicturePileImpl::RasterStats raster_stats;
    790   pile->RasterToBitmap(
    791       &canvas, canvas_rect, contents_scale, &raster_stats);
    792 
    793   SkColor* pixels = reinterpret_cast<SkColor*>(bitmap.getPixels());
    794   int num_pixels = bitmap.width() * bitmap.height();
    795   for (int i = 0; i < num_pixels; ++i) {
    796     EXPECT_EQ(SkColorGetA(pixels[i]), 0u);
    797   }
    798 }
    799 
    800 }  // namespace
    801 }  // namespace cc
    802