diff --git a/include/core/SkData.h b/include/core/SkData.h index 6da8e11366..e25ef50d71 100644 --- a/include/core/SkData.h +++ b/include/core/SkData.h @@ -11,6 +11,7 @@ #include "SkRefCnt.h" struct SkFILE; +class SkStream; /** * SkData holds an immutable data buffer. Not only is the data immutable, @@ -137,6 +138,13 @@ public: */ static SkData* NewFromFD(int fd); + /** + * Attempt to read size bytes into a SkData. If the read succeeds, return the data, + * else return NULL. Either way the stream's cursor may have been changed as a result + * of calling read(). + */ + static SkData* NewFromStream(SkStream*, size_t size); + /** * Create a new dataref using a subset of the data in the specified * src dataref. diff --git a/src/core/SkData.cpp b/src/core/SkData.cpp index a7a3d8d559..11cab7e98b 100644 --- a/src/core/SkData.cpp +++ b/src/core/SkData.cpp @@ -9,6 +9,7 @@ #include "SkLazyPtr.h" #include "SkOSFile.h" #include "SkReadBuffer.h" +#include "SkStream.h" #include "SkWriteBuffer.h" static void sk_inplace_sentinel_releaseproc(const void*, size_t, void*) { @@ -198,3 +199,14 @@ SkData* SkData::NewWithCString(const char cstr[]) { } return NewWithCopy(cstr, size); } + +/////////////////////////////////////////////////////////////////////////////// + +SkData* SkData::NewFromStream(SkStream* stream, size_t size) { + SkAutoDataUnref data(SkData::NewUninitialized(size)); + if (stream->read(data->writable_data(), size) != size) { + return NULL; + } + return data.detach(); +} + diff --git a/src/core/SkPictureData.cpp b/src/core/SkPictureData.cpp index a2d10e3b9b..656515830c 100644 --- a/src/core/SkPictureData.cpp +++ b/src/core/SkPictureData.cpp @@ -390,14 +390,13 @@ bool SkPictureData::parseStreamTag(SkStream* stream, SkDEBUGCODE(bool haveBuffer = false;) switch (tag) { - case SK_PICT_READER_TAG: { - SkAutoMalloc storage(size); - if (stream->read(storage.get(), size) != size) { + case SK_PICT_READER_TAG: + SkASSERT(NULL == fOpData); + fOpData = SkData::NewFromStream(stream, size); + if (!fOpData) { return false; } - SkASSERT(NULL == fOpData); - fOpData = SkData::NewFromMalloc(storage.detach(), size); - } break; + break; case SK_PICT_FACTORY_TAG: { SkASSERT(!haveBuffer); // Remove this code when v21 and below are no longer supported. At the @@ -538,13 +537,13 @@ bool SkPictureData::parseBufferTag(SkReadBuffer& buffer, } } break; case SK_PICT_READER_TAG: { - SkAutoMalloc storage(size); - if (!buffer.readByteArray(storage.get(), size) || + SkAutoDataUnref data(SkData::NewUninitialized(size)); + if (!buffer.readByteArray(data->writable_data(), size) || !buffer.validate(NULL == fOpData)) { return false; } SkASSERT(NULL == fOpData); - fOpData = SkData::NewFromMalloc(storage.detach(), size); + fOpData = data.detach(); } break; case SK_PICT_PICTURE_TAG: { if (!buffer.validate((0 == fPictureCount) && (NULL == fPictureRefs))) { diff --git a/src/core/SkStream.cpp b/src/core/SkStream.cpp index e290b21580..ff14a8b26f 100644 --- a/src/core/SkStream.cpp +++ b/src/core/SkStream.cpp @@ -869,13 +869,7 @@ SkData* SkCopyStreamToData(SkStream* stream) { SkASSERT(stream != NULL); if (stream->hasLength()) { - const size_t length = stream->getLength(); - SkData* data = SkData::NewUninitialized(length); - if (stream->read(data->writable_data(), length) != length) { - data->unref(); - data = NULL; - } - return data; + return SkData::NewFromStream(stream, stream->getLength()); } SkDynamicMemoryWStream tempStream; @@ -902,9 +896,7 @@ SkStreamRewindable* SkStreamRewindableFromSkStream(SkStream* stream) { if (stream->hasPosition()) { // If stream has length, but can't rewind. length -= stream->getPosition(); } - SkAutoTUnref data(SkData::NewUninitialized(length)); - SkDEBUGCODE(size_t read =) stream->read(data->writable_data(), length); - SkASSERT(length == read); + SkAutoTUnref data(SkData::NewFromStream(stream, length)); return SkNEW_ARGS(SkMemoryStream, (data.get())); } SkDynamicMemoryWStream tempStream; diff --git a/src/images/SkDecodingImageGenerator.cpp b/src/images/SkDecodingImageGenerator.cpp index 359eb370ad..3b5cb784ed 100644 --- a/src/images/SkDecodingImageGenerator.cpp +++ b/src/images/SkDecodingImageGenerator.cpp @@ -139,13 +139,11 @@ SkData* DecodingImageGenerator::onRefEncodedData() { return NULL; } size_t length = fStream->getLength(); - if (0 == length) { - return NULL; + if (length) { + fData = SkData::NewFromStream(fStream, length); } - fData = SkData::NewUninitialized(length); - SkCheckResult(fStream->read(fData->writable_data(), length), length); } - return SkRef(fData); + return SkSafeRef(fData); } bool DecodingImageGenerator::onGetPixels(const SkImageInfo& info, diff --git a/tests/MallocPixelRefTest.cpp b/tests/MallocPixelRefTest.cpp index c20c8836b2..e267d6f72c 100644 --- a/tests/MallocPixelRefTest.cpp +++ b/tests/MallocPixelRefTest.cpp @@ -32,22 +32,18 @@ DEF_TEST(MallocPixelRef, reporter) { { size_t rowBytes = info.minRowBytes() - 1; size_t size = info.getSafeSize(rowBytes); - void* addr = sk_malloc_throw(size); - SkAutoDataUnref data(SkData::NewFromMalloc(addr, size)); + SkAutoDataUnref data(SkData::NewUninitialized(size)); SkAutoTUnref pr( - SkMallocPixelRef::NewWithData(info, rowBytes, - NULL, data.get())); + SkMallocPixelRef::NewWithData(info, rowBytes, NULL, data)); // rowbytes too small. REPORTER_ASSERT(reporter, NULL == pr.get()); } { size_t rowBytes = info.minRowBytes() + 2; size_t size = info.getSafeSize(rowBytes) - 1; - void* addr = sk_malloc_throw(size); - SkAutoDataUnref data(SkData::NewFromMalloc(addr, size)); + SkAutoDataUnref data(SkData::NewUninitialized(size)); SkAutoTUnref pr( - SkMallocPixelRef::NewWithData(info, rowBytes, NULL, - data.get())); + SkMallocPixelRef::NewWithData(info, rowBytes, NULL, data)); // data too small. REPORTER_ASSERT(reporter, NULL == pr.get()); } @@ -77,7 +73,6 @@ DEF_TEST(MallocPixelRef, reporter) { { int x = 0; SkAutoMalloc memory(size); - REPORTER_ASSERT(reporter, memory.get() != NULL); SkAutoTUnref pr( SkMallocPixelRef::NewWithProc(info, rowBytes, NULL, memory.get(), set_to_one_proc, @@ -98,9 +93,7 @@ DEF_TEST(MallocPixelRef, reporter) { REPORTER_ASSERT(reporter, addr == pr->pixels()); } { - void* addr = sk_malloc_throw(size); - SkAutoDataUnref data(SkData::NewFromMalloc(addr, size)); - REPORTER_ASSERT(reporter, data.get() != NULL); + SkAutoDataUnref data(SkData::NewUninitialized(size)); SkData* dataPtr = data.get(); REPORTER_ASSERT(reporter, dataPtr->unique()); SkAutoTUnref pr( diff --git a/tests/SurfaceTest.cpp b/tests/SurfaceTest.cpp index a175f64951..dcf9a859dd 100644 --- a/tests/SurfaceTest.cpp +++ b/tests/SurfaceTest.cpp @@ -74,8 +74,7 @@ static void test_image(skiatest::Reporter* reporter) { SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1); size_t rowBytes = info.minRowBytes(); size_t size = info.getSafeSize(rowBytes); - void* addr = sk_malloc_throw(size); - SkData* data = SkData::NewFromMalloc(addr, size); + SkData* data = SkData::NewUninitialized(size); REPORTER_ASSERT(reporter, 1 == data->getRefCnt()); SkImage* image = SkImage::NewRasterData(info, data, rowBytes); @@ -92,9 +91,9 @@ static SkImage* createImage(ImageType imageType, GrContext* context, const size_t rowBytes = info.minRowBytes(); const size_t size = rowBytes * info.height(); - void* addr = sk_malloc_throw(size); + SkAutoTUnref data(SkData::NewUninitialized(size)); + void* addr = data->writable_data(); sk_memset32((SkPMColor*)addr, pmcolor, SkToInt(size >> 2)); - SkAutoTUnref data(SkData::NewFromMalloc(addr, size)); switch (imageType) { case kRasterCopy_ImageType: diff --git a/tools/lua/lua_app.cpp b/tools/lua/lua_app.cpp index 50b1352c2d..023ddaeaca 100644 --- a/tools/lua/lua_app.cpp +++ b/tools/lua/lua_app.cpp @@ -18,14 +18,11 @@ extern "C" { } static SkData* read_into_data(const char file[]) { - SkAutoTUnref stream(SkStream::NewFromFile(file)); - if (!stream.get()) { - return SkData::NewEmpty(); + SkData* data = SkData::NewFromFileName(file); + if (!data) { + data = SkData::NewEmpty(); } - size_t len = stream->getLength(); - void* buffer = sk_malloc_throw(len); - stream->read(buffer, len); - return SkData::NewFromMalloc(buffer, len); + return data; } int tool_main(int argc, char** argv); diff --git a/tools/lua/lua_pictures.cpp b/tools/lua/lua_pictures.cpp index 635cf0bed0..6c7087fc05 100644 --- a/tools/lua/lua_pictures.cpp +++ b/tools/lua/lua_pictures.cpp @@ -47,17 +47,6 @@ static SkPicture* load_picture(const char path[]) { return pic; } -static SkData* read_into_data(const char file[]) { - SkAutoTUnref stream(SkStream::NewFromFile(file)); - if (!stream.get()) { - return SkData::NewEmpty(); - } - size_t len = stream->getLength(); - void* buffer = sk_malloc_throw(len); - stream->read(buffer, len); - return SkData::NewFromMalloc(buffer, len); -} - static void call_canvas(lua_State* L, SkLuaCanvas* canvas, const char pictureFile[], const char funcName[]) { lua_getglobal(L, funcName); @@ -97,7 +86,10 @@ int tool_main(int argc, char** argv) { SkLua L(summary); for (int i = 0; i < FLAGS_luaFile.count(); ++i) { - SkAutoDataUnref data(read_into_data(FLAGS_luaFile[i])); + SkAutoDataUnref data(SkData::NewFromFileName(FLAGS_luaFile[i])); + if (NULL == data.get()) { + data.reset(SkData::NewEmpty()); + } if (!FLAGS_quiet) { SkDebugf("loading %s...\n", FLAGS_luaFile[i]); } diff --git a/tools/skdiff_utils.cpp b/tools/skdiff_utils.cpp index 14a9fec7e4..a00ea7b567 100644 --- a/tools/skdiff_utils.cpp +++ b/tools/skdiff_utils.cpp @@ -25,27 +25,11 @@ bool are_buffers_equal(SkData* skdata1, SkData* skdata2) { } SkData* read_file(const char* file_path) { - SkFILEStream fileStream(file_path); - if (!fileStream.isValid()) { + SkData* data = SkData::NewFromFileName(file_path); + if (!data) { SkDebugf("WARNING: could not open file <%s> for reading\n", file_path); - return NULL; } - size_t bytesInFile = fileStream.getLength(); - size_t bytesLeftToRead = bytesInFile; - - void* bufferStart = sk_malloc_throw(bytesInFile); - char* bufferPointer = (char*)bufferStart; - while (bytesLeftToRead > 0) { - size_t bytesReadThisTime = fileStream.read(bufferPointer, bytesLeftToRead); - if (0 == bytesReadThisTime) { - SkDebugf("WARNING: error reading from <%s>\n", file_path); - sk_free(bufferStart); - return NULL; - } - bytesLeftToRead -= bytesReadThisTime; - bufferPointer += bytesReadThisTime; - } - return SkData::NewFromMalloc(bufferStart, bytesInFile); + return data; } bool get_bitmap(SkData* fileBits, DiffResource& resource, SkImageDecoder::Mode mode) {