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, LazyPixelRefIterator) { |
| 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::LazyPixelRefIterator iterator; |
| 198 EXPECT_FALSE(iterator); |
| 199 // Incrementing an invalid iterator does not do anything. |
| 200 EXPECT_FALSE(++iterator); |
| 201 EXPECT_FALSE(++iterator); |
| 202 EXPECT_FALSE(++iterator); |
| 203 EXPECT_FALSE(++iterator); |
| 204 } |
| 205 for (int y = 0; y < 4; ++y) { |
| 206 for (int x = 0; x < 4; ++x) { |
| 207 Picture::LazyPixelRefIterator iterator( |
| 208 gfx::Rect(x * 512, y * 512, 500, 500), |
| 209 picture); |
| 210 if ((x + y) & 1) { |
| 211 EXPECT_TRUE(iterator) << x << " " << y; |
| 212 EXPECT_TRUE(*iterator == lazy_bitmap[y][x].pixelRef()) << x << " " << y; |
| 213 EXPECT_FALSE(++iterator) << x << " " << y; |
| 214 } else { |
| 215 EXPECT_FALSE(iterator) << x << " " << y; |
| 216 } |
| 217 } |
| 218 } |
| 219 // Capture 4 pixel refs. |
| 220 { |
| 221 Picture::LazyPixelRefIterator iterator( |
| 222 gfx::Rect(512, 512, 2048, 2048), |
| 223 picture); |
| 224 EXPECT_TRUE(iterator); |
| 225 EXPECT_TRUE(*iterator == lazy_bitmap[1][2].pixelRef()); |
| 226 EXPECT_TRUE(++iterator); |
| 227 EXPECT_TRUE(*iterator == lazy_bitmap[2][1].pixelRef()); |
| 228 EXPECT_TRUE(++iterator); |
| 229 EXPECT_TRUE(*iterator == lazy_bitmap[2][3].pixelRef()); |
| 230 EXPECT_TRUE(++iterator); |
| 231 EXPECT_TRUE(*iterator == lazy_bitmap[3][2].pixelRef()); |
| 232 EXPECT_FALSE(++iterator); |
| 233 } |
| 234 |
| 235 // Copy test. |
| 236 Picture::LazyPixelRefIterator iterator( |
| 237 gfx::Rect(512, 512, 2048, 2048), |
| 238 picture); |
| 239 EXPECT_TRUE(iterator); |
| 240 EXPECT_TRUE(*iterator == lazy_bitmap[1][2].pixelRef()); |
| 241 EXPECT_TRUE(++iterator); |
| 242 EXPECT_TRUE(*iterator == lazy_bitmap[2][1].pixelRef()); |
| 243 |
| 244 // copy now points to the same spot as iterator, |
| 245 // but both can be incremented independently. |
| 246 Picture::LazyPixelRefIterator copy = iterator; |
| 247 EXPECT_TRUE(++iterator); |
| 248 EXPECT_TRUE(*iterator == lazy_bitmap[2][3].pixelRef()); |
| 249 EXPECT_TRUE(++iterator); |
| 250 EXPECT_TRUE(*iterator == lazy_bitmap[3][2].pixelRef()); |
| 251 EXPECT_FALSE(++iterator); |
| 252 |
| 253 EXPECT_TRUE(copy); |
| 254 EXPECT_TRUE(*copy == lazy_bitmap[2][1].pixelRef()); |
| 255 EXPECT_TRUE(++copy); |
| 256 EXPECT_TRUE(*copy == lazy_bitmap[2][3].pixelRef()); |
| 257 EXPECT_TRUE(++copy); |
| 258 EXPECT_TRUE(*copy == lazy_bitmap[3][2].pixelRef()); |
| 259 EXPECT_FALSE(++copy); |
| 260 } |
| 261 |
| 262 TEST(PictureTest, LazyPixelRefIteratorNonZeroLayer) { |
| 263 gfx::Rect layer_rect(1024, 0, 2048, 2048); |
| 264 |
| 265 SkTileGridPicture::TileGridInfo tile_grid_info; |
| 266 tile_grid_info.fTileInterval = SkISize::Make(512, 512); |
| 267 tile_grid_info.fMargin.setEmpty(); |
| 268 tile_grid_info.fOffset.setZero(); |
| 269 |
| 270 FakeContentLayerClient content_layer_client; |
| 271 |
| 272 // Lazy pixel refs are found in the following grids: |
| 273 // |---|---|---|---| |
| 274 // | | x | | x | |
| 275 // |---|---|---|---| |
| 276 // | x | | x | | |
| 277 // |---|---|---|---| |
| 278 // | | x | | x | |
| 279 // |---|---|---|---| |
| 280 // | x | | x | | |
| 281 // |---|---|---|---| |
| 282 SkBitmap lazy_bitmap[4][4]; |
| 283 for (int y = 0; y < 4; ++y) { |
| 284 for (int x = 0; x < 4; ++x) { |
| 285 if ((x + y) & 1) { |
| 286 CreateBitmap(gfx::Size(500, 500), "lazy", &lazy_bitmap[y][x]); |
| 287 content_layer_client.add_draw_bitmap( |
| 288 lazy_bitmap[y][x], |
| 289 gfx::Point(1024 + x * 512 + 6, y * 512 + 6)); |
| 290 } |
| 291 } |
| 292 } |
| 293 |
| 294 scoped_refptr<Picture> picture = Picture::Create(layer_rect); |
| 295 picture->Record(&content_layer_client, NULL, tile_grid_info); |
| 296 |
| 297 // Default iterator does not have any pixel refs |
| 298 { |
| 299 Picture::LazyPixelRefIterator iterator; |
| 300 EXPECT_FALSE(iterator); |
| 301 // Incrementing an invalid iterator does not do anything. |
| 302 EXPECT_FALSE(++iterator); |
| 303 EXPECT_FALSE(++iterator); |
| 304 EXPECT_FALSE(++iterator); |
| 305 EXPECT_FALSE(++iterator); |
| 306 } |
| 307 for (int y = 0; y < 4; ++y) { |
| 308 for (int x = 0; x < 4; ++x) { |
| 309 Picture::LazyPixelRefIterator iterator( |
| 310 gfx::Rect(1024 + x * 512, y * 512, 500, 500), |
| 311 picture); |
| 312 if ((x + y) & 1) { |
| 313 EXPECT_TRUE(iterator) << x << " " << y; |
| 314 EXPECT_TRUE(*iterator == lazy_bitmap[y][x].pixelRef()); |
| 315 EXPECT_FALSE(++iterator) << x << " " << y; |
| 316 } else { |
| 317 EXPECT_FALSE(iterator) << x << " " << y; |
| 318 } |
| 319 } |
| 320 } |
| 321 // Capture 4 pixel refs. |
| 322 { |
| 323 Picture::LazyPixelRefIterator iterator( |
| 324 gfx::Rect(1024 + 512, 512, 2048, 2048), |
| 325 picture); |
| 326 EXPECT_TRUE(iterator); |
| 327 EXPECT_TRUE(*iterator == lazy_bitmap[1][2].pixelRef()); |
| 328 EXPECT_TRUE(++iterator); |
| 329 EXPECT_TRUE(*iterator == lazy_bitmap[2][1].pixelRef()); |
| 330 EXPECT_TRUE(++iterator); |
| 331 EXPECT_TRUE(*iterator == lazy_bitmap[2][3].pixelRef()); |
| 332 EXPECT_TRUE(++iterator); |
| 333 EXPECT_TRUE(*iterator == lazy_bitmap[3][2].pixelRef()); |
| 334 EXPECT_FALSE(++iterator); |
| 335 } |
| 336 |
| 337 // Copy test. |
| 338 { |
| 339 Picture::LazyPixelRefIterator iterator( |
| 340 gfx::Rect(1024 + 512, 512, 2048, 2048), |
| 341 picture); |
| 342 EXPECT_TRUE(iterator); |
| 343 EXPECT_TRUE(*iterator == lazy_bitmap[1][2].pixelRef()); |
| 344 EXPECT_TRUE(++iterator); |
| 345 EXPECT_TRUE(*iterator == lazy_bitmap[2][1].pixelRef()); |
| 346 |
| 347 // copy now points to the same spot as iterator, |
| 348 // but both can be incremented independently. |
| 349 Picture::LazyPixelRefIterator copy = iterator; |
| 350 EXPECT_TRUE(++iterator); |
| 351 EXPECT_TRUE(*iterator == lazy_bitmap[2][3].pixelRef()); |
| 352 EXPECT_TRUE(++iterator); |
| 353 EXPECT_TRUE(*iterator == lazy_bitmap[3][2].pixelRef()); |
| 354 EXPECT_FALSE(++iterator); |
| 355 |
| 356 EXPECT_TRUE(copy); |
| 357 EXPECT_TRUE(*copy == lazy_bitmap[2][1].pixelRef()); |
| 358 EXPECT_TRUE(++copy); |
| 359 EXPECT_TRUE(*copy == lazy_bitmap[2][3].pixelRef()); |
| 360 EXPECT_TRUE(++copy); |
| 361 EXPECT_TRUE(*copy == lazy_bitmap[3][2].pixelRef()); |
| 362 EXPECT_FALSE(++copy); |
| 363 } |
| 364 |
| 365 // Non intersecting rects |
| 366 { |
| 367 Picture::LazyPixelRefIterator iterator( |
| 368 gfx::Rect(0, 0, 1000, 1000), |
| 369 picture); |
| 370 EXPECT_FALSE(iterator); |
| 371 } |
| 372 { |
| 373 Picture::LazyPixelRefIterator iterator( |
| 374 gfx::Rect(3500, 0, 1000, 1000), |
| 375 picture); |
| 376 EXPECT_FALSE(iterator); |
| 377 } |
| 378 { |
| 379 Picture::LazyPixelRefIterator iterator( |
| 380 gfx::Rect(0, 1100, 1000, 1000), |
| 381 picture); |
| 382 EXPECT_FALSE(iterator); |
| 383 } |
| 384 { |
| 385 Picture::LazyPixelRefIterator iterator( |
| 386 gfx::Rect(3500, 1100, 1000, 1000), |
| 387 picture); |
| 388 EXPECT_FALSE(iterator); |
| 389 } |
| 390 } |
| 391 |
| 392 TEST(PictureTest, LazyPixelRefIteratorOnePixelQuery) { |
| 393 gfx::Rect layer_rect(2048, 2048); |
| 394 |
| 395 SkTileGridPicture::TileGridInfo tile_grid_info; |
| 396 tile_grid_info.fTileInterval = SkISize::Make(512, 512); |
| 397 tile_grid_info.fMargin.setEmpty(); |
| 398 tile_grid_info.fOffset.setZero(); |
| 399 |
| 400 FakeContentLayerClient content_layer_client; |
| 401 |
| 402 // Lazy pixel refs are found in the following grids: |
| 403 // |---|---|---|---| |
| 404 // | | x | | x | |
| 405 // |---|---|---|---| |
| 406 // | x | | x | | |
| 407 // |---|---|---|---| |
| 408 // | | x | | x | |
| 409 // |---|---|---|---| |
| 410 // | x | | x | | |
| 411 // |---|---|---|---| |
| 412 SkBitmap lazy_bitmap[4][4]; |
| 413 for (int y = 0; y < 4; ++y) { |
| 414 for (int x = 0; x < 4; ++x) { |
| 415 if ((x + y) & 1) { |
| 416 CreateBitmap(gfx::Size(500, 500), "lazy", &lazy_bitmap[y][x]); |
| 417 content_layer_client.add_draw_bitmap( |
| 418 lazy_bitmap[y][x], |
| 419 gfx::Point(x * 512 + 6, y * 512 + 6)); |
| 420 } |
| 421 } |
| 422 } |
| 423 |
| 424 scoped_refptr<Picture> picture = Picture::Create(layer_rect); |
| 425 picture->Record(&content_layer_client, NULL, tile_grid_info); |
| 426 |
| 427 for (int y = 0; y < 4; ++y) { |
| 428 for (int x = 0; x < 4; ++x) { |
| 429 Picture::LazyPixelRefIterator iterator( |
| 430 gfx::Rect(x * 512, y * 512 + 256, 1, 1), |
| 431 picture); |
| 432 if ((x + y) & 1) { |
| 433 EXPECT_TRUE(iterator) << x << " " << y; |
| 434 EXPECT_TRUE(*iterator == lazy_bitmap[y][x].pixelRef()); |
| 435 EXPECT_FALSE(++iterator) << x << " " << y; |
| 436 } else { |
| 437 EXPECT_FALSE(iterator) << x << " " << y; |
| 438 } |
| 439 } |
| 440 } |
| 441 } |
123 } // namespace | 442 } // namespace |
124 } // namespace cc | 443 } // namespace cc |
OLD | NEW |