- add sources.
[platform/framework/web/crosswalk.git] / src / cc / resources / picture_unittest.cc
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 "cc/resources/picture.h"
6
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"
20
21 namespace cc {
22 namespace {
23
24 TEST(PictureTest, AsBase64String) {
25   SkGraphics::Init();
26
27   gfx::Rect layer_rect(100, 100);
28
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();
33
34   FakeContentLayerClient content_layer_client;
35
36   scoped_ptr<base::Value> tmp;
37
38   SkPaint red_paint;
39   red_paint.setColor(SkColorSetARGB(255, 255, 0, 0));
40   SkPaint green_paint;
41   green_paint.setColor(SkColorSetARGB(255, 0, 255, 0));
42
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());
48
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,
53                            tile_grid_info);
54   scoped_ptr<base::Value> serialized_one_rect(
55       one_rect_picture->AsValue());
56
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());
61
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);
67
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());
72   EXPECT_TRUE(
73       memcmp(one_rect_buffer, one_rect_buffer_check, 4 * 100 * 100) == 0);
74
75   // Two rect picture.
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,
79                            tile_grid_info);
80
81   scoped_ptr<base::Value> serialized_two_rect(
82       two_rect_picture->AsValue());
83
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());
88
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);
94
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());
99   EXPECT_TRUE(
100       memcmp(two_rect_buffer, two_rect_buffer_check, 4 * 100 * 100) == 0);
101 }
102
103 TEST(PictureTest, PixelRefIterator) {
104   gfx::Rect layer_rect(2048, 2048);
105
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();
110
111   FakeContentLayerClient content_layer_client;
112
113   // Lazy pixel refs are found in the following grids:
114   // |---|---|---|---|
115   // |   | x |   | x |
116   // |---|---|---|---|
117   // | x |   | x |   |
118   // |---|---|---|---|
119   // |   | x |   | x |
120   // |---|---|---|---|
121   // | x |   | x |   |
122   // |---|---|---|---|
123   SkBitmap lazy_bitmap[4][4];
124   for (int y = 0; y < 4; ++y) {
125     for (int x = 0; x < 4; ++x) {
126       if ((x + y) & 1) {
127         CreateBitmap(gfx::Size(500, 500), "lazy", &lazy_bitmap[y][x]);
128         SkPaint paint;
129         content_layer_client.add_draw_bitmap(
130             lazy_bitmap[y][x],
131             gfx::Point(x * 512 + 6, y * 512 + 6), paint);
132       }
133     }
134   }
135
136   scoped_refptr<Picture> picture = Picture::Create(layer_rect);
137   picture->Record(&content_layer_client,
138                   tile_grid_info);
139   picture->GatherPixelRefs(tile_grid_info);
140
141   // Default iterator does not have any pixel refs
142   {
143     Picture::PixelRefIterator iterator;
144     EXPECT_FALSE(iterator);
145   }
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),
149                                          picture.get());
150       if ((x + y) & 1) {
151         EXPECT_TRUE(iterator) << x << " " << y;
152         EXPECT_TRUE(*iterator == lazy_bitmap[y][x].pixelRef()) << x << " " << y;
153         EXPECT_FALSE(++iterator) << x << " " << y;
154       } else {
155         EXPECT_FALSE(iterator) << x << " " << y;
156       }
157     }
158   }
159   // Capture 4 pixel refs.
160   {
161     Picture::PixelRefIterator iterator(gfx::Rect(512, 512, 2048, 2048),
162                                        picture.get());
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);
172   }
173
174   // Copy test.
175   Picture::PixelRefIterator iterator(gfx::Rect(512, 512, 2048, 2048),
176                                      picture.get());
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());
181
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);
190
191   EXPECT_TRUE(copy);
192   EXPECT_TRUE(*copy == lazy_bitmap[2][1].pixelRef());
193   EXPECT_TRUE(++copy);
194   EXPECT_TRUE(*copy == lazy_bitmap[2][3].pixelRef());
195   EXPECT_TRUE(++copy);
196   EXPECT_TRUE(*copy == lazy_bitmap[3][2].pixelRef());
197   EXPECT_FALSE(++copy);
198 }
199
200 TEST(PictureTest, PixelRefIteratorNonZeroLayer) {
201   gfx::Rect layer_rect(1024, 0, 2048, 2048);
202
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();
207
208   FakeContentLayerClient content_layer_client;
209
210   // Lazy 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 lazy_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(gfx::Size(500, 500), "lazy", &lazy_bitmap[y][x]);
225         SkPaint paint;
226         content_layer_client.add_draw_bitmap(
227             lazy_bitmap[y][x],
228             gfx::Point(1024 + x * 512 + 6, y * 512 + 6), paint);
229       }
230     }
231   }
232
233   scoped_refptr<Picture> picture = Picture::Create(layer_rect);
234   picture->Record(&content_layer_client,
235                   tile_grid_info);
236   picture->GatherPixelRefs(tile_grid_info);
237
238   // Default iterator does not have any pixel refs
239   {
240     Picture::PixelRefIterator iterator;
241     EXPECT_FALSE(iterator);
242   }
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());
247       if ((x + y) & 1) {
248         EXPECT_TRUE(iterator) << x << " " << y;
249         EXPECT_TRUE(*iterator == lazy_bitmap[y][x].pixelRef());
250         EXPECT_FALSE(++iterator) << x << " " << y;
251       } else {
252         EXPECT_FALSE(iterator) << x << " " << y;
253       }
254     }
255   }
256   // Capture 4 pixel refs.
257   {
258     Picture::PixelRefIterator iterator(gfx::Rect(1024 + 512, 512, 2048, 2048),
259                                        picture.get());
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);
269   }
270
271   // Copy test.
272   {
273     Picture::PixelRefIterator iterator(gfx::Rect(1024 + 512, 512, 2048, 2048),
274                                        picture.get());
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());
279
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);
288
289     EXPECT_TRUE(copy);
290     EXPECT_TRUE(*copy == lazy_bitmap[2][1].pixelRef());
291     EXPECT_TRUE(++copy);
292     EXPECT_TRUE(*copy == lazy_bitmap[2][3].pixelRef());
293     EXPECT_TRUE(++copy);
294     EXPECT_TRUE(*copy == lazy_bitmap[3][2].pixelRef());
295     EXPECT_FALSE(++copy);
296   }
297
298   // Non intersecting rects
299   {
300     Picture::PixelRefIterator iterator(gfx::Rect(0, 0, 1000, 1000),
301                                        picture.get());
302     EXPECT_FALSE(iterator);
303   }
304   {
305     Picture::PixelRefIterator iterator(gfx::Rect(3500, 0, 1000, 1000),
306                                        picture.get());
307     EXPECT_FALSE(iterator);
308   }
309   {
310     Picture::PixelRefIterator iterator(gfx::Rect(0, 1100, 1000, 1000),
311                                        picture.get());
312     EXPECT_FALSE(iterator);
313   }
314   {
315     Picture::PixelRefIterator iterator(gfx::Rect(3500, 1100, 1000, 1000),
316                                        picture.get());
317     EXPECT_FALSE(iterator);
318   }
319 }
320
321 TEST(PictureTest, PixelRefIteratorOnePixelQuery) {
322   gfx::Rect layer_rect(2048, 2048);
323
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();
328
329   FakeContentLayerClient content_layer_client;
330
331   // Lazy pixel refs are found in the following grids:
332   // |---|---|---|---|
333   // |   | x |   | x |
334   // |---|---|---|---|
335   // | x |   | x |   |
336   // |---|---|---|---|
337   // |   | x |   | x |
338   // |---|---|---|---|
339   // | x |   | x |   |
340   // |---|---|---|---|
341   SkBitmap lazy_bitmap[4][4];
342   for (int y = 0; y < 4; ++y) {
343     for (int x = 0; x < 4; ++x) {
344       if ((x + y) & 1) {
345         CreateBitmap(gfx::Size(500, 500), "lazy", &lazy_bitmap[y][x]);
346         SkPaint paint;
347         content_layer_client.add_draw_bitmap(
348             lazy_bitmap[y][x],
349             gfx::Point(x * 512 + 6, y * 512 + 6), paint);
350       }
351     }
352   }
353
354   scoped_refptr<Picture> picture = Picture::Create(layer_rect);
355   picture->Record(&content_layer_client,
356                   tile_grid_info);
357   picture->GatherPixelRefs(tile_grid_info);
358
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());
363       if ((x + y) & 1) {
364         EXPECT_TRUE(iterator) << x << " " << y;
365         EXPECT_TRUE(*iterator == lazy_bitmap[y][x].pixelRef());
366         EXPECT_FALSE(++iterator) << x << " " << y;
367       } else {
368         EXPECT_FALSE(iterator) << x << " " << y;
369       }
370     }
371   }
372 }
373
374 TEST(PictureTest, CreateFromSkpValue) {
375   SkGraphics::Init();
376
377   gfx::Rect layer_rect(100, 200);
378
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();
383
384   FakeContentLayerClient content_layer_client;
385
386   scoped_ptr<base::Value> tmp;
387
388   SkPaint red_paint;
389   red_paint.setColor(SkColorSetARGB(255, 255, 0, 0));
390   SkPaint green_paint;
391   green_paint.setColor(SkColorSetARGB(255, 0, 255, 0));
392
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());
398
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,
403                            tile_grid_info);
404   scoped_ptr<base::Value> serialized_one_rect(
405       one_rect_picture->AsValue());
406
407   const base::DictionaryValue* value = NULL;
408   EXPECT_TRUE(serialized_one_rect->GetAsDictionary(&value));
409
410   // Decode the picture from base64.
411   const base::Value* skp_value;
412   EXPECT_TRUE(value->Get("skp64", &skp_value));
413
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());
418
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());
423 }
424 }  // namespace
425 }  // namespace cc