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