xref: /aosp_15_r20/external/skia/include/core/SkFlattenable.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 
22 /** \class SkFlattenable
23 
24  SkFlattenable is the base class for objects that need to be flattened
25  into a data stream for either transport or as part of the key to the
26  font cache.
27  */
28 class SK_API SkFlattenable : public SkRefCnt {
29 public:
30     enum Type {
31         kSkColorFilter_Type,
32         kSkBlender_Type,
33         kSkDrawable_Type,
34         kSkDrawLooper_Type,  // no longer supported by Skia
35         kSkImageFilter_Type,
36         kSkMaskFilter_Type,
37         kSkPathEffect_Type,
38         kSkShader_Type,
39     };
40 
41     typedef sk_sp<SkFlattenable> (*Factory)(SkReadBuffer&);
42 
SkFlattenable()43     SkFlattenable() {}
44 
45     /** Implement this to return a factory function pointer that can be called
46      to recreate your class given a buffer (previously written to by your
47      override of flatten().
48      */
49     virtual Factory getFactory() const = 0;
50 
51     /**
52      *  Returns the name of the object's class.
53      */
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 
61     /**
62      *  Override this if your subclass needs to record data that it will need to recreate itself
63      *  from its CreateProc (returned by getFactory()).
64      *
65      *  DEPRECATED public : will move to protected ... use serialize() instead
66      */
flatten(SkWriteBuffer &)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:
81     class PrivateInitializer {
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
116