SkPicturePlayback.cpp revision 75cf29be4d3bcda4ce4118facec574242d4cc106
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 "SkOffsetTable.h"
10#include "SkPicturePlayback.h"
11#include "SkPictureRecord.h"
12#include "SkPictureStateTree.h"
13#include "SkReadBuffer.h"
14#include "SkTypeface.h"
15#include "SkTSort.h"
16#include "SkWriteBuffer.h"
17
18template <typename T> int SafeCount(const T* obj) {
19    return obj ? obj->count() : 0;
20}
21
22/*  Define this to spew out a debug statement whenever we skip the remainder of
23    a save/restore block because a clip... command returned false (empty).
24 */
25#define SPEW_CLIP_SKIPPINGx
26
27SkPicturePlayback::SkPicturePlayback() {
28    this->init();
29}
30
31SkPicturePlayback::SkPicturePlayback(const SkPictureRecord& record, bool deepCopy) {
32#ifdef SK_DEBUG_SIZE
33    size_t overallBytes, bitmapBytes, matricesBytes,
34    paintBytes, pathBytes, pictureBytes, regionBytes;
35    int bitmaps = record.bitmaps(&bitmapBytes);
36    int matrices = record.matrices(&matricesBytes);
37    int paints = record.paints(&paintBytes);
38    int paths = record.paths(&pathBytes);
39    int pictures = record.pictures(&pictureBytes);
40    int regions = record.regions(&regionBytes);
41    SkDebugf("picture record mem used %zd (stream %zd) ", record.size(),
42             record.streamlen());
43    if (bitmaps != 0)
44        SkDebugf("bitmaps size %zd (bitmaps:%d) ", bitmapBytes, bitmaps);
45    if (matrices != 0)
46        SkDebugf("matrices size %zd (matrices:%d) ", matricesBytes, matrices);
47    if (paints != 0)
48        SkDebugf("paints size %zd (paints:%d) ", paintBytes, paints);
49    if (paths != 0)
50        SkDebugf("paths size %zd (paths:%d) ", pathBytes, paths);
51    if (pictures != 0)
52        SkDebugf("pictures size %zd (pictures:%d) ", pictureBytes, pictures);
53    if (regions != 0)
54        SkDebugf("regions size %zd (regions:%d) ", regionBytes, regions);
55    if (record.fPointWrites != 0)
56        SkDebugf("points size %zd (points:%d) ", record.fPointBytes, record.fPointWrites);
57    if (record.fRectWrites != 0)
58        SkDebugf("rects size %zd (rects:%d) ", record.fRectBytes, record.fRectWrites);
59    if (record.fTextWrites != 0)
60        SkDebugf("text size %zd (text strings:%d) ", record.fTextBytes, record.fTextWrites);
61
62    SkDebugf("\n");
63#endif
64#ifdef SK_DEBUG_DUMP
65    record.dumpMatrices();
66    record.dumpPaints();
67#endif
68
69    record.validate(record.writeStream().bytesWritten(), 0);
70    const SkWriter32& writer = record.writeStream();
71    this->init();
72    SkASSERT(!fOpData);
73    if (writer.bytesWritten() == 0) {
74        fOpData = SkData::NewEmpty();
75        return;
76    }
77    fOpData = writer.snapshotAsData();
78
79    fBoundingHierarchy = record.fBoundingHierarchy;
80    fStateTree = record.fStateTree;
81
82    SkSafeRef(fBoundingHierarchy);
83    SkSafeRef(fStateTree);
84
85    if (NULL != fBoundingHierarchy) {
86        fBoundingHierarchy->flushDeferredInserts();
87    }
88
89    // copy over the refcnt dictionary to our reader
90    record.fFlattenableHeap.setupPlaybacks();
91
92    fBitmaps = record.fBitmapHeap->extractBitmaps();
93    fPaints = record.fPaints.unflattenToArray();
94
95    fBitmapHeap.reset(SkSafeRef(record.fBitmapHeap));
96    fPathHeap.reset(SkSafeRef(record.fPathHeap));
97
98    fBitmapUseOffsets.reset(SkSafeRef(record.fBitmapUseOffsets.get()));
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     */
152
153    return paint.getPathEffect() ||
154           paint.getShader() ||
155           paint.getMaskFilter() ||
156           paint.getRasterizer() ||
157           paint.getLooper() ||
158           paint.getImageFilter();
159}
160
161SkPicturePlayback::SkPicturePlayback(const SkPicturePlayback& src, SkPictCopyInfo* deepCopyInfo) {
162    this->init();
163
164    fBitmapHeap.reset(SkSafeRef(src.fBitmapHeap.get()));
165    fPathHeap.reset(SkSafeRef(src.fPathHeap.get()));
166
167    fOpData = SkSafeRef(src.fOpData);
168
169    fBoundingHierarchy = src.fBoundingHierarchy;
170    fStateTree = src.fStateTree;
171
172    SkSafeRef(fBoundingHierarchy);
173    SkSafeRef(fStateTree);
174
175    if (deepCopyInfo) {
176        int paintCount = SafeCount(src.fPaints);
177
178        if (src.fBitmaps) {
179            fBitmaps = SkTRefArray<SkBitmap>::Create(src.fBitmaps->begin(), src.fBitmaps->count());
180        }
181
182        if (!deepCopyInfo->initialized) {
183            /* The alternative to doing this is to have a clone method on the paint and have it make
184             * the deep copy of its internal structures as needed. The holdup to doing that is at
185             * this point we would need to pass the SkBitmapHeap so that we don't unnecessarily
186             * flatten the pixels in a bitmap shader.
187             */
188            deepCopyInfo->paintData.setCount(paintCount);
189
190            /* Use an SkBitmapHeap to avoid flattening bitmaps in shaders. If there already is one,
191             * use it. If this SkPicturePlayback was created from a stream, fBitmapHeap will be
192             * NULL, so create a new one.
193             */
194            if (fBitmapHeap.get() == NULL) {
195                // FIXME: Put this on the stack inside SkPicture::clone. Further, is it possible to
196                // do the rest of this initialization in SkPicture::clone as well?
197                SkBitmapHeap* heap = SkNEW(SkBitmapHeap);
198                deepCopyInfo->controller.setBitmapStorage(heap);
199                heap->unref();
200            } else {
201                deepCopyInfo->controller.setBitmapStorage(fBitmapHeap);
202            }
203
204            SkDEBUGCODE(int heapSize = SafeCount(fBitmapHeap.get());)
205            for (int i = 0; i < paintCount; i++) {
206                if (needs_deep_copy(src.fPaints->at(i))) {
207                    deepCopyInfo->paintData[i] =
208                        SkFlatData::Create<SkPaint::FlatteningTraits>(&deepCopyInfo->controller,
209                                                          src.fPaints->at(i), 0);
210
211                } else {
212                    // this is our sentinel, which we use in the unflatten loop
213                    deepCopyInfo->paintData[i] = NULL;
214                }
215            }
216            SkASSERT(SafeCount(fBitmapHeap.get()) == heapSize);
217
218            // needed to create typeface playback
219            deepCopyInfo->controller.setupPlaybacks();
220            deepCopyInfo->initialized = true;
221        }
222
223        fPaints = SkTRefArray<SkPaint>::Create(paintCount);
224        SkASSERT(deepCopyInfo->paintData.count() == paintCount);
225        SkBitmapHeap* bmHeap = deepCopyInfo->controller.getBitmapHeap();
226        SkTypefacePlayback* tfPlayback = deepCopyInfo->controller.getTypefacePlayback();
227        for (int i = 0; i < paintCount; i++) {
228            if (deepCopyInfo->paintData[i]) {
229                deepCopyInfo->paintData[i]->unflatten<SkPaint::FlatteningTraits>(
230                    &fPaints->writableAt(i), bmHeap, tfPlayback);
231            } else {
232                // needs_deep_copy was false, so just need to assign
233                fPaints->writableAt(i) = src.fPaints->at(i);
234            }
235        }
236
237    } else {
238        fBitmaps = SkSafeRef(src.fBitmaps);
239        fPaints = SkSafeRef(src.fPaints);
240    }
241
242    fPictureCount = src.fPictureCount;
243    fPictureRefs = SkNEW_ARRAY(SkPicture*, fPictureCount);
244    for (int i = 0; i < fPictureCount; i++) {
245        if (deepCopyInfo) {
246            fPictureRefs[i] = src.fPictureRefs[i]->clone();
247        } else {
248            fPictureRefs[i] = src.fPictureRefs[i];
249            fPictureRefs[i]->ref();
250        }
251    }
252}
253
254void SkPicturePlayback::init() {
255    fBitmaps = NULL;
256    fPaints = NULL;
257    fPictureRefs = NULL;
258    fPictureCount = 0;
259    fOpData = NULL;
260    fFactoryPlayback = NULL;
261    fBoundingHierarchy = NULL;
262    fStateTree = NULL;
263    fCachedActiveOps = NULL;
264    fCurOffset = 0;
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    if (0 != fCachedActiveOps->fOps.count()) {
833        SkTQSort<SkPictureStateTree::Draw>(
834            reinterpret_cast<SkPictureStateTree::Draw**>(fCachedActiveOps->fOps.begin()),
835            reinterpret_cast<SkPictureStateTree::Draw**>(fCachedActiveOps->fOps.end()-1));
836    }
837
838    fCachedActiveOps->fCacheQueryRect = query;
839    return *fCachedActiveOps;
840}
841
842class SkAutoResetOpID {
843public:
844    SkAutoResetOpID(SkPicturePlayback* playback) : fPlayback(playback) { }
845    ~SkAutoResetOpID() {
846        if (NULL != fPlayback) {
847            fPlayback->resetOpID();
848        }
849    }
850
851private:
852    SkPicturePlayback* fPlayback;
853};
854
855void SkPicturePlayback::draw(SkCanvas& canvas, SkDrawPictureCallback* callback) {
856    SkAutoResetOpID aroi(this);
857    SkASSERT(0 == fCurOffset);
858
859#ifdef ENABLE_TIME_DRAW
860    SkAutoTime  at("SkPicture::draw", 50);
861#endif
862
863#ifdef SPEW_CLIP_SKIPPING
864    SkipClipRec skipRect, skipRRect, skipRegion, skipPath, skipCull;
865    int opCount = 0;
866#endif
867
868#ifdef SK_BUILD_FOR_ANDROID
869    SkAutoMutexAcquire autoMutex(fDrawMutex);
870#endif
871
872    // kDrawComplete will be the signal that we have reached the end of
873    // the command stream
874    static const uint32_t kDrawComplete = SK_MaxU32;
875
876    SkReader32 reader(fOpData->bytes(), fOpData->size());
877    TextContainer text;
878    const SkTDArray<void*>* activeOps = NULL;
879
880    if (NULL != fStateTree && NULL != fBoundingHierarchy) {
881        SkRect clipBounds;
882        if (canvas.getClipBounds(&clipBounds)) {
883            SkIRect query;
884            clipBounds.roundOut(&query);
885
886            const SkPicture::OperationList& activeOpsList = this->getActiveOps(query);
887            if (activeOpsList.valid()) {
888                if (0 == activeOpsList.numOps()) {
889                    return;     // nothing to draw
890                }
891
892                // Since the opList is valid we know it is our derived class
893                activeOps = &((const CachedOperationList&)activeOpsList).fOps;
894            }
895        }
896    }
897
898    SkPictureStateTree::Iterator it = (NULL == activeOps) ?
899        SkPictureStateTree::Iterator() :
900        fStateTree->getIterator(*activeOps, &canvas);
901
902    if (it.isValid()) {
903        uint32_t skipTo = it.draw();
904        if (kDrawComplete == skipTo) {
905            return;
906        }
907        reader.setOffset(skipTo);
908    }
909
910    this->preLoadBitmaps(activeOps);
911
912    // Record this, so we can concat w/ it if we encounter a setMatrix()
913    SkMatrix initialMatrix = canvas.getTotalMatrix();
914    int originalSaveCount = canvas.getSaveCount();
915
916#ifdef SK_BUILD_FOR_ANDROID
917    fAbortCurrentPlayback = false;
918#endif
919
920#ifdef SK_DEVELOPER
921    int opIndex = -1;
922#endif
923
924    while (!reader.eof()) {
925        if (callback && callback->abortDrawing()) {
926            canvas.restoreToCount(originalSaveCount);
927            return;
928        }
929#ifdef SK_BUILD_FOR_ANDROID
930        if (fAbortCurrentPlayback) {
931            return;
932        }
933#endif
934
935#ifdef SPEW_CLIP_SKIPPING
936        opCount++;
937#endif
938
939        fCurOffset = reader.offset();
940        uint32_t size;
941        DrawType op = read_op_and_size(&reader, &size);
942        size_t skipTo = 0;
943        if (NOOP == op) {
944            // NOOPs are to be ignored - do not propagate them any further
945            skipTo = fCurOffset + size;
946#ifdef SK_DEVELOPER
947        } else {
948            opIndex++;
949            if (this->preDraw(opIndex, op)) {
950                skipTo = fCurOffset + size;
951            }
952#endif
953        }
954
955        if (0 != skipTo) {
956            if (it.isValid()) {
957                // If using a bounding box hierarchy, advance the state tree
958                // iterator until at or after skipTo
959                uint32_t adjustedSkipTo;
960                do {
961                    adjustedSkipTo = it.draw();
962                } while (adjustedSkipTo < skipTo);
963                skipTo = adjustedSkipTo;
964            }
965            if (kDrawComplete == skipTo) {
966                break;
967            }
968            reader.setOffset(skipTo);
969            continue;
970        }
971
972        switch (op) {
973            case CLIP_PATH: {
974                const SkPath& path = getPath(reader);
975                uint32_t packed = reader.readInt();
976                SkRegion::Op regionOp = ClipParams_unpackRegionOp(packed);
977                bool doAA = ClipParams_unpackDoAA(packed);
978                size_t offsetToRestore = reader.readInt();
979                SkASSERT(!offsetToRestore || \
980                    offsetToRestore >= reader.offset());
981                canvas.clipPath(path, regionOp, doAA);
982                if (canvas.isClipEmpty() && offsetToRestore) {
983#ifdef SPEW_CLIP_SKIPPING
984                    skipPath.recordSkip(offsetToRestore - reader.offset());
985#endif
986                    reader.setOffset(offsetToRestore);
987                }
988            } break;
989            case CLIP_REGION: {
990                SkRegion region;
991                this->getRegion(reader, &region);
992                uint32_t packed = reader.readInt();
993                SkRegion::Op regionOp = ClipParams_unpackRegionOp(packed);
994                size_t offsetToRestore = reader.readInt();
995                SkASSERT(!offsetToRestore || \
996                    offsetToRestore >= reader.offset());
997                canvas.clipRegion(region, regionOp);
998                if (canvas.isClipEmpty() && offsetToRestore) {
999#ifdef SPEW_CLIP_SKIPPING
1000                    skipRegion.recordSkip(offsetToRestore - reader.offset());
1001#endif
1002                    reader.setOffset(offsetToRestore);
1003                }
1004            } break;
1005            case CLIP_RECT: {
1006                const SkRect& rect = reader.skipT<SkRect>();
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.clipRect(rect, regionOp, doAA);
1014                if (canvas.isClipEmpty() && offsetToRestore) {
1015#ifdef SPEW_CLIP_SKIPPING
1016                    skipRect.recordSkip(offsetToRestore - reader.offset());
1017#endif
1018                    reader.setOffset(offsetToRestore);
1019                }
1020            } break;
1021            case CLIP_RRECT: {
1022                SkRRect rrect;
1023                reader.readRRect(&rrect);
1024                uint32_t packed = reader.readInt();
1025                SkRegion::Op regionOp = ClipParams_unpackRegionOp(packed);
1026                bool doAA = ClipParams_unpackDoAA(packed);
1027                size_t offsetToRestore = reader.readInt();
1028                SkASSERT(!offsetToRestore || \
1029                         offsetToRestore >= reader.offset());
1030                canvas.clipRRect(rrect, regionOp, doAA);
1031                if (canvas.isClipEmpty() && offsetToRestore) {
1032#ifdef SPEW_CLIP_SKIPPING
1033                    skipRRect.recordSkip(offsetToRestore - reader.offset());
1034#endif
1035                    reader.setOffset(offsetToRestore);
1036                }
1037            } break;
1038            case PUSH_CULL: {
1039                const SkRect& cullRect = reader.skipT<SkRect>();
1040                size_t offsetToRestore = reader.readInt();
1041                if (offsetToRestore && canvas.quickReject(cullRect)) {
1042#ifdef SPEW_CLIP_SKIPPING
1043                    skipCull.recordSkip(offsetToRestore - reader.offset());
1044#endif
1045                    reader.setOffset(offsetToRestore);
1046                } else {
1047                    canvas.pushCull(cullRect);
1048                }
1049            } break;
1050            case POP_CULL:
1051                canvas.popCull();
1052                break;
1053            case CONCAT: {
1054                SkMatrix matrix;
1055                this->getMatrix(reader, &matrix);
1056                canvas.concat(matrix);
1057                break;
1058            }
1059            case DRAW_BITMAP: {
1060                const SkPaint* paint = this->getPaint(reader);
1061                const SkBitmap& bitmap = this->getBitmap(reader);
1062                const SkPoint& loc = reader.skipT<SkPoint>();
1063                canvas.drawBitmap(bitmap, loc.fX, loc.fY, paint);
1064            } break;
1065            case DRAW_BITMAP_RECT_TO_RECT: {
1066                const SkPaint* paint = this->getPaint(reader);
1067                const SkBitmap& bitmap = this->getBitmap(reader);
1068                const SkRect* src = this->getRectPtr(reader);   // may be null
1069                const SkRect& dst = reader.skipT<SkRect>();     // required
1070                SkCanvas::DrawBitmapRectFlags flags;
1071                flags = (SkCanvas::DrawBitmapRectFlags) reader.readInt();
1072                canvas.drawBitmapRectToRect(bitmap, src, dst, paint, flags);
1073            } break;
1074            case DRAW_BITMAP_MATRIX: {
1075                const SkPaint* paint = this->getPaint(reader);
1076                const SkBitmap& bitmap = this->getBitmap(reader);
1077                SkMatrix matrix;
1078                this->getMatrix(reader, &matrix);
1079                canvas.drawBitmapMatrix(bitmap, matrix, paint);
1080            } break;
1081            case DRAW_BITMAP_NINE: {
1082                const SkPaint* paint = this->getPaint(reader);
1083                const SkBitmap& bitmap = this->getBitmap(reader);
1084                const SkIRect& src = reader.skipT<SkIRect>();
1085                const SkRect& dst = reader.skipT<SkRect>();
1086                canvas.drawBitmapNine(bitmap, src, dst, paint);
1087            } break;
1088            case DRAW_CLEAR:
1089                canvas.clear(reader.readInt());
1090                break;
1091            case DRAW_DATA: {
1092                size_t length = reader.readInt();
1093                canvas.drawData(reader.skip(length), length);
1094                // skip handles padding the read out to a multiple of 4
1095            } break;
1096            case DRAW_DRRECT: {
1097                const SkPaint& paint = *this->getPaint(reader);
1098                SkRRect outer, inner;
1099                reader.readRRect(&outer);
1100                reader.readRRect(&inner);
1101                canvas.drawDRRect(outer, inner, paint);
1102            } break;
1103            case BEGIN_COMMENT_GROUP: {
1104                const char* desc = reader.readString();
1105                canvas.beginCommentGroup(desc);
1106            } break;
1107            case COMMENT: {
1108                const char* kywd = reader.readString();
1109                const char* value = reader.readString();
1110                canvas.addComment(kywd, value);
1111            } break;
1112            case END_COMMENT_GROUP: {
1113                canvas.endCommentGroup();
1114            } break;
1115            case DRAW_OVAL: {
1116                const SkPaint& paint = *this->getPaint(reader);
1117                canvas.drawOval(reader.skipT<SkRect>(), paint);
1118            } break;
1119            case DRAW_PAINT:
1120                canvas.drawPaint(*this->getPaint(reader));
1121                break;
1122            case DRAW_PATH: {
1123                const SkPaint& paint = *this->getPaint(reader);
1124                canvas.drawPath(getPath(reader), paint);
1125            } break;
1126            case DRAW_PICTURE:
1127                canvas.drawPicture(this->getPicture(reader));
1128                break;
1129            case DRAW_POINTS: {
1130                const SkPaint& paint = *this->getPaint(reader);
1131                SkCanvas::PointMode mode = (SkCanvas::PointMode)reader.readInt();
1132                size_t count = reader.readInt();
1133                const SkPoint* pts = (const SkPoint*)reader.skip(sizeof(SkPoint) * count);
1134                canvas.drawPoints(mode, count, pts, paint);
1135            } break;
1136            case DRAW_POS_TEXT: {
1137                const SkPaint& paint = *this->getPaint(reader);
1138                getText(reader, &text);
1139                size_t points = reader.readInt();
1140                const SkPoint* pos = (const SkPoint*)reader.skip(points * sizeof(SkPoint));
1141                canvas.drawPosText(text.text(), text.length(), pos, paint);
1142            } break;
1143            case DRAW_POS_TEXT_TOP_BOTTOM: {
1144                const SkPaint& paint = *this->getPaint(reader);
1145                getText(reader, &text);
1146                size_t points = reader.readInt();
1147                const SkPoint* pos = (const SkPoint*)reader.skip(points * sizeof(SkPoint));
1148                const SkScalar top = reader.readScalar();
1149                const SkScalar bottom = reader.readScalar();
1150                if (!canvas.quickRejectY(top, bottom)) {
1151                    canvas.drawPosText(text.text(), text.length(), pos, paint);
1152                }
1153            } break;
1154            case DRAW_POS_TEXT_H: {
1155                const SkPaint& paint = *this->getPaint(reader);
1156                getText(reader, &text);
1157                size_t xCount = reader.readInt();
1158                const SkScalar constY = reader.readScalar();
1159                const SkScalar* xpos = (const SkScalar*)reader.skip(xCount * sizeof(SkScalar));
1160                canvas.drawPosTextH(text.text(), text.length(), xpos, constY,
1161                                    paint);
1162            } break;
1163            case DRAW_POS_TEXT_H_TOP_BOTTOM: {
1164                const SkPaint& paint = *this->getPaint(reader);
1165                getText(reader, &text);
1166                size_t xCount = reader.readInt();
1167                const SkScalar* xpos = (const SkScalar*)reader.skip((3 + xCount) * sizeof(SkScalar));
1168                const SkScalar top = *xpos++;
1169                const SkScalar bottom = *xpos++;
1170                const SkScalar constY = *xpos++;
1171                if (!canvas.quickRejectY(top, bottom)) {
1172                    canvas.drawPosTextH(text.text(), text.length(), xpos,
1173                                        constY, paint);
1174                }
1175            } break;
1176            case DRAW_RECT: {
1177                const SkPaint& paint = *this->getPaint(reader);
1178                canvas.drawRect(reader.skipT<SkRect>(), paint);
1179            } break;
1180            case DRAW_RRECT: {
1181                const SkPaint& paint = *this->getPaint(reader);
1182                SkRRect rrect;
1183                reader.readRRect(&rrect);
1184                canvas.drawRRect(rrect, paint);
1185            } break;
1186            case DRAW_SPRITE: {
1187                const SkPaint* paint = this->getPaint(reader);
1188                const SkBitmap& bitmap = this->getBitmap(reader);
1189                int left = reader.readInt();
1190                int top = reader.readInt();
1191                canvas.drawSprite(bitmap, left, top, paint);
1192            } break;
1193            case DRAW_TEXT: {
1194                const SkPaint& paint = *this->getPaint(reader);
1195                this->getText(reader, &text);
1196                SkScalar x = reader.readScalar();
1197                SkScalar y = reader.readScalar();
1198                canvas.drawText(text.text(), text.length(), x, y, paint);
1199            } break;
1200            case DRAW_TEXT_TOP_BOTTOM: {
1201                const SkPaint& paint = *this->getPaint(reader);
1202                this->getText(reader, &text);
1203                const SkScalar* ptr = (const SkScalar*)reader.skip(4 * sizeof(SkScalar));
1204                // ptr[0] == x
1205                // ptr[1] == y
1206                // ptr[2] == top
1207                // ptr[3] == bottom
1208                if (!canvas.quickRejectY(ptr[2], ptr[3])) {
1209                    canvas.drawText(text.text(), text.length(), ptr[0], ptr[1],
1210                                    paint);
1211                }
1212            } break;
1213            case DRAW_TEXT_ON_PATH: {
1214                const SkPaint& paint = *this->getPaint(reader);
1215                getText(reader, &text);
1216                const SkPath& path = this->getPath(reader);
1217                SkMatrix matrix;
1218                this->getMatrix(reader, &matrix);
1219                canvas.drawTextOnPath(text.text(), text.length(), path, &matrix, paint);
1220            } break;
1221            case DRAW_VERTICES: {
1222                SkAutoTUnref<SkXfermode> xfer;
1223                const SkPaint& paint = *this->getPaint(reader);
1224                DrawVertexFlags flags = (DrawVertexFlags)reader.readInt();
1225                SkCanvas::VertexMode vmode = (SkCanvas::VertexMode)reader.readInt();
1226                int vCount = reader.readInt();
1227                const SkPoint* verts = (const SkPoint*)reader.skip(
1228                                                    vCount * sizeof(SkPoint));
1229                const SkPoint* texs = NULL;
1230                const SkColor* colors = NULL;
1231                const uint16_t* indices = NULL;
1232                int iCount = 0;
1233                if (flags & DRAW_VERTICES_HAS_TEXS) {
1234                    texs = (const SkPoint*)reader.skip(
1235                                                    vCount * sizeof(SkPoint));
1236                }
1237                if (flags & DRAW_VERTICES_HAS_COLORS) {
1238                    colors = (const SkColor*)reader.skip(
1239                                                    vCount * sizeof(SkColor));
1240                }
1241                if (flags & DRAW_VERTICES_HAS_INDICES) {
1242                    iCount = reader.readInt();
1243                    indices = (const uint16_t*)reader.skip(
1244                                                    iCount * sizeof(uint16_t));
1245                }
1246                if (flags & DRAW_VERTICES_HAS_XFER) {
1247                    int mode = reader.readInt();
1248                    if (mode < 0 || mode > SkXfermode::kLastMode) {
1249                        mode = SkXfermode::kModulate_Mode;
1250                    }
1251                    xfer.reset(SkXfermode::Create((SkXfermode::Mode)mode));
1252                }
1253                canvas.drawVertices(vmode, vCount, verts, texs, colors, xfer,
1254                                    indices, iCount, paint);
1255            } break;
1256            case RESTORE:
1257                canvas.restore();
1258                break;
1259            case ROTATE:
1260                canvas.rotate(reader.readScalar());
1261                break;
1262            case SAVE:
1263                canvas.save((SkCanvas::SaveFlags) reader.readInt());
1264                break;
1265            case SAVE_LAYER: {
1266                const SkRect* boundsPtr = this->getRectPtr(reader);
1267                const SkPaint* paint = this->getPaint(reader);
1268                canvas.saveLayer(boundsPtr, paint, (SkCanvas::SaveFlags) reader.readInt());
1269                } break;
1270            case SCALE: {
1271                SkScalar sx = reader.readScalar();
1272                SkScalar sy = reader.readScalar();
1273                canvas.scale(sx, sy);
1274            } break;
1275            case SET_MATRIX: {
1276                SkMatrix matrix;
1277                this->getMatrix(reader, &matrix);
1278                matrix.postConcat(initialMatrix);
1279                canvas.setMatrix(matrix);
1280            } break;
1281            case SKEW: {
1282                SkScalar sx = reader.readScalar();
1283                SkScalar sy = reader.readScalar();
1284                canvas.skew(sx, sy);
1285            } break;
1286            case TRANSLATE: {
1287                SkScalar dx = reader.readScalar();
1288                SkScalar dy = reader.readScalar();
1289                canvas.translate(dx, dy);
1290            } break;
1291            default:
1292                SkASSERT(0);
1293        }
1294
1295#ifdef SK_DEVELOPER
1296        this->postDraw(opIndex);
1297#endif
1298
1299        if (it.isValid()) {
1300            uint32_t skipTo = it.draw();
1301            if (kDrawComplete == skipTo) {
1302                break;
1303            }
1304            reader.setOffset(skipTo);
1305        }
1306    }
1307
1308#ifdef SPEW_CLIP_SKIPPING
1309    {
1310        size_t size =  skipRect.fSize + skipRRect.fSize + skipPath.fSize + skipRegion.fSize +
1311                skipCull.fSize;
1312        SkDebugf("--- Clip skips %d%% rect:%d rrect:%d path:%d rgn:%d cull:%d\n",
1313             size * 100 / reader.offset(), skipRect.fCount, skipRRect.fCount,
1314                 skipPath.fCount, skipRegion.fCount, skipCull.fCount);
1315        SkDebugf("--- Total ops: %d\n", opCount);
1316    }
1317#endif
1318//    this->dumpSize();
1319}
1320
1321///////////////////////////////////////////////////////////////////////////////
1322
1323#ifdef SK_DEBUG_SIZE
1324int SkPicturePlayback::size(size_t* sizePtr) {
1325    int objects = bitmaps(sizePtr);
1326    objects += paints(sizePtr);
1327    objects += paths(sizePtr);
1328    objects += pictures(sizePtr);
1329    objects += regions(sizePtr);
1330    *sizePtr = fOpData.size();
1331    return objects;
1332}
1333
1334int SkPicturePlayback::bitmaps(size_t* size) {
1335    size_t result = 0;
1336    for (int index = 0; index < fBitmapCount; index++) {
1337     //   const SkBitmap& bitmap = fBitmaps[index];
1338        result += sizeof(SkBitmap); // bitmap->size();
1339    }
1340    *size = result;
1341    return fBitmapCount;
1342}
1343
1344int SkPicturePlayback::paints(size_t* size) {
1345    size_t result = 0;
1346    for (int index = 0; index < fPaintCount; index++) {
1347    //    const SkPaint& paint = fPaints[index];
1348        result += sizeof(SkPaint); // paint->size();
1349    }
1350    *size = result;
1351    return fPaintCount;
1352}
1353
1354int SkPicturePlayback::paths(size_t* size) {
1355    size_t result = 0;
1356    for (int index = 0; index < fPathCount; index++) {
1357        const SkPath& path = fPaths[index];
1358        result += path.flatten(NULL);
1359    }
1360    *size = result;
1361    return fPathCount;
1362}
1363#endif
1364
1365#ifdef SK_DEBUG_DUMP
1366void SkPicturePlayback::dumpBitmap(const SkBitmap& bitmap) const {
1367    char pBuffer[DUMP_BUFFER_SIZE];
1368    char* bufferPtr = pBuffer;
1369    bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer),
1370        "BitmapData bitmap%p = {", &bitmap);
1371    bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer),
1372        "{kWidth, %d}, ", bitmap.width());
1373    bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer),
1374        "{kHeight, %d}, ", bitmap.height());
1375    bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer),
1376        "{kRowBytes, %d}, ", bitmap.rowBytes());
1377//        start here;
1378    SkDebugf("%s{0}};\n", pBuffer);
1379}
1380
1381void dumpMatrix(const SkMatrix& matrix) const {
1382    SkMatrix defaultMatrix;
1383    defaultMatrix.reset();
1384    char pBuffer[DUMP_BUFFER_SIZE];
1385    char* bufferPtr = pBuffer;
1386    bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer),
1387        "MatrixData matrix%p = {", &matrix);
1388    SkScalar scaleX = matrix.getScaleX();
1389    if (scaleX != defaultMatrix.getScaleX())
1390        bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer),
1391            "{kScaleX, %g}, ", SkScalarToFloat(scaleX));
1392    SkScalar scaleY = matrix.getScaleY();
1393    if (scaleY != defaultMatrix.getScaleY())
1394        bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer),
1395            "{kScaleY, %g}, ", SkScalarToFloat(scaleY));
1396    SkScalar skewX = matrix.getSkewX();
1397    if (skewX != defaultMatrix.getSkewX())
1398        bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer),
1399            "{kSkewX, %g}, ", SkScalarToFloat(skewX));
1400    SkScalar skewY = matrix.getSkewY();
1401    if (skewY != defaultMatrix.getSkewY())
1402        bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer),
1403            "{kSkewY, %g}, ", SkScalarToFloat(skewY));
1404    SkScalar translateX = matrix.getTranslateX();
1405    if (translateX != defaultMatrix.getTranslateX())
1406        bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer),
1407            "{kTranslateX, %g}, ", SkScalarToFloat(translateX));
1408    SkScalar translateY = matrix.getTranslateY();
1409    if (translateY != defaultMatrix.getTranslateY())
1410        bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer),
1411            "{kTranslateY, %g}, ", SkScalarToFloat(translateY));
1412    SkScalar perspX = matrix.getPerspX();
1413    if (perspX != defaultMatrix.getPerspX())
1414        bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer),
1415            "{kPerspX, %g}, ", perspX);
1416    SkScalar perspY = matrix.getPerspY();
1417    if (perspY != defaultMatrix.getPerspY())
1418        bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer),
1419            "{kPerspY, %g}, ", perspY);
1420    SkDebugf("%s{0}};\n", pBuffer);
1421}
1422
1423void dumpPaint(const SkPaint& paint) const {
1424    SkPaint defaultPaint;
1425    char pBuffer[DUMP_BUFFER_SIZE];
1426    char* bufferPtr = pBuffer;
1427    bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer),
1428        "PaintPointers paintPtrs%p = {", &paint);
1429    const SkTypeface* typeface = paint.getTypeface();
1430    if (typeface != defaultPaint.getTypeface())
1431        bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer),
1432            "{kTypeface, %p}, ", typeface);
1433    const SkPathEffect* pathEffect = paint.getPathEffect();
1434    if (pathEffect != defaultPaint.getPathEffect())
1435        bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer),
1436            "{kPathEffect, %p}, ", pathEffect);
1437    const SkShader* shader = paint.getShader();
1438    if (shader != defaultPaint.getShader())
1439        bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer),
1440            "{kShader, %p}, ", shader);
1441    const SkXfermode* xfermode = paint.getXfermode();
1442    if (xfermode != defaultPaint.getXfermode())
1443        bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer),
1444            "{kXfermode, %p}, ", xfermode);
1445    const SkMaskFilter* maskFilter = paint.getMaskFilter();
1446    if (maskFilter != defaultPaint.getMaskFilter())
1447        bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer),
1448            "{kMaskFilter, %p}, ", maskFilter);
1449    const SkColorFilter* colorFilter = paint.getColorFilter();
1450    if (colorFilter != defaultPaint.getColorFilter())
1451        bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer),
1452            "{kColorFilter, %p}, ", colorFilter);
1453    const SkRasterizer* rasterizer = paint.getRasterizer();
1454    if (rasterizer != defaultPaint.getRasterizer())
1455        bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer),
1456            "{kRasterizer, %p}, ", rasterizer);
1457    const SkDrawLooper* drawLooper = paint.getLooper();
1458    if (drawLooper != defaultPaint.getLooper())
1459        bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer),
1460            "{kDrawLooper, %p}, ", drawLooper);
1461    SkDebugf("%s{0}};\n", pBuffer);
1462    bufferPtr = pBuffer;
1463    bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer),
1464        "PaintScalars paintScalars%p = {", &paint);
1465    SkScalar textSize = paint.getTextSize();
1466    if (textSize != defaultPaint.getTextSize())
1467        bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer),
1468            "{kTextSize, %g}, ", SkScalarToFloat(textSize));
1469    SkScalar textScaleX = paint.getTextScaleX();
1470    if (textScaleX != defaultPaint.getTextScaleX())
1471        bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer),
1472            "{kTextScaleX, %g}, ", SkScalarToFloat(textScaleX));
1473    SkScalar textSkewX = paint.getTextSkewX();
1474    if (textSkewX != defaultPaint.getTextSkewX())
1475        bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer),
1476            "{kTextSkewX, %g}, ", SkScalarToFloat(textSkewX));
1477    SkScalar strokeWidth = paint.getStrokeWidth();
1478    if (strokeWidth != defaultPaint.getStrokeWidth())
1479        bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer),
1480            "{kStrokeWidth, %g}, ", SkScalarToFloat(strokeWidth));
1481    SkScalar strokeMiter = paint.getStrokeMiter();
1482    if (strokeMiter != defaultPaint.getStrokeMiter())
1483        bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer),
1484            "{kStrokeMiter, %g}, ", SkScalarToFloat(strokeMiter));
1485    SkDebugf("%s{0}};\n", pBuffer);
1486    bufferPtr = pBuffer;
1487    bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer),
1488        "PaintInts = paintInts%p = {", &paint);
1489    unsigned color = paint.getColor();
1490    if (color != defaultPaint.getColor())
1491        bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer),
1492            "{kColor, 0x%x}, ", color);
1493    unsigned flags = paint.getFlags();
1494    if (flags != defaultPaint.getFlags())
1495        bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer),
1496            "{kFlags, 0x%x}, ", flags);
1497    int align = paint.getTextAlign();
1498    if (align != defaultPaint.getTextAlign())
1499        bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer),
1500            "{kAlign, 0x%x}, ", align);
1501    int strokeCap = paint.getStrokeCap();
1502    if (strokeCap != defaultPaint.getStrokeCap())
1503        bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer),
1504            "{kStrokeCap, 0x%x}, ", strokeCap);
1505    int strokeJoin = paint.getStrokeJoin();
1506    if (strokeJoin != defaultPaint.getStrokeJoin())
1507        bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer),
1508            "{kAlign, 0x%x}, ", strokeJoin);
1509    int style = paint.getStyle();
1510    if (style != defaultPaint.getStyle())
1511        bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer),
1512            "{kStyle, 0x%x}, ", style);
1513    int textEncoding = paint.getTextEncoding();
1514    if (textEncoding != defaultPaint.getTextEncoding())
1515        bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer),
1516            "{kTextEncoding, 0x%x}, ", textEncoding);
1517    SkDebugf("%s{0}};\n", pBuffer);
1518
1519    SkDebugf("PaintData paint%p = {paintPtrs%p, paintScalars%p, paintInts%p};\n",
1520        &paint, &paint, &paint, &paint);
1521}
1522
1523void SkPicturePlayback::dumpPath(const SkPath& path) const {
1524    SkDebugf("path dump unimplemented\n");
1525}
1526
1527void SkPicturePlayback::dumpPicture(const SkPicture& picture) const {
1528    SkDebugf("picture dump unimplemented\n");
1529}
1530
1531void SkPicturePlayback::dumpRegion(const SkRegion& region) const {
1532    SkDebugf("region dump unimplemented\n");
1533}
1534
1535int SkPicturePlayback::dumpDrawType(char* bufferPtr, char* buffer, DrawType drawType) {
1536    return snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - buffer),
1537        "k%s, ", DrawTypeToString(drawType));
1538}
1539
1540int SkPicturePlayback::dumpInt(char* bufferPtr, char* buffer, char* name) {
1541    return snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - buffer),
1542        "%s:%d, ", name, getInt());
1543}
1544
1545int SkPicturePlayback::dumpRect(char* bufferPtr, char* buffer, char* name) {
1546    const SkRect* rect = fReader.skipRect();
1547    return snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - buffer),
1548        "%s:{l:%g t:%g r:%g b:%g}, ", name, SkScalarToFloat(rect.fLeft),
1549        SkScalarToFloat(rect.fTop),
1550        SkScalarToFloat(rect.fRight), SkScalarToFloat(rect.fBottom));
1551}
1552
1553int SkPicturePlayback::dumpPoint(char* bufferPtr, char* buffer, char* name) {
1554    SkPoint pt;
1555    getPoint(&pt);
1556    return snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - buffer),
1557        "%s:{x:%g y:%g}, ", name, SkScalarToFloat(pt.fX),
1558        SkScalarToFloat(pt.fY));
1559}
1560
1561void SkPicturePlayback::dumpPointArray(char** bufferPtrPtr, char* buffer, int count) {
1562    char* bufferPtr = *bufferPtrPtr;
1563    const SkPoint* pts = (const SkPoint*)fReadStream.getAtPos();
1564    fReadStream.skip(sizeof(SkPoint) * count);
1565    bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - buffer),
1566        "count:%d {", count);
1567    for (int index = 0; index < count; index++)
1568        bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - buffer),
1569        "{x:%g y:%g}, ", SkScalarToFloat(pts[index].fX),
1570        SkScalarToFloat(pts[index].fY));
1571    bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - buffer),
1572        "} ");
1573    *bufferPtrPtr = bufferPtr;
1574}
1575
1576int SkPicturePlayback::dumpPtr(char* bufferPtr, char* buffer, char* name, void* ptr) {
1577    return snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - buffer),
1578        "%s:%p, ", name, ptr);
1579}
1580
1581int SkPicturePlayback::dumpRectPtr(char* bufferPtr, char* buffer, char* name) {
1582    char result;
1583    fReadStream.read(&result, sizeof(result));
1584    if (result)
1585        return dumpRect(bufferPtr, buffer, name);
1586    else
1587        return snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - buffer),
1588            "%s:NULL, ", name);
1589}
1590
1591int SkPicturePlayback::dumpScalar(char* bufferPtr, char* buffer, char* name) {
1592    return snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - buffer),
1593        "%s:%d, ", name, getScalar());
1594}
1595
1596void SkPicturePlayback::dumpText(char** bufferPtrPtr, char* buffer) {
1597    char* bufferPtr = *bufferPtrPtr;
1598    int length = getInt();
1599    bufferPtr += dumpDrawType(bufferPtr, buffer);
1600    fReadStream.skipToAlign4();
1601    char* text = (char*) fReadStream.getAtPos();
1602    fReadStream.skip(length);
1603    bufferPtr += dumpInt(bufferPtr, buffer, "length");
1604    int limit = DUMP_BUFFER_SIZE - (bufferPtr - buffer) - 2;
1605    length >>= 1;
1606    if (limit > length)
1607        limit = length;
1608    if (limit > 0) {
1609        *bufferPtr++ = '"';
1610        for (int index = 0; index < limit; index++) {
1611            *bufferPtr++ = *(unsigned short*) text;
1612            text += sizeof(unsigned short);
1613        }
1614        *bufferPtr++ = '"';
1615    }
1616    *bufferPtrPtr = bufferPtr;
1617}
1618
1619#define DUMP_DRAWTYPE(drawType) \
1620    bufferPtr += dumpDrawType(bufferPtr, buffer, drawType)
1621
1622#define DUMP_INT(name) \
1623    bufferPtr += dumpInt(bufferPtr, buffer, #name)
1624
1625#define DUMP_RECT_PTR(name) \
1626    bufferPtr += dumpRectPtr(bufferPtr, buffer, #name)
1627
1628#define DUMP_POINT(name) \
1629    bufferPtr += dumpRect(bufferPtr, buffer, #name)
1630
1631#define DUMP_RECT(name) \
1632    bufferPtr += dumpRect(bufferPtr, buffer, #name)
1633
1634#define DUMP_POINT_ARRAY(count) \
1635    dumpPointArray(&bufferPtr, buffer, count)
1636
1637#define DUMP_PTR(name, ptr) \
1638    bufferPtr += dumpPtr(bufferPtr, buffer, #name, (void*) ptr)
1639
1640#define DUMP_SCALAR(name) \
1641    bufferPtr += dumpScalar(bufferPtr, buffer, #name)
1642
1643#define DUMP_TEXT() \
1644    dumpText(&bufferPtr, buffer)
1645
1646void SkPicturePlayback::dumpStream() {
1647    SkDebugf("RecordStream stream = {\n");
1648    DrawType drawType;
1649    TextContainer text;
1650    fReadStream.rewind();
1651    char buffer[DUMP_BUFFER_SIZE], * bufferPtr;
1652    while (fReadStream.read(&drawType, sizeof(drawType))) {
1653        bufferPtr = buffer;
1654        DUMP_DRAWTYPE(drawType);
1655        switch (drawType) {
1656            case CLIP_PATH: {
1657                DUMP_PTR(SkPath, &getPath());
1658                DUMP_INT(SkRegion::Op);
1659                DUMP_INT(offsetToRestore);
1660                } break;
1661            case CLIP_REGION: {
1662                DUMP_INT(SkRegion::Op);
1663                DUMP_INT(offsetToRestore);
1664            } break;
1665            case CLIP_RECT: {
1666                DUMP_RECT(rect);
1667                DUMP_INT(SkRegion::Op);
1668                DUMP_INT(offsetToRestore);
1669                } break;
1670            case CONCAT:
1671                break;
1672            case DRAW_BITMAP: {
1673                DUMP_PTR(SkPaint, getPaint());
1674                DUMP_PTR(SkBitmap, &getBitmap());
1675                DUMP_SCALAR(left);
1676                DUMP_SCALAR(top);
1677                } break;
1678            case DRAW_PAINT:
1679                DUMP_PTR(SkPaint, getPaint());
1680                break;
1681            case DRAW_PATH: {
1682                DUMP_PTR(SkPaint, getPaint());
1683                DUMP_PTR(SkPath, &getPath());
1684                } break;
1685            case DRAW_PICTURE: {
1686                DUMP_PTR(SkPicture, &getPicture());
1687                } break;
1688            case DRAW_POINTS: {
1689                DUMP_PTR(SkPaint, getPaint());
1690                (void)getInt(); // PointMode
1691                size_t count = getInt();
1692                fReadStream.skipToAlign4();
1693                DUMP_POINT_ARRAY(count);
1694                } break;
1695            case DRAW_POS_TEXT: {
1696                DUMP_PTR(SkPaint, getPaint());
1697                DUMP_TEXT();
1698                size_t points = getInt();
1699                fReadStream.skipToAlign4();
1700                DUMP_POINT_ARRAY(points);
1701                } break;
1702            case DRAW_POS_TEXT_H: {
1703                DUMP_PTR(SkPaint, getPaint());
1704                DUMP_TEXT();
1705                size_t points = getInt();
1706                fReadStream.skipToAlign4();
1707                DUMP_SCALAR(top);
1708                DUMP_SCALAR(bottom);
1709                DUMP_SCALAR(constY);
1710                DUMP_POINT_ARRAY(points);
1711                } break;
1712            case DRAW_RECT: {
1713                DUMP_PTR(SkPaint, getPaint());
1714                DUMP_RECT(rect);
1715                } break;
1716            case DRAW_SPRITE: {
1717                DUMP_PTR(SkPaint, getPaint());
1718                DUMP_PTR(SkBitmap, &getBitmap());
1719                DUMP_SCALAR(left);
1720                DUMP_SCALAR(top);
1721                } break;
1722            case DRAW_TEXT: {
1723                DUMP_PTR(SkPaint, getPaint());
1724                DUMP_TEXT();
1725                DUMP_SCALAR(x);
1726                DUMP_SCALAR(y);
1727                } break;
1728            case DRAW_TEXT_ON_PATH: {
1729                DUMP_PTR(SkPaint, getPaint());
1730                DUMP_TEXT();
1731                DUMP_PTR(SkPath, &getPath());
1732                } break;
1733            case RESTORE:
1734                break;
1735            case ROTATE:
1736                DUMP_SCALAR(rotate);
1737                break;
1738            case SAVE:
1739                DUMP_INT(SkCanvas::SaveFlags);
1740                break;
1741            case SAVE_LAYER: {
1742                DUMP_RECT_PTR(layer);
1743                DUMP_PTR(SkPaint, getPaint());
1744                DUMP_INT(SkCanvas::SaveFlags);
1745                } break;
1746            case SCALE: {
1747                DUMP_SCALAR(sx);
1748                DUMP_SCALAR(sy);
1749                } break;
1750            case SKEW: {
1751                DUMP_SCALAR(sx);
1752                DUMP_SCALAR(sy);
1753                } break;
1754            case TRANSLATE: {
1755                DUMP_SCALAR(dx);
1756                DUMP_SCALAR(dy);
1757                } break;
1758            default:
1759                SkASSERT(0);
1760        }
1761        SkDebugf("%s\n", buffer);
1762    }
1763}
1764
1765void SkPicturePlayback::dump() const {
1766    char pBuffer[DUMP_BUFFER_SIZE];
1767    char* bufferPtr = pBuffer;
1768    int index;
1769    if (fBitmapCount > 0)
1770        SkDebugf("// bitmaps (%d)\n", fBitmapCount);
1771    for (index = 0; index < fBitmapCount; index++) {
1772        const SkBitmap& bitmap = fBitmaps[index];
1773        dumpBitmap(bitmap);
1774    }
1775    if (fBitmapCount > 0)
1776        bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer),
1777            "Bitmaps bitmaps = {");
1778    for (index = 0; index < fBitmapCount; index++)
1779        bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer),
1780            "bitmap%p, ", &fBitmaps[index]);
1781    if (fBitmapCount > 0)
1782        SkDebugf("%s0};\n", pBuffer);
1783
1784
1785    if (fPaintCount > 0)
1786        SkDebugf("// paints (%d)\n", fPaintCount);
1787    for (index = 0; index < fPaintCount; index++) {
1788        const SkPaint& paint = fPaints[index];
1789        dumpPaint(paint);
1790    }
1791    bufferPtr = pBuffer;
1792    if (fPaintCount > 0)
1793        bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer),
1794            "Paints paints = {");
1795    for (index = 0; index < fPaintCount; index++)
1796        bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer),
1797            "paint%p, ", &fPaints[index]);
1798    if (fPaintCount > 0)
1799        SkDebugf("%s0};\n", pBuffer);
1800
1801    for (index = 0; index < fPathCount; index++) {
1802        const SkPath& path = fPaths[index];
1803        dumpPath(path);
1804    }
1805    bufferPtr = pBuffer;
1806    if (fPathCount > 0)
1807        bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer),
1808            "Paths paths = {");
1809    for (index = 0; index < fPathCount; index++)
1810        bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer),
1811            "path%p, ", &fPaths[index]);
1812    if (fPathCount > 0)
1813        SkDebugf("%s0};\n", pBuffer);
1814
1815    for (index = 0; index < fPictureCount; index++) {
1816        dumpPicture(*fPictureRefs[index]);
1817    }
1818    bufferPtr = pBuffer;
1819    if (fPictureCount > 0)
1820        bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer),
1821            "Pictures pictures = {");
1822    for (index = 0; index < fPictureCount; index++)
1823        bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer),
1824            "picture%p, ", fPictureRefs[index]);
1825    if (fPictureCount > 0)
1826        SkDebugf("%s0};\n", pBuffer);
1827
1828    const_cast<SkPicturePlayback*>(this)->dumpStream();
1829}
1830
1831#endif
1832