blob: 9056b075766aed24255db379a7461878c8800556 [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;
29
30#if defined(SK_DEBUG) && defined(SK_BUILD_FOR_MAC)
31 #define DEBUG_NON_DETERMINISTIC_ASSERT
32#endif
33
34class SkReadBuffer {
35public:
36 SkReadBuffer();
37 SkReadBuffer(const void* data, size_t size);
38 SkReadBuffer(SkStream* stream);
39 virtual ~SkReadBuffer();
40
commit-bot@chromium.org7ed173b2014-05-20 17:31:08 +000041 enum Version {
mtklein88fd0fb2014-12-01 06:56:38 -080042 /*
commit-bot@chromium.org7ed173b2014-05-20 17:31:08 +000043 kFilterLevelIsEnum_Version = 23,
44 kGradientFlippedFlag_Version = 24,
45 kDashWritesPhaseIntervals_Version = 25,
46 kColorShaderNoBool_Version = 26,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000047 kNoUnitMappers_Version = 27,
commit-bot@chromium.org968edca2014-05-23 13:21:55 +000048 kNoMoreBitmapFlatten_Version = 28,
scroggoc870d492014-07-11 10:42:12 -070049 kSimplifyLocalMatrix_Version = 30,
senorblanco55b6d8b2014-07-30 11:26:46 -070050 kImageFilterUniqueID_Version = 31,
djsollen3b625542014-08-14 06:29:02 -070051 kRemoveAndroidPaintOpts_Version = 32,
reed9fa60da2014-08-21 07:59:51 -070052 kFlattenCreateProc_Version = 33,
mtklein88fd0fb2014-12-01 06:56:38 -080053 */
reedc5e15a12014-09-29 12:10:27 -070054 kRemoveColorTableAlpha_Version = 36,
sugoi234f0362014-10-23 13:59:52 -070055 kDropShadowMode_Version = 37,
Justin Novosad52340752014-12-02 14:50:56 -050056 kPictureImageFilterResolution_Version = 38,
commit-bot@chromium.org7ed173b2014-05-20 17:31:08 +000057 };
58
59 /**
60 * Returns true IFF the version is older than the specified version.
61 */
62 bool isVersionLT(Version targetVersion) const {
63 SkASSERT(targetVersion > 0);
64 return fVersion > 0 && fVersion < targetVersion;
65 }
commit-bot@chromium.org0943f5f2014-03-28 18:05:47 +000066
67 /** 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 +000068 void setVersion(int version) {
69 SkASSERT(0 == fVersion || version == fVersion);
70 fVersion = version;
commit-bot@chromium.org0943f5f2014-03-28 18:05:47 +000071 }
72
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000073 enum Flags {
74 kCrossProcess_Flag = 1 << 0,
75 kScalarIsFloat_Flag = 1 << 1,
76 kPtrIs64Bit_Flag = 1 << 2,
77 kValidation_Flag = 1 << 3,
78 };
79
80 void setFlags(uint32_t flags) { fFlags = flags; }
81 uint32_t getFlags() const { return fFlags; }
82
83 bool isCrossProcess() const {
84 return this->isValidating() || SkToBool(fFlags & kCrossProcess_Flag);
85 }
86 bool isScalarFloat() const { return SkToBool(fFlags & kScalarIsFloat_Flag); }
87 bool isPtr64Bit() const { return SkToBool(fFlags & kPtrIs64Bit_Flag); }
88 bool isValidating() const { return SkToBool(fFlags & kValidation_Flag); }
89
90 SkReader32* getReader32() { return &fReader; }
91
commit-bot@chromium.orgf1177812014-04-23 19:19:44 +000092 size_t size() { return fReader.size(); }
93 size_t offset() { return fReader.offset(); }
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000094 bool eof() { return fReader.eof(); }
sugoi0951fe12014-06-06 06:44:16 -070095 virtual const void* skip(size_t size) { return fReader.skip(size); }
commit-bot@chromium.org39426e22014-04-16 16:24:08 +000096 void* readFunctionPtr() { return fReader.readPtr(); }
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000097
98 // primitives
99 virtual bool readBool();
100 virtual SkColor readColor();
101 virtual SkFixed readFixed();
102 virtual int32_t readInt();
103 virtual SkScalar readScalar();
104 virtual uint32_t readUInt();
105 virtual int32_t read32();
106
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000107 // strings -- the caller is responsible for freeing the string contents
108 virtual void readString(SkString* string);
109 virtual void* readEncodedString(size_t* length, SkPaint::TextEncoding encoding);
110
111 // common data structures
112 virtual void readPoint(SkPoint* point);
113 SkPoint readPoint() { SkPoint p; this->readPoint(&p); return p; }
114 virtual void readMatrix(SkMatrix* matrix);
115 virtual void readIRect(SkIRect* rect);
116 virtual void readRect(SkRect* rect);
117 virtual void readRegion(SkRegion* region);
mtklein88fd0fb2014-12-01 06:56:38 -0800118
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000119 virtual void readPath(SkPath* path);
120 void readPaint(SkPaint* paint) { paint->unflatten(*this); }
121
122 virtual SkFlattenable* readFlattenable(SkFlattenable::Type);
123 template <typename T> T* readFlattenable() {
124 return (T*) this->readFlattenable(T::GetFlattenableType());
125 }
126 SkColorFilter* readColorFilter() { return this->readFlattenable<SkColorFilter>(); }
127 SkDrawLooper* readDrawLooper() { return this->readFlattenable<SkDrawLooper>(); }
128 SkImageFilter* readImageFilter() { return this->readFlattenable<SkImageFilter>(); }
129 SkMaskFilter* readMaskFilter() { return this->readFlattenable<SkMaskFilter>(); }
130 SkPathEffect* readPathEffect() { return this->readFlattenable<SkPathEffect>(); }
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000131 SkRasterizer* readRasterizer() { return this->readFlattenable<SkRasterizer>(); }
132 SkShader* readShader() { return this->readFlattenable<SkShader>(); }
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000133 SkXfermode* readXfermode() { return this->readFlattenable<SkXfermode>(); }
134
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +0000135 /**
136 * Like readFlattenable() but explicitly just skips the data that was written for the
137 * flattenable (or the sentinel that there wasn't one).
138 */
139 virtual void skipFlattenable();
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000140
141 // binary data and arrays
142 virtual bool readByteArray(void* value, size_t size);
143 virtual bool readColorArray(SkColor* colors, size_t size);
144 virtual bool readIntArray(int32_t* values, size_t size);
145 virtual bool readPointArray(SkPoint* points, size_t size);
146 virtual bool readScalarArray(SkScalar* values, size_t size);
147
148 SkData* readByteArrayAsData() {
149 size_t len = this->getArrayCount();
150 if (!this->validateAvailable(len)) {
151 return SkData::NewEmpty();
152 }
153 void* buffer = sk_malloc_throw(len);
154 this->readByteArray(buffer, len);
155 return SkData::NewFromMalloc(buffer, len);
156 }
157
158 // helpers to get info about arrays and binary data
159 virtual uint32_t getArrayCount();
160
commit-bot@chromium.org968edca2014-05-23 13:21:55 +0000161 /**
162 * Returns false if the bitmap could not be completely read. In that case, it will be set
163 * to have width/height, but no pixels.
164 */
165 bool readBitmap(SkBitmap* bitmap);
166
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000167 virtual SkTypeface* readTypeface();
168
169 void setBitmapStorage(SkBitmapHeapReader* bitmapStorage) {
170 SkRefCnt_SafeAssign(fBitmapStorage, bitmapStorage);
171 }
172
173 void setTypefaceArray(SkTypeface* array[], int count) {
174 fTFArray = array;
175 fTFCount = count;
176 }
177
178 /**
179 * Call this with a pre-loaded array of Factories, in the same order as
180 * were created/written by the writer. SkPicture uses this.
181 */
182 void setFactoryPlayback(SkFlattenable::Factory array[], int count) {
183 fFactoryTDArray = NULL;
184 fFactoryArray = array;
185 fFactoryCount = count;
186 }
187
188 /**
189 * Call this with an initially empty array, so the reader can cache each
190 * factory it sees by name. Used by the pipe code in conjunction with
191 * SkWriteBuffer::setNamedFactoryRecorder.
192 */
193 void setFactoryArray(SkTDArray<SkFlattenable::Factory>* array) {
194 fFactoryTDArray = array;
195 fFactoryArray = NULL;
196 fFactoryCount = 0;
197 }
198
199 /**
200 * Provide a function to decode an SkBitmap from encoded data. Only used if the writer
201 * encoded the SkBitmap. If the proper decoder cannot be used, a red bitmap with the
202 * appropriate size will be used.
203 */
204 void setBitmapDecoder(SkPicture::InstallPixelRefProc bitmapDecoder) {
205 fBitmapDecoder = bitmapDecoder;
206 }
207
208 // Default impelementations don't check anything.
209 virtual bool validate(bool isValid) { return true; }
210 virtual bool isValid() const { return true; }
211 virtual bool validateAvailable(size_t size) { return true; }
212
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000213protected:
214 SkReader32 fReader;
215
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000216private:
217 bool readArray(void* value, size_t size, size_t elementSize);
218
219 uint32_t fFlags;
commit-bot@chromium.org7ed173b2014-05-20 17:31:08 +0000220 int fVersion;
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000221
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000222 void* fMemoryPtr;
223
224 SkBitmapHeapReader* fBitmapStorage;
225 SkTypeface** fTFArray;
226 int fTFCount;
227
228 SkTDArray<SkFlattenable::Factory>* fFactoryTDArray;
229 SkFlattenable::Factory* fFactoryArray;
230 int fFactoryCount;
231
232 SkPicture::InstallPixelRefProc fBitmapDecoder;
233
234#ifdef DEBUG_NON_DETERMINISTIC_ASSERT
235 // Debugging counter to keep track of how many bitmaps we
236 // have decoded.
237 int fDecodedBitmapIndex;
238#endif // DEBUG_NON_DETERMINISTIC_ASSERT
239};
240
241#endif // SkReadBuffer_DEFINED