diff options
author | robertphillips <robertphillips@google.com> | 2016-02-04 10:52:42 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-02-04 10:52:42 -0800 |
commit | b6c65e99956b867e24bd5bd68ae37673a9fd4b27 (patch) | |
tree | cb320967a3e8253a1100881942b540594f33315d /tests | |
parent | 7471c780d48afd4dc02ed45c60a2fd2efa9e5a84 (diff) |
Add SkSpecialImage & SkSpecialSurface classes
Initial classes.
GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1579323002
Review URL: https://codereview.chromium.org/1579323002
Diffstat (limited to 'tests')
-rw-r--r-- | tests/SpecialImageTest.cpp | 150 | ||||
-rw-r--r-- | tests/SpecialSurfaceTest.cpp | 115 |
2 files changed, 265 insertions, 0 deletions
diff --git a/tests/SpecialImageTest.cpp b/tests/SpecialImageTest.cpp new file mode 100644 index 0000000000..3ae8d33911 --- /dev/null +++ b/tests/SpecialImageTest.cpp @@ -0,0 +1,150 @@ +/* + * Copyright 2016 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file + */ + +#include "SkBitmap.h" +#include "SkCanvas.h" +#include "SkImage.h" +#include "SkSpecialImage.h" +#include "SkSpecialSurface.h" +#include "Test.h" + +#if SK_SUPPORT_GPU +#include "GrContext.h" +#endif + +class TestingSpecialImageAccess { +public: + static const SkIRect& Subset(const SkSpecialImage* img) { + return img->subset(); + } + + static bool PeekPixels(const SkSpecialImage* img, SkPixmap* pixmap) { + return img->peekPixels(pixmap); + } + + static GrTexture* PeekTexture(const SkSpecialImage* img) { + return img->peekTexture(); + } +}; + +// This test creates backing resources exactly sized to [kFullSize x kFullSize]. +// It then wraps them in an SkSpecialImage with only the center (red) region being active. +// It then draws the SkSpecialImage to a full sized (all blue) canvas and checks that none +// of the inactive (green) region leaked out. + +static const int kSmallerSize = 10; +static const int kPad = 3; +static const int kFullSize = kSmallerSize + 2 * kPad; + +// Create a bitmap with red in the center and green around it +static SkBitmap create_bm() { + SkBitmap bm; + bm.allocN32Pixels(kFullSize, kFullSize, true); + + SkCanvas temp(bm); + + temp.clear(SK_ColorGREEN); + SkPaint p; + p.setColor(SK_ColorRED); + p.setAntiAlias(false); + + temp.drawRect(SkRect::MakeXYWH(SkIntToScalar(kPad), SkIntToScalar(kPad), + SkIntToScalar(kSmallerSize), SkIntToScalar(kSmallerSize)), + p); + + return bm; +} + +// Basic test of the SkSpecialImage public API (e.g., peekTexture, peekPixels & draw) +static void test_image(SkSpecialImage* img, skiatest::Reporter* reporter, + bool peekPixelsSucceeds, bool peekTextureSucceeds) { + const SkIRect subset = TestingSpecialImageAccess::Subset(img); + REPORTER_ASSERT(reporter, kPad == subset.left()); + REPORTER_ASSERT(reporter, kPad == subset.top()); + REPORTER_ASSERT(reporter, kSmallerSize == subset.width()); + REPORTER_ASSERT(reporter, kSmallerSize == subset.height()); + + //-------------- + REPORTER_ASSERT(reporter, peekTextureSucceeds == !!TestingSpecialImageAccess::PeekTexture(img)); + + //-------------- + SkPixmap pixmap; + REPORTER_ASSERT(reporter, peekPixelsSucceeds == + !!TestingSpecialImageAccess::PeekPixels(img, &pixmap)); + if (peekPixelsSucceeds) { + REPORTER_ASSERT(reporter, kFullSize == pixmap.width()); + REPORTER_ASSERT(reporter, kFullSize == pixmap.height()); + } + + //-------------- + SkImageInfo info = SkImageInfo::MakeN32(kFullSize, kFullSize, kOpaque_SkAlphaType); + + SkAutoTUnref<SkSpecialSurface> surf(img->newSurface(info)); + + SkCanvas* canvas = surf->getCanvas(); + + canvas->clear(SK_ColorBLUE); + img->draw(canvas, kPad, kPad, nullptr); + + SkBitmap bm; + bm.allocN32Pixels(kFullSize, kFullSize, true); + + bool result = canvas->readPixels(bm.info(), bm.getPixels(), bm.rowBytes(), 0, 0); + SkASSERT_RELEASE(result); + + // Only the center (red) portion should've been drawn into the canvas + REPORTER_ASSERT(reporter, SK_ColorBLUE == bm.getColor(kPad-1, kPad-1)); + REPORTER_ASSERT(reporter, SK_ColorRED == bm.getColor(kPad, kPad)); + REPORTER_ASSERT(reporter, SK_ColorRED == bm.getColor(kSmallerSize+kPad-1, + kSmallerSize+kPad-1)); + REPORTER_ASSERT(reporter, SK_ColorBLUE == bm.getColor(kSmallerSize+kPad, + kSmallerSize+kPad)); +} + +DEF_TEST(SpecialImage_Raster, reporter) { + SkBitmap bm = create_bm(); + + const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize); + + SkAutoTUnref<SkSpecialImage> img(SkSpecialImage::NewFromRaster(subset, bm)); + test_image(img, reporter, true, false); +} + +DEF_TEST(SpecialImage_Image, reporter) { + SkBitmap bm = create_bm(); + + SkAutoTUnref<SkImage> fullImage(SkImage::NewFromBitmap(bm)); + + const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize); + + SkAutoTUnref<SkSpecialImage> img(SkSpecialImage::NewFromImage(subset, fullImage)); + test_image(img, reporter, true, false); +} + +#if SK_SUPPORT_GPU +DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_Gpu, reporter, context) { + SkBitmap bm = create_bm(); + + GrSurfaceDesc desc; + desc.fConfig = kSkia8888_GrPixelConfig; + desc.fFlags = kNone_GrSurfaceFlags; + desc.fWidth = kFullSize; + desc.fHeight = kFullSize; + + SkAutoTUnref<GrTexture> texture(context->textureProvider()->createTexture(desc, false, + bm.getPixels(), 0)); + if (!texture) { + return; + } + + const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize); + + SkAutoTUnref<SkSpecialImage> img(SkSpecialImage::NewFromGpu(subset, texture)); + test_image(img, reporter, false, true); +} + +#endif diff --git a/tests/SpecialSurfaceTest.cpp b/tests/SpecialSurfaceTest.cpp new file mode 100644 index 0000000000..b1cbf68082 --- /dev/null +++ b/tests/SpecialSurfaceTest.cpp @@ -0,0 +1,115 @@ +/* +* Copyright 2016 Google Inc. +* +* Use of this source code is governed by a BSD-style license that can be +* found in the LICENSE file +*/ + +#include "SkCanvas.h" +#include "SkSpecialImage.h" +#include "SkSpecialSurface.h" +#include "Test.h" + +#if SK_SUPPORT_GPU +#include "GrContext.h" +#include "SkGr.h" +#endif + +class TestingSpecialSurfaceAccess { +public: + static const SkIRect& Subset(const SkSpecialSurface* surf) { + return surf->subset(); + } + + static const SkIRect& Subset(const SkSpecialImage* img) { + return img->subset(); + } +}; + +// Both 'kSmallerSize' and 'kFullSize' need to be a non-power-of-2 to exercise +// the gpu's loose fit behavior +static const int kSmallerSize = 10; +static const int kPad = 5; +static const int kFullSize = kSmallerSize + 2 * kPad; + +// Exercise the public API of SkSpecialSurface (e.g., getCanvas, newImageSnapshot) +static void test_surface(SkSpecialSurface* surf, skiatest::Reporter* reporter, int offset) { + + const SkIRect surfSubset = TestingSpecialSurfaceAccess::Subset(surf); + REPORTER_ASSERT(reporter, offset == surfSubset.fLeft); + REPORTER_ASSERT(reporter, offset == surfSubset.fTop); + REPORTER_ASSERT(reporter, kSmallerSize == surfSubset.width()); + REPORTER_ASSERT(reporter, kSmallerSize == surfSubset.height()); + + SkCanvas* canvas = surf->getCanvas(); + SkASSERT_RELEASE(canvas); + + canvas->clear(SK_ColorRED); + + SkAutoTUnref<SkSpecialImage> img(surf->newImageSnapshot()); + REPORTER_ASSERT(reporter, img); + + const SkIRect imgSubset = TestingSpecialSurfaceAccess::Subset(img); + REPORTER_ASSERT(reporter, surfSubset == imgSubset); + + // the canvas was invalidated by the newImageSnapshot call + REPORTER_ASSERT(reporter, !surf->getCanvas()); +} + +DEF_TEST(SpecialSurface_Raster, reporter) { + + SkImageInfo info = SkImageInfo::MakeN32(kSmallerSize, kSmallerSize, kOpaque_SkAlphaType); + SkAutoTUnref<SkSpecialSurface> surf(SkSpecialSurface::NewRaster(info)); + + test_surface(surf, reporter, 0); +} + +DEF_TEST(SpecialSurface_Raster2, reporter) { + + SkBitmap bm; + bm.allocN32Pixels(kFullSize, kFullSize, true); + + const SkIRect subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize); + + SkAutoTUnref<SkSpecialSurface> surf(SkSpecialSurface::NewFromBitmap(subset, bm)); + + test_surface(surf, reporter, kPad); + + // TODO: check that the clear didn't escape the active region +} + +#if SK_SUPPORT_GPU + +DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialSurface_Gpu1, reporter, context) { + GrSurfaceDesc desc; + desc.fConfig = kSkia8888_GrPixelConfig; + desc.fFlags = kRenderTarget_GrSurfaceFlag; + desc.fWidth = kSmallerSize; + desc.fHeight = kSmallerSize; + + SkAutoTUnref<SkSpecialSurface> surf(SkSpecialSurface::NewRenderTarget(context, desc)); + + test_surface(surf, reporter, 0); +} + +// test the more flexible factory +DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialSurface_Gpu2, reporter, context) { + GrSurfaceDesc desc; + desc.fConfig = kSkia8888_GrPixelConfig; + desc.fFlags = kRenderTarget_GrSurfaceFlag; + desc.fWidth = kFullSize; + desc.fHeight = kFullSize; + + SkAutoTUnref<GrTexture> temp(context->textureProvider()->createApproxTexture(desc)); + SkASSERT_RELEASE(temp); + + const SkIRect subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize); + + SkAutoTUnref<SkSpecialSurface> surf(SkSpecialSurface::NewFromTexture(subset, temp)); + + test_surface(surf, reporter, kPad); + + // TODO: check that the clear didn't escape the active region +} + +#endif |