| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 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 #include <new> | 7 #include <new> |
| 8 #include "SkBBoxHierarchy.h" | 8 #include "SkBBoxHierarchy.h" |
| 9 #include "SkDrawPictureCallback.h" | 9 #include "SkDrawPictureCallback.h" |
| 10 #include "SkPictureData.h" | 10 #include "SkPictureData.h" |
| 11 #include "SkPictureRecord.h" | 11 #include "SkPictureRecord.h" |
| 12 #include "SkPictureStateTree.h" | |
| 13 #include "SkReadBuffer.h" | 12 #include "SkReadBuffer.h" |
| 14 #include "SkTypeface.h" | 13 #include "SkTypeface.h" |
| 15 #include "SkTSort.h" | 14 #include "SkTSort.h" |
| 16 #include "SkWriteBuffer.h" | 15 #include "SkWriteBuffer.h" |
| 17 | 16 |
| 18 #if SK_SUPPORT_GPU | 17 #if SK_SUPPORT_GPU |
| 19 #include "GrContext.h" | 18 #include "GrContext.h" |
| 20 #endif | 19 #endif |
| 21 | 20 |
| 22 template <typename T> int SafeCount(const T* obj) { | 21 template <typename T> int SafeCount(const T* obj) { |
| 23 return obj ? obj->count() : 0; | 22 return obj ? obj->count() : 0; |
| 24 } | 23 } |
| 25 | 24 |
| 26 SkPictureData::SkPictureData(const SkPictInfo& info) | 25 SkPictureData::SkPictureData(const SkPictInfo& info) |
| 27 : fInfo(info) { | 26 : fInfo(info) { |
| 28 this->init(); | 27 this->init(); |
| 29 } | 28 } |
| 30 | 29 |
| 31 void SkPictureData::initForPlayback() const { | 30 void SkPictureData::initForPlayback() const { |
| 32 // ensure that the paths bounds are pre-computed | 31 // ensure that the paths bounds are pre-computed |
| 33 if (NULL != fPathHeap.get()) { | 32 if (NULL != fPathHeap.get()) { |
| 34 for (int i = 0; i < fPathHeap->count(); i++) { | 33 for (int i = 0; i < fPathHeap->count(); i++) { |
| 35 (*fPathHeap.get())[i].updateBoundsCache(); | 34 (*fPathHeap.get())[i].updateBoundsCache(); |
| 36 } | 35 } |
| 37 } | 36 } |
| 38 } | 37 } |
| 39 | 38 |
| 40 SkPictureData::SkPictureData(const SkPictureRecord& record, | 39 SkPictureData::SkPictureData(const SkPictureRecord& record, |
| 41 const SkPictInfo& info, | 40 const SkPictInfo& info, |
| 42 bool deepCopyOps) | 41 bool deepCopyOps) |
| 43 : fInfo(info) { | 42 : fInfo(info) { |
| 44 #ifdef SK_DEBUG_SIZE | 43 #ifdef SK_DEBUG_SIZE |
| 45 size_t overallBytes, bitmapBytes, matricesBytes, | 44 size_t overallBytes, bitmapBytes, matricesBytes, |
| 46 paintBytes, pathBytes, pictureBytes, regionBytes; | 45 paintBytes, pathBytes, pictureBytes, regionBytes; |
| 47 int bitmaps = record.bitmaps(&bitmapBytes); | 46 int bitmaps = record.bitmaps(&bitmapBytes); |
| 48 int matrices = record.matrices(&matricesBytes); | 47 int matrices = record.matrices(&matricesBytes); |
| 49 int paints = record.paints(&paintBytes); | 48 int paints = record.paints(&paintBytes); |
| 50 int paths = record.paths(&pathBytes); | 49 int paths = record.paths(&pathBytes); |
| 51 int pictures = record.pictures(&pictureBytes); | 50 int pictures = record.pictures(&pictureBytes); |
| 52 int regions = record.regions(®ionBytes); | 51 int regions = record.regions(®ionBytes); |
| (...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 282 } else { | 281 } else { |
| 283 size_t len = strlen(name); | 282 size_t len = strlen(name); |
| 284 size += SkWStream::SizeOfPackedUInt(len); | 283 size += SkWStream::SizeOfPackedUInt(len); |
| 285 size += len; | 284 size += len; |
| 286 } | 285 } |
| 287 } | 286 } |
| 288 | 287 |
| 289 return size; | 288 return size; |
| 290 } | 289 } |
| 291 | 290 |
| 291 static void write_tag_size(SkWriteBuffer& buffer, uint32_t tag, size_t size) { |
| 292 buffer.writeUInt(tag); |
| 293 buffer.writeUInt(SkToU32(size)); |
| 294 } |
| 295 |
| 296 static void write_tag_size(SkWStream* stream, uint32_t tag, size_t size) { |
| 297 stream->write32(tag); |
| 298 stream->write32(SkToU32(size)); |
| 299 } |
| 300 |
| 292 void SkPictureData::WriteFactories(SkWStream* stream, const SkFactorySet& rec) { | 301 void SkPictureData::WriteFactories(SkWStream* stream, const SkFactorySet& rec) { |
| 293 int count = rec.count(); | 302 int count = rec.count(); |
| 294 | 303 |
| 295 SkAutoSTMalloc<16, SkFlattenable::Factory> storage(count); | 304 SkAutoSTMalloc<16, SkFlattenable::Factory> storage(count); |
| 296 SkFlattenable::Factory* array = (SkFlattenable::Factory*)storage.get(); | 305 SkFlattenable::Factory* array = (SkFlattenable::Factory*)storage.get(); |
| 297 rec.copyToArray(array); | 306 rec.copyToArray(array); |
| 298 | 307 |
| 299 size_t size = compute_chunk_size(array, count); | 308 size_t size = compute_chunk_size(array, count); |
| 300 | 309 |
| 301 // TODO: write_tag_size should really take a size_t | 310 // TODO: write_tag_size should really take a size_t |
| 302 SkPicture::WriteTagSize(stream, SK_PICT_FACTORY_TAG, (uint32_t) size); | 311 write_tag_size(stream, SK_PICT_FACTORY_TAG, (uint32_t) size); |
| 303 SkDEBUGCODE(size_t start = stream->bytesWritten()); | 312 SkDEBUGCODE(size_t start = stream->bytesWritten()); |
| 304 stream->write32(count); | 313 stream->write32(count); |
| 305 | 314 |
| 306 for (int i = 0; i < count; i++) { | 315 for (int i = 0; i < count; i++) { |
| 307 const char* name = SkFlattenable::FactoryToName(array[i]); | 316 const char* name = SkFlattenable::FactoryToName(array[i]); |
| 308 // SkDebugf("---- write factories [%d] %p <%s>\n", i, array[i], name); | 317 // SkDebugf("---- write factories [%d] %p <%s>\n", i, array[i], name); |
| 309 if (NULL == name || 0 == *name) { | 318 if (NULL == name || 0 == *name) { |
| 310 stream->writePackedUInt(0); | 319 stream->writePackedUInt(0); |
| 311 } else { | 320 } else { |
| 312 size_t len = strlen(name); | 321 size_t len = strlen(name); |
| 313 stream->writePackedUInt(len); | 322 stream->writePackedUInt(len); |
| 314 stream->write(name, len); | 323 stream->write(name, len); |
| 315 } | 324 } |
| 316 } | 325 } |
| 317 | 326 |
| 318 SkASSERT(size == (stream->bytesWritten() - start)); | 327 SkASSERT(size == (stream->bytesWritten() - start)); |
| 319 } | 328 } |
| 320 | 329 |
| 321 void SkPictureData::WriteTypefaces(SkWStream* stream, const SkRefCntSet& rec) { | 330 void SkPictureData::WriteTypefaces(SkWStream* stream, const SkRefCntSet& rec) { |
| 322 int count = rec.count(); | 331 int count = rec.count(); |
| 323 | 332 |
| 324 SkPicture::WriteTagSize(stream, SK_PICT_TYPEFACE_TAG, count); | 333 write_tag_size(stream, SK_PICT_TYPEFACE_TAG, count); |
| 325 | 334 |
| 326 SkAutoSTMalloc<16, SkTypeface*> storage(count); | 335 SkAutoSTMalloc<16, SkTypeface*> storage(count); |
| 327 SkTypeface** array = (SkTypeface**)storage.get(); | 336 SkTypeface** array = (SkTypeface**)storage.get(); |
| 328 rec.copyToArray((SkRefCnt**)array); | 337 rec.copyToArray((SkRefCnt**)array); |
| 329 | 338 |
| 330 for (int i = 0; i < count; i++) { | 339 for (int i = 0; i < count; i++) { |
| 331 array[i]->serialize(stream); | 340 array[i]->serialize(stream); |
| 332 } | 341 } |
| 333 } | 342 } |
| 334 | 343 |
| 335 void SkPictureData::flattenToBuffer(SkWriteBuffer& buffer) const { | 344 void SkPictureData::flattenToBuffer(SkWriteBuffer& buffer) const { |
| 336 int i, n; | 345 int i, n; |
| 337 | 346 |
| 338 if ((n = SafeCount(fBitmaps)) > 0) { | 347 if ((n = SafeCount(fBitmaps)) > 0) { |
| 339 SkPicture::WriteTagSize(buffer, SK_PICT_BITMAP_BUFFER_TAG, n); | 348 write_tag_size(buffer, SK_PICT_BITMAP_BUFFER_TAG, n); |
| 340 for (i = 0; i < n; i++) { | 349 for (i = 0; i < n; i++) { |
| 341 buffer.writeBitmap((*fBitmaps)[i]); | 350 buffer.writeBitmap((*fBitmaps)[i]); |
| 342 } | 351 } |
| 343 } | 352 } |
| 344 | 353 |
| 345 if ((n = SafeCount(fPaints)) > 0) { | 354 if ((n = SafeCount(fPaints)) > 0) { |
| 346 SkPicture::WriteTagSize(buffer, SK_PICT_PAINT_BUFFER_TAG, n); | 355 write_tag_size(buffer, SK_PICT_PAINT_BUFFER_TAG, n); |
| 347 for (i = 0; i < n; i++) { | 356 for (i = 0; i < n; i++) { |
| 348 buffer.writePaint((*fPaints)[i]); | 357 buffer.writePaint((*fPaints)[i]); |
| 349 } | 358 } |
| 350 } | 359 } |
| 351 | 360 |
| 352 if ((n = SafeCount(fPathHeap.get())) > 0) { | 361 if ((n = SafeCount(fPathHeap.get())) > 0) { |
| 353 SkPicture::WriteTagSize(buffer, SK_PICT_PATH_BUFFER_TAG, n); | 362 write_tag_size(buffer, SK_PICT_PATH_BUFFER_TAG, n); |
| 354 fPathHeap->flatten(buffer); | 363 fPathHeap->flatten(buffer); |
| 355 } | 364 } |
| 356 } | 365 } |
| 357 | 366 |
| 358 void SkPictureData::serialize(SkWStream* stream, | 367 void SkPictureData::serialize(SkWStream* stream, |
| 359 SkPicture::EncodeBitmap encoder) const { | 368 SkPicture::EncodeBitmap encoder) const { |
| 360 SkPicture::WriteTagSize(stream, SK_PICT_READER_TAG, fOpData->size()); | 369 write_tag_size(stream, SK_PICT_READER_TAG, fOpData->size()); |
| 361 stream->write(fOpData->bytes(), fOpData->size()); | 370 stream->write(fOpData->bytes(), fOpData->size()); |
| 362 | 371 |
| 363 if (fPictureCount > 0) { | 372 if (fPictureCount > 0) { |
| 364 SkPicture::WriteTagSize(stream, SK_PICT_PICTURE_TAG, fPictureCount); | 373 write_tag_size(stream, SK_PICT_PICTURE_TAG, fPictureCount); |
| 365 for (int i = 0; i < fPictureCount; i++) { | 374 for (int i = 0; i < fPictureCount; i++) { |
| 366 fPictureRefs[i]->serialize(stream, encoder); | 375 fPictureRefs[i]->serialize(stream, encoder); |
| 367 } | 376 } |
| 368 } | 377 } |
| 369 | 378 |
| 370 // Write some of our data into a writebuffer, and then serialize that | 379 // Write some of our data into a writebuffer, and then serialize that |
| 371 // into our stream | 380 // into our stream |
| 372 { | 381 { |
| 373 SkRefCntSet typefaceSet; | 382 SkRefCntSet typefaceSet; |
| 374 SkFactorySet factSet; | 383 SkFactorySet factSet; |
| 375 | 384 |
| 376 SkWriteBuffer buffer(SkWriteBuffer::kCrossProcess_Flag); | 385 SkWriteBuffer buffer(SkWriteBuffer::kCrossProcess_Flag); |
| 377 buffer.setTypefaceRecorder(&typefaceSet); | 386 buffer.setTypefaceRecorder(&typefaceSet); |
| 378 buffer.setFactoryRecorder(&factSet); | 387 buffer.setFactoryRecorder(&factSet); |
| 379 buffer.setBitmapEncoder(encoder); | 388 buffer.setBitmapEncoder(encoder); |
| 380 | 389 |
| 381 this->flattenToBuffer(buffer); | 390 this->flattenToBuffer(buffer); |
| 382 | 391 |
| 383 // We have to write these two sets into the stream *before* we write | 392 // We have to write these two sets into the stream *before* we write |
| 384 // the buffer, since parsing that buffer will require that we already | 393 // the buffer, since parsing that buffer will require that we already |
| 385 // have these sets available to use. | 394 // have these sets available to use. |
| 386 WriteFactories(stream, factSet); | 395 WriteFactories(stream, factSet); |
| 387 WriteTypefaces(stream, typefaceSet); | 396 WriteTypefaces(stream, typefaceSet); |
| 388 | 397 |
| 389 SkPicture::WriteTagSize(stream, SK_PICT_BUFFER_SIZE_TAG, buffer.bytesWri
tten()); | 398 write_tag_size(stream, SK_PICT_BUFFER_SIZE_TAG, buffer.bytesWritten()); |
| 390 buffer.writeToStream(stream); | 399 buffer.writeToStream(stream); |
| 391 } | 400 } |
| 392 | 401 |
| 393 stream->write32(SK_PICT_EOF_TAG); | 402 stream->write32(SK_PICT_EOF_TAG); |
| 394 } | 403 } |
| 395 | 404 |
| 396 void SkPictureData::flatten(SkWriteBuffer& buffer) const { | 405 void SkPictureData::flatten(SkWriteBuffer& buffer) const { |
| 397 SkPicture::WriteTagSize(buffer, SK_PICT_READER_TAG, fOpData->size()); | 406 write_tag_size(buffer, SK_PICT_READER_TAG, fOpData->size()); |
| 398 buffer.writeByteArray(fOpData->bytes(), fOpData->size()); | 407 buffer.writeByteArray(fOpData->bytes(), fOpData->size()); |
| 399 | 408 |
| 400 if (fPictureCount > 0) { | 409 if (fPictureCount > 0) { |
| 401 SkPicture::WriteTagSize(buffer, SK_PICT_PICTURE_TAG, fPictureCount); | 410 write_tag_size(buffer, SK_PICT_PICTURE_TAG, fPictureCount); |
| 402 for (int i = 0; i < fPictureCount; i++) { | 411 for (int i = 0; i < fPictureCount; i++) { |
| 403 fPictureRefs[i]->flatten(buffer); | 412 fPictureRefs[i]->flatten(buffer); |
| 404 } | 413 } |
| 405 } | 414 } |
| 406 | 415 |
| 407 // Write this picture playback's data into a writebuffer | 416 // Write this picture playback's data into a writebuffer |
| 408 this->flattenToBuffer(buffer); | 417 this->flattenToBuffer(buffer); |
| 409 buffer.write32(SK_PICT_EOF_TAG); | 418 buffer.write32(SK_PICT_EOF_TAG); |
| 410 } | 419 } |
| 411 | 420 |
| (...skipping 823 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1235 for (index = 0; index < fPictureCount; index++) | 1244 for (index = 0; index < fPictureCount; index++) |
| 1236 bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer
), | 1245 bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer
), |
| 1237 "picture%p, ", fPictureRefs[index]); | 1246 "picture%p, ", fPictureRefs[index]); |
| 1238 if (fPictureCount > 0) | 1247 if (fPictureCount > 0) |
| 1239 SkDebugf("%s0};\n", pBuffer); | 1248 SkDebugf("%s0};\n", pBuffer); |
| 1240 | 1249 |
| 1241 const_cast<SkPictureData*>(this)->dumpStream(); | 1250 const_cast<SkPictureData*>(this)->dumpStream(); |
| 1242 } | 1251 } |
| 1243 | 1252 |
| 1244 #endif | 1253 #endif |
| OLD | NEW |