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
This commit is contained in:
scroggo 2016-01-19 07:53:39 -08:00 committed by Commit bot
parent a6bf4c54aa
commit a913275bda
7 changed files with 41 additions and 111 deletions

View File

@ -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<SkStreamRewindable> 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<SkData> 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<const char*>(input->getMemoryBase());
if (base && input->hasPosition() && input->hasLength()) {

View File

@ -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.

View File

@ -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<SkData> 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<uint32_t*>(buf))) == kASTCMagicNumber);

View File

@ -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<SkData> 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();

View File

@ -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<SkData> 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?

View File

@ -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<SkData> 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));

View File

@ -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 <MobileCoreServices/MobileCoreServices.h>
#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*>(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) {