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 "SkPatchUtils.h" | 9 #include "SkPatchUtils.h" |
10 #include "SkPictureData.h" | 10 #include "SkPictureData.h" |
11 #include "SkPicturePlayback.h" | 11 #include "SkPicturePlayback.h" |
12 #include "SkPictureRecord.h" | 12 #include "SkPictureRecord.h" |
13 #include "SkPictureStateTree.h" | |
14 #include "SkReader32.h" | 13 #include "SkReader32.h" |
15 #include "SkTextBlob.h" | 14 #include "SkTextBlob.h" |
16 #include "SkTDArray.h" | 15 #include "SkTDArray.h" |
17 #include "SkTypes.h" | 16 #include "SkTypes.h" |
18 | 17 |
19 /* | 18 /* |
20 * Read the next op code and chunk size from 'reader'. The returned size | 19 * Read the next op code and chunk size from 'reader'. The returned size |
21 * is the entire size of the chunk (including the opcode). Thus, the | 20 * is the entire size of the chunk (including the opcode). Thus, the |
22 * offset just prior to calling ReadOpAndSize + 'size' is the offset | 21 * offset just prior to calling ReadOpAndSize + 'size' is the offset |
23 * to the next chunk's op code. This also means that the size of a chunk | 22 * to the next chunk's op code. This also means that the size of a chunk |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 void get_text(SkReader32* reader, TextContainer* text) { | 58 void get_text(SkReader32* reader, TextContainer* text) { |
60 size_t length = text->fByteLength = reader->readInt(); | 59 size_t length = text->fByteLength = reader->readInt(); |
61 text->fText = (const char*)reader->skip(length); | 60 text->fText = (const char*)reader->skip(length); |
62 } | 61 } |
63 | 62 |
64 // FIXME: SkBitmaps are stateful, so we need to copy them to play back in multip
le threads. | 63 // FIXME: SkBitmaps are stateful, so we need to copy them to play back in multip
le threads. |
65 static SkBitmap shallow_copy(const SkBitmap& bitmap) { | 64 static SkBitmap shallow_copy(const SkBitmap& bitmap) { |
66 return bitmap; | 65 return bitmap; |
67 } | 66 } |
68 | 67 |
69 const SkPicture::OperationList* SkPicturePlayback::getActiveOps(const SkCanvas*
canvas) { | |
70 | |
71 if (fUseBBH) { | |
72 SkRect clipBounds; | |
73 if (canvas->getClipBounds(&clipBounds)) { | |
74 return fPictureData->getActiveOps(clipBounds); | |
75 } | |
76 } | |
77 | |
78 return NULL; | |
79 } | |
80 | |
81 // Initialize the state tree iterator. Return false if there is nothing left to
draw. | |
82 bool SkPicturePlayback::initIterator(SkPictureStateTree::Iterator* iter, | |
83 SkCanvas* canvas, | |
84 const SkPicture::OperationList *activeOpsLi
st) { | |
85 | |
86 if (activeOpsList) { | |
87 if (0 == activeOpsList->numOps()) { | |
88 return false; // nothing to draw | |
89 } | |
90 | |
91 fPictureData->initIterator(iter, activeOpsList->fOps, canvas); | |
92 } | |
93 | |
94 return true; | |
95 } | |
96 | |
97 // If 'iter' is valid use it to skip forward through the picture. | |
98 void SkPicturePlayback::StepIterator(SkPictureStateTree::Iterator* iter, SkReade
r32* reader) { | |
99 if (iter->isValid()) { | |
100 uint32_t skipTo = iter->nextDraw(); | |
101 if (SkPictureStateTree::Iterator::kDrawComplete == skipTo) { | |
102 reader->setOffset(reader->size()); // skip to end | |
103 } else { | |
104 reader->setOffset(skipTo); | |
105 } | |
106 } | |
107 } | |
108 | |
109 // Update the iterator and state tree to catch up with the skipped ops. | |
110 void SkPicturePlayback::SkipIterTo(SkPictureStateTree::Iterator* iter, | |
111 SkReader32* reader, | |
112 uint32_t skipTo) { | |
113 SkASSERT(skipTo <= reader->size()); | |
114 SkASSERT(reader->offset() <= skipTo); // should only be skipping forward | |
115 | |
116 if (iter->isValid()) { | |
117 // If using a bounding box hierarchy, advance the state tree | |
118 // iterator until at or after skipTo | |
119 uint32_t adjustedSkipTo; | |
120 do { | |
121 adjustedSkipTo = iter->nextDraw(); | |
122 } while (adjustedSkipTo < skipTo); | |
123 skipTo = adjustedSkipTo; | |
124 } | |
125 if (SkPictureStateTree::Iterator::kDrawComplete == skipTo) { | |
126 reader->setOffset(reader->size()); // skip to end | |
127 } else { | |
128 reader->setOffset(skipTo); | |
129 } | |
130 } | |
131 | |
132 void SkPicturePlayback::draw(SkCanvas* canvas, SkDrawPictureCallback* callback)
{ | 68 void SkPicturePlayback::draw(SkCanvas* canvas, SkDrawPictureCallback* callback)
{ |
133 AutoResetOpID aroi(this); | 69 AutoResetOpID aroi(this); |
134 SkASSERT(0 == fCurOffset); | 70 SkASSERT(0 == fCurOffset); |
135 | 71 |
136 SkAutoTDelete<const SkPicture::OperationList> activeOpsList(this->getActiveO
ps(canvas)); | |
137 SkPictureStateTree::Iterator it; | |
138 | |
139 if (!this->initIterator(&it, canvas, activeOpsList.get())) { | |
140 return; // nothing to draw | |
141 } | |
142 | |
143 SkReader32 reader(fPictureData->opData()->bytes(), fPictureData->opData()->s
ize()); | 72 SkReader32 reader(fPictureData->opData()->bytes(), fPictureData->opData()->s
ize()); |
144 | 73 |
145 StepIterator(&it, &reader); | |
146 | |
147 // Record this, so we can concat w/ it if we encounter a setMatrix() | 74 // Record this, so we can concat w/ it if we encounter a setMatrix() |
148 SkMatrix initialMatrix = canvas->getTotalMatrix(); | 75 SkMatrix initialMatrix = canvas->getTotalMatrix(); |
149 | 76 |
150 SkAutoCanvasRestore acr(canvas, false); | 77 SkAutoCanvasRestore acr(canvas, false); |
151 | 78 |
152 while (!reader.eof()) { | 79 while (!reader.eof()) { |
153 if (callback && callback->abortDrawing()) { | 80 if (callback && callback->abortDrawing()) { |
154 return; | 81 return; |
155 } | 82 } |
156 | 83 |
157 fCurOffset = reader.offset(); | 84 fCurOffset = reader.offset(); |
158 uint32_t size; | 85 uint32_t size; |
159 DrawType op = ReadOpAndSize(&reader, &size); | 86 DrawType op = ReadOpAndSize(&reader, &size); |
160 if (NOOP == op) { | |
161 // NOOPs are to be ignored - do not propagate them any further | |
162 SkipIterTo(&it, &reader, fCurOffset + size); | |
163 continue; | |
164 } | |
165 | 87 |
166 this->handleOp(&reader, op, size, canvas, initialMatrix); | 88 this->handleOp(&reader, op, size, canvas, initialMatrix); |
167 | |
168 StepIterator(&it, &reader); | |
169 } | 89 } |
170 } | 90 } |
171 | 91 |
172 void SkPicturePlayback::handleOp(SkReader32* reader, | 92 void SkPicturePlayback::handleOp(SkReader32* reader, |
173 DrawType op, | 93 DrawType op, |
174 uint32_t size, | 94 uint32_t size, |
175 SkCanvas* canvas, | 95 SkCanvas* canvas, |
176 const SkMatrix& initialMatrix) { | 96 const SkMatrix& initialMatrix) { |
177 switch (op) { | 97 switch (op) { |
| 98 case NOOP: { |
| 99 SkASSERT(size >= 4); |
| 100 reader->skip(size - 4); |
| 101 } break; |
178 case CLIP_PATH: { | 102 case CLIP_PATH: { |
179 const SkPath& path = fPictureData->getPath(reader); | 103 const SkPath& path = fPictureData->getPath(reader); |
180 uint32_t packed = reader->readInt(); | 104 uint32_t packed = reader->readInt(); |
181 SkRegion::Op regionOp = ClipParams_unpackRegionOp(packed); | 105 SkRegion::Op regionOp = ClipParams_unpackRegionOp(packed); |
182 bool doAA = ClipParams_unpackDoAA(packed); | 106 bool doAA = ClipParams_unpackDoAA(packed); |
183 size_t offsetToRestore = reader->readInt(); | 107 size_t offsetToRestore = reader->readInt(); |
184 SkASSERT(!offsetToRestore || offsetToRestore >= reader->offset()); | 108 SkASSERT(!offsetToRestore || offsetToRestore >= reader->offset()); |
185 canvas->clipPath(path, regionOp, doAA); | 109 canvas->clipPath(path, regionOp, doAA); |
186 if (canvas->isClipEmpty() && offsetToRestore) { | 110 if (canvas->isClipEmpty() && offsetToRestore) { |
187 reader->setOffset(offsetToRestore); | 111 reader->setOffset(offsetToRestore); |
(...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
511 SkScalar sx = reader->readScalar(); | 435 SkScalar sx = reader->readScalar(); |
512 SkScalar sy = reader->readScalar(); | 436 SkScalar sy = reader->readScalar(); |
513 canvas->skew(sx, sy); | 437 canvas->skew(sx, sy); |
514 } break; | 438 } break; |
515 case TRANSLATE: { | 439 case TRANSLATE: { |
516 SkScalar dx = reader->readScalar(); | 440 SkScalar dx = reader->readScalar(); |
517 SkScalar dy = reader->readScalar(); | 441 SkScalar dy = reader->readScalar(); |
518 canvas->translate(dx, dy); | 442 canvas->translate(dx, dy); |
519 } break; | 443 } break; |
520 default: | 444 default: |
521 SkASSERT(0); | 445 SkASSERTF(false, "Unknown draw type: %d", op); |
522 } | 446 } |
523 } | 447 } |
524 | 448 |
OLD | NEW |