| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "../src/image/SkImagePriv.h" | 8 #include "../src/image/SkImagePriv.h" |
| 9 #include "../src/image/SkSurface_Base.h" | 9 #include "../src/image/SkSurface_Base.h" |
| 10 #include "SkBitmap.h" | 10 #include "SkBitmap.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 | 25 |
| 26 static const int gWidth = 2; | 26 static const int gWidth = 2; |
| 27 static const int gHeight = 2; | 27 static const int gHeight = 2; |
| 28 | 28 |
| 29 static void create(SkBitmap* bm, SkColor color) { | 29 static void create(SkBitmap* bm, SkColor color) { |
| 30 bm->allocN32Pixels(gWidth, gHeight); | 30 bm->allocN32Pixels(gWidth, gHeight); |
| 31 bm->eraseColor(color); | 31 bm->eraseColor(color); |
| 32 } | 32 } |
| 33 | 33 |
| 34 static SkSurface* createSurface(SkColor color) { | 34 static SkSurface* createSurface(SkColor color) { |
| 35 SkSurface* surface = SkSurface::NewRasterPMColor(gWidth, gHeight); | 35 SkSurface* surface = SkSurface::NewRasterN32Premul(gWidth, gHeight); |
| 36 surface->getCanvas()->clear(color); | 36 surface->getCanvas()->clear(color); |
| 37 return surface; | 37 return surface; |
| 38 } | 38 } |
| 39 | 39 |
| 40 static SkPMColor read_pixel(SkSurface* surface, int x, int y) { | 40 static SkPMColor read_pixel(SkSurface* surface, int x, int y) { |
| 41 SkPMColor pixel = 0; | 41 SkPMColor pixel = 0; |
| 42 SkBitmap bitmap; | 42 SkBitmap bitmap; |
| 43 bitmap.installPixels(SkImageInfo::MakeN32Premul(1, 1), &pixel, 4); | 43 bitmap.installPixels(SkImageInfo::MakeN32Premul(1, 1), &pixel, 4); |
| 44 SkCanvas canvas(bitmap); | 44 SkCanvas canvas(bitmap); |
| 45 | 45 |
| (...skipping 398 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 444 int fFlushedDrawCommandsCount; | 444 int fFlushedDrawCommandsCount; |
| 445 int fSkippedPendingDrawCommandsCount; | 445 int fSkippedPendingDrawCommandsCount; |
| 446 | 446 |
| 447 private: | 447 private: |
| 448 typedef SkDeferredCanvas::NotificationClient INHERITED; | 448 typedef SkDeferredCanvas::NotificationClient INHERITED; |
| 449 }; | 449 }; |
| 450 | 450 |
| 451 // Verifies that the deferred canvas triggers a flush when its memory | 451 // Verifies that the deferred canvas triggers a flush when its memory |
| 452 // limit is exceeded | 452 // limit is exceeded |
| 453 static void TestDeferredCanvasMemoryLimit(skiatest::Reporter* reporter) { | 453 static void TestDeferredCanvasMemoryLimit(skiatest::Reporter* reporter) { |
| 454 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(100, 100)); | 454 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(100, 100)); |
| 455 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()
)); | 455 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()
)); |
| 456 | 456 |
| 457 NotificationCounter notificationCounter; | 457 NotificationCounter notificationCounter; |
| 458 canvas->setNotificationClient(¬ificationCounter); | 458 canvas->setNotificationClient(¬ificationCounter); |
| 459 | 459 |
| 460 canvas->setMaxRecordingStorage(160000); | 460 canvas->setMaxRecordingStorage(160000); |
| 461 | 461 |
| 462 SkBitmap sourceImage; | 462 SkBitmap sourceImage; |
| 463 // 100 by 100 image, takes 40,000 bytes in memory | 463 // 100 by 100 image, takes 40,000 bytes in memory |
| 464 sourceImage.allocN32Pixels(100, 100); | 464 sourceImage.allocN32Pixels(100, 100); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 479 NotificationCounter notificationCounter; | 479 NotificationCounter notificationCounter; |
| 480 canvas->setNotificationClient(¬ificationCounter); | 480 canvas->setNotificationClient(¬ificationCounter); |
| 481 | 481 |
| 482 canvas->silentFlush(); // will skip the initial clear that was recorded in c
reateSurface | 482 canvas->silentFlush(); // will skip the initial clear that was recorded in c
reateSurface |
| 483 | 483 |
| 484 REPORTER_ASSERT(reporter, 0 == notificationCounter.fFlushedDrawCommandsCount
); | 484 REPORTER_ASSERT(reporter, 0 == notificationCounter.fFlushedDrawCommandsCount
); |
| 485 REPORTER_ASSERT(reporter, 1 == notificationCounter.fSkippedPendingDrawComman
dsCount); | 485 REPORTER_ASSERT(reporter, 1 == notificationCounter.fSkippedPendingDrawComman
dsCount); |
| 486 } | 486 } |
| 487 | 487 |
| 488 static void TestDeferredCanvasBitmapCaching(skiatest::Reporter* reporter) { | 488 static void TestDeferredCanvasBitmapCaching(skiatest::Reporter* reporter) { |
| 489 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(100, 100)); | 489 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(100, 100)); |
| 490 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()
)); | 490 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()
)); |
| 491 | 491 |
| 492 NotificationCounter notificationCounter; | 492 NotificationCounter notificationCounter; |
| 493 canvas->setNotificationClient(¬ificationCounter); | 493 canvas->setNotificationClient(¬ificationCounter); |
| 494 | 494 |
| 495 const int imageCount = 2; | 495 const int imageCount = 2; |
| 496 SkBitmap sourceImages[imageCount]; | 496 SkBitmap sourceImages[imageCount]; |
| 497 for (int i = 0; i < imageCount; i++) { | 497 for (int i = 0; i < imageCount; i++) { |
| 498 sourceImages[i].allocN32Pixels(100, 100); | 498 sourceImages[i].allocN32Pixels(100, 100); |
| 499 sourceImages[i].eraseColor(SK_ColorGREEN); | 499 sourceImages[i].eraseColor(SK_ColorGREEN); |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 559 canvas->drawBitmap(sourceImages[1], 0, 0, NULL); | 559 canvas->drawBitmap(sourceImages[1], 0, 0, NULL); |
| 560 sourceImages[1].notifyPixelsChanged(); | 560 sourceImages[1].notifyPixelsChanged(); |
| 561 canvas->drawBitmap(sourceImages[1], 0, 0, NULL); | 561 canvas->drawBitmap(sourceImages[1], 0, 0, NULL); |
| 562 REPORTER_ASSERT(reporter, canvas->storageAllocatedForRecording() > 2*bitmapS
ize); | 562 REPORTER_ASSERT(reporter, canvas->storageAllocatedForRecording() > 2*bitmapS
ize); |
| 563 | 563 |
| 564 // Verify that nothing in this test caused commands to be skipped | 564 // Verify that nothing in this test caused commands to be skipped |
| 565 REPORTER_ASSERT(reporter, 0 == notificationCounter.fSkippedPendingDrawComman
dsCount); | 565 REPORTER_ASSERT(reporter, 0 == notificationCounter.fSkippedPendingDrawComman
dsCount); |
| 566 } | 566 } |
| 567 | 567 |
| 568 static void TestDeferredCanvasSkip(skiatest::Reporter* reporter) { | 568 static void TestDeferredCanvasSkip(skiatest::Reporter* reporter) { |
| 569 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(100, 100)); | 569 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(100, 100)); |
| 570 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()
)); | 570 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()
)); |
| 571 | 571 |
| 572 NotificationCounter notificationCounter; | 572 NotificationCounter notificationCounter; |
| 573 canvas->setNotificationClient(¬ificationCounter); | 573 canvas->setNotificationClient(¬ificationCounter); |
| 574 canvas->clear(0x0); | 574 canvas->clear(0x0); |
| 575 REPORTER_ASSERT(reporter, 1 == notificationCounter.fSkippedPendingDrawComman
dsCount); | 575 REPORTER_ASSERT(reporter, 1 == notificationCounter.fSkippedPendingDrawComman
dsCount); |
| 576 REPORTER_ASSERT(reporter, 0 == notificationCounter.fFlushedDrawCommandsCount
); | 576 REPORTER_ASSERT(reporter, 0 == notificationCounter.fFlushedDrawCommandsCount
); |
| 577 canvas->flush(); | 577 canvas->flush(); |
| 578 REPORTER_ASSERT(reporter, 1 == notificationCounter.fSkippedPendingDrawComman
dsCount); | 578 REPORTER_ASSERT(reporter, 1 == notificationCounter.fSkippedPendingDrawComman
dsCount); |
| 579 REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount
); | 579 REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount
); |
| 580 | 580 |
| 581 } | 581 } |
| 582 | 582 |
| 583 static void TestDeferredCanvasBitmapShaderNoLeak(skiatest::Reporter* reporter) { | 583 static void TestDeferredCanvasBitmapShaderNoLeak(skiatest::Reporter* reporter) { |
| 584 // This is a regression test for crbug.com/155875 | 584 // This is a regression test for crbug.com/155875 |
| 585 // This test covers a code path that inserts bitmaps into the bitmap heap th
rough the | 585 // This test covers a code path that inserts bitmaps into the bitmap heap th
rough the |
| 586 // flattening of SkBitmapProcShaders. The refcount in the bitmap heap is mai
ntained through | 586 // flattening of SkBitmapProcShaders. The refcount in the bitmap heap is mai
ntained through |
| 587 // the flattening and unflattening of the shader. | 587 // the flattening and unflattening of the shader. |
| 588 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(100, 100)); | 588 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(100, 100)); |
| 589 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()
)); | 589 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()
)); |
| 590 // test will fail if nbIterations is not in sync with | 590 // test will fail if nbIterations is not in sync with |
| 591 // BITMAPS_TO_KEEP in SkGPipeWrite.cpp | 591 // BITMAPS_TO_KEEP in SkGPipeWrite.cpp |
| 592 const int nbIterations = 5; | 592 const int nbIterations = 5; |
| 593 size_t bytesAllocated = 0; | 593 size_t bytesAllocated = 0; |
| 594 for(int pass = 0; pass < 2; ++pass) { | 594 for(int pass = 0; pass < 2; ++pass) { |
| 595 for(int i = 0; i < nbIterations; ++i) { | 595 for(int i = 0; i < nbIterations; ++i) { |
| 596 SkPaint paint; | 596 SkPaint paint; |
| 597 SkBitmap paintPattern; | 597 SkBitmap paintPattern; |
| 598 paintPattern.allocN32Pixels(10, 10); | 598 paintPattern.allocN32Pixels(10, 10); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 613 REPORTER_ASSERT(reporter, newBytesAllocated == bytesAllocated); | 613 REPORTER_ASSERT(reporter, newBytesAllocated == bytesAllocated); |
| 614 } | 614 } |
| 615 } | 615 } |
| 616 } | 616 } |
| 617 // All cached resources should be evictable since last canvas call was flush
() | 617 // All cached resources should be evictable since last canvas call was flush
() |
| 618 canvas->freeMemoryIfPossible(~0U); | 618 canvas->freeMemoryIfPossible(~0U); |
| 619 REPORTER_ASSERT(reporter, 0 == canvas->storageAllocatedForRecording()); | 619 REPORTER_ASSERT(reporter, 0 == canvas->storageAllocatedForRecording()); |
| 620 } | 620 } |
| 621 | 621 |
| 622 static void TestDeferredCanvasBitmapSizeThreshold(skiatest::Reporter* reporter)
{ | 622 static void TestDeferredCanvasBitmapSizeThreshold(skiatest::Reporter* reporter)
{ |
| 623 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(100, 100)); | 623 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(100, 100)); |
| 624 | 624 |
| 625 SkBitmap sourceImage; | 625 SkBitmap sourceImage; |
| 626 // 100 by 100 image, takes 40,000 bytes in memory | 626 // 100 by 100 image, takes 40,000 bytes in memory |
| 627 sourceImage.allocN32Pixels(100, 100); | 627 sourceImage.allocN32Pixels(100, 100); |
| 628 sourceImage.eraseColor(SK_ColorGREEN); | 628 sourceImage.eraseColor(SK_ColorGREEN); |
| 629 | 629 |
| 630 // 1 under : should not store the image | 630 // 1 under : should not store the image |
| 631 { | 631 { |
| 632 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.g
et())); | 632 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.g
et())); |
| 633 canvas->setBitmapSizeThreshold(39999); | 633 canvas->setBitmapSizeThreshold(39999); |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 804 REPORTER_ASSERT(reporter, get_surface_ptr(alternateSurface, useGpu) == p
ixels2); | 804 REPORTER_ASSERT(reporter, get_surface_ptr(alternateSurface, useGpu) == p
ixels2); |
| 805 // Verify that a flushed draw command will trigger a copy on write on al
ternateSurface. | 805 // Verify that a flushed draw command will trigger a copy on write on al
ternateSurface. |
| 806 canvas->clear(SK_ColorWHITE); | 806 canvas->clear(SK_ColorWHITE); |
| 807 canvas->flush(); | 807 canvas->flush(); |
| 808 REPORTER_ASSERT(reporter, get_surface_ptr(surface, useGpu) == pixels1); | 808 REPORTER_ASSERT(reporter, get_surface_ptr(surface, useGpu) == pixels1); |
| 809 REPORTER_ASSERT(reporter, get_surface_ptr(alternateSurface, useGpu) != p
ixels2); | 809 REPORTER_ASSERT(reporter, get_surface_ptr(alternateSurface, useGpu) != p
ixels2); |
| 810 } | 810 } |
| 811 } | 811 } |
| 812 | 812 |
| 813 static void TestDeferredCanvasCreateCompatibleDevice(skiatest::Reporter* reporte
r) { | 813 static void TestDeferredCanvasCreateCompatibleDevice(skiatest::Reporter* reporte
r) { |
| 814 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(100, 100)); | 814 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(100, 100)); |
| 815 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()
)); | 815 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()
)); |
| 816 | 816 |
| 817 NotificationCounter notificationCounter; | 817 NotificationCounter notificationCounter; |
| 818 canvas->setNotificationClient(¬ificationCounter); | 818 canvas->setNotificationClient(¬ificationCounter); |
| 819 | 819 |
| 820 SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10); | 820 SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10); |
| 821 SkAutoTUnref<SkSurface> secondarySurface(canvas->newSurface(info)); | 821 SkAutoTUnref<SkSurface> secondarySurface(canvas->newSurface(info)); |
| 822 | 822 |
| 823 SkRect rect = SkRect::MakeWH(5, 5); | 823 SkRect rect = SkRect::MakeWH(5, 5); |
| 824 SkPaint paint; | 824 SkPaint paint; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 835 SkRect rect; | 835 SkRect rect; |
| 836 rect.setXYWH(SkIntToScalar(0), SkIntToScalar(0), SkIntToScalar(gWidth), SkIn
tToScalar(gHeight)); | 836 rect.setXYWH(SkIntToScalar(0), SkIntToScalar(0), SkIntToScalar(gWidth), SkIn
tToScalar(gHeight)); |
| 837 SkRect clip; | 837 SkRect clip; |
| 838 clip.setXYWH(SkIntToScalar(0), SkIntToScalar(0), SkIntToScalar(1), SkIntToSc
alar(1)); | 838 clip.setXYWH(SkIntToScalar(0), SkIntToScalar(0), SkIntToScalar(1), SkIntToSc
alar(1)); |
| 839 | 839 |
| 840 SkPaint paint; | 840 SkPaint paint; |
| 841 SkISize size = SkISize::Make(gWidth, gHeight); | 841 SkISize size = SkISize::Make(gWidth, gHeight); |
| 842 | 842 |
| 843 SkAutoTUnref<SkSurface> surface(createSurface(0xFFFFFFFF)); | 843 SkAutoTUnref<SkSurface> surface(createSurface(0xFFFFFFFF)); |
| 844 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()
)); | 844 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()
)); |
| 845 SkSurface* newSurface = SkSurface::NewRasterPMColor(4, 4); | 845 SkSurface* newSurface = SkSurface::NewRasterN32Premul(4, 4); |
| 846 SkAutoTUnref<SkSurface> aur(newSurface); | 846 SkAutoTUnref<SkSurface> aur(newSurface); |
| 847 | 847 |
| 848 for (int i = 0; i < 2; ++i) { | 848 for (int i = 0; i < 2; ++i) { |
| 849 if (i == 1) { | 849 if (i == 1) { |
| 850 canvas->setSurface(newSurface); | 850 canvas->setSurface(newSurface); |
| 851 size = SkISize::Make(4, 4); | 851 size = SkISize::Make(4, 4); |
| 852 } | 852 } |
| 853 | 853 |
| 854 // verify that canvas size is correctly initialized or set | 854 // verify that canvas size is correctly initialized or set |
| 855 REPORTER_ASSERT(reporter, size == canvas->getCanvasSize()); | 855 REPORTER_ASSERT(reporter, size == canvas->getCanvasSize()); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 899 TestDeferredCanvasSurface(reporter, NULL); | 899 TestDeferredCanvasSurface(reporter, NULL); |
| 900 TestDeferredCanvasSetSurface(reporter, NULL); | 900 TestDeferredCanvasSetSurface(reporter, NULL); |
| 901 } | 901 } |
| 902 | 902 |
| 903 DEF_GPUTEST(DeferredCanvas_GPU, reporter, factory) { | 903 DEF_GPUTEST(DeferredCanvas_GPU, reporter, factory) { |
| 904 if (factory != NULL) { | 904 if (factory != NULL) { |
| 905 TestDeferredCanvasSurface(reporter, factory); | 905 TestDeferredCanvasSurface(reporter, factory); |
| 906 TestDeferredCanvasSetSurface(reporter, factory); | 906 TestDeferredCanvasSetSurface(reporter, factory); |
| 907 } | 907 } |
| 908 } | 908 } |
| OLD | NEW |