Remove implicit conversions from scoped_refptr to T* in cc/
[chromium-blink-merge.git] / cc / resources / picture_unittest.cc
blobe6d692bf4f491da163fa0b10d474b2eb88602602
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 "cc/resources/picture.h"
7 #include "base/memory/ref_counted.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/values.h"
10 #include "cc/test/fake_content_layer_client.h"
11 #include "cc/test/skia_common.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "third_party/skia/include/core/SkBBHFactory.h"
14 #include "third_party/skia/include/core/SkCanvas.h"
15 #include "third_party/skia/include/core/SkGraphics.h"
16 #include "third_party/skia/include/core/SkPixelRef.h"
17 #include "ui/gfx/rect.h"
18 #include "ui/gfx/skia_util.h"
20 namespace cc {
21 namespace {
23 TEST(PictureTest, AsBase64String) {
24 SkGraphics::Init();
26 gfx::Rect layer_rect(100, 100);
28 SkTileGridFactory::TileGridInfo tile_grid_info;
29 tile_grid_info.fTileInterval = SkISize::Make(100, 100);
30 tile_grid_info.fMargin.setEmpty();
31 tile_grid_info.fOffset.setZero();
33 FakeContentLayerClient content_layer_client;
35 scoped_ptr<base::Value> tmp;
37 SkPaint red_paint;
38 red_paint.setColor(SkColorSetARGB(255, 255, 0, 0));
39 SkPaint green_paint;
40 green_paint.setColor(SkColorSetARGB(255, 0, 255, 0));
42 // Invalid picture (not a dict).
43 tmp.reset(new base::StringValue("abc!@#$%"));
44 scoped_refptr<Picture> invalid_picture =
45 Picture::CreateFromValue(tmp.get());
46 EXPECT_FALSE(invalid_picture.get());
48 Picture::RecordingMode kRecordingModes[] = {Picture::RECORD_NORMALLY,
49 Picture::RECORD_WITH_SKRECORD};
51 // Single full-size rect picture.
52 content_layer_client.add_draw_rect(layer_rect, red_paint);
54 for (size_t i = 0; i < arraysize(kRecordingModes); ++i) {
55 scoped_refptr<Picture> one_rect_picture =
56 Picture::Create(layer_rect,
57 &content_layer_client,
58 tile_grid_info,
59 false,
60 kRecordingModes[i]);
61 scoped_ptr<base::Value> serialized_one_rect(one_rect_picture->AsValue());
63 // Reconstruct the picture.
64 scoped_refptr<Picture> one_rect_picture_check =
65 Picture::CreateFromValue(serialized_one_rect.get());
66 EXPECT_TRUE(!!one_rect_picture_check.get());
68 // Check for equivalence.
69 unsigned char one_rect_buffer[4 * 100 * 100] = {0};
70 DrawPicture(one_rect_buffer, layer_rect, one_rect_picture);
71 unsigned char one_rect_buffer_check[4 * 100 * 100] = {0};
72 DrawPicture(one_rect_buffer_check, layer_rect, one_rect_picture_check);
74 EXPECT_EQ(one_rect_picture->LayerRect(),
75 one_rect_picture_check->LayerRect());
76 EXPECT_EQ(one_rect_picture->OpaqueRect(),
77 one_rect_picture_check->OpaqueRect());
78 EXPECT_EQ(0, memcmp(one_rect_buffer, one_rect_buffer_check, 4 * 100 * 100));
81 // Two rect picture.
82 content_layer_client.add_draw_rect(gfx::Rect(25, 25, 50, 50), green_paint);
84 for (size_t i = 0; i < arraysize(kRecordingModes); ++i) {
85 scoped_refptr<Picture> two_rect_picture =
86 Picture::Create(layer_rect,
87 &content_layer_client,
88 tile_grid_info,
89 false,
90 Picture::RECORD_NORMALLY);
92 scoped_ptr<base::Value> serialized_two_rect(two_rect_picture->AsValue());
94 // Reconstruct the picture.
95 scoped_refptr<Picture> two_rect_picture_check =
96 Picture::CreateFromValue(serialized_two_rect.get());
97 EXPECT_TRUE(!!two_rect_picture_check.get());
99 // Check for equivalence.
100 unsigned char two_rect_buffer[4 * 100 * 100] = {0};
101 DrawPicture(two_rect_buffer, layer_rect, two_rect_picture);
102 unsigned char two_rect_buffer_check[4 * 100 * 100] = {0};
103 DrawPicture(two_rect_buffer_check, layer_rect, two_rect_picture_check);
105 EXPECT_EQ(two_rect_picture->LayerRect(),
106 two_rect_picture_check->LayerRect());
107 EXPECT_EQ(two_rect_picture->OpaqueRect(),
108 two_rect_picture_check->OpaqueRect());
109 EXPECT_EQ(0, memcmp(two_rect_buffer, two_rect_buffer_check, 4 * 100 * 100));
113 TEST(PictureTest, PixelRefIterator) {
114 gfx::Rect layer_rect(2048, 2048);
116 SkTileGridFactory::TileGridInfo tile_grid_info;
117 tile_grid_info.fTileInterval = SkISize::Make(512, 512);
118 tile_grid_info.fMargin.setEmpty();
119 tile_grid_info.fOffset.setZero();
121 FakeContentLayerClient content_layer_client;
123 // Discardable pixel refs are found in the following grids:
124 // |---|---|---|---|
125 // | | x | | x |
126 // |---|---|---|---|
127 // | x | | x | |
128 // |---|---|---|---|
129 // | | x | | x |
130 // |---|---|---|---|
131 // | x | | x | |
132 // |---|---|---|---|
133 SkBitmap discardable_bitmap[4][4];
134 for (int y = 0; y < 4; ++y) {
135 for (int x = 0; x < 4; ++x) {
136 if ((x + y) & 1) {
137 CreateBitmap(
138 gfx::Size(500, 500), "discardable", &discardable_bitmap[y][x]);
139 SkPaint paint;
140 content_layer_client.add_draw_bitmap(
141 discardable_bitmap[y][x],
142 gfx::Point(x * 512 + 6, y * 512 + 6), paint);
147 scoped_refptr<Picture> picture = Picture::Create(layer_rect,
148 &content_layer_client,
149 tile_grid_info,
150 true,
151 Picture::RECORD_NORMALLY);
153 // Default iterator does not have any pixel refs
155 Picture::PixelRefIterator iterator;
156 EXPECT_FALSE(iterator);
158 for (int y = 0; y < 4; ++y) {
159 for (int x = 0; x < 4; ++x) {
160 Picture::PixelRefIterator iterator(gfx::Rect(x * 512, y * 512, 500, 500),
161 picture.get());
162 if ((x + y) & 1) {
163 EXPECT_TRUE(iterator) << x << " " << y;
164 EXPECT_TRUE(*iterator == discardable_bitmap[y][x].pixelRef()) << x <<
165 " " << y;
166 EXPECT_FALSE(++iterator) << x << " " << y;
167 } else {
168 EXPECT_FALSE(iterator) << x << " " << y;
172 // Capture 4 pixel refs.
174 Picture::PixelRefIterator iterator(gfx::Rect(512, 512, 2048, 2048),
175 picture.get());
176 EXPECT_TRUE(iterator);
177 EXPECT_TRUE(*iterator == discardable_bitmap[1][2].pixelRef());
178 EXPECT_TRUE(++iterator);
179 EXPECT_TRUE(*iterator == discardable_bitmap[2][1].pixelRef());
180 EXPECT_TRUE(++iterator);
181 EXPECT_TRUE(*iterator == discardable_bitmap[2][3].pixelRef());
182 EXPECT_TRUE(++iterator);
183 EXPECT_TRUE(*iterator == discardable_bitmap[3][2].pixelRef());
184 EXPECT_FALSE(++iterator);
187 // Copy test.
188 Picture::PixelRefIterator iterator(gfx::Rect(512, 512, 2048, 2048),
189 picture.get());
190 EXPECT_TRUE(iterator);
191 EXPECT_TRUE(*iterator == discardable_bitmap[1][2].pixelRef());
192 EXPECT_TRUE(++iterator);
193 EXPECT_TRUE(*iterator == discardable_bitmap[2][1].pixelRef());
195 // copy now points to the same spot as iterator,
196 // but both can be incremented independently.
197 Picture::PixelRefIterator copy = iterator;
198 EXPECT_TRUE(++iterator);
199 EXPECT_TRUE(*iterator == discardable_bitmap[2][3].pixelRef());
200 EXPECT_TRUE(++iterator);
201 EXPECT_TRUE(*iterator == discardable_bitmap[3][2].pixelRef());
202 EXPECT_FALSE(++iterator);
204 EXPECT_TRUE(copy);
205 EXPECT_TRUE(*copy == discardable_bitmap[2][1].pixelRef());
206 EXPECT_TRUE(++copy);
207 EXPECT_TRUE(*copy == discardable_bitmap[2][3].pixelRef());
208 EXPECT_TRUE(++copy);
209 EXPECT_TRUE(*copy == discardable_bitmap[3][2].pixelRef());
210 EXPECT_FALSE(++copy);
213 TEST(PictureTest, PixelRefIteratorNonZeroLayer) {
214 gfx::Rect layer_rect(1024, 0, 2048, 2048);
216 SkTileGridFactory::TileGridInfo tile_grid_info;
217 tile_grid_info.fTileInterval = SkISize::Make(512, 512);
218 tile_grid_info.fMargin.setEmpty();
219 tile_grid_info.fOffset.setZero();
221 FakeContentLayerClient content_layer_client;
223 // Discardable pixel refs are found in the following grids:
224 // |---|---|---|---|
225 // | | x | | x |
226 // |---|---|---|---|
227 // | x | | x | |
228 // |---|---|---|---|
229 // | | x | | x |
230 // |---|---|---|---|
231 // | x | | x | |
232 // |---|---|---|---|
233 SkBitmap discardable_bitmap[4][4];
234 for (int y = 0; y < 4; ++y) {
235 for (int x = 0; x < 4; ++x) {
236 if ((x + y) & 1) {
237 CreateBitmap(
238 gfx::Size(500, 500), "discardable", &discardable_bitmap[y][x]);
239 SkPaint paint;
240 content_layer_client.add_draw_bitmap(
241 discardable_bitmap[y][x],
242 gfx::Point(1024 + x * 512 + 6, y * 512 + 6), paint);
247 scoped_refptr<Picture> picture = Picture::Create(layer_rect,
248 &content_layer_client,
249 tile_grid_info,
250 true,
251 Picture::RECORD_NORMALLY);
253 // Default iterator does not have any pixel refs
255 Picture::PixelRefIterator iterator;
256 EXPECT_FALSE(iterator);
258 for (int y = 0; y < 4; ++y) {
259 for (int x = 0; x < 4; ++x) {
260 Picture::PixelRefIterator iterator(
261 gfx::Rect(1024 + x * 512, y * 512, 500, 500), picture.get());
262 if ((x + y) & 1) {
263 EXPECT_TRUE(iterator) << x << " " << y;
264 EXPECT_TRUE(*iterator == discardable_bitmap[y][x].pixelRef());
265 EXPECT_FALSE(++iterator) << x << " " << y;
266 } else {
267 EXPECT_FALSE(iterator) << x << " " << y;
271 // Capture 4 pixel refs.
273 Picture::PixelRefIterator iterator(gfx::Rect(1024 + 512, 512, 2048, 2048),
274 picture.get());
275 EXPECT_TRUE(iterator);
276 EXPECT_TRUE(*iterator == discardable_bitmap[1][2].pixelRef());
277 EXPECT_TRUE(++iterator);
278 EXPECT_TRUE(*iterator == discardable_bitmap[2][1].pixelRef());
279 EXPECT_TRUE(++iterator);
280 EXPECT_TRUE(*iterator == discardable_bitmap[2][3].pixelRef());
281 EXPECT_TRUE(++iterator);
282 EXPECT_TRUE(*iterator == discardable_bitmap[3][2].pixelRef());
283 EXPECT_FALSE(++iterator);
286 // Copy test.
288 Picture::PixelRefIterator iterator(gfx::Rect(1024 + 512, 512, 2048, 2048),
289 picture.get());
290 EXPECT_TRUE(iterator);
291 EXPECT_TRUE(*iterator == discardable_bitmap[1][2].pixelRef());
292 EXPECT_TRUE(++iterator);
293 EXPECT_TRUE(*iterator == discardable_bitmap[2][1].pixelRef());
295 // copy now points to the same spot as iterator,
296 // but both can be incremented independently.
297 Picture::PixelRefIterator copy = iterator;
298 EXPECT_TRUE(++iterator);
299 EXPECT_TRUE(*iterator == discardable_bitmap[2][3].pixelRef());
300 EXPECT_TRUE(++iterator);
301 EXPECT_TRUE(*iterator == discardable_bitmap[3][2].pixelRef());
302 EXPECT_FALSE(++iterator);
304 EXPECT_TRUE(copy);
305 EXPECT_TRUE(*copy == discardable_bitmap[2][1].pixelRef());
306 EXPECT_TRUE(++copy);
307 EXPECT_TRUE(*copy == discardable_bitmap[2][3].pixelRef());
308 EXPECT_TRUE(++copy);
309 EXPECT_TRUE(*copy == discardable_bitmap[3][2].pixelRef());
310 EXPECT_FALSE(++copy);
313 // Non intersecting rects
315 Picture::PixelRefIterator iterator(gfx::Rect(0, 0, 1000, 1000),
316 picture.get());
317 EXPECT_FALSE(iterator);
320 Picture::PixelRefIterator iterator(gfx::Rect(3500, 0, 1000, 1000),
321 picture.get());
322 EXPECT_FALSE(iterator);
325 Picture::PixelRefIterator iterator(gfx::Rect(0, 1100, 1000, 1000),
326 picture.get());
327 EXPECT_FALSE(iterator);
330 Picture::PixelRefIterator iterator(gfx::Rect(3500, 1100, 1000, 1000),
331 picture.get());
332 EXPECT_FALSE(iterator);
336 TEST(PictureTest, PixelRefIteratorOnePixelQuery) {
337 gfx::Rect layer_rect(2048, 2048);
339 SkTileGridFactory::TileGridInfo tile_grid_info;
340 tile_grid_info.fTileInterval = SkISize::Make(512, 512);
341 tile_grid_info.fMargin.setEmpty();
342 tile_grid_info.fOffset.setZero();
344 FakeContentLayerClient content_layer_client;
346 // Discardable pixel refs are found in the following grids:
347 // |---|---|---|---|
348 // | | x | | x |
349 // |---|---|---|---|
350 // | x | | x | |
351 // |---|---|---|---|
352 // | | x | | x |
353 // |---|---|---|---|
354 // | x | | x | |
355 // |---|---|---|---|
356 SkBitmap discardable_bitmap[4][4];
357 for (int y = 0; y < 4; ++y) {
358 for (int x = 0; x < 4; ++x) {
359 if ((x + y) & 1) {
360 CreateBitmap(
361 gfx::Size(500, 500), "discardable", &discardable_bitmap[y][x]);
362 SkPaint paint;
363 content_layer_client.add_draw_bitmap(
364 discardable_bitmap[y][x],
365 gfx::Point(x * 512 + 6, y * 512 + 6), paint);
370 scoped_refptr<Picture> picture = Picture::Create(layer_rect,
371 &content_layer_client,
372 tile_grid_info,
373 true,
374 Picture::RECORD_NORMALLY);
376 for (int y = 0; y < 4; ++y) {
377 for (int x = 0; x < 4; ++x) {
378 Picture::PixelRefIterator iterator(
379 gfx::Rect(x * 512, y * 512 + 256, 1, 1), picture.get());
380 if ((x + y) & 1) {
381 EXPECT_TRUE(iterator) << x << " " << y;
382 EXPECT_TRUE(*iterator == discardable_bitmap[y][x].pixelRef());
383 EXPECT_FALSE(++iterator) << x << " " << y;
384 } else {
385 EXPECT_FALSE(iterator) << x << " " << y;
391 TEST(PictureTest, CreateFromSkpValue) {
392 SkGraphics::Init();
394 gfx::Rect layer_rect(100, 200);
396 SkTileGridFactory::TileGridInfo tile_grid_info;
397 tile_grid_info.fTileInterval = SkISize::Make(100, 200);
398 tile_grid_info.fMargin.setEmpty();
399 tile_grid_info.fOffset.setZero();
401 FakeContentLayerClient content_layer_client;
403 scoped_ptr<base::Value> tmp;
405 SkPaint red_paint;
406 red_paint.setColor(SkColorSetARGB(255, 255, 0, 0));
407 SkPaint green_paint;
408 green_paint.setColor(SkColorSetARGB(255, 0, 255, 0));
410 // Invalid picture (not a dict).
411 tmp.reset(new base::StringValue("abc!@#$%"));
412 scoped_refptr<Picture> invalid_picture =
413 Picture::CreateFromSkpValue(tmp.get());
414 EXPECT_TRUE(!invalid_picture.get());
416 // Single full-size rect picture.
417 content_layer_client.add_draw_rect(layer_rect, red_paint);
418 scoped_refptr<Picture> one_rect_picture =
419 Picture::Create(layer_rect,
420 &content_layer_client,
421 tile_grid_info,
422 false,
423 Picture::RECORD_NORMALLY);
424 scoped_ptr<base::Value> serialized_one_rect(
425 one_rect_picture->AsValue());
427 const base::DictionaryValue* value = NULL;
428 EXPECT_TRUE(serialized_one_rect->GetAsDictionary(&value));
430 // Decode the picture from base64.
431 const base::Value* skp_value;
432 EXPECT_TRUE(value->Get("skp64", &skp_value));
434 // Reconstruct the picture.
435 scoped_refptr<Picture> one_rect_picture_check =
436 Picture::CreateFromSkpValue(skp_value);
437 EXPECT_TRUE(!!one_rect_picture_check.get());
439 EXPECT_EQ(100, one_rect_picture_check->LayerRect().width());
440 EXPECT_EQ(200, one_rect_picture_check->LayerRect().height());
441 EXPECT_EQ(100, one_rect_picture_check->OpaqueRect().width());
442 EXPECT_EQ(200, one_rect_picture_check->OpaqueRect().height());
445 TEST(PictureTest, RecordingModes) {
446 SkGraphics::Init();
448 gfx::Rect layer_rect(100, 200);
450 SkTileGridFactory::TileGridInfo tile_grid_info;
451 tile_grid_info.fTileInterval = SkISize::Make(100, 200);
452 tile_grid_info.fMargin.setEmpty();
453 tile_grid_info.fOffset.setZero();
455 FakeContentLayerClient content_layer_client;
456 EXPECT_EQ(NULL, content_layer_client.last_canvas());
458 scoped_refptr<Picture> picture = Picture::Create(layer_rect,
459 &content_layer_client,
460 tile_grid_info,
461 false,
462 Picture::RECORD_NORMALLY);
463 EXPECT_TRUE(content_layer_client.last_canvas() != NULL);
464 EXPECT_EQ(ContentLayerClient::GRAPHICS_CONTEXT_ENABLED,
465 content_layer_client.last_context_status());
466 EXPECT_TRUE(picture.get());
468 picture = Picture::Create(layer_rect,
469 &content_layer_client,
470 tile_grid_info,
471 false,
472 Picture::RECORD_WITH_SK_NULL_CANVAS);
473 EXPECT_TRUE(content_layer_client.last_canvas() != NULL);
474 EXPECT_EQ(ContentLayerClient::GRAPHICS_CONTEXT_ENABLED,
475 content_layer_client.last_context_status());
476 EXPECT_TRUE(picture.get());
478 picture = Picture::Create(layer_rect,
479 &content_layer_client,
480 tile_grid_info,
481 false,
482 Picture::RECORD_WITH_PAINTING_DISABLED);
483 EXPECT_TRUE(content_layer_client.last_canvas() != NULL);
484 EXPECT_EQ(ContentLayerClient::GRAPHICS_CONTEXT_DISABLED,
485 content_layer_client.last_context_status());
486 EXPECT_TRUE(picture.get());
488 picture = Picture::Create(layer_rect,
489 &content_layer_client,
490 tile_grid_info,
491 false,
492 Picture::RECORD_WITH_SKRECORD);
493 EXPECT_TRUE(content_layer_client.last_canvas() != NULL);
494 EXPECT_TRUE(picture.get());
496 EXPECT_EQ(4, Picture::RECORDING_MODE_COUNT);
499 } // namespace
500 } // namespace cc