Make sp variants for SkData
Review URL: https://codereview.chromium.org/1779263003
This commit is contained in:
parent
51c8cfc64c
commit
fde05114e6
@ -14,6 +14,8 @@
|
||||
|
||||
class SkStream;
|
||||
|
||||
#define SK_SUPPORT_LEGACY_DATA_FACTORIES
|
||||
|
||||
/**
|
||||
* SkData holds an immutable data buffer. Not only is the data immutable,
|
||||
* but the actual ptr that is returned (by data() or bytes()) is guaranteed
|
||||
@ -67,6 +69,7 @@ public:
|
||||
* effectively returning 0 == memcmp(...)
|
||||
*/
|
||||
bool equals(const SkData* other) const;
|
||||
bool equals(sk_sp<const SkData>& other) const { return this->equals(other.get()); }
|
||||
|
||||
/**
|
||||
* Function that, if provided, will be called when the SkData goes out
|
||||
@ -77,13 +80,14 @@ public:
|
||||
/**
|
||||
* Create a new dataref by copying the specified data
|
||||
*/
|
||||
static SkData* NewWithCopy(const void* data, size_t length);
|
||||
static sk_sp<SkData> MakeWithCopy(const void* data, size_t length);
|
||||
|
||||
|
||||
/**
|
||||
* Create a new data with uninitialized contents. The caller should call writable_data()
|
||||
* to write into the buffer, but this must be done before another ref() is made.
|
||||
*/
|
||||
static SkData* NewUninitialized(size_t length);
|
||||
static sk_sp<SkData> MakeUninitialized(size_t length);
|
||||
|
||||
/**
|
||||
* Create a new dataref by copying the specified c-string
|
||||
@ -91,33 +95,33 @@ public:
|
||||
* equal to strlen(cstr) + 1. If cstr is NULL, it will be treated the same
|
||||
* as "".
|
||||
*/
|
||||
static SkData* NewWithCString(const char cstr[]);
|
||||
static sk_sp<SkData> MakeWithCString(const char cstr[]);
|
||||
|
||||
/**
|
||||
* Create a new dataref, taking the ptr as is, and using the
|
||||
* releaseproc to free it. The proc may be NULL.
|
||||
*/
|
||||
static SkData* NewWithProc(const void* ptr, size_t length, ReleaseProc proc, void* context);
|
||||
static sk_sp<SkData> MakeWithProc(const void* ptr, size_t length, ReleaseProc proc, void* ctx);
|
||||
|
||||
/**
|
||||
* Call this when the data parameter is already const and will outlive the lifetime of the
|
||||
* SkData. Suitable for with const globals.
|
||||
*/
|
||||
static SkData* NewWithoutCopy(const void* data, size_t length) {
|
||||
return NewWithProc(data, length, DummyReleaseProc, NULL);
|
||||
static sk_sp<SkData> MakeWithoutCopy(const void* data, size_t length) {
|
||||
return MakeWithProc(data, length, DummyReleaseProc, nullptr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new dataref from a pointer allocated by malloc. The Data object
|
||||
* takes ownership of that allocation, and will handling calling sk_free.
|
||||
*/
|
||||
static SkData* NewFromMalloc(const void* data, size_t length);
|
||||
static sk_sp<SkData> MakeFromMalloc(const void* data, size_t length);
|
||||
|
||||
/**
|
||||
* Create a new dataref the file with the specified path.
|
||||
* If the file cannot be opened, this returns NULL.
|
||||
*/
|
||||
static SkData* NewFromFileName(const char path[]);
|
||||
static sk_sp<SkData> MakeFromFileName(const char path[]);
|
||||
|
||||
/**
|
||||
* Create a new dataref from a stdio FILE.
|
||||
@ -126,7 +130,7 @@ public:
|
||||
* The FILE must be open for reading only.
|
||||
* Returns NULL on failure.
|
||||
*/
|
||||
static SkData* NewFromFILE(FILE* f);
|
||||
static sk_sp<SkData> MakeFromFILE(FILE* f);
|
||||
|
||||
/**
|
||||
* Create a new dataref from a file descriptor.
|
||||
@ -135,26 +139,57 @@ public:
|
||||
* The file descriptor must be open for reading only.
|
||||
* Returns NULL on failure.
|
||||
*/
|
||||
static SkData* NewFromFD(int fd);
|
||||
static sk_sp<SkData> MakeFromFD(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);
|
||||
static sk_sp<SkData> MakeFromStream(SkStream*, size_t size);
|
||||
|
||||
/**
|
||||
* Create a new dataref using a subset of the data in the specified
|
||||
* src dataref.
|
||||
*/
|
||||
static SkData* NewSubset(const SkData* src, size_t offset, size_t length);
|
||||
static sk_sp<SkData> MakeSubset(const SkData* src, size_t offset, size_t length);
|
||||
|
||||
/**
|
||||
* Returns a new empty dataref (or a reference to a shared empty dataref).
|
||||
* New or shared, the caller must see that unref() is eventually called.
|
||||
*/
|
||||
static SkData* NewEmpty();
|
||||
static sk_sp<SkData> MakeEmpty();
|
||||
|
||||
#ifdef SK_SUPPORT_LEGACY_DATA_FACTORIES
|
||||
static SkData* NewWithCopy(const void* data, size_t length) {
|
||||
return MakeWithCopy(data, length).release();
|
||||
}
|
||||
static SkData* NewUninitialized(size_t length) {
|
||||
return MakeUninitialized(length).release();
|
||||
}
|
||||
static SkData* NewWithCString(const char cstr[]) {
|
||||
return MakeWithCString(cstr).release();
|
||||
}
|
||||
static SkData* NewWithProc(const void* ptr, size_t length, ReleaseProc proc, void* context) {
|
||||
return MakeWithProc(ptr, length, proc, context).release();
|
||||
}
|
||||
static SkData* NewWithoutCopy(const void* data, size_t length) {
|
||||
return MakeWithoutCopy(data, length).release();
|
||||
}
|
||||
static SkData* NewFromMalloc(const void* data, size_t length) {
|
||||
return MakeFromMalloc(data, length).release();
|
||||
}
|
||||
static SkData* NewFromFileName(const char path[]) { return MakeFromFileName(path).release(); }
|
||||
static SkData* NewFromFILE(FILE* f) { return MakeFromFILE(f).release(); }
|
||||
static SkData* NewFromFD(int fd) { return MakeFromFD(fd).release(); }
|
||||
static SkData* NewFromStream(SkStream* stream, size_t size) {
|
||||
return MakeFromStream(stream, size).release();
|
||||
}
|
||||
static SkData* NewSubset(const SkData* src, size_t offset, size_t length) {
|
||||
return MakeSubset(src, offset, length).release();
|
||||
}
|
||||
static SkData* NewEmpty() { return MakeEmpty().release(); }
|
||||
#endif
|
||||
|
||||
private:
|
||||
ReleaseProc fReleaseProc;
|
||||
@ -179,14 +214,16 @@ private:
|
||||
friend SkData* sk_new_empty_data();
|
||||
|
||||
// shared internal factory
|
||||
static SkData* PrivateNewWithCopy(const void* srcOrNull, size_t length);
|
||||
static sk_sp<SkData> PrivateNewWithCopy(const void* srcOrNull, size_t length);
|
||||
|
||||
static void DummyReleaseProc(const void*, void*) {}
|
||||
|
||||
typedef SkRefCnt INHERITED;
|
||||
};
|
||||
|
||||
#ifdef SK_SUPPORT_LEGACY_DATA_FACTORIES
|
||||
/** Typedef of SkAutoTUnref<SkData> for automatically unref-ing a SkData. */
|
||||
typedef SkAutoTUnref<SkData> SkAutoDataUnref;
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
@ -8,11 +8,10 @@
|
||||
#ifndef SkStream_DEFINED
|
||||
#define SkStream_DEFINED
|
||||
|
||||
#include "SkData.h"
|
||||
#include "SkRefCnt.h"
|
||||
#include "SkScalar.h"
|
||||
|
||||
class SkData;
|
||||
|
||||
class SkStream;
|
||||
class SkStreamRewindable;
|
||||
class SkStreamSeekable;
|
||||
@ -270,11 +269,11 @@ public:
|
||||
const void* getMemoryBase() override;
|
||||
|
||||
private:
|
||||
FILE* fFILE;
|
||||
FILE* fFILE;
|
||||
SkString fName;
|
||||
Ownership fOwnership;
|
||||
// fData is lazilly initialized when needed.
|
||||
mutable SkAutoTUnref<SkData> fData;
|
||||
mutable sk_sp<SkData> fData;
|
||||
|
||||
typedef SkStreamAsset INHERITED;
|
||||
};
|
||||
@ -291,10 +290,12 @@ public:
|
||||
|
||||
/** 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*);
|
||||
|
||||
virtual ~SkMemoryStream();
|
||||
/** Creates the stream to read from the specified data */
|
||||
SkMemoryStream(sk_sp<SkData>);
|
||||
|
||||
/** Resets the stream to the specified data and length,
|
||||
just like the constructor.
|
||||
@ -341,8 +342,8 @@ public:
|
||||
const void* getMemoryBase() override;
|
||||
|
||||
private:
|
||||
SkData* fData;
|
||||
size_t fOffset;
|
||||
sk_sp<SkData> fData;
|
||||
size_t fOffset;
|
||||
|
||||
typedef SkStreamMemory INHERITED;
|
||||
};
|
||||
@ -417,7 +418,7 @@ private:
|
||||
Block* fHead;
|
||||
Block* fTail;
|
||||
size_t fBytesWritten;
|
||||
mutable SkData* fCopy; // is invalidated if we write after it is created
|
||||
mutable sk_sp<SkData> fCopy; // is invalidated if we write after it is created
|
||||
|
||||
void invalidateCopy();
|
||||
|
||||
|
@ -246,7 +246,7 @@ public:
|
||||
/**
|
||||
* Captures a snapshot of the data as it is right now, and return it.
|
||||
*/
|
||||
SkData* snapshotAsData() const;
|
||||
sk_sp<SkData> snapshotAsData() const;
|
||||
private:
|
||||
void growToAtLeast(size_t size);
|
||||
|
||||
|
@ -67,7 +67,7 @@ private:
|
||||
static void initProcessingLuts(ColorCubeProcesingCache* cache);
|
||||
};
|
||||
|
||||
SkAutoDataUnref fCubeData;
|
||||
sk_sp<SkData> fCubeData;
|
||||
int32_t fUniqueID;
|
||||
|
||||
mutable ColorCubeProcesingCache fCache;
|
||||
|
@ -677,15 +677,15 @@ sk_maskfilter_t* sk_maskfilter_new_blur(sk_blurstyle_t cstyle, float sigma) {
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
sk_data_t* sk_data_new_with_copy(const void* src, size_t length) {
|
||||
return ToData(SkData::NewWithCopy(src, length));
|
||||
return ToData(SkData::MakeWithCopy(src, length).release());
|
||||
}
|
||||
|
||||
sk_data_t* sk_data_new_from_malloc(const void* memory, size_t length) {
|
||||
return ToData(SkData::NewFromMalloc(memory, length));
|
||||
return ToData(SkData::MakeFromMalloc(memory, length).release());
|
||||
}
|
||||
|
||||
sk_data_t* sk_data_new_subset(const sk_data_t* csrc, size_t offset, size_t length) {
|
||||
return ToData(SkData::NewSubset(AsData(csrc), offset, length));
|
||||
return ToData(SkData::MakeSubset(AsData(csrc), offset, length).release());
|
||||
}
|
||||
|
||||
void sk_data_ref(const sk_data_t* cdata) {
|
||||
|
@ -1163,7 +1163,7 @@ bool SkBitmap::ReadRawPixels(SkReadBuffer* buffer, SkBitmap* bitmap) {
|
||||
return false;
|
||||
}
|
||||
|
||||
SkAutoDataUnref data(SkData::NewUninitialized(SkToSizeT(ramSize)));
|
||||
sk_sp<SkData> data(SkData::MakeUninitialized(SkToSizeT(ramSize)));
|
||||
unsigned char* dst = (unsigned char*)data->writable_data();
|
||||
buffer->readByteArray(dst, SkToSizeT(snugSize));
|
||||
|
||||
|
@ -59,9 +59,9 @@ size_t SkData::copyRange(size_t offset, size_t length, void* buffer) const {
|
||||
return length;
|
||||
}
|
||||
|
||||
SkData* SkData::PrivateNewWithCopy(const void* srcOrNull, size_t length) {
|
||||
sk_sp<SkData> SkData::PrivateNewWithCopy(const void* srcOrNull, size_t length) {
|
||||
if (0 == length) {
|
||||
return SkData::NewEmpty();
|
||||
return SkData::MakeEmpty();
|
||||
}
|
||||
|
||||
const size_t actualLength = length + sizeof(SkData);
|
||||
@ -75,14 +75,15 @@ SkData* SkData::PrivateNewWithCopy(const void* srcOrNull, size_t length) {
|
||||
if (srcOrNull) {
|
||||
memcpy(data->writable_data(), srcOrNull, length);
|
||||
}
|
||||
return data;
|
||||
return sk_sp<SkData>(data);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
SK_DECLARE_STATIC_ONCE_PTR(SkData, gEmpty);
|
||||
SkData* SkData::NewEmpty() {
|
||||
return SkRef(gEmpty.get([]{return new SkData(nullptr, 0, nullptr, nullptr); }));
|
||||
sk_sp<SkData> SkData::MakeEmpty() {
|
||||
SkData* data = SkRef(gEmpty.get([]{return new SkData(nullptr, 0, nullptr, nullptr); }));
|
||||
return sk_sp<SkData>(data);
|
||||
}
|
||||
|
||||
// assumes fPtr was allocated via sk_malloc
|
||||
@ -90,21 +91,21 @@ static void sk_free_releaseproc(const void* ptr, void*) {
|
||||
sk_free((void*)ptr);
|
||||
}
|
||||
|
||||
SkData* SkData::NewFromMalloc(const void* data, size_t length) {
|
||||
return new SkData(data, length, sk_free_releaseproc, nullptr);
|
||||
sk_sp<SkData> SkData::MakeFromMalloc(const void* data, size_t length) {
|
||||
return sk_sp<SkData>(new SkData(data, length, sk_free_releaseproc, nullptr));
|
||||
}
|
||||
|
||||
SkData* SkData::NewWithCopy(const void* src, size_t length) {
|
||||
sk_sp<SkData> SkData::MakeWithCopy(const void* src, size_t length) {
|
||||
SkASSERT(src);
|
||||
return PrivateNewWithCopy(src, length);
|
||||
}
|
||||
|
||||
SkData* SkData::NewUninitialized(size_t length) {
|
||||
sk_sp<SkData> SkData::MakeUninitialized(size_t length) {
|
||||
return PrivateNewWithCopy(nullptr, length);
|
||||
}
|
||||
|
||||
SkData* SkData::NewWithProc(const void* ptr, size_t length, ReleaseProc proc, void* context) {
|
||||
return new SkData(ptr, length, proc, context);
|
||||
sk_sp<SkData> SkData::MakeWithProc(const void* ptr, size_t length, ReleaseProc proc, void* ctx) {
|
||||
return sk_sp<SkData>(new SkData(ptr, length, proc, ctx));
|
||||
}
|
||||
|
||||
// assumes fPtr was allocated with sk_fmmap
|
||||
@ -113,34 +114,34 @@ static void sk_mmap_releaseproc(const void* addr, void* ctx) {
|
||||
sk_fmunmap(addr, length);
|
||||
}
|
||||
|
||||
SkData* SkData::NewFromFILE(FILE* f) {
|
||||
sk_sp<SkData> SkData::MakeFromFILE(FILE* f) {
|
||||
size_t size;
|
||||
void* addr = sk_fmmap(f, &size);
|
||||
if (nullptr == addr) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return SkData::NewWithProc(addr, size, sk_mmap_releaseproc, reinterpret_cast<void*>(size));
|
||||
return SkData::MakeWithProc(addr, size, sk_mmap_releaseproc, reinterpret_cast<void*>(size));
|
||||
}
|
||||
|
||||
SkData* SkData::NewFromFileName(const char path[]) {
|
||||
sk_sp<SkData> SkData::MakeFromFileName(const char path[]) {
|
||||
FILE* f = path ? sk_fopen(path, kRead_SkFILE_Flag) : nullptr;
|
||||
if (nullptr == f) {
|
||||
return nullptr;
|
||||
}
|
||||
SkData* data = NewFromFILE(f);
|
||||
auto data = MakeFromFILE(f);
|
||||
sk_fclose(f);
|
||||
return data;
|
||||
}
|
||||
|
||||
SkData* SkData::NewFromFD(int fd) {
|
||||
sk_sp<SkData> SkData::MakeFromFD(int fd) {
|
||||
size_t size;
|
||||
void* addr = sk_fdmmap(fd, &size);
|
||||
if (nullptr == addr) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return SkData::NewWithProc(addr, size, sk_mmap_releaseproc, nullptr);
|
||||
return SkData::MakeWithProc(addr, size, sk_mmap_releaseproc, nullptr);
|
||||
}
|
||||
|
||||
// assumes context is a SkData
|
||||
@ -149,7 +150,7 @@ static void sk_dataref_releaseproc(const void*, void* context) {
|
||||
src->unref();
|
||||
}
|
||||
|
||||
SkData* SkData::NewSubset(const SkData* src, size_t offset, size_t length) {
|
||||
sk_sp<SkData> SkData::MakeSubset(const SkData* src, size_t offset, size_t length) {
|
||||
/*
|
||||
We could, if we wanted/need to, just make a deep copy of src's data,
|
||||
rather than referencing it. This would duplicate the storage (of the
|
||||
@ -158,7 +159,7 @@ SkData* SkData::NewSubset(const SkData* src, size_t offset, size_t length) {
|
||||
|
||||
size_t available = src->size();
|
||||
if (offset >= available || 0 == length) {
|
||||
return SkData::NewEmpty();
|
||||
return SkData::MakeEmpty();
|
||||
}
|
||||
available -= offset;
|
||||
if (length > available) {
|
||||
@ -167,11 +168,11 @@ SkData* SkData::NewSubset(const SkData* src, size_t offset, size_t length) {
|
||||
SkASSERT(length > 0);
|
||||
|
||||
src->ref(); // this will be balanced in sk_dataref_releaseproc
|
||||
return new SkData(src->bytes() + offset, length, sk_dataref_releaseproc,
|
||||
const_cast<SkData*>(src));
|
||||
return sk_sp<SkData>(new SkData(src->bytes() + offset, length, sk_dataref_releaseproc,
|
||||
const_cast<SkData*>(src)));
|
||||
}
|
||||
|
||||
SkData* SkData::NewWithCString(const char cstr[]) {
|
||||
sk_sp<SkData> SkData::MakeWithCString(const char cstr[]) {
|
||||
size_t size;
|
||||
if (nullptr == cstr) {
|
||||
cstr = "";
|
||||
@ -179,16 +180,16 @@ SkData* SkData::NewWithCString(const char cstr[]) {
|
||||
} else {
|
||||
size = strlen(cstr) + 1;
|
||||
}
|
||||
return NewWithCopy(cstr, size);
|
||||
return MakeWithCopy(cstr, size);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
SkData* SkData::NewFromStream(SkStream* stream, size_t size) {
|
||||
SkAutoDataUnref data(SkData::NewUninitialized(size));
|
||||
sk_sp<SkData> SkData::MakeFromStream(SkStream* stream, size_t size) {
|
||||
sk_sp<SkData> data(SkData::MakeUninitialized(size));
|
||||
if (stream->read(data->writable_data(), size) != size) {
|
||||
return nullptr;
|
||||
}
|
||||
return data.detach();
|
||||
return data;
|
||||
}
|
||||
|
||||
|
@ -15,9 +15,9 @@ SkData* SkValidatingSerializeFlattenable(SkFlattenable* flattenable) {
|
||||
SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag);
|
||||
writer.writeFlattenable(flattenable);
|
||||
size_t size = writer.bytesWritten();
|
||||
SkData* data = SkData::NewUninitialized(size);
|
||||
auto data = SkData::MakeUninitialized(size);
|
||||
writer.writeToMemory(data->writable_data());
|
||||
return data;
|
||||
return data.release();
|
||||
}
|
||||
|
||||
SkFlattenable* SkValidatingDeserializeFlattenable(const void* data, size_t size,
|
||||
|
@ -96,7 +96,7 @@ bool SkFontDescriptor::Deserialize(SkStream* stream, SkFontDescriptor* result) {
|
||||
|
||||
size_t length = stream->readPackedUInt();
|
||||
if (length > 0) {
|
||||
SkAutoTUnref<SkData> data(SkData::NewUninitialized(length));
|
||||
sk_sp<SkData> data(SkData::MakeUninitialized(length));
|
||||
if (stream->read(data->writable_data(), length) == length) {
|
||||
result->fFontData.reset(new SkFontData(new SkMemoryStream(data),
|
||||
index, axis, axisCount));
|
||||
|
@ -1943,7 +1943,7 @@ void SkPaint::unflatten(SkReadBuffer& buffer) {
|
||||
// Annotations have moved to drawAnnotation, so we just drop this one on the floor.
|
||||
SkString key;
|
||||
buffer.readString(&key);
|
||||
SkSafeUnref(buffer.readByteArrayAsData());
|
||||
(void)buffer.readByteArrayAsData();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
@ -140,9 +140,9 @@ SkPicture* SkPicture::Forwardport(const SkPictInfo& info, const SkPictureData* d
|
||||
}
|
||||
|
||||
static bool default_install(const void* src, size_t length, SkBitmap* dst) {
|
||||
SkAutoTUnref<SkData> encoded(SkData::NewWithCopy(src, length));
|
||||
sk_sp<SkData> encoded(SkData::MakeWithCopy(src, length));
|
||||
return encoded && SkDEPRECATED_InstallDiscardablePixelRef(
|
||||
SkImageGenerator::NewFromEncoded(encoded), dst);
|
||||
SkImageGenerator::NewFromEncoded(encoded.get()), dst);
|
||||
}
|
||||
|
||||
SkPicture* SkPicture::CreateFromStream(SkStream* stream) {
|
||||
|
@ -93,13 +93,10 @@ void SkPictureData::init() {
|
||||
fTextBlobCount = 0;
|
||||
fImageRefs = nullptr;
|
||||
fImageCount = 0;
|
||||
fOpData = nullptr;
|
||||
fFactoryPlayback = nullptr;
|
||||
}
|
||||
|
||||
SkPictureData::~SkPictureData() {
|
||||
SkSafeUnref(fOpData);
|
||||
|
||||
for (int i = 0; i < fPictureCount; i++) {
|
||||
fPictureRefs[i]->unref();
|
||||
}
|
||||
@ -358,7 +355,7 @@ bool SkPictureData::parseStreamTag(SkStream* stream,
|
||||
switch (tag) {
|
||||
case SK_PICT_READER_TAG:
|
||||
SkASSERT(nullptr == fOpData);
|
||||
fOpData = SkData::NewFromStream(stream, size);
|
||||
fOpData = SkData::MakeFromStream(stream, size);
|
||||
if (!fOpData) {
|
||||
return false;
|
||||
}
|
||||
@ -529,13 +526,13 @@ bool SkPictureData::parseBufferTag(SkReadBuffer& buffer, uint32_t tag, uint32_t
|
||||
}
|
||||
break;
|
||||
case SK_PICT_READER_TAG: {
|
||||
SkAutoDataUnref data(SkData::NewUninitialized(size));
|
||||
auto data(SkData::MakeUninitialized(size));
|
||||
if (!buffer.readByteArray(data->writable_data(), size) ||
|
||||
!buffer.validate(nullptr == fOpData)) {
|
||||
return false;
|
||||
}
|
||||
SkASSERT(nullptr == fOpData);
|
||||
fOpData = data.detach();
|
||||
fOpData = std::move(data);
|
||||
} break;
|
||||
case SK_PICT_PICTURE_TAG:
|
||||
if (!new_array_from_buffer(buffer, size, &fPictureRefs, &fPictureCount,
|
||||
|
@ -77,7 +77,7 @@ public:
|
||||
|
||||
int opCount() const { return fContentInfo.numOperations(); }
|
||||
|
||||
const SkData* opData() const { return fOpData; }
|
||||
const sk_sp<SkData>& opData() const { return fOpData; }
|
||||
|
||||
protected:
|
||||
explicit SkPictureData(const SkPictInfo& info);
|
||||
@ -158,7 +158,7 @@ private:
|
||||
SkTArray<SkPaint> fPaints;
|
||||
SkTArray<SkPath> fPaths;
|
||||
|
||||
SkData* fOpData; // opcodes and parameters
|
||||
sk_sp<SkData> fOpData; // opcodes and parameters
|
||||
|
||||
const SkPicture** fPictureRefs;
|
||||
int fPictureCount;
|
||||
|
@ -178,8 +178,7 @@ void SkPicturePlayback::handleOp(SkReader32* reader,
|
||||
case DRAW_ANNOTATION: {
|
||||
const SkRect& rect = reader->skipT<SkRect>();
|
||||
const char* key = reader->readString();
|
||||
SkAutoTUnref<SkData> value(reader->readData());
|
||||
canvas->drawAnnotation(rect, key, value);
|
||||
canvas->drawAnnotation(rect, key, reader->readData().get());
|
||||
} break;
|
||||
case DRAW_ATLAS: {
|
||||
const SkPaint* paint = fPictureData->getPaint(reader);
|
||||
|
@ -43,15 +43,15 @@ public:
|
||||
return fImageRefs;
|
||||
}
|
||||
|
||||
SkData* opData(bool deepCopy) const {
|
||||
sk_sp<SkData> opData(bool deepCopy) const {
|
||||
this->validate(fWriter.bytesWritten(), 0);
|
||||
|
||||
if (fWriter.bytesWritten() == 0) {
|
||||
return SkData::NewEmpty();
|
||||
return SkData::MakeEmpty();
|
||||
}
|
||||
|
||||
if (deepCopy) {
|
||||
return SkData::NewWithCopy(fWriter.contiguousArray(), fWriter.bytesWritten());
|
||||
return SkData::MakeWithCopy(fWriter.contiguousArray(), fWriter.bytesWritten());
|
||||
}
|
||||
|
||||
return fWriter.snapshotAsData();
|
||||
|
@ -323,9 +323,9 @@ const SkData* SkAutoPixmapStorage::detachPixelsAsData() {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const SkData* data = SkData::NewFromMalloc(fStorage, this->getSafeSize());
|
||||
auto data = SkData::MakeFromMalloc(fStorage, this->getSafeSize());
|
||||
fStorage = nullptr;
|
||||
this->INHERITED::reset();
|
||||
|
||||
return data;
|
||||
return data.release();
|
||||
}
|
||||
|
@ -152,14 +152,14 @@ public:
|
||||
virtual bool readPointArray(SkPoint* points, size_t size);
|
||||
virtual bool readScalarArray(SkScalar* values, size_t size);
|
||||
|
||||
SkData* readByteArrayAsData() {
|
||||
sk_sp<SkData> readByteArrayAsData() {
|
||||
size_t len = this->getArrayCount();
|
||||
if (!this->validateAvailable(len)) {
|
||||
return SkData::NewEmpty();
|
||||
return SkData::MakeEmpty();
|
||||
}
|
||||
void* buffer = sk_malloc_throw(len);
|
||||
this->readByteArray(buffer, len);
|
||||
return SkData::NewFromMalloc(buffer, len);
|
||||
return SkData::MakeFromMalloc(buffer, len);
|
||||
}
|
||||
|
||||
// helpers to get info about arrays and binary data
|
||||
|
@ -135,12 +135,12 @@ public:
|
||||
*/
|
||||
size_t readIntoString(SkString* copy);
|
||||
|
||||
SkData* readData() {
|
||||
sk_sp<SkData> readData() {
|
||||
uint32_t byteLength = this->readU32();
|
||||
if (0 == byteLength) {
|
||||
return SkData::NewEmpty();
|
||||
return SkData::MakeEmpty();
|
||||
}
|
||||
return SkData::NewWithCopy(this->skip(byteLength), byteLength);
|
||||
return SkData::MakeWithCopy(this->skip(byteLength), byteLength);
|
||||
}
|
||||
|
||||
private:
|
||||
|
@ -245,8 +245,8 @@ SkStreamAsset* SkFILEStream::duplicate() const {
|
||||
}
|
||||
}
|
||||
|
||||
fData.reset(SkData::NewFromFILE(fFILE));
|
||||
if (nullptr == fData.get()) {
|
||||
fData = SkData::MakeFromFILE(fFILE);
|
||||
if (nullptr == fData) {
|
||||
return nullptr;
|
||||
}
|
||||
return new SkMemoryStream(fData);
|
||||
@ -283,21 +283,21 @@ const void* SkFILEStream::getMemoryBase() {
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
static SkData* newFromParams(const void* src, size_t size, bool copyData) {
|
||||
static sk_sp<SkData> newFromParams(const void* src, size_t size, bool copyData) {
|
||||
if (copyData) {
|
||||
return SkData::NewWithCopy(src, size);
|
||||
return SkData::MakeWithCopy(src, size);
|
||||
} else {
|
||||
return SkData::NewWithoutCopy(src, size);
|
||||
return SkData::MakeWithoutCopy(src, size);
|
||||
}
|
||||
}
|
||||
|
||||
SkMemoryStream::SkMemoryStream() {
|
||||
fData = SkData::NewEmpty();
|
||||
fData = SkData::MakeEmpty();
|
||||
fOffset = 0;
|
||||
}
|
||||
|
||||
SkMemoryStream::SkMemoryStream(size_t size) {
|
||||
fData = SkData::NewUninitialized(size);
|
||||
fData = SkData::MakeUninitialized(size);
|
||||
fOffset = 0;
|
||||
}
|
||||
|
||||
@ -306,44 +306,41 @@ SkMemoryStream::SkMemoryStream(const void* src, size_t size, bool copyData) {
|
||||
fOffset = 0;
|
||||
}
|
||||
|
||||
SkMemoryStream::SkMemoryStream(SkData* data) {
|
||||
if (nullptr == data) {
|
||||
fData = SkData::NewEmpty();
|
||||
} else {
|
||||
fData = data;
|
||||
fData->ref();
|
||||
SkMemoryStream::SkMemoryStream(sk_sp<SkData> data) : fData(std::move(data)) {
|
||||
if (nullptr == fData) {
|
||||
fData = SkData::MakeEmpty();
|
||||
}
|
||||
fOffset = 0;
|
||||
}
|
||||
|
||||
SkMemoryStream::~SkMemoryStream() {
|
||||
fData->unref();
|
||||
SkMemoryStream::SkMemoryStream(SkData* data) {
|
||||
if (nullptr == data) {
|
||||
fData = SkData::MakeEmpty();
|
||||
} else {
|
||||
fData = sk_ref_sp(data);
|
||||
}
|
||||
fOffset = 0;
|
||||
}
|
||||
|
||||
void SkMemoryStream::setMemoryOwned(const void* src, size_t size) {
|
||||
fData->unref();
|
||||
fData = SkData::NewFromMalloc(src, size);
|
||||
fData = SkData::MakeFromMalloc(src, size);
|
||||
fOffset = 0;
|
||||
}
|
||||
|
||||
void SkMemoryStream::setMemory(const void* src, size_t size, bool copyData) {
|
||||
fData->unref();
|
||||
fData = newFromParams(src, size, copyData);
|
||||
fOffset = 0;
|
||||
}
|
||||
|
||||
SkData* SkMemoryStream::copyToData() const {
|
||||
fData->ref();
|
||||
return fData;
|
||||
return SkSafeRef(fData.get());
|
||||
}
|
||||
|
||||
SkData* SkMemoryStream::setData(SkData* data) {
|
||||
fData->unref();
|
||||
if (nullptr == data) {
|
||||
fData = SkData::NewEmpty();
|
||||
fData = SkData::MakeEmpty();
|
||||
} else {
|
||||
fData = data;
|
||||
fData->ref();
|
||||
fData = sk_ref_sp(data);
|
||||
}
|
||||
fOffset = 0;
|
||||
return data;
|
||||
@ -519,9 +516,8 @@ struct SkDynamicMemoryWStream::Block {
|
||||
};
|
||||
|
||||
SkDynamicMemoryWStream::SkDynamicMemoryWStream()
|
||||
: fHead(nullptr), fTail(nullptr), fBytesWritten(0), fCopy(nullptr)
|
||||
{
|
||||
}
|
||||
: fHead(nullptr), fTail(nullptr), fBytesWritten(0)
|
||||
{}
|
||||
|
||||
SkDynamicMemoryWStream::~SkDynamicMemoryWStream()
|
||||
{
|
||||
@ -655,19 +651,16 @@ void SkDynamicMemoryWStream::padToAlign4()
|
||||
|
||||
SkData* SkDynamicMemoryWStream::copyToData() const {
|
||||
if (nullptr == fCopy) {
|
||||
SkData* data = SkData::NewUninitialized(fBytesWritten);
|
||||
auto data = SkData::MakeUninitialized(fBytesWritten);
|
||||
// be sure to call copyTo() before we assign to fCopy
|
||||
this->copyTo(data->writable_data());
|
||||
fCopy = data;
|
||||
fCopy = std::move(data);
|
||||
}
|
||||
return SkRef(fCopy);
|
||||
return SkRef(fCopy.get());
|
||||
}
|
||||
|
||||
void SkDynamicMemoryWStream::invalidateCopy() {
|
||||
if (fCopy) {
|
||||
fCopy->unref();
|
||||
fCopy = nullptr;
|
||||
}
|
||||
fCopy = nullptr;
|
||||
}
|
||||
|
||||
class SkBlockMemoryRefCnt : public SkRefCnt {
|
||||
@ -854,21 +847,21 @@ bool SkDebugWStream::write(const void* buffer, size_t size)
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
static SkData* mmap_filename(const char path[]) {
|
||||
static sk_sp<SkData> mmap_filename(const char path[]) {
|
||||
FILE* file = sk_fopen(path, kRead_SkFILE_Flag);
|
||||
if (nullptr == file) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
SkData* data = SkData::NewFromFILE(file);
|
||||
auto data = SkData::MakeFromFILE(file);
|
||||
sk_fclose(file);
|
||||
return data;
|
||||
}
|
||||
|
||||
SkStreamAsset* SkStream::NewFromFile(const char path[]) {
|
||||
SkAutoTUnref<SkData> data(mmap_filename(path));
|
||||
if (data.get()) {
|
||||
return new SkMemoryStream(data.get());
|
||||
auto data(mmap_filename(path));
|
||||
if (data) {
|
||||
return new SkMemoryStream(std::move(data));
|
||||
}
|
||||
|
||||
// If we get here, then our attempt at using mmap failed, so try normal
|
||||
@ -882,11 +875,11 @@ SkStreamAsset* SkStream::NewFromFile(const char path[]) {
|
||||
}
|
||||
|
||||
// Declared in SkStreamPriv.h:
|
||||
SkData* SkCopyStreamToData(SkStream* stream) {
|
||||
sk_sp<SkData> SkCopyStreamToData(SkStream* stream) {
|
||||
SkASSERT(stream != nullptr);
|
||||
|
||||
if (stream->hasLength()) {
|
||||
return SkData::NewFromStream(stream, stream->getLength());
|
||||
return SkData::MakeFromStream(stream, stream->getLength());
|
||||
}
|
||||
|
||||
SkDynamicMemoryWStream tempStream;
|
||||
@ -896,7 +889,7 @@ SkData* SkCopyStreamToData(SkStream* stream) {
|
||||
size_t bytesRead = stream->read(buffer, bufferSize);
|
||||
tempStream.write(buffer, bytesRead);
|
||||
} while (!stream->isAtEnd());
|
||||
return tempStream.copyToData();
|
||||
return sk_sp<SkData>(tempStream.copyToData());
|
||||
}
|
||||
|
||||
bool SkStreamCopy(SkWStream* out, SkStream* input) {
|
||||
|
@ -8,6 +8,8 @@
|
||||
#ifndef SkStreamPriv_DEFINED
|
||||
#define SkStreamPriv_DEFINED
|
||||
|
||||
#include "SkRefCnt.h"
|
||||
|
||||
class SkData;
|
||||
class SkStream;
|
||||
class SkWStream;
|
||||
@ -23,7 +25,7 @@ class SkWStream;
|
||||
* will have a ref count of one upon return and belongs to the
|
||||
* caller. Returns nullptr on failure.
|
||||
*/
|
||||
SkData* SkCopyStreamToData(SkStream* stream);
|
||||
sk_sp<SkData> SkCopyStreamToData(SkStream* stream);
|
||||
|
||||
/**
|
||||
* Copies the input stream from the current position to the end.
|
||||
|
@ -76,6 +76,6 @@ void SkWriter32::growToAtLeast(size_t size) {
|
||||
}
|
||||
}
|
||||
|
||||
SkData* SkWriter32::snapshotAsData() const {
|
||||
return SkData::NewWithCopy(fData, fUsed);
|
||||
sk_sp<SkData> SkWriter32::snapshotAsData() const {
|
||||
return SkData::MakeWithCopy(fData, fUsed);
|
||||
}
|
||||
|
@ -138,17 +138,17 @@ void SkColorCubeFilter::filterSpan(const SkPMColor src[], int count, SkPMColor d
|
||||
|
||||
SkFlattenable* SkColorCubeFilter::CreateProc(SkReadBuffer& buffer) {
|
||||
int cubeDimension = buffer.readInt();
|
||||
SkAutoDataUnref cubeData(buffer.readByteArrayAsData());
|
||||
if (!buffer.validate(is_valid_3D_lut(cubeData, cubeDimension))) {
|
||||
auto cubeData(buffer.readByteArrayAsData());
|
||||
if (!buffer.validate(is_valid_3D_lut(cubeData.get(), cubeDimension))) {
|
||||
return nullptr;
|
||||
}
|
||||
return Create(cubeData, cubeDimension);
|
||||
return Create(cubeData.get(), cubeDimension);
|
||||
}
|
||||
|
||||
void SkColorCubeFilter::flatten(SkWriteBuffer& buffer) const {
|
||||
this->INHERITED::flatten(buffer);
|
||||
buffer.writeInt(fCache.cubeDimension());
|
||||
buffer.writeDataAsByteArray(fCubeData);
|
||||
buffer.writeDataAsByteArray(fCubeData.get());
|
||||
}
|
||||
|
||||
#ifndef SK_IGNORE_TO_STRING
|
||||
|
@ -44,7 +44,7 @@ static inline int read_24bit(const uint8_t* buf) {
|
||||
}
|
||||
|
||||
SkImageDecoder::Result SkASTCImageDecoder::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) {
|
||||
SkAutoTUnref<SkData> data(SkCopyStreamToData(stream));
|
||||
auto data = SkCopyStreamToData(stream);
|
||||
if (!data || !data->size()) {
|
||||
return kFailure;
|
||||
}
|
||||
|
@ -50,12 +50,12 @@ private:
|
||||
|
||||
SkImageDecoder::Result SkKTXImageDecoder::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) {
|
||||
// TODO: Implement SkStream::copyToData() that's cheap for memory and file streams
|
||||
SkAutoDataUnref data(SkCopyStreamToData(stream));
|
||||
auto data = SkCopyStreamToData(stream);
|
||||
if (nullptr == data) {
|
||||
return kFailure;
|
||||
}
|
||||
|
||||
SkKTXFile ktxFile(data);
|
||||
SkKTXFile ktxFile(data.get());
|
||||
if (!ktxFile.valid()) {
|
||||
return kFailure;
|
||||
}
|
||||
|
@ -97,7 +97,7 @@ 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.
|
||||
|
||||
SkAutoTUnref<SkData> data(SkCopyStreamToData(stream));
|
||||
auto data = SkCopyStreamToData(stream);
|
||||
if (!data) {
|
||||
return kFailure;
|
||||
}
|
||||
|
@ -73,9 +73,8 @@ static int calculateRowBytesFor8888(int w, int bitCount)
|
||||
return 0;
|
||||
}
|
||||
|
||||
SkImageDecoder::Result SkICOImageDecoder::onDecode(SkStream* stream, SkBitmap* bm, Mode mode)
|
||||
{
|
||||
SkAutoTUnref<SkData> data(SkCopyStreamToData(stream));
|
||||
SkImageDecoder::Result SkICOImageDecoder::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) {
|
||||
auto data = SkCopyStreamToData(stream);
|
||||
if (!data) {
|
||||
return kFailure;
|
||||
}
|
||||
|
@ -34,7 +34,7 @@ private:
|
||||
/////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
SkImageDecoder::Result SkPKMImageDecoder::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) {
|
||||
SkAutoTUnref<SkData> data(SkCopyStreamToData(stream));
|
||||
sk_sp<SkData> data(SkCopyStreamToData(stream));
|
||||
if (!data || !data->size()) {
|
||||
return kFailure;
|
||||
}
|
||||
|
@ -36,7 +36,7 @@ static void data_unref_proc(void* skdata, const void*, size_t) {
|
||||
|
||||
static CGDataProviderRef SkStreamToDataProvider(SkStream* stream) {
|
||||
// TODO: use callbacks, so we don't have to load all the data into RAM
|
||||
SkData* skdata = SkCopyStreamToData(stream);
|
||||
SkData* skdata = SkCopyStreamToData(stream).release();
|
||||
if (!skdata) {
|
||||
return nullptr;
|
||||
}
|
||||
|
@ -83,7 +83,7 @@ SkData* SkOTUtils::RenameFont(SkStreamAsset* fontData, const char* fontName, int
|
||||
size_t originalDataSize = fontData->getLength() - oldNameTablePhysicalSize;
|
||||
size_t newDataSize = originalDataSize + nameTablePhysicalSize;
|
||||
|
||||
SkAutoTUnref<SkData> rewrittenFontData(SkData::NewUninitialized(newDataSize));
|
||||
auto rewrittenFontData = SkData::MakeUninitialized(newDataSize);
|
||||
SK_OT_BYTE* data = static_cast<SK_OT_BYTE*>(rewrittenFontData->writable_data());
|
||||
|
||||
if (fontData->read(data, oldNameTableOffset) < oldNameTableOffset) {
|
||||
@ -157,7 +157,7 @@ SkData* SkOTUtils::RenameFont(SkStreamAsset* fontData, const char* fontName, int
|
||||
}
|
||||
}
|
||||
|
||||
return rewrittenFontData.detach();
|
||||
return rewrittenFontData.release();
|
||||
}
|
||||
|
||||
|
||||
|
@ -898,20 +898,21 @@ DEF_TEST(Codec_raw_notseekable, r) {
|
||||
DEF_TEST(Codec_webp_peek, r) {
|
||||
const char* path = "baby_tux.webp";
|
||||
SkString fullPath(GetResourcePath(path));
|
||||
SkAutoTUnref<SkData> data(SkData::NewFromFileName(fullPath.c_str()));
|
||||
auto data = SkData::MakeFromFileName(fullPath.c_str());
|
||||
if (!data) {
|
||||
SkDebugf("Missing resource '%s'\n", path);
|
||||
return;
|
||||
}
|
||||
|
||||
// The limit is less than webp needs to peek or read.
|
||||
SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(new LimitedPeekingMemStream(data, 25)));
|
||||
SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(
|
||||
new LimitedPeekingMemStream(data.get(), 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, 0)));
|
||||
codec.reset(SkCodec::NewFromStream(new LimitedPeekingMemStream(data.get(), 0)));
|
||||
REPORTER_ASSERT(r, codec);
|
||||
|
||||
test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
|
||||
@ -930,12 +931,12 @@ DEF_TEST(Codec_wbmp, r) {
|
||||
}
|
||||
|
||||
// Modify the stream to contain a second byte with some bits set.
|
||||
SkAutoTUnref<SkData> data(SkCopyStreamToData(stream));
|
||||
auto data = SkCopyStreamToData(stream);
|
||||
uint8_t* writeableData = static_cast<uint8_t*>(data->writable_data());
|
||||
writeableData[1] = static_cast<uint8_t>(~0x9F);
|
||||
|
||||
// SkCodec should support this.
|
||||
SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(data));
|
||||
SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(data.get()));
|
||||
REPORTER_ASSERT(r, codec);
|
||||
if (!codec) {
|
||||
return;
|
||||
|
@ -304,12 +304,12 @@ DEF_TEST(Writer32_data, reporter) {
|
||||
sizeWritten += sizes[2];
|
||||
REPORTER_ASSERT(reporter, sizeWritten == writer.bytesWritten());
|
||||
|
||||
SkAutoTUnref<SkData> result(writer.snapshotAsData());
|
||||
auto result(writer.snapshotAsData());
|
||||
|
||||
SkReader32 reader(result->data(), result->size());
|
||||
SkAutoTUnref<SkData> d0(reader.readData()),
|
||||
d1(reader.readData()),
|
||||
d2(reader.readData());
|
||||
auto d0(reader.readData()),
|
||||
d1(reader.readData()),
|
||||
d2(reader.readData());
|
||||
|
||||
REPORTER_ASSERT(reporter, 0 == d0->size());
|
||||
REPORTER_ASSERT(reporter, strlen(str)+1 == d1->size());
|
||||
|
Loading…
Reference in New Issue
Block a user