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 "base/compiler_specific.h" | 5 #include "base/compiler_specific.h" |
6 #include "skia/ext/analysis_canvas.h" | 6 #include "skia/ext/analysis_canvas.h" |
7 | |
8 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 #include "third_party/skia/include/core/SkShader.h" |
9 | 9 |
10 namespace { | 10 namespace { |
11 | 11 |
12 void solidColorFill(skia::AnalysisCanvas& canvas) { | 12 void solidColorFill(skia::AnalysisCanvas& canvas) { |
13 canvas.clear(SkColorSetARGB(255, 255, 255, 255)); | 13 canvas.clear(SkColorSetARGB(255, 255, 255, 255)); |
14 } | 14 } |
15 | 15 |
16 void transparentFill(skia::AnalysisCanvas& canvas) { | 16 void transparentFill(skia::AnalysisCanvas& canvas) { |
17 canvas.clear(SkColorSetARGB(0, 0, 0, 0)); | 17 canvas.clear(SkColorSetARGB(0, 0, 0, 0)); |
18 } | 18 } |
19 | 19 |
20 } // namespace | 20 } // namespace |
21 namespace skia { | 21 namespace skia { |
22 | 22 |
| 23 class TestPixelRef : public SkPixelRef { |
| 24 public: |
| 25 // Pure virtual implementation. |
| 26 SkFlattenable::Factory getFactory() { return NULL; } |
| 27 void* onLockPixels(SkColorTable**) { return NULL; } |
| 28 void onUnlockPixels() {} |
| 29 }; |
| 30 |
| 31 class TestLazyPixelRef : public LazyPixelRef { |
| 32 public: |
| 33 // Pure virtual implementation. |
| 34 SkFlattenable::Factory getFactory() { return NULL; } |
| 35 void* onLockPixels(SkColorTable**) { return NULL; } |
| 36 void onUnlockPixels() {} |
| 37 bool PrepareToDecode(const PrepareParams& params) { return true; } |
| 38 void Decode() {} |
| 39 }; |
| 40 |
| 41 class TestShader : public SkShader { |
| 42 public: |
| 43 TestShader(SkBitmap* bitmap) |
| 44 : bitmap_(bitmap) { |
| 45 } |
| 46 |
| 47 SkShader::BitmapType asABitmap(SkBitmap* bitmap, |
| 48 SkMatrix*, TileMode xy[2]) const { |
| 49 *bitmap = *bitmap_; |
| 50 return SkShader::kDefault_BitmapType; |
| 51 } |
| 52 |
| 53 // Pure virtual implementation. |
| 54 void shadeSpan(int x, int y, SkPMColor[], int count) {} |
| 55 SkFlattenable::Factory getFactory() { return NULL; } |
| 56 |
| 57 private: |
| 58 |
| 59 SkBitmap* bitmap_; |
| 60 }; |
| 61 |
23 TEST(AnalysisCanvasTest, EmptyCanvas) { | 62 TEST(AnalysisCanvasTest, EmptyCanvas) { |
24 SkBitmap emptyBitmap; | 63 SkBitmap emptyBitmap; |
25 emptyBitmap.setConfig(SkBitmap::kNo_Config, 255, 255); | 64 emptyBitmap.setConfig(SkBitmap::kNo_Config, 255, 255); |
26 skia::AnalysisDevice device(emptyBitmap); | 65 skia::AnalysisDevice device(emptyBitmap); |
27 skia::AnalysisCanvas canvas(&device); | 66 skia::AnalysisCanvas canvas(&device); |
28 | 67 |
29 SkColor color; | 68 SkColor color; |
30 EXPECT_FALSE(canvas.getColorIfSolid(&color)); | 69 EXPECT_FALSE(canvas.getColorIfSolid(&color)); |
31 EXPECT_FALSE(canvas.isTransparent()); | 70 EXPECT_FALSE(canvas.isTransparent()); |
32 EXPECT_TRUE(canvas.isCheap()); | 71 EXPECT_TRUE(canvas.isCheap()); |
(...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
303 | 342 |
304 transparentFill(canvas); | 343 transparentFill(canvas); |
305 EXPECT_FALSE(canvas.getColorIfSolid(&outputColor)); | 344 EXPECT_FALSE(canvas.getColorIfSolid(&outputColor)); |
306 EXPECT_TRUE(canvas.isTransparent()); | 345 EXPECT_TRUE(canvas.isTransparent()); |
307 | 346 |
308 solidColorFill(canvas); | 347 solidColorFill(canvas); |
309 EXPECT_TRUE(canvas.getColorIfSolid(&outputColor)); | 348 EXPECT_TRUE(canvas.getColorIfSolid(&outputColor)); |
310 EXPECT_FALSE(canvas.isTransparent()); | 349 EXPECT_FALSE(canvas.isTransparent()); |
311 } | 350 } |
312 | 351 |
| 352 TEST(AnalysisCanvasTest, LazyPixelRefs) { |
| 353 // Set up two lazy and two non-lazy pixel refs and the corresponding bitmaps. |
| 354 TestLazyPixelRef firstLazyPixelRef; |
| 355 firstLazyPixelRef.setURI("lazy"); |
| 356 TestLazyPixelRef secondLazyPixelRef; |
| 357 secondLazyPixelRef.setURI("lazy"); |
| 358 |
| 359 TestPixelRef firstNonLazyPixelRef; |
| 360 TestPixelRef secondNonLazyPixelRef; |
| 361 secondNonLazyPixelRef.setURI("notsolazy"); |
| 362 |
| 363 SkBitmap firstLazyBitmap; |
| 364 firstLazyBitmap.setConfig(SkBitmap::kNo_Config, 255, 255); |
| 365 firstLazyBitmap.setPixelRef(&firstLazyPixelRef); |
| 366 SkBitmap secondLazyBitmap; |
| 367 secondLazyBitmap.setConfig(SkBitmap::kNo_Config, 255, 255); |
| 368 secondLazyBitmap.setPixelRef(&secondLazyPixelRef); |
| 369 |
| 370 SkBitmap firstNonLazyBitmap; |
| 371 firstNonLazyBitmap.setConfig(SkBitmap::kNo_Config, 255, 255); |
| 372 SkBitmap secondNonLazyBitmap; |
| 373 secondNonLazyBitmap.setConfig(SkBitmap::kNo_Config, 255, 255); |
| 374 secondNonLazyBitmap.setPixelRef(&secondNonLazyPixelRef); |
| 375 |
| 376 // The testcase starts here. |
| 377 SkBitmap emptyBitmap; |
| 378 emptyBitmap.setConfig(SkBitmap::kNo_Config, 255, 255); |
| 379 skia::AnalysisDevice device(emptyBitmap); |
| 380 skia::AnalysisCanvas canvas(&device); |
| 381 |
| 382 // This should be the first ref. |
| 383 canvas.drawBitmap(firstLazyBitmap, 0, 0); |
| 384 // The following will be ignored (non-lazy). |
| 385 canvas.drawBitmap(firstNonLazyBitmap, 0, 0); |
| 386 canvas.drawBitmap(firstNonLazyBitmap, 0, 0); |
| 387 canvas.drawBitmap(secondNonLazyBitmap, 0, 0); |
| 388 canvas.drawBitmap(secondNonLazyBitmap, 0, 0); |
| 389 // This one will be ignored (already exists). |
| 390 canvas.drawBitmap(firstLazyBitmap, 0, 0); |
| 391 // This should be the second ref. |
| 392 canvas.drawBitmap(secondLazyBitmap, 0, 0); |
| 393 |
| 394 std::list<skia::LazyPixelRef*> pixelRefs; |
| 395 canvas.consumeLazyPixelRefs(&pixelRefs); |
| 396 |
| 397 // We expect to get only lazy pixel refs and only unique results. |
| 398 EXPECT_EQ(pixelRefs.size(), 2u); |
| 399 if (!pixelRefs.empty()) { |
| 400 EXPECT_EQ(pixelRefs.front(), |
| 401 static_cast<LazyPixelRef*>(&firstLazyPixelRef)); |
| 402 EXPECT_EQ(pixelRefs.back(), |
| 403 static_cast<LazyPixelRef*>(&secondLazyPixelRef)); |
| 404 } |
| 405 } |
| 406 |
| 407 TEST(AnalysisCanvasTest, PixelRefsFromPaint) { |
| 408 TestLazyPixelRef lazyPixelRef; |
| 409 lazyPixelRef.setURI("lazy"); |
| 410 |
| 411 TestPixelRef nonLazyPixelRef; |
| 412 nonLazyPixelRef.setURI("notsolazy"); |
| 413 |
| 414 SkBitmap lazyBitmap; |
| 415 lazyBitmap.setConfig(SkBitmap::kNo_Config, 255, 255); |
| 416 lazyBitmap.setPixelRef(&lazyPixelRef); |
| 417 |
| 418 SkBitmap nonLazyBitmap; |
| 419 nonLazyBitmap.setConfig(SkBitmap::kNo_Config, 255, 255); |
| 420 nonLazyBitmap.setPixelRef(&nonLazyPixelRef); |
| 421 |
| 422 TestShader lazyShader(&lazyBitmap); |
| 423 TestShader nonLazyShader(&nonLazyBitmap); |
| 424 |
| 425 SkPaint lazyPaint; |
| 426 lazyPaint.setShader(&lazyShader); |
| 427 SkPaint nonLazyPaint; |
| 428 nonLazyPaint.setShader(&nonLazyShader); |
| 429 |
| 430 SkBitmap emptyBitmap; |
| 431 emptyBitmap.setConfig(SkBitmap::kNo_Config, 255, 255); |
| 432 skia::AnalysisDevice device(emptyBitmap); |
| 433 skia::AnalysisCanvas canvas(&device); |
| 434 |
| 435 canvas.drawRect(SkRect::MakeWH(255, 255), lazyPaint); |
| 436 canvas.drawRect(SkRect::MakeWH(255, 255), lazyPaint); |
| 437 canvas.drawRect(SkRect::MakeWH(255, 255), lazyPaint); |
| 438 canvas.drawRect(SkRect::MakeWH(255, 255), nonLazyPaint); |
| 439 canvas.drawRect(SkRect::MakeWH(255, 255), nonLazyPaint); |
| 440 canvas.drawRect(SkRect::MakeWH(255, 255), nonLazyPaint); |
| 441 |
| 442 std::list<skia::LazyPixelRef*> pixelRefs; |
| 443 canvas.consumeLazyPixelRefs(&pixelRefs); |
| 444 |
| 445 // We expect to get only lazy pixel refs and only unique results. |
| 446 EXPECT_EQ(pixelRefs.size(), 1u); |
| 447 if (!pixelRefs.empty()) { |
| 448 EXPECT_EQ(pixelRefs.front(), static_cast<LazyPixelRef*>(&lazyPixelRef)); |
| 449 } |
| 450 } |
| 451 |
313 } // namespace skia | 452 } // namespace skia |
OLD | NEW |