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
->RerecordPile();
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
->RerecordPile();
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
->RerecordPile();
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
->RerecordPile();
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
, PixelRefIteratorEmpty
) {
163 gfx::Size
tile_size(128, 128);
164 gfx::Size
layer_bounds(256, 256);
166 // Create a filled pile with no recording.
167 scoped_refptr
<FakePicturePileImpl
> pile
=
168 FakePicturePileImpl::CreateFilledPile(tile_size
, layer_bounds
);
170 // Tile sized iterators.
172 PicturePileImpl::PixelRefIterator
iterator(
173 gfx::Rect(0, 0, 128, 128), 1.0, pile
.get());
174 EXPECT_FALSE(iterator
);
177 PicturePileImpl::PixelRefIterator
iterator(
178 gfx::Rect(0, 0, 256, 256), 2.0, pile
.get());
179 EXPECT_FALSE(iterator
);
182 PicturePileImpl::PixelRefIterator
iterator(
183 gfx::Rect(0, 0, 64, 64), 0.5, pile
.get());
184 EXPECT_FALSE(iterator
);
186 // Shifted tile sized iterators.
188 PicturePileImpl::PixelRefIterator
iterator(
189 gfx::Rect(140, 140, 128, 128), 1.0, pile
.get());
190 EXPECT_FALSE(iterator
);
193 PicturePileImpl::PixelRefIterator
iterator(
194 gfx::Rect(280, 280, 256, 256), 2.0, pile
.get());
195 EXPECT_FALSE(iterator
);
198 PicturePileImpl::PixelRefIterator
iterator(
199 gfx::Rect(70, 70, 64, 64), 0.5, pile
.get());
200 EXPECT_FALSE(iterator
);
202 // Layer sized iterators.
204 PicturePileImpl::PixelRefIterator
iterator(
205 gfx::Rect(0, 0, 256, 256), 1.0, pile
.get());
206 EXPECT_FALSE(iterator
);
209 PicturePileImpl::PixelRefIterator
iterator(
210 gfx::Rect(0, 0, 512, 512), 2.0, pile
.get());
211 EXPECT_FALSE(iterator
);
214 PicturePileImpl::PixelRefIterator
iterator(
215 gfx::Rect(0, 0, 128, 128), 0.5, pile
.get());
216 EXPECT_FALSE(iterator
);
220 TEST(PicturePileImplTest
, PixelRefIteratorNoDiscardableRefs
) {
221 gfx::Size
tile_size(128, 128);
222 gfx::Size
layer_bounds(256, 256);
224 scoped_ptr
<FakePicturePile
> recording_source
=
225 FakePicturePile::CreateFilledPile(tile_size
, layer_bounds
);
226 SkPaint simple_paint
;
227 simple_paint
.setColor(SkColorSetARGB(255, 12, 23, 34));
229 SkBitmap non_discardable_bitmap
;
230 CreateBitmap(gfx::Size(128, 128), "notdiscardable", &non_discardable_bitmap
);
232 recording_source
->add_draw_rect_with_paint(gfx::Rect(0, 0, 256, 256),
234 recording_source
->add_draw_rect_with_paint(gfx::Rect(128, 128, 512, 512),
236 recording_source
->add_draw_rect_with_paint(gfx::Rect(512, 0, 256, 256),
238 recording_source
->add_draw_rect_with_paint(gfx::Rect(0, 512, 256, 256),
240 recording_source
->add_draw_bitmap(non_discardable_bitmap
, gfx::Point(128, 0));
241 recording_source
->add_draw_bitmap(non_discardable_bitmap
, gfx::Point(0, 128));
242 recording_source
->add_draw_bitmap(non_discardable_bitmap
,
243 gfx::Point(150, 150));
244 recording_source
->RerecordPile();
246 scoped_refptr
<FakePicturePileImpl
> pile
=
247 FakePicturePileImpl::CreateFromPile(recording_source
.get(), nullptr);
249 // Tile sized iterators.
251 PicturePileImpl::PixelRefIterator
iterator(
252 gfx::Rect(0, 0, 128, 128), 1.0, pile
.get());
253 EXPECT_FALSE(iterator
);
256 PicturePileImpl::PixelRefIterator
iterator(
257 gfx::Rect(0, 0, 256, 256), 2.0, pile
.get());
258 EXPECT_FALSE(iterator
);
261 PicturePileImpl::PixelRefIterator
iterator(
262 gfx::Rect(0, 0, 64, 64), 0.5, pile
.get());
263 EXPECT_FALSE(iterator
);
265 // Shifted tile sized iterators.
267 PicturePileImpl::PixelRefIterator
iterator(
268 gfx::Rect(140, 140, 128, 128), 1.0, pile
.get());
269 EXPECT_FALSE(iterator
);
272 PicturePileImpl::PixelRefIterator
iterator(
273 gfx::Rect(280, 280, 256, 256), 2.0, pile
.get());
274 EXPECT_FALSE(iterator
);
277 PicturePileImpl::PixelRefIterator
iterator(
278 gfx::Rect(70, 70, 64, 64), 0.5, pile
.get());
279 EXPECT_FALSE(iterator
);
281 // Layer sized iterators.
283 PicturePileImpl::PixelRefIterator
iterator(
284 gfx::Rect(0, 0, 256, 256), 1.0, pile
.get());
285 EXPECT_FALSE(iterator
);
288 PicturePileImpl::PixelRefIterator
iterator(
289 gfx::Rect(0, 0, 512, 512), 2.0, pile
.get());
290 EXPECT_FALSE(iterator
);
293 PicturePileImpl::PixelRefIterator
iterator(
294 gfx::Rect(0, 0, 128, 128), 0.5, pile
.get());
295 EXPECT_FALSE(iterator
);
299 TEST(PicturePileImplTest
, PixelRefIteratorDiscardableRefs
) {
300 gfx::Size
tile_size(128, 128);
301 gfx::Size
layer_bounds(256, 256);
303 scoped_ptr
<FakePicturePile
> recording_source
=
304 FakePicturePile::CreateFilledPile(tile_size
, layer_bounds
);
306 SkBitmap discardable_bitmap
[2][2];
307 CreateBitmap(gfx::Size(32, 32), "discardable", &discardable_bitmap
[0][0]);
308 CreateBitmap(gfx::Size(32, 32), "discardable", &discardable_bitmap
[1][0]);
309 CreateBitmap(gfx::Size(32, 32), "discardable", &discardable_bitmap
[1][1]);
311 // Discardable pixel refs are found in the following cells:
317 recording_source
->add_draw_bitmap(discardable_bitmap
[0][0], gfx::Point(0, 0));
318 recording_source
->add_draw_bitmap(discardable_bitmap
[1][0],
320 recording_source
->add_draw_bitmap(discardable_bitmap
[1][1],
321 gfx::Point(140, 140));
322 recording_source
->RerecordPile();
324 scoped_refptr
<FakePicturePileImpl
> pile
=
325 FakePicturePileImpl::CreateFromPile(recording_source
.get(), nullptr);
327 // Tile sized iterators. These should find only one pixel ref.
329 PicturePileImpl::PixelRefIterator
iterator(
330 gfx::Rect(0, 0, 128, 128), 1.0, pile
.get());
331 EXPECT_TRUE(iterator
);
332 EXPECT_TRUE(*iterator
== discardable_bitmap
[0][0].pixelRef());
333 EXPECT_FALSE(++iterator
);
336 PicturePileImpl::PixelRefIterator
iterator(
337 gfx::Rect(0, 0, 256, 256), 2.0, pile
.get());
338 EXPECT_TRUE(iterator
);
339 EXPECT_TRUE(*iterator
== discardable_bitmap
[0][0].pixelRef());
340 EXPECT_FALSE(++iterator
);
343 PicturePileImpl::PixelRefIterator
iterator(
344 gfx::Rect(0, 0, 64, 64), 0.5, pile
.get());
345 EXPECT_TRUE(iterator
);
346 EXPECT_TRUE(*iterator
== discardable_bitmap
[0][0].pixelRef());
347 EXPECT_FALSE(++iterator
);
349 // Shifted tile sized iterators. These should find only one pixel ref.
351 PicturePileImpl::PixelRefIterator
iterator(
352 gfx::Rect(140, 140, 128, 128), 1.0, pile
.get());
353 EXPECT_TRUE(iterator
);
354 EXPECT_TRUE(*iterator
== discardable_bitmap
[1][1].pixelRef());
355 EXPECT_FALSE(++iterator
);
358 PicturePileImpl::PixelRefIterator
iterator(
359 gfx::Rect(280, 280, 256, 256), 2.0, pile
.get());
360 EXPECT_TRUE(iterator
);
361 EXPECT_TRUE(*iterator
== discardable_bitmap
[1][1].pixelRef());
362 EXPECT_FALSE(++iterator
);
365 PicturePileImpl::PixelRefIterator
iterator(
366 gfx::Rect(70, 70, 64, 64), 0.5, pile
.get());
367 EXPECT_TRUE(iterator
);
368 EXPECT_TRUE(*iterator
== discardable_bitmap
[1][1].pixelRef());
369 EXPECT_FALSE(++iterator
);
371 // Ensure there's no discardable pixel refs in the empty cell
373 PicturePileImpl::PixelRefIterator
iterator(
374 gfx::Rect(140, 0, 128, 128), 1.0, pile
.get());
375 EXPECT_FALSE(iterator
);
377 // Layer sized iterators. These should find all 3 pixel refs.
379 PicturePileImpl::PixelRefIterator
iterator(
380 gfx::Rect(0, 0, 256, 256), 1.0, pile
.get());
381 EXPECT_TRUE(iterator
);
382 EXPECT_TRUE(*iterator
== discardable_bitmap
[0][0].pixelRef());
383 EXPECT_TRUE(++iterator
);
384 EXPECT_TRUE(*iterator
== discardable_bitmap
[1][0].pixelRef());
385 EXPECT_TRUE(++iterator
);
386 EXPECT_TRUE(*iterator
== discardable_bitmap
[1][1].pixelRef());
387 EXPECT_FALSE(++iterator
);
390 PicturePileImpl::PixelRefIterator
iterator(
391 gfx::Rect(0, 0, 512, 512), 2.0, pile
.get());
392 EXPECT_TRUE(iterator
);
393 EXPECT_TRUE(*iterator
== discardable_bitmap
[0][0].pixelRef());
394 EXPECT_TRUE(++iterator
);
395 EXPECT_TRUE(*iterator
== discardable_bitmap
[1][0].pixelRef());
396 EXPECT_TRUE(++iterator
);
397 EXPECT_TRUE(*iterator
== discardable_bitmap
[1][1].pixelRef());
398 EXPECT_FALSE(++iterator
);
401 PicturePileImpl::PixelRefIterator
iterator(
402 gfx::Rect(0, 0, 128, 128), 0.5, pile
.get());
403 EXPECT_TRUE(iterator
);
404 EXPECT_TRUE(*iterator
== discardable_bitmap
[0][0].pixelRef());
405 EXPECT_TRUE(++iterator
);
406 EXPECT_TRUE(*iterator
== discardable_bitmap
[1][0].pixelRef());
407 EXPECT_TRUE(++iterator
);
408 EXPECT_TRUE(*iterator
== discardable_bitmap
[1][1].pixelRef());
409 EXPECT_FALSE(++iterator
);
413 TEST(PicturePileImplTest
, PixelRefIteratorDiscardableRefsOneTile
) {
414 gfx::Size
tile_size(256, 256);
415 gfx::Size
layer_bounds(512, 512);
417 scoped_ptr
<FakePicturePile
> recording_source
=
418 FakePicturePile::CreateFilledPile(tile_size
, layer_bounds
);
420 SkBitmap discardable_bitmap
[2][2];
421 CreateBitmap(gfx::Size(32, 32), "discardable", &discardable_bitmap
[0][0]);
422 CreateBitmap(gfx::Size(32, 32), "discardable", &discardable_bitmap
[0][1]);
423 CreateBitmap(gfx::Size(32, 32), "discardable", &discardable_bitmap
[1][1]);
425 // Discardable pixel refs are found in the following cells:
431 recording_source
->add_draw_bitmap(discardable_bitmap
[0][0], gfx::Point(0, 0));
432 recording_source
->add_draw_bitmap(discardable_bitmap
[0][1],
434 recording_source
->add_draw_bitmap(discardable_bitmap
[1][1],
435 gfx::Point(260, 260));
436 recording_source
->RerecordPile();
438 scoped_refptr
<FakePicturePileImpl
> pile
=
439 FakePicturePileImpl::CreateFromPile(recording_source
.get(), nullptr);
441 // Tile sized iterators. These should find only one pixel ref.
443 PicturePileImpl::PixelRefIterator
iterator(
444 gfx::Rect(0, 0, 256, 256), 1.0, pile
.get());
445 EXPECT_TRUE(iterator
);
446 EXPECT_TRUE(*iterator
== discardable_bitmap
[0][0].pixelRef());
447 EXPECT_FALSE(++iterator
);
450 PicturePileImpl::PixelRefIterator
iterator(
451 gfx::Rect(0, 0, 512, 512), 2.0, pile
.get());
452 EXPECT_TRUE(iterator
);
453 EXPECT_TRUE(*iterator
== discardable_bitmap
[0][0].pixelRef());
454 EXPECT_FALSE(++iterator
);
457 PicturePileImpl::PixelRefIterator
iterator(
458 gfx::Rect(0, 0, 128, 128), 0.5, pile
.get());
459 EXPECT_TRUE(iterator
);
460 EXPECT_TRUE(*iterator
== discardable_bitmap
[0][0].pixelRef());
461 EXPECT_FALSE(++iterator
);
463 // Shifted tile sized iterators. These should find only one pixel ref.
465 PicturePileImpl::PixelRefIterator
iterator(
466 gfx::Rect(260, 260, 256, 256), 1.0, pile
.get());
467 EXPECT_TRUE(iterator
);
468 EXPECT_TRUE(*iterator
== discardable_bitmap
[1][1].pixelRef());
469 EXPECT_FALSE(++iterator
);
472 PicturePileImpl::PixelRefIterator
iterator(
473 gfx::Rect(520, 520, 512, 512), 2.0, pile
.get());
474 EXPECT_TRUE(iterator
);
475 EXPECT_TRUE(*iterator
== discardable_bitmap
[1][1].pixelRef());
476 EXPECT_FALSE(++iterator
);
479 PicturePileImpl::PixelRefIterator
iterator(
480 gfx::Rect(130, 130, 128, 128), 0.5, pile
.get());
481 EXPECT_TRUE(iterator
);
482 EXPECT_TRUE(*iterator
== discardable_bitmap
[1][1].pixelRef());
483 EXPECT_FALSE(++iterator
);
485 // Ensure there's no discardable pixel refs in the empty cell
487 PicturePileImpl::PixelRefIterator
iterator(
488 gfx::Rect(0, 256, 256, 256), 1.0, pile
.get());
489 EXPECT_FALSE(iterator
);
491 // Layer sized iterators. These should find three pixel ref.
493 PicturePileImpl::PixelRefIterator
iterator(
494 gfx::Rect(0, 0, 512, 512), 1.0, pile
.get());
495 EXPECT_TRUE(iterator
);
496 EXPECT_TRUE(*iterator
== discardable_bitmap
[0][0].pixelRef());
497 EXPECT_TRUE(++iterator
);
498 EXPECT_TRUE(*iterator
== discardable_bitmap
[0][1].pixelRef());
499 EXPECT_TRUE(++iterator
);
500 EXPECT_TRUE(*iterator
== discardable_bitmap
[1][1].pixelRef());
501 EXPECT_FALSE(++iterator
);
504 PicturePileImpl::PixelRefIterator
iterator(
505 gfx::Rect(0, 0, 1024, 1024), 2.0, pile
.get());
506 EXPECT_TRUE(iterator
);
507 EXPECT_TRUE(*iterator
== discardable_bitmap
[0][0].pixelRef());
508 EXPECT_TRUE(++iterator
);
509 EXPECT_TRUE(*iterator
== discardable_bitmap
[0][1].pixelRef());
510 EXPECT_TRUE(++iterator
);
511 EXPECT_TRUE(*iterator
== discardable_bitmap
[1][1].pixelRef());
512 EXPECT_FALSE(++iterator
);
515 PicturePileImpl::PixelRefIterator
iterator(
516 gfx::Rect(0, 0, 256, 256), 0.5, pile
.get());
517 EXPECT_TRUE(iterator
);
518 EXPECT_TRUE(*iterator
== discardable_bitmap
[0][0].pixelRef());
519 EXPECT_TRUE(++iterator
);
520 EXPECT_TRUE(*iterator
== discardable_bitmap
[0][1].pixelRef());
521 EXPECT_TRUE(++iterator
);
522 EXPECT_TRUE(*iterator
== discardable_bitmap
[1][1].pixelRef());
523 EXPECT_FALSE(++iterator
);
527 PicturePileImpl::PixelRefIterator
iterator(
528 gfx::Rect(0, 0, 512, 512), 1.0, pile
.get());
529 EXPECT_TRUE(iterator
);
530 EXPECT_TRUE(*iterator
== discardable_bitmap
[0][0].pixelRef());
531 EXPECT_TRUE(++iterator
);
532 EXPECT_TRUE(*iterator
== discardable_bitmap
[0][1].pixelRef());
534 // copy now points to the same spot as iterator,
535 // but both can be incremented independently.
536 PicturePileImpl::PixelRefIterator copy
= iterator
;
537 EXPECT_TRUE(++iterator
);
538 EXPECT_TRUE(*iterator
== discardable_bitmap
[1][1].pixelRef());
539 EXPECT_FALSE(++iterator
);
542 EXPECT_TRUE(*copy
== discardable_bitmap
[0][1].pixelRef());
544 EXPECT_TRUE(*copy
== discardable_bitmap
[1][1].pixelRef());
545 EXPECT_FALSE(++copy
);
548 TEST(PicturePileImplTest
, PixelRefIteratorDiscardableRefsBaseNonDiscardable
) {
549 gfx::Size
tile_size(256, 256);
550 gfx::Size
layer_bounds(512, 512);
552 scoped_ptr
<FakePicturePile
> recording_source
=
553 FakePicturePile::CreateFilledPile(tile_size
, layer_bounds
);
555 SkBitmap non_discardable_bitmap
;
556 CreateBitmap(gfx::Size(512, 512), "notdiscardable", &non_discardable_bitmap
);
558 SkBitmap discardable_bitmap
[2][2];
559 CreateBitmap(gfx::Size(128, 128), "discardable", &discardable_bitmap
[0][0]);
560 CreateBitmap(gfx::Size(128, 128), "discardable", &discardable_bitmap
[0][1]);
561 CreateBitmap(gfx::Size(128, 128), "discardable", &discardable_bitmap
[1][1]);
563 // One large non-discardable bitmap covers the whole grid.
564 // Discardable pixel refs are found in the following cells:
570 recording_source
->add_draw_bitmap(non_discardable_bitmap
, gfx::Point(0, 0));
571 recording_source
->add_draw_bitmap(discardable_bitmap
[0][0], gfx::Point(0, 0));
572 recording_source
->add_draw_bitmap(discardable_bitmap
[0][1],
574 recording_source
->add_draw_bitmap(discardable_bitmap
[1][1],
575 gfx::Point(260, 260));
576 recording_source
->RerecordPile();
578 scoped_refptr
<FakePicturePileImpl
> pile
=
579 FakePicturePileImpl::CreateFromPile(recording_source
.get(), nullptr);
581 // Tile sized iterators. These should find only one pixel ref.
583 PicturePileImpl::PixelRefIterator
iterator(
584 gfx::Rect(0, 0, 256, 256), 1.0, pile
.get());
585 EXPECT_TRUE(iterator
);
586 EXPECT_TRUE(*iterator
== discardable_bitmap
[0][0].pixelRef());
587 EXPECT_FALSE(++iterator
);
590 PicturePileImpl::PixelRefIterator
iterator(
591 gfx::Rect(0, 0, 512, 512), 2.0, pile
.get());
592 EXPECT_TRUE(iterator
);
593 EXPECT_TRUE(*iterator
== discardable_bitmap
[0][0].pixelRef());
594 EXPECT_FALSE(++iterator
);
597 PicturePileImpl::PixelRefIterator
iterator(
598 gfx::Rect(0, 0, 128, 128), 0.5, pile
.get());
599 EXPECT_TRUE(iterator
);
600 EXPECT_TRUE(*iterator
== discardable_bitmap
[0][0].pixelRef());
601 EXPECT_FALSE(++iterator
);
603 // Shifted tile sized iterators. These should find only one pixel ref.
605 PicturePileImpl::PixelRefIterator
iterator(
606 gfx::Rect(260, 260, 256, 256), 1.0, pile
.get());
607 EXPECT_TRUE(iterator
);
608 EXPECT_TRUE(*iterator
== discardable_bitmap
[1][1].pixelRef());
609 EXPECT_FALSE(++iterator
);
612 PicturePileImpl::PixelRefIterator
iterator(
613 gfx::Rect(520, 520, 512, 512), 2.0, pile
.get());
614 EXPECT_TRUE(iterator
);
615 EXPECT_TRUE(*iterator
== discardable_bitmap
[1][1].pixelRef());
616 EXPECT_FALSE(++iterator
);
619 PicturePileImpl::PixelRefIterator
iterator(
620 gfx::Rect(130, 130, 128, 128), 0.5, pile
.get());
621 EXPECT_TRUE(iterator
);
622 EXPECT_TRUE(*iterator
== discardable_bitmap
[1][1].pixelRef());
623 EXPECT_FALSE(++iterator
);
625 // Ensure there's no discardable pixel refs in the empty cell
627 PicturePileImpl::PixelRefIterator
iterator(
628 gfx::Rect(0, 256, 256, 256), 1.0, pile
.get());
629 EXPECT_FALSE(iterator
);
631 // Layer sized iterators. These should find three pixel ref.
633 PicturePileImpl::PixelRefIterator
iterator(
634 gfx::Rect(0, 0, 512, 512), 1.0, pile
.get());
635 EXPECT_TRUE(iterator
);
636 EXPECT_TRUE(*iterator
== discardable_bitmap
[0][0].pixelRef());
637 EXPECT_TRUE(++iterator
);
638 EXPECT_TRUE(*iterator
== discardable_bitmap
[0][1].pixelRef());
639 EXPECT_TRUE(++iterator
);
640 EXPECT_TRUE(*iterator
== discardable_bitmap
[1][1].pixelRef());
641 EXPECT_FALSE(++iterator
);
644 PicturePileImpl::PixelRefIterator
iterator(
645 gfx::Rect(0, 0, 1024, 1024), 2.0, pile
.get());
646 EXPECT_TRUE(iterator
);
647 EXPECT_TRUE(*iterator
== discardable_bitmap
[0][0].pixelRef());
648 EXPECT_TRUE(++iterator
);
649 EXPECT_TRUE(*iterator
== discardable_bitmap
[0][1].pixelRef());
650 EXPECT_TRUE(++iterator
);
651 EXPECT_TRUE(*iterator
== discardable_bitmap
[1][1].pixelRef());
652 EXPECT_FALSE(++iterator
);
655 PicturePileImpl::PixelRefIterator
iterator(
656 gfx::Rect(0, 0, 256, 256), 0.5, pile
.get());
657 EXPECT_TRUE(iterator
);
658 EXPECT_TRUE(*iterator
== discardable_bitmap
[0][0].pixelRef());
659 EXPECT_TRUE(++iterator
);
660 EXPECT_TRUE(*iterator
== discardable_bitmap
[0][1].pixelRef());
661 EXPECT_TRUE(++iterator
);
662 EXPECT_TRUE(*iterator
== discardable_bitmap
[1][1].pixelRef());
663 EXPECT_FALSE(++iterator
);
667 TEST(PicturePileImplTest
, RasterFullContents
) {
668 gfx::Size
tile_size(1000, 1000);
669 gfx::Size
layer_bounds(3, 5);
670 float contents_scale
= 1.5f
;
671 float raster_divisions
= 2.f
;
673 scoped_ptr
<FakePicturePile
> recording_source
=
674 FakePicturePile::CreateFilledPile(tile_size
, layer_bounds
);
675 recording_source
->SetBackgroundColor(SK_ColorBLACK
);
676 recording_source
->SetIsSolidColor(false);
677 recording_source
->SetRequiresClear(false);
678 recording_source
->SetClearCanvasWithDebugColor(false);
680 // Because the caller sets content opaque, it also promises that it
681 // has at least filled in layer_bounds opaquely.
683 white_paint
.setColor(SK_ColorWHITE
);
684 recording_source
->add_draw_rect_with_paint(gfx::Rect(layer_bounds
),
687 recording_source
->SetMinContentsScale(contents_scale
);
688 recording_source
->RerecordPile();
690 scoped_refptr
<FakePicturePileImpl
> pile
=
691 FakePicturePileImpl::CreateFromPile(recording_source
.get(), nullptr);
693 gfx::Size
content_bounds(
694 gfx::ToCeiledSize(gfx::ScaleSize(layer_bounds
, contents_scale
)));
696 // Simulate drawing into different tiles at different offsets.
697 int step_x
= std::ceil(content_bounds
.width() / raster_divisions
);
698 int step_y
= std::ceil(content_bounds
.height() / raster_divisions
);
699 for (int offset_x
= 0; offset_x
< content_bounds
.width();
700 offset_x
+= step_x
) {
701 for (int offset_y
= 0; offset_y
< content_bounds
.height();
702 offset_y
+= step_y
) {
703 gfx::Rect
content_rect(offset_x
, offset_y
, step_x
, step_y
);
704 content_rect
.Intersect(gfx::Rect(content_bounds
));
706 // Simulate a canvas rect larger than the content rect. Every pixel
707 // up to one pixel outside the content rect is guaranteed to be opaque.
708 // Outside of that is undefined.
709 gfx::Rect
canvas_rect(content_rect
);
710 canvas_rect
.Inset(0, 0, -1, -1);
713 bitmap
.allocN32Pixels(canvas_rect
.width(), canvas_rect
.height());
714 SkCanvas
canvas(bitmap
);
715 canvas
.clear(SK_ColorTRANSPARENT
);
717 pile
->PlaybackToCanvas(&canvas
, canvas_rect
, contents_scale
);
719 SkColor
* pixels
= reinterpret_cast<SkColor
*>(bitmap
.getPixels());
720 int num_pixels
= bitmap
.width() * bitmap
.height();
721 bool all_white
= true;
722 for (int i
= 0; i
< num_pixels
; ++i
) {
723 EXPECT_EQ(SkColorGetA(pixels
[i
]), 255u);
724 all_white
&= (SkColorGetR(pixels
[i
]) == 255);
725 all_white
&= (SkColorGetG(pixels
[i
]) == 255);
726 all_white
&= (SkColorGetB(pixels
[i
]) == 255);
729 // If the canvas doesn't extend past the edge of the content,
730 // it should be entirely white. Otherwise, the edge of the content
731 // will be non-white.
732 EXPECT_EQ(all_white
, gfx::Rect(content_bounds
).Contains(canvas_rect
));
737 TEST(PicturePileImpl
, RasterContentsTransparent
) {
738 gfx::Size
tile_size(1000, 1000);
739 gfx::Size
layer_bounds(5, 3);
740 float contents_scale
= 0.5f
;
742 scoped_ptr
<FakePicturePile
> recording_source
=
743 FakePicturePile::CreateFilledPile(tile_size
, layer_bounds
);
744 recording_source
->SetBackgroundColor(SK_ColorTRANSPARENT
);
745 recording_source
->SetRequiresClear(true);
746 recording_source
->SetMinContentsScale(contents_scale
);
747 recording_source
->SetClearCanvasWithDebugColor(false);
748 recording_source
->RerecordPile();
750 scoped_refptr
<FakePicturePileImpl
> pile
=
751 FakePicturePileImpl::CreateFromPile(recording_source
.get(), nullptr);
752 gfx::Size
content_bounds(
753 gfx::ToCeiledSize(gfx::ScaleSize(layer_bounds
, contents_scale
)));
755 gfx::Rect
canvas_rect(content_bounds
);
756 canvas_rect
.Inset(0, 0, -1, -1);
759 bitmap
.allocN32Pixels(canvas_rect
.width(), canvas_rect
.height());
760 SkCanvas
canvas(bitmap
);
762 pile
->PlaybackToCanvas(&canvas
, canvas_rect
, contents_scale
);
764 SkColor
* pixels
= reinterpret_cast<SkColor
*>(bitmap
.getPixels());
765 int num_pixels
= bitmap
.width() * bitmap
.height();
766 for (int i
= 0; i
< num_pixels
; ++i
) {
767 EXPECT_EQ(SkColorGetA(pixels
[i
]), 0u);
771 class OverlapTest
: public ::testing::TestWithParam
<float> {
773 static float MinContentsScale() { return 1.f
/ 4.f
; }
776 TEST_P(OverlapTest
, NoOverlap
) {
777 gfx::Size
tile_size(10, 10);
778 gfx::Size
layer_bounds(30, 30);
779 gfx::Size
bigger_than_layer_bounds(300, 300);
780 float contents_scale
= GetParam();
781 // Pick an opaque color to not have to deal with premultiplication off-by-one.
782 SkColor test_color
= SkColorSetARGB(255, 45, 56, 67);
784 scoped_ptr
<FakePicturePile
> recording_source
=
785 FakePicturePile::CreateFilledPile(tile_size
, layer_bounds
);
786 recording_source
->SetBackgroundColor(SK_ColorTRANSPARENT
);
787 recording_source
->SetRequiresClear(true);
788 recording_source
->SetMinContentsScale(MinContentsScale());
789 recording_source
->SetClearCanvasWithDebugColor(true);
792 color_paint
.setColor(test_color
);
793 // Additive paint, so that if two paints overlap, the color will change.
794 color_paint
.setXfermodeMode(SkXfermode::kPlus_Mode
);
795 // Paint outside the layer to make sure that blending works.
796 recording_source
->add_draw_rect_with_paint(
797 gfx::RectF(bigger_than_layer_bounds
), color_paint
);
798 recording_source
->RerecordPile();
800 scoped_refptr
<FakePicturePileImpl
> pile
=
801 FakePicturePileImpl::CreateFromPile(recording_source
.get(), nullptr);
802 gfx::Size
content_bounds(
803 gfx::ToCeiledSize(gfx::ScaleSize(layer_bounds
, contents_scale
)));
806 bitmap
.allocN32Pixels(content_bounds
.width(), content_bounds
.height());
807 SkCanvas
canvas(bitmap
);
809 pile
->PlaybackToCanvas(&canvas
, gfx::Rect(content_bounds
), contents_scale
);
811 for (int y
= 0; y
< bitmap
.height(); y
++) {
812 for (int x
= 0; x
< bitmap
.width(); x
++) {
813 SkColor color
= bitmap
.getColor(x
, y
);
814 EXPECT_EQ(SkColorGetR(test_color
), SkColorGetR(color
)) << "x: " << x
816 EXPECT_EQ(SkColorGetG(test_color
), SkColorGetG(color
)) << "x: " << x
818 EXPECT_EQ(SkColorGetB(test_color
), SkColorGetB(color
)) << "x: " << x
820 EXPECT_EQ(SkColorGetA(test_color
), SkColorGetA(color
)) << "x: " << x
822 if (test_color
!= color
)
828 INSTANTIATE_TEST_CASE_P(PicturePileImpl
,
830 ::testing::Values(1.f
, 0.873f
, 1.f
/ 4.f
, 4.f
));
832 TEST(PicturePileImplTest
, PixelRefIteratorBorders
) {
833 // 3 tile width / 1 tile height pile
834 gfx::Size
tile_size(128, 128);
835 gfx::Size
layer_bounds(320, 128);
837 // Fake picture pile uses a tile grid the size of the tile. So,
838 // any iteration that intersects with a tile will return all pixel refs
840 scoped_ptr
<FakePicturePile
> recording_source
=
841 FakePicturePile::CreateFilledPile(tile_size
, layer_bounds
);
842 recording_source
->SetMinContentsScale(0.5f
);
844 // Bitmaps 0-2 are exactly on tiles 0-2, so that they overlap the borders
845 // of adjacent tiles.
846 gfx::Rect bitmap_rects
[] = {
847 recording_source
->tiling().TileBounds(0, 0),
848 recording_source
->tiling().TileBounds(1, 0),
849 recording_source
->tiling().TileBounds(2, 0),
851 SkBitmap discardable_bitmap
[arraysize(bitmap_rects
)];
853 for (size_t i
= 0; i
< arraysize(bitmap_rects
); ++i
) {
854 CreateBitmap(bitmap_rects
[i
].size(), "discardable", &discardable_bitmap
[i
]);
855 recording_source
->add_draw_bitmap(discardable_bitmap
[i
],
856 bitmap_rects
[i
].origin());
859 recording_source
->RerecordPile();
861 scoped_refptr
<FakePicturePileImpl
> pile
=
862 FakePicturePileImpl::CreateFromPile(recording_source
.get(), nullptr);
864 // Sanity check that bitmaps 0-2 intersect the borders of their adjacent
865 // tiles, but not the actual tiles.
867 bitmap_rects
[0].Intersects(pile
->tiling().TileBoundsWithBorder(1, 0)));
868 EXPECT_FALSE(bitmap_rects
[0].Intersects(pile
->tiling().TileBounds(1, 0)));
870 bitmap_rects
[1].Intersects(pile
->tiling().TileBoundsWithBorder(0, 0)));
871 EXPECT_FALSE(bitmap_rects
[1].Intersects(pile
->tiling().TileBounds(0, 0)));
873 bitmap_rects
[1].Intersects(pile
->tiling().TileBoundsWithBorder(2, 0)));
874 EXPECT_FALSE(bitmap_rects
[1].Intersects(pile
->tiling().TileBounds(2, 0)));
876 bitmap_rects
[2].Intersects(pile
->tiling().TileBoundsWithBorder(1, 0)));
877 EXPECT_FALSE(bitmap_rects
[2].Intersects(pile
->tiling().TileBounds(1, 0)));
879 // Tile-sized iterators.
881 // Because tile 0's borders extend onto tile 1, it will include both
882 // bitmap 0 and 1. However, it should *not* include bitmap 2.
883 PicturePileImpl::PixelRefIterator
iterator(
884 pile
->tiling().TileBounds(0, 0), 1.f
, pile
.get());
885 EXPECT_TRUE(iterator
);
886 EXPECT_TRUE(*iterator
== discardable_bitmap
[0].pixelRef());
887 EXPECT_TRUE(++iterator
);
888 EXPECT_TRUE(*iterator
== discardable_bitmap
[1].pixelRef());
889 EXPECT_FALSE(++iterator
);
892 // Tile 1 + borders hits all bitmaps.
893 PicturePileImpl::PixelRefIterator
iterator(
894 pile
->tiling().TileBounds(1, 0), 1.f
, pile
.get());
895 EXPECT_TRUE(iterator
);
896 EXPECT_TRUE(*iterator
== discardable_bitmap
[0].pixelRef());
897 EXPECT_TRUE(++iterator
);
898 EXPECT_TRUE(*iterator
== discardable_bitmap
[1].pixelRef());
899 EXPECT_TRUE(++iterator
);
900 EXPECT_TRUE(*iterator
== discardable_bitmap
[2].pixelRef());
901 EXPECT_FALSE(++iterator
);
904 // Tile 2 should not include bitmap 0, which is only on tile 0 and the
905 // borders of tile 1.
906 PicturePileImpl::PixelRefIterator
iterator(
907 pile
->tiling().TileBounds(2, 0), 1.f
, pile
.get());
908 EXPECT_TRUE(iterator
);
909 EXPECT_TRUE(*iterator
== discardable_bitmap
[1].pixelRef());
910 EXPECT_TRUE(++iterator
);
911 EXPECT_TRUE(*iterator
== discardable_bitmap
[2].pixelRef());
912 EXPECT_FALSE(++iterator
);