Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 | 1 // Temporary shim to keep a couple dependencies working in Chromium. |
| 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 | 2 #ifndef SkFlattenableBuffers_DEFINED |
| 10 #define SkFlattenableBuffers_DEFINED | 3 #define SkFlattenableBuffers_DEFINED |
| 11 | 4 |
| 12 #include "SkColor.h" | 5 #include "SkReadBuffer.h" |
| 13 #include "SkData.h" | 6 #include "SkWriteBuffer.h" |
| 14 #include "SkPaint.h" | |
| 15 #include "SkPoint.h" | |
| 16 | 7 |
| 17 class SkBitmap; | 8 typedef SkReadBuffer SkFlattenableReadBuffer; |
|
scroggo
2014/01/29 20:07:21
Do we need this typedef? Is there any reason not t
mtklein
2014/01/29 20:38:29
We don't, but Chromium does. Once I can clean tha
| |
| 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 | 9 |
| 34 class SkFlattenableReadBuffer { | 10 #endif//SkFlattenableBuffers_DEFINED |
| 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 encodi ng) = 0; | |
| 73 | |
| 74 /** | |
| 75 @param type This parameter is only used when using SkValidatingReadBuffe r. It will verify | |
| 76 that the object about to be deserialized is of the given typ e or early return | |
| 77 NULL otherwise. The type provided here is the type of the ba se 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 specifie s the allocation | |
| 105 * size in number of elements (or in bytes, for void*) of the pointer param eter. 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 ot herwise | |
| 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 nex t 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 = NULL; | |
| 143 if (this->validateAvailable(len)) { | |
| 144 buffer = sk_malloc_throw(len); | |
| 145 (void)this->readByteArray(buffer, len); | |
| 146 } else { | |
| 147 len = 0; | |
| 148 } | |
| 149 return SkData::NewFromMalloc(buffer, len); | |
| 150 } | |
| 151 | |
| 152 /** This function validates that the isValid input parameter is true | |
| 153 * If isValidating() is false, then true is always returned | |
| 154 * If isValidating() is true, then true is returned until validate() is cal led with isValid | |
| 155 * set to false. When isValid is false, an error flag will be set internall y and, from that | |
| 156 * point on, validate() will return false. The error flag cannot be unset. | |
| 157 * | |
| 158 * @param isValid result of a test that is expected to be true | |
| 159 */ | |
| 160 virtual bool validate(bool isValid); | |
| 161 | |
| 162 /** This function returns true by default | |
| 163 * If isValidating() is true, it will return false if the internal error fl ag is set. | |
| 164 * Otherwise, it will return true. | |
| 165 */ | |
| 166 virtual bool isValid() const { return true; } | |
| 167 | |
| 168 /** This function returns true by default | |
| 169 * If isValidating() is true, it will return whether there's | |
| 170 * at least "size" memory left to read in the stream. | |
| 171 * | |
| 172 * @param size amount of memory that should still be available | |
| 173 */ | |
| 174 virtual bool validateAvailable(size_t size) { return true; } | |
| 175 | |
| 176 private: | |
| 177 template <typename T> T* readFlattenableT(); | |
| 178 uint32_t fFlags; | |
| 179 }; | |
| 180 | |
| 181 /////////////////////////////////////////////////////////////////////////////// | |
| 182 | |
| 183 class SkFlattenableWriteBuffer { | |
| 184 public: | |
| 185 SkFlattenableWriteBuffer(); | |
| 186 virtual ~SkFlattenableWriteBuffer(); | |
| 187 | |
| 188 virtual bool isOrderedBinaryBuffer() { return false; } | |
| 189 virtual SkOrderedWriteBuffer* getOrderedBinaryBuffer() { sk_throw(); return NULL; } | |
| 190 | |
| 191 // primitives | |
| 192 virtual void writeByteArray(const void* data, size_t size) = 0; | |
| 193 virtual void writeBool(bool value) = 0; | |
| 194 virtual void writeFixed(SkFixed value) = 0; | |
| 195 virtual void writeScalar(SkScalar value) = 0; | |
| 196 virtual void writeScalarArray(const SkScalar* value, uint32_t count) = 0; | |
| 197 virtual void writeInt(int32_t value) = 0; | |
| 198 virtual void writeIntArray(const int32_t* value, uint32_t count) = 0; | |
| 199 virtual void writeUInt(uint32_t value) = 0; | |
| 200 virtual void write32(int32_t value) = 0; // printf in hex | |
| 201 virtual void writeString(const char* value) = 0; | |
| 202 virtual void writeEncodedString(const void* value, size_t byteLength, | |
| 203 SkPaint::TextEncoding encoding) = 0; | |
| 204 | |
| 205 // common data structures | |
| 206 virtual void writeFlattenable(const SkFlattenable* flattenable) = 0; | |
| 207 virtual void writeColor(const SkColor& color) = 0; | |
| 208 virtual void writeColorArray(const SkColor* color, uint32_t count) = 0; | |
| 209 virtual void writePoint(const SkPoint& point) = 0; | |
| 210 virtual void writePointArray(const SkPoint* points, uint32_t count) = 0; | |
| 211 virtual void writeMatrix(const SkMatrix& matrix) = 0; | |
| 212 virtual void writeIRect(const SkIRect& rect) = 0; | |
| 213 virtual void writeRect(const SkRect& rect) = 0; | |
| 214 virtual void writeRegion(const SkRegion& region) = 0; | |
| 215 virtual void writePath(const SkPath& path) = 0; | |
| 216 virtual size_t writeStream(SkStream* stream, size_t length) = 0; | |
| 217 | |
| 218 // helper functions | |
| 219 virtual void writeFunctionPtr(void* ptr); | |
| 220 virtual void writePaint(const SkPaint& paint); | |
| 221 | |
| 222 virtual void writeBitmap(const SkBitmap& bitmap) = 0; | |
| 223 virtual void writeTypeface(SkTypeface* typeface) = 0; | |
| 224 | |
| 225 virtual bool writeToStream(SkWStream*) = 0; | |
| 226 | |
| 227 enum Flags { | |
| 228 kCrossProcess_Flag = 0x01, | |
| 229 /** The kValidation_Flag is used here to make sure the write operation | |
| 230 * is symmetric with the read operation using the equivalent flag | |
| 231 * SkFlattenableReadBuffer::kValidation_Flag. | |
| 232 */ | |
| 233 kValidation_Flag = 0x02, | |
| 234 }; | |
| 235 | |
| 236 uint32_t getFlags() const { return fFlags; } | |
| 237 void setFlags(uint32_t flags) { fFlags = flags; } | |
| 238 | |
| 239 bool isCrossProcess() const { | |
| 240 return SkToBool(fFlags & (kCrossProcess_Flag | kValidation_Flag)); | |
| 241 } | |
| 242 | |
| 243 bool isValidating() const { | |
| 244 return SkToBool(fFlags & kValidation_Flag); | |
| 245 } | |
| 246 | |
| 247 bool persistTypeface() const { return (fFlags & kCrossProcess_Flag) != 0; } | |
| 248 | |
| 249 void writeDataAsByteArray(SkData* data) { | |
| 250 this->writeByteArray(data->data(), data->size()); | |
| 251 } | |
| 252 | |
| 253 protected: | |
| 254 // A helper function so that each subclass does not have to be a friend of S kFlattenable | |
| 255 void flattenObject(const SkFlattenable* obj, SkFlattenableWriteBuffer& buffe r); | |
| 256 | |
| 257 uint32_t fFlags; | |
| 258 }; | |
| 259 | |
| 260 #endif | |
| OLD | NEW |