diff options
author | halcanary <halcanary@google.com> | 2014-08-18 13:48:13 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2014-08-18 13:48:13 -0700 |
commit | f128f53a5bc36e2a991302cb40848df580ad515f (patch) | |
tree | 8b3261a94adbd3d6fa7b8b6c078c9a7f565a5022 /include | |
parent | 3e8232b6ef6b04290f0c280f88b38f8cd2c3f3ec (diff) |
Revert "Move SkReadBuffer.h and SkReader32.h out of include."
This reverts commit 2a51d7c74cec217195f861677de8998b382b39e4.
Breaking Blink
NOTRY=true
R=bungeman@google.com
TBR=bungeman@google.com
Author: halcanary@google.com
Review URL: https://codereview.chromium.org/472303006
Diffstat (limited to 'include')
-rw-r--r-- | include/core/SkFlattenableBuffers.h | 10 | ||||
-rw-r--r-- | include/core/SkReadBuffer.h | 235 | ||||
-rw-r--r-- | include/core/SkReader32.h | 160 |
3 files changed, 405 insertions, 0 deletions
diff --git a/include/core/SkFlattenableBuffers.h b/include/core/SkFlattenableBuffers.h new file mode 100644 index 0000000000..3e5d5b94ee --- /dev/null +++ b/include/core/SkFlattenableBuffers.h @@ -0,0 +1,10 @@ +// Temporary shim to keep a couple dependencies working in Chromium. +#ifndef SkFlattenableBuffers_DEFINED +#define SkFlattenableBuffers_DEFINED + +#include "SkReadBuffer.h" +#include "SkWriteBuffer.h" + +typedef SkReadBuffer SkFlattenableReadBuffer; + +#endif//SkFlattenableBuffers_DEFINED diff --git a/include/core/SkReadBuffer.h b/include/core/SkReadBuffer.h new file mode 100644 index 0000000000..cd3468670d --- /dev/null +++ b/include/core/SkReadBuffer.h @@ -0,0 +1,235 @@ +/* + * Copyright 2011 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef SkReadBuffer_DEFINED +#define SkReadBuffer_DEFINED + +#include "SkBitmapHeap.h" +#include "SkColorFilter.h" +#include "SkData.h" +#include "SkDrawLooper.h" +#include "SkImageFilter.h" +#include "SkMaskFilter.h" +#include "SkPath.h" +#include "SkPathEffect.h" +#include "SkPicture.h" +#include "SkRasterizer.h" +#include "SkReadBuffer.h" +#include "SkReader32.h" +#include "SkRefCnt.h" +#include "SkShader.h" +#include "SkWriteBuffer.h" +#include "SkXfermode.h" + +class SkBitmap; + +#if defined(SK_DEBUG) && defined(SK_BUILD_FOR_MAC) + #define DEBUG_NON_DETERMINISTIC_ASSERT +#endif + +class SkReadBuffer { +public: + SkReadBuffer(); + SkReadBuffer(const void* data, size_t size); + SkReadBuffer(SkStream* stream); + virtual ~SkReadBuffer(); + + enum Version { + kFilterLevelIsEnum_Version = 23, + kGradientFlippedFlag_Version = 24, + kDashWritesPhaseIntervals_Version = 25, + kColorShaderNoBool_Version = 26, + kNoUnitMappers_Version = 27, + kNoMoreBitmapFlatten_Version = 28, + kSimplifyLocalMatrix_Version = 30, + kImageFilterUniqueID_Version = 31, + kRemoveAndroidPaintOpts_Version = 32, + }; + + /** + * Returns true IFF the version is older than the specified version. + */ + bool isVersionLT(Version targetVersion) const { + SkASSERT(targetVersion > 0); + return fVersion > 0 && fVersion < targetVersion; + } + + /** This may be called at most once; most clients of SkReadBuffer should not mess with it. */ + void setVersion(int version) { + SkASSERT(0 == fVersion || version == fVersion); + fVersion = version; + } + + enum Flags { + kCrossProcess_Flag = 1 << 0, + kScalarIsFloat_Flag = 1 << 1, + kPtrIs64Bit_Flag = 1 << 2, + kValidation_Flag = 1 << 3, + }; + + void setFlags(uint32_t flags) { fFlags = flags; } + uint32_t getFlags() const { return fFlags; } + + bool isCrossProcess() const { + return this->isValidating() || SkToBool(fFlags & kCrossProcess_Flag); + } + bool isScalarFloat() const { return SkToBool(fFlags & kScalarIsFloat_Flag); } + bool isPtr64Bit() const { return SkToBool(fFlags & kPtrIs64Bit_Flag); } + bool isValidating() const { return SkToBool(fFlags & kValidation_Flag); } + + SkReader32* getReader32() { return &fReader; } + + size_t size() { return fReader.size(); } + size_t offset() { return fReader.offset(); } + bool eof() { return fReader.eof(); } + virtual const void* skip(size_t size) { return fReader.skip(size); } + void* readFunctionPtr() { return fReader.readPtr(); } + + // primitives + virtual bool readBool(); + virtual SkColor readColor(); + virtual SkFixed readFixed(); + virtual int32_t readInt(); + virtual SkScalar readScalar(); + virtual uint32_t readUInt(); + virtual int32_t read32(); + + // strings -- the caller is responsible for freeing the string contents + virtual void readString(SkString* string); + virtual void* readEncodedString(size_t* length, SkPaint::TextEncoding encoding); + + // common data structures + virtual void readPoint(SkPoint* point); + SkPoint readPoint() { SkPoint p; this->readPoint(&p); return p; } + virtual void readMatrix(SkMatrix* matrix); + virtual void readIRect(SkIRect* rect); + virtual void readRect(SkRect* rect); + virtual void readRegion(SkRegion* region); + + virtual void readPath(SkPath* path); + void readPaint(SkPaint* paint) { paint->unflatten(*this); } + + virtual SkFlattenable* readFlattenable(SkFlattenable::Type); + template <typename T> T* readFlattenable() { + return (T*) this->readFlattenable(T::GetFlattenableType()); + } + SkColorFilter* readColorFilter() { return this->readFlattenable<SkColorFilter>(); } + SkDrawLooper* readDrawLooper() { return this->readFlattenable<SkDrawLooper>(); } + SkImageFilter* readImageFilter() { return this->readFlattenable<SkImageFilter>(); } + SkMaskFilter* readMaskFilter() { return this->readFlattenable<SkMaskFilter>(); } + SkPathEffect* readPathEffect() { return this->readFlattenable<SkPathEffect>(); } + SkRasterizer* readRasterizer() { return this->readFlattenable<SkRasterizer>(); } + SkShader* readShader() { return this->readFlattenable<SkShader>(); } + SkXfermode* readXfermode() { return this->readFlattenable<SkXfermode>(); } + + /** + * Like readFlattenable() but explicitly just skips the data that was written for the + * flattenable (or the sentinel that there wasn't one). + */ + virtual void skipFlattenable(); + + // binary data and arrays + virtual bool readByteArray(void* value, size_t size); + virtual bool readColorArray(SkColor* colors, size_t size); + virtual bool readIntArray(int32_t* values, size_t size); + virtual bool readPointArray(SkPoint* points, size_t size); + virtual bool readScalarArray(SkScalar* values, size_t size); + + SkData* readByteArrayAsData() { + size_t len = this->getArrayCount(); + if (!this->validateAvailable(len)) { + return SkData::NewEmpty(); + } + void* buffer = sk_malloc_throw(len); + this->readByteArray(buffer, len); + return SkData::NewFromMalloc(buffer, len); + } + + // helpers to get info about arrays and binary data + virtual uint32_t getArrayCount(); + + /** + * Returns false if the bitmap could not be completely read. In that case, it will be set + * to have width/height, but no pixels. + */ + bool readBitmap(SkBitmap* bitmap); + + virtual SkTypeface* readTypeface(); + + void setBitmapStorage(SkBitmapHeapReader* bitmapStorage) { + SkRefCnt_SafeAssign(fBitmapStorage, bitmapStorage); + } + + void setTypefaceArray(SkTypeface* array[], int count) { + fTFArray = array; + fTFCount = count; + } + + /** + * Call this with a pre-loaded array of Factories, in the same order as + * were created/written by the writer. SkPicture uses this. + */ + void setFactoryPlayback(SkFlattenable::Factory array[], int count) { + fFactoryTDArray = NULL; + fFactoryArray = array; + fFactoryCount = count; + } + + /** + * Call this with an initially empty array, so the reader can cache each + * factory it sees by name. Used by the pipe code in conjunction with + * SkWriteBuffer::setNamedFactoryRecorder. + */ + void setFactoryArray(SkTDArray<SkFlattenable::Factory>* array) { + fFactoryTDArray = array; + fFactoryArray = NULL; + fFactoryCount = 0; + } + + /** + * Provide a function to decode an SkBitmap from encoded data. Only used if the writer + * encoded the SkBitmap. If the proper decoder cannot be used, a red bitmap with the + * appropriate size will be used. + */ + void setBitmapDecoder(SkPicture::InstallPixelRefProc bitmapDecoder) { + fBitmapDecoder = bitmapDecoder; + } + + // Default impelementations don't check anything. + virtual bool validate(bool isValid) { return true; } + virtual bool isValid() const { return true; } + virtual bool validateAvailable(size_t size) { return true; } + +protected: + SkReader32 fReader; + +private: + bool readArray(void* value, size_t size, size_t elementSize); + + uint32_t fFlags; + int fVersion; + + void* fMemoryPtr; + + SkBitmapHeapReader* fBitmapStorage; + SkTypeface** fTFArray; + int fTFCount; + + SkTDArray<SkFlattenable::Factory>* fFactoryTDArray; + SkFlattenable::Factory* fFactoryArray; + int fFactoryCount; + + SkPicture::InstallPixelRefProc fBitmapDecoder; + +#ifdef DEBUG_NON_DETERMINISTIC_ASSERT + // Debugging counter to keep track of how many bitmaps we + // have decoded. + int fDecodedBitmapIndex; +#endif // DEBUG_NON_DETERMINISTIC_ASSERT +}; + +#endif // SkReadBuffer_DEFINED diff --git a/include/core/SkReader32.h b/include/core/SkReader32.h new file mode 100644 index 0000000000..3d874d170a --- /dev/null +++ b/include/core/SkReader32.h @@ -0,0 +1,160 @@ + +/* + * 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 "SkMatrix.h" +#include "SkPath.h" +#include "SkRegion.h" +#include "SkRRect.h" +#include "SkScalar.h" + +class SkString; + +class SkReader32 : SkNoncopyable { +public: + SkReader32() : fCurr(NULL), fStop(NULL), fBase(NULL) {} + 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 != NULL); + SkASSERT(ptr_align_4(fCurr)); + 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 NULL) and return the null-ternimated address of the + * string within the reader's buffer. + */ + const char* readString(size_t* len = NULL); + + /** + * 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); + +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*)NULL) & 3) == 0; + } +#endif +}; + +#endif |