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