blob: dd7f95aa3cacda3bd77893ca63e121931cbd379d [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00003 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00006 */
7
epoger@google.comec3ed6a2011-07-28 14:26:00 +00008
reed@android.com8a1c16f2008-12-17 15:59:43 +00009#ifndef SkBuffer_DEFINED
10#define SkBuffer_DEFINED
11
12#include "SkScalar.h"
bungemand3ebb482015-08-05 13:57:49 -070013#include "SkTypes.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014
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.comfbfcd562012-08-23 18:09:54 +000020 to attempt to read a value from an empty RBuffer (data == null).
reed@android.com8a1c16f2008-12-17 15:59:43 +000021*/
22class SkRBuffer : SkNoncopyable {
23public:
Ben Wagnera93a14a2017-08-28 10:34:05 -040024 SkRBuffer() : fData(nullptr), fPos(nullptr), fStop(nullptr) {}
Mike Reed1026ccf2017-01-08 14:35:29 -050025
reed@android.com8a1c16f2008-12-17 15:59:43 +000026 /** Initialize RBuffer with a data point and length.
27 */
reed@google.com7894b922011-05-03 15:41:49 +000028 SkRBuffer(const void* data, size_t size) {
Ben Wagnera93a14a2017-08-28 10:34:05 -040029 SkASSERT(data != nullptr || size == 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +000030 fData = (const char*)data;
31 fPos = (const char*)data;
32 fStop = (const char*)data + size;
33 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000034
reed@android.com8a1c16f2008-12-17 15:59:43 +000035 /** Return the number of bytes that have been read from the beginning
36 of the data pointer.
37 */
Mike Reed1026ccf2017-01-08 14:35:29 -050038 size_t pos() const { return fPos - fData; }
reed@android.com8a1c16f2008-12-17 15:59:43 +000039 /** 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 Reed1026ccf2017-01-08 14:35:29 -050042 size_t size() const { return fStop - fData; }
reed@android.com8a1c16f2008-12-17 15:59:43 +000043 /** 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 Reed1026ccf2017-01-08 14:35:29 -050047 bool eof() const { return fPos >= fStop; }
48
49 size_t available() const { return fStop - fPos; }
50
51 bool isValid() const { return fValid; }
reed@android.com8a1c16f2008-12-17 15:59:43 +000052
53 /** Read the specified number of bytes from the data pointer. If buffer is not
54 null, copy those bytes into buffer.
55 */
Mike Reed1026ccf2017-01-08 14:35:29 -050056 bool read(void* buffer, size_t size);
57 bool skipToAlign4();
reed@google.com7894b922011-05-03 15:41:49 +000058
Mike Reed1026ccf2017-01-08 14:35:29 -050059 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.com8a1c16f2008-12-17 15:59:43 +000062
Mike Reed1026ccf2017-01-08 14:35:29 -050063private:
reed@android.com8a1c16f2008-12-17 15:59:43 +000064 const char* fData;
65 const char* fPos;
66 const char* fStop;
Mike Reed1026ccf2017-01-08 14:35:29 -050067 bool fValid = true;
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000068};
69
reed@android.com8a1c16f2008-12-17 15:59:43 +000070/** \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*/
78class SkWBuffer : SkNoncopyable {
79public:
Ben Wagnera93a14a2017-08-28 10:34:05 -040080 SkWBuffer() : fData(nullptr), fPos(nullptr), fStop(nullptr) {}
reed@android.com8a1c16f2008-12-17 15:59:43 +000081 SkWBuffer(void* data) { reset(data); }
82 SkWBuffer(void* data, size_t size) { reset(data, size); }
83
reed@google.com7894b922011-05-03 15:41:49 +000084 void reset(void* data) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000085 fData = (char*)data;
86 fPos = (char*)data;
Ben Wagnera93a14a2017-08-28 10:34:05 -040087 fStop = nullptr; // no bounds checking
reed@android.com8a1c16f2008-12-17 15:59:43 +000088 }
reed@google.com7894b922011-05-03 15:41:49 +000089
90 void reset(void* data, size_t size) {
Ben Wagnera93a14a2017-08-28 10:34:05 -040091 SkASSERT(data != nullptr || size == 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +000092 fData = (char*)data;
93 fPos = (char*)data;
94 fStop = (char*)data + size;
95 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000096
reed@android.com8a1c16f2008-12-17 15:59:43 +000097 size_t pos() const { return fPos - fData; }
reed@android.com8a1c16f2008-12-17 15:59:43 +000098 void* skip(size_t size); // return start of skipped data
reed@google.com7894b922011-05-03 15:41:49 +000099
100 void write(const void* buffer, size_t size) {
101 if (size) {
102 this->writeNoSizeCheck(buffer, size);
103 }
104 }
105
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106 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.combc4b66f2012-08-16 16:46:27 +0000115private:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000116 void writeNoSizeCheck(const void* buffer, size_t size);
117
118 char* fData;
119 char* fPos;
120 char* fStop;
121};
122
123#endif