Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / skia / src / core / SkPictureReplacementPlayback.cpp
1
2 /*
3  * Copyright 2014 Google Inc.
4  * 
5  * Use of this source code is governed by a BSD-style license that can be
6  * found in the LICENSE file.
7  */
8
9 #include "SkCanvas.h"
10 #include "SkPicture.h"
11 #include "SkPictureData.h"
12 #include "SkPictureReplacementPlayback.h"
13
14
15 SkPictureReplacementPlayback::PlaybackReplacements::ReplacementInfo*
16 SkPictureReplacementPlayback::PlaybackReplacements::push() {
17     SkDEBUGCODE(this->validate());
18     return fReplacements.push();
19 }
20
21 void SkPictureReplacementPlayback::PlaybackReplacements::freeAll() {
22     for (int i = 0; i < fReplacements.count(); ++i) {
23         SkDELETE(fReplacements[i].fBM);
24     }
25     fReplacements.reset();
26 }
27
28 #ifdef SK_DEBUG
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);
33
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);
37         }
38     }
39 }
40 #endif
41
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
51         }
52     }
53
54     return NULL;
55 }
56
57 bool SkPictureReplacementPlayback::replaceOps(SkPictureStateTree::Iterator* iter,
58                                               SkReader32* reader,
59                                               SkCanvas* canvas,
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());
65         if (NULL != temp) {
66             SkASSERT(NULL != temp->fBM);
67             SkASSERT(NULL != temp->fPaint);
68             canvas->save();
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);
75             canvas->restore();
76
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
80                 canvas->save();
81
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.
88
89                 uint32_t skipTo;
90                 do {
91                     skipTo = iter->nextDraw();
92                     if (SkPictureStateTree::Iterator::kDrawComplete == skipTo) {
93                         break;
94                     }
95
96                     if (skipTo <= temp->fStop) {
97                         reader->setOffset(skipTo);
98                         uint32_t size;
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) {
105                             canvas->save();
106                         }
107                     }
108                 } while (skipTo <= temp->fStop);
109
110                 if (SkPictureStateTree::Iterator::kDrawComplete == skipTo) {
111                     reader->setOffset(reader->size());      // skip to end
112                     return true;
113                 }
114
115                 reader->setOffset(skipTo);
116             } else {
117                 reader->setOffset(temp->fStop);
118                 uint32_t size;
119                 SkDEBUGCODE(DrawType op = ) ReadOpAndSize(reader, &size);
120                 SkASSERT(RESTORE == op);
121             }
122
123             return true;
124         }
125     }
126
127     return false;
128 }
129
130 void SkPictureReplacementPlayback::draw(SkCanvas* canvas, SkDrawPictureCallback* callback) {
131     AutoResetOpID aroi(this);
132     SkASSERT(0 == fCurOffset);
133
134     SkPictureStateTree::Iterator it;
135
136     if (!this->initIterator(&it, canvas, fActiveOpsList)) {
137         return;  // nothing to draw
138     }
139
140     SkReader32 reader(fPictureData->opData()->bytes(), fPictureData->opData()->size());
141
142     StepIterator(&it, &reader);
143
144     // Record this, so we can concat w/ it if we encounter a setMatrix()
145     SkMatrix initialMatrix = canvas->getTotalMatrix();
146
147     SkAutoCanvasRestore acr(canvas, false);
148
149     while (!reader.eof()) {
150         if (NULL != callback && callback->abortDrawing()) {
151             return;
152         }
153
154         if (this->replaceOps(&it, &reader, canvas, initialMatrix)) {
155             continue;
156         }
157
158         fCurOffset = reader.offset();
159         uint32_t size;
160         DrawType op = ReadOpAndSize(&reader, &size);
161         if (NOOP == op) {
162             // NOOPs are to be ignored - do not propagate them any further
163             SkipIterTo(&it, &reader, fCurOffset + size);
164             continue;
165         }
166
167         this->handleOp(&reader, op, size, canvas, initialMatrix);
168
169         StepIterator(&it, &reader);
170     }
171 }