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