| 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" | |
| 11 #include "SkData.h" | 10 #include "SkData.h" |
| 12 #include "SkPixelRef.h" | 11 #include "SkPixelRef.h" |
| 13 #include "SkPtrRecorder.h" | 12 #include "SkPtrRecorder.h" |
| 14 #include "SkStream.h" | 13 #include "SkStream.h" |
| 15 #include "SkTypeface.h" | 14 #include "SkTypeface.h" |
| 16 | 15 |
| 17 SkWriteBuffer::SkWriteBuffer(uint32_t flags) | 16 SkWriteBuffer::SkWriteBuffer(uint32_t flags) |
| 18 : fFlags(flags) | 17 : fFlags(flags) |
| 19 , fFactorySet(nullptr) | 18 , fFactorySet(nullptr) |
| 20 , fBitmapHeap(nullptr) | |
| 21 , fTFSet(nullptr) { | 19 , fTFSet(nullptr) { |
| 22 } | 20 } |
| 23 | 21 |
| 24 SkWriteBuffer::SkWriteBuffer(void* storage, size_t storageSize, uint32_t flags) | 22 SkWriteBuffer::SkWriteBuffer(void* storage, size_t storageSize, uint32_t flags) |
| 25 : fFlags(flags) | 23 : fFlags(flags) |
| 26 , fFactorySet(nullptr) | 24 , fFactorySet(nullptr) |
| 27 , fWriter(storage, storageSize) | 25 , fWriter(storage, storageSize) |
| 28 , fBitmapHeap(nullptr) | |
| 29 , fTFSet(nullptr) { | 26 , fTFSet(nullptr) { |
| 30 } | 27 } |
| 31 | 28 |
| 32 SkWriteBuffer::~SkWriteBuffer() { | 29 SkWriteBuffer::~SkWriteBuffer() { |
| 33 SkSafeUnref(fFactorySet); | 30 SkSafeUnref(fFactorySet); |
| 34 SkSafeUnref(fBitmapHeap); | |
| 35 SkSafeUnref(fTFSet); | 31 SkSafeUnref(fTFSet); |
| 36 } | 32 } |
| 37 | 33 |
| 38 void SkWriteBuffer::writeByteArray(const void* data, size_t size) { | 34 void SkWriteBuffer::writeByteArray(const void* data, size_t size) { |
| 39 fWriter.write32(SkToU32(size)); | 35 fWriter.write32(SkToU32(size)); |
| 40 fWriter.writePad(data, size); | 36 fWriter.writePad(data, size); |
| 41 } | 37 } |
| 42 | 38 |
| 43 void SkWriteBuffer::writeBool(bool value) { | 39 void SkWriteBuffer::writeBool(bool value) { |
| 44 fWriter.writeBool(value); | 40 fWriter.writeBool(value); |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 buffer->write32(origin.fX); | 128 buffer->write32(origin.fX); |
| 133 buffer->write32(origin.fY); | 129 buffer->write32(origin.fY); |
| 134 } | 130 } |
| 135 | 131 |
| 136 void SkWriteBuffer::writeBitmap(const SkBitmap& bitmap) { | 132 void SkWriteBuffer::writeBitmap(const SkBitmap& bitmap) { |
| 137 // Record the width and height. This way if readBitmap fails a dummy bitmap
can be drawn at the | 133 // Record the width and height. This way if readBitmap fails a dummy bitmap
can be drawn at the |
| 138 // right size. | 134 // right size. |
| 139 this->writeInt(bitmap.width()); | 135 this->writeInt(bitmap.width()); |
| 140 this->writeInt(bitmap.height()); | 136 this->writeInt(bitmap.height()); |
| 141 | 137 |
| 142 // Record information about the bitmap in one of three ways, in order of pri
ority: | 138 // Record information about the bitmap in one of two ways, in order of prior
ity: |
| 143 // 1. If there is an SkBitmapHeap, store it in the heap. The client can avoi
d serializing the | 139 // 1. If there is a function for encoding bitmaps, use it to write an encode
d version of the |
| 144 // bitmap entirely or serialize it later as desired. A boolean value of t
rue will be written | |
| 145 // to the stream to signify that a heap was used. | |
| 146 // 2. If there is a function for encoding bitmaps, use it to write an encode
d version of the | |
| 147 // bitmap. After writing a boolean value of false, signifying that a heap
was not used, write | 140 // bitmap. After writing a boolean value of false, signifying that a heap
was not used, write |
| 148 // the size of the encoded data. A non-zero size signifies that encoded d
ata was written. | 141 // the size of the encoded data. A non-zero size signifies that encoded d
ata was written. |
| 149 // 3. Call SkBitmap::flatten. After writing a boolean value of false, signif
ying that a heap was | 142 // 2. Call SkBitmap::flatten. After writing a boolean value of false, signif
ying that a heap was |
| 150 // not used, write a zero to signify that the data was not encoded. | 143 // not used, write a zero to signify that the data was not encoded. |
| 151 bool useBitmapHeap = fBitmapHeap != nullptr; | 144 |
| 152 // Write a bool: true if the SkBitmapHeap is to be used, in which case the r
eader must use an | 145 // Write a bool to indicate that we did not use an SkBitmapHeap. That featur
e is deprecated. |
| 153 // SkBitmapHeapReader to read the SkBitmap. False if the bitmap was serializ
ed another way. | 146 this->writeBool(false); |
| 154 this->writeBool(useBitmapHeap); | |
| 155 if (useBitmapHeap) { | |
| 156 SkASSERT(nullptr == fPixelSerializer); | |
| 157 int32_t slot = fBitmapHeap->insert(bitmap); | |
| 158 fWriter.write32(slot); | |
| 159 // crbug.com/155875 | |
| 160 // The generation ID is not required information. We write it to prevent
collisions | |
| 161 // in SkFlatDictionary. It is possible to get a collision when a previo
usly | |
| 162 // unflattened (i.e. stale) instance of a similar flattenable is in the
dictionary | |
| 163 // and the instance currently being written is re-using the same slot fr
om the | |
| 164 // bitmap heap. | |
| 165 fWriter.write32(bitmap.getGenerationID()); | |
| 166 return; | |
| 167 } | |
| 168 | 147 |
| 169 SkPixelRef* pixelRef = bitmap.pixelRef(); | 148 SkPixelRef* pixelRef = bitmap.pixelRef(); |
| 170 if (pixelRef) { | 149 if (pixelRef) { |
| 171 // see if the pixelref already has an encoded version | 150 // see if the pixelref already has an encoded version |
| 172 SkAutoDataUnref existingData(pixelRef->refEncodedData()); | 151 SkAutoDataUnref existingData(pixelRef->refEncodedData()); |
| 173 if (existingData.get() != nullptr) { | 152 if (existingData.get() != nullptr) { |
| 174 // Assumes that if the client did not set a serializer, they are | 153 // Assumes that if the client did not set a serializer, they are |
| 175 // happy to get the encoded data. | 154 // happy to get the encoded data. |
| 176 if (!fPixelSerializer || fPixelSerializer->useEncodedData(existingDa
ta->data(), | 155 if (!fPixelSerializer || fPixelSerializer->useEncodedData(existingDa
ta->data(), |
| 177 existingDa
ta->size())) { | 156 existingDa
ta->size())) { |
| 178 write_encoded_bitmap(this, existingData, bitmap.pixelRefOrigin()
); | 157 write_encoded_bitmap(this, existingData, bitmap.pixelRefOrigin()
); |
| 179 return; | 158 return; |
| 180 } | 159 } |
| 181 } | 160 } |
| 182 | 161 |
| 183 // see if the caller wants to manually encode | 162 // see if the caller wants to manually encode |
| 184 SkAutoPixmapUnlock result; | 163 SkAutoPixmapUnlock result; |
| 185 if (fPixelSerializer && bitmap.requestLock(&result)) { | 164 if (fPixelSerializer && bitmap.requestLock(&result)) { |
| 186 SkASSERT(nullptr == fBitmapHeap); | |
| 187 SkAutoDataUnref data(fPixelSerializer->encode(result.pixmap())); | 165 SkAutoDataUnref data(fPixelSerializer->encode(result.pixmap())); |
| 188 if (data.get() != nullptr) { | 166 if (data.get() != nullptr) { |
| 189 // if we have to "encode" the bitmap, then we assume there is no | 167 // if we have to "encode" the bitmap, then we assume there is no |
| 190 // offset to share, since we are effectively creating a new pixe
lref | 168 // offset to share, since we are effectively creating a new pixe
lref |
| 191 write_encoded_bitmap(this, data, SkIPoint::Make(0, 0)); | 169 write_encoded_bitmap(this, data, SkIPoint::Make(0, 0)); |
| 192 return; | 170 return; |
| 193 } | 171 } |
| 194 } | 172 } |
| 195 } | 173 } |
| 196 | 174 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 222 SkFactorySet* SkWriteBuffer::setFactoryRecorder(SkFactorySet* rec) { | 200 SkFactorySet* SkWriteBuffer::setFactoryRecorder(SkFactorySet* rec) { |
| 223 SkRefCnt_SafeAssign(fFactorySet, rec); | 201 SkRefCnt_SafeAssign(fFactorySet, rec); |
| 224 return rec; | 202 return rec; |
| 225 } | 203 } |
| 226 | 204 |
| 227 SkRefCntSet* SkWriteBuffer::setTypefaceRecorder(SkRefCntSet* rec) { | 205 SkRefCntSet* SkWriteBuffer::setTypefaceRecorder(SkRefCntSet* rec) { |
| 228 SkRefCnt_SafeAssign(fTFSet, rec); | 206 SkRefCnt_SafeAssign(fTFSet, rec); |
| 229 return rec; | 207 return rec; |
| 230 } | 208 } |
| 231 | 209 |
| 232 void SkWriteBuffer::setBitmapHeap(SkBitmapHeap* bitmapHeap) { | |
| 233 SkRefCnt_SafeAssign(fBitmapHeap, bitmapHeap); | |
| 234 if (bitmapHeap != nullptr) { | |
| 235 SkASSERT(nullptr == fPixelSerializer); | |
| 236 fPixelSerializer.reset(nullptr); | |
| 237 } | |
| 238 } | |
| 239 | |
| 240 void SkWriteBuffer::setPixelSerializer(SkPixelSerializer* serializer) { | 210 void SkWriteBuffer::setPixelSerializer(SkPixelSerializer* serializer) { |
| 241 fPixelSerializer.reset(serializer); | 211 fPixelSerializer.reset(serializer); |
| 242 if (serializer) { | 212 if (serializer) { |
| 243 serializer->ref(); | 213 serializer->ref(); |
| 244 SkASSERT(nullptr == fBitmapHeap); | |
| 245 SkSafeUnref(fBitmapHeap); | |
| 246 fBitmapHeap = nullptr; | |
| 247 } | 214 } |
| 248 } | 215 } |
| 249 | 216 |
| 250 void SkWriteBuffer::writeFlattenable(const SkFlattenable* flattenable) { | 217 void SkWriteBuffer::writeFlattenable(const SkFlattenable* flattenable) { |
| 251 /* | 218 /* |
| 252 * The first 32 bits tell us... | 219 * The first 32 bits tell us... |
| 253 * 0: failure to write the flattenable | 220 * 0: failure to write the flattenable |
| 254 * >0: index (1-based) into fFactorySet or fFlattenableDict or | 221 * >0: index (1-based) into fFactorySet or fFlattenableDict or |
| 255 * the first character of a string | 222 * the first character of a string |
| 256 */ | 223 */ |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 301 // make room for the size of the flattened object | 268 // make room for the size of the flattened object |
| 302 (void)fWriter.reserve(sizeof(uint32_t)); | 269 (void)fWriter.reserve(sizeof(uint32_t)); |
| 303 // record the current size, so we can subtract after the object writes. | 270 // record the current size, so we can subtract after the object writes. |
| 304 size_t offset = fWriter.bytesWritten(); | 271 size_t offset = fWriter.bytesWritten(); |
| 305 // now flatten the object | 272 // now flatten the object |
| 306 flattenable->flatten(*this); | 273 flattenable->flatten(*this); |
| 307 size_t objSize = fWriter.bytesWritten() - offset; | 274 size_t objSize = fWriter.bytesWritten() - offset; |
| 308 // record the obj's size | 275 // record the obj's size |
| 309 fWriter.overwriteTAt(offset - sizeof(uint32_t), SkToU32(objSize)); | 276 fWriter.overwriteTAt(offset - sizeof(uint32_t), SkToU32(objSize)); |
| 310 } | 277 } |
| OLD | NEW |