| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkWriteBuffer.h" | 8 #include "SkWriteBuffer.h" |
| 9 #include "SkBitmap.h" | 9 #include "SkBitmap.h" |
| 10 #include "SkBitmapHeap.h" | 10 #include "SkBitmapHeap.h" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 , fBitmapHeap(nullptr) | 28 , fBitmapHeap(nullptr) |
| 29 , fTFSet(nullptr) { | 29 , fTFSet(nullptr) { |
| 30 } | 30 } |
| 31 | 31 |
| 32 SkWriteBuffer::~SkWriteBuffer() { | 32 SkWriteBuffer::~SkWriteBuffer() { |
| 33 SkSafeUnref(fFactorySet); | 33 SkSafeUnref(fFactorySet); |
| 34 SkSafeUnref(fBitmapHeap); | 34 SkSafeUnref(fBitmapHeap); |
| 35 SkSafeUnref(fTFSet); | 35 SkSafeUnref(fTFSet); |
| 36 } | 36 } |
| 37 | 37 |
| 38 void SkWriteBuffer::writeByteArray(const void* data, size_t size) { | 38 void SkWriteBuffer::writeByteArray(const char* name, const void* data, size_t si
ze) { |
| 39 fWriter.write32(SkToU32(size)); | 39 fWriter.write32(SkToU32(size)); |
| 40 fWriter.writePad(data, size); | 40 fWriter.writePad(data, size); |
| 41 } | 41 } |
| 42 | 42 |
| 43 void SkWriteBuffer::writeBool(bool value) { | 43 void SkWriteBuffer::writeBool(const char* name, bool value) { |
| 44 fWriter.writeBool(value); | 44 fWriter.writeBool(value); |
| 45 } | 45 } |
| 46 | 46 |
| 47 void SkWriteBuffer::writeScalar(SkScalar value) { | 47 void SkWriteBuffer::writeScalar(const char* name, SkScalar value) { |
| 48 fWriter.writeScalar(value); | 48 fWriter.writeScalar(value); |
| 49 } | 49 } |
| 50 | 50 |
| 51 void SkWriteBuffer::writeScalarArray(const SkScalar* value, uint32_t count) { | 51 void SkWriteBuffer::writeScalarArray(const char* name, const SkScalar* value, ui
nt32_t count) { |
| 52 fWriter.write32(count); | 52 fWriter.write32(count); |
| 53 fWriter.write(value, count * sizeof(SkScalar)); | 53 fWriter.write(value, count * sizeof(SkScalar)); |
| 54 } | 54 } |
| 55 | 55 |
| 56 void SkWriteBuffer::writeInt(int32_t value) { | 56 void SkWriteBuffer::writeInt(const char* name, int32_t value) { |
| 57 fWriter.write32(value); | 57 fWriter.write32(value); |
| 58 } | 58 } |
| 59 | 59 |
| 60 void SkWriteBuffer::writeIntArray(const int32_t* value, uint32_t count) { | 60 void SkWriteBuffer::writeIntArray(const char* name, const int32_t* value, uint32
_t count) { |
| 61 fWriter.write32(count); | 61 fWriter.write32(count); |
| 62 fWriter.write(value, count * sizeof(int32_t)); | 62 fWriter.write(value, count * sizeof(int32_t)); |
| 63 } | 63 } |
| 64 | 64 |
| 65 void SkWriteBuffer::writeUInt(uint32_t value) { | 65 void SkWriteBuffer::writeUInt(const char* name, uint32_t value) { |
| 66 fWriter.write32(value); | 66 fWriter.write32(value); |
| 67 } | 67 } |
| 68 | 68 |
| 69 void SkWriteBuffer::write32(int32_t value) { | 69 void SkWriteBuffer::write32(const char* name, int32_t value) { |
| 70 fWriter.write32(value); | 70 fWriter.write32(value); |
| 71 } | 71 } |
| 72 | 72 |
| 73 void SkWriteBuffer::writeString(const char* value) { | 73 void SkWriteBuffer::writeString(const char* name, const char* value) { |
| 74 fWriter.writeString(value); | 74 fWriter.writeString(value); |
| 75 } | 75 } |
| 76 | 76 |
| 77 void SkWriteBuffer::writeEncodedString(const void* value, size_t byteLength, | 77 void SkWriteBuffer::writeEncodedString(const char* name, const void* value, size
_t byteLength, |
| 78 SkPaint::TextEncoding encoding) { | 78 SkPaint::TextEncoding encoding) { |
| 79 fWriter.writeInt(encoding); | 79 fWriter.writeInt(encoding); |
| 80 fWriter.writeInt(SkToU32(byteLength)); | 80 fWriter.writeInt(SkToU32(byteLength)); |
| 81 fWriter.write(value, byteLength); | 81 fWriter.write(value, byteLength); |
| 82 } | 82 } |
| 83 | 83 |
| 84 | 84 |
| 85 void SkWriteBuffer::writeColor(const SkColor& color) { | 85 void SkWriteBuffer::writeColor(const char* name, const SkColor& color) { |
| 86 fWriter.write32(color); | 86 fWriter.write32(color); |
| 87 } | 87 } |
| 88 | 88 |
| 89 void SkWriteBuffer::writeColorArray(const SkColor* color, uint32_t count) { | 89 void SkWriteBuffer::writeColorArray(const char* name, const SkColor* color, uint
32_t count) { |
| 90 fWriter.write32(count); | 90 fWriter.write32(count); |
| 91 fWriter.write(color, count * sizeof(SkColor)); | 91 fWriter.write(color, count * sizeof(SkColor)); |
| 92 } | 92 } |
| 93 | 93 |
| 94 void SkWriteBuffer::writePoint(const SkPoint& point) { | 94 void SkWriteBuffer::writePoint(const char* name, const SkPoint& point) { |
| 95 fWriter.writeScalar(point.fX); | 95 fWriter.writeScalar(point.fX); |
| 96 fWriter.writeScalar(point.fY); | 96 fWriter.writeScalar(point.fY); |
| 97 } | 97 } |
| 98 | 98 |
| 99 void SkWriteBuffer::writePointArray(const SkPoint* point, uint32_t count) { | 99 void SkWriteBuffer::writePointArray(const char* name, const SkPoint* point, uint
32_t count) { |
| 100 fWriter.write32(count); | 100 fWriter.write32(count); |
| 101 fWriter.write(point, count * sizeof(SkPoint)); | 101 fWriter.write(point, count * sizeof(SkPoint)); |
| 102 } | 102 } |
| 103 | 103 |
| 104 void SkWriteBuffer::writeMatrix(const SkMatrix& matrix) { | 104 void SkWriteBuffer::writeMatrix(const char* name, const SkMatrix& matrix) { |
| 105 fWriter.writeMatrix(matrix); | 105 fWriter.writeMatrix(matrix); |
| 106 } | 106 } |
| 107 | 107 |
| 108 void SkWriteBuffer::writeIRect(const SkIRect& rect) { | 108 void SkWriteBuffer::writeIRect(const char* name, const SkIRect& rect) { |
| 109 fWriter.write(&rect, sizeof(SkIRect)); | 109 fWriter.write(&rect, sizeof(SkIRect)); |
| 110 } | 110 } |
| 111 | 111 |
| 112 void SkWriteBuffer::writeRect(const SkRect& rect) { | 112 void SkWriteBuffer::writeRect(const char* name, const SkRect& rect) { |
| 113 fWriter.writeRect(rect); | 113 fWriter.writeRect(rect); |
| 114 } | 114 } |
| 115 | 115 |
| 116 void SkWriteBuffer::writeRegion(const SkRegion& region) { | 116 void SkWriteBuffer::writeRegion(const char* name, const SkRegion& region) { |
| 117 fWriter.writeRegion(region); | 117 fWriter.writeRegion(region); |
| 118 } | 118 } |
| 119 | 119 |
| 120 void SkWriteBuffer::writePath(const SkPath& path) { | 120 void SkWriteBuffer::writePath(const char* name, const SkPath& path) { |
| 121 fWriter.writePath(path); | 121 fWriter.writePath(path); |
| 122 } | 122 } |
| 123 | 123 |
| 124 size_t SkWriteBuffer::writeStream(SkStream* stream, size_t length) { | 124 size_t SkWriteBuffer::writeStream(const char* name, SkStream* stream, size_t len
gth) { |
| 125 fWriter.write32(SkToU32(length)); | 125 fWriter.write32(SkToU32(length)); |
| 126 size_t bytesWritten = fWriter.readFromStream(stream, length); | 126 size_t bytesWritten = fWriter.readFromStream(stream, length); |
| 127 if (bytesWritten < length) { | 127 if (bytesWritten < length) { |
| 128 fWriter.reservePad(length - bytesWritten); | 128 fWriter.reservePad(length - bytesWritten); |
| 129 } | 129 } |
| 130 return bytesWritten; | 130 return bytesWritten; |
| 131 } | 131 } |
| 132 | 132 |
| 133 bool SkWriteBuffer::writeToStream(SkWStream* stream) { | 133 bool SkWriteBuffer::writeToStream(SkWStream* stream) { |
| 134 return fWriter.writeToStream(stream); | 134 return fWriter.writeToStream(stream); |
| 135 } | 135 } |
| 136 | 136 |
| 137 static void write_encoded_bitmap(SkWriteBuffer* buffer, SkData* data, | 137 static void write_encoded_bitmap(SkWriteBuffer* buffer, SkData* data, |
| 138 const SkIPoint& origin) { | 138 const SkIPoint& origin) { |
| 139 buffer->writeUInt(SkToU32(data->size())); | 139 buffer->writeUInt("size", SkToU32(data->size())); |
| 140 buffer->getWriter32()->writePad(data->data(), data->size()); | 140 buffer->getWriter32()->writePad(data->data(), data->size()); |
| 141 buffer->write32(origin.fX); | 141 buffer->write32("x", origin.fX); |
| 142 buffer->write32(origin.fY); | 142 buffer->write32("y", origin.fY); |
| 143 } | 143 } |
| 144 | 144 |
| 145 void SkWriteBuffer::writeBitmap(const SkBitmap& bitmap) { | 145 void SkWriteBuffer::writeBitmap(const char* name, const SkBitmap& bitmap) { |
| 146 // Record the width and height. This way if readBitmap fails a dummy bitmap
can be drawn at the | 146 // Record the width and height. This way if readBitmap fails a dummy bitmap
can be drawn at the |
| 147 // right size. | 147 // right size. |
| 148 this->writeInt(bitmap.width()); | 148 this->writeInt("width", bitmap.width()); |
| 149 this->writeInt(bitmap.height()); | 149 this->writeInt("height", bitmap.height()); |
| 150 | 150 |
| 151 // Record information about the bitmap in one of three ways, in order of pri
ority: | 151 // Record information about the bitmap in one of three ways, in order of pri
ority: |
| 152 // 1. If there is an SkBitmapHeap, store it in the heap. The client can avoi
d serializing the | 152 // 1. If there is an SkBitmapHeap, store it in the heap. The client can avoi
d serializing the |
| 153 // bitmap entirely or serialize it later as desired. A boolean value of t
rue will be written | 153 // bitmap entirely or serialize it later as desired. A boolean value of t
rue will be written |
| 154 // to the stream to signify that a heap was used. | 154 // to the stream to signify that a heap was used. |
| 155 // 2. If there is a function for encoding bitmaps, use it to write an encode
d version of the | 155 // 2. If there is a function for encoding bitmaps, use it to write an encode
d version of the |
| 156 // bitmap. After writing a boolean value of false, signifying that a heap
was not used, write | 156 // bitmap. After writing a boolean value of false, signifying that a heap
was not used, write |
| 157 // the size of the encoded data. A non-zero size signifies that encoded d
ata was written. | 157 // the size of the encoded data. A non-zero size signifies that encoded d
ata was written. |
| 158 // 3. Call SkBitmap::flatten. After writing a boolean value of false, signif
ying that a heap was | 158 // 3. Call SkBitmap::flatten. After writing a boolean value of false, signif
ying that a heap was |
| 159 // not used, write a zero to signify that the data was not encoded. | 159 // not used, write a zero to signify that the data was not encoded. |
| 160 bool useBitmapHeap = fBitmapHeap != nullptr; | 160 bool useBitmapHeap = fBitmapHeap != nullptr; |
| 161 // Write a bool: true if the SkBitmapHeap is to be used, in which case the r
eader must use an | 161 // Write a bool: true if the SkBitmapHeap is to be used, in which case the r
eader must use an |
| 162 // SkBitmapHeapReader to read the SkBitmap. False if the bitmap was serializ
ed another way. | 162 // SkBitmapHeapReader to read the SkBitmap. False if the bitmap was serializ
ed another way. |
| 163 this->writeBool(useBitmapHeap); | 163 this->writeBool("useBitmapHeap", useBitmapHeap); |
| 164 if (useBitmapHeap) { | 164 if (useBitmapHeap) { |
| 165 SkASSERT(nullptr == fPixelSerializer); | 165 SkASSERT(nullptr == fPixelSerializer); |
| 166 int32_t slot = fBitmapHeap->insert(bitmap); | 166 int32_t slot = fBitmapHeap->insert(bitmap); |
| 167 fWriter.write32(slot); | 167 fWriter.write32(slot); |
| 168 // crbug.com/155875 | 168 // crbug.com/155875 |
| 169 // The generation ID is not required information. We write it to prevent
collisions | 169 // The generation ID is not required information. We write it to prevent
collisions |
| 170 // in SkFlatDictionary. It is possible to get a collision when a previo
usly | 170 // in SkFlatDictionary. It is possible to get a collision when a previo
usly |
| 171 // unflattened (i.e. stale) instance of a similar flattenable is in the
dictionary | 171 // unflattened (i.e. stale) instance of a similar flattenable is in the
dictionary |
| 172 // and the instance currently being written is re-using the same slot fr
om the | 172 // and the instance currently being written is re-using the same slot fr
om the |
| 173 // bitmap heap. | 173 // bitmap heap. |
| (...skipping 22 matching lines...) Expand all Loading... |
| 196 SkAutoDataUnref data(fPixelSerializer->encode(result.pixmap())); | 196 SkAutoDataUnref data(fPixelSerializer->encode(result.pixmap())); |
| 197 if (data.get() != nullptr) { | 197 if (data.get() != nullptr) { |
| 198 // if we have to "encode" the bitmap, then we assume there is no | 198 // if we have to "encode" the bitmap, then we assume there is no |
| 199 // offset to share, since we are effectively creating a new pixe
lref | 199 // offset to share, since we are effectively creating a new pixe
lref |
| 200 write_encoded_bitmap(this, data, SkIPoint::Make(0, 0)); | 200 write_encoded_bitmap(this, data, SkIPoint::Make(0, 0)); |
| 201 return; | 201 return; |
| 202 } | 202 } |
| 203 } | 203 } |
| 204 } | 204 } |
| 205 | 205 |
| 206 this->writeUInt(0); // signal raw pixels | 206 this->writeUInt("rawPixelsTag", 0); // signal raw pixels |
| 207 SkBitmap::WriteRawPixels(this, bitmap); | 207 SkBitmap::WriteRawPixels(this, bitmap); |
| 208 } | 208 } |
| 209 | 209 |
| 210 void SkWriteBuffer::writeImage(const SkImage* image) { | 210 void SkWriteBuffer::writeImage(const char* name, const SkImage* image) { |
| 211 this->writeInt(image->width()); | 211 this->writeInt("width", image->width()); |
| 212 this->writeInt(image->height()); | 212 this->writeInt("height", image->height()); |
| 213 | 213 |
| 214 SkAutoTUnref<SkData> encoded(image->encode(this->getPixelSerializer())); | 214 SkAutoTUnref<SkData> encoded(image->encode(this->getPixelSerializer())); |
| 215 if (encoded && encoded->size() > 0) { | 215 if (encoded && encoded->size() > 0) { |
| 216 write_encoded_bitmap(this, encoded, SkIPoint::Make(0, 0)); | 216 write_encoded_bitmap(this, encoded, SkIPoint::Make(0, 0)); |
| 217 return; | 217 return; |
| 218 } | 218 } |
| 219 | 219 |
| 220 this->writeUInt(0); // signal no pixels (in place of the size of the encoded
data) | 220 this->writeUInt("noPixelsTag", 0); // signal no pixels (in place of the size
of the encoded data) |
| 221 } | 221 } |
| 222 | 222 |
| 223 void SkWriteBuffer::writeTypeface(SkTypeface* obj) { | 223 void SkWriteBuffer::writeTypeface(const char* name, SkTypeface* obj) { |
| 224 if (nullptr == obj || nullptr == fTFSet) { | 224 if (nullptr == obj || nullptr == fTFSet) { |
| 225 fWriter.write32(0); | 225 fWriter.write32(0); |
| 226 } else { | 226 } else { |
| 227 fWriter.write32(fTFSet->add(obj)); | 227 fWriter.write32(fTFSet->add(obj)); |
| 228 } | 228 } |
| 229 } | 229 } |
| 230 | 230 |
| 231 SkFactorySet* SkWriteBuffer::setFactoryRecorder(SkFactorySet* rec) { | 231 SkFactorySet* SkWriteBuffer::setFactoryRecorder(SkFactorySet* rec) { |
| 232 SkRefCnt_SafeAssign(fFactorySet, rec); | 232 SkRefCnt_SafeAssign(fFactorySet, rec); |
| 233 return rec; | 233 return rec; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 249 void SkWriteBuffer::setPixelSerializer(SkPixelSerializer* serializer) { | 249 void SkWriteBuffer::setPixelSerializer(SkPixelSerializer* serializer) { |
| 250 fPixelSerializer.reset(serializer); | 250 fPixelSerializer.reset(serializer); |
| 251 if (serializer) { | 251 if (serializer) { |
| 252 serializer->ref(); | 252 serializer->ref(); |
| 253 SkASSERT(nullptr == fBitmapHeap); | 253 SkASSERT(nullptr == fBitmapHeap); |
| 254 SkSafeUnref(fBitmapHeap); | 254 SkSafeUnref(fBitmapHeap); |
| 255 fBitmapHeap = nullptr; | 255 fBitmapHeap = nullptr; |
| 256 } | 256 } |
| 257 } | 257 } |
| 258 | 258 |
| 259 void SkWriteBuffer::writeFlattenable(const SkFlattenable* flattenable) { | 259 void SkWriteBuffer::writeFlattenable(const char* name, const SkFlattenable* flat
tenable) { |
| 260 /* | 260 /* |
| 261 * The first 32 bits tell us... | 261 * The first 32 bits tell us... |
| 262 * 0: failure to write the flattenable | 262 * 0: failure to write the flattenable |
| 263 * >0: index (1-based) into fFactorySet or fFlattenableDict or | 263 * >0: index (1-based) into fFactorySet or fFlattenableDict or |
| 264 * the first character of a string | 264 * the first character of a string |
| 265 */ | 265 */ |
| 266 if (nullptr == flattenable) { | 266 if (nullptr == flattenable) { |
| 267 this->write32(0); | 267 this->write32("index", 0); |
| 268 return; | 268 return; |
| 269 } | 269 } |
| 270 | 270 |
| 271 /* | 271 /* |
| 272 * We can write 1 of 2 versions of the flattenable: | 272 * We can write 1 of 2 versions of the flattenable: |
| 273 * 1. index into fFactorySet : This assumes the writer will later | 273 * 1. index into fFactorySet : This assumes the writer will later |
| 274 * resolve the function-ptrs into strings for its reader. SkPicture | 274 * resolve the function-ptrs into strings for its reader. SkPicture |
| 275 * does exactly this, by writing a table of names (matching the indices
) | 275 * does exactly this, by writing a table of names (matching the indices
) |
| 276 * up front in its serialized form. | 276 * up front in its serialized form. |
| 277 * 2. string name of the flattenable or index into fFlattenableDict: We | 277 * 2. string name of the flattenable or index into fFlattenableDict: We |
| 278 * store the string to allow the reader to specify its own factories | 278 * store the string to allow the reader to specify its own factories |
| 279 * after write time. In order to improve compression, if we have | 279 * after write time. In order to improve compression, if we have |
| 280 * already written the string, we write its index instead. | 280 * already written the string, we write its index instead. |
| 281 */ | 281 */ |
| 282 if (fFactorySet) { | 282 if (fFactorySet) { |
| 283 SkFlattenable::Factory factory = flattenable->getFactory(); | 283 SkFlattenable::Factory factory = flattenable->getFactory(); |
| 284 SkASSERT(factory); | 284 SkASSERT(factory); |
| 285 this->write32(fFactorySet->add(factory)); | 285 this->write32("factoryIndex", fFactorySet->add(factory)); |
| 286 } else { | 286 } else { |
| 287 const char* name = flattenable->getTypeName(); | 287 const char* name = flattenable->getTypeName(); |
| 288 SkASSERT(name); | 288 SkASSERT(name); |
| 289 SkString key(name); | 289 SkString key(name); |
| 290 if (uint32_t* indexPtr = fFlattenableDict.find(key)) { | 290 if (uint32_t* indexPtr = fFlattenableDict.find(key)) { |
| 291 // We will write the index as a 32-bit int. We want the first byte | 291 // We will write the index as a 32-bit int. We want the first byte |
| 292 // that we send to be zero - this will act as a sentinel that we | 292 // that we send to be zero - this will act as a sentinel that we |
| 293 // have an index (not a string). This means that we will send the | 293 // have an index (not a string). This means that we will send the |
| 294 // the index shifted left by 8. The remaining 24-bits should be | 294 // the index shifted left by 8. The remaining 24-bits should be |
| 295 // plenty to store the index. Note that this strategy depends on | 295 // plenty to store the index. Note that this strategy depends on |
| 296 // being little endian. | 296 // being little endian. |
| 297 SkASSERT(0 == *indexPtr >> 24); | 297 SkASSERT(0 == *indexPtr >> 24); |
| 298 this->write32(*indexPtr << 8); | 298 this->write32("dictIndex", *indexPtr << 8); |
| 299 } else { | 299 } else { |
| 300 // Otherwise write the string. Clients should not use the empty | 300 // Otherwise write the string. Clients should not use the empty |
| 301 // string as a name, or we will have a problem. | 301 // string as a name, or we will have a problem. |
| 302 SkASSERT(strcmp("", name)); | 302 SkASSERT(strcmp("", name)); |
| 303 this->writeString(name); | 303 this->writeString("typeName", name); |
| 304 | 304 |
| 305 // Add key to dictionary. | 305 // Add key to dictionary. |
| 306 fFlattenableDict.set(key, fFlattenableDict.count() + 1); | 306 fFlattenableDict.set(key, fFlattenableDict.count() + 1); |
| 307 } | 307 } |
| 308 } | 308 } |
| 309 | 309 |
| 310 // make room for the size of the flattened object | 310 // make room for the size of the flattened object |
| 311 (void)fWriter.reserve(sizeof(uint32_t)); | 311 (void)fWriter.reserve(sizeof(uint32_t)); |
| 312 // record the current size, so we can subtract after the object writes. | 312 // record the current size, so we can subtract after the object writes. |
| 313 size_t offset = fWriter.bytesWritten(); | 313 size_t offset = fWriter.bytesWritten(); |
| 314 // now flatten the object | 314 // now flatten the object |
| 315 flattenable->flatten(*this); | 315 flattenable->flatten(*this); |
| 316 size_t objSize = fWriter.bytesWritten() - offset; | 316 size_t objSize = fWriter.bytesWritten() - offset; |
| 317 // record the obj's size | 317 // record the obj's size |
| 318 fWriter.overwriteTAt(offset - sizeof(uint32_t), SkToU32(objSize)); | 318 fWriter.overwriteTAt(offset - sizeof(uint32_t), SkToU32(objSize)); |
| 319 } | 319 } |
| OLD | NEW |