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