Index: tests/SpecialImageTest.cpp |
diff --git a/tests/SpecialImageTest.cpp b/tests/SpecialImageTest.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..0680a4292afe5802c041d6f66de240881fb19101 |
--- /dev/null |
+++ b/tests/SpecialImageTest.cpp |
@@ -0,0 +1,120 @@ |
+/* |
+ * 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 |
+ |
+static const int kFullWidthHeight = 16; |
+static const int kSmallerWidthHeight = 10; |
+ |
+// Create a bitmap with red in the upper-left corner and green everywhere else |
+static SkBitmap create_bm() { |
+ SkBitmap bm; |
+ bm.allocN32Pixels(kFullWidthHeight, kFullWidthHeight, true); |
+ |
+ SkCanvas temp(bm); |
+ |
+ temp.clear(SK_ColorGREEN); |
+ SkPaint p; |
+ p.setColor(SK_ColorRED); |
+ p.setAntiAlias(false); |
+ |
+ temp.drawRect(SkRect::MakeIWH(kSmallerWidthHeight, kSmallerWidthHeight), 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) { |
+ REPORTER_ASSERT(reporter, kSmallerWidthHeight == img->width()); |
+ REPORTER_ASSERT(reporter, kSmallerWidthHeight == img->height()); |
+ |
+ //-------------- |
+ REPORTER_ASSERT(reporter, peekTextureSucceeds == !!img->peekTexture()); |
+ |
+ //-------------- |
+ SkImageInfo info; |
+ size_t rowBytes; |
+ REPORTER_ASSERT(reporter, peekPixelsSucceeds == !!img->peekPixels(&info, &rowBytes)); |
+ if (peekPixelsSucceeds) { |
+ REPORTER_ASSERT(reporter, kFullWidthHeight == info.width()); |
+ REPORTER_ASSERT(reporter, kFullWidthHeight == info.height()); |
+ } |
+ |
+ //-------------- |
+ info = SkImageInfo::MakeN32(kFullWidthHeight, kFullWidthHeight, kOpaque_SkAlphaType); |
+ |
+ SkAutoTUnref<SkSpecialSurface> surf(img->newSurface(info)); |
+ |
+ SkCanvas* canvas = surf->getCanvas(); |
+ |
+ canvas->clear(SK_ColorBLUE); |
+ img->draw(canvas, 0, 0, nullptr); |
+ |
+ SkBitmap bm; |
+ bm.allocN32Pixels(kFullWidthHeight, kFullWidthHeight, true); |
+ |
+ bool result = canvas->readPixels(bm.info(), bm.getPixels(), bm.rowBytes(), 0, 0); |
+ SK_ALWAYSBREAK(result); |
+ |
+ // Only the upper-left corner should've been drawn into the canvas |
+ REPORTER_ASSERT(reporter, SK_ColorRED == bm.getColor(kSmallerWidthHeight-1, |
+ kSmallerWidthHeight-1)); |
+ REPORTER_ASSERT(reporter, SK_ColorBLUE == bm.getColor(kSmallerWidthHeight, |
+ kSmallerWidthHeight)); |
+} |
+ |
+DEF_TEST(SpecialImage_Raster, reporter) { |
+ SkBitmap bm = create_bm(); |
+ |
+ SkAutoTUnref<SkSpecialImage> img(SkSpecialImage::New(kSmallerWidthHeight, kSmallerWidthHeight, |
+ bm)); |
+ test_image(img, reporter, true, false); |
+} |
+ |
+DEF_TEST(SpecialImage_Image, reporter) { |
+ SkBitmap bm = create_bm(); |
+ |
+ SkAutoTUnref<SkImage> fullImage(SkImage::NewFromBitmap(bm)); |
+ |
+ SkAutoTUnref<SkSpecialImage> img(SkSpecialImage::New(kSmallerWidthHeight, kSmallerWidthHeight, |
+ 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 = kFullWidthHeight; |
+ desc.fHeight = kFullWidthHeight; |
+ |
+ SkAutoTUnref<GrTexture> texture(context->textureProvider()->createTexture(desc, false, |
+ bm.getPixels(), 0)); |
+ if (!texture) { |
+ return; |
+ } |
+ |
+ SkAutoTUnref<SkSpecialImage> img(SkSpecialImage::New(kSmallerWidthHeight, kSmallerWidthHeight, |
+ texture)); |
+ test_image(img, reporter, false, true); |
+} |
+ |
+#endif |