commit-bot@chromium.org | c4b21e6 | 2014-04-11 18:33:31 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 8 | #include "SkRecordDraw.h" |
mtklein | 131a22b | 2014-08-25 14:16:15 -0700 | [diff] [blame^] | 9 | #include "SkPatchUtils.h" |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 10 | |
mtklein | 5ad6ee1 | 2014-08-11 08:08:43 -0700 | [diff] [blame] | 11 | void SkRecordDraw(const SkRecord& record, |
| 12 | SkCanvas* canvas, |
| 13 | const SkBBoxHierarchy* bbh, |
| 14 | SkDrawPictureCallback* callback) { |
Mike Klein | c11530e | 2014-06-24 11:29:06 -0400 | [diff] [blame] | 15 | SkAutoCanvasRestore saveRestore(canvas, true /*save now, restore at exit*/); |
mtklein | 5ad6ee1 | 2014-08-11 08:08:43 -0700 | [diff] [blame] | 16 | |
| 17 | if (NULL != bbh) { |
mtklein | 5ad6ee1 | 2014-08-11 08:08:43 -0700 | [diff] [blame] | 18 | // Draw only ops that affect pixels in the canvas's current clip. |
mtklein | a723b57 | 2014-08-15 11:49:49 -0700 | [diff] [blame] | 19 | SkIRect query; |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 20 | |
| 21 | // The SkRecord and BBH were recorded in identity space. This canvas |
| 22 | // is not necessarily in that same space. getClipBounds() returns us |
| 23 | // this canvas' clip bounds transformed back into identity space, which |
| 24 | // lets us query the BBH. |
| 25 | SkRect clipBounds = { 0, 0, 0, 0 }; |
| 26 | (void)canvas->getClipBounds(&clipBounds); |
mtklein | a723b57 | 2014-08-15 11:49:49 -0700 | [diff] [blame] | 27 | clipBounds.roundOut(&query); |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 28 | |
mtklein | 5ad6ee1 | 2014-08-11 08:08:43 -0700 | [diff] [blame] | 29 | SkTDArray<void*> ops; |
mtklein | a723b57 | 2014-08-15 11:49:49 -0700 | [diff] [blame] | 30 | bbh->search(query, &ops); |
mtklein | 5ad6ee1 | 2014-08-11 08:08:43 -0700 | [diff] [blame] | 31 | |
mtklein | 5ad6ee1 | 2014-08-11 08:08:43 -0700 | [diff] [blame] | 32 | SkRecords::Draw draw(canvas); |
| 33 | for (int i = 0; i < ops.count(); i++) { |
| 34 | if (NULL != callback && callback->abortDrawing()) { |
| 35 | return; |
| 36 | } |
| 37 | record.visit<void>((uintptr_t)ops[i], draw); // See FillBounds below. |
| 38 | } |
| 39 | } else { |
| 40 | // Draw all ops. |
| 41 | for (SkRecords::Draw draw(canvas); draw.index() < record.count(); draw.next()) { |
| 42 | if (NULL != callback && callback->abortDrawing()) { |
| 43 | return; |
| 44 | } |
| 45 | record.visit<void>(draw.index(), draw); |
| 46 | } |
commit-bot@chromium.org | 73b55eb | 2014-04-14 20:35:12 +0000 | [diff] [blame] | 47 | } |
commit-bot@chromium.org | 27f6b0d | 2014-05-09 14:59:29 +0000 | [diff] [blame] | 48 | } |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 49 | |
commit-bot@chromium.org | 27f6b0d | 2014-05-09 14:59:29 +0000 | [diff] [blame] | 50 | namespace SkRecords { |
commit-bot@chromium.org | 73b55eb | 2014-04-14 20:35:12 +0000 | [diff] [blame] | 51 | |
mtklein | 7cdc1ee | 2014-07-07 10:41:04 -0700 | [diff] [blame] | 52 | // FIXME: SkBitmaps are stateful, so we need to copy them to play back in multiple threads. |
| 53 | static SkBitmap shallow_copy(const SkBitmap& bitmap) { |
| 54 | return bitmap; |
| 55 | } |
| 56 | |
commit-bot@chromium.org | 2e0c32a | 2014-04-28 16:19:45 +0000 | [diff] [blame] | 57 | // NoOps draw nothing. |
commit-bot@chromium.org | 27f6b0d | 2014-05-09 14:59:29 +0000 | [diff] [blame] | 58 | template <> void Draw::draw(const NoOp&) {} |
commit-bot@chromium.org | 73b55eb | 2014-04-14 20:35:12 +0000 | [diff] [blame] | 59 | |
commit-bot@chromium.org | 27f6b0d | 2014-05-09 14:59:29 +0000 | [diff] [blame] | 60 | #define DRAW(T, call) template <> void Draw::draw(const T& r) { fCanvas->call; } |
commit-bot@chromium.org | 73b55eb | 2014-04-14 20:35:12 +0000 | [diff] [blame] | 61 | DRAW(Restore, restore()); |
Florin Malita | 5f6102d | 2014-06-30 10:13:28 -0400 | [diff] [blame] | 62 | DRAW(Save, save()); |
commit-bot@chromium.org | 73b55eb | 2014-04-14 20:35:12 +0000 | [diff] [blame] | 63 | DRAW(SaveLayer, saveLayer(r.bounds, r.paint, r.flags)); |
| 64 | DRAW(PopCull, popCull()); |
commit-bot@chromium.org | 88c3e27 | 2014-04-22 16:57:20 +0000 | [diff] [blame] | 65 | DRAW(PushCull, pushCull(r.rect)); |
commit-bot@chromium.org | 73b55eb | 2014-04-14 20:35:12 +0000 | [diff] [blame] | 66 | DRAW(Clear, clear(r.color)); |
commit-bot@chromium.org | 99bd7d8 | 2014-05-19 15:51:12 +0000 | [diff] [blame] | 67 | DRAW(SetMatrix, setMatrix(SkMatrix::Concat(fInitialCTM, r.matrix))); |
commit-bot@chromium.org | 73b55eb | 2014-04-14 20:35:12 +0000 | [diff] [blame] | 68 | |
| 69 | DRAW(ClipPath, clipPath(r.path, r.op, r.doAA)); |
| 70 | DRAW(ClipRRect, clipRRect(r.rrect, r.op, r.doAA)); |
| 71 | DRAW(ClipRect, clipRect(r.rect, r.op, r.doAA)); |
| 72 | DRAW(ClipRegion, clipRegion(r.region, r.op)); |
| 73 | |
mtklein | 5f0e822 | 2014-08-22 11:44:26 -0700 | [diff] [blame] | 74 | DRAW(BeginCommentGroup, beginCommentGroup(r.description)); |
| 75 | DRAW(AddComment, addComment(r.key, r.value)); |
| 76 | DRAW(EndCommentGroup, endCommentGroup()); |
| 77 | |
mtklein | 7cdc1ee | 2014-07-07 10:41:04 -0700 | [diff] [blame] | 78 | DRAW(DrawBitmap, drawBitmap(shallow_copy(r.bitmap), r.left, r.top, r.paint)); |
| 79 | DRAW(DrawBitmapMatrix, drawBitmapMatrix(shallow_copy(r.bitmap), r.matrix, r.paint)); |
| 80 | DRAW(DrawBitmapNine, drawBitmapNine(shallow_copy(r.bitmap), r.center, r.dst, r.paint)); |
| 81 | DRAW(DrawBitmapRectToRect, |
| 82 | drawBitmapRectToRect(shallow_copy(r.bitmap), r.src, r.dst, r.paint, r.flags)); |
commit-bot@chromium.org | 73b55eb | 2014-04-14 20:35:12 +0000 | [diff] [blame] | 83 | DRAW(DrawDRRect, drawDRRect(r.outer, r.inner, r.paint)); |
| 84 | DRAW(DrawOval, drawOval(r.oval, r.paint)); |
| 85 | DRAW(DrawPaint, drawPaint(r.paint)); |
| 86 | DRAW(DrawPath, drawPath(r.path, r.paint)); |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 87 | DRAW(DrawPatch, drawPatch(r.cubics, r.colors, r.texCoords, r.xmode.get(), r.paint)); |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 88 | DRAW(DrawPicture, drawPicture(r.picture, r.matrix, r.paint)); |
commit-bot@chromium.org | 73b55eb | 2014-04-14 20:35:12 +0000 | [diff] [blame] | 89 | DRAW(DrawPoints, drawPoints(r.mode, r.count, r.pts, r.paint)); |
| 90 | DRAW(DrawPosText, drawPosText(r.text, r.byteLength, r.pos, r.paint)); |
| 91 | DRAW(DrawPosTextH, drawPosTextH(r.text, r.byteLength, r.xpos, r.y, r.paint)); |
| 92 | DRAW(DrawRRect, drawRRect(r.rrect, r.paint)); |
| 93 | DRAW(DrawRect, drawRect(r.rect, r.paint)); |
mtklein | 7cdc1ee | 2014-07-07 10:41:04 -0700 | [diff] [blame] | 94 | DRAW(DrawSprite, drawSprite(shallow_copy(r.bitmap), r.left, r.top, r.paint)); |
commit-bot@chromium.org | 73b55eb | 2014-04-14 20:35:12 +0000 | [diff] [blame] | 95 | DRAW(DrawText, drawText(r.text, r.byteLength, r.x, r.y, r.paint)); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 96 | DRAW(DrawTextBlob, drawTextBlob(r.blob, r.x, r.y, r.paint)); |
commit-bot@chromium.org | 73b55eb | 2014-04-14 20:35:12 +0000 | [diff] [blame] | 97 | DRAW(DrawTextOnPath, drawTextOnPath(r.text, r.byteLength, r.path, r.matrix, r.paint)); |
| 98 | DRAW(DrawVertices, drawVertices(r.vmode, r.vertexCount, r.vertices, r.texs, r.colors, |
| 99 | r.xmode.get(), r.indices, r.indexCount, r.paint)); |
| 100 | #undef DRAW |
| 101 | |
mtklein | 5ad6ee1 | 2014-08-11 08:08:43 -0700 | [diff] [blame] | 102 | |
| 103 | // This is an SkRecord visitor that fills an SkBBoxHierarchy. |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 104 | // |
| 105 | // The interesting part here is how to calculate bounds for ops which don't |
| 106 | // have intrinsic bounds. What is the bounds of a Save or a Translate? |
| 107 | // |
| 108 | // We answer this by thinking about a particular definition of bounds: if I |
| 109 | // don't execute this op, pixels in this rectangle might draw incorrectly. So |
| 110 | // the bounds of a Save, a Translate, a Restore, etc. are the union of the |
| 111 | // bounds of Draw* ops that they might have an effect on. For any given |
| 112 | // Save/Restore block, the bounds of the Save, the Restore, and any other |
| 113 | // non-drawing ("control") ops inside are exactly the union of the bounds of |
| 114 | // the drawing ops inside that block. |
| 115 | // |
| 116 | // To implement this, we keep a stack of active Save blocks. As we consume ops |
| 117 | // inside the Save/Restore block, drawing ops are unioned with the bounds of |
| 118 | // the block, and control ops are stashed away for later. When we finish the |
| 119 | // block with a Restore, our bounds are complete, and we go back and fill them |
| 120 | // in for all the control ops we stashed away. |
mtklein | 5ad6ee1 | 2014-08-11 08:08:43 -0700 | [diff] [blame] | 121 | class FillBounds : SkNoncopyable { |
| 122 | public: |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 123 | FillBounds(const SkRecord& record, SkBBoxHierarchy* bbh) : fBounds(record.count()) { |
| 124 | // Calculate bounds for all ops. This won't go quite in order, so we'll need |
| 125 | // to store the bounds separately then feed them in to the BBH later in order. |
mtklein | a723b57 | 2014-08-15 11:49:49 -0700 | [diff] [blame] | 126 | const SkIRect largest = SkIRect::MakeLargest(); |
mtklein | 6332f1d | 2014-08-19 07:09:40 -0700 | [diff] [blame] | 127 | fCTM = &SkMatrix::I(); |
mtklein | a723b57 | 2014-08-15 11:49:49 -0700 | [diff] [blame] | 128 | fCurrentClipBounds = largest; |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 129 | for (fCurrentOp = 0; fCurrentOp < record.count(); fCurrentOp++) { |
| 130 | record.visit<void>(fCurrentOp, *this); |
| 131 | } |
mtklein | 5ad6ee1 | 2014-08-11 08:08:43 -0700 | [diff] [blame] | 132 | |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 133 | // If we have any lingering unpaired Saves, simulate restores to make |
| 134 | // sure all ops in those Save blocks have their bounds calculated. |
| 135 | while (!fSaveStack.isEmpty()) { |
| 136 | this->popSaveBlock(); |
| 137 | } |
| 138 | |
| 139 | // Any control ops not part of any Save/Restore block draw everywhere. |
| 140 | while (!fControlIndices.isEmpty()) { |
mtklein | a723b57 | 2014-08-15 11:49:49 -0700 | [diff] [blame] | 141 | this->popControl(largest); |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | // Finally feed all stored bounds into the BBH. They'll be returned in this order. |
| 145 | SkASSERT(NULL != bbh); |
| 146 | for (uintptr_t i = 0; i < record.count(); i++) { |
| 147 | if (!fBounds[i].isEmpty()) { |
| 148 | bbh->insert((void*)i, fBounds[i], true/*ok to defer*/); |
| 149 | } |
| 150 | } |
| 151 | bbh->flushDeferredInserts(); |
| 152 | } |
mtklein | 5ad6ee1 | 2014-08-11 08:08:43 -0700 | [diff] [blame] | 153 | |
mtklein | a723b57 | 2014-08-15 11:49:49 -0700 | [diff] [blame] | 154 | template <typename T> void operator()(const T& op) { |
| 155 | this->updateCTM(op); |
| 156 | this->updateClipBounds(op); |
| 157 | this->trackBounds(op); |
mtklein | 5ad6ee1 | 2014-08-11 08:08:43 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | private: |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 161 | struct SaveBounds { |
mtklein | a723b57 | 2014-08-15 11:49:49 -0700 | [diff] [blame] | 162 | int controlOps; // Number of control ops in this Save block, including the Save. |
| 163 | SkIRect bounds; // Bounds of everything in the block. |
| 164 | const SkPaint* paint; // Unowned. If set, adjusts the bounds of all ops in this block. |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 165 | }; |
| 166 | |
mtklein | 6cfa73a | 2014-08-13 13:33:49 -0700 | [diff] [blame] | 167 | template <typename T> void updateCTM(const T&) { /* most ops don't change the CTM */ } |
mtklein | 6332f1d | 2014-08-19 07:09:40 -0700 | [diff] [blame] | 168 | void updateCTM(const Restore& op) { fCTM = &op.matrix; } |
| 169 | void updateCTM(const SetMatrix& op) { fCTM = &op.matrix; } |
mtklein | a723b57 | 2014-08-15 11:49:49 -0700 | [diff] [blame] | 170 | |
| 171 | template <typename T> void updateClipBounds(const T&) { /* most ops don't change the clip */ } |
| 172 | // Each of these devBounds fields is the state of the device bounds after the op. |
| 173 | // So Restore's devBounds are those bounds saved by its paired Save or SaveLayer. |
| 174 | void updateClipBounds(const Restore& op) { fCurrentClipBounds = op.devBounds; } |
| 175 | void updateClipBounds(const ClipPath& op) { fCurrentClipBounds = op.devBounds; } |
| 176 | void updateClipBounds(const ClipRRect& op) { fCurrentClipBounds = op.devBounds; } |
| 177 | void updateClipBounds(const ClipRect& op) { fCurrentClipBounds = op.devBounds; } |
| 178 | void updateClipBounds(const ClipRegion& op) { fCurrentClipBounds = op.devBounds; } |
| 179 | void updateClipBounds(const SaveLayer& op) { |
| 180 | if (op.bounds) { |
| 181 | fCurrentClipBounds.intersect(this->adjustAndMap(*op.bounds, op.paint)); |
| 182 | } |
| 183 | } |
mtklein | 6cfa73a | 2014-08-13 13:33:49 -0700 | [diff] [blame] | 184 | |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 185 | // The bounds of these ops must be calculated when we hit the Restore |
| 186 | // from the bounds of the ops in the same Save block. |
mtklein | a723b57 | 2014-08-15 11:49:49 -0700 | [diff] [blame] | 187 | void trackBounds(const Save&) { this->pushSaveBlock(NULL); } |
mtklein | a723b57 | 2014-08-15 11:49:49 -0700 | [diff] [blame] | 188 | void trackBounds(const SaveLayer& op) { this->pushSaveBlock(op.paint); } |
| 189 | void trackBounds(const Restore&) { fBounds[fCurrentOp] = this->popSaveBlock(); } |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 190 | |
mtklein | 68199a2 | 2014-08-25 13:49:29 -0700 | [diff] [blame] | 191 | void trackBounds(const SetMatrix&) { this->pushControl(); } |
| 192 | void trackBounds(const ClipRect&) { this->pushControl(); } |
| 193 | void trackBounds(const ClipRRect&) { this->pushControl(); } |
| 194 | void trackBounds(const ClipPath&) { this->pushControl(); } |
| 195 | void trackBounds(const ClipRegion&) { this->pushControl(); } |
| 196 | void trackBounds(const PushCull&) { this->pushControl(); } |
| 197 | void trackBounds(const PopCull&) { this->pushControl(); } |
| 198 | void trackBounds(const BeginCommentGroup&) { this->pushControl(); } |
| 199 | void trackBounds(const AddComment&) { this->pushControl(); } |
| 200 | void trackBounds(const EndCommentGroup&) { this->pushControl(); } |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 201 | |
| 202 | // For all other ops, we can calculate and store the bounds directly now. |
| 203 | template <typename T> void trackBounds(const T& op) { |
| 204 | fBounds[fCurrentOp] = this->bounds(op); |
| 205 | this->updateSaveBounds(fBounds[fCurrentOp]); |
mtklein | 5ad6ee1 | 2014-08-11 08:08:43 -0700 | [diff] [blame] | 206 | } |
| 207 | |
mtklein | a723b57 | 2014-08-15 11:49:49 -0700 | [diff] [blame] | 208 | void pushSaveBlock(const SkPaint* paint) { |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 209 | // Starting a new Save block. Push a new entry to represent that. |
mtklein | a723b57 | 2014-08-15 11:49:49 -0700 | [diff] [blame] | 210 | SaveBounds sb = { 0, SkIRect::MakeEmpty(), paint }; |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 211 | fSaveStack.push(sb); |
| 212 | this->pushControl(); |
| 213 | } |
| 214 | |
mtklein | d910f54 | 2014-08-22 09:06:34 -0700 | [diff] [blame] | 215 | static bool PaintMayAffectTransparentBlack(const SkPaint* paint) { |
| 216 | // FIXME: this is very conservative |
| 217 | return paint && (paint->getImageFilter() || paint->getColorFilter()); |
| 218 | } |
| 219 | |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 220 | SkIRect popSaveBlock() { |
| 221 | // We're done the Save block. Apply the block's bounds to all control ops inside it. |
| 222 | SaveBounds sb; |
| 223 | fSaveStack.pop(&sb); |
mtklein | d910f54 | 2014-08-22 09:06:34 -0700 | [diff] [blame] | 224 | |
| 225 | // If the paint affects transparent black, we can't trust any of our calculated bounds. |
| 226 | const SkIRect& bounds = |
| 227 | PaintMayAffectTransparentBlack(sb.paint) ? fCurrentClipBounds : sb.bounds; |
| 228 | |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 229 | while (sb.controlOps --> 0) { |
mtklein | d910f54 | 2014-08-22 09:06:34 -0700 | [diff] [blame] | 230 | this->popControl(bounds); |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | // This whole Save block may be part another Save block. |
mtklein | d910f54 | 2014-08-22 09:06:34 -0700 | [diff] [blame] | 234 | this->updateSaveBounds(bounds); |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 235 | |
| 236 | // If called from a real Restore (not a phony one for balance), it'll need the bounds. |
mtklein | d910f54 | 2014-08-22 09:06:34 -0700 | [diff] [blame] | 237 | return bounds; |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | void pushControl() { |
| 241 | fControlIndices.push(fCurrentOp); |
| 242 | if (!fSaveStack.isEmpty()) { |
| 243 | fSaveStack.top().controlOps++; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | void popControl(const SkIRect& bounds) { |
| 248 | fBounds[fControlIndices.top()] = bounds; |
| 249 | fControlIndices.pop(); |
| 250 | } |
| 251 | |
| 252 | void updateSaveBounds(const SkIRect& bounds) { |
| 253 | // If we're in a Save block, expand its bounds to cover these bounds too. |
| 254 | if (!fSaveStack.isEmpty()) { |
| 255 | fSaveStack.top().bounds.join(bounds); |
| 256 | } |
| 257 | } |
| 258 | |
mtklein | 131a22b | 2014-08-25 14:16:15 -0700 | [diff] [blame^] | 259 | // FIXME: this method could use better bounds |
| 260 | SkIRect bounds(const DrawText&) const { return fCurrentClipBounds; } |
mtklein | 68199a2 | 2014-08-25 13:49:29 -0700 | [diff] [blame] | 261 | |
mtklein | 131a22b | 2014-08-25 14:16:15 -0700 | [diff] [blame^] | 262 | SkIRect bounds(const Clear&) const { return SkIRect::MakeLargest(); } // Ignores the clip. |
mtklein | 68199a2 | 2014-08-25 13:49:29 -0700 | [diff] [blame] | 263 | SkIRect bounds(const DrawPaint&) const { return fCurrentClipBounds; } |
mtklein | 479601b | 2014-08-18 08:45:33 -0700 | [diff] [blame] | 264 | SkIRect bounds(const NoOp&) const { return SkIRect::MakeEmpty(); } // NoOps don't draw. |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 265 | |
mtklein | 131a22b | 2014-08-25 14:16:15 -0700 | [diff] [blame^] | 266 | SkIRect bounds(const DrawSprite& op) const { |
| 267 | const SkBitmap& bm = op.bitmap; |
| 268 | return SkIRect::MakeXYWH(op.left, op.top, bm.width(), bm.height()); // Ignores the matrix. |
| 269 | } |
| 270 | |
mtklein | 62b67ae | 2014-08-18 11:10:37 -0700 | [diff] [blame] | 271 | SkIRect bounds(const DrawRect& op) const { return this->adjustAndMap(op.rect, &op.paint); } |
| 272 | SkIRect bounds(const DrawOval& op) const { return this->adjustAndMap(op.oval, &op.paint); } |
| 273 | SkIRect bounds(const DrawRRect& op) const { |
| 274 | return this->adjustAndMap(op.rrect.rect(), &op.paint); |
| 275 | } |
| 276 | SkIRect bounds(const DrawDRRect& op) const { |
| 277 | return this->adjustAndMap(op.outer.rect(), &op.paint); |
| 278 | } |
| 279 | |
| 280 | SkIRect bounds(const DrawBitmapRectToRect& op) const { |
| 281 | return this->adjustAndMap(op.dst, op.paint); |
| 282 | } |
| 283 | SkIRect bounds(const DrawBitmapNine& op) const { |
| 284 | return this->adjustAndMap(op.dst, op.paint); |
| 285 | } |
| 286 | SkIRect bounds(const DrawBitmap& op) const { |
| 287 | const SkBitmap& bm = op.bitmap; |
| 288 | return this->adjustAndMap(SkRect::MakeXYWH(op.left, op.top, bm.width(), bm.height()), |
| 289 | op.paint); |
| 290 | } |
| 291 | SkIRect bounds(const DrawBitmapMatrix& op) const { |
| 292 | const SkBitmap& bm = op.bitmap; |
| 293 | SkRect dst = SkRect::MakeWH(bm.width(), bm.height()); |
| 294 | op.matrix.mapRect(&dst); |
| 295 | return this->adjustAndMap(dst, op.paint); |
| 296 | } |
| 297 | |
| 298 | SkIRect bounds(const DrawPath& op) const { |
| 299 | return op.path.isInverseFillType() ? fCurrentClipBounds |
| 300 | : this->adjustAndMap(op.path.getBounds(), &op.paint); |
| 301 | } |
| 302 | SkIRect bounds(const DrawPoints& op) const { |
| 303 | SkRect dst; |
| 304 | dst.set(op.pts, op.count); |
| 305 | |
| 306 | // Pad the bounding box a little to make sure hairline points' bounds aren't empty. |
| 307 | SkScalar stroke = SkMaxScalar(op.paint.getStrokeWidth(), 0.01f); |
| 308 | dst.outset(stroke/2, stroke/2); |
| 309 | |
| 310 | return this->adjustAndMap(dst, &op.paint); |
| 311 | } |
mtklein | 131a22b | 2014-08-25 14:16:15 -0700 | [diff] [blame^] | 312 | SkIRect bounds(const DrawPatch& op) const { |
| 313 | SkRect dst; |
| 314 | dst.set(op.cubics, SkPatchUtils::kNumCtrlPts); |
| 315 | return this->adjustAndMap(dst, &op.paint); |
| 316 | } |
| 317 | SkIRect bounds(const DrawVertices& op) const { |
| 318 | SkRect dst; |
| 319 | dst.set(op.vertices, op.vertexCount); |
| 320 | return this->adjustAndMap(dst, &op.paint); |
| 321 | } |
| 322 | |
| 323 | SkIRect bounds(const DrawPicture& op) const { |
| 324 | SkRect dst = SkRect::MakeWH(op.picture->width(), op.picture->height()); |
| 325 | if (op.matrix) { |
| 326 | op.matrix->mapRect(&dst); |
| 327 | } |
| 328 | return this->adjustAndMap(dst, op.paint); |
| 329 | } |
mtklein | 62b67ae | 2014-08-18 11:10:37 -0700 | [diff] [blame] | 330 | |
| 331 | SkIRect bounds(const DrawPosText& op) const { |
| 332 | const int N = op.paint.countText(op.text, op.byteLength); |
| 333 | if (N == 0) { |
| 334 | return SkIRect::MakeEmpty(); |
| 335 | } |
| 336 | |
| 337 | SkRect dst; |
| 338 | dst.set(op.pos, op.paint.countText(op.text, N)); |
| 339 | AdjustTextForFontMetrics(&dst, op.paint); |
| 340 | return this->adjustAndMap(dst, &op.paint); |
| 341 | } |
| 342 | SkIRect bounds(const DrawPosTextH& op) const { |
| 343 | const int N = op.paint.countText(op.text, op.byteLength); |
| 344 | if (N == 0) { |
| 345 | return SkIRect::MakeEmpty(); |
| 346 | } |
| 347 | |
| 348 | SkScalar left = op.xpos[0], right = op.xpos[0]; |
| 349 | for (int i = 1; i < N; i++) { |
| 350 | left = SkMinScalar(left, op.xpos[i]); |
| 351 | right = SkMaxScalar(right, op.xpos[i]); |
| 352 | } |
| 353 | SkRect dst = { left, op.y, right, op.y }; |
| 354 | AdjustTextForFontMetrics(&dst, op.paint); |
| 355 | return this->adjustAndMap(dst, &op.paint); |
| 356 | } |
mtklein | 131a22b | 2014-08-25 14:16:15 -0700 | [diff] [blame^] | 357 | SkIRect bounds(const DrawTextOnPath& op) const { |
| 358 | SkRect dst = op.path.getBounds(); |
| 359 | |
| 360 | // Pad all sides by the maximum padding in any direction we'd normally apply. |
| 361 | SkRect pad = { 0, 0, 0, 0}; |
| 362 | AdjustTextForFontMetrics(&pad, op.paint); |
| 363 | |
| 364 | // That maximum padding happens to always be the right pad today. |
| 365 | SkASSERT(pad.fLeft == -pad.fRight); |
| 366 | SkASSERT(pad.fTop == -pad.fBottom); |
| 367 | SkASSERT(pad.fRight > pad.fBottom); |
| 368 | dst.outset(pad.fRight, pad.fRight); |
| 369 | |
| 370 | return this->adjustAndMap(dst, &op.paint); |
| 371 | } |
| 372 | |
| 373 | SkIRect bounds(const DrawTextBlob& op) const { |
| 374 | SkRect dst = op.blob->bounds(); |
| 375 | dst.offset(op.x, op.y); |
| 376 | // TODO: remove when implicit bounds are plumbed through |
| 377 | if (dst.isEmpty()) { |
| 378 | return fCurrentClipBounds; |
| 379 | } |
| 380 | return this->adjustAndMap(dst, &op.paint); |
| 381 | } |
mtklein | 62b67ae | 2014-08-18 11:10:37 -0700 | [diff] [blame] | 382 | |
| 383 | static void AdjustTextForFontMetrics(SkRect* rect, const SkPaint& paint) { |
caryclark | 9a657fa | 2014-08-20 05:24:29 -0700 | [diff] [blame] | 384 | #ifdef SK_DEBUG |
mtklein | a19afb4 | 2014-08-19 17:47:14 -0700 | [diff] [blame] | 385 | SkRect correct = *rect; |
| 386 | #endif |
mtklein | c846049 | 2014-08-21 16:39:12 -0700 | [diff] [blame] | 387 | const SkScalar yPad = 2.0f * paint.getTextSize(), // In practice, this seems to be enough. |
mtklein | a19afb4 | 2014-08-19 17:47:14 -0700 | [diff] [blame] | 388 | xPad = 4.0f * yPad; // Hack for very wide Github logo font. |
| 389 | rect->outset(xPad, yPad); |
caryclark | 9a657fa | 2014-08-20 05:24:29 -0700 | [diff] [blame] | 390 | #ifdef SK_DEBUG |
mtklein | 62b67ae | 2014-08-18 11:10:37 -0700 | [diff] [blame] | 391 | SkPaint::FontMetrics metrics; |
| 392 | paint.getFontMetrics(&metrics); |
mtklein | a19afb4 | 2014-08-19 17:47:14 -0700 | [diff] [blame] | 393 | correct.fLeft += metrics.fXMin; |
| 394 | correct.fTop += metrics.fTop; |
| 395 | correct.fRight += metrics.fXMax; |
| 396 | correct.fBottom += metrics.fBottom; |
mtklein | d13291a | 2014-08-21 14:46:49 -0700 | [diff] [blame] | 397 | // See skia:2862 for why we ignore small text sizes. |
| 398 | SkASSERTF(paint.getTextSize() < 0.001f || rect->contains(correct), |
| 399 | "%f %f %f %f vs. %f %f %f %f\n", |
mtklein | a19afb4 | 2014-08-19 17:47:14 -0700 | [diff] [blame] | 400 | -xPad, -yPad, +xPad, +yPad, |
| 401 | metrics.fXMin, metrics.fTop, metrics.fXMax, metrics.fBottom); |
| 402 | #endif |
mtklein | 62b67ae | 2014-08-18 11:10:37 -0700 | [diff] [blame] | 403 | } |
| 404 | |
mtklein | 479601b | 2014-08-18 08:45:33 -0700 | [diff] [blame] | 405 | // Returns true if rect was meaningfully adjusted for the effects of paint, |
| 406 | // false if the paint could affect the rect in unknown ways. |
| 407 | static bool AdjustForPaint(const SkPaint* paint, SkRect* rect) { |
mtklein | a723b57 | 2014-08-15 11:49:49 -0700 | [diff] [blame] | 408 | if (paint) { |
| 409 | if (paint->canComputeFastBounds()) { |
mtklein | 479601b | 2014-08-18 08:45:33 -0700 | [diff] [blame] | 410 | *rect = paint->computeFastBounds(*rect, rect); |
| 411 | return true; |
mtklein | a723b57 | 2014-08-15 11:49:49 -0700 | [diff] [blame] | 412 | } |
mtklein | 479601b | 2014-08-18 08:45:33 -0700 | [diff] [blame] | 413 | return false; |
| 414 | } |
| 415 | return true; |
| 416 | } |
| 417 | |
| 418 | // Adjust rect for all paints that may affect its geometry, then map it to device space. |
| 419 | SkIRect adjustAndMap(SkRect rect, const SkPaint* paint) const { |
| 420 | // Inverted rectangles really confuse our BBHs. |
| 421 | rect.sort(); |
| 422 | |
| 423 | // Adjust the rect for its own paint. |
| 424 | if (!AdjustForPaint(paint, &rect)) { |
| 425 | // The paint could do anything to our bounds. The only safe answer is the current clip. |
| 426 | return fCurrentClipBounds; |
mtklein | a723b57 | 2014-08-15 11:49:49 -0700 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | // Adjust rect for all the paints from the SaveLayers we're inside. |
mtklein | a723b57 | 2014-08-15 11:49:49 -0700 | [diff] [blame] | 430 | for (int i = fSaveStack.count() - 1; i >= 0; i--) { |
mtklein | 479601b | 2014-08-18 08:45:33 -0700 | [diff] [blame] | 431 | if (!AdjustForPaint(fSaveStack[i].paint, &rect)) { |
| 432 | // Same deal as above. |
| 433 | return fCurrentClipBounds; |
mtklein | a723b57 | 2014-08-15 11:49:49 -0700 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | |
| 437 | // Map the rect back to device space. |
mtklein | 6332f1d | 2014-08-19 07:09:40 -0700 | [diff] [blame] | 438 | fCTM->mapRect(&rect); |
mtklein | a723b57 | 2014-08-15 11:49:49 -0700 | [diff] [blame] | 439 | SkIRect devRect; |
| 440 | rect.roundOut(&devRect); |
mtklein | 479601b | 2014-08-18 08:45:33 -0700 | [diff] [blame] | 441 | |
| 442 | // Nothing can draw outside the current clip. |
| 443 | // (Only bounded ops call into this method, so oddballs like Clear don't matter here.) |
| 444 | devRect.intersect(fCurrentClipBounds); |
mtklein | a723b57 | 2014-08-15 11:49:49 -0700 | [diff] [blame] | 445 | return devRect; |
| 446 | } |
| 447 | |
| 448 | // Conservative device bounds for each op in the SkRecord. |
| 449 | SkAutoTMalloc<SkIRect> fBounds; |
| 450 | |
| 451 | // We walk fCurrentOp through the SkRecord, as we go using updateCTM() |
| 452 | // and updateClipBounds() to maintain the exact CTM (fCTM) and conservative |
| 453 | // device bounds of the current clip (fCurrentClipBounds). |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 454 | unsigned fCurrentOp; |
mtklein | 6332f1d | 2014-08-19 07:09:40 -0700 | [diff] [blame] | 455 | const SkMatrix* fCTM; |
mtklein | a723b57 | 2014-08-15 11:49:49 -0700 | [diff] [blame] | 456 | SkIRect fCurrentClipBounds; |
| 457 | |
| 458 | // Used to track the bounds of Save/Restore blocks and the control ops inside them. |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 459 | SkTDArray<SaveBounds> fSaveStack; |
| 460 | SkTDArray<unsigned> fControlIndices; |
mtklein | 5ad6ee1 | 2014-08-11 08:08:43 -0700 | [diff] [blame] | 461 | }; |
| 462 | |
commit-bot@chromium.org | 27f6b0d | 2014-05-09 14:59:29 +0000 | [diff] [blame] | 463 | } // namespace SkRecords |
mtklein | 5ad6ee1 | 2014-08-11 08:08:43 -0700 | [diff] [blame] | 464 | |
| 465 | void SkRecordFillBounds(const SkRecord& record, SkBBoxHierarchy* bbh) { |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 466 | SkRecords::FillBounds(record, bbh); |
mtklein | 5ad6ee1 | 2014-08-11 08:08:43 -0700 | [diff] [blame] | 467 | } |