Rewrite SkTRegistry to take any trivially-copyable type.
authormtklein@google.com <mtklein@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Wed, 4 Sep 2013 17:20:18 +0000 (17:20 +0000)
committermtklein@google.com <mtklein@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Wed, 4 Sep 2013 17:20:18 +0000 (17:20 +0000)
Obviously these are all currently function pointers of type T(*)(P) for various
T and P.  In bench refactoring, I'm trying to register a function pointer of
type T(*)(), which can't be done as is (passing P=void doesn't work).  This
also lets us register things like primitives, which is conceivable useful.

BUG=
R=reed@google.com, scroggo@google.com

Review URL: https://codereview.chromium.org/23453031

git-svn-id: http://skia.googlecode.com/svn/trunk@11082 2bbb7eff-a529-9590-31e7-b0007b416f81

21 files changed:
bench/SkBenchmark.h
experimental/SkiaExamples/SkExample.h
gm/gm.cpp
gm/gm.h
include/core/SkImageDecoder.h
include/core/SkImageEncoder.h
include/core/SkTRegistry.h
src/images/SkImageDecoder_FactoryDefault.cpp
src/images/SkImageDecoder_FactoryRegistrar.cpp
src/images/SkImageDecoder_libbmp.cpp
src/images/SkImageDecoder_libgif.cpp
src/images/SkImageDecoder_libico.cpp
src/images/SkImageDecoder_libjpeg.cpp
src/images/SkImageDecoder_libpng.cpp
src/images/SkImageDecoder_libwebp.cpp
src/images/SkImageDecoder_wbmp.cpp
src/images/SkImageEncoder_Factory.cpp
src/images/SkMovie_gif.cpp
src/ports/SkImageDecoder_CG.cpp
src/ports/SkImageDecoder_WIC.cpp
tests/Test.h

index 404d4c3..7ebe104 100644 (file)
@@ -154,6 +154,6 @@ private:
     typedef SkRefCnt INHERITED;
 };
 
-typedef SkTRegistry<SkBenchmark*, void*> BenchRegistry;
+typedef SkTRegistry<SkBenchmark*(*)(void*)> BenchRegistry;
 
 #endif
index 51fe21c..bea3aec 100644 (file)
@@ -28,7 +28,7 @@ public:
 
     SkString getName() { return fName; };
     // Use this public registry to tell the world about your sample.
-    typedef SkTRegistry<SkExample*, SkExampleWindow*> Registry;
+    typedef SkTRegistry<SkExample*(*)(SkExampleWindow*)> Registry;
 
 protected:
     SkExampleWindow* fWindow;
index c1e75e9..f794fbf 100644 (file)
--- a/gm/gm.cpp
+++ b/gm/gm.cpp
@@ -63,4 +63,4 @@ void GM::drawSizeBounds(SkCanvas* canvas, SkColor color) {
 }
 
 // need to explicitly declare this, or we get some weird infinite loop llist
-template GMRegistry* SkTRegistry<GM*, void*>::gHead;
+template GMRegistry* SkTRegistry<GM*(*)(void*)>::gHead;
diff --git a/gm/gm.h b/gm/gm.h
index 022536d..6cd97b5 100644 (file)
--- a/gm/gm.h
+++ b/gm/gm.h
@@ -111,7 +111,7 @@ namespace skiagm {
         bool     fHaveCalledOnceBeforeDraw;
     };
 
-    typedef SkTRegistry<GM*, void*> GMRegistry;
+    typedef SkTRegistry<GM*(*)(void*)> GMRegistry;
 }
 
 #endif
index 7bdaaa8..a8cbe63 100644 (file)
@@ -15,6 +15,7 @@
 #include "SkImage.h"
 #include "SkRect.h"
 #include "SkRefCnt.h"
+#include "SkTRegistry.h"
 #include "SkTypes.h"
 
 class SkStream;
@@ -557,4 +558,10 @@ DECLARE_DECODER_CREATOR(PNGImageDecoder);
 DECLARE_DECODER_CREATOR(WBMPImageDecoder);
 DECLARE_DECODER_CREATOR(WEBPImageDecoder);
 
+
+// Typedefs to make registering decoder and formatter callbacks easier.
+// These have to be defined outside SkImageDecoder. :(
+typedef SkTRegistry<SkImageDecoder*(*)(SkStream*)>        SkImageDecoder_DecodeReg;
+typedef SkTRegistry<SkImageDecoder::Format(*)(SkStream*)> SkImageDecoder_FormatReg;
+
 #endif
index b990aff..5622eee 100644 (file)
@@ -9,6 +9,7 @@
 #define SkImageEncoder_DEFINED
 
 #include "SkTypes.h"
+#include "SkTRegistry.h"
 
 class SkBitmap;
 class SkData;
@@ -97,4 +98,7 @@ DECLARE_ENCODER_CREATOR(JPEGImageEncoder);
 DECLARE_ENCODER_CREATOR(PNGImageEncoder);
 DECLARE_ENCODER_CREATOR(WEBPImageEncoder);
 
+// Typedef to make registering encoder callback easier
+// This has to be defined outside SkImageEncoder. :(
+typedef SkTRegistry<SkImageEncoder*(*)(SkImageEncoder::Type)> SkImageEncoder_EncodeReg;
 #endif
index 34fcffd..0994c99 100644 (file)
     and provides a function-pointer. This can be used to auto-register a set of
     services, e.g. a set of image codecs.
  */
-template <typename T, typename P> class SkTRegistry : SkNoncopyable {
+template <typename T> class SkTRegistry : SkNoncopyable {
 public:
-    typedef T (*Factory)(P);
+    typedef T Factory;
 
-    SkTRegistry(Factory fact) {
+    explicit SkTRegistry(T fact) : fFact(fact) {
 #ifdef SK_BUILD_FOR_ANDROID
         // work-around for double-initialization bug
         {
@@ -33,15 +33,14 @@ public:
             }
         }
 #endif
-        fFact = fact;
         fChain = gHead;
-        gHead = this;
+        gHead  = this;
     }
 
     static const SkTRegistry* Head() { return gHead; }
 
     const SkTRegistry* next() const { return fChain; }
-    Factory factory() const { return fFact; }
+    const Factory& factory() const { return fFact; }
 
 private:
     Factory      fFact;
@@ -51,6 +50,6 @@ private:
 };
 
 // The caller still needs to declare an instance of this somewhere
-template <typename T, typename P> SkTRegistry<T, P>* SkTRegistry<T, P>::gHead;
+template <typename T> SkTRegistry<T>* SkTRegistry<T>::gHead;
 
 #endif
index 565519a..247f571 100644 (file)
@@ -9,7 +9,6 @@
 #include "SkImageDecoder.h"
 #include "SkMovie.h"
 #include "SkStream.h"
-#include "SkTRegistry.h"
 
 extern SkImageDecoder* image_decoder_from_stream(SkStream*);
 
@@ -19,7 +18,7 @@ SkImageDecoder* SkImageDecoder::Factory(SkStream* stream) {
 
 /////////////////////////////////////////////////////////////////////////
 
-typedef SkTRegistry<SkMovie*, SkStream*> MovieReg;
+typedef SkTRegistry<SkMovie*(*)(SkStream*)> MovieReg;
 
 SkMovie* SkMovie::DecodeStream(SkStream* stream) {
     const MovieReg* curr = MovieReg::Head();
index f1eca3d..9ed17e0 100644 (file)
 // for checking all the the registered SkImageDecoders for one that matches an
 // input SkStream.
 
-typedef SkTRegistry<SkImageDecoder*, SkStream*> DecodeReg;
-
-// N.B. You can't use "DecodeReg::gHead here" due to complex C++
-// corner cases.
-template DecodeReg* SkTRegistry<SkImageDecoder*, SkStream*>::gHead;
+template SkImageDecoder_DecodeReg* SkImageDecoder_DecodeReg::gHead;
 
 SkImageDecoder* image_decoder_from_stream(SkStream*);
 
 SkImageDecoder* image_decoder_from_stream(SkStream* stream) {
     SkImageDecoder* codec = NULL;
-    const DecodeReg* curr = DecodeReg::Head();
+    const SkImageDecoder_DecodeReg* curr = SkImageDecoder_DecodeReg::Head();
     while (curr) {
         codec = curr->factory()(stream);
         // we rewind here, because we promise later when we call "decode", that
@@ -47,12 +43,10 @@ SkImageDecoder* image_decoder_from_stream(SkStream* stream) {
     return NULL;
 }
 
-typedef SkTRegistry<SkImageDecoder::Format, SkStream*> FormatReg;
-
-template FormatReg* SkTRegistry<SkImageDecoder::Format, SkStream*>::gHead;
+template SkImageDecoder_FormatReg* SkImageDecoder_FormatReg::gHead;
 
 SkImageDecoder::Format SkImageDecoder::GetStreamFormat(SkStream* stream) {
-    const FormatReg* curr = FormatReg::Head();
+    const SkImageDecoder_FormatReg* curr = SkImageDecoder_FormatReg::Head();
     while (curr != NULL) {
         Format format = curr->factory()(stream);
         if (!stream->rewind()) {
index 73e7a20..2edd57c 100644 (file)
@@ -14,7 +14,6 @@
 #include "SkStream.h"
 #include "SkStreamHelpers.h"
 #include "SkTDArray.h"
-#include "SkTRegistry.h"
 
 class SkBMPImageDecoder : public SkImageDecoder {
 public:
@@ -52,7 +51,7 @@ static SkImageDecoder* sk_libbmp_dfactory(SkStream* stream) {
     return NULL;
 }
 
-static SkTRegistry<SkImageDecoder*, SkStream*> gReg(sk_libbmp_dfactory);
+static SkImageDecoder_DecodeReg gReg(sk_libbmp_dfactory);
 
 static SkImageDecoder::Format get_format_bmp(SkStream* stream) {
     if (is_bmp(stream)) {
@@ -61,7 +60,7 @@ static SkImageDecoder::Format get_format_bmp(SkStream* stream) {
     return SkImageDecoder::kUnknown_Format;
 }
 
-static SkTRegistry<SkImageDecoder::Format, SkStream*> gFormatReg(get_format_bmp);
+static SkImageDecoder_FormatReg gFormatReg(get_format_bmp);
 
 ///////////////////////////////////////////////////////////////////////////////
 
index d368ecc..21d95aa 100644 (file)
@@ -368,8 +368,6 @@ static bool is_gif(SkStream* stream) {
     return false;
 }
 
-#include "SkTRegistry.h"
-
 static SkImageDecoder* sk_libgif_dfactory(SkStream* stream) {
     if (is_gif(stream)) {
         return SkNEW(SkGIFImageDecoder);
@@ -377,7 +375,7 @@ static SkImageDecoder* sk_libgif_dfactory(SkStream* stream) {
     return NULL;
 }
 
-static SkTRegistry<SkImageDecoder*, SkStream*> gReg(sk_libgif_dfactory);
+static SkImageDecoder_DecodeReg gReg(sk_libgif_dfactory);
 
 static SkImageDecoder::Format get_format_gif(SkStream* stream) {
     if (is_gif(stream)) {
@@ -386,4 +384,4 @@ static SkImageDecoder::Format get_format_gif(SkStream* stream) {
     return SkImageDecoder::kUnknown_Format;
 }
 
-static SkTRegistry<SkImageDecoder::Format, SkStream*> gFormatReg(get_format_gif);
+static SkImageDecoder_FormatReg gFormatReg(get_format_gif);
index 47fa729..c0f0f91 100644 (file)
@@ -398,8 +398,6 @@ static bool is_ico(SkStream* stream) {
     return true;
 }
 
-#include "SkTRegistry.h"
-
 static SkImageDecoder* sk_libico_dfactory(SkStream* stream) {
     if (is_ico(stream)) {
         return SkNEW(SkICOImageDecoder);
@@ -407,7 +405,7 @@ static SkImageDecoder* sk_libico_dfactory(SkStream* stream) {
     return NULL;
 }
 
-static SkTRegistry<SkImageDecoder*, SkStream*> gReg(sk_libico_dfactory);
+static SkImageDecoder_DecodeReg gReg(sk_libico_dfactory);
 
 static SkImageDecoder::Format get_format_ico(SkStream* stream) {
     if (is_ico(stream)) {
@@ -416,4 +414,4 @@ static SkImageDecoder::Format get_format_ico(SkStream* stream) {
     return SkImageDecoder::kUnknown_Format;
 }
 
-static SkTRegistry<SkImageDecoder::Format, SkStream*> gFormatReg(get_format_ico);
+static SkImageDecoder_FormatReg gFormatReg(get_format_ico);
index 2bd4818..d50e4eb 100644 (file)
@@ -1136,8 +1136,6 @@ static bool is_jpeg(SkStream* stream) {
     return true;
 }
 
-#include "SkTRegistry.h"
-
 static SkImageDecoder* sk_libjpeg_dfactory(SkStream* stream) {
     if (is_jpeg(stream)) {
         return SkNEW(SkJPEGImageDecoder);
@@ -1157,6 +1155,6 @@ static SkImageEncoder* sk_libjpeg_efactory(SkImageEncoder::Type t) {
 }
 
 
-static SkTRegistry<SkImageDecoder*, SkStream*> gDReg(sk_libjpeg_dfactory);
-static SkTRegistry<SkImageDecoder::Format, SkStream*> gFormatReg(get_format_jpeg);
-static SkTRegistry<SkImageEncoder*, SkImageEncoder::Type> gEReg(sk_libjpeg_efactory);
+static SkImageDecoder_DecodeReg gDReg(sk_libjpeg_dfactory);
+static SkImageDecoder_FormatReg gFormatReg(get_format_jpeg);
+static SkImageEncoder_EncodeReg gEReg(sk_libjpeg_efactory);
index c188a2f..56c19fd 100644 (file)
@@ -1174,8 +1174,6 @@ DEFINE_DECODER_CREATOR(PNGImageDecoder);
 DEFINE_ENCODER_CREATOR(PNGImageEncoder);
 ///////////////////////////////////////////////////////////////////////////////
 
-#include "SkTRegistry.h"
-
 static bool is_png(SkStream* stream) {
     char buf[PNG_BYTES_TO_CHECK];
     if (stream->read(buf, PNG_BYTES_TO_CHECK) == PNG_BYTES_TO_CHECK &&
@@ -1203,6 +1201,6 @@ SkImageEncoder* sk_libpng_efactory(SkImageEncoder::Type t) {
     return (SkImageEncoder::kPNG_Type == t) ? SkNEW(SkPNGImageEncoder) : NULL;
 }
 
-static SkTRegistry<SkImageEncoder*, SkImageEncoder::Type> gEReg(sk_libpng_efactory);
-static SkTRegistry<SkImageDecoder::Format, SkStream*> gFormatReg(get_format_png);
-static SkTRegistry<SkImageDecoder*, SkStream*> gDReg(sk_libpng_dfactory);
+static SkImageDecoder_DecodeReg gDReg(sk_libpng_dfactory);
+static SkImageDecoder_FormatReg gFormatReg(get_format_png);
+static SkImageEncoder_EncodeReg gEReg(sk_libpng_efactory);
index 7fb1cf0..92b5c4f 100644 (file)
@@ -582,8 +582,6 @@ DEFINE_DECODER_CREATOR(WEBPImageDecoder);
 DEFINE_ENCODER_CREATOR(WEBPImageEncoder);
 ///////////////////////////////////////////////////////////////////////////////
 
-#include "SkTRegistry.h"
-
 static SkImageDecoder* sk_libwebp_dfactory(SkStream* stream) {
     int width, height, hasAlpha;
     if (!webp_parse_header(stream, &width, &height, &hasAlpha)) {
@@ -606,6 +604,6 @@ static SkImageEncoder* sk_libwebp_efactory(SkImageEncoder::Type t) {
       return (SkImageEncoder::kWEBP_Type == t) ? SkNEW(SkWEBPImageEncoder) : NULL;
 }
 
-static SkTRegistry<SkImageDecoder*, SkStream*> gDReg(sk_libwebp_dfactory);
-static SkTRegistry<SkImageDecoder::Format, SkStream*> gFormatReg(get_format_webp);
-static SkTRegistry<SkImageEncoder*, SkImageEncoder::Type> gEReg(sk_libwebp_efactory);
+static SkImageDecoder_DecodeReg gDReg(sk_libwebp_dfactory);
+static SkImageDecoder_FormatReg gFormatReg(get_format_webp);
+static SkImageEncoder_EncodeReg gEReg(sk_libwebp_efactory);
index 8b1659b..9460d55 100644 (file)
@@ -152,8 +152,6 @@ bool SkWBMPImageDecoder::onDecode(SkStream* stream, SkBitmap* decodedBitmap,
 DEFINE_DECODER_CREATOR(WBMPImageDecoder);
 ///////////////////////////////////////////////////////////////////////////////
 
-#include "SkTRegistry.h"
-
 static SkImageDecoder* sk_wbmp_dfactory(SkStream* stream) {
     wbmp_head   head;
 
@@ -171,5 +169,5 @@ static SkImageDecoder::Format get_format_wbmp(SkStream* stream) {
     return SkImageDecoder::kUnknown_Format;
 }
 
-static SkTRegistry<SkImageDecoder*, SkStream*> gReg(sk_wbmp_dfactory);
-static SkTRegistry<SkImageDecoder::Format, SkStream*> gFormatReg(get_format_wbmp);
+static SkImageDecoder_DecodeReg gDReg(sk_wbmp_dfactory);
+static SkImageDecoder_FormatReg gFormatReg(get_format_wbmp);
index 10f8d64..2931d12 100644 (file)
@@ -6,18 +6,13 @@
  * found in the LICENSE file.
  */
 
-
 #include "SkImageEncoder.h"
-#include "SkTRegistry.h"
-
-typedef SkTRegistry<SkImageEncoder*, SkImageEncoder::Type> EncodeReg;
 
-// Can't use the typedef here because of complex C++ corner cases
-template EncodeReg* SkTRegistry<SkImageEncoder*, SkImageEncoder::Type>::gHead;
+template SkImageEncoder_EncodeReg* SkImageEncoder_EncodeReg::gHead;
 
 SkImageEncoder* SkImageEncoder::Create(Type t) {
     SkImageEncoder* codec = NULL;
-    const EncodeReg* curr = EncodeReg::Head();
+    const SkImageEncoder_EncodeReg* curr = SkImageEncoder_EncodeReg::Head();
     while (curr) {
         if ((codec = curr->factory()(t)) != NULL) {
             return codec;
index 3158168..e4c7003 100644 (file)
@@ -446,4 +446,4 @@ SkMovie* Factory(SkStream* stream) {
     return NULL;
 }
 
-static SkTRegistry<SkMovie*, SkStream*> gReg(Factory);
+static SkTRegistry<SkMovie*(*)(SkStream*)> gReg(Factory);
index b1d1613..9751f72 100644 (file)
@@ -260,7 +260,7 @@ static SkImageEncoder* sk_imageencoder_cg_factory(SkImageEncoder::Type t) {
     return SkNEW_ARGS(SkImageEncoder_CG, (t));
 }
 
-static SkTRegistry<SkImageEncoder*, SkImageEncoder::Type> gEReg(sk_imageencoder_cg_factory);
+static SkTRegistry<SkImageEncoder*(*)(SkImageEncoder::Type)> gEReg(sk_imageencoder_cg_factory);
 
 struct FormatConversion {
     CFStringRef             fUTType;
@@ -302,4 +302,4 @@ static SkImageDecoder::Format get_format_cg(SkStream *stream) {
     return UTType_to_Format(name);
 }
 
-static SkTRegistry<SkImageDecoder::Format, SkStream*> gFormatReg(get_format_cg);
+static SkTRegistry<SkImageDecoder::Format(*)(SkStream*)> gFormatReg(get_format_cg);
index 77f4b95..54d0427 100644 (file)
@@ -432,7 +432,7 @@ static SkImageEncoder* sk_imageencoder_wic_factory(SkImageEncoder::Type t) {
     return SkNEW_ARGS(SkImageEncoder_WIC, (t));
 }
 
-static SkTRegistry<SkImageEncoder*, SkImageEncoder::Type> gEReg(sk_imageencoder_wic_factory);
+static SkTRegistry<SkImageEncoder*(*)(SkImageEncoder::Type)> gEReg(sk_imageencoder_wic_factory);
 
 static SkImageDecoder::Format get_format_wic(SkStream* stream) {
     SkImageDecoder::Format format;
@@ -443,4 +443,4 @@ static SkImageDecoder::Format get_format_wic(SkStream* stream) {
     return format;
 }
 
-static SkTRegistry<SkImageDecoder::Format, SkStream*> gFormatReg(get_format_wic);
+static SkTRegistry<SkImageDecoder::Format(*)(SkStream*)> gFormatReg(get_format_wic);
index 27b31df..1c89f98 100644 (file)
@@ -86,7 +86,7 @@ namespace skiatest {
     private:
     };
 
-    typedef SkTRegistry<Test*, void*> TestRegistry;
+    typedef SkTRegistry<Test*(*)(void*)> TestRegistry;
 }
 
 #define REPORTER_ASSERT(r, cond)                                        \