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