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