diff options
62 files changed, 351 insertions, 336 deletions
diff --git a/bench/AndroidCodecBench.cpp b/bench/AndroidCodecBench.cpp index e0680bd9b4..d9abac63d8 100644 --- a/bench/AndroidCodecBench.cpp +++ b/bench/AndroidCodecBench.cpp @@ -29,7 +29,7 @@ bool AndroidCodecBench::isSuitableFor(Backend backend) { } void AndroidCodecBench::onDelayedSetup() { - std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::NewFromData(fData)); + std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::MakeFromData(fData)); SkISize scaledSize = codec->getSampledDimensions(fSampleSize); fInfo = codec->getInfo().makeWH(scaledSize.width(), scaledSize.height()) @@ -46,7 +46,7 @@ void AndroidCodecBench::onDraw(int n, SkCanvas* canvas) { SkAndroidCodec::AndroidOptions options; options.fSampleSize = fSampleSize; for (int i = 0; i < n; i++) { - codec.reset(SkAndroidCodec::NewFromData(fData)); + codec = SkAndroidCodec::MakeFromData(fData); #ifdef SK_DEBUG const SkCodec::Result result = #endif diff --git a/bench/CodecBench.cpp b/bench/CodecBench.cpp index f72294e246..29443746f7 100644 --- a/bench/CodecBench.cpp +++ b/bench/CodecBench.cpp @@ -24,11 +24,8 @@ CodecBench::CodecBench(SkString baseName, SkData* encoded, SkColorType colorType // Parse filename and the color type to give the benchmark a useful name fName.printf("Codec_%s_%s%s", baseName.c_str(), color_type_to_str(colorType), alpha_type_to_str(alphaType)); -#ifdef SK_DEBUG // Ensure that we can create an SkCodec from this data. - std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(fData)); - SkASSERT(codec); -#endif + SkASSERT(SkCodec::MakeFromData(fData)); } const char* CodecBench::onGetName() { @@ -40,7 +37,7 @@ bool CodecBench::isSuitableFor(Backend backend) { } void CodecBench::onDelayedSetup() { - std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(fData)); + std::unique_ptr<SkCodec> codec = SkCodec::MakeFromData(fData); fInfo = codec->getInfo().makeColorType(fColorType) .makeAlphaType(fAlphaType) @@ -56,7 +53,7 @@ void CodecBench::onDraw(int n, SkCanvas* canvas) { options.fZeroInitialized = SkCodec::kYes_ZeroInitialized; } for (int i = 0; i < n; i++) { - codec.reset(SkCodec::NewFromData(fData)); + codec = SkCodec::MakeFromData(fData); #ifdef SK_DEBUG const SkCodec::Result result = #endif diff --git a/bench/ColorCodecBench.cpp b/bench/ColorCodecBench.cpp index 867b8a1418..680c1d916f 100644 --- a/bench/ColorCodecBench.cpp +++ b/bench/ColorCodecBench.cpp @@ -34,13 +34,10 @@ bool ColorCodecBench::isSuitableFor(Backend backend) { } void ColorCodecBench::decodeAndXform() { - std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(fEncoded)); - SkASSERT(codec); - #ifdef SK_DEBUG SkCodec::Result result = #endif - codec->getPixels(fDstInfo, fDst.get(), fDstInfo.minRowBytes()); + SkCodec::MakeFromData(fEncoded)->getPixels(fDstInfo, fDst.get(), fDstInfo.minRowBytes()); SkASSERT(SkCodec::kSuccess == result); } @@ -61,7 +58,7 @@ void ColorCodecBench::xformOnly() { } void ColorCodecBench::onDelayedSetup() { - std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(fEncoded)); + std::unique_ptr<SkCodec> codec = SkCodec::MakeFromData(fEncoded); fSrcInfo = codec->getInfo().makeColorType(kRGBA_8888_SkColorType); fDstInfo = fSrcInfo; diff --git a/bench/nanobench.cpp b/bench/nanobench.cpp index 95e12118f3..196cb220d0 100644 --- a/bench/nanobench.cpp +++ b/bench/nanobench.cpp @@ -815,7 +815,7 @@ public: continue; } sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str())); - std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(encoded)); + std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded)); if (!codec) { // Nothing to time. SkDebugf("Cannot find codec for %s\n", path.c_str()); @@ -894,7 +894,7 @@ public: continue; } sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str())); - std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::NewFromData(encoded)); + std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::MakeFromData(encoded)); if (!codec) { // Nothing to time. SkDebugf("Cannot find codec for %s\n", path.c_str()); @@ -531,7 +531,7 @@ static void push_codec_srcs(Path path) { info("Couldn't read %s.", path.c_str()); return; } - std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(encoded)); + std::unique_ptr<SkCodec> codec = SkCodec::MakeFromData(encoded); if (nullptr == codec.get()) { info("Couldn't create codec for %s.", path.c_str()); return; diff --git a/dm/DMSrcSink.cpp b/dm/DMSrcSink.cpp index 6876254ce7..0e295a09c9 100644 --- a/dm/DMSrcSink.cpp +++ b/dm/DMSrcSink.cpp @@ -419,7 +419,7 @@ Error CodecSrc::draw(SkCanvas* canvas) const { return SkStringPrintf("Couldn't read %s.", fPath.c_str()); } - std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(encoded)); + std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded)); if (nullptr == codec.get()) { return SkStringPrintf("Couldn't create codec for %s.", fPath.c_str()); } @@ -757,7 +757,7 @@ Error CodecSrc::draw(SkCanvas* canvas) const { SkISize CodecSrc::size() const { sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str())); - std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(encoded)); + std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded)); if (nullptr == codec) { return {0, 0}; } @@ -818,8 +818,8 @@ Error AndroidCodecSrc::draw(SkCanvas* canvas) const { if (!encoded) { return SkStringPrintf("Couldn't read %s.", fPath.c_str()); } - std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::NewFromData(encoded)); - if (nullptr == codec.get()) { + std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::MakeFromData(encoded)); + if (nullptr == codec) { return SkStringPrintf("Couldn't create android codec for %s.", fPath.c_str()); } @@ -869,7 +869,7 @@ Error AndroidCodecSrc::draw(SkCanvas* canvas) const { SkISize AndroidCodecSrc::size() const { sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str())); - std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::NewFromData(encoded)); + std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::MakeFromData(encoded)); if (nullptr == codec) { return {0, 0}; } @@ -990,7 +990,7 @@ Error ImageGenSrc::draw(SkCanvas* canvas) const { SkISize ImageGenSrc::size() const { sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str())); - std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(encoded)); + std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded)); if (nullptr == codec) { return {0, 0}; } @@ -1052,8 +1052,8 @@ Error ColorCodecSrc::draw(SkCanvas* canvas) const { return SkStringPrintf("Couldn't read %s.", fPath.c_str()); } - std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(encoded)); - if (nullptr == codec.get()) { + std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded)); + if (nullptr == codec) { return SkStringPrintf("Couldn't create codec for %s.", fPath.c_str()); } @@ -1123,7 +1123,7 @@ Error ColorCodecSrc::draw(SkCanvas* canvas) const { SkISize ColorCodecSrc::size() const { sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str())); - std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(encoded)); + std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded)); if (nullptr == codec) { return {0, 0}; } diff --git a/fuzz/fuzz.cpp b/fuzz/fuzz.cpp index e030979210..f8897e6663 100644 --- a/fuzz/fuzz.cpp +++ b/fuzz/fuzz.cpp @@ -195,7 +195,7 @@ static void fuzz_img(sk_sp<SkData> bytes, uint8_t scale, uint8_t mode) { // This is mostly copied from DMSrcSink's CodecSrc::draw method. SkDebugf("Decoding\n"); - std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(bytes)); + std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(bytes)); if (nullptr == codec.get()) { SkDebugf("[terminated] Couldn't create codec.\n"); return; diff --git a/gm/animatedGif.cpp b/gm/animatedGif.cpp index fc5217fa8d..5686193971 100644 --- a/gm/animatedGif.cpp +++ b/gm/animatedGif.cpp @@ -123,7 +123,7 @@ private: return false; } - fCodec.reset(SkCodec::NewFromStream(stream.release())); + fCodec = SkCodec::MakeFromStream(std::move(stream)); if (!fCodec) { SkDebugf("Could create codec from %s", FLAGS_animatedGif[0]); return false; diff --git a/gm/bitmapimage.cpp b/gm/bitmapimage.cpp index 9832041132..ccd34875e0 100644 --- a/gm/bitmapimage.cpp +++ b/gm/bitmapimage.cpp @@ -36,7 +36,7 @@ protected: } // Create matching bitmap. - std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(GetResourceAsStream(path).release())); + std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream(GetResourceAsStream(path))); SkBitmap bitmap; bitmap.allocPixels(codec->getInfo()); codec->getPixels(codec->getInfo(), bitmap.getPixels(), bitmap.rowBytes()); diff --git a/gm/encode-srgb.cpp b/gm/encode-srgb.cpp index bffe19606f..be3cd62f8b 100644 --- a/gm/encode-srgb.cpp +++ b/gm/encode-srgb.cpp @@ -56,7 +56,7 @@ static void make(SkBitmap* bitmap, SkColorType colorType, SkAlphaType alphaType, } sk_sp<SkData> data = GetResourceAsData(resource); - std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(data)); + std::unique_ptr<SkCodec> codec = SkCodec::MakeFromData(data); SkImageInfo dstInfo = codec->getInfo().makeColorType(colorType) .makeAlphaType(alphaType) .makeColorSpace(fix_for_colortype(colorSpace, colorType)); diff --git a/gm/makecolorspace.cpp b/gm/makecolorspace.cpp index b3b937ec40..0ea3d40746 100644 --- a/gm/makecolorspace.cpp +++ b/gm/makecolorspace.cpp @@ -15,7 +15,7 @@ sk_sp<SkImage> make_raster_image(const char* path, SkTransferFunctionBehavior behavior) { SkString resourcePath = GetResourcePath(path); sk_sp<SkData> resourceData = SkData::MakeFromFileName(resourcePath.c_str()); - std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(resourceData)); + std::unique_ptr<SkCodec> codec = SkCodec::MakeFromData(resourceData); SkBitmap bitmap; bitmap.allocPixels(codec->getInfo()); diff --git a/gm/readpixels.cpp b/gm/readpixels.cpp index 69d22bb7f2..bd2fe9bcc3 100644 --- a/gm/readpixels.cpp +++ b/gm/readpixels.cpp @@ -52,7 +52,7 @@ static const int kHeight = 64; static sk_sp<SkImage> make_raster_image(SkColorType colorType) { std::unique_ptr<SkStream> stream(GetResourceAsStream("google_chrome.ico")); - std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream.release())); + std::unique_ptr<SkCodec> codec = SkCodec::MakeFromStream(std::move(stream)); SkBitmap bitmap; SkImageInfo info = codec->getInfo().makeWH(kWidth, kHeight) diff --git a/include/codec/SkAndroidCodec.h b/include/codec/SkAndroidCodec.h index b4dd3854e6..e785411ea9 100644 --- a/include/codec/SkAndroidCodec.h +++ b/include/codec/SkAndroidCodec.h @@ -29,7 +29,8 @@ public: * If NULL is returned, the stream is deleted immediately. Otherwise, the * SkCodec takes ownership of it, and will delete it when done with it. */ - static SkAndroidCodec* NewFromStream(SkStream*, SkPngChunkReader* = NULL); + static std::unique_ptr<SkAndroidCodec> MakeFromStream(std::unique_ptr<SkStream>, + SkPngChunkReader* = nullptr); /** * If this data represents an encoded image that we know how to decode, @@ -38,10 +39,19 @@ public: * The SkPngChunkReader handles unknown chunks in PNGs. * See SkCodec.h for more details. */ - static SkAndroidCodec* NewFromData(sk_sp<SkData>, SkPngChunkReader* = NULL); + static std::unique_ptr<SkAndroidCodec> MakeFromData(sk_sp<SkData>, SkPngChunkReader* = nullptr); + +#ifdef SK_SUPPORT_LEGACY_CODEC_NEW + static SkAndroidCodec* NewFromStream(SkStream* stream, SkPngChunkReader* reader) { + return MakeFromStream(std::unique_ptr<SkStream>(stream), reader).release(); + } + static SkAndroidCodec* NewFromData(sk_sp<SkData> data, SkPngChunkReader* reader) { + return MakeFromData(std::move(data), reader).release(); + } static SkAndroidCodec* NewFromData(SkData* data, SkPngChunkReader* reader) { return NewFromData(sk_ref_sp(data), reader); } +#endif virtual ~SkAndroidCodec() {} diff --git a/include/codec/SkCodec.h b/include/codec/SkCodec.h index bb780f0906..1d6d904e28 100644 --- a/include/codec/SkCodec.h +++ b/include/codec/SkCodec.h @@ -139,8 +139,8 @@ public: * If NULL is returned, the stream is deleted immediately. Otherwise, the * SkCodec takes ownership of it, and will delete it when done with it. */ - static SkCodec* NewFromStream(SkStream*, Result* = nullptr, - SkPngChunkReader* = nullptr); + static std::unique_ptr<SkCodec> MakeFromStream(std::unique_ptr<SkStream>, Result* = nullptr, + SkPngChunkReader* = nullptr); /** * If this data represents an encoded image that we know how to decode, @@ -158,10 +158,15 @@ public: * If the PNG does not contain unknown chunks, the SkPngChunkReader * will not be used or modified. */ - static SkCodec* NewFromData(sk_sp<SkData>, SkPngChunkReader* = NULL); + static std::unique_ptr<SkCodec> MakeFromData(sk_sp<SkData>, SkPngChunkReader* = nullptr); + +#ifdef SK_SUPPORT_LEGACY_CODEC_NEW + static SkCodec* NewFromStream(SkStream* str, Result* res, SkPngChunkReader* chunk); + static SkCodec* NewFromData(sk_sp<SkData>, SkPngChunkReader* = nullptr); static SkCodec* NewFromData(SkData* data, SkPngChunkReader* reader) { return NewFromData(sk_ref_sp(data), reader); } +#endif virtual ~SkCodec(); @@ -674,25 +679,21 @@ public: protected: using XformFormat = SkColorSpaceXform::ColorFormat; - /** - * Takes ownership of SkStream* - */ SkCodec(int width, int height, const SkEncodedInfo&, XformFormat srcFormat, - SkStream*, + std::unique_ptr<SkStream>, sk_sp<SkColorSpace>, Origin = kTopLeft_Origin); /** - * Takes ownership of SkStream* * Allows the subclass to set the recommended SkImageInfo */ SkCodec(const SkEncodedInfo&, const SkImageInfo&, XformFormat srcFormat, - SkStream*, + std::unique_ptr<SkStream>, Origin = kTopLeft_Origin); virtual SkISize onGetScaledDimensions(float /*desiredScale*/) const { diff --git a/samplecode/DecodeFile.h b/samplecode/DecodeFile.h index 1ae98a4a72..eeb1cce0b8 100644 --- a/samplecode/DecodeFile.h +++ b/samplecode/DecodeFile.h @@ -17,7 +17,7 @@ static inline bool decode_file(const char* filename, SkBitmap* bitmap, SkColorType colorType = kN32_SkColorType, bool requireUnpremul = false) { sk_sp<SkData> data(SkData::MakeFromFileName(filename)); - std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(data)); + std::unique_ptr<SkCodec> codec = SkCodec::MakeFromData(data); if (!codec) { return false; } diff --git a/src/android/SkBitmapRegionDecoder.cpp b/src/android/SkBitmapRegionDecoder.cpp index 8298fb5298..a6e3ac6d94 100644 --- a/src/android/SkBitmapRegionDecoder.cpp +++ b/src/android/SkBitmapRegionDecoder.cpp @@ -22,11 +22,10 @@ SkBitmapRegionDecoder* SkBitmapRegionDecoder::Create( 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 0245ea2f1e..3a23e06c8d 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 @@ SkAndroidCodec::SkAndroidCodec(SkCodec* codec) , 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 @@ SkAndroidCodec* SkAndroidCodec::NewFromStream(SkStream* stream, SkPngChunkReader 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 0e744351f0..1071ff38e7 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 0b9f91977e..24277fc539 100644 --- a/src/codec/SkBmpBaseCodec.h +++ b/src/codec/SkBmpBaseCodec.h @@ -25,7 +25,7 @@ public: 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 70397f656f..18f8768e0d 100644 --- a/src/codec/SkBmpCodec.cpp +++ b/src/codec/SkBmpCodec.cpp @@ -67,16 +67,17 @@ bool SkBmpCodec::IsBmp(const void* buffer, size_t bytesRead) { * 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 @@ SkCodec::Result SkBmpCodec::ReadHeader(SkStream* stream, bool inIco, // 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 @@ SkCodec::Result SkBmpCodec::ReadHeader(SkStream* stream, bool inIco, 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 @@ SkCodec::Result SkBmpCodec::ReadHeader(SkStream* stream, bool inIco, // 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 @@ SkCodec::Result SkBmpCodec::ReadHeader(SkStream* stream, bool inIco, * 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 d5f5a32eae..651f1be248 100644 --- a/src/codec/SkBmpCodec.h +++ b/src/codec/SkBmpCodec.h @@ -28,17 +28,17 @@ public: * 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 @@ protected: /* * 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 @@ private: * 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 51060e5e99..aec56ee2fa 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 8d8d64c168..4b0dbde026 100644 --- a/src/codec/SkBmpMaskCodec.h +++ b/src/codec/SkBmpMaskCodec.h @@ -22,7 +22,7 @@ public: /* * 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 @@ public: * @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 0fcd290add..23a2c98eb1 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 d6c17c3c68..70e97a7833 100644 --- a/src/codec/SkBmpRLECodec.h +++ b/src/codec/SkBmpRLECodec.h @@ -22,7 +22,7 @@ public: /* * 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 @@ public: * 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 46a5715dc9..53aa91c6fa 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 f9ce5c6839..60587fb48d 100644 --- a/src/codec/SkBmpStandardCodec.h +++ b/src/codec/SkBmpStandardCodec.h @@ -23,7 +23,7 @@ public: /* * 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 @@ public: * 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 872fe2b689..4fb0ea9350 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 @@ SkCodec* SkCodec::NewFromStream(SkStream* stream, 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 @@ SkCodec* SkCodec::NewFromStream(SkStream* stream, // 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 @@ SkCodec* SkCodec::NewFromStream(SkStream* stream, 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(int width, int height, const SkEncodedInfo& info, {} 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 @@ std::vector<SkCodec::FrameInfo> SkCodec::getFrameInfo() { } 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 7be6277f56..e6ca2122ea 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 @@ static SkImageInfo adjust_info(const SkImageInfo& info) { 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 4d0c0780a6..1b2cbc28b2 100644 --- a/src/codec/SkCodecImageGenerator.h +++ b/src/codec/SkCodecImageGenerator.h @@ -34,7 +34,7 @@ private: /* * 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 412b8ff1f7..d54cbd558e 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 @@ static SkCodec::Result gif_error(const char* msg, SkCodec::Result result = SkCod 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 @@ SkCodec* SkGifCodec::NewFromStream(SkStream* stream, Result* result) { 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 6ecd419ea3..89c84bdda3 100644 --- a/src/codec/SkGifCodec.h +++ b/src/codec/SkGifCodec.h @@ -29,7 +29,7 @@ public: * 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 2e61bfe45d..fce14a2f35 100644 --- a/src/codec/SkIcoCodec.cpp +++ b/src/codec/SkIcoCodec.cpp @@ -26,18 +26,15 @@ bool SkIcoCodec::IsIco(const void* buffer, size_t bytesRead) { !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 @@ SkCodec* SkIcoCodec::NewFromStream(SkStream* stream, Result* result) { // 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 @@ SkCodec* SkIcoCodec::NewFromStream(SkStream* stream, Result* result) { // 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 @@ SkCodec* SkIcoCodec::NewFromStream(SkStream* stream, Result* result) { 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 @@ SkCodec* SkIcoCodec::NewFromStream(SkStream* stream, Result* result) { 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 @@ SkCodec* SkIcoCodec::NewFromStream(SkStream* stream, Result* result) { *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 0c27934e0c..aa19c9b935 100644 --- a/src/codec/SkIcoCodec.h +++ b/src/codec/SkIcoCodec.h @@ -25,7 +25,7 @@ public: * 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 f3dbdf199c..8f88138afb 100644 --- a/src/codec/SkJpegCodec.cpp +++ b/src/codec/SkJpegCodec.cpp @@ -252,8 +252,9 @@ SkCodec::Result SkJpegCodec::ReadHeader(SkStream* stream, SkCodec** codecOut, 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 @@ SkCodec::Result SkJpegCodec::ReadHeader(SkStream* stream, SkCodec** codecOut, 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 96228f305a..1409182a97 100644 --- a/src/codec/SkJpegCodec.h +++ b/src/codec/SkJpegCodec.h @@ -31,7 +31,7 @@ public: * 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 @@ private: /* * 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 @@ private: * @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 560b5bfe6b..13e45ab609 100644 --- a/src/codec/SkPngCodec.cpp +++ b/src/codec/SkPngCodec.cpp @@ -480,9 +480,10 @@ void SkPngCodec::applyXformRow(void* dst, const void* src) { 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 @@ private: 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 @@ void AutoCleanPng::infoCallback(size_t idatLength) { } 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 @@ void AutoCleanPng::infoCallback(size_t idatLength) { } 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 @@ uint64_t SkPngCodec::onGetFillValue(const SkImageInfo& dstInfo) const { 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 b5f9347705..8496705258 100644 --- a/src/codec/SkPngCodec.h +++ b/src/codec/SkPngCodec.h @@ -23,8 +23,8 @@ public: 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 @@ protected: 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 d23eec0de6..dbcbd28499 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 @@ public: * 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 @@ private: // 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 @@ public: 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 @@ public: } } } - return new SkMemoryStream(data); + return skstd::make_unique<SkMemoryStream>(data); } private: @@ -333,12 +333,11 @@ private: 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 @@ public: 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 @@ public: 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 @@ public: 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 @@ private: * 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 @@ SkCodec* SkRawCodec::NewFromStream(SkStream* stream, Result* result) { // 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 @@ SkCodec* SkRawCodec::NewFromStream(SkStream* stream, Result* result) { } *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 5ed721190f..c258ac1651 100644 --- a/src/codec/SkRawCodec.h +++ b/src/codec/SkRawCodec.h @@ -28,7 +28,7 @@ public: * 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 754065d97a..00253fa751 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 2b60775dd2..7543e32ff7 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 eea5e4babc..934d7d5b9a 100644 --- a/src/codec/SkWbmpCodec.cpp +++ b/src/codec/SkWbmpCodec.cpp @@ -95,10 +95,11 @@ bool SkWbmpCodec::readRow(uint8_t* row) { 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 @@ bool SkWbmpCodec::IsWbmp(const void* buffer, size_t bytesRead) { 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 @@ SkCodec* SkWbmpCodec::NewFromStream(SkStream* stream, Result* result) { *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 f81b428e4f..03287710e0 100644 --- a/src/codec/SkWbmpCodec.h +++ b/src/codec/SkWbmpCodec.h @@ -21,7 +21,7 @@ public: * 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 @@ private: */ 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 02e6ff2bf9..94fa175f82 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 @@ static SkAlphaType alpha_type(bool hasAlpha) { // 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 @@ SkCodec* SkWebpCodec::NewFromStream(SkStream* stream, Result* result) { *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 @@ SkCodec::Result SkWebpCodec::onGetPixels(const SkImageInfo& dstInfo, void* dst, } 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 e06d0972fe..60bff61220 100644 --- a/src/codec/SkWebpCodec.h +++ b/src/codec/SkWebpCodec.h @@ -28,7 +28,7 @@ static const size_t WEBP_VP8_HEADER_SIZE = 30; 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 @@ protected: } 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; diff --git a/tests/BadIcoTest.cpp b/tests/BadIcoTest.cpp index 670c2ac311..21343a2a5c 100644 --- a/tests/BadIcoTest.cpp +++ b/tests/BadIcoTest.cpp @@ -32,7 +32,7 @@ DEF_TEST(BadImage, reporter) { for (size_t i = 0; i < SK_ARRAY_COUNT(badImages); ++i) { SkString resourcePath = SkOSPath::Join(badImagesFolder, badImages[i]); std::unique_ptr<SkStream> stream(GetResourceAsStream(resourcePath.c_str())); - std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream.release())); + std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream(std::move(stream))); // These images are corrupt. It's not important whether we succeed/fail in codec // creation or decoding. We just want to make sure that we don't crash. diff --git a/tests/CodecAnimTest.cpp b/tests/CodecAnimTest.cpp index 84dc9faa6b..612a0facd0 100644 --- a/tests/CodecAnimTest.cpp +++ b/tests/CodecAnimTest.cpp @@ -37,9 +37,7 @@ DEF_TEST(Codec_trunc, r) { if (!data) { return; } - data = SkData::MakeSubset(data.get(), 0, 23); - std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(data)); - codec->getFrameInfo(); + SkCodec::MakeFromData(SkData::MakeSubset(data.get(), 0, 23))->getFrameInfo(); } // 565 does not support alpha, but there is no reason for it not to support an @@ -50,7 +48,7 @@ DEF_TEST(Codec_565, r) { if (!data) { return; } - std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(std::move(data))); + std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(std::move(data))); auto info = codec->getInfo().makeColorType(kRGB_565_SkColorType); SkBitmap bm; bm.allocPixels(info); @@ -175,7 +173,7 @@ DEF_TEST(Codec_frames, r) { continue; } - std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(data)); + std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(data)); if (!codec) { ERRORF(r, "Failed to create an SkCodec from '%s'", rec.fName); continue; @@ -227,7 +225,7 @@ DEF_TEST(Codec_frames, r) { for (auto mode : { TestMode::kVector, TestMode::kIndividual }) { // Re-create the codec to reset state and test parsing. - codec.reset(SkCodec::NewFromData(data)); + codec = SkCodec::MakeFromData(data); int frameCount; std::vector<SkCodec::FrameInfo> frameInfos; diff --git a/tests/CodecExactReadTest.cpp b/tests/CodecExactReadTest.cpp index 9b6acc8bc9..d00c0bcf97 100644 --- a/tests/CodecExactReadTest.cpp +++ b/tests/CodecExactReadTest.cpp @@ -11,6 +11,7 @@ #include "SkBitmap.h" #include "SkCodec.h" #include "SkData.h" +#include "SkMakeUnique.h" #include "SkStream.h" namespace { @@ -67,7 +68,8 @@ DEF_TEST(Codec_end, r) { SkMemoryStream stream(std::move(multiData)); for (int i = 0; i < kNumImages; ++i) { - std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(new UnowningStream(&stream))); + std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream( + skstd::make_unique<UnowningStream>(&stream))); if (!codec) { ERRORF(r, "Failed to create a codec from %s, iteration %i", path, i); continue; diff --git a/tests/CodecPartialTest.cpp b/tests/CodecPartialTest.cpp index 4a56f46e21..e40ff8b714 100644 --- a/tests/CodecPartialTest.cpp +++ b/tests/CodecPartialTest.cpp @@ -9,6 +9,7 @@ #include "SkCodec.h" #include "SkData.h" #include "SkImageInfo.h" +#include "SkMakeUnique.h" #include "SkRWBuffer.h" #include "SkString.h" @@ -24,7 +25,7 @@ static SkImageInfo standardize_info(SkCodec* codec) { } static bool create_truth(sk_sp<SkData> data, SkBitmap* dst) { - std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(std::move(data))); + std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(std::move(data))); if (!codec) { return false; } @@ -64,7 +65,7 @@ static void test_partial(skiatest::Reporter* r, const char* name, size_t minByte // Note that we cheat and hold on to a pointer to stream, though it is owned by // partialCodec. - std::unique_ptr<SkCodec> partialCodec(SkCodec::NewFromStream(stream)); + std::unique_ptr<SkCodec> partialCodec(SkCodec::MakeFromStream(std::unique_ptr<SkStream>(stream))); if (!partialCodec) { // Technically, this could be a small file where half the file is not // enough. @@ -146,7 +147,7 @@ DEF_TEST(Codec_requiredFrame, r) { return; } - std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(file)); + std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(file)); if (!codec) { ERRORF(r, "Failed to create codec from %s", path); return; @@ -166,7 +167,7 @@ DEF_TEST(Codec_requiredFrame, r) { return; } stream = new HaltingStream(file, i); - partialCodec.reset(SkCodec::NewFromStream(stream)); + partialCodec = SkCodec::MakeFromStream(std::unique_ptr<SkStream>(stream)); } std::vector<SkCodec::FrameInfo> partialInfo; @@ -193,8 +194,7 @@ DEF_TEST(Codec_partialAnim, r) { // This stream will be owned by fullCodec, but we hang on to the pointer // to determine frame offsets. - SkStream* stream = new SkMemoryStream(file); - std::unique_ptr<SkCodec> fullCodec(SkCodec::NewFromStream(stream)); + std::unique_ptr<SkCodec> fullCodec(SkCodec::MakeFromStream(skstd::make_unique<SkMemoryStream>(file))); const auto info = standardize_info(fullCodec.get()); // frameByteCounts stores the number of bytes to decode a particular frame. @@ -233,7 +233,8 @@ DEF_TEST(Codec_partialAnim, r) { // Now decode frames partially, then completely, and compare to the original. HaltingStream* haltingStream = new HaltingStream(file, frameByteCounts[0]); - std::unique_ptr<SkCodec> partialCodec(SkCodec::NewFromStream(haltingStream)); + std::unique_ptr<SkCodec> partialCodec(SkCodec::MakeFromStream( + std::unique_ptr<SkStream>(haltingStream))); if (!partialCodec) { ERRORF(r, "Failed to create a partial codec from %s with %i bytes out of %i", path, frameByteCounts[0], file->size()); @@ -287,8 +288,8 @@ static void test_interleaved(skiatest::Reporter* r, const char* name) { return; } const size_t halfSize = file->size() / 2; - std::unique_ptr<SkCodec> partialCodec(SkCodec::NewFromStream( - new HaltingStream(std::move(file), halfSize))); + std::unique_ptr<SkCodec> partialCodec(SkCodec::MakeFromStream( + skstd::make_unique<HaltingStream>(std::move(file), halfSize))); if (!partialCodec) { ERRORF(r, "Failed to create codec for %s", name); return; @@ -351,7 +352,7 @@ static unsigned char gNoGlobalColorMap[] = { // Test that a gif file truncated before its local color map behaves as expected. DEF_TEST(Codec_GifPreMap, r) { sk_sp<SkData> data = SkData::MakeWithoutCopy(gNoGlobalColorMap, sizeof(gNoGlobalColorMap)); - std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(data)); + std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(data)); if (!codec) { ERRORF(r, "failed to create codec"); return; @@ -365,7 +366,7 @@ DEF_TEST(Codec_GifPreMap, r) { REPORTER_ASSERT(r, result == SkCodec::kSuccess); // Truncate to 23 bytes, just before the color map. This should fail to decode. - codec.reset(SkCodec::NewFromData(SkData::MakeWithoutCopy(gNoGlobalColorMap, 23))); + codec = SkCodec::MakeFromData(SkData::MakeWithoutCopy(gNoGlobalColorMap, 23)); REPORTER_ASSERT(r, codec); if (codec) { SkBitmap bm; @@ -378,7 +379,7 @@ DEF_TEST(Codec_GifPreMap, r) { // cannot start an incremental decode until we have more data. If we did, // we would be using the wrong color table. HaltingStream* stream = new HaltingStream(data, 23); - codec.reset(SkCodec::NewFromStream(stream)); + codec = SkCodec::MakeFromStream(std::unique_ptr<SkStream>(stream)); REPORTER_ASSERT(r, codec); if (codec) { SkBitmap bm; @@ -406,7 +407,7 @@ DEF_TEST(Codec_emptyIDAT, r) { // Truncate to the beginning of the IDAT, immediately after the IDAT tag. file = SkData::MakeSubset(file.get(), 0, 80); - std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(std::move(file))); + std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(std::move(file))); if (!codec) { ERRORF(r, "Failed to create a codec for %s", name); return; @@ -436,8 +437,8 @@ DEF_TEST(Codec_incomplete, r) { for (size_t len = 14; len <= file->size(); len += 5) { SkCodec::Result result; - auto* stream = new SkMemoryStream(file->data(), len); - std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream, &result)); + std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream( + skstd::make_unique<SkMemoryStream>(file->data(), len), &result)); if (codec) { if (result != SkCodec::kSuccess) { ERRORF(r, "Created an SkCodec for %s with %lu bytes, but " diff --git a/tests/CodecPriv.h b/tests/CodecPriv.h index d80bb69672..8362599e95 100644 --- a/tests/CodecPriv.h +++ b/tests/CodecPriv.h @@ -12,7 +12,7 @@ #include "SkData.h" inline bool decode_memory(const void* mem, size_t size, SkBitmap* bm) { - std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(SkData::MakeWithoutCopy(mem, size))); + std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(SkData::MakeWithoutCopy(mem, size))); if (!codec) { return false; } diff --git a/tests/CodecTest.cpp b/tests/CodecTest.cpp index 12e3f2c13e..698af46de7 100644 --- a/tests/CodecTest.cpp +++ b/tests/CodecTest.cpp @@ -18,6 +18,7 @@ #include "SkFrontBufferedStream.h" #include "SkImageEncoder.h" #include "SkImageEncoderPriv.h" +#include "SkMakeUnique.h" #include "SkMD5.h" #include "SkOSPath.h" #include "SkJpegEncoder.h" @@ -273,10 +274,9 @@ static void check(skiatest::Reporter* r, bool isIncomplete = supportsIncomplete; if (isIncomplete) { size_t size = stream->getLength(); - sk_sp<SkData> data((SkData::MakeFromStream(stream.get(), 2 * size / 3))); - codec.reset(SkCodec::NewFromData(data)); + codec = SkCodec::MakeFromData(SkData::MakeFromStream(stream.get(), 2 * size / 3)); } else { - codec.reset(SkCodec::NewFromStream(stream.release())); + codec = SkCodec::MakeFromStream(std::move(stream)); } if (!codec) { ERRORF(r, "Unable to decode '%s'", path); @@ -414,9 +414,9 @@ static void check(skiatest::Reporter* r, if (isIncomplete) { size_t size = stream->getLength(); sk_sp<SkData> data((SkData::MakeFromStream(stream.get(), 2 * size / 3))); - androidCodec.reset(SkAndroidCodec::NewFromData(data)); + androidCodec = SkAndroidCodec::MakeFromData(data); } else { - androidCodec.reset(SkAndroidCodec::NewFromStream(stream.release())); + androidCodec = SkAndroidCodec::MakeFromStream(std::move(stream)); } if (!androidCodec) { ERRORF(r, "Unable to decode '%s'", path); @@ -445,7 +445,7 @@ static void check(skiatest::Reporter* r, SkStream* bufferedStream = SkFrontBufferedStream::Create( new SkMemoryStream(std::move(fullData)), SkCodec::MinBufferedBytesNeeded()); REPORTER_ASSERT(r, bufferedStream); - codec.reset(SkCodec::NewFromStream(bufferedStream)); + codec = SkCodec::MakeFromStream(std::unique_ptr<SkStream>(bufferedStream)); REPORTER_ASSERT(r, codec); if (codec) { test_info(r, codec.get(), info, SkCodec::kSuccess, &codecDigest); @@ -530,12 +530,10 @@ DEF_TEST(Codec_raw, r) { static void test_invalid_stream(skiatest::Reporter* r, const void* stream, size_t len) { // Neither of these calls should return a codec. Bots should catch us if we leaked anything. - SkCodec* codec = SkCodec::NewFromStream(new SkMemoryStream(stream, len, false)); - REPORTER_ASSERT(r, !codec); - - SkAndroidCodec* androidCodec = - SkAndroidCodec::NewFromStream(new SkMemoryStream(stream, len, false)); - REPORTER_ASSERT(r, !androidCodec); + REPORTER_ASSERT(r, !SkCodec::MakeFromStream( + skstd::make_unique<SkMemoryStream>(stream, len, false))); + REPORTER_ASSERT(r, !SkAndroidCodec::MakeFromStream( + skstd::make_unique<SkMemoryStream>(stream, len, false))); } // Ensure that SkCodec::NewFromStream handles freeing the passed in SkStream, @@ -564,11 +562,8 @@ DEF_TEST(Codec_leaks, r) { DEF_TEST(Codec_null, r) { // Attempting to create an SkCodec or an SkAndroidCodec with null should not // crash. - SkCodec* codec = SkCodec::NewFromStream(nullptr); - REPORTER_ASSERT(r, !codec); - - SkAndroidCodec* androidCodec = SkAndroidCodec::NewFromStream(nullptr); - REPORTER_ASSERT(r, !androidCodec); + REPORTER_ASSERT(r, !SkCodec::MakeFromStream(nullptr)); + REPORTER_ASSERT(r, !SkAndroidCodec::MakeFromStream(nullptr)); } static void test_dimensions(skiatest::Reporter* r, const char path[]) { @@ -577,7 +572,7 @@ static void test_dimensions(skiatest::Reporter* r, const char path[]) { if (!stream) { return; } - std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(stream.release())); + std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::MakeFromStream(std::move(stream))); if (!codec) { ERRORF(r, "Unable to create codec '%s'", path); return; @@ -641,8 +636,7 @@ static void test_invalid(skiatest::Reporter* r, const char path[]) { if (!stream) { return; } - std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream.release())); - REPORTER_ASSERT(r, nullptr == codec); + REPORTER_ASSERT(r, !SkCodec::MakeFromStream(std::move(stream))); } DEF_TEST(Codec_Empty, r) { @@ -789,7 +783,7 @@ DEF_TEST(Codec_pngChunkReader, r) { ChunkReader chunkReader(r); // Now read the file with SkCodec. - std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(wStream.detachAsData(), &chunkReader)); + std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(wStream.detachAsData(), &chunkReader)); REPORTER_ASSERT(r, codec); if (!codec) { return; @@ -864,7 +858,8 @@ DEF_TEST(Codec_raw_notseekable, r) { return; } - std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(new NotAssetMemStream(std::move(data)))); + std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream( + skstd::make_unique<NotAssetMemStream>(std::move(data)))); REPORTER_ASSERT(r, codec); test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr); @@ -883,14 +878,14 @@ DEF_TEST(Codec_webp_peek, r) { } // The limit is less than webp needs to peek or read. - std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream( - new LimitedPeekingMemStream(data, 25))); + std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream( + skstd::make_unique<LimitedPeekingMemStream>(data, 25))); REPORTER_ASSERT(r, codec); test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr); // Similarly, a stream which does not peek should still succeed. - codec.reset(SkCodec::NewFromStream(new LimitedPeekingMemStream(data, 0))); + codec = SkCodec::MakeFromStream(skstd::make_unique<LimitedPeekingMemStream>(data, 0)); REPORTER_ASSERT(r, codec); test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr); @@ -913,7 +908,7 @@ DEF_TEST(Codec_wbmp_restrictive, r) { writeableData[1] = static_cast<uint8_t>(~0x9F); // SkCodec should support this. - std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(data)); + std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(data)); REPORTER_ASSERT(r, codec); if (!codec) { return; @@ -931,7 +926,7 @@ DEF_TEST(Codec_wbmp_max_size, r) { 0x83, 0xFF, 0x7F, // W: 65535 0x83, 0xFF, 0x7F }; // H: 65535 std::unique_ptr<SkStream> stream(new SkMemoryStream(maxSizeWbmp, sizeof(maxSizeWbmp), false)); - std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream.release())); + std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream(std::move(stream))); REPORTER_ASSERT(r, codec); if (!codec) return; @@ -945,7 +940,7 @@ DEF_TEST(Codec_wbmp_max_size, r) { 0x84, 0x80, 0x00, // W: 65536 0x84, 0x80, 0x00 }; // H: 65536 stream.reset(new SkMemoryStream(tooBigWbmp, sizeof(tooBigWbmp), false)); - codec.reset(SkCodec::NewFromStream(stream.release())); + codec = SkCodec::MakeFromStream(std::move(stream)); REPORTER_ASSERT(r, !codec); } @@ -958,7 +953,7 @@ DEF_TEST(Codec_jpeg_rewind, r) { } data = SkData::MakeSubset(data.get(), 0, data->size() / 2); - std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::NewFromData(data)); + std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::MakeFromData(data)); if (!codec) { ERRORF(r, "Unable to create codec '%s'.", path); return; @@ -1001,7 +996,7 @@ DEF_TEST(Codec_jpeg_rewind, r) { } static void check_color_xform(skiatest::Reporter* r, const char* path) { - std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(GetResourceAsStream(path).release())); + std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::MakeFromStream(GetResourceAsStream(path))); SkAndroidCodec::AndroidOptions opts; opts.fSampleSize = 3; @@ -1062,7 +1057,7 @@ static void check_round_trip(skiatest::Reporter* r, SkCodec* origCodec, const Sk sk_sp<SkData> data = sk_sp<SkData>(sk_tool_utils::EncodeImageToData(bm1, SkEncodedImageFormat::kPNG, 100)); - std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(data)); + std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(data)); REPORTER_ASSERT(r, color_type_match(info.colorType(), codec->getInfo().colorType())); REPORTER_ASSERT(r, alpha_type_match(info.alphaType(), codec->getInfo().alphaType())); @@ -1078,9 +1073,7 @@ static void check_round_trip(skiatest::Reporter* r, SkCodec* origCodec, const Sk } DEF_TEST(Codec_PngRoundTrip, r) { - const char* path = "mandrill_512_q075.jpg"; - std::unique_ptr<SkStream> stream(GetResourceAsStream(path)); - std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream.release())); + auto codec = SkCodec::MakeFromStream(GetResourceAsStream("mandrill_512_q075.jpg")); SkColorType colorTypesOpaque[] = { kRGB_565_SkColorType, kRGBA_8888_SkColorType, kBGRA_8888_SkColorType @@ -1090,14 +1083,10 @@ DEF_TEST(Codec_PngRoundTrip, r) { check_round_trip(r, codec.get(), newInfo); } - path = "grayscale.jpg"; - stream = GetResourceAsStream(path); - codec.reset(SkCodec::NewFromStream(stream.release())); + codec = SkCodec::MakeFromStream(GetResourceAsStream("grayscale.jpg")); check_round_trip(r, codec.get(), codec->getInfo()); - path = "yellow_rose.png"; - stream = GetResourceAsStream(path); - codec.reset(SkCodec::NewFromStream(stream.release())); + codec = SkCodec::MakeFromStream(GetResourceAsStream("yellow_rose.png")); SkColorType colorTypesWithAlpha[] = { kRGBA_8888_SkColorType, kBGRA_8888_SkColorType @@ -1115,9 +1104,7 @@ DEF_TEST(Codec_PngRoundTrip, r) { } } - path = "index8.png"; - stream = GetResourceAsStream(path); - codec.reset(SkCodec::NewFromStream(stream.release())); + codec = SkCodec::MakeFromStream(GetResourceAsStream("index8.png")); for (SkAlphaType alphaType : alphaTypes) { SkImageInfo newInfo = codec->getInfo().makeAlphaType(alphaType) @@ -1134,7 +1121,7 @@ static void test_conversion_possible(skiatest::Reporter* r, const char* path, return; } - std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream.release())); + std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream(std::move(stream))); if (!codec) { ERRORF(r, "failed to create a codec for %s", path); return; @@ -1211,7 +1198,7 @@ DEF_TEST(Codec_skipFullParse, r) { // Note that we cheat and hold on to the stream pointer, but SkCodec will // take ownership. We will not refer to the stream after the SkCodec // deletes it. - std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(streamObj.release())); + std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream(std::move(streamObj))); if (!codec) { ERRORF(r, "Failed to create codec for %s", path); return; @@ -1296,7 +1283,7 @@ DEF_TEST(Codec_fallBack, r) { return; } - std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream.release())); + std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream(std::move(stream))); if (!codec) { ERRORF(r, "Failed to create codec for %s,", file); continue; @@ -1328,7 +1315,7 @@ DEF_TEST(Codec_reusePng, r) { return; } - std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(stream.release())); + std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::MakeFromStream(std::move(stream))); if (!codec) { ERRORF(r, "Failed to create codec\n"); return; @@ -1358,8 +1345,7 @@ DEF_TEST(Codec_rowsDecoded, r) { } // This is enough to read the header etc, but no rows. - auto data = SkData::MakeFromStream(stream.get(), 99); - std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(data)); + std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(SkData::MakeFromStream(stream.get(), 99))); if (!codec) { ERRORF(r, "Failed to create codec\n"); return; @@ -1386,7 +1372,7 @@ static void test_invalid_images(skiatest::Reporter* r, const char* path, return; } - std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream.release())); + std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream(std::move(stream))); REPORTER_ASSERT(r, codec); test_info(r, codec.get(), codec->getInfo().makeColorType(kN32_SkColorType), expectedResult, @@ -1408,7 +1394,7 @@ static void test_invalid_header(skiatest::Reporter* r, const char* path) { return; } - std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream.release())); + std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream(std::move(stream))); REPORTER_ASSERT(r, !codec); } @@ -1429,7 +1415,7 @@ DEF_TEST(Codec_InvalidAnimated, r) { return; } - std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream.release())); + std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream(std::move(stream))); REPORTER_ASSERT(r, codec); if (!codec) { return; @@ -1491,7 +1477,7 @@ static void test_encode_icc(skiatest::Reporter* r, SkEncodedImageFormat format, SkDynamicMemoryWStream srgbBuf; encode_format(&srgbBuf, pixmap, unpremulBehavior, format); sk_sp<SkData> srgbData = srgbBuf.detachAsData(); - std::unique_ptr<SkCodec> srgbCodec(SkCodec::NewFromData(srgbData)); + std::unique_ptr<SkCodec> srgbCodec(SkCodec::MakeFromData(srgbData)); REPORTER_ASSERT(r, srgbCodec->getInfo().colorSpace() == SkColorSpace::MakeSRGB().get()); // Test with P3 color space. @@ -1501,7 +1487,7 @@ static void test_encode_icc(skiatest::Reporter* r, SkEncodedImageFormat format, pixmap.setColorSpace(p3); encode_format(&p3Buf, pixmap, unpremulBehavior, format); sk_sp<SkData> p3Data = p3Buf.detachAsData(); - std::unique_ptr<SkCodec> p3Codec(SkCodec::NewFromData(p3Data)); + std::unique_ptr<SkCodec> p3Codec(SkCodec::MakeFromData(p3Data)); REPORTER_ASSERT(r, p3Codec->getInfo().colorSpace()->gammaCloseToSRGB()); SkMatrix44 mat0(SkMatrix44::kUninitialized_Constructor); SkMatrix44 mat1(SkMatrix44::kUninitialized_Constructor); diff --git a/tests/ColorSpaceTest.cpp b/tests/ColorSpaceTest.cpp index 9b2e1b3dc6..efbfe8a29c 100644 --- a/tests/ColorSpaceTest.cpp +++ b/tests/ColorSpaceTest.cpp @@ -53,7 +53,7 @@ static void test_path(skiatest::Reporter* r, const char* path, return; } - std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream.release())); + std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream(std::move(stream))); REPORTER_ASSERT(r, nullptr != codec); if (!codec) { return; diff --git a/tests/ExifTest.cpp b/tests/ExifTest.cpp index f9e357d5e6..181f0f9f04 100644 --- a/tests/ExifTest.cpp +++ b/tests/ExifTest.cpp @@ -16,13 +16,12 @@ DEF_TEST(ExifOrientation, r) { return; } - std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream.release())); + std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream(std::move(stream))); REPORTER_ASSERT(r, nullptr != codec); SkCodec::Origin origin = codec->getOrigin(); REPORTER_ASSERT(r, SkCodec::kTopRight_Origin == origin); - stream = GetResourceAsStream("mandrill_512_q075.jpg"); - codec.reset(SkCodec::NewFromStream(stream.release())); + codec = SkCodec::MakeFromStream(GetResourceAsStream("mandrill_512_q075.jpg")); REPORTER_ASSERT(r, nullptr != codec); origin = codec->getOrigin(); REPORTER_ASSERT(r, SkCodec::kTopLeft_Origin == origin); diff --git a/tests/FrontBufferedStreamTest.cpp b/tests/FrontBufferedStreamTest.cpp index 69ff488358..aa2dc89a11 100644 --- a/tests/FrontBufferedStreamTest.cpp +++ b/tests/FrontBufferedStreamTest.cpp @@ -285,5 +285,5 @@ DEF_TEST(ShortFrontBufferedStream, reporter) { // This will fail to create a codec. However, what we really want to test is that we // won't read past the end of the stream. - std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream.release())); + std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream(std::move(stream))); } diff --git a/tests/GifTest.cpp b/tests/GifTest.cpp index 5749df1be5..daa5cf56d0 100644 --- a/tests/GifTest.cpp +++ b/tests/GifTest.cpp @@ -195,7 +195,7 @@ DEF_TEST(Gif, reporter) { // Likewise, incremental decoding should succeed here. { sk_sp<SkData> data = SkData::MakeWithoutCopy(gGIFDataNoColormap, 31); - std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(data)); + std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(data)); REPORTER_ASSERT(reporter, codec); if (codec) { auto info = codec->getInfo().makeColorType(kN32_SkColorType); @@ -232,7 +232,7 @@ DEF_TEST(Gif_Sampled, r) { return; } - std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(stream.release())); + std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::MakeFromStream(std::move(stream))); REPORTER_ASSERT(r, codec); if (!codec) { return; @@ -258,7 +258,7 @@ DEF_TEST(Codec_GifTruncated, r) { // This is right before the header for the first image. data = SkData::MakeSubset(data.get(), 0, 446); - std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(data)); + std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(data)); REPORTER_ASSERT(r, !codec); } @@ -270,7 +270,7 @@ DEF_TEST(Codec_GifTruncated2, r) { // This is after the header, but before the color table. data = SkData::MakeSubset(data.get(), 0, 23); - std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(data)); + std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(data)); if (!codec) { ERRORF(r, "Failed to create codec with partial data"); return; diff --git a/tests/StreamBufferTest.cpp b/tests/StreamBufferTest.cpp index 96bca35c92..35bebad274 100644 --- a/tests/StreamBufferTest.cpp +++ b/tests/StreamBufferTest.cpp @@ -6,6 +6,7 @@ */ #include "SkData.h" +#include "SkMakeUnique.h" #include "SkOSPath.h" #include "SkStream.h" #include "SkStreamBuffer.h" @@ -25,8 +26,9 @@ static void test_get_data_at_position(skiatest::Reporter* r, SkStreamBuffer* buf } // Test buffering from the beginning, by different amounts. -static void test_buffer_from_beginning(skiatest::Reporter* r, SkStream* stream, size_t length) { - SkStreamBuffer buffer(stream); +static void test_buffer_from_beginning(skiatest::Reporter* r, std::unique_ptr<SkStream> stream, + size_t length) { + SkStreamBuffer buffer(std::move(stream)); // Buffer an arbitrary amount: size_t buffered = length / 2; @@ -42,9 +44,9 @@ static void test_buffer_from_beginning(skiatest::Reporter* r, SkStream* stream, } // Test flushing the stream as we read. -static void test_flushing(skiatest::Reporter* r, SkStream* stream, size_t length, +static void test_flushing(skiatest::Reporter* r, std::unique_ptr<SkStream> stream, size_t length, bool getDataAtPosition) { - SkStreamBuffer buffer(stream); + SkStreamBuffer buffer(std::move(stream)); const size_t step = 5; for (size_t position = 0; position + step <= length; position += step) { REPORTER_ASSERT(r, buffer.buffer(step)); @@ -80,12 +82,12 @@ DEF_TEST(StreamBuffer, r) { } struct { - std::function<SkStream*()> createStream; - bool skipIfNoTmpDir; + std::function<std::unique_ptr<SkStream>()> createStream; + bool skipIfNoTmpDir; } factories[] = { - { [&data]() { return new SkMemoryStream(data); }, false }, - { [&data]() { return new NotAssetMemStream(data); }, false }, - { [&path]() { return new SkFILEStream(path.c_str()); }, true }, + { [&data]() { return skstd::make_unique<SkMemoryStream>(data); }, false }, + { [&data]() { return skstd::make_unique<NotAssetMemStream>(data); }, false }, + { [&path]() { return skstd::make_unique<SkFILEStream>(path.c_str()); }, true }, }; for (auto f : factories) { @@ -98,8 +100,9 @@ DEF_TEST(StreamBuffer, r) { } // Stream that will receive more data. Will be owned by the SkStreamBuffer. - HaltingStream* stream = new HaltingStream(data, 6); - SkStreamBuffer buffer(stream); + auto halting = skstd::make_unique<HaltingStream>(data, 6); + HaltingStream* peekHalting = halting.get(); + SkStreamBuffer buffer(std::move(halting)); // Can only buffer less than what's available (6). REPORTER_ASSERT(r, !buffer.buffer(7)); @@ -107,7 +110,7 @@ DEF_TEST(StreamBuffer, r) { REPORTER_ASSERT(r, !memcmp(buffer.get(), gText, 5)); // Add some more data. We can buffer and read all of it. - stream->addNewData(8); + peekHalting->addNewData(8); REPORTER_ASSERT(r, buffer.buffer(14)); REPORTER_ASSERT(r, !memcmp(buffer.get(), gText, 14)); @@ -116,9 +119,9 @@ DEF_TEST(StreamBuffer, r) { // Add some data, and try to read more. Can only read what is // available. - stream->addNewData(9); + peekHalting->addNewData(9); REPORTER_ASSERT(r, !buffer.buffer(13)); - stream->addNewData(4); + peekHalting->addNewData(4); REPORTER_ASSERT(r, buffer.buffer(13)); // Do not call get on this data. We'll come back to this data after adding @@ -126,7 +129,7 @@ DEF_TEST(StreamBuffer, r) { buffer.flush(); const size_t remaining = size - 27; REPORTER_ASSERT(r, remaining > 0); - stream->addNewData(remaining); + peekHalting->addNewData(remaining); REPORTER_ASSERT(r, buffer.buffer(remaining)); REPORTER_ASSERT(r, !memcmp(buffer.get(), gText + 27, remaining)); diff --git a/tests/YUVTest.cpp b/tests/YUVTest.cpp index 0c31c0929a..825e2d1628 100644 --- a/tests/YUVTest.cpp +++ b/tests/YUVTest.cpp @@ -20,7 +20,7 @@ static void codec_yuv(skiatest::Reporter* reporter, if (!stream) { return; } - std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream.release())); + std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream(std::move(stream))); REPORTER_ASSERT(reporter, codec); if (!codec) { return; diff --git a/third_party/gif/SkGifImageReader.h b/third_party/gif/SkGifImageReader.h index 0ba767ee89..d87b68f450 100644 --- a/third_party/gif/SkGifImageReader.h +++ b/third_party/gif/SkGifImageReader.h @@ -280,13 +280,13 @@ private: class SkGifImageReader final : public SkFrameHolder { public: // This takes ownership of stream. - SkGifImageReader(SkStream* stream) + SkGifImageReader(std::unique_ptr<SkStream> stream) : m_client(nullptr) , m_state(SkGIFType) , m_bytesToConsume(6) // Number of bytes for GIF type, either "GIF87a" or "GIF89a". , m_version(0) , m_loopCount(cLoopCountNotSeen) - , m_streamBuffer(stream) + , m_streamBuffer(std::move(stream)) , m_parseCompleted(false) , m_firstFrameHasAlpha(false) { diff --git a/tools/colorspaceinfo.cpp b/tools/colorspaceinfo.cpp index ec1101946c..c35e735d8c 100644 --- a/tools/colorspaceinfo.cpp +++ b/tools/colorspaceinfo.cpp @@ -463,7 +463,7 @@ int main(int argc, char** argv) { if (FLAGS_icc) { colorSpace = SkColorSpace::MakeICC(data->bytes(), data->size()); } else { - codec.reset(SkCodec::NewFromData(data)); + codec = SkCodec::MakeFromData(data); colorSpace = sk_ref_sp(codec->getInfo().colorSpace()); SkDebugf("SkCodec would naturally decode as colorType=%s\n", sk_tool_utils::colortype_name(codec->getInfo().colorType())); diff --git a/tools/get_images_from_skps.cpp b/tools/get_images_from_skps.cpp index 0debe367bd..0a2164cf39 100644 --- a/tools/get_images_from_skps.cpp +++ b/tools/get_images_from_skps.cpp @@ -61,7 +61,7 @@ struct Sniffer : public SkPixelSerializer { gSeen.add(digest); sk_sp<SkData> data(SkData::MakeWithoutCopy(ptr, len)); - std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(data)); + std::unique_ptr<SkCodec> codec = SkCodec::MakeFromData(data); if (!codec) { // FIXME: This code is currently unreachable because we create an empty generator when // we fail to create a codec. diff --git a/tools/skdiff/skdiff_utils.cpp b/tools/skdiff/skdiff_utils.cpp index 7a1b7e88a4..fddd99cef3 100644 --- a/tools/skdiff/skdiff_utils.cpp +++ b/tools/skdiff/skdiff_utils.cpp @@ -35,7 +35,7 @@ sk_sp<SkData> read_file(const char* file_path) { } bool get_bitmap(sk_sp<SkData> fileBits, DiffResource& resource, bool sizeOnly) { - std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(fileBits)); + auto codec = SkCodec::MakeFromData(fileBits); if (!codec) { SkDebugf("ERROR: could not create codec for <%s>\n", resource.fFullPath.c_str()); resource.fStatus = DiffResource::kCouldNotDecode_Status; |