Chromium Code Reviews| 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. |
|
Stephen White
2014/08/14 17:40:04
I'm not super-familiar with the BBH code, but it l
mtklein
2014/08/14 17:54:03
Yeah, stole this from there.
As far as I can tell
| |
| 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 fClipBounds = 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 void updateClipBounds(const Restore& op) { fClipBounds = op.devBounds; } | |
| 174 void updateClipBounds(const ClipPath& op) { fClipBounds = op.devBounds; } | |
| 175 void updateClipBounds(const ClipRRect& op) { fClipBounds = op.devBounds; } | |
| 176 void updateClipBounds(const ClipRect& op) { fClipBounds = op.devBounds; } | |
| 177 void updateClipBounds(const ClipRegion& op) { fClipBounds = op.devBounds; } | |
| 178 void updateClipBounds(const SaveLayer& op) { | |
| 179 if (op.bounds) { | |
| 180 fClipBounds.intersect(this->adjustAndMap(*op.bounds, op.paint)); | |
| 181 } | |
| 182 } | |
| 161 | 183 |
| 162 // The bounds of these ops must be calculated when we hit the Restore | 184 // 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. | 185 // from the bounds of the ops in the same Save block. |
| 164 void trackBounds(const Save&) { this->pushSaveBlock(); } | 186 void trackBounds(const Save&) { this->pushSaveBlock(NULL); } |
| 165 // TODO: bounds of SaveLayer may be more complicated? | 187 // TODO: bounds of SaveLayer may be more complicated? |
| 166 void trackBounds(const SaveLayer&) { this->pushSaveBlock(); } | 188 void trackBounds(const SaveLayer& op) { this->pushSaveBlock(op.paint); } |
| 167 void trackBounds(const Restore&) { fBounds[fCurrentOp] = this->popSaveBlo ck(); } | 189 void trackBounds(const Restore&) { fBounds[fCurrentOp] = this->popSaveBlock( ); } |
| 168 | 190 |
| 169 void trackBounds(const Concat&) { this->pushControl(); } | 191 void trackBounds(const Concat&) { this->pushControl(); } |
| 170 void trackBounds(const SetMatrix&) { this->pushControl(); } | 192 void trackBounds(const SetMatrix&) { this->pushControl(); } |
| 171 void trackBounds(const ClipRect&) { this->pushControl(); } | 193 void trackBounds(const ClipRect&) { this->pushControl(); } |
| 172 void trackBounds(const ClipRRect&) { this->pushControl(); } | 194 void trackBounds(const ClipRRect&) { this->pushControl(); } |
| 173 void trackBounds(const ClipPath&) { this->pushControl(); } | 195 void trackBounds(const ClipPath&) { this->pushControl(); } |
| 174 void trackBounds(const ClipRegion&) { this->pushControl(); } | 196 void trackBounds(const ClipRegion&) { this->pushControl(); } |
| 175 | 197 |
| 176 // For all other ops, we can calculate and store the bounds directly now. | 198 // For all other ops, we can calculate and store the bounds directly now. |
| 177 template <typename T> void trackBounds(const T& op) { | 199 template <typename T> void trackBounds(const T& op) { |
| 178 fBounds[fCurrentOp] = this->bounds(op); | 200 fBounds[fCurrentOp] = this->bounds(op); |
| 179 this->updateSaveBounds(fBounds[fCurrentOp]); | 201 this->updateSaveBounds(fBounds[fCurrentOp]); |
| 180 } | 202 } |
| 181 | 203 |
| 182 // TODO: remove this trivially-safe default when done bounding all ops | 204 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. | 205 // Starting a new Save block. Push a new entry to represent that. |
| 187 SaveBounds sb = { 0, SkIRect::MakeEmpty() }; | 206 SaveBounds sb = { 0, SkIRect::MakeEmpty(), paint }; |
| 188 fSaveStack.push(sb); | 207 fSaveStack.push(sb); |
| 189 this->pushControl(); | 208 this->pushControl(); |
| 190 } | 209 } |
| 191 | 210 |
| 192 SkIRect popSaveBlock() { | 211 SkIRect popSaveBlock() { |
| 193 // We're done the Save block. Apply the block's bounds to all control o ps inside it. | 212 // We're done the Save block. Apply the block's bounds to all control o ps inside it. |
| 194 SaveBounds sb; | 213 SaveBounds sb; |
| 195 fSaveStack.pop(&sb); | 214 fSaveStack.pop(&sb); |
| 196 while (sb.controlOps --> 0) { | 215 while (sb.controlOps --> 0) { |
| 197 this->popControl(sb.bounds); | 216 this->popControl(sb.bounds); |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 216 fControlIndices.pop(); | 235 fControlIndices.pop(); |
| 217 } | 236 } |
| 218 | 237 |
| 219 void updateSaveBounds(const SkIRect& bounds) { | 238 void updateSaveBounds(const SkIRect& bounds) { |
| 220 // If we're in a Save block, expand its bounds to cover these bounds too . | 239 // If we're in a Save block, expand its bounds to cover these bounds too . |
| 221 if (!fSaveStack.isEmpty()) { | 240 if (!fSaveStack.isEmpty()) { |
| 222 fSaveStack.top().bounds.join(bounds); | 241 fSaveStack.top().bounds.join(bounds); |
| 223 } | 242 } |
| 224 } | 243 } |
| 225 | 244 |
| 226 SkIRect bounds(const NoOp&) { return SkIRect::MakeEmpty(); } // NoOps don't draw anywhere. | 245 // TODO: Remove this default when done bounding all ops. |
| 246 template <typename T> SkIRect bounds(const T&) { return fClipBounds; } | |
| 247 SkIRect bounds(const Clear&) { return SkIRect::MakeLargest(); } // Ignores the clip | |
| 248 SkIRect bounds(const NoOp&) { return SkIRect::MakeEmpty(); } // NoOps do n't draw anywhere. | |
| 249 | |
| 250 // Adjust rect for all paints that may affect its geometry, then map it to d evice space. | |
| 251 SkIRect adjustAndMap(SkRect rect, const SkPaint* paint) { | |
| 252 // Adjust rect for its own paint. | |
| 253 if (paint && paint->canComputeFastBounds()) { | |
| 254 rect = paint->computeFastBounds(rect, &rect); | |
|
Stephen White
2014/08/14 17:23:07
If you have a paint that can't computeFastBounds()
mtklein
2014/08/14 17:54:03
Thanks, good catch. All the bounds are the clip r
| |
| 255 } | |
| 256 // Adjust rect for all the paints from the SaveLayers we're inside. | |
| 257 // For SaveLayers, only image filters will affect the bounds. | |
| 258 for (int i = fSaveStack.count() - 1; i >= 0; i--) { | |
| 259 if (fSaveStack[i].paint && | |
| 260 fSaveStack[i].paint->getImageFilter() && | |
| 261 fSaveStack[i].paint->canComputeFastBounds()) { | |
| 262 rect = fSaveStack[i].paint->computeFastBounds(rect, &rect); | |
| 263 } | |
| 264 } | |
| 265 // Map the rect back to device space. | |
| 266 fCTM.mapRect(&rect); | |
| 267 SkIRect devRect; | |
| 268 rect.roundOut(&devRect); | |
| 269 return devRect; | |
| 270 } | |
| 227 | 271 |
| 228 SkAutoTMalloc<SkIRect> fBounds; // One for each op in the record. | 272 SkAutoTMalloc<SkIRect> fBounds; // One for each op in the record. |
| 229 SkMatrix fCTM; | 273 SkMatrix fCTM; |
| 274 SkIRect fClipBounds; | |
| 230 unsigned fCurrentOp; | 275 unsigned fCurrentOp; |
| 231 SkTDArray<SaveBounds> fSaveStack; | 276 SkTDArray<SaveBounds> fSaveStack; |
| 232 SkTDArray<unsigned> fControlIndices; | 277 SkTDArray<unsigned> fControlIndices; |
| 233 }; | 278 }; |
| 234 | 279 |
| 235 } // namespace SkRecords | 280 } // namespace SkRecords |
| 236 | 281 |
| 237 void SkRecordFillBounds(const SkRecord& record, SkBBoxHierarchy* bbh) { | 282 void SkRecordFillBounds(const SkRecord& record, SkBBoxHierarchy* bbh) { |
| 238 SkRecords::FillBounds(record, bbh); | 283 SkRecords::FillBounds(record, bbh); |
| 239 } | 284 } |
| OLD | NEW |