Consolidate PixelRef flattables with the standard impl

The flatten method on these functions can no longer be
const as SkFlattenables declaration is not const and 
would result in the const methods only being called when
the reference to the object was const.
Review URL: https://codereview.appspot.com/5941043

git-svn-id: http://skia.googlecode.com/svn/trunk@3533 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
djsollen@google.com 2012-03-28 20:47:01 +00:00
parent a728e35edc
commit 5370cd969d
17 changed files with 46 additions and 189 deletions

View File

@ -29,15 +29,9 @@ public:
void* getAddr() const { return fStorage; } void* getAddr() const { return fStorage; }
// overrides from SkPixelRef // overrides from SkPixelRef
virtual void flatten(SkFlattenableWriteBuffer&) const; virtual void flatten(SkFlattenableWriteBuffer&);
virtual Factory getFactory() const { SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkMallocPixelRef)
return Create;
}
static SkPixelRef* Create(SkFlattenableReadBuffer& buffer) {
return SkNEW_ARGS(SkMallocPixelRef, (buffer));
}
SK_DECLARE_PIXEL_REF_REGISTRAR()
protected: protected:
// overrides from SkPixelRef // overrides from SkPixelRef
virtual void* onLockPixels(SkColorTable**); virtual void* onLockPixels(SkColorTable**);

View File

@ -13,35 +13,15 @@
#include "SkBitmap.h" #include "SkBitmap.h"
#include "SkRefCnt.h" #include "SkRefCnt.h"
#include "SkString.h" #include "SkString.h"
#include "SkFlattenable.h"
class SkColorTable; class SkColorTable;
struct SkIRect; struct SkIRect;
class SkMutex; class SkMutex;
class SkFlattenableReadBuffer;
class SkFlattenableWriteBuffer;
// this is an opaque class, not interpreted by skia // this is an opaque class, not interpreted by skia
class SkGpuTexture; class SkGpuTexture;
#if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
#define SK_DECLARE_PIXEL_REF_REGISTRAR()
#define SK_DEFINE_PIXEL_REF_REGISTRAR(pixelRef) \
static SkPixelRef::Registrar g##pixelRef##Reg(#pixelRef, \
pixelRef::Create);
#else
#define SK_DECLARE_PIXEL_REF_REGISTRAR() static void Init();
#define SK_DEFINE_PIXEL_REF_REGISTRAR(pixelRef) \
void pixelRef::Init() { \
SkPixelRef::Registrar(#pixelRef, Create); \
}
#endif
/** \class SkPixelRef /** \class SkPixelRef
This class is the smart container for pixel memory, and is used with This class is the smart container for pixel memory, and is used with
@ -50,7 +30,7 @@ class SkGpuTexture;
This class can be shared/accessed between multiple threads. This class can be shared/accessed between multiple threads.
*/ */
class SK_API SkPixelRef : public SkRefCnt { class SK_API SkPixelRef : public SkFlattenable {
public: public:
explicit SkPixelRef(SkBaseMutex* mutex = NULL); explicit SkPixelRef(SkBaseMutex* mutex = NULL);
@ -143,11 +123,7 @@ public:
virtual SkPixelRef* deepCopy(SkBitmap::Config config) { return NULL; } virtual SkPixelRef* deepCopy(SkBitmap::Config config) { return NULL; }
// serialization // serialization
virtual void flatten(SkFlattenableWriteBuffer&);
typedef SkPixelRef* (*Factory)(SkFlattenableReadBuffer&);
virtual Factory getFactory() const { return NULL; }
virtual void flatten(SkFlattenableWriteBuffer&) const;
#ifdef SK_BUILD_FOR_ANDROID #ifdef SK_BUILD_FOR_ANDROID
/** /**
@ -165,17 +141,6 @@ public:
virtual void globalUnref(); virtual void globalUnref();
#endif #endif
static Factory NameToFactory(const char name[]);
static const char* FactoryToName(Factory);
static void Register(const char name[], Factory);
class Registrar {
public:
Registrar(const char name[], Factory factory) {
SkPixelRef::Register(name, factory);
}
};
protected: protected:
/** Called when the lockCount goes from 0 to 1. The caller will have already /** Called when the lockCount goes from 0 to 1. The caller will have already
acquire a mutex for thread safety, so this method need not do that. acquire a mutex for thread safety, so this method need not do that.
@ -206,9 +171,6 @@ protected:
SkPixelRef(SkFlattenableReadBuffer&, SkBaseMutex*); SkPixelRef(SkFlattenableReadBuffer&, SkBaseMutex*);
private: private:
#if !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
static void InitializeFlattenables();
#endif
SkBaseMutex* fMutex; // must remain in scope for the life of this object SkBaseMutex* fMutex; // must remain in scope for the life of this object
void* fPixels; void* fPixels;
@ -222,7 +184,7 @@ private:
// can go from false to true, but never from true to false // can go from false to true, but never from true to false
bool fIsImmutable; bool fIsImmutable;
friend class SkGraphics; typedef SkFlattenable INHERITED;
}; };
#endif #endif

View File

@ -48,6 +48,8 @@ public:
// override from SkPixelRef // override from SkPixelRef
virtual SkGpuTexture* getTexture(); virtual SkGpuTexture* getTexture();
SK_DECLARE_UNFLATTENABLE_OBJECT()
protected: protected:
// override from SkPixelRef // override from SkPixelRef
virtual bool onReadPixels(SkBitmap* dst, const SkIRect* subset); virtual bool onReadPixels(SkBitmap* dst, const SkIRect* subset);
@ -71,6 +73,8 @@ public:
// override from SkPixelRef // override from SkPixelRef
virtual SkGpuTexture* getTexture(); virtual SkGpuTexture* getTexture();
SK_DECLARE_UNFLATTENABLE_OBJECT()
protected: protected:
// override from SkPixelRef // override from SkPixelRef
virtual bool onReadPixels(SkBitmap* dst, const SkIRect* subset); virtual bool onReadPixels(SkBitmap* dst, const SkIRect* subset);

View File

@ -32,6 +32,15 @@ public:
const SkRegion& beginUpdate(SkBitmap* device); const SkRegion& beginUpdate(SkBitmap* device);
void endUpdate(); void endUpdate();
virtual void flatten(SkFlattenableWriteBuffer&);
SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkFlipPixelRef)
protected:
virtual void* onLockPixels(SkColorTable**);
virtual void onUnlockPixels();
SkFlipPixelRef(SkFlattenableReadBuffer&);
private: private:
void getFrontBack(const void** front, void** back) const { void getFrontBack(const void** front, void** back) const {
@ -50,22 +59,6 @@ private:
static void CopyBitsFromAddr(const SkBitmap& dst, const SkRegion& clip, static void CopyBitsFromAddr(const SkBitmap& dst, const SkRegion& clip,
const void* srcAddr); const void* srcAddr);
// serialization
public:
virtual Factory getFactory() const { return Create; }
virtual void flatten(SkFlattenableWriteBuffer&) const;
static SkPixelRef* Create(SkFlattenableReadBuffer& buffer);
SK_DECLARE_PIXEL_REF_REGISTRAR()
protected:
virtual void* onLockPixels(SkColorTable**);
virtual void onUnlockPixels();
SkFlipPixelRef(SkFlattenableReadBuffer&);
private:
SkMutex fMutex; SkMutex fMutex;
SkPageFlipper fFlipper; SkPageFlipper fFlipper;

View File

@ -61,7 +61,7 @@ public:
SkImageDecoderFactory* setDecoderFactory(SkImageDecoderFactory*); SkImageDecoderFactory* setDecoderFactory(SkImageDecoderFactory*);
// overrides // overrides
virtual void flatten(SkFlattenableWriteBuffer&) const; virtual void flatten(SkFlattenableWriteBuffer&);
protected: protected:
/** Override if you want to install a custom allocator. /** Override if you want to install a custom allocator.

View File

@ -18,13 +18,7 @@ public:
SkImageRef_GlobalPool(SkStream*, SkBitmap::Config, int sampleSize = 1); SkImageRef_GlobalPool(SkStream*, SkBitmap::Config, int sampleSize = 1);
virtual ~SkImageRef_GlobalPool(); virtual ~SkImageRef_GlobalPool();
// overrides SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkImageRef_GlobalPool)
virtual Factory getFactory() const {
return Create;
}
static SkPixelRef* Create(SkFlattenableReadBuffer&);
SK_DECLARE_PIXEL_REF_REGISTRAR()
// API to control the global pool // API to control the global pool

View File

@ -1418,16 +1418,11 @@ void SkBitmap::flatten(SkFlattenableWriteBuffer& buffer) const {
} }
if (fPixelRef) { if (fPixelRef) {
SkPixelRef::Factory fact = fPixelRef->getFactory(); if (fPixelRef->getFactory()) {
if (fact) { buffer.write8(SERIALIZE_PIXELTYPE_REF_DATA);
const char* name = SkPixelRef::FactoryToName(fact); buffer.write32(fPixelRefOffset);
if (name && *name) { buffer.writeFlattenable(fPixelRef);
buffer.write8(SERIALIZE_PIXELTYPE_REF_DATA); return;
buffer.write32(fPixelRefOffset);
buffer.writeString(name);
fPixelRef->flatten(buffer);
return;
}
} }
// if we get here, we can't record the pixels // if we get here, we can't record the pixels
buffer.write8(SERIALIZE_PIXELTYPE_NONE); buffer.write8(SERIALIZE_PIXELTYPE_NONE);
@ -1472,9 +1467,7 @@ void SkBitmap::unflatten(SkFlattenableReadBuffer& buffer) {
} }
case SERIALIZE_PIXELTYPE_REF_DATA: { case SERIALIZE_PIXELTYPE_REF_DATA: {
size_t offset = buffer.readU32(); size_t offset = buffer.readU32();
const char* factoryName = buffer.readString(); SkPixelRef* pr = static_cast<SkPixelRef*>(buffer.readFlattenable());
SkPixelRef::Factory fact = SkPixelRef::NameToFactory(factoryName);
SkPixelRef* pr = fact(buffer);
SkSafeUnref(this->setPixelRef(pr, offset)); SkSafeUnref(this->setPixelRef(pr, offset));
break; break;
} }

View File

@ -54,7 +54,6 @@ void SkGraphics::GetVersion(int32_t* major, int32_t* minor, int32_t* patch) {
void SkGraphics::Init() { void SkGraphics::Init() {
#if !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS #if !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
SkFlattenable::InitializeFlattenables(); SkFlattenable::InitializeFlattenables();
SkPixelRef::InitializeFlattenables();
#endif #endif
#ifdef BUILD_EMBOSS_TABLE #ifdef BUILD_EMBOSS_TABLE
SkEmbossMask_BuildTable(); SkEmbossMask_BuildTable();

View File

@ -34,7 +34,7 @@ void SkMallocPixelRef::onUnlockPixels() {
// nothing to do // nothing to do
} }
void SkMallocPixelRef::flatten(SkFlattenableWriteBuffer& buffer) const { void SkMallocPixelRef::flatten(SkFlattenableWriteBuffer& buffer) {
this->INHERITED::flatten(buffer); this->INHERITED::flatten(buffer);
buffer.write32(fSize); buffer.write32(fSize);
@ -59,4 +59,4 @@ SkMallocPixelRef::SkMallocPixelRef(SkFlattenableReadBuffer& buffer)
} }
} }
SK_DEFINE_PIXEL_REF_REGISTRAR(SkMallocPixelRef) SK_DEFINE_FLATTENABLE_REGISTRAR(SkMallocPixelRef)

View File

@ -36,7 +36,8 @@ SkPixelRef::SkPixelRef(SkBaseMutex* mutex) {
fIsImmutable = false; fIsImmutable = false;
} }
SkPixelRef::SkPixelRef(SkFlattenableReadBuffer& buffer, SkBaseMutex* mutex) { SkPixelRef::SkPixelRef(SkFlattenableReadBuffer& buffer, SkBaseMutex* mutex)
: INHERITED(buffer) {
if (NULL == mutex) { if (NULL == mutex) {
mutex = &gPixelRefMutex; mutex = &gPixelRefMutex;
} }
@ -48,7 +49,8 @@ SkPixelRef::SkPixelRef(SkFlattenableReadBuffer& buffer, SkBaseMutex* mutex) {
fIsImmutable = buffer.readBool(); fIsImmutable = buffer.readBool();
} }
void SkPixelRef::flatten(SkFlattenableWriteBuffer& buffer) const { void SkPixelRef::flatten(SkFlattenableWriteBuffer& buffer) {
this->INHERITED::flatten(buffer);
buffer.writeBool(fIsImmutable); buffer.writeBool(fIsImmutable);
} }
@ -110,71 +112,6 @@ bool SkPixelRef::onReadPixels(SkBitmap* dst, const SkIRect* subset) {
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
#define MAX_PAIR_COUNT 16
struct Pair {
const char* fName;
SkPixelRef::Factory fFactory;
};
static int gCount;
static Pair gPairs[MAX_PAIR_COUNT];
void SkPixelRef::Register(const char name[], Factory factory) {
SkASSERT(name);
SkASSERT(factory);
static bool gOnce;
if (!gOnce) {
gCount = 0;
gOnce = true;
}
SkASSERT(gCount < MAX_PAIR_COUNT);
gPairs[gCount].fName = name;
gPairs[gCount].fFactory = factory;
gCount += 1;
}
#if !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS && defined(SK_DEBUG)
static void report_no_entries(const char* functionName) {
if (!gCount) {
SkDebugf("%s has no registered name/factory pairs."
" Call SkGraphics::Init() at process initialization time.",
functionName);
}
}
#endif
SkPixelRef::Factory SkPixelRef::NameToFactory(const char name[]) {
#if !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS && defined(SK_DEBUG)
report_no_entries(__FUNCTION__);
#endif
const Pair* pairs = gPairs;
for (int i = gCount - 1; i >= 0; --i) {
if (strcmp(pairs[i].fName, name) == 0) {
return pairs[i].fFactory;
}
}
return NULL;
}
const char* SkPixelRef::FactoryToName(Factory fact) {
#if !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS && defined(SK_DEBUG)
report_no_entries(__FUNCTION__);
#endif
const Pair* pairs = gPairs;
for (int i = gCount - 1; i >= 0; --i) {
if (pairs[i].fFactory == fact) {
return pairs[i].fName;
}
}
return NULL;
}
///////////////////////////////////////////////////////////////////////////////
#ifdef SK_BUILD_FOR_ANDROID #ifdef SK_BUILD_FOR_ANDROID
void SkPixelRef::globalRef(void* data) { void SkPixelRef::globalRef(void* data) {
this->ref(); this->ref();

View File

@ -60,7 +60,7 @@ void SkFlipPixelRef::swapPages() {
fMutex.release(); fMutex.release();
} }
void SkFlipPixelRef::flatten(SkFlattenableWriteBuffer& buffer) const { void SkFlipPixelRef::flatten(SkFlattenableWriteBuffer& buffer) {
this->INHERITED::flatten(buffer); this->INHERITED::flatten(buffer);
buffer.write32(fSize); buffer.write32(fSize);
@ -77,11 +77,7 @@ SkFlipPixelRef::SkFlipPixelRef(SkFlattenableReadBuffer& buffer)
buffer.read(fPage0, fSize); buffer.read(fPage0, fSize);
} }
SkPixelRef* SkFlipPixelRef::Create(SkFlattenableReadBuffer& buffer) { SK_DEFINE_FLATTENABLE_REGISTRAR(SkFlipPixelRef)
return SkNEW_ARGS(SkFlipPixelRef, (buffer));
}
SK_DEFINE_PIXEL_REF_REGISTRAR(SkFlipPixelRef)
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////

View File

@ -189,7 +189,7 @@ SkImageRef::SkImageRef(SkFlattenableReadBuffer& buffer)
fFactory = NULL; fFactory = NULL;
} }
void SkImageRef::flatten(SkFlattenableWriteBuffer& buffer) const { void SkImageRef::flatten(SkFlattenableWriteBuffer& buffer) {
this->INHERITED::flatten(buffer); this->INHERITED::flatten(buffer);
buffer.write8(fConfig); buffer.write8(fConfig);

View File

@ -71,11 +71,7 @@ SkImageRef_GlobalPool::SkImageRef_GlobalPool(SkFlattenableReadBuffer& buffer)
this->mutex()->release(); this->mutex()->release();
} }
SkPixelRef* SkImageRef_GlobalPool::Create(SkFlattenableReadBuffer& buffer) { SK_DEFINE_FLATTENABLE_REGISTRAR(SkImageRef_GlobalPool)
return SkNEW_ARGS(SkImageRef_GlobalPool, (buffer));
}
SK_DEFINE_PIXEL_REF_REGISTRAR(SkImageRef_GlobalPool)
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// global imagerefpool wrappers // global imagerefpool wrappers

View File

@ -23,13 +23,10 @@ void SkFlattenable::InitializeFlattenables() {
SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkCornerPathEffect) SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkCornerPathEffect)
SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkDashPathEffect) SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkDashPathEffect)
SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkLayerDrawLooper) SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkLayerDrawLooper)
SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkMallocPixelRef)
SkBlurMaskFilter::InitializeFlattenables(); SkBlurMaskFilter::InitializeFlattenables();
SkColorFilter::InitializeFlattenables(); SkColorFilter::InitializeFlattenables();
SkGradientShader::InitializeFlattenables(); SkGradientShader::InitializeFlattenables();
SkXfermode::InitializeFlattenables(); SkXfermode::InitializeFlattenables();
} }
void SkPixelRef::InitializeFlattenables() {
SkMallocPixelRef::Init();
}

View File

@ -76,19 +76,15 @@ void SkFlattenable::InitializeFlattenables() {
SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkColorFilterImageFilter) SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkColorFilterImageFilter)
SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkDownSampleImageFilter) SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkDownSampleImageFilter)
SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkFlipPixelRef)
SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkImageRef_GlobalPool)
SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkMallocPixelRef)
SkBlurMaskFilter::InitializeFlattenables(); SkBlurMaskFilter::InitializeFlattenables();
SkColorFilter::InitializeFlattenables(); SkColorFilter::InitializeFlattenables();
SkGradientShader::InitializeFlattenables(); SkGradientShader::InitializeFlattenables();
SkTableColorFilter::InitializeFlattenables(); SkTableColorFilter::InitializeFlattenables();
SkXfermode::InitializeFlattenables(); SkXfermode::InitializeFlattenables();
}
void SkPixelRef::InitializeFlattenables() {
SkFlipPixelRef::Init();
SkImageRef_GlobalPool::Init();
SkMallocPixelRef::Init();
} }
#endif #endif

View File

@ -210,7 +210,7 @@ void SkImageRef_ashmem::onUnlockPixels() {
fBitmap.setPixels(NULL, NULL); fBitmap.setPixels(NULL, NULL);
} }
void SkImageRef_ashmem::flatten(SkFlattenableWriteBuffer& buffer) const { void SkImageRef_ashmem::flatten(SkFlattenableWriteBuffer& buffer) {
this->INHERITED::flatten(buffer); this->INHERITED::flatten(buffer);
const char* uri = getURI(); const char* uri = getURI();
if (uri) { if (uri) {
@ -241,4 +241,4 @@ SkPixelRef* SkImageRef_ashmem::Create(SkFlattenableReadBuffer& buffer) {
return SkNEW_ARGS(SkImageRef_ashmem, (buffer)); return SkNEW_ARGS(SkImageRef_ashmem, (buffer));
} }
SK_DEFINE_PIXEL_REF_REGISTRAR(SkImageRef_ashmem) SK_DEFINE_FLATTENABLE_REGISTRAR(SkImageRef_ashmem)

View File

@ -23,13 +23,9 @@ public:
virtual ~SkImageRef_ashmem(); virtual ~SkImageRef_ashmem();
// overrides // overrides
virtual void flatten(SkFlattenableWriteBuffer&) const; virtual void flatten(SkFlattenableWriteBuffer&);
virtual Factory getFactory() const { SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkImageRef_ashmem)
return Create;
}
static SkPixelRef* Create(SkFlattenableReadBuffer&);
SK_DECLARE_PIXEL_REF_REGISTRAR()
protected: protected:
virtual bool onDecode(SkImageDecoder* codec, SkStream* stream, virtual bool onDecode(SkImageDecoder* codec, SkStream* stream,
SkBitmap* bitmap, SkBitmap::Config config, SkBitmap* bitmap, SkBitmap::Config config,