• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         kSkBlender_Type,
31         kSkDrawable_Type,
32         kSkDrawLooper_Type,  // no longer used internally by Skia
33         kSkImageFilter_Type,
34         kSkMaskFilter_Type,
35         kSkPathEffect_Type,
36         kSkShader_Type,
37     };
38 
39     typedef sk_sp<SkFlattenable> (*Factory)(SkReadBuffer&);
40 
SkFlattenable()41     SkFlattenable() {}
42 
43     /** Implement this to return a factory function pointer that can be called
44      to recreate your class given a buffer (previously written to by your
45      override of flatten().
46      */
47     virtual Factory getFactory() const = 0;
48 
49     /**
50      *  Returns the name of the object's class.
51      */
52     virtual const char* getTypeName() const = 0;
53 
54     static Factory NameToFactory(const char name[]);
55     static const char* FactoryToName(Factory);
56 
57     static void Register(const char name[], Factory);
58 
59     /**
60      *  Override this if your subclass needs to record data that it will need to recreate itself
61      *  from its CreateProc (returned by getFactory()).
62      *
63      *  DEPRECATED public : will move to protected ... use serialize() instead
64      */
flatten(SkWriteBuffer &)65     virtual void flatten(SkWriteBuffer&) const {}
66 
67     virtual Type getFlattenableType() const = 0;
68 
69     //
70     // public ways to serialize / deserialize
71     //
72     sk_sp<SkData> serialize(const SkSerialProcs* = nullptr) const;
73     size_t serialize(void* memory, size_t memory_size,
74                      const SkSerialProcs* = nullptr) const;
75     static sk_sp<SkFlattenable> Deserialize(Type, const void* data, size_t length,
76                                             const SkDeserialProcs* procs = nullptr);
77 
78 protected:
79     class PrivateInitializer {
80     public:
81         static void InitEffects();
82         static void InitImageFilters();
83     };
84 
85 private:
86     static void RegisterFlattenablesIfNeeded();
87     static void Finalize();
88 
89     friend class SkGraphics;
90 
91     using INHERITED = SkRefCnt;
92 };
93 
94 #if defined(SK_DISABLE_EFFECT_DESERIALIZATION)
95     #define SK_REGISTER_FLATTENABLE(type) do{}while(false)
96 
97     #define SK_FLATTENABLE_HOOKS(type)                                   \
98         static sk_sp<SkFlattenable> CreateProc(SkReadBuffer&);           \
99         friend class SkFlattenable::PrivateInitializer;                  \
100         Factory getFactory() const override { return nullptr; }          \
101         const char* getTypeName() const override { return #type; }
102 #else
103     #define SK_REGISTER_FLATTENABLE(type)                                \
104         SkFlattenable::Register(#type, type::CreateProc)
105 
106     #define SK_FLATTENABLE_HOOKS(type)                                   \
107         static sk_sp<SkFlattenable> CreateProc(SkReadBuffer&);           \
108         friend class SkFlattenable::PrivateInitializer;                  \
109         Factory getFactory() const override { return type::CreateProc; } \
110         const char* getTypeName() const override { return #type; }
111 #endif
112 
113 #endif
114