commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
| 9 | #ifndef SkReadBuffer_DEFINED |
| 10 | #define SkReadBuffer_DEFINED |
| 11 | |
| 12 | #include "SkBitmapHeap.h" |
| 13 | #include "SkColorFilter.h" |
| 14 | #include "SkData.h" |
| 15 | #include "SkDrawLooper.h" |
| 16 | #include "SkImageFilter.h" |
| 17 | #include "SkMaskFilter.h" |
| 18 | #include "SkPath.h" |
| 19 | #include "SkPathEffect.h" |
| 20 | #include "SkPicture.h" |
| 21 | #include "SkPixelRef.h" |
| 22 | #include "SkRasterizer.h" |
| 23 | #include "SkReadBuffer.h" |
| 24 | #include "SkReader32.h" |
| 25 | #include "SkRefCnt.h" |
| 26 | #include "SkShader.h" |
| 27 | #include "SkUnitMapper.h" |
| 28 | #include "SkWriteBuffer.h" |
| 29 | #include "SkXfermode.h" |
| 30 | |
| 31 | class SkBitmap; |
| 32 | |
| 33 | #if defined(SK_DEBUG) && defined(SK_BUILD_FOR_MAC) |
| 34 | #define DEBUG_NON_DETERMINISTIC_ASSERT |
| 35 | #endif |
| 36 | |
| 37 | class SkReadBuffer { |
| 38 | public: |
| 39 | SkReadBuffer(); |
| 40 | SkReadBuffer(const void* data, size_t size); |
| 41 | SkReadBuffer(SkStream* stream); |
| 42 | virtual ~SkReadBuffer(); |
| 43 | |
commit-bot@chromium.org | 0943f5f | 2014-03-28 18:05:47 +0000 | [diff] [blame] | 44 | /** Return the version of the serialized picture this buffer holds, or 0 if unset. */ |
| 45 | int pictureVersion() const { return fPictureVersion; } |
| 46 | |
| 47 | /** This may be called at most once; most clients of SkReadBuffer should not mess with it. */ |
| 48 | void setPictureVersion(int version) { |
fmalita@google.com | 667240a | 2014-04-08 14:11:26 +0000 | [diff] [blame] | 49 | SkASSERT(0 == fPictureVersion || version == fPictureVersion); |
commit-bot@chromium.org | 0943f5f | 2014-03-28 18:05:47 +0000 | [diff] [blame] | 50 | fPictureVersion = version; |
| 51 | } |
| 52 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 53 | enum Flags { |
| 54 | kCrossProcess_Flag = 1 << 0, |
| 55 | kScalarIsFloat_Flag = 1 << 1, |
| 56 | kPtrIs64Bit_Flag = 1 << 2, |
| 57 | kValidation_Flag = 1 << 3, |
| 58 | }; |
| 59 | |
| 60 | void setFlags(uint32_t flags) { fFlags = flags; } |
| 61 | uint32_t getFlags() const { return fFlags; } |
| 62 | |
| 63 | bool isCrossProcess() const { |
| 64 | return this->isValidating() || SkToBool(fFlags & kCrossProcess_Flag); |
| 65 | } |
| 66 | bool isScalarFloat() const { return SkToBool(fFlags & kScalarIsFloat_Flag); } |
| 67 | bool isPtr64Bit() const { return SkToBool(fFlags & kPtrIs64Bit_Flag); } |
| 68 | bool isValidating() const { return SkToBool(fFlags & kValidation_Flag); } |
| 69 | |
| 70 | SkReader32* getReader32() { return &fReader; } |
| 71 | |
commit-bot@chromium.org | f117781 | 2014-04-23 19:19:44 +0000 | [diff] [blame^] | 72 | size_t size() { return fReader.size(); } |
| 73 | size_t offset() { return fReader.offset(); } |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 74 | bool eof() { return fReader.eof(); } |
| 75 | const void* skip(size_t size) { return fReader.skip(size); } |
commit-bot@chromium.org | 39426e2 | 2014-04-16 16:24:08 +0000 | [diff] [blame] | 76 | void* readFunctionPtr() { return fReader.readPtr(); } |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 77 | |
| 78 | // primitives |
| 79 | virtual bool readBool(); |
| 80 | virtual SkColor readColor(); |
| 81 | virtual SkFixed readFixed(); |
| 82 | virtual int32_t readInt(); |
| 83 | virtual SkScalar readScalar(); |
| 84 | virtual uint32_t readUInt(); |
| 85 | virtual int32_t read32(); |
| 86 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 87 | // strings -- the caller is responsible for freeing the string contents |
| 88 | virtual void readString(SkString* string); |
| 89 | virtual void* readEncodedString(size_t* length, SkPaint::TextEncoding encoding); |
| 90 | |
| 91 | // common data structures |
| 92 | virtual void readPoint(SkPoint* point); |
| 93 | SkPoint readPoint() { SkPoint p; this->readPoint(&p); return p; } |
| 94 | virtual void readMatrix(SkMatrix* matrix); |
| 95 | virtual void readIRect(SkIRect* rect); |
| 96 | virtual void readRect(SkRect* rect); |
| 97 | virtual void readRegion(SkRegion* region); |
| 98 | virtual void readPath(SkPath* path); |
| 99 | void readPaint(SkPaint* paint) { paint->unflatten(*this); } |
| 100 | |
| 101 | virtual SkFlattenable* readFlattenable(SkFlattenable::Type); |
| 102 | template <typename T> T* readFlattenable() { |
| 103 | return (T*) this->readFlattenable(T::GetFlattenableType()); |
| 104 | } |
| 105 | SkColorFilter* readColorFilter() { return this->readFlattenable<SkColorFilter>(); } |
| 106 | SkDrawLooper* readDrawLooper() { return this->readFlattenable<SkDrawLooper>(); } |
| 107 | SkImageFilter* readImageFilter() { return this->readFlattenable<SkImageFilter>(); } |
| 108 | SkMaskFilter* readMaskFilter() { return this->readFlattenable<SkMaskFilter>(); } |
| 109 | SkPathEffect* readPathEffect() { return this->readFlattenable<SkPathEffect>(); } |
| 110 | SkPixelRef* readPixelRef() { return this->readFlattenable<SkPixelRef>(); } |
| 111 | SkRasterizer* readRasterizer() { return this->readFlattenable<SkRasterizer>(); } |
| 112 | SkShader* readShader() { return this->readFlattenable<SkShader>(); } |
| 113 | SkUnitMapper* readUnitMapper() { return this->readFlattenable<SkUnitMapper>(); } |
| 114 | SkXfermode* readXfermode() { return this->readFlattenable<SkXfermode>(); } |
| 115 | |
| 116 | |
| 117 | // binary data and arrays |
| 118 | virtual bool readByteArray(void* value, size_t size); |
| 119 | virtual bool readColorArray(SkColor* colors, size_t size); |
| 120 | virtual bool readIntArray(int32_t* values, size_t size); |
| 121 | virtual bool readPointArray(SkPoint* points, size_t size); |
| 122 | virtual bool readScalarArray(SkScalar* values, size_t size); |
| 123 | |
| 124 | SkData* readByteArrayAsData() { |
| 125 | size_t len = this->getArrayCount(); |
| 126 | if (!this->validateAvailable(len)) { |
| 127 | return SkData::NewEmpty(); |
| 128 | } |
| 129 | void* buffer = sk_malloc_throw(len); |
| 130 | this->readByteArray(buffer, len); |
| 131 | return SkData::NewFromMalloc(buffer, len); |
| 132 | } |
| 133 | |
| 134 | // helpers to get info about arrays and binary data |
| 135 | virtual uint32_t getArrayCount(); |
| 136 | |
| 137 | virtual void readBitmap(SkBitmap* bitmap); |
| 138 | virtual SkTypeface* readTypeface(); |
| 139 | |
| 140 | void setBitmapStorage(SkBitmapHeapReader* bitmapStorage) { |
| 141 | SkRefCnt_SafeAssign(fBitmapStorage, bitmapStorage); |
| 142 | } |
| 143 | |
| 144 | void setTypefaceArray(SkTypeface* array[], int count) { |
| 145 | fTFArray = array; |
| 146 | fTFCount = count; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Call this with a pre-loaded array of Factories, in the same order as |
| 151 | * were created/written by the writer. SkPicture uses this. |
| 152 | */ |
| 153 | void setFactoryPlayback(SkFlattenable::Factory array[], int count) { |
| 154 | fFactoryTDArray = NULL; |
| 155 | fFactoryArray = array; |
| 156 | fFactoryCount = count; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Call this with an initially empty array, so the reader can cache each |
| 161 | * factory it sees by name. Used by the pipe code in conjunction with |
| 162 | * SkWriteBuffer::setNamedFactoryRecorder. |
| 163 | */ |
| 164 | void setFactoryArray(SkTDArray<SkFlattenable::Factory>* array) { |
| 165 | fFactoryTDArray = array; |
| 166 | fFactoryArray = NULL; |
| 167 | fFactoryCount = 0; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Provide a function to decode an SkBitmap from encoded data. Only used if the writer |
| 172 | * encoded the SkBitmap. If the proper decoder cannot be used, a red bitmap with the |
| 173 | * appropriate size will be used. |
| 174 | */ |
| 175 | void setBitmapDecoder(SkPicture::InstallPixelRefProc bitmapDecoder) { |
| 176 | fBitmapDecoder = bitmapDecoder; |
| 177 | } |
| 178 | |
| 179 | // Default impelementations don't check anything. |
| 180 | virtual bool validate(bool isValid) { return true; } |
| 181 | virtual bool isValid() const { return true; } |
| 182 | virtual bool validateAvailable(size_t size) { return true; } |
| 183 | |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 184 | protected: |
| 185 | SkReader32 fReader; |
| 186 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 187 | private: |
| 188 | bool readArray(void* value, size_t size, size_t elementSize); |
| 189 | |
| 190 | uint32_t fFlags; |
commit-bot@chromium.org | 0943f5f | 2014-03-28 18:05:47 +0000 | [diff] [blame] | 191 | int fPictureVersion; |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 192 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 193 | void* fMemoryPtr; |
| 194 | |
| 195 | SkBitmapHeapReader* fBitmapStorage; |
| 196 | SkTypeface** fTFArray; |
| 197 | int fTFCount; |
| 198 | |
| 199 | SkTDArray<SkFlattenable::Factory>* fFactoryTDArray; |
| 200 | SkFlattenable::Factory* fFactoryArray; |
| 201 | int fFactoryCount; |
| 202 | |
| 203 | SkPicture::InstallPixelRefProc fBitmapDecoder; |
| 204 | |
| 205 | #ifdef DEBUG_NON_DETERMINISTIC_ASSERT |
| 206 | // Debugging counter to keep track of how many bitmaps we |
| 207 | // have decoded. |
| 208 | int fDecodedBitmapIndex; |
| 209 | #endif // DEBUG_NON_DETERMINISTIC_ASSERT |
| 210 | }; |
| 211 | |
| 212 | #endif // SkReadBuffer_DEFINED |