1 2 /* 3 * Copyright 2012 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 SkFlattenableBuffers_DEFINED 10 #define SkFlattenableBuffers_DEFINED 11 12 #include "SkColor.h" 13 #include "SkData.h" 14 #include "SkPaint.h" 15 #include "SkPoint.h" 16 17 class SkBitmap; 18 class SkDrawLooper; 19 class SkFlattenable; 20 struct SkIRect; 21 class SkMatrix; 22 class SkOrderedReadBuffer; 23 class SkOrderedWriteBuffer; 24 class SkPath; 25 class SkPixelRef; 26 struct SkRect; 27 class SkRegion; 28 class SkStream; 29 class SkString; 30 class SkTypeface; 31 class SkUnitMapper; 32 class SkWStream; 33 34 class SkFlattenableReadBuffer { 35 public: 36 SkFlattenableReadBuffer(); 37 virtual ~SkFlattenableReadBuffer(); 38 39 bool isOrderedBinaryBuffer() { return NULL != getOrderedBinaryBuffer(); } 40 virtual SkOrderedReadBuffer* getOrderedBinaryBuffer() { return NULL; } 41 42 enum Flags { 43 kCrossProcess_Flag = 1 << 0, 44 kScalarIsFloat_Flag = 1 << 1, 45 kPtrIs64Bit_Flag = 1 << 2, 46 /** The kValidation_Flag is used to force stream validations (by making 47 * sure that no operation reads past the end of the stream, for example) 48 * and error handling if any reading operation yields an invalid value. 49 */ 50 kValidation_Flag = 1 << 3, 51 }; 52 53 void setFlags(uint32_t flags) { fFlags = flags; } 54 uint32_t getFlags() const { return fFlags; } 55 56 bool isCrossProcess() const { return SkToBool(fFlags & (kCrossProcess_Flag | kValidation_Flag)); } 57 bool isScalarFloat() const { return SkToBool(fFlags & kScalarIsFloat_Flag); } 58 bool isPtr64Bit() const { return SkToBool(fFlags & kPtrIs64Bit_Flag); } 59 bool isValidating() const { return SkToBool(fFlags & kValidation_Flag); } 60 61 // primitives 62 virtual bool readBool() = 0; 63 virtual SkColor readColor() = 0; 64 virtual SkFixed readFixed() = 0; 65 virtual int32_t readInt() = 0; 66 virtual SkScalar readScalar() = 0; 67 virtual uint32_t readUInt() = 0; 68 virtual int32_t read32() = 0; 69 70 // strings -- the caller is responsible for freeing the string contents 71 virtual void readString(SkString* string) = 0; 72 virtual void* readEncodedString(size_t* length, SkPaint::TextEncoding encoding) = 0; 73 74 /** 75 @param type This parameter is only used when using SkValidatingReadBuffer. It will verify 76 that the object about to be deserialized is of the given type or early return 77 NULL otherwise. The type provided here is the type of the base class of the 78 object to deserialize. 79 */ 80 virtual SkFlattenable* readFlattenable(SkFlattenable::Type type) = 0; 81 82 SkColorFilter* readColorFilter(); 83 SkDrawLooper* readDrawLooper(); 84 SkImageFilter* readImageFilter(); 85 SkMaskFilter* readMaskFilter(); 86 SkPathEffect* readPathEffect(); 87 SkPixelRef* readPixelRef(); 88 SkRasterizer* readRasterizer(); 89 SkShader* readShader(); 90 SkUnitMapper* readUnitMapper(); 91 SkXfermode* readXfermode(); 92 93 // common data structures 94 virtual void readPoint(SkPoint* point) = 0; 95 virtual void readMatrix(SkMatrix* matrix) = 0; 96 virtual void readIRect(SkIRect* rect) = 0; 97 virtual void readRect(SkRect* rect) = 0; 98 virtual void readRegion(SkRegion* region) = 0; 99 virtual void readPath(SkPath* path) = 0; 100 101 // binary data and arrays 102 103 /** 104 * In the following read.*Array(...) functions, the size parameter specifies the allocation 105 * size in number of elements (or in bytes, for void*) of the pointer parameter. If the 106 * pointer parameter's size does not match the size to be read, the pointer parameter's memory 107 * will then stay uninitialized, the cursor will be moved to the end of the stream and, in the 108 * case where isValidating() is true, an error flag will be set internally (see 109 * SkValidatingReadBuffer). 110 * If the sizes match, then "size" amount of memory will be read. 111 * 112 * @param size amount of memory expected to be read 113 * @return true if the size parameter matches the size to be read, false otherwise 114 */ 115 virtual bool readByteArray(void* value, size_t size) = 0; 116 virtual bool readColorArray(SkColor* colors, size_t size) = 0; 117 virtual bool readIntArray(int32_t* values, size_t size) = 0; 118 virtual bool readPointArray(SkPoint* points, size_t size) = 0; 119 virtual bool readScalarArray(SkScalar* values, size_t size) = 0; 120 121 /** This helper peeks into the buffer and reports back the length of the next array in 122 * the buffer but does not change the state of the buffer. 123 */ 124 virtual uint32_t getArrayCount() = 0; 125 126 // helper functions 127 virtual void* readFunctionPtr(); 128 virtual void readPaint(SkPaint* paint); 129 130 virtual void readBitmap(SkBitmap* bitmap) = 0; 131 virtual SkTypeface* readTypeface() = 0; 132 133 // helper function for classes with const SkPoint members 134 SkPoint readPoint() { 135 SkPoint point; 136 this->readPoint(&point); 137 return point; 138 } 139 140 SkData* readByteArrayAsData() { 141 size_t len = this->getArrayCount(); 142 void* buffer = sk_malloc_throw(len); 143 (void)this->readByteArray(buffer, len); 144 return SkData::NewFromMalloc(buffer, len); 145 } 146 147 /** This function validates that the isValid input parameter is true 148 * If isValidating() is false, then true is always returned 149 * If isValidating() is true, then true is returned until validate() is called with isValid 150 * set to false. When isValid is false, an error flag will be set internally and, from that 151 * point on, validate() will return false. The error flag cannot be unset. 152 * 153 * @param isValid result of a test that is expected to be true 154 */ 155 virtual bool validate(bool isValid); 156 157 /** This function returns true by default 158 * If isValidating() is true, it will return false if the internal error flag is set. 159 * Otherwise, it will return true. 160 */ 161 virtual bool isValid() const { return true; } 162 163 private: 164 template <typename T> T* readFlattenableT(); 165 uint32_t fFlags; 166 }; 167 168 /////////////////////////////////////////////////////////////////////////////// 169 170 class SkFlattenableWriteBuffer { 171 public: 172 SkFlattenableWriteBuffer(); 173 virtual ~SkFlattenableWriteBuffer(); 174 175 virtual bool isOrderedBinaryBuffer() { return false; } 176 virtual SkOrderedWriteBuffer* getOrderedBinaryBuffer() { sk_throw(); return NULL; } 177 178 // primitives 179 virtual void writeByteArray(const void* data, size_t size) = 0; 180 virtual void writeBool(bool value) = 0; 181 virtual void writeFixed(SkFixed value) = 0; 182 virtual void writeScalar(SkScalar value) = 0; 183 virtual void writeScalarArray(const SkScalar* value, uint32_t count) = 0; 184 virtual void writeInt(int32_t value) = 0; 185 virtual void writeIntArray(const int32_t* value, uint32_t count) = 0; 186 virtual void writeUInt(uint32_t value) = 0; 187 virtual void write32(int32_t value) = 0; // printf in hex 188 virtual void writeString(const char* value) = 0; 189 virtual void writeEncodedString(const void* value, size_t byteLength, 190 SkPaint::TextEncoding encoding) = 0; 191 192 // common data structures 193 virtual void writeFlattenable(const SkFlattenable* flattenable) = 0; 194 virtual void writeColor(const SkColor& color) = 0; 195 virtual void writeColorArray(const SkColor* color, uint32_t count) = 0; 196 virtual void writePoint(const SkPoint& point) = 0; 197 virtual void writePointArray(const SkPoint* points, uint32_t count) = 0; 198 virtual void writeMatrix(const SkMatrix& matrix) = 0; 199 virtual void writeIRect(const SkIRect& rect) = 0; 200 virtual void writeRect(const SkRect& rect) = 0; 201 virtual void writeRegion(const SkRegion& region) = 0; 202 virtual void writePath(const SkPath& path) = 0; 203 virtual size_t writeStream(SkStream* stream, size_t length) = 0; 204 205 // helper functions 206 virtual void writeFunctionPtr(void* ptr); 207 virtual void writePaint(const SkPaint& paint); 208 209 virtual void writeBitmap(const SkBitmap& bitmap) = 0; 210 virtual void writeTypeface(SkTypeface* typeface) = 0; 211 212 virtual bool writeToStream(SkWStream*) = 0; 213 214 enum Flags { 215 kCrossProcess_Flag = 0x01, 216 /** The kValidation_Flag is used here to make sure the write operation 217 * is symmetric with the read operation using the equivalent flag 218 * SkFlattenableReadBuffer::kValidation_Flag. 219 */ 220 kValidation_Flag = 0x02, 221 }; 222 223 uint32_t getFlags() const { return fFlags; } 224 void setFlags(uint32_t flags) { fFlags = flags; } 225 226 bool isCrossProcess() const { 227 return SkToBool(fFlags & (kCrossProcess_Flag | kValidation_Flag)); 228 } 229 230 bool isValidating() const { 231 return SkToBool(fFlags & kValidation_Flag); 232 } 233 234 bool persistTypeface() const { return (fFlags & kCrossProcess_Flag) != 0; } 235 236 void writeDataAsByteArray(SkData* data) { 237 this->writeByteArray(data->data(), data->size()); 238 } 239 240 protected: 241 // A helper function so that each subclass does not have to be a friend of SkFlattenable 242 void flattenObject(const SkFlattenable* obj, SkFlattenableWriteBuffer& buffer); 243 244 uint32_t fFlags; 245 }; 246 247 #endif 248