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

Unified Diff: tests/PipeTest.cpp

Issue 2201323003: add pipecanvas (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: add test for writeImage Created 4 years, 3 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
« src/core/SkPipe.h ('K') | « src/pipe/SkRefSet.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tests/PipeTest.cpp
diff --git a/tests/PipeTest.cpp b/tests/PipeTest.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..f345debb8ca46ba012350137059f516fd5219163
--- /dev/null
+++ b/tests/PipeTest.cpp
@@ -0,0 +1,88 @@
+/*
+ * 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 "Resources.h"
+#include "SkCanvas.h"
+#include "SkPipe.h"
+#include "SkPaint.h"
+#include "SkStream.h"
+#include "SkSurface.h"
+#include "Test.h"
+
+#include "SkNullCanvas.h"
+#include "SkAutoPixmapStorage.h"
+
+static void drain(SkPipeDeserializer* deserial, SkDynamicMemoryWStream* stream) {
+ std::unique_ptr<SkCanvas> canvas(SkCreateNullCanvas());
+ sk_sp<SkData> data = stream->detachAsData();
+ deserial->playback(data->data(), data->size(), canvas.get());
+}
+
+static sk_sp<SkImage> drain_as_image(SkPipeDeserializer* deserial, SkDynamicMemoryWStream* stream) {
+ sk_sp<SkData> data = stream->detachAsData();
+ return deserial->readImage(data->data(), data->size());
+}
+
+static bool deep_equal(SkImage* a, SkImage* b) {
+ if (a->width() != b->width() || a->height() != b->height()) {
+ return false;
+ }
+
+ const SkImageInfo info = SkImageInfo::MakeN32Premul(a->width(), a->height());
+ SkAutoPixmapStorage pmapA, pmapB;
+ pmapA.alloc(info);
+ pmapB.alloc(info);
+
+ if (!a->readPixels(pmapA, 0, 0) || !b->readPixels(pmapB, 0, 0)) {
+ return false;
+ }
+
+ for (int y = 0; y < info.height(); ++y) {
+ if (memcmp(pmapA.addr32(0, y), pmapB.addr32(0, y), info.width() * sizeof(SkPMColor))) {
+ return false;
+ }
+ }
+ return true;
+}
+
+DEF_TEST(Pipe_image, reporter) {
+ sk_sp<SkImage> img = GetResourceAsImage("mandrill_128.png");
+ SkASSERT(img.get());
+
+ SkPipeSerializer serializer;
+ SkPipeDeserializer deserializer;
+
+ SkDynamicMemoryWStream stream;
+ SkCanvas* wc = serializer.beginWrite(SkRect::MakeWH(100, 100), &stream);
+ wc->drawImage(img, 0, 0, nullptr);
+ serializer.endWrite();
+ size_t offset0 = stream.bytesWritten();
+ REPORTER_ASSERT(reporter, offset0 > 100); // the raw image must be sorta big
+ drain(&deserializer, &stream);
+
+ // try drawing the same image again -- it should be much smaller
+ wc = serializer.beginWrite(SkRect::MakeWH(100, 100), &stream);
+ wc->drawImage(img, 0, 0, nullptr);
+ size_t offset1 = stream.bytesWritten();
+ serializer.endWrite();
+ REPORTER_ASSERT(reporter, offset1 <= 32);
+ drain(&deserializer, &stream);
+
+ // try serializing the same image directly, again it should be small
+ serializer.write(img.get(), &stream);
+ size_t offset2 = stream.bytesWritten();
+ REPORTER_ASSERT(reporter, offset2 <= 32);
+ auto img1 = drain_as_image(&deserializer, &stream);
+ REPORTER_ASSERT(reporter, deep_equal(img.get(), img1.get()));
+
+ // try serializing the same image directly (again), check that it is the same!
+ serializer.write(img.get(), &stream);
+ size_t offset3 = stream.bytesWritten();
+ REPORTER_ASSERT(reporter, offset3 <= 32);
+ auto img2 = drain_as_image(&deserializer, &stream);
+ REPORTER_ASSERT(reporter, img1.get() == img2.get());
+}
« src/core/SkPipe.h ('K') | « src/pipe/SkRefSet.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698