Add input/output image logging to processor tests

Moves SkBitmap to base64 uri code into test utils

Bug: skia:
Change-Id: I4f92761fd2e643c38ed00a200b1cb62778a85948
Reviewed-on: https://skia-review.googlesource.com/158340
Reviewed-by: Brian Osman <brianosman@google.com>
Commit-Queue: Michael Ludwig <michaelludwig@google.com>
diff --git a/tests/TestUtils.cpp b/tests/TestUtils.cpp
index 8b611c2..1e03b6be 100644
--- a/tests/TestUtils.cpp
+++ b/tests/TestUtils.cpp
@@ -13,6 +13,8 @@
 #include "GrSurfaceProxy.h"
 #include "GrTextureProxy.h"
 #include "ProxyUtils.h"
+#include "SkBase64.h"
+#include "SkPngEncoder.h"
 
 void test_read_pixels(skiatest::Reporter* reporter,
                       GrSurfaceContext* srcContext, uint32_t expectedPixelValues[],
@@ -142,3 +144,38 @@
     }
     return true;
 }
+
+bool bitmap_to_base64_data_uri(const SkBitmap& bitmap, SkString* dst) {
+    SkPixmap pm;
+    if (!bitmap.peekPixels(&pm)) {
+        dst->set("peekPixels failed");
+        return false;
+    }
+
+    // We're going to embed this PNG in a data URI, so make it as small as possible
+    SkPngEncoder::Options options;
+    options.fFilterFlags = SkPngEncoder::FilterFlag::kAll;
+    options.fZLibLevel = 9;
+
+    SkDynamicMemoryWStream wStream;
+    if (!SkPngEncoder::Encode(&wStream, pm, options)) {
+        dst->set("SkPngEncoder::Encode failed");
+        return false;
+    }
+
+    sk_sp<SkData> pngData = wStream.detachAsData();
+    size_t len = SkBase64::Encode(pngData->data(), pngData->size(), nullptr);
+
+    // The PNG can be almost arbitrarily large. We don't want to fill our logs with enormous URLs.
+    // Infra says these can be pretty big, as long as we're only outputting them on failure.
+    static const size_t kMaxBase64Length = 1024 * 1024;
+    if (len > kMaxBase64Length) {
+        dst->printf("Encoded image too large (%u bytes)", static_cast<uint32_t>(len));
+        return false;
+    }
+
+    dst->resize(len);
+    SkBase64::Encode(pngData->data(), pngData->size(), dst->writable_str());
+    dst->prepend("data:image/png;base64,");
+    return true;
+}