• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 "SkRasterizer.h"
21 #include "SkReadBuffer.h"
22 #include "SkReader32.h"
23 #include "SkRefCnt.h"
24 #include "SkShader.h"
25 #include "SkWriteBuffer.h"
26 #include "SkXfermode.h"
27 
28 class SkBitmap;
29 class SkImage;
30 
31 #if defined(SK_DEBUG) && defined(SK_BUILD_FOR_MAC)
32     #define DEBUG_NON_DETERMINISTIC_ASSERT
33 #endif
34 
35 class SkReadBuffer {
36 public:
37     SkReadBuffer();
38     SkReadBuffer(const void* data, size_t size);
39     SkReadBuffer(SkStream* stream);
40     virtual ~SkReadBuffer();
41 
42     enum Version {
43         /*
44         kFilterLevelIsEnum_Version         = 23,
45         kGradientFlippedFlag_Version       = 24,
46         kDashWritesPhaseIntervals_Version  = 25,
47         kColorShaderNoBool_Version         = 26,
48         kNoUnitMappers_Version             = 27,
49         kNoMoreBitmapFlatten_Version       = 28,
50         kSimplifyLocalMatrix_Version       = 30,
51         kImageFilterUniqueID_Version       = 31,
52         kRemoveAndroidPaintOpts_Version    = 32,
53         kFlattenCreateProc_Version         = 33,
54         */
55         kRemoveColorTableAlpha_Version     = 36,
56         kDropShadowMode_Version            = 37,
57         kPictureImageFilterResolution_Version = 38,
58         kPictureImageFilterLevel_Version   = 39,
59         kImageFilterNoUniqueID_Version     = 40,
60         kBitmapSourceFilterQuality_Version = 41,
61         kPictureShaderHasPictureBool_Version = 42,
62         kHasDrawImageOpCodes_Version       = 43,
63     };
64 
65     /**
66      *  Returns true IFF the version is older than the specified version.
67      */
isVersionLT(Version targetVersion)68     bool isVersionLT(Version targetVersion) const {
69         SkASSERT(targetVersion > 0);
70         return fVersion > 0 && fVersion < targetVersion;
71     }
72 
73     /** This may be called at most once; most clients of SkReadBuffer should not mess with it. */
setVersion(int version)74     void setVersion(int version) {
75         SkASSERT(0 == fVersion || version == fVersion);
76         fVersion = version;
77     }
78 
79     enum Flags {
80         kCrossProcess_Flag  = 1 << 0,
81         kScalarIsFloat_Flag = 1 << 1,
82         kPtrIs64Bit_Flag    = 1 << 2,
83         kValidation_Flag    = 1 << 3,
84     };
85 
setFlags(uint32_t flags)86     void setFlags(uint32_t flags) { fFlags = flags; }
getFlags()87     uint32_t getFlags() const { return fFlags; }
88 
isCrossProcess()89     bool isCrossProcess() const {
90         return this->isValidating() || SkToBool(fFlags & kCrossProcess_Flag);
91     }
isScalarFloat()92     bool isScalarFloat() const { return SkToBool(fFlags & kScalarIsFloat_Flag); }
isPtr64Bit()93     bool isPtr64Bit() const { return SkToBool(fFlags & kPtrIs64Bit_Flag); }
isValidating()94     bool isValidating() const { return SkToBool(fFlags & kValidation_Flag); }
95 
getReader32()96     SkReader32* getReader32() { return &fReader; }
97 
size()98     size_t size() { return fReader.size(); }
offset()99     size_t offset() { return fReader.offset(); }
eof()100     bool eof() { return fReader.eof(); }
skip(size_t size)101     virtual const void* skip(size_t size) { return fReader.skip(size); }
readFunctionPtr()102     void* readFunctionPtr() { return fReader.readPtr(); }
103 
104     // primitives
105     virtual bool readBool();
106     virtual SkColor readColor();
107     virtual SkFixed readFixed();
108     virtual int32_t readInt();
109     virtual SkScalar readScalar();
110     virtual uint32_t readUInt();
111     virtual int32_t read32();
112 
113     // 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);
readPoint()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);
124 
125     virtual void readPath(SkPath* path);
readPaint(SkPaint * paint)126     void readPaint(SkPaint* paint) { paint->unflatten(*this); }
127 
128     virtual SkFlattenable* readFlattenable(SkFlattenable::Type);
readFlattenable()129     template <typename T> T* readFlattenable() {
130         return (T*) this->readFlattenable(T::GetFlattenableType());
131     }
readColorFilter()132     SkColorFilter* readColorFilter() { return this->readFlattenable<SkColorFilter>(); }
readDrawLooper()133     SkDrawLooper*  readDrawLooper()  { return this->readFlattenable<SkDrawLooper>(); }
readImageFilter()134     SkImageFilter* readImageFilter() { return this->readFlattenable<SkImageFilter>(); }
readMaskFilter()135     SkMaskFilter*  readMaskFilter()  { return this->readFlattenable<SkMaskFilter>(); }
readPathEffect()136     SkPathEffect*  readPathEffect()  { return this->readFlattenable<SkPathEffect>(); }
readRasterizer()137     SkRasterizer*  readRasterizer()  { return this->readFlattenable<SkRasterizer>(); }
readShader()138     SkShader*      readShader()      { return this->readFlattenable<SkShader>(); }
readXfermode()139     SkXfermode*    readXfermode()    { return this->readFlattenable<SkXfermode>(); }
140 
141     /**
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();
146 
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 
readByteArrayAsData()154     SkData* readByteArrayAsData() {
155         size_t len = this->getArrayCount();
156         if (!this->validateAvailable(len)) {
157             return SkData::NewEmpty();
158         }
159         void* buffer = sk_malloc_throw(len);
160         this->readByteArray(buffer, len);
161         return SkData::NewFromMalloc(buffer, len);
162     }
163 
164     // helpers to get info about arrays and binary data
165     virtual uint32_t getArrayCount();
166 
167     /**
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 
173     SkImage* readImage();
174 
175     virtual SkTypeface* readTypeface();
176 
setBitmapStorage(SkBitmapHeapReader * bitmapStorage)177     void setBitmapStorage(SkBitmapHeapReader* bitmapStorage) {
178         SkRefCnt_SafeAssign(fBitmapStorage, bitmapStorage);
179     }
180 
setTypefaceArray(SkTypeface * array[],int count)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      */
setFactoryPlayback(SkFlattenable::Factory array[],int count)190     void setFactoryPlayback(SkFlattenable::Factory array[], int count) {
191         fFactoryTDArray = nullptr;
192         fFactoryArray = array;
193         fFactoryCount = count;
194     }
195 
196     /**
197      *  Call this with an initially empty array, so the reader can cache each
198      *  factory it sees by name. Used by the pipe code in conjunction with
199      *  SkWriteBuffer::setNamedFactoryRecorder.
200      */
setFactoryArray(SkTDArray<SkFlattenable::Factory> * array)201     void setFactoryArray(SkTDArray<SkFlattenable::Factory>* array) {
202         fFactoryTDArray = array;
203         fFactoryArray = nullptr;
204         fFactoryCount = 0;
205     }
206 
207     /**
208      *  Provide a function to decode an SkBitmap from encoded data. Only used if the writer
209      *  encoded the SkBitmap. If the proper decoder cannot be used, a red bitmap with the
210      *  appropriate size will be used.
211      */
setBitmapDecoder(SkPicture::InstallPixelRefProc bitmapDecoder)212     void setBitmapDecoder(SkPicture::InstallPixelRefProc bitmapDecoder) {
213         fBitmapDecoder = bitmapDecoder;
214     }
215 
216     // Default impelementations don't check anything.
validate(bool isValid)217     virtual bool validate(bool isValid) { return true; }
isValid()218     virtual bool isValid() const { return true; }
validateAvailable(size_t size)219     virtual bool validateAvailable(size_t size) { return true; }
220 
221 protected:
222     SkReader32 fReader;
223 
224 private:
225     bool readArray(void* value, size_t size, size_t elementSize);
226 
227     uint32_t fFlags;
228     int fVersion;
229 
230     void* fMemoryPtr;
231 
232     SkBitmapHeapReader* fBitmapStorage;
233     SkTypeface** fTFArray;
234     int        fTFCount;
235 
236     SkTDArray<SkFlattenable::Factory>* fFactoryTDArray;
237     SkFlattenable::Factory* fFactoryArray;
238     int                     fFactoryCount;
239 
240     SkPicture::InstallPixelRefProc fBitmapDecoder;
241 
242 #ifdef DEBUG_NON_DETERMINISTIC_ASSERT
243     // Debugging counter to keep track of how many bitmaps we
244     // have decoded.
245     int fDecodedBitmapIndex;
246 #endif // DEBUG_NON_DETERMINISTIC_ASSERT
247 };
248 
249 #endif // SkReadBuffer_DEFINED
250