reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2006 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 3 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 8 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 9 | #ifndef SkBuffer_DEFINED |
| 10 | #define SkBuffer_DEFINED |
| 11 | |
| 12 | #include "SkScalar.h" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 13 | #include "SkTypes.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 14 | |
| 15 | /** \class SkRBuffer |
| 16 | |
| 17 | Light weight class for reading data from a memory block. |
| 18 | The RBuffer is given the buffer to read from, with either a specified size |
| 19 | or no size (in which case no range checking is performed). It is iillegal |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 20 | to attempt to read a value from an empty RBuffer (data == null). |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 21 | */ |
| 22 | class SkRBuffer : SkNoncopyable { |
| 23 | public: |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 24 | SkRBuffer() : fData(nullptr), fPos(nullptr), fStop(nullptr) {} |
Mike Reed | 1026ccf | 2017-01-08 14:35:29 -0500 | [diff] [blame] | 25 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 26 | /** Initialize RBuffer with a data point and length. |
| 27 | */ |
reed@google.com | 7894b92 | 2011-05-03 15:41:49 +0000 | [diff] [blame] | 28 | SkRBuffer(const void* data, size_t size) { |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 29 | SkASSERT(data != nullptr || size == 0); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 30 | fData = (const char*)data; |
| 31 | fPos = (const char*)data; |
| 32 | fStop = (const char*)data + size; |
| 33 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 34 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 35 | /** Return the number of bytes that have been read from the beginning |
| 36 | of the data pointer. |
| 37 | */ |
Mike Reed | 1026ccf | 2017-01-08 14:35:29 -0500 | [diff] [blame] | 38 | size_t pos() const { return fPos - fData; } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 39 | /** Return the total size of the data pointer. Only defined if the length was |
| 40 | specified in the constructor or in a call to reset(). |
| 41 | */ |
Mike Reed | 1026ccf | 2017-01-08 14:35:29 -0500 | [diff] [blame] | 42 | size_t size() const { return fStop - fData; } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 43 | /** Return true if the buffer has read to the end of the data pointer. |
| 44 | Only defined if the length was specified in the constructor or in a call |
| 45 | to reset(). Always returns true if the length was not specified. |
| 46 | */ |
Mike Reed | 1026ccf | 2017-01-08 14:35:29 -0500 | [diff] [blame] | 47 | bool eof() const { return fPos >= fStop; } |
| 48 | |
| 49 | size_t available() const { return fStop - fPos; } |
| 50 | |
| 51 | bool isValid() const { return fValid; } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 52 | |
| 53 | /** Read the specified number of bytes from the data pointer. If buffer is not |
| 54 | null, copy those bytes into buffer. |
| 55 | */ |
Mike Reed | 1026ccf | 2017-01-08 14:35:29 -0500 | [diff] [blame] | 56 | bool read(void* buffer, size_t size); |
| 57 | bool skipToAlign4(); |
reed@google.com | 7894b92 | 2011-05-03 15:41:49 +0000 | [diff] [blame] | 58 | |
Mike Reed | 1026ccf | 2017-01-08 14:35:29 -0500 | [diff] [blame] | 59 | bool readU8(uint8_t* x) { return this->read(x, 1); } |
| 60 | bool readS32(int32_t* x) { return this->read(x, 4); } |
| 61 | bool readU32(uint32_t* x) { return this->read(x, 4); } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 62 | |
Mike Reed | 1026ccf | 2017-01-08 14:35:29 -0500 | [diff] [blame] | 63 | private: |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 64 | const char* fData; |
| 65 | const char* fPos; |
| 66 | const char* fStop; |
Mike Reed | 1026ccf | 2017-01-08 14:35:29 -0500 | [diff] [blame] | 67 | bool fValid = true; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 68 | }; |
| 69 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 70 | /** \class SkWBuffer |
| 71 | |
| 72 | Light weight class for writing data to a memory block. |
| 73 | The WBuffer is given the buffer to write into, with either a specified size |
| 74 | or no size, in which case no range checking is performed. An empty WBuffer |
| 75 | is legal, in which case no data is ever written, but the relative pos() |
| 76 | is updated. |
| 77 | */ |
| 78 | class SkWBuffer : SkNoncopyable { |
| 79 | public: |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 80 | SkWBuffer() : fData(nullptr), fPos(nullptr), fStop(nullptr) {} |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 81 | SkWBuffer(void* data) { reset(data); } |
| 82 | SkWBuffer(void* data, size_t size) { reset(data, size); } |
| 83 | |
reed@google.com | 7894b92 | 2011-05-03 15:41:49 +0000 | [diff] [blame] | 84 | void reset(void* data) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 85 | fData = (char*)data; |
| 86 | fPos = (char*)data; |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 87 | fStop = nullptr; // no bounds checking |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 88 | } |
reed@google.com | 7894b92 | 2011-05-03 15:41:49 +0000 | [diff] [blame] | 89 | |
| 90 | void reset(void* data, size_t size) { |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 91 | SkASSERT(data != nullptr || size == 0); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 92 | fData = (char*)data; |
| 93 | fPos = (char*)data; |
| 94 | fStop = (char*)data + size; |
| 95 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 96 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 97 | size_t pos() const { return fPos - fData; } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 98 | void* skip(size_t size); // return start of skipped data |
reed@google.com | 7894b92 | 2011-05-03 15:41:49 +0000 | [diff] [blame] | 99 | |
| 100 | void write(const void* buffer, size_t size) { |
| 101 | if (size) { |
| 102 | this->writeNoSizeCheck(buffer, size); |
| 103 | } |
| 104 | } |
| 105 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 106 | size_t padToAlign4(); |
| 107 | |
| 108 | void writePtr(const void* x) { this->writeNoSizeCheck(&x, sizeof(x)); } |
| 109 | void writeScalar(SkScalar x) { this->writeNoSizeCheck(&x, 4); } |
| 110 | void write32(int32_t x) { this->writeNoSizeCheck(&x, 4); } |
| 111 | void write16(int16_t x) { this->writeNoSizeCheck(&x, 2); } |
| 112 | void write8(int8_t x) { this->writeNoSizeCheck(&x, 1); } |
| 113 | void writeBool(bool x) { this->write8(x); } |
| 114 | |
reed@google.com | bc4b66f | 2012-08-16 16:46:27 +0000 | [diff] [blame] | 115 | private: |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 116 | void writeNoSizeCheck(const void* buffer, size_t size); |
| 117 | |
| 118 | char* fData; |
| 119 | char* fPos; |
| 120 | char* fStop; |
| 121 | }; |
| 122 | |
| 123 | #endif |