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

Unified Diff: tests/SpecialImageTest.cpp

Issue 1579323002: Add SkSpecialImage & SkSpecialSurface classes (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Address code review comments Created 4 years, 11 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 side-by-side diff with in-line comments
Download patch
Index: tests/SpecialImageTest.cpp
diff --git a/tests/SpecialImageTest.cpp b/tests/SpecialImageTest.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..49ff374cacc6d8ae67ddd743c8387e4449553a7b
--- /dev/null
+++ b/tests/SpecialImageTest.cpp
@@ -0,0 +1,133 @@
+/*
+ * 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
+
+// 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) {
+ REPORTER_ASSERT(reporter, kPad == img->activeRect().left());
+ REPORTER_ASSERT(reporter, kPad == img->activeRect().top());
+ REPORTER_ASSERT(reporter, kSmallerSize == img->activeRect().width());
+ REPORTER_ASSERT(reporter, kSmallerSize == img->activeRect().height());
+
+ //--------------
+ REPORTER_ASSERT(reporter, peekTextureSucceeds == !!img->peekTexture());
+
+ //--------------
+ SkPixmap pixmap;
+ REPORTER_ASSERT(reporter, peekPixelsSucceeds == !!img->peekPixels(&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);
+ SK_ALWAYSBREAK(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& activeRect = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
+
+ SkAutoTUnref<SkSpecialImage> img(SkSpecialImage::NewRaster(activeRect, bm));
+ test_image(img, reporter, true, false);
+}
+
+DEF_TEST(SpecialImage_Image, reporter) {
+ SkBitmap bm = create_bm();
+
+ SkAutoTUnref<SkImage> fullImage(SkImage::NewFromBitmap(bm));
+
+ const SkIRect& activeRect = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
+
+ SkAutoTUnref<SkSpecialImage> img(SkSpecialImage::NewImage(activeRect, 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& activeRect = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
+
+ SkAutoTUnref<SkSpecialImage> img(SkSpecialImage::NewGpu(activeRect, texture));
+ test_image(img, reporter, false, true);
+}
+
+#endif

Powered by Google App Engine
This is Rietveld 408576698