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, |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 63 | kAnnotationsMovedToCanvas_Version = 44, |
commit-bot@chromium.org | 7ed173b | 2014-05-20 17:31:08 +0000 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | /** |
| 67 | * Returns true IFF the version is older than the specified version. |
| 68 | */ |
| 69 | bool isVersionLT(Version targetVersion) const { |
| 70 | SkASSERT(targetVersion > 0); |
| 71 | return fVersion > 0 && fVersion < targetVersion; |
| 72 | } |
commit-bot@chromium.org | 0943f5f | 2014-03-28 18:05:47 +0000 | [diff] [blame] | 73 | |
| 74 | /** 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] | 75 | void setVersion(int version) { |
| 76 | SkASSERT(0 == fVersion || version == fVersion); |
| 77 | fVersion = version; |
commit-bot@chromium.org | 0943f5f | 2014-03-28 18:05:47 +0000 | [diff] [blame] | 78 | } |
| 79 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 80 | enum Flags { |
| 81 | kCrossProcess_Flag = 1 << 0, |
| 82 | kScalarIsFloat_Flag = 1 << 1, |
| 83 | kPtrIs64Bit_Flag = 1 << 2, |
| 84 | kValidation_Flag = 1 << 3, |
| 85 | }; |
| 86 | |
| 87 | void setFlags(uint32_t flags) { fFlags = flags; } |
| 88 | uint32_t getFlags() const { return fFlags; } |
| 89 | |
| 90 | bool isCrossProcess() const { |
| 91 | return this->isValidating() || SkToBool(fFlags & kCrossProcess_Flag); |
| 92 | } |
| 93 | bool isScalarFloat() const { return SkToBool(fFlags & kScalarIsFloat_Flag); } |
| 94 | bool isPtr64Bit() const { return SkToBool(fFlags & kPtrIs64Bit_Flag); } |
| 95 | bool isValidating() const { return SkToBool(fFlags & kValidation_Flag); } |
| 96 | |
| 97 | SkReader32* getReader32() { return &fReader; } |
| 98 | |
commit-bot@chromium.org | f117781 | 2014-04-23 19:19:44 +0000 | [diff] [blame] | 99 | size_t size() { return fReader.size(); } |
| 100 | size_t offset() { return fReader.offset(); } |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 101 | bool eof() { return fReader.eof(); } |
sugoi | 0951fe1 | 2014-06-06 06:44:16 -0700 | [diff] [blame] | 102 | 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] | 103 | void* readFunctionPtr() { return fReader.readPtr(); } |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 104 | |
| 105 | // primitives |
| 106 | virtual bool readBool(); |
| 107 | virtual SkColor readColor(); |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 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 | |
mtklein | 3b37545 | 2016-04-04 14:57:19 -0700 | [diff] [blame] | 128 | virtual SkFlattenable* readFlattenable(SkFlattenable::Type); |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 129 | template <typename T> sk_sp<T> readFlattenable() { |
mtklein | 3b37545 | 2016-04-04 14:57:19 -0700 | [diff] [blame] | 130 | return sk_sp<T>((T*)this->readFlattenable(T::GetFlattenableType())); |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 131 | } |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 132 | sk_sp<SkColorFilter> readColorFilter() { return this->readFlattenable<SkColorFilter>(); } |
| 133 | sk_sp<SkDrawLooper> readDrawLooper() { return this->readFlattenable<SkDrawLooper>(); } |
| 134 | sk_sp<SkImageFilter> readImageFilter() { return this->readFlattenable<SkImageFilter>(); } |
| 135 | sk_sp<SkMaskFilter> readMaskFilter() { return this->readFlattenable<SkMaskFilter>(); } |
| 136 | sk_sp<SkPathEffect> readPathEffect() { return this->readFlattenable<SkPathEffect>(); } |
| 137 | sk_sp<SkRasterizer> readRasterizer() { return this->readFlattenable<SkRasterizer>(); } |
| 138 | sk_sp<SkShader> readShader() { return this->readFlattenable<SkShader>(); } |
| 139 | sk_sp<SkXfermode> readXfermode() { return this->readFlattenable<SkXfermode>(); } |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 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 | |
reed | fde0511 | 2016-03-11 13:02:28 -0800 | [diff] [blame] | 154 | sk_sp<SkData> readByteArrayAsData() { |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 155 | size_t len = this->getArrayCount(); |
| 156 | if (!this->validateAvailable(len)) { |
reed | fde0511 | 2016-03-11 13:02:28 -0800 | [diff] [blame] | 157 | return SkData::MakeEmpty(); |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 158 | } |
| 159 | void* buffer = sk_malloc_throw(len); |
| 160 | this->readByteArray(buffer, len); |
reed | fde0511 | 2016-03-11 13:02:28 -0800 | [diff] [blame] | 161 | return SkData::MakeFromMalloc(buffer, len); |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 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) { |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 191 | fFactoryArray = array; |
| 192 | fFactoryCount = count; |
| 193 | } |
| 194 | |
| 195 | /** |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 196 | * Provide a function to decode an SkBitmap from encoded data. Only used if the writer |
| 197 | * encoded the SkBitmap. If the proper decoder cannot be used, a red bitmap with the |
| 198 | * appropriate size will be used. |
| 199 | */ |
| 200 | void setBitmapDecoder(SkPicture::InstallPixelRefProc bitmapDecoder) { |
| 201 | fBitmapDecoder = bitmapDecoder; |
| 202 | } |
| 203 | |
| 204 | // Default impelementations don't check anything. |
| 205 | virtual bool validate(bool isValid) { return true; } |
| 206 | virtual bool isValid() const { return true; } |
| 207 | virtual bool validateAvailable(size_t size) { return true; } |
| 208 | |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 209 | protected: |
| 210 | SkReader32 fReader; |
| 211 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 212 | private: |
| 213 | bool readArray(void* value, size_t size, size_t elementSize); |
| 214 | |
| 215 | uint32_t fFlags; |
commit-bot@chromium.org | 7ed173b | 2014-05-20 17:31:08 +0000 | [diff] [blame] | 216 | int fVersion; |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 217 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 218 | void* fMemoryPtr; |
| 219 | |
| 220 | SkBitmapHeapReader* fBitmapStorage; |
| 221 | SkTypeface** fTFArray; |
| 222 | int fTFCount; |
| 223 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 224 | SkFlattenable::Factory* fFactoryArray; |
| 225 | int fFactoryCount; |
| 226 | |
| 227 | SkPicture::InstallPixelRefProc fBitmapDecoder; |
| 228 | |
| 229 | #ifdef DEBUG_NON_DETERMINISTIC_ASSERT |
| 230 | // Debugging counter to keep track of how many bitmaps we |
| 231 | // have decoded. |
| 232 | int fDecodedBitmapIndex; |
| 233 | #endif // DEBUG_NON_DETERMINISTIC_ASSERT |
| 234 | }; |
| 235 | |
| 236 | #endif // SkReadBuffer_DEFINED |