| OLD | NEW |
| 1 | 1 |
| 2 /* | 2 /* |
| 3 * Copyright 2012 Google Inc. | 3 * Copyright 2012 Google Inc. |
| 4 * | 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 #include "SkWriteBuffer.h" | 9 #include "SkWriteBuffer.h" |
| 10 #include "SkBitmap.h" | 10 #include "SkBitmap.h" |
| 11 #include "SkBitmapHeap.h" | 11 #include "SkBitmapHeap.h" |
| 12 #include "SkData.h" | 12 #include "SkData.h" |
| 13 #include "SkPixelRef.h" | 13 #include "SkPixelRef.h" |
| 14 #include "SkPtrRecorder.h" | 14 #include "SkPtrRecorder.h" |
| 15 #include "SkStream.h" | 15 #include "SkStream.h" |
| 16 #include "SkTypeface.h" | 16 #include "SkTypeface.h" |
| 17 | 17 |
| 18 SkWriteBuffer::SkWriteBuffer(uint32_t flags) | 18 SkWriteBuffer::SkWriteBuffer(uint32_t flags) |
| 19 : fFlags(flags) | 19 : fFlags(flags) |
| 20 , fFactorySet(NULL) | 20 , fFactorySet(NULL) |
| 21 , fNamedFactorySet(NULL) | 21 , fNamedFactorySet(NULL) |
| 22 , fBitmapHeap(NULL) | 22 , fBitmapHeap(NULL) |
| 23 , fTFSet(NULL) { | 23 , fTFSet(NULL) |
| 24 , fBitmapEncoder(NULL) { |
| 24 } | 25 } |
| 25 | 26 |
| 26 SkWriteBuffer::SkWriteBuffer(void* storage, size_t storageSize, uint32_t flags) | 27 SkWriteBuffer::SkWriteBuffer(void* storage, size_t storageSize, uint32_t flags) |
| 27 : fFlags(flags) | 28 : fFlags(flags) |
| 28 , fFactorySet(NULL) | 29 , fFactorySet(NULL) |
| 29 , fNamedFactorySet(NULL) | 30 , fNamedFactorySet(NULL) |
| 30 , fWriter(storage, storageSize) | 31 , fWriter(storage, storageSize) |
| 31 , fBitmapHeap(NULL) | 32 , fBitmapHeap(NULL) |
| 32 , fTFSet(NULL) { | 33 , fTFSet(NULL) |
| 34 , fBitmapEncoder(NULL) { |
| 33 } | 35 } |
| 34 | 36 |
| 35 SkWriteBuffer::~SkWriteBuffer() { | 37 SkWriteBuffer::~SkWriteBuffer() { |
| 36 SkSafeUnref(fFactorySet); | 38 SkSafeUnref(fFactorySet); |
| 37 SkSafeUnref(fNamedFactorySet); | 39 SkSafeUnref(fNamedFactorySet); |
| 38 SkSafeUnref(fBitmapHeap); | 40 SkSafeUnref(fBitmapHeap); |
| 39 SkSafeUnref(fTFSet); | 41 SkSafeUnref(fTFSet); |
| 40 } | 42 } |
| 41 | 43 |
| 42 void SkWriteBuffer::writeByteArray(const void* data, size_t size) { | 44 void SkWriteBuffer::writeByteArray(const void* data, size_t size) { |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 // 2. If there is a function for encoding bitmaps, use it to write an encode
d version of the | 165 // 2. If there is a function for encoding bitmaps, use it to write an encode
d version of the |
| 164 // bitmap. After writing a boolean value of false, signifying that a heap
was not used, write | 166 // bitmap. After writing a boolean value of false, signifying that a heap
was not used, write |
| 165 // the size of the encoded data. A non-zero size signifies that encoded d
ata was written. | 167 // the size of the encoded data. A non-zero size signifies that encoded d
ata was written. |
| 166 // 3. Call SkBitmap::flatten. After writing a boolean value of false, signif
ying that a heap was | 168 // 3. Call SkBitmap::flatten. After writing a boolean value of false, signif
ying that a heap was |
| 167 // not used, write a zero to signify that the data was not encoded. | 169 // not used, write a zero to signify that the data was not encoded. |
| 168 bool useBitmapHeap = fBitmapHeap != NULL; | 170 bool useBitmapHeap = fBitmapHeap != NULL; |
| 169 // Write a bool: true if the SkBitmapHeap is to be used, in which case the r
eader must use an | 171 // Write a bool: true if the SkBitmapHeap is to be used, in which case the r
eader must use an |
| 170 // SkBitmapHeapReader to read the SkBitmap. False if the bitmap was serializ
ed another way. | 172 // SkBitmapHeapReader to read the SkBitmap. False if the bitmap was serializ
ed another way. |
| 171 this->writeBool(useBitmapHeap); | 173 this->writeBool(useBitmapHeap); |
| 172 if (useBitmapHeap) { | 174 if (useBitmapHeap) { |
| 173 SkASSERT(NULL == fPixelSerializer); | 175 SkASSERT(NULL == fBitmapEncoder); |
| 174 int32_t slot = fBitmapHeap->insert(bitmap); | 176 int32_t slot = fBitmapHeap->insert(bitmap); |
| 175 fWriter.write32(slot); | 177 fWriter.write32(slot); |
| 176 // crbug.com/155875 | 178 // crbug.com/155875 |
| 177 // The generation ID is not required information. We write it to prevent
collisions | 179 // The generation ID is not required information. We write it to prevent
collisions |
| 178 // in SkFlatDictionary. It is possible to get a collision when a previo
usly | 180 // in SkFlatDictionary. It is possible to get a collision when a previo
usly |
| 179 // unflattened (i.e. stale) instance of a similar flattenable is in the
dictionary | 181 // unflattened (i.e. stale) instance of a similar flattenable is in the
dictionary |
| 180 // and the instance currently being written is re-using the same slot fr
om the | 182 // and the instance currently being written is re-using the same slot fr
om the |
| 181 // bitmap heap. | 183 // bitmap heap. |
| 182 fWriter.write32(bitmap.getGenerationID()); | 184 fWriter.write32(bitmap.getGenerationID()); |
| 183 return; | 185 return; |
| 184 } | 186 } |
| 185 | 187 |
| 186 SkPixelRef* pixelRef = bitmap.pixelRef(); | 188 // see if the pixelref already has an encoded version |
| 187 if (pixelRef) { | 189 if (bitmap.pixelRef()) { |
| 188 // see if the pixelref already has an encoded version | 190 SkAutoDataUnref data(bitmap.pixelRef()->refEncodedData()); |
| 189 SkAutoDataUnref existingData(bitmap.pixelRef()->refEncodedData()); | 191 if (data.get() != NULL) { |
| 190 if (existingData.get() != NULL) { | 192 write_encoded_bitmap(this, data, bitmap.pixelRefOrigin()); |
| 191 // Assumes that if the client did not set a serializer, they are | 193 return; |
| 192 // happy to get the encoded data. | |
| 193 if (!fPixelSerializer || fPixelSerializer->useEncodedData(existingDa
ta->data(), | |
| 194 existingDa
ta->size())) { | |
| 195 write_encoded_bitmap(this, existingData, bitmap.pixelRefOrigin()
); | |
| 196 return; | |
| 197 } | |
| 198 } | |
| 199 | |
| 200 // see if the caller wants to manually encode | |
| 201 if (fPixelSerializer) { | |
| 202 SkASSERT(NULL == fBitmapHeap); | |
| 203 SkAutoLockPixels alp(bitmap); | |
| 204 SkAutoDataUnref data(fPixelSerializer->encodePixels(bitmap.info(), | |
| 205 bitmap.getPixels
(), | |
| 206 bitmap.rowBytes(
))); | |
| 207 if (data.get() != NULL) { | |
| 208 // if we have to "encode" the bitmap, then we assume there is no | |
| 209 // offset to share, since we are effectively creating a new pixe
lref | |
| 210 write_encoded_bitmap(this, data, SkIPoint::Make(0, 0)); | |
| 211 return; | |
| 212 } | |
| 213 } | 194 } |
| 214 } | 195 } |
| 215 | 196 |
| 197 // see if the caller wants to manually encode |
| 198 if (fBitmapEncoder != NULL) { |
| 199 SkASSERT(NULL == fBitmapHeap); |
| 200 size_t offset = 0; // this parameter is deprecated/ignored |
| 201 // if we have to "encode" the bitmap, then we assume there is no |
| 202 // offset to share, since we are effectively creating a new pixelref |
| 203 SkAutoDataUnref data(fBitmapEncoder(&offset, bitmap)); |
| 204 if (data.get() != NULL) { |
| 205 write_encoded_bitmap(this, data, SkIPoint::Make(0, 0)); |
| 206 return; |
| 207 } |
| 208 } |
| 209 |
| 216 this->writeUInt(0); // signal raw pixels | 210 this->writeUInt(0); // signal raw pixels |
| 217 SkBitmap::WriteRawPixels(this, bitmap); | 211 SkBitmap::WriteRawPixels(this, bitmap); |
| 218 } | 212 } |
| 219 | 213 |
| 220 void SkWriteBuffer::writeTypeface(SkTypeface* obj) { | 214 void SkWriteBuffer::writeTypeface(SkTypeface* obj) { |
| 221 if (NULL == obj || NULL == fTFSet) { | 215 if (NULL == obj || NULL == fTFSet) { |
| 222 fWriter.write32(0); | 216 fWriter.write32(0); |
| 223 } else { | 217 } else { |
| 224 fWriter.write32(fTFSet->add(obj)); | 218 fWriter.write32(fTFSet->add(obj)); |
| 225 } | 219 } |
| (...skipping 18 matching lines...) Expand all Loading... |
| 244 } | 238 } |
| 245 | 239 |
| 246 SkRefCntSet* SkWriteBuffer::setTypefaceRecorder(SkRefCntSet* rec) { | 240 SkRefCntSet* SkWriteBuffer::setTypefaceRecorder(SkRefCntSet* rec) { |
| 247 SkRefCnt_SafeAssign(fTFSet, rec); | 241 SkRefCnt_SafeAssign(fTFSet, rec); |
| 248 return rec; | 242 return rec; |
| 249 } | 243 } |
| 250 | 244 |
| 251 void SkWriteBuffer::setBitmapHeap(SkBitmapHeap* bitmapHeap) { | 245 void SkWriteBuffer::setBitmapHeap(SkBitmapHeap* bitmapHeap) { |
| 252 SkRefCnt_SafeAssign(fBitmapHeap, bitmapHeap); | 246 SkRefCnt_SafeAssign(fBitmapHeap, bitmapHeap); |
| 253 if (bitmapHeap != NULL) { | 247 if (bitmapHeap != NULL) { |
| 254 SkASSERT(NULL == fPixelSerializer); | 248 SkASSERT(NULL == fBitmapEncoder); |
| 255 fPixelSerializer.reset(NULL); | 249 fBitmapEncoder = NULL; |
| 256 } | 250 } |
| 257 } | 251 } |
| 258 | 252 |
| 259 void SkWriteBuffer::setPixelSerializer(SkPixelSerializer* serializer) { | 253 void SkWriteBuffer::setBitmapEncoder(SkPicture::EncodeBitmap bitmapEncoder) { |
| 260 fPixelSerializer.reset(serializer); | 254 fBitmapEncoder = bitmapEncoder; |
| 261 if (serializer) { | 255 if (bitmapEncoder != NULL) { |
| 262 serializer->ref(); | |
| 263 SkASSERT(NULL == fBitmapHeap); | 256 SkASSERT(NULL == fBitmapHeap); |
| 264 SkSafeUnref(fBitmapHeap); | 257 SkSafeUnref(fBitmapHeap); |
| 265 fBitmapHeap = NULL; | 258 fBitmapHeap = NULL; |
| 266 } | 259 } |
| 267 } | 260 } |
| 268 | 261 |
| 269 void SkWriteBuffer::writeFlattenable(const SkFlattenable* flattenable) { | 262 void SkWriteBuffer::writeFlattenable(const SkFlattenable* flattenable) { |
| 270 /* | 263 /* |
| 271 * If we have a factoryset, then the first 32bits tell us... | 264 * If we have a factoryset, then the first 32bits tell us... |
| 272 * 0: failure to write the flattenable | 265 * 0: failure to write the flattenable |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 320 // make room for the size of the flattened object | 313 // make room for the size of the flattened object |
| 321 (void)fWriter.reserve(sizeof(uint32_t)); | 314 (void)fWriter.reserve(sizeof(uint32_t)); |
| 322 // record the current size, so we can subtract after the object writes. | 315 // record the current size, so we can subtract after the object writes. |
| 323 size_t offset = fWriter.bytesWritten(); | 316 size_t offset = fWriter.bytesWritten(); |
| 324 // now flatten the object | 317 // now flatten the object |
| 325 flattenable->flatten(*this); | 318 flattenable->flatten(*this); |
| 326 size_t objSize = fWriter.bytesWritten() - offset; | 319 size_t objSize = fWriter.bytesWritten() - offset; |
| 327 // record the obj's size | 320 // record the obj's size |
| 328 fWriter.overwriteTAt(offset - sizeof(uint32_t), SkToU32(objSize)); | 321 fWriter.overwriteTAt(offset - sizeof(uint32_t), SkToU32(objSize)); |
| 329 } | 322 } |
| OLD | NEW |