From 5370cd969d8f3957e4306068e6195ac1bca3d6cd Mon Sep 17 00:00:00 2001 From: "djsollen@google.com" Date: Wed, 28 Mar 2012 20:47:01 +0000 Subject: [PATCH] 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 --- include/core/SkMallocPixelRef.h | 10 +-- include/core/SkPixelRef.h | 46 ++---------- include/gpu/SkGrTexturePixelRef.h | 4 ++ include/images/SkFlipPixelRef.h | 25 +++---- include/images/SkImageRef.h | 2 +- include/images/SkImageRef_GlobalPool.h | 8 +-- src/core/SkBitmap.cpp | 19 ++--- src/core/SkGraphics.cpp | 1 - src/core/SkMallocPixelRef.cpp | 4 +- src/core/SkPixelRef.cpp | 71 ++----------------- src/images/SkFlipPixelRef.cpp | 8 +-- src/images/SkImageRef.cpp | 2 +- src/images/SkImageRef_GlobalPool.cpp | 6 +- src/ports/SkGlobalInitialization_chromium.cpp | 5 +- src/ports/SkGlobalInitialization_default.cpp | 12 ++-- src/ports/SkImageRef_ashmem.cpp | 4 +- src/ports/SkImageRef_ashmem.h | 8 +-- 17 files changed, 46 insertions(+), 189 deletions(-) diff --git a/include/core/SkMallocPixelRef.h b/include/core/SkMallocPixelRef.h index 903bad334..cdfec7587 100644 --- a/include/core/SkMallocPixelRef.h +++ b/include/core/SkMallocPixelRef.h @@ -29,15 +29,9 @@ public: 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)); - } + virtual void flatten(SkFlattenableWriteBuffer&); + SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkMallocPixelRef) - SK_DECLARE_PIXEL_REF_REGISTRAR() protected: // overrides from SkPixelRef virtual void* onLockPixels(SkColorTable**); diff --git a/include/core/SkPixelRef.h b/include/core/SkPixelRef.h index d5f6ab2b6..d84c28587 100644 --- a/include/core/SkPixelRef.h +++ b/include/core/SkPixelRef.h @@ -13,35 +13,15 @@ #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 @@ -50,7 +30,7 @@ class SkGpuTexture; 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); @@ -143,11 +123,7 @@ public: 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 /** @@ -165,17 +141,6 @@ public: 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. @@ -206,9 +171,6 @@ protected: 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; @@ -222,7 +184,7 @@ private: // can go from false to true, but never from true to false bool fIsImmutable; - friend class SkGraphics; + typedef SkFlattenable INHERITED; }; #endif diff --git a/include/gpu/SkGrTexturePixelRef.h b/include/gpu/SkGrTexturePixelRef.h index ab92eff49..fd0e750ec 100644 --- a/include/gpu/SkGrTexturePixelRef.h +++ b/include/gpu/SkGrTexturePixelRef.h @@ -48,6 +48,8 @@ public: // override from SkPixelRef virtual SkGpuTexture* getTexture(); + SK_DECLARE_UNFLATTENABLE_OBJECT() + protected: // override from SkPixelRef virtual bool onReadPixels(SkBitmap* dst, const SkIRect* subset); @@ -71,6 +73,8 @@ public: // override from SkPixelRef virtual SkGpuTexture* getTexture(); + SK_DECLARE_UNFLATTENABLE_OBJECT() + protected: // override from SkPixelRef virtual bool onReadPixels(SkBitmap* dst, const SkIRect* subset); diff --git a/include/images/SkFlipPixelRef.h b/include/images/SkFlipPixelRef.h index 455a3dace..c9ba02687 100644 --- a/include/images/SkFlipPixelRef.h +++ b/include/images/SkFlipPixelRef.h @@ -32,6 +32,15 @@ public: 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 { @@ -50,22 +59,6 @@ private: 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; diff --git a/include/images/SkImageRef.h b/include/images/SkImageRef.h index f0f06b60f..8fc4754e9 100644 --- a/include/images/SkImageRef.h +++ b/include/images/SkImageRef.h @@ -61,7 +61,7 @@ public: SkImageDecoderFactory* setDecoderFactory(SkImageDecoderFactory*); // overrides - virtual void flatten(SkFlattenableWriteBuffer&) const; + virtual void flatten(SkFlattenableWriteBuffer&); protected: /** Override if you want to install a custom allocator. diff --git a/include/images/SkImageRef_GlobalPool.h b/include/images/SkImageRef_GlobalPool.h index 909ee7105..03c23673c 100644 --- a/include/images/SkImageRef_GlobalPool.h +++ b/include/images/SkImageRef_GlobalPool.h @@ -18,13 +18,7 @@ public: 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 diff --git a/src/core/SkBitmap.cpp b/src/core/SkBitmap.cpp index e292f39a7..da87e7715 100644 --- a/src/core/SkBitmap.cpp +++ b/src/core/SkBitmap.cpp @@ -1418,16 +1418,11 @@ void SkBitmap::flatten(SkFlattenableWriteBuffer& buffer) const { } 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); @@ -1472,9 +1467,7 @@ void SkBitmap::unflatten(SkFlattenableReadBuffer& buffer) { } 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(buffer.readFlattenable()); SkSafeUnref(this->setPixelRef(pr, offset)); break; } diff --git a/src/core/SkGraphics.cpp b/src/core/SkGraphics.cpp index ff38f8591..db1c9be31 100644 --- a/src/core/SkGraphics.cpp +++ b/src/core/SkGraphics.cpp @@ -54,7 +54,6 @@ void SkGraphics::GetVersion(int32_t* major, int32_t* minor, int32_t* patch) { void SkGraphics::Init() { #if !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS SkFlattenable::InitializeFlattenables(); - SkPixelRef::InitializeFlattenables(); #endif #ifdef BUILD_EMBOSS_TABLE SkEmbossMask_BuildTable(); diff --git a/src/core/SkMallocPixelRef.cpp b/src/core/SkMallocPixelRef.cpp index 3e220756d..1cf64a1db 100644 --- a/src/core/SkMallocPixelRef.cpp +++ b/src/core/SkMallocPixelRef.cpp @@ -34,7 +34,7 @@ void SkMallocPixelRef::onUnlockPixels() { // nothing to do } -void SkMallocPixelRef::flatten(SkFlattenableWriteBuffer& buffer) const { +void SkMallocPixelRef::flatten(SkFlattenableWriteBuffer& buffer) { this->INHERITED::flatten(buffer); buffer.write32(fSize); @@ -59,4 +59,4 @@ SkMallocPixelRef::SkMallocPixelRef(SkFlattenableReadBuffer& buffer) } } -SK_DEFINE_PIXEL_REF_REGISTRAR(SkMallocPixelRef) +SK_DEFINE_FLATTENABLE_REGISTRAR(SkMallocPixelRef) diff --git a/src/core/SkPixelRef.cpp b/src/core/SkPixelRef.cpp index d5e1b8152..2d4daae78 100644 --- a/src/core/SkPixelRef.cpp +++ b/src/core/SkPixelRef.cpp @@ -36,7 +36,8 @@ SkPixelRef::SkPixelRef(SkBaseMutex* mutex) { fIsImmutable = false; } -SkPixelRef::SkPixelRef(SkFlattenableReadBuffer& buffer, SkBaseMutex* mutex) { +SkPixelRef::SkPixelRef(SkFlattenableReadBuffer& buffer, SkBaseMutex* mutex) + : INHERITED(buffer) { if (NULL == mutex) { mutex = &gPixelRefMutex; } @@ -48,7 +49,8 @@ SkPixelRef::SkPixelRef(SkFlattenableReadBuffer& buffer, SkBaseMutex* mutex) { fIsImmutable = buffer.readBool(); } -void SkPixelRef::flatten(SkFlattenableWriteBuffer& buffer) const { +void SkPixelRef::flatten(SkFlattenableWriteBuffer& buffer) { + this->INHERITED::flatten(buffer); 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 void SkPixelRef::globalRef(void* data) { this->ref(); diff --git a/src/images/SkFlipPixelRef.cpp b/src/images/SkFlipPixelRef.cpp index e81c83cef..768a67dc4 100644 --- a/src/images/SkFlipPixelRef.cpp +++ b/src/images/SkFlipPixelRef.cpp @@ -60,7 +60,7 @@ void SkFlipPixelRef::swapPages() { fMutex.release(); } -void SkFlipPixelRef::flatten(SkFlattenableWriteBuffer& buffer) const { +void SkFlipPixelRef::flatten(SkFlattenableWriteBuffer& buffer) { this->INHERITED::flatten(buffer); buffer.write32(fSize); @@ -77,11 +77,7 @@ SkFlipPixelRef::SkFlipPixelRef(SkFlattenableReadBuffer& buffer) 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) /////////////////////////////////////////////////////////////////////////////// diff --git a/src/images/SkImageRef.cpp b/src/images/SkImageRef.cpp index 1d6b270d6..cdd80e76c 100644 --- a/src/images/SkImageRef.cpp +++ b/src/images/SkImageRef.cpp @@ -189,7 +189,7 @@ SkImageRef::SkImageRef(SkFlattenableReadBuffer& buffer) fFactory = NULL; } -void SkImageRef::flatten(SkFlattenableWriteBuffer& buffer) const { +void SkImageRef::flatten(SkFlattenableWriteBuffer& buffer) { this->INHERITED::flatten(buffer); buffer.write8(fConfig); diff --git a/src/images/SkImageRef_GlobalPool.cpp b/src/images/SkImageRef_GlobalPool.cpp index b77402395..bd63f729b 100644 --- a/src/images/SkImageRef_GlobalPool.cpp +++ b/src/images/SkImageRef_GlobalPool.cpp @@ -71,11 +71,7 @@ SkImageRef_GlobalPool::SkImageRef_GlobalPool(SkFlattenableReadBuffer& buffer) 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 diff --git a/src/ports/SkGlobalInitialization_chromium.cpp b/src/ports/SkGlobalInitialization_chromium.cpp index 5a2119e5d..08e83dc05 100644 --- a/src/ports/SkGlobalInitialization_chromium.cpp +++ b/src/ports/SkGlobalInitialization_chromium.cpp @@ -23,13 +23,10 @@ void SkFlattenable::InitializeFlattenables() { 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(); -} diff --git a/src/ports/SkGlobalInitialization_default.cpp b/src/ports/SkGlobalInitialization_default.cpp index a03b92200..8b8a2a71a 100644 --- a/src/ports/SkGlobalInitialization_default.cpp +++ b/src/ports/SkGlobalInitialization_default.cpp @@ -76,19 +76,15 @@ void SkFlattenable::InitializeFlattenables() { 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 diff --git a/src/ports/SkImageRef_ashmem.cpp b/src/ports/SkImageRef_ashmem.cpp index f9c6aff71..a9208e868 100644 --- a/src/ports/SkImageRef_ashmem.cpp +++ b/src/ports/SkImageRef_ashmem.cpp @@ -210,7 +210,7 @@ void SkImageRef_ashmem::onUnlockPixels() { 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) { @@ -241,4 +241,4 @@ SkPixelRef* SkImageRef_ashmem::Create(SkFlattenableReadBuffer& buffer) { return SkNEW_ARGS(SkImageRef_ashmem, (buffer)); } -SK_DEFINE_PIXEL_REF_REGISTRAR(SkImageRef_ashmem) +SK_DEFINE_FLATTENABLE_REGISTRAR(SkImageRef_ashmem) diff --git a/src/ports/SkImageRef_ashmem.h b/src/ports/SkImageRef_ashmem.h index f50ea80c6..38442f6ce 100644 --- a/src/ports/SkImageRef_ashmem.h +++ b/src/ports/SkImageRef_ashmem.h @@ -23,13 +23,9 @@ public: 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,