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