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 | |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame^] | 8 | #include "SkLayerInfo.h" |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 9 | #include "SkRecordDraw.h" |
mtklein | 131a22b | 2014-08-25 14:16:15 -0700 | [diff] [blame] | 10 | #include "SkPatchUtils.h" |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 11 | |
mtklein | 5ad6ee1 | 2014-08-11 08:08:43 -0700 | [diff] [blame] | 12 | void SkRecordDraw(const SkRecord& record, |
| 13 | SkCanvas* canvas, |
| 14 | const SkBBoxHierarchy* bbh, |
| 15 | SkDrawPictureCallback* callback) { |
Mike Klein | c11530e | 2014-06-24 11:29:06 -0400 | [diff] [blame] | 16 | SkAutoCanvasRestore saveRestore(canvas, true /*save now, restore at exit*/); |
mtklein | 5ad6ee1 | 2014-08-11 08:08:43 -0700 | [diff] [blame] | 17 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 18 | if (bbh) { |
mtklein | 5ad6ee1 | 2014-08-11 08:08:43 -0700 | [diff] [blame] | 19 | // Draw only ops that affect pixels in the canvas's current clip. |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 20 | // The SkRecord and BBH were recorded in identity space. This canvas |
| 21 | // is not necessarily in that same space. getClipBounds() returns us |
| 22 | // this canvas' clip bounds transformed back into identity space, which |
| 23 | // lets us query the BBH. |
junov | a41d3c3 | 2014-10-30 11:44:19 -0700 | [diff] [blame] | 24 | SkRect query; |
| 25 | if (!canvas->getClipBounds(&query)) { |
| 26 | return; |
| 27 | } |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 28 | |
mtklein | 6bd4196 | 2014-10-02 07:41:56 -0700 | [diff] [blame] | 29 | SkTDArray<unsigned> 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++) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 34 | if (callback && callback->abortDrawing()) { |
mtklein | 5ad6ee1 | 2014-08-11 08:08:43 -0700 | [diff] [blame] | 35 | return; |
| 36 | } |
danakj | d239d42 | 2014-11-03 12:43:30 -0800 | [diff] [blame] | 37 | // This visit call uses the SkRecords::Draw::operator() to call |
| 38 | // methods on the |canvas|, wrapped by methods defined with the |
| 39 | // DRAW() macro. |
mtklein | 6bd4196 | 2014-10-02 07:41:56 -0700 | [diff] [blame] | 40 | record.visit<void>(ops[i], draw); |
mtklein | 5ad6ee1 | 2014-08-11 08:08:43 -0700 | [diff] [blame] | 41 | } |
| 42 | } else { |
| 43 | // Draw all ops. |
mtklein | 00f30bd | 2014-09-02 12:03:31 -0700 | [diff] [blame] | 44 | SkRecords::Draw draw(canvas); |
| 45 | for (unsigned i = 0; i < record.count(); i++) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 46 | if (callback && callback->abortDrawing()) { |
mtklein | 5ad6ee1 | 2014-08-11 08:08:43 -0700 | [diff] [blame] | 47 | return; |
| 48 | } |
danakj | d239d42 | 2014-11-03 12:43:30 -0800 | [diff] [blame] | 49 | // This visit call uses the SkRecords::Draw::operator() to call |
| 50 | // methods on the |canvas|, wrapped by methods defined with the |
| 51 | // DRAW() macro. |
mtklein | 00f30bd | 2014-09-02 12:03:31 -0700 | [diff] [blame] | 52 | record.visit<void>(i, draw); |
mtklein | 5ad6ee1 | 2014-08-11 08:08:43 -0700 | [diff] [blame] | 53 | } |
commit-bot@chromium.org | 73b55eb | 2014-04-14 20:35:12 +0000 | [diff] [blame] | 54 | } |
commit-bot@chromium.org | 27f6b0d | 2014-05-09 14:59:29 +0000 | [diff] [blame] | 55 | } |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 56 | |
mtklein | 00f30bd | 2014-09-02 12:03:31 -0700 | [diff] [blame] | 57 | void SkRecordPartialDraw(const SkRecord& record, |
| 58 | SkCanvas* canvas, |
| 59 | const SkRect& clearRect, |
robertphillips | 4815fe5 | 2014-09-16 10:32:43 -0700 | [diff] [blame] | 60 | unsigned start, unsigned stop, |
| 61 | const SkMatrix& initialCTM) { |
mtklein | 00f30bd | 2014-09-02 12:03:31 -0700 | [diff] [blame] | 62 | SkAutoCanvasRestore saveRestore(canvas, true /*save now, restore at exit*/); |
| 63 | |
| 64 | stop = SkTMin(stop, record.count()); |
robertphillips | 4815fe5 | 2014-09-16 10:32:43 -0700 | [diff] [blame] | 65 | SkRecords::PartialDraw draw(canvas, clearRect, initialCTM); |
mtklein | 00f30bd | 2014-09-02 12:03:31 -0700 | [diff] [blame] | 66 | for (unsigned i = start; i < stop; i++) { |
| 67 | record.visit<void>(i, draw); |
| 68 | } |
| 69 | } |
| 70 | |
commit-bot@chromium.org | 27f6b0d | 2014-05-09 14:59:29 +0000 | [diff] [blame] | 71 | namespace SkRecords { |
commit-bot@chromium.org | 73b55eb | 2014-04-14 20:35:12 +0000 | [diff] [blame] | 72 | |
mtklein | 7cdc1ee | 2014-07-07 10:41:04 -0700 | [diff] [blame] | 73 | // FIXME: SkBitmaps are stateful, so we need to copy them to play back in multiple threads. |
| 74 | static SkBitmap shallow_copy(const SkBitmap& bitmap) { |
| 75 | return bitmap; |
| 76 | } |
| 77 | |
commit-bot@chromium.org | 2e0c32a | 2014-04-28 16:19:45 +0000 | [diff] [blame] | 78 | // NoOps draw nothing. |
commit-bot@chromium.org | 27f6b0d | 2014-05-09 14:59:29 +0000 | [diff] [blame] | 79 | template <> void Draw::draw(const NoOp&) {} |
commit-bot@chromium.org | 73b55eb | 2014-04-14 20:35:12 +0000 | [diff] [blame] | 80 | |
commit-bot@chromium.org | 27f6b0d | 2014-05-09 14:59:29 +0000 | [diff] [blame] | 81 | #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] | 82 | DRAW(Restore, restore()); |
Florin Malita | 5f6102d | 2014-06-30 10:13:28 -0400 | [diff] [blame] | 83 | DRAW(Save, save()); |
commit-bot@chromium.org | 73b55eb | 2014-04-14 20:35:12 +0000 | [diff] [blame] | 84 | DRAW(SaveLayer, saveLayer(r.bounds, r.paint, r.flags)); |
| 85 | DRAW(PopCull, popCull()); |
commit-bot@chromium.org | 88c3e27 | 2014-04-22 16:57:20 +0000 | [diff] [blame] | 86 | DRAW(PushCull, pushCull(r.rect)); |
commit-bot@chromium.org | 73b55eb | 2014-04-14 20:35:12 +0000 | [diff] [blame] | 87 | DRAW(Clear, clear(r.color)); |
commit-bot@chromium.org | 99bd7d8 | 2014-05-19 15:51:12 +0000 | [diff] [blame] | 88 | DRAW(SetMatrix, setMatrix(SkMatrix::Concat(fInitialCTM, r.matrix))); |
commit-bot@chromium.org | 73b55eb | 2014-04-14 20:35:12 +0000 | [diff] [blame] | 89 | |
| 90 | DRAW(ClipPath, clipPath(r.path, r.op, r.doAA)); |
| 91 | DRAW(ClipRRect, clipRRect(r.rrect, r.op, r.doAA)); |
| 92 | DRAW(ClipRect, clipRect(r.rect, r.op, r.doAA)); |
| 93 | DRAW(ClipRegion, clipRegion(r.region, r.op)); |
| 94 | |
mtklein | 5f0e822 | 2014-08-22 11:44:26 -0700 | [diff] [blame] | 95 | DRAW(BeginCommentGroup, beginCommentGroup(r.description)); |
| 96 | DRAW(AddComment, addComment(r.key, r.value)); |
| 97 | DRAW(EndCommentGroup, endCommentGroup()); |
| 98 | |
mtklein | 7cdc1ee | 2014-07-07 10:41:04 -0700 | [diff] [blame] | 99 | DRAW(DrawBitmap, drawBitmap(shallow_copy(r.bitmap), r.left, r.top, r.paint)); |
| 100 | DRAW(DrawBitmapMatrix, drawBitmapMatrix(shallow_copy(r.bitmap), r.matrix, r.paint)); |
| 101 | DRAW(DrawBitmapNine, drawBitmapNine(shallow_copy(r.bitmap), r.center, r.dst, r.paint)); |
| 102 | DRAW(DrawBitmapRectToRect, |
| 103 | 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] | 104 | DRAW(DrawDRRect, drawDRRect(r.outer, r.inner, r.paint)); |
piotaixr | 6515175 | 2014-10-16 11:58:39 -0700 | [diff] [blame] | 105 | DRAW(DrawImage, drawImage(r.image, r.left, r.top, r.paint)); |
| 106 | DRAW(DrawImageRect, drawImageRect(r.image, r.src, r.dst, r.paint)); |
commit-bot@chromium.org | 73b55eb | 2014-04-14 20:35:12 +0000 | [diff] [blame] | 107 | DRAW(DrawOval, drawOval(r.oval, r.paint)); |
| 108 | DRAW(DrawPaint, drawPaint(r.paint)); |
| 109 | DRAW(DrawPath, drawPath(r.path, r.paint)); |
mtklein | 9b222a5 | 2014-09-18 11:16:31 -0700 | [diff] [blame] | 110 | DRAW(DrawPatch, drawPatch(r.cubics, r.colors, r.texCoords, r.xmode, r.paint)); |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 111 | DRAW(DrawPicture, drawPicture(r.picture, r.matrix, r.paint)); |
commit-bot@chromium.org | 73b55eb | 2014-04-14 20:35:12 +0000 | [diff] [blame] | 112 | DRAW(DrawPoints, drawPoints(r.mode, r.count, r.pts, r.paint)); |
| 113 | DRAW(DrawPosText, drawPosText(r.text, r.byteLength, r.pos, r.paint)); |
| 114 | DRAW(DrawPosTextH, drawPosTextH(r.text, r.byteLength, r.xpos, r.y, r.paint)); |
| 115 | DRAW(DrawRRect, drawRRect(r.rrect, r.paint)); |
| 116 | DRAW(DrawRect, drawRect(r.rect, r.paint)); |
mtklein | 7cdc1ee | 2014-07-07 10:41:04 -0700 | [diff] [blame] | 117 | 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] | 118 | DRAW(DrawText, drawText(r.text, r.byteLength, r.x, r.y, r.paint)); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 119 | 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] | 120 | DRAW(DrawTextOnPath, drawTextOnPath(r.text, r.byteLength, r.path, r.matrix, r.paint)); |
| 121 | DRAW(DrawVertices, drawVertices(r.vmode, r.vertexCount, r.vertices, r.texs, r.colors, |
| 122 | r.xmode.get(), r.indices, r.indexCount, r.paint)); |
mtklein | 29dfaa8 | 2014-09-04 14:12:44 -0700 | [diff] [blame] | 123 | DRAW(DrawData, drawData(r.data, r.length)); |
commit-bot@chromium.org | 73b55eb | 2014-04-14 20:35:12 +0000 | [diff] [blame] | 124 | #undef DRAW |
| 125 | |
mtklein | 5ad6ee1 | 2014-08-11 08:08:43 -0700 | [diff] [blame] | 126 | // This is an SkRecord visitor that fills an SkBBoxHierarchy. |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 127 | // |
| 128 | // The interesting part here is how to calculate bounds for ops which don't |
| 129 | // have intrinsic bounds. What is the bounds of a Save or a Translate? |
| 130 | // |
| 131 | // We answer this by thinking about a particular definition of bounds: if I |
| 132 | // don't execute this op, pixels in this rectangle might draw incorrectly. So |
| 133 | // the bounds of a Save, a Translate, a Restore, etc. are the union of the |
| 134 | // bounds of Draw* ops that they might have an effect on. For any given |
| 135 | // Save/Restore block, the bounds of the Save, the Restore, and any other |
| 136 | // non-drawing ("control") ops inside are exactly the union of the bounds of |
| 137 | // the drawing ops inside that block. |
| 138 | // |
| 139 | // To implement this, we keep a stack of active Save blocks. As we consume ops |
| 140 | // inside the Save/Restore block, drawing ops are unioned with the bounds of |
| 141 | // the block, and control ops are stashed away for later. When we finish the |
| 142 | // block with a Restore, our bounds are complete, and we go back and fill them |
| 143 | // in for all the control ops we stashed away. |
mtklein | 5ad6ee1 | 2014-08-11 08:08:43 -0700 | [diff] [blame] | 144 | class FillBounds : SkNoncopyable { |
| 145 | public: |
robertphillips | 4e8e342 | 2014-11-12 06:46:08 -0800 | [diff] [blame] | 146 | FillBounds(const SkRect& cullRect, const SkRecord& record) |
| 147 | : fNumRecords(record.count()) |
| 148 | , fCullRect(cullRect) |
robertphillips | 4d52afe | 2014-11-03 08:19:44 -0800 | [diff] [blame] | 149 | , fBounds(record.count()) { |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 150 | // Calculate bounds for all ops. This won't go quite in order, so we'll need |
| 151 | // to store the bounds separately then feed them in to the BBH later in order. |
mtklein | 6332f1d | 2014-08-19 07:09:40 -0700 | [diff] [blame] | 152 | fCTM = &SkMatrix::I(); |
robertphillips | 4d52afe | 2014-11-03 08:19:44 -0800 | [diff] [blame] | 153 | fCurrentClipBounds = fCullRect; |
robertphillips | 4e8e342 | 2014-11-12 06:46:08 -0800 | [diff] [blame] | 154 | } |
mtklein | 5ad6ee1 | 2014-08-11 08:08:43 -0700 | [diff] [blame] | 155 | |
robertphillips | 4e8e342 | 2014-11-12 06:46:08 -0800 | [diff] [blame] | 156 | void setCurrentOp(unsigned currentOp) { fCurrentOp = currentOp; } |
| 157 | |
| 158 | void cleanUp(SkBBoxHierarchy* bbh) { |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 159 | // If we have any lingering unpaired Saves, simulate restores to make |
| 160 | // sure all ops in those Save blocks have their bounds calculated. |
| 161 | while (!fSaveStack.isEmpty()) { |
| 162 | this->popSaveBlock(); |
| 163 | } |
| 164 | |
| 165 | // Any control ops not part of any Save/Restore block draw everywhere. |
| 166 | while (!fControlIndices.isEmpty()) { |
robertphillips | 4d52afe | 2014-11-03 08:19:44 -0800 | [diff] [blame] | 167 | this->popControl(fCullRect); |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | // 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] | 171 | SkASSERT(bbh); |
robertphillips | 4e8e342 | 2014-11-12 06:46:08 -0800 | [diff] [blame] | 172 | bbh->insert(&fBounds, fNumRecords); |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 173 | } |
mtklein | 5ad6ee1 | 2014-08-11 08:08:43 -0700 | [diff] [blame] | 174 | |
mtklein | a723b57 | 2014-08-15 11:49:49 -0700 | [diff] [blame] | 175 | template <typename T> void operator()(const T& op) { |
| 176 | this->updateCTM(op); |
| 177 | this->updateClipBounds(op); |
| 178 | this->trackBounds(op); |
mtklein | 5ad6ee1 | 2014-08-11 08:08:43 -0700 | [diff] [blame] | 179 | } |
| 180 | |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 181 | // In this file, SkRect are in local coordinates, Bounds are translated back to identity space. |
| 182 | typedef SkRect Bounds; |
| 183 | |
robertphillips | 4e8e342 | 2014-11-12 06:46:08 -0800 | [diff] [blame] | 184 | unsigned currentOp() const { return fCurrentOp; } |
| 185 | const SkMatrix& ctm() const { return *fCTM; } |
robertphillips | 4e8e342 | 2014-11-12 06:46:08 -0800 | [diff] [blame] | 186 | const Bounds& getBounds(unsigned index) const { return fBounds[index]; } |
| 187 | |
| 188 | // Adjust rect for all paints that may affect its geometry, then map it to identity space. |
| 189 | Bounds adjustAndMap(SkRect rect, const SkPaint* paint) const { |
| 190 | // Inverted rectangles really confuse our BBHs. |
| 191 | rect.sort(); |
| 192 | |
| 193 | // Adjust the rect for its own paint. |
| 194 | if (!AdjustForPaint(paint, &rect)) { |
| 195 | // The paint could do anything to our bounds. The only safe answer is the current clip. |
| 196 | return fCurrentClipBounds; |
| 197 | } |
| 198 | |
| 199 | // Adjust rect for all the paints from the SaveLayers we're inside. |
| 200 | if (!this->adjustForSaveLayerPaints(&rect)) { |
| 201 | // Same deal as above. |
| 202 | return fCurrentClipBounds; |
| 203 | } |
| 204 | |
| 205 | // Map the rect back to identity space. |
| 206 | fCTM->mapRect(&rect); |
| 207 | |
| 208 | // Nothing can draw outside the current clip. |
| 209 | // (Only bounded ops call into this method, so oddballs like Clear don't matter here.) |
| 210 | rect.intersect(fCurrentClipBounds); |
| 211 | return rect; |
| 212 | } |
| 213 | |
| 214 | private: |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 215 | struct SaveBounds { |
mtklein | a723b57 | 2014-08-15 11:49:49 -0700 | [diff] [blame] | 216 | int controlOps; // Number of control ops in this Save block, including the Save. |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 217 | Bounds bounds; // Bounds of everything in the block. |
mtklein | a723b57 | 2014-08-15 11:49:49 -0700 | [diff] [blame] | 218 | 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] | 219 | }; |
| 220 | |
mtklein | 8e393bf | 2014-10-01 12:48:58 -0700 | [diff] [blame] | 221 | // Only Restore and SetMatrix change the CTM. |
| 222 | template <typename T> void updateCTM(const T&) {} |
mtklein | 6332f1d | 2014-08-19 07:09:40 -0700 | [diff] [blame] | 223 | void updateCTM(const Restore& op) { fCTM = &op.matrix; } |
| 224 | void updateCTM(const SetMatrix& op) { fCTM = &op.matrix; } |
mtklein | a723b57 | 2014-08-15 11:49:49 -0700 | [diff] [blame] | 225 | |
mtklein | 8e393bf | 2014-10-01 12:48:58 -0700 | [diff] [blame] | 226 | // Most ops don't change the clip. |
| 227 | template <typename T> void updateClipBounds(const T&) {} |
Mike Klein | 271a030 | 2014-09-23 15:28:38 -0400 | [diff] [blame] | 228 | |
mtklein | 8e393bf | 2014-10-01 12:48:58 -0700 | [diff] [blame] | 229 | // Clip{Path,RRect,Rect,Region} obviously change the clip. They all know their bounds already. |
| 230 | void updateClipBounds(const ClipPath& op) { this->updateClipBoundsForClipOp(op.devBounds); } |
| 231 | void updateClipBounds(const ClipRRect& op) { this->updateClipBoundsForClipOp(op.devBounds); } |
| 232 | void updateClipBounds(const ClipRect& op) { this->updateClipBoundsForClipOp(op.devBounds); } |
| 233 | void updateClipBounds(const ClipRegion& op) { this->updateClipBoundsForClipOp(op.devBounds); } |
Mike Klein | 271a030 | 2014-09-23 15:28:38 -0400 | [diff] [blame] | 234 | |
mtklein | 8e393bf | 2014-10-01 12:48:58 -0700 | [diff] [blame] | 235 | // The bounds of clip ops need to be adjusted for the paints of saveLayers they're inside. |
| 236 | void updateClipBoundsForClipOp(const SkIRect& devBounds) { |
| 237 | Bounds clip = SkRect::Make(devBounds); |
Mike Klein | 271a030 | 2014-09-23 15:28:38 -0400 | [diff] [blame] | 238 | // We don't call adjustAndMap() because as its last step it would intersect the adjusted |
| 239 | // clip bounds with the previous clip, exactly what we can't do when the clip grows. |
robertphillips | 4d52afe | 2014-11-03 08:19:44 -0800 | [diff] [blame] | 240 | fCurrentClipBounds = this->adjustForSaveLayerPaints(&clip) ? clip : fCullRect; |
Mike Klein | 271a030 | 2014-09-23 15:28:38 -0400 | [diff] [blame] | 241 | } |
| 242 | |
mtklein | 8e393bf | 2014-10-01 12:48:58 -0700 | [diff] [blame] | 243 | // Restore holds the devBounds for the clip after the {save,saveLayer}/restore block completes. |
| 244 | void updateClipBounds(const Restore& op) { |
| 245 | // This is just like the clip ops above, but we need to skip the effects (if any) of our |
| 246 | // paired saveLayer (if it is one); it has not yet been popped off the save stack. Our |
| 247 | // devBounds reflect the state of the world after the saveLayer/restore block is done, |
| 248 | // so they are not affected by the saveLayer's paint. |
| 249 | const int kSavesToIgnore = 1; |
| 250 | Bounds clip = SkRect::Make(op.devBounds); |
| 251 | fCurrentClipBounds = |
robertphillips | 4d52afe | 2014-11-03 08:19:44 -0800 | [diff] [blame] | 252 | this->adjustForSaveLayerPaints(&clip, kSavesToIgnore) ? clip : fCullRect; |
mtklein | 8e393bf | 2014-10-01 12:48:58 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Mike Klein | 271a030 | 2014-09-23 15:28:38 -0400 | [diff] [blame] | 255 | // We also take advantage of SaveLayer bounds when present to further cut the clip down. |
mtklein | a723b57 | 2014-08-15 11:49:49 -0700 | [diff] [blame] | 256 | void updateClipBounds(const SaveLayer& op) { |
| 257 | if (op.bounds) { |
Mike Klein | 271a030 | 2014-09-23 15:28:38 -0400 | [diff] [blame] | 258 | // adjustAndMap() intersects these layer bounds with the previous clip for us. |
| 259 | fCurrentClipBounds = this->adjustAndMap(*op.bounds, op.paint); |
mtklein | a723b57 | 2014-08-15 11:49:49 -0700 | [diff] [blame] | 260 | } |
| 261 | } |
mtklein | 6cfa73a | 2014-08-13 13:33:49 -0700 | [diff] [blame] | 262 | |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 263 | // The bounds of these ops must be calculated when we hit the Restore |
| 264 | // from the bounds of the ops in the same Save block. |
mtklein | a723b57 | 2014-08-15 11:49:49 -0700 | [diff] [blame] | 265 | void trackBounds(const Save&) { this->pushSaveBlock(NULL); } |
mtklein | a723b57 | 2014-08-15 11:49:49 -0700 | [diff] [blame] | 266 | void trackBounds(const SaveLayer& op) { this->pushSaveBlock(op.paint); } |
| 267 | void trackBounds(const Restore&) { fBounds[fCurrentOp] = this->popSaveBlock(); } |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 268 | |
mtklein | 68199a2 | 2014-08-25 13:49:29 -0700 | [diff] [blame] | 269 | void trackBounds(const SetMatrix&) { this->pushControl(); } |
| 270 | void trackBounds(const ClipRect&) { this->pushControl(); } |
| 271 | void trackBounds(const ClipRRect&) { this->pushControl(); } |
| 272 | void trackBounds(const ClipPath&) { this->pushControl(); } |
| 273 | void trackBounds(const ClipRegion&) { this->pushControl(); } |
| 274 | void trackBounds(const PushCull&) { this->pushControl(); } |
| 275 | void trackBounds(const PopCull&) { this->pushControl(); } |
| 276 | void trackBounds(const BeginCommentGroup&) { this->pushControl(); } |
| 277 | void trackBounds(const AddComment&) { this->pushControl(); } |
| 278 | void trackBounds(const EndCommentGroup&) { this->pushControl(); } |
mtklein | 29dfaa8 | 2014-09-04 14:12:44 -0700 | [diff] [blame] | 279 | void trackBounds(const DrawData&) { this->pushControl(); } |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 280 | |
| 281 | // For all other ops, we can calculate and store the bounds directly now. |
| 282 | template <typename T> void trackBounds(const T& op) { |
| 283 | fBounds[fCurrentOp] = this->bounds(op); |
| 284 | this->updateSaveBounds(fBounds[fCurrentOp]); |
mtklein | 5ad6ee1 | 2014-08-11 08:08:43 -0700 | [diff] [blame] | 285 | } |
| 286 | |
mtklein | a723b57 | 2014-08-15 11:49:49 -0700 | [diff] [blame] | 287 | void pushSaveBlock(const SkPaint* paint) { |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 288 | // Starting a new Save block. Push a new entry to represent that. |
robertphillips | 4d52afe | 2014-11-03 08:19:44 -0800 | [diff] [blame] | 289 | SaveBounds sb; |
| 290 | sb.controlOps = 0; |
| 291 | // If the paint affects transparent black, the bound shouldn't be smaller |
| 292 | // than the current clip bounds. |
| 293 | sb.bounds = |
| 294 | PaintMayAffectTransparentBlack(paint) ? fCurrentClipBounds : Bounds::MakeEmpty(); |
| 295 | sb.paint = paint; |
| 296 | |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 297 | fSaveStack.push(sb); |
| 298 | this->pushControl(); |
| 299 | } |
| 300 | |
mtklein | d910f54 | 2014-08-22 09:06:34 -0700 | [diff] [blame] | 301 | static bool PaintMayAffectTransparentBlack(const SkPaint* paint) { |
dneto | 327f905 | 2014-09-15 10:53:16 -0700 | [diff] [blame] | 302 | if (paint) { |
| 303 | // FIXME: this is very conservative |
| 304 | if (paint->getImageFilter() || paint->getColorFilter()) { |
| 305 | return true; |
| 306 | } |
| 307 | |
| 308 | // Unusual Xfermodes require us to process a saved layer |
| 309 | // even with operations outisde the clip. |
| 310 | // For example, DstIn is used by masking layers. |
| 311 | // https://code.google.com/p/skia/issues/detail?id=1291 |
| 312 | // https://crbug.com/401593 |
| 313 | SkXfermode* xfermode = paint->getXfermode(); |
| 314 | SkXfermode::Mode mode; |
| 315 | // SrcOver is ok, and is also the common case with a NULL xfermode. |
| 316 | // So we should make that the fast path and bypass the mode extraction |
| 317 | // and test. |
| 318 | if (xfermode && xfermode->asMode(&mode)) { |
| 319 | switch (mode) { |
| 320 | // For each of the following transfer modes, if the source |
| 321 | // alpha is zero (our transparent black), the resulting |
| 322 | // blended alpha is not necessarily equal to the original |
| 323 | // destination alpha. |
| 324 | case SkXfermode::kClear_Mode: |
| 325 | case SkXfermode::kSrc_Mode: |
| 326 | case SkXfermode::kSrcIn_Mode: |
| 327 | case SkXfermode::kDstIn_Mode: |
| 328 | case SkXfermode::kSrcOut_Mode: |
| 329 | case SkXfermode::kDstATop_Mode: |
| 330 | case SkXfermode::kModulate_Mode: |
| 331 | return true; |
| 332 | break; |
| 333 | default: |
| 334 | break; |
| 335 | } |
| 336 | } |
| 337 | } |
| 338 | return false; |
mtklein | d910f54 | 2014-08-22 09:06:34 -0700 | [diff] [blame] | 339 | } |
| 340 | |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 341 | Bounds popSaveBlock() { |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 342 | // We're done the Save block. Apply the block's bounds to all control ops inside it. |
| 343 | SaveBounds sb; |
| 344 | fSaveStack.pop(&sb); |
mtklein | d910f54 | 2014-08-22 09:06:34 -0700 | [diff] [blame] | 345 | |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 346 | while (sb.controlOps --> 0) { |
robertphillips | 4d52afe | 2014-11-03 08:19:44 -0800 | [diff] [blame] | 347 | this->popControl(sb.bounds); |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | // This whole Save block may be part another Save block. |
robertphillips | 4d52afe | 2014-11-03 08:19:44 -0800 | [diff] [blame] | 351 | this->updateSaveBounds(sb.bounds); |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 352 | |
| 353 | // If called from a real Restore (not a phony one for balance), it'll need the bounds. |
robertphillips | 4d52afe | 2014-11-03 08:19:44 -0800 | [diff] [blame] | 354 | return sb.bounds; |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | void pushControl() { |
| 358 | fControlIndices.push(fCurrentOp); |
| 359 | if (!fSaveStack.isEmpty()) { |
| 360 | fSaveStack.top().controlOps++; |
| 361 | } |
| 362 | } |
| 363 | |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 364 | void popControl(const Bounds& bounds) { |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 365 | fBounds[fControlIndices.top()] = bounds; |
| 366 | fControlIndices.pop(); |
| 367 | } |
| 368 | |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 369 | void updateSaveBounds(const Bounds& bounds) { |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 370 | // If we're in a Save block, expand its bounds to cover these bounds too. |
| 371 | if (!fSaveStack.isEmpty()) { |
| 372 | fSaveStack.top().bounds.join(bounds); |
| 373 | } |
| 374 | } |
| 375 | |
mtklein | 131a22b | 2014-08-25 14:16:15 -0700 | [diff] [blame] | 376 | // FIXME: this method could use better bounds |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 377 | Bounds bounds(const DrawText&) const { return fCurrentClipBounds; } |
mtklein | 68199a2 | 2014-08-25 13:49:29 -0700 | [diff] [blame] | 378 | |
robertphillips | 4d52afe | 2014-11-03 08:19:44 -0800 | [diff] [blame] | 379 | Bounds bounds(const Clear&) const { return fCullRect; } // Ignores the clip. |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 380 | Bounds bounds(const DrawPaint&) const { return fCurrentClipBounds; } |
| 381 | Bounds bounds(const NoOp&) const { return Bounds::MakeEmpty(); } // NoOps don't draw. |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 382 | |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 383 | Bounds bounds(const DrawSprite& op) const { |
mtklein | 131a22b | 2014-08-25 14:16:15 -0700 | [diff] [blame] | 384 | const SkBitmap& bm = op.bitmap; |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 385 | 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] | 386 | } |
| 387 | |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 388 | Bounds bounds(const DrawRect& op) const { return this->adjustAndMap(op.rect, &op.paint); } |
| 389 | Bounds bounds(const DrawOval& op) const { return this->adjustAndMap(op.oval, &op.paint); } |
| 390 | Bounds bounds(const DrawRRect& op) const { |
mtklein | 62b67ae | 2014-08-18 11:10:37 -0700 | [diff] [blame] | 391 | return this->adjustAndMap(op.rrect.rect(), &op.paint); |
| 392 | } |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 393 | Bounds bounds(const DrawDRRect& op) const { |
mtklein | 62b67ae | 2014-08-18 11:10:37 -0700 | [diff] [blame] | 394 | return this->adjustAndMap(op.outer.rect(), &op.paint); |
| 395 | } |
piotaixr | 6515175 | 2014-10-16 11:58:39 -0700 | [diff] [blame] | 396 | Bounds bounds(const DrawImage& op) const { |
| 397 | const SkImage* image = op.image; |
| 398 | SkRect rect = SkRect::MakeXYWH(op.left, op.top, image->width(), image->height()); |
mtklein | 62b67ae | 2014-08-18 11:10:37 -0700 | [diff] [blame] | 399 | |
piotaixr | 6515175 | 2014-10-16 11:58:39 -0700 | [diff] [blame] | 400 | return this->adjustAndMap(rect, op.paint); |
| 401 | } |
| 402 | Bounds bounds(const DrawImageRect& op) const { |
| 403 | return this->adjustAndMap(op.dst, op.paint); |
| 404 | } |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 405 | Bounds bounds(const DrawBitmapRectToRect& op) const { |
mtklein | 62b67ae | 2014-08-18 11:10:37 -0700 | [diff] [blame] | 406 | return this->adjustAndMap(op.dst, op.paint); |
| 407 | } |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 408 | Bounds bounds(const DrawBitmapNine& op) const { |
mtklein | 62b67ae | 2014-08-18 11:10:37 -0700 | [diff] [blame] | 409 | return this->adjustAndMap(op.dst, op.paint); |
| 410 | } |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 411 | Bounds bounds(const DrawBitmap& op) const { |
mtklein | 62b67ae | 2014-08-18 11:10:37 -0700 | [diff] [blame] | 412 | const SkBitmap& bm = op.bitmap; |
| 413 | return this->adjustAndMap(SkRect::MakeXYWH(op.left, op.top, bm.width(), bm.height()), |
| 414 | op.paint); |
| 415 | } |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 416 | Bounds bounds(const DrawBitmapMatrix& op) const { |
mtklein | 62b67ae | 2014-08-18 11:10:37 -0700 | [diff] [blame] | 417 | const SkBitmap& bm = op.bitmap; |
| 418 | SkRect dst = SkRect::MakeWH(bm.width(), bm.height()); |
| 419 | op.matrix.mapRect(&dst); |
| 420 | return this->adjustAndMap(dst, op.paint); |
| 421 | } |
| 422 | |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 423 | Bounds bounds(const DrawPath& op) const { |
mtklein | 62b67ae | 2014-08-18 11:10:37 -0700 | [diff] [blame] | 424 | return op.path.isInverseFillType() ? fCurrentClipBounds |
| 425 | : this->adjustAndMap(op.path.getBounds(), &op.paint); |
| 426 | } |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 427 | Bounds bounds(const DrawPoints& op) const { |
mtklein | 62b67ae | 2014-08-18 11:10:37 -0700 | [diff] [blame] | 428 | SkRect dst; |
| 429 | dst.set(op.pts, op.count); |
| 430 | |
| 431 | // Pad the bounding box a little to make sure hairline points' bounds aren't empty. |
| 432 | SkScalar stroke = SkMaxScalar(op.paint.getStrokeWidth(), 0.01f); |
| 433 | dst.outset(stroke/2, stroke/2); |
| 434 | |
| 435 | return this->adjustAndMap(dst, &op.paint); |
| 436 | } |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 437 | Bounds bounds(const DrawPatch& op) const { |
mtklein | 131a22b | 2014-08-25 14:16:15 -0700 | [diff] [blame] | 438 | SkRect dst; |
| 439 | dst.set(op.cubics, SkPatchUtils::kNumCtrlPts); |
| 440 | return this->adjustAndMap(dst, &op.paint); |
| 441 | } |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 442 | Bounds bounds(const DrawVertices& op) const { |
mtklein | 131a22b | 2014-08-25 14:16:15 -0700 | [diff] [blame] | 443 | SkRect dst; |
| 444 | dst.set(op.vertices, op.vertexCount); |
| 445 | return this->adjustAndMap(dst, &op.paint); |
| 446 | } |
| 447 | |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 448 | Bounds bounds(const DrawPicture& op) const { |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 449 | SkRect dst = op.picture->cullRect(); |
mtklein | 131a22b | 2014-08-25 14:16:15 -0700 | [diff] [blame] | 450 | if (op.matrix) { |
| 451 | op.matrix->mapRect(&dst); |
| 452 | } |
| 453 | return this->adjustAndMap(dst, op.paint); |
| 454 | } |
mtklein | 62b67ae | 2014-08-18 11:10:37 -0700 | [diff] [blame] | 455 | |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 456 | Bounds bounds(const DrawPosText& op) const { |
mtklein | 62b67ae | 2014-08-18 11:10:37 -0700 | [diff] [blame] | 457 | const int N = op.paint.countText(op.text, op.byteLength); |
| 458 | if (N == 0) { |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 459 | return Bounds::MakeEmpty(); |
mtklein | 62b67ae | 2014-08-18 11:10:37 -0700 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | SkRect dst; |
mtklein | 937c9c7 | 2014-09-02 15:19:48 -0700 | [diff] [blame] | 463 | dst.set(op.pos, N); |
mtklein | 62b67ae | 2014-08-18 11:10:37 -0700 | [diff] [blame] | 464 | AdjustTextForFontMetrics(&dst, op.paint); |
| 465 | return this->adjustAndMap(dst, &op.paint); |
| 466 | } |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 467 | Bounds bounds(const DrawPosTextH& op) const { |
mtklein | 62b67ae | 2014-08-18 11:10:37 -0700 | [diff] [blame] | 468 | const int N = op.paint.countText(op.text, op.byteLength); |
| 469 | if (N == 0) { |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 470 | return Bounds::MakeEmpty(); |
mtklein | 62b67ae | 2014-08-18 11:10:37 -0700 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | SkScalar left = op.xpos[0], right = op.xpos[0]; |
| 474 | for (int i = 1; i < N; i++) { |
| 475 | left = SkMinScalar(left, op.xpos[i]); |
| 476 | right = SkMaxScalar(right, op.xpos[i]); |
| 477 | } |
| 478 | SkRect dst = { left, op.y, right, op.y }; |
| 479 | AdjustTextForFontMetrics(&dst, op.paint); |
| 480 | return this->adjustAndMap(dst, &op.paint); |
| 481 | } |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 482 | Bounds bounds(const DrawTextOnPath& op) const { |
mtklein | 131a22b | 2014-08-25 14:16:15 -0700 | [diff] [blame] | 483 | SkRect dst = op.path.getBounds(); |
| 484 | |
mtklein | ed167ac | 2014-10-29 16:07:10 -0700 | [diff] [blame] | 485 | // Pad all sides by the maximum padding in any direction we'd normally apply. |
mtklein | 131a22b | 2014-08-25 14:16:15 -0700 | [diff] [blame] | 486 | SkRect pad = { 0, 0, 0, 0}; |
| 487 | AdjustTextForFontMetrics(&pad, op.paint); |
mtklein | ed167ac | 2014-10-29 16:07:10 -0700 | [diff] [blame] | 488 | |
| 489 | // That maximum padding happens to always be the right pad today. |
| 490 | SkASSERT(pad.fLeft == -pad.fRight); |
| 491 | SkASSERT(pad.fTop == -pad.fBottom); |
| 492 | SkASSERT(pad.fRight > pad.fBottom); |
| 493 | dst.outset(pad.fRight, pad.fRight); |
mtklein | 131a22b | 2014-08-25 14:16:15 -0700 | [diff] [blame] | 494 | |
| 495 | return this->adjustAndMap(dst, &op.paint); |
| 496 | } |
| 497 | |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 498 | Bounds bounds(const DrawTextBlob& op) const { |
mtklein | 131a22b | 2014-08-25 14:16:15 -0700 | [diff] [blame] | 499 | SkRect dst = op.blob->bounds(); |
| 500 | dst.offset(op.x, op.y); |
mtklein | 131a22b | 2014-08-25 14:16:15 -0700 | [diff] [blame] | 501 | return this->adjustAndMap(dst, &op.paint); |
| 502 | } |
mtklein | 62b67ae | 2014-08-18 11:10:37 -0700 | [diff] [blame] | 503 | |
| 504 | static void AdjustTextForFontMetrics(SkRect* rect, const SkPaint& paint) { |
mtklein | ed167ac | 2014-10-29 16:07:10 -0700 | [diff] [blame] | 505 | #ifdef SK_DEBUG |
| 506 | SkRect correct = *rect; |
| 507 | #endif |
| 508 | // crbug.com/373785 ~~> xPad = 4x yPad |
| 509 | // crbug.com/424824 ~~> bump yPad from 2x text size to 2.5x |
| 510 | const SkScalar yPad = 2.5f * paint.getTextSize(), |
| 511 | xPad = 4.0f * yPad; |
| 512 | rect->outset(xPad, yPad); |
caryclark | 9a657fa | 2014-08-20 05:24:29 -0700 | [diff] [blame] | 513 | #ifdef SK_DEBUG |
mtklein | 62b67ae | 2014-08-18 11:10:37 -0700 | [diff] [blame] | 514 | SkPaint::FontMetrics metrics; |
| 515 | paint.getFontMetrics(&metrics); |
mtklein | ed167ac | 2014-10-29 16:07:10 -0700 | [diff] [blame] | 516 | correct.fLeft += metrics.fXMin; |
| 517 | correct.fTop += metrics.fTop; |
| 518 | correct.fRight += metrics.fXMax; |
| 519 | correct.fBottom += metrics.fBottom; |
mtklein | d13291a | 2014-08-21 14:46:49 -0700 | [diff] [blame] | 520 | // See skia:2862 for why we ignore small text sizes. |
mtklein | ed167ac | 2014-10-29 16:07:10 -0700 | [diff] [blame] | 521 | SkASSERTF(paint.getTextSize() < 0.001f || rect->contains(correct), |
| 522 | "%f %f %f %f vs. %f %f %f %f\n", |
| 523 | -xPad, -yPad, +xPad, +yPad, |
| 524 | metrics.fXMin, metrics.fTop, metrics.fXMax, metrics.fBottom); |
mtklein | a19afb4 | 2014-08-19 17:47:14 -0700 | [diff] [blame] | 525 | #endif |
mtklein | 62b67ae | 2014-08-18 11:10:37 -0700 | [diff] [blame] | 526 | } |
| 527 | |
mtklein | 479601b | 2014-08-18 08:45:33 -0700 | [diff] [blame] | 528 | // Returns true if rect was meaningfully adjusted for the effects of paint, |
| 529 | // false if the paint could affect the rect in unknown ways. |
| 530 | static bool AdjustForPaint(const SkPaint* paint, SkRect* rect) { |
mtklein | a723b57 | 2014-08-15 11:49:49 -0700 | [diff] [blame] | 531 | if (paint) { |
| 532 | if (paint->canComputeFastBounds()) { |
mtklein | 479601b | 2014-08-18 08:45:33 -0700 | [diff] [blame] | 533 | *rect = paint->computeFastBounds(*rect, rect); |
| 534 | return true; |
mtklein | a723b57 | 2014-08-15 11:49:49 -0700 | [diff] [blame] | 535 | } |
mtklein | 479601b | 2014-08-18 08:45:33 -0700 | [diff] [blame] | 536 | return false; |
| 537 | } |
| 538 | return true; |
| 539 | } |
| 540 | |
mtklein | 8e393bf | 2014-10-01 12:48:58 -0700 | [diff] [blame] | 541 | bool adjustForSaveLayerPaints(SkRect* rect, int savesToIgnore = 0) const { |
| 542 | for (int i = fSaveStack.count() - 1 - savesToIgnore; i >= 0; i--) { |
Mike Klein | 271a030 | 2014-09-23 15:28:38 -0400 | [diff] [blame] | 543 | if (!AdjustForPaint(fSaveStack[i].paint, rect)) { |
| 544 | return false; |
| 545 | } |
| 546 | } |
| 547 | return true; |
| 548 | } |
| 549 | |
robertphillips | 4e8e342 | 2014-11-12 06:46:08 -0800 | [diff] [blame] | 550 | const unsigned fNumRecords; |
mtklein | a723b57 | 2014-08-15 11:49:49 -0700 | [diff] [blame] | 551 | |
robertphillips | 4d52afe | 2014-11-03 08:19:44 -0800 | [diff] [blame] | 552 | // We do not guarantee anything for operations outside of the cull rect |
| 553 | const SkRect fCullRect; |
| 554 | |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 555 | // Conservative identity-space bounds for each op in the SkRecord. |
| 556 | SkAutoTMalloc<Bounds> fBounds; |
mtklein | a723b57 | 2014-08-15 11:49:49 -0700 | [diff] [blame] | 557 | |
| 558 | // We walk fCurrentOp through the SkRecord, as we go using updateCTM() |
| 559 | // and updateClipBounds() to maintain the exact CTM (fCTM) and conservative |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 560 | // identity-space bounds of the current clip (fCurrentClipBounds). |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 561 | unsigned fCurrentOp; |
mtklein | 6332f1d | 2014-08-19 07:09:40 -0700 | [diff] [blame] | 562 | const SkMatrix* fCTM; |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 563 | Bounds fCurrentClipBounds; |
mtklein | a723b57 | 2014-08-15 11:49:49 -0700 | [diff] [blame] | 564 | |
| 565 | // 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] | 566 | SkTDArray<SaveBounds> fSaveStack; |
| 567 | SkTDArray<unsigned> fControlIndices; |
mtklein | 5ad6ee1 | 2014-08-11 08:08:43 -0700 | [diff] [blame] | 568 | }; |
| 569 | |
robertphillips | 4e8e342 | 2014-11-12 06:46:08 -0800 | [diff] [blame] | 570 | // SkRecord visitor to gather saveLayer/restore information. |
| 571 | class CollectLayers : SkNoncopyable { |
| 572 | public: |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame^] | 573 | CollectLayers(const SkRect& cullRect, const SkRecord& record, SkLayerInfo* accelData) |
robertphillips | 4e8e342 | 2014-11-12 06:46:08 -0800 | [diff] [blame] | 574 | : fSaveLayersInStack(0) |
| 575 | , fAccelData(accelData) |
| 576 | , fFillBounds(cullRect, record) { |
| 577 | } |
| 578 | |
| 579 | void setCurrentOp(unsigned currentOp) { fFillBounds.setCurrentOp(currentOp); } |
| 580 | |
| 581 | void cleanUp(SkBBoxHierarchy* bbh) { |
| 582 | // fFillBounds must perform its cleanUp first so that all the bounding |
| 583 | // boxes associated with unbalanced restores are updated (prior to |
| 584 | // fetching their bound in popSaveLayerInfo). |
| 585 | fFillBounds.cleanUp(bbh); |
| 586 | |
| 587 | while (!fSaveLayerStack.isEmpty()) { |
| 588 | this->popSaveLayerInfo(); |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | template <typename T> void operator()(const T& op) { |
| 593 | fFillBounds(op); |
| 594 | this->trackSaveLayers(op); |
| 595 | } |
| 596 | |
| 597 | private: |
| 598 | struct SaveLayerInfo { |
| 599 | SaveLayerInfo() { } |
robertphillips | 74576eb | 2014-11-12 07:25:02 -0800 | [diff] [blame] | 600 | SaveLayerInfo(int opIndex, bool isSaveLayer, const SkPaint* paint) |
robertphillips | 4e8e342 | 2014-11-12 06:46:08 -0800 | [diff] [blame] | 601 | : fStartIndex(opIndex) |
| 602 | , fIsSaveLayer(isSaveLayer) |
| 603 | , fHasNestedSaveLayer(false) |
robertphillips | 74576eb | 2014-11-12 07:25:02 -0800 | [diff] [blame] | 604 | , fPaint(paint) { |
robertphillips | 4e8e342 | 2014-11-12 06:46:08 -0800 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | int fStartIndex; |
| 608 | bool fIsSaveLayer; |
| 609 | bool fHasNestedSaveLayer; |
| 610 | const SkPaint* fPaint; |
robertphillips | 4e8e342 | 2014-11-12 06:46:08 -0800 | [diff] [blame] | 611 | }; |
| 612 | |
| 613 | template <typename T> void trackSaveLayers(const T& op) { |
| 614 | /* most ops aren't involved in saveLayers */ |
| 615 | } |
| 616 | void trackSaveLayers(const Save& s) { this->pushSaveLayerInfo(false, NULL); } |
| 617 | void trackSaveLayers(const SaveLayer& sl) { this->pushSaveLayerInfo(true, sl.paint); } |
| 618 | void trackSaveLayers(const Restore& r) { this->popSaveLayerInfo(); } |
| 619 | |
| 620 | void trackSaveLayers(const DrawPicture& dp) { |
| 621 | // For sub-pictures, we wrap their layer information within the parent |
| 622 | // picture's rendering hierarchy |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame^] | 623 | SkPicture::AccelData::Key key = SkLayerInfo::ComputeKey(); |
robertphillips | 4e8e342 | 2014-11-12 06:46:08 -0800 | [diff] [blame] | 624 | |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame^] | 625 | const SkLayerInfo* childData = |
| 626 | static_cast<const SkLayerInfo*>(dp.picture->EXPERIMENTAL_getAccelData(key)); |
robertphillips | 4e8e342 | 2014-11-12 06:46:08 -0800 | [diff] [blame] | 627 | if (!childData) { |
| 628 | // If the child layer hasn't been generated with saveLayer data we |
| 629 | // assume the worst (i.e., that it does contain layers which nest |
| 630 | // inside existing layers). Layers within sub-pictures that don't |
| 631 | // have saveLayer data cannot be hoisted. |
| 632 | // TODO: could the analysis data be use to fine tune this? |
| 633 | this->updateStackForSaveLayer(); |
| 634 | return; |
| 635 | } |
| 636 | |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame^] | 637 | for (int i = 0; i < childData->numBlocks(); ++i) { |
| 638 | const SkLayerInfo::BlockInfo& src = childData->block(i); |
robertphillips | 4e8e342 | 2014-11-12 06:46:08 -0800 | [diff] [blame] | 639 | |
robertphillips | 74576eb | 2014-11-12 07:25:02 -0800 | [diff] [blame] | 640 | FillBounds::Bounds newBound = fFillBounds.adjustAndMap(src.fBounds, dp.paint); |
| 641 | if (newBound.isEmpty()) { |
robertphillips | 4e8e342 | 2014-11-12 06:46:08 -0800 | [diff] [blame] | 642 | continue; |
| 643 | } |
| 644 | |
| 645 | this->updateStackForSaveLayer(); |
| 646 | |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame^] | 647 | SkLayerInfo::BlockInfo& dst = fAccelData->addBlock(); |
robertphillips | 4e8e342 | 2014-11-12 06:46:08 -0800 | [diff] [blame] | 648 | |
| 649 | // If src.fPicture is NULL the layer is in dp.picture; otherwise |
| 650 | // it belongs to a sub-picture. |
| 651 | dst.fPicture = src.fPicture ? src.fPicture : static_cast<const SkPicture*>(dp.picture); |
| 652 | dst.fPicture->ref(); |
robertphillips | 74576eb | 2014-11-12 07:25:02 -0800 | [diff] [blame] | 653 | dst.fBounds = newBound; |
robertphillips | 4e8e342 | 2014-11-12 06:46:08 -0800 | [diff] [blame] | 654 | dst.fLocalMat = src.fLocalMat; |
| 655 | dst.fPreMat = src.fPreMat; |
| 656 | dst.fPreMat.postConcat(fFillBounds.ctm()); |
| 657 | if (src.fPaint) { |
| 658 | dst.fPaint = SkNEW_ARGS(SkPaint, (*src.fPaint)); |
| 659 | } |
| 660 | dst.fSaveLayerOpID = src.fSaveLayerOpID; |
| 661 | dst.fRestoreOpID = src.fRestoreOpID; |
| 662 | dst.fHasNestedLayers = src.fHasNestedLayers; |
| 663 | dst.fIsNested = fSaveLayersInStack > 0 || src.fIsNested; |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | // Inform all the saveLayers already on the stack that they now have a |
| 668 | // nested saveLayer inside them |
| 669 | void updateStackForSaveLayer() { |
| 670 | for (int index = fSaveLayerStack.count() - 1; index >= 0; --index) { |
| 671 | if (fSaveLayerStack[index].fHasNestedSaveLayer) { |
| 672 | break; |
| 673 | } |
| 674 | fSaveLayerStack[index].fHasNestedSaveLayer = true; |
| 675 | if (fSaveLayerStack[index].fIsSaveLayer) { |
| 676 | break; |
| 677 | } |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | void pushSaveLayerInfo(bool isSaveLayer, const SkPaint* paint) { |
| 682 | if (isSaveLayer) { |
| 683 | this->updateStackForSaveLayer(); |
| 684 | ++fSaveLayersInStack; |
| 685 | } |
| 686 | |
robertphillips | 74576eb | 2014-11-12 07:25:02 -0800 | [diff] [blame] | 687 | fSaveLayerStack.push(SaveLayerInfo(fFillBounds.currentOp(), isSaveLayer, paint)); |
robertphillips | 4e8e342 | 2014-11-12 06:46:08 -0800 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | void popSaveLayerInfo() { |
| 691 | if (fSaveLayerStack.count() <= 0) { |
| 692 | SkASSERT(false); |
| 693 | return; |
| 694 | } |
| 695 | |
| 696 | SaveLayerInfo sli; |
| 697 | fSaveLayerStack.pop(&sli); |
| 698 | |
| 699 | if (!sli.fIsSaveLayer) { |
| 700 | return; |
| 701 | } |
| 702 | |
| 703 | --fSaveLayersInStack; |
| 704 | |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame^] | 705 | SkLayerInfo::BlockInfo& block = fAccelData->addBlock(); |
robertphillips | 4e8e342 | 2014-11-12 06:46:08 -0800 | [diff] [blame] | 706 | |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame^] | 707 | SkASSERT(NULL == block.fPicture); // This layer is in the top-most picture |
robertphillips | 4e8e342 | 2014-11-12 06:46:08 -0800 | [diff] [blame] | 708 | |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame^] | 709 | block.fBounds = fFillBounds.getBounds(sli.fStartIndex); |
| 710 | block.fLocalMat = fFillBounds.ctm(); |
| 711 | block.fPreMat = SkMatrix::I(); |
robertphillips | 4e8e342 | 2014-11-12 06:46:08 -0800 | [diff] [blame] | 712 | if (sli.fPaint) { |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame^] | 713 | block.fPaint = SkNEW_ARGS(SkPaint, (*sli.fPaint)); |
robertphillips | 4e8e342 | 2014-11-12 06:46:08 -0800 | [diff] [blame] | 714 | } |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame^] | 715 | block.fSaveLayerOpID = sli.fStartIndex; |
| 716 | block.fRestoreOpID = fFillBounds.currentOp(); |
| 717 | block.fHasNestedLayers = sli.fHasNestedSaveLayer; |
| 718 | block.fIsNested = fSaveLayersInStack > 0; |
robertphillips | 4e8e342 | 2014-11-12 06:46:08 -0800 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | // Used to collect saveLayer information for layer hoisting |
| 722 | int fSaveLayersInStack; |
| 723 | SkTDArray<SaveLayerInfo> fSaveLayerStack; |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame^] | 724 | SkLayerInfo* fAccelData; |
robertphillips | 4e8e342 | 2014-11-12 06:46:08 -0800 | [diff] [blame] | 725 | |
| 726 | SkRecords::FillBounds fFillBounds; |
| 727 | }; |
robertphillips | 4e8e342 | 2014-11-12 06:46:08 -0800 | [diff] [blame] | 728 | |
commit-bot@chromium.org | 27f6b0d | 2014-05-09 14:59:29 +0000 | [diff] [blame] | 729 | } // namespace SkRecords |
mtklein | 5ad6ee1 | 2014-08-11 08:08:43 -0700 | [diff] [blame] | 730 | |
robertphillips | 4d52afe | 2014-11-03 08:19:44 -0800 | [diff] [blame] | 731 | void SkRecordFillBounds(const SkRect& cullRect, const SkRecord& record, SkBBoxHierarchy* bbh) { |
robertphillips | 4e8e342 | 2014-11-12 06:46:08 -0800 | [diff] [blame] | 732 | SkRecords::FillBounds visitor(cullRect, record); |
| 733 | |
| 734 | for (unsigned curOp = 0; curOp < record.count(); curOp++) { |
| 735 | visitor.setCurrentOp(curOp); |
| 736 | record.visit<void>(curOp, visitor); |
| 737 | } |
| 738 | |
| 739 | visitor.cleanUp(bbh); |
mtklein | 5ad6ee1 | 2014-08-11 08:08:43 -0700 | [diff] [blame] | 740 | } |
robertphillips | 4e8e342 | 2014-11-12 06:46:08 -0800 | [diff] [blame] | 741 | |
robertphillips | 4e8e342 | 2014-11-12 06:46:08 -0800 | [diff] [blame] | 742 | void SkRecordComputeLayers(const SkRect& cullRect, const SkRecord& record, |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame^] | 743 | SkBBoxHierarchy* bbh, SkLayerInfo* data) { |
robertphillips | 4e8e342 | 2014-11-12 06:46:08 -0800 | [diff] [blame] | 744 | SkRecords::CollectLayers visitor(cullRect, record, data); |
| 745 | |
| 746 | for (unsigned curOp = 0; curOp < record.count(); curOp++) { |
| 747 | visitor.setCurrentOp(curOp); |
| 748 | record.visit<void>(curOp, visitor); |
| 749 | } |
| 750 | |
| 751 | visitor.cleanUp(bbh); |
| 752 | } |
robertphillips | 4e8e342 | 2014-11-12 06:46:08 -0800 | [diff] [blame] | 753 | |