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