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 
13 class SkData;
14 class SkReadBuffer;
15 class SkWriteBuffer;
16 
17 struct SkSerialProcs;
18 struct SkDeserialProcs;
19 
26 class SK_API SkFlattenable : public SkRefCnt {
27 public:
28  enum Type {
31  kSkDrawLooper_Type, // no longer used internally by Skia
36  kSkUnused_Type4, // used to be SkRasterizer
38  kSkUnused_Type, // used to be SkUnitMapper
40  kSkUnused_Type3, // use to be NormalSource,
41  };
42 
43  typedef sk_sp<SkFlattenable> (*Factory)(SkReadBuffer&);
44 
46 
51  virtual Factory getFactory() const = 0;
52 
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 
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:
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  using INHERITED = SkRefCnt;
96 };
97 
98 #if defined(SK_DISABLE_EFFECT_DESERIALIZATION)
99  #define SK_REGISTER_FLATTENABLE(type) do{}while(false)
100 
101  #define SK_FLATTENABLE_HOOKS(type) \
102  static sk_sp<SkFlattenable> CreateProc(SkReadBuffer&); \
103  friend class SkFlattenable::PrivateInitializer; \
104  Factory getFactory() const override { return nullptr; } \
105  const char* getTypeName() const override { return #type; }
106 #else
107  #define SK_REGISTER_FLATTENABLE(type) \
108  SkFlattenable::Register(#type, type::CreateProc)
109 
110  #define SK_FLATTENABLE_HOOKS(type) \
111  static sk_sp<SkFlattenable> CreateProc(SkReadBuffer&); \
112  friend class SkFlattenable::PrivateInitializer; \
113  Factory getFactory() const override { return type::CreateProc; } \
114  const char* getTypeName() const override { return #type; }
115 #endif
116 
117 #endif
SkFlattenable::getFactory
virtual Factory getFactory() const =0
Implement this to return a factory function pointer that can be called to recreate your class given a...
SkRefCnt
Definition: SkRefCnt.h:118
SkFlattenable::kSkUnused_Type
@ kSkUnused_Type
Definition: SkFlattenable.h:38
SkFlattenable::kSkPixelRef_Type
@ kSkPixelRef_Type
Definition: SkFlattenable.h:35
SkFlattenable::Deserialize
static sk_sp< SkFlattenable > Deserialize(Type, const void *data, size_t length, const SkDeserialProcs *procs=nullptr)
SkFlattenable::Type
Type
Definition: SkFlattenable.h:28
SkData
SkData holds an immutable data buffer.
Definition: SkData.h:22
SkFlattenable::PrivateInitializer
Definition: SkFlattenable.h:83
SkFlattenable::serialize
size_t serialize(void *memory, size_t memory_size, const SkSerialProcs *=nullptr) const
SkRefCnt.h
SkFlattenable::kSkPathEffect_Type
@ kSkPathEffect_Type
Definition: SkFlattenable.h:34
SkFlattenable::kSkMaskFilter_Type
@ kSkMaskFilter_Type
Definition: SkFlattenable.h:33
sk_sp
Shared pointer class to wrap classes that support a ref()/unref() interface.
Definition: SkRefCnt.h:215
SkGraphics
Definition: SkGraphics.h:17
SkFlattenable::kSkImageFilter_Type
@ kSkImageFilter_Type
Definition: SkFlattenable.h:32
SkFlattenable::SkFlattenable
SkFlattenable()
Definition: SkFlattenable.h:45
SkFlattenable::serialize
sk_sp< SkData > serialize(const SkSerialProcs *=nullptr) const
SK_API
#define SK_API
Definition: SkTypes.h:181
SkFlattenable::kSkDrawLooper_Type
@ kSkDrawLooper_Type
Definition: SkFlattenable.h:31
SkFlattenable::FactoryToName
static const char * FactoryToName(Factory)
SkFlattenable::kSkDrawable_Type
@ kSkDrawable_Type
Definition: SkFlattenable.h:30
SkFlattenable::kSkUnused_Type2
@ kSkUnused_Type2
Definition: SkFlattenable.h:39
SkFlattenable::kSkColorFilter_Type
@ kSkColorFilter_Type
Definition: SkFlattenable.h:29
SkFlattenable::PrivateInitializer::InitImageFilters
static void InitImageFilters()
SkFlattenable::getTypeName
virtual const char * getTypeName() const =0
Returns the name of the object's class.
SkFlattenable::Register
static void Register(const char name[], Factory)
SkFlattenable::kSkShaderBase_Type
@ kSkShaderBase_Type
Definition: SkFlattenable.h:37
SkFlattenable::getFlattenableType
virtual Type getFlattenableType() const =0
SkSerialProcs
Definition: SkSerialProcs.h:50
SkFlattenable::PrivateInitializer::InitEffects
static void InitEffects()
SkFlattenable::flatten
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:69
SkDeserialProcs
Definition: SkSerialProcs.h:61
SkFlattenable::NameToFactory
static Factory NameToFactory(const char name[])
SkFlattenable::kSkUnused_Type4
@ kSkUnused_Type4
Definition: SkFlattenable.h:36
SkFlattenable
Definition: SkFlattenable.h:26
SkFlattenable::kSkUnused_Type3
@ kSkUnused_Type3
Definition: SkFlattenable.h:40