3 * Copyright 2014 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
10 #include "SkPicture.h"
11 #include "SkPictureData.h"
12 #include "SkPictureReplacementPlayback.h"
15 SkPictureReplacementPlayback::PlaybackReplacements::ReplacementInfo*
16 SkPictureReplacementPlayback::PlaybackReplacements::push() {
17 SkDEBUGCODE(this->validate());
18 return fReplacements.push();
21 void SkPictureReplacementPlayback::PlaybackReplacements::freeAll() {
22 for (int i = 0; i < fReplacements.count(); ++i) {
23 SkDELETE(fReplacements[i].fBM);
25 fReplacements.reset();
29 void SkPictureReplacementPlayback::PlaybackReplacements::validate() const {
30 // Check that the ranges are monotonically increasing and non-overlapping
31 if (fReplacements.count() > 0) {
32 SkASSERT(fReplacements[0].fStart < fReplacements[0].fStop);
34 for (int i = 1; i < fReplacements.count(); ++i) {
35 SkASSERT(fReplacements[i].fStart < fReplacements[i].fStop);
36 SkASSERT(fReplacements[i - 1].fStop < fReplacements[i].fStart);
42 // TODO: Replace with hash or pass in "lastLookedUp" hint
43 SkPictureReplacementPlayback::PlaybackReplacements::ReplacementInfo*
44 SkPictureReplacementPlayback::PlaybackReplacements::lookupByStart(size_t start) {
45 SkDEBUGCODE(this->validate());
46 for (int i = 0; i < fReplacements.count(); ++i) {
47 if (start == fReplacements[i].fStart) {
48 return &fReplacements[i];
49 } else if (start < fReplacements[i].fStart) {
50 return NULL; // the ranges are monotonically increasing and non-overlapping
57 bool SkPictureReplacementPlayback::replaceOps(SkPictureStateTree::Iterator* iter,
60 const SkMatrix& initialMatrix) {
61 if (NULL != fReplacements) {
62 // Potentially replace a block of operations with a single drawBitmap call
63 PlaybackReplacements::ReplacementInfo* temp =
64 fReplacements->lookupByStart(reader->offset());
66 SkASSERT(NULL != temp->fBM);
67 SkASSERT(NULL != temp->fPaint);
69 canvas->setMatrix(initialMatrix);
70 SkRect src = SkRect::Make(temp->fSrcRect);
71 SkRect dst = SkRect::MakeXYWH(temp->fPos.fX, temp->fPos.fY,
72 temp->fSrcRect.width(),
73 temp->fSrcRect.height());
74 canvas->drawBitmapRectToRect(*temp->fBM, &src, dst, temp->fPaint);
77 if (iter->isValid()) {
78 // This save is needed since the BBH will automatically issue
79 // a restore to balanced the saveLayer we're skipping
82 // At this point we know that the PictureStateTree was aiming
83 // for some draw op within temp's saveLayer (although potentially
84 // in a separate saveLayer nested inside it).
85 // We need to skip all the operations inside temp's range
86 // along with all the associated state changes but update
87 // the state tree to the first operation outside temp's range.
91 skipTo = iter->nextDraw();
92 if (SkPictureStateTree::Iterator::kDrawComplete == skipTo) {
96 if (skipTo <= temp->fStop) {
97 reader->setOffset(skipTo);
99 DrawType op = ReadOpAndSize(reader, &size);
100 // Since we are relying on the normal SkPictureStateTree
101 // playback we need to convert any nested saveLayer calls
102 // it may issue into saves (so that all its internal
103 // restores will be balanced).
104 if (SAVE_LAYER == op) {
108 } while (skipTo <= temp->fStop);
110 if (SkPictureStateTree::Iterator::kDrawComplete == skipTo) {
111 reader->setOffset(reader->size()); // skip to end
115 reader->setOffset(skipTo);
117 reader->setOffset(temp->fStop);
119 SkDEBUGCODE(DrawType op = ) ReadOpAndSize(reader, &size);
120 SkASSERT(RESTORE == op);
130 void SkPictureReplacementPlayback::draw(SkCanvas* canvas, SkDrawPictureCallback* callback) {
131 AutoResetOpID aroi(this);
132 SkASSERT(0 == fCurOffset);
134 SkPictureStateTree::Iterator it;
136 if (!this->initIterator(&it, canvas, fActiveOpsList)) {
137 return; // nothing to draw
140 SkReader32 reader(fPictureData->opData()->bytes(), fPictureData->opData()->size());
142 StepIterator(&it, &reader);
144 // Record this, so we can concat w/ it if we encounter a setMatrix()
145 SkMatrix initialMatrix = canvas->getTotalMatrix();
147 SkAutoCanvasRestore acr(canvas, false);
149 while (!reader.eof()) {
150 if (NULL != callback && callback->abortDrawing()) {
154 if (this->replaceOps(&it, &reader, canvas, initialMatrix)) {
158 fCurOffset = reader.offset();
160 DrawType op = ReadOpAndSize(&reader, &size);
162 // NOOPs are to be ignored - do not propagate them any further
163 SkipIterTo(&it, &reader, fCurOffset + size);
167 this->handleOp(&reader, op, size, canvas, initialMatrix);
169 StepIterator(&it, &reader);