1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/include/core/SkReadBuffer.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,207 @@ 1.4 + 1.5 +/* 1.6 + * Copyright 2011 Google Inc. 1.7 + * 1.8 + * Use of this source code is governed by a BSD-style license that can be 1.9 + * found in the LICENSE file. 1.10 + */ 1.11 + 1.12 +#ifndef SkReadBuffer_DEFINED 1.13 +#define SkReadBuffer_DEFINED 1.14 + 1.15 +#include "SkBitmapHeap.h" 1.16 +#include "SkColorFilter.h" 1.17 +#include "SkData.h" 1.18 +#include "SkDrawLooper.h" 1.19 +#include "SkImageFilter.h" 1.20 +#include "SkMaskFilter.h" 1.21 +#include "SkPath.h" 1.22 +#include "SkPathEffect.h" 1.23 +#include "SkPicture.h" 1.24 +#include "SkPixelRef.h" 1.25 +#include "SkRasterizer.h" 1.26 +#include "SkReadBuffer.h" 1.27 +#include "SkReader32.h" 1.28 +#include "SkRefCnt.h" 1.29 +#include "SkShader.h" 1.30 +#include "SkUnitMapper.h" 1.31 +#include "SkWriteBuffer.h" 1.32 +#include "SkXfermode.h" 1.33 + 1.34 +class SkBitmap; 1.35 + 1.36 +#if defined(SK_DEBUG) && defined(SK_BUILD_FOR_MAC) 1.37 + #define DEBUG_NON_DETERMINISTIC_ASSERT 1.38 +#endif 1.39 + 1.40 +class SkReadBuffer { 1.41 +public: 1.42 + SkReadBuffer(); 1.43 + SkReadBuffer(const void* data, size_t size); 1.44 + SkReadBuffer(SkStream* stream); 1.45 + virtual ~SkReadBuffer(); 1.46 + 1.47 + enum Flags { 1.48 + kCrossProcess_Flag = 1 << 0, 1.49 + kScalarIsFloat_Flag = 1 << 1, 1.50 + kPtrIs64Bit_Flag = 1 << 2, 1.51 + kValidation_Flag = 1 << 3, 1.52 + }; 1.53 + 1.54 + void setFlags(uint32_t flags) { fFlags = flags; } 1.55 + uint32_t getFlags() const { return fFlags; } 1.56 + 1.57 + bool isCrossProcess() const { 1.58 + return this->isValidating() || SkToBool(fFlags & kCrossProcess_Flag); 1.59 + } 1.60 + bool isScalarFloat() const { return SkToBool(fFlags & kScalarIsFloat_Flag); } 1.61 + bool isPtr64Bit() const { return SkToBool(fFlags & kPtrIs64Bit_Flag); } 1.62 + bool isValidating() const { return SkToBool(fFlags & kValidation_Flag); } 1.63 + 1.64 + SkReader32* getReader32() { return &fReader; } 1.65 + 1.66 + uint32_t size() { return fReader.size(); } 1.67 + uint32_t offset() { return fReader.offset(); } 1.68 + bool eof() { return fReader.eof(); } 1.69 + const void* skip(size_t size) { return fReader.skip(size); } 1.70 + 1.71 + // primitives 1.72 + virtual bool readBool(); 1.73 + virtual SkColor readColor(); 1.74 + virtual SkFixed readFixed(); 1.75 + virtual int32_t readInt(); 1.76 + virtual SkScalar readScalar(); 1.77 + virtual uint32_t readUInt(); 1.78 + virtual int32_t read32(); 1.79 + 1.80 + void* readFunctionPtr() { 1.81 + void* ptr; 1.82 + this->readByteArray(&ptr, sizeof(ptr)); 1.83 + return ptr; 1.84 + } 1.85 + 1.86 + // strings -- the caller is responsible for freeing the string contents 1.87 + virtual void readString(SkString* string); 1.88 + virtual void* readEncodedString(size_t* length, SkPaint::TextEncoding encoding); 1.89 + 1.90 + // common data structures 1.91 + virtual void readPoint(SkPoint* point); 1.92 + SkPoint readPoint() { SkPoint p; this->readPoint(&p); return p; } 1.93 + virtual void readMatrix(SkMatrix* matrix); 1.94 + virtual void readIRect(SkIRect* rect); 1.95 + virtual void readRect(SkRect* rect); 1.96 + virtual void readRegion(SkRegion* region); 1.97 + virtual void readPath(SkPath* path); 1.98 + void readPaint(SkPaint* paint) { paint->unflatten(*this); } 1.99 + 1.100 + virtual SkFlattenable* readFlattenable(SkFlattenable::Type); 1.101 + template <typename T> T* readFlattenable() { 1.102 + return (T*) this->readFlattenable(T::GetFlattenableType()); 1.103 + } 1.104 + SkColorFilter* readColorFilter() { return this->readFlattenable<SkColorFilter>(); } 1.105 + SkDrawLooper* readDrawLooper() { return this->readFlattenable<SkDrawLooper>(); } 1.106 + SkImageFilter* readImageFilter() { return this->readFlattenable<SkImageFilter>(); } 1.107 + SkMaskFilter* readMaskFilter() { return this->readFlattenable<SkMaskFilter>(); } 1.108 + SkPathEffect* readPathEffect() { return this->readFlattenable<SkPathEffect>(); } 1.109 + SkPixelRef* readPixelRef() { return this->readFlattenable<SkPixelRef>(); } 1.110 + SkRasterizer* readRasterizer() { return this->readFlattenable<SkRasterizer>(); } 1.111 + SkShader* readShader() { return this->readFlattenable<SkShader>(); } 1.112 + SkUnitMapper* readUnitMapper() { return this->readFlattenable<SkUnitMapper>(); } 1.113 + SkXfermode* readXfermode() { return this->readFlattenable<SkXfermode>(); } 1.114 + 1.115 + 1.116 + // binary data and arrays 1.117 + virtual bool readByteArray(void* value, size_t size); 1.118 + virtual bool readColorArray(SkColor* colors, size_t size); 1.119 + virtual bool readIntArray(int32_t* values, size_t size); 1.120 + virtual bool readPointArray(SkPoint* points, size_t size); 1.121 + virtual bool readScalarArray(SkScalar* values, size_t size); 1.122 + 1.123 + SkData* readByteArrayAsData() { 1.124 + size_t len = this->getArrayCount(); 1.125 + if (!this->validateAvailable(len)) { 1.126 + return SkData::NewEmpty(); 1.127 + } 1.128 + void* buffer = sk_malloc_throw(len); 1.129 + this->readByteArray(buffer, len); 1.130 + return SkData::NewFromMalloc(buffer, len); 1.131 + } 1.132 + 1.133 + // helpers to get info about arrays and binary data 1.134 + virtual uint32_t getArrayCount(); 1.135 + 1.136 + virtual void readBitmap(SkBitmap* bitmap); 1.137 + virtual SkTypeface* readTypeface(); 1.138 + 1.139 + void setBitmapStorage(SkBitmapHeapReader* bitmapStorage) { 1.140 + SkRefCnt_SafeAssign(fBitmapStorage, bitmapStorage); 1.141 + } 1.142 + 1.143 + void setTypefaceArray(SkTypeface* array[], int count) { 1.144 + fTFArray = array; 1.145 + fTFCount = count; 1.146 + } 1.147 + 1.148 + /** 1.149 + * Call this with a pre-loaded array of Factories, in the same order as 1.150 + * were created/written by the writer. SkPicture uses this. 1.151 + */ 1.152 + void setFactoryPlayback(SkFlattenable::Factory array[], int count) { 1.153 + fFactoryTDArray = NULL; 1.154 + fFactoryArray = array; 1.155 + fFactoryCount = count; 1.156 + } 1.157 + 1.158 + /** 1.159 + * Call this with an initially empty array, so the reader can cache each 1.160 + * factory it sees by name. Used by the pipe code in conjunction with 1.161 + * SkWriteBuffer::setNamedFactoryRecorder. 1.162 + */ 1.163 + void setFactoryArray(SkTDArray<SkFlattenable::Factory>* array) { 1.164 + fFactoryTDArray = array; 1.165 + fFactoryArray = NULL; 1.166 + fFactoryCount = 0; 1.167 + } 1.168 + 1.169 + /** 1.170 + * Provide a function to decode an SkBitmap from encoded data. Only used if the writer 1.171 + * encoded the SkBitmap. If the proper decoder cannot be used, a red bitmap with the 1.172 + * appropriate size will be used. 1.173 + */ 1.174 + void setBitmapDecoder(SkPicture::InstallPixelRefProc bitmapDecoder) { 1.175 + fBitmapDecoder = bitmapDecoder; 1.176 + } 1.177 + 1.178 + // Default impelementations don't check anything. 1.179 + virtual bool validate(bool isValid) { return true; } 1.180 + virtual bool isValid() const { return true; } 1.181 + virtual bool validateAvailable(size_t size) { return true; } 1.182 + 1.183 +protected: 1.184 + SkReader32 fReader; 1.185 + 1.186 +private: 1.187 + bool readArray(void* value, size_t size, size_t elementSize); 1.188 + 1.189 + uint32_t fFlags; 1.190 + 1.191 + void* fMemoryPtr; 1.192 + 1.193 + SkBitmapHeapReader* fBitmapStorage; 1.194 + SkTypeface** fTFArray; 1.195 + int fTFCount; 1.196 + 1.197 + SkTDArray<SkFlattenable::Factory>* fFactoryTDArray; 1.198 + SkFlattenable::Factory* fFactoryArray; 1.199 + int fFactoryCount; 1.200 + 1.201 + SkPicture::InstallPixelRefProc fBitmapDecoder; 1.202 + 1.203 +#ifdef DEBUG_NON_DETERMINISTIC_ASSERT 1.204 + // Debugging counter to keep track of how many bitmaps we 1.205 + // have decoded. 1.206 + int fDecodedBitmapIndex; 1.207 +#endif // DEBUG_NON_DETERMINISTIC_ASSERT 1.208 +}; 1.209 + 1.210 +#endif // SkReadBuffer_DEFINED