From a913275bda105fd545af471b724d7e8c1dacb9ae Mon Sep 17 00:00:00 2001 From: scroggo Date: Tue, 19 Jan 2016 07:53:39 -0800 Subject: SkStream/Priv cleanups Replace all callers of SkCopyStreamToStorage with SkCopyStreamToData, which is simpler and does the same thing. Remove SkStreamRewindableFromSkStream, which is unused. BUG=skia:4788 GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1604963002 Review URL: https://codereview.chromium.org/1604963002 --- src/core/SkStream.cpp | 64 ------------------------------------ src/core/SkStreamPriv.h | 32 ++++-------------- src/images/SkImageDecoder_astc.cpp | 8 ++--- src/images/SkImageDecoder_libbmp.cpp | 14 +++++--- src/images/SkImageDecoder_libico.cpp | 11 +++++-- src/images/SkImageDecoder_pkm.cpp | 8 ++--- src/ports/SkImageDecoder_CG.cpp | 15 +++++---- 7 files changed, 41 insertions(+), 111 deletions(-) diff --git a/src/core/SkStream.cpp b/src/core/SkStream.cpp index ef4c6baae4..9529308e86 100644 --- a/src/core/SkStream.cpp +++ b/src/core/SkStream.cpp @@ -881,42 +881,6 @@ SkStreamAsset* SkStream::NewFromFile(const char path[]) { return stream; } -// Declared in SkStreamPriv.h: -size_t SkCopyStreamToStorage(SkAutoMalloc* storage, SkStream* stream) { - SkASSERT(storage != nullptr); - SkASSERT(stream != nullptr); - - if (stream->hasLength()) { - const size_t length = stream->getLength(); - void* dst = storage->reset(length); - if (stream->read(dst, length) != length) { - return 0; - } - return length; - } - - SkDynamicMemoryWStream tempStream; - // Arbitrary buffer size. -#if defined(GOOGLE3) - // Stack frame size is limited in GOOGLE3. - const size_t bufferSize = 8 * 1024; // 8KB -#else - const size_t bufferSize = 256 * 1024; // 256KB -#endif - char buffer[bufferSize]; - SkDEBUGCODE(size_t debugLength = 0;) - do { - size_t bytesRead = stream->read(buffer, bufferSize); - tempStream.write(buffer, bytesRead); - SkDEBUGCODE(debugLength += bytesRead); - SkASSERT(tempStream.bytesWritten() == debugLength); - } while (!stream->isAtEnd()); - const size_t length = tempStream.bytesWritten(); - void* dst = storage->reset(length); - tempStream.copyTo(dst); - return length; -} - // Declared in SkStreamPriv.h: SkData* SkCopyStreamToData(SkStream* stream) { SkASSERT(stream != nullptr); @@ -935,34 +899,6 @@ SkData* SkCopyStreamToData(SkStream* stream) { return tempStream.copyToData(); } -SkStreamRewindable* SkStreamRewindableFromSkStream(SkStream* stream) { - if (!stream) { - return nullptr; - } - SkAutoTDelete dupStream(stream->duplicate()); - if (dupStream) { - return dupStream.detach(); - } - stream->rewind(); - if (stream->hasLength()) { - size_t length = stream->getLength(); - if (stream->hasPosition()) { // If stream has length, but can't rewind. - length -= stream->getPosition(); - } - SkAutoTUnref data(SkData::NewFromStream(stream, length)); - return new SkMemoryStream(data.get()); - } - SkDynamicMemoryWStream tempStream; - const size_t bufferSize = 4096; - char buffer[bufferSize]; - do { - size_t bytesRead = stream->read(buffer, bufferSize); - tempStream.write(buffer, bytesRead); - } while (!stream->isAtEnd()); - return tempStream.detachAsStream(); // returns a SkBlockMemoryStream, - // cheaper than copying to SkData -} - bool SkStreamCopy(SkWStream* out, SkStream* input) { const char* base = static_cast(input->getMemoryBase()); if (base && input->hasPosition() && input->hasLength()) { diff --git a/src/core/SkStreamPriv.h b/src/core/SkStreamPriv.h index b82f7d84e9..75f7cce8df 100644 --- a/src/core/SkStreamPriv.h +++ b/src/core/SkStreamPriv.h @@ -8,40 +8,22 @@ #ifndef SkStreamPriv_DEFINED #define SkStreamPriv_DEFINED -class SkAutoMalloc; -class SkStream; -class SkStreamRewindable; class SkData; - -/** - * Copy the provided stream to memory allocated by storage. - * Used by SkImageDecoder_libbmp and SkImageDecoder_libico. - * @param storage Allocator to hold the memory. Will be reset to be large - * enough to hold the entire stream. Upon successful return, - * storage->get() will point to data holding the SkStream's entire - * contents. - * @param stream SkStream to be copied into storage. - * @return size_t Total number of bytes in the SkStream, which is also the - * number of bytes pointed to by storage->get(). Returns 0 on failure. - */ -size_t SkCopyStreamToStorage(SkAutoMalloc* storage, SkStream* stream); +class SkStream; +class SkWStream; /** * Copy the provided stream to an SkData variable. + * + * Note: Assumes the stream is at the beginning. If it has a length, + * but is not at the beginning, this call will fail (return NULL). + * * @param stream SkStream to be copied into data. * @return SkData* The resulting SkData after the copy. This data * will have a ref count of one upon return and belongs to the * caller. Returns nullptr on failure. */ -SkData *SkCopyStreamToData(SkStream* stream); - -/** - * Attempt to convert this stream to a StreamRewindable in the - * cheapest possible manner (calling duplicate() if possible, and - * otherwise allocating memory for a copy). The position of the - * input stream is left in an indeterminate state. - */ -SkStreamRewindable* SkStreamRewindableFromSkStream(SkStream* stream); +SkData* SkCopyStreamToData(SkStream* stream); /** * Copies the input stream from the current position to the end. diff --git a/src/images/SkImageDecoder_astc.cpp b/src/images/SkImageDecoder_astc.cpp index e1a6a06fa7..8989464f3b 100644 --- a/src/images/SkImageDecoder_astc.cpp +++ b/src/images/SkImageDecoder_astc.cpp @@ -5,6 +5,7 @@ * found in the LICENSE file. */ +#include "SkData.h" #include "SkEndian.h" #include "SkColorPriv.h" #include "SkImageDecoder.h" @@ -43,13 +44,12 @@ static inline int read_24bit(const uint8_t* buf) { } SkImageDecoder::Result SkASTCImageDecoder::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) { - SkAutoMalloc autoMal; - const size_t length = SkCopyStreamToStorage(&autoMal, stream); - if (0 == length) { + SkAutoTUnref data(SkCopyStreamToData(stream)); + if (!data || !data->size()) { return kFailure; } - unsigned char* buf = (unsigned char*)autoMal.get(); + unsigned char* buf = (unsigned char*) data->data(); // Make sure that the magic header is there... SkASSERT(SkEndian_SwapLE32(*(reinterpret_cast(buf))) == kASTCMagicNumber); diff --git a/src/images/SkImageDecoder_libbmp.cpp b/src/images/SkImageDecoder_libbmp.cpp index 179162356a..4a6f71c82d 100644 --- a/src/images/SkImageDecoder_libbmp.cpp +++ b/src/images/SkImageDecoder_libbmp.cpp @@ -9,6 +9,7 @@ #include "bmpdecoderhelper.h" #include "SkColorPriv.h" +#include "SkData.h" #include "SkImageDecoder.h" #include "SkScaledBitmapSampler.h" #include "SkStream.h" @@ -96,10 +97,13 @@ SkImageDecoder::Result SkBMPImageDecoder::onDecode(SkStream* stream, SkBitmap* b // First read the entire stream, so that all of the data can be passed to // the BmpDecoderHelper. - // Allocated space used to hold the data. - SkAutoMalloc storage; + SkAutoTUnref data(SkCopyStreamToData(stream)); + if (!data) { + return kFailure; + } + // Byte length of all of the data. - const size_t length = SkCopyStreamToStorage(&storage, stream); + const size_t length = data->size(); if (0 == length) { return kFailure; } @@ -111,7 +115,7 @@ SkImageDecoder::Result SkBMPImageDecoder::onDecode(SkStream* stream, SkBitmap* b { image_codec::BmpDecoderHelper helper; const int max_pixels = 16383*16383; // max width*height - if (!helper.DecodeImage((const char*)storage.get(), length, + if (!helper.DecodeImage((const char*) data->data(), length, max_pixels, &callback)) { return kFailure; } @@ -119,7 +123,7 @@ SkImageDecoder::Result SkBMPImageDecoder::onDecode(SkStream* stream, SkBitmap* b // we don't need this anymore, so free it now (before we try to allocate // the bitmap's pixels) rather than waiting for its destructor - storage.free(); + data.reset(nullptr); int width = callback.width(); int height = callback.height(); diff --git a/src/images/SkImageDecoder_libico.cpp b/src/images/SkImageDecoder_libico.cpp index cb21b6906e..93a875d4cb 100644 --- a/src/images/SkImageDecoder_libico.cpp +++ b/src/images/SkImageDecoder_libico.cpp @@ -6,6 +6,7 @@ */ #include "SkColorPriv.h" +#include "SkData.h" #include "SkImageDecoder.h" #include "SkStream.h" #include "SkStreamPriv.h" @@ -74,14 +75,18 @@ static int calculateRowBytesFor8888(int w, int bitCount) SkImageDecoder::Result SkICOImageDecoder::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) { - SkAutoMalloc autoMal; - const size_t length = SkCopyStreamToStorage(&autoMal, stream); + SkAutoTUnref data(SkCopyStreamToData(stream)); + if (!data) { + return kFailure; + } + + const size_t length = data->size(); // Check that the buffer is large enough to read the directory header if (length < 6) { return kFailure; } - unsigned char* buf = (unsigned char*)autoMal.get(); + unsigned char* buf = (unsigned char*) data->data(); //these should always be the same - should i use for error checking? - what about files that have some //incorrect values, but still decode properly? diff --git a/src/images/SkImageDecoder_pkm.cpp b/src/images/SkImageDecoder_pkm.cpp index 71004614f5..098a4ee0ae 100644 --- a/src/images/SkImageDecoder_pkm.cpp +++ b/src/images/SkImageDecoder_pkm.cpp @@ -6,6 +6,7 @@ */ #include "SkColorPriv.h" +#include "SkData.h" #include "SkImageDecoder.h" #include "SkScaledBitmapSampler.h" #include "SkStream.h" @@ -33,13 +34,12 @@ private: ///////////////////////////////////////////////////////////////////////////////////////// SkImageDecoder::Result SkPKMImageDecoder::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) { - SkAutoMalloc autoMal; - const size_t length = SkCopyStreamToStorage(&autoMal, stream); - if (0 == length) { + SkAutoTUnref data(SkCopyStreamToData(stream)); + if (!data || !data->size()) { return kFailure; } - unsigned char* buf = (unsigned char*)autoMal.get(); + unsigned char* buf = (unsigned char*) data->data(); // Make sure original PKM header is there... SkASSERT(etc1_pkm_is_valid(buf)); diff --git a/src/ports/SkImageDecoder_CG.cpp b/src/ports/SkImageDecoder_CG.cpp index 4be5874253..2deaf21d76 100644 --- a/src/ports/SkImageDecoder_CG.cpp +++ b/src/ports/SkImageDecoder_CG.cpp @@ -10,6 +10,7 @@ #include "SkCGUtils.h" #include "SkColorPriv.h" +#include "SkData.h" #include "SkImageDecoder.h" #include "SkImageEncoder.h" #include "SkMovie.h" @@ -28,17 +29,19 @@ #include #endif -static void malloc_release_proc(void* info, const void* data, size_t size) { - sk_free(info); +static void data_unref_proc(void* skdata, const void*, size_t) { + SkASSERT(skdata); + static_cast(skdata)->unref(); } static CGDataProviderRef SkStreamToDataProvider(SkStream* stream) { // TODO: use callbacks, so we don't have to load all the data into RAM - SkAutoMalloc storage; - const size_t len = SkCopyStreamToStorage(&storage, stream); - void* data = storage.detach(); + SkData* skdata = SkCopyStreamToData(stream); + if (!skdata) { + return nullptr; + } - return CGDataProviderCreateWithData(data, data, len, malloc_release_proc); + return CGDataProviderCreateWithData(skdata, skdata->data(), skdata->size(), data_unref_proc); } static CGImageSourceRef SkStreamToCGImageSource(SkStream* stream) { -- cgit v1.2.3