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
diff --git a/include/core/SkMallocPixelRef.h b/include/core/SkMallocPixelRef.h
index 903bad3..cdfec75 100644
--- a/include/core/SkMallocPixelRef.h
+++ b/include/core/SkMallocPixelRef.h
@@ -29,15 +29,9 @@
     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 d5f6ab2..d84c285 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 @@
 
     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 @@
     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 @@
     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 @@
     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 @@
     // 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 ab92eff..fd0e750 100644
--- a/include/gpu/SkGrTexturePixelRef.h
+++ b/include/gpu/SkGrTexturePixelRef.h
@@ -48,6 +48,8 @@
     // 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 @@
     // 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 455a3da..c9ba026 100644
--- a/include/images/SkFlipPixelRef.h
+++ b/include/images/SkFlipPixelRef.h
@@ -32,6 +32,15 @@
 
     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 @@
     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 f0f06b6..8fc4754 100644
--- a/include/images/SkImageRef.h
+++ b/include/images/SkImageRef.h
@@ -61,7 +61,7 @@
     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 909ee71..03c2367 100644
--- a/include/images/SkImageRef_GlobalPool.h
+++ b/include/images/SkImageRef_GlobalPool.h
@@ -18,13 +18,7 @@
     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