Skia
2D Graphics Library
SkFlattenable.h
Go to the documentation of this file.
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 #include "include/core/SkTypes.h"
13 
14 #include <cstddef>
15 
16 class SkData;
17 class SkReadBuffer;
18 class SkWriteBuffer;
19 struct SkDeserialProcs;
20 struct SkSerialProcs;
21 
28 class SK_API SkFlattenable : public SkRefCnt {
29 public:
30  enum Type {
34  kSkDrawLooper_Type, // no longer used internally by Skia
39  };
40 
41  typedef sk_sp<SkFlattenable> (*Factory)(SkReadBuffer&);
42 
44 
49  virtual Factory getFactory() const = 0;
50 
54  virtual const char* getTypeName() const = 0;
55 
56  static Factory NameToFactory(const char name[]);
57  static const char* FactoryToName(Factory);
58 
59  static void Register(const char name[], Factory);
60 
67  virtual void flatten(SkWriteBuffer&) const {}
68 
69  virtual Type getFlattenableType() const = 0;
70 
71  //
72  // public ways to serialize / deserialize
73  //
74  sk_sp<SkData> serialize(const SkSerialProcs* = nullptr) const;
75  size_t serialize(void* memory, size_t memory_size,
76  const SkSerialProcs* = nullptr) const;
77  static sk_sp<SkFlattenable> Deserialize(Type, const void* data, size_t length,
78  const SkDeserialProcs* procs = nullptr);
79 
80 protected:
82  public:
83  static void InitEffects();
84  static void InitImageFilters();
85  };
86 
87 private:
88  static void RegisterFlattenablesIfNeeded();
89  static void Finalize();
90 
91  friend class SkGraphics;
92 
93  using INHERITED = SkRefCnt;
94 };
95 
96 #if defined(SK_DISABLE_EFFECT_DESERIALIZATION)
97  #define SK_REGISTER_FLATTENABLE(type) do{}while(false)
98 
99  #define SK_FLATTENABLE_HOOKS(type) \
100  static sk_sp<SkFlattenable> CreateProc(SkReadBuffer&); \
101  friend class SkFlattenable::PrivateInitializer; \
102  Factory getFactory() const override { return nullptr; } \
103  const char* getTypeName() const override { return #type; }
104 #else
105  #define SK_REGISTER_FLATTENABLE(type) \
106  SkFlattenable::Register(#type, type::CreateProc)
107 
108  #define SK_FLATTENABLE_HOOKS(type) \
109  static sk_sp<SkFlattenable> CreateProc(SkReadBuffer&); \
110  friend class SkFlattenable::PrivateInitializer; \
111  Factory getFactory() const override { return type::CreateProc; } \
112  const char* getTypeName() const override { return #type; }
113 #endif
114 
115 #endif
SkData holds an immutable data buffer.
Definition: SkData.h:25
Definition: SkFlattenable.h:81
SkFlattenable is the base class for objects that need to be flattened into a data stream for either t...
Definition: SkFlattenable.h:28
virtual void flatten(SkWriteBuffer &) const
Override this if your subclass needs to record data that it will need to recreate itself from its Cre...
Definition: SkFlattenable.h:67
static void Register(const char name[], Factory)
virtual const char * getTypeName() const =0
Returns the name of the object's class.
static Factory NameToFactory(const char name[])
sk_sp< SkData > serialize(const SkSerialProcs *=nullptr) const
static const char * FactoryToName(Factory)
virtual Type getFlattenableType() const =0
static sk_sp< SkFlattenable > Deserialize(Type, const void *data, size_t length, const SkDeserialProcs *procs=nullptr)
SkFlattenable()
Definition: SkFlattenable.h:43
Type
Definition: SkFlattenable.h:30
@ kSkDrawable_Type
Definition: SkFlattenable.h:33
@ kSkPathEffect_Type
Definition: SkFlattenable.h:37
@ kSkShader_Type
Definition: SkFlattenable.h:38
@ kSkMaskFilter_Type
Definition: SkFlattenable.h:36
@ kSkColorFilter_Type
Definition: SkFlattenable.h:31
@ kSkDrawLooper_Type
Definition: SkFlattenable.h:34
@ kSkBlender_Type
Definition: SkFlattenable.h:32
@ kSkImageFilter_Type
Definition: SkFlattenable.h:35
size_t serialize(void *memory, size_t memory_size, const SkSerialProcs *=nullptr) const
virtual Factory getFactory() const =0
Implement this to return a factory function pointer that can be called to recreate your class given a...
Definition: SkGraphics.h:23
Definition: SkRefCnt.h:119
Definition: SkSerialProcs.h:97
Definition: SkSerialProcs.h:86