scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 SkStreamBuffer_DEFINED |
| 9 | #define SkStreamBuffer_DEFINED |
| 10 | |
Leon Scroggins III | 932efed | 2016-12-16 11:39:51 -0500 | [diff] [blame] | 11 | #include "SkData.h" |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 12 | #include "SkStream.h" |
| 13 | #include "SkTypes.h" |
Leon Scroggins III | 932efed | 2016-12-16 11:39:51 -0500 | [diff] [blame] | 14 | #include "../private/SkTHash.h" |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 15 | |
| 16 | /** |
| 17 | * Helper class for reading from a stream that may not have all its data |
| 18 | * available yet. |
| 19 | * |
| 20 | * Used by GIFImageReader, and currently set up for that use case. |
| 21 | * |
| 22 | * Buffers up to 256 * 3 bytes (256 colors, with 3 bytes each) to support GIF. |
| 23 | * FIXME (scroggo): Make this more general purpose? |
| 24 | */ |
| 25 | class SkStreamBuffer : SkNoncopyable { |
| 26 | public: |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame^] | 27 | SkStreamBuffer(std::unique_ptr<SkStream>); |
Leon Scroggins III | 932efed | 2016-12-16 11:39:51 -0500 | [diff] [blame] | 28 | ~SkStreamBuffer(); |
| 29 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 30 | /** |
| 31 | * Return a pointer the buffered data. |
| 32 | * |
Leon Scroggins III | 932efed | 2016-12-16 11:39:51 -0500 | [diff] [blame] | 33 | * The number of bytes buffered is the number passed to buffer() |
| 34 | * after the last call to flush(). |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 35 | */ |
Leon Scroggins III | 932efed | 2016-12-16 11:39:51 -0500 | [diff] [blame] | 36 | const char* get() const; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * Buffer from the stream into our buffer. |
| 40 | * |
Leon Scroggins III | 932efed | 2016-12-16 11:39:51 -0500 | [diff] [blame] | 41 | * If this call returns true, get() can be used to access |bytes| bytes |
| 42 | * from the stream. In addition, markPosition() can be called to mark this |
| 43 | * position and enable calling getAtPosition() later to retrieve |bytes| |
| 44 | * bytes. |
| 45 | * |
| 46 | * @param bytes Total number of bytes desired. |
| 47 | * |
| 48 | * @return Whether all bytes were successfully buffered. |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 49 | */ |
Leon Scroggins III | 932efed | 2016-12-16 11:39:51 -0500 | [diff] [blame] | 50 | bool buffer(size_t bytes); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 51 | |
| 52 | /** |
| 53 | * Flush the buffer. |
| 54 | * |
| 55 | * After this call, no bytes are buffered. |
| 56 | */ |
| 57 | void flush() { |
Leon Scroggins III | 932efed | 2016-12-16 11:39:51 -0500 | [diff] [blame] | 58 | if (fHasLengthAndPosition) { |
| 59 | if (fTrulyBuffered < fBytesBuffered) { |
| 60 | fStream->move(fBytesBuffered - fTrulyBuffered); |
| 61 | } |
| 62 | fTrulyBuffered = 0; |
| 63 | } |
| 64 | fPosition += fBytesBuffered; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 65 | fBytesBuffered = 0; |
| 66 | } |
| 67 | |
Leon Scroggins III | 932efed | 2016-12-16 11:39:51 -0500 | [diff] [blame] | 68 | /** |
| 69 | * Mark the current position in the stream to return to it later. |
| 70 | * |
| 71 | * This is the position of the start of the buffer. After this call, a |
| 72 | * a client can call getDataAtPosition to retrieve all the bytes currently |
| 73 | * buffered. |
| 74 | * |
| 75 | * @return size_t Position which can be passed to getDataAtPosition later |
| 76 | * to retrieve the data currently buffered. |
| 77 | */ |
| 78 | size_t markPosition(); |
| 79 | |
| 80 | /** |
| 81 | * Retrieve data at position, as previously marked by markPosition(). |
| 82 | * |
| 83 | * @param position Position to retrieve data, as marked by markPosition(). |
| 84 | * @param length Amount of data required at position. |
| 85 | * @return SkData The data at position. |
| 86 | */ |
| 87 | sk_sp<SkData> getDataAtPosition(size_t position, size_t length); |
| 88 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 89 | private: |
| 90 | static constexpr size_t kMaxSize = 256 * 3; |
| 91 | |
| 92 | std::unique_ptr<SkStream> fStream; |
Leon Scroggins III | 932efed | 2016-12-16 11:39:51 -0500 | [diff] [blame] | 93 | size_t fPosition; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 94 | char fBuffer[kMaxSize]; |
| 95 | size_t fBytesBuffered; |
Leon Scroggins III | 932efed | 2016-12-16 11:39:51 -0500 | [diff] [blame] | 96 | // If the stream has a length and position, we can make two optimizations: |
| 97 | // - We can skip buffering |
| 98 | // - During parsing, we can store the position and size of data that is |
| 99 | // needed later during decoding. |
| 100 | const bool fHasLengthAndPosition; |
| 101 | // When fHasLengthAndPosition is true, we do not need to actually buffer |
| 102 | // inside buffer(). We'll buffer inside get(). This keeps track of how many |
| 103 | // bytes we've buffered inside get(), for the (non-existent) case of: |
| 104 | // buffer(n) |
| 105 | // get() |
| 106 | // buffer(n + u) |
| 107 | // get() |
| 108 | // The second call to get() needs to only truly buffer the part that was |
| 109 | // not already buffered. |
| 110 | mutable size_t fTrulyBuffered; |
| 111 | // Only used if !fHasLengthAndPosition. In that case, markPosition will |
| 112 | // copy into an SkData, stored here. |
| 113 | SkTHashMap<size_t, SkData*> fMarkedData; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 114 | }; |
| 115 | #endif // SkStreamBuffer_DEFINED |
| 116 | |