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 "cc/test/fake_content_layer_client.h" | 9 #include "cc/test/fake_content_layer_client.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
10 #include "third_party/skia/include/core/SkCanvas.h" | 11 #include "third_party/skia/include/core/SkCanvas.h" |
11 #include "third_party/skia/include/core/SkDevice.h" | 12 #include "third_party/skia/include/core/SkDevice.h" |
12 #include "third_party/skia/include/core/SkGraphics.h" | 13 #include "third_party/skia/include/core/SkGraphics.h" |
| 14 #include "third_party/skia/include/core/SkPixelRef.h" |
13 #include "third_party/skia/include/core/SkTileGridPicture.h" | 15 #include "third_party/skia/include/core/SkTileGridPicture.h" |
14 #include "ui/gfx/rect.h" | 16 #include "ui/gfx/rect.h" |
15 #include "ui/gfx/skia_util.h" | 17 #include "ui/gfx/skia_util.h" |
16 | 18 |
17 namespace cc { | 19 namespace cc { |
18 namespace { | 20 namespace { |
19 | 21 |
| 22 class TestLazyPixelRef : public skia::LazyPixelRef { |
| 23 public: |
| 24 // Pure virtual implementation. |
| 25 TestLazyPixelRef(int width, int height) |
| 26 : pixels_(new char[4 * width * height]) {} |
| 27 virtual SkFlattenable::Factory getFactory() OVERRIDE { return NULL; } |
| 28 virtual void* onLockPixels(SkColorTable** color_table) OVERRIDE { |
| 29 return pixels_.get(); |
| 30 } |
| 31 virtual void onUnlockPixels() OVERRIDE {} |
| 32 virtual bool PrepareToDecode(const PrepareParams& params) OVERRIDE { |
| 33 return true; |
| 34 } |
| 35 virtual SkPixelRef* deepCopy( |
| 36 SkBitmap::Config config, |
| 37 const SkIRect* subset) OVERRIDE { |
| 38 this->ref(); |
| 39 return this; |
| 40 } |
| 41 virtual void Decode() OVERRIDE {} |
| 42 private: |
| 43 scoped_ptr<char[]> pixels_; |
| 44 }; |
| 45 |
20 void DrawPicture(unsigned char* buffer, | 46 void DrawPicture(unsigned char* buffer, |
21 gfx::Rect layer_rect, | 47 gfx::Rect layer_rect, |
22 scoped_refptr<Picture> picture) { | 48 scoped_refptr<Picture> picture) { |
23 SkBitmap bitmap; | 49 SkBitmap bitmap; |
24 bitmap.setConfig(SkBitmap::kARGB_8888_Config, | 50 bitmap.setConfig(SkBitmap::kARGB_8888_Config, |
25 layer_rect.width(), | 51 layer_rect.width(), |
26 layer_rect.height()); | 52 layer_rect.height()); |
27 bitmap.setPixels(buffer); | 53 bitmap.setPixels(buffer); |
28 SkDevice device(bitmap); | 54 SkDevice device(bitmap); |
29 SkCanvas canvas(&device); | 55 SkCanvas canvas(&device); |
30 canvas.clipRect(gfx::RectToSkRect(layer_rect)); | 56 canvas.clipRect(gfx::RectToSkRect(layer_rect)); |
31 picture->Raster(&canvas, layer_rect, 1.0f, false); | 57 picture->Raster(&canvas, layer_rect, 1.0f, false); |
32 } | 58 } |
33 | 59 |
| 60 void CreateBitmap(gfx::Size size, const char* uri, SkBitmap* bitmap) { |
| 61 SkAutoTUnref<TestLazyPixelRef> lazy_pixel_ref; |
| 62 lazy_pixel_ref.reset(new TestLazyPixelRef(size.width(), size.height())); |
| 63 lazy_pixel_ref->setURI(uri); |
| 64 |
| 65 bitmap->setConfig(SkBitmap::kARGB_8888_Config, |
| 66 size.width(), |
| 67 size.height()); |
| 68 bitmap->setPixelRef(lazy_pixel_ref); |
| 69 } |
| 70 |
34 TEST(PictureTest, AsBase64String) { | 71 TEST(PictureTest, AsBase64String) { |
35 SkGraphics::Init(); | 72 SkGraphics::Init(); |
36 | 73 |
37 gfx::Rect layer_rect(100, 100); | 74 gfx::Rect layer_rect(100, 100); |
38 | 75 |
39 SkTileGridPicture::TileGridInfo tile_grid_info; | 76 SkTileGridPicture::TileGridInfo tile_grid_info; |
40 tile_grid_info.fTileInterval = SkISize::Make(100, 100); | 77 tile_grid_info.fTileInterval = SkISize::Make(100, 100); |
41 tile_grid_info.fMargin.setEmpty(); | 78 tile_grid_info.fMargin.setEmpty(); |
42 tile_grid_info.fOffset.setZero(); | 79 tile_grid_info.fOffset.setZero(); |
43 | 80 |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 DrawPicture(two_rect_buffer_check, layer_rect, two_rect_picture_check); | 150 DrawPicture(two_rect_buffer_check, layer_rect, two_rect_picture_check); |
114 | 151 |
115 EXPECT_EQ(two_rect_picture->LayerRect(), | 152 EXPECT_EQ(two_rect_picture->LayerRect(), |
116 two_rect_picture_check->LayerRect()); | 153 two_rect_picture_check->LayerRect()); |
117 EXPECT_EQ(two_rect_picture->OpaqueRect(), | 154 EXPECT_EQ(two_rect_picture->OpaqueRect(), |
118 two_rect_picture_check->OpaqueRect()); | 155 two_rect_picture_check->OpaqueRect()); |
119 EXPECT_TRUE( | 156 EXPECT_TRUE( |
120 memcmp(two_rect_buffer, two_rect_buffer_check, 4 * 100 * 100) == 0); | 157 memcmp(two_rect_buffer, two_rect_buffer_check, 4 * 100 * 100) == 0); |
121 } | 158 } |
122 | 159 |
| 160 TEST(PictureTest, PixelRefIterator) { |
| 161 gfx::Rect layer_rect(2048, 2048); |
| 162 |
| 163 SkTileGridPicture::TileGridInfo tile_grid_info; |
| 164 tile_grid_info.fTileInterval = SkISize::Make(512, 512); |
| 165 tile_grid_info.fMargin.setEmpty(); |
| 166 tile_grid_info.fOffset.setZero(); |
| 167 |
| 168 FakeContentLayerClient content_layer_client; |
| 169 |
| 170 // Lazy pixel refs are found in the following grids: |
| 171 // |---|---|---|---| |
| 172 // | | x | | x | |
| 173 // |---|---|---|---| |
| 174 // | x | | x | | |
| 175 // |---|---|---|---| |
| 176 // | | x | | x | |
| 177 // |---|---|---|---| |
| 178 // | x | | x | | |
| 179 // |---|---|---|---| |
| 180 SkBitmap lazy_bitmap[4][4]; |
| 181 for (int y = 0; y < 4; ++y) { |
| 182 for (int x = 0; x < 4; ++x) { |
| 183 if ((x + y) & 1) { |
| 184 CreateBitmap(gfx::Size(500, 500), "lazy", &lazy_bitmap[y][x]); |
| 185 content_layer_client.add_draw_bitmap( |
| 186 lazy_bitmap[y][x], |
| 187 gfx::Point(x * 512 + 6, y * 512 + 6)); |
| 188 } |
| 189 } |
| 190 } |
| 191 |
| 192 scoped_refptr<Picture> picture = Picture::Create(layer_rect); |
| 193 picture->Record(&content_layer_client, NULL, tile_grid_info); |
| 194 |
| 195 // Default iterator does not have any pixel refs |
| 196 { |
| 197 Picture::PixelRefIterator iterator; |
| 198 EXPECT_FALSE(iterator); |
| 199 } |
| 200 for (int y = 0; y < 4; ++y) { |
| 201 for (int x = 0; x < 4; ++x) { |
| 202 Picture::PixelRefIterator iterator( |
| 203 gfx::Rect(x * 512, y * 512, 500, 500), |
| 204 picture); |
| 205 if ((x + y) & 1) { |
| 206 EXPECT_TRUE(iterator) << x << " " << y; |
| 207 EXPECT_TRUE(*iterator == lazy_bitmap[y][x].pixelRef()) << x << " " << y; |
| 208 EXPECT_FALSE(++iterator) << x << " " << y; |
| 209 } else { |
| 210 EXPECT_FALSE(iterator) << x << " " << y; |
| 211 } |
| 212 } |
| 213 } |
| 214 // Capture 4 pixel refs. |
| 215 { |
| 216 Picture::PixelRefIterator iterator( |
| 217 gfx::Rect(512, 512, 2048, 2048), |
| 218 picture); |
| 219 EXPECT_TRUE(iterator); |
| 220 EXPECT_TRUE(*iterator == lazy_bitmap[1][2].pixelRef()); |
| 221 EXPECT_TRUE(++iterator); |
| 222 EXPECT_TRUE(*iterator == lazy_bitmap[2][1].pixelRef()); |
| 223 EXPECT_TRUE(++iterator); |
| 224 EXPECT_TRUE(*iterator == lazy_bitmap[2][3].pixelRef()); |
| 225 EXPECT_TRUE(++iterator); |
| 226 EXPECT_TRUE(*iterator == lazy_bitmap[3][2].pixelRef()); |
| 227 EXPECT_FALSE(++iterator); |
| 228 } |
| 229 |
| 230 // Copy test. |
| 231 Picture::PixelRefIterator iterator( |
| 232 gfx::Rect(512, 512, 2048, 2048), |
| 233 picture); |
| 234 EXPECT_TRUE(iterator); |
| 235 EXPECT_TRUE(*iterator == lazy_bitmap[1][2].pixelRef()); |
| 236 EXPECT_TRUE(++iterator); |
| 237 EXPECT_TRUE(*iterator == lazy_bitmap[2][1].pixelRef()); |
| 238 |
| 239 // copy now points to the same spot as iterator, |
| 240 // but both can be incremented independently. |
| 241 Picture::PixelRefIterator copy = iterator; |
| 242 EXPECT_TRUE(++iterator); |
| 243 EXPECT_TRUE(*iterator == lazy_bitmap[2][3].pixelRef()); |
| 244 EXPECT_TRUE(++iterator); |
| 245 EXPECT_TRUE(*iterator == lazy_bitmap[3][2].pixelRef()); |
| 246 EXPECT_FALSE(++iterator); |
| 247 |
| 248 EXPECT_TRUE(copy); |
| 249 EXPECT_TRUE(*copy == lazy_bitmap[2][1].pixelRef()); |
| 250 EXPECT_TRUE(++copy); |
| 251 EXPECT_TRUE(*copy == lazy_bitmap[2][3].pixelRef()); |
| 252 EXPECT_TRUE(++copy); |
| 253 EXPECT_TRUE(*copy == lazy_bitmap[3][2].pixelRef()); |
| 254 EXPECT_FALSE(++copy); |
| 255 } |
| 256 |
| 257 TEST(PictureTest, PixelRefIteratorNonZeroLayer) { |
| 258 gfx::Rect layer_rect(1024, 0, 2048, 2048); |
| 259 |
| 260 SkTileGridPicture::TileGridInfo tile_grid_info; |
| 261 tile_grid_info.fTileInterval = SkISize::Make(512, 512); |
| 262 tile_grid_info.fMargin.setEmpty(); |
| 263 tile_grid_info.fOffset.setZero(); |
| 264 |
| 265 FakeContentLayerClient content_layer_client; |
| 266 |
| 267 // Lazy pixel refs are found in the following grids: |
| 268 // |---|---|---|---| |
| 269 // | | x | | x | |
| 270 // |---|---|---|---| |
| 271 // | x | | x | | |
| 272 // |---|---|---|---| |
| 273 // | | x | | x | |
| 274 // |---|---|---|---| |
| 275 // | x | | x | | |
| 276 // |---|---|---|---| |
| 277 SkBitmap lazy_bitmap[4][4]; |
| 278 for (int y = 0; y < 4; ++y) { |
| 279 for (int x = 0; x < 4; ++x) { |
| 280 if ((x + y) & 1) { |
| 281 CreateBitmap(gfx::Size(500, 500), "lazy", &lazy_bitmap[y][x]); |
| 282 content_layer_client.add_draw_bitmap( |
| 283 lazy_bitmap[y][x], |
| 284 gfx::Point(1024 + x * 512 + 6, y * 512 + 6)); |
| 285 } |
| 286 } |
| 287 } |
| 288 |
| 289 scoped_refptr<Picture> picture = Picture::Create(layer_rect); |
| 290 picture->Record(&content_layer_client, NULL, tile_grid_info); |
| 291 |
| 292 // Default iterator does not have any pixel refs |
| 293 { |
| 294 Picture::PixelRefIterator iterator; |
| 295 EXPECT_FALSE(iterator); |
| 296 } |
| 297 for (int y = 0; y < 4; ++y) { |
| 298 for (int x = 0; x < 4; ++x) { |
| 299 Picture::PixelRefIterator iterator( |
| 300 gfx::Rect(1024 + x * 512, y * 512, 500, 500), |
| 301 picture); |
| 302 if ((x + y) & 1) { |
| 303 EXPECT_TRUE(iterator) << x << " " << y; |
| 304 EXPECT_TRUE(*iterator == lazy_bitmap[y][x].pixelRef()); |
| 305 EXPECT_FALSE(++iterator) << x << " " << y; |
| 306 } else { |
| 307 EXPECT_FALSE(iterator) << x << " " << y; |
| 308 } |
| 309 } |
| 310 } |
| 311 // Capture 4 pixel refs. |
| 312 { |
| 313 Picture::PixelRefIterator iterator( |
| 314 gfx::Rect(1024 + 512, 512, 2048, 2048), |
| 315 picture); |
| 316 EXPECT_TRUE(iterator); |
| 317 EXPECT_TRUE(*iterator == lazy_bitmap[1][2].pixelRef()); |
| 318 EXPECT_TRUE(++iterator); |
| 319 EXPECT_TRUE(*iterator == lazy_bitmap[2][1].pixelRef()); |
| 320 EXPECT_TRUE(++iterator); |
| 321 EXPECT_TRUE(*iterator == lazy_bitmap[2][3].pixelRef()); |
| 322 EXPECT_TRUE(++iterator); |
| 323 EXPECT_TRUE(*iterator == lazy_bitmap[3][2].pixelRef()); |
| 324 EXPECT_FALSE(++iterator); |
| 325 } |
| 326 |
| 327 // Copy test. |
| 328 { |
| 329 Picture::PixelRefIterator iterator( |
| 330 gfx::Rect(1024 + 512, 512, 2048, 2048), |
| 331 picture); |
| 332 EXPECT_TRUE(iterator); |
| 333 EXPECT_TRUE(*iterator == lazy_bitmap[1][2].pixelRef()); |
| 334 EXPECT_TRUE(++iterator); |
| 335 EXPECT_TRUE(*iterator == lazy_bitmap[2][1].pixelRef()); |
| 336 |
| 337 // copy now points to the same spot as iterator, |
| 338 // but both can be incremented independently. |
| 339 Picture::PixelRefIterator copy = iterator; |
| 340 EXPECT_TRUE(++iterator); |
| 341 EXPECT_TRUE(*iterator == lazy_bitmap[2][3].pixelRef()); |
| 342 EXPECT_TRUE(++iterator); |
| 343 EXPECT_TRUE(*iterator == lazy_bitmap[3][2].pixelRef()); |
| 344 EXPECT_FALSE(++iterator); |
| 345 |
| 346 EXPECT_TRUE(copy); |
| 347 EXPECT_TRUE(*copy == lazy_bitmap[2][1].pixelRef()); |
| 348 EXPECT_TRUE(++copy); |
| 349 EXPECT_TRUE(*copy == lazy_bitmap[2][3].pixelRef()); |
| 350 EXPECT_TRUE(++copy); |
| 351 EXPECT_TRUE(*copy == lazy_bitmap[3][2].pixelRef()); |
| 352 EXPECT_FALSE(++copy); |
| 353 } |
| 354 |
| 355 // Non intersecting rects |
| 356 { |
| 357 Picture::PixelRefIterator iterator( |
| 358 gfx::Rect(0, 0, 1000, 1000), |
| 359 picture); |
| 360 EXPECT_FALSE(iterator); |
| 361 } |
| 362 { |
| 363 Picture::PixelRefIterator iterator( |
| 364 gfx::Rect(3500, 0, 1000, 1000), |
| 365 picture); |
| 366 EXPECT_FALSE(iterator); |
| 367 } |
| 368 { |
| 369 Picture::PixelRefIterator iterator( |
| 370 gfx::Rect(0, 1100, 1000, 1000), |
| 371 picture); |
| 372 EXPECT_FALSE(iterator); |
| 373 } |
| 374 { |
| 375 Picture::PixelRefIterator iterator( |
| 376 gfx::Rect(3500, 1100, 1000, 1000), |
| 377 picture); |
| 378 EXPECT_FALSE(iterator); |
| 379 } |
| 380 } |
| 381 |
| 382 TEST(PictureTest, PixelRefIteratorOnePixelQuery) { |
| 383 gfx::Rect layer_rect(2048, 2048); |
| 384 |
| 385 SkTileGridPicture::TileGridInfo tile_grid_info; |
| 386 tile_grid_info.fTileInterval = SkISize::Make(512, 512); |
| 387 tile_grid_info.fMargin.setEmpty(); |
| 388 tile_grid_info.fOffset.setZero(); |
| 389 |
| 390 FakeContentLayerClient content_layer_client; |
| 391 |
| 392 // Lazy pixel refs are found in the following grids: |
| 393 // |---|---|---|---| |
| 394 // | | x | | x | |
| 395 // |---|---|---|---| |
| 396 // | x | | x | | |
| 397 // |---|---|---|---| |
| 398 // | | x | | x | |
| 399 // |---|---|---|---| |
| 400 // | x | | x | | |
| 401 // |---|---|---|---| |
| 402 SkBitmap lazy_bitmap[4][4]; |
| 403 for (int y = 0; y < 4; ++y) { |
| 404 for (int x = 0; x < 4; ++x) { |
| 405 if ((x + y) & 1) { |
| 406 CreateBitmap(gfx::Size(500, 500), "lazy", &lazy_bitmap[y][x]); |
| 407 content_layer_client.add_draw_bitmap( |
| 408 lazy_bitmap[y][x], |
| 409 gfx::Point(x * 512 + 6, y * 512 + 6)); |
| 410 } |
| 411 } |
| 412 } |
| 413 |
| 414 scoped_refptr<Picture> picture = Picture::Create(layer_rect); |
| 415 picture->Record(&content_layer_client, NULL, tile_grid_info); |
| 416 |
| 417 for (int y = 0; y < 4; ++y) { |
| 418 for (int x = 0; x < 4; ++x) { |
| 419 Picture::PixelRefIterator iterator( |
| 420 gfx::Rect(x * 512, y * 512 + 256, 1, 1), |
| 421 picture); |
| 422 if ((x + y) & 1) { |
| 423 EXPECT_TRUE(iterator) << x << " " << y; |
| 424 EXPECT_TRUE(*iterator == lazy_bitmap[y][x].pixelRef()); |
| 425 EXPECT_FALSE(++iterator) << x << " " << y; |
| 426 } else { |
| 427 EXPECT_FALSE(iterator) << x << " " << y; |
| 428 } |
| 429 } |
| 430 } |
| 431 } |
123 } // namespace | 432 } // namespace |
124 } // namespace cc | 433 } // namespace cc |
OLD | NEW |