aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests
diff options
context:
space:
mode:
authorGravatar reed <reed@google.com>2015-06-22 12:48:26 -0700
committerGravatar Commit bot <commit-bot@chromium.org>2015-06-22 12:48:26 -0700
commit871872f3f247f6b699617f6d9ef50ef5da6fbe74 (patch)
tree50358d6f699dd433607d9bc3c29da63e44c5d00f /tests
parentc1f56b518218d1caa65d6b7101bebf0d28c02a92 (diff)
change old picture serialization to really handle images
Diffstat (limited to 'tests')
-rw-r--r--tests/CachedDecodingPixelRefTest.cpp2
-rw-r--r--tests/DrawBitmapRectTest.cpp4
-rw-r--r--tests/ImageTest.cpp105
-rw-r--r--tests/SurfaceTest.cpp2
4 files changed, 109 insertions, 4 deletions
diff --git a/tests/CachedDecodingPixelRefTest.cpp b/tests/CachedDecodingPixelRefTest.cpp
index 66e5273625..465b69b856 100644
--- a/tests/CachedDecodingPixelRefTest.cpp
+++ b/tests/CachedDecodingPixelRefTest.cpp
@@ -249,7 +249,7 @@ static void check_pixelref(TestImageGenerator::TestType type,
// Ignore factory; use global cache.
success = SkCachingPixelRef::Install(gen.detach(), &lazy);
} else {
- success = SkInstallDiscardablePixelRef(gen.detach(), &lazy, factory);
+ success = SkInstallDiscardablePixelRef(gen.detach(), NULL, &lazy, factory);
}
REPORTER_ASSERT(reporter, success);
if (TestImageGenerator::kSucceedGetPixels_TestType == type) {
diff --git a/tests/DrawBitmapRectTest.cpp b/tests/DrawBitmapRectTest.cpp
index 80167b7083..e6d46a751d 100644
--- a/tests/DrawBitmapRectTest.cpp
+++ b/tests/DrawBitmapRectTest.cpp
@@ -33,8 +33,8 @@ static void test_faulty_pixelref(skiatest::Reporter* reporter) {
SkAutoTUnref<SkDiscardableMemoryPool> pool(
SkDiscardableMemoryPool::Create(10 * 1000, NULL));
SkBitmap bm;
- bool installSuccess = SkInstallDiscardablePixelRef(SkNEW(FailureImageGenerator), &bm, pool);
- REPORTER_ASSERT(reporter, installSuccess);
+ bool success = SkInstallDiscardablePixelRef(SkNEW(FailureImageGenerator), NULL, &bm, pool);
+ REPORTER_ASSERT(reporter, success);
// now our bitmap has a pixelref, but we know it will fail to lock
SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(200, 200));
diff --git a/tests/ImageTest.cpp b/tests/ImageTest.cpp
new file mode 100644
index 0000000000..b196ca8f34
--- /dev/null
+++ b/tests/ImageTest.cpp
@@ -0,0 +1,105 @@
+/*
+ * Copyright 2015 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 "SkData.h"
+#include "SkDevice.h"
+#include "SkImageEncoder.h"
+#include "SkImage_Base.h"
+#include "SkRRect.h"
+#include "SkSurface.h"
+#include "SkUtils.h"
+#include "Test.h"
+
+#if SK_SUPPORT_GPU
+#include "GrContextFactory.h"
+#include "GrTest.h"
+#include "gl/GrGLInterface.h"
+#include "gl/GrGLUtil.h"
+#else
+class GrContextFactory;
+class GrContext;
+#endif
+
+static void assert_equal(skiatest::Reporter* reporter, SkImage* a, const SkIRect* subsetA,
+ SkImage* b) {
+ const int widthA = subsetA ? subsetA->width() : a->width();
+ const int heightA = subsetA ? subsetA->height() : a->height();
+
+ REPORTER_ASSERT(reporter, widthA == b->width());
+ REPORTER_ASSERT(reporter, heightA == b->height());
+#if 0
+ // see skbug.com/3965
+ bool AO = a->isOpaque();
+ bool BO = b->isOpaque();
+ REPORTER_ASSERT(reporter, AO == BO);
+#endif
+
+ SkImageInfo info = SkImageInfo::MakeN32(widthA, heightA,
+ a->isOpaque() ? kOpaque_SkAlphaType : kPremul_SkAlphaType);
+ SkAutoPixmapStorage pmapA, pmapB;
+ pmapA.alloc(info);
+ pmapB.alloc(info);
+
+ const int srcX = subsetA ? subsetA->x() : 0;
+ const int srcY = subsetA ? subsetA->y() : 0;
+
+ REPORTER_ASSERT(reporter, a->readPixels(pmapA, srcX, srcY));
+ REPORTER_ASSERT(reporter, b->readPixels(pmapB, 0, 0));
+
+ const size_t widthBytes = widthA * info.bytesPerPixel();
+ for (int y = 0; y < heightA; ++y) {
+ REPORTER_ASSERT(reporter, !memcmp(pmapA.addr32(0, y), pmapB.addr32(0, y), widthBytes));
+ }
+}
+
+static SkImage* make_image(GrContext* ctx, int w, int h, const SkIRect& ir) {
+ const SkImageInfo info = SkImageInfo::MakeN32(w, h, kOpaque_SkAlphaType);
+ SkAutoTUnref<SkSurface> surface(ctx ?
+ SkSurface::NewRenderTarget(ctx, SkSurface::kNo_Budgeted, info) :
+ SkSurface::NewRaster(info));
+ SkCanvas* canvas = surface->getCanvas();
+ canvas->clear(SK_ColorWHITE);
+
+ SkPaint paint;
+ paint.setColor(SK_ColorBLACK);
+ canvas->drawRect(SkRect::Make(ir), paint);
+ return surface->newImageSnapshot();
+}
+
+static void test_encode(skiatest::Reporter* reporter, GrContext* ctx) {
+ const SkIRect ir = SkIRect::MakeXYWH(5, 5, 10, 10);
+ SkAutoTUnref<SkImage> orig(make_image(ctx, 20, 20, ir));
+ SkAutoTUnref<SkData> origEncoded(orig->encode());
+ REPORTER_ASSERT(reporter, origEncoded);
+ REPORTER_ASSERT(reporter, origEncoded->size() > 0);
+
+ SkAutoTUnref<SkImage> decoded(SkImage::NewFromEncoded(origEncoded));
+ REPORTER_ASSERT(reporter, decoded);
+ assert_equal(reporter, orig, NULL, decoded);
+
+ // Now see if we can instantiate an image from a subset of the surface/origEncoded
+
+ decoded.reset(SkImage::NewFromEncoded(origEncoded, &ir));
+ REPORTER_ASSERT(reporter, decoded);
+ assert_equal(reporter, orig, &ir, decoded);
+}
+
+DEF_TEST(Image_Encode_Cpu, reporter) {
+ test_encode(reporter, NULL);
+}
+
+#if SK_SUPPORT_GPU
+DEF_GPUTEST(Image_Encode_Gpu, reporter, factory) {
+ GrContext* ctx = factory->get(GrContextFactory::kNative_GLContextType);
+ if (!ctx) {
+ REPORTER_ASSERT(reporter, false);
+ return;
+ }
+ test_encode(reporter, ctx);
+}
+#endif
diff --git a/tests/SurfaceTest.cpp b/tests/SurfaceTest.cpp
index 0eebfeb4ef..0114fb66fc 100644
--- a/tests/SurfaceTest.cpp
+++ b/tests/SurfaceTest.cpp
@@ -251,7 +251,7 @@ static SkImage* createImage(ImageType imageType, GrContext* context, SkColor col
bitmap.installPixels(info, addr, rowBytes);
SkAutoTUnref<SkData> src(
SkImageEncoder::EncodeData(bitmap, SkImageEncoder::kPNG_Type, 100));
- return SkImage::NewFromData(src);
+ return SkImage::NewFromEncoded(src);
}
}
SkASSERT(false);