Sync: Avoid deadlock in SyncBackendRegistrar / ModelSafeWorker on sync backend shutdown.
[chromium-blink-merge.git] / cc / resources / picture_unittest.cc
blob4abeceb54331441dd4ec983b3f9166da769d37e6
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 // Single full-size rect picture.
49 content_layer_client.add_draw_rect(layer_rect, red_paint);
51 scoped_refptr<Picture> one_rect_picture =
52 Picture::Create(layer_rect,
53 &content_layer_client,
54 tile_grid_info,
55 false,
56 Picture::RECORD_NORMALLY);
57 scoped_ptr<base::Value> serialized_one_rect(one_rect_picture->AsValue());
59 // Reconstruct the picture.
60 scoped_refptr<Picture> one_rect_picture_check =
61 Picture::CreateFromValue(serialized_one_rect.get());
62 EXPECT_TRUE(!!one_rect_picture_check.get());
64 // Check for equivalence.
65 unsigned char one_rect_buffer[4 * 100 * 100] = {0};
66 DrawPicture(one_rect_buffer, layer_rect, one_rect_picture);
67 unsigned char one_rect_buffer_check[4 * 100 * 100] = {0};
68 DrawPicture(one_rect_buffer_check, layer_rect, one_rect_picture_check);
70 EXPECT_EQ(one_rect_picture->LayerRect(), one_rect_picture_check->LayerRect());
71 EXPECT_EQ(0, memcmp(one_rect_buffer, one_rect_buffer_check, 4 * 100 * 100));
73 // Two rect picture.
74 content_layer_client.add_draw_rect(gfx::Rect(25, 25, 50, 50), green_paint);
76 scoped_refptr<Picture> two_rect_picture =
77 Picture::Create(layer_rect,
78 &content_layer_client,
79 tile_grid_info,
80 false,
81 Picture::RECORD_NORMALLY);
83 scoped_ptr<base::Value> serialized_two_rect(two_rect_picture->AsValue());
85 // Reconstruct the picture.
86 scoped_refptr<Picture> two_rect_picture_check =
87 Picture::CreateFromValue(serialized_two_rect.get());
88 EXPECT_TRUE(!!two_rect_picture_check.get());
90 // Check for equivalence.
91 unsigned char two_rect_buffer[4 * 100 * 100] = {0};
92 DrawPicture(two_rect_buffer, layer_rect, two_rect_picture);
93 unsigned char two_rect_buffer_check[4 * 100 * 100] = {0};
94 DrawPicture(two_rect_buffer_check, layer_rect, two_rect_picture_check);
96 EXPECT_EQ(two_rect_picture->LayerRect(), two_rect_picture_check->LayerRect());
97 EXPECT_EQ(0, memcmp(two_rect_buffer, two_rect_buffer_check, 4 * 100 * 100));
100 TEST(PictureTest, PixelRefIterator) {
101 gfx::Rect layer_rect(2048, 2048);
103 SkTileGridFactory::TileGridInfo tile_grid_info;
104 tile_grid_info.fTileInterval = SkISize::Make(512, 512);
105 tile_grid_info.fMargin.setEmpty();
106 tile_grid_info.fOffset.setZero();
108 FakeContentLayerClient content_layer_client;
110 // Discardable pixel refs are found in the following grids:
111 // |---|---|---|---|
112 // | | x | | x |
113 // |---|---|---|---|
114 // | x | | x | |
115 // |---|---|---|---|
116 // | | x | | x |
117 // |---|---|---|---|
118 // | x | | x | |
119 // |---|---|---|---|
120 SkBitmap discardable_bitmap[4][4];
121 for (int y = 0; y < 4; ++y) {
122 for (int x = 0; x < 4; ++x) {
123 if ((x + y) & 1) {
124 CreateBitmap(
125 gfx::Size(500, 500), "discardable", &discardable_bitmap[y][x]);
126 SkPaint paint;
127 content_layer_client.add_draw_bitmap(
128 discardable_bitmap[y][x],
129 gfx::Point(x * 512 + 6, y * 512 + 6), paint);
134 scoped_refptr<Picture> picture = Picture::Create(layer_rect,
135 &content_layer_client,
136 tile_grid_info,
137 true,
138 Picture::RECORD_NORMALLY);
140 // Default iterator does not have any pixel refs
142 Picture::PixelRefIterator iterator;
143 EXPECT_FALSE(iterator);
145 for (int y = 0; y < 4; ++y) {
146 for (int x = 0; x < 4; ++x) {
147 Picture::PixelRefIterator iterator(gfx::Rect(x * 512, y * 512, 500, 500),
148 picture.get());
149 if ((x + y) & 1) {
150 EXPECT_TRUE(iterator) << x << " " << y;
151 EXPECT_TRUE(*iterator == discardable_bitmap[y][x].pixelRef()) << x <<
152 " " << y;
153 EXPECT_FALSE(++iterator) << x << " " << y;
154 } else {
155 EXPECT_FALSE(iterator) << x << " " << y;
159 // Capture 4 pixel refs.
161 Picture::PixelRefIterator iterator(gfx::Rect(512, 512, 2048, 2048),
162 picture.get());
163 EXPECT_TRUE(iterator);
164 EXPECT_TRUE(*iterator == discardable_bitmap[1][2].pixelRef());
165 EXPECT_TRUE(++iterator);
166 EXPECT_TRUE(*iterator == discardable_bitmap[2][1].pixelRef());
167 EXPECT_TRUE(++iterator);
168 EXPECT_TRUE(*iterator == discardable_bitmap[2][3].pixelRef());
169 EXPECT_TRUE(++iterator);
170 EXPECT_TRUE(*iterator == discardable_bitmap[3][2].pixelRef());
171 EXPECT_FALSE(++iterator);
174 // Copy test.
175 Picture::PixelRefIterator iterator(gfx::Rect(512, 512, 2048, 2048),
176 picture.get());
177 EXPECT_TRUE(iterator);
178 EXPECT_TRUE(*iterator == discardable_bitmap[1][2].pixelRef());
179 EXPECT_TRUE(++iterator);
180 EXPECT_TRUE(*iterator == discardable_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 == discardable_bitmap[2][3].pixelRef());
187 EXPECT_TRUE(++iterator);
188 EXPECT_TRUE(*iterator == discardable_bitmap[3][2].pixelRef());
189 EXPECT_FALSE(++iterator);
191 EXPECT_TRUE(copy);
192 EXPECT_TRUE(*copy == discardable_bitmap[2][1].pixelRef());
193 EXPECT_TRUE(++copy);
194 EXPECT_TRUE(*copy == discardable_bitmap[2][3].pixelRef());
195 EXPECT_TRUE(++copy);
196 EXPECT_TRUE(*copy == discardable_bitmap[3][2].pixelRef());
197 EXPECT_FALSE(++copy);
200 TEST(PictureTest, PixelRefIteratorNonZeroLayer) {
201 gfx::Rect layer_rect(1024, 0, 2048, 2048);
203 SkTileGridFactory::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 // Discardable pixel refs are found in the following grids:
211 // |---|---|---|---|
212 // | | x | | x |
213 // |---|---|---|---|
214 // | x | | x | |
215 // |---|---|---|---|
216 // | | x | | x |
217 // |---|---|---|---|
218 // | x | | x | |
219 // |---|---|---|---|
220 SkBitmap discardable_bitmap[4][4];
221 for (int y = 0; y < 4; ++y) {
222 for (int x = 0; x < 4; ++x) {
223 if ((x + y) & 1) {
224 CreateBitmap(
225 gfx::Size(500, 500), "discardable", &discardable_bitmap[y][x]);
226 SkPaint paint;
227 content_layer_client.add_draw_bitmap(
228 discardable_bitmap[y][x],
229 gfx::Point(1024 + x * 512 + 6, y * 512 + 6), paint);
234 scoped_refptr<Picture> picture = Picture::Create(layer_rect,
235 &content_layer_client,
236 tile_grid_info,
237 true,
238 Picture::RECORD_NORMALLY);
240 // Default iterator does not have any pixel refs
242 Picture::PixelRefIterator iterator;
243 EXPECT_FALSE(iterator);
245 for (int y = 0; y < 4; ++y) {
246 for (int x = 0; x < 4; ++x) {
247 Picture::PixelRefIterator iterator(
248 gfx::Rect(1024 + x * 512, y * 512, 500, 500), picture.get());
249 if ((x + y) & 1) {
250 EXPECT_TRUE(iterator) << x << " " << y;
251 EXPECT_TRUE(*iterator == discardable_bitmap[y][x].pixelRef());
252 EXPECT_FALSE(++iterator) << x << " " << y;
253 } else {
254 EXPECT_FALSE(iterator) << x << " " << y;
258 // Capture 4 pixel refs.
260 Picture::PixelRefIterator iterator(gfx::Rect(1024 + 512, 512, 2048, 2048),
261 picture.get());
262 EXPECT_TRUE(iterator);
263 EXPECT_TRUE(*iterator == discardable_bitmap[1][2].pixelRef());
264 EXPECT_TRUE(++iterator);
265 EXPECT_TRUE(*iterator == discardable_bitmap[2][1].pixelRef());
266 EXPECT_TRUE(++iterator);
267 EXPECT_TRUE(*iterator == discardable_bitmap[2][3].pixelRef());
268 EXPECT_TRUE(++iterator);
269 EXPECT_TRUE(*iterator == discardable_bitmap[3][2].pixelRef());
270 EXPECT_FALSE(++iterator);
273 // Copy test.
275 Picture::PixelRefIterator iterator(gfx::Rect(1024 + 512, 512, 2048, 2048),
276 picture.get());
277 EXPECT_TRUE(iterator);
278 EXPECT_TRUE(*iterator == discardable_bitmap[1][2].pixelRef());
279 EXPECT_TRUE(++iterator);
280 EXPECT_TRUE(*iterator == discardable_bitmap[2][1].pixelRef());
282 // copy now points to the same spot as iterator,
283 // but both can be incremented independently.
284 Picture::PixelRefIterator copy = iterator;
285 EXPECT_TRUE(++iterator);
286 EXPECT_TRUE(*iterator == discardable_bitmap[2][3].pixelRef());
287 EXPECT_TRUE(++iterator);
288 EXPECT_TRUE(*iterator == discardable_bitmap[3][2].pixelRef());
289 EXPECT_FALSE(++iterator);
291 EXPECT_TRUE(copy);
292 EXPECT_TRUE(*copy == discardable_bitmap[2][1].pixelRef());
293 EXPECT_TRUE(++copy);
294 EXPECT_TRUE(*copy == discardable_bitmap[2][3].pixelRef());
295 EXPECT_TRUE(++copy);
296 EXPECT_TRUE(*copy == discardable_bitmap[3][2].pixelRef());
297 EXPECT_FALSE(++copy);
300 // Non intersecting rects
302 Picture::PixelRefIterator iterator(gfx::Rect(0, 0, 1000, 1000),
303 picture.get());
304 EXPECT_FALSE(iterator);
307 Picture::PixelRefIterator iterator(gfx::Rect(3500, 0, 1000, 1000),
308 picture.get());
309 EXPECT_FALSE(iterator);
312 Picture::PixelRefIterator iterator(gfx::Rect(0, 1100, 1000, 1000),
313 picture.get());
314 EXPECT_FALSE(iterator);
317 Picture::PixelRefIterator iterator(gfx::Rect(3500, 1100, 1000, 1000),
318 picture.get());
319 EXPECT_FALSE(iterator);
323 TEST(PictureTest, PixelRefIteratorOnePixelQuery) {
324 gfx::Rect layer_rect(2048, 2048);
326 SkTileGridFactory::TileGridInfo tile_grid_info;
327 tile_grid_info.fTileInterval = SkISize::Make(512, 512);
328 tile_grid_info.fMargin.setEmpty();
329 tile_grid_info.fOffset.setZero();
331 FakeContentLayerClient content_layer_client;
333 // Discardable pixel refs are found in the following grids:
334 // |---|---|---|---|
335 // | | x | | x |
336 // |---|---|---|---|
337 // | x | | x | |
338 // |---|---|---|---|
339 // | | x | | x |
340 // |---|---|---|---|
341 // | x | | x | |
342 // |---|---|---|---|
343 SkBitmap discardable_bitmap[4][4];
344 for (int y = 0; y < 4; ++y) {
345 for (int x = 0; x < 4; ++x) {
346 if ((x + y) & 1) {
347 CreateBitmap(
348 gfx::Size(500, 500), "discardable", &discardable_bitmap[y][x]);
349 SkPaint paint;
350 content_layer_client.add_draw_bitmap(
351 discardable_bitmap[y][x],
352 gfx::Point(x * 512 + 6, y * 512 + 6), paint);
357 scoped_refptr<Picture> picture = Picture::Create(layer_rect,
358 &content_layer_client,
359 tile_grid_info,
360 true,
361 Picture::RECORD_NORMALLY);
363 for (int y = 0; y < 4; ++y) {
364 for (int x = 0; x < 4; ++x) {
365 Picture::PixelRefIterator iterator(
366 gfx::Rect(x * 512, y * 512 + 256, 1, 1), picture.get());
367 if ((x + y) & 1) {
368 EXPECT_TRUE(iterator) << x << " " << y;
369 EXPECT_TRUE(*iterator == discardable_bitmap[y][x].pixelRef());
370 EXPECT_FALSE(++iterator) << x << " " << y;
371 } else {
372 EXPECT_FALSE(iterator) << x << " " << y;
378 TEST(PictureTest, CreateFromSkpValue) {
379 SkGraphics::Init();
381 gfx::Rect layer_rect(100, 200);
383 SkTileGridFactory::TileGridInfo tile_grid_info;
384 tile_grid_info.fTileInterval = SkISize::Make(100, 200);
385 tile_grid_info.fMargin.setEmpty();
386 tile_grid_info.fOffset.setZero();
388 FakeContentLayerClient content_layer_client;
390 scoped_ptr<base::Value> tmp;
392 SkPaint red_paint;
393 red_paint.setColor(SkColorSetARGB(255, 255, 0, 0));
394 SkPaint green_paint;
395 green_paint.setColor(SkColorSetARGB(255, 0, 255, 0));
397 // Invalid picture (not a dict).
398 tmp.reset(new base::StringValue("abc!@#$%"));
399 scoped_refptr<Picture> invalid_picture =
400 Picture::CreateFromSkpValue(tmp.get());
401 EXPECT_TRUE(!invalid_picture.get());
403 // Single full-size rect picture.
404 content_layer_client.add_draw_rect(layer_rect, red_paint);
405 scoped_refptr<Picture> one_rect_picture =
406 Picture::Create(layer_rect,
407 &content_layer_client,
408 tile_grid_info,
409 false,
410 Picture::RECORD_NORMALLY);
411 scoped_ptr<base::Value> serialized_one_rect(
412 one_rect_picture->AsValue());
414 const base::DictionaryValue* value = NULL;
415 EXPECT_TRUE(serialized_one_rect->GetAsDictionary(&value));
417 // Decode the picture from base64.
418 const base::Value* skp_value;
419 EXPECT_TRUE(value->Get("skp64", &skp_value));
421 // Reconstruct the picture.
422 scoped_refptr<Picture> one_rect_picture_check =
423 Picture::CreateFromSkpValue(skp_value);
424 EXPECT_TRUE(!!one_rect_picture_check.get());
426 EXPECT_EQ(100, one_rect_picture_check->LayerRect().width());
427 EXPECT_EQ(200, one_rect_picture_check->LayerRect().height());
430 TEST(PictureTest, RecordingModes) {
431 SkGraphics::Init();
433 gfx::Rect layer_rect(100, 200);
435 SkTileGridFactory::TileGridInfo tile_grid_info;
436 tile_grid_info.fTileInterval = SkISize::Make(100, 200);
437 tile_grid_info.fMargin.setEmpty();
438 tile_grid_info.fOffset.setZero();
440 FakeContentLayerClient content_layer_client;
441 EXPECT_EQ(NULL, content_layer_client.last_canvas());
443 scoped_refptr<Picture> picture = Picture::Create(layer_rect,
444 &content_layer_client,
445 tile_grid_info,
446 false,
447 Picture::RECORD_NORMALLY);
448 EXPECT_TRUE(content_layer_client.last_canvas() != NULL);
449 EXPECT_EQ(ContentLayerClient::GRAPHICS_CONTEXT_ENABLED,
450 content_layer_client.last_context_status());
451 EXPECT_TRUE(picture.get());
453 picture = Picture::Create(layer_rect,
454 &content_layer_client,
455 tile_grid_info,
456 false,
457 Picture::RECORD_WITH_SK_NULL_CANVAS);
458 EXPECT_TRUE(content_layer_client.last_canvas() != NULL);
459 EXPECT_EQ(ContentLayerClient::GRAPHICS_CONTEXT_ENABLED,
460 content_layer_client.last_context_status());
461 EXPECT_TRUE(picture.get());
463 picture = Picture::Create(layer_rect,
464 &content_layer_client,
465 tile_grid_info,
466 false,
467 Picture::RECORD_WITH_PAINTING_DISABLED);
468 EXPECT_TRUE(content_layer_client.last_canvas() != NULL);
469 EXPECT_EQ(ContentLayerClient::GRAPHICS_CONTEXT_DISABLED,
470 content_layer_client.last_context_status());
471 EXPECT_TRUE(picture.get());
473 EXPECT_EQ(3, Picture::RECORDING_MODE_COUNT);
476 } // namespace
477 } // namespace cc