add Make factory to SkMemoryStream (simplify call-sites)

Bug: skia:6888
Change-Id: Ia4e432673ed089a91229697c8dde0489f220000d
Reviewed-on: https://skia-review.googlesource.com/26884
Reviewed-by: Mike Reed <reed@google.com>
Commit-Queue: Mike Reed <reed@google.com>
diff --git a/include/core/SkStream.h b/include/core/SkStream.h
index 01fd82a..1862117 100644
--- a/include/core/SkStream.h
+++ b/include/core/SkStream.h
@@ -263,6 +263,11 @@
 
     ~SkFILEStream() override;
 
+    static std::unique_ptr<SkFILEStream> Make(const char path[]) {
+        SkFILEStream* stream = new SkFILEStream(path);
+        return stream->isValid() ? std::unique_ptr<SkFILEStream>(stream) : nullptr;
+    }
+
     /** Returns true if the current path could be opened. */
     bool isValid() const { return fFILE != nullptr; }
 
@@ -308,6 +313,15 @@
     /** Creates the stream to read from the specified data */
     SkMemoryStream(sk_sp<SkData>);
 
+    /** Returns a stream with a copy of the input data. */
+    static std::unique_ptr<SkMemoryStream> MakeCopy(const void* data, size_t length);
+
+    /** Returns a stream with a bare pointer reference to the input data. */
+    static std::unique_ptr<SkMemoryStream> MakeDirect(const void* data, size_t length);
+
+    /** Returns a stream with a shared reference to the input data. */
+    static std::unique_ptr<SkMemoryStream> Make(sk_sp<SkData> data);
+
     /** Resets the stream to the specified data and length,
         just like the constructor.
         if copyData is true, the stream makes a private copy of the data
diff --git a/src/codec/SkAndroidCodec.cpp b/src/codec/SkAndroidCodec.cpp
index 3a23e06..3a57352 100644
--- a/src/codec/SkAndroidCodec.cpp
+++ b/src/codec/SkAndroidCodec.cpp
@@ -103,7 +103,7 @@
         return nullptr;
     }
 
-    return MakeFromStream(skstd::make_unique<SkMemoryStream>(std::move(data)), chunkReader);
+    return MakeFromStream(SkMemoryStream::Make(std::move(data)), chunkReader);
 }
 
 SkColorType SkAndroidCodec::computeOutputColorType(SkColorType requestedColorType) {
diff --git a/src/codec/SkCodec.cpp b/src/codec/SkCodec.cpp
index d0958e8..09343e2 100644
--- a/src/codec/SkCodec.cpp
+++ b/src/codec/SkCodec.cpp
@@ -123,8 +123,7 @@
     if (!data) {
         return nullptr;
     }
-    return MakeFromStream(std::unique_ptr<SkStream>(new SkMemoryStream(std::move(data))),
-                                                    nullptr, reader);
+    return MakeFromStream(SkMemoryStream::Make(std::move(data)), nullptr, reader);
 }
 
 SkCodec::SkCodec(int width, int height, const SkEncodedInfo& info,
diff --git a/src/codec/SkIcoCodec.cpp b/src/codec/SkIcoCodec.cpp
index fce14a2..24549fa 100644
--- a/src/codec/SkIcoCodec.cpp
+++ b/src/codec/SkIcoCodec.cpp
@@ -144,7 +144,7 @@
         }
 
         sk_sp<SkData> data(SkData::MakeFromMalloc(buffer.release(), size));
-        std::unique_ptr<SkMemoryStream> embeddedStream(new SkMemoryStream(data));
+        auto embeddedStream = SkMemoryStream::Make(data);
         bytesRead += size;
 
         // Check if the embedded codec is bmp or png and create the codec
diff --git a/src/codec/SkRawCodec.cpp b/src/codec/SkRawCodec.cpp
index dbcbd28..cf41d17 100644
--- a/src/codec/SkRawCodec.cpp
+++ b/src/codec/SkRawCodec.cpp
@@ -288,7 +288,7 @@
                 }
             }
         }
-        return skstd::make_unique<SkMemoryStream>(data);
+        return SkMemoryStream::Make(data);
     }
 
 private:
@@ -381,7 +381,7 @@
             sk_sp<SkData> data(SkData::MakeWithCopy(
                 static_cast<const uint8_t*>(fStream->getMemoryBase()) + offset, bytesToRead));
             fStream.reset();
-            return skstd::make_unique<SkMemoryStream>(data);
+            return SkMemoryStream::Make(data);
         } else {
             sk_sp<SkData> data(SkData::MakeUninitialized(bytesToRead));
             if (!fStream->seek(offset)) {
@@ -391,7 +391,7 @@
             if (bytesRead < bytesToRead) {
                 data = SkData::MakeSubset(data.get(), 0, bytesRead);
             }
-            return skstd::make_unique<SkMemoryStream>(data);
+            return SkMemoryStream::Make(data);
         }
     }
 private:
diff --git a/src/core/SkFontDescriptor.cpp b/src/core/SkFontDescriptor.cpp
index 73ea205..519e8f2 100644
--- a/src/core/SkFontDescriptor.cpp
+++ b/src/core/SkFontDescriptor.cpp
@@ -108,7 +108,7 @@
         sk_sp<SkData> data(SkData::MakeUninitialized(length));
         if (stream->read(data->writable_data(), length) == length) {
             result->fFontData = skstd::make_unique<SkFontData>(
-                skstd::make_unique<SkMemoryStream>(data), index, axis, axisCount);
+                                   SkMemoryStream::Make(std::move(data)), index, axis, axisCount);
         } else {
             SkDEBUGFAIL("Could not read font data");
             return false;
diff --git a/src/core/SkStream.cpp b/src/core/SkStream.cpp
index 7bb2079..139a72f 100644
--- a/src/core/SkStream.cpp
+++ b/src/core/SkStream.cpp
@@ -274,6 +274,18 @@
     fOffset = 0;
 }
 
+std::unique_ptr<SkMemoryStream> SkMemoryStream::MakeCopy(const void* data, size_t length) {
+    return skstd::make_unique<SkMemoryStream>(data, length, true);
+}
+
+std::unique_ptr<SkMemoryStream> SkMemoryStream::MakeDirect(const void* data, size_t length) {
+    return skstd::make_unique<SkMemoryStream>(data, length, false);
+}
+
+std::unique_ptr<SkMemoryStream> SkMemoryStream::Make(sk_sp<SkData> data) {
+    return skstd::make_unique<SkMemoryStream>(std::move(data));
+}
+
 void SkMemoryStream::setMemoryOwned(const void* src, size_t size) {
     fData = SkData::MakeFromMalloc(src, size);
     fOffset = 0;
diff --git a/tests/CodecTest.cpp b/tests/CodecTest.cpp
index 698af46..4e68cd1 100644
--- a/tests/CodecTest.cpp
+++ b/tests/CodecTest.cpp
@@ -1389,8 +1389,8 @@
 
 static void test_invalid_header(skiatest::Reporter* r, const char* path) {
     SkString resourcePath = GetResourcePath(path);
-    std::unique_ptr<SkFILEStream> stream(new SkFILEStream(resourcePath.c_str()));
-    if (!stream->isValid()) {
+    auto stream = SkFILEStream::Make(resourcePath.c_str());
+    if (!stream) {
         return;
     }
 
diff --git a/tests/GifTest.cpp b/tests/GifTest.cpp
index daa5cf5..29150ba 100644
--- a/tests/GifTest.cpp
+++ b/tests/GifTest.cpp
@@ -225,10 +225,9 @@
 // Regression test for decoding a gif image with sampleSize of 4, which was
 // previously crashing.
 DEF_TEST(Gif_Sampled, r) {
-    std::unique_ptr<SkFILEStream> stream(
-            new SkFILEStream(GetResourcePath("test640x479.gif").c_str()));
-    REPORTER_ASSERT(r, stream->isValid());
-    if (!stream->isValid()) {
+    auto stream = SkFILEStream::Make(GetResourcePath("test640x479.gif").c_str());
+    REPORTER_ASSERT(r, stream);
+    if (!stream) {
         return;
     }
 
diff --git a/tools/Resources.cpp b/tools/Resources.cpp
index 27ccef4..948949b 100644
--- a/tools/Resources.cpp
+++ b/tools/Resources.cpp
@@ -46,8 +46,8 @@
 
 std::unique_ptr<SkStreamAsset> GetResourceAsStream(const char* resource) {
     SkString resourcePath = GetResourcePath(resource);
-    std::unique_ptr<SkFILEStream> stream(new SkFILEStream(resourcePath.c_str()));
-    if (!stream->isValid()) {
+    auto stream = SkFILEStream::Make(resourcePath.c_str());
+    if (!stream) {
         SkDebugf("Resource %s not found.\n", resource);
         return nullptr;
     }