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