use unique_ptr for codec factories

Will need guards for android (at least)

Bug: skia:
Change-Id: I2bb8e656997984489ef1f2e41cd3d301c4e7b947
Reviewed-on: https://skia-review.googlesource.com/26040
Reviewed-by: Mike Klein <mtklein@chromium.org>
Commit-Queue: Mike Reed <reed@google.com>
diff --git a/src/android/SkBitmapRegionDecoder.cpp b/src/android/SkBitmapRegionDecoder.cpp
index 8298fb5..a6e3ac6 100644
--- a/src/android/SkBitmapRegionDecoder.cpp
+++ b/src/android/SkBitmapRegionDecoder.cpp
@@ -22,11 +22,10 @@
     std::unique_ptr<SkStreamRewindable> streamDeleter(stream);
     switch (strategy) {
         case kAndroidCodec_Strategy: {
-            std::unique_ptr<SkAndroidCodec> codec(
-                    SkAndroidCodec::NewFromStream(streamDeleter.release()));
+            auto codec = SkAndroidCodec::MakeFromStream(std::move(streamDeleter));
             if (nullptr == codec) {
                 SkCodecPrintf("Error: Failed to create codec.\n");
-                return NULL;
+                return nullptr;
             }
 
             switch ((SkEncodedImageFormat)codec->getEncodedFormat()) {
diff --git a/src/codec/SkAndroidCodec.cpp b/src/codec/SkAndroidCodec.cpp
index 0245ea2..3a23e06 100644
--- a/src/codec/SkAndroidCodec.cpp
+++ b/src/codec/SkAndroidCodec.cpp
@@ -8,6 +8,7 @@
 #include "SkAndroidCodec.h"
 #include "SkCodec.h"
 #include "SkCodecPriv.h"
+#include "SkMakeUnique.h"
 #include "SkRawAdapterCodec.h"
 #include "SkSampledCodec.h"
 #include "SkWebpAdapterCodec.h"
@@ -65,8 +66,8 @@
     , fCodec(codec)
 {}
 
-SkAndroidCodec* SkAndroidCodec::NewFromStream(SkStream* stream, SkPngChunkReader* chunkReader) {
-    std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream, nullptr, chunkReader));
+std::unique_ptr<SkAndroidCodec> SkAndroidCodec::MakeFromStream(std::unique_ptr<SkStream> stream, SkPngChunkReader* chunkReader) {
+    auto codec = SkCodec::MakeFromStream(std::move(stream), nullptr, chunkReader);
     if (nullptr == codec) {
         return nullptr;
     }
@@ -82,26 +83,27 @@
         case SkEncodedImageFormat::kGIF:
         case SkEncodedImageFormat::kBMP:
         case SkEncodedImageFormat::kWBMP:
-            return new SkSampledCodec(codec.release());
+            return skstd::make_unique<SkSampledCodec>(codec.release());
 #ifdef SK_HAS_WEBP_LIBRARY
         case SkEncodedImageFormat::kWEBP:
-            return new SkWebpAdapterCodec((SkWebpCodec*) codec.release());
+            return skstd::make_unique<SkWebpAdapterCodec>((SkWebpCodec*) codec.release());
 #endif
 #ifdef SK_CODEC_DECODES_RAW
         case SkEncodedImageFormat::kDNG:
-            return new SkRawAdapterCodec((SkRawCodec*)codec.release());
+            return skstd::make_unique<SkRawAdapterCodec>((SkRawCodec*)codec.release());
 #endif
         default:
             return nullptr;
     }
 }
 
-SkAndroidCodec* SkAndroidCodec::NewFromData(sk_sp<SkData> data, SkPngChunkReader* chunkReader) {
+std::unique_ptr<SkAndroidCodec> SkAndroidCodec::MakeFromData(sk_sp<SkData> data,
+                                                             SkPngChunkReader* chunkReader) {
     if (!data) {
         return nullptr;
     }
 
-    return NewFromStream(new SkMemoryStream(data), chunkReader);
+    return MakeFromStream(skstd::make_unique<SkMemoryStream>(std::move(data)), chunkReader);
 }
 
 SkColorType SkAndroidCodec::computeOutputColorType(SkColorType requestedColorType) {
diff --git a/src/codec/SkBmpBaseCodec.cpp b/src/codec/SkBmpBaseCodec.cpp
index 0e74435..1071ff3 100644
--- a/src/codec/SkBmpBaseCodec.cpp
+++ b/src/codec/SkBmpBaseCodec.cpp
@@ -9,8 +9,9 @@
 
 SkBmpBaseCodec::~SkBmpBaseCodec() {}
 
-SkBmpBaseCodec::SkBmpBaseCodec(int width, int height, const SkEncodedInfo& info, SkStream* stream,
+SkBmpBaseCodec::SkBmpBaseCodec(int width, int height, const SkEncodedInfo& info,
+                               std::unique_ptr<SkStream> stream,
                                uint16_t bitsPerPixel, SkCodec::SkScanlineOrder rowOrder)
-    : INHERITED(width, height, info, stream, bitsPerPixel, rowOrder)
+    : INHERITED(width, height, info, std::move(stream), bitsPerPixel, rowOrder)
     , fSrcBuffer(sk_malloc_flags(this->srcRowBytes(), 0))
 {}
diff --git a/src/codec/SkBmpBaseCodec.h b/src/codec/SkBmpBaseCodec.h
index 0b9f919..24277fc 100644
--- a/src/codec/SkBmpBaseCodec.h
+++ b/src/codec/SkBmpBaseCodec.h
@@ -25,7 +25,7 @@
     bool didCreateSrcBuffer() const { return fSrcBuffer != nullptr; }
 
 protected:
-    SkBmpBaseCodec(int width, int height, const SkEncodedInfo& info, SkStream* stream,
+    SkBmpBaseCodec(int width, int height, const SkEncodedInfo& info, std::unique_ptr<SkStream>,
                    uint16_t bitsPerPixel, SkCodec::SkScanlineOrder rowOrder);
 
     uint8_t* srcBuffer() { return reinterpret_cast<uint8_t*>(fSrcBuffer.get()); }
diff --git a/src/codec/SkBmpCodec.cpp b/src/codec/SkBmpCodec.cpp
index 70397f6..18f8768 100644
--- a/src/codec/SkBmpCodec.cpp
+++ b/src/codec/SkBmpCodec.cpp
@@ -67,16 +67,17 @@
  * Creates a bmp decoder
  * Reads enough of the stream to determine the image format
  */
-SkCodec* SkBmpCodec::NewFromStream(SkStream* stream, Result* result) {
-    return SkBmpCodec::NewFromStream(stream, result, false);
+std::unique_ptr<SkCodec> SkBmpCodec::MakeFromStream(std::unique_ptr<SkStream> stream,
+                                                    Result* result) {
+    return SkBmpCodec::MakeFromStream(std::move(stream), result, false);
 }
 
 /*
  * Creates a bmp decoder for a bmp embedded in ico
  * Reads enough of the stream to determine the image format
  */
-SkCodec* SkBmpCodec::NewFromIco(SkStream* stream, Result* result) {
-    return SkBmpCodec::NewFromStream(stream, result, true);
+std::unique_ptr<SkCodec> SkBmpCodec::MakeFromIco(std::unique_ptr<SkStream> stream, Result* result) {
+    return SkBmpCodec::MakeFromStream(std::move(stream), result, true);
 }
 
 // Header size constants
@@ -476,9 +477,11 @@
 
                 // Set the image info and create a codec.
                 const SkEncodedInfo info = SkEncodedInfo::Make(color, alpha, bitsPerComponent);
-                codecOut->reset(new SkBmpStandardCodec(width, height, info, stream, bitsPerPixel,
-                                                       numColors, bytesPerColor, offset - bytesRead,
-                                                       rowOrder, isOpaque, inIco));
+                codecOut->reset(new SkBmpStandardCodec(width, height, info,
+                                                       std::unique_ptr<SkStream>(stream),
+                                                       bitsPerPixel, numColors, bytesPerColor,
+                                                       offset - bytesRead, rowOrder, isOpaque,
+                                                       inIco));
                 return static_cast<SkBmpStandardCodec*>(codecOut->get())->didCreateSrcBuffer()
                         ? kSuccess : kInvalidInput;
             }
@@ -532,7 +535,8 @@
                     alpha = SkEncodedInfo::kOpaque_Alpha;
                 }
                 const SkEncodedInfo info = SkEncodedInfo::Make(color, alpha, 8);
-                codecOut->reset(new SkBmpMaskCodec(width, height, info, stream, bitsPerPixel,
+                codecOut->reset(new SkBmpMaskCodec(width, height, info,
+                                                   std::unique_ptr<SkStream>(stream), bitsPerPixel,
                                                    masks.release(), rowOrder));
                 return static_cast<SkBmpMaskCodec*>(codecOut->get())->didCreateSrcBuffer()
                         ? kSuccess : kInvalidInput;
@@ -563,7 +567,8 @@
                 // For that reason, we always indicate that we are kBGRA.
                 const SkEncodedInfo info = SkEncodedInfo::Make(SkEncodedInfo::kBGRA_Color,
                         SkEncodedInfo::kBinary_Alpha, 8);
-                codecOut->reset(new SkBmpRLECodec(width, height, info, stream, bitsPerPixel,
+                codecOut->reset(new SkBmpRLECodec(width, height, info,
+                                                  std::unique_ptr<SkStream>(stream), bitsPerPixel,
                                                   numColors, bytesPerColor, offset - bytesRead,
                                                   rowOrder));
             }
@@ -579,21 +584,22 @@
  * Creates a bmp decoder
  * Reads enough of the stream to determine the image format
  */
-SkCodec* SkBmpCodec::NewFromStream(SkStream* stream, Result* result, bool inIco) {
-    std::unique_ptr<SkStream> streamDeleter(stream);
+std::unique_ptr<SkCodec> SkBmpCodec::MakeFromStream(std::unique_ptr<SkStream> stream,
+                                                    Result* result, bool inIco) {
     std::unique_ptr<SkCodec> codec;
-    *result = ReadHeader(stream, inIco, &codec);
+    *result = ReadHeader(stream.get(), inIco, &codec);
     if (codec) {
-        // codec has taken ownership of stream, so we do not need to
-        // delete it.
-        streamDeleter.release();
+        // codec has taken ownership of stream, so we do not need to delete it.
+        stream.release();
     }
-    return kSuccess == *result ? codec.release() : nullptr;
+    return kSuccess == *result ? std::move(codec) : nullptr;
 }
 
-SkBmpCodec::SkBmpCodec(int width, int height, const SkEncodedInfo& info, SkStream* stream,
+SkBmpCodec::SkBmpCodec(int width, int height, const SkEncodedInfo& info,
+                       std::unique_ptr<SkStream> stream,
         uint16_t bitsPerPixel, SkCodec::SkScanlineOrder rowOrder)
-    : INHERITED(width, height, info, kXformSrcColorFormat, stream, SkColorSpace::MakeSRGB())
+    : INHERITED(width, height, info, kXformSrcColorFormat, std::move(stream),
+                SkColorSpace::MakeSRGB())
     , fBitsPerPixel(bitsPerPixel)
     , fRowOrder(rowOrder)
     , fSrcRowBytes(SkAlign4(compute_row_bytes(width, fBitsPerPixel)))
diff --git a/src/codec/SkBmpCodec.h b/src/codec/SkBmpCodec.h
index d5f5a32..651f1be 100644
--- a/src/codec/SkBmpCodec.h
+++ b/src/codec/SkBmpCodec.h
@@ -28,17 +28,17 @@
      * Creates a bmp decoder
      * Reads enough of the stream to determine the image format
      */
-    static SkCodec* NewFromStream(SkStream*, Result*);
+    static std::unique_ptr<SkCodec> MakeFromStream(std::unique_ptr<SkStream>, Result*);
 
     /*
      * Creates a bmp decoder for a bmp embedded in ico
      * Reads enough of the stream to determine the image format
      */
-    static SkCodec* NewFromIco(SkStream*, Result*);
+    static std::unique_ptr<SkCodec> MakeFromIco(std::unique_ptr<SkStream>, Result*);
 
 protected:
 
-    SkBmpCodec(int width, int height, const SkEncodedInfo& info, SkStream* stream,
+    SkBmpCodec(int width, int height, const SkEncodedInfo& info, std::unique_ptr<SkStream>,
             uint16_t bitsPerPixel, SkCodec::SkScanlineOrder rowOrder);
 
     SkEncodedImageFormat onGetEncodedFormat() const override { return SkEncodedImageFormat::kBMP; }
@@ -46,7 +46,6 @@
     /*
      * Read enough of the stream to initialize the SkBmpCodec.
      * On kSuccess, if codecOut is not nullptr, it will be set to a new SkBmpCodec.
-     * If an SkCodec is created, it will take ownership of the SkStream.
      */
     static Result ReadHeader(SkStream*, bool inIco, std::unique_ptr<SkCodec>* codecOut);
 
@@ -112,7 +111,7 @@
      * Creates a bmp decoder
      * Reads enough of the stream to determine the image format
      */
-    static SkCodec* NewFromStream(SkStream*, Result*, bool inIco);
+    static std::unique_ptr<SkCodec> MakeFromStream(std::unique_ptr<SkStream>, Result*, bool inIco);
 
     /*
      * Decodes the next dstInfo.height() lines.
diff --git a/src/codec/SkBmpMaskCodec.cpp b/src/codec/SkBmpMaskCodec.cpp
index 51060e5..aec56ee 100644
--- a/src/codec/SkBmpMaskCodec.cpp
+++ b/src/codec/SkBmpMaskCodec.cpp
@@ -12,10 +12,11 @@
 /*
  * Creates an instance of the decoder
  */
-SkBmpMaskCodec::SkBmpMaskCodec(int width, int height, const SkEncodedInfo& info, SkStream* stream,
+SkBmpMaskCodec::SkBmpMaskCodec(int width, int height, const SkEncodedInfo& info,
+                               std::unique_ptr<SkStream> stream,
                                uint16_t bitsPerPixel, SkMasks* masks,
                                SkCodec::SkScanlineOrder rowOrder)
-    : INHERITED(width, height, info, stream, bitsPerPixel, rowOrder)
+    : INHERITED(width, height, info, std::move(stream), bitsPerPixel, rowOrder)
     , fMasks(masks)
     , fMaskSwizzler(nullptr)
 {}
diff --git a/src/codec/SkBmpMaskCodec.h b/src/codec/SkBmpMaskCodec.h
index 8d8d64c..4b0dbde 100644
--- a/src/codec/SkBmpMaskCodec.h
+++ b/src/codec/SkBmpMaskCodec.h
@@ -22,7 +22,7 @@
     /*
      * Creates an instance of the decoder
      *
-     * Called only by SkBmpCodec::NewFromStream
+     * Called only by SkBmpCodec::MakeFromStream
      * There should be no other callers despite this being public
      *
      * @param info contains properties of the encoded data
@@ -31,7 +31,7 @@
      * @param masks color masks for certain bmp formats
      * @param rowOrder indicates whether rows are ordered top-down or bottom-up
      */
-    SkBmpMaskCodec(int width, int height, const SkEncodedInfo& info, SkStream* stream,
+    SkBmpMaskCodec(int width, int height, const SkEncodedInfo& info, std::unique_ptr<SkStream>,
             uint16_t bitsPerPixel, SkMasks* masks,
             SkCodec::SkScanlineOrder rowOrder);
 
diff --git a/src/codec/SkBmpRLECodec.cpp b/src/codec/SkBmpRLECodec.cpp
index 0fcd290..23a2c98 100644
--- a/src/codec/SkBmpRLECodec.cpp
+++ b/src/codec/SkBmpRLECodec.cpp
@@ -14,11 +14,12 @@
  * Creates an instance of the decoder
  * Called only by NewFromStream
  */
-SkBmpRLECodec::SkBmpRLECodec(int width, int height, const SkEncodedInfo& info, SkStream* stream,
+SkBmpRLECodec::SkBmpRLECodec(int width, int height, const SkEncodedInfo& info,
+                             std::unique_ptr<SkStream> stream,
                              uint16_t bitsPerPixel, uint32_t numColors,
                              uint32_t bytesPerColor, uint32_t offset,
                              SkCodec::SkScanlineOrder rowOrder)
-    : INHERITED(width, height, info, stream, bitsPerPixel, rowOrder)
+    : INHERITED(width, height, info, std::move(stream), bitsPerPixel, rowOrder)
     , fColorTable(nullptr)
     , fNumColors(numColors)
     , fBytesPerColor(bytesPerColor)
diff --git a/src/codec/SkBmpRLECodec.h b/src/codec/SkBmpRLECodec.h
index d6c17c3..70e97a7 100644
--- a/src/codec/SkBmpRLECodec.h
+++ b/src/codec/SkBmpRLECodec.h
@@ -22,7 +22,7 @@
     /*
      * Creates an instance of the decoder
      *
-     * Called only by SkBmpCodec::NewFromStream
+     * Called only by SkBmpCodec::MakeFromStream
      * There should be no other callers despite this being public
      *
      * @param info contains properties of the encoded data
@@ -35,7 +35,7 @@
      *               headers
      * @param rowOrder indicates whether rows are ordered top-down or bottom-up
      */
-    SkBmpRLECodec(int width, int height, const SkEncodedInfo& info, SkStream* stream,
+    SkBmpRLECodec(int width, int height, const SkEncodedInfo& info, std::unique_ptr<SkStream>,
             uint16_t bitsPerPixel, uint32_t numColors, uint32_t bytesPerColor,
             uint32_t offset, SkCodec::SkScanlineOrder rowOrder);
 
diff --git a/src/codec/SkBmpStandardCodec.cpp b/src/codec/SkBmpStandardCodec.cpp
index 46a5715..53aa91c 100644
--- a/src/codec/SkBmpStandardCodec.cpp
+++ b/src/codec/SkBmpStandardCodec.cpp
@@ -15,11 +15,11 @@
  * Called only by NewFromStream
  */
 SkBmpStandardCodec::SkBmpStandardCodec(int width, int height, const SkEncodedInfo& info,
-                                       SkStream* stream, uint16_t bitsPerPixel, uint32_t numColors,
-                                       uint32_t bytesPerColor, uint32_t offset,
+                                       std::unique_ptr<SkStream> stream, uint16_t bitsPerPixel,
+                                       uint32_t numColors, uint32_t bytesPerColor, uint32_t offset,
                                        SkCodec::SkScanlineOrder rowOrder,
                                        bool isOpaque, bool inIco)
-    : INHERITED(width, height, info, stream, bitsPerPixel, rowOrder)
+    : INHERITED(width, height, info, std::move(stream), bitsPerPixel, rowOrder)
     , fColorTable(nullptr)
     , fNumColors(numColors)
     , fBytesPerColor(bytesPerColor)
diff --git a/src/codec/SkBmpStandardCodec.h b/src/codec/SkBmpStandardCodec.h
index f9ce5c6..60587fb 100644
--- a/src/codec/SkBmpStandardCodec.h
+++ b/src/codec/SkBmpStandardCodec.h
@@ -23,7 +23,7 @@
     /*
      * Creates an instance of the decoder
      *
-     * Called only by SkBmpCodec::NewFromStream
+     * Called only by SkBmpCodec::MakeFromStream
      * There should be no other callers despite this being public
      *
      * @param info contains properties of the encoded data
@@ -39,10 +39,10 @@
      *                 the icp mask, if there is one)
      * @param inIco    indicates if the bmp is embedded in an ico file
      */
-    SkBmpStandardCodec(int width, int height, const SkEncodedInfo& info, SkStream* stream,
-            uint16_t bitsPerPixel, uint32_t numColors, uint32_t bytesPerColor,
-            uint32_t offset, SkCodec::SkScanlineOrder rowOrder, bool isOpaque,
-            bool inIco);
+    SkBmpStandardCodec(int width, int height, const SkEncodedInfo& info,
+                       std::unique_ptr<SkStream> stream, uint16_t bitsPerPixel, uint32_t numColors,
+                       uint32_t bytesPerColor, uint32_t offset, SkCodec::SkScanlineOrder rowOrder,
+                       bool isOpaque, bool inIco);
 
 protected:
 
diff --git a/src/codec/SkCodec.cpp b/src/codec/SkCodec.cpp
index 872fe2b..4fb0ea9 100644
--- a/src/codec/SkCodec.cpp
+++ b/src/codec/SkCodec.cpp
@@ -26,30 +26,30 @@
 
 struct DecoderProc {
     bool (*IsFormat)(const void*, size_t);
-    SkCodec* (*NewFromStream)(SkStream*, SkCodec::Result*);
+    std::unique_ptr<SkCodec> (*MakeFromStream)(std::unique_ptr<SkStream>, SkCodec::Result*);
 };
 
 static const DecoderProc gDecoderProcs[] = {
 #ifdef SK_HAS_JPEG_LIBRARY
-    { SkJpegCodec::IsJpeg, SkJpegCodec::NewFromStream },
+    { SkJpegCodec::IsJpeg, SkJpegCodec::MakeFromStream },
 #endif
 #ifdef SK_HAS_WEBP_LIBRARY
-    { SkWebpCodec::IsWebp, SkWebpCodec::NewFromStream },
+    { SkWebpCodec::IsWebp, SkWebpCodec::MakeFromStream },
 #endif
-    { SkGifCodec::IsGif, SkGifCodec::NewFromStream },
+    { SkGifCodec::IsGif, SkGifCodec::MakeFromStream },
 #ifdef SK_HAS_PNG_LIBRARY
-    { SkIcoCodec::IsIco, SkIcoCodec::NewFromStream },
+    { SkIcoCodec::IsIco, SkIcoCodec::MakeFromStream },
 #endif
-    { SkBmpCodec::IsBmp, SkBmpCodec::NewFromStream },
-    { SkWbmpCodec::IsWbmp, SkWbmpCodec::NewFromStream }
+    { SkBmpCodec::IsBmp, SkBmpCodec::MakeFromStream },
+    { SkWbmpCodec::IsWbmp, SkWbmpCodec::MakeFromStream }
 };
 
 size_t SkCodec::MinBufferedBytesNeeded() {
     return WEBP_VP8_HEADER_SIZE;
 }
 
-SkCodec* SkCodec::NewFromStream(SkStream* stream,
-        Result* outResult, SkPngChunkReader* chunkReader) {
+std::unique_ptr<SkCodec> SkCodec::MakeFromStream(std::unique_ptr<SkStream> stream,
+                                                 Result* outResult, SkPngChunkReader* chunkReader) {
     Result resultStorage;
     if (!outResult) {
         outResult = &resultStorage;
@@ -60,8 +60,6 @@
         return nullptr;
     }
 
-    std::unique_ptr<SkStream> streamDeleter(stream);
-
     // 14 is enough to read all of the supported types.
     const size_t bytesToRead = 14;
     SkASSERT(bytesToRead <= MinBufferedBytesNeeded());
@@ -96,19 +94,19 @@
     // But this code follows the same pattern as the loop.
 #ifdef SK_HAS_PNG_LIBRARY
     if (SkPngCodec::IsPng(buffer, bytesRead)) {
-        return SkPngCodec::NewFromStream(streamDeleter.release(), outResult, chunkReader);
+        return SkPngCodec::MakeFromStream(std::move(stream), outResult, chunkReader);
     } else
 #endif
     {
         for (DecoderProc proc : gDecoderProcs) {
             if (proc.IsFormat(buffer, bytesRead)) {
-                return proc.NewFromStream(streamDeleter.release(), outResult);
+                return proc.MakeFromStream(std::move(stream), outResult);
             }
         }
 
 #ifdef SK_CODEC_DECODES_RAW
         // Try to treat the input as RAW if all the other checks failed.
-        return SkRawCodec::NewFromStream(streamDeleter.release(), outResult);
+        return SkRawCodec::MakeFromStream(std::move(stream), outResult);
 #endif
     }
 
@@ -121,20 +119,21 @@
     return nullptr;
 }
 
-SkCodec* SkCodec::NewFromData(sk_sp<SkData> data, SkPngChunkReader* reader) {
+std::unique_ptr<SkCodec> SkCodec::MakeFromData(sk_sp<SkData> data, SkPngChunkReader* reader) {
     if (!data) {
         return nullptr;
     }
-    return NewFromStream(new SkMemoryStream(data), nullptr, reader);
+    return MakeFromStream(std::unique_ptr<SkStream>(new SkMemoryStream(std::move(data))),
+                                                    nullptr, reader);
 }
 
 SkCodec::SkCodec(int width, int height, const SkEncodedInfo& info,
-        XformFormat srcFormat, SkStream* stream,
+        XformFormat srcFormat, std::unique_ptr<SkStream> stream,
         sk_sp<SkColorSpace> colorSpace, Origin origin)
     : fEncodedInfo(info)
     , fSrcInfo(info.makeImageInfo(width, height, std::move(colorSpace)))
     , fSrcXformFormat(srcFormat)
-    , fStream(stream)
+    , fStream(std::move(stream))
     , fNeedsRewind(false)
     , fOrigin(origin)
     , fDstInfo()
@@ -143,11 +142,11 @@
 {}
 
 SkCodec::SkCodec(const SkEncodedInfo& info, const SkImageInfo& imageInfo,
-        XformFormat srcFormat, SkStream* stream, Origin origin)
+        XformFormat srcFormat, std::unique_ptr<SkStream> stream, Origin origin)
     : fEncodedInfo(info)
     , fSrcInfo(imageInfo)
     , fSrcXformFormat(srcFormat)
-    , fStream(stream)
+    , fStream(std::move(stream))
     , fNeedsRewind(false)
     , fOrigin(origin)
     , fDstInfo()
@@ -619,3 +618,12 @@
     }
     return result;
 }
+
+#ifdef SK_SUPPORT_LEGACY_CODEC_NEW
+SkCodec* SkCodec::NewFromStream(SkStream* str, Result* res, SkPngChunkReader* chunk) {
+    return MakeFromStream(std::unique_ptr<SkStream*>(str), res, chunk).release();
+}
+SkCodec* SkCodec::NewFromData(sk_sp<SkData> data, SkPngChunkReader* chunk) {
+    return MakeFromData(std::move(data), chunk).release();
+}
+#endif
diff --git a/src/codec/SkCodecImageGenerator.cpp b/src/codec/SkCodecImageGenerator.cpp
index 7be6277..e6ca212 100644
--- a/src/codec/SkCodecImageGenerator.cpp
+++ b/src/codec/SkCodecImageGenerator.cpp
@@ -9,12 +9,12 @@
 #include "SkMakeUnique.h"
 
 std::unique_ptr<SkImageGenerator> SkCodecImageGenerator::MakeFromEncodedCodec(sk_sp<SkData> data) {
-    SkCodec* codec = SkCodec::NewFromData(data);
+    auto codec = SkCodec::MakeFromData(data);
     if (nullptr == codec) {
         return nullptr;
     }
 
-    return std::unique_ptr<SkImageGenerator>(new SkCodecImageGenerator(codec, data));
+    return std::unique_ptr<SkImageGenerator>(new SkCodecImageGenerator(std::move(codec), data));
 }
 
 static SkImageInfo adjust_info(const SkImageInfo& info) {
@@ -25,9 +25,9 @@
     return newInfo;
 }
 
-SkCodecImageGenerator::SkCodecImageGenerator(SkCodec* codec, sk_sp<SkData> data)
+SkCodecImageGenerator::SkCodecImageGenerator(std::unique_ptr<SkCodec> codec, sk_sp<SkData> data)
     : INHERITED(adjust_info(codec->getInfo()))
-    , fCodec(codec)
+    , fCodec(std::move(codec))
     , fData(std::move(data))
 {}
 
diff --git a/src/codec/SkCodecImageGenerator.h b/src/codec/SkCodecImageGenerator.h
index 4d0c078..1b2cbc2 100644
--- a/src/codec/SkCodecImageGenerator.h
+++ b/src/codec/SkCodecImageGenerator.h
@@ -34,7 +34,7 @@
     /*
      * Takes ownership of codec
      */
-    SkCodecImageGenerator(SkCodec* codec, sk_sp<SkData>);
+    SkCodecImageGenerator(std::unique_ptr<SkCodec>, sk_sp<SkData>);
 
     std::unique_ptr<SkCodec> fCodec;
     sk_sp<SkData> fData;
diff --git a/src/codec/SkGifCodec.cpp b/src/codec/SkGifCodec.cpp
index 412b8ff..d54cbd5 100644
--- a/src/codec/SkGifCodec.cpp
+++ b/src/codec/SkGifCodec.cpp
@@ -35,6 +35,7 @@
 #include "SkColorPriv.h"
 #include "SkColorTable.h"
 #include "SkGifCodec.h"
+#include "SkMakeUnique.h"
 #include "SkRasterPipeline.h"
 #include "SkStream.h"
 #include "SkSwizzler.h"
@@ -68,8 +69,9 @@
     return result;
 }
 
-SkCodec* SkGifCodec::NewFromStream(SkStream* stream, Result* result) {
-    std::unique_ptr<SkGifImageReader> reader(new SkGifImageReader(stream));
+std::unique_ptr<SkCodec> SkGifCodec::MakeFromStream(std::unique_ptr<SkStream> stream,
+                                                    Result* result) {
+    std::unique_ptr<SkGifImageReader> reader(new SkGifImageReader(std::move(stream)));
     *result = reader->parse(SkGifImageReader::SkGIFSizeQuery);
     if (*result != kSuccess) {
         return nullptr;
@@ -102,7 +104,7 @@
     const auto imageInfo = SkImageInfo::Make(reader->screenWidth(), reader->screenHeight(),
                                              kN32_SkColorType, alphaType,
                                              SkColorSpace::MakeSRGB());
-    return new SkGifCodec(encodedInfo, imageInfo, reader.release());
+    return std::unique_ptr<SkCodec>(new SkGifCodec(encodedInfo, imageInfo, reader.release()));
 }
 
 bool SkGifCodec::onRewind() {
diff --git a/src/codec/SkGifCodec.h b/src/codec/SkGifCodec.h
index 6ecd419..89c84bd 100644
--- a/src/codec/SkGifCodec.h
+++ b/src/codec/SkGifCodec.h
@@ -29,7 +29,7 @@
      * Assumes IsGif was called and returned true
      * Reads enough of the stream to determine the image format
      */
-    static SkCodec* NewFromStream(SkStream*, Result*);
+    static std::unique_ptr<SkCodec> MakeFromStream(std::unique_ptr<SkStream>, Result*);
 
     // Callback for SkGifImageReader when a row is available.
     bool haveDecodedRow(int frameIndex, const unsigned char* rowBegin,
diff --git a/src/codec/SkIcoCodec.cpp b/src/codec/SkIcoCodec.cpp
index 2e61bfe..fce14a2 100644
--- a/src/codec/SkIcoCodec.cpp
+++ b/src/codec/SkIcoCodec.cpp
@@ -26,18 +26,15 @@
             !memcmp(buffer, curSig, sizeof(curSig)));
 }
 
-SkCodec* SkIcoCodec::NewFromStream(SkStream* stream, Result* result) {
-    // Ensure that we do not leak the input stream
-    std::unique_ptr<SkStream> inputStream(stream);
-
+std::unique_ptr<SkCodec> SkIcoCodec::MakeFromStream(std::unique_ptr<SkStream> stream,
+                                                    Result* result) {
     // Header size constants
     static const uint32_t kIcoDirectoryBytes = 6;
     static const uint32_t kIcoDirEntryBytes = 16;
 
     // Read the directory header
     std::unique_ptr<uint8_t[]> dirBuffer(new uint8_t[kIcoDirectoryBytes]);
-    if (inputStream.get()->read(dirBuffer.get(), kIcoDirectoryBytes) !=
-            kIcoDirectoryBytes) {
+    if (stream->read(dirBuffer.get(), kIcoDirectoryBytes) != kIcoDirectoryBytes) {
         SkCodecPrintf("Error: unable to read ico directory header.\n");
         *result = kIncompleteInput;
         return nullptr;
@@ -71,8 +68,7 @@
     // Iterate over directory entries
     for (uint32_t i = 0; i < numImages; i++) {
         uint8_t entryBuffer[kIcoDirEntryBytes];
-        if (inputStream->read(entryBuffer, kIcoDirEntryBytes) !=
-                kIcoDirEntryBytes) {
+        if (stream->read(entryBuffer, kIcoDirEntryBytes) != kIcoDirEntryBytes) {
             SkCodecPrintf("Error: Dir entries truncated in ico.\n");
             *result = kIncompleteInput;
             return nullptr;
@@ -128,7 +124,7 @@
 
         // If we cannot skip, assume we have reached the end of the stream and
         // stop trying to make codecs
-        if (inputStream.get()->skip(offset - bytesRead) != offset - bytesRead) {
+        if (stream->skip(offset - bytesRead) != offset - bytesRead) {
             SkCodecPrintf("Warning: could not skip to ico offset.\n");
             break;
         }
@@ -141,7 +137,7 @@
             break;
         }
 
-        if (inputStream->read(buffer.get(), size) != size) {
+        if (stream->read(buffer.get(), size) != size) {
             SkCodecPrintf("Warning: could not create embedded stream.\n");
             *result = kIncompleteInput;
             break;
@@ -152,17 +148,17 @@
         bytesRead += size;
 
         // Check if the embedded codec is bmp or png and create the codec
-        SkCodec* codec = nullptr;
+        std::unique_ptr<SkCodec> codec;
         Result dummyResult;
         if (SkPngCodec::IsPng((const char*) data->bytes(), data->size())) {
-            codec = SkPngCodec::NewFromStream(embeddedStream.release(), &dummyResult);
+            codec = SkPngCodec::MakeFromStream(std::move(embeddedStream), &dummyResult);
         } else {
-            codec = SkBmpCodec::NewFromIco(embeddedStream.release(), &dummyResult);
+            codec = SkBmpCodec::MakeFromIco(std::move(embeddedStream), &dummyResult);
         }
 
         // Save a valid codec
         if (nullptr != codec) {
-            codecs->push_back().reset(codec);
+            codecs->push_back().reset(codec.release());
         }
     }
 
@@ -192,7 +188,8 @@
     *result = kSuccess;
     // The original stream is no longer needed, because the embedded codecs own their
     // own streams.
-    return new SkIcoCodec(width, height, info, codecs.release(), sk_ref_sp(colorSpace));
+    return std::unique_ptr<SkCodec>(new SkIcoCodec(width, height, info, codecs.release(),
+                                                   sk_ref_sp(colorSpace)));
 }
 
 /*
diff --git a/src/codec/SkIcoCodec.h b/src/codec/SkIcoCodec.h
index 0c27934..aa19c9b 100644
--- a/src/codec/SkIcoCodec.h
+++ b/src/codec/SkIcoCodec.h
@@ -25,7 +25,7 @@
      * Creates an Ico decoder
      * Reads enough of the stream to determine the image format
      */
-    static SkCodec* NewFromStream(SkStream*, Result*);
+    static std::unique_ptr<SkCodec> MakeFromStream(std::unique_ptr<SkStream>, Result*);
 
 protected:
 
diff --git a/src/codec/SkJpegCodec.cpp b/src/codec/SkJpegCodec.cpp
index f3dbdf1..8f88138 100644
--- a/src/codec/SkJpegCodec.cpp
+++ b/src/codec/SkJpegCodec.cpp
@@ -252,8 +252,9 @@
 
         const int width = decoderMgr->dinfo()->image_width;
         const int height = decoderMgr->dinfo()->image_height;
-        SkJpegCodec* codec = new SkJpegCodec(width, height, info, stream, decoderMgr.release(),
-                                             std::move(colorSpace), orientation);
+        SkJpegCodec* codec = new SkJpegCodec(width, height, info, std::unique_ptr<SkStream>(stream),
+                                             decoderMgr.release(), std::move(colorSpace),
+                                             orientation);
         *codecOut = codec;
     } else {
         SkASSERT(nullptr != decoderMgrOut);
@@ -262,27 +263,29 @@
     return kSuccess;
 }
 
-SkCodec* SkJpegCodec::NewFromStream(SkStream* stream, Result* result) {
-    return SkJpegCodec::NewFromStream(stream, result, SkColorSpace::MakeSRGB());
+std::unique_ptr<SkCodec> SkJpegCodec::MakeFromStream(std::unique_ptr<SkStream> stream,
+                                                     Result* result) {
+    return SkJpegCodec::MakeFromStream(std::move(stream), result, SkColorSpace::MakeSRGB());
 }
 
-SkCodec* SkJpegCodec::NewFromStream(SkStream* stream, Result* result,
+std::unique_ptr<SkCodec> SkJpegCodec::MakeFromStream(std::unique_ptr<SkStream> stream,
+                                                     Result* result,
                                     sk_sp<SkColorSpace> defaultColorSpace) {
-    std::unique_ptr<SkStream> streamDeleter(stream);
     SkCodec* codec = nullptr;
-    *result = ReadHeader(stream, &codec, nullptr, std::move(defaultColorSpace));
+    *result = ReadHeader(stream.get(), &codec, nullptr, std::move(defaultColorSpace));
     if (kSuccess == *result) {
         // Codec has taken ownership of the stream, we do not need to delete it
         SkASSERT(codec);
-        streamDeleter.release();
-        return codec;
+        stream.release();
+        return std::unique_ptr<SkCodec>(codec);
     }
     return nullptr;
 }
 
-SkJpegCodec::SkJpegCodec(int width, int height, const SkEncodedInfo& info, SkStream* stream,
-        JpegDecoderMgr* decoderMgr, sk_sp<SkColorSpace> colorSpace, Origin origin)
-    : INHERITED(width, height, info, SkColorSpaceXform::kRGBA_8888_ColorFormat, stream,
+SkJpegCodec::SkJpegCodec(int width, int height, const SkEncodedInfo& info,
+                         std::unique_ptr<SkStream> stream, JpegDecoderMgr* decoderMgr,
+                         sk_sp<SkColorSpace> colorSpace, Origin origin)
+    : INHERITED(width, height, info, SkColorSpaceXform::kRGBA_8888_ColorFormat, std::move(stream),
                 std::move(colorSpace), origin)
     , fDecoderMgr(decoderMgr)
     , fReadyState(decoderMgr->dinfo()->global_state)
diff --git a/src/codec/SkJpegCodec.h b/src/codec/SkJpegCodec.h
index 96228f3..1409182 100644
--- a/src/codec/SkJpegCodec.h
+++ b/src/codec/SkJpegCodec.h
@@ -31,7 +31,7 @@
      * Assumes IsJpeg was called and returned true
      * Takes ownership of the stream
      */
-    static SkCodec* NewFromStream(SkStream*, Result*);
+    static std::unique_ptr<SkCodec> MakeFromStream(std::unique_ptr<SkStream>, Result*);
 
 protected:
 
@@ -63,7 +63,8 @@
     /*
      * Allows SkRawCodec to communicate the color space from the exif data.
      */
-    static SkCodec* NewFromStream(SkStream*, Result*, sk_sp<SkColorSpace> defaultColorSpace);
+    static std::unique_ptr<SkCodec> MakeFromStream(std::unique_ptr<SkStream>, Result*,
+                                                   sk_sp<SkColorSpace> defaultColorSpace);
 
     /*
      * Read enough of the stream to initialize the SkJpegCodec.
@@ -99,7 +100,7 @@
      * @param decoderMgr holds decompress struct, src manager, and error manager
      *                   takes ownership
      */
-    SkJpegCodec(int width, int height, const SkEncodedInfo& info, SkStream* stream,
+    SkJpegCodec(int width, int height, const SkEncodedInfo& info, std::unique_ptr<SkStream> stream,
             JpegDecoderMgr* decoderMgr, sk_sp<SkColorSpace> colorSpace, Origin origin);
 
     /*
diff --git a/src/codec/SkPngCodec.cpp b/src/codec/SkPngCodec.cpp
index 560b5bf..13e45ab 100644
--- a/src/codec/SkPngCodec.cpp
+++ b/src/codec/SkPngCodec.cpp
@@ -480,9 +480,10 @@
 
 class SkPngNormalDecoder : public SkPngCodec {
 public:
-    SkPngNormalDecoder(const SkEncodedInfo& info, const SkImageInfo& imageInfo, SkStream* stream,
-            SkPngChunkReader* reader, png_structp png_ptr, png_infop info_ptr, int bitDepth)
-        : INHERITED(info, imageInfo, stream, reader, png_ptr, info_ptr, bitDepth)
+    SkPngNormalDecoder(const SkEncodedInfo& info, const SkImageInfo& imageInfo,
+                       std::unique_ptr<SkStream> stream, SkPngChunkReader* reader,
+                       png_structp png_ptr, png_infop info_ptr, int bitDepth)
+        : INHERITED(info, imageInfo, std::move(stream), reader, png_ptr, info_ptr, bitDepth)
         , fRowsWrittenToOutput(0)
         , fDst(nullptr)
         , fRowBytes(0)
@@ -598,9 +599,9 @@
 class SkPngInterlacedDecoder : public SkPngCodec {
 public:
     SkPngInterlacedDecoder(const SkEncodedInfo& info, const SkImageInfo& imageInfo,
-            SkStream* stream, SkPngChunkReader* reader, png_structp png_ptr, png_infop info_ptr,
-            int bitDepth, int numberPasses)
-        : INHERITED(info, imageInfo, stream, reader, png_ptr, info_ptr, bitDepth)
+            std::unique_ptr<SkStream> stream, SkPngChunkReader* reader, png_structp png_ptr,
+            png_infop info_ptr, int bitDepth, int numberPasses)
+        : INHERITED(info, imageInfo, std::move(stream), reader, png_ptr, info_ptr, bitDepth)
         , fNumberPasses(numberPasses)
         , fFirstRow(0)
         , fLastRow(0)
@@ -922,11 +923,12 @@
         }
 
         if (1 == numberPasses) {
-            *fOutCodec = new SkPngNormalDecoder(encodedInfo, imageInfo, fStream,
-                    fChunkReader, fPng_ptr, fInfo_ptr, bitDepth);
+            *fOutCodec = new SkPngNormalDecoder(encodedInfo, imageInfo,
+                   std::unique_ptr<SkStream>(fStream), fChunkReader, fPng_ptr, fInfo_ptr, bitDepth);
         } else {
-            *fOutCodec = new SkPngInterlacedDecoder(encodedInfo, imageInfo, fStream,
-                    fChunkReader, fPng_ptr, fInfo_ptr, bitDepth, numberPasses);
+            *fOutCodec = new SkPngInterlacedDecoder(encodedInfo, imageInfo,
+                    std::unique_ptr<SkStream>(fStream), fChunkReader, fPng_ptr, fInfo_ptr, bitDepth,
+                    numberPasses);
         }
         static_cast<SkPngCodec*>(*fOutCodec)->setIdatLength(idatLength);
     }
@@ -937,9 +939,9 @@
 }
 
 SkPngCodec::SkPngCodec(const SkEncodedInfo& encodedInfo, const SkImageInfo& imageInfo,
-                       SkStream* stream, SkPngChunkReader* chunkReader, void* png_ptr,
-                       void* info_ptr, int bitDepth)
-    : INHERITED(encodedInfo, imageInfo, png_select_xform_format(encodedInfo), stream)
+                       std::unique_ptr<SkStream> stream, SkPngChunkReader* chunkReader,
+                       void* png_ptr, void* info_ptr, int bitDepth)
+    : INHERITED(encodedInfo, imageInfo, png_select_xform_format(encodedInfo), std::move(stream))
     , fPngChunkReader(SkSafeRef(chunkReader))
     , fPng_ptr(png_ptr)
     , fInfo_ptr(info_ptr)
@@ -1145,15 +1147,14 @@
     return INHERITED::onGetFillValue(dstInfo);
 }
 
-SkCodec* SkPngCodec::NewFromStream(SkStream* stream, Result* result, SkPngChunkReader* chunkReader) {
-    std::unique_ptr<SkStream> streamDeleter(stream);
-
+std::unique_ptr<SkCodec> SkPngCodec::MakeFromStream(std::unique_ptr<SkStream> stream,
+                                                    Result* result, SkPngChunkReader* chunkReader) {
     SkCodec* outCodec = nullptr;
-    *result = read_header(stream, chunkReader, &outCodec, nullptr, nullptr);
+    *result = read_header(stream.get(), chunkReader, &outCodec, nullptr, nullptr);
     if (kSuccess == *result) {
         // Codec has taken ownership of the stream.
         SkASSERT(outCodec);
-        streamDeleter.release();
+        stream.release();
     }
-    return outCodec;
+    return std::unique_ptr<SkCodec>(outCodec);
 }
diff --git a/src/codec/SkPngCodec.h b/src/codec/SkPngCodec.h
index b5f9347..8496705 100644
--- a/src/codec/SkPngCodec.h
+++ b/src/codec/SkPngCodec.h
@@ -23,8 +23,8 @@
     static bool IsPng(const char*, size_t);
 
     // Assume IsPng was called and returned true.
-    static SkCodec* NewFromStream(SkStream*, Result*,
-                                  SkPngChunkReader* = nullptr);
+    static std::unique_ptr<SkCodec> MakeFromStream(std::unique_ptr<SkStream>, Result*,
+                                                   SkPngChunkReader* = nullptr);
 
     // FIXME (scroggo): Temporarily needed by AutoCleanPng.
     void setIdatLength(size_t len) { fIdatLength = len; }
@@ -45,8 +45,8 @@
         void* fPtr;
     };
 
-    SkPngCodec(const SkEncodedInfo&, const SkImageInfo&, SkStream*, SkPngChunkReader*,
-            void* png_ptr, void* info_ptr, int bitDepth);
+    SkPngCodec(const SkEncodedInfo&, const SkImageInfo&, std::unique_ptr<SkStream>,
+               SkPngChunkReader*, void* png_ptr, void* info_ptr, int bitDepth);
 
     Result onGetPixels(const SkImageInfo&, void*, size_t, const Options&, int*)
             override;
diff --git a/src/codec/SkRawCodec.cpp b/src/codec/SkRawCodec.cpp
index d23eec0..dbcbd28 100644
--- a/src/codec/SkRawCodec.cpp
+++ b/src/codec/SkRawCodec.cpp
@@ -10,6 +10,7 @@
 #include "SkColorPriv.h"
 #include "SkData.h"
 #include "SkJpegCodec.h"
+#include "SkMakeUnique.h"
 #include "SkMutex.h"
 #include "SkRawCodec.h"
 #include "SkRefCnt.h"
@@ -196,7 +197,7 @@
      * Note: for performance reason, this function is destructive to the SkRawStream. One should
      *       abandon current object after the function call.
      */
-   virtual SkMemoryStream* transferBuffer(size_t offset, size_t size) = 0;
+   virtual std::unique_ptr<SkMemoryStream> transferBuffer(size_t offset, size_t size) = 0;
 };
 
 class SkRawLimitedDynamicMemoryWStream : public SkDynamicMemoryWStream {
@@ -225,13 +226,12 @@
 // Note: the maximum buffer size is 100MB (limited by SkRawLimitedDynamicMemoryWStream).
 class SkRawBufferedStream : public SkRawStream {
 public:
-    // Will take the ownership of the stream.
-    explicit SkRawBufferedStream(SkStream* stream)
-        : fStream(stream)
+    explicit SkRawBufferedStream(std::unique_ptr<SkStream> stream)
+        : fStream(std::move(stream))
         , fWholeStreamRead(false)
     {
         // Only use SkRawBufferedStream when the stream is not an asset stream.
-        SkASSERT(!is_asset_stream(*stream));
+        SkASSERT(!is_asset_stream(*fStream));
     }
 
     ~SkRawBufferedStream() override {}
@@ -256,7 +256,7 @@
         return this->bufferMoreData(sum) && fStreamBuffer.read(data, offset, length);
     }
 
-    SkMemoryStream* transferBuffer(size_t offset, size_t size) override {
+    std::unique_ptr<SkMemoryStream> transferBuffer(size_t offset, size_t size) override {
         sk_sp<SkData> data(SkData::MakeUninitialized(size));
         if (offset > fStreamBuffer.bytesWritten()) {
             // If the offset is not buffered, read from fStream directly and skip the buffering.
@@ -288,7 +288,7 @@
                 }
             }
         }
-        return new SkMemoryStream(data);
+        return skstd::make_unique<SkMemoryStream>(data);
     }
 
 private:
@@ -333,12 +333,11 @@
 
 class SkRawAssetStream : public SkRawStream {
 public:
-    // Will take the ownership of the stream.
-    explicit SkRawAssetStream(SkStream* stream)
-        : fStream(stream)
+    explicit SkRawAssetStream(std::unique_ptr<SkStream> stream)
+        : fStream(std::move(stream))
     {
         // Only use SkRawAssetStream when the stream is an asset stream.
-        SkASSERT(is_asset_stream(*stream));
+        SkASSERT(is_asset_stream(*fStream));
     }
 
     ~SkRawAssetStream() override {}
@@ -361,7 +360,7 @@
         return fStream->seek(offset) && (fStream->read(data, length) == length);
     }
 
-    SkMemoryStream* transferBuffer(size_t offset, size_t size) override {
+    std::unique_ptr<SkMemoryStream> transferBuffer(size_t offset, size_t size) override {
         if (fStream->getLength() < offset) {
             return nullptr;
         }
@@ -382,7 +381,7 @@
             sk_sp<SkData> data(SkData::MakeWithCopy(
                 static_cast<const uint8_t*>(fStream->getMemoryBase()) + offset, bytesToRead));
             fStream.reset();
-            return new SkMemoryStream(data);
+            return skstd::make_unique<SkMemoryStream>(data);
         } else {
             sk_sp<SkData> data(SkData::MakeUninitialized(bytesToRead));
             if (!fStream->seek(offset)) {
@@ -392,7 +391,7 @@
             if (bytesRead < bytesToRead) {
                 data = SkData::MakeSubset(data.get(), 0, bytesRead);
             }
-            return new SkMemoryStream(data);
+            return skstd::make_unique<SkMemoryStream>(data);
         }
     }
 private:
@@ -630,12 +629,13 @@
  * SkJpegCodec. If PIEX returns kFail, then the file is invalid, return nullptr. In other cases,
  * fallback to create SkRawCodec for DNG images.
  */
-SkCodec* SkRawCodec::NewFromStream(SkStream* stream, Result* result) {
+std::unique_ptr<SkCodec> SkRawCodec::MakeFromStream(std::unique_ptr<SkStream> stream,
+                                                    Result* result) {
     std::unique_ptr<SkRawStream> rawStream;
     if (is_asset_stream(*stream)) {
-        rawStream.reset(new SkRawAssetStream(stream));
+        rawStream.reset(new SkRawAssetStream(std::move(stream)));
     } else {
-        rawStream.reset(new SkRawBufferedStream(stream));
+        rawStream.reset(new SkRawBufferedStream(std::move(stream)));
     }
 
     // Does not take the ownership of rawStream.
@@ -666,13 +666,14 @@
             // transferBuffer() is destructive to the rawStream. Abandon the rawStream after this
             // function call.
             // FIXME: one may avoid the copy of memoryStream and use the buffered rawStream.
-            SkMemoryStream* memoryStream =
-                rawStream->transferBuffer(imageData.preview.offset, imageData.preview.length);
+            auto memoryStream = rawStream->transferBuffer(imageData.preview.offset,
+                                                          imageData.preview.length);
             if (!memoryStream) {
                 *result = kInvalidInput;
                 return nullptr;
             }
-            return SkJpegCodec::NewFromStream(memoryStream, result, std::move(colorSpace));
+            return SkJpegCodec::MakeFromStream(std::move(memoryStream), result,
+                                               std::move(colorSpace));
         }
     }
 
@@ -689,7 +690,7 @@
     }
 
     *result = kSuccess;
-    return new SkRawCodec(dngImage.release());
+    return std::unique_ptr<SkCodec>(new SkRawCodec(dngImage.release()));
 }
 
 SkCodec::Result SkRawCodec::onGetPixels(const SkImageInfo& dstInfo, void* dst,
diff --git a/src/codec/SkRawCodec.h b/src/codec/SkRawCodec.h
index 5ed7211..c258ac1 100644
--- a/src/codec/SkRawCodec.h
+++ b/src/codec/SkRawCodec.h
@@ -28,7 +28,7 @@
      * Creates a RAW decoder
      * Takes ownership of the stream
      */
-    static SkCodec* NewFromStream(SkStream*, Result*);
+    static std::unique_ptr<SkCodec> MakeFromStream(std::unique_ptr<SkStream>, Result*);
 
     ~SkRawCodec() override;
 
diff --git a/src/codec/SkStreamBuffer.cpp b/src/codec/SkStreamBuffer.cpp
index 754065d..00253fa 100644
--- a/src/codec/SkStreamBuffer.cpp
+++ b/src/codec/SkStreamBuffer.cpp
@@ -7,11 +7,11 @@
 
 #include "SkStreamBuffer.h"
 
-SkStreamBuffer::SkStreamBuffer(SkStream* stream)
-    : fStream(stream)
+SkStreamBuffer::SkStreamBuffer(std::unique_ptr<SkStream> stream)
+    : fStream(std::move(stream))
     , fPosition(0)
     , fBytesBuffered(0)
-    , fHasLengthAndPosition(stream->hasLength() && stream->hasPosition())
+    , fHasLengthAndPosition(fStream->hasLength() && fStream->hasPosition())
     , fTrulyBuffered(0)
 {}
 
diff --git a/src/codec/SkStreamBuffer.h b/src/codec/SkStreamBuffer.h
index 2b60775..7543e32 100644
--- a/src/codec/SkStreamBuffer.h
+++ b/src/codec/SkStreamBuffer.h
@@ -24,9 +24,7 @@
  */
 class SkStreamBuffer : SkNoncopyable {
 public:
-    // Takes ownership of the SkStream.
-    SkStreamBuffer(SkStream*);
-
+    SkStreamBuffer(std::unique_ptr<SkStream>);
     ~SkStreamBuffer();
 
     /**
diff --git a/src/codec/SkWbmpCodec.cpp b/src/codec/SkWbmpCodec.cpp
index eea5e4b..934d7d5 100644
--- a/src/codec/SkWbmpCodec.cpp
+++ b/src/codec/SkWbmpCodec.cpp
@@ -95,10 +95,11 @@
     return this->stream()->read(row, fSrcRowBytes) == fSrcRowBytes;
 }
 
-SkWbmpCodec::SkWbmpCodec(int width, int height, const SkEncodedInfo& info, SkStream* stream)
+SkWbmpCodec::SkWbmpCodec(int width, int height, const SkEncodedInfo& info,
+                         std::unique_ptr<SkStream> stream)
     // Wbmp does not need a colorXform, so choose an arbitrary srcFormat.
     : INHERITED(width, height, info, SkColorSpaceXform::ColorFormat(),
-                stream, SkColorSpace::MakeSRGB())
+                std::move(stream), SkColorSpace::MakeSRGB())
     , fSrcRowBytes(get_src_row_bytes(this->getInfo().width()))
     , fSwizzler(nullptr)
 {}
@@ -145,10 +146,10 @@
     return read_header(&stream, nullptr);
 }
 
-SkCodec* SkWbmpCodec::NewFromStream(SkStream* stream, Result* result) {
-    std::unique_ptr<SkStream> streamDeleter(stream);
+std::unique_ptr<SkCodec> SkWbmpCodec::MakeFromStream(std::unique_ptr<SkStream> stream,
+                                                     Result* result) {
     SkISize size;
-    if (!read_header(stream, &size)) {
+    if (!read_header(stream.get(), &size)) {
         // This already succeeded in IsWbmp, so this stream was corrupted in/
         // after rewind.
         *result = kCouldNotRewind;
@@ -157,7 +158,8 @@
     *result = kSuccess;
     SkEncodedInfo info = SkEncodedInfo::Make(SkEncodedInfo::kGray_Color,
             SkEncodedInfo::kOpaque_Alpha, 1);
-    return new SkWbmpCodec(size.width(), size.height(), info, streamDeleter.release());
+    return std::unique_ptr<SkCodec>(new SkWbmpCodec(size.width(), size.height(), info,
+                                                    std::move(stream)));
 }
 
 int SkWbmpCodec::onGetScanlines(void* dst, int count, size_t dstRowBytes) {
diff --git a/src/codec/SkWbmpCodec.h b/src/codec/SkWbmpCodec.h
index f81b428..0328771 100644
--- a/src/codec/SkWbmpCodec.h
+++ b/src/codec/SkWbmpCodec.h
@@ -21,7 +21,7 @@
      * Creates a wbmp codec
      * Takes ownership of the stream
      */
-    static SkCodec* NewFromStream(SkStream*, Result*);
+    static std::unique_ptr<SkCodec> MakeFromStream(std::unique_ptr<SkStream>, Result*);
 
 protected:
     SkEncodedImageFormat onGetEncodedFormat() const override;
@@ -44,7 +44,7 @@
      */
     bool readRow(uint8_t* row);
 
-    SkWbmpCodec(int width, int height, const SkEncodedInfo&, SkStream*);
+    SkWbmpCodec(int width, int height, const SkEncodedInfo&, std::unique_ptr<SkStream>);
 
     const size_t                fSrcRowBytes;
 
diff --git a/src/codec/SkWebpCodec.cpp b/src/codec/SkWebpCodec.cpp
index 02e6ff2..94fa175 100644
--- a/src/codec/SkWebpCodec.cpp
+++ b/src/codec/SkWebpCodec.cpp
@@ -11,6 +11,7 @@
 #include "SkCodecAnimationPriv.h"
 #include "SkCodecPriv.h"
 #include "SkColorSpaceXform.h"
+#include "SkMakeUnique.h"
 #include "SkRasterPipeline.h"
 #include "SkSampler.h"
 #include "SkStreamPriv.h"
@@ -44,19 +45,18 @@
 
 // Parse headers of RIFF container, and check for valid Webp (VP8) content.
 // Returns an SkWebpCodec on success
-SkCodec* SkWebpCodec::NewFromStream(SkStream* stream, Result* result) {
-    std::unique_ptr<SkStream> streamDeleter(stream);
-
+std::unique_ptr<SkCodec> SkWebpCodec::MakeFromStream(std::unique_ptr<SkStream> stream,
+                                                     Result* result) {
     // Webp demux needs a contiguous data buffer.
     sk_sp<SkData> data = nullptr;
     if (stream->getMemoryBase()) {
         // It is safe to make without copy because we'll hold onto the stream.
         data = SkData::MakeWithoutCopy(stream->getMemoryBase(), stream->getLength());
     } else {
-        data = SkCopyStreamToData(stream);
+        data = SkCopyStreamToData(stream.get());
 
         // If we are forced to copy the stream to a data, we can go ahead and delete the stream.
-        streamDeleter.reset(nullptr);
+        stream.reset(nullptr);
     }
 
     // It's a little strange that the |demux| will outlive |webpData|, though it needs the
@@ -162,9 +162,8 @@
 
     *result = kSuccess;
     SkEncodedInfo info = SkEncodedInfo::Make(color, alpha, 8);
-    return new SkWebpCodec(width, height, info, std::move(colorSpace),
-                           streamDeleter.release(), demux.release(),
-                           std::move(data));
+    return std::unique_ptr<SkCodec>(new SkWebpCodec(width, height, info, std::move(colorSpace),
+                                           std::move(stream), demux.release(), std::move(data)));
 }
 
 SkISize SkWebpCodec::onGetScaledDimensions(float desiredScale) const {
@@ -628,9 +627,9 @@
 }
 
 SkWebpCodec::SkWebpCodec(int width, int height, const SkEncodedInfo& info,
-                         sk_sp<SkColorSpace> colorSpace, SkStream* stream, WebPDemuxer* demux,
-                         sk_sp<SkData> data)
-    : INHERITED(width, height, info, SkColorSpaceXform::kBGRA_8888_ColorFormat, stream,
+                         sk_sp<SkColorSpace> colorSpace, std::unique_ptr<SkStream> stream,
+                         WebPDemuxer* demux, sk_sp<SkData> data)
+    : INHERITED(width, height, info, SkColorSpaceXform::kBGRA_8888_ColorFormat, std::move(stream),
                 std::move(colorSpace))
     , fDemux(demux)
     , fData(std::move(data))
diff --git a/src/codec/SkWebpCodec.h b/src/codec/SkWebpCodec.h
index e06d097..60bff61 100644
--- a/src/codec/SkWebpCodec.h
+++ b/src/codec/SkWebpCodec.h
@@ -28,7 +28,7 @@
 class SkWebpCodec final : public SkCodec {
 public:
     // Assumes IsWebp was called and returned true.
-    static SkCodec* NewFromStream(SkStream*, Result*);
+    static std::unique_ptr<SkCodec> MakeFromStream(std::unique_ptr<SkStream>, Result*);
     static bool IsWebp(const void*, size_t);
 protected:
     Result onGetPixels(const SkImageInfo&, void*, size_t, const Options&, int*) override;
@@ -49,8 +49,8 @@
     }
 
 private:
-    SkWebpCodec(int width, int height, const SkEncodedInfo&, sk_sp<SkColorSpace>, SkStream*,
-                WebPDemuxer*, sk_sp<SkData>);
+    SkWebpCodec(int width, int height, const SkEncodedInfo&, sk_sp<SkColorSpace>,
+                std::unique_ptr<SkStream>, WebPDemuxer*, sk_sp<SkData>);
 
     SkAutoTCallVProc<WebPDemuxer, WebPDemuxDelete> fDemux;