Debian Codegraph @

include/core/SkStream.h

raw
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 SkStream_DEFINED
9 #define SkStream_DEFINED
10
11 #include "include/core/SkData.h"
12 #include "include/core/SkRefCnt.h"
13 #include "include/core/SkScalar.h"
14 #include "include/core/SkTypes.h"
15 #include "include/private/base/SkCPUTypes.h"
16 #include "include/private/base/SkTo.h"
17
18 #include <cstdint>
19 #include <cstdio>
20 #include <cstring>
21 #include <memory>
22 #include <utility>
23 #include <vector>
24
25 class SkStreamAsset;
26
27 /**
28 * SkStream -- abstraction for a source of bytes. Subclasses can be backed by
29 * memory, or a file, or something else.
30 */
31 class SK_API SkStream {
32 public:
33 virtual ~SkStream() {}
34 SkStream() {}
35
36 /**
37 * Attempts to open the specified file as a stream, returns nullptr on failure.
38 */
39 static std::unique_ptr<SkStreamAsset> MakeFromFile(const char path[]);
40
41 /** Reads or skips size number of bytes.
42 * If buffer == NULL, skip size bytes, return how many were skipped.
43 * If buffer != NULL, copy size bytes into buffer, return how many were copied.
44 * @param buffer when NULL skip size bytes, otherwise copy size bytes into buffer
45 * @param size the number of bytes to skip or copy
46 * @return the number of bytes actually read.
47 */
48 virtual size_t read(void* buffer, size_t size) = 0;
49
50 /** Skip size number of bytes.
51 * @return the actual number bytes that could be skipped.
52 */
53 size_t skip(size_t size) {
54 return this->read(nullptr, size);
55 }
56
57 /**
58 * Attempt to peek at size bytes.
59 * If this stream supports peeking, copy min(size, peekable bytes) into
60 * buffer, and return the number of bytes copied.
61 * If the stream does not support peeking, or cannot peek any bytes,
62 * return 0 and leave buffer unchanged.
63 * The stream is guaranteed to be in the same visible state after this
64 * call, regardless of success or failure.
65 * @param buffer Must not be NULL, and must be at least size bytes. Destination
66 * to copy bytes.
67 * @param size Number of bytes to copy.
68 * @return The number of bytes peeked/copied.
69 */
70 virtual size_t peek(void* /*buffer*/, size_t /*size*/) const { return 0; }
71
72 /** Returns true when all the bytes in the stream have been read.
73 * As SkStream represents synchronous I/O, isAtEnd returns false when the
74 * final stream length isn't known yet, even when all the bytes available
75 * so far have been read.
76 * This may return true early (when there are no more bytes to be read)
77 * or late (after the first unsuccessful read).
78 */
79 virtual bool isAtEnd() const = 0;
80
81 [[nodiscard]] bool readS8(int8_t*);
82 [[nodiscard]] bool readS16(int16_t*);
83 [[nodiscard]] bool readS32(int32_t*);
84 [[nodiscard]] bool readS64(int64_t*);
85
86 [[nodiscard]] bool readU8(uint8_t* i) { return this->readS8((int8_t*)i); }
87 [[nodiscard]] bool readU16(uint16_t* i) { return this->readS16((int16_t*)i); }
88 [[nodiscard]] bool readU32(uint32_t* i) { return this->readS32((int32_t*)i); }
89 [[nodiscard]] bool readU64(uint64_t* i) { return this->readS64((int64_t*)i); }
90
91 [[nodiscard]] bool readBool(bool* b) {
92 uint8_t i;
93 if (!this->readU8(&i)) { return false; }
94 *b = (i != 0);
95 return true;
96 }
97 [[nodiscard]] bool readScalar(SkScalar*);
98 [[nodiscard]] bool readPackedUInt(size_t*);
99
100 //SkStreamRewindable
101 /** Rewinds to the beginning of the stream. Returns true if the stream is known
102 * to be at the beginning after this call returns.
103 */
104 virtual bool rewind() { return false; }
105
106 /** Duplicates this stream. If this cannot be done, returns NULL.
107 * The returned stream will be positioned at the beginning of its data.
108 */
109 std::unique_ptr<SkStream> duplicate() const {
110 return std::unique_ptr<SkStream>(this->onDuplicate());
111 }
112 /** Duplicates this stream. If this cannot be done, returns NULL.
113 * The returned stream will be positioned the same as this stream.
114 */
115 std::unique_ptr<SkStream> fork() const {
116 return std::unique_ptr<SkStream>(this->onFork());
117 }
118
119 //SkStreamSeekable
120 /** Returns true if this stream can report its current position. */
121 virtual bool hasPosition() const { return false; }
122 /** Returns the current position in the stream. If this cannot be done, returns 0. */
123 virtual size_t getPosition() const { return 0; }
124
125 /** Seeks to an absolute position in the stream. If this cannot be done, returns false.
126 * If an attempt is made to seek past the end of the stream, the position will be set
127 * to the end of the stream.
128 */
129 virtual bool seek(size_t /*position*/) { return false; }
130
131 /** Seeks to an relative offset in the stream. If this cannot be done, returns false.
132 * If an attempt is made to move to a position outside the stream, the position will be set
133 * to the closest point within the stream (beginning or end).
134 */
135 virtual bool move(long /*offset*/) { return false; }
136
137 //SkStreamAsset
138 /** Returns true if this stream can report its total length. */
139 virtual bool hasLength() const { return false; }
140 /** Returns the total length of the stream. If this cannot be done, returns 0. */
141 virtual size_t getLength() const { return 0; }
142
143 //SkStreamMemory
144 /** Returns the starting address for the data. If this cannot be done, returns NULL. */
145 virtual const void* getMemoryBase() { return nullptr; }
146 virtual sk_sp<const SkData> getData() const { return nullptr; }
147
148 private:
149 virtual SkStream* onDuplicate() const { return nullptr; }
150 virtual SkStream* onFork() const { return nullptr; }
151
152 SkStream(SkStream&&) = delete;
153 SkStream(const SkStream&) = delete;
154 SkStream& operator=(SkStream&&) = delete;
155 SkStream& operator=(const SkStream&) = delete;
156 };
157
158 /** SkStreamRewindable is a SkStream for which rewind and duplicate are required. */
159 class SK_API SkStreamRewindable : public SkStream {
160 public:
161 bool rewind() override = 0;
162 std::unique_ptr<SkStreamRewindable> duplicate() const {
163 return std::unique_ptr<SkStreamRewindable>(this->onDuplicate());
164 }
165 private:
166 SkStreamRewindable* onDuplicate() const override = 0;
167 };
168
169 /** SkStreamSeekable is a SkStreamRewindable for which position, seek, move, and fork are required. */
170 class SK_API SkStreamSeekable : public SkStreamRewindable {
171 public:
172 std::unique_ptr<SkStreamSeekable> duplicate() const {
173 return std::unique_ptr<SkStreamSeekable>(this->onDuplicate());
174 }
175
176 bool hasPosition() const override { return true; }
177 size_t getPosition() const override = 0;
178 bool seek(size_t position) override = 0;
179 bool move(long offset) override = 0;
180
181 std::unique_ptr<SkStreamSeekable> fork() const {
182 return std::unique_ptr<SkStreamSeekable>(this->onFork());
183 }
184 private:
185 SkStreamSeekable* onDuplicate() const override = 0;
186 SkStreamSeekable* onFork() const override = 0;
187 };
188
189 /** SkStreamAsset is a SkStreamSeekable for which getLength is required. */
190 class SK_API SkStreamAsset : public SkStreamSeekable {
191 public:
192 bool hasLength() const override { return true; }
193 size_t getLength() const override = 0;
194
195 std::unique_ptr<SkStreamAsset> duplicate() const {
196 return std::unique_ptr<SkStreamAsset>(this->onDuplicate());
197 }
198 std::unique_ptr<SkStreamAsset> fork() const {
199 return std::unique_ptr<SkStreamAsset>(this->onFork());
200 }
201 private:
202 SkStreamAsset* onDuplicate() const override = 0;
203 SkStreamAsset* onFork() const override = 0;
204 };
205
206 /** SkStreamMemory is a SkStreamAsset for which getMemoryBase is required. */
207 class SK_API SkStreamMemory : public SkStreamAsset {
208 public:
209 const void* getMemoryBase() override = 0;
210
211 std::unique_ptr<SkStreamMemory> duplicate() const {
212 return std::unique_ptr<SkStreamMemory>(this->onDuplicate());
213 }
214 std::unique_ptr<SkStreamMemory> fork() const {
215 return std::unique_ptr<SkStreamMemory>(this->onFork());
216 }
217 private:
218 SkStreamMemory* onDuplicate() const override = 0;
219 SkStreamMemory* onFork() const override = 0;
220 };
221
222 class SK_API SkWStream {
223 public:
224 virtual ~SkWStream();
225 SkWStream() {}
226
227 /** Called to write bytes to a SkWStream. Returns true on success
228 @param buffer the address of at least size bytes to be written to the stream
229 @param size The number of bytes in buffer to write to the stream
230 @return true on success
231 */
232 virtual bool write(const void* buffer, size_t size) = 0;
233 virtual void flush();
234
235 virtual size_t bytesWritten() const = 0;
236
237 // helpers
238
239 bool write8(U8CPU value) {
240 uint8_t v = SkToU8(value);
241 return this->write(&v, 1);
242 }
243 bool write16(U16CPU value) {
244 uint16_t v = SkToU16(value);
245 return this->write(&v, 2);
246 }
247 bool write32(uint32_t value) {
248 return this->write(&value, 4);
249 }
250 bool write64(uint64_t value) {
251 return this->write(&value, 8);
252 }
253
254 bool writeText(const char text[]) {
255 SkASSERT(text);
256 return this->write(text, std::strlen(text));
257 }
258
259 bool newline() { return this->write("\n", std::strlen("\n")); }
260
261 bool writeDecAsText(int32_t);
262 bool writeBigDecAsText(int64_t, int minDigits = 0);
263 bool writeHexAsText(uint32_t, int minDigits = 0);
264 bool writeScalarAsText(SkScalar);
265
266 bool writeBool(bool v) { return this->write8(v); }
267 bool writeScalar(SkScalar);
268 bool writePackedUInt(size_t);
269
270 bool writeStream(SkStream* input, size_t length);
271
272 /**
273 * This returns the number of bytes in the stream required to store
274 * 'value'.
275 */
276 static int SizeOfPackedUInt(size_t value);
277
278 private:
279 SkWStream(const SkWStream&) = delete;
280 SkWStream& operator=(const SkWStream&) = delete;
281 };
282
283 class SK_API SkNullWStream : public SkWStream {
284 public:
285 SkNullWStream() : fBytesWritten(0) {}
286
287 bool write(const void* , size_t n) override { fBytesWritten += n; return true; }
288 void flush() override {}
289 size_t bytesWritten() const override { return fBytesWritten; }
290
291 private:
292 size_t fBytesWritten;
293 };
294
295 ////////////////////////////////////////////////////////////////////////////////////////
296
297 /** A stream that wraps a C FILE* file stream. */
298 class SK_API SkFILEStream : public SkStreamAsset {
299 public:
300 /** Initialize the stream by calling sk_fopen on the specified path.
301 * This internal stream will be closed in the destructor.
302 */
303 explicit SkFILEStream(const char path[] = nullptr);
304
305 /** Initialize the stream with an existing C FILE stream.
306 * The current position of the C FILE stream will be considered the
307 * beginning of the SkFILEStream and the current seek end of the FILE will be the end.
308 * The C FILE stream will be closed in the destructor.
309 */
310 explicit SkFILEStream(FILE* file);
311
312 /** Initialize the stream with an existing C FILE stream.
313 * The current position of the C FILE stream will be considered the
314 * beginning of the SkFILEStream and size bytes later will be the end.
315 * The C FILE stream will be closed in the destructor.
316 */
317 explicit SkFILEStream(FILE* file, size_t size);
318
319 ~SkFILEStream() override;
320
321 static std::unique_ptr<SkFILEStream> Make(const char path[]) {
322 std::unique_ptr<SkFILEStream> stream(new SkFILEStream(path));
323 return stream->isValid() ? std::move(stream) : nullptr;
324 }
325
326 /** Returns true if the current path could be opened. */
327 bool isValid() const { return fFILE != nullptr; }
328
329 /** Close this SkFILEStream. */
330 void close();
331
332 size_t read(void* buffer, size_t size) override;
333 bool isAtEnd() const override;
334
335 bool rewind() override;
336 std::unique_ptr<SkStreamAsset> duplicate() const {
337 return std::unique_ptr<SkStreamAsset>(this->onDuplicate());
338 }
339
340 size_t getPosition() const override;
341 bool seek(size_t position) override;
342 bool move(long offset) override;
343
344 std::unique_ptr<SkStreamAsset> fork() const {
345 return std::unique_ptr<SkStreamAsset>(this->onFork());
346 }
347
348 size_t getLength() const override;
349
350 private:
351 explicit SkFILEStream(FILE*, size_t size, size_t start);
352 explicit SkFILEStream(std::shared_ptr<FILE>, size_t end, size_t start);
353 explicit SkFILEStream(std::shared_ptr<FILE>, size_t end, size_t start, size_t current);
354
355 SkStreamAsset* onDuplicate() const override;
356 SkStreamAsset* onFork() const override;
357
358 std::shared_ptr<FILE> fFILE;
359 // My own council will I keep on sizes and offsets.
360 // These are seek positions in the underling FILE, not offsets into the stream.
361 size_t fEnd;
362 size_t fStart;
363 size_t fCurrent;
364
365 using INHERITED = SkStreamAsset;
366 };
367
368 // A read only view into a block of memory.
369 class SK_API SkMemoryStream : public SkStreamMemory {
370 public:
371 SkMemoryStream();
372
373 /** We allocate (and free) the memory. Write to it via getMemoryBase() */
374 explicit SkMemoryStream(size_t length);
375
376 /** If copyData is true, the stream makes a private copy of the data. */
377 SkMemoryStream(const void* data, size_t length, bool copyData = false);
378
379 /** Creates the stream to read from the specified data */
380 explicit SkMemoryStream(sk_sp<const SkData> data);
381
382 /** Returns a stream with a copy of the input data. */
383 static std::unique_ptr<SkMemoryStream> MakeCopy(const void* data, size_t length);
384
385 /** Returns a stream with a bare pointer reference to the input data. */
386 static std::unique_ptr<SkMemoryStream> MakeDirect(const void* data, size_t length);
387
388 /** Returns a stream with a shared reference to the input data. */
389 static std::unique_ptr<SkMemoryStream> Make(sk_sp<const SkData> data);
390
391 /** Resets the stream to the specified data and length,
392 just like the constructor.
393 if copyData is true, the stream makes a private copy of the data
394 */
395 virtual void setMemory(const void* data, size_t length,
396 bool copyData = false);
397 /** Replace any memory buffer with the specified buffer. The caller
398 must have allocated data with sk_malloc or sk_realloc, since it
399 will be freed with sk_free.
400 */
401 void setMemoryOwned(const void* data, size_t length);
402
403 sk_sp<const SkData> getData() const override { return fData; }
404
405 void setData(sk_sp<const SkData> data);
406
407 const void* getAtPos();
408
409 size_t read(void* buffer, size_t size) override;
410 bool isAtEnd() const override;
411
412 size_t peek(void* buffer, size_t size) const override;
413
414 bool rewind() override;
415
416 std::unique_ptr<SkMemoryStream> duplicate() const {
417 return std::unique_ptr<SkMemoryStream>(this->onDuplicate());
418 }
419
420 size_t getPosition() const override;
421 bool seek(size_t position) override;
422 bool move(long offset) override;
423
424 std::unique_ptr<SkMemoryStream> fork() const {
425 return std::unique_ptr<SkMemoryStream>(this->onFork());
426 }
427
428 size_t getLength() const override;
429
430 const void* getMemoryBase() override;
431
432 private:
433 SkMemoryStream* onDuplicate() const override;
434 SkMemoryStream* onFork() const override;
435
436 sk_sp<const SkData> fData;
437 size_t fOffset;
438
439 using INHERITED = SkStreamMemory;
440 };
441
442 /////////////////////////////////////////////////////////////////////////////////////////////
443
444 class SK_API SkFILEWStream : public SkWStream {
445 public:
446 explicit SkFILEWStream(const char path[]);
447 ~SkFILEWStream() override;
448
449 /** Returns true if the current path could be opened.
450 */
451 bool isValid() const { return fFILE != nullptr; }
452
453 bool write(const void* buffer, size_t size) override;
454 void flush() override;
455 void fsync();
456 size_t bytesWritten() const override;
457
458 private:
459 FILE* fFILE;
460
461 using INHERITED = SkWStream;
462 };
463
464 class SK_API SkDynamicMemoryWStream : public SkWStream {
465 public:
466 SkDynamicMemoryWStream() = default;
467 SkDynamicMemoryWStream(SkDynamicMemoryWStream&&);
468 SkDynamicMemoryWStream& operator=(SkDynamicMemoryWStream&&);
469 ~SkDynamicMemoryWStream() override;
470
471 bool write(const void* buffer, size_t size) override;
472 size_t bytesWritten() const override;
473
474 bool read(void* buffer, size_t offset, size_t size);
475
476 /** More efficient version of read(dst, 0, bytesWritten()). */
477 void copyTo(void* dst) const;
478 bool writeToStream(SkWStream* dst) const;
479
480 /** Equivalent to copyTo() followed by reset(), but may save memory use. */
481 void copyToAndReset(void* dst);
482
483 /** Equivalent to writeToStream() followed by reset(), but may save memory use. */
484 bool writeToAndReset(SkWStream* dst);
485
486 /** Equivalent to writeToStream() followed by reset(), but may save memory use.
487 When the dst is also a SkDynamicMemoryWStream, the implementation is constant time. */
488 bool writeToAndReset(SkDynamicMemoryWStream* dst);
489
490 /** Prepend this stream to dst, resetting this. */
491 void prependToAndReset(SkDynamicMemoryWStream* dst);
492
493 /** Return the contents as SkData, and then reset the stream. */
494 sk_sp<SkData> detachAsData();
495
496 /** Return the contents as vector, and then reset the stream. */
497 std::vector<uint8_t> detachAsVector();
498
499 /** Reset, returning a reader stream with the current content. */
500 std::unique_ptr<SkStreamAsset> detachAsStream();
501
502 /** Reset the stream to its original, empty, state. */
503 void reset();
504 void padToAlign4();
505 private:
506 struct Block;
507 Block* fHead = nullptr;
508 Block* fTail = nullptr;
509 size_t fBytesWrittenBeforeTail = 0;
510
511 #ifdef SK_DEBUG
512 void validate() const;
513 #else
514 void validate() const {}
515 #endif
516
517 // For access to the Block type.
518 friend class SkBlockMemoryStream;
519 friend class SkBlockMemoryRefCnt;
520
521 using INHERITED = SkWStream;
522 };
523
524 #endif