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.
5 #include "base/memory/scoped_ptr.h"
6 #include "cc/test/fake_picture_pile_impl.h"
7 #include "cc/test/skia_common.h"
8 #include "skia/ext/refptr.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "third_party/skia/include/core/SkPixelRef.h"
11 #include "third_party/skia/include/core/SkShader.h"
12 #include "ui/gfx/geometry/rect.h"
13 #include "ui/gfx/geometry/size_conversions.h"
18 TEST(PicturePileImplTest
, AnalyzeIsSolidUnscaled
) {
19 gfx::Size
tile_size(100, 100);
20 gfx::Size
layer_bounds(400, 400);
22 scoped_ptr
<FakePicturePile
> recording_source
=
23 FakePicturePile::CreateFilledPile(tile_size
, layer_bounds
);
26 SkColor solid_color
= SkColorSetARGB(255, 12, 23, 34);
27 solid_paint
.setColor(solid_color
);
29 SkColor non_solid_color
= SkColorSetARGB(128, 45, 56, 67);
30 SkPaint non_solid_paint
;
31 non_solid_paint
.setColor(non_solid_color
);
33 recording_source
->add_draw_rect_with_paint(gfx::Rect(0, 0, 400, 400),
35 recording_source
->Rerecord();
37 scoped_refptr
<FakePicturePileImpl
> pile
=
38 FakePicturePileImpl::CreateFromPile(recording_source
.get(), nullptr);
40 // Ensure everything is solid.
41 for (int y
= 0; y
<= 300; y
+= 100) {
42 for (int x
= 0; x
<= 300; x
+= 100) {
43 RasterSource::SolidColorAnalysis analysis
;
44 gfx::Rect
rect(x
, y
, 100, 100);
45 pile
->PerformSolidColorAnalysis(rect
, 1.0, &analysis
);
46 EXPECT_TRUE(analysis
.is_solid_color
) << rect
.ToString();
47 EXPECT_EQ(analysis
.solid_color
, solid_color
) << rect
.ToString();
51 // Add one non-solid pixel and recreate the raster source.
52 recording_source
->add_draw_rect_with_paint(gfx::Rect(50, 50, 1, 1),
54 recording_source
->Rerecord();
55 pile
= FakePicturePileImpl::CreateFromPile(recording_source
.get(), nullptr);
57 RasterSource::SolidColorAnalysis analysis
;
58 pile
->PerformSolidColorAnalysis(gfx::Rect(0, 0, 100, 100), 1.0, &analysis
);
59 EXPECT_FALSE(analysis
.is_solid_color
);
61 pile
->PerformSolidColorAnalysis(gfx::Rect(100, 0, 100, 100), 1.0, &analysis
);
62 EXPECT_TRUE(analysis
.is_solid_color
);
63 EXPECT_EQ(analysis
.solid_color
, solid_color
);
65 // Boundaries should be clipped.
66 analysis
.is_solid_color
= false;
67 pile
->PerformSolidColorAnalysis(gfx::Rect(350, 0, 100, 100), 1.0, &analysis
);
68 EXPECT_TRUE(analysis
.is_solid_color
);
69 EXPECT_EQ(analysis
.solid_color
, solid_color
);
71 analysis
.is_solid_color
= false;
72 pile
->PerformSolidColorAnalysis(gfx::Rect(0, 350, 100, 100), 1.0, &analysis
);
73 EXPECT_TRUE(analysis
.is_solid_color
);
74 EXPECT_EQ(analysis
.solid_color
, solid_color
);
76 analysis
.is_solid_color
= false;
77 pile
->PerformSolidColorAnalysis(gfx::Rect(350, 350, 100, 100), 1.0,
79 EXPECT_TRUE(analysis
.is_solid_color
);
80 EXPECT_EQ(analysis
.solid_color
, solid_color
);
83 TEST(PicturePileImplTest
, AnalyzeIsSolidScaled
) {
84 gfx::Size
tile_size(100, 100);
85 gfx::Size
layer_bounds(400, 400);
87 scoped_ptr
<FakePicturePile
> recording_source
=
88 FakePicturePile::CreateFilledPile(tile_size
, layer_bounds
);
90 SkColor solid_color
= SkColorSetARGB(255, 12, 23, 34);
92 solid_paint
.setColor(solid_color
);
94 SkColor non_solid_color
= SkColorSetARGB(128, 45, 56, 67);
95 SkPaint non_solid_paint
;
96 non_solid_paint
.setColor(non_solid_color
);
98 recording_source
->add_draw_rect_with_paint(gfx::Rect(0, 0, 400, 400),
100 recording_source
->Rerecord();
102 scoped_refptr
<FakePicturePileImpl
> pile
=
103 FakePicturePileImpl::CreateFromPile(recording_source
.get(), nullptr);
105 // Ensure everything is solid.
106 for (int y
= 0; y
<= 30; y
+= 10) {
107 for (int x
= 0; x
<= 30; x
+= 10) {
108 RasterSource::SolidColorAnalysis analysis
;
109 gfx::Rect
rect(x
, y
, 10, 10);
110 pile
->PerformSolidColorAnalysis(rect
, 0.1f
, &analysis
);
111 EXPECT_TRUE(analysis
.is_solid_color
) << rect
.ToString();
112 EXPECT_EQ(analysis
.solid_color
, solid_color
) << rect
.ToString();
116 // Add one non-solid pixel and recreate the raster source.
117 recording_source
->add_draw_rect_with_paint(gfx::Rect(50, 50, 1, 1),
119 recording_source
->Rerecord();
120 pile
= FakePicturePileImpl::CreateFromPile(recording_source
.get(), nullptr);
122 RasterSource::SolidColorAnalysis analysis
;
123 pile
->PerformSolidColorAnalysis(gfx::Rect(0, 0, 10, 10), 0.1f
, &analysis
);
124 EXPECT_FALSE(analysis
.is_solid_color
);
126 pile
->PerformSolidColorAnalysis(gfx::Rect(10, 0, 10, 10), 0.1f
, &analysis
);
127 EXPECT_TRUE(analysis
.is_solid_color
);
128 EXPECT_EQ(analysis
.solid_color
, solid_color
);
130 // Boundaries should be clipped.
131 analysis
.is_solid_color
= false;
132 pile
->PerformSolidColorAnalysis(gfx::Rect(35, 0, 10, 10), 0.1f
, &analysis
);
133 EXPECT_TRUE(analysis
.is_solid_color
);
134 EXPECT_EQ(analysis
.solid_color
, solid_color
);
136 analysis
.is_solid_color
= false;
137 pile
->PerformSolidColorAnalysis(gfx::Rect(0, 35, 10, 10), 0.1f
, &analysis
);
138 EXPECT_TRUE(analysis
.is_solid_color
);
139 EXPECT_EQ(analysis
.solid_color
, solid_color
);
141 analysis
.is_solid_color
= false;
142 pile
->PerformSolidColorAnalysis(gfx::Rect(35, 35, 10, 10), 0.1f
, &analysis
);
143 EXPECT_TRUE(analysis
.is_solid_color
);
144 EXPECT_EQ(analysis
.solid_color
, solid_color
);
147 TEST(PicturePileImplTest
, AnalyzeIsSolidEmpty
) {
148 gfx::Size
tile_size(100, 100);
149 gfx::Size
layer_bounds(400, 400);
151 scoped_refptr
<FakePicturePileImpl
> pile
=
152 FakePicturePileImpl::CreateFilledPile(tile_size
, layer_bounds
);
153 RasterSource::SolidColorAnalysis analysis
;
154 EXPECT_FALSE(analysis
.is_solid_color
);
156 pile
->PerformSolidColorAnalysis(gfx::Rect(0, 0, 400, 400), 1.f
, &analysis
);
158 EXPECT_TRUE(analysis
.is_solid_color
);
159 EXPECT_EQ(analysis
.solid_color
, SkColorSetARGB(0, 0, 0, 0));
162 TEST(PicturePileImplTest
, PixelRefIteratorDiscardableRefsOneTile
) {
163 gfx::Size
tile_size(256, 256);
164 gfx::Size
layer_bounds(512, 512);
166 scoped_ptr
<FakePicturePile
> recording_source
=
167 FakePicturePile::CreateFilledPile(tile_size
, layer_bounds
);
169 SkBitmap discardable_bitmap
[2][2];
170 CreateDiscardableBitmap(gfx::Size(32, 32), &discardable_bitmap
[0][0]);
171 CreateDiscardableBitmap(gfx::Size(32, 32), &discardable_bitmap
[0][1]);
172 CreateDiscardableBitmap(gfx::Size(32, 32), &discardable_bitmap
[1][1]);
174 // Discardable pixel refs are found in the following cells:
180 recording_source
->add_draw_bitmap(discardable_bitmap
[0][0], gfx::Point(0, 0));
181 recording_source
->add_draw_bitmap(discardable_bitmap
[0][1],
183 recording_source
->add_draw_bitmap(discardable_bitmap
[1][1],
184 gfx::Point(260, 260));
185 recording_source
->SetGatherPixelRefs(true);
186 recording_source
->Rerecord();
188 scoped_refptr
<FakePicturePileImpl
> pile
=
189 FakePicturePileImpl::CreateFromPile(recording_source
.get(), nullptr);
191 // Tile sized iterators. These should find only one pixel ref.
193 PicturePileImpl::PixelRefIterator
iterator(
194 gfx::Rect(0, 0, 256, 256), 1.0, pile
.get());
195 EXPECT_TRUE(iterator
);
196 EXPECT_TRUE(iterator
->pixel_ref
== discardable_bitmap
[0][0].pixelRef());
197 EXPECT_EQ(gfx::SkRectToRectF(iterator
->pixel_ref_rect
).ToString(),
198 gfx::RectF(32, 32).ToString());
199 EXPECT_FALSE(++iterator
);
202 PicturePileImpl::PixelRefIterator
iterator(
203 gfx::Rect(0, 0, 512, 512), 2.0, pile
.get());
204 EXPECT_TRUE(iterator
);
205 EXPECT_TRUE(iterator
->pixel_ref
== discardable_bitmap
[0][0].pixelRef());
206 EXPECT_EQ(gfx::SkRectToRectF(iterator
->pixel_ref_rect
).ToString(),
207 gfx::RectF(32, 32).ToString());
208 EXPECT_FALSE(++iterator
);
211 PicturePileImpl::PixelRefIterator
iterator(
212 gfx::Rect(0, 0, 128, 128), 0.5, pile
.get());
213 EXPECT_TRUE(iterator
);
214 EXPECT_TRUE(iterator
->pixel_ref
== discardable_bitmap
[0][0].pixelRef());
215 EXPECT_EQ(gfx::SkRectToRectF(iterator
->pixel_ref_rect
).ToString(),
216 gfx::RectF(32, 32).ToString());
217 EXPECT_FALSE(++iterator
);
219 // Shifted tile sized iterators. These should find only one pixel ref.
221 PicturePileImpl::PixelRefIterator
iterator(
222 gfx::Rect(260, 260, 256, 256), 1.0, pile
.get());
223 EXPECT_TRUE(iterator
);
224 EXPECT_TRUE(iterator
->pixel_ref
== discardable_bitmap
[1][1].pixelRef());
225 EXPECT_EQ(gfx::SkRectToRectF(iterator
->pixel_ref_rect
).ToString(),
226 gfx::RectF(260, 260, 32, 32).ToString());
227 EXPECT_FALSE(++iterator
);
230 PicturePileImpl::PixelRefIterator
iterator(
231 gfx::Rect(520, 520, 512, 512), 2.0, pile
.get());
232 EXPECT_TRUE(iterator
);
233 EXPECT_TRUE(iterator
->pixel_ref
== discardable_bitmap
[1][1].pixelRef());
234 EXPECT_EQ(gfx::SkRectToRectF(iterator
->pixel_ref_rect
).ToString(),
235 gfx::RectF(260, 260, 32, 32).ToString());
236 EXPECT_FALSE(++iterator
);
239 PicturePileImpl::PixelRefIterator
iterator(
240 gfx::Rect(130, 130, 128, 128), 0.5, pile
.get());
241 EXPECT_TRUE(iterator
);
242 EXPECT_TRUE(iterator
->pixel_ref
== discardable_bitmap
[1][1].pixelRef());
243 EXPECT_EQ(gfx::SkRectToRectF(iterator
->pixel_ref_rect
).ToString(),
244 gfx::RectF(260, 260, 32, 32).ToString());
245 EXPECT_FALSE(++iterator
);
247 // Ensure there's no discardable pixel refs in the empty cell
249 PicturePileImpl::PixelRefIterator
iterator(
250 gfx::Rect(0, 256, 256, 256), 1.0, pile
.get());
251 EXPECT_FALSE(iterator
);
253 // Layer sized iterators. These should find three pixel ref.
255 PicturePileImpl::PixelRefIterator
iterator(
256 gfx::Rect(0, 0, 512, 512), 1.0, pile
.get());
257 EXPECT_TRUE(iterator
);
258 EXPECT_TRUE(iterator
->pixel_ref
== discardable_bitmap
[0][0].pixelRef());
259 EXPECT_EQ(gfx::SkRectToRectF(iterator
->pixel_ref_rect
).ToString(),
260 gfx::RectF(32, 32).ToString());
261 EXPECT_TRUE(++iterator
);
262 EXPECT_TRUE(iterator
->pixel_ref
== discardable_bitmap
[0][1].pixelRef());
263 EXPECT_EQ(gfx::SkRectToRectF(iterator
->pixel_ref_rect
).ToString(),
264 gfx::RectF(260, 0, 32, 32).ToString());
265 EXPECT_TRUE(++iterator
);
266 EXPECT_TRUE(iterator
->pixel_ref
== discardable_bitmap
[1][1].pixelRef());
267 EXPECT_EQ(gfx::SkRectToRectF(iterator
->pixel_ref_rect
).ToString(),
268 gfx::RectF(260, 260, 32, 32).ToString());
269 EXPECT_FALSE(++iterator
);
272 PicturePileImpl::PixelRefIterator
iterator(
273 gfx::Rect(0, 0, 1024, 1024), 2.0, pile
.get());
274 EXPECT_TRUE(iterator
);
275 EXPECT_TRUE(iterator
->pixel_ref
== discardable_bitmap
[0][0].pixelRef());
276 EXPECT_EQ(gfx::SkRectToRectF(iterator
->pixel_ref_rect
).ToString(),
277 gfx::RectF(32, 32).ToString());
278 EXPECT_TRUE(++iterator
);
279 EXPECT_TRUE(iterator
->pixel_ref
== discardable_bitmap
[0][1].pixelRef());
280 EXPECT_EQ(gfx::SkRectToRectF(iterator
->pixel_ref_rect
).ToString(),
281 gfx::RectF(260, 0, 32, 32).ToString());
282 EXPECT_TRUE(++iterator
);
283 EXPECT_TRUE(iterator
->pixel_ref
== discardable_bitmap
[1][1].pixelRef());
284 EXPECT_EQ(gfx::SkRectToRectF(iterator
->pixel_ref_rect
).ToString(),
285 gfx::RectF(260, 260, 32, 32).ToString());
286 EXPECT_FALSE(++iterator
);
289 PicturePileImpl::PixelRefIterator
iterator(
290 gfx::Rect(0, 0, 256, 256), 0.5, pile
.get());
291 EXPECT_TRUE(iterator
);
292 EXPECT_TRUE(iterator
->pixel_ref
== discardable_bitmap
[0][0].pixelRef());
293 EXPECT_EQ(gfx::RectF(32, 32).ToString(),
294 gfx::SkRectToRectF(iterator
->pixel_ref_rect
).ToString());
295 EXPECT_TRUE(++iterator
);
296 EXPECT_TRUE(iterator
->pixel_ref
== discardable_bitmap
[0][1].pixelRef());
297 EXPECT_EQ(gfx::RectF(260, 0, 32, 32).ToString(),
298 gfx::SkRectToRectF(iterator
->pixel_ref_rect
).ToString());
299 EXPECT_TRUE(++iterator
);
300 EXPECT_TRUE(iterator
->pixel_ref
== discardable_bitmap
[1][1].pixelRef());
301 EXPECT_EQ(gfx::RectF(260, 260, 32, 32).ToString(),
302 gfx::SkRectToRectF(iterator
->pixel_ref_rect
).ToString());
303 EXPECT_FALSE(++iterator
);
308 PicturePileImpl::PixelRefIterator
iterator(gfx::Rect(0, 0, 512, 512), 1.0,
310 EXPECT_TRUE(iterator
);
311 EXPECT_TRUE(iterator
->pixel_ref
== discardable_bitmap
[0][0].pixelRef());
312 EXPECT_EQ(gfx::RectF(32, 32).ToString(),
313 gfx::SkRectToRectF(iterator
->pixel_ref_rect
).ToString());
314 EXPECT_TRUE(++iterator
);
315 EXPECT_TRUE(iterator
->pixel_ref
== discardable_bitmap
[0][1].pixelRef());
316 EXPECT_EQ(gfx::RectF(260, 0, 32, 32).ToString(),
317 gfx::SkRectToRectF(iterator
->pixel_ref_rect
).ToString());
319 // copy now points to the same spot as iterator,
320 // but both can be incremented independently.
321 PicturePileImpl::PixelRefIterator copy
= iterator
;
322 EXPECT_TRUE(++iterator
);
323 EXPECT_TRUE(iterator
->pixel_ref
== discardable_bitmap
[1][1].pixelRef());
324 EXPECT_EQ(gfx::RectF(260, 260, 32, 32).ToString(),
325 gfx::SkRectToRectF(iterator
->pixel_ref_rect
).ToString());
326 EXPECT_FALSE(++iterator
);
329 EXPECT_TRUE(copy
->pixel_ref
== discardable_bitmap
[0][1].pixelRef());
330 EXPECT_EQ(gfx::RectF(260, 0, 32, 32).ToString(),
331 gfx::SkRectToRectF(copy
->pixel_ref_rect
).ToString());
333 EXPECT_TRUE(copy
->pixel_ref
== discardable_bitmap
[1][1].pixelRef());
334 EXPECT_EQ(gfx::RectF(260, 260, 32, 32).ToString(),
335 gfx::SkRectToRectF(copy
->pixel_ref_rect
).ToString());
336 EXPECT_FALSE(++copy
);
340 TEST(PicturePileImplTest
, RasterFullContents
) {
341 gfx::Size
tile_size(1000, 1000);
342 gfx::Size
layer_bounds(3, 5);
343 float contents_scale
= 1.5f
;
344 float raster_divisions
= 2.f
;
346 scoped_ptr
<FakePicturePile
> recording_source
=
347 FakePicturePile::CreateFilledPile(tile_size
, layer_bounds
);
348 recording_source
->SetBackgroundColor(SK_ColorBLACK
);
349 recording_source
->SetIsSolidColor(false);
350 recording_source
->SetRequiresClear(false);
351 recording_source
->SetClearCanvasWithDebugColor(false);
353 // Because the caller sets content opaque, it also promises that it
354 // has at least filled in layer_bounds opaquely.
356 white_paint
.setColor(SK_ColorWHITE
);
357 recording_source
->add_draw_rect_with_paint(gfx::Rect(layer_bounds
),
360 recording_source
->SetMinContentsScale(contents_scale
);
361 recording_source
->Rerecord();
363 scoped_refptr
<FakePicturePileImpl
> pile
=
364 FakePicturePileImpl::CreateFromPile(recording_source
.get(), nullptr);
366 gfx::Size
content_bounds(
367 gfx::ToCeiledSize(gfx::ScaleSize(layer_bounds
, contents_scale
)));
369 // Simulate drawing into different tiles at different offsets.
370 int step_x
= std::ceil(content_bounds
.width() / raster_divisions
);
371 int step_y
= std::ceil(content_bounds
.height() / raster_divisions
);
372 for (int offset_x
= 0; offset_x
< content_bounds
.width();
373 offset_x
+= step_x
) {
374 for (int offset_y
= 0; offset_y
< content_bounds
.height();
375 offset_y
+= step_y
) {
376 gfx::Rect
content_rect(offset_x
, offset_y
, step_x
, step_y
);
377 content_rect
.Intersect(gfx::Rect(content_bounds
));
379 // Simulate a canvas rect larger than the content rect. Every pixel
380 // up to one pixel outside the content rect is guaranteed to be opaque.
381 // Outside of that is undefined.
382 gfx::Rect
canvas_rect(content_rect
);
383 canvas_rect
.Inset(0, 0, -1, -1);
386 bitmap
.allocN32Pixels(canvas_rect
.width(), canvas_rect
.height());
387 SkCanvas
canvas(bitmap
);
388 canvas
.clear(SK_ColorTRANSPARENT
);
390 pile
->PlaybackToCanvas(&canvas
, canvas_rect
, canvas_rect
, contents_scale
);
392 SkColor
* pixels
= reinterpret_cast<SkColor
*>(bitmap
.getPixels());
393 int num_pixels
= bitmap
.width() * bitmap
.height();
394 bool all_white
= true;
395 for (int i
= 0; i
< num_pixels
; ++i
) {
396 EXPECT_EQ(SkColorGetA(pixels
[i
]), 255u);
397 all_white
&= (SkColorGetR(pixels
[i
]) == 255);
398 all_white
&= (SkColorGetG(pixels
[i
]) == 255);
399 all_white
&= (SkColorGetB(pixels
[i
]) == 255);
402 // If the canvas doesn't extend past the edge of the content,
403 // it should be entirely white. Otherwise, the edge of the content
404 // will be non-white.
405 EXPECT_EQ(all_white
, gfx::Rect(content_bounds
).Contains(canvas_rect
));
410 TEST(PicturePileImpl
, RasterContentsTransparent
) {
411 gfx::Size
tile_size(1000, 1000);
412 gfx::Size
layer_bounds(5, 3);
413 float contents_scale
= 0.5f
;
415 scoped_ptr
<FakePicturePile
> recording_source
=
416 FakePicturePile::CreateFilledPile(tile_size
, layer_bounds
);
417 recording_source
->SetBackgroundColor(SK_ColorTRANSPARENT
);
418 recording_source
->SetRequiresClear(true);
419 recording_source
->SetMinContentsScale(contents_scale
);
420 recording_source
->SetClearCanvasWithDebugColor(false);
421 recording_source
->Rerecord();
423 scoped_refptr
<FakePicturePileImpl
> pile
=
424 FakePicturePileImpl::CreateFromPile(recording_source
.get(), nullptr);
425 gfx::Size
content_bounds(
426 gfx::ToCeiledSize(gfx::ScaleSize(layer_bounds
, contents_scale
)));
428 gfx::Rect
canvas_rect(content_bounds
);
429 canvas_rect
.Inset(0, 0, -1, -1);
432 bitmap
.allocN32Pixels(canvas_rect
.width(), canvas_rect
.height());
433 SkCanvas
canvas(bitmap
);
435 pile
->PlaybackToCanvas(&canvas
, canvas_rect
, canvas_rect
, contents_scale
);
437 SkColor
* pixels
= reinterpret_cast<SkColor
*>(bitmap
.getPixels());
438 int num_pixels
= bitmap
.width() * bitmap
.height();
439 for (int i
= 0; i
< num_pixels
; ++i
) {
440 EXPECT_EQ(SkColorGetA(pixels
[i
]), 0u);
444 class OverlapTest
: public ::testing::TestWithParam
<float> {
446 static float MinContentsScale() { return 1.f
/ 4.f
; }
449 TEST_P(OverlapTest
, NoOverlap
) {
450 gfx::Size
tile_size(10, 10);
451 gfx::Size
layer_bounds(30, 30);
452 gfx::Size
bigger_than_layer_bounds(300, 300);
453 float contents_scale
= GetParam();
454 // Pick an opaque color to not have to deal with premultiplication off-by-one.
455 SkColor test_color
= SkColorSetARGB(255, 45, 56, 67);
457 scoped_ptr
<FakePicturePile
> recording_source
=
458 FakePicturePile::CreateFilledPile(tile_size
, layer_bounds
);
459 recording_source
->SetBackgroundColor(SK_ColorTRANSPARENT
);
460 recording_source
->SetRequiresClear(true);
461 recording_source
->SetMinContentsScale(MinContentsScale());
462 recording_source
->SetClearCanvasWithDebugColor(true);
465 color_paint
.setColor(test_color
);
466 // Additive paint, so that if two paints overlap, the color will change.
467 color_paint
.setXfermodeMode(SkXfermode::kPlus_Mode
);
468 // Paint outside the layer to make sure that blending works.
469 recording_source
->add_draw_rect_with_paint(
470 gfx::RectF(bigger_than_layer_bounds
), color_paint
);
471 recording_source
->Rerecord();
473 scoped_refptr
<FakePicturePileImpl
> pile
=
474 FakePicturePileImpl::CreateFromPile(recording_source
.get(), nullptr);
475 gfx::Size
content_bounds(
476 gfx::ToCeiledSize(gfx::ScaleSize(layer_bounds
, contents_scale
)));
479 bitmap
.allocN32Pixels(content_bounds
.width(), content_bounds
.height());
480 SkCanvas
canvas(bitmap
);
482 pile
->PlaybackToCanvas(&canvas
, gfx::Rect(content_bounds
),
483 gfx::Rect(content_bounds
), contents_scale
);
485 for (int y
= 0; y
< bitmap
.height(); y
++) {
486 for (int x
= 0; x
< bitmap
.width(); x
++) {
487 SkColor color
= bitmap
.getColor(x
, y
);
488 EXPECT_EQ(SkColorGetR(test_color
), SkColorGetR(color
)) << "x: " << x
490 EXPECT_EQ(SkColorGetG(test_color
), SkColorGetG(color
)) << "x: " << x
492 EXPECT_EQ(SkColorGetB(test_color
), SkColorGetB(color
)) << "x: " << x
494 EXPECT_EQ(SkColorGetA(test_color
), SkColorGetA(color
)) << "x: " << x
496 if (test_color
!= color
)
502 INSTANTIATE_TEST_CASE_P(PicturePileImpl
,
504 ::testing::Values(1.f
, 0.873f
, 1.f
/ 4.f
, 4.f
));
506 TEST(PicturePileImplTest
, PixelRefIteratorBorders
) {
507 // 3 tile width / 1 tile height pile
508 gfx::Size
tile_size(128, 128);
509 gfx::Size
layer_bounds(320, 128);
511 // Fake picture pile uses a tile grid the size of the tile. So,
512 // any iteration that intersects with a tile will return all pixel refs
514 scoped_ptr
<FakePicturePile
> recording_source
=
515 FakePicturePile::CreateFilledPile(tile_size
, layer_bounds
);
516 recording_source
->SetMinContentsScale(0.5f
);
518 // Bitmaps 0-2 are exactly on tiles 0-2, so that they overlap the borders
519 // of adjacent tiles.
520 gfx::Rect bitmap_rects
[] = {
521 recording_source
->tiling().TileBounds(0, 0),
522 recording_source
->tiling().TileBounds(1, 0),
523 recording_source
->tiling().TileBounds(2, 0),
525 gfx::RectF expected_rects
[] = {
526 gfx::RectF(recording_source
->tiling().TileBounds(0, 0)),
527 gfx::RectF(recording_source
->tiling().TileBounds(1, 0)),
528 gfx::RectF(recording_source
->tiling().TileBounds(2, 0)),
530 SkBitmap discardable_bitmap
[arraysize(bitmap_rects
)];
532 for (size_t i
= 0; i
< arraysize(bitmap_rects
); ++i
) {
533 CreateDiscardableBitmap(bitmap_rects
[i
].size(), &discardable_bitmap
[i
]);
534 recording_source
->add_draw_bitmap(discardable_bitmap
[i
],
535 bitmap_rects
[i
].origin());
538 recording_source
->SetGatherPixelRefs(true);
539 recording_source
->Rerecord();
541 scoped_refptr
<FakePicturePileImpl
> pile
=
542 FakePicturePileImpl::CreateFromPile(recording_source
.get(), nullptr);
544 // Sanity check that bitmaps 0-2 intersect the borders of their adjacent
545 // tiles, but not the actual tiles.
547 bitmap_rects
[0].Intersects(pile
->tiling().TileBoundsWithBorder(1, 0)));
548 EXPECT_FALSE(bitmap_rects
[0].Intersects(pile
->tiling().TileBounds(1, 0)));
550 bitmap_rects
[1].Intersects(pile
->tiling().TileBoundsWithBorder(0, 0)));
551 EXPECT_FALSE(bitmap_rects
[1].Intersects(pile
->tiling().TileBounds(0, 0)));
553 bitmap_rects
[1].Intersects(pile
->tiling().TileBoundsWithBorder(2, 0)));
554 EXPECT_FALSE(bitmap_rects
[1].Intersects(pile
->tiling().TileBounds(2, 0)));
556 bitmap_rects
[2].Intersects(pile
->tiling().TileBoundsWithBorder(1, 0)));
557 EXPECT_FALSE(bitmap_rects
[2].Intersects(pile
->tiling().TileBounds(1, 0)));
559 // Tile-sized iterators.
561 // Because tile 0's borders extend onto tile 1, it will include both
562 // bitmap 0 and 1. However, it should *not* include bitmap 2.
563 PicturePileImpl::PixelRefIterator
iterator(
564 pile
->tiling().TileBounds(0, 0), 1.f
, pile
.get());
565 EXPECT_TRUE(iterator
);
566 EXPECT_TRUE(iterator
->pixel_ref
== discardable_bitmap
[0].pixelRef());
567 EXPECT_EQ(expected_rects
[0].ToString(),
568 gfx::SkRectToRectF(iterator
->pixel_ref_rect
).ToString());
569 EXPECT_TRUE(++iterator
);
570 EXPECT_TRUE(iterator
->pixel_ref
== discardable_bitmap
[1].pixelRef());
571 EXPECT_EQ(expected_rects
[1].ToString(),
572 gfx::SkRectToRectF(iterator
->pixel_ref_rect
).ToString());
573 EXPECT_FALSE(++iterator
);
576 // Tile 1 + borders hits all bitmaps.
577 PicturePileImpl::PixelRefIterator
iterator(
578 pile
->tiling().TileBounds(1, 0), 1.f
, pile
.get());
579 EXPECT_TRUE(iterator
);
580 EXPECT_TRUE(iterator
->pixel_ref
== discardable_bitmap
[0].pixelRef());
581 EXPECT_EQ(expected_rects
[0].ToString(),
582 gfx::SkRectToRectF(iterator
->pixel_ref_rect
).ToString());
583 EXPECT_TRUE(++iterator
);
584 EXPECT_TRUE(iterator
->pixel_ref
== discardable_bitmap
[1].pixelRef());
585 EXPECT_EQ(expected_rects
[1].ToString(),
586 gfx::SkRectToRectF(iterator
->pixel_ref_rect
).ToString());
587 EXPECT_TRUE(++iterator
);
588 EXPECT_TRUE(iterator
->pixel_ref
== discardable_bitmap
[2].pixelRef());
589 EXPECT_EQ(expected_rects
[2].ToString(),
590 gfx::SkRectToRectF(iterator
->pixel_ref_rect
).ToString());
591 EXPECT_FALSE(++iterator
);
594 // Tile 2 should not include bitmap 0, which is only on tile 0 and the
595 // borders of tile 1.
596 PicturePileImpl::PixelRefIterator
iterator(
597 pile
->tiling().TileBounds(2, 0), 1.f
, pile
.get());
598 EXPECT_TRUE(iterator
);
599 EXPECT_TRUE(iterator
->pixel_ref
== discardable_bitmap
[1].pixelRef());
600 EXPECT_EQ(expected_rects
[1].ToString(),
601 gfx::SkRectToRectF(iterator
->pixel_ref_rect
).ToString());
602 EXPECT_TRUE(++iterator
);
603 EXPECT_TRUE(iterator
->pixel_ref
== discardable_bitmap
[2].pixelRef());
604 EXPECT_EQ(expected_rects
[2].ToString(),
605 gfx::SkRectToRectF(iterator
->pixel_ref_rect
).ToString());
606 EXPECT_FALSE(++iterator
);