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 "base/memory/scoped_ptr.h"
6 #include "cc/test/fake_picture_pile_impl.h"
7 #include "cc/test/fake_rendering_stats_instrumentation.h"
8 #include "cc/test/skia_common.h"
9 #include "skia/ext/lazy_pixel_ref.h"
10 #include "skia/ext/refptr.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "third_party/skia/include/core/SkPixelRef.h"
13 #include "third_party/skia/include/core/SkShader.h"
14 #include "ui/gfx/rect.h"
15 #include "ui/gfx/size_conversions.h"
16
17 namespace cc {
18 namespace {
19
TEST(PicturePileImplTest,AnalyzeIsSolidUnscaled)20 TEST(PicturePileImplTest, AnalyzeIsSolidUnscaled) {
21 gfx::Size tile_size(100, 100);
22 gfx::Size layer_bounds(400, 400);
23
24 scoped_refptr<FakePicturePileImpl> pile =
25 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
26
27 SkColor solid_color = SkColorSetARGB(255, 12, 23, 34);
28 SkPaint solid_paint;
29 solid_paint.setColor(solid_color);
30
31 SkColor non_solid_color = SkColorSetARGB(128, 45, 56, 67);
32 SkPaint non_solid_paint;
33 non_solid_paint.setColor(non_solid_color);
34
35 pile->add_draw_rect_with_paint(gfx::Rect(0, 0, 400, 400), solid_paint);
36 pile->RerecordPile();
37
38 // Ensure everything is solid
39 for (int y = 0; y <= 300; y += 100) {
40 for (int x = 0; x <= 300; x += 100) {
41 PicturePileImpl::Analysis analysis;
42 gfx::Rect rect(x, y, 100, 100);
43 pile->AnalyzeInRect(rect, 1.0, &analysis);
44 EXPECT_TRUE(analysis.is_solid_color) << rect.ToString();
45 EXPECT_EQ(analysis.solid_color, solid_color) << rect.ToString();
46 }
47 }
48
49 // One pixel non solid
50 pile->add_draw_rect_with_paint(gfx::Rect(50, 50, 1, 1), non_solid_paint);
51 pile->RerecordPile();
52
53 PicturePileImpl::Analysis analysis;
54 pile->AnalyzeInRect(gfx::Rect(0, 0, 100, 100), 1.0, &analysis);
55 EXPECT_FALSE(analysis.is_solid_color);
56
57 pile->AnalyzeInRect(gfx::Rect(100, 0, 100, 100), 1.0, &analysis);
58 EXPECT_TRUE(analysis.is_solid_color);
59 EXPECT_EQ(analysis.solid_color, solid_color);
60
61 // Boundaries should be clipped
62 analysis.is_solid_color = false;
63 pile->AnalyzeInRect(gfx::Rect(350, 0, 100, 100), 1.0, &analysis);
64 EXPECT_TRUE(analysis.is_solid_color);
65 EXPECT_EQ(analysis.solid_color, solid_color);
66
67 analysis.is_solid_color = false;
68 pile->AnalyzeInRect(gfx::Rect(0, 350, 100, 100), 1.0, &analysis);
69 EXPECT_TRUE(analysis.is_solid_color);
70 EXPECT_EQ(analysis.solid_color, solid_color);
71
72 analysis.is_solid_color = false;
73 pile->AnalyzeInRect(gfx::Rect(350, 350, 100, 100), 1.0, &analysis);
74 EXPECT_TRUE(analysis.is_solid_color);
75 EXPECT_EQ(analysis.solid_color, solid_color);
76 }
77
TEST(PicturePileImplTest,AnalyzeIsSolidScaled)78 TEST(PicturePileImplTest, AnalyzeIsSolidScaled) {
79 gfx::Size tile_size(100, 100);
80 gfx::Size layer_bounds(400, 400);
81
82 scoped_refptr<FakePicturePileImpl> pile =
83 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
84
85 SkColor solid_color = SkColorSetARGB(255, 12, 23, 34);
86 SkPaint solid_paint;
87 solid_paint.setColor(solid_color);
88
89 SkColor non_solid_color = SkColorSetARGB(128, 45, 56, 67);
90 SkPaint non_solid_paint;
91 non_solid_paint.setColor(non_solid_color);
92
93 pile->add_draw_rect_with_paint(gfx::Rect(0, 0, 400, 400), solid_paint);
94 pile->RerecordPile();
95
96 // Ensure everything is solid
97 for (int y = 0; y <= 30; y += 10) {
98 for (int x = 0; x <= 30; x += 10) {
99 PicturePileImpl::Analysis analysis;
100 gfx::Rect rect(x, y, 10, 10);
101 pile->AnalyzeInRect(rect, 0.1f, &analysis);
102 EXPECT_TRUE(analysis.is_solid_color) << rect.ToString();
103 EXPECT_EQ(analysis.solid_color, solid_color) << rect.ToString();
104 }
105 }
106
107 // One pixel non solid
108 pile->add_draw_rect_with_paint(gfx::Rect(50, 50, 1, 1), non_solid_paint);
109 pile->RerecordPile();
110
111 PicturePileImpl::Analysis analysis;
112 pile->AnalyzeInRect(gfx::Rect(0, 0, 10, 10), 0.1f, &analysis);
113 EXPECT_FALSE(analysis.is_solid_color);
114
115 pile->AnalyzeInRect(gfx::Rect(10, 0, 10, 10), 0.1f, &analysis);
116 EXPECT_TRUE(analysis.is_solid_color);
117 EXPECT_EQ(analysis.solid_color, solid_color);
118
119 // Boundaries should be clipped
120 analysis.is_solid_color = false;
121 pile->AnalyzeInRect(gfx::Rect(35, 0, 10, 10), 0.1f, &analysis);
122 EXPECT_TRUE(analysis.is_solid_color);
123 EXPECT_EQ(analysis.solid_color, solid_color);
124
125 analysis.is_solid_color = false;
126 pile->AnalyzeInRect(gfx::Rect(0, 35, 10, 10), 0.1f, &analysis);
127 EXPECT_TRUE(analysis.is_solid_color);
128 EXPECT_EQ(analysis.solid_color, solid_color);
129
130 analysis.is_solid_color = false;
131 pile->AnalyzeInRect(gfx::Rect(35, 35, 10, 10), 0.1f, &analysis);
132 EXPECT_TRUE(analysis.is_solid_color);
133 EXPECT_EQ(analysis.solid_color, solid_color);
134 }
135
TEST(PicturePileImplTest,AnalyzeIsSolidEmpty)136 TEST(PicturePileImplTest, AnalyzeIsSolidEmpty) {
137 gfx::Size tile_size(100, 100);
138 gfx::Size layer_bounds(400, 400);
139
140 scoped_refptr<FakePicturePileImpl> pile =
141 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
142 PicturePileImpl::Analysis analysis;
143 EXPECT_FALSE(analysis.is_solid_color);
144
145 pile->AnalyzeInRect(gfx::Rect(0, 0, 400, 400), 1.f, &analysis);
146
147 EXPECT_TRUE(analysis.is_solid_color);
148 EXPECT_EQ(analysis.solid_color, SkColorSetARGB(0, 0, 0, 0));
149 }
150
TEST(PicturePileImplTest,PixelRefIteratorEmpty)151 TEST(PicturePileImplTest, PixelRefIteratorEmpty) {
152 gfx::Size tile_size(128, 128);
153 gfx::Size layer_bounds(256, 256);
154
155 // Create a filled pile with no recording.
156 scoped_refptr<FakePicturePileImpl> pile =
157 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
158
159 // Tile sized iterators.
160 {
161 PicturePileImpl::PixelRefIterator iterator(
162 gfx::Rect(0, 0, 128, 128), 1.0, pile.get());
163 EXPECT_FALSE(iterator);
164 }
165 {
166 PicturePileImpl::PixelRefIterator iterator(
167 gfx::Rect(0, 0, 256, 256), 2.0, pile.get());
168 EXPECT_FALSE(iterator);
169 }
170 {
171 PicturePileImpl::PixelRefIterator iterator(
172 gfx::Rect(0, 0, 64, 64), 0.5, pile.get());
173 EXPECT_FALSE(iterator);
174 }
175 // Shifted tile sized iterators.
176 {
177 PicturePileImpl::PixelRefIterator iterator(
178 gfx::Rect(140, 140, 128, 128), 1.0, pile.get());
179 EXPECT_FALSE(iterator);
180 }
181 {
182 PicturePileImpl::PixelRefIterator iterator(
183 gfx::Rect(280, 280, 256, 256), 2.0, pile.get());
184 EXPECT_FALSE(iterator);
185 }
186 {
187 PicturePileImpl::PixelRefIterator iterator(
188 gfx::Rect(70, 70, 64, 64), 0.5, pile.get());
189 EXPECT_FALSE(iterator);
190 }
191 // Layer sized iterators.
192 {
193 PicturePileImpl::PixelRefIterator iterator(
194 gfx::Rect(0, 0, 256, 256), 1.0, pile.get());
195 EXPECT_FALSE(iterator);
196 }
197 {
198 PicturePileImpl::PixelRefIterator iterator(
199 gfx::Rect(0, 0, 512, 512), 2.0, pile.get());
200 EXPECT_FALSE(iterator);
201 }
202 {
203 PicturePileImpl::PixelRefIterator iterator(
204 gfx::Rect(0, 0, 128, 128), 0.5, pile.get());
205 EXPECT_FALSE(iterator);
206 }
207 }
208
TEST(PicturePileImplTest,PixelRefIteratorNoLazyRefs)209 TEST(PicturePileImplTest, PixelRefIteratorNoLazyRefs) {
210 gfx::Size tile_size(128, 128);
211 gfx::Size layer_bounds(256, 256);
212
213 scoped_refptr<FakePicturePileImpl> pile =
214 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
215
216 SkPaint simple_paint;
217 simple_paint.setColor(SkColorSetARGB(255, 12, 23, 34));
218
219 SkBitmap non_lazy_bitmap;
220 CreateBitmap(gfx::Size(128, 128), "notlazy", &non_lazy_bitmap);
221
222 pile->add_draw_rect_with_paint(gfx::Rect(0, 0, 256, 256), simple_paint);
223 pile->add_draw_rect_with_paint(gfx::Rect(128, 128, 512, 512), simple_paint);
224 pile->add_draw_rect_with_paint(gfx::Rect(512, 0, 256, 256), simple_paint);
225 pile->add_draw_rect_with_paint(gfx::Rect(0, 512, 256, 256), simple_paint);
226 pile->add_draw_bitmap(non_lazy_bitmap, gfx::Point(128, 0));
227 pile->add_draw_bitmap(non_lazy_bitmap, gfx::Point(0, 128));
228 pile->add_draw_bitmap(non_lazy_bitmap, gfx::Point(150, 150));
229
230 pile->RerecordPile();
231
232 // Tile sized iterators.
233 {
234 PicturePileImpl::PixelRefIterator iterator(
235 gfx::Rect(0, 0, 128, 128), 1.0, pile.get());
236 EXPECT_FALSE(iterator);
237 }
238 {
239 PicturePileImpl::PixelRefIterator iterator(
240 gfx::Rect(0, 0, 256, 256), 2.0, pile.get());
241 EXPECT_FALSE(iterator);
242 }
243 {
244 PicturePileImpl::PixelRefIterator iterator(
245 gfx::Rect(0, 0, 64, 64), 0.5, pile.get());
246 EXPECT_FALSE(iterator);
247 }
248 // Shifted tile sized iterators.
249 {
250 PicturePileImpl::PixelRefIterator iterator(
251 gfx::Rect(140, 140, 128, 128), 1.0, pile.get());
252 EXPECT_FALSE(iterator);
253 }
254 {
255 PicturePileImpl::PixelRefIterator iterator(
256 gfx::Rect(280, 280, 256, 256), 2.0, pile.get());
257 EXPECT_FALSE(iterator);
258 }
259 {
260 PicturePileImpl::PixelRefIterator iterator(
261 gfx::Rect(70, 70, 64, 64), 0.5, pile.get());
262 EXPECT_FALSE(iterator);
263 }
264 // Layer sized iterators.
265 {
266 PicturePileImpl::PixelRefIterator iterator(
267 gfx::Rect(0, 0, 256, 256), 1.0, pile.get());
268 EXPECT_FALSE(iterator);
269 }
270 {
271 PicturePileImpl::PixelRefIterator iterator(
272 gfx::Rect(0, 0, 512, 512), 2.0, pile.get());
273 EXPECT_FALSE(iterator);
274 }
275 {
276 PicturePileImpl::PixelRefIterator iterator(
277 gfx::Rect(0, 0, 128, 128), 0.5, pile.get());
278 EXPECT_FALSE(iterator);
279 }
280 }
281
TEST(PicturePileImplTest,PixelRefIteratorLazyRefs)282 TEST(PicturePileImplTest, PixelRefIteratorLazyRefs) {
283 gfx::Size tile_size(128, 128);
284 gfx::Size layer_bounds(256, 256);
285
286 scoped_refptr<FakePicturePileImpl> pile =
287 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
288
289 SkBitmap lazy_bitmap[2][2];
290 CreateBitmap(gfx::Size(32, 32), "lazy", &lazy_bitmap[0][0]);
291 CreateBitmap(gfx::Size(32, 32), "lazy", &lazy_bitmap[1][0]);
292 CreateBitmap(gfx::Size(32, 32), "lazy", &lazy_bitmap[1][1]);
293
294 // Lazy pixel refs are found in the following cells:
295 // |---|---|
296 // | x | |
297 // |---|---|
298 // | x | x |
299 // |---|---|
300 pile->add_draw_bitmap(lazy_bitmap[0][0], gfx::Point(0, 0));
301 pile->add_draw_bitmap(lazy_bitmap[1][0], gfx::Point(0, 130));
302 pile->add_draw_bitmap(lazy_bitmap[1][1], gfx::Point(140, 140));
303
304 pile->RerecordPile();
305
306 // Tile sized iterators. These should find only one pixel ref.
307 {
308 PicturePileImpl::PixelRefIterator iterator(
309 gfx::Rect(0, 0, 128, 128), 1.0, pile.get());
310 EXPECT_TRUE(iterator);
311 EXPECT_TRUE(*iterator == lazy_bitmap[0][0].pixelRef());
312 EXPECT_FALSE(++iterator);
313 }
314 {
315 PicturePileImpl::PixelRefIterator iterator(
316 gfx::Rect(0, 0, 256, 256), 2.0, pile.get());
317 EXPECT_TRUE(iterator);
318 EXPECT_TRUE(*iterator == lazy_bitmap[0][0].pixelRef());
319 EXPECT_FALSE(++iterator);
320 }
321 {
322 PicturePileImpl::PixelRefIterator iterator(
323 gfx::Rect(0, 0, 64, 64), 0.5, pile.get());
324 EXPECT_TRUE(iterator);
325 EXPECT_TRUE(*iterator == lazy_bitmap[0][0].pixelRef());
326 EXPECT_FALSE(++iterator);
327 }
328 // Shifted tile sized iterators. These should find only one pixel ref.
329 {
330 PicturePileImpl::PixelRefIterator iterator(
331 gfx::Rect(140, 140, 128, 128), 1.0, pile.get());
332 EXPECT_TRUE(iterator);
333 EXPECT_TRUE(*iterator == lazy_bitmap[1][1].pixelRef());
334 EXPECT_FALSE(++iterator);
335 }
336 {
337 PicturePileImpl::PixelRefIterator iterator(
338 gfx::Rect(280, 280, 256, 256), 2.0, pile.get());
339 EXPECT_TRUE(iterator);
340 EXPECT_TRUE(*iterator == lazy_bitmap[1][1].pixelRef());
341 EXPECT_FALSE(++iterator);
342 }
343 {
344 PicturePileImpl::PixelRefIterator iterator(
345 gfx::Rect(70, 70, 64, 64), 0.5, pile.get());
346 EXPECT_TRUE(iterator);
347 EXPECT_TRUE(*iterator == lazy_bitmap[1][1].pixelRef());
348 EXPECT_FALSE(++iterator);
349 }
350 // Ensure there's no lazy pixel refs in the empty cell
351 {
352 PicturePileImpl::PixelRefIterator iterator(
353 gfx::Rect(140, 0, 128, 128), 1.0, pile.get());
354 EXPECT_FALSE(iterator);
355 }
356 // Layer sized iterators. These should find all 3 pixel refs.
357 {
358 PicturePileImpl::PixelRefIterator iterator(
359 gfx::Rect(0, 0, 256, 256), 1.0, pile.get());
360 EXPECT_TRUE(iterator);
361 EXPECT_TRUE(*iterator == lazy_bitmap[0][0].pixelRef());
362 EXPECT_TRUE(++iterator);
363 EXPECT_TRUE(*iterator == lazy_bitmap[1][0].pixelRef());
364 EXPECT_TRUE(++iterator);
365 EXPECT_TRUE(*iterator == lazy_bitmap[1][1].pixelRef());
366 EXPECT_FALSE(++iterator);
367 }
368 {
369 PicturePileImpl::PixelRefIterator iterator(
370 gfx::Rect(0, 0, 512, 512), 2.0, pile.get());
371 EXPECT_TRUE(iterator);
372 EXPECT_TRUE(*iterator == lazy_bitmap[0][0].pixelRef());
373 EXPECT_TRUE(++iterator);
374 EXPECT_TRUE(*iterator == lazy_bitmap[1][0].pixelRef());
375 EXPECT_TRUE(++iterator);
376 EXPECT_TRUE(*iterator == lazy_bitmap[1][1].pixelRef());
377 EXPECT_FALSE(++iterator);
378 }
379 {
380 PicturePileImpl::PixelRefIterator iterator(
381 gfx::Rect(0, 0, 128, 128), 0.5, pile.get());
382 EXPECT_TRUE(iterator);
383 EXPECT_TRUE(*iterator == lazy_bitmap[0][0].pixelRef());
384 EXPECT_TRUE(++iterator);
385 EXPECT_TRUE(*iterator == lazy_bitmap[1][0].pixelRef());
386 EXPECT_TRUE(++iterator);
387 EXPECT_TRUE(*iterator == lazy_bitmap[1][1].pixelRef());
388 EXPECT_FALSE(++iterator);
389 }
390 }
391
TEST(PicturePileImplTest,PixelRefIteratorLazyRefsOneTile)392 TEST(PicturePileImplTest, PixelRefIteratorLazyRefsOneTile) {
393 gfx::Size tile_size(256, 256);
394 gfx::Size layer_bounds(512, 512);
395
396 scoped_refptr<FakePicturePileImpl> pile =
397 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
398
399 SkBitmap lazy_bitmap[2][2];
400 CreateBitmap(gfx::Size(32, 32), "lazy", &lazy_bitmap[0][0]);
401 CreateBitmap(gfx::Size(32, 32), "lazy", &lazy_bitmap[0][1]);
402 CreateBitmap(gfx::Size(32, 32), "lazy", &lazy_bitmap[1][1]);
403
404 // Lazy pixel refs are found in the following cells:
405 // |---|---|
406 // | x | x |
407 // |---|---|
408 // | | x |
409 // |---|---|
410 pile->add_draw_bitmap(lazy_bitmap[0][0], gfx::Point(0, 0));
411 pile->add_draw_bitmap(lazy_bitmap[0][1], gfx::Point(260, 0));
412 pile->add_draw_bitmap(lazy_bitmap[1][1], gfx::Point(260, 260));
413
414 pile->RerecordPile();
415
416 // Tile sized iterators. These should find only one pixel ref.
417 {
418 PicturePileImpl::PixelRefIterator iterator(
419 gfx::Rect(0, 0, 256, 256), 1.0, pile.get());
420 EXPECT_TRUE(iterator);
421 EXPECT_TRUE(*iterator == lazy_bitmap[0][0].pixelRef());
422 EXPECT_FALSE(++iterator);
423 }
424 {
425 PicturePileImpl::PixelRefIterator iterator(
426 gfx::Rect(0, 0, 512, 512), 2.0, pile.get());
427 EXPECT_TRUE(iterator);
428 EXPECT_TRUE(*iterator == lazy_bitmap[0][0].pixelRef());
429 EXPECT_FALSE(++iterator);
430 }
431 {
432 PicturePileImpl::PixelRefIterator iterator(
433 gfx::Rect(0, 0, 128, 128), 0.5, pile.get());
434 EXPECT_TRUE(iterator);
435 EXPECT_TRUE(*iterator == lazy_bitmap[0][0].pixelRef());
436 EXPECT_FALSE(++iterator);
437 }
438 // Shifted tile sized iterators. These should find only one pixel ref.
439 {
440 PicturePileImpl::PixelRefIterator iterator(
441 gfx::Rect(260, 260, 256, 256), 1.0, pile.get());
442 EXPECT_TRUE(iterator);
443 EXPECT_TRUE(*iterator == lazy_bitmap[1][1].pixelRef());
444 EXPECT_FALSE(++iterator);
445 }
446 {
447 PicturePileImpl::PixelRefIterator iterator(
448 gfx::Rect(520, 520, 512, 512), 2.0, pile.get());
449 EXPECT_TRUE(iterator);
450 EXPECT_TRUE(*iterator == lazy_bitmap[1][1].pixelRef());
451 EXPECT_FALSE(++iterator);
452 }
453 {
454 PicturePileImpl::PixelRefIterator iterator(
455 gfx::Rect(130, 130, 128, 128), 0.5, pile.get());
456 EXPECT_TRUE(iterator);
457 EXPECT_TRUE(*iterator == lazy_bitmap[1][1].pixelRef());
458 EXPECT_FALSE(++iterator);
459 }
460 // Ensure there's no lazy pixel refs in the empty cell
461 {
462 PicturePileImpl::PixelRefIterator iterator(
463 gfx::Rect(0, 256, 256, 256), 1.0, pile.get());
464 EXPECT_FALSE(iterator);
465 }
466 // Layer sized iterators. These should find three pixel ref.
467 {
468 PicturePileImpl::PixelRefIterator iterator(
469 gfx::Rect(0, 0, 512, 512), 1.0, pile.get());
470 EXPECT_TRUE(iterator);
471 EXPECT_TRUE(*iterator == lazy_bitmap[0][0].pixelRef());
472 EXPECT_TRUE(++iterator);
473 EXPECT_TRUE(*iterator == lazy_bitmap[0][1].pixelRef());
474 EXPECT_TRUE(++iterator);
475 EXPECT_TRUE(*iterator == lazy_bitmap[1][1].pixelRef());
476 EXPECT_FALSE(++iterator);
477 }
478 {
479 PicturePileImpl::PixelRefIterator iterator(
480 gfx::Rect(0, 0, 1024, 1024), 2.0, pile.get());
481 EXPECT_TRUE(iterator);
482 EXPECT_TRUE(*iterator == lazy_bitmap[0][0].pixelRef());
483 EXPECT_TRUE(++iterator);
484 EXPECT_TRUE(*iterator == lazy_bitmap[0][1].pixelRef());
485 EXPECT_TRUE(++iterator);
486 EXPECT_TRUE(*iterator == lazy_bitmap[1][1].pixelRef());
487 EXPECT_FALSE(++iterator);
488 }
489 {
490 PicturePileImpl::PixelRefIterator iterator(
491 gfx::Rect(0, 0, 256, 256), 0.5, pile.get());
492 EXPECT_TRUE(iterator);
493 EXPECT_TRUE(*iterator == lazy_bitmap[0][0].pixelRef());
494 EXPECT_TRUE(++iterator);
495 EXPECT_TRUE(*iterator == lazy_bitmap[0][1].pixelRef());
496 EXPECT_TRUE(++iterator);
497 EXPECT_TRUE(*iterator == lazy_bitmap[1][1].pixelRef());
498 EXPECT_FALSE(++iterator);
499 }
500
501 // Copy test.
502 PicturePileImpl::PixelRefIterator iterator(
503 gfx::Rect(0, 0, 512, 512), 1.0, pile.get());
504 EXPECT_TRUE(iterator);
505 EXPECT_TRUE(*iterator == lazy_bitmap[0][0].pixelRef());
506 EXPECT_TRUE(++iterator);
507 EXPECT_TRUE(*iterator == lazy_bitmap[0][1].pixelRef());
508
509 // copy now points to the same spot as iterator,
510 // but both can be incremented independently.
511 PicturePileImpl::PixelRefIterator copy = iterator;
512 EXPECT_TRUE(++iterator);
513 EXPECT_TRUE(*iterator == lazy_bitmap[1][1].pixelRef());
514 EXPECT_FALSE(++iterator);
515
516 EXPECT_TRUE(copy);
517 EXPECT_TRUE(*copy == lazy_bitmap[0][1].pixelRef());
518 EXPECT_TRUE(++copy);
519 EXPECT_TRUE(*copy == lazy_bitmap[1][1].pixelRef());
520 EXPECT_FALSE(++copy);
521 }
522
TEST(PicturePileImplTest,PixelRefIteratorLazyRefsBaseNonLazy)523 TEST(PicturePileImplTest, PixelRefIteratorLazyRefsBaseNonLazy) {
524 gfx::Size tile_size(256, 256);
525 gfx::Size layer_bounds(512, 512);
526
527 scoped_refptr<FakePicturePileImpl> pile =
528 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
529
530 SkBitmap non_lazy_bitmap;
531 CreateBitmap(gfx::Size(512, 512), "notlazy", &non_lazy_bitmap);
532
533 SkBitmap lazy_bitmap[2][2];
534 CreateBitmap(gfx::Size(128, 128), "lazy", &lazy_bitmap[0][0]);
535 CreateBitmap(gfx::Size(128, 128), "lazy", &lazy_bitmap[0][1]);
536 CreateBitmap(gfx::Size(128, 128), "lazy", &lazy_bitmap[1][1]);
537
538 // One large non-lazy bitmap covers the whole grid.
539 // Lazy pixel refs are found in the following cells:
540 // |---|---|
541 // | x | x |
542 // |---|---|
543 // | | x |
544 // |---|---|
545 pile->add_draw_bitmap(non_lazy_bitmap, gfx::Point(0, 0));
546 pile->add_draw_bitmap(lazy_bitmap[0][0], gfx::Point(0, 0));
547 pile->add_draw_bitmap(lazy_bitmap[0][1], gfx::Point(260, 0));
548 pile->add_draw_bitmap(lazy_bitmap[1][1], gfx::Point(260, 260));
549
550 pile->RerecordPile();
551
552 // Tile sized iterators. These should find only one pixel ref.
553 {
554 PicturePileImpl::PixelRefIterator iterator(
555 gfx::Rect(0, 0, 256, 256), 1.0, pile.get());
556 EXPECT_TRUE(iterator);
557 EXPECT_TRUE(*iterator == lazy_bitmap[0][0].pixelRef());
558 EXPECT_FALSE(++iterator);
559 }
560 {
561 PicturePileImpl::PixelRefIterator iterator(
562 gfx::Rect(0, 0, 512, 512), 2.0, pile.get());
563 EXPECT_TRUE(iterator);
564 EXPECT_TRUE(*iterator == lazy_bitmap[0][0].pixelRef());
565 EXPECT_FALSE(++iterator);
566 }
567 {
568 PicturePileImpl::PixelRefIterator iterator(
569 gfx::Rect(0, 0, 128, 128), 0.5, pile.get());
570 EXPECT_TRUE(iterator);
571 EXPECT_TRUE(*iterator == lazy_bitmap[0][0].pixelRef());
572 EXPECT_FALSE(++iterator);
573 }
574 // Shifted tile sized iterators. These should find only one pixel ref.
575 {
576 PicturePileImpl::PixelRefIterator iterator(
577 gfx::Rect(260, 260, 256, 256), 1.0, pile.get());
578 EXPECT_TRUE(iterator);
579 EXPECT_TRUE(*iterator == lazy_bitmap[1][1].pixelRef());
580 EXPECT_FALSE(++iterator);
581 }
582 {
583 PicturePileImpl::PixelRefIterator iterator(
584 gfx::Rect(520, 520, 512, 512), 2.0, pile.get());
585 EXPECT_TRUE(iterator);
586 EXPECT_TRUE(*iterator == lazy_bitmap[1][1].pixelRef());
587 EXPECT_FALSE(++iterator);
588 }
589 {
590 PicturePileImpl::PixelRefIterator iterator(
591 gfx::Rect(130, 130, 128, 128), 0.5, pile.get());
592 EXPECT_TRUE(iterator);
593 EXPECT_TRUE(*iterator == lazy_bitmap[1][1].pixelRef());
594 EXPECT_FALSE(++iterator);
595 }
596 // Ensure there's no lazy pixel refs in the empty cell
597 {
598 PicturePileImpl::PixelRefIterator iterator(
599 gfx::Rect(0, 256, 256, 256), 1.0, pile.get());
600 EXPECT_FALSE(iterator);
601 }
602 // Layer sized iterators. These should find three pixel ref.
603 {
604 PicturePileImpl::PixelRefIterator iterator(
605 gfx::Rect(0, 0, 512, 512), 1.0, pile.get());
606 EXPECT_TRUE(iterator);
607 EXPECT_TRUE(*iterator == lazy_bitmap[0][0].pixelRef());
608 EXPECT_TRUE(++iterator);
609 EXPECT_TRUE(*iterator == lazy_bitmap[0][1].pixelRef());
610 EXPECT_TRUE(++iterator);
611 EXPECT_TRUE(*iterator == lazy_bitmap[1][1].pixelRef());
612 EXPECT_FALSE(++iterator);
613 }
614 {
615 PicturePileImpl::PixelRefIterator iterator(
616 gfx::Rect(0, 0, 1024, 1024), 2.0, pile.get());
617 EXPECT_TRUE(iterator);
618 EXPECT_TRUE(*iterator == lazy_bitmap[0][0].pixelRef());
619 EXPECT_TRUE(++iterator);
620 EXPECT_TRUE(*iterator == lazy_bitmap[0][1].pixelRef());
621 EXPECT_TRUE(++iterator);
622 EXPECT_TRUE(*iterator == lazy_bitmap[1][1].pixelRef());
623 EXPECT_FALSE(++iterator);
624 }
625 {
626 PicturePileImpl::PixelRefIterator iterator(
627 gfx::Rect(0, 0, 256, 256), 0.5, pile.get());
628 EXPECT_TRUE(iterator);
629 EXPECT_TRUE(*iterator == lazy_bitmap[0][0].pixelRef());
630 EXPECT_TRUE(++iterator);
631 EXPECT_TRUE(*iterator == lazy_bitmap[0][1].pixelRef());
632 EXPECT_TRUE(++iterator);
633 EXPECT_TRUE(*iterator == lazy_bitmap[1][1].pixelRef());
634 EXPECT_FALSE(++iterator);
635 }
636 }
637
TEST(PicturePileImpl,RasterContentsOpaque)638 TEST(PicturePileImpl, RasterContentsOpaque) {
639 gfx::Size tile_size(1000, 1000);
640 gfx::Size layer_bounds(3, 5);
641 float contents_scale = 1.5f;
642 float raster_divisions = 2.f;
643
644 scoped_refptr<FakePicturePileImpl> pile =
645 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
646 // Because the caller sets content opaque, it also promises that it
647 // has at least filled in layer_bounds opaquely.
648 SkPaint white_paint;
649 white_paint.setColor(SK_ColorWHITE);
650 pile->add_draw_rect_with_paint(gfx::Rect(layer_bounds), white_paint);
651
652 pile->SetMinContentsScale(contents_scale);
653 pile->set_background_color(SK_ColorBLACK);
654 pile->set_contents_opaque(true);
655 pile->set_clear_canvas_with_debug_color(false);
656 pile->RerecordPile();
657
658 gfx::Size content_bounds(
659 gfx::ToCeiledSize(gfx::ScaleSize(layer_bounds, contents_scale)));
660
661 // Simulate drawing into different tiles at different offsets.
662 int step_x = std::ceil(content_bounds.width() / raster_divisions);
663 int step_y = std::ceil(content_bounds.height() / raster_divisions);
664 for (int offset_x = 0; offset_x < content_bounds.width();
665 offset_x += step_x) {
666 for (int offset_y = 0; offset_y < content_bounds.height();
667 offset_y += step_y) {
668 gfx::Rect content_rect(offset_x, offset_y, step_x, step_y);
669 content_rect.Intersect(gfx::Rect(content_bounds));
670
671 // Simulate a canvas rect larger than the content rect. Every pixel
672 // up to one pixel outside the content rect is guaranteed to be opaque.
673 // Outside of that is undefined.
674 gfx::Rect canvas_rect(content_rect);
675 canvas_rect.Inset(0, 0, -1, -1);
676
677 SkBitmap bitmap;
678 bitmap.setConfig(SkBitmap::kARGB_8888_Config,
679 canvas_rect.width(),
680 canvas_rect.height());
681 bitmap.allocPixels();
682 SkCanvas canvas(bitmap);
683 canvas.clear(SK_ColorTRANSPARENT);
684
685 FakeRenderingStatsInstrumentation rendering_stats_instrumentation;
686
687 pile->RasterToBitmap(&canvas,
688 canvas_rect,
689 contents_scale,
690 &rendering_stats_instrumentation);
691
692 SkColor* pixels = reinterpret_cast<SkColor*>(bitmap.getPixels());
693 int num_pixels = bitmap.width() * bitmap.height();
694 bool all_white = true;
695 for (int i = 0; i < num_pixels; ++i) {
696 EXPECT_EQ(SkColorGetA(pixels[i]), 255u);
697 all_white &= (SkColorGetR(pixels[i]) == 255);
698 all_white &= (SkColorGetG(pixels[i]) == 255);
699 all_white &= (SkColorGetB(pixels[i]) == 255);
700 }
701
702 // If the canvas doesn't extend past the edge of the content,
703 // it should be entirely white. Otherwise, the edge of the content
704 // will be non-white.
705 EXPECT_EQ(all_white, gfx::Rect(content_bounds).Contains(canvas_rect));
706 }
707 }
708 }
709
TEST(PicturePileImpl,RasterContentsTransparent)710 TEST(PicturePileImpl, RasterContentsTransparent) {
711 gfx::Size tile_size(1000, 1000);
712 gfx::Size layer_bounds(5, 3);
713 float contents_scale = 0.5f;
714
715 scoped_refptr<FakePicturePileImpl> pile =
716 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
717 pile->set_background_color(SK_ColorTRANSPARENT);
718 pile->set_contents_opaque(false);
719 pile->SetMinContentsScale(contents_scale);
720 pile->set_clear_canvas_with_debug_color(false);
721 pile->RerecordPile();
722
723 gfx::Size content_bounds(
724 gfx::ToCeiledSize(gfx::ScaleSize(layer_bounds, contents_scale)));
725
726 gfx::Rect canvas_rect(content_bounds);
727 canvas_rect.Inset(0, 0, -1, -1);
728
729 SkBitmap bitmap;
730 bitmap.setConfig(SkBitmap::kARGB_8888_Config,
731 canvas_rect.width(),
732 canvas_rect.height());
733 bitmap.allocPixels();
734 SkCanvas canvas(bitmap);
735
736 FakeRenderingStatsInstrumentation rendering_stats_instrumentation;
737 pile->RasterToBitmap(
738 &canvas, canvas_rect, contents_scale, &rendering_stats_instrumentation);
739
740 SkColor* pixels = reinterpret_cast<SkColor*>(bitmap.getPixels());
741 int num_pixels = bitmap.width() * bitmap.height();
742 for (int i = 0; i < num_pixels; ++i) {
743 EXPECT_EQ(SkColorGetA(pixels[i]), 0u);
744 }
745 }
746
747 } // namespace
748 } // namespace cc
749