blob: 4da27d4fe0c6902de0a908a3637ebcfe120d2d48 [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.orge2b193c2014-05-16 13:49:08 +000026#include "SkUnitMapper.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000027#include "SkWriteBuffer.h"
28#include "SkXfermode.h"
29
30class SkBitmap;
31
32#if defined(SK_DEBUG) && defined(SK_BUILD_FOR_MAC)
33 #define DEBUG_NON_DETERMINISTIC_ASSERT
34#endif
35
36class SkReadBuffer {
37public:
38 SkReadBuffer();
39 SkReadBuffer(const void* data, size_t size);
40 SkReadBuffer(SkStream* stream);
41 virtual ~SkReadBuffer();
42
commit-bot@chromium.org7ed173b2014-05-20 17:31:08 +000043 enum Version {
44 kFilterLevelIsEnum_Version = 23,
45 kGradientFlippedFlag_Version = 24,
46 kDashWritesPhaseIntervals_Version = 25,
47 kColorShaderNoBool_Version = 26,
48 };
49
50 /**
51 * Returns true IFF the version is older than the specified version.
52 */
53 bool isVersionLT(Version targetVersion) const {
54 SkASSERT(targetVersion > 0);
55 return fVersion > 0 && fVersion < targetVersion;
56 }
commit-bot@chromium.org0943f5f2014-03-28 18:05:47 +000057
58 /** 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 +000059 void setVersion(int version) {
60 SkASSERT(0 == fVersion || version == fVersion);
61 fVersion = version;
commit-bot@chromium.org0943f5f2014-03-28 18:05:47 +000062 }
63
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000064 enum Flags {
65 kCrossProcess_Flag = 1 << 0,
66 kScalarIsFloat_Flag = 1 << 1,
67 kPtrIs64Bit_Flag = 1 << 2,
68 kValidation_Flag = 1 << 3,
69 };
70
71 void setFlags(uint32_t flags) { fFlags = flags; }
72 uint32_t getFlags() const { return fFlags; }
73
74 bool isCrossProcess() const {
75 return this->isValidating() || SkToBool(fFlags & kCrossProcess_Flag);
76 }
77 bool isScalarFloat() const { return SkToBool(fFlags & kScalarIsFloat_Flag); }
78 bool isPtr64Bit() const { return SkToBool(fFlags & kPtrIs64Bit_Flag); }
79 bool isValidating() const { return SkToBool(fFlags & kValidation_Flag); }
80
81 SkReader32* getReader32() { return &fReader; }
82
commit-bot@chromium.orgf1177812014-04-23 19:19:44 +000083 size_t size() { return fReader.size(); }
84 size_t offset() { return fReader.offset(); }
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000085 bool eof() { return fReader.eof(); }
86 const void* skip(size_t size) { return fReader.skip(size); }
commit-bot@chromium.org39426e22014-04-16 16:24:08 +000087 void* readFunctionPtr() { return fReader.readPtr(); }
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000088
89 // primitives
90 virtual bool readBool();
91 virtual SkColor readColor();
92 virtual SkFixed readFixed();
93 virtual int32_t readInt();
94 virtual SkScalar readScalar();
95 virtual uint32_t readUInt();
96 virtual int32_t read32();
97
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000098 // strings -- the caller is responsible for freeing the string contents
99 virtual void readString(SkString* string);
100 virtual void* readEncodedString(size_t* length, SkPaint::TextEncoding encoding);
101
102 // common data structures
103 virtual void readPoint(SkPoint* point);
104 SkPoint readPoint() { SkPoint p; this->readPoint(&p); return p; }
105 virtual void readMatrix(SkMatrix* matrix);
106 virtual void readIRect(SkIRect* rect);
107 virtual void readRect(SkRect* rect);
108 virtual void readRegion(SkRegion* region);
109 virtual void readPath(SkPath* path);
110 void readPaint(SkPaint* paint) { paint->unflatten(*this); }
111
112 virtual SkFlattenable* readFlattenable(SkFlattenable::Type);
113 template <typename T> T* readFlattenable() {
114 return (T*) this->readFlattenable(T::GetFlattenableType());
115 }
116 SkColorFilter* readColorFilter() { return this->readFlattenable<SkColorFilter>(); }
117 SkDrawLooper* readDrawLooper() { return this->readFlattenable<SkDrawLooper>(); }
118 SkImageFilter* readImageFilter() { return this->readFlattenable<SkImageFilter>(); }
119 SkMaskFilter* readMaskFilter() { return this->readFlattenable<SkMaskFilter>(); }
120 SkPathEffect* readPathEffect() { return this->readFlattenable<SkPathEffect>(); }
121 SkPixelRef* readPixelRef() { return this->readFlattenable<SkPixelRef>(); }
122 SkRasterizer* readRasterizer() { return this->readFlattenable<SkRasterizer>(); }
123 SkShader* readShader() { return this->readFlattenable<SkShader>(); }
commit-bot@chromium.orge2b193c2014-05-16 13:49:08 +0000124 SkUnitMapper* readUnitMapper() { return this->readFlattenable<SkUnitMapper>(); }
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000125 SkXfermode* readXfermode() { return this->readFlattenable<SkXfermode>(); }
126
127
128 // binary data and arrays
129 virtual bool readByteArray(void* value, size_t size);
130 virtual bool readColorArray(SkColor* colors, size_t size);
131 virtual bool readIntArray(int32_t* values, size_t size);
132 virtual bool readPointArray(SkPoint* points, size_t size);
133 virtual bool readScalarArray(SkScalar* values, size_t size);
134
135 SkData* readByteArrayAsData() {
136 size_t len = this->getArrayCount();
137 if (!this->validateAvailable(len)) {
138 return SkData::NewEmpty();
139 }
140 void* buffer = sk_malloc_throw(len);
141 this->readByteArray(buffer, len);
142 return SkData::NewFromMalloc(buffer, len);
143 }
144
145 // helpers to get info about arrays and binary data
146 virtual uint32_t getArrayCount();
147
148 virtual void readBitmap(SkBitmap* bitmap);
149 virtual SkTypeface* readTypeface();
150
151 void setBitmapStorage(SkBitmapHeapReader* bitmapStorage) {
152 SkRefCnt_SafeAssign(fBitmapStorage, bitmapStorage);
153 }
154
155 void setTypefaceArray(SkTypeface* array[], int count) {
156 fTFArray = array;
157 fTFCount = count;
158 }
159
160 /**
161 * Call this with a pre-loaded array of Factories, in the same order as
162 * were created/written by the writer. SkPicture uses this.
163 */
164 void setFactoryPlayback(SkFlattenable::Factory array[], int count) {
165 fFactoryTDArray = NULL;
166 fFactoryArray = array;
167 fFactoryCount = count;
168 }
169
170 /**
171 * Call this with an initially empty array, so the reader can cache each
172 * factory it sees by name. Used by the pipe code in conjunction with
173 * SkWriteBuffer::setNamedFactoryRecorder.
174 */
175 void setFactoryArray(SkTDArray<SkFlattenable::Factory>* array) {
176 fFactoryTDArray = array;
177 fFactoryArray = NULL;
178 fFactoryCount = 0;
179 }
180
181 /**
182 * Provide a function to decode an SkBitmap from encoded data. Only used if the writer
183 * encoded the SkBitmap. If the proper decoder cannot be used, a red bitmap with the
184 * appropriate size will be used.
185 */
186 void setBitmapDecoder(SkPicture::InstallPixelRefProc bitmapDecoder) {
187 fBitmapDecoder = bitmapDecoder;
188 }
189
190 // Default impelementations don't check anything.
191 virtual bool validate(bool isValid) { return true; }
192 virtual bool isValid() const { return true; }
193 virtual bool validateAvailable(size_t size) { return true; }
194
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000195protected:
196 SkReader32 fReader;
197
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000198private:
199 bool readArray(void* value, size_t size, size_t elementSize);
200
201 uint32_t fFlags;
commit-bot@chromium.org7ed173b2014-05-20 17:31:08 +0000202 int fVersion;
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000203
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000204 void* fMemoryPtr;
205
206 SkBitmapHeapReader* fBitmapStorage;
207 SkTypeface** fTFArray;
208 int fTFCount;
209
210 SkTDArray<SkFlattenable::Factory>* fFactoryTDArray;
211 SkFlattenable::Factory* fFactoryArray;
212 int fFactoryCount;
213
214 SkPicture::InstallPixelRefProc fBitmapDecoder;
215
216#ifdef DEBUG_NON_DETERMINISTIC_ASSERT
217 // Debugging counter to keep track of how many bitmaps we
218 // have decoded.
219 int fDecodedBitmapIndex;
220#endif // DEBUG_NON_DETERMINISTIC_ASSERT
221};
222
223#endif // SkReadBuffer_DEFINED