Update SkData and SkDataSet to make better use of SkFlattenable.

TEST=DataRefTest.cpp

Review URL: https://codereview.appspot.com/6441076

git-svn-id: http://skia.googlecode.com/svn/trunk@4850 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
djsollen@google.com 2012-07-31 13:17:31 +00:00
parent 2d537a18f4
commit a1da103431
4 changed files with 50 additions and 34 deletions

View File

@ -11,14 +11,14 @@
#ifndef SkData_DEFINED
#define SkData_DEFINED
#include "SkRefCnt.h"
#include "SkFlattenable.h"
/**
* 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
* to always be the same for the life of this instance.
*/
class SK_API SkData : public SkRefCnt {
class SK_API SkData : public SkFlattenable {
public:
SK_DECLARE_INST_COUNT(SkData)
@ -100,6 +100,12 @@ public:
*/
static SkData* NewEmpty();
SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkData)
protected:
SkData(SkFlattenableReadBuffer&);
virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE;
private:
ReleaseProc fReleaseProc;
void* fReleaseProcContext;
@ -117,7 +123,7 @@ private:
// access to the destructor.
friend class SkAutoTUnref<SkData>::BlockRef<SkData>;
typedef SkRefCnt INHERITED;
typedef SkFlattenable INHERITED;
};
/**

View File

@ -8,20 +8,18 @@
#ifndef SkDataSet_DEFINED
#define SkDataSet_DEFINED
#include "SkRefCnt.h"
#include "SkData.h"
#include "SkFlattenable.h"
class SkStream;
class SkWStream;
class SkFlattenableReadBuffer;
class SkFlattenableWriteBuffer;
class SkDataSet : public SkRefCnt {
class SkDataSet : public SkFlattenable {
public:
/**
* Returns a new empty dataset. Note: since SkDataSet is immutable, this
* "new" set may be the same one that was returned before, but each
* returned object must have its reference-count balanced regardles.
* returned object must have its reference-count balanced regardless.
*
* SkDataSet* empty = SkDataSet::NewEmpty();
* ...
@ -73,13 +71,18 @@ public:
explicit SkDataSet(SkStream*);
void writeToStream(SkWStream*) const;
explicit SkDataSet(SkFlattenableReadBuffer&);
void flatten(SkFlattenableWriteBuffer&) const;
SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkDataSet)
protected:
SkDataSet(SkFlattenableReadBuffer&);
virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE;
private:
int32_t fCount;
uint32_t fKeySize;
Pair* fPairs;
typedef SkFlattenable INHERITED;
};
#endif

View File

@ -29,12 +29,12 @@ SkData* SkAnnotation::find(const char name[]) const {
SkAnnotation::SkAnnotation(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) {
fFlags = buffer.readU32();
fDataSet = SkNEW_ARGS(SkDataSet, (buffer));
fDataSet = (SkDataSet*)buffer.readFlattenable();
}
void SkAnnotation::flatten(SkFlattenableWriteBuffer& buffer) const {
buffer.write32(fFlags);
fDataSet->flatten(buffer);
buffer.writeFlattenable(fDataSet);
}
SK_DEFINE_FLATTENABLE_REGISTRAR(SkAnnotation)

View File

@ -119,6 +119,32 @@ SkData* SkData::NewWithCString(const char cstr[]) {
return NewWithCopy(cstr, size);
}
///////////////////////////////////////////////////////////////////////////////
void SkData::flatten(SkFlattenableWriteBuffer& buffer) const {
buffer.write32(fSize);
buffer.writePad(fPtr, fSize);
}
SkData::SkData(SkFlattenableReadBuffer& buffer) {
fSize = buffer.readU32();
fReleaseProcContext = NULL;
if (0 == fSize) {
fPtr = NULL;
fReleaseProc = NULL;
} else {
// buffer.read expects a 4-byte aligned size
size_t size4 = SkAlign4(fSize);
void* data = sk_malloc_throw(size4);
buffer.read(data, size4);
fPtr = data;
fReleaseProc = sk_free_releaseproc;
}
}
SK_DEFINE_FLATTENABLE_REGISTRAR(SkData)
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
@ -135,26 +161,6 @@ static SkData* dupdata(SkData* data) {
return data;
}
static SkData* read_data(SkFlattenableReadBuffer& buffer) {
size_t size = buffer.readU32();
if (0 == size) {
return SkData::NewEmpty();
} else {
// buffer.read expects a 4-byte aligned size
size_t size4 = SkAlign4(size);
void* block = sk_malloc_throw(size4);
buffer.read(block, size4);
// we pass the "real" size to NewFromMalloc, since its needs to report
// the same size that was written.
return SkData::NewFromMalloc(block, size);
}
}
static void write_data(SkFlattenableWriteBuffer& buffer, SkData* data) {
buffer.write32(data->size());
buffer.writePad(data->data(), data->size());
}
static SkData* findValue(const char key[], const SkDataSet::Pair array[], int n) {
for (int i = 0; i < n; ++i) {
if (!strcmp(key, array[i].fKey)) {
@ -247,7 +253,7 @@ void SkDataSet::flatten(SkFlattenableWriteBuffer& buffer) const {
// our first key points to all the key storage
buffer.writePad(fPairs[0].fKey, fKeySize);
for (int i = 0; i < fCount; ++i) {
write_data(buffer, fPairs[i].fValue);
buffer.writeFlattenable(fPairs[i].fValue);
}
}
}
@ -285,7 +291,7 @@ SkDataSet::SkDataSet(SkFlattenableReadBuffer& buffer) {
for (int i = 0; i < fCount; ++i) {
fPairs[i].fKey = keyStorage;
keyStorage += strlen(keyStorage) + 1;
fPairs[i].fValue = read_data(buffer);
fPairs[i].fValue = (SkData*)buffer.readFlattenable();
}
} else {
fKeySize = 0;
@ -302,3 +308,4 @@ SkDataSet* SkDataSet::NewEmpty() {
return gEmptySet;
}
SK_DEFINE_FLATTENABLE_REGISTRAR(SkDataSet)