blob: 5364bee28597e6755389437737e3a9ad5c614301 [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"
20#include "SkPixelRef.h"
21#include "SkRasterizer.h"
22#include "SkReadBuffer.h"
23#include "SkReader32.h"
24#include "SkRefCnt.h"
25#include "SkShader.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000026#include "SkWriteBuffer.h"
27#include "SkXfermode.h"
28
29class SkBitmap;
30
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 {
43 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,
commit-bot@chromium.org7ed173b2014-05-20 17:31:08 +000049 };
50
51 /**
52 * Returns true IFF the version is older than the specified version.
53 */
54 bool isVersionLT(Version targetVersion) const {
55 SkASSERT(targetVersion > 0);
56 return fVersion > 0 && fVersion < targetVersion;
57 }
commit-bot@chromium.org0943f5f2014-03-28 18:05:47 +000058
59 /** 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 +000060 void setVersion(int version) {
61 SkASSERT(0 == fVersion || version == fVersion);
62 fVersion = version;
commit-bot@chromium.org0943f5f2014-03-28 18:05:47 +000063 }
64
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000065 enum Flags {
66 kCrossProcess_Flag = 1 << 0,
67 kScalarIsFloat_Flag = 1 << 1,
68 kPtrIs64Bit_Flag = 1 << 2,
69 kValidation_Flag = 1 << 3,
70 };
71
72 void setFlags(uint32_t flags) { fFlags = flags; }
73 uint32_t getFlags() const { return fFlags; }
74
75 bool isCrossProcess() const {
76 return this->isValidating() || SkToBool(fFlags & kCrossProcess_Flag);
77 }
78 bool isScalarFloat() const { return SkToBool(fFlags & kScalarIsFloat_Flag); }
79 bool isPtr64Bit() const { return SkToBool(fFlags & kPtrIs64Bit_Flag); }
80 bool isValidating() const { return SkToBool(fFlags & kValidation_Flag); }
81
82 SkReader32* getReader32() { return &fReader; }
83
commit-bot@chromium.orgf1177812014-04-23 19:19:44 +000084 size_t size() { return fReader.size(); }
85 size_t offset() { return fReader.offset(); }
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000086 bool eof() { return fReader.eof(); }
87 const void* skip(size_t size) { return fReader.skip(size); }
commit-bot@chromium.org39426e22014-04-16 16:24:08 +000088 void* readFunctionPtr() { return fReader.readPtr(); }
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000089
90 // primitives
91 virtual bool readBool();
92 virtual SkColor readColor();
93 virtual SkFixed readFixed();
94 virtual int32_t readInt();
95 virtual SkScalar readScalar();
96 virtual uint32_t readUInt();
97 virtual int32_t read32();
98
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000099 // strings -- the caller is responsible for freeing the string contents
100 virtual void readString(SkString* string);
101 virtual void* readEncodedString(size_t* length, SkPaint::TextEncoding encoding);
102
103 // common data structures
104 virtual void readPoint(SkPoint* point);
105 SkPoint readPoint() { SkPoint p; this->readPoint(&p); return p; }
106 virtual void readMatrix(SkMatrix* matrix);
107 virtual void readIRect(SkIRect* rect);
108 virtual void readRect(SkRect* rect);
109 virtual void readRegion(SkRegion* region);
110 virtual void readPath(SkPath* path);
111 void readPaint(SkPaint* paint) { paint->unflatten(*this); }
112
113 virtual SkFlattenable* readFlattenable(SkFlattenable::Type);
114 template <typename T> T* readFlattenable() {
115 return (T*) this->readFlattenable(T::GetFlattenableType());
116 }
117 SkColorFilter* readColorFilter() { return this->readFlattenable<SkColorFilter>(); }
118 SkDrawLooper* readDrawLooper() { return this->readFlattenable<SkDrawLooper>(); }
119 SkImageFilter* readImageFilter() { return this->readFlattenable<SkImageFilter>(); }
120 SkMaskFilter* readMaskFilter() { return this->readFlattenable<SkMaskFilter>(); }
121 SkPathEffect* readPathEffect() { return this->readFlattenable<SkPathEffect>(); }
122 SkPixelRef* readPixelRef() { return this->readFlattenable<SkPixelRef>(); }
123 SkRasterizer* readRasterizer() { return this->readFlattenable<SkRasterizer>(); }
124 SkShader* readShader() { return this->readFlattenable<SkShader>(); }
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000125 SkXfermode* readXfermode() { return this->readFlattenable<SkXfermode>(); }
126
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +0000127 /**
128 * Like readFlattenable() but explicitly just skips the data that was written for the
129 * flattenable (or the sentinel that there wasn't one).
130 */
131 virtual void skipFlattenable();
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000132
133 // binary data and arrays
134 virtual bool readByteArray(void* value, size_t size);
135 virtual bool readColorArray(SkColor* colors, size_t size);
136 virtual bool readIntArray(int32_t* values, size_t size);
137 virtual bool readPointArray(SkPoint* points, size_t size);
138 virtual bool readScalarArray(SkScalar* values, size_t size);
139
140 SkData* readByteArrayAsData() {
141 size_t len = this->getArrayCount();
142 if (!this->validateAvailable(len)) {
143 return SkData::NewEmpty();
144 }
145 void* buffer = sk_malloc_throw(len);
146 this->readByteArray(buffer, len);
147 return SkData::NewFromMalloc(buffer, len);
148 }
149
150 // helpers to get info about arrays and binary data
151 virtual uint32_t getArrayCount();
152
commit-bot@chromium.org968edca2014-05-23 13:21:55 +0000153 /**
154 * Returns false if the bitmap could not be completely read. In that case, it will be set
155 * to have width/height, but no pixels.
156 */
157 bool readBitmap(SkBitmap* bitmap);
158
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000159 virtual SkTypeface* readTypeface();
160
161 void setBitmapStorage(SkBitmapHeapReader* bitmapStorage) {
162 SkRefCnt_SafeAssign(fBitmapStorage, bitmapStorage);
163 }
164
165 void setTypefaceArray(SkTypeface* array[], int count) {
166 fTFArray = array;
167 fTFCount = count;
168 }
169
170 /**
171 * Call this with a pre-loaded array of Factories, in the same order as
172 * were created/written by the writer. SkPicture uses this.
173 */
174 void setFactoryPlayback(SkFlattenable::Factory array[], int count) {
175 fFactoryTDArray = NULL;
176 fFactoryArray = array;
177 fFactoryCount = count;
178 }
179
180 /**
181 * Call this with an initially empty array, so the reader can cache each
182 * factory it sees by name. Used by the pipe code in conjunction with
183 * SkWriteBuffer::setNamedFactoryRecorder.
184 */
185 void setFactoryArray(SkTDArray<SkFlattenable::Factory>* array) {
186 fFactoryTDArray = array;
187 fFactoryArray = NULL;
188 fFactoryCount = 0;
189 }
190
191 /**
192 * Provide a function to decode an SkBitmap from encoded data. Only used if the writer
193 * encoded the SkBitmap. If the proper decoder cannot be used, a red bitmap with the
194 * appropriate size will be used.
195 */
196 void setBitmapDecoder(SkPicture::InstallPixelRefProc bitmapDecoder) {
197 fBitmapDecoder = bitmapDecoder;
198 }
199
200 // Default impelementations don't check anything.
201 virtual bool validate(bool isValid) { return true; }
202 virtual bool isValid() const { return true; }
203 virtual bool validateAvailable(size_t size) { return true; }
204
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000205protected:
206 SkReader32 fReader;
207
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000208private:
209 bool readArray(void* value, size_t size, size_t elementSize);
210
211 uint32_t fFlags;
commit-bot@chromium.org7ed173b2014-05-20 17:31:08 +0000212 int fVersion;
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000213
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000214 void* fMemoryPtr;
215
216 SkBitmapHeapReader* fBitmapStorage;
217 SkTypeface** fTFArray;
218 int fTFCount;
219
220 SkTDArray<SkFlattenable::Factory>* fFactoryTDArray;
221 SkFlattenable::Factory* fFactoryArray;
222 int fFactoryCount;
223
224 SkPicture::InstallPixelRefProc fBitmapDecoder;
225
226#ifdef DEBUG_NON_DETERMINISTIC_ASSERT
227 // Debugging counter to keep track of how many bitmaps we
228 // have decoded.
229 int fDecodedBitmapIndex;
230#endif // DEBUG_NON_DETERMINISTIC_ASSERT
231};
232
233#endif // SkReadBuffer_DEFINED