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