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