michael@0: michael@0: /* michael@0: * Copyright 2006 The Android Open Source Project michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: michael@0: #ifndef SkFlattenable_DEFINED michael@0: #define SkFlattenable_DEFINED michael@0: michael@0: #include "SkRefCnt.h" michael@0: michael@0: class SkReadBuffer; michael@0: class SkWriteBuffer; michael@0: michael@0: #define SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(flattenable) \ michael@0: SkFlattenable::Registrar(#flattenable, flattenable::CreateProc, \ michael@0: flattenable::GetFlattenableType()); michael@0: michael@0: #define SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP() static void InitializeFlattenables(); michael@0: michael@0: #define SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(flattenable) \ michael@0: void flattenable::InitializeFlattenables() { michael@0: michael@0: #define SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END \ michael@0: } michael@0: michael@0: #define SK_DECLARE_UNFLATTENABLE_OBJECT() \ michael@0: virtual Factory getFactory() const SK_OVERRIDE { return NULL; } michael@0: michael@0: #define SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(flattenable) \ michael@0: virtual Factory getFactory() const SK_OVERRIDE { return CreateProc; } \ michael@0: static SkFlattenable* CreateProc(SkReadBuffer& buffer) { \ michael@0: return SkNEW_ARGS(flattenable, (buffer)); \ michael@0: } michael@0: michael@0: /** For SkFlattenable derived objects with a valid type michael@0: This macro should only be used in base class objects in core michael@0: */ michael@0: #define SK_DEFINE_FLATTENABLE_TYPE(flattenable) \ michael@0: static Type GetFlattenableType() { \ michael@0: return k##flattenable##_Type; \ michael@0: } michael@0: michael@0: /** \class SkFlattenable michael@0: michael@0: SkFlattenable is the base class for objects that need to be flattened michael@0: into a data stream for either transport or as part of the key to the michael@0: font cache. michael@0: */ michael@0: class SK_API SkFlattenable : public SkRefCnt { michael@0: public: michael@0: enum Type { michael@0: kSkColorFilter_Type, michael@0: kSkDrawLooper_Type, michael@0: kSkImageFilter_Type, michael@0: kSkMaskFilter_Type, michael@0: kSkPathEffect_Type, michael@0: kSkPixelRef_Type, michael@0: kSkRasterizer_Type, michael@0: kSkShader_Type, michael@0: kSkUnitMapper_Type, michael@0: kSkXfermode_Type, michael@0: }; michael@0: michael@0: SK_DECLARE_INST_COUNT(SkFlattenable) michael@0: michael@0: typedef SkFlattenable* (*Factory)(SkReadBuffer&); michael@0: michael@0: SkFlattenable() {} michael@0: michael@0: /** Implement this to return a factory function pointer that can be called michael@0: to recreate your class given a buffer (previously written to by your michael@0: override of flatten(). michael@0: */ michael@0: virtual Factory getFactory() const = 0; michael@0: michael@0: /** Returns the name of the object's class michael@0: */ michael@0: const char* getTypeName() const { return FactoryToName(getFactory()); } michael@0: michael@0: static Factory NameToFactory(const char name[]); michael@0: static const char* FactoryToName(Factory); michael@0: static bool NameToType(const char name[], Type* type); michael@0: michael@0: static void Register(const char name[], Factory, Type); michael@0: michael@0: class Registrar { michael@0: public: michael@0: Registrar(const char name[], Factory factory, Type type) { michael@0: SkFlattenable::Register(name, factory, type); michael@0: } michael@0: }; michael@0: michael@0: /** Override this to write data specific to your subclass into the buffer, michael@0: being sure to call your super-class' version first. This data will later michael@0: be passed to your Factory function, returned by getFactory(). michael@0: */ michael@0: virtual void flatten(SkWriteBuffer&) const; michael@0: michael@0: protected: michael@0: SkFlattenable(SkReadBuffer&) {} michael@0: michael@0: private: michael@0: static void InitializeFlattenablesIfNeeded(); michael@0: michael@0: friend class SkGraphics; michael@0: michael@0: typedef SkRefCnt INHERITED; michael@0: }; michael@0: michael@0: #endif