Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(252)

Side by Side Diff: tests/DeferredCanvasTest.cpp

Issue 164203003: replace setConfig+allocPixels with alloc-or-install-pixels (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « tests/ClipperTest.cpp ('k') | tests/DeviceLooperTest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
11 #include "SkBitmapDevice.h" 11 #include "SkBitmapDevice.h"
12 #include "SkBitmapProcShader.h" 12 #include "SkBitmapProcShader.h"
13 #include "SkDeferredCanvas.h" 13 #include "SkDeferredCanvas.h"
14 #include "SkGradientShader.h" 14 #include "SkGradientShader.h"
15 #include "SkShader.h" 15 #include "SkShader.h"
16 #include "SkSurface.h" 16 #include "SkSurface.h"
17 #include "Test.h" 17 #include "Test.h"
18 #if SK_SUPPORT_GPU 18 #if SK_SUPPORT_GPU
19 #include "GrContextFactory.h" 19 #include "GrContextFactory.h"
20 #else 20 #else
21 class GrContextFactory; 21 class GrContextFactory;
22 #endif 22 #endif
23 23
24 static const int gWidth = 2; 24 static const int gWidth = 2;
25 static const int gHeight = 2; 25 static const int gHeight = 2;
26 26
27 static void create(SkBitmap* bm, SkBitmap::Config config, SkColor color) { 27 static void create(SkBitmap* bm, SkColor color) {
28 bm->setConfig(config, gWidth, gHeight); 28 bm->allocN32Pixels(gWidth, gHeight);
29 bm->allocPixels();
30 bm->eraseColor(color); 29 bm->eraseColor(color);
31 } 30 }
32 31
33 static SkSurface* createSurface(SkColor color) { 32 static SkSurface* createSurface(SkColor color) {
34 SkSurface* surface = SkSurface::NewRasterPMColor(gWidth, gHeight); 33 SkSurface* surface = SkSurface::NewRasterPMColor(gWidth, gHeight);
35 surface->getCanvas()->clear(color); 34 surface->getCanvas()->clear(color);
36 return surface; 35 return surface;
37 } 36 }
38 37
39 static SkPMColor read_pixel(SkSurface* surface, int x, int y) { 38 static SkPMColor read_pixel(SkSurface* surface, int x, int y) {
(...skipping 22 matching lines...) Expand all
62 SkBitmap accessed = canvas->getDevice()->accessBitmap(false); 61 SkBitmap accessed = canvas->getDevice()->accessBitmap(false);
63 62
64 // verify that clear was executed 63 // verify that clear was executed
65 REPORTER_ASSERT(reporter, 0 == read_pixel(surface, 0, 0)); 64 REPORTER_ASSERT(reporter, 0 == read_pixel(surface, 0, 0));
66 } 65 }
67 66
68 class MockSurface : public SkSurface_Base { 67 class MockSurface : public SkSurface_Base {
69 public: 68 public:
70 MockSurface(int width, int height) : SkSurface_Base(width, height) { 69 MockSurface(int width, int height) : SkSurface_Base(width, height) {
71 clearCounts(); 70 clearCounts();
72 fBitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height); 71 fBitmap.allocN32Pixels(width, height);
73 fBitmap.allocPixels();
74 } 72 }
75 73
76 virtual SkCanvas* onNewCanvas() SK_OVERRIDE { 74 virtual SkCanvas* onNewCanvas() SK_OVERRIDE {
77 return SkNEW_ARGS(SkCanvas, (fBitmap)); 75 return SkNEW_ARGS(SkCanvas, (fBitmap));
78 } 76 }
79 77
80 virtual SkSurface* onNewSurface(const SkImageInfo&) SK_OVERRIDE { 78 virtual SkSurface* onNewSurface(const SkImageInfo&) SK_OVERRIDE {
81 return NULL; 79 return NULL;
82 } 80 }
83 81
(...skipping 16 matching lines...) Expand all
100 98
101 int fDiscardCount, fRetainCount; 99 int fDiscardCount, fRetainCount;
102 SkBitmap fBitmap; 100 SkBitmap fBitmap;
103 }; 101 };
104 102
105 static void TestDeferredCanvasWritePixelsToSurface(skiatest::Reporter* reporter) { 103 static void TestDeferredCanvasWritePixelsToSurface(skiatest::Reporter* reporter) {
106 SkAutoTUnref<MockSurface> surface(SkNEW_ARGS(MockSurface, (10, 10))); 104 SkAutoTUnref<MockSurface> surface(SkNEW_ARGS(MockSurface, (10, 10)));
107 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get() )); 105 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get() ));
108 106
109 SkBitmap srcBitmap; 107 SkBitmap srcBitmap;
110 srcBitmap.setConfig(SkBitmap::kARGB_8888_Config, 10, 10); 108 srcBitmap.allocN32Pixels(10, 10);
111 srcBitmap.allocPixels();
112 srcBitmap.eraseColor(SK_ColorGREEN); 109 srcBitmap.eraseColor(SK_ColorGREEN);
113 // Tests below depend on this bitmap being recognized as opaque 110 // Tests below depend on this bitmap being recognized as opaque
114 111
115 // Preliminary sanity check: no copy on write if no active snapshot 112 // Preliminary sanity check: no copy on write if no active snapshot
116 surface->clearCounts(); 113 surface->clearCounts();
117 canvas->clear(SK_ColorWHITE); 114 canvas->clear(SK_ColorWHITE);
118 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); 115 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
119 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); 116 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
120 117
121 surface->clearCounts(); 118 surface->clearCounts();
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after
344 paint.setStyle(SkPaint::kFill_Style); 341 paint.setStyle(SkPaint::kFill_Style);
345 paint.setAlpha(255); 342 paint.setAlpha(255);
346 paint.setXfermodeMode(SkXfermode::kSrcIn_Mode); 343 paint.setXfermodeMode(SkXfermode::kSrcIn_Mode);
347 canvas->drawRect(fullRect, paint); 344 canvas->drawRect(fullRect, paint);
348 REPORTER_ASSERT(reporter, !canvas->isFreshFrame()); 345 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
349 } 346 }
350 { 347 {
351 SkPaint paint; 348 SkPaint paint;
352 paint.setStyle(SkPaint::kFill_Style); 349 paint.setStyle(SkPaint::kFill_Style);
353 SkBitmap bmp; 350 SkBitmap bmp;
354 create(&bmp, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF); 351 create(&bmp, 0xFFFFFFFF);
355 bmp.setAlphaType(kOpaque_SkAlphaType); 352 bmp.setAlphaType(kOpaque_SkAlphaType);
356 SkShader* shader = SkShader::CreateBitmapShader(bmp, 353 SkShader* shader = SkShader::CreateBitmapShader(bmp,
357 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode); 354 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
358 paint.setShader(shader)->unref(); 355 paint.setShader(shader)->unref();
359 canvas->drawRect(fullRect, paint); 356 canvas->drawRect(fullRect, paint);
360 REPORTER_ASSERT(reporter, canvas->isFreshFrame()); 357 REPORTER_ASSERT(reporter, canvas->isFreshFrame());
361 } 358 }
362 359
363 // Verify that full frame rects with different forms of non-opaque paint 360 // Verify that full frame rects with different forms of non-opaque paint
364 // do not trigger frames to be marked as fresh 361 // do not trigger frames to be marked as fresh
(...skipping 17 matching lines...) Expand all
382 SkShader* shader = SkGradientShader::CreateTwoPointConical( 379 SkShader* shader = SkGradientShader::CreateTwoPointConical(
383 pt1, r1, pt2, r2, colors, pos, 2, SkShader::kClamp_TileMode, NULL); 380 pt1, r1, pt2, r2, colors, pos, 2, SkShader::kClamp_TileMode, NULL);
384 paint.setShader(shader)->unref(); 381 paint.setShader(shader)->unref();
385 canvas->drawRect(fullRect, paint); 382 canvas->drawRect(fullRect, paint);
386 REPORTER_ASSERT(reporter, !canvas->isFreshFrame()); 383 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
387 } 384 }
388 { 385 {
389 SkPaint paint; 386 SkPaint paint;
390 paint.setStyle(SkPaint::kFill_Style); 387 paint.setStyle(SkPaint::kFill_Style);
391 SkBitmap bmp; 388 SkBitmap bmp;
392 create(&bmp, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF); 389 create(&bmp, 0xFFFFFFFF);
393 bmp.setAlphaType(kPremul_SkAlphaType); 390 bmp.setAlphaType(kPremul_SkAlphaType);
394 SkShader* shader = SkShader::CreateBitmapShader(bmp, 391 SkShader* shader = SkShader::CreateBitmapShader(bmp,
395 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode); 392 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
396 paint.setShader(shader)->unref(); 393 paint.setShader(shader)->unref();
397 canvas->drawRect(fullRect, paint); 394 canvas->drawRect(fullRect, paint);
398 REPORTER_ASSERT(reporter, !canvas->isFreshFrame()); 395 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
399 } 396 }
400 397
401 // Verify that incomplete coverage does not trigger a fresh frame 398 // Verify that incomplete coverage does not trigger a fresh frame
402 { 399 {
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
500 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(100, 100)); 497 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(100, 100));
501 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get() )); 498 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get() ));
502 499
503 NotificationCounter notificationCounter; 500 NotificationCounter notificationCounter;
504 canvas->setNotificationClient(&notificationCounter); 501 canvas->setNotificationClient(&notificationCounter);
505 502
506 canvas->setMaxRecordingStorage(160000); 503 canvas->setMaxRecordingStorage(160000);
507 504
508 SkBitmap sourceImage; 505 SkBitmap sourceImage;
509 // 100 by 100 image, takes 40,000 bytes in memory 506 // 100 by 100 image, takes 40,000 bytes in memory
510 sourceImage.setConfig(SkBitmap::kARGB_8888_Config, 100, 100); 507 sourceImage.allocN32Pixels(100, 100);
511 sourceImage.allocPixels();
512 508
513 for (int i = 0; i < 5; i++) { 509 for (int i = 0; i < 5; i++) {
514 sourceImage.notifyPixelsChanged(); // to force re-serialization 510 sourceImage.notifyPixelsChanged(); // to force re-serialization
515 canvas->drawBitmap(sourceImage, 0, 0, NULL); 511 canvas->drawBitmap(sourceImage, 0, 0, NULL);
516 } 512 }
517 513
518 REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount ); 514 REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount );
519 } 515 }
520 516
521 static void TestDeferredCanvasBitmapCaching(skiatest::Reporter* reporter) { 517 static void TestDeferredCanvasBitmapCaching(skiatest::Reporter* reporter) {
522 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(100, 100)); 518 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(100, 100));
523 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get() )); 519 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get() ));
524 520
525 NotificationCounter notificationCounter; 521 NotificationCounter notificationCounter;
526 canvas->setNotificationClient(&notificationCounter); 522 canvas->setNotificationClient(&notificationCounter);
527 523
528 const int imageCount = 2; 524 const int imageCount = 2;
529 SkBitmap sourceImages[imageCount]; 525 SkBitmap sourceImages[imageCount];
530 for (int i = 0; i < imageCount; i++) 526 for (int i = 0; i < imageCount; i++) {
531 { 527 sourceImages[i].allocN32Pixels(100, 100);
532 sourceImages[i].setConfig(SkBitmap::kARGB_8888_Config, 100, 100);
533 sourceImages[i].allocPixels();
534 } 528 }
535 529
536 size_t bitmapSize = sourceImages[0].getSize(); 530 size_t bitmapSize = sourceImages[0].getSize();
537 531
538 canvas->drawBitmap(sourceImages[0], 0, 0, NULL); 532 canvas->drawBitmap(sourceImages[0], 0, 0, NULL);
539 REPORTER_ASSERT(reporter, 1 == notificationCounter.fStorageAllocatedChangedC ount); 533 REPORTER_ASSERT(reporter, 1 == notificationCounter.fStorageAllocatedChangedC ount);
540 // stored bitmap + drawBitmap command 534 // stored bitmap + drawBitmap command
541 REPORTER_ASSERT(reporter, canvas->storageAllocatedForRecording() > bitmapSiz e); 535 REPORTER_ASSERT(reporter, canvas->storageAllocatedForRecording() > bitmapSiz e);
542 536
543 // verify that nothing can be freed at this point 537 // verify that nothing can be freed at this point
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
622 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(100, 100)); 616 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(100, 100));
623 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get() )); 617 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get() ));
624 // test will fail if nbIterations is not in sync with 618 // test will fail if nbIterations is not in sync with
625 // BITMAPS_TO_KEEP in SkGPipeWrite.cpp 619 // BITMAPS_TO_KEEP in SkGPipeWrite.cpp
626 const int nbIterations = 5; 620 const int nbIterations = 5;
627 size_t bytesAllocated = 0; 621 size_t bytesAllocated = 0;
628 for(int pass = 0; pass < 2; ++pass) { 622 for(int pass = 0; pass < 2; ++pass) {
629 for(int i = 0; i < nbIterations; ++i) { 623 for(int i = 0; i < nbIterations; ++i) {
630 SkPaint paint; 624 SkPaint paint;
631 SkBitmap paintPattern; 625 SkBitmap paintPattern;
632 paintPattern.setConfig(SkBitmap::kARGB_8888_Config, 10, 10); 626 paintPattern.allocN32Pixels(10, 10);
633 paintPattern.allocPixels();
634 paint.setShader(SkNEW_ARGS(SkBitmapProcShader, 627 paint.setShader(SkNEW_ARGS(SkBitmapProcShader,
635 (paintPattern, SkShader::kClamp_TileMode, SkShader::kClamp_TileM ode)))->unref(); 628 (paintPattern, SkShader::kClamp_TileMode, SkShader::kClamp_TileM ode)))->unref();
636 canvas->drawPaint(paint); 629 canvas->drawPaint(paint);
637 canvas->flush(); 630 canvas->flush();
638 631
639 // In the first pass, memory allocation should be monotonically incr easing as 632 // In the first pass, memory allocation should be monotonically incr easing as
640 // the bitmap heap slots fill up. In the second pass memory allocat ion should be 633 // the bitmap heap slots fill up. In the second pass memory allocat ion should be
641 // stable as bitmap heap slots get recycled. 634 // stable as bitmap heap slots get recycled.
642 size_t newBytesAllocated = canvas->storageAllocatedForRecording(); 635 size_t newBytesAllocated = canvas->storageAllocatedForRecording();
643 if (pass == 0) { 636 if (pass == 0) {
644 REPORTER_ASSERT(reporter, newBytesAllocated > bytesAllocated); 637 REPORTER_ASSERT(reporter, newBytesAllocated > bytesAllocated);
645 bytesAllocated = newBytesAllocated; 638 bytesAllocated = newBytesAllocated;
646 } else { 639 } else {
647 REPORTER_ASSERT(reporter, newBytesAllocated == bytesAllocated); 640 REPORTER_ASSERT(reporter, newBytesAllocated == bytesAllocated);
648 } 641 }
649 } 642 }
650 } 643 }
651 // All cached resources should be evictable since last canvas call was flush () 644 // All cached resources should be evictable since last canvas call was flush ()
652 canvas->freeMemoryIfPossible(~0U); 645 canvas->freeMemoryIfPossible(~0U);
653 REPORTER_ASSERT(reporter, 0 == canvas->storageAllocatedForRecording()); 646 REPORTER_ASSERT(reporter, 0 == canvas->storageAllocatedForRecording());
654 } 647 }
655 648
656 static void TestDeferredCanvasBitmapSizeThreshold(skiatest::Reporter* reporter) { 649 static void TestDeferredCanvasBitmapSizeThreshold(skiatest::Reporter* reporter) {
657 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(100, 100)); 650 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(100, 100));
658 651
659 SkBitmap sourceImage; 652 SkBitmap sourceImage;
660 // 100 by 100 image, takes 40,000 bytes in memory 653 // 100 by 100 image, takes 40,000 bytes in memory
661 sourceImage.setConfig(SkBitmap::kARGB_8888_Config, 100, 100); 654 sourceImage.allocN32Pixels(100, 100);
662 sourceImage.allocPixels();
663 655
664 // 1 under : should not store the image 656 // 1 under : should not store the image
665 { 657 {
666 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.g et())); 658 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.g et()));
667 canvas->setBitmapSizeThreshold(39999); 659 canvas->setBitmapSizeThreshold(39999);
668 canvas->drawBitmap(sourceImage, 0, 0, NULL); 660 canvas->drawBitmap(sourceImage, 0, 0, NULL);
669 size_t newBytesAllocated = canvas->storageAllocatedForRecording(); 661 size_t newBytesAllocated = canvas->storageAllocatedForRecording();
670 REPORTER_ASSERT(reporter, newBytesAllocated == 0); 662 REPORTER_ASSERT(reporter, newBytesAllocated == 0);
671 } 663 }
672 664
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after
850 TestDeferredCanvasBitmapSizeThreshold(reporter); 842 TestDeferredCanvasBitmapSizeThreshold(reporter);
851 TestDeferredCanvasCreateCompatibleDevice(reporter); 843 TestDeferredCanvasCreateCompatibleDevice(reporter);
852 TestDeferredCanvasWritePixelsToSurface(reporter); 844 TestDeferredCanvasWritePixelsToSurface(reporter);
853 TestDeferredCanvasSurface(reporter, NULL); 845 TestDeferredCanvasSurface(reporter, NULL);
854 TestDeferredCanvasSetSurface(reporter, NULL); 846 TestDeferredCanvasSetSurface(reporter, NULL);
855 if (NULL != factory) { 847 if (NULL != factory) {
856 TestDeferredCanvasSurface(reporter, factory); 848 TestDeferredCanvasSurface(reporter, factory);
857 TestDeferredCanvasSetSurface(reporter, factory); 849 TestDeferredCanvasSetSurface(reporter, factory);
858 } 850 }
859 } 851 }
OLDNEW
« no previous file with comments | « tests/ClipperTest.cpp ('k') | tests/DeviceLooperTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698