2 * Copyright 2006 The Android Open Source Project
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef SkFlattenable_DEFINED
9 #define SkFlattenable_DEFINED
17 * Flattening is straight-forward:
18 * 1. call getFactory() so we have a function-ptr to recreate the subclass
19 * 2. call flatten(buffer) to write out enough data for the factory to read
21 * Unflattening is easy for the caller: new_instance = factory(buffer)
23 * The complexity of supporting this is as follows.
25 * If your subclass wants to control unflattening, use this macro in your declaration:
26 * SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS
27 * This will provide a getFactory(), and require that the subclass implements CreateProc.
29 * For older buffers (before the DEEPFLATTENING change, the macros below declare
30 * a thin factory DeepCreateProc. It checks the version of the buffer, and if it is pre-deep,
31 * then it calls through to a (usually protected) constructor, passing the buffer.
32 * If the buffer is newer, then it directly calls the "real" factory: CreateProc.
35 #define SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP() static void InitializeFlattenables();
37 #define SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(flattenable) \
38 void flattenable::InitializeFlattenables() {
40 #define SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END \
43 #define SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(flattenable) \
44 SkFlattenable::Registrar(#flattenable, flattenable::CreateProc, \
45 flattenable::GetFlattenableType());
47 #define SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(flattenable) \
49 static SkFlattenable* CreateProc(SkReadBuffer&); \
50 friend class SkPrivateEffectInitializer; \
52 Factory getFactory() const SK_OVERRIDE { return CreateProc; }
54 /** For SkFlattenable derived objects with a valid type
55 This macro should only be used in base class objects in core
57 #define SK_DEFINE_FLATTENABLE_TYPE(flattenable) \
58 static Type GetFlattenableType() { \
59 return k##flattenable##_Type; \
62 /** \class SkFlattenable
64 SkFlattenable is the base class for objects that need to be flattened
65 into a data stream for either transport or as part of the key to the
68 class SK_API SkFlattenable : public SkRefCnt {
79 kSkUnused_Type, // used to be SkUnitMapper
83 SK_DECLARE_INST_COUNT(SkFlattenable)
85 typedef SkFlattenable* (*Factory)(SkReadBuffer&);
89 /** Implement this to return a factory function pointer that can be called
90 to recreate your class given a buffer (previously written to by your
91 override of flatten().
93 virtual Factory getFactory() const = 0;
95 /** Returns the name of the object's class
97 const char* getTypeName() const { return FactoryToName(getFactory()); }
99 static Factory NameToFactory(const char name[]);
100 static const char* FactoryToName(Factory);
101 static bool NameToType(const char name[], Type* type);
103 static void Register(const char name[], Factory, Type);
107 Registrar(const char name[], Factory factory, Type type) {
108 SkFlattenable::Register(name, factory, type);
113 * Override this if your subclass needs to record data that it will need to recreate itself
114 * from its CreateProc (returned by getFactory()).
116 virtual void flatten(SkWriteBuffer&) const {}
119 static void InitializeFlattenablesIfNeeded();
121 friend class SkGraphics;
123 typedef SkRefCnt INHERITED;