From: reed Date: Mon, 12 Sep 2016 19:01:44 +0000 (-0700) Subject: change SkStreams to work with sk_sp instead of SkData* X-Git-Tag: accepted/tizen/5.0/unified/20181102.025319~106^2~461 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=42943c8aa9c611c18ad0f1a30a27669f3d82239c;p=platform%2Fupstream%2FlibSkiaSharp.git change SkStreams to work with sk_sp instead of SkData* BUG=skia: GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2333713002 Review-Url: https://codereview.chromium.org/2333713002 --- diff --git a/bench/AndroidCodecBench.cpp b/bench/AndroidCodecBench.cpp index 6443ce6..97e1176 100644 --- a/bench/AndroidCodecBench.cpp +++ b/bench/AndroidCodecBench.cpp @@ -29,7 +29,7 @@ bool AndroidCodecBench::isSuitableFor(Backend backend) { } void AndroidCodecBench::onDelayedSetup() { - SkAutoTDelete codec(SkAndroidCodec::NewFromData(fData.get())); + SkAutoTDelete codec(SkAndroidCodec::NewFromData(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.get())); + codec.reset(SkAndroidCodec::NewFromData(fData)); #ifdef SK_DEBUG const SkCodec::Result result = #endif diff --git a/bench/BitmapRegionDecoderBench.cpp b/bench/BitmapRegionDecoderBench.cpp index 709ad0e..dd60b18 100644 --- a/bench/BitmapRegionDecoderBench.cpp +++ b/bench/BitmapRegionDecoderBench.cpp @@ -36,8 +36,7 @@ bool BitmapRegionDecoderBench::isSuitableFor(Backend backend) { } void BitmapRegionDecoderBench::onDelayedSetup() { - fBRD.reset(SkBitmapRegionDecoder::Create(fData.get(), - SkBitmapRegionDecoder::kAndroidCodec_Strategy)); + fBRD.reset(SkBitmapRegionDecoder::Create(fData, SkBitmapRegionDecoder::kAndroidCodec_Strategy)); } void BitmapRegionDecoderBench::onDraw(int n, SkCanvas* canvas) { diff --git a/bench/CodecBench.cpp b/bench/CodecBench.cpp index e3bf1cc..9bd404d 100644 --- a/bench/CodecBench.cpp +++ b/bench/CodecBench.cpp @@ -26,7 +26,7 @@ CodecBench::CodecBench(SkString baseName, SkData* encoded, SkColorType colorType alpha_type_to_str(alphaType)); #ifdef SK_DEBUG // Ensure that we can create an SkCodec from this data. - SkAutoTDelete codec(SkCodec::NewFromData(fData.get())); + SkAutoTDelete codec(SkCodec::NewFromData(fData)); SkASSERT(codec); #endif } @@ -40,7 +40,7 @@ bool CodecBench::isSuitableFor(Backend backend) { } void CodecBench::onDelayedSetup() { - SkAutoTDelete codec(SkCodec::NewFromData(fData.get())); + SkAutoTDelete codec(SkCodec::NewFromData(fData)); fInfo = codec->getInfo().makeColorType(fColorType) .makeAlphaType(fAlphaType) @@ -59,7 +59,7 @@ void CodecBench::onDraw(int n, SkCanvas* canvas) { } for (int i = 0; i < n; i++) { colorCount = 256; - codec.reset(SkCodec::NewFromData(fData.get())); + codec.reset(SkCodec::NewFromData(fData)); #ifdef SK_DEBUG const SkCodec::Result result = #endif diff --git a/bench/ColorCodecBench.cpp b/bench/ColorCodecBench.cpp index 9f57ddc..9aa5d73 100644 --- a/bench/ColorCodecBench.cpp +++ b/bench/ColorCodecBench.cpp @@ -40,7 +40,7 @@ bool ColorCodecBench::isSuitableFor(Backend backend) { } void ColorCodecBench::decodeAndXform() { - SkAutoTDelete codec(SkCodec::NewFromData(fEncoded.get())); + SkAutoTDelete codec(SkCodec::NewFromData(fEncoded)); SkASSERT(codec); #ifdef SK_DEBUG @@ -52,7 +52,7 @@ void ColorCodecBench::decodeAndXform() { #if defined(SK_TEST_QCMS) void ColorCodecBench::decodeAndXformQCMS() { - SkAutoTDelete codec(SkCodec::NewFromData(fEncoded.get())); + SkAutoTDelete codec(SkCodec::NewFromData(fEncoded)); #ifdef SK_DEBUG const SkCodec::Result result = #endif @@ -135,7 +135,7 @@ void ColorCodecBench::xformOnlyQCMS() { #endif void ColorCodecBench::onDelayedSetup() { - SkAutoTDelete codec(SkCodec::NewFromData(fEncoded.get())); + SkAutoTDelete codec(SkCodec::NewFromData(fEncoded)); fSrcData = codec->getICCData(); sk_sp dstData = SkData::MakeFromFileName( GetResourcePath("icc_profiles/HP_ZR30w.icc").c_str()); diff --git a/bench/nanobench.cpp b/bench/nanobench.cpp index a2f931e..ae415fa 100644 --- a/bench/nanobench.cpp +++ b/bench/nanobench.cpp @@ -529,7 +529,7 @@ static Target* is_enabled(Benchmark* bench, const Config& config) { return target; } -static bool valid_brd_bench(SkData* encoded, SkColorType colorType, uint32_t sampleSize, +static bool valid_brd_bench(sk_sp encoded, SkColorType colorType, uint32_t sampleSize, uint32_t minOutputSize, int* width, int* height) { SkAutoTDelete brd( SkBitmapRegionDecoder::Create(encoded, SkBitmapRegionDecoder::kAndroidCodec_Strategy)); @@ -756,7 +756,7 @@ public: continue; } sk_sp encoded(SkData::MakeFromFileName(path.c_str())); - SkAutoTDelete codec(SkCodec::NewFromData(encoded.get())); + SkAutoTDelete codec(SkCodec::NewFromData(encoded)); if (!codec) { // Nothing to time. SkDebugf("Cannot find codec for %s\n", path.c_str()); @@ -840,7 +840,7 @@ public: continue; } sk_sp encoded(SkData::MakeFromFileName(path.c_str())); - SkAutoTDelete codec(SkAndroidCodec::NewFromData(encoded.get())); + SkAutoTDelete codec(SkAndroidCodec::NewFromData(encoded)); if (!codec) { // Nothing to time. SkDebugf("Cannot find codec for %s\n", path.c_str()); @@ -896,7 +896,7 @@ public: int width = 0; int height = 0; - if (!valid_brd_bench(encoded.get(), colorType, sampleSize, minOutputSize, + if (!valid_brd_bench(encoded, colorType, sampleSize, minOutputSize, &width, &height)) { break; } diff --git a/dm/DM.cpp b/dm/DM.cpp index a815827..a8fb43b 100644 --- a/dm/DM.cpp +++ b/dm/DM.cpp @@ -496,7 +496,7 @@ static void push_codec_srcs(Path path) { info("Couldn't read %s.", path.c_str()); return; } - SkAutoTDelete codec(SkCodec::NewFromData(encoded.get())); + SkAutoTDelete codec(SkCodec::NewFromData(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 fcc4a33..c3c8a7b 100644 --- a/dm/DMSrcSink.cpp +++ b/dm/DMSrcSink.cpp @@ -104,8 +104,7 @@ static SkBitmapRegionDecoder* create_brd(Path path) { if (!encoded) { return NULL; } - return SkBitmapRegionDecoder::Create(encoded.get(), - SkBitmapRegionDecoder::kAndroidCodec_Strategy); + return SkBitmapRegionDecoder::Create(encoded, SkBitmapRegionDecoder::kAndroidCodec_Strategy); } Error BRDSrc::draw(SkCanvas* canvas) const { @@ -384,7 +383,7 @@ Error CodecSrc::draw(SkCanvas* canvas) const { return SkStringPrintf("Couldn't read %s.", fPath.c_str()); } - SkAutoTDelete codec(SkCodec::NewFromData(encoded.get())); + SkAutoTDelete codec(SkCodec::NewFromData(encoded)); if (nullptr == codec.get()) { return SkStringPrintf("Couldn't create codec for %s.", fPath.c_str()); } @@ -632,7 +631,7 @@ Error CodecSrc::draw(SkCanvas* canvas) const { SkISize CodecSrc::size() const { sk_sp encoded(SkData::MakeFromFileName(fPath.c_str())); - SkAutoTDelete codec(SkCodec::NewFromData(encoded.get())); + SkAutoTDelete codec(SkCodec::NewFromData(encoded)); if (nullptr == codec) { return SkISize::Make(0, 0); } @@ -668,7 +667,7 @@ Error AndroidCodecSrc::draw(SkCanvas* canvas) const { if (!encoded) { return SkStringPrintf("Couldn't read %s.", fPath.c_str()); } - SkAutoTDelete codec(SkAndroidCodec::NewFromData(encoded.get())); + SkAutoTDelete codec(SkAndroidCodec::NewFromData(encoded)); if (nullptr == codec.get()) { return SkStringPrintf("Couldn't create android codec for %s.", fPath.c_str()); } @@ -721,7 +720,7 @@ Error AndroidCodecSrc::draw(SkCanvas* canvas) const { SkISize AndroidCodecSrc::size() const { sk_sp encoded(SkData::MakeFromFileName(fPath.c_str())); - SkAutoTDelete codec(SkAndroidCodec::NewFromData(encoded.get())); + SkAutoTDelete codec(SkAndroidCodec::NewFromData(encoded)); if (nullptr == codec) { return SkISize::Make(0, 0); } @@ -828,7 +827,7 @@ Error ImageGenSrc::draw(SkCanvas* canvas) const { SkISize ImageGenSrc::size() const { sk_sp encoded(SkData::MakeFromFileName(fPath.c_str())); - SkAutoTDelete codec(SkCodec::NewFromData(encoded.get())); + SkAutoTDelete codec(SkCodec::NewFromData(encoded)); if (nullptr == codec) { return SkISize::Make(0, 0); } @@ -873,7 +872,7 @@ Error ColorCodecSrc::draw(SkCanvas* canvas) const { return SkStringPrintf("Couldn't read %s.", fPath.c_str()); } - SkAutoTDelete codec(SkCodec::NewFromData(encoded.get())); + SkAutoTDelete codec(SkCodec::NewFromData(encoded)); if (nullptr == codec.get()) { return SkStringPrintf("Couldn't create codec for %s.", fPath.c_str()); } @@ -979,7 +978,7 @@ Error ColorCodecSrc::draw(SkCanvas* canvas) const { SkISize ColorCodecSrc::size() const { sk_sp encoded(SkData::MakeFromFileName(fPath.c_str())); - SkAutoTDelete codec(SkCodec::NewFromData(encoded.get())); + SkAutoTDelete codec(SkCodec::NewFromData(encoded)); if (nullptr == codec) { return SkISize::Make(0, 0); } diff --git a/fuzz/Fuzz.h b/fuzz/Fuzz.h index 16a7f98..0ab3c1b 100644 --- a/fuzz/Fuzz.h +++ b/fuzz/Fuzz.h @@ -14,7 +14,7 @@ class Fuzz : SkNoncopyable { public: - explicit Fuzz(SkData*); + explicit Fuzz(sk_sp); // Returns the total number of "random" bytes available. size_t size(); diff --git a/fuzz/fuzz.cpp b/fuzz/fuzz.cpp index d2dc787..ec47aa2 100644 --- a/fuzz/fuzz.cpp +++ b/fuzz/fuzz.cpp @@ -34,11 +34,11 @@ static int printUsage(const char* name) { } static uint8_t calculate_option(SkData*); -static int fuzz_api(SkData*); -static int fuzz_img(SkData*, uint8_t, uint8_t); -static int fuzz_skp(SkData*); -static int fuzz_icc(SkData*); -static int fuzz_color_deserialize(SkData*); +static int fuzz_api(sk_sp); +static int fuzz_img(sk_sp, uint8_t, uint8_t); +static int fuzz_skp(sk_sp); +static int fuzz_icc(sk_sp); +static int fuzz_color_deserialize(sk_sp); int main(int argc, char** argv) { SkCommandLineFlags::Parse(argc, argv); @@ -54,21 +54,21 @@ int main(int argc, char** argv) { if (!FLAGS_type.isEmpty()) { switch (FLAGS_type[0][0]) { - case 'a': return fuzz_api(bytes.get()); + case 'a': return fuzz_api(bytes); - case 'c': return fuzz_color_deserialize(bytes.get()); + case 'c': return fuzz_color_deserialize(bytes); case 'i': if (FLAGS_type[0][1] == 'c') { //icc - return fuzz_icc(bytes.get()); + return fuzz_icc(bytes); } // We only allow one degree of freedom to avoid a search space explosion for afl-fuzz. if (FLAGS_type[0][6] == 's') { // image_scale - return fuzz_img(bytes.get(), option, 0); + return fuzz_img(bytes, option, 0); } // image_mode - return fuzz_img(bytes.get(), 0, option); - case 's': return fuzz_skp(bytes.get()); + return fuzz_img(bytes, 0, option); + case 's': return fuzz_skp(bytes); } } return printUsage(argv[0]); @@ -88,7 +88,7 @@ static uint8_t calculate_option(SkData* bytes) { return total; } -int fuzz_api(SkData* bytes) { +int fuzz_api(sk_sp bytes) { const char* name = FLAGS_name.isEmpty() ? "" : FLAGS_name[0]; for (auto r = SkTRegistry::Head(); r; r = r->next()) { @@ -117,7 +117,7 @@ static void dump_png(SkBitmap bitmap) { } } -int fuzz_img(SkData* bytes, uint8_t scale, uint8_t mode) { +int fuzz_img(sk_sp bytes, uint8_t scale, uint8_t mode) { // We can scale 1x, 2x, 4x, 8x, 16x scale = scale % 5; float fscale = (float)pow(2.0f, scale); @@ -360,7 +360,7 @@ int fuzz_img(SkData* bytes, uint8_t scale, uint8_t mode) { return 0; } -int fuzz_skp(SkData* bytes) { +int fuzz_skp(sk_sp bytes) { SkMemoryStream stream(bytes); SkDebugf("Decoding\n"); sk_sp pic(SkPicture::MakeFromStream(&stream)); @@ -381,7 +381,7 @@ int fuzz_skp(SkData* bytes) { return 0; } -int fuzz_icc(SkData* bytes) { +int fuzz_icc(sk_sp bytes) { sk_sp space(SkColorSpace::NewICC(bytes->data(), bytes->size())); if (!space) { SkDebugf("[terminated] Couldn't decode ICC.\n"); @@ -391,7 +391,7 @@ int fuzz_icc(SkData* bytes) { return 0; } -int fuzz_color_deserialize(SkData* bytes) { +int fuzz_color_deserialize(sk_sp bytes) { sk_sp space(SkColorSpace::Deserialize(bytes->data(), bytes->size())); if (!space) { SkDebugf("[terminated] Couldn't deserialize Colorspace.\n"); @@ -401,7 +401,7 @@ int fuzz_color_deserialize(SkData* bytes) { return 0; } -Fuzz::Fuzz(SkData* bytes) : fBytes(SkSafeRef(bytes)), fNextByte(0) {} +Fuzz::Fuzz(sk_sp bytes) : fBytes(bytes), fNextByte(0) {} void Fuzz::signalBug () { SkDebugf("Signal bug\n"); raise(SIGSEGV); } void Fuzz::signalBoring() { SkDebugf("Signal boring\n"); exit(0); } diff --git a/gyp/skia_for_android_framework_defines.gypi b/gyp/skia_for_android_framework_defines.gypi index 26f6998..2937f84 100644 --- a/gyp/skia_for_android_framework_defines.gypi +++ b/gyp/skia_for_android_framework_defines.gypi @@ -30,6 +30,7 @@ 'SK_SUPPORT_LEGACY_XFERMODE_PTR', 'SK_IGNORE_GPU_DITHER', 'SK_SUPPORT_LEGACY_PICTUREINSTALLPIXELREF', + 'SK_SUPPORT_LEGACY_STREAM_DATA', ], }, } diff --git a/include/android/SkBitmapRegionDecoder.h b/include/android/SkBitmapRegionDecoder.h index 913f947..b8922d4 100644 --- a/include/android/SkBitmapRegionDecoder.h +++ b/include/android/SkBitmapRegionDecoder.h @@ -29,8 +29,7 @@ public: * @param strategy Strategy used for scaling and subsetting * @return Tries to create an SkBitmapRegionDecoder, returns NULL on failure */ - static SkBitmapRegionDecoder* Create( - SkData* data, Strategy strategy); + static SkBitmapRegionDecoder* Create(sk_sp, Strategy strategy); /* * @param stream Takes ownership of the stream diff --git a/include/codec/SkAndroidCodec.h b/include/codec/SkAndroidCodec.h index 7fee5be..c7587b6 100644 --- a/include/codec/SkAndroidCodec.h +++ b/include/codec/SkAndroidCodec.h @@ -37,10 +37,11 @@ public: * * The SkPngChunkReader handles unknown chunks in PNGs. * See SkCodec.h for more details. - * - * Will take a ref if it returns a codec, else will not affect the data. */ - static SkAndroidCodec* NewFromData(SkData*, SkPngChunkReader* = NULL); + static SkAndroidCodec* NewFromData(sk_sp, SkPngChunkReader* = NULL); + static SkAndroidCodec* NewFromData(SkData* data, SkPngChunkReader* reader) { + return NewFromData(sk_ref_sp(data), reader); + } virtual ~SkAndroidCodec() {} diff --git a/include/codec/SkCodec.h b/include/codec/SkCodec.h index ced0c63..bee4f3c 100644 --- a/include/codec/SkCodec.h +++ b/include/codec/SkCodec.h @@ -95,10 +95,11 @@ public: * failure to decode the image. * If the PNG does not contain unknown chunks, the SkPngChunkReader * will not be used or modified. - * - * Will take a ref if it returns a codec, else will not affect the data. */ - static SkCodec* NewFromData(SkData*, SkPngChunkReader* = NULL); + static SkCodec* NewFromData(sk_sp, SkPngChunkReader* = NULL); + static SkCodec* NewFromData(SkData* data, SkPngChunkReader* reader) { + return NewFromData(sk_ref_sp(data), reader); + } virtual ~SkCodec(); diff --git a/include/core/SkStream.h b/include/core/SkStream.h index 4bfaeda..48ac577 100644 --- a/include/core/SkStream.h +++ b/include/core/SkStream.h @@ -291,11 +291,13 @@ public: /** If copyData is true, the stream makes a private copy of the data. */ SkMemoryStream(const void* data, size_t length, bool copyData = false); +#ifdef SK_SUPPORT_LEGACY_STREAM_DATA /** Use the specified data as the memory for this stream. * The stream will call ref() on the data (assuming it is not NULL). * DEPRECATED */ SkMemoryStream(SkData*); +#endif /** Creates the stream to read from the specified data */ SkMemoryStream(sk_sp); @@ -312,17 +314,24 @@ public: */ void setMemoryOwned(const void* data, size_t length); + sk_sp asData() const { return fData; } + void setData(sk_sp); +#ifdef SK_SUPPORT_LEGACY_STREAM_DATA /** Return the stream's data in a SkData. * The caller must call unref() when it is finished using the data. */ - SkData* copyToData() const; + SkData* copyToData() const { return asData().release(); } /** * Use the specified data as the memory for this stream. * The stream will call ref() on the data (assuming it is not NULL). * The function returns the data parameter as a convenience. */ - SkData* setData(SkData*); + SkData* setData(SkData* data) { + this->setData(sk_ref_sp(data)); + return data; + } +#endif void skipToAlign4(); const void* getAtPos(); @@ -404,11 +413,18 @@ public: void copyTo(void* dst) const; void writeToStream(SkWStream* dst) const; + sk_sp snapshotAsData() const; + // Return the contents as SkData, and then reset the stream. + sk_sp detachAsData(); +#ifdef SK_SUPPORT_LEGACY_STREAM_DATA /** * Return a copy of the data written so far. This call is responsible for * calling unref() when they are finished with the data. */ - SkData* copyToData() const; + SkData* copyToData() const { + return snapshotAsData().release(); + } +#endif /** Reset, returning a reader stream with the current content. */ SkStreamAsset* detachAsStream(); diff --git a/public.bzl b/public.bzl index e3dcade..7a0719e 100644 --- a/public.bzl +++ b/public.bzl @@ -611,6 +611,7 @@ DEFINES_ALL = [ "SK_SUPPORT_LEGACY_TYPEFACE_PTR", "SK_SUPPORT_LEGACY_XFERMODE_PTR", "SK_SUPPORT_LEGACY_PICTUREINSTALLPIXELREF", + "SK_SUPPORT_LEGACY_STREAM_DATA", ] ################################################################################ diff --git a/samplecode/DecodeFile.h b/samplecode/DecodeFile.h index 801d67c..b954af6 100644 --- a/samplecode/DecodeFile.h +++ b/samplecode/DecodeFile.h @@ -15,7 +15,7 @@ static inline bool decode_file(const char* filename, SkBitmap* bitmap, bool requireUnpremul = false) { SkASSERT(kIndex_8_SkColorType != colorType); sk_sp data(SkData::MakeFromFileName(filename)); - SkAutoTDelete codec(SkCodec::NewFromData(data.get())); + SkAutoTDelete codec(SkCodec::NewFromData(data)); if (!codec) { return false; } diff --git a/src/android/SkBitmapRegionDecoder.cpp b/src/android/SkBitmapRegionDecoder.cpp index 324d1be..6dd48c5 100644 --- a/src/android/SkBitmapRegionDecoder.cpp +++ b/src/android/SkBitmapRegionDecoder.cpp @@ -12,7 +12,7 @@ #include "SkCodecPriv.h" SkBitmapRegionDecoder* SkBitmapRegionDecoder::Create( - SkData* data, Strategy strategy) { + sk_sp data, Strategy strategy) { return SkBitmapRegionDecoder::Create(new SkMemoryStream(data), strategy); } diff --git a/src/codec/SkAndroidCodec.cpp b/src/codec/SkAndroidCodec.cpp index db36564..2324243 100644 --- a/src/codec/SkAndroidCodec.cpp +++ b/src/codec/SkAndroidCodec.cpp @@ -55,7 +55,7 @@ SkAndroidCodec* SkAndroidCodec::NewFromStream(SkStream* stream, SkPngChunkReader } } -SkAndroidCodec* SkAndroidCodec::NewFromData(SkData* data, SkPngChunkReader* chunkReader) { +SkAndroidCodec* SkAndroidCodec::NewFromData(sk_sp data, SkPngChunkReader* chunkReader) { if (!data) { return nullptr; } diff --git a/src/codec/SkCodec.cpp b/src/codec/SkCodec.cpp index 2372f4c..b076954 100644 --- a/src/codec/SkCodec.cpp +++ b/src/codec/SkCodec.cpp @@ -107,7 +107,7 @@ SkCodec* SkCodec::NewFromStream(SkStream* stream, return nullptr; } -SkCodec* SkCodec::NewFromData(SkData* data, SkPngChunkReader* reader) { +SkCodec* SkCodec::NewFromData(sk_sp data, SkPngChunkReader* reader) { if (!data) { return nullptr; } diff --git a/src/codec/SkCodecImageGenerator.cpp b/src/codec/SkCodecImageGenerator.cpp index 1bae1df..8108f0d 100644 --- a/src/codec/SkCodecImageGenerator.cpp +++ b/src/codec/SkCodecImageGenerator.cpp @@ -7,7 +7,7 @@ #include "SkCodecImageGenerator.h" -SkImageGenerator* SkCodecImageGenerator::NewFromEncodedCodec(SkData* data) { +SkImageGenerator* SkCodecImageGenerator::NewFromEncodedCodec(sk_sp data) { SkCodec* codec = SkCodec::NewFromData(data); if (nullptr == codec) { return nullptr; @@ -24,10 +24,10 @@ static SkImageInfo make_premul(const SkImageInfo& info) { return info; } -SkCodecImageGenerator::SkCodecImageGenerator(SkCodec* codec, SkData* data) +SkCodecImageGenerator::SkCodecImageGenerator(SkCodec* codec, sk_sp data) : INHERITED(make_premul(codec->getInfo())) , fCodec(codec) - , fData(SkRef(data)) + , fData(std::move(data)) {} SkData* SkCodecImageGenerator::onRefEncodedData(SK_REFENCODEDDATA_CTXPARAM) { diff --git a/src/codec/SkCodecImageGenerator.h b/src/codec/SkCodecImageGenerator.h index b125643..22a39aa 100644 --- a/src/codec/SkCodecImageGenerator.h +++ b/src/codec/SkCodecImageGenerator.h @@ -14,11 +14,11 @@ public: /* * If this data represents an encoded image that we know how to decode, * return an SkCodecImageGenerator. Otherwise return nullptr. - * - * Refs the data if an image generator can be returned. Otherwise does - * not affect the data. */ - static SkImageGenerator* NewFromEncodedCodec(SkData* data); + static SkImageGenerator* NewFromEncodedCodec(sk_sp); + static SkImageGenerator* NewFromEncodedCodec(SkData* data) { + return NewFromEncodedCodec(sk_ref_sp(data)); + } protected: SkData* onRefEncodedData(SK_REFENCODEDDATA_CTXPARAM) override; @@ -33,9 +33,8 @@ protected: private: /* * Takes ownership of codec - * Refs the data */ - SkCodecImageGenerator(SkCodec* codec, SkData* data); + SkCodecImageGenerator(SkCodec* codec, sk_sp); SkAutoTDelete fCodec; sk_sp fData; diff --git a/src/core/SkStream.cpp b/src/core/SkStream.cpp index e2b3e64..48eb92c 100644 --- a/src/core/SkStream.cpp +++ b/src/core/SkStream.cpp @@ -306,6 +306,7 @@ SkMemoryStream::SkMemoryStream(sk_sp data) : fData(std::move(data)) { fOffset = 0; } +#ifdef SK_SUPPORT_LEGACY_STREAM_DATA SkMemoryStream::SkMemoryStream(SkData* data) { if (nullptr == data) { fData = SkData::MakeEmpty(); @@ -314,6 +315,7 @@ SkMemoryStream::SkMemoryStream(SkData* data) { } fOffset = 0; } +#endif void SkMemoryStream::setMemoryOwned(const void* src, size_t size) { fData = SkData::MakeFromMalloc(src, size); @@ -325,18 +327,13 @@ void SkMemoryStream::setMemory(const void* src, size_t size, bool copyData) { fOffset = 0; } -SkData* SkMemoryStream::copyToData() const { - return SkSafeRef(fData.get()); -} - -SkData* SkMemoryStream::setData(SkData* data) { +void SkMemoryStream::setData(sk_sp data) { if (nullptr == data) { fData = SkData::MakeEmpty(); } else { - fData = sk_ref_sp(data); + fData = data; } fOffset = 0; - return data; } void SkMemoryStream::skipToAlign4() { @@ -642,14 +639,20 @@ void SkDynamicMemoryWStream::padToAlign4() write(&zero, padBytes); } -SkData* SkDynamicMemoryWStream::copyToData() const { +sk_sp SkDynamicMemoryWStream::snapshotAsData() const { if (nullptr == fCopy) { auto data = SkData::MakeUninitialized(fBytesWritten); // be sure to call copyTo() before we assign to fCopy this->copyTo(data->writable_data()); fCopy = std::move(data); } - return SkRef(fCopy.get()); + return fCopy; +} + +sk_sp SkDynamicMemoryWStream::detachAsData() { + sk_sp data = this->snapshotAsData(); + this->reset(); + return data; } void SkDynamicMemoryWStream::invalidateCopy() { @@ -882,7 +885,7 @@ sk_sp SkCopyStreamToData(SkStream* stream) { size_t bytesRead = stream->read(buffer, bufferSize); tempStream.write(buffer, bytesRead); } while (!stream->isAtEnd()); - return sk_sp(tempStream.copyToData()); + return tempStream.detachAsData(); } bool SkStreamCopy(SkWStream* out, SkStream* input) { diff --git a/src/images/SkImageEncoder.cpp b/src/images/SkImageEncoder.cpp index 22bbc57..023885f 100644 --- a/src/images/SkImageEncoder.cpp +++ b/src/images/SkImageEncoder.cpp @@ -31,7 +31,7 @@ SkData* SkImageEncoder::encodeData(const SkBitmap& bm, int quality) { SkDynamicMemoryWStream stream; quality = SkMin32(100, SkMax32(0, quality)); if (this->onEncode(&stream, bm, quality)) { - return stream.copyToData(); + return stream.detachAsData().release(); } return nullptr; } diff --git a/src/pathops/SkPathOpsOp.cpp b/src/pathops/SkPathOpsOp.cpp index 9472594..b7a48b0 100644 --- a/src/pathops/SkPathOpsOp.cpp +++ b/src/pathops/SkPathOpsOp.cpp @@ -206,7 +206,7 @@ static const bool gOutInverse[kReverseDifference_SkPathOp + 1][2][2] = { static void dump_path(FILE* file, const SkPath& path, bool force, bool dumpAsHex) { SkDynamicMemoryWStream wStream; path.dump(&wStream, force, dumpAsHex); - sk_sp data(wStream.copyToData()); + sk_sp data(wStream.detachAsData()); fprintf(file, "%.*s\n", (int) data->size(), data->data()); } diff --git a/src/ports/SkFontHost_win.cpp b/src/ports/SkFontHost_win.cpp index 45e76c7..ca80bae 100644 --- a/src/ports/SkFontHost_win.cpp +++ b/src/ports/SkFontHost_win.cpp @@ -2458,7 +2458,7 @@ protected: SkTypeface* onCreateFromData(SkData* data, int ttcIndex) const override { // could be in base impl - return this->createFromStream(new SkMemoryStream(data)); + return this->createFromStream(new SkMemoryStream(sk_ref_sp(data))); } SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const override { diff --git a/src/ports/SkFontMgr_android.cpp b/src/ports/SkFontMgr_android.cpp index 8d2bc6b..8a1916a 100644 --- a/src/ports/SkFontMgr_android.cpp +++ b/src/ports/SkFontMgr_android.cpp @@ -406,7 +406,7 @@ protected: } SkTypeface* onCreateFromData(SkData* data, int ttcIndex) const override { - return this->createFromStream(new SkMemoryStream(data), ttcIndex); + return this->createFromStream(new SkMemoryStream(sk_ref_sp(data)), ttcIndex); } SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const override { diff --git a/src/ports/SkFontMgr_custom.cpp b/src/ports/SkFontMgr_custom.cpp index 6588766..97489de 100644 --- a/src/ports/SkFontMgr_custom.cpp +++ b/src/ports/SkFontMgr_custom.cpp @@ -261,7 +261,7 @@ protected: } SkTypeface* onCreateFromData(SkData* data, int ttcIndex) const override { - return this->createFromStream(new SkMemoryStream(data), ttcIndex); + return this->createFromStream(new SkMemoryStream(sk_ref_sp(data)), ttcIndex); } SkTypeface* onCreateFromStream(SkStreamAsset* bareStream, int ttcIndex) const override { diff --git a/src/ports/SkFontMgr_fontconfig.cpp b/src/ports/SkFontMgr_fontconfig.cpp index fd5f1b0..54fa1b3 100644 --- a/src/ports/SkFontMgr_fontconfig.cpp +++ b/src/ports/SkFontMgr_fontconfig.cpp @@ -903,7 +903,7 @@ protected: } SkTypeface* onCreateFromData(SkData* data, int ttcIndex) const override { - return this->createFromStream(new SkMemoryStream(data), ttcIndex); + return this->createFromStream(new SkMemoryStream(sk_ref_sp(data)), ttcIndex); } SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const override { diff --git a/src/ports/SkFontMgr_win_dw.cpp b/src/ports/SkFontMgr_win_dw.cpp index bacac2c..833f95b 100644 --- a/src/ports/SkFontMgr_win_dw.cpp +++ b/src/ports/SkFontMgr_win_dw.cpp @@ -929,7 +929,7 @@ SkTypeface* SkFontMgr_DirectWrite::onCreateFromStream(SkStreamAsset* stream, int } SkTypeface* SkFontMgr_DirectWrite::onCreateFromData(SkData* data, int ttcIndex) const { - return this->createFromStream(new SkMemoryStream(data), ttcIndex); + return this->createFromStream(new SkMemoryStream(sk_ref_sp(data)), ttcIndex); } SkTypeface* SkFontMgr_DirectWrite::onCreateFromFile(const char path[], int ttcIndex) const { diff --git a/src/ports/SkImageGeneratorWIC.cpp b/src/ports/SkImageGeneratorWIC.cpp index f22cb5a..52d1377 100644 --- a/src/ports/SkImageGeneratorWIC.cpp +++ b/src/ports/SkImageGeneratorWIC.cpp @@ -31,7 +31,7 @@ SkImageGenerator* SkImageGeneratorWIC::NewFromEncodedWIC(SkData* data) { SkTScopedComPtr iStream; // Note that iStream will take ownership of the new memory stream because // we set |deleteOnRelease| to true. - hr = SkIStream::CreateFromSkStream(new SkMemoryStream(data), true, &iStream); + hr = SkIStream::CreateFromSkStream(new SkMemoryStream(sk_ref_sp(data)), true, &iStream); if (FAILED(hr)) { return nullptr; } diff --git a/src/xps/SkXPSDevice.cpp b/src/xps/SkXPSDevice.cpp index db15284..566651e 100644 --- a/src/xps/SkXPSDevice.cpp +++ b/src/xps/SkXPSDevice.cpp @@ -662,7 +662,7 @@ HRESULT SkXPSDevice::createXpsImageBrush( HRM(E_FAIL, "Unable to encode bitmap as png."); } SkMemoryStream* read = new SkMemoryStream; - read->setData(write.copyToData())->unref(); + read->setData(write.detachAsData()); SkTScopedComPtr readWrapper; HRM(SkIStream::CreateFromSkStream(read, true, &readWrapper), "Could not create stream from png data."); diff --git a/tests/AnnotationTest.cpp b/tests/AnnotationTest.cpp index a4109c5..0a9dfea 100644 --- a/tests/AnnotationTest.cpp +++ b/tests/AnnotationTest.cpp @@ -51,7 +51,7 @@ DEF_TEST(Annotation_PdfLink, reporter) { SkAnnotateRectWithURL(canvas, r, data.get()); REPORTER_ASSERT(reporter, doc->close()); - sk_sp out(outStream.copyToData()); + sk_sp out = outStream.detachAsData(); const char* rawOutput = (const char*)out->data(); REPORTER_ASSERT(reporter, ContainsString(rawOutput, out->size(), "/Annots ")); @@ -69,7 +69,7 @@ DEF_TEST(Annotation_NamedDestination, reporter) { SkAnnotateNamedDestination(canvas, p, data.get()); REPORTER_ASSERT(reporter, doc->close()); - sk_sp out(outStream.copyToData()); + sk_sp out = outStream.detachAsData(); const char* rawOutput = (const char*)out->data(); REPORTER_ASSERT(reporter, diff --git a/tests/CachedDecodingPixelRefTest.cpp b/tests/CachedDecodingPixelRefTest.cpp index 1c89895..b6253c7 100644 --- a/tests/CachedDecodingPixelRefTest.cpp +++ b/tests/CachedDecodingPixelRefTest.cpp @@ -38,11 +38,10 @@ static void make_test_image(SkBitmap* bm) { /** * encode this bitmap into some data via SkImageEncoder */ -static SkData* create_data_from_bitmap(const SkBitmap& bm, - SkImageEncoder::Type type) { +static sk_sp create_data_from_bitmap(const SkBitmap& bm, SkImageEncoder::Type type) { SkDynamicMemoryWStream stream; if (SkImageEncoder::EncodeStream(&stream, bm, type, 100)) { - return stream.copyToData(); + return stream.detachAsData(); } return nullptr; } @@ -107,7 +106,7 @@ static void test_three_encodings(skiatest::Reporter* reporter, }; for (size_t i = 0; i < SK_ARRAY_COUNT(types); i++) { SkImageEncoder::Type type = types[i]; - sk_sp encoded(create_data_from_bitmap(original, type)); + sk_sp encoded = create_data_from_bitmap(original, type); REPORTER_ASSERT(reporter, encoded.get() != nullptr); if (nullptr == encoded.get()) { continue; diff --git a/tests/CodecPriv.h b/tests/CodecPriv.h index 4e42731..a5afb09 100644 --- a/tests/CodecPriv.h +++ b/tests/CodecPriv.h @@ -10,9 +10,7 @@ #include "SkData.h" inline bool decode_memory(const void* mem, size_t size, SkBitmap* bm) { - sk_sp data(SkData::MakeWithoutCopy(mem, size)); - - SkAutoTDelete codec(SkCodec::NewFromData(data.get())); + SkAutoTDelete codec(SkCodec::NewFromData(SkData::MakeWithoutCopy(mem, size))); if (!codec) { return false; } diff --git a/tests/CodecTest.cpp b/tests/CodecTest.cpp index b01750d..348bc64 100644 --- a/tests/CodecTest.cpp +++ b/tests/CodecTest.cpp @@ -210,7 +210,7 @@ static void check(skiatest::Reporter* r, if (isIncomplete) { size_t size = stream->getLength(); sk_sp data((SkData::MakeFromStream(stream, 2 * size / 3))); - codec.reset(SkCodec::NewFromData(data.get())); + codec.reset(SkCodec::NewFromData(data)); } else { codec.reset(SkCodec::NewFromStream(stream.release())); } @@ -339,7 +339,7 @@ static void check(skiatest::Reporter* r, if (isIncomplete) { size_t size = stream->getLength(); sk_sp data((SkData::MakeFromStream(stream, 2 * size / 3))); - androidCodec.reset(SkAndroidCodec::NewFromData(data.get())); + androidCodec.reset(SkAndroidCodec::NewFromData(data)); } else { androidCodec.reset(SkAndroidCodec::NewFromStream(stream.release())); } @@ -824,8 +824,7 @@ DEF_TEST(Codec_pngChunkReader, r) { ChunkReader chunkReader(r); // Now read the file with SkCodec. - sk_sp data(wStream.copyToData()); - SkAutoTDelete codec(SkCodec::NewFromData(data.get(), &chunkReader)); + SkAutoTDelete codec(SkCodec::NewFromData(wStream.detachAsData(), &chunkReader)); REPORTER_ASSERT(r, codec); if (!codec) { return; @@ -865,8 +864,8 @@ DEF_TEST(Codec_pngChunkReader, r) { // Stream that can only peek up to a limit class LimitedPeekingMemStream : public SkStream { public: - LimitedPeekingMemStream(SkData* data, size_t limit) - : fStream(data) + LimitedPeekingMemStream(sk_sp data, size_t limit) + : fStream(std::move(data)) , fLimit(limit) {} size_t peek(void* buf, size_t bytes) const override { @@ -948,13 +947,13 @@ DEF_TEST(Codec_webp_peek, r) { // The limit is less than webp needs to peek or read. SkAutoTDelete codec(SkCodec::NewFromStream( - new LimitedPeekingMemStream(data.get(), 25))); + new 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.get(), 0))); + codec.reset(SkCodec::NewFromStream(new LimitedPeekingMemStream(data, 0))); REPORTER_ASSERT(r, codec); test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr); @@ -978,7 +977,7 @@ DEF_TEST(Codec_wbmp, r) { writeableData[1] = static_cast(~0x9F); // SkCodec should support this. - SkAutoTDelete codec(SkCodec::NewFromData(data.get())); + SkAutoTDelete codec(SkCodec::NewFromData(data)); REPORTER_ASSERT(r, codec); if (!codec) { return; @@ -1083,7 +1082,7 @@ static void check_round_trip(skiatest::Reporter* r, const SkBitmap& bm1) { sk_sp(SkImageEncoder::EncodeData(bm1, SkImageEncoder::kPNG_Type, 100)); // Prepare to decode. The codec should recognize that the PNG is 565. - SkAutoTDelete codec(SkCodec::NewFromData(data.get())); + SkAutoTDelete codec(SkCodec::NewFromData(data)); REPORTER_ASSERT(r, origColorType == codec->getInfo().colorType()); REPORTER_ASSERT(r, origAlphaType == codec->getInfo().alphaType()); diff --git a/tests/PDFDocumentTest.cpp b/tests/PDFDocumentTest.cpp index c871375..afc05e4 100644 --- a/tests/PDFDocumentTest.cpp +++ b/tests/PDFDocumentTest.cpp @@ -193,8 +193,8 @@ DEF_TEST(SkPDF_pdfa_document, r) { pdfMetadata, nullptr, /* pdfa = */ true); doc->beginPage(64, 64)->drawColor(SK_ColorRED); doc->close(); - sk_sp data(buffer.copyToData()); - buffer.reset(); + sk_sp data(buffer.detachAsData()); + static const char* expectations[] = { "sRGB IEC61966-2.1", "test document", @@ -212,8 +212,7 @@ DEF_TEST(SkPDF_pdfa_document, r) { pdfMetadata, nullptr, /* pdfa = */ true); doc->beginPage(64, 64)->drawColor(SK_ColorRED); doc->close(); - data.reset(buffer.copyToData()); - buffer.reset(); + data = buffer.detachAsData(); static const char* moreExpectations[] = { "/Producer (phoney library)", diff --git a/tests/PDFGlyphsToUnicodeTest.cpp b/tests/PDFGlyphsToUnicodeTest.cpp index f65ee3e..b3ee2d8 100644 --- a/tests/PDFGlyphsToUnicodeTest.cpp +++ b/tests/PDFGlyphsToUnicodeTest.cpp @@ -15,7 +15,7 @@ static const int kMaximumGlyphCount = SK_MaxU16 + 1; static bool stream_equals(const SkDynamicMemoryWStream& stream, size_t offset, const char* buffer, size_t len) { - sk_sp data(stream.copyToData()); + sk_sp data = stream.snapshotAsData(); if (offset + len > data->size()) { return false; } diff --git a/tests/PDFJpegEmbedTest.cpp b/tests/PDFJpegEmbedTest.cpp index 93c2044..2e75c39 100644 --- a/tests/PDFJpegEmbedTest.cpp +++ b/tests/PDFJpegEmbedTest.cpp @@ -78,9 +78,8 @@ DEF_TEST(SkPDF_JpegEmbedTest, r) { canvas->flush(); document->endPage(); document->close(); - sk_sp pdfData(pdf.copyToData()); + sk_sp pdfData(pdf.detachAsData()); SkASSERT(pdfData); - pdf.reset(); REPORTER_ASSERT(r, is_subset_of(mandrillData.get(), pdfData.get())); @@ -102,9 +101,8 @@ DEF_TEST(SkPDF_JpegEmbedTest, r) { canvas->flush(); document->endPage(); document->close(); - pdfData.reset(pdf.copyToData()); + pdfData = pdf.detachAsData(); SkASSERT(pdfData); - pdf.reset(); REPORTER_ASSERT(r, is_subset_of(mandrillData.get(), pdfData.get())); diff --git a/tests/PDFMetadataAttributeTest.cpp b/tests/PDFMetadataAttributeTest.cpp index 6e7cc24..01351fa 100644 --- a/tests/PDFMetadataAttributeTest.cpp +++ b/tests/PDFMetadataAttributeTest.cpp @@ -29,7 +29,7 @@ DEF_TEST(SkPDF_Metadata, r) { metadata, nullptr, false); doc->beginPage(612.0f, 792.0f); doc->close(); - sk_sp data(pdf.copyToData()); + sk_sp data = pdf.detachAsData(); static const char* expectations[] = { "/Title (A1)", "/Author (A2)", diff --git a/tests/PDFPrimitivesTest.cpp b/tests/PDFPrimitivesTest.cpp index c719f48..2d9468e 100644 --- a/tests/PDFPrimitivesTest.cpp +++ b/tests/PDFPrimitivesTest.cpp @@ -106,7 +106,7 @@ static void TestPDFStream(skiatest::Reporter* reporter) { compressedByteStream.writeToStream(&expected); compressedByteStream.reset(); expected.writeText("\nendstream"); - sk_sp expectedResultData2(expected.copyToData()); + sk_sp expectedResultData2(expected.detachAsData()); SkString result = emit_to_string(*stream); #ifndef SK_PDF_LESS_COMPRESSION assert_eql(reporter, diff --git a/tests/PathTest.cpp b/tests/PathTest.cpp index 032b646..483d14e 100644 --- a/tests/PathTest.cpp +++ b/tests/PathTest.cpp @@ -4083,7 +4083,7 @@ static void compare_dump(skiatest::Reporter* reporter, const SkPath& path, bool bool dumpAsHex, const char* str) { SkDynamicMemoryWStream wStream; path.dump(&wStream, force, dumpAsHex); - sk_sp data(wStream.copyToData()); + sk_sp data = wStream.detachAsData(); REPORTER_ASSERT(reporter, data->size() == strlen(str)); if (strlen(str) > 0) { REPORTER_ASSERT(reporter, !memcmp(data->data(), str, strlen(str))); diff --git a/tests/PictureTest.cpp b/tests/PictureTest.cpp index 7b187b4..6ab57c0 100644 --- a/tests/PictureTest.cpp +++ b/tests/PictureTest.cpp @@ -632,7 +632,7 @@ static sk_sp serialized_picture_from_bitmap(const SkBitmap& bitmap) { SkAutoTUnref serializer( SkImageEncoder::CreatePixelSerializer()); picture->serialize(&wStream, serializer); - return sk_sp(wStream.copyToData()); + return wStream.detachAsData(); } struct ErrorContext { @@ -670,7 +670,7 @@ DEF_TEST(Picture_EncodedData, reporter) { if (!SkImageEncoder::EncodeStream(&wStream, original, SkImageEncoder::kPNG_Type, 100)) { return; } - sk_sp data(wStream.copyToData()); + sk_sp data = wStream.detachAsData(); SkBitmap bm; bool installSuccess = SkDEPRECATED_InstallDiscardablePixelRef(data.get(), &bm); diff --git a/tests/StreamTest.cpp b/tests/StreamTest.cpp index 5697782..74e6055 100644 --- a/tests/StreamTest.cpp +++ b/tests/StreamTest.cpp @@ -117,13 +117,13 @@ static void TestWStream(skiatest::Reporter* reporter) { REPORTER_ASSERT(reporter, ds.getOffset() == 100 * 26); { - sk_sp data(ds.copyToData()); + sk_sp data = ds.snapshotAsData(); REPORTER_ASSERT(reporter, 100 * 26 == data->size()); REPORTER_ASSERT(reporter, memcmp(dst, data->data(), data->size()) == 0); } { - // Test that this works after a copyToData. + // Test that this works after a snapshot. SkAutoTDelete stream(ds.detachAsStream()); REPORTER_ASSERT(reporter, ds.getOffset() == 0); test_loop_stream(reporter, stream.get(), s, 26, 100); @@ -174,15 +174,14 @@ static void TestPackedUInt(skiatest::Reporter* reporter) { static void TestDereferencingData(SkMemoryStream* memStream) { memStream->read(nullptr, 0); memStream->getMemoryBase(); - sk_sp data(memStream->copyToData()); + (void)memStream->asData(); } static void TestNullData() { - SkData* nullData = nullptr; - SkMemoryStream memStream(nullData); + SkMemoryStream memStream(nullptr); TestDereferencingData(&memStream); - memStream.setData(nullData); + memStream.setData(nullptr); TestDereferencingData(&memStream); } @@ -404,8 +403,7 @@ static void stream_copy_test(skiatest::Reporter* reporter, ERRORF(reporter, "SkStreamCopy failed"); return; } - sk_sp data(tgt.copyToData()); - tgt.reset(); + sk_sp data(tgt.detachAsData()); if (data->size() != N) { ERRORF(reporter, "SkStreamCopy incorrect size"); return; diff --git a/tests/TypefaceTest.cpp b/tests/TypefaceTest.cpp index 6f803ce..436f129 100644 --- a/tests/TypefaceTest.cpp +++ b/tests/TypefaceTest.cpp @@ -21,12 +21,11 @@ static void TypefaceStyle_test(skiatest::Reporter* reporter, uint16_t weight, uint16_t width, SkData* data) { sk_sp dataCopy; - SkData* dataToUse = data; - if (!dataToUse->unique()) { + if (!data->unique()) { dataCopy = SkData::MakeWithCopy(data->data(), data->size()); - dataToUse = dataCopy.get(); + data = dataCopy.get(); } - SkSFNTHeader* sfntHeader = static_cast(dataToUse->writable_data()); + SkSFNTHeader* sfntHeader = static_cast(data->writable_data()); SkSFNTHeader::TableDirectoryEntry* tableEntry = SkTAfter(sfntHeader); @@ -46,7 +45,7 @@ static void TypefaceStyle_test(skiatest::Reporter* reporter, using WidthType = SkOTTableOS2_V0::WidthClass::Value; os2Table->usWidthClass.value = static_cast(SkEndian_SwapBE16(width)); - sk_sp newTypeface(SkTypeface::MakeFromStream(new SkMemoryStream(dataToUse))); + sk_sp newTypeface(SkTypeface::MakeFromStream(new SkMemoryStream(sk_ref_sp(data)))); SkASSERT_RELEASE(newTypeface); SkFontStyle newStyle = newTypeface->fontStyle(); diff --git a/tools/debugger/SkDrawCommand.cpp b/tools/debugger/SkDrawCommand.cpp index d77a554..6ca3fe0 100644 --- a/tools/debugger/SkDrawCommand.cpp +++ b/tools/debugger/SkDrawCommand.cpp @@ -707,11 +707,10 @@ bool SkDrawCommand::flatten(const SkImage& image, Json::Value* target, SkDynamicMemoryWStream out; SkDrawCommand::WritePNG((const png_bytep) encodedBitmap->bytes(), image.width(), image.height(), out, false); - SkData* encoded = out.copyToData(); + sk_sp encoded = out.detachAsData(); Json::Value jsonData; encode_data(encoded->data(), encoded->size(), "image/png", urlDataManager, &jsonData); (*target)[SKDEBUGCANVAS_ATTRIBUTE_DATA] = jsonData; - encoded->unref(); return true; } diff --git a/tools/fiddle/fiddle_main.cpp b/tools/fiddle/fiddle_main.cpp index 0fa6158..f83026f 100644 --- a/tools/fiddle/fiddle_main.cpp +++ b/tools/fiddle/fiddle_main.cpp @@ -140,7 +140,7 @@ int main() { srand(0); draw(document->beginPage(options.size.width(), options.size.height())); document->close(); - pdfData.reset(pdfStream.copyToData()); + pdfData = pdfStream.detachAsData(); } } if (options.skp) { @@ -152,7 +152,7 @@ int main() { auto picture = recorder.finishRecordingAsPicture(); SkDynamicMemoryWStream skpStream; picture->serialize(&skpStream); - skpData.reset(skpStream.copyToData()); + skpData = skpStream.detachAsData(); } printf("{\n"); diff --git a/tools/get_images_from_skps.cpp b/tools/get_images_from_skps.cpp index f9ba34b..1ec70b3 100644 --- a/tools/get_images_from_skps.cpp +++ b/tools/get_images_from_skps.cpp @@ -54,7 +54,7 @@ struct Sniffer : public SkPixelSerializer { gSeen.add(digest); sk_sp data(SkData::MakeWithoutCopy(ptr, len)); - SkAutoTDelete codec(SkCodec::NewFromData(data.get())); + SkAutoTDelete codec(SkCodec::NewFromData(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_main.cpp b/tools/skdiff_main.cpp index 0c60677..ef58b1b 100644 --- a/tools/skdiff_main.cpp +++ b/tools/skdiff_main.cpp @@ -345,7 +345,7 @@ static void get_bounds(DiffResource& resource, const char* name) { if (resource.fBitmap.empty() && !DiffResource::isStatusFailed(resource.fStatus)) { sk_sp fileBits(read_file(resource.fFullPath.c_str())); if (fileBits) { - get_bitmap(fileBits.get(), resource, true); + get_bitmap(fileBits, resource, true); } else { SkDebugf("WARNING: couldn't read %s file <%s>\n", name, resource.fFullPath.c_str()); resource.fStatus = DiffResource::kCouldNotRead_Status; @@ -496,8 +496,8 @@ static void create_diff_images (DiffMetricProc dmp, VERBOSE_STATUS("MATCH", ANSI_COLOR_GREEN, baseFiles[i]); } else { AutoReleasePixels arp(drp); - get_bitmap(baseFileBits.get(), drp->fBase, false); - get_bitmap(comparisonFileBits.get(), drp->fComparison, false); + get_bitmap(baseFileBits, drp->fBase, false); + get_bitmap(comparisonFileBits, drp->fComparison, false); VERBOSE_STATUS("DIFFERENT", ANSI_COLOR_RED, baseFiles[i]); if (DiffResource::kDecoded_Status == drp->fBase.fStatus && DiffResource::kDecoded_Status == drp->fComparison.fStatus) { diff --git a/tools/skdiff_utils.cpp b/tools/skdiff_utils.cpp index 3a37733..609d75d 100644 --- a/tools/skdiff_utils.cpp +++ b/tools/skdiff_utils.cpp @@ -33,7 +33,7 @@ sk_sp read_file(const char* file_path) { return data; } -bool get_bitmap(SkData* fileBits, DiffResource& resource, bool sizeOnly) { +bool get_bitmap(sk_sp fileBits, DiffResource& resource, bool sizeOnly) { SkAutoTDelete codec(SkCodec::NewFromData(fileBits)); if (!codec) { SkDebugf("ERROR: could not create codec for <%s>\n", resource.fFullPath.c_str()); diff --git a/tools/skdiff_utils.h b/tools/skdiff_utils.h index c535ae2..c799325 100644 --- a/tools/skdiff_utils.h +++ b/tools/skdiff_utils.h @@ -25,7 +25,7 @@ bool are_buffers_equal(SkData* skdata1, SkData* skdata2); sk_sp read_file(const char* file_path); /** Decodes the fileBits into the resource.fBitmap. Returns false on failure. */ -bool get_bitmap(SkData* fileBits, DiffResource& resource, bool sizeOnly); +bool get_bitmap(sk_sp fileBits, DiffResource& resource, bool sizeOnly); /** Writes the bitmap as a PNG to the path specified. */ bool write_bitmap(const SkString& path, const SkBitmap& bitmap); diff --git a/tools/skiaserve/Request.cpp b/tools/skiaserve/Request.cpp index 22fa211..ea2d17a 100644 --- a/tools/skiaserve/Request.cpp +++ b/tools/skiaserve/Request.cpp @@ -65,7 +65,7 @@ sk_sp Request::writeCanvasToPng(SkCanvas* canvas) { SkDynamicMemoryWStream buffer; SkDrawCommand::WritePNG((const png_bytep) encodedBitmap->bytes(), bmp->width(), bmp->height(), buffer, true); - return sk_sp(buffer.copyToData()); + return buffer.detachAsData(); } SkCanvas* Request::getCanvas() { @@ -117,7 +117,7 @@ sk_sp Request::writeOutSkp() { SkAutoTUnref serializer(SkImageEncoder::CreatePixelSerializer()); picture->serialize(&outStream, serializer); - return sk_sp(outStream.copyToData()); + return outStream.detachAsData(); } GrContext* Request::getContext() { @@ -257,7 +257,7 @@ sk_sp Request::getJsonOps(int n) { SkDynamicMemoryWStream stream; stream.writeText(Json::FastWriter().write(root).c_str()); - return sk_sp(stream.copyToData()); + return stream.detachAsData(); } sk_sp Request::getJsonBatchList(int n) { @@ -269,7 +269,7 @@ sk_sp Request::getJsonBatchList(int n) { SkDynamicMemoryWStream stream; stream.writeText(Json::FastWriter().write(result).c_str()); - return sk_sp(stream.copyToData()); + return stream.detachAsData(); } sk_sp Request::getJsonInfo(int n) { @@ -301,7 +301,7 @@ SkColor Request::getPixel(int x, int y) { // Convert to format suitable for inspection sk_sp encodedBitmap = sk_tools::encode_bitmap_for_png(*bitmap); - SkASSERT(encodedBitmap.get()); + SkASSERT(encodedBitmap); const uint8_t* start = encodedBitmap->bytes() + ((y * bitmap->width() + x) * 4); SkColor result = SkColorSetARGB(start[3], start[0], start[1], start[2]); diff --git a/tools/skiaserve/urlhandlers/BreakHandler.cpp b/tools/skiaserve/urlhandlers/BreakHandler.cpp index 7766adb..f4f741f 100644 --- a/tools/skiaserve/urlhandlers/BreakHandler.cpp +++ b/tools/skiaserve/urlhandlers/BreakHandler.cpp @@ -80,6 +80,5 @@ int BreakHandler::handle(Request* request, MHD_Connection* connection, canvas->restoreToCount(saveCount); SkDynamicMemoryWStream stream; stream.writeText(Json::FastWriter().write(response).c_str()); - sk_sp data(stream.copyToData()); - return SendData(connection, data.get(), "application/json"); + return SendData(connection, stream.detachAsData().get(), "application/json"); } diff --git a/tools/viewer/ImageSlide.cpp b/tools/viewer/ImageSlide.cpp index 7dcee5d..78fb17b 100644 --- a/tools/viewer/ImageSlide.cpp +++ b/tools/viewer/ImageSlide.cpp @@ -37,7 +37,7 @@ void ImageSlide::draw(SkCanvas* canvas) { void ImageSlide::load(SkScalar, SkScalar) { sk_sp encoded = SkData::MakeFromFileName(fPath.c_str()); - SkAutoTDelete codec(SkCodec::NewFromData(encoded.get())); + SkAutoTDelete codec(SkCodec::NewFromData(encoded)); if (!codec) { return; } diff --git a/tools/visualize_color_gamut.cpp b/tools/visualize_color_gamut.cpp index cc15601..1bacf5f 100644 --- a/tools/visualize_color_gamut.cpp +++ b/tools/visualize_color_gamut.cpp @@ -124,7 +124,7 @@ int main(int argc, char** argv) { SkDebugf("Cannot find input image.\n"); return -1; } - SkAutoTDelete codec(SkCodec::NewFromData(data.get())); + SkAutoTDelete codec(SkCodec::NewFromData(data)); if (!codec) { SkDebugf("Invalid input image.\n"); return -1;