OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "cc/resources/picture.h" | 5 #include "cc/resources/picture.h" |
6 | 6 |
7 #include "base/memory/ref_counted.h" | 7 #include "base/memory/ref_counted.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/values.h" | 9 #include "base/values.h" |
10 #include "cc/test/fake_content_layer_client.h" | 10 #include "cc/test/fake_content_layer_client.h" |
(...skipping 15 matching lines...) Expand all Loading... |
26 SkGraphics::Init(); | 26 SkGraphics::Init(); |
27 | 27 |
28 gfx::Rect layer_rect(100, 100); | 28 gfx::Rect layer_rect(100, 100); |
29 | 29 |
30 SkTileGridPicture::TileGridInfo tile_grid_info; | 30 SkTileGridPicture::TileGridInfo tile_grid_info; |
31 tile_grid_info.fTileInterval = SkISize::Make(100, 100); | 31 tile_grid_info.fTileInterval = SkISize::Make(100, 100); |
32 tile_grid_info.fMargin.setEmpty(); | 32 tile_grid_info.fMargin.setEmpty(); |
33 tile_grid_info.fOffset.setZero(); | 33 tile_grid_info.fOffset.setZero(); |
34 | 34 |
35 FakeContentLayerClient content_layer_client; | 35 FakeContentLayerClient content_layer_client; |
36 FakeRenderingStatsInstrumentation stats_instrumentation; | |
37 | 36 |
38 scoped_ptr<base::Value> tmp; | 37 scoped_ptr<base::Value> tmp; |
39 | 38 |
40 SkPaint red_paint; | 39 SkPaint red_paint; |
41 red_paint.setColor(SkColorSetARGB(255, 255, 0, 0)); | 40 red_paint.setColor(SkColorSetARGB(255, 255, 0, 0)); |
42 SkPaint green_paint; | 41 SkPaint green_paint; |
43 green_paint.setColor(SkColorSetARGB(255, 0, 255, 0)); | 42 green_paint.setColor(SkColorSetARGB(255, 0, 255, 0)); |
44 | 43 |
45 // Invalid picture (not a dict). | 44 // Invalid picture (not a dict). |
46 tmp.reset(new base::StringValue("abc!@#$%")); | 45 tmp.reset(new base::StringValue("abc!@#$%")); |
47 scoped_refptr<Picture> invalid_picture = | 46 scoped_refptr<Picture> invalid_picture = |
48 Picture::CreateFromValue(tmp.get()); | 47 Picture::CreateFromValue(tmp.get()); |
49 EXPECT_TRUE(!invalid_picture.get()); | 48 EXPECT_TRUE(!invalid_picture.get()); |
50 | 49 |
51 // Single full-size rect picture. | 50 // Single full-size rect picture. |
52 content_layer_client.add_draw_rect(layer_rect, red_paint); | 51 content_layer_client.add_draw_rect(layer_rect, red_paint); |
53 scoped_refptr<Picture> one_rect_picture = Picture::Create(layer_rect); | 52 scoped_refptr<Picture> one_rect_picture = Picture::Create(layer_rect); |
54 one_rect_picture->Record(&content_layer_client, | 53 one_rect_picture->Record(&content_layer_client, |
55 tile_grid_info, | 54 tile_grid_info); |
56 &stats_instrumentation); | |
57 scoped_ptr<base::Value> serialized_one_rect( | 55 scoped_ptr<base::Value> serialized_one_rect( |
58 one_rect_picture->AsValue()); | 56 one_rect_picture->AsValue()); |
59 | 57 |
60 // Reconstruct the picture. | 58 // Reconstruct the picture. |
61 scoped_refptr<Picture> one_rect_picture_check = | 59 scoped_refptr<Picture> one_rect_picture_check = |
62 Picture::CreateFromValue(serialized_one_rect.get()); | 60 Picture::CreateFromValue(serialized_one_rect.get()); |
63 EXPECT_TRUE(!!one_rect_picture_check.get()); | 61 EXPECT_TRUE(!!one_rect_picture_check.get()); |
64 | 62 |
65 // Check for equivalence. | 63 // Check for equivalence. |
66 unsigned char one_rect_buffer[4 * 100 * 100] = {0}; | 64 unsigned char one_rect_buffer[4 * 100 * 100] = {0}; |
67 DrawPicture(one_rect_buffer, layer_rect, one_rect_picture); | 65 DrawPicture(one_rect_buffer, layer_rect, one_rect_picture); |
68 unsigned char one_rect_buffer_check[4 * 100 * 100] = {0}; | 66 unsigned char one_rect_buffer_check[4 * 100 * 100] = {0}; |
69 DrawPicture(one_rect_buffer_check, layer_rect, one_rect_picture_check); | 67 DrawPicture(one_rect_buffer_check, layer_rect, one_rect_picture_check); |
70 | 68 |
71 EXPECT_EQ(one_rect_picture->LayerRect(), | 69 EXPECT_EQ(one_rect_picture->LayerRect(), |
72 one_rect_picture_check->LayerRect()); | 70 one_rect_picture_check->LayerRect()); |
73 EXPECT_EQ(one_rect_picture->OpaqueRect(), | 71 EXPECT_EQ(one_rect_picture->OpaqueRect(), |
74 one_rect_picture_check->OpaqueRect()); | 72 one_rect_picture_check->OpaqueRect()); |
75 EXPECT_TRUE( | 73 EXPECT_TRUE( |
76 memcmp(one_rect_buffer, one_rect_buffer_check, 4 * 100 * 100) == 0); | 74 memcmp(one_rect_buffer, one_rect_buffer_check, 4 * 100 * 100) == 0); |
77 | 75 |
78 // Two rect picture. | 76 // Two rect picture. |
79 content_layer_client.add_draw_rect(gfx::Rect(25, 25, 50, 50), green_paint); | 77 content_layer_client.add_draw_rect(gfx::Rect(25, 25, 50, 50), green_paint); |
80 scoped_refptr<Picture> two_rect_picture = Picture::Create(layer_rect); | 78 scoped_refptr<Picture> two_rect_picture = Picture::Create(layer_rect); |
81 two_rect_picture->Record(&content_layer_client, | 79 two_rect_picture->Record(&content_layer_client, |
82 tile_grid_info, | 80 tile_grid_info); |
83 &stats_instrumentation); | |
84 | 81 |
85 scoped_ptr<base::Value> serialized_two_rect( | 82 scoped_ptr<base::Value> serialized_two_rect( |
86 two_rect_picture->AsValue()); | 83 two_rect_picture->AsValue()); |
87 | 84 |
88 // Reconstruct the picture. | 85 // Reconstruct the picture. |
89 scoped_refptr<Picture> two_rect_picture_check = | 86 scoped_refptr<Picture> two_rect_picture_check = |
90 Picture::CreateFromValue(serialized_two_rect.get()); | 87 Picture::CreateFromValue(serialized_two_rect.get()); |
91 EXPECT_TRUE(!!two_rect_picture_check.get()); | 88 EXPECT_TRUE(!!two_rect_picture_check.get()); |
92 | 89 |
93 // Check for equivalence. | 90 // Check for equivalence. |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 CreateBitmap(gfx::Size(500, 500), "lazy", &lazy_bitmap[y][x]); | 129 CreateBitmap(gfx::Size(500, 500), "lazy", &lazy_bitmap[y][x]); |
133 content_layer_client.add_draw_bitmap( | 130 content_layer_client.add_draw_bitmap( |
134 lazy_bitmap[y][x], | 131 lazy_bitmap[y][x], |
135 gfx::Point(x * 512 + 6, y * 512 + 6)); | 132 gfx::Point(x * 512 + 6, y * 512 + 6)); |
136 } | 133 } |
137 } | 134 } |
138 } | 135 } |
139 | 136 |
140 scoped_refptr<Picture> picture = Picture::Create(layer_rect); | 137 scoped_refptr<Picture> picture = Picture::Create(layer_rect); |
141 picture->Record(&content_layer_client, | 138 picture->Record(&content_layer_client, |
142 tile_grid_info, | 139 tile_grid_info); |
143 &stats_instrumentation); | |
144 picture->GatherPixelRefs(tile_grid_info, &stats_instrumentation); | 140 picture->GatherPixelRefs(tile_grid_info, &stats_instrumentation); |
145 | 141 |
146 // Default iterator does not have any pixel refs | 142 // Default iterator does not have any pixel refs |
147 { | 143 { |
148 Picture::PixelRefIterator iterator; | 144 Picture::PixelRefIterator iterator; |
149 EXPECT_FALSE(iterator); | 145 EXPECT_FALSE(iterator); |
150 } | 146 } |
151 for (int y = 0; y < 4; ++y) { | 147 for (int y = 0; y < 4; ++y) { |
152 for (int x = 0; x < 4; ++x) { | 148 for (int x = 0; x < 4; ++x) { |
153 Picture::PixelRefIterator iterator(gfx::Rect(x * 512, y * 512, 500, 500), | 149 Picture::PixelRefIterator iterator(gfx::Rect(x * 512, y * 512, 500, 500), |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 CreateBitmap(gfx::Size(500, 500), "lazy", &lazy_bitmap[y][x]); | 226 CreateBitmap(gfx::Size(500, 500), "lazy", &lazy_bitmap[y][x]); |
231 content_layer_client.add_draw_bitmap( | 227 content_layer_client.add_draw_bitmap( |
232 lazy_bitmap[y][x], | 228 lazy_bitmap[y][x], |
233 gfx::Point(1024 + x * 512 + 6, y * 512 + 6)); | 229 gfx::Point(1024 + x * 512 + 6, y * 512 + 6)); |
234 } | 230 } |
235 } | 231 } |
236 } | 232 } |
237 | 233 |
238 scoped_refptr<Picture> picture = Picture::Create(layer_rect); | 234 scoped_refptr<Picture> picture = Picture::Create(layer_rect); |
239 picture->Record(&content_layer_client, | 235 picture->Record(&content_layer_client, |
240 tile_grid_info, | 236 tile_grid_info); |
241 &stats_instrumentation); | |
242 picture->GatherPixelRefs(tile_grid_info, &stats_instrumentation); | 237 picture->GatherPixelRefs(tile_grid_info, &stats_instrumentation); |
243 | 238 |
244 // Default iterator does not have any pixel refs | 239 // Default iterator does not have any pixel refs |
245 { | 240 { |
246 Picture::PixelRefIterator iterator; | 241 Picture::PixelRefIterator iterator; |
247 EXPECT_FALSE(iterator); | 242 EXPECT_FALSE(iterator); |
248 } | 243 } |
249 for (int y = 0; y < 4; ++y) { | 244 for (int y = 0; y < 4; ++y) { |
250 for (int x = 0; x < 4; ++x) { | 245 for (int x = 0; x < 4; ++x) { |
251 Picture::PixelRefIterator iterator( | 246 Picture::PixelRefIterator iterator( |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
352 CreateBitmap(gfx::Size(500, 500), "lazy", &lazy_bitmap[y][x]); | 347 CreateBitmap(gfx::Size(500, 500), "lazy", &lazy_bitmap[y][x]); |
353 content_layer_client.add_draw_bitmap( | 348 content_layer_client.add_draw_bitmap( |
354 lazy_bitmap[y][x], | 349 lazy_bitmap[y][x], |
355 gfx::Point(x * 512 + 6, y * 512 + 6)); | 350 gfx::Point(x * 512 + 6, y * 512 + 6)); |
356 } | 351 } |
357 } | 352 } |
358 } | 353 } |
359 | 354 |
360 scoped_refptr<Picture> picture = Picture::Create(layer_rect); | 355 scoped_refptr<Picture> picture = Picture::Create(layer_rect); |
361 picture->Record(&content_layer_client, | 356 picture->Record(&content_layer_client, |
362 tile_grid_info, | 357 tile_grid_info); |
363 &stats_instrumentation); | |
364 picture->GatherPixelRefs(tile_grid_info, &stats_instrumentation); | 358 picture->GatherPixelRefs(tile_grid_info, &stats_instrumentation); |
365 | 359 |
366 for (int y = 0; y < 4; ++y) { | 360 for (int y = 0; y < 4; ++y) { |
367 for (int x = 0; x < 4; ++x) { | 361 for (int x = 0; x < 4; ++x) { |
368 Picture::PixelRefIterator iterator( | 362 Picture::PixelRefIterator iterator( |
369 gfx::Rect(x * 512, y * 512 + 256, 1, 1), picture.get()); | 363 gfx::Rect(x * 512, y * 512 + 256, 1, 1), picture.get()); |
370 if ((x + y) & 1) { | 364 if ((x + y) & 1) { |
371 EXPECT_TRUE(iterator) << x << " " << y; | 365 EXPECT_TRUE(iterator) << x << " " << y; |
372 EXPECT_TRUE(*iterator == lazy_bitmap[y][x].pixelRef()); | 366 EXPECT_TRUE(*iterator == lazy_bitmap[y][x].pixelRef()); |
373 EXPECT_FALSE(++iterator) << x << " " << y; | 367 EXPECT_FALSE(++iterator) << x << " " << y; |
374 } else { | 368 } else { |
375 EXPECT_FALSE(iterator) << x << " " << y; | 369 EXPECT_FALSE(iterator) << x << " " << y; |
376 } | 370 } |
377 } | 371 } |
378 } | 372 } |
379 } | 373 } |
380 | 374 |
381 TEST(PictureTest, CreateFromSkpValue) { | 375 TEST(PictureTest, CreateFromSkpValue) { |
382 SkGraphics::Init(); | 376 SkGraphics::Init(); |
383 | 377 |
384 gfx::Rect layer_rect(100, 200); | 378 gfx::Rect layer_rect(100, 200); |
385 | 379 |
386 SkTileGridPicture::TileGridInfo tile_grid_info; | 380 SkTileGridPicture::TileGridInfo tile_grid_info; |
387 tile_grid_info.fTileInterval = SkISize::Make(100, 200); | 381 tile_grid_info.fTileInterval = SkISize::Make(100, 200); |
388 tile_grid_info.fMargin.setEmpty(); | 382 tile_grid_info.fMargin.setEmpty(); |
389 tile_grid_info.fOffset.setZero(); | 383 tile_grid_info.fOffset.setZero(); |
390 | 384 |
391 FakeContentLayerClient content_layer_client; | 385 FakeContentLayerClient content_layer_client; |
392 FakeRenderingStatsInstrumentation stats_instrumentation; | |
393 | 386 |
394 scoped_ptr<base::Value> tmp; | 387 scoped_ptr<base::Value> tmp; |
395 | 388 |
396 SkPaint red_paint; | 389 SkPaint red_paint; |
397 red_paint.setColor(SkColorSetARGB(255, 255, 0, 0)); | 390 red_paint.setColor(SkColorSetARGB(255, 255, 0, 0)); |
398 SkPaint green_paint; | 391 SkPaint green_paint; |
399 green_paint.setColor(SkColorSetARGB(255, 0, 255, 0)); | 392 green_paint.setColor(SkColorSetARGB(255, 0, 255, 0)); |
400 | 393 |
401 // Invalid picture (not a dict). | 394 // Invalid picture (not a dict). |
402 tmp.reset(new base::StringValue("abc!@#$%")); | 395 tmp.reset(new base::StringValue("abc!@#$%")); |
403 scoped_refptr<Picture> invalid_picture = | 396 scoped_refptr<Picture> invalid_picture = |
404 Picture::CreateFromSkpValue(tmp.get()); | 397 Picture::CreateFromSkpValue(tmp.get()); |
405 EXPECT_TRUE(!invalid_picture.get()); | 398 EXPECT_TRUE(!invalid_picture.get()); |
406 | 399 |
407 // Single full-size rect picture. | 400 // Single full-size rect picture. |
408 content_layer_client.add_draw_rect(layer_rect, red_paint); | 401 content_layer_client.add_draw_rect(layer_rect, red_paint); |
409 scoped_refptr<Picture> one_rect_picture = Picture::Create(layer_rect); | 402 scoped_refptr<Picture> one_rect_picture = Picture::Create(layer_rect); |
410 one_rect_picture->Record(&content_layer_client, | 403 one_rect_picture->Record(&content_layer_client, |
411 tile_grid_info, | 404 tile_grid_info); |
412 &stats_instrumentation); | |
413 scoped_ptr<base::Value> serialized_one_rect( | 405 scoped_ptr<base::Value> serialized_one_rect( |
414 one_rect_picture->AsValue()); | 406 one_rect_picture->AsValue()); |
415 | 407 |
416 const base::DictionaryValue* value = NULL; | 408 const base::DictionaryValue* value = NULL; |
417 EXPECT_TRUE(serialized_one_rect->GetAsDictionary(&value)); | 409 EXPECT_TRUE(serialized_one_rect->GetAsDictionary(&value)); |
418 | 410 |
419 // Decode the picture from base64. | 411 // Decode the picture from base64. |
420 const base::Value* skp_value; | 412 const base::Value* skp_value; |
421 EXPECT_TRUE(value->Get("skp64", &skp_value)); | 413 EXPECT_TRUE(value->Get("skp64", &skp_value)); |
422 | 414 |
423 // Reconstruct the picture. | 415 // Reconstruct the picture. |
424 scoped_refptr<Picture> one_rect_picture_check = | 416 scoped_refptr<Picture> one_rect_picture_check = |
425 Picture::CreateFromSkpValue(skp_value); | 417 Picture::CreateFromSkpValue(skp_value); |
426 EXPECT_TRUE(!!one_rect_picture_check.get()); | 418 EXPECT_TRUE(!!one_rect_picture_check.get()); |
427 | 419 |
428 EXPECT_EQ(100, one_rect_picture_check->LayerRect().width()); | 420 EXPECT_EQ(100, one_rect_picture_check->LayerRect().width()); |
429 EXPECT_EQ(200, one_rect_picture_check->LayerRect().height()); | 421 EXPECT_EQ(200, one_rect_picture_check->LayerRect().height()); |
430 EXPECT_EQ(100, one_rect_picture_check->OpaqueRect().width()); | 422 EXPECT_EQ(100, one_rect_picture_check->OpaqueRect().width()); |
431 EXPECT_EQ(200, one_rect_picture_check->OpaqueRect().height()); | 423 EXPECT_EQ(200, one_rect_picture_check->OpaqueRect().height()); |
432 } | 424 } |
433 } // namespace | 425 } // namespace |
434 } // namespace cc | 426 } // namespace cc |
OLD | NEW |