Remove SkReader32, and some API from SkWriter32
After removing all unused API from SkReader32, it only had a handful of functions, and it was (rightly) only used by SkReadBuffer. Remove the temptation to use it by just folding it into SkReadBuffer. SkWriter32 had some unnecessary functions as well (only used in unit tests), so those are gone. There is still a strange relationship: SkWriteBuffer is just an interface - SkReadBuffer is actually the complement of SkBinaryWriteBuffer/SkWriter32. Those two classes produce data in the exact same format, but with slightly different interfaces. (The choice about which one is used is mostly about high-level serialization vs. low-level helpers). Change-Id: I1e823755febecd2e053ea732b21295d8f4d9d832 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/295557 Reviewed-by: Mike Klein <mtklein@google.com> Commit-Queue: Brian Osman <brianosman@google.com>
This commit is contained in:
parent
ae413d8b81
commit
ff7bee9817
@ -302,7 +302,6 @@ skia_core_sources = [
|
||||
"$_src/core/SkRasterPipelineBlitter.cpp",
|
||||
"$_src/core/SkReadBuffer.cpp",
|
||||
"$_src/core/SkReadBuffer.h",
|
||||
"$_src/core/SkReader32.h",
|
||||
"$_src/core/SkRecord.cpp",
|
||||
"$_src/core/SkRecordDraw.cpp",
|
||||
"$_src/core/SkRecordOpts.cpp",
|
||||
|
@ -212,7 +212,6 @@ tests_sources = [
|
||||
"$_tests/RandomTest.cpp",
|
||||
"$_tests/ReadPixelsTest.cpp",
|
||||
"$_tests/ReadWriteAlphaTest.cpp",
|
||||
"$_tests/Reader32Test.cpp",
|
||||
"$_tests/RecordDrawTest.cpp",
|
||||
"$_tests/RecordOptsTest.cpp",
|
||||
"$_tests/RecordPatternTest.cpp",
|
||||
|
@ -1891,7 +1891,6 @@ private:
|
||||
|
||||
friend class SkPerspIter;
|
||||
friend class SkMatrixPriv;
|
||||
friend class SkReader32;
|
||||
friend class SerializationTest;
|
||||
};
|
||||
SK_END_REQUIRE_DENSE
|
||||
|
@ -18,7 +18,6 @@
|
||||
|
||||
class SkData;
|
||||
class SkPictureRecord;
|
||||
class SkReader32;
|
||||
struct SkSerialProcs;
|
||||
class SkStream;
|
||||
class SkWStream;
|
||||
|
@ -60,13 +60,15 @@ SkReadBuffer::SkReadBuffer(const void* data, size_t size) {
|
||||
void SkReadBuffer::setMemory(const void* data, size_t size) {
|
||||
this->validate(IsPtrAlign4(data) && (SkAlign4(size) == size));
|
||||
if (!fError) {
|
||||
fReader.setMemory(data, size);
|
||||
fBase = fCurr = (const char*)data;
|
||||
fStop = fBase + size;
|
||||
}
|
||||
}
|
||||
|
||||
void SkReadBuffer::setInvalid() {
|
||||
if (!fError) {
|
||||
// When an error is found, send the read cursor to the end of the stream
|
||||
fReader.skip(fReader.available());
|
||||
fCurr = fStop;
|
||||
fError = true;
|
||||
}
|
||||
}
|
||||
@ -74,13 +76,13 @@ void SkReadBuffer::setInvalid() {
|
||||
const void* SkReadBuffer::skip(size_t size) {
|
||||
size_t inc = SkAlign4(size);
|
||||
this->validate(inc >= size);
|
||||
const void* addr = fReader.peek();
|
||||
this->validate(IsPtrAlign4(addr) && fReader.isAvailable(inc));
|
||||
const void* addr = fCurr;
|
||||
this->validate(IsPtrAlign4(addr) && this->isAvailable(inc));
|
||||
if (fError) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
fReader.skip(size);
|
||||
fCurr += inc;
|
||||
return addr;
|
||||
}
|
||||
|
||||
@ -105,14 +107,22 @@ SkColor SkReadBuffer::readColor() {
|
||||
|
||||
int32_t SkReadBuffer::readInt() {
|
||||
const size_t inc = sizeof(int32_t);
|
||||
this->validate(IsPtrAlign4(fReader.peek()) && fReader.isAvailable(inc));
|
||||
return fError ? 0 : fReader.readInt();
|
||||
if (!this->validate(IsPtrAlign4(fCurr) && this->isAvailable(inc))) {
|
||||
return 0;
|
||||
}
|
||||
int32_t value = *((const int32_t*)fCurr);
|
||||
fCurr += inc;
|
||||
return value;
|
||||
}
|
||||
|
||||
SkScalar SkReadBuffer::readScalar() {
|
||||
const size_t inc = sizeof(SkScalar);
|
||||
this->validate(IsPtrAlign4(fReader.peek()) && fReader.isAvailable(inc));
|
||||
return fError ? 0 : fReader.readScalar();
|
||||
if (!this->validate(IsPtrAlign4(fCurr) && this->isAvailable(inc))) {
|
||||
return 0;
|
||||
}
|
||||
SkScalar value = *((const SkScalar*)fCurr);
|
||||
fCurr += inc;
|
||||
return value;
|
||||
}
|
||||
|
||||
uint32_t SkReadBuffer::readUInt() {
|
||||
@ -124,11 +134,11 @@ int32_t SkReadBuffer::read32() {
|
||||
}
|
||||
|
||||
uint8_t SkReadBuffer::peekByte() {
|
||||
if (fReader.available() <= 0) {
|
||||
if (this->available() <= 0) {
|
||||
fError = true;
|
||||
return 0;
|
||||
}
|
||||
return *((uint8_t*) fReader.peek());
|
||||
return *((uint8_t*)fCurr);
|
||||
}
|
||||
|
||||
bool SkReadBuffer::readPad32(void* buffer, size_t bytes) {
|
||||
@ -180,7 +190,7 @@ void SkReadBuffer::readPoint3(SkPoint3* point) {
|
||||
void SkReadBuffer::readMatrix(SkMatrix* matrix) {
|
||||
size_t size = 0;
|
||||
if (this->isValid()) {
|
||||
size = SkMatrixPriv::ReadFromMemory(matrix, fReader.peek(), fReader.available());
|
||||
size = SkMatrixPriv::ReadFromMemory(matrix, fCurr, this->available());
|
||||
(void)this->validate((SkAlign4(size) == size) && (0 != size));
|
||||
}
|
||||
if (!this->isValid()) {
|
||||
@ -202,15 +212,20 @@ void SkReadBuffer::readRect(SkRect* rect) {
|
||||
}
|
||||
|
||||
void SkReadBuffer::readRRect(SkRRect* rrect) {
|
||||
if (!this->validate(fReader.readRRect(rrect))) {
|
||||
rrect->setEmpty();
|
||||
size_t size = 0;
|
||||
if (!fError) {
|
||||
size = rrect->readFromMemory(fCurr, this->available());
|
||||
if (!this->validate((SkAlign4(size) == size) && (0 != size))) {
|
||||
rrect->setEmpty();
|
||||
}
|
||||
}
|
||||
(void)this->skip(size);
|
||||
}
|
||||
|
||||
void SkReadBuffer::readRegion(SkRegion* region) {
|
||||
size_t size = 0;
|
||||
if (!fError) {
|
||||
size = region->readFromMemory(fReader.peek(), fReader.available());
|
||||
size = region->readFromMemory(fCurr, this->available());
|
||||
if (!this->validate((SkAlign4(size) == size) && (0 != size))) {
|
||||
region->setEmpty();
|
||||
}
|
||||
@ -221,7 +236,7 @@ void SkReadBuffer::readRegion(SkRegion* region) {
|
||||
void SkReadBuffer::readPath(SkPath* path) {
|
||||
size_t size = 0;
|
||||
if (!fError) {
|
||||
size = path->readFromMemory(fReader.peek(), fReader.available());
|
||||
size = path->readFromMemory(fCurr, this->available());
|
||||
if (!this->validate((SkAlign4(size) == size) && (0 != size))) {
|
||||
path->reset();
|
||||
}
|
||||
@ -270,7 +285,7 @@ const void* SkReadBuffer::skipByteArray(size_t* size) {
|
||||
|
||||
sk_sp<SkData> SkReadBuffer::readByteArrayAsData() {
|
||||
size_t numBytes = this->getArrayCount();
|
||||
if (!this->validate(fReader.isAvailable(numBytes))) {
|
||||
if (!this->validate(this->isAvailable(numBytes))) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@ -283,8 +298,10 @@ sk_sp<SkData> SkReadBuffer::readByteArrayAsData() {
|
||||
|
||||
uint32_t SkReadBuffer::getArrayCount() {
|
||||
const size_t inc = sizeof(uint32_t);
|
||||
fError = fError || !IsPtrAlign4(fReader.peek()) || !fReader.isAvailable(inc);
|
||||
return fError ? 0 : *(uint32_t*)fReader.peek();
|
||||
if (!this->validate(IsPtrAlign4(fCurr) && this->isAvailable(inc))) {
|
||||
return 0;
|
||||
}
|
||||
return *((uint32_t*)fCurr);
|
||||
}
|
||||
|
||||
/* Format:
|
||||
@ -431,10 +448,10 @@ SkFlattenable* SkReadBuffer::readFlattenable(SkFlattenable::Type ft) {
|
||||
sk_sp<SkFlattenable> obj;
|
||||
uint32_t sizeRecorded = this->read32();
|
||||
if (factory) {
|
||||
size_t offset = fReader.offset();
|
||||
size_t offset = this->offset();
|
||||
obj = (*factory)(*this);
|
||||
// check that we read the amount we expected
|
||||
size_t sizeRead = fReader.offset() - offset;
|
||||
size_t sizeRead = this->offset() - offset;
|
||||
if (sizeRecorded != sizeRead) {
|
||||
this->validate(false);
|
||||
return nullptr;
|
||||
@ -445,7 +462,7 @@ SkFlattenable* SkReadBuffer::readFlattenable(SkFlattenable::Type ft) {
|
||||
}
|
||||
} else {
|
||||
// we must skip the remaining data
|
||||
fReader.skip(sizeRecorded);
|
||||
this->skip(sizeRecorded);
|
||||
}
|
||||
if (!this->isValid()) {
|
||||
return nullptr;
|
||||
|
@ -16,11 +16,11 @@
|
||||
#include "include/core/SkPathEffect.h"
|
||||
#include "include/core/SkPicture.h"
|
||||
#include "include/core/SkRefCnt.h"
|
||||
#include "include/core/SkScalar.h"
|
||||
#include "include/core/SkSerialProcs.h"
|
||||
#include "src/core/SkMaskFilterBase.h"
|
||||
#include "src/core/SkPaintPriv.h"
|
||||
#include "src/core/SkPicturePriv.h"
|
||||
#include "src/core/SkReader32.h"
|
||||
#include "src/core/SkWriteBuffer.h"
|
||||
#include "src/shaders/SkShaderBase.h"
|
||||
|
||||
@ -50,12 +50,12 @@ public:
|
||||
fVersion = version;
|
||||
}
|
||||
|
||||
size_t size() const { return fReader.size(); }
|
||||
size_t offset() const { return fReader.offset(); }
|
||||
bool eof() { return fReader.eof(); }
|
||||
size_t size() const { return fStop - fBase; }
|
||||
size_t offset() const { return fCurr - fBase; }
|
||||
bool eof() { return fCurr >= fStop; }
|
||||
const void* skip(size_t size);
|
||||
const void* skip(size_t count, size_t size); // does safe multiply
|
||||
size_t available() const { return fReader.available(); }
|
||||
size_t available() const { return fStop - fCurr; }
|
||||
|
||||
template <typename T> const T* skipT() {
|
||||
return static_cast<const T*>(this->skip(sizeof(T)));
|
||||
@ -172,7 +172,7 @@ public:
|
||||
*/
|
||||
template <typename T>
|
||||
bool validateCanReadN(size_t n) {
|
||||
return this->validate(n <= (fReader.available() / sizeof(T)));
|
||||
return this->validate(n <= (this->available() / sizeof(T)));
|
||||
}
|
||||
|
||||
bool isValid() const { return !fError; }
|
||||
@ -198,8 +198,12 @@ private:
|
||||
|
||||
void setInvalid();
|
||||
bool readArray(void* value, size_t size, size_t elementSize);
|
||||
bool isAvailable(size_t size) const { return size <= this->available(); }
|
||||
|
||||
SkReader32 fReader;
|
||||
// These are always 4-byte aligned
|
||||
const char* fCurr; // current position within buffer
|
||||
const char* fStop; // end of buffer
|
||||
const char* fBase; // beginning of buffer
|
||||
|
||||
// Only used if we do not have an fFactoryArray.
|
||||
SkTHashMap<uint32_t, SkFlattenable::Factory> fFlattenableDict;
|
||||
|
@ -1,169 +0,0 @@
|
||||
/*
|
||||
* Copyright 2008 The Android Open Source Project
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license that can be
|
||||
* found in the LICENSE file.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef SkReader32_DEFINED
|
||||
#define SkReader32_DEFINED
|
||||
|
||||
#include "include/core/SkData.h"
|
||||
#include "include/core/SkMatrix.h"
|
||||
#include "include/core/SkPath.h"
|
||||
#include "include/core/SkRRect.h"
|
||||
#include "include/core/SkRegion.h"
|
||||
#include "include/core/SkScalar.h"
|
||||
#include "include/private/SkNoncopyable.h"
|
||||
|
||||
class SkString;
|
||||
|
||||
class SkReader32 : SkNoncopyable {
|
||||
public:
|
||||
SkReader32() : fCurr(nullptr), fStop(nullptr), fBase(nullptr) {}
|
||||
SkReader32(const void* data, size_t size) {
|
||||
this->setMemory(data, size);
|
||||
}
|
||||
|
||||
void setMemory(const void* data, size_t size) {
|
||||
SkASSERT(ptr_align_4(data));
|
||||
SkASSERT(SkAlign4(size) == size);
|
||||
|
||||
fBase = fCurr = (const char*)data;
|
||||
fStop = (const char*)data + size;
|
||||
}
|
||||
|
||||
size_t size() const { return fStop - fBase; }
|
||||
size_t offset() const { return fCurr - fBase; }
|
||||
bool eof() const { return fCurr >= fStop; }
|
||||
const void* base() const { return fBase; }
|
||||
const void* peek() const { return fCurr; }
|
||||
|
||||
size_t available() const { return fStop - fCurr; }
|
||||
bool isAvailable(size_t size) const { return size <= this->available(); }
|
||||
|
||||
void rewind() { fCurr = fBase; }
|
||||
|
||||
void setOffset(size_t offset) {
|
||||
SkASSERT(SkAlign4(offset) == offset);
|
||||
SkASSERT(offset <= this->size());
|
||||
fCurr = fBase + offset;
|
||||
}
|
||||
|
||||
bool readBool() { return this->readInt() != 0; }
|
||||
|
||||
int32_t readInt() {
|
||||
SkASSERT(ptr_align_4(fCurr));
|
||||
int32_t value = *(const int32_t*)fCurr;
|
||||
fCurr += sizeof(value);
|
||||
SkASSERT(fCurr <= fStop);
|
||||
return value;
|
||||
}
|
||||
|
||||
void* readPtr() {
|
||||
void* ptr;
|
||||
// we presume this "if" is resolved at compile-time
|
||||
if (4 == sizeof(void*)) {
|
||||
ptr = *(void**)fCurr;
|
||||
} else {
|
||||
memcpy(&ptr, fCurr, sizeof(void*));
|
||||
}
|
||||
fCurr += sizeof(void*);
|
||||
return ptr;
|
||||
}
|
||||
|
||||
SkScalar readScalar() {
|
||||
SkASSERT(ptr_align_4(fCurr));
|
||||
SkScalar value = *(const SkScalar*)fCurr;
|
||||
fCurr += sizeof(value);
|
||||
SkASSERT(fCurr <= fStop);
|
||||
return value;
|
||||
}
|
||||
|
||||
const void* skip(size_t size) {
|
||||
SkASSERT(ptr_align_4(fCurr));
|
||||
const void* addr = fCurr;
|
||||
fCurr += SkAlign4(size);
|
||||
SkASSERT(fCurr <= fStop);
|
||||
return addr;
|
||||
}
|
||||
|
||||
template <typename T> const T& skipT() {
|
||||
SkASSERT(SkAlign4(sizeof(T)) == sizeof(T));
|
||||
return *(const T*)this->skip(sizeof(T));
|
||||
}
|
||||
|
||||
void read(void* dst, size_t size) {
|
||||
SkASSERT(0 == size || dst != nullptr);
|
||||
SkASSERT(ptr_align_4(fCurr));
|
||||
sk_careful_memcpy(dst, fCurr, size);
|
||||
fCurr += SkAlign4(size);
|
||||
SkASSERT(fCurr <= fStop);
|
||||
}
|
||||
|
||||
uint8_t readU8() { return (uint8_t)this->readInt(); }
|
||||
uint16_t readU16() { return (uint16_t)this->readInt(); }
|
||||
int32_t readS32() { return this->readInt(); }
|
||||
uint32_t readU32() { return this->readInt(); }
|
||||
|
||||
bool readPath(SkPath* path) {
|
||||
return this->readObjectFromMemory(path);
|
||||
}
|
||||
|
||||
bool readMatrix(SkMatrix* matrix) {
|
||||
return this->readObjectFromMemory(matrix);
|
||||
}
|
||||
|
||||
bool readRRect(SkRRect* rrect) {
|
||||
return this->readObjectFromMemory(rrect);
|
||||
}
|
||||
|
||||
bool readRegion(SkRegion* rgn) {
|
||||
return this->readObjectFromMemory(rgn);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the length of a string (written by SkWriter32::writeString) into
|
||||
* len (if len is not nullptr) and return the null-ternimated address of the
|
||||
* string within the reader's buffer.
|
||||
*/
|
||||
const char* readString(size_t* len = nullptr);
|
||||
|
||||
/**
|
||||
* Read the string (written by SkWriter32::writeString) and return it in
|
||||
* copy (if copy is not null). Return the length of the string.
|
||||
*/
|
||||
size_t readIntoString(SkString* copy);
|
||||
|
||||
sk_sp<SkData> readData() {
|
||||
uint32_t byteLength = this->readU32();
|
||||
if (0 == byteLength) {
|
||||
return SkData::MakeEmpty();
|
||||
}
|
||||
return SkData::MakeWithCopy(this->skip(byteLength), byteLength);
|
||||
}
|
||||
|
||||
private:
|
||||
template <typename T> bool readObjectFromMemory(T* obj) {
|
||||
size_t size = obj->readFromMemory(this->peek(), this->available());
|
||||
// If readFromMemory() fails (which means that available() was too small), it returns 0
|
||||
bool success = (size > 0) && (size <= this->available()) && (SkAlign4(size) == size);
|
||||
// In case of failure, we want to skip to the end
|
||||
(void)this->skip(success ? size : this->available());
|
||||
return success;
|
||||
}
|
||||
|
||||
// these are always 4-byte aligned
|
||||
const char* fCurr; // current position within buffer
|
||||
const char* fStop; // end of buffer
|
||||
const char* fBase; // beginning of buffer
|
||||
|
||||
#ifdef SK_DEBUG
|
||||
static bool ptr_align_4(const void* ptr) {
|
||||
return (((const char*)ptr - (const char*)nullptr) & 3) == 0;
|
||||
}
|
||||
#endif
|
||||
};
|
||||
|
||||
#endif
|
@ -5,11 +5,8 @@
|
||||
* found in the LICENSE file.
|
||||
*/
|
||||
|
||||
#include "include/core/SkString.h"
|
||||
#include "include/private/SkTo.h"
|
||||
#include "src/core/SkMatrixPriv.h"
|
||||
#include "src/core/SkReader32.h"
|
||||
|
||||
#include "src/core/SkWriter32.h"
|
||||
|
||||
void SkWriter32::writeMatrix(const SkMatrix& matrix) {
|
||||
@ -18,33 +15,6 @@ void SkWriter32::writeMatrix(const SkMatrix& matrix) {
|
||||
SkMatrixPriv::WriteToMemory(matrix, this->reserve(size));
|
||||
}
|
||||
|
||||
/*
|
||||
* Strings are stored as: length[4-bytes] + string_data + '\0' + pad_to_mul_4
|
||||
*/
|
||||
|
||||
const char* SkReader32::readString(size_t* outLen) {
|
||||
size_t len = this->readU32();
|
||||
const void* ptr = this->peek();
|
||||
|
||||
// skip over the string + '\0' and then pad to a multiple of 4
|
||||
size_t alignedSize = SkAlign4(len + 1);
|
||||
this->skip(alignedSize);
|
||||
|
||||
if (outLen) {
|
||||
*outLen = len;
|
||||
}
|
||||
return (const char*)ptr;
|
||||
}
|
||||
|
||||
size_t SkReader32::readIntoString(SkString* copy) {
|
||||
size_t len;
|
||||
const char* ptr = this->readString(&len);
|
||||
if (copy) {
|
||||
copy->set(ptr, len);
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
void SkWriter32::writeString(const char str[], size_t len) {
|
||||
if (nullptr == str) {
|
||||
str = "";
|
||||
|
@ -110,12 +110,6 @@ public:
|
||||
*(int32_t*)this->reserve(sizeof(value)) = value;
|
||||
}
|
||||
|
||||
void writePtr(void* value) {
|
||||
// this->reserve() only returns 4-byte aligned pointers,
|
||||
// so this may be an under-aligned write if we were to do this like the others.
|
||||
memcpy(this->reserve(sizeof(value)), &value, sizeof(value));
|
||||
}
|
||||
|
||||
void writeScalar(SkScalar value) {
|
||||
*(SkScalar*)this->reserve(sizeof(value)) = value;
|
||||
}
|
||||
@ -190,10 +184,9 @@ public:
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes a string to the writer, which can be retrieved with
|
||||
* SkReader32::readString().
|
||||
* The length can be specified, or if -1 is passed, it will be computed by
|
||||
* calling strlen(). The length must be < max size_t.
|
||||
* Writes a string to the writer, which can be retrieved with SkReadBuffer::readString().
|
||||
* The length can be specified, or if -1 is passed, it will be computed by calling strlen().
|
||||
* The length must be < max size_t.
|
||||
*
|
||||
* If you write NULL, it will be read as "".
|
||||
*/
|
||||
|
@ -22,8 +22,8 @@
|
||||
#include "src/core/SkAutoMalloc.h"
|
||||
#include "src/core/SkGeometry.h"
|
||||
#include "src/core/SkPathPriv.h"
|
||||
#include "src/core/SkReader32.h"
|
||||
#include "src/core/SkWriter32.h"
|
||||
#include "src/core/SkReadBuffer.h"
|
||||
#include "src/core/SkWriteBuffer.h"
|
||||
#include "tests/Test.h"
|
||||
|
||||
#include <cmath>
|
||||
@ -2588,12 +2588,12 @@ static void test_isNestedFillRects(skiatest::Reporter* reporter) {
|
||||
|
||||
static void write_and_read_back(skiatest::Reporter* reporter,
|
||||
const SkPath& p) {
|
||||
SkWriter32 writer;
|
||||
SkBinaryWriteBuffer writer;
|
||||
writer.writePath(p);
|
||||
size_t size = writer.bytesWritten();
|
||||
SkAutoMalloc storage(size);
|
||||
writer.flatten(storage.get());
|
||||
SkReader32 reader(storage.get(), size);
|
||||
writer.writeToMemory(storage.get());
|
||||
SkReadBuffer reader(storage.get(), size);
|
||||
|
||||
SkPath readBack;
|
||||
REPORTER_ASSERT(reporter, readBack != p);
|
||||
|
@ -1,90 +0,0 @@
|
||||
/*
|
||||
* Copyright 2011 Google Inc.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license that can be
|
||||
* found in the LICENSE file.
|
||||
*/
|
||||
|
||||
#include "src/core/SkReader32.h"
|
||||
#include "tests/Test.h"
|
||||
|
||||
static void assert_eof(skiatest::Reporter* reporter, const SkReader32& reader) {
|
||||
REPORTER_ASSERT(reporter, reader.eof());
|
||||
REPORTER_ASSERT(reporter, reader.size() == reader.offset());
|
||||
REPORTER_ASSERT(reporter, (const char*)reader.peek() ==
|
||||
(const char*)reader.base() + reader.size());
|
||||
}
|
||||
|
||||
static void assert_start(skiatest::Reporter* reporter, const SkReader32& reader) {
|
||||
REPORTER_ASSERT(reporter, 0 == reader.offset());
|
||||
REPORTER_ASSERT(reporter, reader.size() == reader.available());
|
||||
REPORTER_ASSERT(reporter, reader.isAvailable(reader.size()));
|
||||
REPORTER_ASSERT(reporter, !reader.isAvailable(reader.size() + 1));
|
||||
REPORTER_ASSERT(reporter, reader.peek() == reader.base());
|
||||
}
|
||||
|
||||
static void assert_empty(skiatest::Reporter* reporter, const SkReader32& reader) {
|
||||
REPORTER_ASSERT(reporter, 0 == reader.size());
|
||||
REPORTER_ASSERT(reporter, 0 == reader.offset());
|
||||
REPORTER_ASSERT(reporter, 0 == reader.available());
|
||||
REPORTER_ASSERT(reporter, !reader.isAvailable(1));
|
||||
assert_eof(reporter, reader);
|
||||
assert_start(reporter, reader);
|
||||
}
|
||||
|
||||
DEF_TEST(Reader32, reporter) {
|
||||
SkReader32 reader;
|
||||
assert_empty(reporter, reader);
|
||||
REPORTER_ASSERT(reporter, nullptr == reader.base());
|
||||
REPORTER_ASSERT(reporter, nullptr == reader.peek());
|
||||
|
||||
size_t i;
|
||||
|
||||
const int32_t data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||
const SkScalar data2[] = { 0, SK_Scalar1, -SK_Scalar1, SK_Scalar1/2 };
|
||||
const size_t bufsize = sizeof(data) > sizeof(data2) ?
|
||||
sizeof(data) : sizeof(data2);
|
||||
char buffer[bufsize];
|
||||
|
||||
reader.setMemory(data, sizeof(data));
|
||||
for (i = 0; i < SK_ARRAY_COUNT(data); ++i) {
|
||||
REPORTER_ASSERT(reporter, sizeof(data) == reader.size());
|
||||
REPORTER_ASSERT(reporter, i*4 == reader.offset());
|
||||
REPORTER_ASSERT(reporter, (const void*)data == reader.base());
|
||||
REPORTER_ASSERT(reporter, (const void*)&data[i] == reader.peek());
|
||||
REPORTER_ASSERT(reporter, data[i] == reader.readInt());
|
||||
}
|
||||
assert_eof(reporter, reader);
|
||||
reader.rewind();
|
||||
assert_start(reporter, reader);
|
||||
reader.read(buffer, sizeof(data));
|
||||
REPORTER_ASSERT(reporter, !memcmp(data, buffer, sizeof(data)));
|
||||
|
||||
reader.setMemory(data2, sizeof(data2));
|
||||
for (i = 0; i < SK_ARRAY_COUNT(data2); ++i) {
|
||||
REPORTER_ASSERT(reporter, sizeof(data2) == reader.size());
|
||||
REPORTER_ASSERT(reporter, i*4 == reader.offset());
|
||||
REPORTER_ASSERT(reporter, (const void*)data2 == reader.base());
|
||||
REPORTER_ASSERT(reporter, (const void*)&data2[i] == reader.peek());
|
||||
REPORTER_ASSERT(reporter, data2[i] == reader.readScalar());
|
||||
}
|
||||
assert_eof(reporter, reader);
|
||||
reader.rewind();
|
||||
assert_start(reporter, reader);
|
||||
reader.read(buffer, sizeof(data2));
|
||||
REPORTER_ASSERT(reporter, !memcmp(data2, buffer, sizeof(data2)));
|
||||
|
||||
reader.setMemory(nullptr, 0);
|
||||
assert_empty(reporter, reader);
|
||||
REPORTER_ASSERT(reporter, nullptr == reader.base());
|
||||
REPORTER_ASSERT(reporter, nullptr == reader.peek());
|
||||
|
||||
// need to handle read(null, 0) and not get undefined behavior from memcpy
|
||||
{
|
||||
uint32_t storage[100 / sizeof(uint32_t)];
|
||||
reader.setMemory(storage, sizeof(storage));
|
||||
char buffer[10];
|
||||
reader.read(buffer, 0); // easy case, since we pass a ptr
|
||||
reader.read(nullptr, 0); // undef case, read() can't blindly call memcpy
|
||||
}
|
||||
}
|
@ -7,7 +7,7 @@
|
||||
|
||||
#include "include/utils/SkRandom.h"
|
||||
#include "src/core/SkAutoMalloc.h"
|
||||
#include "src/core/SkReader32.h"
|
||||
#include "src/core/SkReadBuffer.h"
|
||||
#include "src/core/SkWriter32.h"
|
||||
#include "tests/Test.h"
|
||||
|
||||
@ -70,34 +70,6 @@ static void test_rewind(skiatest::Reporter* reporter) {
|
||||
}
|
||||
}
|
||||
|
||||
static void test_ptr(skiatest::Reporter* reporter) {
|
||||
SkSWriter32<32> writer;
|
||||
|
||||
void* p0 = reporter;
|
||||
void* p1 = &writer;
|
||||
|
||||
// try writing ptrs where at least one of them may be at a non-multiple of
|
||||
// 8 boundary, to confirm this works on 64bit machines.
|
||||
|
||||
writer.writePtr(p0);
|
||||
writer.write8(0x33);
|
||||
writer.writePtr(p1);
|
||||
writer.write8(0x66);
|
||||
|
||||
size_t size = writer.bytesWritten();
|
||||
REPORTER_ASSERT(reporter, 2 * sizeof(void*) + 2 * sizeof(int32_t));
|
||||
|
||||
char buffer[32];
|
||||
SkASSERT(sizeof(buffer) >= size);
|
||||
writer.flatten(buffer);
|
||||
|
||||
SkReader32 reader(buffer, size);
|
||||
REPORTER_ASSERT(reporter, reader.readPtr() == p0);
|
||||
REPORTER_ASSERT(reporter, reader.readInt() == 0x33);
|
||||
REPORTER_ASSERT(reporter, reader.readPtr() == p1);
|
||||
REPORTER_ASSERT(reporter, reader.readInt() == 0x66);
|
||||
}
|
||||
|
||||
static void test1(skiatest::Reporter* reporter, SkWriter32* writer) {
|
||||
const uint32_t data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||
for (size_t i = 0; i < SK_ARRAY_COUNT(data); ++i) {
|
||||
@ -112,37 +84,6 @@ static void test1(skiatest::Reporter* reporter, SkWriter32* writer) {
|
||||
REPORTER_ASSERT(reporter, !memcmp(data, buffer, sizeof(buffer)));
|
||||
}
|
||||
|
||||
static void test2(skiatest::Reporter* reporter, SkWriter32* writer) {
|
||||
static const char gStr[] = "abcdefghimjklmnopqrstuvwxyz";
|
||||
size_t i;
|
||||
|
||||
size_t len = 0;
|
||||
for (i = 0; i <= 26; ++i) {
|
||||
len += SkWriter32::WriteStringSize(gStr, i);
|
||||
writer->writeString(gStr, i);
|
||||
}
|
||||
REPORTER_ASSERT(reporter, writer->bytesWritten() == len);
|
||||
|
||||
SkAutoMalloc storage(len);
|
||||
writer->flatten(storage.get());
|
||||
|
||||
SkReader32 reader;
|
||||
reader.setMemory(storage.get(), len);
|
||||
for (i = 0; i <= 26; ++i) {
|
||||
REPORTER_ASSERT(reporter, !reader.eof());
|
||||
const char* str = reader.readString(&len);
|
||||
REPORTER_ASSERT(reporter, i == len);
|
||||
REPORTER_ASSERT(reporter, strlen(str) == len);
|
||||
REPORTER_ASSERT(reporter, !memcmp(str, gStr, len));
|
||||
// Ensure that the align4 of the string is padded with zeroes.
|
||||
size_t alignedSize = SkAlign4(len + 1);
|
||||
for (size_t j = len; j < alignedSize; j++) {
|
||||
REPORTER_ASSERT(reporter, 0 == str[j]);
|
||||
}
|
||||
}
|
||||
REPORTER_ASSERT(reporter, reader.eof());
|
||||
}
|
||||
|
||||
static void testWritePad(skiatest::Reporter* reporter, SkWriter32* writer) {
|
||||
// Create some random data to write.
|
||||
const size_t dataSize = 10;
|
||||
@ -166,8 +107,7 @@ static void testWritePad(skiatest::Reporter* reporter, SkWriter32* writer) {
|
||||
SkAutoMalloc readStorage(totalBytes);
|
||||
writer->flatten(readStorage.get());
|
||||
|
||||
SkReader32 reader;
|
||||
reader.setMemory(readStorage.get(), totalBytes);
|
||||
SkReadBuffer reader(readStorage.get(), totalBytes);
|
||||
|
||||
for (size_t len = 0; len < dataSize; len++) {
|
||||
const char* readPtr = static_cast<const char*>(reader.skip(len));
|
||||
@ -223,9 +163,6 @@ DEF_TEST(Writer32_dynamic, reporter) {
|
||||
SkWriter32 writer;
|
||||
test1(reporter, &writer);
|
||||
|
||||
writer.reset();
|
||||
test2(reporter, &writer);
|
||||
|
||||
writer.reset();
|
||||
testWritePad(reporter, &writer);
|
||||
|
||||
@ -236,10 +173,8 @@ DEF_TEST(Writer32_dynamic, reporter) {
|
||||
DEF_TEST(Writer32_small, reporter) {
|
||||
SkSWriter32<8 * sizeof(intptr_t)> writer;
|
||||
test1(reporter, &writer);
|
||||
writer.reset(); // should just rewind our storage
|
||||
test2(reporter, &writer);
|
||||
|
||||
writer.reset();
|
||||
writer.reset(); // should just rewind our storage
|
||||
testWritePad(reporter, &writer);
|
||||
|
||||
writer.reset();
|
||||
@ -249,10 +184,8 @@ DEF_TEST(Writer32_small, reporter) {
|
||||
DEF_TEST(Writer32_large, reporter) {
|
||||
SkSWriter32<1024 * sizeof(intptr_t)> writer;
|
||||
test1(reporter, &writer);
|
||||
writer.reset(); // should just rewind our storage
|
||||
test2(reporter, &writer);
|
||||
|
||||
writer.reset();
|
||||
writer.reset(); // should just rewind our storage
|
||||
testWritePad(reporter, &writer);
|
||||
|
||||
writer.reset();
|
||||
@ -262,7 +195,6 @@ DEF_TEST(Writer32_large, reporter) {
|
||||
DEF_TEST(Writer32_misc, reporter) {
|
||||
test_reserve(reporter);
|
||||
test_string_null(reporter);
|
||||
test_ptr(reporter);
|
||||
test_rewind(reporter);
|
||||
}
|
||||
|
||||
@ -294,10 +226,10 @@ DEF_TEST(Writer32_data, reporter) {
|
||||
|
||||
auto result(writer.snapshotAsData());
|
||||
|
||||
SkReader32 reader(result->data(), result->size());
|
||||
auto d0(reader.readData()),
|
||||
d1(reader.readData()),
|
||||
d2(reader.readData());
|
||||
SkReadBuffer reader(result->data(), result->size());
|
||||
auto d0(reader.readByteArrayAsData()),
|
||||
d1(reader.readByteArrayAsData()),
|
||||
d2(reader.readByteArrayAsData());
|
||||
|
||||
REPORTER_ASSERT(reporter, 0 == d0->size());
|
||||
REPORTER_ASSERT(reporter, strlen(str)+1 == d1->size());
|
||||
|
Loading…
Reference in New Issue
Block a user