Home | History | Annotate | Download | only in trees
      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 "build/build_config.h"
      6 #include "cc/layers/content_layer.h"
      7 #include "cc/layers/solid_color_layer.h"
      8 #include "cc/layers/texture_layer.h"
      9 #include "cc/output/copy_output_request.h"
     10 #include "cc/output/copy_output_result.h"
     11 #include "cc/test/layer_tree_pixel_test.h"
     12 #include "cc/test/paths.h"
     13 #include "cc/test/solid_color_content_layer_client.h"
     14 #include "cc/trees/layer_tree_impl.h"
     15 
     16 #if !defined(OS_ANDROID)
     17 
     18 namespace cc {
     19 namespace {
     20 
     21 class LayerTreeHostReadbackPixelTest : public LayerTreePixelTest {
     22  protected:
     23   virtual scoped_ptr<CopyOutputRequest> CreateCopyOutputRequest() OVERRIDE {
     24     scoped_ptr<CopyOutputRequest> request;
     25 
     26     switch (test_type_) {
     27       case GL_WITH_BITMAP:
     28       case SOFTWARE_WITH_BITMAP:
     29         request = CopyOutputRequest::CreateBitmapRequest(
     30             base::Bind(&LayerTreeHostReadbackPixelTest::ReadbackResultAsBitmap,
     31                        base::Unretained(this)));
     32         break;
     33       case SOFTWARE_WITH_DEFAULT:
     34         request = CopyOutputRequest::CreateRequest(
     35             base::Bind(&LayerTreeHostReadbackPixelTest::ReadbackResultAsBitmap,
     36                        base::Unretained(this)));
     37         break;
     38       case GL_WITH_DEFAULT:
     39         request = CopyOutputRequest::CreateRequest(
     40             base::Bind(&LayerTreeHostReadbackPixelTest::ReadbackResultAsTexture,
     41                        base::Unretained(this)));
     42         break;
     43     }
     44 
     45     if (!copy_subrect_.IsEmpty())
     46       request->set_area(copy_subrect_);
     47     return request.Pass();
     48   }
     49 
     50   void ReadbackResultAsBitmap(scoped_ptr<CopyOutputResult> result) {
     51     EXPECT_TRUE(proxy()->IsMainThread());
     52     EXPECT_TRUE(result->HasBitmap());
     53     result_bitmap_ = result->TakeBitmap().Pass();
     54     EndTest();
     55   }
     56 
     57   void ReadbackResultAsTexture(scoped_ptr<CopyOutputResult> result) {
     58     EXPECT_TRUE(proxy()->IsMainThread());
     59     EXPECT_TRUE(result->HasTexture());
     60 
     61     TextureMailbox texture_mailbox;
     62     scoped_ptr<SingleReleaseCallback> release_callback;
     63     result->TakeTexture(&texture_mailbox, &release_callback);
     64     EXPECT_TRUE(texture_mailbox.IsValid());
     65     EXPECT_TRUE(texture_mailbox.IsTexture());
     66 
     67     scoped_ptr<SkBitmap> bitmap =
     68         CopyTextureMailboxToBitmap(result->size(), texture_mailbox);
     69     release_callback->Run(0, false);
     70 
     71     ReadbackResultAsBitmap(CopyOutputResult::CreateBitmapResult(bitmap.Pass()));
     72   }
     73 
     74   gfx::Rect copy_subrect_;
     75 };
     76 
     77 TEST_F(LayerTreeHostReadbackPixelTest, ReadbackRootLayer_Software) {
     78   scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
     79       gfx::Rect(200, 200), SK_ColorWHITE);
     80 
     81   scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
     82       gfx::Rect(200, 200), SK_ColorGREEN);
     83   background->AddChild(green);
     84 
     85   RunPixelTest(SOFTWARE_WITH_DEFAULT,
     86                background,
     87                base::FilePath(FILE_PATH_LITERAL(
     88                    "green.png")));
     89 }
     90 
     91 TEST_F(LayerTreeHostReadbackPixelTest, ReadbackRootLayer_Software_Bitmap) {
     92   scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
     93       gfx::Rect(200, 200), SK_ColorWHITE);
     94 
     95   scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
     96       gfx::Rect(200, 200), SK_ColorGREEN);
     97   background->AddChild(green);
     98 
     99   RunPixelTest(SOFTWARE_WITH_BITMAP,
    100                background,
    101                base::FilePath(FILE_PATH_LITERAL(
    102                    "green.png")));
    103 }
    104 
    105 TEST_F(LayerTreeHostReadbackPixelTest, ReadbackRootLayer_GL_Bitmap) {
    106   scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
    107       gfx::Rect(200, 200), SK_ColorWHITE);
    108 
    109   scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
    110       gfx::Rect(200, 200), SK_ColorGREEN);
    111   background->AddChild(green);
    112 
    113   RunPixelTest(GL_WITH_BITMAP,
    114                background,
    115                base::FilePath(FILE_PATH_LITERAL(
    116                    "green.png")));
    117 }
    118 
    119 TEST_F(LayerTreeHostReadbackPixelTest, ReadbackRootLayer_GL) {
    120   scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
    121       gfx::Rect(200, 200), SK_ColorWHITE);
    122 
    123   scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
    124       gfx::Rect(200, 200), SK_ColorGREEN);
    125   background->AddChild(green);
    126 
    127   RunPixelTest(GL_WITH_DEFAULT,
    128                background,
    129                base::FilePath(FILE_PATH_LITERAL(
    130                    "green.png")));
    131 }
    132 
    133 TEST_F(LayerTreeHostReadbackPixelTest,
    134        ReadbackRootLayerWithChild_Software) {
    135   scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
    136       gfx::Rect(200, 200), SK_ColorWHITE);
    137 
    138   scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
    139       gfx::Rect(200, 200), SK_ColorGREEN);
    140   background->AddChild(green);
    141 
    142   scoped_refptr<SolidColorLayer> blue = CreateSolidColorLayer(
    143       gfx::Rect(150, 150, 50, 50), SK_ColorBLUE);
    144   green->AddChild(blue);
    145 
    146   RunPixelTest(SOFTWARE_WITH_DEFAULT,
    147                background,
    148                base::FilePath(FILE_PATH_LITERAL(
    149                    "green_with_blue_corner.png")));
    150 }
    151 
    152 TEST_F(LayerTreeHostReadbackPixelTest, ReadbackRootLayerWithChild_GL_Bitmap) {
    153   scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
    154       gfx::Rect(200, 200), SK_ColorWHITE);
    155 
    156   scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
    157       gfx::Rect(200, 200), SK_ColorGREEN);
    158   background->AddChild(green);
    159 
    160   scoped_refptr<SolidColorLayer> blue = CreateSolidColorLayer(
    161       gfx::Rect(150, 150, 50, 50), SK_ColorBLUE);
    162   green->AddChild(blue);
    163 
    164   RunPixelTest(GL_WITH_BITMAP,
    165                background,
    166                base::FilePath(FILE_PATH_LITERAL(
    167                    "green_with_blue_corner.png")));
    168 }
    169 
    170 TEST_F(LayerTreeHostReadbackPixelTest, ReadbackRootLayerWithChild_GL) {
    171   scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
    172       gfx::Rect(200, 200), SK_ColorWHITE);
    173 
    174   scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
    175       gfx::Rect(200, 200), SK_ColorGREEN);
    176   background->AddChild(green);
    177 
    178   scoped_refptr<SolidColorLayer> blue = CreateSolidColorLayer(
    179       gfx::Rect(150, 150, 50, 50), SK_ColorBLUE);
    180   green->AddChild(blue);
    181 
    182   RunPixelTest(GL_WITH_DEFAULT,
    183                background,
    184                base::FilePath(FILE_PATH_LITERAL(
    185                    "green_with_blue_corner.png")));
    186 }
    187 
    188 TEST_F(LayerTreeHostReadbackPixelTest, ReadbackNonRootLayer_Software) {
    189   scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
    190       gfx::Rect(200, 200), SK_ColorWHITE);
    191 
    192   scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
    193       gfx::Rect(200, 200), SK_ColorGREEN);
    194   background->AddChild(green);
    195 
    196   RunPixelTestWithReadbackTarget(SOFTWARE_WITH_DEFAULT,
    197                                  background,
    198                                  green.get(),
    199                                  base::FilePath(FILE_PATH_LITERAL(
    200                                      "green.png")));
    201 }
    202 
    203 TEST_F(LayerTreeHostReadbackPixelTest, ReadbackNonRootLayer_GL_Bitmap) {
    204   scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
    205       gfx::Rect(200, 200), SK_ColorWHITE);
    206 
    207   scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
    208       gfx::Rect(200, 200), SK_ColorGREEN);
    209   background->AddChild(green);
    210 
    211   RunPixelTestWithReadbackTarget(GL_WITH_BITMAP,
    212                                  background,
    213                                  green.get(),
    214                                  base::FilePath(FILE_PATH_LITERAL(
    215                                      "green.png")));
    216 }
    217 
    218 TEST_F(LayerTreeHostReadbackPixelTest, ReadbackNonRootLayer_GL) {
    219   scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
    220       gfx::Rect(200, 200), SK_ColorWHITE);
    221 
    222   scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
    223       gfx::Rect(200, 200), SK_ColorGREEN);
    224   background->AddChild(green);
    225 
    226   RunPixelTestWithReadbackTarget(GL_WITH_DEFAULT,
    227                                  background,
    228                                  green.get(),
    229                                  base::FilePath(FILE_PATH_LITERAL(
    230                                      "green.png")));
    231 }
    232 
    233 TEST_F(LayerTreeHostReadbackPixelTest,
    234        ReadbackSmallNonRootLayer_Software) {
    235   scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
    236       gfx::Rect(200, 200), SK_ColorWHITE);
    237 
    238   scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
    239       gfx::Rect(100, 100, 100, 100), SK_ColorGREEN);
    240   background->AddChild(green);
    241 
    242   RunPixelTestWithReadbackTarget(SOFTWARE_WITH_DEFAULT,
    243                                  background,
    244                                  green.get(),
    245                                  base::FilePath(FILE_PATH_LITERAL(
    246                                      "green_small.png")));
    247 }
    248 
    249 TEST_F(LayerTreeHostReadbackPixelTest, ReadbackSmallNonRootLayer_GL_Bitmap) {
    250   scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
    251       gfx::Rect(200, 200), SK_ColorWHITE);
    252 
    253   scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
    254       gfx::Rect(100, 100, 100, 100), SK_ColorGREEN);
    255   background->AddChild(green);
    256 
    257   RunPixelTestWithReadbackTarget(GL_WITH_BITMAP,
    258                                  background,
    259                                  green.get(),
    260                                  base::FilePath(FILE_PATH_LITERAL(
    261                                      "green_small.png")));
    262 }
    263 
    264 TEST_F(LayerTreeHostReadbackPixelTest, ReadbackSmallNonRootLayer_GL) {
    265   scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
    266       gfx::Rect(200, 200), SK_ColorWHITE);
    267 
    268   scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
    269       gfx::Rect(100, 100, 100, 100), SK_ColorGREEN);
    270   background->AddChild(green);
    271 
    272   RunPixelTestWithReadbackTarget(GL_WITH_DEFAULT,
    273                                  background,
    274                                  green.get(),
    275                                  base::FilePath(FILE_PATH_LITERAL(
    276                                      "green_small.png")));
    277 }
    278 
    279 TEST_F(LayerTreeHostReadbackPixelTest,
    280        ReadbackSmallNonRootLayerWithChild_Software) {
    281   scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
    282       gfx::Rect(200, 200), SK_ColorWHITE);
    283 
    284   scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
    285       gfx::Rect(100, 100, 100, 100), SK_ColorGREEN);
    286   background->AddChild(green);
    287 
    288   scoped_refptr<SolidColorLayer> blue = CreateSolidColorLayer(
    289       gfx::Rect(50, 50, 50, 50), SK_ColorBLUE);
    290   green->AddChild(blue);
    291 
    292   RunPixelTestWithReadbackTarget(SOFTWARE_WITH_DEFAULT,
    293                                  background,
    294                                  green.get(),
    295                                  base::FilePath(FILE_PATH_LITERAL(
    296                                      "green_small_with_blue_corner.png")));
    297 }
    298 
    299 TEST_F(LayerTreeHostReadbackPixelTest,
    300        ReadbackSmallNonRootLayerWithChild_GL_Bitmap) {
    301   scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
    302       gfx::Rect(200, 200), SK_ColorWHITE);
    303 
    304   scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
    305       gfx::Rect(100, 100, 100, 100), SK_ColorGREEN);
    306   background->AddChild(green);
    307 
    308   scoped_refptr<SolidColorLayer> blue = CreateSolidColorLayer(
    309       gfx::Rect(50, 50, 50, 50), SK_ColorBLUE);
    310   green->AddChild(blue);
    311 
    312   RunPixelTestWithReadbackTarget(GL_WITH_BITMAP,
    313                                  background,
    314                                  green.get(),
    315                                  base::FilePath(FILE_PATH_LITERAL(
    316                                      "green_small_with_blue_corner.png")));
    317 }
    318 
    319 TEST_F(LayerTreeHostReadbackPixelTest, ReadbackSmallNonRootLayerWithChild_GL) {
    320   scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
    321       gfx::Rect(200, 200), SK_ColorWHITE);
    322 
    323   scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
    324       gfx::Rect(100, 100, 100, 100), SK_ColorGREEN);
    325   background->AddChild(green);
    326 
    327   scoped_refptr<SolidColorLayer> blue = CreateSolidColorLayer(
    328       gfx::Rect(50, 50, 50, 50), SK_ColorBLUE);
    329   green->AddChild(blue);
    330 
    331   RunPixelTestWithReadbackTarget(GL_WITH_DEFAULT,
    332                                  background,
    333                                  green.get(),
    334                                  base::FilePath(FILE_PATH_LITERAL(
    335                                      "green_small_with_blue_corner.png")));
    336 }
    337 
    338 TEST_F(LayerTreeHostReadbackPixelTest,
    339        ReadbackSubtreeSurroundsTargetLayer_Software) {
    340   scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
    341       gfx::Rect(0, 0, 200, 200), SK_ColorWHITE);
    342 
    343   scoped_refptr<SolidColorLayer> target = CreateSolidColorLayer(
    344       gfx::Rect(100, 100, 100, 100), SK_ColorRED);
    345   background->AddChild(target);
    346 
    347   scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
    348       gfx::Rect(-100, -100, 300, 300), SK_ColorGREEN);
    349   target->AddChild(green);
    350 
    351   scoped_refptr<SolidColorLayer> blue = CreateSolidColorLayer(
    352       gfx::Rect(50, 50, 50, 50), SK_ColorBLUE);
    353   target->AddChild(blue);
    354 
    355   copy_subrect_ = gfx::Rect(0, 0, 100, 100);
    356   RunPixelTestWithReadbackTarget(SOFTWARE_WITH_DEFAULT,
    357                                  background,
    358                                  target.get(),
    359                                  base::FilePath(FILE_PATH_LITERAL(
    360                                      "green_small_with_blue_corner.png")));
    361 }
    362 
    363 TEST_F(LayerTreeHostReadbackPixelTest,
    364        ReadbackSubtreeSurroundsLayer_GL_Bitmap) {
    365   scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
    366       gfx::Rect(0, 0, 200, 200), SK_ColorWHITE);
    367 
    368   scoped_refptr<SolidColorLayer> target = CreateSolidColorLayer(
    369       gfx::Rect(100, 100, 100, 100), SK_ColorRED);
    370   background->AddChild(target);
    371 
    372   scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
    373       gfx::Rect(-100, -100, 300, 300), SK_ColorGREEN);
    374   target->AddChild(green);
    375 
    376   scoped_refptr<SolidColorLayer> blue = CreateSolidColorLayer(
    377       gfx::Rect(50, 50, 50, 50), SK_ColorBLUE);
    378   target->AddChild(blue);
    379 
    380   copy_subrect_ = gfx::Rect(0, 0, 100, 100);
    381   RunPixelTestWithReadbackTarget(GL_WITH_BITMAP,
    382                                  background,
    383                                  target.get(),
    384                                  base::FilePath(FILE_PATH_LITERAL(
    385                                      "green_small_with_blue_corner.png")));
    386 }
    387 
    388 TEST_F(LayerTreeHostReadbackPixelTest,
    389        ReadbackSubtreeSurroundsTargetLayer_GL) {
    390   scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
    391       gfx::Rect(0, 0, 200, 200), SK_ColorWHITE);
    392 
    393   scoped_refptr<SolidColorLayer> target = CreateSolidColorLayer(
    394       gfx::Rect(100, 100, 100, 100), SK_ColorRED);
    395   background->AddChild(target);
    396 
    397   scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
    398       gfx::Rect(-100, -100, 300, 300), SK_ColorGREEN);
    399   target->AddChild(green);
    400 
    401   scoped_refptr<SolidColorLayer> blue = CreateSolidColorLayer(
    402       gfx::Rect(50, 50, 50, 50), SK_ColorBLUE);
    403   target->AddChild(blue);
    404 
    405   copy_subrect_ = gfx::Rect(0, 0, 100, 100);
    406   RunPixelTestWithReadbackTarget(GL_WITH_DEFAULT,
    407                                  background,
    408                                  target.get(),
    409                                  base::FilePath(FILE_PATH_LITERAL(
    410                                      "green_small_with_blue_corner.png")));
    411 }
    412 
    413 TEST_F(LayerTreeHostReadbackPixelTest,
    414        ReadbackSubtreeExtendsBeyondTargetLayer_Software) {
    415   scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
    416       gfx::Rect(0, 0, 200, 200), SK_ColorWHITE);
    417 
    418   scoped_refptr<SolidColorLayer> target = CreateSolidColorLayer(
    419       gfx::Rect(50, 50, 150, 150), SK_ColorRED);
    420   background->AddChild(target);
    421 
    422   scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
    423       gfx::Rect(50, 50, 200, 200), SK_ColorGREEN);
    424   target->AddChild(green);
    425 
    426   scoped_refptr<SolidColorLayer> blue = CreateSolidColorLayer(
    427       gfx::Rect(100, 100, 50, 50), SK_ColorBLUE);
    428   target->AddChild(blue);
    429 
    430   copy_subrect_ = gfx::Rect(50, 50, 100, 100);
    431   RunPixelTestWithReadbackTarget(SOFTWARE_WITH_DEFAULT,
    432                                  background,
    433                                  target.get(),
    434                                  base::FilePath(FILE_PATH_LITERAL(
    435                                      "green_small_with_blue_corner.png")));
    436 }
    437 
    438 TEST_F(LayerTreeHostReadbackPixelTest,
    439        ReadbackSubtreeExtendsBeyondTargetLayer_GL_Bitmap) {
    440   scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
    441       gfx::Rect(0, 0, 200, 200), SK_ColorWHITE);
    442 
    443   scoped_refptr<SolidColorLayer> target = CreateSolidColorLayer(
    444       gfx::Rect(50, 50, 150, 150), SK_ColorRED);
    445   background->AddChild(target);
    446 
    447   scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
    448       gfx::Rect(50, 50, 200, 200), SK_ColorGREEN);
    449   target->AddChild(green);
    450 
    451   scoped_refptr<SolidColorLayer> blue = CreateSolidColorLayer(
    452       gfx::Rect(100, 100, 50, 50), SK_ColorBLUE);
    453   target->AddChild(blue);
    454 
    455   copy_subrect_ = gfx::Rect(50, 50, 100, 100);
    456   RunPixelTestWithReadbackTarget(GL_WITH_BITMAP,
    457                                  background,
    458                                  target.get(),
    459                                  base::FilePath(FILE_PATH_LITERAL(
    460                                      "green_small_with_blue_corner.png")));
    461 }
    462 
    463 TEST_F(LayerTreeHostReadbackPixelTest,
    464        ReadbackSubtreeExtendsBeyondTargetLayer_GL) {
    465   scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
    466       gfx::Rect(0, 0, 200, 200), SK_ColorWHITE);
    467 
    468   scoped_refptr<SolidColorLayer> target = CreateSolidColorLayer(
    469       gfx::Rect(50, 50, 150, 150), SK_ColorRED);
    470   background->AddChild(target);
    471 
    472   scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
    473       gfx::Rect(50, 50, 200, 200), SK_ColorGREEN);
    474   target->AddChild(green);
    475 
    476   scoped_refptr<SolidColorLayer> blue = CreateSolidColorLayer(
    477       gfx::Rect(100, 100, 50, 50), SK_ColorBLUE);
    478   target->AddChild(blue);
    479 
    480   copy_subrect_ = gfx::Rect(50, 50, 100, 100);
    481   RunPixelTestWithReadbackTarget(GL_WITH_DEFAULT,
    482                                  background,
    483                                  target.get(),
    484                                  base::FilePath(FILE_PATH_LITERAL(
    485                                      "green_small_with_blue_corner.png")));
    486 }
    487 
    488 TEST_F(LayerTreeHostReadbackPixelTest, ReadbackSubrect_Software) {
    489   scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
    490       gfx::Rect(200, 200), SK_ColorWHITE);
    491 
    492   scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
    493       gfx::Rect(200, 200), SK_ColorGREEN);
    494   background->AddChild(green);
    495 
    496   scoped_refptr<SolidColorLayer> blue = CreateSolidColorLayer(
    497       gfx::Rect(100, 100, 50, 50), SK_ColorBLUE);
    498   green->AddChild(blue);
    499 
    500   // Grab the middle of the root layer.
    501   copy_subrect_ = gfx::Rect(50, 50, 100, 100);
    502 
    503   RunPixelTest(SOFTWARE_WITH_DEFAULT,
    504                background,
    505                base::FilePath(FILE_PATH_LITERAL(
    506                    "green_small_with_blue_corner.png")));
    507 }
    508 
    509 TEST_F(LayerTreeHostReadbackPixelTest, ReadbackSubrect_GL_Bitmap) {
    510   scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
    511       gfx::Rect(200, 200), SK_ColorWHITE);
    512 
    513   scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
    514       gfx::Rect(200, 200), SK_ColorGREEN);
    515   background->AddChild(green);
    516 
    517   scoped_refptr<SolidColorLayer> blue = CreateSolidColorLayer(
    518       gfx::Rect(100, 100, 50, 50), SK_ColorBLUE);
    519   green->AddChild(blue);
    520 
    521   // Grab the middle of the root layer.
    522   copy_subrect_ = gfx::Rect(50, 50, 100, 100);
    523 
    524   RunPixelTest(GL_WITH_BITMAP,
    525                background,
    526                base::FilePath(FILE_PATH_LITERAL(
    527                    "green_small_with_blue_corner.png")));
    528 }
    529 
    530 TEST_F(LayerTreeHostReadbackPixelTest, ReadbackSubrect_GL) {
    531   scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
    532       gfx::Rect(200, 200), SK_ColorWHITE);
    533 
    534   scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
    535       gfx::Rect(200, 200), SK_ColorGREEN);
    536   background->AddChild(green);
    537 
    538   scoped_refptr<SolidColorLayer> blue = CreateSolidColorLayer(
    539       gfx::Rect(100, 100, 50, 50), SK_ColorBLUE);
    540   green->AddChild(blue);
    541 
    542   // Grab the middle of the root layer.
    543   copy_subrect_ = gfx::Rect(50, 50, 100, 100);
    544 
    545   RunPixelTest(GL_WITH_DEFAULT,
    546                background,
    547                base::FilePath(FILE_PATH_LITERAL(
    548                    "green_small_with_blue_corner.png")));
    549 }
    550 
    551 TEST_F(LayerTreeHostReadbackPixelTest, ReadbackNonRootLayerSubrect_Software) {
    552   scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
    553       gfx::Rect(200, 200), SK_ColorWHITE);
    554 
    555   scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
    556       gfx::Rect(25, 25, 150, 150), SK_ColorGREEN);
    557   background->AddChild(green);
    558 
    559   scoped_refptr<SolidColorLayer> blue = CreateSolidColorLayer(
    560       gfx::Rect(75, 75, 50, 50), SK_ColorBLUE);
    561   green->AddChild(blue);
    562 
    563   // Grab the middle of the green layer.
    564   copy_subrect_ = gfx::Rect(25, 25, 100, 100);
    565 
    566   RunPixelTestWithReadbackTarget(SOFTWARE_WITH_DEFAULT,
    567                                  background,
    568                                  green.get(),
    569                                  base::FilePath(FILE_PATH_LITERAL(
    570                                      "green_small_with_blue_corner.png")));
    571 }
    572 
    573 TEST_F(LayerTreeHostReadbackPixelTest, ReadbackNonRootLayerSubrect_GL_Bitmap) {
    574   scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
    575       gfx::Rect(200, 200), SK_ColorWHITE);
    576 
    577   scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
    578       gfx::Rect(25, 25, 150, 150), SK_ColorGREEN);
    579   background->AddChild(green);
    580 
    581   scoped_refptr<SolidColorLayer> blue = CreateSolidColorLayer(
    582       gfx::Rect(75, 75, 50, 50), SK_ColorBLUE);
    583   green->AddChild(blue);
    584 
    585   // Grab the middle of the green layer.
    586   copy_subrect_ = gfx::Rect(25, 25, 100, 100);
    587 
    588   RunPixelTestWithReadbackTarget(GL_WITH_BITMAP,
    589                                  background,
    590                                  green.get(),
    591                                  base::FilePath(FILE_PATH_LITERAL(
    592                                      "green_small_with_blue_corner.png")));
    593 }
    594 
    595 TEST_F(LayerTreeHostReadbackPixelTest, ReadbackNonRootLayerSubrect_GL) {
    596   scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
    597       gfx::Rect(200, 200), SK_ColorWHITE);
    598 
    599   scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
    600       gfx::Rect(25, 25, 150, 150), SK_ColorGREEN);
    601   background->AddChild(green);
    602 
    603   scoped_refptr<SolidColorLayer> blue = CreateSolidColorLayer(
    604       gfx::Rect(75, 75, 50, 50), SK_ColorBLUE);
    605   green->AddChild(blue);
    606 
    607   // Grab the middle of the green layer.
    608   copy_subrect_ = gfx::Rect(25, 25, 100, 100);
    609 
    610   RunPixelTestWithReadbackTarget(GL_WITH_DEFAULT,
    611                                  background,
    612                                  green.get(),
    613                                  base::FilePath(FILE_PATH_LITERAL(
    614                                      "green_small_with_blue_corner.png")));
    615 }
    616 
    617 class LayerTreeHostReadbackDeviceScalePixelTest
    618     : public LayerTreeHostReadbackPixelTest {
    619  protected:
    620   LayerTreeHostReadbackDeviceScalePixelTest()
    621       : device_scale_factor_(1.f),
    622         white_client_(SK_ColorWHITE),
    623         green_client_(SK_ColorGREEN),
    624         blue_client_(SK_ColorBLUE) {}
    625 
    626   virtual void InitializeSettings(LayerTreeSettings* settings) OVERRIDE {
    627     // Cause the device scale factor to be inherited by contents scales.
    628     settings->layer_transforms_should_scale_layer_contents = true;
    629   }
    630 
    631   virtual void SetupTree() OVERRIDE {
    632     layer_tree_host()->SetDeviceScaleFactor(device_scale_factor_);
    633     LayerTreePixelTest::SetupTree();
    634   }
    635 
    636   virtual void DrawLayersOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
    637     LayerImpl* root_impl = host_impl->active_tree()->root_layer();
    638 
    639     LayerImpl* background_impl = root_impl->children()[0];
    640     EXPECT_EQ(device_scale_factor_, background_impl->contents_scale_x());
    641     EXPECT_EQ(device_scale_factor_, background_impl->contents_scale_y());
    642 
    643     LayerImpl* green_impl = background_impl->children()[0];
    644     EXPECT_EQ(device_scale_factor_, green_impl->contents_scale_x());
    645     EXPECT_EQ(device_scale_factor_, green_impl->contents_scale_y());
    646 
    647     LayerImpl* blue_impl = green_impl->children()[0];
    648     EXPECT_EQ(device_scale_factor_, blue_impl->contents_scale_x());
    649     EXPECT_EQ(device_scale_factor_, blue_impl->contents_scale_y());
    650   }
    651 
    652   float device_scale_factor_;
    653   SolidColorContentLayerClient white_client_;
    654   SolidColorContentLayerClient green_client_;
    655   SolidColorContentLayerClient blue_client_;
    656 };
    657 
    658 TEST_F(LayerTreeHostReadbackDeviceScalePixelTest,
    659        ReadbackSubrect_Software) {
    660   scoped_refptr<ContentLayer> background = ContentLayer::Create(&white_client_);
    661   background->SetAnchorPoint(gfx::PointF());
    662   background->SetBounds(gfx::Size(100, 100));
    663   background->SetIsDrawable(true);
    664 
    665   scoped_refptr<ContentLayer> green = ContentLayer::Create(&green_client_);
    666   green->SetAnchorPoint(gfx::PointF());
    667   green->SetBounds(gfx::Size(100, 100));
    668   green->SetIsDrawable(true);
    669   background->AddChild(green);
    670 
    671   scoped_refptr<ContentLayer> blue = ContentLayer::Create(&blue_client_);
    672   blue->SetAnchorPoint(gfx::PointF());
    673   blue->SetPosition(gfx::Point(50, 50));
    674   blue->SetBounds(gfx::Size(25, 25));
    675   blue->SetIsDrawable(true);
    676   green->AddChild(blue);
    677 
    678   // Grab the middle of the root layer.
    679   copy_subrect_ = gfx::Rect(25, 25, 50, 50);
    680   device_scale_factor_ = 2.f;
    681 
    682   this->impl_side_painting_ = false;
    683   RunPixelTest(SOFTWARE_WITH_DEFAULT,
    684                background,
    685                base::FilePath(FILE_PATH_LITERAL(
    686                    "green_small_with_blue_corner.png")));
    687 }
    688 
    689 TEST_F(LayerTreeHostReadbackDeviceScalePixelTest,
    690        ReadbackSubrect_GL) {
    691   scoped_refptr<ContentLayer> background = ContentLayer::Create(&white_client_);
    692   background->SetAnchorPoint(gfx::PointF());
    693   background->SetBounds(gfx::Size(100, 100));
    694   background->SetIsDrawable(true);
    695 
    696   scoped_refptr<ContentLayer> green = ContentLayer::Create(&green_client_);
    697   green->SetAnchorPoint(gfx::PointF());
    698   green->SetBounds(gfx::Size(100, 100));
    699   green->SetIsDrawable(true);
    700   background->AddChild(green);
    701 
    702   scoped_refptr<ContentLayer> blue = ContentLayer::Create(&blue_client_);
    703   blue->SetAnchorPoint(gfx::PointF());
    704   blue->SetPosition(gfx::Point(50, 50));
    705   blue->SetBounds(gfx::Size(25, 25));
    706   blue->SetIsDrawable(true);
    707   green->AddChild(blue);
    708 
    709   // Grab the middle of the root layer.
    710   copy_subrect_ = gfx::Rect(25, 25, 50, 50);
    711   device_scale_factor_ = 2.f;
    712 
    713   this->impl_side_painting_ = false;
    714   RunPixelTest(GL_WITH_DEFAULT,
    715                background,
    716                base::FilePath(FILE_PATH_LITERAL(
    717                    "green_small_with_blue_corner.png")));
    718 }
    719 
    720 TEST_F(LayerTreeHostReadbackDeviceScalePixelTest,
    721        ReadbackNonRootLayerSubrect_Software) {
    722   scoped_refptr<ContentLayer> background = ContentLayer::Create(&white_client_);
    723   background->SetAnchorPoint(gfx::PointF());
    724   background->SetBounds(gfx::Size(100, 100));
    725   background->SetIsDrawable(true);
    726 
    727   scoped_refptr<ContentLayer> green = ContentLayer::Create(&green_client_);
    728   green->SetAnchorPoint(gfx::PointF());
    729   green->SetPosition(gfx::Point(10, 20));
    730   green->SetBounds(gfx::Size(90, 80));
    731   green->SetIsDrawable(true);
    732   background->AddChild(green);
    733 
    734   scoped_refptr<ContentLayer> blue = ContentLayer::Create(&blue_client_);
    735   blue->SetAnchorPoint(gfx::PointF());
    736   blue->SetPosition(gfx::Point(50, 50));
    737   blue->SetBounds(gfx::Size(25, 25));
    738   blue->SetIsDrawable(true);
    739   green->AddChild(blue);
    740 
    741   // Grab the green layer's content with blue in the bottom right.
    742   copy_subrect_ = gfx::Rect(25, 25, 50, 50);
    743   device_scale_factor_ = 2.f;
    744 
    745   this->impl_side_painting_ = false;
    746   RunPixelTestWithReadbackTarget(SOFTWARE_WITH_DEFAULT,
    747                                  background,
    748                                  green.get(),
    749                                  base::FilePath(FILE_PATH_LITERAL(
    750                                      "green_small_with_blue_corner.png")));
    751 }
    752 
    753 TEST_F(LayerTreeHostReadbackDeviceScalePixelTest,
    754        ReadbackNonRootLayerSubrect_GL) {
    755   scoped_refptr<ContentLayer> background = ContentLayer::Create(&white_client_);
    756   background->SetAnchorPoint(gfx::PointF());
    757   background->SetBounds(gfx::Size(100, 100));
    758   background->SetIsDrawable(true);
    759 
    760   scoped_refptr<ContentLayer> green = ContentLayer::Create(&green_client_);
    761   green->SetAnchorPoint(gfx::PointF());
    762   green->SetPosition(gfx::Point(10, 20));
    763   green->SetBounds(gfx::Size(90, 80));
    764   green->SetIsDrawable(true);
    765   background->AddChild(green);
    766 
    767   scoped_refptr<ContentLayer> blue = ContentLayer::Create(&blue_client_);
    768   blue->SetAnchorPoint(gfx::PointF());
    769   blue->SetPosition(gfx::Point(50, 50));
    770   blue->SetBounds(gfx::Size(25, 25));
    771   blue->SetIsDrawable(true);
    772   green->AddChild(blue);
    773 
    774   // Grab the green layer's content with blue in the bottom right.
    775   copy_subrect_ = gfx::Rect(25, 25, 50, 50);
    776   device_scale_factor_ = 2.f;
    777 
    778   this->impl_side_painting_ = false;
    779   RunPixelTestWithReadbackTarget(GL_WITH_DEFAULT,
    780                                  background,
    781                                  green.get(),
    782                                  base::FilePath(FILE_PATH_LITERAL(
    783                                      "green_small_with_blue_corner.png")));
    784 }
    785 
    786 class LayerTreeHostReadbackViaCompositeAndReadbackPixelTest
    787     : public LayerTreePixelTest {
    788  protected:
    789   LayerTreeHostReadbackViaCompositeAndReadbackPixelTest()
    790       : device_scale_factor_(1.f),
    791         white_client_(SK_ColorWHITE),
    792         green_client_(SK_ColorGREEN),
    793         blue_client_(SK_ColorBLUE) {}
    794 
    795   virtual void InitializeSettings(LayerTreeSettings* settings) OVERRIDE {
    796     // Cause the device scale factor to be inherited by contents scales.
    797     settings->layer_transforms_should_scale_layer_contents = true;
    798   }
    799 
    800   virtual void SetupTree() OVERRIDE {
    801     layer_tree_host()->SetDeviceScaleFactor(device_scale_factor_);
    802     LayerTreePixelTest::SetupTree();
    803   }
    804 
    805   virtual void BeginTest() OVERRIDE {
    806     EXPECT_EQ(device_scale_factor_, layer_tree_host()->device_scale_factor());
    807     if (TestEnded())
    808       return;
    809 
    810     gfx::Rect device_viewport_copy_rect(
    811         layer_tree_host()->device_viewport_size());
    812     if (!device_viewport_copy_subrect_.IsEmpty())
    813       device_viewport_copy_rect.Intersect(device_viewport_copy_subrect_);
    814 
    815     scoped_ptr<SkBitmap> bitmap(new SkBitmap);
    816     bitmap->setConfig(SkBitmap::kARGB_8888_Config,
    817                       device_viewport_copy_rect.width(),
    818                       device_viewport_copy_rect.height());
    819     bitmap->allocPixels();
    820     {
    821       scoped_ptr<SkAutoLockPixels> lock(new SkAutoLockPixels(*bitmap));
    822       layer_tree_host()->CompositeAndReadback(bitmap->getPixels(),
    823                                               device_viewport_copy_rect);
    824     }
    825 
    826     result_bitmap_ = bitmap.Pass();
    827     EndTest();
    828   }
    829 
    830   virtual void DrawLayersOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
    831     LayerImpl* root_impl = host_impl->active_tree()->root_layer();
    832 
    833     LayerImpl* background_impl = root_impl->children()[0];
    834     EXPECT_EQ(device_scale_factor_, background_impl->contents_scale_x());
    835     EXPECT_EQ(device_scale_factor_, background_impl->contents_scale_y());
    836 
    837     LayerImpl* green_impl = background_impl->children()[0];
    838     EXPECT_EQ(device_scale_factor_, green_impl->contents_scale_x());
    839     EXPECT_EQ(device_scale_factor_, green_impl->contents_scale_y());
    840 
    841     LayerImpl* blue_impl = green_impl->children()[0];
    842     EXPECT_EQ(device_scale_factor_, blue_impl->contents_scale_x());
    843     EXPECT_EQ(device_scale_factor_, blue_impl->contents_scale_y());
    844   }
    845 
    846   gfx::Rect device_viewport_copy_subrect_;
    847   float device_scale_factor_;
    848   SolidColorContentLayerClient white_client_;
    849   SolidColorContentLayerClient green_client_;
    850   SolidColorContentLayerClient blue_client_;
    851 };
    852 
    853 TEST_F(LayerTreeHostReadbackViaCompositeAndReadbackPixelTest,
    854        CompositeAndReadback_Software_1) {
    855   scoped_refptr<ContentLayer> background = ContentLayer::Create(&white_client_);
    856   background->SetAnchorPoint(gfx::PointF());
    857   background->SetBounds(gfx::Size(200, 200));
    858   background->SetIsDrawable(true);
    859 
    860   scoped_refptr<ContentLayer> green = ContentLayer::Create(&green_client_);
    861   green->SetAnchorPoint(gfx::PointF());
    862   green->SetBounds(gfx::Size(200, 200));
    863   green->SetIsDrawable(true);
    864   background->AddChild(green);
    865 
    866   scoped_refptr<ContentLayer> blue = ContentLayer::Create(&blue_client_);
    867   blue->SetAnchorPoint(gfx::PointF());
    868   blue->SetPosition(gfx::Point(100, 100));
    869   blue->SetBounds(gfx::Size(50, 50));
    870   blue->SetIsDrawable(true);
    871   green->AddChild(blue);
    872 
    873   // Grab the middle of the device viewport.
    874   device_viewport_copy_subrect_ = gfx::Rect(50, 50, 100, 100);
    875   device_scale_factor_ = 1.f;
    876 
    877   this->impl_side_painting_ = false;
    878   RunPixelTestWithReadbackTarget(SOFTWARE_WITH_DEFAULT,
    879                                  background,
    880                                  green.get(),
    881                                  base::FilePath(FILE_PATH_LITERAL(
    882                                      "green_small_with_blue_corner.png")));
    883 }
    884 
    885 TEST_F(LayerTreeHostReadbackViaCompositeAndReadbackPixelTest,
    886        CompositeAndReadback_Software_2) {
    887   scoped_refptr<ContentLayer> background = ContentLayer::Create(&white_client_);
    888   background->SetAnchorPoint(gfx::PointF());
    889   background->SetBounds(gfx::Size(100, 100));
    890   background->SetIsDrawable(true);
    891 
    892   scoped_refptr<ContentLayer> green = ContentLayer::Create(&green_client_);
    893   green->SetAnchorPoint(gfx::PointF());
    894   green->SetBounds(gfx::Size(100, 100));
    895   green->SetIsDrawable(true);
    896   background->AddChild(green);
    897 
    898   scoped_refptr<ContentLayer> blue = ContentLayer::Create(&blue_client_);
    899   blue->SetAnchorPoint(gfx::PointF());
    900   blue->SetPosition(gfx::Point(50, 50));
    901   blue->SetBounds(gfx::Size(25, 25));
    902   blue->SetIsDrawable(true);
    903   green->AddChild(blue);
    904 
    905   // Grab the middle of the device viewport.
    906   device_viewport_copy_subrect_ = gfx::Rect(50, 50, 100, 100);
    907   device_scale_factor_ = 2.f;
    908 
    909   this->impl_side_painting_ = false;
    910   RunPixelTestWithReadbackTarget(SOFTWARE_WITH_DEFAULT,
    911                                  background,
    912                                  green.get(),
    913                                  base::FilePath(FILE_PATH_LITERAL(
    914                                      "green_small_with_blue_corner.png")));
    915 }
    916 
    917 TEST_F(LayerTreeHostReadbackViaCompositeAndReadbackPixelTest,
    918        CompositeAndReadback_GL_1) {
    919   scoped_refptr<ContentLayer> background = ContentLayer::Create(&white_client_);
    920   background->SetAnchorPoint(gfx::PointF());
    921   background->SetBounds(gfx::Size(200, 200));
    922   background->SetIsDrawable(true);
    923 
    924   scoped_refptr<ContentLayer> green = ContentLayer::Create(&green_client_);
    925   green->SetAnchorPoint(gfx::PointF());
    926   green->SetBounds(gfx::Size(200, 200));
    927   green->SetIsDrawable(true);
    928   background->AddChild(green);
    929 
    930   scoped_refptr<ContentLayer> blue = ContentLayer::Create(&blue_client_);
    931   blue->SetAnchorPoint(gfx::PointF());
    932   blue->SetPosition(gfx::Point(100, 100));
    933   blue->SetBounds(gfx::Size(50, 50));
    934   blue->SetIsDrawable(true);
    935   green->AddChild(blue);
    936 
    937   // Grab the middle of the device viewport.
    938   device_viewport_copy_subrect_ = gfx::Rect(50, 50, 100, 100);
    939   device_scale_factor_ = 1.f;
    940 
    941   this->impl_side_painting_ = false;
    942   RunPixelTestWithReadbackTarget(GL_WITH_DEFAULT,
    943                                  background,
    944                                  green.get(),
    945                                  base::FilePath(FILE_PATH_LITERAL(
    946                                      "green_small_with_blue_corner.png")));
    947 }
    948 
    949 TEST_F(LayerTreeHostReadbackViaCompositeAndReadbackPixelTest,
    950        CompositeAndReadback_GL_2) {
    951   scoped_refptr<ContentLayer> background = ContentLayer::Create(&white_client_);
    952   background->SetAnchorPoint(gfx::PointF());
    953   background->SetBounds(gfx::Size(100, 100));
    954   background->SetIsDrawable(true);
    955 
    956   scoped_refptr<ContentLayer> green = ContentLayer::Create(&green_client_);
    957   green->SetAnchorPoint(gfx::PointF());
    958   green->SetBounds(gfx::Size(100, 100));
    959   green->SetIsDrawable(true);
    960   background->AddChild(green);
    961 
    962   scoped_refptr<ContentLayer> blue = ContentLayer::Create(&blue_client_);
    963   blue->SetAnchorPoint(gfx::PointF());
    964   blue->SetPosition(gfx::Point(50, 50));
    965   blue->SetBounds(gfx::Size(25, 25));
    966   blue->SetIsDrawable(true);
    967   green->AddChild(blue);
    968 
    969   // Grab the middle of the device viewport.
    970   device_viewport_copy_subrect_ = gfx::Rect(50, 50, 100, 100);
    971   device_scale_factor_ = 2.f;
    972 
    973   this->impl_side_painting_ = false;
    974   RunPixelTestWithReadbackTarget(GL_WITH_DEFAULT,
    975                                  background,
    976                                  green.get(),
    977                                  base::FilePath(FILE_PATH_LITERAL(
    978                                      "green_small_with_blue_corner.png")));
    979 }
    980 
    981 TEST_F(LayerTreeHostReadbackPixelTest, ReadbackNonRootLayerOutsideViewport) {
    982   scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
    983       gfx::Rect(200, 200), SK_ColorWHITE);
    984 
    985   scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
    986       gfx::Rect(200, 200), SK_ColorGREEN);
    987   // Only the top left quarter of the layer is inside the viewport, so the
    988   // blue layer is entirely outside.
    989   green->SetPosition(gfx::Point(100, 100));
    990   background->AddChild(green);
    991 
    992   scoped_refptr<SolidColorLayer> blue = CreateSolidColorLayer(
    993       gfx::Rect(150, 150, 50, 50), SK_ColorBLUE);
    994   green->AddChild(blue);
    995 
    996   RunPixelTestWithReadbackTarget(GL_WITH_DEFAULT,
    997                                  background,
    998                                  green.get(),
    999                                  base::FilePath(FILE_PATH_LITERAL(
   1000                                      "green_with_blue_corner.png")));
   1001 }
   1002 
   1003 }  // namespace
   1004 }  // namespace cc
   1005 
   1006 #endif  // OS_ANDROID
   1007