blob: e77d1adfe32ff55b8a67e14bcaeb5ade14617fe6 [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,
reedf70b5312016-03-04 16:36:20 -080063 kAnnotationsMovedToCanvas_Version = 44,
commit-bot@chromium.org7ed173b2014-05-20 17:31:08 +000064 };
65
66 /**
67 * Returns true IFF the version is older than the specified version.
68 */
69 bool isVersionLT(Version targetVersion) const {
70 SkASSERT(targetVersion > 0);
71 return fVersion > 0 && fVersion < targetVersion;
72 }
commit-bot@chromium.org0943f5f2014-03-28 18:05:47 +000073
74 /** 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 +000075 void setVersion(int version) {
76 SkASSERT(0 == fVersion || version == fVersion);
77 fVersion = version;
commit-bot@chromium.org0943f5f2014-03-28 18:05:47 +000078 }
79
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000080 enum Flags {
81 kCrossProcess_Flag = 1 << 0,
82 kScalarIsFloat_Flag = 1 << 1,
83 kPtrIs64Bit_Flag = 1 << 2,
84 kValidation_Flag = 1 << 3,
85 };
86
87 void setFlags(uint32_t flags) { fFlags = flags; }
88 uint32_t getFlags() const { return fFlags; }
89
90 bool isCrossProcess() const {
91 return this->isValidating() || SkToBool(fFlags & kCrossProcess_Flag);
92 }
93 bool isScalarFloat() const { return SkToBool(fFlags & kScalarIsFloat_Flag); }
94 bool isPtr64Bit() const { return SkToBool(fFlags & kPtrIs64Bit_Flag); }
95 bool isValidating() const { return SkToBool(fFlags & kValidation_Flag); }
96
97 SkReader32* getReader32() { return &fReader; }
98
commit-bot@chromium.orgf1177812014-04-23 19:19:44 +000099 size_t size() { return fReader.size(); }
100 size_t offset() { return fReader.offset(); }
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000101 bool eof() { return fReader.eof(); }
sugoi0951fe12014-06-06 06:44:16 -0700102 virtual const void* skip(size_t size) { return fReader.skip(size); }
commit-bot@chromium.org39426e22014-04-16 16:24:08 +0000103 void* readFunctionPtr() { return fReader.readPtr(); }
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000104
105 // primitives
106 virtual bool readBool();
107 virtual SkColor readColor();
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000108 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
mtklein3b375452016-04-04 14:57:19 -0700128 virtual SkFlattenable* readFlattenable(SkFlattenable::Type);
reed60c9b582016-04-03 09:11:13 -0700129 template <typename T> sk_sp<T> readFlattenable() {
mtklein3b375452016-04-04 14:57:19 -0700130 return sk_sp<T>((T*)this->readFlattenable(T::GetFlattenableType()));
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000131 }
reed60c9b582016-04-03 09:11:13 -0700132 sk_sp<SkColorFilter> readColorFilter() { return this->readFlattenable<SkColorFilter>(); }
133 sk_sp<SkDrawLooper> readDrawLooper() { return this->readFlattenable<SkDrawLooper>(); }
134 sk_sp<SkImageFilter> readImageFilter() { return this->readFlattenable<SkImageFilter>(); }
135 sk_sp<SkMaskFilter> readMaskFilter() { return this->readFlattenable<SkMaskFilter>(); }
136 sk_sp<SkPathEffect> readPathEffect() { return this->readFlattenable<SkPathEffect>(); }
137 sk_sp<SkRasterizer> readRasterizer() { return this->readFlattenable<SkRasterizer>(); }
138 sk_sp<SkShader> readShader() { return this->readFlattenable<SkShader>(); }
139 sk_sp<SkXfermode> readXfermode() { return this->readFlattenable<SkXfermode>(); }
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000140
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
reedfde05112016-03-11 13:02:28 -0800154 sk_sp<SkData> readByteArrayAsData() {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000155 size_t len = this->getArrayCount();
156 if (!this->validateAvailable(len)) {
reedfde05112016-03-11 13:02:28 -0800157 return SkData::MakeEmpty();
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000158 }
159 void* buffer = sk_malloc_throw(len);
160 this->readByteArray(buffer, len);
reedfde05112016-03-11 13:02:28 -0800161 return SkData::MakeFromMalloc(buffer, len);
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000162 }
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) {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000191 fFactoryArray = array;
192 fFactoryCount = count;
193 }
194
195 /**
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000196 * Provide a function to decode an SkBitmap from encoded data. Only used if the writer
197 * encoded the SkBitmap. If the proper decoder cannot be used, a red bitmap with the
198 * appropriate size will be used.
199 */
200 void setBitmapDecoder(SkPicture::InstallPixelRefProc bitmapDecoder) {
201 fBitmapDecoder = bitmapDecoder;
202 }
203
204 // Default impelementations don't check anything.
205 virtual bool validate(bool isValid) { return true; }
206 virtual bool isValid() const { return true; }
207 virtual bool validateAvailable(size_t size) { return true; }
208
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000209protected:
210 SkReader32 fReader;
211
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000212private:
213 bool readArray(void* value, size_t size, size_t elementSize);
214
215 uint32_t fFlags;
commit-bot@chromium.org7ed173b2014-05-20 17:31:08 +0000216 int fVersion;
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000217
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000218 void* fMemoryPtr;
219
220 SkBitmapHeapReader* fBitmapStorage;
221 SkTypeface** fTFArray;
222 int fTFCount;
223
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000224 SkFlattenable::Factory* fFactoryArray;
225 int fFactoryCount;
226
227 SkPicture::InstallPixelRefProc fBitmapDecoder;
228
229#ifdef DEBUG_NON_DETERMINISTIC_ASSERT
230 // Debugging counter to keep track of how many bitmaps we
231 // have decoded.
232 int fDecodedBitmapIndex;
233#endif // DEBUG_NON_DETERMINISTIC_ASSERT
234};
235
236#endif // SkReadBuffer_DEFINED