Explicitly add python-numpy dependency to install-build-deps.
[chromium-blink-merge.git] / cc / resources / picture_pile_impl_unittest.cc
blob6e9afc7fda851cb6d642008763146066bc0e0422
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"
15 namespace cc {
16 namespace {
18 TEST(PicturePileImplTest, AnalyzeIsSolidUnscaled) {
19 gfx::Size tile_size(100, 100);
20 gfx::Size layer_bounds(400, 400);
22 scoped_refptr<FakePicturePileImpl> pile =
23 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
25 SkColor solid_color = SkColorSetARGB(255, 12, 23, 34);
26 SkPaint solid_paint;
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 pile->add_draw_rect_with_paint(gfx::Rect(0, 0, 400, 400), solid_paint);
34 pile->RerecordPile();
36 // Ensure everything is solid
37 for (int y = 0; y <= 300; y += 100) {
38 for (int x = 0; x <= 300; x += 100) {
39 RasterSource::SolidColorAnalysis analysis;
40 gfx::Rect rect(x, y, 100, 100);
41 pile->PerformSolidColorAnalysis(rect, 1.0, &analysis);
42 EXPECT_TRUE(analysis.is_solid_color) << rect.ToString();
43 EXPECT_EQ(analysis.solid_color, solid_color) << rect.ToString();
47 // One pixel non solid
48 pile->add_draw_rect_with_paint(gfx::Rect(50, 50, 1, 1), non_solid_paint);
49 pile->RerecordPile();
51 RasterSource::SolidColorAnalysis analysis;
52 pile->PerformSolidColorAnalysis(gfx::Rect(0, 0, 100, 100), 1.0, &analysis);
53 EXPECT_FALSE(analysis.is_solid_color);
55 pile->PerformSolidColorAnalysis(gfx::Rect(100, 0, 100, 100), 1.0, &analysis);
56 EXPECT_TRUE(analysis.is_solid_color);
57 EXPECT_EQ(analysis.solid_color, solid_color);
59 // Boundaries should be clipped
60 analysis.is_solid_color = false;
61 pile->PerformSolidColorAnalysis(gfx::Rect(350, 0, 100, 100), 1.0, &analysis);
62 EXPECT_TRUE(analysis.is_solid_color);
63 EXPECT_EQ(analysis.solid_color, solid_color);
65 analysis.is_solid_color = false;
66 pile->PerformSolidColorAnalysis(gfx::Rect(0, 350, 100, 100), 1.0, &analysis);
67 EXPECT_TRUE(analysis.is_solid_color);
68 EXPECT_EQ(analysis.solid_color, solid_color);
70 analysis.is_solid_color = false;
71 pile->PerformSolidColorAnalysis(gfx::Rect(350, 350, 100, 100), 1.0,
72 &analysis);
73 EXPECT_TRUE(analysis.is_solid_color);
74 EXPECT_EQ(analysis.solid_color, solid_color);
77 TEST(PicturePileImplTest, AnalyzeIsSolidScaled) {
78 gfx::Size tile_size(100, 100);
79 gfx::Size layer_bounds(400, 400);
81 scoped_refptr<FakePicturePileImpl> pile =
82 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
84 SkColor solid_color = SkColorSetARGB(255, 12, 23, 34);
85 SkPaint solid_paint;
86 solid_paint.setColor(solid_color);
88 SkColor non_solid_color = SkColorSetARGB(128, 45, 56, 67);
89 SkPaint non_solid_paint;
90 non_solid_paint.setColor(non_solid_color);
92 pile->add_draw_rect_with_paint(gfx::Rect(0, 0, 400, 400), solid_paint);
93 pile->RerecordPile();
95 // Ensure everything is solid
96 for (int y = 0; y <= 30; y += 10) {
97 for (int x = 0; x <= 30; x += 10) {
98 RasterSource::SolidColorAnalysis analysis;
99 gfx::Rect rect(x, y, 10, 10);
100 pile->PerformSolidColorAnalysis(rect, 0.1f, &analysis);
101 EXPECT_TRUE(analysis.is_solid_color) << rect.ToString();
102 EXPECT_EQ(analysis.solid_color, solid_color) << rect.ToString();
106 // One pixel non solid
107 pile->add_draw_rect_with_paint(gfx::Rect(50, 50, 1, 1), non_solid_paint);
108 pile->RerecordPile();
110 RasterSource::SolidColorAnalysis analysis;
111 pile->PerformSolidColorAnalysis(gfx::Rect(0, 0, 10, 10), 0.1f, &analysis);
112 EXPECT_FALSE(analysis.is_solid_color);
114 pile->PerformSolidColorAnalysis(gfx::Rect(10, 0, 10, 10), 0.1f, &analysis);
115 EXPECT_TRUE(analysis.is_solid_color);
116 EXPECT_EQ(analysis.solid_color, solid_color);
118 // Boundaries should be clipped
119 analysis.is_solid_color = false;
120 pile->PerformSolidColorAnalysis(gfx::Rect(35, 0, 10, 10), 0.1f, &analysis);
121 EXPECT_TRUE(analysis.is_solid_color);
122 EXPECT_EQ(analysis.solid_color, solid_color);
124 analysis.is_solid_color = false;
125 pile->PerformSolidColorAnalysis(gfx::Rect(0, 35, 10, 10), 0.1f, &analysis);
126 EXPECT_TRUE(analysis.is_solid_color);
127 EXPECT_EQ(analysis.solid_color, solid_color);
129 analysis.is_solid_color = false;
130 pile->PerformSolidColorAnalysis(gfx::Rect(35, 35, 10, 10), 0.1f, &analysis);
131 EXPECT_TRUE(analysis.is_solid_color);
132 EXPECT_EQ(analysis.solid_color, solid_color);
135 TEST(PicturePileImplTest, AnalyzeIsSolidEmpty) {
136 gfx::Size tile_size(100, 100);
137 gfx::Size layer_bounds(400, 400);
139 scoped_refptr<FakePicturePileImpl> pile =
140 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
141 RasterSource::SolidColorAnalysis analysis;
142 EXPECT_FALSE(analysis.is_solid_color);
144 pile->PerformSolidColorAnalysis(gfx::Rect(0, 0, 400, 400), 1.f, &analysis);
146 EXPECT_TRUE(analysis.is_solid_color);
147 EXPECT_EQ(analysis.solid_color, SkColorSetARGB(0, 0, 0, 0));
150 TEST(PicturePileImplTest, PixelRefIteratorEmpty) {
151 gfx::Size tile_size(128, 128);
152 gfx::Size layer_bounds(256, 256);
154 // Create a filled pile with no recording.
155 scoped_refptr<FakePicturePileImpl> pile =
156 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
158 // Tile sized iterators.
160 PicturePileImpl::PixelRefIterator iterator(
161 gfx::Rect(0, 0, 128, 128), 1.0, pile.get());
162 EXPECT_FALSE(iterator);
165 PicturePileImpl::PixelRefIterator iterator(
166 gfx::Rect(0, 0, 256, 256), 2.0, pile.get());
167 EXPECT_FALSE(iterator);
170 PicturePileImpl::PixelRefIterator iterator(
171 gfx::Rect(0, 0, 64, 64), 0.5, pile.get());
172 EXPECT_FALSE(iterator);
174 // Shifted tile sized iterators.
176 PicturePileImpl::PixelRefIterator iterator(
177 gfx::Rect(140, 140, 128, 128), 1.0, pile.get());
178 EXPECT_FALSE(iterator);
181 PicturePileImpl::PixelRefIterator iterator(
182 gfx::Rect(280, 280, 256, 256), 2.0, pile.get());
183 EXPECT_FALSE(iterator);
186 PicturePileImpl::PixelRefIterator iterator(
187 gfx::Rect(70, 70, 64, 64), 0.5, pile.get());
188 EXPECT_FALSE(iterator);
190 // Layer sized iterators.
192 PicturePileImpl::PixelRefIterator iterator(
193 gfx::Rect(0, 0, 256, 256), 1.0, pile.get());
194 EXPECT_FALSE(iterator);
197 PicturePileImpl::PixelRefIterator iterator(
198 gfx::Rect(0, 0, 512, 512), 2.0, pile.get());
199 EXPECT_FALSE(iterator);
202 PicturePileImpl::PixelRefIterator iterator(
203 gfx::Rect(0, 0, 128, 128), 0.5, pile.get());
204 EXPECT_FALSE(iterator);
208 TEST(PicturePileImplTest, PixelRefIteratorNoDiscardableRefs) {
209 gfx::Size tile_size(128, 128);
210 gfx::Size layer_bounds(256, 256);
212 scoped_refptr<FakePicturePileImpl> pile =
213 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
215 SkPaint simple_paint;
216 simple_paint.setColor(SkColorSetARGB(255, 12, 23, 34));
218 SkBitmap non_discardable_bitmap;
219 CreateBitmap(gfx::Size(128, 128), "notdiscardable", &non_discardable_bitmap);
221 pile->add_draw_rect_with_paint(gfx::Rect(0, 0, 256, 256), simple_paint);
222 pile->add_draw_rect_with_paint(gfx::Rect(128, 128, 512, 512), simple_paint);
223 pile->add_draw_rect_with_paint(gfx::Rect(512, 0, 256, 256), simple_paint);
224 pile->add_draw_rect_with_paint(gfx::Rect(0, 512, 256, 256), simple_paint);
225 pile->add_draw_bitmap(non_discardable_bitmap, gfx::Point(128, 0));
226 pile->add_draw_bitmap(non_discardable_bitmap, gfx::Point(0, 128));
227 pile->add_draw_bitmap(non_discardable_bitmap, gfx::Point(150, 150));
229 pile->RerecordPile();
231 // Tile sized iterators.
233 PicturePileImpl::PixelRefIterator iterator(
234 gfx::Rect(0, 0, 128, 128), 1.0, pile.get());
235 EXPECT_FALSE(iterator);
238 PicturePileImpl::PixelRefIterator iterator(
239 gfx::Rect(0, 0, 256, 256), 2.0, pile.get());
240 EXPECT_FALSE(iterator);
243 PicturePileImpl::PixelRefIterator iterator(
244 gfx::Rect(0, 0, 64, 64), 0.5, pile.get());
245 EXPECT_FALSE(iterator);
247 // Shifted tile sized iterators.
249 PicturePileImpl::PixelRefIterator iterator(
250 gfx::Rect(140, 140, 128, 128), 1.0, pile.get());
251 EXPECT_FALSE(iterator);
254 PicturePileImpl::PixelRefIterator iterator(
255 gfx::Rect(280, 280, 256, 256), 2.0, pile.get());
256 EXPECT_FALSE(iterator);
259 PicturePileImpl::PixelRefIterator iterator(
260 gfx::Rect(70, 70, 64, 64), 0.5, pile.get());
261 EXPECT_FALSE(iterator);
263 // Layer sized iterators.
265 PicturePileImpl::PixelRefIterator iterator(
266 gfx::Rect(0, 0, 256, 256), 1.0, pile.get());
267 EXPECT_FALSE(iterator);
270 PicturePileImpl::PixelRefIterator iterator(
271 gfx::Rect(0, 0, 512, 512), 2.0, pile.get());
272 EXPECT_FALSE(iterator);
275 PicturePileImpl::PixelRefIterator iterator(
276 gfx::Rect(0, 0, 128, 128), 0.5, pile.get());
277 EXPECT_FALSE(iterator);
281 TEST(PicturePileImplTest, PixelRefIteratorDiscardableRefs) {
282 gfx::Size tile_size(128, 128);
283 gfx::Size layer_bounds(256, 256);
285 scoped_refptr<FakePicturePileImpl> pile =
286 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
288 SkBitmap discardable_bitmap[2][2];
289 CreateBitmap(gfx::Size(32, 32), "discardable", &discardable_bitmap[0][0]);
290 CreateBitmap(gfx::Size(32, 32), "discardable", &discardable_bitmap[1][0]);
291 CreateBitmap(gfx::Size(32, 32), "discardable", &discardable_bitmap[1][1]);
293 // Discardable pixel refs are found in the following cells:
294 // |---|---|
295 // | x | |
296 // |---|---|
297 // | x | x |
298 // |---|---|
299 pile->add_draw_bitmap(discardable_bitmap[0][0], gfx::Point(0, 0));
300 pile->add_draw_bitmap(discardable_bitmap[1][0], gfx::Point(0, 130));
301 pile->add_draw_bitmap(discardable_bitmap[1][1], gfx::Point(140, 140));
303 pile->RerecordPile();
305 // Tile sized iterators. These should find only one pixel ref.
307 PicturePileImpl::PixelRefIterator iterator(
308 gfx::Rect(0, 0, 128, 128), 1.0, pile.get());
309 EXPECT_TRUE(iterator);
310 EXPECT_TRUE(*iterator == discardable_bitmap[0][0].pixelRef());
311 EXPECT_FALSE(++iterator);
314 PicturePileImpl::PixelRefIterator iterator(
315 gfx::Rect(0, 0, 256, 256), 2.0, pile.get());
316 EXPECT_TRUE(iterator);
317 EXPECT_TRUE(*iterator == discardable_bitmap[0][0].pixelRef());
318 EXPECT_FALSE(++iterator);
321 PicturePileImpl::PixelRefIterator iterator(
322 gfx::Rect(0, 0, 64, 64), 0.5, pile.get());
323 EXPECT_TRUE(iterator);
324 EXPECT_TRUE(*iterator == discardable_bitmap[0][0].pixelRef());
325 EXPECT_FALSE(++iterator);
327 // Shifted tile sized iterators. These should find only one pixel ref.
329 PicturePileImpl::PixelRefIterator iterator(
330 gfx::Rect(140, 140, 128, 128), 1.0, pile.get());
331 EXPECT_TRUE(iterator);
332 EXPECT_TRUE(*iterator == discardable_bitmap[1][1].pixelRef());
333 EXPECT_FALSE(++iterator);
336 PicturePileImpl::PixelRefIterator iterator(
337 gfx::Rect(280, 280, 256, 256), 2.0, pile.get());
338 EXPECT_TRUE(iterator);
339 EXPECT_TRUE(*iterator == discardable_bitmap[1][1].pixelRef());
340 EXPECT_FALSE(++iterator);
343 PicturePileImpl::PixelRefIterator iterator(
344 gfx::Rect(70, 70, 64, 64), 0.5, pile.get());
345 EXPECT_TRUE(iterator);
346 EXPECT_TRUE(*iterator == discardable_bitmap[1][1].pixelRef());
347 EXPECT_FALSE(++iterator);
349 // Ensure there's no discardable pixel refs in the empty cell
351 PicturePileImpl::PixelRefIterator iterator(
352 gfx::Rect(140, 0, 128, 128), 1.0, pile.get());
353 EXPECT_FALSE(iterator);
355 // Layer sized iterators. These should find all 3 pixel refs.
357 PicturePileImpl::PixelRefIterator iterator(
358 gfx::Rect(0, 0, 256, 256), 1.0, pile.get());
359 EXPECT_TRUE(iterator);
360 EXPECT_TRUE(*iterator == discardable_bitmap[0][0].pixelRef());
361 EXPECT_TRUE(++iterator);
362 EXPECT_TRUE(*iterator == discardable_bitmap[1][0].pixelRef());
363 EXPECT_TRUE(++iterator);
364 EXPECT_TRUE(*iterator == discardable_bitmap[1][1].pixelRef());
365 EXPECT_FALSE(++iterator);
368 PicturePileImpl::PixelRefIterator iterator(
369 gfx::Rect(0, 0, 512, 512), 2.0, pile.get());
370 EXPECT_TRUE(iterator);
371 EXPECT_TRUE(*iterator == discardable_bitmap[0][0].pixelRef());
372 EXPECT_TRUE(++iterator);
373 EXPECT_TRUE(*iterator == discardable_bitmap[1][0].pixelRef());
374 EXPECT_TRUE(++iterator);
375 EXPECT_TRUE(*iterator == discardable_bitmap[1][1].pixelRef());
376 EXPECT_FALSE(++iterator);
379 PicturePileImpl::PixelRefIterator iterator(
380 gfx::Rect(0, 0, 128, 128), 0.5, 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);
391 TEST(PicturePileImplTest, PixelRefIteratorDiscardableRefsOneTile) {
392 gfx::Size tile_size(256, 256);
393 gfx::Size layer_bounds(512, 512);
395 scoped_refptr<FakePicturePileImpl> pile =
396 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
398 SkBitmap discardable_bitmap[2][2];
399 CreateBitmap(gfx::Size(32, 32), "discardable", &discardable_bitmap[0][0]);
400 CreateBitmap(gfx::Size(32, 32), "discardable", &discardable_bitmap[0][1]);
401 CreateBitmap(gfx::Size(32, 32), "discardable", &discardable_bitmap[1][1]);
403 // Discardable pixel refs are found in the following cells:
404 // |---|---|
405 // | x | x |
406 // |---|---|
407 // | | x |
408 // |---|---|
409 pile->add_draw_bitmap(discardable_bitmap[0][0], gfx::Point(0, 0));
410 pile->add_draw_bitmap(discardable_bitmap[0][1], gfx::Point(260, 0));
411 pile->add_draw_bitmap(discardable_bitmap[1][1], gfx::Point(260, 260));
413 pile->RerecordPile();
415 // Tile sized iterators. These should find only one pixel ref.
417 PicturePileImpl::PixelRefIterator iterator(
418 gfx::Rect(0, 0, 256, 256), 1.0, pile.get());
419 EXPECT_TRUE(iterator);
420 EXPECT_TRUE(*iterator == discardable_bitmap[0][0].pixelRef());
421 EXPECT_FALSE(++iterator);
424 PicturePileImpl::PixelRefIterator iterator(
425 gfx::Rect(0, 0, 512, 512), 2.0, pile.get());
426 EXPECT_TRUE(iterator);
427 EXPECT_TRUE(*iterator == discardable_bitmap[0][0].pixelRef());
428 EXPECT_FALSE(++iterator);
431 PicturePileImpl::PixelRefIterator iterator(
432 gfx::Rect(0, 0, 128, 128), 0.5, pile.get());
433 EXPECT_TRUE(iterator);
434 EXPECT_TRUE(*iterator == discardable_bitmap[0][0].pixelRef());
435 EXPECT_FALSE(++iterator);
437 // Shifted tile sized iterators. These should find only one pixel ref.
439 PicturePileImpl::PixelRefIterator iterator(
440 gfx::Rect(260, 260, 256, 256), 1.0, pile.get());
441 EXPECT_TRUE(iterator);
442 EXPECT_TRUE(*iterator == discardable_bitmap[1][1].pixelRef());
443 EXPECT_FALSE(++iterator);
446 PicturePileImpl::PixelRefIterator iterator(
447 gfx::Rect(520, 520, 512, 512), 2.0, pile.get());
448 EXPECT_TRUE(iterator);
449 EXPECT_TRUE(*iterator == discardable_bitmap[1][1].pixelRef());
450 EXPECT_FALSE(++iterator);
453 PicturePileImpl::PixelRefIterator iterator(
454 gfx::Rect(130, 130, 128, 128), 0.5, pile.get());
455 EXPECT_TRUE(iterator);
456 EXPECT_TRUE(*iterator == discardable_bitmap[1][1].pixelRef());
457 EXPECT_FALSE(++iterator);
459 // Ensure there's no discardable pixel refs in the empty cell
461 PicturePileImpl::PixelRefIterator iterator(
462 gfx::Rect(0, 256, 256, 256), 1.0, pile.get());
463 EXPECT_FALSE(iterator);
465 // Layer sized iterators. These should find three pixel ref.
467 PicturePileImpl::PixelRefIterator iterator(
468 gfx::Rect(0, 0, 512, 512), 1.0, pile.get());
469 EXPECT_TRUE(iterator);
470 EXPECT_TRUE(*iterator == discardable_bitmap[0][0].pixelRef());
471 EXPECT_TRUE(++iterator);
472 EXPECT_TRUE(*iterator == discardable_bitmap[0][1].pixelRef());
473 EXPECT_TRUE(++iterator);
474 EXPECT_TRUE(*iterator == discardable_bitmap[1][1].pixelRef());
475 EXPECT_FALSE(++iterator);
478 PicturePileImpl::PixelRefIterator iterator(
479 gfx::Rect(0, 0, 1024, 1024), 2.0, pile.get());
480 EXPECT_TRUE(iterator);
481 EXPECT_TRUE(*iterator == discardable_bitmap[0][0].pixelRef());
482 EXPECT_TRUE(++iterator);
483 EXPECT_TRUE(*iterator == discardable_bitmap[0][1].pixelRef());
484 EXPECT_TRUE(++iterator);
485 EXPECT_TRUE(*iterator == discardable_bitmap[1][1].pixelRef());
486 EXPECT_FALSE(++iterator);
489 PicturePileImpl::PixelRefIterator iterator(
490 gfx::Rect(0, 0, 256, 256), 0.5, pile.get());
491 EXPECT_TRUE(iterator);
492 EXPECT_TRUE(*iterator == discardable_bitmap[0][0].pixelRef());
493 EXPECT_TRUE(++iterator);
494 EXPECT_TRUE(*iterator == discardable_bitmap[0][1].pixelRef());
495 EXPECT_TRUE(++iterator);
496 EXPECT_TRUE(*iterator == discardable_bitmap[1][1].pixelRef());
497 EXPECT_FALSE(++iterator);
500 // Copy test.
501 PicturePileImpl::PixelRefIterator iterator(
502 gfx::Rect(0, 0, 512, 512), 1.0, pile.get());
503 EXPECT_TRUE(iterator);
504 EXPECT_TRUE(*iterator == discardable_bitmap[0][0].pixelRef());
505 EXPECT_TRUE(++iterator);
506 EXPECT_TRUE(*iterator == discardable_bitmap[0][1].pixelRef());
508 // copy now points to the same spot as iterator,
509 // but both can be incremented independently.
510 PicturePileImpl::PixelRefIterator copy = iterator;
511 EXPECT_TRUE(++iterator);
512 EXPECT_TRUE(*iterator == discardable_bitmap[1][1].pixelRef());
513 EXPECT_FALSE(++iterator);
515 EXPECT_TRUE(copy);
516 EXPECT_TRUE(*copy == discardable_bitmap[0][1].pixelRef());
517 EXPECT_TRUE(++copy);
518 EXPECT_TRUE(*copy == discardable_bitmap[1][1].pixelRef());
519 EXPECT_FALSE(++copy);
522 TEST(PicturePileImplTest, PixelRefIteratorDiscardableRefsBaseNonDiscardable) {
523 gfx::Size tile_size(256, 256);
524 gfx::Size layer_bounds(512, 512);
526 scoped_refptr<FakePicturePileImpl> pile =
527 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
529 SkBitmap non_discardable_bitmap;
530 CreateBitmap(gfx::Size(512, 512), "notdiscardable", &non_discardable_bitmap);
532 SkBitmap discardable_bitmap[2][2];
533 CreateBitmap(gfx::Size(128, 128), "discardable", &discardable_bitmap[0][0]);
534 CreateBitmap(gfx::Size(128, 128), "discardable", &discardable_bitmap[0][1]);
535 CreateBitmap(gfx::Size(128, 128), "discardable", &discardable_bitmap[1][1]);
537 // One large non-discardable bitmap covers the whole grid.
538 // Discardable pixel refs are found in the following cells:
539 // |---|---|
540 // | x | x |
541 // |---|---|
542 // | | x |
543 // |---|---|
544 pile->add_draw_bitmap(non_discardable_bitmap, gfx::Point(0, 0));
545 pile->add_draw_bitmap(discardable_bitmap[0][0], gfx::Point(0, 0));
546 pile->add_draw_bitmap(discardable_bitmap[0][1], gfx::Point(260, 0));
547 pile->add_draw_bitmap(discardable_bitmap[1][1], gfx::Point(260, 260));
549 pile->RerecordPile();
551 // Tile sized iterators. These should find only one pixel ref.
553 PicturePileImpl::PixelRefIterator iterator(
554 gfx::Rect(0, 0, 256, 256), 1.0, pile.get());
555 EXPECT_TRUE(iterator);
556 EXPECT_TRUE(*iterator == discardable_bitmap[0][0].pixelRef());
557 EXPECT_FALSE(++iterator);
560 PicturePileImpl::PixelRefIterator iterator(
561 gfx::Rect(0, 0, 512, 512), 2.0, pile.get());
562 EXPECT_TRUE(iterator);
563 EXPECT_TRUE(*iterator == discardable_bitmap[0][0].pixelRef());
564 EXPECT_FALSE(++iterator);
567 PicturePileImpl::PixelRefIterator iterator(
568 gfx::Rect(0, 0, 128, 128), 0.5, pile.get());
569 EXPECT_TRUE(iterator);
570 EXPECT_TRUE(*iterator == discardable_bitmap[0][0].pixelRef());
571 EXPECT_FALSE(++iterator);
573 // Shifted tile sized iterators. These should find only one pixel ref.
575 PicturePileImpl::PixelRefIterator iterator(
576 gfx::Rect(260, 260, 256, 256), 1.0, pile.get());
577 EXPECT_TRUE(iterator);
578 EXPECT_TRUE(*iterator == discardable_bitmap[1][1].pixelRef());
579 EXPECT_FALSE(++iterator);
582 PicturePileImpl::PixelRefIterator iterator(
583 gfx::Rect(520, 520, 512, 512), 2.0, pile.get());
584 EXPECT_TRUE(iterator);
585 EXPECT_TRUE(*iterator == discardable_bitmap[1][1].pixelRef());
586 EXPECT_FALSE(++iterator);
589 PicturePileImpl::PixelRefIterator iterator(
590 gfx::Rect(130, 130, 128, 128), 0.5, pile.get());
591 EXPECT_TRUE(iterator);
592 EXPECT_TRUE(*iterator == discardable_bitmap[1][1].pixelRef());
593 EXPECT_FALSE(++iterator);
595 // Ensure there's no discardable pixel refs in the empty cell
597 PicturePileImpl::PixelRefIterator iterator(
598 gfx::Rect(0, 256, 256, 256), 1.0, pile.get());
599 EXPECT_FALSE(iterator);
601 // Layer sized iterators. These should find three pixel ref.
603 PicturePileImpl::PixelRefIterator iterator(
604 gfx::Rect(0, 0, 512, 512), 1.0, pile.get());
605 EXPECT_TRUE(iterator);
606 EXPECT_TRUE(*iterator == discardable_bitmap[0][0].pixelRef());
607 EXPECT_TRUE(++iterator);
608 EXPECT_TRUE(*iterator == discardable_bitmap[0][1].pixelRef());
609 EXPECT_TRUE(++iterator);
610 EXPECT_TRUE(*iterator == discardable_bitmap[1][1].pixelRef());
611 EXPECT_FALSE(++iterator);
614 PicturePileImpl::PixelRefIterator iterator(
615 gfx::Rect(0, 0, 1024, 1024), 2.0, pile.get());
616 EXPECT_TRUE(iterator);
617 EXPECT_TRUE(*iterator == discardable_bitmap[0][0].pixelRef());
618 EXPECT_TRUE(++iterator);
619 EXPECT_TRUE(*iterator == discardable_bitmap[0][1].pixelRef());
620 EXPECT_TRUE(++iterator);
621 EXPECT_TRUE(*iterator == discardable_bitmap[1][1].pixelRef());
622 EXPECT_FALSE(++iterator);
625 PicturePileImpl::PixelRefIterator iterator(
626 gfx::Rect(0, 0, 256, 256), 0.5, pile.get());
627 EXPECT_TRUE(iterator);
628 EXPECT_TRUE(*iterator == discardable_bitmap[0][0].pixelRef());
629 EXPECT_TRUE(++iterator);
630 EXPECT_TRUE(*iterator == discardable_bitmap[0][1].pixelRef());
631 EXPECT_TRUE(++iterator);
632 EXPECT_TRUE(*iterator == discardable_bitmap[1][1].pixelRef());
633 EXPECT_FALSE(++iterator);
637 TEST(PicturePileImplTest, RasterFullContents) {
638 gfx::Size tile_size(1000, 1000);
639 gfx::Size layer_bounds(3, 5);
640 float contents_scale = 1.5f;
641 float raster_divisions = 2.f;
643 scoped_refptr<FakePicturePileImpl> pile =
644 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
645 // Because the caller sets content opaque, it also promises that it
646 // has at least filled in layer_bounds opaquely.
647 SkPaint white_paint;
648 white_paint.setColor(SK_ColorWHITE);
649 pile->add_draw_rect_with_paint(gfx::Rect(layer_bounds), white_paint);
651 pile->SetMinContentsScale(contents_scale);
652 pile->set_background_color(SK_ColorBLACK);
653 pile->SetRequiresClear(false);
654 pile->set_clear_canvas_with_debug_color(false);
655 pile->RerecordPile();
657 gfx::Size content_bounds(
658 gfx::ToCeiledSize(gfx::ScaleSize(layer_bounds, contents_scale)));
660 // Simulate drawing into different tiles at different offsets.
661 int step_x = std::ceil(content_bounds.width() / raster_divisions);
662 int step_y = std::ceil(content_bounds.height() / raster_divisions);
663 for (int offset_x = 0; offset_x < content_bounds.width();
664 offset_x += step_x) {
665 for (int offset_y = 0; offset_y < content_bounds.height();
666 offset_y += step_y) {
667 gfx::Rect content_rect(offset_x, offset_y, step_x, step_y);
668 content_rect.Intersect(gfx::Rect(content_bounds));
670 // Simulate a canvas rect larger than the content rect. Every pixel
671 // up to one pixel outside the content rect is guaranteed to be opaque.
672 // Outside of that is undefined.
673 gfx::Rect canvas_rect(content_rect);
674 canvas_rect.Inset(0, 0, -1, -1);
676 SkBitmap bitmap;
677 bitmap.allocN32Pixels(canvas_rect.width(), canvas_rect.height());
678 SkCanvas canvas(bitmap);
679 canvas.clear(SK_ColorTRANSPARENT);
681 pile->PlaybackToCanvas(&canvas, canvas_rect, contents_scale);
683 SkColor* pixels = reinterpret_cast<SkColor*>(bitmap.getPixels());
684 int num_pixels = bitmap.width() * bitmap.height();
685 bool all_white = true;
686 for (int i = 0; i < num_pixels; ++i) {
687 EXPECT_EQ(SkColorGetA(pixels[i]), 255u);
688 all_white &= (SkColorGetR(pixels[i]) == 255);
689 all_white &= (SkColorGetG(pixels[i]) == 255);
690 all_white &= (SkColorGetB(pixels[i]) == 255);
693 // If the canvas doesn't extend past the edge of the content,
694 // it should be entirely white. Otherwise, the edge of the content
695 // will be non-white.
696 EXPECT_EQ(all_white, gfx::Rect(content_bounds).Contains(canvas_rect));
701 TEST(PicturePileImpl, RasterContentsTransparent) {
702 gfx::Size tile_size(1000, 1000);
703 gfx::Size layer_bounds(5, 3);
704 float contents_scale = 0.5f;
706 scoped_refptr<FakePicturePileImpl> pile =
707 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
708 pile->set_background_color(SK_ColorTRANSPARENT);
709 pile->SetRequiresClear(true);
710 pile->SetMinContentsScale(contents_scale);
711 pile->set_clear_canvas_with_debug_color(false);
712 pile->RerecordPile();
714 gfx::Size content_bounds(
715 gfx::ToCeiledSize(gfx::ScaleSize(layer_bounds, contents_scale)));
717 gfx::Rect canvas_rect(content_bounds);
718 canvas_rect.Inset(0, 0, -1, -1);
720 SkBitmap bitmap;
721 bitmap.allocN32Pixels(canvas_rect.width(), canvas_rect.height());
722 SkCanvas canvas(bitmap);
724 pile->PlaybackToCanvas(&canvas, canvas_rect, contents_scale);
726 SkColor* pixels = reinterpret_cast<SkColor*>(bitmap.getPixels());
727 int num_pixels = bitmap.width() * bitmap.height();
728 for (int i = 0; i < num_pixels; ++i) {
729 EXPECT_EQ(SkColorGetA(pixels[i]), 0u);
733 class OverlapTest : public ::testing::TestWithParam<float> {
734 public:
735 static float MinContentsScale() { return 1.f / 4.f; }
738 TEST_P(OverlapTest, NoOverlap) {
739 gfx::Size tile_size(10, 10);
740 gfx::Size layer_bounds(30, 30);
741 gfx::Size bigger_than_layer_bounds(300, 300);
742 float contents_scale = GetParam();
743 // Pick an opaque color to not have to deal with premultiplication off-by-one.
744 SkColor test_color = SkColorSetARGB(255, 45, 56, 67);
746 scoped_refptr<FakePicturePileImpl> pile =
747 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
748 pile->set_background_color(SK_ColorTRANSPARENT);
749 pile->SetRequiresClear(true);
750 pile->SetMinContentsScale(MinContentsScale());
751 pile->set_clear_canvas_with_debug_color(true);
752 SkPaint color_paint;
753 color_paint.setColor(test_color);
754 // Additive paint, so that if two paints overlap, the color will change.
755 color_paint.setXfermodeMode(SkXfermode::kPlus_Mode);
756 // Paint outside the layer to make sure that blending works.
757 pile->add_draw_rect_with_paint(gfx::RectF(bigger_than_layer_bounds),
758 color_paint);
759 pile->RerecordPile();
761 gfx::Size content_bounds(
762 gfx::ToCeiledSize(gfx::ScaleSize(layer_bounds, contents_scale)));
764 SkBitmap bitmap;
765 bitmap.allocN32Pixels(content_bounds.width(), content_bounds.height());
766 SkCanvas canvas(bitmap);
768 pile->PlaybackToCanvas(&canvas, gfx::Rect(content_bounds), contents_scale);
770 for (int y = 0; y < bitmap.height(); y++) {
771 for (int x = 0; x < bitmap.width(); x++) {
772 SkColor color = bitmap.getColor(x, y);
773 EXPECT_EQ(SkColorGetR(test_color), SkColorGetR(color)) << "x: " << x
774 << ", y: " << y;
775 EXPECT_EQ(SkColorGetG(test_color), SkColorGetG(color)) << "x: " << x
776 << ", y: " << y;
777 EXPECT_EQ(SkColorGetB(test_color), SkColorGetB(color)) << "x: " << x
778 << ", y: " << y;
779 EXPECT_EQ(SkColorGetA(test_color), SkColorGetA(color)) << "x: " << x
780 << ", y: " << y;
781 if (test_color != color)
782 break;
787 INSTANTIATE_TEST_CASE_P(PicturePileImpl,
788 OverlapTest,
789 ::testing::Values(1.f, 0.873f, 1.f / 4.f, 4.f));
791 TEST(PicturePileImplTest, PixelRefIteratorBorders) {
792 // 3 tile width / 1 tile height pile
793 gfx::Size tile_size(128, 128);
794 gfx::Size layer_bounds(320, 128);
796 // Fake picture pile impl uses a tile grid the size of the tile. So,
797 // any iteration that intersects with a tile will return all pixel refs
798 // inside of it.
799 scoped_refptr<FakePicturePileImpl> pile =
800 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
801 pile->SetMinContentsScale(0.5f);
803 // Bitmaps 0-2 are exactly on tiles 0-2, so that they overlap the borders
804 // of adjacent tiles.
805 gfx::Rect bitmap_rects[] = {pile->tiling().TileBounds(0, 0),
806 pile->tiling().TileBounds(1, 0),
807 pile->tiling().TileBounds(2, 0), };
808 SkBitmap discardable_bitmap[arraysize(bitmap_rects)];
810 for (size_t i = 0; i < arraysize(bitmap_rects); ++i) {
811 CreateBitmap(bitmap_rects[i].size(), "discardable", &discardable_bitmap[i]);
812 pile->add_draw_bitmap(discardable_bitmap[i], bitmap_rects[i].origin());
815 // Sanity check that bitmaps 0-2 intersect the borders of their adjacent
816 // tiles, but not the actual tiles.
817 EXPECT_TRUE(
818 bitmap_rects[0].Intersects(pile->tiling().TileBoundsWithBorder(1, 0)));
819 EXPECT_FALSE(bitmap_rects[0].Intersects(pile->tiling().TileBounds(1, 0)));
820 EXPECT_TRUE(
821 bitmap_rects[1].Intersects(pile->tiling().TileBoundsWithBorder(0, 0)));
822 EXPECT_FALSE(bitmap_rects[1].Intersects(pile->tiling().TileBounds(0, 0)));
823 EXPECT_TRUE(
824 bitmap_rects[1].Intersects(pile->tiling().TileBoundsWithBorder(2, 0)));
825 EXPECT_FALSE(bitmap_rects[1].Intersects(pile->tiling().TileBounds(2, 0)));
826 EXPECT_TRUE(
827 bitmap_rects[2].Intersects(pile->tiling().TileBoundsWithBorder(1, 0)));
828 EXPECT_FALSE(bitmap_rects[2].Intersects(pile->tiling().TileBounds(1, 0)));
830 pile->RerecordPile();
832 // Tile-sized iterators.
834 // Because tile 0's borders extend onto tile 1, it will include both
835 // bitmap 0 and 1. However, it should *not* include bitmap 2.
836 PicturePileImpl::PixelRefIterator iterator(
837 pile->tiling().TileBounds(0, 0), 1.f, pile.get());
838 EXPECT_TRUE(iterator);
839 EXPECT_TRUE(*iterator == discardable_bitmap[0].pixelRef());
840 EXPECT_TRUE(++iterator);
841 EXPECT_TRUE(*iterator == discardable_bitmap[1].pixelRef());
842 EXPECT_FALSE(++iterator);
845 // Tile 1 + borders hits all bitmaps.
846 PicturePileImpl::PixelRefIterator iterator(
847 pile->tiling().TileBounds(1, 0), 1.f, pile.get());
848 EXPECT_TRUE(iterator);
849 EXPECT_TRUE(*iterator == discardable_bitmap[0].pixelRef());
850 EXPECT_TRUE(++iterator);
851 EXPECT_TRUE(*iterator == discardable_bitmap[1].pixelRef());
852 EXPECT_TRUE(++iterator);
853 EXPECT_TRUE(*iterator == discardable_bitmap[2].pixelRef());
854 EXPECT_FALSE(++iterator);
857 // Tile 2 should not include bitmap 0, which is only on tile 0 and the
858 // borders of tile 1.
859 PicturePileImpl::PixelRefIterator iterator(
860 pile->tiling().TileBounds(2, 0), 1.f, pile.get());
861 EXPECT_TRUE(iterator);
862 EXPECT_TRUE(*iterator == discardable_bitmap[1].pixelRef());
863 EXPECT_TRUE(++iterator);
864 EXPECT_TRUE(*iterator == discardable_bitmap[2].pixelRef());
865 EXPECT_FALSE(++iterator);
869 } // namespace
870 } // namespace cc