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