SkPicturePlayback.cpp revision db0c8753775774aa3f67114491e26ac1be32f38e
1 2/* 3 * Copyright 2011 Google Inc. 4 * 5 * Use of this source code is governed by a BSD-style license that can be 6 * found in the LICENSE file. 7 */ 8#include <new> 9#include "SkBBoxHierarchy.h" 10#include "SkOffsetTable.h" 11#include "SkPicturePlayback.h" 12#include "SkPictureRecord.h" 13#include "SkPictureStateTree.h" 14#include "SkReadBuffer.h" 15#include "SkTypeface.h" 16#include "SkTSort.h" 17#include "SkWriteBuffer.h" 18 19template <typename T> int SafeCount(const T* obj) { 20 return obj ? obj->count() : 0; 21} 22 23/* Define this to spew out a debug statement whenever we skip the remainder of 24 a save/restore block because a clip... command returned false (empty). 25 */ 26#define SPEW_CLIP_SKIPPINGx 27 28SkPicturePlayback::SkPicturePlayback() { 29 this->init(); 30} 31 32SkPicturePlayback::SkPicturePlayback(const SkPictureRecord& record, bool deepCopy) { 33#ifdef SK_DEBUG_SIZE 34 size_t overallBytes, bitmapBytes, matricesBytes, 35 paintBytes, pathBytes, pictureBytes, regionBytes; 36 int bitmaps = record.bitmaps(&bitmapBytes); 37 int matrices = record.matrices(&matricesBytes); 38 int paints = record.paints(&paintBytes); 39 int paths = record.paths(&pathBytes); 40 int pictures = record.pictures(&pictureBytes); 41 int regions = record.regions(®ionBytes); 42 SkDebugf("picture record mem used %zd (stream %zd) ", record.size(), 43 record.streamlen()); 44 if (bitmaps != 0) 45 SkDebugf("bitmaps size %zd (bitmaps:%d) ", bitmapBytes, bitmaps); 46 if (matrices != 0) 47 SkDebugf("matrices size %zd (matrices:%d) ", matricesBytes, matrices); 48 if (paints != 0) 49 SkDebugf("paints size %zd (paints:%d) ", paintBytes, paints); 50 if (paths != 0) 51 SkDebugf("paths size %zd (paths:%d) ", pathBytes, paths); 52 if (pictures != 0) 53 SkDebugf("pictures size %zd (pictures:%d) ", pictureBytes, pictures); 54 if (regions != 0) 55 SkDebugf("regions size %zd (regions:%d) ", regionBytes, regions); 56 if (record.fPointWrites != 0) 57 SkDebugf("points size %zd (points:%d) ", record.fPointBytes, record.fPointWrites); 58 if (record.fRectWrites != 0) 59 SkDebugf("rects size %zd (rects:%d) ", record.fRectBytes, record.fRectWrites); 60 if (record.fTextWrites != 0) 61 SkDebugf("text size %zd (text strings:%d) ", record.fTextBytes, record.fTextWrites); 62 63 SkDebugf("\n"); 64#endif 65#ifdef SK_DEBUG_DUMP 66 record.dumpMatrices(); 67 record.dumpPaints(); 68#endif 69 70 record.validate(record.writeStream().bytesWritten(), 0); 71 const SkWriter32& writer = record.writeStream(); 72 this->init(); 73 SkASSERT(!fOpData); 74 if (writer.bytesWritten() == 0) { 75 fOpData = SkData::NewEmpty(); 76 return; 77 } 78 fOpData = writer.snapshotAsData(); 79 80 fBoundingHierarchy = record.fBoundingHierarchy; 81 fStateTree = record.fStateTree; 82 83 SkSafeRef(fBoundingHierarchy); 84 SkSafeRef(fStateTree); 85 86 if (NULL != fBoundingHierarchy) { 87 fBoundingHierarchy->flushDeferredInserts(); 88 } 89 90 // copy over the refcnt dictionary to our reader 91 record.fFlattenableHeap.setupPlaybacks(); 92 93 fBitmaps = record.fBitmapHeap->extractBitmaps(); 94 fPaints = record.fPaints.unflattenToArray(); 95 96 fBitmapHeap.reset(SkSafeRef(record.fBitmapHeap)); 97 fPathHeap.reset(SkSafeRef(record.fPathHeap)); 98 99 fBitmapUseOffsets.reset(SkSafeRef(record.fBitmapUseOffsets.get())); 100 101 // ensure that the paths bounds are pre-computed 102 if (fPathHeap.get()) { 103 for (int i = 0; i < fPathHeap->count(); i++) { 104 (*fPathHeap)[i].updateBoundsCache(); 105 } 106 } 107 108 const SkTDArray<SkPicture* >& pictures = record.getPictureRefs(); 109 fPictureCount = pictures.count(); 110 if (fPictureCount > 0) { 111 fPictureRefs = SkNEW_ARRAY(SkPicture*, fPictureCount); 112 for (int i = 0; i < fPictureCount; i++) { 113 if (deepCopy) { 114 fPictureRefs[i] = pictures[i]->clone(); 115 } else { 116 fPictureRefs[i] = pictures[i]; 117 fPictureRefs[i]->ref(); 118 } 119 } 120 } 121 122#ifdef SK_DEBUG_SIZE 123 int overall = fPlayback->size(&overallBytes); 124 bitmaps = fPlayback->bitmaps(&bitmapBytes); 125 paints = fPlayback->paints(&paintBytes); 126 paths = fPlayback->paths(&pathBytes); 127 pictures = fPlayback->pictures(&pictureBytes); 128 regions = fPlayback->regions(®ionBytes); 129 SkDebugf("playback size %zd (objects:%d) ", overallBytes, overall); 130 if (bitmaps != 0) 131 SkDebugf("bitmaps size %zd (bitmaps:%d) ", bitmapBytes, bitmaps); 132 if (paints != 0) 133 SkDebugf("paints size %zd (paints:%d) ", paintBytes, paints); 134 if (paths != 0) 135 SkDebugf("paths size %zd (paths:%d) ", pathBytes, paths); 136 if (pictures != 0) 137 SkDebugf("pictures size %zd (pictures:%d) ", pictureBytes, pictures); 138 if (regions != 0) 139 SkDebugf("regions size %zd (regions:%d) ", regionBytes, regions); 140 SkDebugf("\n"); 141#endif 142} 143 144static bool needs_deep_copy(const SkPaint& paint) { 145 /* 146 * These fields are known to be immutable, and so can be shallow-copied 147 * 148 * getTypeface() 149 * getAnnotation() 150 * paint.getColorFilter() 151 * getXfermode() 152 */ 153 154 return paint.getPathEffect() || 155 paint.getShader() || 156 paint.getMaskFilter() || 157 paint.getRasterizer() || 158 paint.getLooper() || 159 paint.getImageFilter(); 160} 161 162SkPicturePlayback::SkPicturePlayback(const SkPicturePlayback& src, SkPictCopyInfo* deepCopyInfo) { 163 this->init(); 164 165 fBitmapHeap.reset(SkSafeRef(src.fBitmapHeap.get())); 166 fPathHeap.reset(SkSafeRef(src.fPathHeap.get())); 167 168 fOpData = SkSafeRef(src.fOpData); 169 170 fBoundingHierarchy = src.fBoundingHierarchy; 171 fStateTree = src.fStateTree; 172 173 SkSafeRef(fBoundingHierarchy); 174 SkSafeRef(fStateTree); 175 176 if (deepCopyInfo) { 177 int paintCount = SafeCount(src.fPaints); 178 179 if (src.fBitmaps) { 180 fBitmaps = SkTRefArray<SkBitmap>::Create(src.fBitmaps->begin(), src.fBitmaps->count()); 181 } 182 183 if (!deepCopyInfo->initialized) { 184 /* The alternative to doing this is to have a clone method on the paint and have it make 185 * the deep copy of its internal structures as needed. The holdup to doing that is at 186 * this point we would need to pass the SkBitmapHeap so that we don't unnecessarily 187 * flatten the pixels in a bitmap shader. 188 */ 189 deepCopyInfo->paintData.setCount(paintCount); 190 191 /* Use an SkBitmapHeap to avoid flattening bitmaps in shaders. If there already is one, 192 * use it. If this SkPicturePlayback was created from a stream, fBitmapHeap will be 193 * NULL, so create a new one. 194 */ 195 if (fBitmapHeap.get() == NULL) { 196 // FIXME: Put this on the stack inside SkPicture::clone. Further, is it possible to 197 // do the rest of this initialization in SkPicture::clone as well? 198 SkBitmapHeap* heap = SkNEW(SkBitmapHeap); 199 deepCopyInfo->controller.setBitmapStorage(heap); 200 heap->unref(); 201 } else { 202 deepCopyInfo->controller.setBitmapStorage(fBitmapHeap); 203 } 204 205 SkDEBUGCODE(int heapSize = SafeCount(fBitmapHeap.get());) 206 for (int i = 0; i < paintCount; i++) { 207 if (needs_deep_copy(src.fPaints->at(i))) { 208 deepCopyInfo->paintData[i] = 209 SkFlatData::Create<SkPaint::FlatteningTraits>(&deepCopyInfo->controller, 210 src.fPaints->at(i), 0); 211 212 } else { 213 // this is our sentinel, which we use in the unflatten loop 214 deepCopyInfo->paintData[i] = NULL; 215 } 216 } 217 SkASSERT(SafeCount(fBitmapHeap.get()) == heapSize); 218 219 // needed to create typeface playback 220 deepCopyInfo->controller.setupPlaybacks(); 221 deepCopyInfo->initialized = true; 222 } 223 224 fPaints = SkTRefArray<SkPaint>::Create(paintCount); 225 SkASSERT(deepCopyInfo->paintData.count() == paintCount); 226 SkBitmapHeap* bmHeap = deepCopyInfo->controller.getBitmapHeap(); 227 SkTypefacePlayback* tfPlayback = deepCopyInfo->controller.getTypefacePlayback(); 228 for (int i = 0; i < paintCount; i++) { 229 if (deepCopyInfo->paintData[i]) { 230 deepCopyInfo->paintData[i]->unflatten<SkPaint::FlatteningTraits>( 231 &fPaints->writableAt(i), bmHeap, tfPlayback); 232 } else { 233 // needs_deep_copy was false, so just need to assign 234 fPaints->writableAt(i) = src.fPaints->at(i); 235 } 236 } 237 238 } else { 239 fBitmaps = SkSafeRef(src.fBitmaps); 240 fPaints = SkSafeRef(src.fPaints); 241 } 242 243 fPictureCount = src.fPictureCount; 244 fPictureRefs = SkNEW_ARRAY(SkPicture*, fPictureCount); 245 for (int i = 0; i < fPictureCount; i++) { 246 if (deepCopyInfo) { 247 fPictureRefs[i] = src.fPictureRefs[i]->clone(); 248 } else { 249 fPictureRefs[i] = src.fPictureRefs[i]; 250 fPictureRefs[i]->ref(); 251 } 252 } 253} 254 255void SkPicturePlayback::init() { 256 fBitmaps = NULL; 257 fPaints = NULL; 258 fPictureRefs = NULL; 259 fPictureCount = 0; 260 fOpData = NULL; 261 fFactoryPlayback = NULL; 262 fBoundingHierarchy = NULL; 263 fStateTree = NULL; 264 fCachedActiveOps = NULL; 265} 266 267SkPicturePlayback::~SkPicturePlayback() { 268 SkSafeUnref(fOpData); 269 270 SkSafeUnref(fBitmaps); 271 SkSafeUnref(fPaints); 272 SkSafeUnref(fBoundingHierarchy); 273 SkSafeUnref(fStateTree); 274 275 SkDELETE(fCachedActiveOps); 276 277 for (int i = 0; i < fPictureCount; i++) { 278 fPictureRefs[i]->unref(); 279 } 280 SkDELETE_ARRAY(fPictureRefs); 281 282 SkDELETE(fFactoryPlayback); 283} 284 285void SkPicturePlayback::dumpSize() const { 286 SkDebugf("--- picture size: ops=%d bitmaps=%d [%d] paints=%d [%d] paths=%d\n", 287 fOpData->size(), 288 SafeCount(fBitmaps), SafeCount(fBitmaps) * sizeof(SkBitmap), 289 SafeCount(fPaints), SafeCount(fPaints) * sizeof(SkPaint), 290 SafeCount(fPathHeap.get())); 291} 292 293bool SkPicturePlayback::containsBitmaps() const { 294 if (fBitmaps && fBitmaps->count() > 0) { 295 return true; 296 } 297 for (int i = 0; i < fPictureCount; ++i) { 298 if (fPictureRefs[i]->willPlayBackBitmaps()) { 299 return true; 300 } 301 } 302 return false; 303} 304 305/////////////////////////////////////////////////////////////////////////////// 306/////////////////////////////////////////////////////////////////////////////// 307 308#include "SkStream.h" 309 310static void write_tag_size(SkWriteBuffer& buffer, uint32_t tag, uint32_t size) { 311 buffer.writeUInt(tag); 312 buffer.writeUInt(size); 313} 314 315static void write_tag_size(SkWStream* stream, uint32_t tag, uint32_t size) { 316 stream->write32(tag); 317 stream->write32(size); 318} 319 320static size_t compute_chunk_size(SkFlattenable::Factory* array, int count) { 321 size_t size = 4; // for 'count' 322 323 for (int i = 0; i < count; i++) { 324 const char* name = SkFlattenable::FactoryToName(array[i]); 325 if (NULL == name || 0 == *name) { 326 size += SkWStream::SizeOfPackedUInt(0); 327 } else { 328 size_t len = strlen(name); 329 size += SkWStream::SizeOfPackedUInt(len); 330 size += len; 331 } 332 } 333 334 return size; 335} 336 337static void write_factories(SkWStream* stream, const SkFactorySet& rec) { 338 int count = rec.count(); 339 340 SkAutoSTMalloc<16, SkFlattenable::Factory> storage(count); 341 SkFlattenable::Factory* array = (SkFlattenable::Factory*)storage.get(); 342 rec.copyToArray(array); 343 344 size_t size = compute_chunk_size(array, count); 345 346 // TODO: write_tag_size should really take a size_t 347 write_tag_size(stream, SK_PICT_FACTORY_TAG, (uint32_t) size); 348 SkDEBUGCODE(size_t start = stream->bytesWritten()); 349 stream->write32(count); 350 351 for (int i = 0; i < count; i++) { 352 const char* name = SkFlattenable::FactoryToName(array[i]); 353// SkDebugf("---- write factories [%d] %p <%s>\n", i, array[i], name); 354 if (NULL == name || 0 == *name) { 355 stream->writePackedUInt(0); 356 } else { 357 uint32_t len = strlen(name); 358 stream->writePackedUInt(len); 359 stream->write(name, len); 360 } 361 } 362 363 SkASSERT(size == (stream->bytesWritten() - start)); 364} 365 366static void write_typefaces(SkWStream* stream, const SkRefCntSet& rec) { 367 int count = rec.count(); 368 369 write_tag_size(stream, SK_PICT_TYPEFACE_TAG, count); 370 371 SkAutoSTMalloc<16, SkTypeface*> storage(count); 372 SkTypeface** array = (SkTypeface**)storage.get(); 373 rec.copyToArray((SkRefCnt**)array); 374 375 for (int i = 0; i < count; i++) { 376 array[i]->serialize(stream); 377 } 378} 379 380void SkPicturePlayback::flattenToBuffer(SkWriteBuffer& buffer) const { 381 int i, n; 382 383 if ((n = SafeCount(fBitmaps)) > 0) { 384 write_tag_size(buffer, SK_PICT_BITMAP_BUFFER_TAG, n); 385 for (i = 0; i < n; i++) { 386 buffer.writeBitmap((*fBitmaps)[i]); 387 } 388 } 389 390 if ((n = SafeCount(fPaints)) > 0) { 391 write_tag_size(buffer, SK_PICT_PAINT_BUFFER_TAG, n); 392 for (i = 0; i < n; i++) { 393 buffer.writePaint((*fPaints)[i]); 394 } 395 } 396 397 if ((n = SafeCount(fPathHeap.get())) > 0) { 398 write_tag_size(buffer, SK_PICT_PATH_BUFFER_TAG, n); 399 fPathHeap->flatten(buffer); 400 } 401} 402 403void SkPicturePlayback::serialize(SkWStream* stream, 404 SkPicture::EncodeBitmap encoder) const { 405 write_tag_size(stream, SK_PICT_READER_TAG, fOpData->size()); 406 stream->write(fOpData->bytes(), fOpData->size()); 407 408 if (fPictureCount > 0) { 409 write_tag_size(stream, SK_PICT_PICTURE_TAG, fPictureCount); 410 for (int i = 0; i < fPictureCount; i++) { 411 fPictureRefs[i]->serialize(stream, encoder); 412 } 413 } 414 415 // Write some of our data into a writebuffer, and then serialize that 416 // into our stream 417 { 418 SkRefCntSet typefaceSet; 419 SkFactorySet factSet; 420 421 SkWriteBuffer buffer(SkWriteBuffer::kCrossProcess_Flag); 422 buffer.setTypefaceRecorder(&typefaceSet); 423 buffer.setFactoryRecorder(&factSet); 424 buffer.setBitmapEncoder(encoder); 425 426 this->flattenToBuffer(buffer); 427 428 // We have to write these two sets into the stream *before* we write 429 // the buffer, since parsing that buffer will require that we already 430 // have these sets available to use. 431 write_factories(stream, factSet); 432 write_typefaces(stream, typefaceSet); 433 434 write_tag_size(stream, SK_PICT_BUFFER_SIZE_TAG, buffer.bytesWritten()); 435 buffer.writeToStream(stream); 436 } 437 438 stream->write32(SK_PICT_EOF_TAG); 439} 440 441void SkPicturePlayback::flatten(SkWriteBuffer& buffer) const { 442 write_tag_size(buffer, SK_PICT_READER_TAG, fOpData->size()); 443 buffer.writeByteArray(fOpData->bytes(), fOpData->size()); 444 445 if (fPictureCount > 0) { 446 write_tag_size(buffer, SK_PICT_PICTURE_TAG, fPictureCount); 447 for (int i = 0; i < fPictureCount; i++) { 448 fPictureRefs[i]->flatten(buffer); 449 } 450 } 451 452 // Write this picture playback's data into a writebuffer 453 this->flattenToBuffer(buffer); 454 buffer.write32(SK_PICT_EOF_TAG); 455} 456 457/////////////////////////////////////////////////////////////////////////////// 458 459/** 460 * Return the corresponding SkReadBuffer flags, given a set of 461 * SkPictInfo flags. 462 */ 463static uint32_t pictInfoFlagsToReadBufferFlags(uint32_t pictInfoFlags) { 464 static const struct { 465 uint32_t fSrc; 466 uint32_t fDst; 467 } gSD[] = { 468 { SkPictInfo::kCrossProcess_Flag, SkReadBuffer::kCrossProcess_Flag }, 469 { SkPictInfo::kScalarIsFloat_Flag, SkReadBuffer::kScalarIsFloat_Flag }, 470 { SkPictInfo::kPtrIs64Bit_Flag, SkReadBuffer::kPtrIs64Bit_Flag }, 471 }; 472 473 uint32_t rbMask = 0; 474 for (size_t i = 0; i < SK_ARRAY_COUNT(gSD); ++i) { 475 if (pictInfoFlags & gSD[i].fSrc) { 476 rbMask |= gSD[i].fDst; 477 } 478 } 479 return rbMask; 480} 481 482bool SkPicturePlayback::parseStreamTag(SkStream* stream, const SkPictInfo& info, uint32_t tag, 483 size_t size, SkPicture::InstallPixelRefProc proc) { 484 /* 485 * By the time we encounter BUFFER_SIZE_TAG, we need to have already seen 486 * its dependents: FACTORY_TAG and TYPEFACE_TAG. These two are not required 487 * but if they are present, they need to have been seen before the buffer. 488 * 489 * We assert that if/when we see either of these, that we have not yet seen 490 * the buffer tag, because if we have, then its too-late to deal with the 491 * factories or typefaces. 492 */ 493 SkDEBUGCODE(bool haveBuffer = false;) 494 495 switch (tag) { 496 case SK_PICT_READER_TAG: { 497 SkAutoMalloc storage(size); 498 if (stream->read(storage.get(), size) != size) { 499 return false; 500 } 501 SkASSERT(NULL == fOpData); 502 fOpData = SkData::NewFromMalloc(storage.detach(), size); 503 } break; 504 case SK_PICT_FACTORY_TAG: { 505 SkASSERT(!haveBuffer); 506 // Remove this code when v21 and below are no longer supported. At the 507 // same time add a new 'count' variable and use it rather then reusing 'size'. 508#ifndef DISABLE_V21_COMPATIBILITY_CODE 509 if (info.fVersion >= 22) { 510 // in v22 this tag's size represents the size of the chunk in bytes 511 // and the number of factory strings is written out separately 512#endif 513 size = stream->readU32(); 514#ifndef DISABLE_V21_COMPATIBILITY_CODE 515 } 516#endif 517 fFactoryPlayback = SkNEW_ARGS(SkFactoryPlayback, (size)); 518 for (size_t i = 0; i < size; i++) { 519 SkString str; 520 const size_t len = stream->readPackedUInt(); 521 str.resize(len); 522 if (stream->read(str.writable_str(), len) != len) { 523 return false; 524 } 525 fFactoryPlayback->base()[i] = SkFlattenable::NameToFactory(str.c_str()); 526 } 527 } break; 528 case SK_PICT_TYPEFACE_TAG: { 529 SkASSERT(!haveBuffer); 530 fTFPlayback.setCount(size); 531 for (size_t i = 0; i < size; i++) { 532 SkAutoTUnref<SkTypeface> tf(SkTypeface::Deserialize(stream)); 533 if (!tf.get()) { // failed to deserialize 534 // fTFPlayback asserts it never has a null, so we plop in 535 // the default here. 536 tf.reset(SkTypeface::RefDefault()); 537 } 538 fTFPlayback.set(i, tf); 539 } 540 } break; 541 case SK_PICT_PICTURE_TAG: { 542 fPictureCount = size; 543 fPictureRefs = SkNEW_ARRAY(SkPicture*, fPictureCount); 544 bool success = true; 545 int i = 0; 546 for ( ; i < fPictureCount; i++) { 547 fPictureRefs[i] = SkPicture::CreateFromStream(stream, proc); 548 if (NULL == fPictureRefs[i]) { 549 success = false; 550 break; 551 } 552 } 553 if (!success) { 554 // Delete all of the pictures that were already created (up to but excluding i): 555 for (int j = 0; j < i; j++) { 556 fPictureRefs[j]->unref(); 557 } 558 // Delete the array 559 SkDELETE_ARRAY(fPictureRefs); 560 fPictureCount = 0; 561 return false; 562 } 563 } break; 564 case SK_PICT_BUFFER_SIZE_TAG: { 565 SkAutoMalloc storage(size); 566 if (stream->read(storage.get(), size) != size) { 567 return false; 568 } 569 570 SkReadBuffer buffer(storage.get(), size); 571 buffer.setFlags(pictInfoFlagsToReadBufferFlags(info.fFlags)); 572 573 fFactoryPlayback->setupBuffer(buffer); 574 fTFPlayback.setupBuffer(buffer); 575 buffer.setBitmapDecoder(proc); 576 577 while (!buffer.eof()) { 578 tag = buffer.readUInt(); 579 size = buffer.readUInt(); 580 if (!this->parseBufferTag(buffer, tag, size)) { 581 return false; 582 } 583 } 584 SkDEBUGCODE(haveBuffer = true;) 585 } break; 586 } 587 return true; // success 588} 589 590bool SkPicturePlayback::parseBufferTag(SkReadBuffer& buffer, 591 uint32_t tag, size_t size) { 592 switch (tag) { 593 case SK_PICT_BITMAP_BUFFER_TAG: { 594 fBitmaps = SkTRefArray<SkBitmap>::Create(size); 595 for (size_t i = 0; i < size; ++i) { 596 SkBitmap* bm = &fBitmaps->writableAt(i); 597 buffer.readBitmap(bm); 598 bm->setImmutable(); 599 } 600 } break; 601 case SK_PICT_PAINT_BUFFER_TAG: { 602 fPaints = SkTRefArray<SkPaint>::Create(size); 603 for (size_t i = 0; i < size; ++i) { 604 buffer.readPaint(&fPaints->writableAt(i)); 605 } 606 } break; 607 case SK_PICT_PATH_BUFFER_TAG: 608 if (size > 0) { 609 fPathHeap.reset(SkNEW_ARGS(SkPathHeap, (buffer))); 610 } 611 break; 612 case SK_PICT_READER_TAG: { 613 SkAutoMalloc storage(size); 614 if (!buffer.readByteArray(storage.get(), size) || 615 !buffer.validate(NULL == fOpData)) { 616 return false; 617 } 618 SkASSERT(NULL == fOpData); 619 fOpData = SkData::NewFromMalloc(storage.detach(), size); 620 } break; 621 case SK_PICT_PICTURE_TAG: { 622 if (!buffer.validate((0 == fPictureCount) && (NULL == fPictureRefs))) { 623 return false; 624 } 625 fPictureCount = size; 626 fPictureRefs = SkNEW_ARRAY(SkPicture*, fPictureCount); 627 bool success = true; 628 int i = 0; 629 for ( ; i < fPictureCount; i++) { 630 fPictureRefs[i] = SkPicture::CreateFromBuffer(buffer); 631 if (NULL == fPictureRefs[i]) { 632 success = false; 633 break; 634 } 635 } 636 if (!success) { 637 // Delete all of the pictures that were already created (up to but excluding i): 638 for (int j = 0; j < i; j++) { 639 fPictureRefs[j]->unref(); 640 } 641 // Delete the array 642 SkDELETE_ARRAY(fPictureRefs); 643 fPictureCount = 0; 644 return false; 645 } 646 } break; 647 default: 648 // The tag was invalid. 649 return false; 650 } 651 return true; // success 652} 653 654SkPicturePlayback* SkPicturePlayback::CreateFromStream(SkStream* stream, 655 const SkPictInfo& info, 656 SkPicture::InstallPixelRefProc proc) { 657 SkAutoTDelete<SkPicturePlayback> playback(SkNEW(SkPicturePlayback)); 658 659 if (!playback->parseStream(stream, info, proc)) { 660 return NULL; 661 } 662 return playback.detach(); 663} 664 665SkPicturePlayback* SkPicturePlayback::CreateFromBuffer(SkReadBuffer& buffer) { 666 SkAutoTDelete<SkPicturePlayback> playback(SkNEW(SkPicturePlayback)); 667 668 if (!playback->parseBuffer(buffer)) { 669 return NULL; 670 } 671 return playback.detach(); 672} 673 674bool SkPicturePlayback::parseStream(SkStream* stream, const SkPictInfo& info, 675 SkPicture::InstallPixelRefProc proc) { 676 for (;;) { 677 uint32_t tag = stream->readU32(); 678 if (SK_PICT_EOF_TAG == tag) { 679 break; 680 } 681 682 uint32_t size = stream->readU32(); 683 if (!this->parseStreamTag(stream, info, tag, size, proc)) { 684 return false; // we're invalid 685 } 686 } 687 return true; 688} 689 690bool SkPicturePlayback::parseBuffer(SkReadBuffer& buffer) { 691 for (;;) { 692 uint32_t tag = buffer.readUInt(); 693 if (SK_PICT_EOF_TAG == tag) { 694 break; 695 } 696 697 uint32_t size = buffer.readUInt(); 698 if (!this->parseBufferTag(buffer, tag, size)) { 699 return false; // we're invalid 700 } 701 } 702 return true; 703} 704 705/////////////////////////////////////////////////////////////////////////////// 706/////////////////////////////////////////////////////////////////////////////// 707 708#ifdef SPEW_CLIP_SKIPPING 709struct SkipClipRec { 710 int fCount; 711 size_t fSize; 712 713 SkipClipRec() { 714 fCount = 0; 715 fSize = 0; 716 } 717 718 void recordSkip(size_t bytes) { 719 fCount += 1; 720 fSize += bytes; 721 } 722}; 723#endif 724 725#ifdef SK_DEVELOPER 726bool SkPicturePlayback::preDraw(int opIndex, int type) { 727 return false; 728} 729 730void SkPicturePlayback::postDraw(int opIndex) { 731} 732#endif 733 734/* 735 * Read the next op code and chunk size from 'reader'. The returned size 736 * is the entire size of the chunk (including the opcode). Thus, the 737 * offset just prior to calling read_op_and_size + 'size' is the offset 738 * to the next chunk's op code. This also means that the size of a chunk 739 * with no arguments (just an opcode) will be 4. 740 */ 741static DrawType read_op_and_size(SkReader32* reader, uint32_t* size) { 742 uint32_t temp = reader->readInt(); 743 uint32_t op; 744 if (((uint8_t) temp) == temp) { 745 // old skp file - no size information 746 op = temp; 747 *size = 0; 748 } else { 749 UNPACK_8_24(temp, op, *size); 750 if (MASK_24 == *size) { 751 *size = reader->readInt(); 752 } 753 } 754 return (DrawType) op; 755} 756 757// The activeOps parameter is actually "const SkTDArray<SkPictureStateTree::Draw*>&". 758// It represents the operations about to be drawn, as generated by some spatial 759// subdivision helper class. It should already be in 'fOffset' sorted order. 760void SkPicturePlayback::preLoadBitmaps(const SkTDArray<void*>* activeOps) { 761 if ((NULL != activeOps && 0 == activeOps->count()) || NULL == fBitmapUseOffsets) { 762 return; 763 } 764 765 if (NULL == activeOps) { 766 // going to need everything 767 return; 768 } 769 770 SkTDArray<int> active; 771 772 SkAutoTDeleteArray<bool> needToCheck(new bool[fBitmapUseOffsets->numIDs()]); 773 for (int i = 0; i < fBitmapUseOffsets->numIDs(); ++i) { 774 needToCheck.get()[i] = true; 775 } 776 777 uint32_t max = ((SkPictureStateTree::Draw*)(*activeOps)[(*activeOps).count()-1])->fOffset; 778 779 for (int i = 0; i < activeOps->count(); ++i) { 780 SkPictureStateTree::Draw* draw = (SkPictureStateTree::Draw*) (*activeOps)[i]; 781 782 for (int j = 0; j < fBitmapUseOffsets->numIDs(); ++j) { 783 if (!needToCheck.get()[j]) { 784 continue; 785 } 786 787 if (!fBitmapUseOffsets->overlap(j, draw->fOffset, max)) { 788 needToCheck.get()[j] = false; 789 continue; 790 } 791 792 if (!fBitmapUseOffsets->includes(j, draw->fOffset)) { 793 continue; 794 } 795 796 *active.append() = j; 797 needToCheck.get()[j] = false; 798 } 799 } 800 801 for (int i = 0; i < active.count(); ++i) { 802 SkDebugf("preload texture %d\n", active[i]); 803 } 804} 805 806uint32_t SkPicturePlayback::CachedOperationList::offset(int index) const { 807 SkASSERT(index < fOps.count()); 808 return ((SkPictureStateTree::Draw*)fOps[index])->fOffset; 809} 810 811const SkMatrix& SkPicturePlayback::CachedOperationList::matrix(int index) const { 812 SkASSERT(index < fOps.count()); 813 return *((SkPictureStateTree::Draw*)fOps[index])->fMatrix; 814} 815 816const SkPicture::OperationList& SkPicturePlayback::getActiveOps(const SkIRect& query) { 817 if (NULL == fStateTree || NULL == fBoundingHierarchy) { 818 return SkPicture::OperationList::InvalidList(); 819 } 820 821 if (NULL == fCachedActiveOps) { 822 fCachedActiveOps = SkNEW(CachedOperationList); 823 } 824 825 if (query == fCachedActiveOps->fCacheQueryRect) { 826 return *fCachedActiveOps; 827 } 828 829 fCachedActiveOps->fOps.rewind(); 830 831 fBoundingHierarchy->search(query, &(fCachedActiveOps->fOps)); 832 833 SkTQSort<SkPictureStateTree::Draw>( 834 reinterpret_cast<SkPictureStateTree::Draw**>(fCachedActiveOps->fOps.begin()), 835 reinterpret_cast<SkPictureStateTree::Draw**>(fCachedActiveOps->fOps.end()-1)); 836 837 fCachedActiveOps->fCacheQueryRect = query; 838 return *fCachedActiveOps; 839} 840 841void SkPicturePlayback::draw(SkCanvas& canvas, SkDrawPictureCallback* callback) { 842#ifdef ENABLE_TIME_DRAW 843 SkAutoTime at("SkPicture::draw", 50); 844#endif 845 846#ifdef SPEW_CLIP_SKIPPING 847 SkipClipRec skipRect, skipRRect, skipRegion, skipPath, skipCull; 848 int opCount = 0; 849#endif 850 851#ifdef SK_BUILD_FOR_ANDROID 852 SkAutoMutexAcquire autoMutex(fDrawMutex); 853#endif 854 855 // kDrawComplete will be the signal that we have reached the end of 856 // the command stream 857 static const uint32_t kDrawComplete = SK_MaxU32; 858 859 SkReader32 reader(fOpData->bytes(), fOpData->size()); 860 TextContainer text; 861 const SkTDArray<void*>* activeOps = NULL; 862 863 if (NULL != fStateTree && NULL != fBoundingHierarchy) { 864 SkRect clipBounds; 865 if (canvas.getClipBounds(&clipBounds)) { 866 SkIRect query; 867 clipBounds.roundOut(&query); 868 869 const SkPicture::OperationList& activeOpsList = this->getActiveOps(query); 870 if (activeOpsList.valid()) { 871 if (0 == activeOpsList.numOps()) { 872 return; // nothing to draw 873 } 874 875 // Since the opList is valid we know it is our derived class 876 activeOps = &((const CachedOperationList&)activeOpsList).fOps; 877 } 878 } 879 } 880 881 SkPictureStateTree::Iterator it = (NULL == activeOps) ? 882 SkPictureStateTree::Iterator() : 883 fStateTree->getIterator(*activeOps, &canvas); 884 885 if (it.isValid()) { 886 uint32_t skipTo = it.draw(); 887 if (kDrawComplete == skipTo) { 888 return; 889 } 890 reader.setOffset(skipTo); 891 } 892 893 this->preLoadBitmaps(activeOps); 894 895 // Record this, so we can concat w/ it if we encounter a setMatrix() 896 SkMatrix initialMatrix = canvas.getTotalMatrix(); 897 int originalSaveCount = canvas.getSaveCount(); 898 899#ifdef SK_BUILD_FOR_ANDROID 900 fAbortCurrentPlayback = false; 901#endif 902 903#ifdef SK_DEVELOPER 904 int opIndex = -1; 905#endif 906 907 while (!reader.eof()) { 908 if (callback && callback->abortDrawing()) { 909 canvas.restoreToCount(originalSaveCount); 910 return; 911 } 912#ifdef SK_BUILD_FOR_ANDROID 913 if (fAbortCurrentPlayback) { 914 return; 915 } 916#endif 917 918#ifdef SPEW_CLIP_SKIPPING 919 opCount++; 920#endif 921 922 size_t curOffset = reader.offset(); 923 uint32_t size; 924 DrawType op = read_op_and_size(&reader, &size); 925 size_t skipTo = 0; 926 if (NOOP == op) { 927 // NOOPs are to be ignored - do not propagate them any further 928 skipTo = curOffset + size; 929#ifdef SK_DEVELOPER 930 } else { 931 opIndex++; 932 if (this->preDraw(opIndex, op)) { 933 skipTo = curOffset + size; 934 } 935#endif 936 } 937 938 if (0 != skipTo) { 939 if (it.isValid()) { 940 // If using a bounding box hierarchy, advance the state tree 941 // iterator until at or after skipTo 942 uint32_t adjustedSkipTo; 943 do { 944 adjustedSkipTo = it.draw(); 945 } while (adjustedSkipTo < skipTo); 946 skipTo = adjustedSkipTo; 947 } 948 if (kDrawComplete == skipTo) { 949 break; 950 } 951 reader.setOffset(skipTo); 952 continue; 953 } 954 955 switch (op) { 956 case CLIP_PATH: { 957 const SkPath& path = getPath(reader); 958 uint32_t packed = reader.readInt(); 959 SkRegion::Op regionOp = ClipParams_unpackRegionOp(packed); 960 bool doAA = ClipParams_unpackDoAA(packed); 961 size_t offsetToRestore = reader.readInt(); 962 SkASSERT(!offsetToRestore || \ 963 offsetToRestore >= reader.offset()); 964 canvas.clipPath(path, regionOp, doAA); 965 if (canvas.isClipEmpty() && offsetToRestore) { 966#ifdef SPEW_CLIP_SKIPPING 967 skipPath.recordSkip(offsetToRestore - reader.offset()); 968#endif 969 reader.setOffset(offsetToRestore); 970 } 971 } break; 972 case CLIP_REGION: { 973 SkRegion region; 974 this->getRegion(reader, ®ion); 975 uint32_t packed = reader.readInt(); 976 SkRegion::Op regionOp = ClipParams_unpackRegionOp(packed); 977 size_t offsetToRestore = reader.readInt(); 978 SkASSERT(!offsetToRestore || \ 979 offsetToRestore >= reader.offset()); 980 canvas.clipRegion(region, regionOp); 981 if (canvas.isClipEmpty() && offsetToRestore) { 982#ifdef SPEW_CLIP_SKIPPING 983 skipRegion.recordSkip(offsetToRestore - reader.offset()); 984#endif 985 reader.setOffset(offsetToRestore); 986 } 987 } break; 988 case CLIP_RECT: { 989 const SkRect& rect = reader.skipT<SkRect>(); 990 uint32_t packed = reader.readInt(); 991 SkRegion::Op regionOp = ClipParams_unpackRegionOp(packed); 992 bool doAA = ClipParams_unpackDoAA(packed); 993 size_t offsetToRestore = reader.readInt(); 994 SkASSERT(!offsetToRestore || \ 995 offsetToRestore >= reader.offset()); 996 canvas.clipRect(rect, regionOp, doAA); 997 if (canvas.isClipEmpty() && offsetToRestore) { 998#ifdef SPEW_CLIP_SKIPPING 999 skipRect.recordSkip(offsetToRestore - reader.offset()); 1000#endif 1001 reader.setOffset(offsetToRestore); 1002 } 1003 } break; 1004 case CLIP_RRECT: { 1005 SkRRect rrect; 1006 reader.readRRect(&rrect); 1007 uint32_t packed = reader.readInt(); 1008 SkRegion::Op regionOp = ClipParams_unpackRegionOp(packed); 1009 bool doAA = ClipParams_unpackDoAA(packed); 1010 size_t offsetToRestore = reader.readInt(); 1011 SkASSERT(!offsetToRestore || \ 1012 offsetToRestore >= reader.offset()); 1013 canvas.clipRRect(rrect, regionOp, doAA); 1014 if (canvas.isClipEmpty() && offsetToRestore) { 1015#ifdef SPEW_CLIP_SKIPPING 1016 skipRRect.recordSkip(offsetToRestore - reader.offset()); 1017#endif 1018 reader.setOffset(offsetToRestore); 1019 } 1020 } break; 1021 case PUSH_CULL: { 1022 const SkRect& cullRect = reader.skipT<SkRect>(); 1023 size_t offsetToRestore = reader.readInt(); 1024 if (offsetToRestore && canvas.quickReject(cullRect)) { 1025#ifdef SPEW_CLIP_SKIPPING 1026 skipCull.recordSkip(offsetToRestore - reader.offset()); 1027#endif 1028 reader.setOffset(offsetToRestore); 1029 } else { 1030 canvas.pushCull(cullRect); 1031 } 1032 } break; 1033 case POP_CULL: 1034 canvas.popCull(); 1035 break; 1036 case CONCAT: { 1037 SkMatrix matrix; 1038 this->getMatrix(reader, &matrix); 1039 canvas.concat(matrix); 1040 break; 1041 } 1042 case DRAW_BITMAP: { 1043 const SkPaint* paint = this->getPaint(reader); 1044 const SkBitmap& bitmap = this->getBitmap(reader); 1045 const SkPoint& loc = reader.skipT<SkPoint>(); 1046 canvas.drawBitmap(bitmap, loc.fX, loc.fY, paint); 1047 } break; 1048 case DRAW_BITMAP_RECT_TO_RECT: { 1049 const SkPaint* paint = this->getPaint(reader); 1050 const SkBitmap& bitmap = this->getBitmap(reader); 1051 const SkRect* src = this->getRectPtr(reader); // may be null 1052 const SkRect& dst = reader.skipT<SkRect>(); // required 1053 SkCanvas::DrawBitmapRectFlags flags; 1054 flags = (SkCanvas::DrawBitmapRectFlags) reader.readInt(); 1055 canvas.drawBitmapRectToRect(bitmap, src, dst, paint, flags); 1056 } break; 1057 case DRAW_BITMAP_MATRIX: { 1058 const SkPaint* paint = this->getPaint(reader); 1059 const SkBitmap& bitmap = this->getBitmap(reader); 1060 SkMatrix matrix; 1061 this->getMatrix(reader, &matrix); 1062 canvas.drawBitmapMatrix(bitmap, matrix, paint); 1063 } break; 1064 case DRAW_BITMAP_NINE: { 1065 const SkPaint* paint = this->getPaint(reader); 1066 const SkBitmap& bitmap = this->getBitmap(reader); 1067 const SkIRect& src = reader.skipT<SkIRect>(); 1068 const SkRect& dst = reader.skipT<SkRect>(); 1069 canvas.drawBitmapNine(bitmap, src, dst, paint); 1070 } break; 1071 case DRAW_CLEAR: 1072 canvas.clear(reader.readInt()); 1073 break; 1074 case DRAW_DATA: { 1075 size_t length = reader.readInt(); 1076 canvas.drawData(reader.skip(length), length); 1077 // skip handles padding the read out to a multiple of 4 1078 } break; 1079 case DRAW_DRRECT: { 1080 const SkPaint& paint = *this->getPaint(reader); 1081 SkRRect outer, inner; 1082 reader.readRRect(&outer); 1083 reader.readRRect(&inner); 1084 canvas.drawDRRect(outer, inner, paint); 1085 } break; 1086 case BEGIN_COMMENT_GROUP: { 1087 const char* desc = reader.readString(); 1088 canvas.beginCommentGroup(desc); 1089 } break; 1090 case COMMENT: { 1091 const char* kywd = reader.readString(); 1092 const char* value = reader.readString(); 1093 canvas.addComment(kywd, value); 1094 } break; 1095 case END_COMMENT_GROUP: { 1096 canvas.endCommentGroup(); 1097 } break; 1098 case DRAW_OVAL: { 1099 const SkPaint& paint = *this->getPaint(reader); 1100 canvas.drawOval(reader.skipT<SkRect>(), paint); 1101 } break; 1102 case DRAW_PAINT: 1103 canvas.drawPaint(*this->getPaint(reader)); 1104 break; 1105 case DRAW_PATH: { 1106 const SkPaint& paint = *this->getPaint(reader); 1107 canvas.drawPath(getPath(reader), paint); 1108 } break; 1109 case DRAW_PICTURE: 1110 canvas.drawPicture(this->getPicture(reader)); 1111 break; 1112 case DRAW_POINTS: { 1113 const SkPaint& paint = *this->getPaint(reader); 1114 SkCanvas::PointMode mode = (SkCanvas::PointMode)reader.readInt(); 1115 size_t count = reader.readInt(); 1116 const SkPoint* pts = (const SkPoint*)reader.skip(sizeof(SkPoint) * count); 1117 canvas.drawPoints(mode, count, pts, paint); 1118 } break; 1119 case DRAW_POS_TEXT: { 1120 const SkPaint& paint = *this->getPaint(reader); 1121 getText(reader, &text); 1122 size_t points = reader.readInt(); 1123 const SkPoint* pos = (const SkPoint*)reader.skip(points * sizeof(SkPoint)); 1124 canvas.drawPosText(text.text(), text.length(), pos, paint); 1125 } break; 1126 case DRAW_POS_TEXT_TOP_BOTTOM: { 1127 const SkPaint& paint = *this->getPaint(reader); 1128 getText(reader, &text); 1129 size_t points = reader.readInt(); 1130 const SkPoint* pos = (const SkPoint*)reader.skip(points * sizeof(SkPoint)); 1131 const SkScalar top = reader.readScalar(); 1132 const SkScalar bottom = reader.readScalar(); 1133 if (!canvas.quickRejectY(top, bottom)) { 1134 canvas.drawPosText(text.text(), text.length(), pos, paint); 1135 } 1136 } break; 1137 case DRAW_POS_TEXT_H: { 1138 const SkPaint& paint = *this->getPaint(reader); 1139 getText(reader, &text); 1140 size_t xCount = reader.readInt(); 1141 const SkScalar constY = reader.readScalar(); 1142 const SkScalar* xpos = (const SkScalar*)reader.skip(xCount * sizeof(SkScalar)); 1143 canvas.drawPosTextH(text.text(), text.length(), xpos, constY, 1144 paint); 1145 } break; 1146 case DRAW_POS_TEXT_H_TOP_BOTTOM: { 1147 const SkPaint& paint = *this->getPaint(reader); 1148 getText(reader, &text); 1149 size_t xCount = reader.readInt(); 1150 const SkScalar* xpos = (const SkScalar*)reader.skip((3 + xCount) * sizeof(SkScalar)); 1151 const SkScalar top = *xpos++; 1152 const SkScalar bottom = *xpos++; 1153 const SkScalar constY = *xpos++; 1154 if (!canvas.quickRejectY(top, bottom)) { 1155 canvas.drawPosTextH(text.text(), text.length(), xpos, 1156 constY, paint); 1157 } 1158 } break; 1159 case DRAW_RECT: { 1160 const SkPaint& paint = *this->getPaint(reader); 1161 canvas.drawRect(reader.skipT<SkRect>(), paint); 1162 } break; 1163 case DRAW_RRECT: { 1164 const SkPaint& paint = *this->getPaint(reader); 1165 SkRRect rrect; 1166 reader.readRRect(&rrect); 1167 canvas.drawRRect(rrect, paint); 1168 } break; 1169 case DRAW_SPRITE: { 1170 const SkPaint* paint = this->getPaint(reader); 1171 const SkBitmap& bitmap = this->getBitmap(reader); 1172 int left = reader.readInt(); 1173 int top = reader.readInt(); 1174 canvas.drawSprite(bitmap, left, top, paint); 1175 } break; 1176 case DRAW_TEXT: { 1177 const SkPaint& paint = *this->getPaint(reader); 1178 this->getText(reader, &text); 1179 SkScalar x = reader.readScalar(); 1180 SkScalar y = reader.readScalar(); 1181 canvas.drawText(text.text(), text.length(), x, y, paint); 1182 } break; 1183 case DRAW_TEXT_TOP_BOTTOM: { 1184 const SkPaint& paint = *this->getPaint(reader); 1185 this->getText(reader, &text); 1186 const SkScalar* ptr = (const SkScalar*)reader.skip(4 * sizeof(SkScalar)); 1187 // ptr[0] == x 1188 // ptr[1] == y 1189 // ptr[2] == top 1190 // ptr[3] == bottom 1191 if (!canvas.quickRejectY(ptr[2], ptr[3])) { 1192 canvas.drawText(text.text(), text.length(), ptr[0], ptr[1], 1193 paint); 1194 } 1195 } break; 1196 case DRAW_TEXT_ON_PATH: { 1197 const SkPaint& paint = *this->getPaint(reader); 1198 getText(reader, &text); 1199 const SkPath& path = this->getPath(reader); 1200 SkMatrix matrix; 1201 this->getMatrix(reader, &matrix); 1202 canvas.drawTextOnPath(text.text(), text.length(), path, &matrix, paint); 1203 } break; 1204 case DRAW_VERTICES: { 1205 SkAutoTUnref<SkXfermode> xfer; 1206 const SkPaint& paint = *this->getPaint(reader); 1207 DrawVertexFlags flags = (DrawVertexFlags)reader.readInt(); 1208 SkCanvas::VertexMode vmode = (SkCanvas::VertexMode)reader.readInt(); 1209 int vCount = reader.readInt(); 1210 const SkPoint* verts = (const SkPoint*)reader.skip( 1211 vCount * sizeof(SkPoint)); 1212 const SkPoint* texs = NULL; 1213 const SkColor* colors = NULL; 1214 const uint16_t* indices = NULL; 1215 int iCount = 0; 1216 if (flags & DRAW_VERTICES_HAS_TEXS) { 1217 texs = (const SkPoint*)reader.skip( 1218 vCount * sizeof(SkPoint)); 1219 } 1220 if (flags & DRAW_VERTICES_HAS_COLORS) { 1221 colors = (const SkColor*)reader.skip( 1222 vCount * sizeof(SkColor)); 1223 } 1224 if (flags & DRAW_VERTICES_HAS_INDICES) { 1225 iCount = reader.readInt(); 1226 indices = (const uint16_t*)reader.skip( 1227 iCount * sizeof(uint16_t)); 1228 } 1229 if (flags & DRAW_VERTICES_HAS_XFER) { 1230 int mode = reader.readInt(); 1231 if (mode < 0 || mode > SkXfermode::kLastMode) { 1232 mode = SkXfermode::kModulate_Mode; 1233 } 1234 xfer.reset(SkXfermode::Create((SkXfermode::Mode)mode)); 1235 } 1236 canvas.drawVertices(vmode, vCount, verts, texs, colors, xfer, 1237 indices, iCount, paint); 1238 } break; 1239 case RESTORE: 1240 canvas.restore(); 1241 break; 1242 case ROTATE: 1243 canvas.rotate(reader.readScalar()); 1244 break; 1245 case SAVE: 1246 canvas.save((SkCanvas::SaveFlags) reader.readInt()); 1247 break; 1248 case SAVE_LAYER: { 1249 const SkRect* boundsPtr = this->getRectPtr(reader); 1250 const SkPaint* paint = this->getPaint(reader); 1251 canvas.saveLayer(boundsPtr, paint, (SkCanvas::SaveFlags) reader.readInt()); 1252 } break; 1253 case SCALE: { 1254 SkScalar sx = reader.readScalar(); 1255 SkScalar sy = reader.readScalar(); 1256 canvas.scale(sx, sy); 1257 } break; 1258 case SET_MATRIX: { 1259 SkMatrix matrix; 1260 this->getMatrix(reader, &matrix); 1261 matrix.postConcat(initialMatrix); 1262 canvas.setMatrix(matrix); 1263 } break; 1264 case SKEW: { 1265 SkScalar sx = reader.readScalar(); 1266 SkScalar sy = reader.readScalar(); 1267 canvas.skew(sx, sy); 1268 } break; 1269 case TRANSLATE: { 1270 SkScalar dx = reader.readScalar(); 1271 SkScalar dy = reader.readScalar(); 1272 canvas.translate(dx, dy); 1273 } break; 1274 default: 1275 SkASSERT(0); 1276 } 1277 1278#ifdef SK_DEVELOPER 1279 this->postDraw(opIndex); 1280#endif 1281 1282 if (it.isValid()) { 1283 uint32_t skipTo = it.draw(); 1284 if (kDrawComplete == skipTo) { 1285 break; 1286 } 1287 reader.setOffset(skipTo); 1288 } 1289 } 1290 1291#ifdef SPEW_CLIP_SKIPPING 1292 { 1293 size_t size = skipRect.fSize + skipRRect.fSize + skipPath.fSize + skipRegion.fSize + 1294 skipCull.fSize; 1295 SkDebugf("--- Clip skips %d%% rect:%d rrect:%d path:%d rgn:%d cull:%d\n", 1296 size * 100 / reader.offset(), skipRect.fCount, skipRRect.fCount, 1297 skipPath.fCount, skipRegion.fCount, skipCull.fCount); 1298 SkDebugf("--- Total ops: %d\n", opCount); 1299 } 1300#endif 1301// this->dumpSize(); 1302} 1303 1304/////////////////////////////////////////////////////////////////////////////// 1305 1306#ifdef SK_DEBUG_SIZE 1307int SkPicturePlayback::size(size_t* sizePtr) { 1308 int objects = bitmaps(sizePtr); 1309 objects += paints(sizePtr); 1310 objects += paths(sizePtr); 1311 objects += pictures(sizePtr); 1312 objects += regions(sizePtr); 1313 *sizePtr = fOpData.size(); 1314 return objects; 1315} 1316 1317int SkPicturePlayback::bitmaps(size_t* size) { 1318 size_t result = 0; 1319 for (int index = 0; index < fBitmapCount; index++) { 1320 // const SkBitmap& bitmap = fBitmaps[index]; 1321 result += sizeof(SkBitmap); // bitmap->size(); 1322 } 1323 *size = result; 1324 return fBitmapCount; 1325} 1326 1327int SkPicturePlayback::paints(size_t* size) { 1328 size_t result = 0; 1329 for (int index = 0; index < fPaintCount; index++) { 1330 // const SkPaint& paint = fPaints[index]; 1331 result += sizeof(SkPaint); // paint->size(); 1332 } 1333 *size = result; 1334 return fPaintCount; 1335} 1336 1337int SkPicturePlayback::paths(size_t* size) { 1338 size_t result = 0; 1339 for (int index = 0; index < fPathCount; index++) { 1340 const SkPath& path = fPaths[index]; 1341 result += path.flatten(NULL); 1342 } 1343 *size = result; 1344 return fPathCount; 1345} 1346#endif 1347 1348#ifdef SK_DEBUG_DUMP 1349void SkPicturePlayback::dumpBitmap(const SkBitmap& bitmap) const { 1350 char pBuffer[DUMP_BUFFER_SIZE]; 1351 char* bufferPtr = pBuffer; 1352 bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), 1353 "BitmapData bitmap%p = {", &bitmap); 1354 bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), 1355 "{kWidth, %d}, ", bitmap.width()); 1356 bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), 1357 "{kHeight, %d}, ", bitmap.height()); 1358 bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), 1359 "{kRowBytes, %d}, ", bitmap.rowBytes()); 1360// start here; 1361 SkDebugf("%s{0}};\n", pBuffer); 1362} 1363 1364void dumpMatrix(const SkMatrix& matrix) const { 1365 SkMatrix defaultMatrix; 1366 defaultMatrix.reset(); 1367 char pBuffer[DUMP_BUFFER_SIZE]; 1368 char* bufferPtr = pBuffer; 1369 bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), 1370 "MatrixData matrix%p = {", &matrix); 1371 SkScalar scaleX = matrix.getScaleX(); 1372 if (scaleX != defaultMatrix.getScaleX()) 1373 bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), 1374 "{kScaleX, %g}, ", SkScalarToFloat(scaleX)); 1375 SkScalar scaleY = matrix.getScaleY(); 1376 if (scaleY != defaultMatrix.getScaleY()) 1377 bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), 1378 "{kScaleY, %g}, ", SkScalarToFloat(scaleY)); 1379 SkScalar skewX = matrix.getSkewX(); 1380 if (skewX != defaultMatrix.getSkewX()) 1381 bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), 1382 "{kSkewX, %g}, ", SkScalarToFloat(skewX)); 1383 SkScalar skewY = matrix.getSkewY(); 1384 if (skewY != defaultMatrix.getSkewY()) 1385 bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), 1386 "{kSkewY, %g}, ", SkScalarToFloat(skewY)); 1387 SkScalar translateX = matrix.getTranslateX(); 1388 if (translateX != defaultMatrix.getTranslateX()) 1389 bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), 1390 "{kTranslateX, %g}, ", SkScalarToFloat(translateX)); 1391 SkScalar translateY = matrix.getTranslateY(); 1392 if (translateY != defaultMatrix.getTranslateY()) 1393 bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), 1394 "{kTranslateY, %g}, ", SkScalarToFloat(translateY)); 1395 SkScalar perspX = matrix.getPerspX(); 1396 if (perspX != defaultMatrix.getPerspX()) 1397 bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), 1398 "{kPerspX, %g}, ", perspX); 1399 SkScalar perspY = matrix.getPerspY(); 1400 if (perspY != defaultMatrix.getPerspY()) 1401 bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), 1402 "{kPerspY, %g}, ", perspY); 1403 SkDebugf("%s{0}};\n", pBuffer); 1404} 1405 1406void dumpPaint(const SkPaint& paint) const { 1407 SkPaint defaultPaint; 1408 char pBuffer[DUMP_BUFFER_SIZE]; 1409 char* bufferPtr = pBuffer; 1410 bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), 1411 "PaintPointers paintPtrs%p = {", &paint); 1412 const SkTypeface* typeface = paint.getTypeface(); 1413 if (typeface != defaultPaint.getTypeface()) 1414 bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), 1415 "{kTypeface, %p}, ", typeface); 1416 const SkPathEffect* pathEffect = paint.getPathEffect(); 1417 if (pathEffect != defaultPaint.getPathEffect()) 1418 bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), 1419 "{kPathEffect, %p}, ", pathEffect); 1420 const SkShader* shader = paint.getShader(); 1421 if (shader != defaultPaint.getShader()) 1422 bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), 1423 "{kShader, %p}, ", shader); 1424 const SkXfermode* xfermode = paint.getXfermode(); 1425 if (xfermode != defaultPaint.getXfermode()) 1426 bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), 1427 "{kXfermode, %p}, ", xfermode); 1428 const SkMaskFilter* maskFilter = paint.getMaskFilter(); 1429 if (maskFilter != defaultPaint.getMaskFilter()) 1430 bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), 1431 "{kMaskFilter, %p}, ", maskFilter); 1432 const SkColorFilter* colorFilter = paint.getColorFilter(); 1433 if (colorFilter != defaultPaint.getColorFilter()) 1434 bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), 1435 "{kColorFilter, %p}, ", colorFilter); 1436 const SkRasterizer* rasterizer = paint.getRasterizer(); 1437 if (rasterizer != defaultPaint.getRasterizer()) 1438 bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), 1439 "{kRasterizer, %p}, ", rasterizer); 1440 const SkDrawLooper* drawLooper = paint.getLooper(); 1441 if (drawLooper != defaultPaint.getLooper()) 1442 bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), 1443 "{kDrawLooper, %p}, ", drawLooper); 1444 SkDebugf("%s{0}};\n", pBuffer); 1445 bufferPtr = pBuffer; 1446 bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), 1447 "PaintScalars paintScalars%p = {", &paint); 1448 SkScalar textSize = paint.getTextSize(); 1449 if (textSize != defaultPaint.getTextSize()) 1450 bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), 1451 "{kTextSize, %g}, ", SkScalarToFloat(textSize)); 1452 SkScalar textScaleX = paint.getTextScaleX(); 1453 if (textScaleX != defaultPaint.getTextScaleX()) 1454 bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), 1455 "{kTextScaleX, %g}, ", SkScalarToFloat(textScaleX)); 1456 SkScalar textSkewX = paint.getTextSkewX(); 1457 if (textSkewX != defaultPaint.getTextSkewX()) 1458 bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), 1459 "{kTextSkewX, %g}, ", SkScalarToFloat(textSkewX)); 1460 SkScalar strokeWidth = paint.getStrokeWidth(); 1461 if (strokeWidth != defaultPaint.getStrokeWidth()) 1462 bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), 1463 "{kStrokeWidth, %g}, ", SkScalarToFloat(strokeWidth)); 1464 SkScalar strokeMiter = paint.getStrokeMiter(); 1465 if (strokeMiter != defaultPaint.getStrokeMiter()) 1466 bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), 1467 "{kStrokeMiter, %g}, ", SkScalarToFloat(strokeMiter)); 1468 SkDebugf("%s{0}};\n", pBuffer); 1469 bufferPtr = pBuffer; 1470 bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), 1471 "PaintInts = paintInts%p = {", &paint); 1472 unsigned color = paint.getColor(); 1473 if (color != defaultPaint.getColor()) 1474 bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), 1475 "{kColor, 0x%x}, ", color); 1476 unsigned flags = paint.getFlags(); 1477 if (flags != defaultPaint.getFlags()) 1478 bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), 1479 "{kFlags, 0x%x}, ", flags); 1480 int align = paint.getTextAlign(); 1481 if (align != defaultPaint.getTextAlign()) 1482 bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), 1483 "{kAlign, 0x%x}, ", align); 1484 int strokeCap = paint.getStrokeCap(); 1485 if (strokeCap != defaultPaint.getStrokeCap()) 1486 bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), 1487 "{kStrokeCap, 0x%x}, ", strokeCap); 1488 int strokeJoin = paint.getStrokeJoin(); 1489 if (strokeJoin != defaultPaint.getStrokeJoin()) 1490 bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), 1491 "{kAlign, 0x%x}, ", strokeJoin); 1492 int style = paint.getStyle(); 1493 if (style != defaultPaint.getStyle()) 1494 bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), 1495 "{kStyle, 0x%x}, ", style); 1496 int textEncoding = paint.getTextEncoding(); 1497 if (textEncoding != defaultPaint.getTextEncoding()) 1498 bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), 1499 "{kTextEncoding, 0x%x}, ", textEncoding); 1500 SkDebugf("%s{0}};\n", pBuffer); 1501 1502 SkDebugf("PaintData paint%p = {paintPtrs%p, paintScalars%p, paintInts%p};\n", 1503 &paint, &paint, &paint, &paint); 1504} 1505 1506void SkPicturePlayback::dumpPath(const SkPath& path) const { 1507 SkDebugf("path dump unimplemented\n"); 1508} 1509 1510void SkPicturePlayback::dumpPicture(const SkPicture& picture) const { 1511 SkDebugf("picture dump unimplemented\n"); 1512} 1513 1514void SkPicturePlayback::dumpRegion(const SkRegion& region) const { 1515 SkDebugf("region dump unimplemented\n"); 1516} 1517 1518int SkPicturePlayback::dumpDrawType(char* bufferPtr, char* buffer, DrawType drawType) { 1519 return snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - buffer), 1520 "k%s, ", DrawTypeToString(drawType)); 1521} 1522 1523int SkPicturePlayback::dumpInt(char* bufferPtr, char* buffer, char* name) { 1524 return snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - buffer), 1525 "%s:%d, ", name, getInt()); 1526} 1527 1528int SkPicturePlayback::dumpRect(char* bufferPtr, char* buffer, char* name) { 1529 const SkRect* rect = fReader.skipRect(); 1530 return snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - buffer), 1531 "%s:{l:%g t:%g r:%g b:%g}, ", name, SkScalarToFloat(rect.fLeft), 1532 SkScalarToFloat(rect.fTop), 1533 SkScalarToFloat(rect.fRight), SkScalarToFloat(rect.fBottom)); 1534} 1535 1536int SkPicturePlayback::dumpPoint(char* bufferPtr, char* buffer, char* name) { 1537 SkPoint pt; 1538 getPoint(&pt); 1539 return snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - buffer), 1540 "%s:{x:%g y:%g}, ", name, SkScalarToFloat(pt.fX), 1541 SkScalarToFloat(pt.fY)); 1542} 1543 1544void SkPicturePlayback::dumpPointArray(char** bufferPtrPtr, char* buffer, int count) { 1545 char* bufferPtr = *bufferPtrPtr; 1546 const SkPoint* pts = (const SkPoint*)fReadStream.getAtPos(); 1547 fReadStream.skip(sizeof(SkPoint) * count); 1548 bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - buffer), 1549 "count:%d {", count); 1550 for (int index = 0; index < count; index++) 1551 bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - buffer), 1552 "{x:%g y:%g}, ", SkScalarToFloat(pts[index].fX), 1553 SkScalarToFloat(pts[index].fY)); 1554 bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - buffer), 1555 "} "); 1556 *bufferPtrPtr = bufferPtr; 1557} 1558 1559int SkPicturePlayback::dumpPtr(char* bufferPtr, char* buffer, char* name, void* ptr) { 1560 return snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - buffer), 1561 "%s:%p, ", name, ptr); 1562} 1563 1564int SkPicturePlayback::dumpRectPtr(char* bufferPtr, char* buffer, char* name) { 1565 char result; 1566 fReadStream.read(&result, sizeof(result)); 1567 if (result) 1568 return dumpRect(bufferPtr, buffer, name); 1569 else 1570 return snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - buffer), 1571 "%s:NULL, ", name); 1572} 1573 1574int SkPicturePlayback::dumpScalar(char* bufferPtr, char* buffer, char* name) { 1575 return snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - buffer), 1576 "%s:%d, ", name, getScalar()); 1577} 1578 1579void SkPicturePlayback::dumpText(char** bufferPtrPtr, char* buffer) { 1580 char* bufferPtr = *bufferPtrPtr; 1581 int length = getInt(); 1582 bufferPtr += dumpDrawType(bufferPtr, buffer); 1583 fReadStream.skipToAlign4(); 1584 char* text = (char*) fReadStream.getAtPos(); 1585 fReadStream.skip(length); 1586 bufferPtr += dumpInt(bufferPtr, buffer, "length"); 1587 int limit = DUMP_BUFFER_SIZE - (bufferPtr - buffer) - 2; 1588 length >>= 1; 1589 if (limit > length) 1590 limit = length; 1591 if (limit > 0) { 1592 *bufferPtr++ = '"'; 1593 for (int index = 0; index < limit; index++) { 1594 *bufferPtr++ = *(unsigned short*) text; 1595 text += sizeof(unsigned short); 1596 } 1597 *bufferPtr++ = '"'; 1598 } 1599 *bufferPtrPtr = bufferPtr; 1600} 1601 1602#define DUMP_DRAWTYPE(drawType) \ 1603 bufferPtr += dumpDrawType(bufferPtr, buffer, drawType) 1604 1605#define DUMP_INT(name) \ 1606 bufferPtr += dumpInt(bufferPtr, buffer, #name) 1607 1608#define DUMP_RECT_PTR(name) \ 1609 bufferPtr += dumpRectPtr(bufferPtr, buffer, #name) 1610 1611#define DUMP_POINT(name) \ 1612 bufferPtr += dumpRect(bufferPtr, buffer, #name) 1613 1614#define DUMP_RECT(name) \ 1615 bufferPtr += dumpRect(bufferPtr, buffer, #name) 1616 1617#define DUMP_POINT_ARRAY(count) \ 1618 dumpPointArray(&bufferPtr, buffer, count) 1619 1620#define DUMP_PTR(name, ptr) \ 1621 bufferPtr += dumpPtr(bufferPtr, buffer, #name, (void*) ptr) 1622 1623#define DUMP_SCALAR(name) \ 1624 bufferPtr += dumpScalar(bufferPtr, buffer, #name) 1625 1626#define DUMP_TEXT() \ 1627 dumpText(&bufferPtr, buffer) 1628 1629void SkPicturePlayback::dumpStream() { 1630 SkDebugf("RecordStream stream = {\n"); 1631 DrawType drawType; 1632 TextContainer text; 1633 fReadStream.rewind(); 1634 char buffer[DUMP_BUFFER_SIZE], * bufferPtr; 1635 while (fReadStream.read(&drawType, sizeof(drawType))) { 1636 bufferPtr = buffer; 1637 DUMP_DRAWTYPE(drawType); 1638 switch (drawType) { 1639 case CLIP_PATH: { 1640 DUMP_PTR(SkPath, &getPath()); 1641 DUMP_INT(SkRegion::Op); 1642 DUMP_INT(offsetToRestore); 1643 } break; 1644 case CLIP_REGION: { 1645 DUMP_INT(SkRegion::Op); 1646 DUMP_INT(offsetToRestore); 1647 } break; 1648 case CLIP_RECT: { 1649 DUMP_RECT(rect); 1650 DUMP_INT(SkRegion::Op); 1651 DUMP_INT(offsetToRestore); 1652 } break; 1653 case CONCAT: 1654 break; 1655 case DRAW_BITMAP: { 1656 DUMP_PTR(SkPaint, getPaint()); 1657 DUMP_PTR(SkBitmap, &getBitmap()); 1658 DUMP_SCALAR(left); 1659 DUMP_SCALAR(top); 1660 } break; 1661 case DRAW_PAINT: 1662 DUMP_PTR(SkPaint, getPaint()); 1663 break; 1664 case DRAW_PATH: { 1665 DUMP_PTR(SkPaint, getPaint()); 1666 DUMP_PTR(SkPath, &getPath()); 1667 } break; 1668 case DRAW_PICTURE: { 1669 DUMP_PTR(SkPicture, &getPicture()); 1670 } break; 1671 case DRAW_POINTS: { 1672 DUMP_PTR(SkPaint, getPaint()); 1673 (void)getInt(); // PointMode 1674 size_t count = getInt(); 1675 fReadStream.skipToAlign4(); 1676 DUMP_POINT_ARRAY(count); 1677 } break; 1678 case DRAW_POS_TEXT: { 1679 DUMP_PTR(SkPaint, getPaint()); 1680 DUMP_TEXT(); 1681 size_t points = getInt(); 1682 fReadStream.skipToAlign4(); 1683 DUMP_POINT_ARRAY(points); 1684 } break; 1685 case DRAW_POS_TEXT_H: { 1686 DUMP_PTR(SkPaint, getPaint()); 1687 DUMP_TEXT(); 1688 size_t points = getInt(); 1689 fReadStream.skipToAlign4(); 1690 DUMP_SCALAR(top); 1691 DUMP_SCALAR(bottom); 1692 DUMP_SCALAR(constY); 1693 DUMP_POINT_ARRAY(points); 1694 } break; 1695 case DRAW_RECT: { 1696 DUMP_PTR(SkPaint, getPaint()); 1697 DUMP_RECT(rect); 1698 } break; 1699 case DRAW_SPRITE: { 1700 DUMP_PTR(SkPaint, getPaint()); 1701 DUMP_PTR(SkBitmap, &getBitmap()); 1702 DUMP_SCALAR(left); 1703 DUMP_SCALAR(top); 1704 } break; 1705 case DRAW_TEXT: { 1706 DUMP_PTR(SkPaint, getPaint()); 1707 DUMP_TEXT(); 1708 DUMP_SCALAR(x); 1709 DUMP_SCALAR(y); 1710 } break; 1711 case DRAW_TEXT_ON_PATH: { 1712 DUMP_PTR(SkPaint, getPaint()); 1713 DUMP_TEXT(); 1714 DUMP_PTR(SkPath, &getPath()); 1715 } break; 1716 case RESTORE: 1717 break; 1718 case ROTATE: 1719 DUMP_SCALAR(rotate); 1720 break; 1721 case SAVE: 1722 DUMP_INT(SkCanvas::SaveFlags); 1723 break; 1724 case SAVE_LAYER: { 1725 DUMP_RECT_PTR(layer); 1726 DUMP_PTR(SkPaint, getPaint()); 1727 DUMP_INT(SkCanvas::SaveFlags); 1728 } break; 1729 case SCALE: { 1730 DUMP_SCALAR(sx); 1731 DUMP_SCALAR(sy); 1732 } break; 1733 case SKEW: { 1734 DUMP_SCALAR(sx); 1735 DUMP_SCALAR(sy); 1736 } break; 1737 case TRANSLATE: { 1738 DUMP_SCALAR(dx); 1739 DUMP_SCALAR(dy); 1740 } break; 1741 default: 1742 SkASSERT(0); 1743 } 1744 SkDebugf("%s\n", buffer); 1745 } 1746} 1747 1748void SkPicturePlayback::dump() const { 1749 char pBuffer[DUMP_BUFFER_SIZE]; 1750 char* bufferPtr = pBuffer; 1751 int index; 1752 if (fBitmapCount > 0) 1753 SkDebugf("// bitmaps (%d)\n", fBitmapCount); 1754 for (index = 0; index < fBitmapCount; index++) { 1755 const SkBitmap& bitmap = fBitmaps[index]; 1756 dumpBitmap(bitmap); 1757 } 1758 if (fBitmapCount > 0) 1759 bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), 1760 "Bitmaps bitmaps = {"); 1761 for (index = 0; index < fBitmapCount; index++) 1762 bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), 1763 "bitmap%p, ", &fBitmaps[index]); 1764 if (fBitmapCount > 0) 1765 SkDebugf("%s0};\n", pBuffer); 1766 1767 1768 if (fPaintCount > 0) 1769 SkDebugf("// paints (%d)\n", fPaintCount); 1770 for (index = 0; index < fPaintCount; index++) { 1771 const SkPaint& paint = fPaints[index]; 1772 dumpPaint(paint); 1773 } 1774 bufferPtr = pBuffer; 1775 if (fPaintCount > 0) 1776 bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), 1777 "Paints paints = {"); 1778 for (index = 0; index < fPaintCount; index++) 1779 bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), 1780 "paint%p, ", &fPaints[index]); 1781 if (fPaintCount > 0) 1782 SkDebugf("%s0};\n", pBuffer); 1783 1784 for (index = 0; index < fPathCount; index++) { 1785 const SkPath& path = fPaths[index]; 1786 dumpPath(path); 1787 } 1788 bufferPtr = pBuffer; 1789 if (fPathCount > 0) 1790 bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), 1791 "Paths paths = {"); 1792 for (index = 0; index < fPathCount; index++) 1793 bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), 1794 "path%p, ", &fPaths[index]); 1795 if (fPathCount > 0) 1796 SkDebugf("%s0};\n", pBuffer); 1797 1798 for (index = 0; index < fPictureCount; index++) { 1799 dumpPicture(*fPictureRefs[index]); 1800 } 1801 bufferPtr = pBuffer; 1802 if (fPictureCount > 0) 1803 bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), 1804 "Pictures pictures = {"); 1805 for (index = 0; index < fPictureCount; index++) 1806 bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), 1807 "picture%p, ", fPictureRefs[index]); 1808 if (fPictureCount > 0) 1809 SkDebugf("%s0};\n", pBuffer); 1810 1811 const_cast<SkPicturePlayback*>(this)->dumpStream(); 1812} 1813 1814#endif 1815