| 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::writeBool(bool value) { |
| 44 fWriter.writeBool(value); | 44 fWriter.writeBool(value); |
| 45 } | 45 } |
| 46 | 46 |
| 47 void SkWriteBuffer::writeScalar(SkScalar value) { | 47 void SkBinaryWriteBuffer::writeScalar(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 SkBinaryWriteBuffer::writeScalarArray(const SkScalar* value, uint32_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 SkBinaryWriteBuffer::writeInt(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 SkBinaryWriteBuffer::writeIntArray(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 SkBinaryWriteBuffer::writeUInt(uint32_t value) { |
| 66 fWriter.write32(value); | 66 fWriter.write32(value); |
| 67 } | 67 } |
| 68 | 68 |
| 69 void SkWriteBuffer::write32(int32_t value) { | 69 void SkBinaryWriteBuffer::write32(int32_t value) { |
| 70 fWriter.write32(value); | 70 fWriter.write32(value); |
| 71 } | 71 } |
| 72 | 72 |
| 73 void SkWriteBuffer::writeString(const char* value) { | 73 void SkBinaryWriteBuffer::writeString(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 SkBinaryWriteBuffer::writeEncodedString(const void* value, size_t byteLengt
h, |
| 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 SkBinaryWriteBuffer::writeColor(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 SkBinaryWriteBuffer::writeColorArray(const SkColor* color, uint32_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 SkBinaryWriteBuffer::writePoint(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 SkBinaryWriteBuffer::writePointArray(const SkPoint* point, uint32_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 SkBinaryWriteBuffer::writeMatrix(const SkMatrix& matrix) { |
| 105 fWriter.writeMatrix(matrix); | 105 fWriter.writeMatrix(matrix); |
| 106 } | 106 } |
| 107 | 107 |
| 108 void SkWriteBuffer::writeIRect(const SkIRect& rect) { | 108 void SkBinaryWriteBuffer::writeIRect(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 SkBinaryWriteBuffer::writeRect(const SkRect& rect) { |
| 113 fWriter.writeRect(rect); | 113 fWriter.writeRect(rect); |
| 114 } | 114 } |
| 115 | 115 |
| 116 void SkWriteBuffer::writeRegion(const SkRegion& region) { | 116 void SkBinaryWriteBuffer::writeRegion(const SkRegion& region) { |
| 117 fWriter.writeRegion(region); | 117 fWriter.writeRegion(region); |
| 118 } | 118 } |
| 119 | 119 |
| 120 void SkWriteBuffer::writePath(const SkPath& path) { | 120 void SkBinaryWriteBuffer::writePath(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 SkBinaryWriteBuffer::writeStream(SkStream* stream, size_t length) { |
| 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 SkBinaryWriteBuffer::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(SkBinaryWriteBuffer* buffer, SkData* data, |
| 138 const SkIPoint& origin) { | 138 const SkIPoint& origin) { |
| 139 buffer->writeUInt(SkToU32(data->size())); | 139 buffer->writeUInt(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(origin.fX); |
| 142 buffer->write32(origin.fY); | 142 buffer->write32(origin.fY); |
| 143 } | 143 } |
| 144 | 144 |
| 145 void SkWriteBuffer::writeBitmap(const SkBitmap& bitmap) { | 145 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 | 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(bitmap.width()); |
| 149 this->writeInt(bitmap.height()); | 149 this->writeInt(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 |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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(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 SkBinaryWriteBuffer::writeImage(const SkImage* image) { |
| 211 this->writeInt(image->width()); | 211 this->writeInt(image->width()); |
| 212 this->writeInt(image->height()); | 212 this->writeInt(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(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 SkBinaryWriteBuffer::writeTypeface(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* SkBinaryWriteBuffer::setFactoryRecorder(SkFactorySet* rec) { |
| 232 SkRefCnt_SafeAssign(fFactorySet, rec); | 232 SkRefCnt_SafeAssign(fFactorySet, rec); |
| 233 return rec; | 233 return rec; |
| 234 } | 234 } |
| 235 | 235 |
| 236 SkRefCntSet* SkWriteBuffer::setTypefaceRecorder(SkRefCntSet* rec) { | 236 SkRefCntSet* SkBinaryWriteBuffer::setTypefaceRecorder(SkRefCntSet* rec) { |
| 237 SkRefCnt_SafeAssign(fTFSet, rec); | 237 SkRefCnt_SafeAssign(fTFSet, rec); |
| 238 return rec; | 238 return rec; |
| 239 } | 239 } |
| 240 | 240 |
| 241 void SkWriteBuffer::setBitmapHeap(SkBitmapHeap* bitmapHeap) { | 241 void SkBinaryWriteBuffer::setBitmapHeap(SkBitmapHeap* bitmapHeap) { |
| 242 SkRefCnt_SafeAssign(fBitmapHeap, bitmapHeap); | 242 SkRefCnt_SafeAssign(fBitmapHeap, bitmapHeap); |
| 243 if (bitmapHeap != nullptr) { | 243 if (bitmapHeap != nullptr) { |
| 244 SkASSERT(nullptr == fPixelSerializer); | 244 SkASSERT(nullptr == fPixelSerializer); |
| 245 fPixelSerializer.reset(nullptr); | 245 fPixelSerializer.reset(nullptr); |
| 246 } | 246 } |
| 247 } | 247 } |
| 248 | 248 |
| 249 void SkWriteBuffer::setPixelSerializer(SkPixelSerializer* serializer) { | 249 void SkBinaryWriteBuffer::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 SkBinaryWriteBuffer::writeFlattenable(const SkFlattenable* flattenable) { |
| 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(0); |
| 268 return; | 268 return; |
| 269 } | 269 } |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 |