michael@0: michael@0: /* michael@0: * Copyright 2011 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: #include michael@0: #include "SkBBoxHierarchy.h" michael@0: #include "SkOffsetTable.h" michael@0: #include "SkPicturePlayback.h" michael@0: #include "SkPictureRecord.h" michael@0: #include "SkPictureStateTree.h" michael@0: #include "SkReadBuffer.h" michael@0: #include "SkTypeface.h" michael@0: #include "SkTSort.h" michael@0: #include "SkWriteBuffer.h" michael@0: michael@0: template int SafeCount(const T* obj) { michael@0: return obj ? obj->count() : 0; michael@0: } michael@0: michael@0: /* Define this to spew out a debug statement whenever we skip the remainder of michael@0: a save/restore block because a clip... command returned false (empty). michael@0: */ michael@0: #define SPEW_CLIP_SKIPPINGx michael@0: michael@0: SkPicturePlayback::SkPicturePlayback() { michael@0: this->init(); michael@0: } michael@0: michael@0: SkPicturePlayback::SkPicturePlayback(const SkPictureRecord& record, bool deepCopy) { michael@0: #ifdef SK_DEBUG_SIZE michael@0: size_t overallBytes, bitmapBytes, matricesBytes, michael@0: paintBytes, pathBytes, pictureBytes, regionBytes; michael@0: int bitmaps = record.bitmaps(&bitmapBytes); michael@0: int matrices = record.matrices(&matricesBytes); michael@0: int paints = record.paints(&paintBytes); michael@0: int paths = record.paths(&pathBytes); michael@0: int pictures = record.pictures(&pictureBytes); michael@0: int regions = record.regions(®ionBytes); michael@0: SkDebugf("picture record mem used %zd (stream %zd) ", record.size(), michael@0: record.streamlen()); michael@0: if (bitmaps != 0) michael@0: SkDebugf("bitmaps size %zd (bitmaps:%d) ", bitmapBytes, bitmaps); michael@0: if (matrices != 0) michael@0: SkDebugf("matrices size %zd (matrices:%d) ", matricesBytes, matrices); michael@0: if (paints != 0) michael@0: SkDebugf("paints size %zd (paints:%d) ", paintBytes, paints); michael@0: if (paths != 0) michael@0: SkDebugf("paths size %zd (paths:%d) ", pathBytes, paths); michael@0: if (pictures != 0) michael@0: SkDebugf("pictures size %zd (pictures:%d) ", pictureBytes, pictures); michael@0: if (regions != 0) michael@0: SkDebugf("regions size %zd (regions:%d) ", regionBytes, regions); michael@0: if (record.fPointWrites != 0) michael@0: SkDebugf("points size %zd (points:%d) ", record.fPointBytes, record.fPointWrites); michael@0: if (record.fRectWrites != 0) michael@0: SkDebugf("rects size %zd (rects:%d) ", record.fRectBytes, record.fRectWrites); michael@0: if (record.fTextWrites != 0) michael@0: SkDebugf("text size %zd (text strings:%d) ", record.fTextBytes, record.fTextWrites); michael@0: michael@0: SkDebugf("\n"); michael@0: #endif michael@0: #ifdef SK_DEBUG_DUMP michael@0: record.dumpMatrices(); michael@0: record.dumpPaints(); michael@0: #endif michael@0: michael@0: record.validate(record.writeStream().bytesWritten(), 0); michael@0: const SkWriter32& writer = record.writeStream(); michael@0: init(); michael@0: SkASSERT(!fOpData); michael@0: if (writer.bytesWritten() == 0) { michael@0: fOpData = SkData::NewEmpty(); michael@0: return; michael@0: } michael@0: fOpData = writer.snapshotAsData(); michael@0: michael@0: fBoundingHierarchy = record.fBoundingHierarchy; michael@0: fStateTree = record.fStateTree; michael@0: michael@0: SkSafeRef(fBoundingHierarchy); michael@0: SkSafeRef(fStateTree); michael@0: michael@0: if (NULL != fBoundingHierarchy) { michael@0: fBoundingHierarchy->flushDeferredInserts(); michael@0: } michael@0: michael@0: // copy over the refcnt dictionary to our reader michael@0: record.fFlattenableHeap.setupPlaybacks(); michael@0: michael@0: fBitmaps = record.fBitmapHeap->extractBitmaps(); michael@0: fPaints = record.fPaints.unflattenToArray(); michael@0: michael@0: fBitmapHeap.reset(SkSafeRef(record.fBitmapHeap)); michael@0: fPathHeap.reset(SkSafeRef(record.fPathHeap)); michael@0: michael@0: fBitmapUseOffsets.reset(SkSafeRef(record.fBitmapUseOffsets.get())); michael@0: michael@0: // ensure that the paths bounds are pre-computed michael@0: if (fPathHeap.get()) { michael@0: for (int i = 0; i < fPathHeap->count(); i++) { michael@0: (*fPathHeap)[i].updateBoundsCache(); michael@0: } michael@0: } michael@0: michael@0: const SkTDArray& pictures = record.getPictureRefs(); michael@0: fPictureCount = pictures.count(); michael@0: if (fPictureCount > 0) { michael@0: fPictureRefs = SkNEW_ARRAY(SkPicture*, fPictureCount); michael@0: for (int i = 0; i < fPictureCount; i++) { michael@0: if (deepCopy) { michael@0: fPictureRefs[i] = pictures[i]->clone(); michael@0: } else { michael@0: fPictureRefs[i] = pictures[i]; michael@0: fPictureRefs[i]->ref(); michael@0: } michael@0: } michael@0: } michael@0: michael@0: #ifdef SK_DEBUG_SIZE michael@0: int overall = fPlayback->size(&overallBytes); michael@0: bitmaps = fPlayback->bitmaps(&bitmapBytes); michael@0: paints = fPlayback->paints(&paintBytes); michael@0: paths = fPlayback->paths(&pathBytes); michael@0: pictures = fPlayback->pictures(&pictureBytes); michael@0: regions = fPlayback->regions(®ionBytes); michael@0: SkDebugf("playback size %zd (objects:%d) ", overallBytes, overall); michael@0: if (bitmaps != 0) michael@0: SkDebugf("bitmaps size %zd (bitmaps:%d) ", bitmapBytes, bitmaps); michael@0: if (paints != 0) michael@0: SkDebugf("paints size %zd (paints:%d) ", paintBytes, paints); michael@0: if (paths != 0) michael@0: SkDebugf("paths size %zd (paths:%d) ", pathBytes, paths); michael@0: if (pictures != 0) michael@0: SkDebugf("pictures size %zd (pictures:%d) ", pictureBytes, pictures); michael@0: if (regions != 0) michael@0: SkDebugf("regions size %zd (regions:%d) ", regionBytes, regions); michael@0: SkDebugf("\n"); michael@0: #endif michael@0: } michael@0: michael@0: static bool needs_deep_copy(const SkPaint& paint) { michael@0: /* michael@0: * These fields are known to be immutable, and so can be shallow-copied michael@0: * michael@0: * getTypeface() michael@0: * getAnnotation() michael@0: * paint.getColorFilter() michael@0: * getXfermode() michael@0: */ michael@0: michael@0: return paint.getPathEffect() || michael@0: paint.getShader() || michael@0: paint.getMaskFilter() || michael@0: paint.getRasterizer() || michael@0: paint.getLooper() || michael@0: paint.getImageFilter(); michael@0: } michael@0: michael@0: SkPicturePlayback::SkPicturePlayback(const SkPicturePlayback& src, SkPictCopyInfo* deepCopyInfo) { michael@0: this->init(); michael@0: michael@0: fBitmapHeap.reset(SkSafeRef(src.fBitmapHeap.get())); michael@0: fPathHeap.reset(SkSafeRef(src.fPathHeap.get())); michael@0: michael@0: fOpData = SkSafeRef(src.fOpData); michael@0: michael@0: fBoundingHierarchy = src.fBoundingHierarchy; michael@0: fStateTree = src.fStateTree; michael@0: michael@0: SkSafeRef(fBoundingHierarchy); michael@0: SkSafeRef(fStateTree); michael@0: michael@0: if (deepCopyInfo) { michael@0: int paintCount = SafeCount(src.fPaints); michael@0: michael@0: if (src.fBitmaps) { michael@0: fBitmaps = SkTRefArray::Create(src.fBitmaps->begin(), src.fBitmaps->count()); michael@0: } michael@0: michael@0: if (!deepCopyInfo->initialized) { michael@0: /* The alternative to doing this is to have a clone method on the paint and have it make michael@0: * the deep copy of its internal structures as needed. The holdup to doing that is at michael@0: * this point we would need to pass the SkBitmapHeap so that we don't unnecessarily michael@0: * flatten the pixels in a bitmap shader. michael@0: */ michael@0: deepCopyInfo->paintData.setCount(paintCount); michael@0: michael@0: /* Use an SkBitmapHeap to avoid flattening bitmaps in shaders. If there already is one, michael@0: * use it. If this SkPicturePlayback was created from a stream, fBitmapHeap will be michael@0: * NULL, so create a new one. michael@0: */ michael@0: if (fBitmapHeap.get() == NULL) { michael@0: // FIXME: Put this on the stack inside SkPicture::clone. Further, is it possible to michael@0: // do the rest of this initialization in SkPicture::clone as well? michael@0: SkBitmapHeap* heap = SkNEW(SkBitmapHeap); michael@0: deepCopyInfo->controller.setBitmapStorage(heap); michael@0: heap->unref(); michael@0: } else { michael@0: deepCopyInfo->controller.setBitmapStorage(fBitmapHeap); michael@0: } michael@0: michael@0: SkDEBUGCODE(int heapSize = SafeCount(fBitmapHeap.get());) michael@0: for (int i = 0; i < paintCount; i++) { michael@0: if (needs_deep_copy(src.fPaints->at(i))) { michael@0: deepCopyInfo->paintData[i] = michael@0: SkFlatData::Create(&deepCopyInfo->controller, michael@0: src.fPaints->at(i), 0); michael@0: michael@0: } else { michael@0: // this is our sentinel, which we use in the unflatten loop michael@0: deepCopyInfo->paintData[i] = NULL; michael@0: } michael@0: } michael@0: SkASSERT(SafeCount(fBitmapHeap.get()) == heapSize); michael@0: michael@0: // needed to create typeface playback michael@0: deepCopyInfo->controller.setupPlaybacks(); michael@0: deepCopyInfo->initialized = true; michael@0: } michael@0: michael@0: fPaints = SkTRefArray::Create(paintCount); michael@0: SkASSERT(deepCopyInfo->paintData.count() == paintCount); michael@0: SkBitmapHeap* bmHeap = deepCopyInfo->controller.getBitmapHeap(); michael@0: SkTypefacePlayback* tfPlayback = deepCopyInfo->controller.getTypefacePlayback(); michael@0: for (int i = 0; i < paintCount; i++) { michael@0: if (deepCopyInfo->paintData[i]) { michael@0: deepCopyInfo->paintData[i]->unflatten( michael@0: &fPaints->writableAt(i), bmHeap, tfPlayback); michael@0: } else { michael@0: // needs_deep_copy was false, so just need to assign michael@0: fPaints->writableAt(i) = src.fPaints->at(i); michael@0: } michael@0: } michael@0: michael@0: } else { michael@0: fBitmaps = SkSafeRef(src.fBitmaps); michael@0: fPaints = SkSafeRef(src.fPaints); michael@0: } michael@0: michael@0: fPictureCount = src.fPictureCount; michael@0: fPictureRefs = SkNEW_ARRAY(SkPicture*, fPictureCount); michael@0: for (int i = 0; i < fPictureCount; i++) { michael@0: if (deepCopyInfo) { michael@0: fPictureRefs[i] = src.fPictureRefs[i]->clone(); michael@0: } else { michael@0: fPictureRefs[i] = src.fPictureRefs[i]; michael@0: fPictureRefs[i]->ref(); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void SkPicturePlayback::init() { michael@0: fBitmaps = NULL; michael@0: fPaints = NULL; michael@0: fPictureRefs = NULL; michael@0: fPictureCount = 0; michael@0: fOpData = NULL; michael@0: fFactoryPlayback = NULL; michael@0: fBoundingHierarchy = NULL; michael@0: fStateTree = NULL; michael@0: } michael@0: michael@0: SkPicturePlayback::~SkPicturePlayback() { michael@0: SkSafeUnref(fOpData); michael@0: michael@0: SkSafeUnref(fBitmaps); michael@0: SkSafeUnref(fPaints); michael@0: SkSafeUnref(fBoundingHierarchy); michael@0: SkSafeUnref(fStateTree); michael@0: michael@0: for (int i = 0; i < fPictureCount; i++) { michael@0: fPictureRefs[i]->unref(); michael@0: } michael@0: SkDELETE_ARRAY(fPictureRefs); michael@0: michael@0: SkDELETE(fFactoryPlayback); michael@0: } michael@0: michael@0: void SkPicturePlayback::dumpSize() const { michael@0: SkDebugf("--- picture size: ops=%d bitmaps=%d [%d] paints=%d [%d] paths=%d\n", michael@0: fOpData->size(), michael@0: SafeCount(fBitmaps), SafeCount(fBitmaps) * sizeof(SkBitmap), michael@0: SafeCount(fPaints), SafeCount(fPaints) * sizeof(SkPaint), michael@0: SafeCount(fPathHeap.get())); michael@0: } michael@0: michael@0: bool SkPicturePlayback::containsBitmaps() const { michael@0: if (fBitmaps && fBitmaps->count() > 0) { michael@0: return true; michael@0: } michael@0: for (int i = 0; i < fPictureCount; ++i) { michael@0: if (fPictureRefs[i]->willPlayBackBitmaps()) { michael@0: return true; michael@0: } michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: #include "SkStream.h" michael@0: michael@0: static void write_tag_size(SkWriteBuffer& buffer, uint32_t tag, uint32_t size) { michael@0: buffer.writeUInt(tag); michael@0: buffer.writeUInt(size); michael@0: } michael@0: michael@0: static void write_tag_size(SkWStream* stream, uint32_t tag, uint32_t size) { michael@0: stream->write32(tag); michael@0: stream->write32(size); michael@0: } michael@0: michael@0: static size_t compute_chunk_size(SkFlattenable::Factory* array, int count) { michael@0: size_t size = 4; // for 'count' michael@0: michael@0: for (int i = 0; i < count; i++) { michael@0: const char* name = SkFlattenable::FactoryToName(array[i]); michael@0: if (NULL == name || 0 == *name) { michael@0: size += SkWStream::SizeOfPackedUInt(0); michael@0: } else { michael@0: size_t len = strlen(name); michael@0: size += SkWStream::SizeOfPackedUInt(len); michael@0: size += len; michael@0: } michael@0: } michael@0: michael@0: return size; michael@0: } michael@0: michael@0: static void write_factories(SkWStream* stream, const SkFactorySet& rec) { michael@0: int count = rec.count(); michael@0: michael@0: SkAutoSTMalloc<16, SkFlattenable::Factory> storage(count); michael@0: SkFlattenable::Factory* array = (SkFlattenable::Factory*)storage.get(); michael@0: rec.copyToArray(array); michael@0: michael@0: size_t size = compute_chunk_size(array, count); michael@0: michael@0: // TODO: write_tag_size should really take a size_t michael@0: write_tag_size(stream, SK_PICT_FACTORY_TAG, (uint32_t) size); michael@0: SkDEBUGCODE(size_t start = stream->bytesWritten()); michael@0: stream->write32(count); michael@0: michael@0: for (int i = 0; i < count; i++) { michael@0: const char* name = SkFlattenable::FactoryToName(array[i]); michael@0: // SkDebugf("---- write factories [%d] %p <%s>\n", i, array[i], name); michael@0: if (NULL == name || 0 == *name) { michael@0: stream->writePackedUInt(0); michael@0: } else { michael@0: uint32_t len = strlen(name); michael@0: stream->writePackedUInt(len); michael@0: stream->write(name, len); michael@0: } michael@0: } michael@0: michael@0: SkASSERT(size == (stream->bytesWritten() - start)); michael@0: } michael@0: michael@0: static void write_typefaces(SkWStream* stream, const SkRefCntSet& rec) { michael@0: int count = rec.count(); michael@0: michael@0: write_tag_size(stream, SK_PICT_TYPEFACE_TAG, count); michael@0: michael@0: SkAutoSTMalloc<16, SkTypeface*> storage(count); michael@0: SkTypeface** array = (SkTypeface**)storage.get(); michael@0: rec.copyToArray((SkRefCnt**)array); michael@0: michael@0: for (int i = 0; i < count; i++) { michael@0: array[i]->serialize(stream); michael@0: } michael@0: } michael@0: michael@0: void SkPicturePlayback::flattenToBuffer(SkWriteBuffer& buffer) const { michael@0: int i, n; michael@0: michael@0: if ((n = SafeCount(fBitmaps)) > 0) { michael@0: write_tag_size(buffer, SK_PICT_BITMAP_BUFFER_TAG, n); michael@0: for (i = 0; i < n; i++) { michael@0: buffer.writeBitmap((*fBitmaps)[i]); michael@0: } michael@0: } michael@0: michael@0: if ((n = SafeCount(fPaints)) > 0) { michael@0: write_tag_size(buffer, SK_PICT_PAINT_BUFFER_TAG, n); michael@0: for (i = 0; i < n; i++) { michael@0: buffer.writePaint((*fPaints)[i]); michael@0: } michael@0: } michael@0: michael@0: if ((n = SafeCount(fPathHeap.get())) > 0) { michael@0: write_tag_size(buffer, SK_PICT_PATH_BUFFER_TAG, n); michael@0: fPathHeap->flatten(buffer); michael@0: } michael@0: } michael@0: michael@0: void SkPicturePlayback::serialize(SkWStream* stream, michael@0: SkPicture::EncodeBitmap encoder) const { michael@0: write_tag_size(stream, SK_PICT_READER_TAG, fOpData->size()); michael@0: stream->write(fOpData->bytes(), fOpData->size()); michael@0: michael@0: if (fPictureCount > 0) { michael@0: write_tag_size(stream, SK_PICT_PICTURE_TAG, fPictureCount); michael@0: for (int i = 0; i < fPictureCount; i++) { michael@0: fPictureRefs[i]->serialize(stream, encoder); michael@0: } michael@0: } michael@0: michael@0: // Write some of our data into a writebuffer, and then serialize that michael@0: // into our stream michael@0: { michael@0: SkRefCntSet typefaceSet; michael@0: SkFactorySet factSet; michael@0: michael@0: SkWriteBuffer buffer(SkWriteBuffer::kCrossProcess_Flag); michael@0: buffer.setTypefaceRecorder(&typefaceSet); michael@0: buffer.setFactoryRecorder(&factSet); michael@0: buffer.setBitmapEncoder(encoder); michael@0: michael@0: this->flattenToBuffer(buffer); michael@0: michael@0: // We have to write these two sets into the stream *before* we write michael@0: // the buffer, since parsing that buffer will require that we already michael@0: // have these sets available to use. michael@0: write_factories(stream, factSet); michael@0: write_typefaces(stream, typefaceSet); michael@0: michael@0: write_tag_size(stream, SK_PICT_BUFFER_SIZE_TAG, buffer.bytesWritten()); michael@0: buffer.writeToStream(stream); michael@0: } michael@0: michael@0: stream->write32(SK_PICT_EOF_TAG); michael@0: } michael@0: michael@0: void SkPicturePlayback::flatten(SkWriteBuffer& buffer) const { michael@0: write_tag_size(buffer, SK_PICT_READER_TAG, fOpData->size()); michael@0: buffer.writeByteArray(fOpData->bytes(), fOpData->size()); michael@0: michael@0: if (fPictureCount > 0) { michael@0: write_tag_size(buffer, SK_PICT_PICTURE_TAG, fPictureCount); michael@0: for (int i = 0; i < fPictureCount; i++) { michael@0: fPictureRefs[i]->flatten(buffer); michael@0: } michael@0: } michael@0: michael@0: // Write this picture playback's data into a writebuffer michael@0: this->flattenToBuffer(buffer); michael@0: buffer.write32(SK_PICT_EOF_TAG); michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: /** michael@0: * Return the corresponding SkReadBuffer flags, given a set of michael@0: * SkPictInfo flags. michael@0: */ michael@0: static uint32_t pictInfoFlagsToReadBufferFlags(uint32_t pictInfoFlags) { michael@0: static const struct { michael@0: uint32_t fSrc; michael@0: uint32_t fDst; michael@0: } gSD[] = { michael@0: { SkPictInfo::kCrossProcess_Flag, SkReadBuffer::kCrossProcess_Flag }, michael@0: { SkPictInfo::kScalarIsFloat_Flag, SkReadBuffer::kScalarIsFloat_Flag }, michael@0: { SkPictInfo::kPtrIs64Bit_Flag, SkReadBuffer::kPtrIs64Bit_Flag }, michael@0: }; michael@0: michael@0: uint32_t rbMask = 0; michael@0: for (size_t i = 0; i < SK_ARRAY_COUNT(gSD); ++i) { michael@0: if (pictInfoFlags & gSD[i].fSrc) { michael@0: rbMask |= gSD[i].fDst; michael@0: } michael@0: } michael@0: return rbMask; michael@0: } michael@0: michael@0: bool SkPicturePlayback::parseStreamTag(SkStream* stream, const SkPictInfo& info, uint32_t tag, michael@0: size_t size, SkPicture::InstallPixelRefProc proc) { michael@0: /* michael@0: * By the time we encounter BUFFER_SIZE_TAG, we need to have already seen michael@0: * its dependents: FACTORY_TAG and TYPEFACE_TAG. These two are not required michael@0: * but if they are present, they need to have been seen before the buffer. michael@0: * michael@0: * We assert that if/when we see either of these, that we have not yet seen michael@0: * the buffer tag, because if we have, then its too-late to deal with the michael@0: * factories or typefaces. michael@0: */ michael@0: SkDEBUGCODE(bool haveBuffer = false;) michael@0: michael@0: switch (tag) { michael@0: case SK_PICT_READER_TAG: { michael@0: SkAutoMalloc storage(size); michael@0: if (stream->read(storage.get(), size) != size) { michael@0: return false; michael@0: } michael@0: SkASSERT(NULL == fOpData); michael@0: fOpData = SkData::NewFromMalloc(storage.detach(), size); michael@0: } break; michael@0: case SK_PICT_FACTORY_TAG: { michael@0: SkASSERT(!haveBuffer); michael@0: // Remove this code when v21 and below are no longer supported. At the michael@0: // same time add a new 'count' variable and use it rather then reusing 'size'. michael@0: #ifndef DISABLE_V21_COMPATIBILITY_CODE michael@0: if (info.fVersion >= 22) { michael@0: // in v22 this tag's size represents the size of the chunk in bytes michael@0: // and the number of factory strings is written out separately michael@0: #endif michael@0: size = stream->readU32(); michael@0: #ifndef DISABLE_V21_COMPATIBILITY_CODE michael@0: } michael@0: #endif michael@0: fFactoryPlayback = SkNEW_ARGS(SkFactoryPlayback, (size)); michael@0: for (size_t i = 0; i < size; i++) { michael@0: SkString str; michael@0: const size_t len = stream->readPackedUInt(); michael@0: str.resize(len); michael@0: if (stream->read(str.writable_str(), len) != len) { michael@0: return false; michael@0: } michael@0: fFactoryPlayback->base()[i] = SkFlattenable::NameToFactory(str.c_str()); michael@0: } michael@0: } break; michael@0: case SK_PICT_TYPEFACE_TAG: { michael@0: SkASSERT(!haveBuffer); michael@0: fTFPlayback.setCount(size); michael@0: for (size_t i = 0; i < size; i++) { michael@0: SkAutoTUnref tf(SkTypeface::Deserialize(stream)); michael@0: if (!tf.get()) { // failed to deserialize michael@0: // fTFPlayback asserts it never has a null, so we plop in michael@0: // the default here. michael@0: tf.reset(SkTypeface::RefDefault()); michael@0: } michael@0: fTFPlayback.set(i, tf); michael@0: } michael@0: } break; michael@0: case SK_PICT_PICTURE_TAG: { michael@0: fPictureCount = size; michael@0: fPictureRefs = SkNEW_ARRAY(SkPicture*, fPictureCount); michael@0: bool success = true; michael@0: int i = 0; michael@0: for ( ; i < fPictureCount; i++) { michael@0: fPictureRefs[i] = SkPicture::CreateFromStream(stream, proc); michael@0: if (NULL == fPictureRefs[i]) { michael@0: success = false; michael@0: break; michael@0: } michael@0: } michael@0: if (!success) { michael@0: // Delete all of the pictures that were already created (up to but excluding i): michael@0: for (int j = 0; j < i; j++) { michael@0: fPictureRefs[j]->unref(); michael@0: } michael@0: // Delete the array michael@0: SkDELETE_ARRAY(fPictureRefs); michael@0: fPictureCount = 0; michael@0: return false; michael@0: } michael@0: } break; michael@0: case SK_PICT_BUFFER_SIZE_TAG: { michael@0: SkAutoMalloc storage(size); michael@0: if (stream->read(storage.get(), size) != size) { michael@0: return false; michael@0: } michael@0: michael@0: SkReadBuffer buffer(storage.get(), size); michael@0: buffer.setFlags(pictInfoFlagsToReadBufferFlags(info.fFlags)); michael@0: michael@0: fFactoryPlayback->setupBuffer(buffer); michael@0: fTFPlayback.setupBuffer(buffer); michael@0: buffer.setBitmapDecoder(proc); michael@0: michael@0: while (!buffer.eof()) { michael@0: tag = buffer.readUInt(); michael@0: size = buffer.readUInt(); michael@0: if (!this->parseBufferTag(buffer, tag, size)) { michael@0: return false; michael@0: } michael@0: } michael@0: SkDEBUGCODE(haveBuffer = true;) michael@0: } break; michael@0: } michael@0: return true; // success michael@0: } michael@0: michael@0: bool SkPicturePlayback::parseBufferTag(SkReadBuffer& buffer, michael@0: uint32_t tag, size_t size) { michael@0: switch (tag) { michael@0: case SK_PICT_BITMAP_BUFFER_TAG: { michael@0: fBitmaps = SkTRefArray::Create(size); michael@0: for (size_t i = 0; i < size; ++i) { michael@0: SkBitmap* bm = &fBitmaps->writableAt(i); michael@0: buffer.readBitmap(bm); michael@0: bm->setImmutable(); michael@0: } michael@0: } break; michael@0: case SK_PICT_PAINT_BUFFER_TAG: { michael@0: fPaints = SkTRefArray::Create(size); michael@0: for (size_t i = 0; i < size; ++i) { michael@0: buffer.readPaint(&fPaints->writableAt(i)); michael@0: } michael@0: } break; michael@0: case SK_PICT_PATH_BUFFER_TAG: michael@0: if (size > 0) { michael@0: fPathHeap.reset(SkNEW_ARGS(SkPathHeap, (buffer))); michael@0: } michael@0: break; michael@0: case SK_PICT_READER_TAG: { michael@0: SkAutoMalloc storage(size); michael@0: if (!buffer.readByteArray(storage.get(), size) || michael@0: !buffer.validate(NULL == fOpData)) { michael@0: return false; michael@0: } michael@0: SkASSERT(NULL == fOpData); michael@0: fOpData = SkData::NewFromMalloc(storage.detach(), size); michael@0: } break; michael@0: case SK_PICT_PICTURE_TAG: { michael@0: if (!buffer.validate((0 == fPictureCount) && (NULL == fPictureRefs))) { michael@0: return false; michael@0: } michael@0: fPictureCount = size; michael@0: fPictureRefs = SkNEW_ARRAY(SkPicture*, fPictureCount); michael@0: bool success = true; michael@0: int i = 0; michael@0: for ( ; i < fPictureCount; i++) { michael@0: fPictureRefs[i] = SkPicture::CreateFromBuffer(buffer); michael@0: if (NULL == fPictureRefs[i]) { michael@0: success = false; michael@0: break; michael@0: } michael@0: } michael@0: if (!success) { michael@0: // Delete all of the pictures that were already created (up to but excluding i): michael@0: for (int j = 0; j < i; j++) { michael@0: fPictureRefs[j]->unref(); michael@0: } michael@0: // Delete the array michael@0: SkDELETE_ARRAY(fPictureRefs); michael@0: fPictureCount = 0; michael@0: return false; michael@0: } michael@0: } break; michael@0: default: michael@0: // The tag was invalid. michael@0: return false; michael@0: } michael@0: return true; // success michael@0: } michael@0: michael@0: SkPicturePlayback* SkPicturePlayback::CreateFromStream(SkStream* stream, michael@0: const SkPictInfo& info, michael@0: SkPicture::InstallPixelRefProc proc) { michael@0: SkAutoTDelete playback(SkNEW(SkPicturePlayback)); michael@0: michael@0: if (!playback->parseStream(stream, info, proc)) { michael@0: return NULL; michael@0: } michael@0: return playback.detach(); michael@0: } michael@0: michael@0: SkPicturePlayback* SkPicturePlayback::CreateFromBuffer(SkReadBuffer& buffer) { michael@0: SkAutoTDelete playback(SkNEW(SkPicturePlayback)); michael@0: michael@0: if (!playback->parseBuffer(buffer)) { michael@0: return NULL; michael@0: } michael@0: return playback.detach(); michael@0: } michael@0: michael@0: bool SkPicturePlayback::parseStream(SkStream* stream, const SkPictInfo& info, michael@0: SkPicture::InstallPixelRefProc proc) { michael@0: for (;;) { michael@0: uint32_t tag = stream->readU32(); michael@0: if (SK_PICT_EOF_TAG == tag) { michael@0: break; michael@0: } michael@0: michael@0: uint32_t size = stream->readU32(); michael@0: if (!this->parseStreamTag(stream, info, tag, size, proc)) { michael@0: return false; // we're invalid michael@0: } michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: bool SkPicturePlayback::parseBuffer(SkReadBuffer& buffer) { michael@0: for (;;) { michael@0: uint32_t tag = buffer.readUInt(); michael@0: if (SK_PICT_EOF_TAG == tag) { michael@0: break; michael@0: } michael@0: michael@0: uint32_t size = buffer.readUInt(); michael@0: if (!this->parseBufferTag(buffer, tag, size)) { michael@0: return false; // we're invalid michael@0: } michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: #ifdef SPEW_CLIP_SKIPPING michael@0: struct SkipClipRec { michael@0: int fCount; michael@0: size_t fSize; michael@0: michael@0: SkipClipRec() { michael@0: fCount = 0; michael@0: fSize = 0; michael@0: } michael@0: michael@0: void recordSkip(size_t bytes) { michael@0: fCount += 1; michael@0: fSize += bytes; michael@0: } michael@0: }; michael@0: #endif michael@0: michael@0: #ifdef SK_DEVELOPER michael@0: bool SkPicturePlayback::preDraw(int opIndex, int type) { michael@0: return false; michael@0: } michael@0: michael@0: void SkPicturePlayback::postDraw(int opIndex) { michael@0: } michael@0: #endif michael@0: michael@0: /* michael@0: * Read the next op code and chunk size from 'reader'. The returned size michael@0: * is the entire size of the chunk (including the opcode). Thus, the michael@0: * offset just prior to calling read_op_and_size + 'size' is the offset michael@0: * to the next chunk's op code. This also means that the size of a chunk michael@0: * with no arguments (just an opcode) will be 4. michael@0: */ michael@0: static DrawType read_op_and_size(SkReader32* reader, uint32_t* size) { michael@0: uint32_t temp = reader->readInt(); michael@0: uint32_t op; michael@0: if (((uint8_t) temp) == temp) { michael@0: // old skp file - no size information michael@0: op = temp; michael@0: *size = 0; michael@0: } else { michael@0: UNPACK_8_24(temp, op, *size); michael@0: if (MASK_24 == *size) { michael@0: *size = reader->readInt(); michael@0: } michael@0: } michael@0: return (DrawType) op; michael@0: } michael@0: michael@0: // The activeOps parameter is actually "const SkTDArray&". michael@0: // It represents the operations about to be drawn, as generated by some spatial michael@0: // subdivision helper class. It should already be in 'fOffset' sorted order. michael@0: void SkPicturePlayback::preLoadBitmaps(const SkTDArray& activeOps) { michael@0: if (0 == activeOps.count() || NULL == fBitmapUseOffsets) { michael@0: return; michael@0: } michael@0: michael@0: SkTDArray active; michael@0: michael@0: SkAutoTDeleteArray needToCheck(new bool[fBitmapUseOffsets->numIDs()]); michael@0: for (int i = 0; i < fBitmapUseOffsets->numIDs(); ++i) { michael@0: needToCheck.get()[i] = true; michael@0: } michael@0: michael@0: uint32_t max = ((SkPictureStateTree::Draw*)activeOps[activeOps.count()-1])->fOffset; michael@0: michael@0: for (int i = 0; i < activeOps.count(); ++i) { michael@0: SkPictureStateTree::Draw* draw = (SkPictureStateTree::Draw*) activeOps[i]; michael@0: michael@0: for (int j = 0; j < fBitmapUseOffsets->numIDs(); ++j) { michael@0: if (!needToCheck.get()[j]) { michael@0: continue; michael@0: } michael@0: michael@0: if (!fBitmapUseOffsets->overlap(j, draw->fOffset, max)) { michael@0: needToCheck.get()[j] = false; michael@0: continue; michael@0: } michael@0: michael@0: if (!fBitmapUseOffsets->includes(j, draw->fOffset)) { michael@0: continue; michael@0: } michael@0: michael@0: *active.append() = j; michael@0: needToCheck.get()[j] = false; michael@0: } michael@0: } michael@0: michael@0: for (int i = 0; i < active.count(); ++i) { michael@0: SkDebugf("preload texture %d\n", active[i]); michael@0: } michael@0: } michael@0: michael@0: void SkPicturePlayback::draw(SkCanvas& canvas, SkDrawPictureCallback* callback) { michael@0: #ifdef ENABLE_TIME_DRAW michael@0: SkAutoTime at("SkPicture::draw", 50); michael@0: #endif michael@0: michael@0: #ifdef SPEW_CLIP_SKIPPING michael@0: SkipClipRec skipRect, skipRRect, skipRegion, skipPath, skipCull; michael@0: int opCount = 0; michael@0: #endif michael@0: michael@0: #ifdef SK_BUILD_FOR_ANDROID michael@0: SkAutoMutexAcquire autoMutex(fDrawMutex); michael@0: #endif michael@0: michael@0: // kDrawComplete will be the signal that we have reached the end of michael@0: // the command stream michael@0: static const uint32_t kDrawComplete = SK_MaxU32; michael@0: michael@0: SkReader32 reader(fOpData->bytes(), fOpData->size()); michael@0: TextContainer text; michael@0: SkTDArray activeOps; michael@0: michael@0: if (NULL != fStateTree && NULL != fBoundingHierarchy) { michael@0: SkRect clipBounds; michael@0: if (canvas.getClipBounds(&clipBounds)) { michael@0: SkIRect query; michael@0: clipBounds.roundOut(&query); michael@0: fBoundingHierarchy->search(query, &activeOps); michael@0: if (activeOps.count() == 0) { michael@0: return; michael@0: } michael@0: SkTQSort( michael@0: reinterpret_cast(activeOps.begin()), michael@0: reinterpret_cast(activeOps.end()-1)); michael@0: } michael@0: } michael@0: michael@0: SkPictureStateTree::Iterator it = (NULL == fStateTree) ? michael@0: SkPictureStateTree::Iterator() : michael@0: fStateTree->getIterator(activeOps, &canvas); michael@0: michael@0: if (it.isValid()) { michael@0: uint32_t skipTo = it.draw(); michael@0: if (kDrawComplete == skipTo) { michael@0: return; michael@0: } michael@0: reader.setOffset(skipTo); michael@0: } michael@0: michael@0: this->preLoadBitmaps(activeOps); michael@0: michael@0: // Record this, so we can concat w/ it if we encounter a setMatrix() michael@0: SkMatrix initialMatrix = canvas.getTotalMatrix(); michael@0: int originalSaveCount = canvas.getSaveCount(); michael@0: michael@0: #ifdef SK_BUILD_FOR_ANDROID michael@0: fAbortCurrentPlayback = false; michael@0: #endif michael@0: michael@0: #ifdef SK_DEVELOPER michael@0: int opIndex = -1; michael@0: #endif michael@0: michael@0: while (!reader.eof()) { michael@0: if (callback && callback->abortDrawing()) { michael@0: canvas.restoreToCount(originalSaveCount); michael@0: return; michael@0: } michael@0: #ifdef SK_BUILD_FOR_ANDROID michael@0: if (fAbortCurrentPlayback) { michael@0: return; michael@0: } michael@0: #endif michael@0: michael@0: #ifdef SPEW_CLIP_SKIPPING michael@0: opCount++; michael@0: #endif michael@0: michael@0: size_t curOffset = reader.offset(); michael@0: uint32_t size; michael@0: DrawType op = read_op_and_size(&reader, &size); michael@0: size_t skipTo = 0; michael@0: if (NOOP == op) { michael@0: // NOOPs are to be ignored - do not propagate them any further michael@0: skipTo = curOffset + size; michael@0: #ifdef SK_DEVELOPER michael@0: } else { michael@0: opIndex++; michael@0: if (this->preDraw(opIndex, op)) { michael@0: skipTo = curOffset + size; michael@0: } michael@0: #endif michael@0: } michael@0: michael@0: if (0 != skipTo) { michael@0: if (it.isValid()) { michael@0: // If using a bounding box hierarchy, advance the state tree michael@0: // iterator until at or after skipTo michael@0: uint32_t adjustedSkipTo; michael@0: do { michael@0: adjustedSkipTo = it.draw(); michael@0: } while (adjustedSkipTo < skipTo); michael@0: skipTo = adjustedSkipTo; michael@0: } michael@0: if (kDrawComplete == skipTo) { michael@0: break; michael@0: } michael@0: reader.setOffset(skipTo); michael@0: continue; michael@0: } michael@0: michael@0: switch (op) { michael@0: case CLIP_PATH: { michael@0: const SkPath& path = getPath(reader); michael@0: uint32_t packed = reader.readInt(); michael@0: SkRegion::Op regionOp = ClipParams_unpackRegionOp(packed); michael@0: bool doAA = ClipParams_unpackDoAA(packed); michael@0: size_t offsetToRestore = reader.readInt(); michael@0: SkASSERT(!offsetToRestore || \ michael@0: offsetToRestore >= reader.offset()); michael@0: canvas.clipPath(path, regionOp, doAA); michael@0: if (canvas.isClipEmpty() && offsetToRestore) { michael@0: #ifdef SPEW_CLIP_SKIPPING michael@0: skipPath.recordSkip(offsetToRestore - reader.offset()); michael@0: #endif michael@0: reader.setOffset(offsetToRestore); michael@0: } michael@0: } break; michael@0: case CLIP_REGION: { michael@0: SkRegion region; michael@0: this->getRegion(reader, ®ion); michael@0: uint32_t packed = reader.readInt(); michael@0: SkRegion::Op regionOp = ClipParams_unpackRegionOp(packed); michael@0: size_t offsetToRestore = reader.readInt(); michael@0: SkASSERT(!offsetToRestore || \ michael@0: offsetToRestore >= reader.offset()); michael@0: canvas.clipRegion(region, regionOp); michael@0: if (canvas.isClipEmpty() && offsetToRestore) { michael@0: #ifdef SPEW_CLIP_SKIPPING michael@0: skipRegion.recordSkip(offsetToRestore - reader.offset()); michael@0: #endif michael@0: reader.setOffset(offsetToRestore); michael@0: } michael@0: } break; michael@0: case CLIP_RECT: { michael@0: const SkRect& rect = reader.skipT(); michael@0: uint32_t packed = reader.readInt(); michael@0: SkRegion::Op regionOp = ClipParams_unpackRegionOp(packed); michael@0: bool doAA = ClipParams_unpackDoAA(packed); michael@0: size_t offsetToRestore = reader.readInt(); michael@0: SkASSERT(!offsetToRestore || \ michael@0: offsetToRestore >= reader.offset()); michael@0: canvas.clipRect(rect, regionOp, doAA); michael@0: if (canvas.isClipEmpty() && offsetToRestore) { michael@0: #ifdef SPEW_CLIP_SKIPPING michael@0: skipRect.recordSkip(offsetToRestore - reader.offset()); michael@0: #endif michael@0: reader.setOffset(offsetToRestore); michael@0: } michael@0: } break; michael@0: case CLIP_RRECT: { michael@0: SkRRect rrect; michael@0: reader.readRRect(&rrect); michael@0: uint32_t packed = reader.readInt(); michael@0: SkRegion::Op regionOp = ClipParams_unpackRegionOp(packed); michael@0: bool doAA = ClipParams_unpackDoAA(packed); michael@0: size_t offsetToRestore = reader.readInt(); michael@0: SkASSERT(!offsetToRestore || \ michael@0: offsetToRestore >= reader.offset()); michael@0: canvas.clipRRect(rrect, regionOp, doAA); michael@0: if (canvas.isClipEmpty() && offsetToRestore) { michael@0: #ifdef SPEW_CLIP_SKIPPING michael@0: skipRRect.recordSkip(offsetToRestore - reader.offset()); michael@0: #endif michael@0: reader.setOffset(offsetToRestore); michael@0: } michael@0: } break; michael@0: case PUSH_CULL: { michael@0: const SkRect& cullRect = reader.skipT(); michael@0: size_t offsetToRestore = reader.readInt(); michael@0: if (offsetToRestore && canvas.quickReject(cullRect)) { michael@0: #ifdef SPEW_CLIP_SKIPPING michael@0: skipCull.recordSkip(offsetToRestore - reader.offset()); michael@0: #endif michael@0: reader.setOffset(offsetToRestore); michael@0: } else { michael@0: canvas.pushCull(cullRect); michael@0: } michael@0: } break; michael@0: case POP_CULL: michael@0: canvas.popCull(); michael@0: break; michael@0: case CONCAT: { michael@0: SkMatrix matrix; michael@0: this->getMatrix(reader, &matrix); michael@0: canvas.concat(matrix); michael@0: break; michael@0: } michael@0: case DRAW_BITMAP: { michael@0: const SkPaint* paint = this->getPaint(reader); michael@0: const SkBitmap& bitmap = this->getBitmap(reader); michael@0: const SkPoint& loc = reader.skipT(); michael@0: canvas.drawBitmap(bitmap, loc.fX, loc.fY, paint); michael@0: } break; michael@0: case DRAW_BITMAP_RECT_TO_RECT: { michael@0: const SkPaint* paint = this->getPaint(reader); michael@0: const SkBitmap& bitmap = this->getBitmap(reader); michael@0: const SkRect* src = this->getRectPtr(reader); // may be null michael@0: const SkRect& dst = reader.skipT(); // required michael@0: SkCanvas::DrawBitmapRectFlags flags; michael@0: flags = (SkCanvas::DrawBitmapRectFlags) reader.readInt(); michael@0: canvas.drawBitmapRectToRect(bitmap, src, dst, paint, flags); michael@0: } break; michael@0: case DRAW_BITMAP_MATRIX: { michael@0: const SkPaint* paint = this->getPaint(reader); michael@0: const SkBitmap& bitmap = this->getBitmap(reader); michael@0: SkMatrix matrix; michael@0: this->getMatrix(reader, &matrix); michael@0: canvas.drawBitmapMatrix(bitmap, matrix, paint); michael@0: } break; michael@0: case DRAW_BITMAP_NINE: { michael@0: const SkPaint* paint = this->getPaint(reader); michael@0: const SkBitmap& bitmap = this->getBitmap(reader); michael@0: const SkIRect& src = reader.skipT(); michael@0: const SkRect& dst = reader.skipT(); michael@0: canvas.drawBitmapNine(bitmap, src, dst, paint); michael@0: } break; michael@0: case DRAW_CLEAR: michael@0: canvas.clear(reader.readInt()); michael@0: break; michael@0: case DRAW_DATA: { michael@0: size_t length = reader.readInt(); michael@0: canvas.drawData(reader.skip(length), length); michael@0: // skip handles padding the read out to a multiple of 4 michael@0: } break; michael@0: case DRAW_DRRECT: { michael@0: const SkPaint& paint = *this->getPaint(reader); michael@0: SkRRect outer, inner; michael@0: reader.readRRect(&outer); michael@0: reader.readRRect(&inner); michael@0: canvas.drawDRRect(outer, inner, paint); michael@0: } break; michael@0: case BEGIN_COMMENT_GROUP: { michael@0: const char* desc = reader.readString(); michael@0: canvas.beginCommentGroup(desc); michael@0: } break; michael@0: case COMMENT: { michael@0: const char* kywd = reader.readString(); michael@0: const char* value = reader.readString(); michael@0: canvas.addComment(kywd, value); michael@0: } break; michael@0: case END_COMMENT_GROUP: { michael@0: canvas.endCommentGroup(); michael@0: } break; michael@0: case DRAW_OVAL: { michael@0: const SkPaint& paint = *this->getPaint(reader); michael@0: canvas.drawOval(reader.skipT(), paint); michael@0: } break; michael@0: case DRAW_PAINT: michael@0: canvas.drawPaint(*this->getPaint(reader)); michael@0: break; michael@0: case DRAW_PATH: { michael@0: const SkPaint& paint = *this->getPaint(reader); michael@0: canvas.drawPath(getPath(reader), paint); michael@0: } break; michael@0: case DRAW_PICTURE: michael@0: canvas.drawPicture(this->getPicture(reader)); michael@0: break; michael@0: case DRAW_POINTS: { michael@0: const SkPaint& paint = *this->getPaint(reader); michael@0: SkCanvas::PointMode mode = (SkCanvas::PointMode)reader.readInt(); michael@0: size_t count = reader.readInt(); michael@0: const SkPoint* pts = (const SkPoint*)reader.skip(sizeof(SkPoint) * count); michael@0: canvas.drawPoints(mode, count, pts, paint); michael@0: } break; michael@0: case DRAW_POS_TEXT: { michael@0: const SkPaint& paint = *this->getPaint(reader); michael@0: getText(reader, &text); michael@0: size_t points = reader.readInt(); michael@0: const SkPoint* pos = (const SkPoint*)reader.skip(points * sizeof(SkPoint)); michael@0: canvas.drawPosText(text.text(), text.length(), pos, paint); michael@0: } break; michael@0: case DRAW_POS_TEXT_TOP_BOTTOM: { michael@0: const SkPaint& paint = *this->getPaint(reader); michael@0: getText(reader, &text); michael@0: size_t points = reader.readInt(); michael@0: const SkPoint* pos = (const SkPoint*)reader.skip(points * sizeof(SkPoint)); michael@0: const SkScalar top = reader.readScalar(); michael@0: const SkScalar bottom = reader.readScalar(); michael@0: if (!canvas.quickRejectY(top, bottom)) { michael@0: canvas.drawPosText(text.text(), text.length(), pos, paint); michael@0: } michael@0: } break; michael@0: case DRAW_POS_TEXT_H: { michael@0: const SkPaint& paint = *this->getPaint(reader); michael@0: getText(reader, &text); michael@0: size_t xCount = reader.readInt(); michael@0: const SkScalar constY = reader.readScalar(); michael@0: const SkScalar* xpos = (const SkScalar*)reader.skip(xCount * sizeof(SkScalar)); michael@0: canvas.drawPosTextH(text.text(), text.length(), xpos, constY, michael@0: paint); michael@0: } break; michael@0: case DRAW_POS_TEXT_H_TOP_BOTTOM: { michael@0: const SkPaint& paint = *this->getPaint(reader); michael@0: getText(reader, &text); michael@0: size_t xCount = reader.readInt(); michael@0: const SkScalar* xpos = (const SkScalar*)reader.skip((3 + xCount) * sizeof(SkScalar)); michael@0: const SkScalar top = *xpos++; michael@0: const SkScalar bottom = *xpos++; michael@0: const SkScalar constY = *xpos++; michael@0: if (!canvas.quickRejectY(top, bottom)) { michael@0: canvas.drawPosTextH(text.text(), text.length(), xpos, michael@0: constY, paint); michael@0: } michael@0: } break; michael@0: case DRAW_RECT: { michael@0: const SkPaint& paint = *this->getPaint(reader); michael@0: canvas.drawRect(reader.skipT(), paint); michael@0: } break; michael@0: case DRAW_RRECT: { michael@0: const SkPaint& paint = *this->getPaint(reader); michael@0: SkRRect rrect; michael@0: reader.readRRect(&rrect); michael@0: canvas.drawRRect(rrect, paint); michael@0: } break; michael@0: case DRAW_SPRITE: { michael@0: const SkPaint* paint = this->getPaint(reader); michael@0: const SkBitmap& bitmap = this->getBitmap(reader); michael@0: int left = reader.readInt(); michael@0: int top = reader.readInt(); michael@0: canvas.drawSprite(bitmap, left, top, paint); michael@0: } break; michael@0: case DRAW_TEXT: { michael@0: const SkPaint& paint = *this->getPaint(reader); michael@0: this->getText(reader, &text); michael@0: SkScalar x = reader.readScalar(); michael@0: SkScalar y = reader.readScalar(); michael@0: canvas.drawText(text.text(), text.length(), x, y, paint); michael@0: } break; michael@0: case DRAW_TEXT_TOP_BOTTOM: { michael@0: const SkPaint& paint = *this->getPaint(reader); michael@0: this->getText(reader, &text); michael@0: const SkScalar* ptr = (const SkScalar*)reader.skip(4 * sizeof(SkScalar)); michael@0: // ptr[0] == x michael@0: // ptr[1] == y michael@0: // ptr[2] == top michael@0: // ptr[3] == bottom michael@0: if (!canvas.quickRejectY(ptr[2], ptr[3])) { michael@0: canvas.drawText(text.text(), text.length(), ptr[0], ptr[1], michael@0: paint); michael@0: } michael@0: } break; michael@0: case DRAW_TEXT_ON_PATH: { michael@0: const SkPaint& paint = *this->getPaint(reader); michael@0: getText(reader, &text); michael@0: const SkPath& path = this->getPath(reader); michael@0: SkMatrix matrix; michael@0: this->getMatrix(reader, &matrix); michael@0: canvas.drawTextOnPath(text.text(), text.length(), path, &matrix, paint); michael@0: } break; michael@0: case DRAW_VERTICES: { michael@0: SkAutoTUnref xfer; michael@0: const SkPaint& paint = *this->getPaint(reader); michael@0: DrawVertexFlags flags = (DrawVertexFlags)reader.readInt(); michael@0: SkCanvas::VertexMode vmode = (SkCanvas::VertexMode)reader.readInt(); michael@0: int vCount = reader.readInt(); michael@0: const SkPoint* verts = (const SkPoint*)reader.skip( michael@0: vCount * sizeof(SkPoint)); michael@0: const SkPoint* texs = NULL; michael@0: const SkColor* colors = NULL; michael@0: const uint16_t* indices = NULL; michael@0: int iCount = 0; michael@0: if (flags & DRAW_VERTICES_HAS_TEXS) { michael@0: texs = (const SkPoint*)reader.skip( michael@0: vCount * sizeof(SkPoint)); michael@0: } michael@0: if (flags & DRAW_VERTICES_HAS_COLORS) { michael@0: colors = (const SkColor*)reader.skip( michael@0: vCount * sizeof(SkColor)); michael@0: } michael@0: if (flags & DRAW_VERTICES_HAS_INDICES) { michael@0: iCount = reader.readInt(); michael@0: indices = (const uint16_t*)reader.skip( michael@0: iCount * sizeof(uint16_t)); michael@0: } michael@0: if (flags & DRAW_VERTICES_HAS_XFER) { michael@0: int mode = reader.readInt(); michael@0: if (mode < 0 || mode > SkXfermode::kLastMode) { michael@0: mode = SkXfermode::kModulate_Mode; michael@0: } michael@0: xfer.reset(SkXfermode::Create((SkXfermode::Mode)mode)); michael@0: } michael@0: canvas.drawVertices(vmode, vCount, verts, texs, colors, xfer, michael@0: indices, iCount, paint); michael@0: } break; michael@0: case RESTORE: michael@0: canvas.restore(); michael@0: break; michael@0: case ROTATE: michael@0: canvas.rotate(reader.readScalar()); michael@0: break; michael@0: case SAVE: michael@0: canvas.save((SkCanvas::SaveFlags) reader.readInt()); michael@0: break; michael@0: case SAVE_LAYER: { michael@0: const SkRect* boundsPtr = this->getRectPtr(reader); michael@0: const SkPaint* paint = this->getPaint(reader); michael@0: canvas.saveLayer(boundsPtr, paint, (SkCanvas::SaveFlags) reader.readInt()); michael@0: } break; michael@0: case SCALE: { michael@0: SkScalar sx = reader.readScalar(); michael@0: SkScalar sy = reader.readScalar(); michael@0: canvas.scale(sx, sy); michael@0: } break; michael@0: case SET_MATRIX: { michael@0: SkMatrix matrix; michael@0: this->getMatrix(reader, &matrix); michael@0: matrix.postConcat(initialMatrix); michael@0: canvas.setMatrix(matrix); michael@0: } break; michael@0: case SKEW: { michael@0: SkScalar sx = reader.readScalar(); michael@0: SkScalar sy = reader.readScalar(); michael@0: canvas.skew(sx, sy); michael@0: } break; michael@0: case TRANSLATE: { michael@0: SkScalar dx = reader.readScalar(); michael@0: SkScalar dy = reader.readScalar(); michael@0: canvas.translate(dx, dy); michael@0: } break; michael@0: default: michael@0: SkASSERT(0); michael@0: } michael@0: michael@0: #ifdef SK_DEVELOPER michael@0: this->postDraw(opIndex); michael@0: #endif michael@0: michael@0: if (it.isValid()) { michael@0: uint32_t skipTo = it.draw(); michael@0: if (kDrawComplete == skipTo) { michael@0: break; michael@0: } michael@0: reader.setOffset(skipTo); michael@0: } michael@0: } michael@0: michael@0: #ifdef SPEW_CLIP_SKIPPING michael@0: { michael@0: size_t size = skipRect.fSize + skipRRect.fSize + skipPath.fSize + skipRegion.fSize + michael@0: skipCull.fSize; michael@0: SkDebugf("--- Clip skips %d%% rect:%d rrect:%d path:%d rgn:%d cull:%d\n", michael@0: size * 100 / reader.offset(), skipRect.fCount, skipRRect.fCount, michael@0: skipPath.fCount, skipRegion.fCount, skipCull.fCount); michael@0: SkDebugf("--- Total ops: %d\n", opCount); michael@0: } michael@0: #endif michael@0: // this->dumpSize(); michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: #ifdef SK_DEBUG_SIZE michael@0: int SkPicturePlayback::size(size_t* sizePtr) { michael@0: int objects = bitmaps(sizePtr); michael@0: objects += paints(sizePtr); michael@0: objects += paths(sizePtr); michael@0: objects += pictures(sizePtr); michael@0: objects += regions(sizePtr); michael@0: *sizePtr = fOpData.size(); michael@0: return objects; michael@0: } michael@0: michael@0: int SkPicturePlayback::bitmaps(size_t* size) { michael@0: size_t result = 0; michael@0: for (int index = 0; index < fBitmapCount; index++) { michael@0: // const SkBitmap& bitmap = fBitmaps[index]; michael@0: result += sizeof(SkBitmap); // bitmap->size(); michael@0: } michael@0: *size = result; michael@0: return fBitmapCount; michael@0: } michael@0: michael@0: int SkPicturePlayback::paints(size_t* size) { michael@0: size_t result = 0; michael@0: for (int index = 0; index < fPaintCount; index++) { michael@0: // const SkPaint& paint = fPaints[index]; michael@0: result += sizeof(SkPaint); // paint->size(); michael@0: } michael@0: *size = result; michael@0: return fPaintCount; michael@0: } michael@0: michael@0: int SkPicturePlayback::paths(size_t* size) { michael@0: size_t result = 0; michael@0: for (int index = 0; index < fPathCount; index++) { michael@0: const SkPath& path = fPaths[index]; michael@0: result += path.flatten(NULL); michael@0: } michael@0: *size = result; michael@0: return fPathCount; michael@0: } michael@0: #endif michael@0: michael@0: #ifdef SK_DEBUG_DUMP michael@0: void SkPicturePlayback::dumpBitmap(const SkBitmap& bitmap) const { michael@0: char pBuffer[DUMP_BUFFER_SIZE]; michael@0: char* bufferPtr = pBuffer; michael@0: bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), michael@0: "BitmapData bitmap%p = {", &bitmap); michael@0: bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), michael@0: "{kWidth, %d}, ", bitmap.width()); michael@0: bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), michael@0: "{kHeight, %d}, ", bitmap.height()); michael@0: bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), michael@0: "{kRowBytes, %d}, ", bitmap.rowBytes()); michael@0: // start here; michael@0: SkDebugf("%s{0}};\n", pBuffer); michael@0: } michael@0: michael@0: void dumpMatrix(const SkMatrix& matrix) const { michael@0: SkMatrix defaultMatrix; michael@0: defaultMatrix.reset(); michael@0: char pBuffer[DUMP_BUFFER_SIZE]; michael@0: char* bufferPtr = pBuffer; michael@0: bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), michael@0: "MatrixData matrix%p = {", &matrix); michael@0: SkScalar scaleX = matrix.getScaleX(); michael@0: if (scaleX != defaultMatrix.getScaleX()) michael@0: bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), michael@0: "{kScaleX, %g}, ", SkScalarToFloat(scaleX)); michael@0: SkScalar scaleY = matrix.getScaleY(); michael@0: if (scaleY != defaultMatrix.getScaleY()) michael@0: bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), michael@0: "{kScaleY, %g}, ", SkScalarToFloat(scaleY)); michael@0: SkScalar skewX = matrix.getSkewX(); michael@0: if (skewX != defaultMatrix.getSkewX()) michael@0: bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), michael@0: "{kSkewX, %g}, ", SkScalarToFloat(skewX)); michael@0: SkScalar skewY = matrix.getSkewY(); michael@0: if (skewY != defaultMatrix.getSkewY()) michael@0: bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), michael@0: "{kSkewY, %g}, ", SkScalarToFloat(skewY)); michael@0: SkScalar translateX = matrix.getTranslateX(); michael@0: if (translateX != defaultMatrix.getTranslateX()) michael@0: bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), michael@0: "{kTranslateX, %g}, ", SkScalarToFloat(translateX)); michael@0: SkScalar translateY = matrix.getTranslateY(); michael@0: if (translateY != defaultMatrix.getTranslateY()) michael@0: bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), michael@0: "{kTranslateY, %g}, ", SkScalarToFloat(translateY)); michael@0: SkScalar perspX = matrix.getPerspX(); michael@0: if (perspX != defaultMatrix.getPerspX()) michael@0: bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), michael@0: "{kPerspX, %g}, ", perspX); michael@0: SkScalar perspY = matrix.getPerspY(); michael@0: if (perspY != defaultMatrix.getPerspY()) michael@0: bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), michael@0: "{kPerspY, %g}, ", perspY); michael@0: SkDebugf("%s{0}};\n", pBuffer); michael@0: } michael@0: michael@0: void dumpPaint(const SkPaint& paint) const { michael@0: SkPaint defaultPaint; michael@0: char pBuffer[DUMP_BUFFER_SIZE]; michael@0: char* bufferPtr = pBuffer; michael@0: bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), michael@0: "PaintPointers paintPtrs%p = {", &paint); michael@0: const SkTypeface* typeface = paint.getTypeface(); michael@0: if (typeface != defaultPaint.getTypeface()) michael@0: bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), michael@0: "{kTypeface, %p}, ", typeface); michael@0: const SkPathEffect* pathEffect = paint.getPathEffect(); michael@0: if (pathEffect != defaultPaint.getPathEffect()) michael@0: bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), michael@0: "{kPathEffect, %p}, ", pathEffect); michael@0: const SkShader* shader = paint.getShader(); michael@0: if (shader != defaultPaint.getShader()) michael@0: bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), michael@0: "{kShader, %p}, ", shader); michael@0: const SkXfermode* xfermode = paint.getXfermode(); michael@0: if (xfermode != defaultPaint.getXfermode()) michael@0: bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), michael@0: "{kXfermode, %p}, ", xfermode); michael@0: const SkMaskFilter* maskFilter = paint.getMaskFilter(); michael@0: if (maskFilter != defaultPaint.getMaskFilter()) michael@0: bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), michael@0: "{kMaskFilter, %p}, ", maskFilter); michael@0: const SkColorFilter* colorFilter = paint.getColorFilter(); michael@0: if (colorFilter != defaultPaint.getColorFilter()) michael@0: bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), michael@0: "{kColorFilter, %p}, ", colorFilter); michael@0: const SkRasterizer* rasterizer = paint.getRasterizer(); michael@0: if (rasterizer != defaultPaint.getRasterizer()) michael@0: bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), michael@0: "{kRasterizer, %p}, ", rasterizer); michael@0: const SkDrawLooper* drawLooper = paint.getLooper(); michael@0: if (drawLooper != defaultPaint.getLooper()) michael@0: bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), michael@0: "{kDrawLooper, %p}, ", drawLooper); michael@0: SkDebugf("%s{0}};\n", pBuffer); michael@0: bufferPtr = pBuffer; michael@0: bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), michael@0: "PaintScalars paintScalars%p = {", &paint); michael@0: SkScalar textSize = paint.getTextSize(); michael@0: if (textSize != defaultPaint.getTextSize()) michael@0: bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), michael@0: "{kTextSize, %g}, ", SkScalarToFloat(textSize)); michael@0: SkScalar textScaleX = paint.getTextScaleX(); michael@0: if (textScaleX != defaultPaint.getTextScaleX()) michael@0: bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), michael@0: "{kTextScaleX, %g}, ", SkScalarToFloat(textScaleX)); michael@0: SkScalar textSkewX = paint.getTextSkewX(); michael@0: if (textSkewX != defaultPaint.getTextSkewX()) michael@0: bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), michael@0: "{kTextSkewX, %g}, ", SkScalarToFloat(textSkewX)); michael@0: SkScalar strokeWidth = paint.getStrokeWidth(); michael@0: if (strokeWidth != defaultPaint.getStrokeWidth()) michael@0: bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), michael@0: "{kStrokeWidth, %g}, ", SkScalarToFloat(strokeWidth)); michael@0: SkScalar strokeMiter = paint.getStrokeMiter(); michael@0: if (strokeMiter != defaultPaint.getStrokeMiter()) michael@0: bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), michael@0: "{kStrokeMiter, %g}, ", SkScalarToFloat(strokeMiter)); michael@0: SkDebugf("%s{0}};\n", pBuffer); michael@0: bufferPtr = pBuffer; michael@0: bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), michael@0: "PaintInts = paintInts%p = {", &paint); michael@0: unsigned color = paint.getColor(); michael@0: if (color != defaultPaint.getColor()) michael@0: bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), michael@0: "{kColor, 0x%x}, ", color); michael@0: unsigned flags = paint.getFlags(); michael@0: if (flags != defaultPaint.getFlags()) michael@0: bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), michael@0: "{kFlags, 0x%x}, ", flags); michael@0: int align = paint.getTextAlign(); michael@0: if (align != defaultPaint.getTextAlign()) michael@0: bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), michael@0: "{kAlign, 0x%x}, ", align); michael@0: int strokeCap = paint.getStrokeCap(); michael@0: if (strokeCap != defaultPaint.getStrokeCap()) michael@0: bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), michael@0: "{kStrokeCap, 0x%x}, ", strokeCap); michael@0: int strokeJoin = paint.getStrokeJoin(); michael@0: if (strokeJoin != defaultPaint.getStrokeJoin()) michael@0: bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), michael@0: "{kAlign, 0x%x}, ", strokeJoin); michael@0: int style = paint.getStyle(); michael@0: if (style != defaultPaint.getStyle()) michael@0: bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), michael@0: "{kStyle, 0x%x}, ", style); michael@0: int textEncoding = paint.getTextEncoding(); michael@0: if (textEncoding != defaultPaint.getTextEncoding()) michael@0: bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), michael@0: "{kTextEncoding, 0x%x}, ", textEncoding); michael@0: SkDebugf("%s{0}};\n", pBuffer); michael@0: michael@0: SkDebugf("PaintData paint%p = {paintPtrs%p, paintScalars%p, paintInts%p};\n", michael@0: &paint, &paint, &paint, &paint); michael@0: } michael@0: michael@0: void SkPicturePlayback::dumpPath(const SkPath& path) const { michael@0: SkDebugf("path dump unimplemented\n"); michael@0: } michael@0: michael@0: void SkPicturePlayback::dumpPicture(const SkPicture& picture) const { michael@0: SkDebugf("picture dump unimplemented\n"); michael@0: } michael@0: michael@0: void SkPicturePlayback::dumpRegion(const SkRegion& region) const { michael@0: SkDebugf("region dump unimplemented\n"); michael@0: } michael@0: michael@0: int SkPicturePlayback::dumpDrawType(char* bufferPtr, char* buffer, DrawType drawType) { michael@0: return snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - buffer), michael@0: "k%s, ", DrawTypeToString(drawType)); michael@0: } michael@0: michael@0: int SkPicturePlayback::dumpInt(char* bufferPtr, char* buffer, char* name) { michael@0: return snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - buffer), michael@0: "%s:%d, ", name, getInt()); michael@0: } michael@0: michael@0: int SkPicturePlayback::dumpRect(char* bufferPtr, char* buffer, char* name) { michael@0: const SkRect* rect = fReader.skipRect(); michael@0: return snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - buffer), michael@0: "%s:{l:%g t:%g r:%g b:%g}, ", name, SkScalarToFloat(rect.fLeft), michael@0: SkScalarToFloat(rect.fTop), michael@0: SkScalarToFloat(rect.fRight), SkScalarToFloat(rect.fBottom)); michael@0: } michael@0: michael@0: int SkPicturePlayback::dumpPoint(char* bufferPtr, char* buffer, char* name) { michael@0: SkPoint pt; michael@0: getPoint(&pt); michael@0: return snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - buffer), michael@0: "%s:{x:%g y:%g}, ", name, SkScalarToFloat(pt.fX), michael@0: SkScalarToFloat(pt.fY)); michael@0: } michael@0: michael@0: void SkPicturePlayback::dumpPointArray(char** bufferPtrPtr, char* buffer, int count) { michael@0: char* bufferPtr = *bufferPtrPtr; michael@0: const SkPoint* pts = (const SkPoint*)fReadStream.getAtPos(); michael@0: fReadStream.skip(sizeof(SkPoint) * count); michael@0: bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - buffer), michael@0: "count:%d {", count); michael@0: for (int index = 0; index < count; index++) michael@0: bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - buffer), michael@0: "{x:%g y:%g}, ", SkScalarToFloat(pts[index].fX), michael@0: SkScalarToFloat(pts[index].fY)); michael@0: bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - buffer), michael@0: "} "); michael@0: *bufferPtrPtr = bufferPtr; michael@0: } michael@0: michael@0: int SkPicturePlayback::dumpPtr(char* bufferPtr, char* buffer, char* name, void* ptr) { michael@0: return snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - buffer), michael@0: "%s:%p, ", name, ptr); michael@0: } michael@0: michael@0: int SkPicturePlayback::dumpRectPtr(char* bufferPtr, char* buffer, char* name) { michael@0: char result; michael@0: fReadStream.read(&result, sizeof(result)); michael@0: if (result) michael@0: return dumpRect(bufferPtr, buffer, name); michael@0: else michael@0: return snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - buffer), michael@0: "%s:NULL, ", name); michael@0: } michael@0: michael@0: int SkPicturePlayback::dumpScalar(char* bufferPtr, char* buffer, char* name) { michael@0: return snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - buffer), michael@0: "%s:%d, ", name, getScalar()); michael@0: } michael@0: michael@0: void SkPicturePlayback::dumpText(char** bufferPtrPtr, char* buffer) { michael@0: char* bufferPtr = *bufferPtrPtr; michael@0: int length = getInt(); michael@0: bufferPtr += dumpDrawType(bufferPtr, buffer); michael@0: fReadStream.skipToAlign4(); michael@0: char* text = (char*) fReadStream.getAtPos(); michael@0: fReadStream.skip(length); michael@0: bufferPtr += dumpInt(bufferPtr, buffer, "length"); michael@0: int limit = DUMP_BUFFER_SIZE - (bufferPtr - buffer) - 2; michael@0: length >>= 1; michael@0: if (limit > length) michael@0: limit = length; michael@0: if (limit > 0) { michael@0: *bufferPtr++ = '"'; michael@0: for (int index = 0; index < limit; index++) { michael@0: *bufferPtr++ = *(unsigned short*) text; michael@0: text += sizeof(unsigned short); michael@0: } michael@0: *bufferPtr++ = '"'; michael@0: } michael@0: *bufferPtrPtr = bufferPtr; michael@0: } michael@0: michael@0: #define DUMP_DRAWTYPE(drawType) \ michael@0: bufferPtr += dumpDrawType(bufferPtr, buffer, drawType) michael@0: michael@0: #define DUMP_INT(name) \ michael@0: bufferPtr += dumpInt(bufferPtr, buffer, #name) michael@0: michael@0: #define DUMP_RECT_PTR(name) \ michael@0: bufferPtr += dumpRectPtr(bufferPtr, buffer, #name) michael@0: michael@0: #define DUMP_POINT(name) \ michael@0: bufferPtr += dumpRect(bufferPtr, buffer, #name) michael@0: michael@0: #define DUMP_RECT(name) \ michael@0: bufferPtr += dumpRect(bufferPtr, buffer, #name) michael@0: michael@0: #define DUMP_POINT_ARRAY(count) \ michael@0: dumpPointArray(&bufferPtr, buffer, count) michael@0: michael@0: #define DUMP_PTR(name, ptr) \ michael@0: bufferPtr += dumpPtr(bufferPtr, buffer, #name, (void*) ptr) michael@0: michael@0: #define DUMP_SCALAR(name) \ michael@0: bufferPtr += dumpScalar(bufferPtr, buffer, #name) michael@0: michael@0: #define DUMP_TEXT() \ michael@0: dumpText(&bufferPtr, buffer) michael@0: michael@0: void SkPicturePlayback::dumpStream() { michael@0: SkDebugf("RecordStream stream = {\n"); michael@0: DrawType drawType; michael@0: TextContainer text; michael@0: fReadStream.rewind(); michael@0: char buffer[DUMP_BUFFER_SIZE], * bufferPtr; michael@0: while (fReadStream.read(&drawType, sizeof(drawType))) { michael@0: bufferPtr = buffer; michael@0: DUMP_DRAWTYPE(drawType); michael@0: switch (drawType) { michael@0: case CLIP_PATH: { michael@0: DUMP_PTR(SkPath, &getPath()); michael@0: DUMP_INT(SkRegion::Op); michael@0: DUMP_INT(offsetToRestore); michael@0: } break; michael@0: case CLIP_REGION: { michael@0: DUMP_INT(SkRegion::Op); michael@0: DUMP_INT(offsetToRestore); michael@0: } break; michael@0: case CLIP_RECT: { michael@0: DUMP_RECT(rect); michael@0: DUMP_INT(SkRegion::Op); michael@0: DUMP_INT(offsetToRestore); michael@0: } break; michael@0: case CONCAT: michael@0: break; michael@0: case DRAW_BITMAP: { michael@0: DUMP_PTR(SkPaint, getPaint()); michael@0: DUMP_PTR(SkBitmap, &getBitmap()); michael@0: DUMP_SCALAR(left); michael@0: DUMP_SCALAR(top); michael@0: } break; michael@0: case DRAW_PAINT: michael@0: DUMP_PTR(SkPaint, getPaint()); michael@0: break; michael@0: case DRAW_PATH: { michael@0: DUMP_PTR(SkPaint, getPaint()); michael@0: DUMP_PTR(SkPath, &getPath()); michael@0: } break; michael@0: case DRAW_PICTURE: { michael@0: DUMP_PTR(SkPicture, &getPicture()); michael@0: } break; michael@0: case DRAW_POINTS: { michael@0: DUMP_PTR(SkPaint, getPaint()); michael@0: (void)getInt(); // PointMode michael@0: size_t count = getInt(); michael@0: fReadStream.skipToAlign4(); michael@0: DUMP_POINT_ARRAY(count); michael@0: } break; michael@0: case DRAW_POS_TEXT: { michael@0: DUMP_PTR(SkPaint, getPaint()); michael@0: DUMP_TEXT(); michael@0: size_t points = getInt(); michael@0: fReadStream.skipToAlign4(); michael@0: DUMP_POINT_ARRAY(points); michael@0: } break; michael@0: case DRAW_POS_TEXT_H: { michael@0: DUMP_PTR(SkPaint, getPaint()); michael@0: DUMP_TEXT(); michael@0: size_t points = getInt(); michael@0: fReadStream.skipToAlign4(); michael@0: DUMP_SCALAR(top); michael@0: DUMP_SCALAR(bottom); michael@0: DUMP_SCALAR(constY); michael@0: DUMP_POINT_ARRAY(points); michael@0: } break; michael@0: case DRAW_RECT: { michael@0: DUMP_PTR(SkPaint, getPaint()); michael@0: DUMP_RECT(rect); michael@0: } break; michael@0: case DRAW_SPRITE: { michael@0: DUMP_PTR(SkPaint, getPaint()); michael@0: DUMP_PTR(SkBitmap, &getBitmap()); michael@0: DUMP_SCALAR(left); michael@0: DUMP_SCALAR(top); michael@0: } break; michael@0: case DRAW_TEXT: { michael@0: DUMP_PTR(SkPaint, getPaint()); michael@0: DUMP_TEXT(); michael@0: DUMP_SCALAR(x); michael@0: DUMP_SCALAR(y); michael@0: } break; michael@0: case DRAW_TEXT_ON_PATH: { michael@0: DUMP_PTR(SkPaint, getPaint()); michael@0: DUMP_TEXT(); michael@0: DUMP_PTR(SkPath, &getPath()); michael@0: } break; michael@0: case RESTORE: michael@0: break; michael@0: case ROTATE: michael@0: DUMP_SCALAR(rotate); michael@0: break; michael@0: case SAVE: michael@0: DUMP_INT(SkCanvas::SaveFlags); michael@0: break; michael@0: case SAVE_LAYER: { michael@0: DUMP_RECT_PTR(layer); michael@0: DUMP_PTR(SkPaint, getPaint()); michael@0: DUMP_INT(SkCanvas::SaveFlags); michael@0: } break; michael@0: case SCALE: { michael@0: DUMP_SCALAR(sx); michael@0: DUMP_SCALAR(sy); michael@0: } break; michael@0: case SKEW: { michael@0: DUMP_SCALAR(sx); michael@0: DUMP_SCALAR(sy); michael@0: } break; michael@0: case TRANSLATE: { michael@0: DUMP_SCALAR(dx); michael@0: DUMP_SCALAR(dy); michael@0: } break; michael@0: default: michael@0: SkASSERT(0); michael@0: } michael@0: SkDebugf("%s\n", buffer); michael@0: } michael@0: } michael@0: michael@0: void SkPicturePlayback::dump() const { michael@0: char pBuffer[DUMP_BUFFER_SIZE]; michael@0: char* bufferPtr = pBuffer; michael@0: int index; michael@0: if (fBitmapCount > 0) michael@0: SkDebugf("// bitmaps (%d)\n", fBitmapCount); michael@0: for (index = 0; index < fBitmapCount; index++) { michael@0: const SkBitmap& bitmap = fBitmaps[index]; michael@0: dumpBitmap(bitmap); michael@0: } michael@0: if (fBitmapCount > 0) michael@0: bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), michael@0: "Bitmaps bitmaps = {"); michael@0: for (index = 0; index < fBitmapCount; index++) michael@0: bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), michael@0: "bitmap%p, ", &fBitmaps[index]); michael@0: if (fBitmapCount > 0) michael@0: SkDebugf("%s0};\n", pBuffer); michael@0: michael@0: michael@0: if (fPaintCount > 0) michael@0: SkDebugf("// paints (%d)\n", fPaintCount); michael@0: for (index = 0; index < fPaintCount; index++) { michael@0: const SkPaint& paint = fPaints[index]; michael@0: dumpPaint(paint); michael@0: } michael@0: bufferPtr = pBuffer; michael@0: if (fPaintCount > 0) michael@0: bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), michael@0: "Paints paints = {"); michael@0: for (index = 0; index < fPaintCount; index++) michael@0: bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), michael@0: "paint%p, ", &fPaints[index]); michael@0: if (fPaintCount > 0) michael@0: SkDebugf("%s0};\n", pBuffer); michael@0: michael@0: for (index = 0; index < fPathCount; index++) { michael@0: const SkPath& path = fPaths[index]; michael@0: dumpPath(path); michael@0: } michael@0: bufferPtr = pBuffer; michael@0: if (fPathCount > 0) michael@0: bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), michael@0: "Paths paths = {"); michael@0: for (index = 0; index < fPathCount; index++) michael@0: bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), michael@0: "path%p, ", &fPaths[index]); michael@0: if (fPathCount > 0) michael@0: SkDebugf("%s0};\n", pBuffer); michael@0: michael@0: for (index = 0; index < fPictureCount; index++) { michael@0: dumpPicture(*fPictureRefs[index]); michael@0: } michael@0: bufferPtr = pBuffer; michael@0: if (fPictureCount > 0) michael@0: bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), michael@0: "Pictures pictures = {"); michael@0: for (index = 0; index < fPictureCount; index++) michael@0: bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer), michael@0: "picture%p, ", fPictureRefs[index]); michael@0: if (fPictureCount > 0) michael@0: SkDebugf("%s0};\n", pBuffer); michael@0: michael@0: const_cast(this)->dumpStream(); michael@0: } michael@0: michael@0: #endif