diff options
author | mtklein@google.com <mtklein@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2013-09-04 17:20:18 +0000 |
---|---|---|
committer | mtklein@google.com <mtklein@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2013-09-04 17:20:18 +0000 |
commit | bd6343b1d60d2a85e930f33f4b06b4502b3e8caa (patch) | |
tree | 4c8e362b21994e36c823e260f32b2f31ace9ffe9 | |
parent | 339e79fbeabae18a8b9ea094293c7c25eaf9dd68 (diff) |
Rewrite SkTRegistry to take any trivially-copyable type.
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
-rw-r--r-- | bench/SkBenchmark.h | 2 | ||||
-rw-r--r-- | experimental/SkiaExamples/SkExample.h | 2 | ||||
-rw-r--r-- | gm/gm.cpp | 2 | ||||
-rw-r--r-- | gm/gm.h | 2 | ||||
-rw-r--r-- | include/core/SkImageDecoder.h | 7 | ||||
-rw-r--r-- | include/core/SkImageEncoder.h | 4 | ||||
-rw-r--r-- | include/core/SkTRegistry.h | 13 | ||||
-rw-r--r-- | src/images/SkImageDecoder_FactoryDefault.cpp | 3 | ||||
-rw-r--r-- | src/images/SkImageDecoder_FactoryRegistrar.cpp | 14 | ||||
-rw-r--r-- | src/images/SkImageDecoder_libbmp.cpp | 5 | ||||
-rw-r--r-- | src/images/SkImageDecoder_libgif.cpp | 6 | ||||
-rw-r--r-- | src/images/SkImageDecoder_libico.cpp | 6 | ||||
-rw-r--r-- | src/images/SkImageDecoder_libjpeg.cpp | 8 | ||||
-rw-r--r-- | src/images/SkImageDecoder_libpng.cpp | 8 | ||||
-rw-r--r-- | src/images/SkImageDecoder_libwebp.cpp | 8 | ||||
-rw-r--r-- | src/images/SkImageDecoder_wbmp.cpp | 6 | ||||
-rw-r--r-- | src/images/SkImageEncoder_Factory.cpp | 9 | ||||
-rw-r--r-- | src/images/SkMovie_gif.cpp | 2 | ||||
-rw-r--r-- | src/ports/SkImageDecoder_CG.cpp | 4 | ||||
-rw-r--r-- | src/ports/SkImageDecoder_WIC.cpp | 4 | ||||
-rw-r--r-- | tests/Test.h | 2 |
21 files changed, 51 insertions, 66 deletions
diff --git a/bench/SkBenchmark.h b/bench/SkBenchmark.h index 404d4c3e7e..7ebe104cb5 100644 --- a/bench/SkBenchmark.h +++ b/bench/SkBenchmark.h @@ -154,6 +154,6 @@ private: typedef SkRefCnt INHERITED; }; -typedef SkTRegistry<SkBenchmark*, void*> BenchRegistry; +typedef SkTRegistry<SkBenchmark*(*)(void*)> BenchRegistry; #endif diff --git a/experimental/SkiaExamples/SkExample.h b/experimental/SkiaExamples/SkExample.h index 51fe21c06c..bea3aec4fc 100644 --- a/experimental/SkiaExamples/SkExample.h +++ b/experimental/SkiaExamples/SkExample.h @@ -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; @@ -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; @@ -111,7 +111,7 @@ namespace skiagm { bool fHaveCalledOnceBeforeDraw; }; - typedef SkTRegistry<GM*, void*> GMRegistry; + typedef SkTRegistry<GM*(*)(void*)> GMRegistry; } #endif diff --git a/include/core/SkImageDecoder.h b/include/core/SkImageDecoder.h index 7bdaaa8eb5..a8cbe63413 100644 --- a/include/core/SkImageDecoder.h +++ b/include/core/SkImageDecoder.h @@ -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 diff --git a/include/core/SkImageEncoder.h b/include/core/SkImageEncoder.h index b990aff2a6..5622eee7e0 100644 --- a/include/core/SkImageEncoder.h +++ b/include/core/SkImageEncoder.h @@ -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 diff --git a/include/core/SkTRegistry.h b/include/core/SkTRegistry.h index 34fcffd5c5..0994c990dc 100644 --- a/include/core/SkTRegistry.h +++ b/include/core/SkTRegistry.h @@ -16,11 +16,11 @@ 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 diff --git a/src/images/SkImageDecoder_FactoryDefault.cpp b/src/images/SkImageDecoder_FactoryDefault.cpp index 565519acc8..247f571800 100644 --- a/src/images/SkImageDecoder_FactoryDefault.cpp +++ b/src/images/SkImageDecoder_FactoryDefault.cpp @@ -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(); diff --git a/src/images/SkImageDecoder_FactoryRegistrar.cpp b/src/images/SkImageDecoder_FactoryRegistrar.cpp index f1eca3d03a..9ed17e0f67 100644 --- a/src/images/SkImageDecoder_FactoryRegistrar.cpp +++ b/src/images/SkImageDecoder_FactoryRegistrar.cpp @@ -14,17 +14,13 @@ // 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()) { diff --git a/src/images/SkImageDecoder_libbmp.cpp b/src/images/SkImageDecoder_libbmp.cpp index 73e7a20144..2edd57c0d1 100644 --- a/src/images/SkImageDecoder_libbmp.cpp +++ b/src/images/SkImageDecoder_libbmp.cpp @@ -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); /////////////////////////////////////////////////////////////////////////////// diff --git a/src/images/SkImageDecoder_libgif.cpp b/src/images/SkImageDecoder_libgif.cpp index d368eccd92..21d95aae00 100644 --- a/src/images/SkImageDecoder_libgif.cpp +++ b/src/images/SkImageDecoder_libgif.cpp @@ -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); diff --git a/src/images/SkImageDecoder_libico.cpp b/src/images/SkImageDecoder_libico.cpp index 47fa7293f0..c0f0f91edf 100644 --- a/src/images/SkImageDecoder_libico.cpp +++ b/src/images/SkImageDecoder_libico.cpp @@ -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); diff --git a/src/images/SkImageDecoder_libjpeg.cpp b/src/images/SkImageDecoder_libjpeg.cpp index 2bd48186ac..d50e4eb015 100644 --- a/src/images/SkImageDecoder_libjpeg.cpp +++ b/src/images/SkImageDecoder_libjpeg.cpp @@ -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); diff --git a/src/images/SkImageDecoder_libpng.cpp b/src/images/SkImageDecoder_libpng.cpp index c188a2fabc..56c19fd98c 100644 --- a/src/images/SkImageDecoder_libpng.cpp +++ b/src/images/SkImageDecoder_libpng.cpp @@ -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); diff --git a/src/images/SkImageDecoder_libwebp.cpp b/src/images/SkImageDecoder_libwebp.cpp index 7fb1cf0e31..92b5c4ffb8 100644 --- a/src/images/SkImageDecoder_libwebp.cpp +++ b/src/images/SkImageDecoder_libwebp.cpp @@ -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); diff --git a/src/images/SkImageDecoder_wbmp.cpp b/src/images/SkImageDecoder_wbmp.cpp index 8b1659ba8d..9460d55899 100644 --- a/src/images/SkImageDecoder_wbmp.cpp +++ b/src/images/SkImageDecoder_wbmp.cpp @@ -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); diff --git a/src/images/SkImageEncoder_Factory.cpp b/src/images/SkImageEncoder_Factory.cpp index 10f8d64dec..2931d12eae 100644 --- a/src/images/SkImageEncoder_Factory.cpp +++ b/src/images/SkImageEncoder_Factory.cpp @@ -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; diff --git a/src/images/SkMovie_gif.cpp b/src/images/SkMovie_gif.cpp index 315816812a..e4c70037de 100644 --- a/src/images/SkMovie_gif.cpp +++ b/src/images/SkMovie_gif.cpp @@ -446,4 +446,4 @@ SkMovie* Factory(SkStream* stream) { return NULL; } -static SkTRegistry<SkMovie*, SkStream*> gReg(Factory); +static SkTRegistry<SkMovie*(*)(SkStream*)> gReg(Factory); diff --git a/src/ports/SkImageDecoder_CG.cpp b/src/ports/SkImageDecoder_CG.cpp index b1d16136c4..9751f72062 100644 --- a/src/ports/SkImageDecoder_CG.cpp +++ b/src/ports/SkImageDecoder_CG.cpp @@ -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); diff --git a/src/ports/SkImageDecoder_WIC.cpp b/src/ports/SkImageDecoder_WIC.cpp index 77f4b95276..54d0427a8d 100644 --- a/src/ports/SkImageDecoder_WIC.cpp +++ b/src/ports/SkImageDecoder_WIC.cpp @@ -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); diff --git a/tests/Test.h b/tests/Test.h index 27b31df94d..1c89f98e09 100644 --- a/tests/Test.h +++ b/tests/Test.h @@ -86,7 +86,7 @@ namespace skiatest { private: }; - typedef SkTRegistry<Test*, void*> TestRegistry; + typedef SkTRegistry<Test*(*)(void*)> TestRegistry; } #define REPORTER_ASSERT(r, cond) \ |