Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkDescriptor.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 SkDescriptor_DEFINED
9#define SkDescriptor_DEFINED
10
15
16#include <cstddef>
17#include <cstdint>
18#include <memory>
19#include <optional>
20
21class SkReadBuffer;
22class SkWriteBuffer;
23
25public:
26 static size_t ComputeOverhead(int entryCount) {
27 SkASSERT(entryCount >= 0);
28 return sizeof(SkDescriptor) + entryCount * sizeof(Entry);
29 }
30
31 static std::unique_ptr<SkDescriptor> Alloc(size_t length);
32
33 //
34 // Ensure the unsized delete is called.
35 void operator delete(void* p);
36 void* operator new(size_t);
37 void* operator new(size_t, void* p) { return p; }
38
39 void flatten(SkWriteBuffer& buffer) const;
40
41 uint32_t getLength() const { return fLength; }
42 void* addEntry(uint32_t tag, size_t length, const void* data = nullptr);
43 void computeChecksum();
44
45 // Assumes that getLength <= capacity of this SkDescriptor.
46 bool isValid() const;
47
48#ifdef SK_DEBUG
49 void assertChecksum() const {
50 SkASSERT(SkDescriptor::ComputeChecksum(this) == fChecksum);
51 }
52#endif
53
54 const void* findEntry(uint32_t tag, uint32_t* length) const;
55
56 std::unique_ptr<SkDescriptor> copy() const;
57
58 // This assumes that all memory added has a length that is a multiple of 4. This is checked
59 // by the assert in addEntry.
60 bool operator==(const SkDescriptor& other) const;
61 bool operator!=(const SkDescriptor& other) const { return !(*this == other); }
62
63 uint32_t getChecksum() const { return fChecksum; }
64
65 struct Entry {
66 uint32_t fTag;
67 uint32_t fLen;
68 };
69
70 uint32_t getCount() const { return fCount; }
71
72 SkString dumpRec() const;
73
74private:
75 SkDescriptor() = default;
77 friend class SkAutoDescriptor;
78
79 static uint32_t ComputeChecksum(const SkDescriptor* desc);
80
81 uint32_t fChecksum{0}; // must be first
82 uint32_t fLength{sizeof(SkDescriptor)}; // must be second
83 uint32_t fCount{0};
84};
85
87public:
89 explicit SkAutoDescriptor(size_t size);
90 explicit SkAutoDescriptor(const SkDescriptor&);
96
97 // Returns no value if there is an error.
98 static std::optional<SkAutoDescriptor> MakeFromBuffer(SkReadBuffer& buffer);
99
100 void reset(size_t size);
101 void reset(const SkDescriptor& desc);
102 SkDescriptor* getDesc() const { SkASSERT(fDesc); return fDesc; }
103
104private:
105 void free();
106 static constexpr size_t kStorageSize
107 = sizeof(SkDescriptor)
108 + sizeof(SkDescriptor::Entry) + sizeof(SkScalerContextRec) // for rec
109 + sizeof(SkDescriptor::Entry) + sizeof(void*) // for typeface
110 + 32; // slop for occasional small extras
111
112 SkDescriptor* fDesc{nullptr};
113 alignas(uint32_t) char fStorage[kStorageSize];
114};
115
116#endif //SkDescriptor_DEFINED
m reset()
#define SkASSERT(cond)
Definition SkAssert.h:116
SkAutoDescriptor & operator=(const SkAutoDescriptor &)
SkDescriptor * getDesc() const
static std::optional< SkAutoDescriptor > MakeFromBuffer(SkReadBuffer &buffer)
void * addEntry(uint32_t tag, size_t length, const void *data=nullptr)
void flatten(SkWriteBuffer &buffer) const
static std::unique_ptr< SkDescriptor > Alloc(size_t length)
std::unique_ptr< SkDescriptor > copy() const
uint32_t getChecksum() const
void computeChecksum()
uint32_t getLength() const
SkString dumpRec() const
static size_t ComputeOverhead(int entryCount)
bool operator!=(const SkDescriptor &other) const
bool isValid() const
const void * findEntry(uint32_t tag, uint32_t *length) const
bool operator==(const SkDescriptor &other) const
uint32_t getCount() const
static const uint8_t buffer[]
size_t length