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 "SkRecordDraw.h" | 8 #include "SkRecordDraw.h" |
9 #include "SkTSort.h" | 9 #include "SkTSort.h" |
10 | 10 |
11 void SkRecordDraw(const SkRecord& record, | 11 void SkRecordDraw(const SkRecord& record, |
12 SkCanvas* canvas, | 12 SkCanvas* canvas, |
13 const SkBBoxHierarchy* bbh, | 13 const SkBBoxHierarchy* bbh, |
14 SkDrawPictureCallback* callback) { | 14 SkDrawPictureCallback* callback) { |
15 SkAutoCanvasRestore saveRestore(canvas, true /*save now, restore at exit*/); | 15 SkAutoCanvasRestore saveRestore(canvas, true /*save now, restore at exit*/); |
16 | 16 |
17 if (NULL != bbh) { | 17 if (NULL != bbh) { |
18 // Draw only ops that affect pixels in the canvas's current clip. | 18 // Draw only ops that affect pixels in the canvas's current clip. |
19 SkIRect devBounds; | 19 SkIRect query; |
20 canvas->getClipDeviceBounds(&devBounds); | 20 #if 1 // TODO: Why is this the right way to make the query? I'd think it'd be
the else branch. |
| 21 SkRect clipBounds; |
| 22 canvas->getClipBounds(&clipBounds); |
| 23 clipBounds.roundOut(&query); |
| 24 #else |
| 25 canvas->getClipDeviceBounds(&query); |
| 26 #endif |
21 SkTDArray<void*> ops; | 27 SkTDArray<void*> ops; |
22 bbh->search(devBounds, &ops); | 28 bbh->search(query, &ops); |
23 | 29 |
24 // FIXME: QuadTree doesn't send these back in the order we inserted them
. :( | 30 // FIXME: QuadTree doesn't send these back in the order we inserted them
. :( |
25 // Also remove the sort in SkPictureData::getActiveOps()? | 31 // Also remove the sort in SkPictureData::getActiveOps()? |
26 if (ops.count() > 0) { | 32 if (ops.count() > 0) { |
27 SkTQSort(ops.begin(), ops.end() - 1, SkTCompareLT<void*>()); | 33 SkTQSort(ops.begin(), ops.end() - 1, SkTCompareLT<void*>()); |
28 } | 34 } |
29 | 35 |
30 SkRecords::Draw draw(canvas); | 36 SkRecords::Draw draw(canvas); |
31 for (int i = 0; i < ops.count(); i++) { | 37 for (int i = 0; i < ops.count(); i++) { |
32 if (NULL != callback && callback->abortDrawing()) { | 38 if (NULL != callback && callback->abortDrawing()) { |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 // To implement this, we keep a stack of active Save blocks. As we consume ops | 116 // To implement this, we keep a stack of active Save blocks. As we consume ops |
111 // inside the Save/Restore block, drawing ops are unioned with the bounds of | 117 // inside the Save/Restore block, drawing ops are unioned with the bounds of |
112 // the block, and control ops are stashed away for later. When we finish the | 118 // the block, and control ops are stashed away for later. When we finish the |
113 // block with a Restore, our bounds are complete, and we go back and fill them | 119 // block with a Restore, our bounds are complete, and we go back and fill them |
114 // in for all the control ops we stashed away. | 120 // in for all the control ops we stashed away. |
115 class FillBounds : SkNoncopyable { | 121 class FillBounds : SkNoncopyable { |
116 public: | 122 public: |
117 FillBounds(const SkRecord& record, SkBBoxHierarchy* bbh) : fBounds(record.co
unt()) { | 123 FillBounds(const SkRecord& record, SkBBoxHierarchy* bbh) : fBounds(record.co
unt()) { |
118 // Calculate bounds for all ops. This won't go quite in order, so we'll
need | 124 // Calculate bounds for all ops. This won't go quite in order, so we'll
need |
119 // to store the bounds separately then feed them in to the BBH later in
order. | 125 // to store the bounds separately then feed them in to the BBH later in
order. |
| 126 const SkIRect largest = SkIRect::MakeLargest(); |
120 fCTM.setIdentity(); | 127 fCTM.setIdentity(); |
| 128 fCurrentClipBounds = largest; |
121 for (fCurrentOp = 0; fCurrentOp < record.count(); fCurrentOp++) { | 129 for (fCurrentOp = 0; fCurrentOp < record.count(); fCurrentOp++) { |
122 record.visit<void>(fCurrentOp, *this); | 130 record.visit<void>(fCurrentOp, *this); |
123 } | 131 } |
124 | 132 |
125 // If we have any lingering unpaired Saves, simulate restores to make | 133 // If we have any lingering unpaired Saves, simulate restores to make |
126 // sure all ops in those Save blocks have their bounds calculated. | 134 // sure all ops in those Save blocks have their bounds calculated. |
127 while (!fSaveStack.isEmpty()) { | 135 while (!fSaveStack.isEmpty()) { |
128 this->popSaveBlock(); | 136 this->popSaveBlock(); |
129 } | 137 } |
130 | 138 |
131 // Any control ops not part of any Save/Restore block draw everywhere. | 139 // Any control ops not part of any Save/Restore block draw everywhere. |
132 while (!fControlIndices.isEmpty()) { | 140 while (!fControlIndices.isEmpty()) { |
133 this->popControl(SkIRect::MakeLargest()); | 141 this->popControl(largest); |
134 } | 142 } |
135 | 143 |
136 // Finally feed all stored bounds into the BBH. They'll be returned in
this order. | 144 // Finally feed all stored bounds into the BBH. They'll be returned in
this order. |
137 SkASSERT(NULL != bbh); | 145 SkASSERT(NULL != bbh); |
138 for (uintptr_t i = 0; i < record.count(); i++) { | 146 for (uintptr_t i = 0; i < record.count(); i++) { |
139 if (!fBounds[i].isEmpty()) { | 147 if (!fBounds[i].isEmpty()) { |
140 bbh->insert((void*)i, fBounds[i], true/*ok to defer*/); | 148 bbh->insert((void*)i, fBounds[i], true/*ok to defer*/); |
141 } | 149 } |
142 } | 150 } |
143 bbh->flushDeferredInserts(); | 151 bbh->flushDeferredInserts(); |
144 } | 152 } |
145 | 153 |
146 template <typename T> void operator()(const T& r) { | 154 template <typename T> void operator()(const T& op) { |
147 this->updateCTM(r); | 155 this->updateCTM(op); |
148 this->trackBounds(r); | 156 this->updateClipBounds(op); |
| 157 this->trackBounds(op); |
149 } | 158 } |
150 | 159 |
151 private: | 160 private: |
152 struct SaveBounds { | 161 struct SaveBounds { |
153 int controlOps; // Number of control ops in this Save block, including
the Save. | 162 int controlOps; // Number of control ops in this Save block, incl
uding the Save. |
154 SkIRect bounds; // Bounds of everything in the block. | 163 SkIRect bounds; // Bounds of everything in the block. |
| 164 const SkPaint* paint; // Unowned. If set, adjusts the bounds of all op
s in this block. |
155 }; | 165 }; |
156 | 166 |
157 template <typename T> void updateCTM(const T&) { /* most ops don't change th
e CTM */ } | 167 template <typename T> void updateCTM(const T&) { /* most ops don't change th
e CTM */ } |
158 void updateCTM(const Restore& r) { fCTM = r.matrix; } | 168 void updateCTM(const Restore& op) { fCTM = op.matrix; } |
159 void updateCTM(const SetMatrix& r) { fCTM = r.matrix; } | 169 void updateCTM(const SetMatrix& op) { fCTM = op.matrix; } |
160 void updateCTM(const Concat& r) { fCTM.preConcat(r.matrix); } | 170 void updateCTM(const Concat& op) { fCTM.preConcat(op.matrix); } |
| 171 |
| 172 template <typename T> void updateClipBounds(const T&) { /* most ops don't ch
ange the clip */ } |
| 173 // Each of these devBounds fields is the state of the device bounds after th
e op. |
| 174 // So Restore's devBounds are those bounds saved by its paired Save or SaveL
ayer. |
| 175 void updateClipBounds(const Restore& op) { fCurrentClipBounds = op.devBou
nds; } |
| 176 void updateClipBounds(const ClipPath& op) { fCurrentClipBounds = op.devBou
nds; } |
| 177 void updateClipBounds(const ClipRRect& op) { fCurrentClipBounds = op.devBou
nds; } |
| 178 void updateClipBounds(const ClipRect& op) { fCurrentClipBounds = op.devBou
nds; } |
| 179 void updateClipBounds(const ClipRegion& op) { fCurrentClipBounds = op.devBou
nds; } |
| 180 void updateClipBounds(const SaveLayer& op) { |
| 181 if (op.bounds) { |
| 182 fCurrentClipBounds.intersect(this->adjustAndMap(*op.bounds, op.paint
)); |
| 183 } |
| 184 } |
161 | 185 |
162 // The bounds of these ops must be calculated when we hit the Restore | 186 // The bounds of these ops must be calculated when we hit the Restore |
163 // from the bounds of the ops in the same Save block. | 187 // from the bounds of the ops in the same Save block. |
164 void trackBounds(const Save&) { this->pushSaveBlock(); } | 188 void trackBounds(const Save&) { this->pushSaveBlock(NULL); } |
165 // TODO: bounds of SaveLayer may be more complicated? | 189 // TODO: bounds of SaveLayer may be more complicated? |
166 void trackBounds(const SaveLayer&) { this->pushSaveBlock(); } | 190 void trackBounds(const SaveLayer& op) { this->pushSaveBlock(op.paint); } |
167 void trackBounds(const Restore&) { fBounds[fCurrentOp] = this->popSaveBlo
ck(); } | 191 void trackBounds(const Restore&) { fBounds[fCurrentOp] = this->popSaveBlock(
); } |
168 | 192 |
169 void trackBounds(const Concat&) { this->pushControl(); } | 193 void trackBounds(const Concat&) { this->pushControl(); } |
170 void trackBounds(const SetMatrix&) { this->pushControl(); } | 194 void trackBounds(const SetMatrix&) { this->pushControl(); } |
171 void trackBounds(const ClipRect&) { this->pushControl(); } | 195 void trackBounds(const ClipRect&) { this->pushControl(); } |
172 void trackBounds(const ClipRRect&) { this->pushControl(); } | 196 void trackBounds(const ClipRRect&) { this->pushControl(); } |
173 void trackBounds(const ClipPath&) { this->pushControl(); } | 197 void trackBounds(const ClipPath&) { this->pushControl(); } |
174 void trackBounds(const ClipRegion&) { this->pushControl(); } | 198 void trackBounds(const ClipRegion&) { this->pushControl(); } |
175 | 199 |
176 // For all other ops, we can calculate and store the bounds directly now. | 200 // For all other ops, we can calculate and store the bounds directly now. |
177 template <typename T> void trackBounds(const T& op) { | 201 template <typename T> void trackBounds(const T& op) { |
178 fBounds[fCurrentOp] = this->bounds(op); | 202 fBounds[fCurrentOp] = this->bounds(op); |
179 this->updateSaveBounds(fBounds[fCurrentOp]); | 203 this->updateSaveBounds(fBounds[fCurrentOp]); |
180 } | 204 } |
181 | 205 |
182 // TODO: remove this trivially-safe default when done bounding all ops | 206 void pushSaveBlock(const SkPaint* paint) { |
183 template <typename T> SkIRect bounds(const T&) { return SkIRect::MakeLargest
(); } | |
184 | |
185 void pushSaveBlock() { | |
186 // Starting a new Save block. Push a new entry to represent that. | 207 // Starting a new Save block. Push a new entry to represent that. |
187 SaveBounds sb = { 0, SkIRect::MakeEmpty() }; | 208 SaveBounds sb = { 0, SkIRect::MakeEmpty(), paint }; |
188 fSaveStack.push(sb); | 209 fSaveStack.push(sb); |
189 this->pushControl(); | 210 this->pushControl(); |
190 } | 211 } |
191 | 212 |
192 SkIRect popSaveBlock() { | 213 SkIRect popSaveBlock() { |
193 // We're done the Save block. Apply the block's bounds to all control o
ps inside it. | 214 // We're done the Save block. Apply the block's bounds to all control o
ps inside it. |
194 SaveBounds sb; | 215 SaveBounds sb; |
195 fSaveStack.pop(&sb); | 216 fSaveStack.pop(&sb); |
196 while (sb.controlOps --> 0) { | 217 while (sb.controlOps --> 0) { |
197 this->popControl(sb.bounds); | 218 this->popControl(sb.bounds); |
(...skipping 18 matching lines...) Expand all Loading... |
216 fControlIndices.pop(); | 237 fControlIndices.pop(); |
217 } | 238 } |
218 | 239 |
219 void updateSaveBounds(const SkIRect& bounds) { | 240 void updateSaveBounds(const SkIRect& bounds) { |
220 // If we're in a Save block, expand its bounds to cover these bounds too
. | 241 // If we're in a Save block, expand its bounds to cover these bounds too
. |
221 if (!fSaveStack.isEmpty()) { | 242 if (!fSaveStack.isEmpty()) { |
222 fSaveStack.top().bounds.join(bounds); | 243 fSaveStack.top().bounds.join(bounds); |
223 } | 244 } |
224 } | 245 } |
225 | 246 |
226 SkIRect bounds(const NoOp&) { return SkIRect::MakeEmpty(); } // NoOps don't
draw anywhere. | 247 // TODO: Remove this default when done bounding all ops. |
| 248 template <typename T> SkIRect bounds(const T&) { return fCurrentClipBounds;
} |
| 249 SkIRect bounds(const Clear&) { return SkIRect::MakeLargest(); } // Ignores
the clip |
| 250 SkIRect bounds(const NoOp&) { return SkIRect::MakeEmpty(); } // NoOps do
n't draw anywhere. |
227 | 251 |
228 SkAutoTMalloc<SkIRect> fBounds; // One for each op in the record. | 252 // Adjust rect for all paints that may affect its geometry, then map it to d
evice space. |
| 253 SkIRect adjustAndMap(SkRect rect, const SkPaint* paint) { |
| 254 // Adjust rect for its own paint. |
| 255 if (paint) { |
| 256 if (paint->canComputeFastBounds()) { |
| 257 rect = paint->computeFastBounds(rect, &rect); |
| 258 } else { |
| 259 // The paint could do anything. The only safe answer is the cur
rent clip. |
| 260 return fCurrentClipBounds; |
| 261 } |
| 262 } |
| 263 |
| 264 // Adjust rect for all the paints from the SaveLayers we're inside. |
| 265 // For SaveLayers, only image filters will affect the bounds. |
| 266 for (int i = fSaveStack.count() - 1; i >= 0; i--) { |
| 267 if (fSaveStack[i].paint && fSaveStack[i].paint->getImageFilter()) { |
| 268 if (paint->canComputeFastBounds()) { |
| 269 rect = fSaveStack[i].paint->computeFastBounds(rect, &rect); |
| 270 } else { |
| 271 // Same deal as above. |
| 272 return fCurrentClipBounds; |
| 273 } |
| 274 } |
| 275 } |
| 276 |
| 277 // Map the rect back to device space. |
| 278 fCTM.mapRect(&rect); |
| 279 SkIRect devRect; |
| 280 rect.roundOut(&devRect); |
| 281 return devRect; |
| 282 } |
| 283 |
| 284 // Conservative device bounds for each op in the SkRecord. |
| 285 SkAutoTMalloc<SkIRect> fBounds; |
| 286 |
| 287 // We walk fCurrentOp through the SkRecord, as we go using updateCTM() |
| 288 // and updateClipBounds() to maintain the exact CTM (fCTM) and conservative |
| 289 // device bounds of the current clip (fCurrentClipBounds). |
| 290 unsigned fCurrentOp; |
229 SkMatrix fCTM; | 291 SkMatrix fCTM; |
230 unsigned fCurrentOp; | 292 SkIRect fCurrentClipBounds; |
| 293 |
| 294 // Used to track the bounds of Save/Restore blocks and the control ops insid
e them. |
231 SkTDArray<SaveBounds> fSaveStack; | 295 SkTDArray<SaveBounds> fSaveStack; |
232 SkTDArray<unsigned> fControlIndices; | 296 SkTDArray<unsigned> fControlIndices; |
233 }; | 297 }; |
234 | 298 |
235 } // namespace SkRecords | 299 } // namespace SkRecords |
236 | 300 |
237 void SkRecordFillBounds(const SkRecord& record, SkBBoxHierarchy* bbh) { | 301 void SkRecordFillBounds(const SkRecord& record, SkBBoxHierarchy* bbh) { |
238 SkRecords::FillBounds(record, bbh); | 302 SkRecords::FillBounds(record, bbh); |
239 } | 303 } |
OLD | NEW |