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 "SkColorFilter.h" 12 #include "SkData.h" 13 #include "SkDrawLooper.h" 14 #include "SkImageFilter.h" 15 #include "SkMaskFilter.h" 16 #include "SkPath.h" 17 #include "SkPathEffect.h" 18 #include "SkPicture.h" 19 #include "SkRasterizer.h" 20 #include "SkReadBuffer.h" 21 #include "SkReader32.h" 22 #include "SkRefCnt.h" 23 #include "SkShader.h" 24 #include "SkTHash.h" 25 #include "SkWriteBuffer.h" 26 #include "SkXfermodePriv.h" 27 28 class SkBitmap; 29 class SkImage; 30 class SkInflator; 31 32 #if defined(SK_DEBUG) && defined(SK_BUILD_FOR_MAC) 33 #define DEBUG_NON_DETERMINISTIC_ASSERT 34 #endif 35 36 class SkReadBuffer { 37 public: 38 SkReadBuffer(); 39 SkReadBuffer(const void* data, size_t size); 40 SkReadBuffer(SkStream* stream); 41 virtual ~SkReadBuffer(); 42 clone(const void * data,size_t size)43 virtual SkReadBuffer* clone(const void* data, size_t size) const { 44 return new SkReadBuffer(data, size); 45 } 46 47 enum Version { 48 /* 49 kFilterLevelIsEnum_Version = 23, 50 kGradientFlippedFlag_Version = 24, 51 kDashWritesPhaseIntervals_Version = 25, 52 kColorShaderNoBool_Version = 26, 53 kNoUnitMappers_Version = 27, 54 kNoMoreBitmapFlatten_Version = 28, 55 kSimplifyLocalMatrix_Version = 30, 56 kImageFilterUniqueID_Version = 31, 57 kRemoveAndroidPaintOpts_Version = 32, 58 kFlattenCreateProc_Version = 33, 59 */ 60 kRemoveColorTableAlpha_Version = 36, 61 kDropShadowMode_Version = 37, 62 kPictureImageFilterResolution_Version = 38, 63 kPictureImageFilterLevel_Version = 39, 64 kImageFilterNoUniqueID_Version = 40, 65 kBitmapSourceFilterQuality_Version = 41, 66 kPictureShaderHasPictureBool_Version = 42, 67 kHasDrawImageOpCodes_Version = 43, 68 kAnnotationsMovedToCanvas_Version = 44, 69 kLightingShaderWritesInvNormRotation = 45, 70 kBlurMaskFilterWritesOccluder = 47, 71 kGradientShaderFloatColor_Version = 49, 72 kXfermodeToBlendMode_Version = 50, 73 kXfermodeToBlendMode2_Version = 51, 74 kTextBlobImplicitRunCount_Version = 52, 75 }; 76 77 /** 78 * Returns true IFF the version is older than the specified version. 79 */ isVersionLT(Version targetVersion)80 bool isVersionLT(Version targetVersion) const { 81 SkASSERT(targetVersion > 0); 82 return fVersion > 0 && fVersion < targetVersion; 83 } 84 getVersion()85 uint32_t getVersion() const { return fVersion; } 86 87 /** This may be called at most once; most clients of SkReadBuffer should not mess with it. */ setVersion(int version)88 void setVersion(int version) { 89 SkASSERT(0 == fVersion || version == fVersion); 90 fVersion = version; 91 } 92 93 enum Flags { 94 kCrossProcess_Flag = 1 << 0, 95 kScalarIsFloat_Flag = 1 << 1, 96 kPtrIs64Bit_Flag = 1 << 2, 97 kValidation_Flag = 1 << 3, 98 }; 99 setFlags(uint32_t flags)100 void setFlags(uint32_t flags) { fFlags = flags; } getFlags()101 uint32_t getFlags() const { return fFlags; } 102 isCrossProcess()103 bool isCrossProcess() const { 104 return this->isValidating() || SkToBool(fFlags & kCrossProcess_Flag); 105 } isScalarFloat()106 bool isScalarFloat() const { return SkToBool(fFlags & kScalarIsFloat_Flag); } isPtr64Bit()107 bool isPtr64Bit() const { return SkToBool(fFlags & kPtrIs64Bit_Flag); } isValidating()108 bool isValidating() const { return SkToBool(fFlags & kValidation_Flag); } 109 size()110 size_t size() { return fReader.size(); } offset()111 size_t offset() { return fReader.offset(); } eof()112 bool eof() { return fReader.eof(); } skip(size_t size)113 virtual const void* skip(size_t size) { return fReader.skip(size); } 114 115 // primitives 116 virtual bool readBool(); 117 virtual SkColor readColor(); 118 virtual int32_t readInt(); 119 virtual SkScalar readScalar(); 120 virtual uint32_t readUInt(); 121 virtual int32_t read32(); 122 123 // peek 124 virtual uint8_t peekByte(); 125 126 // strings -- the caller is responsible for freeing the string contents 127 virtual void readString(SkString* string); 128 129 // common data structures 130 virtual void readColor4f(SkColor4f* color); 131 virtual void readPoint(SkPoint* point); readPoint()132 SkPoint readPoint() { SkPoint p; this->readPoint(&p); return p; } 133 virtual void readMatrix(SkMatrix* matrix); 134 virtual void readIRect(SkIRect* rect); 135 virtual void readRect(SkRect* rect); 136 virtual void readRRect(SkRRect* rrect); 137 virtual void readRegion(SkRegion* region); 138 139 virtual void readPath(SkPath* path); readPaint(SkPaint * paint)140 virtual void readPaint(SkPaint* paint) { paint->unflatten(*this); } 141 142 virtual SkFlattenable* readFlattenable(SkFlattenable::Type); readFlattenable()143 template <typename T> sk_sp<T> readFlattenable() { 144 return sk_sp<T>((T*)this->readFlattenable(T::GetFlattenableType())); 145 } readColorFilter()146 sk_sp<SkColorFilter> readColorFilter() { return this->readFlattenable<SkColorFilter>(); } readDrawLooper()147 sk_sp<SkDrawLooper> readDrawLooper() { return this->readFlattenable<SkDrawLooper>(); } readImageFilter()148 sk_sp<SkImageFilter> readImageFilter() { return this->readFlattenable<SkImageFilter>(); } readMaskFilter()149 sk_sp<SkMaskFilter> readMaskFilter() { return this->readFlattenable<SkMaskFilter>(); } readPathEffect()150 sk_sp<SkPathEffect> readPathEffect() { return this->readFlattenable<SkPathEffect>(); } readRasterizer()151 sk_sp<SkRasterizer> readRasterizer() { return this->readFlattenable<SkRasterizer>(); } readShader()152 sk_sp<SkShader> readShader() { return this->readFlattenable<SkShader>(); } readXfermode()153 sk_sp<SkXfermode> readXfermode() { return this->readFlattenable<SkXfermode>(); } 154 155 // binary data and arrays 156 virtual bool readByteArray(void* value, size_t size); 157 virtual bool readColorArray(SkColor* colors, size_t size); 158 virtual bool readColor4fArray(SkColor4f* colors, size_t size); 159 virtual bool readIntArray(int32_t* values, size_t size); 160 virtual bool readPointArray(SkPoint* points, size_t size); 161 virtual bool readScalarArray(SkScalar* values, size_t size); 162 readByteArrayAsData()163 sk_sp<SkData> readByteArrayAsData() { 164 size_t len = this->getArrayCount(); 165 if (!this->validateAvailable(len)) { 166 return SkData::MakeEmpty(); 167 } 168 void* buffer = sk_malloc_throw(len); 169 this->readByteArray(buffer, len); 170 return SkData::MakeFromMalloc(buffer, len); 171 } 172 173 // helpers to get info about arrays and binary data 174 virtual uint32_t getArrayCount(); 175 176 sk_sp<SkImage> readBitmapAsImage(); 177 sk_sp<SkImage> readImage(); 178 virtual sk_sp<SkTypeface> readTypeface(); 179 setTypefaceArray(SkTypeface * array[],int count)180 void setTypefaceArray(SkTypeface* array[], int count) { 181 fTFArray = array; 182 fTFCount = count; 183 } 184 185 /** 186 * Call this with a pre-loaded array of Factories, in the same order as 187 * were created/written by the writer. SkPicture uses this. 188 */ setFactoryPlayback(SkFlattenable::Factory array[],int count)189 void setFactoryPlayback(SkFlattenable::Factory array[], int count) { 190 fFactoryArray = array; 191 fFactoryCount = count; 192 } 193 194 /** 195 * For an input flattenable (specified by name), set a custom factory proc 196 * to use when unflattening. Will make a copy of |name|. 197 * 198 * If the global registry already has a default factory for the flattenable, 199 * this will override that factory. If a custom factory has already been 200 * set for the flattenable, this will override that factory. 201 * 202 * Custom factories can be removed by calling setCustomFactory("...", nullptr). 203 */ setCustomFactory(const SkString & name,SkFlattenable::Factory factory)204 void setCustomFactory(const SkString& name, SkFlattenable::Factory factory) { 205 fCustomFactory.set(name, factory); 206 } 207 208 // If nullptr is passed, then the default deserializer will be used 209 // which calls SkImage::MakeFromEncoded() 210 void setImageDeserializer(SkImageDeserializer* factory); 211 212 // Default impelementations don't check anything. validate(bool isValid)213 virtual bool validate(bool isValid) { return isValid; } isValid()214 virtual bool isValid() const { return true; } validateAvailable(size_t size)215 virtual bool validateAvailable(size_t size) { return true; } validateIndex(int index,int count)216 bool validateIndex(int index, int count) { 217 return this->validate(index >= 0 && index < count); 218 } 219 getInflator()220 SkInflator* getInflator() const { return fInflator; } setInflator(SkInflator * inf)221 void setInflator(SkInflator* inf) { fInflator = inf; } 222 223 // sk_sp<SkImage> inflateImage(); 224 225 protected: 226 /** 227 * Allows subclass to check if we are using factories for expansion 228 * of flattenables. 229 */ factoryCount()230 int factoryCount() { return fFactoryCount; } 231 232 /** 233 * Checks if a custom factory has been set for a given flattenable. 234 * Returns the custom factory if it exists, or nullptr otherwise. 235 */ getCustomFactory(const SkString & name)236 SkFlattenable::Factory getCustomFactory(const SkString& name) { 237 SkFlattenable::Factory* factoryPtr = fCustomFactory.find(name); 238 return factoryPtr ? *factoryPtr : nullptr; 239 } 240 241 SkReader32 fReader; 242 243 // Only used if we do not have an fFactoryArray. 244 SkTHashMap<uint32_t, SkString> fFlattenableDict; 245 246 private: 247 bool readArray(void* value, size_t size, size_t elementSize); 248 249 uint32_t fFlags; 250 int fVersion; 251 252 void* fMemoryPtr; 253 254 SkTypeface** fTFArray; 255 int fTFCount; 256 257 SkFlattenable::Factory* fFactoryArray; 258 int fFactoryCount; 259 260 // Only used if we do not have an fFactoryArray. 261 SkTHashMap<SkString, SkFlattenable::Factory> fCustomFactory; 262 263 // We do not own this ptr, we just use it (guaranteed to never be null) 264 SkImageDeserializer* fImageDeserializer; 265 266 #ifdef DEBUG_NON_DETERMINISTIC_ASSERT 267 // Debugging counter to keep track of how many bitmaps we 268 // have decoded. 269 int fDecodedBitmapIndex; 270 #endif // DEBUG_NON_DETERMINISTIC_ASSERT 271 272 SkInflator* fInflator = nullptr; 273 }; 274 275 #endif // SkReadBuffer_DEFINED 276