void* getAddr() const { return fStorage; }
// overrides from SkPixelRef
- virtual void flatten(SkFlattenableWriteBuffer&) const;
- virtual Factory getFactory() const {
- return Create;
- }
- static SkPixelRef* Create(SkFlattenableReadBuffer& buffer) {
- return SkNEW_ARGS(SkMallocPixelRef, (buffer));
- }
-
- SK_DECLARE_PIXEL_REF_REGISTRAR()
+ virtual void flatten(SkFlattenableWriteBuffer&);
+ SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkMallocPixelRef)
+
protected:
// overrides from SkPixelRef
virtual void* onLockPixels(SkColorTable**);
#include "SkBitmap.h"
#include "SkRefCnt.h"
#include "SkString.h"
+#include "SkFlattenable.h"
class SkColorTable;
struct SkIRect;
class SkMutex;
-class SkFlattenableReadBuffer;
-class SkFlattenableWriteBuffer;
// this is an opaque class, not interpreted by skia
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
This class is the smart container for pixel memory, and is used with
This class can be shared/accessed between multiple threads.
*/
-class SK_API SkPixelRef : public SkRefCnt {
+class SK_API SkPixelRef : public SkFlattenable {
public:
explicit SkPixelRef(SkBaseMutex* mutex = NULL);
virtual SkPixelRef* deepCopy(SkBitmap::Config config) { return NULL; }
// serialization
-
- typedef SkPixelRef* (*Factory)(SkFlattenableReadBuffer&);
-
- virtual Factory getFactory() const { return NULL; }
- virtual void flatten(SkFlattenableWriteBuffer&) const;
+ virtual void flatten(SkFlattenableWriteBuffer&);
#ifdef SK_BUILD_FOR_ANDROID
/**
virtual void globalUnref();
#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:
/** 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.
SkPixelRef(SkFlattenableReadBuffer&, SkBaseMutex*);
private:
-#if !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
- static void InitializeFlattenables();
-#endif
SkBaseMutex* fMutex; // must remain in scope for the life of this object
void* fPixels;
// can go from false to true, but never from true to false
bool fIsImmutable;
- friend class SkGraphics;
+ typedef SkFlattenable INHERITED;
};
#endif
// override from SkPixelRef
virtual SkGpuTexture* getTexture();
+ SK_DECLARE_UNFLATTENABLE_OBJECT()
+
protected:
// override from SkPixelRef
virtual bool onReadPixels(SkBitmap* dst, const SkIRect* subset);
// override from SkPixelRef
virtual SkGpuTexture* getTexture();
+ SK_DECLARE_UNFLATTENABLE_OBJECT()
+
protected:
// override from SkPixelRef
virtual bool onReadPixels(SkBitmap* dst, const SkIRect* subset);
const SkRegion& beginUpdate(SkBitmap* device);
void endUpdate();
+
+ virtual void flatten(SkFlattenableWriteBuffer&);
+ SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkFlipPixelRef)
+
+protected:
+ virtual void* onLockPixels(SkColorTable**);
+ virtual void onUnlockPixels();
+
+ SkFlipPixelRef(SkFlattenableReadBuffer&);
private:
void getFrontBack(const void** front, void** back) const {
static void CopyBitsFromAddr(const SkBitmap& dst, const SkRegion& clip,
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;
SkPageFlipper fFlipper;
SkImageDecoderFactory* setDecoderFactory(SkImageDecoderFactory*);
// overrides
- virtual void flatten(SkFlattenableWriteBuffer&) const;
+ virtual void flatten(SkFlattenableWriteBuffer&);
protected:
/** Override if you want to install a custom allocator.
SkImageRef_GlobalPool(SkStream*, SkBitmap::Config, int sampleSize = 1);
virtual ~SkImageRef_GlobalPool();
- // overrides
- virtual Factory getFactory() const {
- return Create;
- }
- static SkPixelRef* Create(SkFlattenableReadBuffer&);
-
- SK_DECLARE_PIXEL_REF_REGISTRAR()
+ SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkImageRef_GlobalPool)
// API to control the global pool
}
if (fPixelRef) {
- SkPixelRef::Factory fact = fPixelRef->getFactory();
- if (fact) {
- const char* name = SkPixelRef::FactoryToName(fact);
- if (name && *name) {
- buffer.write8(SERIALIZE_PIXELTYPE_REF_DATA);
- buffer.write32(fPixelRefOffset);
- buffer.writeString(name);
- fPixelRef->flatten(buffer);
- return;
- }
+ if (fPixelRef->getFactory()) {
+ buffer.write8(SERIALIZE_PIXELTYPE_REF_DATA);
+ buffer.write32(fPixelRefOffset);
+ buffer.writeFlattenable(fPixelRef);
+ return;
}
// if we get here, we can't record the pixels
buffer.write8(SERIALIZE_PIXELTYPE_NONE);
}
case SERIALIZE_PIXELTYPE_REF_DATA: {
size_t offset = buffer.readU32();
- const char* factoryName = buffer.readString();
- SkPixelRef::Factory fact = SkPixelRef::NameToFactory(factoryName);
- SkPixelRef* pr = fact(buffer);
+ SkPixelRef* pr = static_cast<SkPixelRef*>(buffer.readFlattenable());
SkSafeUnref(this->setPixelRef(pr, offset));
break;
}
void SkGraphics::Init() {
#if !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
SkFlattenable::InitializeFlattenables();
- SkPixelRef::InitializeFlattenables();
#endif
#ifdef BUILD_EMBOSS_TABLE
SkEmbossMask_BuildTable();
// nothing to do
}
-void SkMallocPixelRef::flatten(SkFlattenableWriteBuffer& buffer) const {
+void SkMallocPixelRef::flatten(SkFlattenableWriteBuffer& buffer) {
this->INHERITED::flatten(buffer);
buffer.write32(fSize);
}
}
-SK_DEFINE_PIXEL_REF_REGISTRAR(SkMallocPixelRef)
+SK_DEFINE_FLATTENABLE_REGISTRAR(SkMallocPixelRef)
fIsImmutable = false;
}
-SkPixelRef::SkPixelRef(SkFlattenableReadBuffer& buffer, SkBaseMutex* mutex) {
+SkPixelRef::SkPixelRef(SkFlattenableReadBuffer& buffer, SkBaseMutex* mutex)
+ : INHERITED(buffer) {
if (NULL == mutex) {
mutex = &gPixelRefMutex;
}
fIsImmutable = buffer.readBool();
}
-void SkPixelRef::flatten(SkFlattenableWriteBuffer& buffer) const {
+void SkPixelRef::flatten(SkFlattenableWriteBuffer& buffer) {
+ this->INHERITED::flatten(buffer);
buffer.writeBool(fIsImmutable);
}
///////////////////////////////////////////////////////////////////////////////
-#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
void SkPixelRef::globalRef(void* data) {
this->ref();
fMutex.release();
}
-void SkFlipPixelRef::flatten(SkFlattenableWriteBuffer& buffer) const {
+void SkFlipPixelRef::flatten(SkFlattenableWriteBuffer& buffer) {
this->INHERITED::flatten(buffer);
buffer.write32(fSize);
buffer.read(fPage0, fSize);
}
-SkPixelRef* SkFlipPixelRef::Create(SkFlattenableReadBuffer& buffer) {
- return SkNEW_ARGS(SkFlipPixelRef, (buffer));
-}
-
-SK_DEFINE_PIXEL_REF_REGISTRAR(SkFlipPixelRef)
+SK_DEFINE_FLATTENABLE_REGISTRAR(SkFlipPixelRef)
///////////////////////////////////////////////////////////////////////////////
fFactory = NULL;
}
-void SkImageRef::flatten(SkFlattenableWriteBuffer& buffer) const {
+void SkImageRef::flatten(SkFlattenableWriteBuffer& buffer) {
this->INHERITED::flatten(buffer);
buffer.write8(fConfig);
this->mutex()->release();
}
-SkPixelRef* SkImageRef_GlobalPool::Create(SkFlattenableReadBuffer& buffer) {
- return SkNEW_ARGS(SkImageRef_GlobalPool, (buffer));
-}
-
-SK_DEFINE_PIXEL_REF_REGISTRAR(SkImageRef_GlobalPool)
+SK_DEFINE_FLATTENABLE_REGISTRAR(SkImageRef_GlobalPool)
///////////////////////////////////////////////////////////////////////////////
// global imagerefpool wrappers
SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkCornerPathEffect)
SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkDashPathEffect)
SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkLayerDrawLooper)
+ SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkMallocPixelRef)
SkBlurMaskFilter::InitializeFlattenables();
SkColorFilter::InitializeFlattenables();
SkGradientShader::InitializeFlattenables();
SkXfermode::InitializeFlattenables();
}
-
-void SkPixelRef::InitializeFlattenables() {
- SkMallocPixelRef::Init();
-}
SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkColorFilterImageFilter)
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();
SkColorFilter::InitializeFlattenables();
SkGradientShader::InitializeFlattenables();
SkTableColorFilter::InitializeFlattenables();
SkXfermode::InitializeFlattenables();
-
-
-}
-
-void SkPixelRef::InitializeFlattenables() {
- SkFlipPixelRef::Init();
- SkImageRef_GlobalPool::Init();
- SkMallocPixelRef::Init();
}
#endif
fBitmap.setPixels(NULL, NULL);
}
-void SkImageRef_ashmem::flatten(SkFlattenableWriteBuffer& buffer) const {
+void SkImageRef_ashmem::flatten(SkFlattenableWriteBuffer& buffer) {
this->INHERITED::flatten(buffer);
const char* uri = getURI();
if (uri) {
return SkNEW_ARGS(SkImageRef_ashmem, (buffer));
}
-SK_DEFINE_PIXEL_REF_REGISTRAR(SkImageRef_ashmem)
+SK_DEFINE_FLATTENABLE_REGISTRAR(SkImageRef_ashmem)
virtual ~SkImageRef_ashmem();
// overrides
- virtual void flatten(SkFlattenableWriteBuffer&) const;
- virtual Factory getFactory() const {
- return Create;
- }
- static SkPixelRef* Create(SkFlattenableReadBuffer&);
+ virtual void flatten(SkFlattenableWriteBuffer&);
+ SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkImageRef_ashmem)
- SK_DECLARE_PIXEL_REF_REGISTRAR()
protected:
virtual bool onDecode(SkImageDecoder* codec, SkStream* stream,
SkBitmap* bitmap, SkBitmap::Config config,