Consolidate PixelRef flattables with the standard impl
authordjsollen@google.com <djsollen@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Wed, 28 Mar 2012 20:47:01 +0000 (20:47 +0000)
committerdjsollen@google.com <djsollen@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Wed, 28 Mar 2012 20:47:01 +0000 (20:47 +0000)
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

17 files changed:
include/core/SkMallocPixelRef.h
include/core/SkPixelRef.h
include/gpu/SkGrTexturePixelRef.h
include/images/SkFlipPixelRef.h
include/images/SkImageRef.h
include/images/SkImageRef_GlobalPool.h
src/core/SkBitmap.cpp
src/core/SkGraphics.cpp
src/core/SkMallocPixelRef.cpp
src/core/SkPixelRef.cpp
src/images/SkFlipPixelRef.cpp
src/images/SkImageRef.cpp
src/images/SkImageRef_GlobalPool.cpp
src/ports/SkGlobalInitialization_chromium.cpp
src/ports/SkGlobalInitialization_default.cpp
src/ports/SkImageRef_ashmem.cpp
src/ports/SkImageRef_ashmem.h

index 903bad3..cdfec75 100644 (file)
@@ -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));
-    }
-
-    SK_DECLARE_PIXEL_REF_REGISTRAR()
+    virtual void flatten(SkFlattenableWriteBuffer&);
+    SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkMallocPixelRef)
+
 protected:
     // overrides from SkPixelRef
     virtual void* onLockPixels(SkColorTable**);
index d5f6ab2..d84c285 100644 (file)
 #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
index ab92eff..fd0e750 100644 (file)
@@ -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);
index 455a3da..c9ba026 100644 (file)
@@ -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;
     
index f0f06b6..8fc4754 100644 (file)
@@ -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.
index 909ee71..03c2367 100644 (file)
@@ -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
 
index e292f39..da87e77 100644 (file)
@@ -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<SkPixelRef*>(buffer.readFlattenable());
             SkSafeUnref(this->setPixelRef(pr, offset));
             break;
         }
index ff38f85..db1c9be 100644 (file)
@@ -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();
index 3e22075..1cf64a1 100644 (file)
@@ -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)
index d5e1b81..2d4daae 100644 (file)
@@ -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();
index e81c83c..768a67d 100644 (file)
@@ -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)
 
 ///////////////////////////////////////////////////////////////////////////////
 
index 1d6b270..cdd80e7 100644 (file)
@@ -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);
index b774023..bd63f72 100644 (file)
@@ -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
index 5a2119e..08e83dc 100644 (file)
@@ -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();
-}
index a03b922..8b8a2a7 100644 (file)
@@ -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
index f9c6aff..a9208e8 100644 (file)
@@ -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)
index f50ea80..38442f6 100644 (file)
@@ -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,