SkFlattenable.h revision c0b7e10c6a68f59e1653e6c18e6bc954b3c3f0cf
1
2/*
3 * Copyright 2006 The Android Open Source Project
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9
10#ifndef SkFlattenable_DEFINED
11#define SkFlattenable_DEFINED
12
13#include "SkRefCnt.h"
14
15class SkFlattenableReadBuffer;
16class SkFlattenableWriteBuffer;
17
18#define SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(flattenable) \
19        SkFlattenable::Registrar(#flattenable, flattenable::CreateProc, \
20                                 flattenable::GetFlattenableType());
21
22#define SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP() static void InitializeFlattenables();
23
24#define SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(flattenable) \
25    void flattenable::InitializeFlattenables() {
26
27#define SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END \
28    }
29
30#define SK_DECLARE_UNFLATTENABLE_OBJECT() \
31    virtual Factory getFactory() const SK_OVERRIDE { return NULL; }
32
33#define SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(flattenable) \
34    virtual Factory getFactory() const SK_OVERRIDE { return CreateProc; } \
35    static SkFlattenable* CreateProc(SkFlattenableReadBuffer& buffer) { \
36        return SkNEW_ARGS(flattenable, (buffer)); \
37    }
38
39/** For SkFlattenable derived objects with a valid type
40    This macro should only be used in base class objects in core
41  */
42#define SK_DEFINE_FLATTENABLE_TYPE(flattenable) \
43    static Type GetFlattenableType() { \
44        return k##flattenable##_Type; \
45    }
46
47/** \class SkFlattenable
48
49 SkFlattenable is the base class for objects that need to be flattened
50 into a data stream for either transport or as part of the key to the
51 font cache.
52 */
53class SK_API SkFlattenable : public SkRefCnt {
54public:
55    enum Type {
56        kSkColorFilter_Type,
57        kSkDrawLooper_Type,
58        kSkImageFilter_Type,
59        kSkMaskFilter_Type,
60        kSkPathEffect_Type,
61        kSkPixelRef_Type,
62        kSkRasterizer_Type,
63        kSkShader_Type,
64        kSkUnitMapper_Type,
65        kSkXfermode_Type,
66    };
67
68    SK_DECLARE_INST_COUNT(SkFlattenable)
69
70    typedef SkFlattenable* (*Factory)(SkFlattenableReadBuffer&);
71
72    SkFlattenable() {}
73
74    /** Implement this to return a factory function pointer that can be called
75     to recreate your class given a buffer (previously written to by your
76     override of flatten().
77     */
78    virtual Factory getFactory() const = 0;
79
80    /** Returns the name of the object's class
81      */
82    const char* getTypeName() const { return FactoryToName(getFactory()); }
83
84    static Factory NameToFactory(const char name[]);
85    static const char* FactoryToName(Factory);
86    static bool NameToType(const char name[], Type* type);
87
88    static void Register(const char name[], Factory, Type);
89
90    class Registrar {
91    public:
92        Registrar(const char name[], Factory factory, Type type) {
93            SkFlattenable::Register(name, factory, type);
94        }
95    };
96
97protected:
98    SkFlattenable(SkFlattenableReadBuffer&) {}
99    /** Override this to write data specific to your subclass into the buffer,
100     being sure to call your super-class' version first. This data will later
101     be passed to your Factory function, returned by getFactory().
102     */
103    virtual void flatten(SkFlattenableWriteBuffer&) const;
104
105private:
106    static void InitializeFlattenablesIfNeeded();
107
108    friend class SkGraphics;
109    friend class SkFlattenableWriteBuffer;
110
111    typedef SkRefCnt INHERITED;
112};
113
114#endif
115