| 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" |
| 11 #include "SkData.h" | 11 #include "SkData.h" |
| 12 #include "SkPixelRef.h" | 12 #include "SkPixelRef.h" |
| 13 #include "SkPtrRecorder.h" | 13 #include "SkPtrRecorder.h" |
| 14 #include "SkStream.h" | 14 #include "SkStream.h" |
| 15 #include "SkTypeface.h" | 15 #include "SkTypeface.h" |
| 16 | 16 |
| 17 SkWriteBuffer::SkWriteBuffer(uint32_t flags) | 17 SkBinaryWriteBuffer::SkBinaryWriteBuffer(uint32_t flags) |
| 18 : fFlags(flags) | 18 : fFlags(flags) |
| 19 , fFactorySet(nullptr) | 19 , fFactorySet(nullptr) |
| 20 , fBitmapHeap(nullptr) | 20 , fBitmapHeap(nullptr) |
| 21 , fTFSet(nullptr) { | 21 , fTFSet(nullptr) { |
| 22 } | 22 } |
| 23 | 23 |
| 24 SkWriteBuffer::SkWriteBuffer(void* storage, size_t storageSize, uint32_t flags) | 24 SkBinaryWriteBuffer::SkBinaryWriteBuffer(void* storage, size_t storageSize, uint
32_t flags) |
| 25 : fFlags(flags) | 25 : fFlags(flags) |
| 26 , fFactorySet(nullptr) | 26 , fFactorySet(nullptr) |
| 27 , fWriter(storage, storageSize) | 27 , fWriter(storage, storageSize) |
| 28 , fBitmapHeap(nullptr) | 28 , fBitmapHeap(nullptr) |
| 29 , fTFSet(nullptr) { | 29 , fTFSet(nullptr) { |
| 30 } | 30 } |
| 31 | 31 |
| 32 SkWriteBuffer::~SkWriteBuffer() { | 32 SkBinaryWriteBuffer::~SkBinaryWriteBuffer() { |
| 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 SkBinaryWriteBuffer::writeByteArray(const void* data, size_t size) { |
| 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 SkBinaryWriteBuffer::writeDataAsByteArray(SkData* data) { |
| 44 this->writeByteArray(data->data(), data->size()); |
| 45 } |
| 46 |
| 47 void SkBinaryWriteBuffer::writeBool(bool value) { |
| 44 fWriter.writeBool(value); | 48 fWriter.writeBool(value); |
| 45 } | 49 } |
| 46 | 50 |
| 47 void SkWriteBuffer::writeScalar(SkScalar value) { | 51 void SkBinaryWriteBuffer::writeScalar(SkScalar value) { |
| 48 fWriter.writeScalar(value); | 52 fWriter.writeScalar(value); |
| 49 } | 53 } |
| 50 | 54 |
| 51 void SkWriteBuffer::writeScalarArray(const SkScalar* value, uint32_t count) { | 55 void SkBinaryWriteBuffer::writeScalarArray(const SkScalar* value, uint32_t count
) { |
| 52 fWriter.write32(count); | 56 fWriter.write32(count); |
| 53 fWriter.write(value, count * sizeof(SkScalar)); | 57 fWriter.write(value, count * sizeof(SkScalar)); |
| 54 } | 58 } |
| 55 | 59 |
| 56 void SkWriteBuffer::writeInt(int32_t value) { | 60 void SkBinaryWriteBuffer::writeInt(int32_t value) { |
| 57 fWriter.write32(value); | 61 fWriter.write32(value); |
| 58 } | 62 } |
| 59 | 63 |
| 60 void SkWriteBuffer::writeIntArray(const int32_t* value, uint32_t count) { | 64 void SkBinaryWriteBuffer::writeIntArray(const int32_t* value, uint32_t count) { |
| 61 fWriter.write32(count); | 65 fWriter.write32(count); |
| 62 fWriter.write(value, count * sizeof(int32_t)); | 66 fWriter.write(value, count * sizeof(int32_t)); |
| 63 } | 67 } |
| 64 | 68 |
| 65 void SkWriteBuffer::writeUInt(uint32_t value) { | 69 void SkBinaryWriteBuffer::writeUInt(uint32_t value) { |
| 66 fWriter.write32(value); | 70 fWriter.write32(value); |
| 67 } | 71 } |
| 68 | 72 |
| 69 void SkWriteBuffer::write32(int32_t value) { | 73 void SkBinaryWriteBuffer::write32(int32_t value) { |
| 70 fWriter.write32(value); | 74 fWriter.write32(value); |
| 71 } | 75 } |
| 72 | 76 |
| 73 void SkWriteBuffer::writeString(const char* value) { | 77 void SkBinaryWriteBuffer::writeString(const char* value) { |
| 74 fWriter.writeString(value); | 78 fWriter.writeString(value); |
| 75 } | 79 } |
| 76 | 80 |
| 77 void SkWriteBuffer::writeEncodedString(const void* value, size_t byteLength, | 81 void SkBinaryWriteBuffer::writeEncodedString(const void* value, size_t byteLengt
h, |
| 78 SkPaint::TextEncoding encoding) { | 82 SkPaint::TextEncoding encoding) { |
| 79 fWriter.writeInt(encoding); | 83 fWriter.writeInt(encoding); |
| 80 fWriter.writeInt(SkToU32(byteLength)); | 84 fWriter.writeInt(SkToU32(byteLength)); |
| 81 fWriter.write(value, byteLength); | 85 fWriter.write(value, byteLength); |
| 82 } | 86 } |
| 83 | 87 |
| 88 void SkBinaryWriteBuffer::writeFunctionPtr(void* ptr) { |
| 89 fWriter.writePtr(ptr); |
| 90 } |
| 84 | 91 |
| 85 void SkWriteBuffer::writeColor(const SkColor& color) { | 92 void SkBinaryWriteBuffer::writeColor(const SkColor& color) { |
| 86 fWriter.write32(color); | 93 fWriter.write32(color); |
| 87 } | 94 } |
| 88 | 95 |
| 89 void SkWriteBuffer::writeColorArray(const SkColor* color, uint32_t count) { | 96 void SkBinaryWriteBuffer::writeColorArray(const SkColor* color, uint32_t count)
{ |
| 90 fWriter.write32(count); | 97 fWriter.write32(count); |
| 91 fWriter.write(color, count * sizeof(SkColor)); | 98 fWriter.write(color, count * sizeof(SkColor)); |
| 92 } | 99 } |
| 93 | 100 |
| 94 void SkWriteBuffer::writePoint(const SkPoint& point) { | 101 void SkBinaryWriteBuffer::writePoint(const SkPoint& point) { |
| 95 fWriter.writeScalar(point.fX); | 102 fWriter.writeScalar(point.fX); |
| 96 fWriter.writeScalar(point.fY); | 103 fWriter.writeScalar(point.fY); |
| 97 } | 104 } |
| 98 | 105 |
| 99 void SkWriteBuffer::writePointArray(const SkPoint* point, uint32_t count) { | 106 void SkBinaryWriteBuffer::writePointArray(const SkPoint* point, uint32_t count)
{ |
| 100 fWriter.write32(count); | 107 fWriter.write32(count); |
| 101 fWriter.write(point, count * sizeof(SkPoint)); | 108 fWriter.write(point, count * sizeof(SkPoint)); |
| 102 } | 109 } |
| 103 | 110 |
| 104 void SkWriteBuffer::writeMatrix(const SkMatrix& matrix) { | 111 void SkBinaryWriteBuffer::writeMatrix(const SkMatrix& matrix) { |
| 105 fWriter.writeMatrix(matrix); | 112 fWriter.writeMatrix(matrix); |
| 106 } | 113 } |
| 107 | 114 |
| 108 void SkWriteBuffer::writeIRect(const SkIRect& rect) { | 115 void SkBinaryWriteBuffer::writeIRect(const SkIRect& rect) { |
| 109 fWriter.write(&rect, sizeof(SkIRect)); | 116 fWriter.write(&rect, sizeof(SkIRect)); |
| 110 } | 117 } |
| 111 | 118 |
| 112 void SkWriteBuffer::writeRect(const SkRect& rect) { | 119 void SkBinaryWriteBuffer::writeRect(const SkRect& rect) { |
| 113 fWriter.writeRect(rect); | 120 fWriter.writeRect(rect); |
| 114 } | 121 } |
| 115 | 122 |
| 116 void SkWriteBuffer::writeRegion(const SkRegion& region) { | 123 void SkBinaryWriteBuffer::writeRegion(const SkRegion& region) { |
| 117 fWriter.writeRegion(region); | 124 fWriter.writeRegion(region); |
| 118 } | 125 } |
| 119 | 126 |
| 120 void SkWriteBuffer::writePath(const SkPath& path) { | 127 void SkBinaryWriteBuffer::writePath(const SkPath& path) { |
| 121 fWriter.writePath(path); | 128 fWriter.writePath(path); |
| 122 } | 129 } |
| 123 | 130 |
| 124 size_t SkWriteBuffer::writeStream(SkStream* stream, size_t length) { | 131 size_t SkBinaryWriteBuffer::writeStream(SkStream* stream, size_t length) { |
| 125 fWriter.write32(SkToU32(length)); | 132 fWriter.write32(SkToU32(length)); |
| 126 size_t bytesWritten = fWriter.readFromStream(stream, length); | 133 size_t bytesWritten = fWriter.readFromStream(stream, length); |
| 127 if (bytesWritten < length) { | 134 if (bytesWritten < length) { |
| 128 fWriter.reservePad(length - bytesWritten); | 135 fWriter.reservePad(length - bytesWritten); |
| 129 } | 136 } |
| 130 return bytesWritten; | 137 return bytesWritten; |
| 131 } | 138 } |
| 132 | 139 |
| 133 bool SkWriteBuffer::writeToStream(SkWStream* stream) { | 140 bool SkBinaryWriteBuffer::writeToStream(SkWStream* stream) { |
| 134 return fWriter.writeToStream(stream); | 141 return fWriter.writeToStream(stream); |
| 135 } | 142 } |
| 136 | 143 |
| 137 static void write_encoded_bitmap(SkWriteBuffer* buffer, SkData* data, | 144 static void write_encoded_bitmap(SkBinaryWriteBuffer* buffer, SkData* data, |
| 138 const SkIPoint& origin) { | 145 const SkIPoint& origin) { |
| 139 buffer->writeUInt(SkToU32(data->size())); | 146 buffer->writeUInt(SkToU32(data->size())); |
| 140 buffer->getWriter32()->writePad(data->data(), data->size()); | 147 buffer->getWriter32()->writePad(data->data(), data->size()); |
| 141 buffer->write32(origin.fX); | 148 buffer->write32(origin.fX); |
| 142 buffer->write32(origin.fY); | 149 buffer->write32(origin.fY); |
| 143 } | 150 } |
| 144 | 151 |
| 145 void SkWriteBuffer::writeBitmap(const SkBitmap& bitmap) { | 152 void SkBinaryWriteBuffer::writeBitmap(const SkBitmap& bitmap) { |
| 146 // Record the width and height. This way if readBitmap fails a dummy bitmap
can be drawn at the | 153 // Record the width and height. This way if readBitmap fails a dummy bitmap
can be drawn at the |
| 147 // right size. | 154 // right size. |
| 148 this->writeInt(bitmap.width()); | 155 this->writeInt(bitmap.width()); |
| 149 this->writeInt(bitmap.height()); | 156 this->writeInt(bitmap.height()); |
| 150 | 157 |
| 151 // Record information about the bitmap in one of three ways, in order of pri
ority: | 158 // 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 | 159 // 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 | 160 // 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. | 161 // 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 | 162 // 2. If there is a function for encoding bitmaps, use it to write an encode
d version of the |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 write_encoded_bitmap(this, data, SkIPoint::Make(0, 0)); | 207 write_encoded_bitmap(this, data, SkIPoint::Make(0, 0)); |
| 201 return; | 208 return; |
| 202 } | 209 } |
| 203 } | 210 } |
| 204 } | 211 } |
| 205 | 212 |
| 206 this->writeUInt(0); // signal raw pixels | 213 this->writeUInt(0); // signal raw pixels |
| 207 SkBitmap::WriteRawPixels(this, bitmap); | 214 SkBitmap::WriteRawPixels(this, bitmap); |
| 208 } | 215 } |
| 209 | 216 |
| 210 void SkWriteBuffer::writeImage(const SkImage* image) { | 217 void SkBinaryWriteBuffer::writeImage(const SkImage* image) { |
| 211 this->writeInt(image->width()); | 218 this->writeInt(image->width()); |
| 212 this->writeInt(image->height()); | 219 this->writeInt(image->height()); |
| 213 | 220 |
| 214 SkAutoTUnref<SkData> encoded(image->encode(this->getPixelSerializer())); | 221 SkAutoTUnref<SkData> encoded(image->encode(this->getPixelSerializer())); |
| 215 if (encoded && encoded->size() > 0) { | 222 if (encoded && encoded->size() > 0) { |
| 216 write_encoded_bitmap(this, encoded, SkIPoint::Make(0, 0)); | 223 write_encoded_bitmap(this, encoded, SkIPoint::Make(0, 0)); |
| 217 return; | 224 return; |
| 218 } | 225 } |
| 219 | 226 |
| 220 this->writeUInt(0); // signal no pixels (in place of the size of the encoded
data) | 227 this->writeUInt(0); // signal no pixels (in place of the size of the encoded
data) |
| 221 } | 228 } |
| 222 | 229 |
| 223 void SkWriteBuffer::writeTypeface(SkTypeface* obj) { | 230 void SkBinaryWriteBuffer::writeTypeface(SkTypeface* obj) { |
| 224 if (nullptr == obj || nullptr == fTFSet) { | 231 if (nullptr == obj || nullptr == fTFSet) { |
| 225 fWriter.write32(0); | 232 fWriter.write32(0); |
| 226 } else { | 233 } else { |
| 227 fWriter.write32(fTFSet->add(obj)); | 234 fWriter.write32(fTFSet->add(obj)); |
| 228 } | 235 } |
| 229 } | 236 } |
| 230 | 237 |
| 231 SkFactorySet* SkWriteBuffer::setFactoryRecorder(SkFactorySet* rec) { | 238 void SkBinaryWriteBuffer::writePaint(const SkPaint& paint) { |
| 239 paint.flatten(*this); |
| 240 } |
| 241 |
| 242 SkFactorySet* SkBinaryWriteBuffer::setFactoryRecorder(SkFactorySet* rec) { |
| 232 SkRefCnt_SafeAssign(fFactorySet, rec); | 243 SkRefCnt_SafeAssign(fFactorySet, rec); |
| 233 return rec; | 244 return rec; |
| 234 } | 245 } |
| 235 | 246 |
| 236 SkRefCntSet* SkWriteBuffer::setTypefaceRecorder(SkRefCntSet* rec) { | 247 SkRefCntSet* SkBinaryWriteBuffer::setTypefaceRecorder(SkRefCntSet* rec) { |
| 237 SkRefCnt_SafeAssign(fTFSet, rec); | 248 SkRefCnt_SafeAssign(fTFSet, rec); |
| 238 return rec; | 249 return rec; |
| 239 } | 250 } |
| 240 | 251 |
| 241 void SkWriteBuffer::setBitmapHeap(SkBitmapHeap* bitmapHeap) { | 252 void SkBinaryWriteBuffer::setBitmapHeap(SkBitmapHeap* bitmapHeap) { |
| 242 SkRefCnt_SafeAssign(fBitmapHeap, bitmapHeap); | 253 SkRefCnt_SafeAssign(fBitmapHeap, bitmapHeap); |
| 243 if (bitmapHeap != nullptr) { | 254 if (bitmapHeap != nullptr) { |
| 244 SkASSERT(nullptr == fPixelSerializer); | 255 SkASSERT(nullptr == fPixelSerializer); |
| 245 fPixelSerializer.reset(nullptr); | 256 fPixelSerializer.reset(nullptr); |
| 246 } | 257 } |
| 247 } | 258 } |
| 248 | 259 |
| 249 void SkWriteBuffer::setPixelSerializer(SkPixelSerializer* serializer) { | 260 void SkBinaryWriteBuffer::setPixelSerializer(SkPixelSerializer* serializer) { |
| 250 fPixelSerializer.reset(serializer); | 261 fPixelSerializer.reset(serializer); |
| 251 if (serializer) { | 262 if (serializer) { |
| 252 serializer->ref(); | 263 serializer->ref(); |
| 253 SkASSERT(nullptr == fBitmapHeap); | 264 SkASSERT(nullptr == fBitmapHeap); |
| 254 SkSafeUnref(fBitmapHeap); | 265 SkSafeUnref(fBitmapHeap); |
| 255 fBitmapHeap = nullptr; | 266 fBitmapHeap = nullptr; |
| 256 } | 267 } |
| 257 } | 268 } |
| 258 | 269 |
| 259 void SkWriteBuffer::writeFlattenable(const SkFlattenable* flattenable) { | 270 void SkBinaryWriteBuffer::writeFlattenable(const SkFlattenable* flattenable) { |
| 260 /* | 271 /* |
| 261 * The first 32 bits tell us... | 272 * The first 32 bits tell us... |
| 262 * 0: failure to write the flattenable | 273 * 0: failure to write the flattenable |
| 263 * >0: index (1-based) into fFactorySet or fFlattenableDict or | 274 * >0: index (1-based) into fFactorySet or fFlattenableDict or |
| 264 * the first character of a string | 275 * the first character of a string |
| 265 */ | 276 */ |
| 266 if (nullptr == flattenable) { | 277 if (nullptr == flattenable) { |
| 267 this->write32(0); | 278 this->write32(0); |
| 268 return; | 279 return; |
| 269 } | 280 } |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 310 // make room for the size of the flattened object | 321 // make room for the size of the flattened object |
| 311 (void)fWriter.reserve(sizeof(uint32_t)); | 322 (void)fWriter.reserve(sizeof(uint32_t)); |
| 312 // record the current size, so we can subtract after the object writes. | 323 // record the current size, so we can subtract after the object writes. |
| 313 size_t offset = fWriter.bytesWritten(); | 324 size_t offset = fWriter.bytesWritten(); |
| 314 // now flatten the object | 325 // now flatten the object |
| 315 flattenable->flatten(*this); | 326 flattenable->flatten(*this); |
| 316 size_t objSize = fWriter.bytesWritten() - offset; | 327 size_t objSize = fWriter.bytesWritten() - offset; |
| 317 // record the obj's size | 328 // record the obj's size |
| 318 fWriter.overwriteTAt(offset - sizeof(uint32_t), SkToU32(objSize)); | 329 fWriter.overwriteTAt(offset - sizeof(uint32_t), SkToU32(objSize)); |
| 319 } | 330 } |
| OLD | NEW |