blob: 3d6dd362acccceb5e93c5f72d46eafbba4a24526 [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
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.org8b0e8ac2014-01-30 18:58:24 +000020#include "SkRasterizer.h"
21#include "SkReadBuffer.h"
22#include "SkReader32.h"
23#include "SkRefCnt.h"
24#include "SkShader.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
commit-bot@chromium.org7ed173b2014-05-20 17:31:08 +000042 enum Version {
mtklein88fd0fb2014-12-01 06:56:38 -080043 /*
commit-bot@chromium.org7ed173b2014-05-20 17:31:08 +000044 kFilterLevelIsEnum_Version = 23,
45 kGradientFlippedFlag_Version = 24,
46 kDashWritesPhaseIntervals_Version = 25,
47 kColorShaderNoBool_Version = 26,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000048 kNoUnitMappers_Version = 27,
commit-bot@chromium.org968edca2014-05-23 13:21:55 +000049 kNoMoreBitmapFlatten_Version = 28,
scroggoc870d492014-07-11 10:42:12 -070050 kSimplifyLocalMatrix_Version = 30,
senorblanco55b6d8b2014-07-30 11:26:46 -070051 kImageFilterUniqueID_Version = 31,
djsollen3b625542014-08-14 06:29:02 -070052 kRemoveAndroidPaintOpts_Version = 32,
reed9fa60da2014-08-21 07:59:51 -070053 kFlattenCreateProc_Version = 33,
mtklein88fd0fb2014-12-01 06:56:38 -080054 */
reedc5e15a12014-09-29 12:10:27 -070055 kRemoveColorTableAlpha_Version = 36,
sugoi234f0362014-10-23 13:59:52 -070056 kDropShadowMode_Version = 37,
Justin Novosad52340752014-12-02 14:50:56 -050057 kPictureImageFilterResolution_Version = 38,
junovf3c78cc2014-12-09 13:07:22 -080058 kPictureImageFilterLevel_Version = 39,
senorblanco4a22a432015-03-18 13:14:54 -070059 kImageFilterNoUniqueID_Version = 40,
mtklein76be9c82015-05-20 12:05:15 -070060 kBitmapSourceFilterQuality_Version = 41,
61 kPictureShaderHasPictureBool_Version = 42,
reed871872f2015-06-22 12:48:26 -070062 kHasDrawImageOpCodes_Version = 43,
commit-bot@chromium.org7ed173b2014-05-20 17:31:08 +000063 };
64
65 /**
66 * Returns true IFF the version is older than the specified version.
67 */
68 bool isVersionLT(Version targetVersion) const {
69 SkASSERT(targetVersion > 0);
70 return fVersion > 0 && fVersion < targetVersion;
71 }
commit-bot@chromium.org0943f5f2014-03-28 18:05:47 +000072
73 /** 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 +000074 void setVersion(int version) {
75 SkASSERT(0 == fVersion || version == fVersion);
76 fVersion = version;
commit-bot@chromium.org0943f5f2014-03-28 18:05:47 +000077 }
78
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000079 enum Flags {
80 kCrossProcess_Flag = 1 << 0,
81 kScalarIsFloat_Flag = 1 << 1,
82 kPtrIs64Bit_Flag = 1 << 2,
83 kValidation_Flag = 1 << 3,
84 };
85
86 void setFlags(uint32_t flags) { fFlags = flags; }
87 uint32_t getFlags() const { return fFlags; }
88
89 bool isCrossProcess() const {
90 return this->isValidating() || SkToBool(fFlags & kCrossProcess_Flag);
91 }
92 bool isScalarFloat() const { return SkToBool(fFlags & kScalarIsFloat_Flag); }
93 bool isPtr64Bit() const { return SkToBool(fFlags & kPtrIs64Bit_Flag); }
94 bool isValidating() const { return SkToBool(fFlags & kValidation_Flag); }
95
96 SkReader32* getReader32() { return &fReader; }
97
commit-bot@chromium.orgf1177812014-04-23 19:19:44 +000098 size_t size() { return fReader.size(); }
99 size_t offset() { return fReader.offset(); }
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000100 bool eof() { return fReader.eof(); }
sugoi0951fe12014-06-06 06:44:16 -0700101 virtual const void* skip(size_t size) { return fReader.skip(size); }
commit-bot@chromium.org39426e22014-04-16 16:24:08 +0000102 void* readFunctionPtr() { return fReader.readPtr(); }
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000103
104 // primitives
105 virtual bool readBool();
106 virtual SkColor readColor();
107 virtual SkFixed readFixed();
108 virtual int32_t readInt();
109 virtual SkScalar readScalar();
110 virtual uint32_t readUInt();
111 virtual int32_t read32();
112
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000113 // 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);
mtklein88fd0fb2014-12-01 06:56:38 -0800124
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000125 virtual void readPath(SkPath* path);
126 void readPaint(SkPaint* paint) { paint->unflatten(*this); }
127
128 virtual SkFlattenable* readFlattenable(SkFlattenable::Type);
129 template <typename T> T* readFlattenable() {
130 return (T*) this->readFlattenable(T::GetFlattenableType());
131 }
132 SkColorFilter* readColorFilter() { return this->readFlattenable<SkColorFilter>(); }
133 SkDrawLooper* readDrawLooper() { return this->readFlattenable<SkDrawLooper>(); }
134 SkImageFilter* readImageFilter() { return this->readFlattenable<SkImageFilter>(); }
135 SkMaskFilter* readMaskFilter() { return this->readFlattenable<SkMaskFilter>(); }
136 SkPathEffect* readPathEffect() { return this->readFlattenable<SkPathEffect>(); }
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000137 SkRasterizer* readRasterizer() { return this->readFlattenable<SkRasterizer>(); }
138 SkShader* readShader() { return this->readFlattenable<SkShader>(); }
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000139 SkXfermode* readXfermode() { return this->readFlattenable<SkXfermode>(); }
140
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +0000141 /**
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.org8b0e8ac2014-01-30 18:58:24 +0000146
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
154 SkData* readByteArrayAsData() {
155 size_t len = this->getArrayCount();
156 if (!this->validateAvailable(len)) {
157 return SkData::NewEmpty();
158 }
159 void* buffer = sk_malloc_throw(len);
160 this->readByteArray(buffer, len);
161 return SkData::NewFromMalloc(buffer, len);
162 }
163
164 // helpers to get info about arrays and binary data
165 virtual uint32_t getArrayCount();
166
commit-bot@chromium.org968edca2014-05-23 13:21:55 +0000167 /**
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
fmalitacd56f812015-09-14 13:31:18 -0700173 SkImage* readImage();
174
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000175 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) {
halcanary96fcdcc2015-08-27 07:41:13 -0700191 fFactoryTDArray = nullptr;
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000192 fFactoryArray = array;
193 fFactoryCount = count;
194 }
195
196 /**
197 * Call this with an initially empty array, so the reader can cache each
198 * factory it sees by name. Used by the pipe code in conjunction with
199 * SkWriteBuffer::setNamedFactoryRecorder.
200 */
201 void setFactoryArray(SkTDArray<SkFlattenable::Factory>* array) {
202 fFactoryTDArray = array;
halcanary96fcdcc2015-08-27 07:41:13 -0700203 fFactoryArray = nullptr;
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000204 fFactoryCount = 0;
205 }
206
207 /**
208 * Provide a function to decode an SkBitmap from encoded data. Only used if the writer
209 * encoded the SkBitmap. If the proper decoder cannot be used, a red bitmap with the
210 * appropriate size will be used.
211 */
212 void setBitmapDecoder(SkPicture::InstallPixelRefProc bitmapDecoder) {
213 fBitmapDecoder = bitmapDecoder;
214 }
215
216 // Default impelementations don't check anything.
217 virtual bool validate(bool isValid) { return true; }
218 virtual bool isValid() const { return true; }
219 virtual bool validateAvailable(size_t size) { return true; }
220
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000221protected:
222 SkReader32 fReader;
223
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000224private:
225 bool readArray(void* value, size_t size, size_t elementSize);
226
227 uint32_t fFlags;
commit-bot@chromium.org7ed173b2014-05-20 17:31:08 +0000228 int fVersion;
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000229
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000230 void* fMemoryPtr;
231
232 SkBitmapHeapReader* fBitmapStorage;
233 SkTypeface** fTFArray;
234 int fTFCount;
235
236 SkTDArray<SkFlattenable::Factory>* fFactoryTDArray;
237 SkFlattenable::Factory* fFactoryArray;
238 int fFactoryCount;
239
240 SkPicture::InstallPixelRefProc fBitmapDecoder;
241
242#ifdef DEBUG_NON_DETERMINISTIC_ASSERT
243 // Debugging counter to keep track of how many bitmaps we
244 // have decoded.
245 int fDecodedBitmapIndex;
246#endif // DEBUG_NON_DETERMINISTIC_ASSERT
247};
248
249#endif // SkReadBuffer_DEFINED