blob: c853c0003623d0adfa6142f49d25cf8e226bf53e [file] [log] [blame]
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +00001/*
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.org8b0e8ac2014-01-30 18:58:24 +000011#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.org8b0e8ac2014-01-30 18:58:24 +000019#include "SkRasterizer.h"
20#include "SkReadBuffer.h"
21#include "SkReader32.h"
22#include "SkRefCnt.h"
23#include "SkShader.h"
msaretta3b3b232016-04-22 12:43:07 -070024#include "SkTHash.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000025#include "SkWriteBuffer.h"
26#include "SkXfermode.h"
27
28class SkBitmap;
fmalitacd56f812015-09-14 13:31:18 -070029class SkImage;
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000030
31#if defined(SK_DEBUG) && defined(SK_BUILD_FOR_MAC)
32 #define DEBUG_NON_DETERMINISTIC_ASSERT
33#endif
34
35class SkReadBuffer {
36public:
37 SkReadBuffer();
38 SkReadBuffer(const void* data, size_t size);
39 SkReadBuffer(SkStream* stream);
40 virtual ~SkReadBuffer();
41
robertphillips99e20892016-04-22 11:40:42 -070042 virtual SkReadBuffer* clone(const void* data, size_t size) const {
43 return new SkReadBuffer(data, size);
44 }
45
commit-bot@chromium.org7ed173b2014-05-20 17:31:08 +000046 enum Version {
mtklein88fd0fb2014-12-01 06:56:38 -080047 /*
commit-bot@chromium.org7ed173b2014-05-20 17:31:08 +000048 kFilterLevelIsEnum_Version = 23,
49 kGradientFlippedFlag_Version = 24,
50 kDashWritesPhaseIntervals_Version = 25,
51 kColorShaderNoBool_Version = 26,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000052 kNoUnitMappers_Version = 27,
commit-bot@chromium.org968edca2014-05-23 13:21:55 +000053 kNoMoreBitmapFlatten_Version = 28,
scroggoc870d492014-07-11 10:42:12 -070054 kSimplifyLocalMatrix_Version = 30,
senorblanco55b6d8b2014-07-30 11:26:46 -070055 kImageFilterUniqueID_Version = 31,
djsollen3b625542014-08-14 06:29:02 -070056 kRemoveAndroidPaintOpts_Version = 32,
reed9fa60da2014-08-21 07:59:51 -070057 kFlattenCreateProc_Version = 33,
mtklein88fd0fb2014-12-01 06:56:38 -080058 */
reedc5e15a12014-09-29 12:10:27 -070059 kRemoveColorTableAlpha_Version = 36,
sugoi234f0362014-10-23 13:59:52 -070060 kDropShadowMode_Version = 37,
Justin Novosad52340752014-12-02 14:50:56 -050061 kPictureImageFilterResolution_Version = 38,
junovf3c78cc2014-12-09 13:07:22 -080062 kPictureImageFilterLevel_Version = 39,
senorblanco4a22a432015-03-18 13:14:54 -070063 kImageFilterNoUniqueID_Version = 40,
mtklein76be9c82015-05-20 12:05:15 -070064 kBitmapSourceFilterQuality_Version = 41,
65 kPictureShaderHasPictureBool_Version = 42,
reed871872f2015-06-22 12:48:26 -070066 kHasDrawImageOpCodes_Version = 43,
reedf70b5312016-03-04 16:36:20 -080067 kAnnotationsMovedToCanvas_Version = 44,
mtklein0576aa92016-04-08 12:19:50 -070068 kLightingShaderWritesInvNormRotation = 45,
commit-bot@chromium.org7ed173b2014-05-20 17:31:08 +000069 };
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.org0943f5f2014-03-28 18:05:47 +000078
79 /** This may be called at most once; most clients of SkReadBuffer should not mess with it. */
commit-bot@chromium.org7ed173b2014-05-20 17:31:08 +000080 void setVersion(int version) {
81 SkASSERT(0 == fVersion || version == fVersion);
82 fVersion = version;
commit-bot@chromium.org0943f5f2014-03-28 18:05:47 +000083 }
84
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000085 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.orgf1177812014-04-23 19:19:44 +0000104 size_t size() { return fReader.size(); }
105 size_t offset() { return fReader.offset(); }
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000106 bool eof() { return fReader.eof(); }
sugoi0951fe12014-06-06 06:44:16 -0700107 virtual const void* skip(size_t size) { return fReader.skip(size); }
robertphillips9ca06c42016-04-20 11:43:33 -0700108
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000109 // primitives
110 virtual bool readBool();
111 virtual SkColor readColor();
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000112 virtual int32_t readInt();
113 virtual SkScalar readScalar();
114 virtual uint32_t readUInt();
115 virtual int32_t read32();
116
msaretta3b3b232016-04-22 12:43:07 -0700117 // peek
118 virtual uint8_t peekByte();
119
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000120 // strings -- the caller is responsible for freeing the string contents
121 virtual void readString(SkString* string);
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000122
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);
robertphillips9ca06c42016-04-20 11:43:33 -0700129 virtual void readRRect(SkRRect* rrect);
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000130 virtual void readRegion(SkRegion* region);
mtklein88fd0fb2014-12-01 06:56:38 -0800131
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000132 virtual void readPath(SkPath* path);
133 void readPaint(SkPaint* paint) { paint->unflatten(*this); }
134
mtklein3b375452016-04-04 14:57:19 -0700135 virtual SkFlattenable* readFlattenable(SkFlattenable::Type);
reed60c9b582016-04-03 09:11:13 -0700136 template <typename T> sk_sp<T> readFlattenable() {
mtklein3b375452016-04-04 14:57:19 -0700137 return sk_sp<T>((T*)this->readFlattenable(T::GetFlattenableType()));
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000138 }
reed60c9b582016-04-03 09:11:13 -0700139 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.org8b0e8ac2014-01-30 18:58:24 +0000147
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000148 // 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
reedfde05112016-03-11 13:02:28 -0800155 sk_sp<SkData> readByteArrayAsData() {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000156 size_t len = this->getArrayCount();
157 if (!this->validateAvailable(len)) {
reedfde05112016-03-11 13:02:28 -0800158 return SkData::MakeEmpty();
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000159 }
160 void* buffer = sk_malloc_throw(len);
161 this->readByteArray(buffer, len);
reedfde05112016-03-11 13:02:28 -0800162 return SkData::MakeFromMalloc(buffer, len);
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000163 }
164
165 // helpers to get info about arrays and binary data
166 virtual uint32_t getArrayCount();
167
commit-bot@chromium.org968edca2014-05-23 13:21:55 +0000168 /**
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
fmalitacd56f812015-09-14 13:31:18 -0700174 SkImage* readImage();
175
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000176 virtual SkTypeface* readTypeface();
177
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000178 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.org8b0e8ac2014-01-30 18:58:24 +0000188 fFactoryArray = array;
189 fFactoryCount = count;
190 }
191
192 /**
msaretta3b3b232016-04-22 12:43:07 -0700193 * 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.org8b0e8ac2014-01-30 18:58:24 +0000207 * 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.
robertphillips9ca06c42016-04-20 11:43:33 -0700216 virtual bool validate(bool isValid) { return isValid; }
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000217 virtual bool isValid() const { return true; }
218 virtual bool validateAvailable(size_t size) { return true; }
robertphillips9ca06c42016-04-20 11:43:33 -0700219 bool validateIndex(int index, int count) {
220 return this->validate(index >= 0 && index < count);
221 }
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000222
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000223protected:
msaretta3b3b232016-04-22 12:43:07 -0700224 /**
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.org9e5f85e2014-03-12 14:46:41 +0000240 SkReader32 fReader;
241
msaretta3b3b232016-04-22 12:43:07 -0700242 // Only used if we do not have an fFactoryArray.
243 SkTHashMap<uint32_t, SkString> fFlattenableDict;
244
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000245private:
246 bool readArray(void* value, size_t size, size_t elementSize);
247
248 uint32_t fFlags;
commit-bot@chromium.org7ed173b2014-05-20 17:31:08 +0000249 int fVersion;
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000250
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000251 void* fMemoryPtr;
252
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000253 SkTypeface** fTFArray;
254 int fTFCount;
255
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000256 SkFlattenable::Factory* fFactoryArray;
257 int fFactoryCount;
258
msaretta3b3b232016-04-22 12:43:07 -0700259 // Only used if we do not have an fFactoryArray.
260 SkTHashMap<SkString, SkFlattenable::Factory> fCustomFactory;
261
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000262 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