1 | /* |
2 | * Copyright 2006 The Android Open Source Project |
3 | * |
4 | * Use of this source code is governed by a BSD-style license that can be |
5 | * found in the LICENSE file. |
6 | */ |
7 | |
8 | #ifndef SkFlattenable_DEFINED |
9 | #define SkFlattenable_DEFINED |
10 | |
11 | #include "include/core/SkRefCnt.h" |
12 | |
13 | class SkData; |
14 | class SkReadBuffer; |
15 | class SkWriteBuffer; |
16 | |
17 | struct SkSerialProcs; |
18 | struct SkDeserialProcs; |
19 | |
20 | /** \class SkFlattenable |
21 | |
22 | SkFlattenable is the base class for objects that need to be flattened |
23 | into a data stream for either transport or as part of the key to the |
24 | font cache. |
25 | */ |
26 | class SK_API SkFlattenable : public SkRefCnt { |
27 | public: |
28 | enum Type { |
29 | kSkColorFilter_Type, |
30 | kSkDrawable_Type, |
31 | kSkDrawLooper_Type, |
32 | kSkImageFilter_Type, |
33 | kSkMaskFilter_Type, |
34 | kSkPathEffect_Type, |
35 | kSkPixelRef_Type, |
36 | kSkUnused_Type4, // used to be SkRasterizer |
37 | kSkShaderBase_Type, |
38 | kSkUnused_Type, // used to be SkUnitMapper |
39 | kSkUnused_Type2, |
40 | kSkUnused_Type3, // use to be NormalSource, |
41 | }; |
42 | |
43 | typedef sk_sp<SkFlattenable> (*Factory)(SkReadBuffer&); |
44 | |
45 | SkFlattenable() {} |
46 | |
47 | /** Implement this to return a factory function pointer that can be called |
48 | to recreate your class given a buffer (previously written to by your |
49 | override of flatten(). |
50 | */ |
51 | virtual Factory getFactory() const = 0; |
52 | |
53 | /** |
54 | * Returns the name of the object's class. |
55 | */ |
56 | virtual const char* getTypeName() const = 0; |
57 | |
58 | static Factory NameToFactory(const char name[]); |
59 | static const char* FactoryToName(Factory); |
60 | |
61 | static void Register(const char name[], Factory); |
62 | |
63 | /** |
64 | * Override this if your subclass needs to record data that it will need to recreate itself |
65 | * from its CreateProc (returned by getFactory()). |
66 | * |
67 | * DEPRECATED public : will move to protected ... use serialize() instead |
68 | */ |
69 | virtual void flatten(SkWriteBuffer&) const {} |
70 | |
71 | virtual Type getFlattenableType() const = 0; |
72 | |
73 | // |
74 | // public ways to serialize / deserialize |
75 | // |
76 | sk_sp<SkData> serialize(const SkSerialProcs* = nullptr) const; |
77 | size_t serialize(void* memory, size_t memory_size, |
78 | const SkSerialProcs* = nullptr) const; |
79 | static sk_sp<SkFlattenable> Deserialize(Type, const void* data, size_t length, |
80 | const SkDeserialProcs* procs = nullptr); |
81 | |
82 | protected: |
83 | class PrivateInitializer { |
84 | public: |
85 | static void InitEffects(); |
86 | static void InitImageFilters(); |
87 | }; |
88 | |
89 | private: |
90 | static void RegisterFlattenablesIfNeeded(); |
91 | static void Finalize(); |
92 | |
93 | friend class SkGraphics; |
94 | |
95 | typedef SkRefCnt INHERITED; |
96 | }; |
97 | |
98 | #define SK_REGISTER_FLATTENABLE(type) SkFlattenable::Register(#type, type::CreateProc) |
99 | |
100 | #define SK_FLATTENABLE_HOOKS(type) \ |
101 | static sk_sp<SkFlattenable> CreateProc(SkReadBuffer&); \ |
102 | friend class SkFlattenable::PrivateInitializer; \ |
103 | Factory getFactory() const override { return type::CreateProc; } \ |
104 | const char* getTypeName() const override { return #type; } |
105 | |
106 | #endif |
107 | |