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