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 SkWriteBuffer::SkWriteBuffer(uint32_t flags) |
18 : fFlags(flags) | 18 : fFlags(flags) |
19 , fFactorySet(nullptr) | 19 , fFactorySet(nullptr) |
20 , fNamedFactorySet(nullptr) | |
21 , fBitmapHeap(nullptr) | 20 , fBitmapHeap(nullptr) |
22 , fTFSet(nullptr) { | 21 , fTFSet(nullptr) { |
23 } | 22 } |
24 | 23 |
25 SkWriteBuffer::SkWriteBuffer(void* storage, size_t storageSize, uint32_t flags) | 24 SkWriteBuffer::SkWriteBuffer(void* storage, size_t storageSize, uint32_t flags) |
26 : fFlags(flags) | 25 : fFlags(flags) |
27 , fFactorySet(nullptr) | 26 , fFactorySet(nullptr) |
28 , fNamedFactorySet(nullptr) | |
29 , fWriter(storage, storageSize) | 27 , fWriter(storage, storageSize) |
30 , fBitmapHeap(nullptr) | 28 , fBitmapHeap(nullptr) |
31 , fTFSet(nullptr) { | 29 , fTFSet(nullptr) { |
32 } | 30 } |
33 | 31 |
34 SkWriteBuffer::~SkWriteBuffer() { | 32 SkWriteBuffer::~SkWriteBuffer() { |
35 SkSafeUnref(fFactorySet); | 33 SkSafeUnref(fFactorySet); |
36 SkSafeUnref(fNamedFactorySet); | |
37 SkSafeUnref(fBitmapHeap); | 34 SkSafeUnref(fBitmapHeap); |
38 SkSafeUnref(fTFSet); | 35 SkSafeUnref(fTFSet); |
39 } | 36 } |
40 | 37 |
41 void SkWriteBuffer::writeByteArray(const void* data, size_t size) { | 38 void SkWriteBuffer::writeByteArray(const void* data, size_t size) { |
42 fWriter.write32(SkToU32(size)); | 39 fWriter.write32(SkToU32(size)); |
43 fWriter.writePad(data, size); | 40 fWriter.writePad(data, size); |
44 } | 41 } |
45 | 42 |
46 void SkWriteBuffer::writeBool(bool value) { | 43 void SkWriteBuffer::writeBool(bool value) { |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
226 void SkWriteBuffer::writeTypeface(SkTypeface* obj) { | 223 void SkWriteBuffer::writeTypeface(SkTypeface* obj) { |
227 if (nullptr == obj || nullptr == fTFSet) { | 224 if (nullptr == obj || nullptr == fTFSet) { |
228 fWriter.write32(0); | 225 fWriter.write32(0); |
229 } else { | 226 } else { |
230 fWriter.write32(fTFSet->add(obj)); | 227 fWriter.write32(fTFSet->add(obj)); |
231 } | 228 } |
232 } | 229 } |
233 | 230 |
234 SkFactorySet* SkWriteBuffer::setFactoryRecorder(SkFactorySet* rec) { | 231 SkFactorySet* SkWriteBuffer::setFactoryRecorder(SkFactorySet* rec) { |
235 SkRefCnt_SafeAssign(fFactorySet, rec); | 232 SkRefCnt_SafeAssign(fFactorySet, rec); |
236 if (fNamedFactorySet != nullptr) { | |
237 fNamedFactorySet->unref(); | |
238 fNamedFactorySet = nullptr; | |
239 } | |
240 return rec; | 233 return rec; |
241 } | 234 } |
242 | 235 |
243 SkNamedFactorySet* SkWriteBuffer::setNamedFactoryRecorder(SkNamedFactorySet* rec
) { | |
244 SkRefCnt_SafeAssign(fNamedFactorySet, rec); | |
245 if (fFactorySet != nullptr) { | |
246 fFactorySet->unref(); | |
247 fFactorySet = nullptr; | |
248 } | |
249 return rec; | |
250 } | |
251 | |
252 SkRefCntSet* SkWriteBuffer::setTypefaceRecorder(SkRefCntSet* rec) { | 236 SkRefCntSet* SkWriteBuffer::setTypefaceRecorder(SkRefCntSet* rec) { |
253 SkRefCnt_SafeAssign(fTFSet, rec); | 237 SkRefCnt_SafeAssign(fTFSet, rec); |
254 return rec; | 238 return rec; |
255 } | 239 } |
256 | 240 |
257 void SkWriteBuffer::setBitmapHeap(SkBitmapHeap* bitmapHeap) { | 241 void SkWriteBuffer::setBitmapHeap(SkBitmapHeap* bitmapHeap) { |
258 SkRefCnt_SafeAssign(fBitmapHeap, bitmapHeap); | 242 SkRefCnt_SafeAssign(fBitmapHeap, bitmapHeap); |
259 if (bitmapHeap != nullptr) { | 243 if (bitmapHeap != nullptr) { |
260 SkASSERT(nullptr == fPixelSerializer); | 244 SkASSERT(nullptr == fPixelSerializer); |
261 fPixelSerializer.reset(nullptr); | 245 fPixelSerializer.reset(nullptr); |
(...skipping 17 matching lines...) Expand all Loading... |
279 * >0: (1-based) index into the SkFactorySet or SkNamedFactorySet | 263 * >0: (1-based) index into the SkFactorySet or SkNamedFactorySet |
280 * If we don't have a factoryset, then the first "ptr" is either the | 264 * If we don't have a factoryset, then the first "ptr" is either the |
281 * factory, or null for failure. | 265 * factory, or null for failure. |
282 * | 266 * |
283 * The distinction is important, since 0-index is 32bits (always), but a | 267 * The distinction is important, since 0-index is 32bits (always), but a |
284 * 0-functionptr might be 32 or 64 bits. | 268 * 0-functionptr might be 32 or 64 bits. |
285 */ | 269 */ |
286 if (nullptr == flattenable) { | 270 if (nullptr == flattenable) { |
287 if (this->isValidating()) { | 271 if (this->isValidating()) { |
288 this->writeString(""); | 272 this->writeString(""); |
289 } else if (fFactorySet != nullptr || fNamedFactorySet != nullptr) { | 273 } else if (fFactorySet != nullptr) { |
290 this->write32(0); | 274 this->write32(0); |
291 } else { | 275 } else { |
292 this->writeFunctionPtr(nullptr); | 276 this->writeFunctionPtr(nullptr); |
293 } | 277 } |
294 return; | 278 return; |
295 } | 279 } |
296 | 280 |
297 SkFlattenable::Factory factory = flattenable->getFactory(); | 281 SkFlattenable::Factory factory = flattenable->getFactory(); |
298 SkASSERT(factory != nullptr); | 282 SkASSERT(factory != nullptr); |
299 | 283 |
300 /* | 284 /* |
301 * We can write 1 of 3 versions of the flattenable: | 285 * We can write 1 of 3 versions of the flattenable: |
302 * 1. function-ptr : this is the fastest for the reader, but assumes that | 286 * 1. function-ptr : this is the fastest for the reader, but assumes that |
303 * the writer and reader are in the same process. | 287 * the writer and reader are in the same process. |
304 * 2. index into fFactorySet : This is assumes the writer will later | 288 * 2. index into fFactorySet : This is assumes the writer will later |
305 * resolve the function-ptrs into strings for its reader. SkPicture | 289 * resolve the function-ptrs into strings for its reader. SkPicture |
306 * does exactly this, by writing a table of names (matching the indices
) | 290 * does exactly this, by writing a table of names (matching the indices
) |
307 * up front in its serialized form. | 291 * up front in its serialized form. |
308 * 3. index into fNamedFactorySet. fNamedFactorySet will also store the | 292 * 3. index into fNamedFactorySet. fNamedFactorySet will also store the |
309 * name. SkGPipe uses this technique so it can write the name to its | 293 * name. SkGPipe uses this technique so it can write the name to its |
310 * stream before writing the flattenable. | 294 * stream before writing the flattenable. |
311 */ | 295 */ |
312 if (this->isValidating()) { | 296 if (this->isValidating()) { |
313 this->writeString(flattenable->getTypeName()); | 297 this->writeString(flattenable->getTypeName()); |
314 } else if (fFactorySet) { | 298 } else if (fFactorySet) { |
315 this->write32(fFactorySet->add(factory)); | 299 this->write32(fFactorySet->add(factory)); |
316 } else if (fNamedFactorySet) { | |
317 int32_t index = fNamedFactorySet->find(factory); | |
318 this->write32(index); | |
319 if (0 == index) { | |
320 return; | |
321 } | |
322 } else { | 300 } else { |
323 this->writeFunctionPtr((void*)factory); | 301 this->writeFunctionPtr((void*)factory); |
324 } | 302 } |
325 | 303 |
326 // make room for the size of the flattened object | 304 // make room for the size of the flattened object |
327 (void)fWriter.reserve(sizeof(uint32_t)); | 305 (void)fWriter.reserve(sizeof(uint32_t)); |
328 // record the current size, so we can subtract after the object writes. | 306 // record the current size, so we can subtract after the object writes. |
329 size_t offset = fWriter.bytesWritten(); | 307 size_t offset = fWriter.bytesWritten(); |
330 // now flatten the object | 308 // now flatten the object |
331 flattenable->flatten(*this); | 309 flattenable->flatten(*this); |
332 size_t objSize = fWriter.bytesWritten() - offset; | 310 size_t objSize = fWriter.bytesWritten() - offset; |
333 // record the obj's size | 311 // record the obj's size |
334 fWriter.overwriteTAt(offset - sizeof(uint32_t), SkToU32(objSize)); | 312 fWriter.overwriteTAt(offset - sizeof(uint32_t), SkToU32(objSize)); |
335 } | 313 } |
OLD | NEW |