Flutter Engine
The Flutter Engine
SkStream.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 SkStream_DEFINED
9#define SkStream_DEFINED
10
11#include "include/core/SkData.h"
17
18#include <cstdint>
19#include <cstdio>
20#include <cstring>
21#include <memory>
22#include <utility>
23class SkStreamAsset;
24
25/**
26 * SkStream -- abstraction for a source of bytes. Subclasses can be backed by
27 * memory, or a file, or something else.
28 */
30public:
31 virtual ~SkStream() {}
33
34 /**
35 * Attempts to open the specified file as a stream, returns nullptr on failure.
36 */
37 static std::unique_ptr<SkStreamAsset> MakeFromFile(const char path[]);
38
39 /** Reads or skips size number of bytes.
40 * If buffer == NULL, skip size bytes, return how many were skipped.
41 * If buffer != NULL, copy size bytes into buffer, return how many were copied.
42 * @param buffer when NULL skip size bytes, otherwise copy size bytes into buffer
43 * @param size the number of bytes to skip or copy
44 * @return the number of bytes actually read.
45 */
46 virtual size_t read(void* buffer, size_t size) = 0;
47
48 /** Skip size number of bytes.
49 * @return the actual number bytes that could be skipped.
50 */
51 size_t skip(size_t size) {
52 return this->read(nullptr, size);
53 }
54
55 /**
56 * Attempt to peek at size bytes.
57 * If this stream supports peeking, copy min(size, peekable bytes) into
58 * buffer, and return the number of bytes copied.
59 * If the stream does not support peeking, or cannot peek any bytes,
60 * return 0 and leave buffer unchanged.
61 * The stream is guaranteed to be in the same visible state after this
62 * call, regardless of success or failure.
63 * @param buffer Must not be NULL, and must be at least size bytes. Destination
64 * to copy bytes.
65 * @param size Number of bytes to copy.
66 * @return The number of bytes peeked/copied.
67 */
68 virtual size_t peek(void* /*buffer*/, size_t /*size*/) const { return 0; }
69
70 /** Returns true when all the bytes in the stream have been read.
71 * As SkStream represents synchronous I/O, isAtEnd returns false when the
72 * final stream length isn't known yet, even when all the bytes available
73 * so far have been read.
74 * This may return true early (when there are no more bytes to be read)
75 * or late (after the first unsuccessful read).
76 */
77 virtual bool isAtEnd() const = 0;
78
79 [[nodiscard]] bool readS8(int8_t*);
80 [[nodiscard]] bool readS16(int16_t*);
81 [[nodiscard]] bool readS32(int32_t*);
82
83 [[nodiscard]] bool readU8(uint8_t* i) { return this->readS8((int8_t*)i); }
84 [[nodiscard]] bool readU16(uint16_t* i) { return this->readS16((int16_t*)i); }
85 [[nodiscard]] bool readU32(uint32_t* i) { return this->readS32((int32_t*)i); }
86
87 [[nodiscard]] bool readBool(bool* b) {
88 uint8_t i;
89 if (!this->readU8(&i)) { return false; }
90 *b = (i != 0);
91 return true;
92 }
93 [[nodiscard]] bool readScalar(SkScalar*);
94 [[nodiscard]] bool readPackedUInt(size_t*);
95
96//SkStreamRewindable
97 /** Rewinds to the beginning of the stream. Returns true if the stream is known
98 * to be at the beginning after this call returns.
99 */
100 virtual bool rewind() { return false; }
101
102 /** Duplicates this stream. If this cannot be done, returns NULL.
103 * The returned stream will be positioned at the beginning of its data.
104 */
105 std::unique_ptr<SkStream> duplicate() const {
106 return std::unique_ptr<SkStream>(this->onDuplicate());
107 }
108 /** Duplicates this stream. If this cannot be done, returns NULL.
109 * The returned stream will be positioned the same as this stream.
110 */
111 std::unique_ptr<SkStream> fork() const {
112 return std::unique_ptr<SkStream>(this->onFork());
113 }
114
115//SkStreamSeekable
116 /** Returns true if this stream can report its current position. */
117 virtual bool hasPosition() const { return false; }
118 /** Returns the current position in the stream. If this cannot be done, returns 0. */
119 virtual size_t getPosition() const { return 0; }
120
121 /** Seeks to an absolute position in the stream. If this cannot be done, returns false.
122 * If an attempt is made to seek past the end of the stream, the position will be set
123 * to the end of the stream.
124 */
125 virtual bool seek(size_t /*position*/) { return false; }
126
127 /** Seeks to an relative offset in the stream. If this cannot be done, returns false.
128 * If an attempt is made to move to a position outside the stream, the position will be set
129 * to the closest point within the stream (beginning or end).
130 */
131 virtual bool move(long /*offset*/) { return false; }
132
133//SkStreamAsset
134 /** Returns true if this stream can report its total length. */
135 virtual bool hasLength() const { return false; }
136 /** Returns the total length of the stream. If this cannot be done, returns 0. */
137 virtual size_t getLength() const { return 0; }
138
139//SkStreamMemory
140 /** Returns the starting address for the data. If this cannot be done, returns NULL. */
141 virtual const void* getMemoryBase() { return nullptr; }
142 virtual sk_sp<SkData> getData() const { return nullptr; }
143
144private:
145 virtual SkStream* onDuplicate() const { return nullptr; }
146 virtual SkStream* onFork() const { return nullptr; }
147
148 SkStream(SkStream&&) = delete;
149 SkStream(const SkStream&) = delete;
150 SkStream& operator=(SkStream&&) = delete;
151 SkStream& operator=(const SkStream&) = delete;
152};
153
154/** SkStreamRewindable is a SkStream for which rewind and duplicate are required. */
156public:
157 bool rewind() override = 0;
158 std::unique_ptr<SkStreamRewindable> duplicate() const {
159 return std::unique_ptr<SkStreamRewindable>(this->onDuplicate());
160 }
161private:
162 SkStreamRewindable* onDuplicate() const override = 0;
163};
164
165/** SkStreamSeekable is a SkStreamRewindable for which position, seek, move, and fork are required. */
167public:
168 std::unique_ptr<SkStreamSeekable> duplicate() const {
169 return std::unique_ptr<SkStreamSeekable>(this->onDuplicate());
170 }
171
172 bool hasPosition() const override { return true; }
173 size_t getPosition() const override = 0;
174 bool seek(size_t position) override = 0;
175 bool move(long offset) override = 0;
176
177 std::unique_ptr<SkStreamSeekable> fork() const {
178 return std::unique_ptr<SkStreamSeekable>(this->onFork());
179 }
180private:
181 SkStreamSeekable* onDuplicate() const override = 0;
182 SkStreamSeekable* onFork() const override = 0;
183};
184
185/** SkStreamAsset is a SkStreamSeekable for which getLength is required. */
187public:
188 bool hasLength() const override { return true; }
189 size_t getLength() const override = 0;
190
191 std::unique_ptr<SkStreamAsset> duplicate() const {
192 return std::unique_ptr<SkStreamAsset>(this->onDuplicate());
193 }
194 std::unique_ptr<SkStreamAsset> fork() const {
195 return std::unique_ptr<SkStreamAsset>(this->onFork());
196 }
197private:
198 SkStreamAsset* onDuplicate() const override = 0;
199 SkStreamAsset* onFork() const override = 0;
200};
201
202/** SkStreamMemory is a SkStreamAsset for which getMemoryBase is required. */
204public:
205 const void* getMemoryBase() override = 0;
206
207 std::unique_ptr<SkStreamMemory> duplicate() const {
208 return std::unique_ptr<SkStreamMemory>(this->onDuplicate());
209 }
210 std::unique_ptr<SkStreamMemory> fork() const {
211 return std::unique_ptr<SkStreamMemory>(this->onFork());
212 }
213private:
214 SkStreamMemory* onDuplicate() const override = 0;
215 SkStreamMemory* onFork() const override = 0;
216};
217
219public:
220 virtual ~SkWStream();
222
223 /** Called to write bytes to a SkWStream. Returns true on success
224 @param buffer the address of at least size bytes to be written to the stream
225 @param size The number of bytes in buffer to write to the stream
226 @return true on success
227 */
228 virtual bool write(const void* buffer, size_t size) = 0;
229 virtual void flush();
230
231 virtual size_t bytesWritten() const = 0;
232
233 // helpers
234
236 uint8_t v = SkToU8(value);
237 return this->write(&v, 1);
238 }
240 uint16_t v = SkToU16(value);
241 return this->write(&v, 2);
242 }
243 bool write32(uint32_t v) {
244 return this->write(&v, 4);
245 }
246
247 bool writeText(const char text[]) {
248 SkASSERT(text);
249 return this->write(text, std::strlen(text));
250 }
251
252 bool newline() { return this->write("\n", std::strlen("\n")); }
253
254 bool writeDecAsText(int32_t);
255 bool writeBigDecAsText(int64_t, int minDigits = 0);
256 bool writeHexAsText(uint32_t, int minDigits = 0);
257 bool writeScalarAsText(SkScalar);
258
259 bool writeBool(bool v) { return this->write8(v); }
260 bool writeScalar(SkScalar);
261 bool writePackedUInt(size_t);
262
263 bool writeStream(SkStream* input, size_t length);
264
265 /**
266 * This returns the number of bytes in the stream required to store
267 * 'value'.
268 */
269 static int SizeOfPackedUInt(size_t value);
270
271private:
272 SkWStream(const SkWStream&) = delete;
273 SkWStream& operator=(const SkWStream&) = delete;
274};
275
277public:
278 SkNullWStream() : fBytesWritten(0) {}
279
280 bool write(const void* , size_t n) override { fBytesWritten += n; return true; }
281 void flush() override {}
282 size_t bytesWritten() const override { return fBytesWritten; }
283
284private:
285 size_t fBytesWritten;
286};
287
288////////////////////////////////////////////////////////////////////////////////////////
289
290/** A stream that wraps a C FILE* file stream. */
292public:
293 /** Initialize the stream by calling sk_fopen on the specified path.
294 * This internal stream will be closed in the destructor.
295 */
296 explicit SkFILEStream(const char path[] = nullptr);
297
298 /** Initialize the stream with an existing C FILE stream.
299 * The current position of the C FILE stream will be considered the
300 * beginning of the SkFILEStream and the current seek end of the FILE will be the end.
301 * The C FILE stream will be closed in the destructor.
302 */
303 explicit SkFILEStream(FILE* file);
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 size bytes later will be the end.
308 * The C FILE stream will be closed in the destructor.
309 */
310 explicit SkFILEStream(FILE* file, size_t size);
311
312 ~SkFILEStream() override;
313
314 static std::unique_ptr<SkFILEStream> Make(const char path[]) {
315 std::unique_ptr<SkFILEStream> stream(new SkFILEStream(path));
316 return stream->isValid() ? std::move(stream) : nullptr;
317 }
318
319 /** Returns true if the current path could be opened. */
320 bool isValid() const { return fFILE != nullptr; }
321
322 /** Close this SkFILEStream. */
323 void close();
324
325 size_t read(void* buffer, size_t size) override;
326 bool isAtEnd() const override;
327
328 bool rewind() override;
329 std::unique_ptr<SkStreamAsset> duplicate() const {
330 return std::unique_ptr<SkStreamAsset>(this->onDuplicate());
331 }
332
333 size_t getPosition() const override;
334 bool seek(size_t position) override;
335 bool move(long offset) override;
336
337 std::unique_ptr<SkStreamAsset> fork() const {
338 return std::unique_ptr<SkStreamAsset>(this->onFork());
339 }
340
341 size_t getLength() const override;
342
343private:
344 explicit SkFILEStream(FILE*, size_t size, size_t start);
345 explicit SkFILEStream(std::shared_ptr<FILE>, size_t end, size_t start);
346 explicit SkFILEStream(std::shared_ptr<FILE>, size_t end, size_t start, size_t current);
347
348 SkStreamAsset* onDuplicate() const override;
349 SkStreamAsset* onFork() const override;
350
351 std::shared_ptr<FILE> fFILE;
352 // My own council will I keep on sizes and offsets.
353 // These are seek positions in the underling FILE, not offsets into the stream.
354 size_t fEnd;
355 size_t fStart;
356 size_t fCurrent;
357
358 using INHERITED = SkStreamAsset;
359};
360
362public:
364
365 /** We allocate (and free) the memory. Write to it via getMemoryBase() */
366 SkMemoryStream(size_t length);
367
368 /** If copyData is true, the stream makes a private copy of the data. */
369 SkMemoryStream(const void* data, size_t length, bool copyData = false);
370
371 /** Creates the stream to read from the specified data */
373
374 /** Returns a stream with a copy of the input data. */
375 static std::unique_ptr<SkMemoryStream> MakeCopy(const void* data, size_t length);
376
377 /** Returns a stream with a bare pointer reference to the input data. */
378 static std::unique_ptr<SkMemoryStream> MakeDirect(const void* data, size_t length);
379
380 /** Returns a stream with a shared reference to the input data. */
381 static std::unique_ptr<SkMemoryStream> Make(sk_sp<SkData> data);
382
383 /** Resets the stream to the specified data and length,
384 just like the constructor.
385 if copyData is true, the stream makes a private copy of the data
386 */
387 virtual void setMemory(const void* data, size_t length,
388 bool copyData = false);
389 /** Replace any memory buffer with the specified buffer. The caller
390 must have allocated data with sk_malloc or sk_realloc, since it
391 will be freed with sk_free.
392 */
393 void setMemoryOwned(const void* data, size_t length);
394
395 sk_sp<SkData> getData() const override { return fData; }
396 void setData(sk_sp<SkData> data);
397
398 const void* getAtPos();
399
400 size_t read(void* buffer, size_t size) override;
401 bool isAtEnd() const override;
402
403 size_t peek(void* buffer, size_t size) const override;
404
405 bool rewind() override;
406
407 std::unique_ptr<SkMemoryStream> duplicate() const {
408 return std::unique_ptr<SkMemoryStream>(this->onDuplicate());
409 }
410
411 size_t getPosition() const override;
412 bool seek(size_t position) override;
413 bool move(long offset) override;
414
415 std::unique_ptr<SkMemoryStream> fork() const {
416 return std::unique_ptr<SkMemoryStream>(this->onFork());
417 }
418
419 size_t getLength() const override;
420
421 const void* getMemoryBase() override;
422
423private:
424 SkMemoryStream* onDuplicate() const override;
425 SkMemoryStream* onFork() const override;
426
427 sk_sp<SkData> fData;
428 size_t fOffset;
429
431};
432
433/////////////////////////////////////////////////////////////////////////////////////////////
434
436public:
437 SkFILEWStream(const char path[]);
438 ~SkFILEWStream() override;
439
440 /** Returns true if the current path could be opened.
441 */
442 bool isValid() const { return fFILE != nullptr; }
443
444 bool write(const void* buffer, size_t size) override;
445 void flush() override;
446 void fsync();
447 size_t bytesWritten() const override;
448
449private:
450 FILE* fFILE;
451
452 using INHERITED = SkWStream;
453};
454
456public:
460 ~SkDynamicMemoryWStream() override;
461
462 bool write(const void* buffer, size_t size) override;
463 size_t bytesWritten() const override;
464
465 bool read(void* buffer, size_t offset, size_t size);
466
467 /** More efficient version of read(dst, 0, bytesWritten()). */
468 void copyTo(void* dst) const;
469 bool writeToStream(SkWStream* dst) const;
470
471 /** Equivalent to copyTo() followed by reset(), but may save memory use. */
472 void copyToAndReset(void* dst);
473
474 /** Equivalent to writeToStream() followed by reset(), but may save memory use. */
475 bool writeToAndReset(SkWStream* dst);
476
477 /** Equivalent to writeToStream() followed by reset(), but may save memory use.
478 When the dst is also a SkDynamicMemoryWStream, the implementation is constant time. */
479 bool writeToAndReset(SkDynamicMemoryWStream* dst);
480
481 /** Prepend this stream to dst, resetting this. */
482 void prependToAndReset(SkDynamicMemoryWStream* dst);
483
484 /** Return the contents as SkData, and then reset the stream. */
485 sk_sp<SkData> detachAsData();
486
487 /** Reset, returning a reader stream with the current content. */
488 std::unique_ptr<SkStreamAsset> detachAsStream();
489
490 /** Reset the stream to its original, empty, state. */
491 void reset();
492 void padToAlign4();
493private:
494 struct Block;
495 Block* fHead = nullptr;
496 Block* fTail = nullptr;
497 size_t fBytesWrittenBeforeTail = 0;
498
499#ifdef SK_DEBUG
500 void validate() const;
501#else
502 void validate() const {}
503#endif
504
505 // For access to the Block type.
508
509 using INHERITED = SkWStream;
510};
511
512#endif
m reset()
static bool rewind(EdgeList *activeEdges, Vertex **current, Vertex *dst, const Comparator &c)
#define SK_API
Definition: SkAPI.h:35
#define SkASSERT(cond)
Definition: SkAssert.h:116
unsigned U16CPU
Definition: SkCPUTypes.h:23
unsigned U8CPU
Definition: SkCPUTypes.h:18
static bool read(SkStream *stream, void *buffer, size_t amount)
#define INHERITED(method,...)
Definition: SkRecorder.cpp:128
constexpr uint16_t SkToU16(S x)
Definition: SkTo.h:24
constexpr uint8_t SkToU8(S x)
Definition: SkTo.h:22
SkDynamicMemoryWStream()=default
std::unique_ptr< SkStreamAsset > duplicate() const
Definition: SkStream.h:329
static std::unique_ptr< SkFILEStream > Make(const char path[])
Definition: SkStream.h:314
std::unique_ptr< SkStreamAsset > fork() const
Definition: SkStream.h:337
bool isValid() const
Definition: SkStream.h:320
bool isValid() const
Definition: SkStream.h:442
sk_sp< SkData > getData() const override
Definition: SkStream.h:395
std::unique_ptr< SkMemoryStream > fork() const
Definition: SkStream.h:415
std::unique_ptr< SkMemoryStream > duplicate() const
Definition: SkStream.h:407
size_t bytesWritten() const override
Definition: SkStream.h:282
void flush() override
Definition: SkStream.h:281
bool write(const void *, size_t n) override
Definition: SkStream.h:280
std::unique_ptr< SkStreamAsset > duplicate() const
Definition: SkStream.h:191
bool hasLength() const override
Definition: SkStream.h:188
size_t getLength() const override=0
SkStreamAsset * onFork() const override=0
SkStreamAsset * onDuplicate() const override=0
std::unique_ptr< SkStreamAsset > fork() const
Definition: SkStream.h:194
SkStreamMemory * onDuplicate() const override=0
std::unique_ptr< SkStreamMemory > duplicate() const
Definition: SkStream.h:207
SkStreamMemory * onFork() const override=0
std::unique_ptr< SkStreamMemory > fork() const
Definition: SkStream.h:210
const void * getMemoryBase() override=0
std::unique_ptr< SkStreamRewindable > duplicate() const
Definition: SkStream.h:158
bool rewind() override=0
SkStreamRewindable * onDuplicate() const override=0
std::unique_ptr< SkStreamSeekable > duplicate() const
Definition: SkStream.h:168
std::unique_ptr< SkStreamSeekable > fork() const
Definition: SkStream.h:177
size_t getPosition() const override=0
SkStreamSeekable * onFork() const override=0
bool move(long offset) override=0
bool hasPosition() const override
Definition: SkStream.h:172
bool seek(size_t position) override=0
SkStreamSeekable * onDuplicate() const override=0
virtual sk_sp< SkData > getData() const
Definition: SkStream.h:142
bool readU8(uint8_t *i)
Definition: SkStream.h:83
virtual SkStream * onFork() const
Definition: SkStream.h:146
virtual bool move(long)
Definition: SkStream.h:131
SkStream()
Definition: SkStream.h:32
std::unique_ptr< SkStream > duplicate() const
Definition: SkStream.h:105
virtual bool rewind()
Definition: SkStream.h:100
virtual SkStream * onDuplicate() const
Definition: SkStream.h:145
bool readBool(bool *b)
Definition: SkStream.h:87
virtual size_t peek(void *, size_t) const
Definition: SkStream.h:68
virtual bool hasPosition() const
Definition: SkStream.h:117
virtual size_t getPosition() const
Definition: SkStream.h:119
std::unique_ptr< SkStream > fork() const
Definition: SkStream.h:111
virtual ~SkStream()
Definition: SkStream.h:31
bool readU16(uint16_t *i)
Definition: SkStream.h:84
size_t skip(size_t size)
Definition: SkStream.h:51
bool readU32(uint32_t *i)
Definition: SkStream.h:85
virtual bool isAtEnd() const =0
virtual bool seek(size_t)
Definition: SkStream.h:125
virtual size_t getLength() const
Definition: SkStream.h:137
virtual const void * getMemoryBase()
Definition: SkStream.h:141
virtual bool hasLength() const
Definition: SkStream.h:135
virtual size_t read(void *buffer, size_t size)=0
virtual bool write(const void *buffer, size_t size)=0
bool write32(uint32_t v)
Definition: SkStream.h:243
bool write8(U8CPU value)
Definition: SkStream.h:235
bool write16(U16CPU value)
Definition: SkStream.h:239
SkWStream()
Definition: SkStream.h:221
virtual size_t bytesWritten() const =0
bool writeBool(bool v)
Definition: SkStream.h:259
bool writeText(const char text[])
Definition: SkStream.h:247
bool newline()
Definition: SkStream.h:252
float SkScalar
Definition: extension.cpp:12
static bool b
glong glong end
uint8_t value
size_t length
std::u16string text
SK_API sk_sp< SkDocument > Make(SkWStream *dst, const SkSerialProcs *=nullptr, std::function< void(const SkPicture *)> onEndPage=nullptr)
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir path
Definition: switches.h:57
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace buffer
Definition: switches.h:126
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259
dst
Definition: cp.py:12
void write(SkWStream *wStream, const T &text)
Definition: skqp.cpp:188
SeparatedVector2 offset
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63