| OLD | NEW |
| (Empty) |
| 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 #include "SkOrderedWriteBuffer.h" | |
| 10 #include "SkBitmap.h" | |
| 11 #include "SkData.h" | |
| 12 #include "SkPixelRef.h" | |
| 13 #include "SkPtrRecorder.h" | |
| 14 #include "SkStream.h" | |
| 15 #include "SkTypeface.h" | |
| 16 | |
| 17 SkOrderedWriteBuffer::SkOrderedWriteBuffer() | |
| 18 : INHERITED() | |
| 19 , fFactorySet(NULL) | |
| 20 , fNamedFactorySet(NULL) | |
| 21 , fBitmapHeap(NULL) | |
| 22 , fTFSet(NULL) | |
| 23 , fBitmapEncoder(NULL) { | |
| 24 } | |
| 25 | |
| 26 SkOrderedWriteBuffer::SkOrderedWriteBuffer(void* storage, size_t storageSize) | |
| 27 : INHERITED() | |
| 28 , fFactorySet(NULL) | |
| 29 , fNamedFactorySet(NULL) | |
| 30 , fWriter(storage, storageSize) | |
| 31 , fBitmapHeap(NULL) | |
| 32 , fTFSet(NULL) | |
| 33 , fBitmapEncoder(NULL) { | |
| 34 } | |
| 35 | |
| 36 SkOrderedWriteBuffer::~SkOrderedWriteBuffer() { | |
| 37 SkSafeUnref(fFactorySet); | |
| 38 SkSafeUnref(fNamedFactorySet); | |
| 39 SkSafeUnref(fBitmapHeap); | |
| 40 SkSafeUnref(fTFSet); | |
| 41 } | |
| 42 | |
| 43 void SkOrderedWriteBuffer::writeByteArray(const void* data, size_t size) { | |
| 44 fWriter.write32(size); | |
| 45 fWriter.writePad(data, size); | |
| 46 } | |
| 47 | |
| 48 void SkOrderedWriteBuffer::writeBool(bool value) { | |
| 49 fWriter.writeBool(value); | |
| 50 } | |
| 51 | |
| 52 void SkOrderedWriteBuffer::writeFixed(SkFixed value) { | |
| 53 fWriter.write32(value); | |
| 54 } | |
| 55 | |
| 56 void SkOrderedWriteBuffer::writeScalar(SkScalar value) { | |
| 57 fWriter.writeScalar(value); | |
| 58 } | |
| 59 | |
| 60 void SkOrderedWriteBuffer::writeScalarArray(const SkScalar* value, uint32_t coun
t) { | |
| 61 fWriter.write32(count); | |
| 62 fWriter.write(value, count * sizeof(SkScalar)); | |
| 63 } | |
| 64 | |
| 65 void SkOrderedWriteBuffer::writeInt(int32_t value) { | |
| 66 fWriter.write32(value); | |
| 67 } | |
| 68 | |
| 69 void SkOrderedWriteBuffer::writeIntArray(const int32_t* value, uint32_t count) { | |
| 70 fWriter.write32(count); | |
| 71 fWriter.write(value, count * sizeof(int32_t)); | |
| 72 } | |
| 73 | |
| 74 void SkOrderedWriteBuffer::writeUInt(uint32_t value) { | |
| 75 fWriter.write32(value); | |
| 76 } | |
| 77 | |
| 78 void SkOrderedWriteBuffer::write32(int32_t value) { | |
| 79 fWriter.write32(value); | |
| 80 } | |
| 81 | |
| 82 void SkOrderedWriteBuffer::writeString(const char* value) { | |
| 83 fWriter.writeString(value); | |
| 84 } | |
| 85 | |
| 86 void SkOrderedWriteBuffer::writeEncodedString(const void* value, size_t byteLeng
th, | |
| 87 SkPaint::TextEncoding encoding) { | |
| 88 fWriter.writeInt(encoding); | |
| 89 fWriter.writeInt(byteLength); | |
| 90 fWriter.write(value, byteLength); | |
| 91 } | |
| 92 | |
| 93 | |
| 94 void SkOrderedWriteBuffer::writeColor(const SkColor& color) { | |
| 95 fWriter.write32(color); | |
| 96 } | |
| 97 | |
| 98 void SkOrderedWriteBuffer::writeColorArray(const SkColor* color, uint32_t count)
{ | |
| 99 fWriter.write32(count); | |
| 100 fWriter.write(color, count * sizeof(SkColor)); | |
| 101 } | |
| 102 | |
| 103 void SkOrderedWriteBuffer::writePoint(const SkPoint& point) { | |
| 104 fWriter.writeScalar(point.fX); | |
| 105 fWriter.writeScalar(point.fY); | |
| 106 } | |
| 107 | |
| 108 void SkOrderedWriteBuffer::writePointArray(const SkPoint* point, uint32_t count)
{ | |
| 109 fWriter.write32(count); | |
| 110 fWriter.write(point, count * sizeof(SkPoint)); | |
| 111 } | |
| 112 | |
| 113 void SkOrderedWriteBuffer::writeMatrix(const SkMatrix& matrix) { | |
| 114 fWriter.writeMatrix(matrix); | |
| 115 } | |
| 116 | |
| 117 void SkOrderedWriteBuffer::writeIRect(const SkIRect& rect) { | |
| 118 fWriter.write(&rect, sizeof(SkIRect)); | |
| 119 } | |
| 120 | |
| 121 void SkOrderedWriteBuffer::writeRect(const SkRect& rect) { | |
| 122 fWriter.writeRect(rect); | |
| 123 } | |
| 124 | |
| 125 void SkOrderedWriteBuffer::writeRegion(const SkRegion& region) { | |
| 126 fWriter.writeRegion(region); | |
| 127 } | |
| 128 | |
| 129 void SkOrderedWriteBuffer::writePath(const SkPath& path) { | |
| 130 fWriter.writePath(path); | |
| 131 } | |
| 132 | |
| 133 size_t SkOrderedWriteBuffer::writeStream(SkStream* stream, size_t length) { | |
| 134 fWriter.write32(length); | |
| 135 size_t bytesWritten = fWriter.readFromStream(stream, length); | |
| 136 if (bytesWritten < length) { | |
| 137 fWriter.reservePad(length - bytesWritten); | |
| 138 } | |
| 139 return bytesWritten; | |
| 140 } | |
| 141 | |
| 142 bool SkOrderedWriteBuffer::writeToStream(SkWStream* stream) { | |
| 143 return fWriter.writeToStream(stream); | |
| 144 } | |
| 145 | |
| 146 static void write_encoded_bitmap(SkOrderedWriteBuffer* buffer, SkData* data, | |
| 147 const SkIPoint& origin) { | |
| 148 buffer->writeUInt(SkToU32(data->size())); | |
| 149 buffer->getWriter32()->writePad(data->data(), data->size()); | |
| 150 buffer->write32(origin.fX); | |
| 151 buffer->write32(origin.fY); | |
| 152 } | |
| 153 | |
| 154 void SkOrderedWriteBuffer::writeBitmap(const SkBitmap& bitmap) { | |
| 155 // Record the width and height. This way if readBitmap fails a dummy bitmap
can be drawn at the | |
| 156 // right size. | |
| 157 this->writeInt(bitmap.width()); | |
| 158 this->writeInt(bitmap.height()); | |
| 159 | |
| 160 // Record information about the bitmap in one of three ways, in order of pri
ority: | |
| 161 // 1. If there is an SkBitmapHeap, store it in the heap. The client can avoi
d serializing the | |
| 162 // bitmap entirely or serialize it later as desired. A boolean value of t
rue will be written | |
| 163 // to the stream to signify that a heap was used. | |
| 164 // 2. If there is a function for encoding bitmaps, use it to write an encode
d version of the | |
| 165 // bitmap. After writing a boolean value of false, signifying that a heap
was not used, write | |
| 166 // the size of the encoded data. A non-zero size signifies that encoded d
ata was written. | |
| 167 // 3. Call SkBitmap::flatten. After writing a boolean value of false, signif
ying that a heap was | |
| 168 // not used, write a zero to signify that the data was not encoded. | |
| 169 bool useBitmapHeap = fBitmapHeap != NULL; | |
| 170 // Write a bool: true if the SkBitmapHeap is to be used, in which case the r
eader must use an | |
| 171 // SkBitmapHeapReader to read the SkBitmap. False if the bitmap was serializ
ed another way. | |
| 172 this->writeBool(useBitmapHeap); | |
| 173 if (useBitmapHeap) { | |
| 174 SkASSERT(NULL == fBitmapEncoder); | |
| 175 int32_t slot = fBitmapHeap->insert(bitmap); | |
| 176 fWriter.write32(slot); | |
| 177 // crbug.com/155875 | |
| 178 // The generation ID is not required information. We write it to prevent
collisions | |
| 179 // in SkFlatDictionary. It is possible to get a collision when a previo
usly | |
| 180 // unflattened (i.e. stale) instance of a similar flattenable is in the
dictionary | |
| 181 // and the instance currently being written is re-using the same slot fr
om the | |
| 182 // bitmap heap. | |
| 183 fWriter.write32(bitmap.getGenerationID()); | |
| 184 return; | |
| 185 } | |
| 186 | |
| 187 // see if the pixelref already has an encoded version | |
| 188 if (bitmap.pixelRef()) { | |
| 189 SkAutoDataUnref data(bitmap.pixelRef()->refEncodedData()); | |
| 190 if (data.get() != NULL) { | |
| 191 write_encoded_bitmap(this, data, bitmap.pixelRefOrigin()); | |
| 192 return; | |
| 193 } | |
| 194 } | |
| 195 | |
| 196 // see if the caller wants to manually encode | |
| 197 if (fBitmapEncoder != NULL) { | |
| 198 SkASSERT(NULL == fBitmapHeap); | |
| 199 size_t offset = 0; // this parameter is deprecated/ignored | |
| 200 // if we have to "encode" the bitmap, then we assume there is no | |
| 201 // offset to share, since we are effectively creating a new pixelref | |
| 202 SkAutoDataUnref data(fBitmapEncoder(&offset, bitmap)); | |
| 203 if (data.get() != NULL) { | |
| 204 write_encoded_bitmap(this, data, SkIPoint::Make(0, 0)); | |
| 205 return; | |
| 206 } | |
| 207 } | |
| 208 | |
| 209 // Bitmap was not encoded. Record a zero, implying that the reader need not
decode. | |
| 210 this->writeUInt(0); | |
| 211 bitmap.flatten(*this); | |
| 212 } | |
| 213 | |
| 214 void SkOrderedWriteBuffer::writeTypeface(SkTypeface* obj) { | |
| 215 if (NULL == obj || NULL == fTFSet) { | |
| 216 fWriter.write32(0); | |
| 217 } else { | |
| 218 fWriter.write32(fTFSet->add(obj)); | |
| 219 } | |
| 220 } | |
| 221 | |
| 222 SkFactorySet* SkOrderedWriteBuffer::setFactoryRecorder(SkFactorySet* rec) { | |
| 223 SkRefCnt_SafeAssign(fFactorySet, rec); | |
| 224 if (fNamedFactorySet != NULL) { | |
| 225 fNamedFactorySet->unref(); | |
| 226 fNamedFactorySet = NULL; | |
| 227 } | |
| 228 return rec; | |
| 229 } | |
| 230 | |
| 231 SkNamedFactorySet* SkOrderedWriteBuffer::setNamedFactoryRecorder(SkNamedFactoryS
et* rec) { | |
| 232 SkRefCnt_SafeAssign(fNamedFactorySet, rec); | |
| 233 if (fFactorySet != NULL) { | |
| 234 fFactorySet->unref(); | |
| 235 fFactorySet = NULL; | |
| 236 } | |
| 237 return rec; | |
| 238 } | |
| 239 | |
| 240 SkRefCntSet* SkOrderedWriteBuffer::setTypefaceRecorder(SkRefCntSet* rec) { | |
| 241 SkRefCnt_SafeAssign(fTFSet, rec); | |
| 242 return rec; | |
| 243 } | |
| 244 | |
| 245 void SkOrderedWriteBuffer::setBitmapHeap(SkBitmapHeap* bitmapHeap) { | |
| 246 SkRefCnt_SafeAssign(fBitmapHeap, bitmapHeap); | |
| 247 if (bitmapHeap != NULL) { | |
| 248 SkASSERT(NULL == fBitmapEncoder); | |
| 249 fBitmapEncoder = NULL; | |
| 250 } | |
| 251 } | |
| 252 | |
| 253 void SkOrderedWriteBuffer::setBitmapEncoder(SkPicture::EncodeBitmap bitmapEncode
r) { | |
| 254 fBitmapEncoder = bitmapEncoder; | |
| 255 if (bitmapEncoder != NULL) { | |
| 256 SkASSERT(NULL == fBitmapHeap); | |
| 257 SkSafeUnref(fBitmapHeap); | |
| 258 fBitmapHeap = NULL; | |
| 259 } | |
| 260 } | |
| 261 | |
| 262 void SkOrderedWriteBuffer::writeFlattenable(const SkFlattenable* flattenable) { | |
| 263 /* | |
| 264 * If we have a factoryset, then the first 32bits tell us... | |
| 265 * 0: failure to write the flattenable | |
| 266 * >0: (1-based) index into the SkFactorySet or SkNamedFactorySet | |
| 267 * If we don't have a factoryset, then the first "ptr" is either the | |
| 268 * factory, or null for failure. | |
| 269 * | |
| 270 * The distinction is important, since 0-index is 32bits (always), but a | |
| 271 * 0-functionptr might be 32 or 64 bits. | |
| 272 */ | |
| 273 | |
| 274 SkFlattenable::Factory factory = NULL; | |
| 275 if (flattenable) { | |
| 276 factory = flattenable->getFactory(); | |
| 277 } | |
| 278 if (NULL == factory) { | |
| 279 if (this->isValidating()) { | |
| 280 this->writeString(""); | |
| 281 SkASSERT(NULL == flattenable); // We shouldn't get in here in this s
cenario | |
| 282 } else if (fFactorySet != NULL || fNamedFactorySet != NULL) { | |
| 283 this->write32(0); | |
| 284 } else { | |
| 285 this->writeFunctionPtr(NULL); | |
| 286 } | |
| 287 return; | |
| 288 } | |
| 289 | |
| 290 /* | |
| 291 * We can write 1 of 3 versions of the flattenable: | |
| 292 * 1. function-ptr : this is the fastest for the reader, but assumes that | |
| 293 * the writer and reader are in the same process. | |
| 294 * 2. index into fFactorySet : This is assumes the writer will later | |
| 295 * resolve the function-ptrs into strings for its reader. SkPicture | |
| 296 * does exactly this, by writing a table of names (matching the indices
) | |
| 297 * up front in its serialized form. | |
| 298 * 3. index into fNamedFactorySet. fNamedFactorySet will also store the | |
| 299 * name. SkGPipe uses this technique so it can write the name to its | |
| 300 * stream before writing the flattenable. | |
| 301 */ | |
| 302 if (this->isValidating()) { | |
| 303 this->writeString(flattenable->getTypeName()); | |
| 304 } else if (fFactorySet) { | |
| 305 this->write32(fFactorySet->add(factory)); | |
| 306 } else if (fNamedFactorySet) { | |
| 307 int32_t index = fNamedFactorySet->find(factory); | |
| 308 this->write32(index); | |
| 309 if (0 == index) { | |
| 310 return; | |
| 311 } | |
| 312 } else { | |
| 313 this->writeFunctionPtr((void*)factory); | |
| 314 } | |
| 315 | |
| 316 // make room for the size of the flattened object | |
| 317 (void)fWriter.reserve(sizeof(uint32_t)); | |
| 318 // record the current size, so we can subtract after the object writes. | |
| 319 uint32_t offset = fWriter.bytesWritten(); | |
| 320 // now flatten the object | |
| 321 flattenObject(flattenable, *this); | |
| 322 uint32_t objSize = fWriter.bytesWritten() - offset; | |
| 323 // record the obj's size | |
| 324 *fWriter.peek32(offset - sizeof(uint32_t)) = objSize; | |
| 325 } | |
| OLD | NEW |