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/SkCanvas.h"
14 #include "third_party/skia/include/core/SkDevice.h"
15 #include "third_party/skia/include/core/SkGraphics.h"
16 #include "third_party/skia/include/core/SkPixelRef.h"
17 #include "third_party/skia/include/core/SkTileGridPicture.h"
18 #include "ui/gfx/rect.h"
19 #include "ui/gfx/skia_util.h"
24 TEST(PictureTest, AsBase64String) {
27 gfx::Rect layer_rect(100, 100);
29 SkTileGridPicture::TileGridInfo tile_grid_info;
30 tile_grid_info.fTileInterval = SkISize::Make(100, 100);
31 tile_grid_info.fMargin.setEmpty();
32 tile_grid_info.fOffset.setZero();
34 FakeContentLayerClient content_layer_client;
36 scoped_ptr<base::Value> tmp;
39 red_paint.setColor(SkColorSetARGB(255, 255, 0, 0));
41 green_paint.setColor(SkColorSetARGB(255, 0, 255, 0));
43 // Invalid picture (not a dict).
44 tmp.reset(new base::StringValue("abc!@#$%"));
45 scoped_refptr<Picture> invalid_picture =
46 Picture::CreateFromValue(tmp.get());
47 EXPECT_TRUE(!invalid_picture.get());
49 // Single full-size rect picture.
50 content_layer_client.add_draw_rect(layer_rect, red_paint);
51 scoped_refptr<Picture> one_rect_picture = Picture::Create(layer_rect);
52 one_rect_picture->Record(&content_layer_client,
54 scoped_ptr<base::Value> serialized_one_rect(
55 one_rect_picture->AsValue());
57 // Reconstruct the picture.
58 scoped_refptr<Picture> one_rect_picture_check =
59 Picture::CreateFromValue(serialized_one_rect.get());
60 EXPECT_TRUE(!!one_rect_picture_check.get());
62 // Check for equivalence.
63 unsigned char one_rect_buffer[4 * 100 * 100] = {0};
64 DrawPicture(one_rect_buffer, layer_rect, one_rect_picture);
65 unsigned char one_rect_buffer_check[4 * 100 * 100] = {0};
66 DrawPicture(one_rect_buffer_check, layer_rect, one_rect_picture_check);
68 EXPECT_EQ(one_rect_picture->LayerRect(),
69 one_rect_picture_check->LayerRect());
70 EXPECT_EQ(one_rect_picture->OpaqueRect(),
71 one_rect_picture_check->OpaqueRect());
73 memcmp(one_rect_buffer, one_rect_buffer_check, 4 * 100 * 100) == 0);
76 content_layer_client.add_draw_rect(gfx::Rect(25, 25, 50, 50), green_paint);
77 scoped_refptr<Picture> two_rect_picture = Picture::Create(layer_rect);
78 two_rect_picture->Record(&content_layer_client,
81 scoped_ptr<base::Value> serialized_two_rect(
82 two_rect_picture->AsValue());
84 // Reconstruct the picture.
85 scoped_refptr<Picture> two_rect_picture_check =
86 Picture::CreateFromValue(serialized_two_rect.get());
87 EXPECT_TRUE(!!two_rect_picture_check.get());
89 // Check for equivalence.
90 unsigned char two_rect_buffer[4 * 100 * 100] = {0};
91 DrawPicture(two_rect_buffer, layer_rect, two_rect_picture);
92 unsigned char two_rect_buffer_check[4 * 100 * 100] = {0};
93 DrawPicture(two_rect_buffer_check, layer_rect, two_rect_picture_check);
95 EXPECT_EQ(two_rect_picture->LayerRect(),
96 two_rect_picture_check->LayerRect());
97 EXPECT_EQ(two_rect_picture->OpaqueRect(),
98 two_rect_picture_check->OpaqueRect());
100 memcmp(two_rect_buffer, two_rect_buffer_check, 4 * 100 * 100) == 0);
103 TEST(PictureTest, PixelRefIterator) {
104 gfx::Rect layer_rect(2048, 2048);
106 SkTileGridPicture::TileGridInfo tile_grid_info;
107 tile_grid_info.fTileInterval = SkISize::Make(512, 512);
108 tile_grid_info.fMargin.setEmpty();
109 tile_grid_info.fOffset.setZero();
111 FakeContentLayerClient content_layer_client;
113 // Lazy pixel refs are found in the following grids:
123 SkBitmap lazy_bitmap[4][4];
124 for (int y = 0; y < 4; ++y) {
125 for (int x = 0; x < 4; ++x) {
127 CreateBitmap(gfx::Size(500, 500), "lazy", &lazy_bitmap[y][x]);
129 content_layer_client.add_draw_bitmap(
131 gfx::Point(x * 512 + 6, y * 512 + 6), paint);
136 scoped_refptr<Picture> picture = Picture::Create(layer_rect);
137 picture->Record(&content_layer_client,
139 picture->GatherPixelRefs(tile_grid_info);
141 // Default iterator does not have any pixel refs
143 Picture::PixelRefIterator iterator;
144 EXPECT_FALSE(iterator);
146 for (int y = 0; y < 4; ++y) {
147 for (int x = 0; x < 4; ++x) {
148 Picture::PixelRefIterator iterator(gfx::Rect(x * 512, y * 512, 500, 500),
151 EXPECT_TRUE(iterator) << x << " " << y;
152 EXPECT_TRUE(*iterator == lazy_bitmap[y][x].pixelRef()) << x << " " << y;
153 EXPECT_FALSE(++iterator) << x << " " << y;
155 EXPECT_FALSE(iterator) << x << " " << y;
159 // Capture 4 pixel refs.
161 Picture::PixelRefIterator iterator(gfx::Rect(512, 512, 2048, 2048),
163 EXPECT_TRUE(iterator);
164 EXPECT_TRUE(*iterator == lazy_bitmap[1][2].pixelRef());
165 EXPECT_TRUE(++iterator);
166 EXPECT_TRUE(*iterator == lazy_bitmap[2][1].pixelRef());
167 EXPECT_TRUE(++iterator);
168 EXPECT_TRUE(*iterator == lazy_bitmap[2][3].pixelRef());
169 EXPECT_TRUE(++iterator);
170 EXPECT_TRUE(*iterator == lazy_bitmap[3][2].pixelRef());
171 EXPECT_FALSE(++iterator);
175 Picture::PixelRefIterator iterator(gfx::Rect(512, 512, 2048, 2048),
177 EXPECT_TRUE(iterator);
178 EXPECT_TRUE(*iterator == lazy_bitmap[1][2].pixelRef());
179 EXPECT_TRUE(++iterator);
180 EXPECT_TRUE(*iterator == lazy_bitmap[2][1].pixelRef());
182 // copy now points to the same spot as iterator,
183 // but both can be incremented independently.
184 Picture::PixelRefIterator copy = iterator;
185 EXPECT_TRUE(++iterator);
186 EXPECT_TRUE(*iterator == lazy_bitmap[2][3].pixelRef());
187 EXPECT_TRUE(++iterator);
188 EXPECT_TRUE(*iterator == lazy_bitmap[3][2].pixelRef());
189 EXPECT_FALSE(++iterator);
192 EXPECT_TRUE(*copy == lazy_bitmap[2][1].pixelRef());
194 EXPECT_TRUE(*copy == lazy_bitmap[2][3].pixelRef());
196 EXPECT_TRUE(*copy == lazy_bitmap[3][2].pixelRef());
197 EXPECT_FALSE(++copy);
200 TEST(PictureTest, PixelRefIteratorNonZeroLayer) {
201 gfx::Rect layer_rect(1024, 0, 2048, 2048);
203 SkTileGridPicture::TileGridInfo tile_grid_info;
204 tile_grid_info.fTileInterval = SkISize::Make(512, 512);
205 tile_grid_info.fMargin.setEmpty();
206 tile_grid_info.fOffset.setZero();
208 FakeContentLayerClient content_layer_client;
210 // Lazy pixel refs are found in the following grids:
220 SkBitmap lazy_bitmap[4][4];
221 for (int y = 0; y < 4; ++y) {
222 for (int x = 0; x < 4; ++x) {
224 CreateBitmap(gfx::Size(500, 500), "lazy", &lazy_bitmap[y][x]);
226 content_layer_client.add_draw_bitmap(
228 gfx::Point(1024 + x * 512 + 6, y * 512 + 6), paint);
233 scoped_refptr<Picture> picture = Picture::Create(layer_rect);
234 picture->Record(&content_layer_client,
236 picture->GatherPixelRefs(tile_grid_info);
238 // Default iterator does not have any pixel refs
240 Picture::PixelRefIterator iterator;
241 EXPECT_FALSE(iterator);
243 for (int y = 0; y < 4; ++y) {
244 for (int x = 0; x < 4; ++x) {
245 Picture::PixelRefIterator iterator(
246 gfx::Rect(1024 + x * 512, y * 512, 500, 500), picture.get());
248 EXPECT_TRUE(iterator) << x << " " << y;
249 EXPECT_TRUE(*iterator == lazy_bitmap[y][x].pixelRef());
250 EXPECT_FALSE(++iterator) << x << " " << y;
252 EXPECT_FALSE(iterator) << x << " " << y;
256 // Capture 4 pixel refs.
258 Picture::PixelRefIterator iterator(gfx::Rect(1024 + 512, 512, 2048, 2048),
260 EXPECT_TRUE(iterator);
261 EXPECT_TRUE(*iterator == lazy_bitmap[1][2].pixelRef());
262 EXPECT_TRUE(++iterator);
263 EXPECT_TRUE(*iterator == lazy_bitmap[2][1].pixelRef());
264 EXPECT_TRUE(++iterator);
265 EXPECT_TRUE(*iterator == lazy_bitmap[2][3].pixelRef());
266 EXPECT_TRUE(++iterator);
267 EXPECT_TRUE(*iterator == lazy_bitmap[3][2].pixelRef());
268 EXPECT_FALSE(++iterator);
273 Picture::PixelRefIterator iterator(gfx::Rect(1024 + 512, 512, 2048, 2048),
275 EXPECT_TRUE(iterator);
276 EXPECT_TRUE(*iterator == lazy_bitmap[1][2].pixelRef());
277 EXPECT_TRUE(++iterator);
278 EXPECT_TRUE(*iterator == lazy_bitmap[2][1].pixelRef());
280 // copy now points to the same spot as iterator,
281 // but both can be incremented independently.
282 Picture::PixelRefIterator copy = iterator;
283 EXPECT_TRUE(++iterator);
284 EXPECT_TRUE(*iterator == lazy_bitmap[2][3].pixelRef());
285 EXPECT_TRUE(++iterator);
286 EXPECT_TRUE(*iterator == lazy_bitmap[3][2].pixelRef());
287 EXPECT_FALSE(++iterator);
290 EXPECT_TRUE(*copy == lazy_bitmap[2][1].pixelRef());
292 EXPECT_TRUE(*copy == lazy_bitmap[2][3].pixelRef());
294 EXPECT_TRUE(*copy == lazy_bitmap[3][2].pixelRef());
295 EXPECT_FALSE(++copy);
298 // Non intersecting rects
300 Picture::PixelRefIterator iterator(gfx::Rect(0, 0, 1000, 1000),
302 EXPECT_FALSE(iterator);
305 Picture::PixelRefIterator iterator(gfx::Rect(3500, 0, 1000, 1000),
307 EXPECT_FALSE(iterator);
310 Picture::PixelRefIterator iterator(gfx::Rect(0, 1100, 1000, 1000),
312 EXPECT_FALSE(iterator);
315 Picture::PixelRefIterator iterator(gfx::Rect(3500, 1100, 1000, 1000),
317 EXPECT_FALSE(iterator);
321 TEST(PictureTest, PixelRefIteratorOnePixelQuery) {
322 gfx::Rect layer_rect(2048, 2048);
324 SkTileGridPicture::TileGridInfo tile_grid_info;
325 tile_grid_info.fTileInterval = SkISize::Make(512, 512);
326 tile_grid_info.fMargin.setEmpty();
327 tile_grid_info.fOffset.setZero();
329 FakeContentLayerClient content_layer_client;
331 // Lazy pixel refs are found in the following grids:
341 SkBitmap lazy_bitmap[4][4];
342 for (int y = 0; y < 4; ++y) {
343 for (int x = 0; x < 4; ++x) {
345 CreateBitmap(gfx::Size(500, 500), "lazy", &lazy_bitmap[y][x]);
347 content_layer_client.add_draw_bitmap(
349 gfx::Point(x * 512 + 6, y * 512 + 6), paint);
354 scoped_refptr<Picture> picture = Picture::Create(layer_rect);
355 picture->Record(&content_layer_client,
357 picture->GatherPixelRefs(tile_grid_info);
359 for (int y = 0; y < 4; ++y) {
360 for (int x = 0; x < 4; ++x) {
361 Picture::PixelRefIterator iterator(
362 gfx::Rect(x * 512, y * 512 + 256, 1, 1), picture.get());
364 EXPECT_TRUE(iterator) << x << " " << y;
365 EXPECT_TRUE(*iterator == lazy_bitmap[y][x].pixelRef());
366 EXPECT_FALSE(++iterator) << x << " " << y;
368 EXPECT_FALSE(iterator) << x << " " << y;
374 TEST(PictureTest, CreateFromSkpValue) {
377 gfx::Rect layer_rect(100, 200);
379 SkTileGridPicture::TileGridInfo tile_grid_info;
380 tile_grid_info.fTileInterval = SkISize::Make(100, 200);
381 tile_grid_info.fMargin.setEmpty();
382 tile_grid_info.fOffset.setZero();
384 FakeContentLayerClient content_layer_client;
386 scoped_ptr<base::Value> tmp;
389 red_paint.setColor(SkColorSetARGB(255, 255, 0, 0));
391 green_paint.setColor(SkColorSetARGB(255, 0, 255, 0));
393 // Invalid picture (not a dict).
394 tmp.reset(new base::StringValue("abc!@#$%"));
395 scoped_refptr<Picture> invalid_picture =
396 Picture::CreateFromSkpValue(tmp.get());
397 EXPECT_TRUE(!invalid_picture.get());
399 // Single full-size rect picture.
400 content_layer_client.add_draw_rect(layer_rect, red_paint);
401 scoped_refptr<Picture> one_rect_picture = Picture::Create(layer_rect);
402 one_rect_picture->Record(&content_layer_client,
404 scoped_ptr<base::Value> serialized_one_rect(
405 one_rect_picture->AsValue());
407 const base::DictionaryValue* value = NULL;
408 EXPECT_TRUE(serialized_one_rect->GetAsDictionary(&value));
410 // Decode the picture from base64.
411 const base::Value* skp_value;
412 EXPECT_TRUE(value->Get("skp64", &skp_value));
414 // Reconstruct the picture.
415 scoped_refptr<Picture> one_rect_picture_check =
416 Picture::CreateFromSkpValue(skp_value);
417 EXPECT_TRUE(!!one_rect_picture_check.get());
419 EXPECT_EQ(100, one_rect_picture_check->LayerRect().width());
420 EXPECT_EQ(200, one_rect_picture_check->LayerRect().height());
421 EXPECT_EQ(100, one_rect_picture_check->OpaqueRect().width());
422 EXPECT_EQ(200, one_rect_picture_check->OpaqueRect().height());