use SkData::NewUninitialized
BUG=skia: R=bungeman@google.com Author: reed@google.com Review URL: https://codereview.chromium.org/565803005
This commit is contained in:
parent
9db328b64f
commit
9594da111d
@ -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.
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
|
@ -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))) {
|
||||
|
@ -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<SkData> data(SkData::NewUninitialized(length));
|
||||
SkDEBUGCODE(size_t read =) stream->read(data->writable_data(), length);
|
||||
SkASSERT(length == read);
|
||||
SkAutoTUnref<SkData> data(SkData::NewFromStream(stream, length));
|
||||
return SkNEW_ARGS(SkMemoryStream, (data.get()));
|
||||
}
|
||||
SkDynamicMemoryWStream tempStream;
|
||||
|
@ -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,
|
||||
|
@ -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<SkMallocPixelRef> 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<SkMallocPixelRef> 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<SkMallocPixelRef> 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<SkMallocPixelRef> pr(
|
||||
|
@ -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<SkData> data(SkData::NewUninitialized(size));
|
||||
void* addr = data->writable_data();
|
||||
sk_memset32((SkPMColor*)addr, pmcolor, SkToInt(size >> 2));
|
||||
SkAutoTUnref<SkData> data(SkData::NewFromMalloc(addr, size));
|
||||
|
||||
switch (imageType) {
|
||||
case kRasterCopy_ImageType:
|
||||
|
@ -18,14 +18,11 @@ extern "C" {
|
||||
}
|
||||
|
||||
static SkData* read_into_data(const char file[]) {
|
||||
SkAutoTUnref<SkStream> 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);
|
||||
|
@ -47,17 +47,6 @@ static SkPicture* load_picture(const char path[]) {
|
||||
return pic;
|
||||
}
|
||||
|
||||
static SkData* read_into_data(const char file[]) {
|
||||
SkAutoTUnref<SkStream> 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]);
|
||||
}
|
||||
|
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user