Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / cc / trees / layer_tree_host_pixeltest_readback.cc
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   LayerTreeHostReadbackPixelTest()
24       : insert_copy_request_after_frame_count_(0) {}
25
26   virtual scoped_ptr<CopyOutputRequest> CreateCopyOutputRequest() OVERRIDE {
27     scoped_ptr<CopyOutputRequest> request;
28
29     switch (test_type_) {
30       case GL_WITH_BITMAP:
31       case SOFTWARE_WITH_BITMAP:
32         request = CopyOutputRequest::CreateBitmapRequest(
33             base::Bind(&LayerTreeHostReadbackPixelTest::ReadbackResultAsBitmap,
34                        base::Unretained(this)));
35         break;
36       case SOFTWARE_WITH_DEFAULT:
37         request = CopyOutputRequest::CreateRequest(
38             base::Bind(&LayerTreeHostReadbackPixelTest::ReadbackResultAsBitmap,
39                        base::Unretained(this)));
40         break;
41       case GL_WITH_DEFAULT:
42         request = CopyOutputRequest::CreateRequest(
43             base::Bind(&LayerTreeHostReadbackPixelTest::ReadbackResultAsTexture,
44                        base::Unretained(this)));
45         break;
46     }
47
48     if (!copy_subrect_.IsEmpty())
49       request->set_area(copy_subrect_);
50     return request.Pass();
51   }
52
53   virtual void BeginTest() OVERRIDE {
54     if (insert_copy_request_after_frame_count_ == 0) {
55       Layer* const target =
56           readback_target_ ? readback_target_ : layer_tree_host()->root_layer();
57       target->RequestCopyOfOutput(CreateCopyOutputRequest().Pass());
58     }
59     PostSetNeedsCommitToMainThread();
60   }
61
62   virtual void DidCommitAndDrawFrame() OVERRIDE {
63     if (insert_copy_request_after_frame_count_ ==
64         layer_tree_host()->source_frame_number()) {
65       Layer* const target =
66           readback_target_ ? readback_target_ : layer_tree_host()->root_layer();
67       target->RequestCopyOfOutput(CreateCopyOutputRequest().Pass());
68     }
69   }
70
71   void ReadbackResultAsBitmap(scoped_ptr<CopyOutputResult> result) {
72     EXPECT_TRUE(proxy()->IsMainThread());
73     EXPECT_TRUE(result->HasBitmap());
74     result_bitmap_ = result->TakeBitmap().Pass();
75     EndTest();
76   }
77
78   void ReadbackResultAsTexture(scoped_ptr<CopyOutputResult> result) {
79     EXPECT_TRUE(proxy()->IsMainThread());
80     EXPECT_TRUE(result->HasTexture());
81
82     TextureMailbox texture_mailbox;
83     scoped_ptr<SingleReleaseCallback> release_callback;
84     result->TakeTexture(&texture_mailbox, &release_callback);
85     EXPECT_TRUE(texture_mailbox.IsValid());
86     EXPECT_TRUE(texture_mailbox.IsTexture());
87
88     scoped_ptr<SkBitmap> bitmap =
89         CopyTextureMailboxToBitmap(result->size(), texture_mailbox);
90     release_callback->Run(0, false);
91
92     ReadbackResultAsBitmap(CopyOutputResult::CreateBitmapResult(bitmap.Pass()));
93   }
94
95   gfx::Rect copy_subrect_;
96   int insert_copy_request_after_frame_count_;
97 };
98
99 void IgnoreReadbackResult(scoped_ptr<CopyOutputResult> result) {}
100
101 TEST_F(LayerTreeHostReadbackPixelTest, ReadbackRootLayer_Software) {
102   scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
103       gfx::Rect(200, 200), SK_ColorWHITE);
104
105   scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
106       gfx::Rect(200, 200), SK_ColorGREEN);
107   background->AddChild(green);
108
109   RunPixelTest(SOFTWARE_WITH_DEFAULT,
110                background,
111                base::FilePath(FILE_PATH_LITERAL(
112                    "green.png")));
113 }
114
115 TEST_F(LayerTreeHostReadbackPixelTest, ReadbackRootLayer_Software_Bitmap) {
116   scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
117       gfx::Rect(200, 200), SK_ColorWHITE);
118
119   scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
120       gfx::Rect(200, 200), SK_ColorGREEN);
121   background->AddChild(green);
122
123   RunPixelTest(SOFTWARE_WITH_BITMAP,
124                background,
125                base::FilePath(FILE_PATH_LITERAL(
126                    "green.png")));
127 }
128
129 TEST_F(LayerTreeHostReadbackPixelTest, ReadbackRootLayer_GL_Bitmap) {
130   scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
131       gfx::Rect(200, 200), SK_ColorWHITE);
132
133   scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
134       gfx::Rect(200, 200), SK_ColorGREEN);
135   background->AddChild(green);
136
137   RunPixelTest(GL_WITH_BITMAP,
138                background,
139                base::FilePath(FILE_PATH_LITERAL(
140                    "green.png")));
141 }
142
143 TEST_F(LayerTreeHostReadbackPixelTest, ReadbackRootLayer_GL) {
144   scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
145       gfx::Rect(200, 200), SK_ColorWHITE);
146
147   scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
148       gfx::Rect(200, 200), SK_ColorGREEN);
149   background->AddChild(green);
150
151   RunPixelTest(GL_WITH_DEFAULT,
152                background,
153                base::FilePath(FILE_PATH_LITERAL(
154                    "green.png")));
155 }
156
157 TEST_F(LayerTreeHostReadbackPixelTest,
158        ReadbackRootLayerWithChild_Software) {
159   scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
160       gfx::Rect(200, 200), SK_ColorWHITE);
161
162   scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
163       gfx::Rect(200, 200), SK_ColorGREEN);
164   background->AddChild(green);
165
166   scoped_refptr<SolidColorLayer> blue = CreateSolidColorLayer(
167       gfx::Rect(150, 150, 50, 50), SK_ColorBLUE);
168   green->AddChild(blue);
169
170   RunPixelTest(SOFTWARE_WITH_DEFAULT,
171                background,
172                base::FilePath(FILE_PATH_LITERAL(
173                    "green_with_blue_corner.png")));
174 }
175
176 TEST_F(LayerTreeHostReadbackPixelTest, ReadbackRootLayerWithChild_GL_Bitmap) {
177   scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
178       gfx::Rect(200, 200), SK_ColorWHITE);
179
180   scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
181       gfx::Rect(200, 200), SK_ColorGREEN);
182   background->AddChild(green);
183
184   scoped_refptr<SolidColorLayer> blue = CreateSolidColorLayer(
185       gfx::Rect(150, 150, 50, 50), SK_ColorBLUE);
186   green->AddChild(blue);
187
188   RunPixelTest(GL_WITH_BITMAP,
189                background,
190                base::FilePath(FILE_PATH_LITERAL(
191                    "green_with_blue_corner.png")));
192 }
193
194 TEST_F(LayerTreeHostReadbackPixelTest, ReadbackRootLayerWithChild_GL) {
195   scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
196       gfx::Rect(200, 200), SK_ColorWHITE);
197
198   scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
199       gfx::Rect(200, 200), SK_ColorGREEN);
200   background->AddChild(green);
201
202   scoped_refptr<SolidColorLayer> blue = CreateSolidColorLayer(
203       gfx::Rect(150, 150, 50, 50), SK_ColorBLUE);
204   green->AddChild(blue);
205
206   RunPixelTest(GL_WITH_DEFAULT,
207                background,
208                base::FilePath(FILE_PATH_LITERAL(
209                    "green_with_blue_corner.png")));
210 }
211
212 TEST_F(LayerTreeHostReadbackPixelTest, ReadbackNonRootLayer_Software) {
213   scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
214       gfx::Rect(200, 200), SK_ColorWHITE);
215
216   scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
217       gfx::Rect(200, 200), SK_ColorGREEN);
218   background->AddChild(green);
219
220   RunPixelTestWithReadbackTarget(SOFTWARE_WITH_DEFAULT,
221                                  background,
222                                  green.get(),
223                                  base::FilePath(FILE_PATH_LITERAL(
224                                      "green.png")));
225 }
226
227 TEST_F(LayerTreeHostReadbackPixelTest, ReadbackNonRootLayer_GL_Bitmap) {
228   scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
229       gfx::Rect(200, 200), SK_ColorWHITE);
230
231   scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
232       gfx::Rect(200, 200), SK_ColorGREEN);
233   background->AddChild(green);
234
235   RunPixelTestWithReadbackTarget(GL_WITH_BITMAP,
236                                  background,
237                                  green.get(),
238                                  base::FilePath(FILE_PATH_LITERAL(
239                                      "green.png")));
240 }
241
242 TEST_F(LayerTreeHostReadbackPixelTest, ReadbackNonRootLayer_GL) {
243   scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
244       gfx::Rect(200, 200), SK_ColorWHITE);
245
246   scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
247       gfx::Rect(200, 200), SK_ColorGREEN);
248   background->AddChild(green);
249
250   RunPixelTestWithReadbackTarget(GL_WITH_DEFAULT,
251                                  background,
252                                  green.get(),
253                                  base::FilePath(FILE_PATH_LITERAL(
254                                      "green.png")));
255 }
256
257 TEST_F(LayerTreeHostReadbackPixelTest,
258        ReadbackSmallNonRootLayer_Software) {
259   scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
260       gfx::Rect(200, 200), SK_ColorWHITE);
261
262   scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
263       gfx::Rect(100, 100, 100, 100), SK_ColorGREEN);
264   background->AddChild(green);
265
266   RunPixelTestWithReadbackTarget(SOFTWARE_WITH_DEFAULT,
267                                  background,
268                                  green.get(),
269                                  base::FilePath(FILE_PATH_LITERAL(
270                                      "green_small.png")));
271 }
272
273 TEST_F(LayerTreeHostReadbackPixelTest, ReadbackSmallNonRootLayer_GL_Bitmap) {
274   scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
275       gfx::Rect(200, 200), SK_ColorWHITE);
276
277   scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
278       gfx::Rect(100, 100, 100, 100), SK_ColorGREEN);
279   background->AddChild(green);
280
281   RunPixelTestWithReadbackTarget(GL_WITH_BITMAP,
282                                  background,
283                                  green.get(),
284                                  base::FilePath(FILE_PATH_LITERAL(
285                                      "green_small.png")));
286 }
287
288 TEST_F(LayerTreeHostReadbackPixelTest, ReadbackSmallNonRootLayer_GL) {
289   scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
290       gfx::Rect(200, 200), SK_ColorWHITE);
291
292   scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
293       gfx::Rect(100, 100, 100, 100), SK_ColorGREEN);
294   background->AddChild(green);
295
296   RunPixelTestWithReadbackTarget(GL_WITH_DEFAULT,
297                                  background,
298                                  green.get(),
299                                  base::FilePath(FILE_PATH_LITERAL(
300                                      "green_small.png")));
301 }
302
303 TEST_F(LayerTreeHostReadbackPixelTest,
304        ReadbackSmallNonRootLayerWithChild_Software) {
305   scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
306       gfx::Rect(200, 200), SK_ColorWHITE);
307
308   scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
309       gfx::Rect(100, 100, 100, 100), SK_ColorGREEN);
310   background->AddChild(green);
311
312   scoped_refptr<SolidColorLayer> blue = CreateSolidColorLayer(
313       gfx::Rect(50, 50, 50, 50), SK_ColorBLUE);
314   green->AddChild(blue);
315
316   RunPixelTestWithReadbackTarget(SOFTWARE_WITH_DEFAULT,
317                                  background,
318                                  green.get(),
319                                  base::FilePath(FILE_PATH_LITERAL(
320                                      "green_small_with_blue_corner.png")));
321 }
322
323 TEST_F(LayerTreeHostReadbackPixelTest,
324        ReadbackSmallNonRootLayerWithChild_GL_Bitmap) {
325   scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
326       gfx::Rect(200, 200), SK_ColorWHITE);
327
328   scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
329       gfx::Rect(100, 100, 100, 100), SK_ColorGREEN);
330   background->AddChild(green);
331
332   scoped_refptr<SolidColorLayer> blue = CreateSolidColorLayer(
333       gfx::Rect(50, 50, 50, 50), SK_ColorBLUE);
334   green->AddChild(blue);
335
336   RunPixelTestWithReadbackTarget(GL_WITH_BITMAP,
337                                  background,
338                                  green.get(),
339                                  base::FilePath(FILE_PATH_LITERAL(
340                                      "green_small_with_blue_corner.png")));
341 }
342
343 TEST_F(LayerTreeHostReadbackPixelTest, ReadbackSmallNonRootLayerWithChild_GL) {
344   scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
345       gfx::Rect(200, 200), SK_ColorWHITE);
346
347   scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
348       gfx::Rect(100, 100, 100, 100), SK_ColorGREEN);
349   background->AddChild(green);
350
351   scoped_refptr<SolidColorLayer> blue = CreateSolidColorLayer(
352       gfx::Rect(50, 50, 50, 50), SK_ColorBLUE);
353   green->AddChild(blue);
354
355   RunPixelTestWithReadbackTarget(GL_WITH_DEFAULT,
356                                  background,
357                                  green.get(),
358                                  base::FilePath(FILE_PATH_LITERAL(
359                                      "green_small_with_blue_corner.png")));
360 }
361
362 TEST_F(LayerTreeHostReadbackPixelTest,
363        ReadbackSubtreeSurroundsTargetLayer_Software) {
364   scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
365       gfx::Rect(0, 0, 200, 200), SK_ColorWHITE);
366
367   scoped_refptr<SolidColorLayer> target = CreateSolidColorLayer(
368       gfx::Rect(100, 100, 100, 100), SK_ColorRED);
369   background->AddChild(target);
370
371   scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
372       gfx::Rect(-100, -100, 300, 300), SK_ColorGREEN);
373   target->AddChild(green);
374
375   scoped_refptr<SolidColorLayer> blue = CreateSolidColorLayer(
376       gfx::Rect(50, 50, 50, 50), SK_ColorBLUE);
377   target->AddChild(blue);
378
379   copy_subrect_ = gfx::Rect(0, 0, 100, 100);
380   RunPixelTestWithReadbackTarget(SOFTWARE_WITH_DEFAULT,
381                                  background,
382                                  target.get(),
383                                  base::FilePath(FILE_PATH_LITERAL(
384                                      "green_small_with_blue_corner.png")));
385 }
386
387 TEST_F(LayerTreeHostReadbackPixelTest,
388        ReadbackSubtreeSurroundsLayer_GL_Bitmap) {
389   scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
390       gfx::Rect(0, 0, 200, 200), SK_ColorWHITE);
391
392   scoped_refptr<SolidColorLayer> target = CreateSolidColorLayer(
393       gfx::Rect(100, 100, 100, 100), SK_ColorRED);
394   background->AddChild(target);
395
396   scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
397       gfx::Rect(-100, -100, 300, 300), SK_ColorGREEN);
398   target->AddChild(green);
399
400   scoped_refptr<SolidColorLayer> blue = CreateSolidColorLayer(
401       gfx::Rect(50, 50, 50, 50), SK_ColorBLUE);
402   target->AddChild(blue);
403
404   copy_subrect_ = gfx::Rect(0, 0, 100, 100);
405   RunPixelTestWithReadbackTarget(GL_WITH_BITMAP,
406                                  background,
407                                  target.get(),
408                                  base::FilePath(FILE_PATH_LITERAL(
409                                      "green_small_with_blue_corner.png")));
410 }
411
412 TEST_F(LayerTreeHostReadbackPixelTest,
413        ReadbackSubtreeSurroundsTargetLayer_GL) {
414   scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
415       gfx::Rect(0, 0, 200, 200), SK_ColorWHITE);
416
417   scoped_refptr<SolidColorLayer> target = CreateSolidColorLayer(
418       gfx::Rect(100, 100, 100, 100), SK_ColorRED);
419   background->AddChild(target);
420
421   scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
422       gfx::Rect(-100, -100, 300, 300), SK_ColorGREEN);
423   target->AddChild(green);
424
425   scoped_refptr<SolidColorLayer> blue = CreateSolidColorLayer(
426       gfx::Rect(50, 50, 50, 50), SK_ColorBLUE);
427   target->AddChild(blue);
428
429   copy_subrect_ = gfx::Rect(0, 0, 100, 100);
430   RunPixelTestWithReadbackTarget(GL_WITH_DEFAULT,
431                                  background,
432                                  target.get(),
433                                  base::FilePath(FILE_PATH_LITERAL(
434                                      "green_small_with_blue_corner.png")));
435 }
436
437 TEST_F(LayerTreeHostReadbackPixelTest,
438        ReadbackSubtreeExtendsBeyondTargetLayer_Software) {
439   scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
440       gfx::Rect(0, 0, 200, 200), SK_ColorWHITE);
441
442   scoped_refptr<SolidColorLayer> target = CreateSolidColorLayer(
443       gfx::Rect(50, 50, 150, 150), SK_ColorRED);
444   background->AddChild(target);
445
446   scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
447       gfx::Rect(50, 50, 200, 200), SK_ColorGREEN);
448   target->AddChild(green);
449
450   scoped_refptr<SolidColorLayer> blue = CreateSolidColorLayer(
451       gfx::Rect(100, 100, 50, 50), SK_ColorBLUE);
452   target->AddChild(blue);
453
454   copy_subrect_ = gfx::Rect(50, 50, 100, 100);
455   RunPixelTestWithReadbackTarget(SOFTWARE_WITH_DEFAULT,
456                                  background,
457                                  target.get(),
458                                  base::FilePath(FILE_PATH_LITERAL(
459                                      "green_small_with_blue_corner.png")));
460 }
461
462 TEST_F(LayerTreeHostReadbackPixelTest,
463        ReadbackSubtreeExtendsBeyondTargetLayer_GL_Bitmap) {
464   scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
465       gfx::Rect(0, 0, 200, 200), SK_ColorWHITE);
466
467   scoped_refptr<SolidColorLayer> target = CreateSolidColorLayer(
468       gfx::Rect(50, 50, 150, 150), SK_ColorRED);
469   background->AddChild(target);
470
471   scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
472       gfx::Rect(50, 50, 200, 200), SK_ColorGREEN);
473   target->AddChild(green);
474
475   scoped_refptr<SolidColorLayer> blue = CreateSolidColorLayer(
476       gfx::Rect(100, 100, 50, 50), SK_ColorBLUE);
477   target->AddChild(blue);
478
479   copy_subrect_ = gfx::Rect(50, 50, 100, 100);
480   RunPixelTestWithReadbackTarget(GL_WITH_BITMAP,
481                                  background,
482                                  target.get(),
483                                  base::FilePath(FILE_PATH_LITERAL(
484                                      "green_small_with_blue_corner.png")));
485 }
486
487 TEST_F(LayerTreeHostReadbackPixelTest,
488        ReadbackSubtreeExtendsBeyondTargetLayer_GL) {
489   scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
490       gfx::Rect(0, 0, 200, 200), SK_ColorWHITE);
491
492   scoped_refptr<SolidColorLayer> target = CreateSolidColorLayer(
493       gfx::Rect(50, 50, 150, 150), SK_ColorRED);
494   background->AddChild(target);
495
496   scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
497       gfx::Rect(50, 50, 200, 200), SK_ColorGREEN);
498   target->AddChild(green);
499
500   scoped_refptr<SolidColorLayer> blue = CreateSolidColorLayer(
501       gfx::Rect(100, 100, 50, 50), SK_ColorBLUE);
502   target->AddChild(blue);
503
504   copy_subrect_ = gfx::Rect(50, 50, 100, 100);
505   RunPixelTestWithReadbackTarget(GL_WITH_DEFAULT,
506                                  background,
507                                  target.get(),
508                                  base::FilePath(FILE_PATH_LITERAL(
509                                      "green_small_with_blue_corner.png")));
510 }
511
512 TEST_F(LayerTreeHostReadbackPixelTest, ReadbackHiddenSubtree_Software) {
513   scoped_refptr<SolidColorLayer> background =
514       CreateSolidColorLayer(gfx::Rect(200, 200), SK_ColorBLACK);
515
516   scoped_refptr<SolidColorLayer> hidden_target =
517       CreateSolidColorLayer(gfx::Rect(200, 200), SK_ColorGREEN);
518   hidden_target->SetHideLayerAndSubtree(true);
519   background->AddChild(hidden_target);
520
521   scoped_refptr<SolidColorLayer> blue =
522       CreateSolidColorLayer(gfx::Rect(150, 150, 50, 50), SK_ColorBLUE);
523   hidden_target->AddChild(blue);
524
525   RunPixelTestWithReadbackTarget(
526       SOFTWARE_WITH_DEFAULT,
527       background,
528       hidden_target.get(),
529       base::FilePath(FILE_PATH_LITERAL("green_with_blue_corner.png")));
530 }
531
532 TEST_F(LayerTreeHostReadbackPixelTest, ReadbackHiddenSubtree_GL_Bitmap) {
533   scoped_refptr<SolidColorLayer> background =
534       CreateSolidColorLayer(gfx::Rect(200, 200), SK_ColorBLACK);
535
536   scoped_refptr<SolidColorLayer> hidden_target =
537       CreateSolidColorLayer(gfx::Rect(200, 200), SK_ColorGREEN);
538   hidden_target->SetHideLayerAndSubtree(true);
539   background->AddChild(hidden_target);
540
541   scoped_refptr<SolidColorLayer> blue =
542       CreateSolidColorLayer(gfx::Rect(150, 150, 50, 50), SK_ColorBLUE);
543   hidden_target->AddChild(blue);
544
545   RunPixelTestWithReadbackTarget(
546       GL_WITH_BITMAP,
547       background,
548       hidden_target.get(),
549       base::FilePath(FILE_PATH_LITERAL("green_with_blue_corner.png")));
550 }
551
552 TEST_F(LayerTreeHostReadbackPixelTest, ReadbackHiddenSubtree_GL) {
553   scoped_refptr<SolidColorLayer> background =
554       CreateSolidColorLayer(gfx::Rect(200, 200), SK_ColorBLACK);
555
556   scoped_refptr<SolidColorLayer> hidden_target =
557       CreateSolidColorLayer(gfx::Rect(200, 200), SK_ColorGREEN);
558   hidden_target->SetHideLayerAndSubtree(true);
559   background->AddChild(hidden_target);
560
561   scoped_refptr<SolidColorLayer> blue =
562       CreateSolidColorLayer(gfx::Rect(150, 150, 50, 50), SK_ColorBLUE);
563   hidden_target->AddChild(blue);
564
565   RunPixelTestWithReadbackTarget(
566       GL_WITH_DEFAULT,
567       background,
568       hidden_target.get(),
569       base::FilePath(FILE_PATH_LITERAL("green_with_blue_corner.png")));
570 }
571
572 TEST_F(LayerTreeHostReadbackPixelTest,
573        HiddenSubtreeNotVisibleWhenDrawnForReadback_Software) {
574   scoped_refptr<SolidColorLayer> background =
575       CreateSolidColorLayer(gfx::Rect(200, 200), SK_ColorBLACK);
576
577   scoped_refptr<SolidColorLayer> hidden_target =
578       CreateSolidColorLayer(gfx::Rect(200, 200), SK_ColorGREEN);
579   hidden_target->SetHideLayerAndSubtree(true);
580   background->AddChild(hidden_target);
581
582   scoped_refptr<SolidColorLayer> blue =
583       CreateSolidColorLayer(gfx::Rect(150, 150, 50, 50), SK_ColorBLUE);
584   hidden_target->AddChild(blue);
585
586   hidden_target->RequestCopyOfOutput(CopyOutputRequest::CreateBitmapRequest(
587       base::Bind(&IgnoreReadbackResult)));
588   RunPixelTest(SOFTWARE_WITH_DEFAULT,
589                background,
590                base::FilePath(FILE_PATH_LITERAL("black.png")));
591 }
592
593 TEST_F(LayerTreeHostReadbackPixelTest,
594        HiddenSubtreeNotVisibleWhenDrawnForReadback_GL_Bitmap) {
595   scoped_refptr<SolidColorLayer> background =
596       CreateSolidColorLayer(gfx::Rect(200, 200), SK_ColorBLACK);
597
598   scoped_refptr<SolidColorLayer> hidden_target =
599       CreateSolidColorLayer(gfx::Rect(200, 200), SK_ColorGREEN);
600   hidden_target->SetHideLayerAndSubtree(true);
601   background->AddChild(hidden_target);
602
603   scoped_refptr<SolidColorLayer> blue =
604       CreateSolidColorLayer(gfx::Rect(150, 150, 50, 50), SK_ColorBLUE);
605   hidden_target->AddChild(blue);
606
607   hidden_target->RequestCopyOfOutput(CopyOutputRequest::CreateBitmapRequest(
608       base::Bind(&IgnoreReadbackResult)));
609   RunPixelTest(GL_WITH_BITMAP,
610                background,
611                base::FilePath(FILE_PATH_LITERAL("black.png")));
612 }
613
614 TEST_F(LayerTreeHostReadbackPixelTest,
615        HiddenSubtreeNotVisibleWhenDrawnForReadback_GL) {
616   scoped_refptr<SolidColorLayer> background =
617       CreateSolidColorLayer(gfx::Rect(200, 200), SK_ColorBLACK);
618
619   scoped_refptr<SolidColorLayer> hidden_target =
620       CreateSolidColorLayer(gfx::Rect(200, 200), SK_ColorGREEN);
621   hidden_target->SetHideLayerAndSubtree(true);
622   background->AddChild(hidden_target);
623
624   scoped_refptr<SolidColorLayer> blue =
625       CreateSolidColorLayer(gfx::Rect(150, 150, 50, 50), SK_ColorBLUE);
626   hidden_target->AddChild(blue);
627
628   hidden_target->RequestCopyOfOutput(CopyOutputRequest::CreateBitmapRequest(
629       base::Bind(&IgnoreReadbackResult)));
630   RunPixelTest(GL_WITH_DEFAULT,
631                background,
632                base::FilePath(FILE_PATH_LITERAL("black.png")));
633 }
634
635 TEST_F(LayerTreeHostReadbackPixelTest, ReadbackSubrect_Software) {
636   scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
637       gfx::Rect(200, 200), SK_ColorWHITE);
638
639   scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
640       gfx::Rect(200, 200), SK_ColorGREEN);
641   background->AddChild(green);
642
643   scoped_refptr<SolidColorLayer> blue = CreateSolidColorLayer(
644       gfx::Rect(100, 100, 50, 50), SK_ColorBLUE);
645   green->AddChild(blue);
646
647   // Grab the middle of the root layer.
648   copy_subrect_ = gfx::Rect(50, 50, 100, 100);
649
650   RunPixelTest(SOFTWARE_WITH_DEFAULT,
651                background,
652                base::FilePath(FILE_PATH_LITERAL(
653                    "green_small_with_blue_corner.png")));
654 }
655
656 TEST_F(LayerTreeHostReadbackPixelTest, ReadbackSubrect_GL_Bitmap) {
657   scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
658       gfx::Rect(200, 200), SK_ColorWHITE);
659
660   scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
661       gfx::Rect(200, 200), SK_ColorGREEN);
662   background->AddChild(green);
663
664   scoped_refptr<SolidColorLayer> blue = CreateSolidColorLayer(
665       gfx::Rect(100, 100, 50, 50), SK_ColorBLUE);
666   green->AddChild(blue);
667
668   // Grab the middle of the root layer.
669   copy_subrect_ = gfx::Rect(50, 50, 100, 100);
670
671   RunPixelTest(GL_WITH_BITMAP,
672                background,
673                base::FilePath(FILE_PATH_LITERAL(
674                    "green_small_with_blue_corner.png")));
675 }
676
677 TEST_F(LayerTreeHostReadbackPixelTest, ReadbackSubrect_GL) {
678   scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
679       gfx::Rect(200, 200), SK_ColorWHITE);
680
681   scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
682       gfx::Rect(200, 200), SK_ColorGREEN);
683   background->AddChild(green);
684
685   scoped_refptr<SolidColorLayer> blue = CreateSolidColorLayer(
686       gfx::Rect(100, 100, 50, 50), SK_ColorBLUE);
687   green->AddChild(blue);
688
689   // Grab the middle of the root layer.
690   copy_subrect_ = gfx::Rect(50, 50, 100, 100);
691
692   RunPixelTest(GL_WITH_DEFAULT,
693                background,
694                base::FilePath(FILE_PATH_LITERAL(
695                    "green_small_with_blue_corner.png")));
696 }
697
698 TEST_F(LayerTreeHostReadbackPixelTest, ReadbackNonRootLayerSubrect_Software) {
699   scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
700       gfx::Rect(200, 200), SK_ColorWHITE);
701
702   scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
703       gfx::Rect(25, 25, 150, 150), SK_ColorGREEN);
704   background->AddChild(green);
705
706   scoped_refptr<SolidColorLayer> blue = CreateSolidColorLayer(
707       gfx::Rect(75, 75, 50, 50), SK_ColorBLUE);
708   green->AddChild(blue);
709
710   // Grab the middle of the green layer.
711   copy_subrect_ = gfx::Rect(25, 25, 100, 100);
712
713   RunPixelTestWithReadbackTarget(SOFTWARE_WITH_DEFAULT,
714                                  background,
715                                  green.get(),
716                                  base::FilePath(FILE_PATH_LITERAL(
717                                      "green_small_with_blue_corner.png")));
718 }
719
720 TEST_F(LayerTreeHostReadbackPixelTest, ReadbackNonRootLayerSubrect_GL_Bitmap) {
721   scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
722       gfx::Rect(200, 200), SK_ColorWHITE);
723
724   scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
725       gfx::Rect(25, 25, 150, 150), SK_ColorGREEN);
726   background->AddChild(green);
727
728   scoped_refptr<SolidColorLayer> blue = CreateSolidColorLayer(
729       gfx::Rect(75, 75, 50, 50), SK_ColorBLUE);
730   green->AddChild(blue);
731
732   // Grab the middle of the green layer.
733   copy_subrect_ = gfx::Rect(25, 25, 100, 100);
734
735   RunPixelTestWithReadbackTarget(GL_WITH_BITMAP,
736                                  background,
737                                  green.get(),
738                                  base::FilePath(FILE_PATH_LITERAL(
739                                      "green_small_with_blue_corner.png")));
740 }
741
742 TEST_F(LayerTreeHostReadbackPixelTest, ReadbackNonRootLayerSubrect_GL) {
743   scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
744       gfx::Rect(200, 200), SK_ColorWHITE);
745
746   scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
747       gfx::Rect(25, 25, 150, 150), SK_ColorGREEN);
748   background->AddChild(green);
749
750   scoped_refptr<SolidColorLayer> blue = CreateSolidColorLayer(
751       gfx::Rect(75, 75, 50, 50), SK_ColorBLUE);
752   green->AddChild(blue);
753
754   // Grab the middle of the green layer.
755   copy_subrect_ = gfx::Rect(25, 25, 100, 100);
756
757   RunPixelTestWithReadbackTarget(GL_WITH_DEFAULT,
758                                  background,
759                                  green.get(),
760                                  base::FilePath(FILE_PATH_LITERAL(
761                                      "green_small_with_blue_corner.png")));
762 }
763
764 TEST_F(LayerTreeHostReadbackPixelTest, ReadbackWhenNoDamage_Software) {
765   scoped_refptr<SolidColorLayer> background =
766       CreateSolidColorLayer(gfx::Rect(0, 0, 200, 200), SK_ColorWHITE);
767
768   scoped_refptr<SolidColorLayer> parent =
769       CreateSolidColorLayer(gfx::Rect(0, 0, 150, 150), SK_ColorRED);
770   background->AddChild(parent);
771
772   scoped_refptr<SolidColorLayer> target =
773       CreateSolidColorLayer(gfx::Rect(0, 0, 100, 100), SK_ColorGREEN);
774   parent->AddChild(target);
775
776   scoped_refptr<SolidColorLayer> blue =
777       CreateSolidColorLayer(gfx::Rect(50, 50, 50, 50), SK_ColorBLUE);
778   target->AddChild(blue);
779
780   insert_copy_request_after_frame_count_ = 1;
781   RunPixelTestWithReadbackTarget(
782       SOFTWARE_WITH_DEFAULT,
783       background,
784       target.get(),
785       base::FilePath(FILE_PATH_LITERAL("green_small_with_blue_corner.png")));
786 }
787
788 TEST_F(LayerTreeHostReadbackPixelTest, ReadbackWhenNoDamage_GL_Bitmap) {
789   scoped_refptr<SolidColorLayer> background =
790       CreateSolidColorLayer(gfx::Rect(0, 0, 200, 200), SK_ColorWHITE);
791
792   scoped_refptr<SolidColorLayer> parent =
793       CreateSolidColorLayer(gfx::Rect(0, 0, 150, 150), SK_ColorRED);
794   background->AddChild(parent);
795
796   scoped_refptr<SolidColorLayer> target =
797       CreateSolidColorLayer(gfx::Rect(0, 0, 100, 100), SK_ColorGREEN);
798   parent->AddChild(target);
799
800   scoped_refptr<SolidColorLayer> blue =
801       CreateSolidColorLayer(gfx::Rect(50, 50, 50, 50), SK_ColorBLUE);
802   target->AddChild(blue);
803
804   insert_copy_request_after_frame_count_ = 1;
805   RunPixelTestWithReadbackTarget(
806       GL_WITH_BITMAP,
807       background,
808       target.get(),
809       base::FilePath(FILE_PATH_LITERAL("green_small_with_blue_corner.png")));
810 }
811
812 TEST_F(LayerTreeHostReadbackPixelTest, ReadbackWhenNoDamage_GL) {
813   scoped_refptr<SolidColorLayer> background =
814       CreateSolidColorLayer(gfx::Rect(0, 0, 200, 200), SK_ColorWHITE);
815
816   scoped_refptr<SolidColorLayer> parent =
817       CreateSolidColorLayer(gfx::Rect(0, 0, 150, 150), SK_ColorRED);
818   background->AddChild(parent);
819
820   scoped_refptr<SolidColorLayer> target =
821       CreateSolidColorLayer(gfx::Rect(0, 0, 100, 100), SK_ColorGREEN);
822   parent->AddChild(target);
823
824   scoped_refptr<SolidColorLayer> blue =
825       CreateSolidColorLayer(gfx::Rect(50, 50, 50, 50), SK_ColorBLUE);
826   target->AddChild(blue);
827
828   insert_copy_request_after_frame_count_ = 1;
829   RunPixelTestWithReadbackTarget(
830       GL_WITH_DEFAULT,
831       background,
832       target.get(),
833       base::FilePath(FILE_PATH_LITERAL("green_small_with_blue_corner.png")));
834 }
835
836 TEST_F(LayerTreeHostReadbackPixelTest,
837        ReadbackOutsideViewportWhenNoDamage_Software) {
838   scoped_refptr<SolidColorLayer> background =
839       CreateSolidColorLayer(gfx::Rect(0, 0, 200, 200), SK_ColorWHITE);
840
841   scoped_refptr<SolidColorLayer> parent =
842       CreateSolidColorLayer(gfx::Rect(0, 0, 200, 200), SK_ColorRED);
843   EXPECT_FALSE(parent->masks_to_bounds());
844   background->AddChild(parent);
845
846   scoped_refptr<SolidColorLayer> target =
847       CreateSolidColorLayer(gfx::Rect(250, 250, 100, 100), SK_ColorGREEN);
848   parent->AddChild(target);
849
850   scoped_refptr<SolidColorLayer> blue =
851       CreateSolidColorLayer(gfx::Rect(50, 50, 50, 50), SK_ColorBLUE);
852   target->AddChild(blue);
853
854   insert_copy_request_after_frame_count_ = 1;
855   RunPixelTestWithReadbackTarget(
856       SOFTWARE_WITH_DEFAULT,
857       background,
858       target.get(),
859       base::FilePath(FILE_PATH_LITERAL("green_small_with_blue_corner.png")));
860 }
861
862 TEST_F(LayerTreeHostReadbackPixelTest,
863        ReadbackOutsideViewportWhenNoDamage_GL_Bitmap) {
864   scoped_refptr<SolidColorLayer> background =
865       CreateSolidColorLayer(gfx::Rect(0, 0, 200, 200), SK_ColorWHITE);
866
867   scoped_refptr<SolidColorLayer> parent =
868       CreateSolidColorLayer(gfx::Rect(0, 0, 200, 200), SK_ColorRED);
869   EXPECT_FALSE(parent->masks_to_bounds());
870   background->AddChild(parent);
871
872   scoped_refptr<SolidColorLayer> target =
873       CreateSolidColorLayer(gfx::Rect(250, 250, 100, 100), SK_ColorGREEN);
874   parent->AddChild(target);
875
876   scoped_refptr<SolidColorLayer> blue =
877       CreateSolidColorLayer(gfx::Rect(50, 50, 50, 50), SK_ColorBLUE);
878   target->AddChild(blue);
879
880   insert_copy_request_after_frame_count_ = 1;
881   RunPixelTestWithReadbackTarget(
882       GL_WITH_BITMAP,
883       background,
884       target.get(),
885       base::FilePath(FILE_PATH_LITERAL("green_small_with_blue_corner.png")));
886 }
887
888 TEST_F(LayerTreeHostReadbackPixelTest, ReadbackOutsideViewportWhenNoDamage_GL) {
889   scoped_refptr<SolidColorLayer> background =
890       CreateSolidColorLayer(gfx::Rect(0, 0, 200, 200), SK_ColorWHITE);
891
892   scoped_refptr<SolidColorLayer> parent =
893       CreateSolidColorLayer(gfx::Rect(0, 0, 200, 200), SK_ColorRED);
894   EXPECT_FALSE(parent->masks_to_bounds());
895   background->AddChild(parent);
896
897   scoped_refptr<SolidColorLayer> target =
898       CreateSolidColorLayer(gfx::Rect(250, 250, 100, 100), SK_ColorGREEN);
899   parent->AddChild(target);
900
901   scoped_refptr<SolidColorLayer> blue =
902       CreateSolidColorLayer(gfx::Rect(50, 50, 50, 50), SK_ColorBLUE);
903   target->AddChild(blue);
904
905   insert_copy_request_after_frame_count_ = 1;
906   RunPixelTestWithReadbackTarget(
907       GL_WITH_DEFAULT,
908       background,
909       target.get(),
910       base::FilePath(FILE_PATH_LITERAL("green_small_with_blue_corner.png")));
911 }
912
913 class LayerTreeHostReadbackDeviceScalePixelTest
914     : public LayerTreeHostReadbackPixelTest {
915  protected:
916   LayerTreeHostReadbackDeviceScalePixelTest()
917       : device_scale_factor_(1.f),
918         white_client_(SK_ColorWHITE),
919         green_client_(SK_ColorGREEN),
920         blue_client_(SK_ColorBLUE) {}
921
922   virtual void InitializeSettings(LayerTreeSettings* settings) OVERRIDE {
923     // Cause the device scale factor to be inherited by contents scales.
924     settings->layer_transforms_should_scale_layer_contents = true;
925   }
926
927   virtual void SetupTree() OVERRIDE {
928     layer_tree_host()->SetDeviceScaleFactor(device_scale_factor_);
929     LayerTreePixelTest::SetupTree();
930   }
931
932   virtual void DrawLayersOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
933     LayerImpl* root_impl = host_impl->active_tree()->root_layer();
934
935     LayerImpl* background_impl = root_impl->children()[0];
936     EXPECT_EQ(device_scale_factor_, background_impl->contents_scale_x());
937     EXPECT_EQ(device_scale_factor_, background_impl->contents_scale_y());
938
939     LayerImpl* green_impl = background_impl->children()[0];
940     EXPECT_EQ(device_scale_factor_, green_impl->contents_scale_x());
941     EXPECT_EQ(device_scale_factor_, green_impl->contents_scale_y());
942
943     LayerImpl* blue_impl = green_impl->children()[0];
944     EXPECT_EQ(device_scale_factor_, blue_impl->contents_scale_x());
945     EXPECT_EQ(device_scale_factor_, blue_impl->contents_scale_y());
946   }
947
948   float device_scale_factor_;
949   SolidColorContentLayerClient white_client_;
950   SolidColorContentLayerClient green_client_;
951   SolidColorContentLayerClient blue_client_;
952 };
953
954 TEST_F(LayerTreeHostReadbackDeviceScalePixelTest,
955        ReadbackSubrect_Software) {
956   scoped_refptr<ContentLayer> background = ContentLayer::Create(&white_client_);
957   background->SetAnchorPoint(gfx::PointF());
958   background->SetBounds(gfx::Size(100, 100));
959   background->SetIsDrawable(true);
960
961   scoped_refptr<ContentLayer> green = ContentLayer::Create(&green_client_);
962   green->SetAnchorPoint(gfx::PointF());
963   green->SetBounds(gfx::Size(100, 100));
964   green->SetIsDrawable(true);
965   background->AddChild(green);
966
967   scoped_refptr<ContentLayer> blue = ContentLayer::Create(&blue_client_);
968   blue->SetAnchorPoint(gfx::PointF());
969   blue->SetPosition(gfx::Point(50, 50));
970   blue->SetBounds(gfx::Size(25, 25));
971   blue->SetIsDrawable(true);
972   green->AddChild(blue);
973
974   // Grab the middle of the root layer.
975   copy_subrect_ = gfx::Rect(25, 25, 50, 50);
976   device_scale_factor_ = 2.f;
977
978   this->impl_side_painting_ = false;
979   RunPixelTest(SOFTWARE_WITH_DEFAULT,
980                background,
981                base::FilePath(FILE_PATH_LITERAL(
982                    "green_small_with_blue_corner.png")));
983 }
984
985 TEST_F(LayerTreeHostReadbackDeviceScalePixelTest,
986        ReadbackSubrect_GL) {
987   scoped_refptr<ContentLayer> background = ContentLayer::Create(&white_client_);
988   background->SetAnchorPoint(gfx::PointF());
989   background->SetBounds(gfx::Size(100, 100));
990   background->SetIsDrawable(true);
991
992   scoped_refptr<ContentLayer> green = ContentLayer::Create(&green_client_);
993   green->SetAnchorPoint(gfx::PointF());
994   green->SetBounds(gfx::Size(100, 100));
995   green->SetIsDrawable(true);
996   background->AddChild(green);
997
998   scoped_refptr<ContentLayer> blue = ContentLayer::Create(&blue_client_);
999   blue->SetAnchorPoint(gfx::PointF());
1000   blue->SetPosition(gfx::Point(50, 50));
1001   blue->SetBounds(gfx::Size(25, 25));
1002   blue->SetIsDrawable(true);
1003   green->AddChild(blue);
1004
1005   // Grab the middle of the root layer.
1006   copy_subrect_ = gfx::Rect(25, 25, 50, 50);
1007   device_scale_factor_ = 2.f;
1008
1009   this->impl_side_painting_ = false;
1010   RunPixelTest(GL_WITH_DEFAULT,
1011                background,
1012                base::FilePath(FILE_PATH_LITERAL(
1013                    "green_small_with_blue_corner.png")));
1014 }
1015
1016 TEST_F(LayerTreeHostReadbackDeviceScalePixelTest,
1017        ReadbackNonRootLayerSubrect_Software) {
1018   scoped_refptr<ContentLayer> background = ContentLayer::Create(&white_client_);
1019   background->SetAnchorPoint(gfx::PointF());
1020   background->SetBounds(gfx::Size(100, 100));
1021   background->SetIsDrawable(true);
1022
1023   scoped_refptr<ContentLayer> green = ContentLayer::Create(&green_client_);
1024   green->SetAnchorPoint(gfx::PointF());
1025   green->SetPosition(gfx::Point(10, 20));
1026   green->SetBounds(gfx::Size(90, 80));
1027   green->SetIsDrawable(true);
1028   background->AddChild(green);
1029
1030   scoped_refptr<ContentLayer> blue = ContentLayer::Create(&blue_client_);
1031   blue->SetAnchorPoint(gfx::PointF());
1032   blue->SetPosition(gfx::Point(50, 50));
1033   blue->SetBounds(gfx::Size(25, 25));
1034   blue->SetIsDrawable(true);
1035   green->AddChild(blue);
1036
1037   // Grab the green layer's content with blue in the bottom right.
1038   copy_subrect_ = gfx::Rect(25, 25, 50, 50);
1039   device_scale_factor_ = 2.f;
1040
1041   this->impl_side_painting_ = false;
1042   RunPixelTestWithReadbackTarget(SOFTWARE_WITH_DEFAULT,
1043                                  background,
1044                                  green.get(),
1045                                  base::FilePath(FILE_PATH_LITERAL(
1046                                      "green_small_with_blue_corner.png")));
1047 }
1048
1049 TEST_F(LayerTreeHostReadbackDeviceScalePixelTest,
1050        ReadbackNonRootLayerSubrect_GL) {
1051   scoped_refptr<ContentLayer> background = ContentLayer::Create(&white_client_);
1052   background->SetAnchorPoint(gfx::PointF());
1053   background->SetBounds(gfx::Size(100, 100));
1054   background->SetIsDrawable(true);
1055
1056   scoped_refptr<ContentLayer> green = ContentLayer::Create(&green_client_);
1057   green->SetAnchorPoint(gfx::PointF());
1058   green->SetPosition(gfx::Point(10, 20));
1059   green->SetBounds(gfx::Size(90, 80));
1060   green->SetIsDrawable(true);
1061   background->AddChild(green);
1062
1063   scoped_refptr<ContentLayer> blue = ContentLayer::Create(&blue_client_);
1064   blue->SetAnchorPoint(gfx::PointF());
1065   blue->SetPosition(gfx::Point(50, 50));
1066   blue->SetBounds(gfx::Size(25, 25));
1067   blue->SetIsDrawable(true);
1068   green->AddChild(blue);
1069
1070   // Grab the green layer's content with blue in the bottom right.
1071   copy_subrect_ = gfx::Rect(25, 25, 50, 50);
1072   device_scale_factor_ = 2.f;
1073
1074   this->impl_side_painting_ = false;
1075   RunPixelTestWithReadbackTarget(GL_WITH_DEFAULT,
1076                                  background,
1077                                  green.get(),
1078                                  base::FilePath(FILE_PATH_LITERAL(
1079                                      "green_small_with_blue_corner.png")));
1080 }
1081
1082 class LayerTreeHostReadbackViaCompositeAndReadbackPixelTest
1083     : public LayerTreePixelTest {
1084  protected:
1085   LayerTreeHostReadbackViaCompositeAndReadbackPixelTest()
1086       : device_scale_factor_(1.f),
1087         white_client_(SK_ColorWHITE),
1088         green_client_(SK_ColorGREEN),
1089         blue_client_(SK_ColorBLUE) {}
1090
1091   virtual void InitializeSettings(LayerTreeSettings* settings) OVERRIDE {
1092     // Cause the device scale factor to be inherited by contents scales.
1093     settings->layer_transforms_should_scale_layer_contents = true;
1094   }
1095
1096   virtual void SetupTree() OVERRIDE {
1097     layer_tree_host()->SetDeviceScaleFactor(device_scale_factor_);
1098     LayerTreePixelTest::SetupTree();
1099   }
1100
1101   virtual void BeginTest() OVERRIDE {
1102     EXPECT_EQ(device_scale_factor_, layer_tree_host()->device_scale_factor());
1103     if (TestEnded())
1104       return;
1105
1106     gfx::Rect device_viewport_copy_rect(
1107         layer_tree_host()->device_viewport_size());
1108     if (!device_viewport_copy_subrect_.IsEmpty())
1109       device_viewport_copy_rect.Intersect(device_viewport_copy_subrect_);
1110
1111     scoped_ptr<SkBitmap> bitmap(new SkBitmap);
1112     bitmap->setConfig(SkBitmap::kARGB_8888_Config,
1113                       device_viewport_copy_rect.width(),
1114                       device_viewport_copy_rect.height());
1115     bitmap->allocPixels();
1116     {
1117       scoped_ptr<SkAutoLockPixels> lock(new SkAutoLockPixels(*bitmap));
1118       layer_tree_host()->CompositeAndReadback(bitmap->getPixels(),
1119                                               device_viewport_copy_rect);
1120     }
1121
1122     result_bitmap_ = bitmap.Pass();
1123     EndTest();
1124   }
1125
1126   virtual void DrawLayersOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
1127     LayerImpl* root_impl = host_impl->active_tree()->root_layer();
1128
1129     LayerImpl* background_impl = root_impl->children()[0];
1130     EXPECT_EQ(device_scale_factor_, background_impl->contents_scale_x());
1131     EXPECT_EQ(device_scale_factor_, background_impl->contents_scale_y());
1132
1133     LayerImpl* green_impl = background_impl->children()[0];
1134     EXPECT_EQ(device_scale_factor_, green_impl->contents_scale_x());
1135     EXPECT_EQ(device_scale_factor_, green_impl->contents_scale_y());
1136
1137     LayerImpl* blue_impl = green_impl->children()[0];
1138     EXPECT_EQ(device_scale_factor_, blue_impl->contents_scale_x());
1139     EXPECT_EQ(device_scale_factor_, blue_impl->contents_scale_y());
1140   }
1141
1142   gfx::Rect device_viewport_copy_subrect_;
1143   float device_scale_factor_;
1144   SolidColorContentLayerClient white_client_;
1145   SolidColorContentLayerClient green_client_;
1146   SolidColorContentLayerClient blue_client_;
1147 };
1148
1149 TEST_F(LayerTreeHostReadbackViaCompositeAndReadbackPixelTest,
1150        CompositeAndReadback_Software_1) {
1151   scoped_refptr<ContentLayer> background = ContentLayer::Create(&white_client_);
1152   background->SetAnchorPoint(gfx::PointF());
1153   background->SetBounds(gfx::Size(200, 200));
1154   background->SetIsDrawable(true);
1155
1156   scoped_refptr<ContentLayer> green = ContentLayer::Create(&green_client_);
1157   green->SetAnchorPoint(gfx::PointF());
1158   green->SetBounds(gfx::Size(200, 200));
1159   green->SetIsDrawable(true);
1160   background->AddChild(green);
1161
1162   scoped_refptr<ContentLayer> blue = ContentLayer::Create(&blue_client_);
1163   blue->SetAnchorPoint(gfx::PointF());
1164   blue->SetPosition(gfx::Point(100, 100));
1165   blue->SetBounds(gfx::Size(50, 50));
1166   blue->SetIsDrawable(true);
1167   green->AddChild(blue);
1168
1169   // Grab the middle of the device viewport.
1170   device_viewport_copy_subrect_ = gfx::Rect(50, 50, 100, 100);
1171   device_scale_factor_ = 1.f;
1172
1173   this->impl_side_painting_ = false;
1174   RunPixelTestWithReadbackTarget(SOFTWARE_WITH_DEFAULT,
1175                                  background,
1176                                  green.get(),
1177                                  base::FilePath(FILE_PATH_LITERAL(
1178                                      "green_small_with_blue_corner.png")));
1179 }
1180
1181 TEST_F(LayerTreeHostReadbackViaCompositeAndReadbackPixelTest,
1182        CompositeAndReadback_Software_2) {
1183   scoped_refptr<ContentLayer> background = ContentLayer::Create(&white_client_);
1184   background->SetAnchorPoint(gfx::PointF());
1185   background->SetBounds(gfx::Size(100, 100));
1186   background->SetIsDrawable(true);
1187
1188   scoped_refptr<ContentLayer> green = ContentLayer::Create(&green_client_);
1189   green->SetAnchorPoint(gfx::PointF());
1190   green->SetBounds(gfx::Size(100, 100));
1191   green->SetIsDrawable(true);
1192   background->AddChild(green);
1193
1194   scoped_refptr<ContentLayer> blue = ContentLayer::Create(&blue_client_);
1195   blue->SetAnchorPoint(gfx::PointF());
1196   blue->SetPosition(gfx::Point(50, 50));
1197   blue->SetBounds(gfx::Size(25, 25));
1198   blue->SetIsDrawable(true);
1199   green->AddChild(blue);
1200
1201   // Grab the middle of the device viewport.
1202   device_viewport_copy_subrect_ = gfx::Rect(50, 50, 100, 100);
1203   device_scale_factor_ = 2.f;
1204
1205   this->impl_side_painting_ = false;
1206   RunPixelTestWithReadbackTarget(SOFTWARE_WITH_DEFAULT,
1207                                  background,
1208                                  green.get(),
1209                                  base::FilePath(FILE_PATH_LITERAL(
1210                                      "green_small_with_blue_corner.png")));
1211 }
1212
1213 TEST_F(LayerTreeHostReadbackViaCompositeAndReadbackPixelTest,
1214        CompositeAndReadback_GL_1) {
1215   scoped_refptr<ContentLayer> background = ContentLayer::Create(&white_client_);
1216   background->SetAnchorPoint(gfx::PointF());
1217   background->SetBounds(gfx::Size(200, 200));
1218   background->SetIsDrawable(true);
1219
1220   scoped_refptr<ContentLayer> green = ContentLayer::Create(&green_client_);
1221   green->SetAnchorPoint(gfx::PointF());
1222   green->SetBounds(gfx::Size(200, 200));
1223   green->SetIsDrawable(true);
1224   background->AddChild(green);
1225
1226   scoped_refptr<ContentLayer> blue = ContentLayer::Create(&blue_client_);
1227   blue->SetAnchorPoint(gfx::PointF());
1228   blue->SetPosition(gfx::Point(100, 100));
1229   blue->SetBounds(gfx::Size(50, 50));
1230   blue->SetIsDrawable(true);
1231   green->AddChild(blue);
1232
1233   // Grab the middle of the device viewport.
1234   device_viewport_copy_subrect_ = gfx::Rect(50, 50, 100, 100);
1235   device_scale_factor_ = 1.f;
1236
1237   this->impl_side_painting_ = false;
1238   RunPixelTestWithReadbackTarget(GL_WITH_DEFAULT,
1239                                  background,
1240                                  green.get(),
1241                                  base::FilePath(FILE_PATH_LITERAL(
1242                                      "green_small_with_blue_corner.png")));
1243 }
1244
1245 TEST_F(LayerTreeHostReadbackViaCompositeAndReadbackPixelTest,
1246        CompositeAndReadback_GL_2) {
1247   scoped_refptr<ContentLayer> background = ContentLayer::Create(&white_client_);
1248   background->SetAnchorPoint(gfx::PointF());
1249   background->SetBounds(gfx::Size(100, 100));
1250   background->SetIsDrawable(true);
1251
1252   scoped_refptr<ContentLayer> green = ContentLayer::Create(&green_client_);
1253   green->SetAnchorPoint(gfx::PointF());
1254   green->SetBounds(gfx::Size(100, 100));
1255   green->SetIsDrawable(true);
1256   background->AddChild(green);
1257
1258   scoped_refptr<ContentLayer> blue = ContentLayer::Create(&blue_client_);
1259   blue->SetAnchorPoint(gfx::PointF());
1260   blue->SetPosition(gfx::Point(50, 50));
1261   blue->SetBounds(gfx::Size(25, 25));
1262   blue->SetIsDrawable(true);
1263   green->AddChild(blue);
1264
1265   // Grab the middle of the device viewport.
1266   device_viewport_copy_subrect_ = gfx::Rect(50, 50, 100, 100);
1267   device_scale_factor_ = 2.f;
1268
1269   this->impl_side_painting_ = false;
1270   RunPixelTestWithReadbackTarget(GL_WITH_DEFAULT,
1271                                  background,
1272                                  green.get(),
1273                                  base::FilePath(FILE_PATH_LITERAL(
1274                                      "green_small_with_blue_corner.png")));
1275 }
1276
1277 TEST_F(LayerTreeHostReadbackPixelTest, ReadbackNonRootLayerOutsideViewport) {
1278   scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
1279       gfx::Rect(200, 200), SK_ColorWHITE);
1280
1281   scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
1282       gfx::Rect(200, 200), SK_ColorGREEN);
1283   // Only the top left quarter of the layer is inside the viewport, so the
1284   // blue layer is entirely outside.
1285   green->SetPosition(gfx::Point(100, 100));
1286   background->AddChild(green);
1287
1288   scoped_refptr<SolidColorLayer> blue = CreateSolidColorLayer(
1289       gfx::Rect(150, 150, 50, 50), SK_ColorBLUE);
1290   green->AddChild(blue);
1291
1292   RunPixelTestWithReadbackTarget(GL_WITH_DEFAULT,
1293                                  background,
1294                                  green.get(),
1295                                  base::FilePath(FILE_PATH_LITERAL(
1296                                      "green_with_blue_corner.png")));
1297 }
1298
1299 }  // namespace
1300 }  // namespace cc
1301
1302 #endif  // OS_ANDROID