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