OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2012 Google Inc. | 3 * Copyright 2012 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 #include "Test.h" | 8 #include "Test.h" |
9 #include "SkBitmap.h" | 9 #include "SkBitmap.h" |
10 #include "SkBitmapProcShader.h" | 10 #include "SkBitmapProcShader.h" |
11 #include "SkDeferredCanvas.h" | 11 #include "SkDeferredCanvas.h" |
12 #include "SkDevice.h" | 12 #include "SkDevice.h" |
13 #include "SkGradientShader.h" | 13 #include "SkGradientShader.h" |
14 #include "SkShader.h" | 14 #include "SkShader.h" |
| 15 #include "SkSurface.h" |
| 16 #if SK_SUPPORT_GPU |
| 17 #include "GrContextFactory.h" |
| 18 #else |
| 19 class GrContextFactory; |
| 20 #endif |
15 | 21 |
16 static const int gWidth = 2; | 22 static const int gWidth = 2; |
17 static const int gHeight = 2; | 23 static const int gHeight = 2; |
18 | 24 |
19 static void create(SkBitmap* bm, SkBitmap::Config config, SkColor color) { | 25 static void create(SkBitmap* bm, SkBitmap::Config config, SkColor color) { |
20 bm->setConfig(config, gWidth, gHeight); | 26 bm->setConfig(config, gWidth, gHeight); |
21 bm->allocPixels(); | 27 bm->allocPixels(); |
22 bm->eraseColor(color); | 28 bm->eraseColor(color); |
23 } | 29 } |
24 | 30 |
(...skipping 433 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
458 { | 464 { |
459 SkDevice device(store); | 465 SkDevice device(store); |
460 SkDeferredCanvas canvas(&device); | 466 SkDeferredCanvas canvas(&device); |
461 canvas.setBitmapSizeThreshold(40001); | 467 canvas.setBitmapSizeThreshold(40001); |
462 canvas.drawBitmap(sourceImage, 0, 0, NULL); | 468 canvas.drawBitmap(sourceImage, 0, 0, NULL); |
463 size_t newBytesAllocated = canvas.storageAllocatedForRecording(); | 469 size_t newBytesAllocated = canvas.storageAllocatedForRecording(); |
464 REPORTER_ASSERT(reporter, newBytesAllocated > 0); | 470 REPORTER_ASSERT(reporter, newBytesAllocated > 0); |
465 } | 471 } |
466 } | 472 } |
467 | 473 |
468 static void TestDeferredCanvas(skiatest::Reporter* reporter) { | 474 |
| 475 typedef void* PixelPtr; |
| 476 // Returns an opaque pointer which, either points to a GrTexture or RAM pixel |
| 477 // buffer. Used to test pointer equality do determine whether a surface points |
| 478 // to the same pixel data storage as before. |
| 479 PixelPtr getSurfacePixelPtr(SkSurface* surface, bool useGpu) { |
| 480 return useGpu ? surface->getCanvas()->getDevice()->accessBitmap(false).getTe
xture() : |
| 481 surface->getCanvas()->getDevice()->accessBitmap(false).getPixels(); |
| 482 } |
| 483 |
| 484 static void TestDeferredCanvasSurface(skiatest::Reporter* reporter, GrContextFac
tory* factory) { |
| 485 SkImage::Info imageSpec = { |
| 486 10, // width |
| 487 10, // height |
| 488 SkImage::kPMColor_ColorType, |
| 489 SkImage::kPremul_AlphaType |
| 490 }; |
| 491 SkSurface* surface; |
| 492 bool useGpu = NULL != factory; |
| 493 #if SK_SUPPORT_GPU |
| 494 if (useGpu) { |
| 495 GrContext* context = factory->get(GrContextFactory::kNative_GLContextTyp
e); |
| 496 surface = SkSurface::NewRenderTarget(context, imageSpec); |
| 497 } else { |
| 498 surface = SkSurface::NewRaster(imageSpec); |
| 499 } |
| 500 #else |
| 501 SkASSERT(!useGpu); |
| 502 surface = SkSurface::NewRaster(imageSpec); |
| 503 #endif |
| 504 SkASSERT(NULL != surface); |
| 505 SkAutoTUnref<SkSurface> aur(surface); |
| 506 SkDeferredCanvas canvas(surface); |
| 507 |
| 508 SkImage* image1 = canvas.newImageShapshot(); |
| 509 SkAutoTUnref<SkImage> aur_i1(image1); |
| 510 PixelPtr pixels1 = getSurfacePixelPtr(surface, useGpu); |
| 511 // The following clear would normally trigger a copy on write, but |
| 512 // it won't because rendering is deferred. |
| 513 canvas.clear(SK_ColorBLACK); |
| 514 // Obtaining a snapshot directly from the surface (as opposed to the |
| 515 // SkDeferredCanvas) will not trigger a flush of deferred draw operations |
| 516 // and will therefore return the same image as the previous snapshot. |
| 517 SkImage* image2 = surface->newImageShapshot(); |
| 518 SkAutoTUnref<SkImage> aur_i2(image2); |
| 519 // Images identical because of deferral |
| 520 REPORTER_ASSERT(reporter, image1->uniqueID() == image2->uniqueID()); |
| 521 // Now we obtain a snpshot via the deferred canvas, which triggers a flush. |
| 522 // Because there is a pending clear, this will generate a different image. |
| 523 SkImage* image3 = canvas.newImageShapshot(); |
| 524 SkAutoTUnref<SkImage> aur_i3(image3); |
| 525 REPORTER_ASSERT(reporter, image1->uniqueID() != image3->uniqueID()); |
| 526 // Verify that backing store is now a different buffer because of copy on |
| 527 // write |
| 528 PixelPtr pixels2 = getSurfacePixelPtr(surface, useGpu); |
| 529 REPORTER_ASSERT(reporter, pixels1 != pixels2); |
| 530 canvas.clear(SK_ColorWHITE); |
| 531 canvas.flush(); |
| 532 PixelPtr pixels3 = getSurfacePixelPtr(surface, useGpu); |
| 533 // Verify that a direct canvas flush with a pending draw does not trigger |
| 534 // a copy on write when the surface is not sharing its buffer with an |
| 535 // SkImage. |
| 536 canvas.clear(SK_ColorBLACK); |
| 537 canvas.flush(); |
| 538 PixelPtr pixels4 = getSurfacePixelPtr(surface, useGpu); |
| 539 REPORTER_ASSERT(reporter, pixels3 == pixels4); |
| 540 } |
| 541 |
| 542 static void TestDeferredCanvas(skiatest::Reporter* reporter, GrContextFactory* f
actory) { |
469 TestDeferredCanvasBitmapAccess(reporter); | 543 TestDeferredCanvasBitmapAccess(reporter); |
470 TestDeferredCanvasFlush(reporter); | 544 TestDeferredCanvasFlush(reporter); |
471 TestDeferredCanvasFreshFrame(reporter); | 545 TestDeferredCanvasFreshFrame(reporter); |
472 TestDeferredCanvasMemoryLimit(reporter); | 546 TestDeferredCanvasMemoryLimit(reporter); |
473 TestDeferredCanvasBitmapCaching(reporter); | 547 TestDeferredCanvasBitmapCaching(reporter); |
474 TestDeferredCanvasSkip(reporter); | 548 TestDeferredCanvasSkip(reporter); |
475 TestDeferredCanvasBitmapShaderNoLeak(reporter); | 549 TestDeferredCanvasBitmapShaderNoLeak(reporter); |
476 TestDeferredCanvasBitmapSizeThreshold(reporter); | 550 TestDeferredCanvasBitmapSizeThreshold(reporter); |
| 551 TestDeferredCanvasSurface(reporter, NULL); |
| 552 if (NULL != factory) { |
| 553 TestDeferredCanvasSurface(reporter, factory); |
| 554 } |
477 } | 555 } |
478 | 556 |
479 #include "TestClassDef.h" | 557 #include "TestClassDef.h" |
480 DEFINE_TESTCLASS("DeferredCanvas", TestDeferredCanvasClass, TestDeferredCanvas) | 558 DEFINE_GPUTESTCLASS("DeferredCanvas", TestDeferredCanvasClass, TestDeferredCanva
s) |
| 559 |
OLD | NEW |