| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkCanvas.h" | 8 #include "SkCanvas.h" |
| 9 #include "SkPictureData.h" | 9 #include "SkPictureData.h" |
| 10 #include "SkPicturePlayback.h" | 10 #include "SkPicturePlayback.h" |
| 11 #include "SkPictureRecord.h" | 11 #include "SkPictureRecord.h" |
| 12 #include "SkPictureStateTree.h" | 12 #include "SkPictureStateTree.h" |
| 13 #include "SkReader32.h" | 13 #include "SkReader32.h" |
| 14 #include "SkTDArray.h" | 14 #include "SkTDArray.h" |
| 15 #include "SkTypes.h" | 15 #include "SkTypes.h" |
| 16 | 16 |
| 17 SkPicturePlayback::PlaybackReplacements::ReplacementInfo* | |
| 18 SkPicturePlayback::PlaybackReplacements::push() { | |
| 19 SkDEBUGCODE(this->validate()); | |
| 20 return fReplacements.push(); | |
| 21 } | |
| 22 | |
| 23 void SkPicturePlayback::PlaybackReplacements::freeAll() { | |
| 24 for (int i = 0; i < fReplacements.count(); ++i) { | |
| 25 SkDELETE(fReplacements[i].fBM); | |
| 26 } | |
| 27 fReplacements.reset(); | |
| 28 } | |
| 29 | |
| 30 #ifdef SK_DEBUG | |
| 31 void SkPicturePlayback::PlaybackReplacements::validate() const { | |
| 32 // Check that the ranges are monotonically increasing and non-overlapping | |
| 33 if (fReplacements.count() > 0) { | |
| 34 SkASSERT(fReplacements[0].fStart < fReplacements[0].fStop); | |
| 35 | |
| 36 for (int i = 1; i < fReplacements.count(); ++i) { | |
| 37 SkASSERT(fReplacements[i].fStart < fReplacements[i].fStop); | |
| 38 SkASSERT(fReplacements[i - 1].fStop < fReplacements[i].fStart); | |
| 39 } | |
| 40 } | |
| 41 } | |
| 42 #endif | |
| 43 | |
| 44 // TODO: Replace with hash or pass in "lastLookedUp" hint | |
| 45 SkPicturePlayback::PlaybackReplacements::ReplacementInfo* | |
| 46 SkPicturePlayback::PlaybackReplacements::lookupByStart(size_t start) { | |
| 47 SkDEBUGCODE(this->validate()); | |
| 48 for (int i = 0; i < fReplacements.count(); ++i) { | |
| 49 if (start == fReplacements[i].fStart) { | |
| 50 return &fReplacements[i]; | |
| 51 } else if (start < fReplacements[i].fStart) { | |
| 52 return NULL; // the ranges are monotonically increasing and non-ove
rlapping | |
| 53 } | |
| 54 } | |
| 55 | |
| 56 return NULL; | |
| 57 } | |
| 58 | |
| 59 /* | 17 /* |
| 60 * Read the next op code and chunk size from 'reader'. The returned size | 18 * Read the next op code and chunk size from 'reader'. The returned size |
| 61 * is the entire size of the chunk (including the opcode). Thus, the | 19 * is the entire size of the chunk (including the opcode). Thus, the |
| 62 * offset just prior to calling ReadOpAndSize + 'size' is the offset | 20 * offset just prior to calling ReadOpAndSize + 'size' is the offset |
| 63 * to the next chunk's op code. This also means that the size of a chunk | 21 * to the next chunk's op code. This also means that the size of a chunk |
| 64 * with no arguments (just an opcode) will be 4. | 22 * with no arguments (just an opcode) will be 4. |
| 65 */ | 23 */ |
| 66 DrawType SkPicturePlayback::ReadOpAndSize(SkReader32* reader, uint32_t* size) { | 24 DrawType SkPicturePlayback::ReadOpAndSize(SkReader32* reader, uint32_t* size) { |
| 67 uint32_t temp = reader->readInt(); | 25 uint32_t temp = reader->readInt(); |
| 68 uint32_t op; | 26 uint32_t op; |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 if (0 == activeOpsList->numOps()) { | 88 if (0 == activeOpsList->numOps()) { |
| 131 return false; // nothing to draw | 89 return false; // nothing to draw |
| 132 } | 90 } |
| 133 | 91 |
| 134 fPictureData->fStateTree->initIterator(iter, activeOpsList->fOps, canvas
); | 92 fPictureData->fStateTree->initIterator(iter, activeOpsList->fOps, canvas
); |
| 135 } | 93 } |
| 136 | 94 |
| 137 return true; | 95 return true; |
| 138 } | 96 } |
| 139 | 97 |
| 140 bool SkPicturePlayback::replaceOps(SkPictureStateTree::Iterator* iter, | |
| 141 SkReader32* reader, | |
| 142 SkCanvas* canvas, | |
| 143 const SkMatrix& initialMatrix) { | |
| 144 if (NULL != fReplacements) { | |
| 145 // Potentially replace a block of operations with a single drawBitmap ca
ll | |
| 146 SkPicturePlayback::PlaybackReplacements::ReplacementInfo* temp = | |
| 147 fReplacements->lookupByStart(reader-
>offset()); | |
| 148 if (NULL != temp) { | |
| 149 SkASSERT(NULL != temp->fBM); | |
| 150 SkASSERT(NULL != temp->fPaint); | |
| 151 canvas->save(); | |
| 152 canvas->setMatrix(initialMatrix); | |
| 153 SkRect src = SkRect::Make(temp->fSrcRect); | |
| 154 SkRect dst = SkRect::MakeXYWH(temp->fPos.fX, temp->fPos.fY, | |
| 155 temp->fSrcRect.width(), | |
| 156 temp->fSrcRect.height()); | |
| 157 canvas->drawBitmapRectToRect(*temp->fBM, &src, dst, temp->fPaint); | |
| 158 canvas->restore(); | |
| 159 | |
| 160 if (iter->isValid()) { | |
| 161 // This save is needed since the BBH will automatically issue | |
| 162 // a restore to balanced the saveLayer we're skipping | |
| 163 canvas->save(); | |
| 164 | |
| 165 // At this point we know that the PictureStateTree was aiming | |
| 166 // for some draw op within temp's saveLayer (although potentiall
y | |
| 167 // in a separate saveLayer nested inside it). | |
| 168 // We need to skip all the operations inside temp's range | |
| 169 // along with all the associated state changes but update | |
| 170 // the state tree to the first operation outside temp's range. | |
| 171 | |
| 172 uint32_t skipTo; | |
| 173 do { | |
| 174 skipTo = iter->nextDraw(); | |
| 175 if (SkPictureStateTree::Iterator::kDrawComplete == skipTo) { | |
| 176 break; | |
| 177 } | |
| 178 | |
| 179 if (skipTo <= temp->fStop) { | |
| 180 reader->setOffset(skipTo); | |
| 181 uint32_t size; | |
| 182 DrawType op = ReadOpAndSize(reader, &size); | |
| 183 // Since we are relying on the normal SkPictureStateTree | |
| 184 // playback we need to convert any nested saveLayer call
s | |
| 185 // it may issue into saves (so that all its internal | |
| 186 // restores will be balanced). | |
| 187 if (SAVE_LAYER == op) { | |
| 188 canvas->save(); | |
| 189 } | |
| 190 } | |
| 191 } while (skipTo <= temp->fStop); | |
| 192 | |
| 193 if (SkPictureStateTree::Iterator::kDrawComplete == skipTo) { | |
| 194 reader->setOffset(reader->size()); // skip to end | |
| 195 return true; | |
| 196 } | |
| 197 | |
| 198 reader->setOffset(skipTo); | |
| 199 } else { | |
| 200 reader->setOffset(temp->fStop); | |
| 201 uint32_t size; | |
| 202 SkDEBUGCODE(DrawType op = ) ReadOpAndSize(reader, &size); | |
| 203 SkASSERT(RESTORE == op); | |
| 204 } | |
| 205 | |
| 206 return true; | |
| 207 } | |
| 208 } | |
| 209 | |
| 210 return false; | |
| 211 } | |
| 212 | |
| 213 // If 'iter' is valid use it to skip forward through the picture. | 98 // If 'iter' is valid use it to skip forward through the picture. |
| 214 void SkPicturePlayback::StepIterator(SkPictureStateTree::Iterator* iter, SkReade
r32* reader) { | 99 void SkPicturePlayback::StepIterator(SkPictureStateTree::Iterator* iter, SkReade
r32* reader) { |
| 215 if (iter->isValid()) { | 100 if (iter->isValid()) { |
| 216 uint32_t skipTo = iter->nextDraw(); | 101 uint32_t skipTo = iter->nextDraw(); |
| 217 if (SkPictureStateTree::Iterator::kDrawComplete == skipTo) { | 102 if (SkPictureStateTree::Iterator::kDrawComplete == skipTo) { |
| 218 reader->setOffset(reader->size()); // skip to end | 103 reader->setOffset(reader->size()); // skip to end |
| 219 } else { | 104 } else { |
| 220 reader->setOffset(skipTo); | 105 reader->setOffset(skipTo); |
| 221 } | 106 } |
| 222 } | 107 } |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 // Record this, so we can concat w/ it if we encounter a setMatrix() | 148 // Record this, so we can concat w/ it if we encounter a setMatrix() |
| 264 SkMatrix initialMatrix = canvas->getTotalMatrix(); | 149 SkMatrix initialMatrix = canvas->getTotalMatrix(); |
| 265 | 150 |
| 266 SkAutoCanvasRestore acr(canvas, false); | 151 SkAutoCanvasRestore acr(canvas, false); |
| 267 | 152 |
| 268 while (!reader.eof()) { | 153 while (!reader.eof()) { |
| 269 if (NULL != callback && callback->abortDrawing()) { | 154 if (NULL != callback && callback->abortDrawing()) { |
| 270 return; | 155 return; |
| 271 } | 156 } |
| 272 | 157 |
| 273 if (this->replaceOps(&it, &reader, canvas, initialMatrix)) { | |
| 274 continue; | |
| 275 } | |
| 276 | |
| 277 fCurOffset = reader.offset(); | 158 fCurOffset = reader.offset(); |
| 278 uint32_t size; | 159 uint32_t size; |
| 279 DrawType op = ReadOpAndSize(&reader, &size); | 160 DrawType op = ReadOpAndSize(&reader, &size); |
| 280 if (NOOP == op) { | 161 if (NOOP == op) { |
| 281 // NOOPs are to be ignored - do not propagate them any further | 162 // NOOPs are to be ignored - do not propagate them any further |
| 282 SkipIterTo(&it, &reader, fCurOffset + size); | 163 SkipIterTo(&it, &reader, fCurOffset + size); |
| 283 continue; | 164 continue; |
| 284 } | 165 } |
| 285 | 166 |
| 286 this->handleOp(&reader, op, size, canvas, initialMatrix); | 167 this->handleOp(&reader, op, size, canvas, initialMatrix); |
| (...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 596 case TRANSLATE: { | 477 case TRANSLATE: { |
| 597 SkScalar dx = reader->readScalar(); | 478 SkScalar dx = reader->readScalar(); |
| 598 SkScalar dy = reader->readScalar(); | 479 SkScalar dy = reader->readScalar(); |
| 599 canvas->translate(dx, dy); | 480 canvas->translate(dx, dy); |
| 600 } break; | 481 } break; |
| 601 default: | 482 default: |
| 602 SkASSERT(0); | 483 SkASSERT(0); |
| 603 } | 484 } |
| 604 } | 485 } |
| 605 | 486 |
| OLD | NEW |