1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com
28a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/*
3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2006 The Android Open Source Project
48a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com *
5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be
6ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file.
78a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */
88a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com
108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifndef SkFlattenable_DEFINED
118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkFlattenable_DEFINED
128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkRefCnt.h"
148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
158b0e8ac5f582de80356019406e2975079bf0829dcommit-bot@chromium.orgclass SkReadBuffer;
168b0e8ac5f582de80356019406e2975079bf0829dcommit-bot@chromium.orgclass SkWriteBuffer;
178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
18a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com#define SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(flattenable) \
19c0b7e10c6a68f59e1653e6c18e6bc954b3c3f0cfcommit-bot@chromium.org        SkFlattenable::Registrar(#flattenable, flattenable::CreateProc, \
20c0b7e10c6a68f59e1653e6c18e6bc954b3c3f0cfcommit-bot@chromium.org                                 flattenable::GetFlattenableType());
21d26147adbbdca85f07dff432025afee0c8614387caryclark@google.com
22a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com#define SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP() static void InitializeFlattenables();
23d26147adbbdca85f07dff432025afee0c8614387caryclark@google.com
24d26147adbbdca85f07dff432025afee0c8614387caryclark@google.com#define SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(flattenable) \
25a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com    void flattenable::InitializeFlattenables() {
26a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com
27d26147adbbdca85f07dff432025afee0c8614387caryclark@google.com#define SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END \
28d26147adbbdca85f07dff432025afee0c8614387caryclark@google.com    }
29d26147adbbdca85f07dff432025afee0c8614387caryclark@google.com
30ba28d03e94dc221d6a803bf2a84a420b9159255cdjsollen@google.com#define SK_DECLARE_UNFLATTENABLE_OBJECT() \
31c2eae4795478ab134a2315b1a9ff2c5de1d049e4robertphillips@google.com    virtual Factory getFactory() const SK_OVERRIDE { return NULL; }
32ba28d03e94dc221d6a803bf2a84a420b9159255cdjsollen@google.com
33ba28d03e94dc221d6a803bf2a84a420b9159255cdjsollen@google.com#define SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(flattenable) \
34c2eae4795478ab134a2315b1a9ff2c5de1d049e4robertphillips@google.com    virtual Factory getFactory() const SK_OVERRIDE { return CreateProc; } \
358b0e8ac5f582de80356019406e2975079bf0829dcommit-bot@chromium.org    static SkFlattenable* CreateProc(SkReadBuffer& buffer) { \
36ba28d03e94dc221d6a803bf2a84a420b9159255cdjsollen@google.com        return SkNEW_ARGS(flattenable, (buffer)); \
37ba28d03e94dc221d6a803bf2a84a420b9159255cdjsollen@google.com    }
38ba28d03e94dc221d6a803bf2a84a420b9159255cdjsollen@google.com
39c0b7e10c6a68f59e1653e6c18e6bc954b3c3f0cfcommit-bot@chromium.org/** For SkFlattenable derived objects with a valid type
40c0b7e10c6a68f59e1653e6c18e6bc954b3c3f0cfcommit-bot@chromium.org    This macro should only be used in base class objects in core
41c0b7e10c6a68f59e1653e6c18e6bc954b3c3f0cfcommit-bot@chromium.org  */
42c0b7e10c6a68f59e1653e6c18e6bc954b3c3f0cfcommit-bot@chromium.org#define SK_DEFINE_FLATTENABLE_TYPE(flattenable) \
43c0b7e10c6a68f59e1653e6c18e6bc954b3c3f0cfcommit-bot@chromium.org    static Type GetFlattenableType() { \
44c0b7e10c6a68f59e1653e6c18e6bc954b3c3f0cfcommit-bot@chromium.org        return k##flattenable##_Type; \
45c0b7e10c6a68f59e1653e6c18e6bc954b3c3f0cfcommit-bot@chromium.org    }
46c0b7e10c6a68f59e1653e6c18e6bc954b3c3f0cfcommit-bot@chromium.org
478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** \class SkFlattenable
48fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkFlattenable is the base class for objects that need to be flattened
508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com into a data stream for either transport or as part of the key to the
518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com font cache.
528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */
537ffb1b21abcc7bbed5a0fc711f6dd7b9dbb4f577ctguil@chromium.orgclass SK_API SkFlattenable : public SkRefCnt {
548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
55c0b7e10c6a68f59e1653e6c18e6bc954b3c3f0cfcommit-bot@chromium.org    enum Type {
56c0b7e10c6a68f59e1653e6c18e6bc954b3c3f0cfcommit-bot@chromium.org        kSkColorFilter_Type,
57c0b7e10c6a68f59e1653e6c18e6bc954b3c3f0cfcommit-bot@chromium.org        kSkDrawLooper_Type,
58c0b7e10c6a68f59e1653e6c18e6bc954b3c3f0cfcommit-bot@chromium.org        kSkImageFilter_Type,
59c0b7e10c6a68f59e1653e6c18e6bc954b3c3f0cfcommit-bot@chromium.org        kSkMaskFilter_Type,
60c0b7e10c6a68f59e1653e6c18e6bc954b3c3f0cfcommit-bot@chromium.org        kSkPathEffect_Type,
61c0b7e10c6a68f59e1653e6c18e6bc954b3c3f0cfcommit-bot@chromium.org        kSkPixelRef_Type,
62c0b7e10c6a68f59e1653e6c18e6bc954b3c3f0cfcommit-bot@chromium.org        kSkRasterizer_Type,
63c0b7e10c6a68f59e1653e6c18e6bc954b3c3f0cfcommit-bot@chromium.org        kSkShader_Type,
6483f23d87f1d67e6e73873e1ef7cda621c43703a0commit-bot@chromium.org        kSkUnused_Type,     // used to be SkUnitMapper
65c0b7e10c6a68f59e1653e6c18e6bc954b3c3f0cfcommit-bot@chromium.org        kSkXfermode_Type,
66c0b7e10c6a68f59e1653e6c18e6bc954b3c3f0cfcommit-bot@chromium.org    };
67c0b7e10c6a68f59e1653e6c18e6bc954b3c3f0cfcommit-bot@chromium.org
6815e9d3e66e161ce23df30bc13f8a0c87d196b463robertphillips@google.com    SK_DECLARE_INST_COUNT(SkFlattenable)
6915e9d3e66e161ce23df30bc13f8a0c87d196b463robertphillips@google.com
708b0e8ac5f582de80356019406e2975079bf0829dcommit-bot@chromium.org    typedef SkFlattenable* (*Factory)(SkReadBuffer&);
71fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkFlattenable() {}
73fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Implement this to return a factory function pointer that can be called
758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     to recreate your class given a buffer (previously written to by your
768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     override of flatten().
778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     */
78c2eae4795478ab134a2315b1a9ff2c5de1d049e4robertphillips@google.com    virtual Factory getFactory() const = 0;
79fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
80c0b7e10c6a68f59e1653e6c18e6bc954b3c3f0cfcommit-bot@chromium.org    /** Returns the name of the object's class
81c0b7e10c6a68f59e1653e6c18e6bc954b3c3f0cfcommit-bot@chromium.org      */
82c0b7e10c6a68f59e1653e6c18e6bc954b3c3f0cfcommit-bot@chromium.org    const char* getTypeName() const { return FactoryToName(getFactory()); }
83c0b7e10c6a68f59e1653e6c18e6bc954b3c3f0cfcommit-bot@chromium.org
848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static Factory NameToFactory(const char name[]);
858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static const char* FactoryToName(Factory);
86c0b7e10c6a68f59e1653e6c18e6bc954b3c3f0cfcommit-bot@chromium.org    static bool NameToType(const char name[], Type* type);
87c0b7e10c6a68f59e1653e6c18e6bc954b3c3f0cfcommit-bot@chromium.org
88c0b7e10c6a68f59e1653e6c18e6bc954b3c3f0cfcommit-bot@chromium.org    static void Register(const char name[], Factory, Type);
89a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com
908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    class Registrar {
918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    public:
92c0b7e10c6a68f59e1653e6c18e6bc954b3c3f0cfcommit-bot@chromium.org        Registrar(const char name[], Factory factory, Type type) {
93c0b7e10c6a68f59e1653e6c18e6bc954b3c3f0cfcommit-bot@chromium.org            SkFlattenable::Register(name, factory, type);
948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    };
96a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com
9754924243c1b65b3ee6d8fa064b50a9b1bb2a19a5djsollen@google.com    /** Override this to write data specific to your subclass into the buffer,
9854924243c1b65b3ee6d8fa064b50a9b1bb2a19a5djsollen@google.com     being sure to call your super-class' version first. This data will later
9954924243c1b65b3ee6d8fa064b50a9b1bb2a19a5djsollen@google.com     be passed to your Factory function, returned by getFactory().
10054924243c1b65b3ee6d8fa064b50a9b1bb2a19a5djsollen@google.com     */
1018b0e8ac5f582de80356019406e2975079bf0829dcommit-bot@chromium.org    virtual void flatten(SkWriteBuffer&) const;
1028b0e8ac5f582de80356019406e2975079bf0829dcommit-bot@chromium.org
1038b0e8ac5f582de80356019406e2975079bf0829dcommit-bot@chromium.orgprotected:
1048b0e8ac5f582de80356019406e2975079bf0829dcommit-bot@chromium.org    SkFlattenable(SkReadBuffer&) {}
105d26147adbbdca85f07dff432025afee0c8614387caryclark@google.com
106d26147adbbdca85f07dff432025afee0c8614387caryclark@google.comprivate:
107c0b7e10c6a68f59e1653e6c18e6bc954b3c3f0cfcommit-bot@chromium.org    static void InitializeFlattenablesIfNeeded();
1089d0c6ecb8440e8e546881a4ff850eb6333f24541caryclark@google.com
1099d0c6ecb8440e8e546881a4ff850eb6333f24541caryclark@google.com    friend class SkGraphics;
11015e9d3e66e161ce23df30bc13f8a0c87d196b463robertphillips@google.com
11115e9d3e66e161ce23df30bc13f8a0c87d196b463robertphillips@google.com    typedef SkRefCnt INHERITED;
1128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
1138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
115