1 /* 2 * Copyright 2011 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 #ifndef SkReadBuffer_DEFINED 9 #define SkReadBuffer_DEFINED 10 11 #include "SkBitmapHeap.h" 12 #include "SkColorFilter.h" 13 #include "SkData.h" 14 #include "SkDrawLooper.h" 15 #include "SkImageFilter.h" 16 #include "SkMaskFilter.h" 17 #include "SkPath.h" 18 #include "SkPathEffect.h" 19 #include "SkPicture.h" 20 #include "SkRasterizer.h" 21 #include "SkReadBuffer.h" 22 #include "SkReader32.h" 23 #include "SkRefCnt.h" 24 #include "SkShader.h" 25 #include "SkWriteBuffer.h" 26 #include "SkXfermode.h" 27 28 class SkBitmap; 29 30 #if defined(SK_DEBUG) && defined(SK_BUILD_FOR_MAC) 31 #define DEBUG_NON_DETERMINISTIC_ASSERT 32 #endif 33 34 class SkReadBuffer { 35 public: 36 SkReadBuffer(); 37 SkReadBuffer(const void* data, size_t size); 38 SkReadBuffer(SkStream* stream); 39 virtual ~SkReadBuffer(); 40 41 enum Version { 42 kFilterLevelIsEnum_Version = 23, 43 kGradientFlippedFlag_Version = 24, 44 kDashWritesPhaseIntervals_Version = 25, 45 kColorShaderNoBool_Version = 26, 46 kNoUnitMappers_Version = 27, 47 kNoMoreBitmapFlatten_Version = 28, 48 kSimplifyLocalMatrix_Version = 30, 49 kImageFilterUniqueID_Version = 31, 50 kRemoveAndroidPaintOpts_Version = 32, 51 kFlattenCreateProc_Version = 33, 52 }; 53 54 /** 55 * Returns true IFF the version is older than the specified version. 56 */ 57 bool isVersionLT(Version targetVersion) const { 58 SkASSERT(targetVersion > 0); 59 return fVersion > 0 && fVersion < targetVersion; 60 } 61 62 /** This may be called at most once; most clients of SkReadBuffer should not mess with it. */ 63 void setVersion(int version) { 64 SkASSERT(0 == fVersion || version == fVersion); 65 fVersion = version; 66 } 67 68 enum Flags { 69 kCrossProcess_Flag = 1 << 0, 70 kScalarIsFloat_Flag = 1 << 1, 71 kPtrIs64Bit_Flag = 1 << 2, 72 kValidation_Flag = 1 << 3, 73 }; 74 75 void setFlags(uint32_t flags) { fFlags = flags; } 76 uint32_t getFlags() const { return fFlags; } 77 78 bool isCrossProcess() const { 79 return this->isValidating() || SkToBool(fFlags & kCrossProcess_Flag); 80 } 81 bool isScalarFloat() const { return SkToBool(fFlags & kScalarIsFloat_Flag); } 82 bool isPtr64Bit() const { return SkToBool(fFlags & kPtrIs64Bit_Flag); } 83 bool isValidating() const { return SkToBool(fFlags & kValidation_Flag); } 84 85 SkReader32* getReader32() { return &fReader; } 86 87 size_t size() { return fReader.size(); } 88 size_t offset() { return fReader.offset(); } 89 bool eof() { return fReader.eof(); } 90 virtual const void* skip(size_t size) { return fReader.skip(size); } 91 void* readFunctionPtr() { return fReader.readPtr(); } 92 93 // primitives 94 virtual bool readBool(); 95 virtual SkColor readColor(); 96 virtual SkFixed readFixed(); 97 virtual int32_t readInt(); 98 virtual SkScalar readScalar(); 99 virtual uint32_t readUInt(); 100 virtual int32_t read32(); 101 102 // strings -- the caller is responsible for freeing the string contents 103 virtual void readString(SkString* string); 104 virtual void* readEncodedString(size_t* length, SkPaint::TextEncoding encoding); 105 106 // common data structures 107 virtual void readPoint(SkPoint* point); 108 SkPoint readPoint() { SkPoint p; this->readPoint(&p); return p; } 109 virtual void readMatrix(SkMatrix* matrix); 110 virtual void readIRect(SkIRect* rect); 111 virtual void readRect(SkRect* rect); 112 virtual void readRegion(SkRegion* region); 113 114 virtual void readPath(SkPath* path); 115 void readPaint(SkPaint* paint) { paint->unflatten(*this); } 116 117 virtual SkFlattenable* readFlattenable(SkFlattenable::Type); 118 template <typename T> T* readFlattenable() { 119 return (T*) this->readFlattenable(T::GetFlattenableType()); 120 } 121 SkColorFilter* readColorFilter() { return this->readFlattenable<SkColorFilter>(); } 122 SkDrawLooper* readDrawLooper() { return this->readFlattenable<SkDrawLooper>(); } 123 SkImageFilter* readImageFilter() { return this->readFlattenable<SkImageFilter>(); } 124 SkMaskFilter* readMaskFilter() { return this->readFlattenable<SkMaskFilter>(); } 125 SkPathEffect* readPathEffect() { return this->readFlattenable<SkPathEffect>(); } 126 SkRasterizer* readRasterizer() { return this->readFlattenable<SkRasterizer>(); } 127 SkShader* readShader() { return this->readFlattenable<SkShader>(); } 128 SkXfermode* readXfermode() { return this->readFlattenable<SkXfermode>(); } 129 130 /** 131 * Like readFlattenable() but explicitly just skips the data that was written for the 132 * flattenable (or the sentinel that there wasn't one). 133 */ 134 virtual void skipFlattenable(); 135 136 // binary data and arrays 137 virtual bool readByteArray(void* value, size_t size); 138 virtual bool readColorArray(SkColor* colors, size_t size); 139 virtual bool readIntArray(int32_t* values, size_t size); 140 virtual bool readPointArray(SkPoint* points, size_t size); 141 virtual bool readScalarArray(SkScalar* values, size_t size); 142 143 SkData* readByteArrayAsData() { 144 size_t len = this->getArrayCount(); 145 if (!this->validateAvailable(len)) { 146 return SkData::NewEmpty(); 147 } 148 void* buffer = sk_malloc_throw(len); 149 this->readByteArray(buffer, len); 150 return SkData::NewFromMalloc(buffer, len); 151 } 152 153 // helpers to get info about arrays and binary data 154 virtual uint32_t getArrayCount(); 155 156 /** 157 * Returns false if the bitmap could not be completely read. In that case, it will be set 158 * to have width/height, but no pixels. 159 */ 160 bool readBitmap(SkBitmap* bitmap); 161 162 virtual SkTypeface* readTypeface(); 163 164 void setBitmapStorage(SkBitmapHeapReader* bitmapStorage) { 165 SkRefCnt_SafeAssign(fBitmapStorage, bitmapStorage); 166 } 167 168 void setTypefaceArray(SkTypeface* array[], int count) { 169 fTFArray = array; 170 fTFCount = count; 171 } 172 173 /** 174 * Call this with a pre-loaded array of Factories, in the same order as 175 * were created/written by the writer. SkPicture uses this. 176 */ 177 void setFactoryPlayback(SkFlattenable::Factory array[], int count) { 178 fFactoryTDArray = NULL; 179 fFactoryArray = array; 180 fFactoryCount = count; 181 } 182 183 /** 184 * Call this with an initially empty array, so the reader can cache each 185 * factory it sees by name. Used by the pipe code in conjunction with 186 * SkWriteBuffer::setNamedFactoryRecorder. 187 */ 188 void setFactoryArray(SkTDArray<SkFlattenable::Factory>* array) { 189 fFactoryTDArray = array; 190 fFactoryArray = NULL; 191 fFactoryCount = 0; 192 } 193 194 /** 195 * Provide a function to decode an SkBitmap from encoded data. Only used if the writer 196 * encoded the SkBitmap. If the proper decoder cannot be used, a red bitmap with the 197 * appropriate size will be used. 198 */ 199 void setBitmapDecoder(SkPicture::InstallPixelRefProc bitmapDecoder) { 200 fBitmapDecoder = bitmapDecoder; 201 } 202 203 // Default impelementations don't check anything. 204 virtual bool validate(bool isValid) { return true; } 205 virtual bool isValid() const { return true; } 206 virtual bool validateAvailable(size_t size) { return true; } 207 208 protected: 209 SkReader32 fReader; 210 211 private: 212 bool readArray(void* value, size_t size, size_t elementSize); 213 214 uint32_t fFlags; 215 int fVersion; 216 217 void* fMemoryPtr; 218 219 SkBitmapHeapReader* fBitmapStorage; 220 SkTypeface** fTFArray; 221 int fTFCount; 222 223 SkTDArray<SkFlattenable::Factory>* fFactoryTDArray; 224 SkFlattenable::Factory* fFactoryArray; 225 int fFactoryCount; 226 227 SkPicture::InstallPixelRefProc fBitmapDecoder; 228 229 #ifdef DEBUG_NON_DETERMINISTIC_ASSERT 230 // Debugging counter to keep track of how many bitmaps we 231 // have decoded. 232 int fDecodedBitmapIndex; 233 #endif // DEBUG_NON_DETERMINISTIC_ASSERT 234 }; 235 236 #endif // SkReadBuffer_DEFINED 237