Debian Codegraph @

include/core/SkData.h

raw
1 /*
2 * Copyright 2011 Google Inc.
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 SkData_DEFINED
9 #define SkData_DEFINED
10
11 #include "include/core/SkRefCnt.h"
12 #include "include/core/SkSpan.h"
13 #include "include/private/base/SkAPI.h"
14
15 #include <cstddef>
16 #include <cstdint>
17 #include <cstdio>
18
19 class SkStream;
20
21 /**
22 * SkData holds a data buffer. It can be created to allocate its own buffer
23 * for the contents, or to share a pointer to the client's buffer. The size and
24 * address of the contents never change for the lifetime of the data object.
25 */
26 class SK_API SkData final : public SkNVRefCnt<SkData> {
27 public:
28 /**
29 * Returns true if this and rhs are the same size, and contain the same contents.
30 * All empty objects compare as equal.
31 */
32 bool operator==(const SkData& rhs) const;
33 bool operator!=(const SkData& rhs) const { return !(*this == rhs); }
34
35 /**
36 * Calls == operator, but first checks if other is null (in which case it returns false)
37 */
38 bool equals(const SkData* other) const {
39 return (other != nullptr) && *this == *other;
40 }
41
42 /**
43 * Return true if both arguments are the same size and contain the same bytes, or if both
44 * arguments are null.
45 */
46 static bool Equals(const SkData* a, const SkData* b) {
47 return (a == nullptr) ? (b == nullptr) : a->equals(b);
48 }
49
50 /**
51 * Returns the number of bytes stored.
52 */
53 size_t size() const { return fSpan.size(); }
54
55 /**
56 * Returns the ptr to the data.
57 */
58 const void* data() const { return fSpan.data(); }
59
60 bool empty() const { return fSpan.empty(); }
61
62 const uint8_t* bytes() const { return reinterpret_cast<const uint8_t*>(this->data()); }
63
64 SkSpan<const uint8_t> byteSpan() const { return {this->bytes(), this->size()}; }
65
66 /**
67 * USE WITH CAUTION.
68 * Be sure other 'owners' of this object are not accessing it in aother thread.
69 */
70 void* writable_data() {
71 return fSpan.data();
72 }
73
74 /** Attempt to create a deep copy of the original data, using the default allocator.
75 *
76 * If offset+length > this->size(), then this returns nullptr.
77 */
78 sk_sp<SkData> copySubset(size_t offset, size_t length) const;
79
80 /** Attempt to return a data that is a reference to a subset of the original data,
81 * This will never make a deep copy of the contents, but will retain a reference
82 * to the original data object.
83 *
84 * If offset+length > this->size(), then this returns nullptr.
85 */
86 sk_sp<SkData> shareSubset(size_t offset, size_t length);
87 sk_sp<const SkData> shareSubset(size_t offset, size_t length) const;
88
89 /**
90 * Helper to copy a range of the data into a caller-provided buffer.
91 * Returns the actual number of bytes copied, after clamping offset and
92 * length to the size of this data. If buffer is NULL, it is ignored, and
93 * only the computed number of bytes is returned.
94 */
95 size_t copyRange(size_t offset, size_t length, void* buffer) const;
96
97 /**
98 * Function that, if provided, will be called when the SkData goes out
99 * of scope, allowing for custom allocation/freeing of the data's contents.
100 */
101 typedef void (*ReleaseProc)(const void* ptr, void* context);
102
103 /**
104 * Create a new dataref by copying the specified data
105 */
106 static sk_sp<SkData> MakeWithCopy(const void* data, size_t length);
107
108
109 /**
110 * Create a new data with uninitialized contents. The caller should call writable_data()
111 * to write into the buffer, but this must be done before another ref() is made.
112 */
113 static sk_sp<SkData> MakeUninitialized(size_t length);
114
115 /**
116 * Create a new data with zero-initialized contents. The caller should call writable_data()
117 * to write into the buffer, but this must be done before another ref() is made.
118 */
119 static sk_sp<SkData> MakeZeroInitialized(size_t length);
120
121 /**
122 * Create a new dataref by copying the specified c-string
123 * (a null-terminated array of bytes). The returned SkData will have size()
124 * equal to strlen(cstr) + 1. If cstr is NULL, it will be treated the same
125 * as "".
126 */
127 static sk_sp<SkData> MakeWithCString(const char cstr[]);
128
129 /**
130 * Create a new dataref, taking the ptr as is, and using the
131 * releaseproc to free it. The proc may be NULL.
132 */
133 static sk_sp<SkData> MakeWithProc(const void* ptr, size_t length, ReleaseProc proc, void* ctx);
134
135 /**
136 * Call this when the data parameter is already const and will outlive the lifetime of the
137 * SkData. Suitable for globals.
138 */
139 static sk_sp<SkData> MakeWithoutCopy(const void* data, size_t length) {
140 return MakeWithProc(data, length, NoopReleaseProc, nullptr);
141 }
142
143 /**
144 * Create a new dataref from a pointer allocated by malloc. The Data object
145 * takes ownership of that allocation, and will handling calling sk_free.
146 */
147 static sk_sp<SkData> MakeFromMalloc(const void* data, size_t length);
148
149 /**
150 * Create a new dataref the file with the specified path.
151 * If the file cannot be opened, this returns NULL.
152 */
153 static sk_sp<SkData> MakeFromFileName(const char path[]);
154
155 /**
156 * Create a new dataref from a stdio FILE.
157 * This does not take ownership of the FILE, nor close it.
158 * The caller is free to close the FILE at its convenience.
159 * The FILE must be open for reading only.
160 * Returns NULL on failure.
161 */
162 static sk_sp<SkData> MakeFromFILE(FILE* f);
163
164 /**
165 * Create a new dataref from a file descriptor.
166 * This does not take ownership of the file descriptor, nor close it.
167 * The caller is free to close the file descriptor at its convenience.
168 * The file descriptor must be open for reading only.
169 * Returns NULL on failure.
170 */
171 static sk_sp<SkData> MakeFromFD(int fd);
172
173 /**
174 * Attempt to read size bytes into a SkData. If the read succeeds, return the data,
175 * else return NULL. Either way the stream's cursor may have been changed as a result
176 * of calling read().
177 */
178 static sk_sp<SkData> MakeFromStream(SkStream*, size_t size);
179
180 /**
181 * DEPRECATED variant of src->shareSubset(offset, length)
182 *
183 * This variant checks if shaerSubset() returned null (because offset or length were out-of-range)
184 * and returns an empty SkData, rather than returning null.
185 */
186 static sk_sp<SkData> MakeSubset(const SkData* src, size_t offset, size_t length) {
187 if (sk_sp<SkData> dst = const_cast<SkData*>(src)->shareSubset(offset, length)) {
188 return dst;
189 }
190 return SkData::MakeEmpty();
191 }
192
193 /**
194 * Returns a new empty dataref (or a reference to a shared empty dataref).
195 * New or shared, the caller must see that unref() is eventually called.
196 */
197 static sk_sp<SkData> MakeEmpty();
198
199 /**
200 * DEPRECATED -- use empty()
201 */
202 bool isEmpty() const { return fSpan.empty(); }
203
204 private:
205 friend class SkNVRefCnt<SkData>;
206 ReleaseProc fReleaseProc;
207 void* fReleaseProcContext;
208 SkSpan<std::byte> fSpan;
209
210 SkData(SkSpan<std::byte>, ReleaseProc, void* context);
211 explicit SkData(size_t size); // inplace new/delete
212 ~SkData();
213
214 // Ensure the unsized delete is called.
215 void operator delete(void* p);
216
217 // shared internal factory
218 static sk_sp<SkData> PrivateNewWithCopy(const void* srcOrNull, size_t length);
219
220 static void NoopReleaseProc(const void*, void*); // {}
221
222 using INHERITED = SkRefCnt;
223 };
224
225 #endif