rename SkPtrRecorder and related wrappers to SkPtrSet, since that is the pattern

is it providing. Also add a templated wrapper to handle typecasting of ptr types.



git-svn-id: http://skia.googlecode.com/svn/trunk@1214 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
mike@reedtribe.org 2011-04-29 01:44:52 +00:00
parent e8f754a41c
commit e9e08cc7b2
9 changed files with 95 additions and 106 deletions

View File

@ -118,48 +118,22 @@ private:
#include "SkPtrRecorder.h"
class SkRefCntRecorder : public SkPtrRecorder {
/**
* Subclass of SkTPtrSet specialed to call ref() and unref() when the
* base class's incPtr() and decPtr() are called. This makes it a valid owner
* of each ptr, which is released when the set is reset or destroyed.
*/
class SkRefCntSet : public SkTPtrSet<SkRefCnt*> {
public:
virtual ~SkRefCntRecorder();
virtual ~SkRefCntSet();
/** Add a refcnt object to the set and ref it if not already present,
or if it is already present, do nothing. Either way, returns 0 if obj
is null, or a base-1 index if obj is not null.
*/
uint32_t record(SkRefCnt* ref) {
return this->recordPtr(ref);
}
// This does not change the owner counts on the objects
void get(SkRefCnt* array[]) const {
this->getPtrs((void**)array);
}
protected:
// overrides
virtual void incPtr(void*);
virtual void decPtr(void*);
private:
typedef SkPtrRecorder INHERITED;
};
class SkFactoryRecorder : public SkPtrRecorder {
public:
/** Add a factory to the set. If it is null return 0, otherwise return a
base-1 index for the factory.
*/
uint32_t record(SkFlattenable::Factory fact) {
return this->recordPtr((void*)fact);
}
void get(SkFlattenable::Factory array[]) const {
this->getPtrs((void**)array);
}
private:
typedef SkPtrRecorder INHERITED;
};
class SkFactorySet : public SkTPtrSet<SkFlattenable::Factory> {};
class SkFlattenableWriteBuffer : public SkWriter32 {
public:
@ -171,14 +145,14 @@ public:
void writeFunctionPtr(void*);
void writeFlattenable(SkFlattenable* flattenable);
SkRefCntRecorder* getTypefaceRecorder() const { return fTFRecorder; }
SkRefCntRecorder* setTypefaceRecorder(SkRefCntRecorder*);
SkRefCntSet* getTypefaceRecorder() const { return fTFSet; }
SkRefCntSet* setTypefaceRecorder(SkRefCntSet*);
SkRefCntRecorder* getRefCntRecorder() const { return fRCRecorder; }
SkRefCntRecorder* setRefCntRecorder(SkRefCntRecorder*);
SkRefCntSet* getRefCntRecorder() const { return fRCSet; }
SkRefCntSet* setRefCntRecorder(SkRefCntSet*);
SkFactoryRecorder* getFactoryRecorder() const { return fFactoryRecorder; }
SkFactoryRecorder* setFactoryRecorder(SkFactoryRecorder*);
SkFactorySet* getFactoryRecorder() const { return fFactorySet; }
SkFactorySet* setFactoryRecorder(SkFactorySet*);
enum Flags {
kCrossProcess_Flag = 0x01
@ -195,10 +169,10 @@ public:
bool persistTypeface() const { return (fFlags & kCrossProcess_Flag) != 0; }
private:
Flags fFlags;
SkRefCntRecorder* fTFRecorder;
SkRefCntRecorder* fRCRecorder;
SkFactoryRecorder* fFactoryRecorder;
Flags fFlags;
SkRefCntSet* fTFSet;
SkRefCntSet* fRCSet;
SkFactorySet* fFactorySet;
typedef SkWriter32 INHERITED;
};

View File

@ -14,8 +14,8 @@
* limitations under the License.
*/
#ifndef SkPtrRecorder_DEFINED
#define SkPtrRecorder_DEFINED
#ifndef SkPtrSet_DEFINED
#define SkPtrSet_DEFINED
#include "SkRefCnt.h"
#include "SkTDArray.h"
@ -26,15 +26,15 @@
* and decPtr(). incPtr() is called each time a unique ptr is added ot the
* set. decPtr() is called on each ptr when the set is destroyed or reset.
*/
class SkPtrRecorder : public SkRefCnt {
class SkPtrSet : public SkRefCnt {
public:
/**
* Add the specified ptr to the set, returning a unique 32bit ID for it
* [1...N]. Duplicate ptrs will return the same ID.
*
* If the ptr is NULL, it is not recorded, and 0 is returned.
* If the ptr is NULL, it is not added, and 0 is returned.
*/
uint32_t recordPtr(void*);
uint32_t add(void*);
/**
* Return the number of (non-null) ptrs in the set.
@ -48,7 +48,7 @@ public:
*
* incPtr() and decPtr() are not called during this operation.
*/
void getPtrs(void* array[]) const;
void copyToArray(void* array[]) const;
/**
* Call decPtr() on each ptr in the set, and the reset the size of the set
@ -67,7 +67,7 @@ private:
};
// we store the ptrs in sorted-order (using Cmp) so that we can efficiently
// detect duplicates when recordPtr() is called. Hence we need to store the
// detect duplicates when add() is called. Hence we need to store the
// ptr and its ID/fIndex explicitly, since the ptr's position in the array
// is not related to its "index".
SkTDArray<Pair> fList;
@ -77,4 +77,22 @@ private:
typedef SkRefCnt INHERITED;
};
/**
* Templated wrapper for SkPtrSet, just meant to automate typecasting
* parameters to and from void* (which the base class expects).
*/
template <typename T> class SkTPtrSet : public SkPtrSet {
public:
uint32_t add(T ptr) {
return this->INHERITED::add((void*)ptr);
}
void copyToArray(T* array) const {
this->INHERITED::copyToArray((void**)array);
}
private:
typedef SkPtrSet INHERITED;
};
#endif

View File

@ -117,48 +117,45 @@ void* SkFlattenableReadBuffer::readFunctionPtr() {
SkFlattenableWriteBuffer::SkFlattenableWriteBuffer(size_t minSize) :
INHERITED(minSize) {
fFlags = (Flags)0;
fRCRecorder = NULL;
fTFRecorder = NULL;
fFactoryRecorder = NULL;
fRCSet = NULL;
fTFSet = NULL;
fFactorySet = NULL;
}
SkFlattenableWriteBuffer::~SkFlattenableWriteBuffer() {
SkSafeUnref(fRCRecorder);
SkSafeUnref(fTFRecorder);
SkSafeUnref(fFactoryRecorder);
SkSafeUnref(fRCSet);
SkSafeUnref(fTFSet);
SkSafeUnref(fFactorySet);
}
SkRefCntRecorder* SkFlattenableWriteBuffer::setRefCntRecorder(
SkRefCntRecorder* rec) {
SkRefCnt_SafeAssign(fRCRecorder, rec);
SkRefCntSet* SkFlattenableWriteBuffer::setRefCntRecorder(SkRefCntSet* rec) {
SkRefCnt_SafeAssign(fRCSet, rec);
return rec;
}
SkRefCntRecorder* SkFlattenableWriteBuffer::setTypefaceRecorder(
SkRefCntRecorder* rec) {
SkRefCnt_SafeAssign(fTFRecorder, rec);
SkRefCntSet* SkFlattenableWriteBuffer::setTypefaceRecorder(SkRefCntSet* rec) {
SkRefCnt_SafeAssign(fTFSet, rec);
return rec;
}
SkFactoryRecorder* SkFlattenableWriteBuffer::setFactoryRecorder(
SkFactoryRecorder* rec) {
SkRefCnt_SafeAssign(fFactoryRecorder, rec);
SkFactorySet* SkFlattenableWriteBuffer::setFactoryRecorder(SkFactorySet* rec) {
SkRefCnt_SafeAssign(fFactorySet, rec);
return rec;
}
void SkFlattenableWriteBuffer::writeTypeface(SkTypeface* obj) {
if (NULL == obj || NULL == fTFRecorder) {
if (NULL == obj || NULL == fTFSet) {
this->write32(0);
} else {
this->write32(fTFRecorder->record(obj));
this->write32(fTFSet->add(obj));
}
}
void SkFlattenableWriteBuffer::writeRefCnt(SkRefCnt* obj) {
if (NULL == obj || NULL == fRCRecorder) {
if (NULL == obj || NULL == fRCSet) {
this->write32(0);
} else {
this->write32(fRCRecorder->record(obj));
this->write32(fRCSet->add(obj));
}
}
@ -168,8 +165,8 @@ void SkFlattenableWriteBuffer::writeFlattenable(SkFlattenable* flattenable) {
factory = flattenable->getFactory();
}
if (fFactoryRecorder) {
this->write32(fFactoryRecorder->record(factory));
if (fFactorySet) {
this->write32(fFactorySet->add(factory));
} else {
this->writeFunctionPtr((void*)factory);
}
@ -193,16 +190,16 @@ void SkFlattenableWriteBuffer::writeFunctionPtr(void* proc) {
///////////////////////////////////////////////////////////////////////////////
SkRefCntRecorder::~SkRefCntRecorder() {
SkRefCntSet::~SkRefCntSet() {
// call this now, while our decPtr() is sill in scope
this->reset();
}
void SkRefCntRecorder::incPtr(void* ptr) {
void SkRefCntSet::incPtr(void* ptr) {
((SkRefCnt*)ptr)->ref();
}
void SkRefCntRecorder::decPtr(void* ptr) {
void SkRefCntSet::decPtr(void* ptr) {
((SkRefCnt*)ptr)->unref();
}

View File

@ -16,7 +16,7 @@ SkFlatData* SkFlatData::Alloc(SkChunkAlloc* heap, int32_t size, int index) {
}
SkFlatBitmap* SkFlatBitmap::Flatten(SkChunkAlloc* heap, const SkBitmap& bitmap,
int index, SkRefCntRecorder* rec) {
int index, SkRefCntSet* rec) {
SkFlattenableWriteBuffer buffer(1024);
buffer.setRefCntRecorder(rec);
@ -82,8 +82,8 @@ void SkFlatMatrix::dump() const {
///////////////////////////////////////////////////////////////////////////////
SkFlatPaint* SkFlatPaint::Flatten(SkChunkAlloc* heap, const SkPaint& paint,
int index, SkRefCntRecorder* rec,
SkRefCntRecorder* faceRecorder) {
int index, SkRefCntSet* rec,
SkRefCntSet* faceRecorder) {
SkFlattenableWriteBuffer buffer(2*sizeof(SkPaint));
buffer.setRefCntRecorder(rec);
buffer.setTypefaceRecorder(faceRecorder);
@ -221,7 +221,7 @@ SkRefCntPlayback::~SkRefCntPlayback() {
this->reset(NULL);
}
void SkRefCntPlayback::reset(const SkRefCntRecorder* rec) {
void SkRefCntPlayback::reset(const SkRefCntSet* rec) {
for (int i = 0; i < fCount; i++) {
SkASSERT(fArray[i]);
fArray[i]->unref();
@ -231,7 +231,7 @@ void SkRefCntPlayback::reset(const SkRefCntRecorder* rec) {
if (rec) {
fCount = rec->count();
fArray = SkNEW_ARRAY(SkRefCnt*, fCount);
rec->get(fArray);
rec->copyToArray(fArray);
for (int i = 0; i < fCount; i++) {
fArray[i]->ref();
}

View File

@ -57,7 +57,7 @@ public:
int count() const { return fCount; }
void reset(const SkRefCntRecorder*);
void reset(const SkRefCntSet*);
void setCount(int count);
SkRefCnt* set(int index, SkRefCnt*);
@ -121,7 +121,7 @@ protected:
class SkFlatBitmap : public SkFlatData {
public:
static SkFlatBitmap* Flatten(SkChunkAlloc*, const SkBitmap&, int index,
SkRefCntRecorder*);
SkRefCntSet*);
void unflatten(SkBitmap* bitmap, SkRefCntPlayback* rcp) const {
SkFlattenableReadBuffer buffer(fBitmapData);
@ -168,8 +168,8 @@ private:
class SkFlatPaint : public SkFlatData {
public:
static SkFlatPaint* Flatten(SkChunkAlloc* heap, const SkPaint& paint,
int index, SkRefCntRecorder*,
SkRefCntRecorder* faceRecorder);
int index, SkRefCntSet*,
SkRefCntSet* faceRecorder);
void unflatten(SkPaint* result, SkRefCntPlayback* rcp,
SkTypefacePlayback* facePlayback) const {

View File

@ -65,10 +65,10 @@ SkPicturePlayback::SkPicturePlayback(const SkPictureRecord& record) {
// copy over the refcnt dictionary to our reader
//
fRCPlayback.reset(&record.fRCRecorder);
fRCPlayback.reset(&record.fRCSet);
fRCPlayback.setupBuffer(fReader);
fTFPlayback.reset(&record.fTFRecorder);
fTFPlayback.reset(&record.fTFSet);
fTFPlayback.setupBuffer(fReader);
const SkTDArray<const SkFlatBitmap* >& bitmaps = record.getBitmaps();
@ -297,14 +297,14 @@ static void writeTagSize(SkWStream* stream, uint32_t tag,
stream->write32(size);
}
static void writeFactories(SkWStream* stream, const SkFactoryRecorder& rec) {
static void writeFactories(SkWStream* stream, const SkFactorySet& rec) {
int count = rec.count();
writeTagSize(stream, PICT_FACTORY_TAG, count);
SkAutoSTMalloc<16, SkFlattenable::Factory> storage(count);
SkFlattenable::Factory* array = (SkFlattenable::Factory*)storage.get();
rec.get(array);
rec.copyToArray(array);
for (int i = 0; i < count; i++) {
const char* name = SkFlattenable::FactoryToName(array[i]);
@ -319,14 +319,14 @@ static void writeFactories(SkWStream* stream, const SkFactoryRecorder& rec) {
}
}
static void writeTypefaces(SkWStream* stream, const SkRefCntRecorder& rec) {
static void writeTypefaces(SkWStream* stream, const SkRefCntSet& rec) {
int count = rec.count();
writeTagSize(stream, PICT_TYPEFACE_TAG, count);
SkAutoSTMalloc<16, SkTypeface*> storage(count);
SkTypeface** array = (SkTypeface**)storage.get();
rec.get((SkRefCnt**)array);
rec.copyToArray((SkRefCnt**)array);
for (int i = 0; i < count; i++) {
array[i]->serialize(stream);
@ -337,14 +337,14 @@ void SkPicturePlayback::serialize(SkWStream* stream) const {
writeTagSize(stream, PICT_READER_TAG, fReader.size());
stream->write(fReader.base(), fReader.size());
SkRefCntRecorder typefaceRecorder;
SkFactoryRecorder factRecorder;
SkRefCntSet typefaceSet;
SkFactorySet factSet;
SkFlattenableWriteBuffer buffer(1024);
buffer.setFlags(SkFlattenableWriteBuffer::kCrossProcess_Flag);
buffer.setTypefaceRecorder(&typefaceRecorder);
buffer.setFactoryRecorder(&factRecorder);
buffer.setTypefaceRecorder(&typefaceSet);
buffer.setFactoryRecorder(&factSet);
int i;
@ -385,8 +385,8 @@ void SkPicturePlayback::serialize(SkWStream* stream) const {
// now we can write to the stream again
writeFactories(stream, factRecorder);
writeTypefaces(stream, typefaceRecorder);
writeFactories(stream, factSet);
writeTypefaces(stream, typefaceSet);
writeTagSize(stream, PICT_PICTURE_TAG, fPictureCount);
for (i = 0; i < fPictureCount; i++) {

View File

@ -440,8 +440,8 @@ void SkPictureRecord::reset() {
fRestoreOffsetStack.setCount(1);
fRestoreOffsetStack.top() = 0;
fRCRecorder.reset();
fTFRecorder.reset();
fRCSet.reset();
fTFSet.reset();
}
void SkPictureRecord::addBitmap(const SkBitmap& bitmap) {
@ -544,7 +544,7 @@ void SkPictureRecord::addText(const void* text, size_t byteLength) {
int SkPictureRecord::find(SkTDArray<const SkFlatBitmap* >& bitmaps, const SkBitmap& bitmap) {
SkFlatBitmap* flat = SkFlatBitmap::Flatten(&fHeap, bitmap, fBitmapIndex,
&fRCRecorder);
&fRCSet);
int index = SkTSearch<SkFlatData>((const SkFlatData**) bitmaps.begin(),
bitmaps.count(), (SkFlatData*) flat, sizeof(flat), &SkFlatData::Compare);
if (index >= 0) {
@ -577,7 +577,7 @@ int SkPictureRecord::find(SkTDArray<const SkFlatPaint* >& paints, const SkPaint*
}
SkFlatPaint* flat = SkFlatPaint::Flatten(&fHeap, *paint, fPaintIndex,
&fRCRecorder, &fTFRecorder);
&fRCSet, &fTFSet);
int index = SkTSearch<SkFlatData>((const SkFlatData**) paints.begin(),
paints.count(), (SkFlatData*) flat, sizeof(flat), &SkFlatData::Compare);
if (index >= 0) {

View File

@ -175,8 +175,8 @@ private:
SkTDArray<SkPicture*> fPictureRefs;
SkTDArray<SkShape*> fShapes;
SkRefCntRecorder fRCRecorder;
SkRefCntRecorder fTFRecorder;
SkRefCntSet fRCSet;
SkRefCntSet fTFSet;
uint32_t fRecordFlags;

View File

@ -1,7 +1,7 @@
#include "SkPtrRecorder.h"
#include "SkTSearch.h"
void SkPtrRecorder::reset() {
void SkPtrSet::reset() {
Pair* p = fList.begin();
Pair* stop = fList.end();
while (p < stop) {
@ -11,11 +11,11 @@ void SkPtrRecorder::reset() {
fList.reset();
}
int SkPtrRecorder::Cmp(const Pair& a, const Pair& b) {
int SkPtrSet::Cmp(const Pair& a, const Pair& b) {
return (char*)a.fPtr - (char*)b.fPtr;
}
uint32_t SkPtrRecorder::recordPtr(void* ptr) {
uint32_t SkPtrSet::add(void* ptr) {
if (NULL == ptr) {
return 0;
}
@ -36,7 +36,7 @@ uint32_t SkPtrRecorder::recordPtr(void* ptr) {
}
}
void SkPtrRecorder::getPtrs(void* array[]) const {
void SkPtrSet::copyToArray(void* array[]) const {
int count = fList.count();
if (count > 0) {
SkASSERT(array);