commit-bot@chromium.org | c4b21e6 | 2014-04-11 18:33:31 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 8 | #include "SkRecordDraw.h" |
mtklein | 131a22b | 2014-08-25 14:16:15 -0700 | [diff] [blame] | 9 | #include "SkPatchUtils.h" |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 10 | |
mtklein | 5ad6ee1 | 2014-08-11 08:08:43 -0700 | [diff] [blame] | 11 | void SkRecordDraw(const SkRecord& record, |
| 12 | SkCanvas* canvas, |
reed | 1bdfd3f | 2014-11-24 14:41:51 -0800 | [diff] [blame] | 13 | SkPicture const* const drawablePicts[], |
reed | 3cb3840 | 2015-02-06 08:36:15 -0800 | [diff] [blame] | 14 | SkDrawable* const drawables[], |
reed | 1bdfd3f | 2014-11-24 14:41:51 -0800 | [diff] [blame] | 15 | int drawableCount, |
mtklein | 5ad6ee1 | 2014-08-11 08:08:43 -0700 | [diff] [blame] | 16 | const SkBBoxHierarchy* bbh, |
robertphillips | 783fe16 | 2015-01-07 07:28:41 -0800 | [diff] [blame] | 17 | SkPicture::AbortCallback* callback) { |
Mike Klein | c11530e | 2014-06-24 11:29:06 -0400 | [diff] [blame] | 18 | SkAutoCanvasRestore saveRestore(canvas, true /*save now, restore at exit*/); |
mtklein | 5ad6ee1 | 2014-08-11 08:08:43 -0700 | [diff] [blame] | 19 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 20 | if (bbh) { |
mtklein | 5ad6ee1 | 2014-08-11 08:08:43 -0700 | [diff] [blame] | 21 | // Draw only ops that affect pixels in the canvas's current clip. |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 22 | // The SkRecord and BBH were recorded in identity space. This canvas |
| 23 | // is not necessarily in that same space. getClipBounds() returns us |
| 24 | // this canvas' clip bounds transformed back into identity space, which |
| 25 | // lets us query the BBH. |
mtklein | 7cc1a34 | 2014-11-20 08:01:09 -0800 | [diff] [blame] | 26 | SkRect query; |
| 27 | if (!canvas->getClipBounds(&query)) { |
mtklein | 49aabde | 2015-01-05 07:02:45 -0800 | [diff] [blame] | 28 | query.setEmpty(); |
mtklein | 7cc1a34 | 2014-11-20 08:01:09 -0800 | [diff] [blame] | 29 | } |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 30 | |
mtklein | c6ad06a | 2015-08-19 09:51:00 -0700 | [diff] [blame] | 31 | SkTDArray<int> ops; |
mtklein | a723b57 | 2014-08-15 11:49:49 -0700 | [diff] [blame] | 32 | bbh->search(query, &ops); |
mtklein | 5ad6ee1 | 2014-08-11 08:08:43 -0700 | [diff] [blame] | 33 | |
reed | 1bdfd3f | 2014-11-24 14:41:51 -0800 | [diff] [blame] | 34 | SkRecords::Draw draw(canvas, drawablePicts, drawables, drawableCount); |
mtklein | 5ad6ee1 | 2014-08-11 08:08:43 -0700 | [diff] [blame] | 35 | for (int i = 0; i < ops.count(); i++) { |
robertphillips | 783fe16 | 2015-01-07 07:28:41 -0800 | [diff] [blame] | 36 | if (callback && callback->abort()) { |
mtklein | 5ad6ee1 | 2014-08-11 08:08:43 -0700 | [diff] [blame] | 37 | return; |
| 38 | } |
danakj | d239d42 | 2014-11-03 12:43:30 -0800 | [diff] [blame] | 39 | // This visit call uses the SkRecords::Draw::operator() to call |
| 40 | // methods on the |canvas|, wrapped by methods defined with the |
| 41 | // DRAW() macro. |
mtklein | 343a63d | 2016-03-22 11:46:53 -0700 | [diff] [blame] | 42 | record.visit(ops[i], draw); |
mtklein | 5ad6ee1 | 2014-08-11 08:08:43 -0700 | [diff] [blame] | 43 | } |
| 44 | } else { |
| 45 | // Draw all ops. |
reed | 1bdfd3f | 2014-11-24 14:41:51 -0800 | [diff] [blame] | 46 | SkRecords::Draw draw(canvas, drawablePicts, drawables, drawableCount); |
mtklein | c6ad06a | 2015-08-19 09:51:00 -0700 | [diff] [blame] | 47 | for (int i = 0; i < record.count(); i++) { |
robertphillips | 783fe16 | 2015-01-07 07:28:41 -0800 | [diff] [blame] | 48 | if (callback && callback->abort()) { |
mtklein | 5ad6ee1 | 2014-08-11 08:08:43 -0700 | [diff] [blame] | 49 | return; |
| 50 | } |
danakj | d239d42 | 2014-11-03 12:43:30 -0800 | [diff] [blame] | 51 | // This visit call uses the SkRecords::Draw::operator() to call |
| 52 | // methods on the |canvas|, wrapped by methods defined with the |
| 53 | // DRAW() macro. |
mtklein | 343a63d | 2016-03-22 11:46:53 -0700 | [diff] [blame] | 54 | record.visit(i, draw); |
mtklein | 5ad6ee1 | 2014-08-11 08:08:43 -0700 | [diff] [blame] | 55 | } |
commit-bot@chromium.org | 73b55eb | 2014-04-14 20:35:12 +0000 | [diff] [blame] | 56 | } |
commit-bot@chromium.org | 27f6b0d | 2014-05-09 14:59:29 +0000 | [diff] [blame] | 57 | } |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 58 | |
reed | 6be2aa9 | 2014-11-18 11:08:05 -0800 | [diff] [blame] | 59 | void SkRecordPartialDraw(const SkRecord& record, SkCanvas* canvas, |
| 60 | SkPicture const* const drawablePicts[], int drawableCount, |
mtklein | c6ad06a | 2015-08-19 09:51:00 -0700 | [diff] [blame] | 61 | int start, int stop, |
robertphillips | 4815fe5 | 2014-09-16 10:32:43 -0700 | [diff] [blame] | 62 | const SkMatrix& initialCTM) { |
mtklein | 00f30bd | 2014-09-02 12:03:31 -0700 | [diff] [blame] | 63 | SkAutoCanvasRestore saveRestore(canvas, true /*save now, restore at exit*/); |
| 64 | |
| 65 | stop = SkTMin(stop, record.count()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 66 | SkRecords::Draw draw(canvas, drawablePicts, nullptr, drawableCount, &initialCTM); |
mtklein | c6ad06a | 2015-08-19 09:51:00 -0700 | [diff] [blame] | 67 | for (int i = start; i < stop; i++) { |
mtklein | 343a63d | 2016-03-22 11:46:53 -0700 | [diff] [blame] | 68 | record.visit(i, draw); |
mtklein | 00f30bd | 2014-09-02 12:03:31 -0700 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
commit-bot@chromium.org | 27f6b0d | 2014-05-09 14:59:29 +0000 | [diff] [blame] | 72 | namespace SkRecords { |
commit-bot@chromium.org | 73b55eb | 2014-04-14 20:35:12 +0000 | [diff] [blame] | 73 | |
commit-bot@chromium.org | 2e0c32a | 2014-04-28 16:19:45 +0000 | [diff] [blame] | 74 | // NoOps draw nothing. |
commit-bot@chromium.org | 27f6b0d | 2014-05-09 14:59:29 +0000 | [diff] [blame] | 75 | template <> void Draw::draw(const NoOp&) {} |
commit-bot@chromium.org | 73b55eb | 2014-04-14 20:35:12 +0000 | [diff] [blame] | 76 | |
commit-bot@chromium.org | 27f6b0d | 2014-05-09 14:59:29 +0000 | [diff] [blame] | 77 | #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] | 78 | DRAW(Restore, restore()); |
Florin Malita | 5f6102d | 2014-06-30 10:13:28 -0400 | [diff] [blame] | 79 | DRAW(Save, save()); |
mtklein | da574d1 | 2016-08-01 11:24:03 -0700 | [diff] [blame] | 80 | DRAW(SaveLayer, saveLayer(SkCanvas::SaveLayerRec(r.bounds, |
| 81 | r.paint, |
| 82 | r.backdrop.get(), |
| 83 | r.saveLayerFlags))); |
commit-bot@chromium.org | 99bd7d8 | 2014-05-19 15:51:12 +0000 | [diff] [blame] | 84 | DRAW(SetMatrix, setMatrix(SkMatrix::Concat(fInitialCTM, r.matrix))); |
mtklein | e9d2052 | 2015-11-19 12:08:24 -0800 | [diff] [blame] | 85 | DRAW(Concat, concat(r.matrix)); |
mtklein | cbdf007 | 2016-08-19 09:05:27 -0700 | [diff] [blame] | 86 | DRAW(Translate, translate(r.dx, r.dy)); |
commit-bot@chromium.org | 73b55eb | 2014-04-14 20:35:12 +0000 | [diff] [blame] | 87 | |
mtklein | cdeeb09 | 2014-11-20 09:14:28 -0800 | [diff] [blame] | 88 | DRAW(ClipPath, clipPath(r.path, r.opAA.op, r.opAA.aa)); |
| 89 | DRAW(ClipRRect, clipRRect(r.rrect, r.opAA.op, r.opAA.aa)); |
| 90 | 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] | 91 | DRAW(ClipRegion, clipRegion(r.region, r.op)); |
| 92 | |
vjiaoblack | 95302da | 2016-07-21 10:25:54 -0700 | [diff] [blame] | 93 | #ifdef SK_EXPERIMENTAL_SHADOWING |
vjiaoblack | e5de130 | 2016-07-13 14:05:28 -0700 | [diff] [blame] | 94 | DRAW(TranslateZ, SkCanvas::translateZ(r.z)); |
vjiaoblack | 95302da | 2016-07-21 10:25:54 -0700 | [diff] [blame] | 95 | #else |
| 96 | template <> void Draw::draw(const TranslateZ& r) { } |
| 97 | #endif |
vjiaoblack | e5de130 | 2016-07-13 14:05:28 -0700 | [diff] [blame] | 98 | |
bsalomon | ac3aa24 | 2016-08-19 11:25:19 -0700 | [diff] [blame] | 99 | DRAW(DrawArc, drawArc(r.oval, r.startAngle, r.sweepAngle, r.useCenter, r.paint)); |
commit-bot@chromium.org | 73b55eb | 2014-04-14 20:35:12 +0000 | [diff] [blame] | 100 | DRAW(DrawDRRect, drawDRRect(r.outer, r.inner, r.paint)); |
mtklein | da574d1 | 2016-08-01 11:24:03 -0700 | [diff] [blame] | 101 | DRAW(DrawImage, drawImage(r.image.get(), r.left, r.top, r.paint)); |
msarett | c573a40 | 2016-08-02 08:05:56 -0700 | [diff] [blame] | 102 | |
| 103 | template <> void Draw::draw(const DrawImageLattice& r) { |
| 104 | SkCanvas::Lattice lattice; |
| 105 | lattice.fXCount = r.xCount; |
| 106 | lattice.fXDivs = r.xDivs; |
| 107 | lattice.fYCount = r.yCount; |
| 108 | lattice.fYDivs = r.yDivs; |
msarett | 0764efe | 2016-09-02 11:24:30 -0700 | [diff] [blame] | 109 | lattice.fFlags = (0 == r.flagCount) ? nullptr : r.flags; |
msarett | 71df2d7 | 2016-09-30 12:41:42 -0700 | [diff] [blame] | 110 | lattice.fBounds = &r.src; |
msarett | c573a40 | 2016-08-02 08:05:56 -0700 | [diff] [blame] | 111 | fCanvas->drawImageLattice(r.image.get(), lattice, r.dst, r.paint); |
| 112 | } |
| 113 | |
mtklein | da574d1 | 2016-08-01 11:24:03 -0700 | [diff] [blame] | 114 | DRAW(DrawImageRect, legacy_drawImageRect(r.image.get(), r.src, r.dst, r.paint, r.constraint)); |
| 115 | DRAW(DrawImageNine, drawImageNine(r.image.get(), r.center, 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)); |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 119 | DRAW(DrawPatch, drawPatch(r.cubics, r.colors, r.texCoords, r.bmode, r.paint)); |
mtklein | da574d1 | 2016-08-01 11:24:03 -0700 | [diff] [blame] | 120 | DRAW(DrawPicture, drawPicture(r.picture.get(), &r.matrix, r.paint)); |
vjiaoblack | 95302da | 2016-07-21 10:25:54 -0700 | [diff] [blame] | 121 | |
| 122 | #ifdef SK_EXPERIMENTAL_SHADOWING |
vjiaoblack | e6f5d56 | 2016-08-25 06:30:23 -0700 | [diff] [blame] | 123 | DRAW(DrawShadowedPicture, drawShadowedPicture(r.picture.get(), &r.matrix, r.paint, r.params)); |
vjiaoblack | 95302da | 2016-07-21 10:25:54 -0700 | [diff] [blame] | 124 | #else |
| 125 | template <> void Draw::draw(const DrawShadowedPicture& r) { } |
| 126 | #endif |
| 127 | |
commit-bot@chromium.org | 73b55eb | 2014-04-14 20:35:12 +0000 | [diff] [blame] | 128 | DRAW(DrawPoints, drawPoints(r.mode, r.count, r.pts, r.paint)); |
| 129 | DRAW(DrawPosText, drawPosText(r.text, r.byteLength, r.pos, r.paint)); |
| 130 | DRAW(DrawPosTextH, drawPosTextH(r.text, r.byteLength, r.xpos, r.y, r.paint)); |
| 131 | DRAW(DrawRRect, drawRRect(r.rrect, r.paint)); |
| 132 | DRAW(DrawRect, drawRect(r.rect, r.paint)); |
msarett | 44df651 | 2016-08-25 13:54:30 -0700 | [diff] [blame] | 133 | DRAW(DrawRegion, drawRegion(r.region, r.paint)); |
commit-bot@chromium.org | 73b55eb | 2014-04-14 20:35:12 +0000 | [diff] [blame] | 134 | DRAW(DrawText, drawText(r.text, r.byteLength, r.x, r.y, r.paint)); |
mtklein | da574d1 | 2016-08-01 11:24:03 -0700 | [diff] [blame] | 135 | DRAW(DrawTextBlob, drawTextBlob(r.blob.get(), r.x, r.y, r.paint)); |
mtklein | af57903 | 2014-12-01 11:03:37 -0800 | [diff] [blame] | 136 | DRAW(DrawTextOnPath, drawTextOnPath(r.text, r.byteLength, r.path, &r.matrix, r.paint)); |
reed | 45561a0 | 2016-07-07 12:47:17 -0700 | [diff] [blame] | 137 | DRAW(DrawTextRSXform, drawTextRSXform(r.text, r.byteLength, r.xforms, r.cull, r.paint)); |
mtklein | da574d1 | 2016-08-01 11:24:03 -0700 | [diff] [blame] | 138 | DRAW(DrawAtlas, drawAtlas(r.atlas.get(), |
| 139 | r.xforms, r.texs, r.colors, r.count, r.mode, r.cull, r.paint)); |
commit-bot@chromium.org | 73b55eb | 2014-04-14 20:35:12 +0000 | [diff] [blame] | 140 | DRAW(DrawVertices, drawVertices(r.vmode, r.vertexCount, r.vertices, r.texs, r.colors, |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 141 | r.bmode, r.indices, r.indexCount, r.paint)); |
mtklein | da574d1 | 2016-08-01 11:24:03 -0700 | [diff] [blame] | 142 | DRAW(DrawAnnotation, drawAnnotation(r.rect, r.key.c_str(), r.value.get())); |
commit-bot@chromium.org | 73b55eb | 2014-04-14 20:35:12 +0000 | [diff] [blame] | 143 | #undef DRAW |
| 144 | |
reed | 6be2aa9 | 2014-11-18 11:08:05 -0800 | [diff] [blame] | 145 | template <> void Draw::draw(const DrawDrawable& r) { |
| 146 | SkASSERT(r.index >= 0); |
| 147 | SkASSERT(r.index < fDrawableCount); |
reed | 1bdfd3f | 2014-11-24 14:41:51 -0800 | [diff] [blame] | 148 | if (fDrawables) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 149 | SkASSERT(nullptr == fDrawablePicts); |
reed | a8db728 | 2015-07-07 10:22:31 -0700 | [diff] [blame] | 150 | fCanvas->drawDrawable(fDrawables[r.index], r.matrix); |
reed | 1bdfd3f | 2014-11-24 14:41:51 -0800 | [diff] [blame] | 151 | } else { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 152 | fCanvas->drawPicture(fDrawablePicts[r.index], r.matrix, nullptr); |
reed | 1bdfd3f | 2014-11-24 14:41:51 -0800 | [diff] [blame] | 153 | } |
reed | 6be2aa9 | 2014-11-18 11:08:05 -0800 | [diff] [blame] | 154 | } |
| 155 | |
mtklein | 5ad6ee1 | 2014-08-11 08:08:43 -0700 | [diff] [blame] | 156 | // This is an SkRecord visitor that fills an SkBBoxHierarchy. |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 157 | // |
| 158 | // The interesting part here is how to calculate bounds for ops which don't |
| 159 | // have intrinsic bounds. What is the bounds of a Save or a Translate? |
| 160 | // |
| 161 | // We answer this by thinking about a particular definition of bounds: if I |
| 162 | // don't execute this op, pixels in this rectangle might draw incorrectly. So |
| 163 | // the bounds of a Save, a Translate, a Restore, etc. are the union of the |
| 164 | // bounds of Draw* ops that they might have an effect on. For any given |
| 165 | // Save/Restore block, the bounds of the Save, the Restore, and any other |
| 166 | // non-drawing ("control") ops inside are exactly the union of the bounds of |
| 167 | // the drawing ops inside that block. |
| 168 | // |
| 169 | // To implement this, we keep a stack of active Save blocks. As we consume ops |
| 170 | // inside the Save/Restore block, drawing ops are unioned with the bounds of |
| 171 | // the block, and control ops are stashed away for later. When we finish the |
| 172 | // block with a Restore, our bounds are complete, and we go back and fill them |
| 173 | // in for all the control ops we stashed away. |
mtklein | 5ad6ee1 | 2014-08-11 08:08:43 -0700 | [diff] [blame] | 174 | class FillBounds : SkNoncopyable { |
| 175 | public: |
mtklein | 40732b3 | 2015-10-24 07:45:47 -0700 | [diff] [blame] | 176 | FillBounds(const SkRect& cullRect, const SkRecord& record, SkRect bounds[]) |
robertphillips | 4e8e342 | 2014-11-12 06:46:08 -0800 | [diff] [blame] | 177 | : fNumRecords(record.count()) |
| 178 | , fCullRect(cullRect) |
mtklein | 40732b3 | 2015-10-24 07:45:47 -0700 | [diff] [blame] | 179 | , fBounds(bounds) { |
mtklein | e9d2052 | 2015-11-19 12:08:24 -0800 | [diff] [blame] | 180 | fCTM = SkMatrix::I(); |
robertphillips | 4d52afe | 2014-11-03 08:19:44 -0800 | [diff] [blame] | 181 | fCurrentClipBounds = fCullRect; |
robertphillips | 4e8e342 | 2014-11-12 06:46:08 -0800 | [diff] [blame] | 182 | } |
mtklein | 5ad6ee1 | 2014-08-11 08:08:43 -0700 | [diff] [blame] | 183 | |
mtklein | 40732b3 | 2015-10-24 07:45:47 -0700 | [diff] [blame] | 184 | void cleanUp() { |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 185 | // If we have any lingering unpaired Saves, simulate restores to make |
| 186 | // sure all ops in those Save blocks have their bounds calculated. |
| 187 | while (!fSaveStack.isEmpty()) { |
| 188 | this->popSaveBlock(); |
| 189 | } |
| 190 | |
| 191 | // Any control ops not part of any Save/Restore block draw everywhere. |
| 192 | while (!fControlIndices.isEmpty()) { |
robertphillips | 4d52afe | 2014-11-03 08:19:44 -0800 | [diff] [blame] | 193 | this->popControl(fCullRect); |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 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 | 40732b3 | 2015-10-24 07:45:47 -0700 | [diff] [blame] | 197 | void setCurrentOp(int currentOp) { fCurrentOp = currentOp; } |
| 198 | |
| 199 | |
mtklein | a723b57 | 2014-08-15 11:49:49 -0700 | [diff] [blame] | 200 | template <typename T> void operator()(const T& op) { |
| 201 | this->updateCTM(op); |
| 202 | this->updateClipBounds(op); |
| 203 | this->trackBounds(op); |
mtklein | 5ad6ee1 | 2014-08-11 08:08:43 -0700 | [diff] [blame] | 204 | } |
| 205 | |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 206 | // In this file, SkRect are in local coordinates, Bounds are translated back to identity space. |
| 207 | typedef SkRect Bounds; |
| 208 | |
mtklein | c6ad06a | 2015-08-19 09:51:00 -0700 | [diff] [blame] | 209 | int currentOp() const { return fCurrentOp; } |
mtklein | e9d2052 | 2015-11-19 12:08:24 -0800 | [diff] [blame] | 210 | const SkMatrix& ctm() const { return fCTM; } |
mtklein | c6ad06a | 2015-08-19 09:51:00 -0700 | [diff] [blame] | 211 | const Bounds& getBounds(int index) const { return fBounds[index]; } |
robertphillips | 4e8e342 | 2014-11-12 06:46:08 -0800 | [diff] [blame] | 212 | |
| 213 | // Adjust rect for all paints that may affect its geometry, then map it to identity space. |
| 214 | Bounds adjustAndMap(SkRect rect, const SkPaint* paint) const { |
| 215 | // Inverted rectangles really confuse our BBHs. |
| 216 | rect.sort(); |
| 217 | |
| 218 | // Adjust the rect for its own paint. |
| 219 | if (!AdjustForPaint(paint, &rect)) { |
| 220 | // The paint could do anything to our bounds. The only safe answer is the current clip. |
| 221 | return fCurrentClipBounds; |
| 222 | } |
| 223 | |
| 224 | // Adjust rect for all the paints from the SaveLayers we're inside. |
| 225 | if (!this->adjustForSaveLayerPaints(&rect)) { |
| 226 | // Same deal as above. |
| 227 | return fCurrentClipBounds; |
| 228 | } |
| 229 | |
| 230 | // Map the rect back to identity space. |
mtklein | e9d2052 | 2015-11-19 12:08:24 -0800 | [diff] [blame] | 231 | fCTM.mapRect(&rect); |
robertphillips | 4e8e342 | 2014-11-12 06:46:08 -0800 | [diff] [blame] | 232 | |
| 233 | // Nothing can draw outside the current clip. |
robertphillips | c187a3c | 2014-12-30 13:53:51 -0800 | [diff] [blame] | 234 | if (!rect.intersect(fCurrentClipBounds)) { |
| 235 | return Bounds::MakeEmpty(); |
| 236 | } |
| 237 | |
robertphillips | 4e8e342 | 2014-11-12 06:46:08 -0800 | [diff] [blame] | 238 | return rect; |
| 239 | } |
| 240 | |
| 241 | private: |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 242 | struct SaveBounds { |
mtklein | a723b57 | 2014-08-15 11:49:49 -0700 | [diff] [blame] | 243 | int controlOps; // Number of control ops in this Save block, including the Save. |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 244 | Bounds bounds; // Bounds of everything in the block. |
mtklein | a723b57 | 2014-08-15 11:49:49 -0700 | [diff] [blame] | 245 | const SkPaint* paint; // Unowned. If set, adjusts the bounds of all ops in this block. |
senorblanco | db64af3 | 2015-12-09 10:11:43 -0800 | [diff] [blame] | 246 | SkMatrix ctm; |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 247 | }; |
| 248 | |
mtklein | cbdf007 | 2016-08-19 09:05:27 -0700 | [diff] [blame] | 249 | // Only Restore, SetMatrix, Concat, and Translate change the CTM. |
mtklein | 8e393bf | 2014-10-01 12:48:58 -0700 | [diff] [blame] | 250 | template <typename T> void updateCTM(const T&) {} |
mtklein | e9d2052 | 2015-11-19 12:08:24 -0800 | [diff] [blame] | 251 | void updateCTM(const Restore& op) { fCTM = op.matrix; } |
| 252 | void updateCTM(const SetMatrix& op) { fCTM = op.matrix; } |
| 253 | void updateCTM(const Concat& op) { fCTM.preConcat(op.matrix); } |
mtklein | cbdf007 | 2016-08-19 09:05:27 -0700 | [diff] [blame] | 254 | void updateCTM(const Translate& op) { fCTM.preTranslate(op.dx, op.dy); } |
mtklein | a723b57 | 2014-08-15 11:49:49 -0700 | [diff] [blame] | 255 | |
mtklein | 8e393bf | 2014-10-01 12:48:58 -0700 | [diff] [blame] | 256 | // Most ops don't change the clip. |
| 257 | template <typename T> void updateClipBounds(const T&) {} |
Mike Klein | 271a030 | 2014-09-23 15:28:38 -0400 | [diff] [blame] | 258 | |
mtklein | 8e393bf | 2014-10-01 12:48:58 -0700 | [diff] [blame] | 259 | // Clip{Path,RRect,Rect,Region} obviously change the clip. They all know their bounds already. |
| 260 | void updateClipBounds(const ClipPath& op) { this->updateClipBoundsForClipOp(op.devBounds); } |
| 261 | void updateClipBounds(const ClipRRect& op) { this->updateClipBoundsForClipOp(op.devBounds); } |
| 262 | void updateClipBounds(const ClipRect& op) { this->updateClipBoundsForClipOp(op.devBounds); } |
| 263 | void updateClipBounds(const ClipRegion& op) { this->updateClipBoundsForClipOp(op.devBounds); } |
Mike Klein | 271a030 | 2014-09-23 15:28:38 -0400 | [diff] [blame] | 264 | |
mtklein | 8e393bf | 2014-10-01 12:48:58 -0700 | [diff] [blame] | 265 | // The bounds of clip ops need to be adjusted for the paints of saveLayers they're inside. |
| 266 | void updateClipBoundsForClipOp(const SkIRect& devBounds) { |
| 267 | Bounds clip = SkRect::Make(devBounds); |
Mike Klein | 271a030 | 2014-09-23 15:28:38 -0400 | [diff] [blame] | 268 | // We don't call adjustAndMap() because as its last step it would intersect the adjusted |
| 269 | // clip bounds with the previous clip, exactly what we can't do when the clip grows. |
schenney | 23d8593 | 2015-03-06 16:20:28 -0800 | [diff] [blame] | 270 | if (this->adjustForSaveLayerPaints(&clip)) { |
| 271 | fCurrentClipBounds = clip.intersect(fCullRect) ? clip : Bounds::MakeEmpty(); |
| 272 | } else { |
| 273 | fCurrentClipBounds = fCullRect; |
| 274 | } |
Mike Klein | 271a030 | 2014-09-23 15:28:38 -0400 | [diff] [blame] | 275 | } |
| 276 | |
mtklein | 8e393bf | 2014-10-01 12:48:58 -0700 | [diff] [blame] | 277 | // Restore holds the devBounds for the clip after the {save,saveLayer}/restore block completes. |
| 278 | void updateClipBounds(const Restore& op) { |
| 279 | // This is just like the clip ops above, but we need to skip the effects (if any) of our |
| 280 | // paired saveLayer (if it is one); it has not yet been popped off the save stack. Our |
| 281 | // devBounds reflect the state of the world after the saveLayer/restore block is done, |
| 282 | // so they are not affected by the saveLayer's paint. |
| 283 | const int kSavesToIgnore = 1; |
| 284 | Bounds clip = SkRect::Make(op.devBounds); |
schenney | 23d8593 | 2015-03-06 16:20:28 -0800 | [diff] [blame] | 285 | if (this->adjustForSaveLayerPaints(&clip, kSavesToIgnore)) { |
| 286 | fCurrentClipBounds = clip.intersect(fCullRect) ? clip : Bounds::MakeEmpty(); |
| 287 | } else { |
| 288 | fCurrentClipBounds = fCullRect; |
| 289 | } |
mtklein | 8e393bf | 2014-10-01 12:48:58 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Mike Klein | 271a030 | 2014-09-23 15:28:38 -0400 | [diff] [blame] | 292 | // 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] | 293 | void updateClipBounds(const SaveLayer& op) { |
| 294 | if (op.bounds) { |
Mike Klein | 271a030 | 2014-09-23 15:28:38 -0400 | [diff] [blame] | 295 | // adjustAndMap() intersects these layer bounds with the previous clip for us. |
| 296 | fCurrentClipBounds = this->adjustAndMap(*op.bounds, op.paint); |
mtklein | a723b57 | 2014-08-15 11:49:49 -0700 | [diff] [blame] | 297 | } |
| 298 | } |
mtklein | 6cfa73a | 2014-08-13 13:33:49 -0700 | [diff] [blame] | 299 | |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 300 | // The bounds of these ops must be calculated when we hit the Restore |
| 301 | // from the bounds of the ops in the same Save block. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 302 | void trackBounds(const Save&) { this->pushSaveBlock(nullptr); } |
mtklein | a723b57 | 2014-08-15 11:49:49 -0700 | [diff] [blame] | 303 | void trackBounds(const SaveLayer& op) { this->pushSaveBlock(op.paint); } |
| 304 | void trackBounds(const Restore&) { fBounds[fCurrentOp] = this->popSaveBlock(); } |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 305 | |
mtklein | 68199a2 | 2014-08-25 13:49:29 -0700 | [diff] [blame] | 306 | void trackBounds(const SetMatrix&) { this->pushControl(); } |
mtklein | e9d2052 | 2015-11-19 12:08:24 -0800 | [diff] [blame] | 307 | void trackBounds(const Concat&) { this->pushControl(); } |
mtklein | cbdf007 | 2016-08-19 09:05:27 -0700 | [diff] [blame] | 308 | void trackBounds(const Translate&) { this->pushControl(); } |
| 309 | void trackBounds(const TranslateZ&) { this->pushControl(); } |
mtklein | 68199a2 | 2014-08-25 13:49:29 -0700 | [diff] [blame] | 310 | void trackBounds(const ClipRect&) { this->pushControl(); } |
| 311 | void trackBounds(const ClipRRect&) { this->pushControl(); } |
| 312 | void trackBounds(const ClipPath&) { this->pushControl(); } |
| 313 | void trackBounds(const ClipRegion&) { this->pushControl(); } |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 314 | |
vjiaoblack | e5de130 | 2016-07-13 14:05:28 -0700 | [diff] [blame] | 315 | |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 316 | // For all other ops, we can calculate and store the bounds directly now. |
| 317 | template <typename T> void trackBounds(const T& op) { |
| 318 | fBounds[fCurrentOp] = this->bounds(op); |
| 319 | this->updateSaveBounds(fBounds[fCurrentOp]); |
mtklein | 5ad6ee1 | 2014-08-11 08:08:43 -0700 | [diff] [blame] | 320 | } |
| 321 | |
mtklein | a723b57 | 2014-08-15 11:49:49 -0700 | [diff] [blame] | 322 | void pushSaveBlock(const SkPaint* paint) { |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 323 | // Starting a new Save block. Push a new entry to represent that. |
robertphillips | 4d52afe | 2014-11-03 08:19:44 -0800 | [diff] [blame] | 324 | SaveBounds sb; |
| 325 | sb.controlOps = 0; |
| 326 | // If the paint affects transparent black, the bound shouldn't be smaller |
| 327 | // than the current clip bounds. |
| 328 | sb.bounds = |
| 329 | PaintMayAffectTransparentBlack(paint) ? fCurrentClipBounds : Bounds::MakeEmpty(); |
| 330 | sb.paint = paint; |
senorblanco | db64af3 | 2015-12-09 10:11:43 -0800 | [diff] [blame] | 331 | sb.ctm = this->fCTM; |
robertphillips | 4d52afe | 2014-11-03 08:19:44 -0800 | [diff] [blame] | 332 | |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 333 | fSaveStack.push(sb); |
| 334 | this->pushControl(); |
| 335 | } |
| 336 | |
mtklein | d910f54 | 2014-08-22 09:06:34 -0700 | [diff] [blame] | 337 | static bool PaintMayAffectTransparentBlack(const SkPaint* paint) { |
dneto | 327f905 | 2014-09-15 10:53:16 -0700 | [diff] [blame] | 338 | if (paint) { |
| 339 | // FIXME: this is very conservative |
| 340 | if (paint->getImageFilter() || paint->getColorFilter()) { |
| 341 | return true; |
| 342 | } |
| 343 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 344 | // Unusual blendmodes require us to process a saved layer |
dneto | 327f905 | 2014-09-15 10:53:16 -0700 | [diff] [blame] | 345 | // even with operations outisde the clip. |
| 346 | // For example, DstIn is used by masking layers. |
| 347 | // https://code.google.com/p/skia/issues/detail?id=1291 |
| 348 | // https://crbug.com/401593 |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 349 | switch (paint->getBlendMode()) { |
| 350 | // For each of the following transfer modes, if the source |
| 351 | // alpha is zero (our transparent black), the resulting |
| 352 | // blended alpha is not necessarily equal to the original |
| 353 | // destination alpha. |
| 354 | case SkBlendMode::kClear: |
| 355 | case SkBlendMode::kSrc: |
| 356 | case SkBlendMode::kSrcIn: |
| 357 | case SkBlendMode::kDstIn: |
| 358 | case SkBlendMode::kSrcOut: |
| 359 | case SkBlendMode::kDstATop: |
| 360 | case SkBlendMode::kModulate: |
| 361 | return true; |
| 362 | break; |
| 363 | default: |
| 364 | break; |
dneto | 327f905 | 2014-09-15 10:53:16 -0700 | [diff] [blame] | 365 | } |
| 366 | } |
| 367 | return false; |
mtklein | d910f54 | 2014-08-22 09:06:34 -0700 | [diff] [blame] | 368 | } |
| 369 | |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 370 | Bounds popSaveBlock() { |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 371 | // We're done the Save block. Apply the block's bounds to all control ops inside it. |
| 372 | SaveBounds sb; |
| 373 | fSaveStack.pop(&sb); |
mtklein | d910f54 | 2014-08-22 09:06:34 -0700 | [diff] [blame] | 374 | |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 375 | while (sb.controlOps --> 0) { |
robertphillips | 4d52afe | 2014-11-03 08:19:44 -0800 | [diff] [blame] | 376 | this->popControl(sb.bounds); |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | // This whole Save block may be part another Save block. |
robertphillips | 4d52afe | 2014-11-03 08:19:44 -0800 | [diff] [blame] | 380 | this->updateSaveBounds(sb.bounds); |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 381 | |
| 382 | // 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] | 383 | return sb.bounds; |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | void pushControl() { |
| 387 | fControlIndices.push(fCurrentOp); |
| 388 | if (!fSaveStack.isEmpty()) { |
| 389 | fSaveStack.top().controlOps++; |
| 390 | } |
| 391 | } |
| 392 | |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 393 | void popControl(const Bounds& bounds) { |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 394 | fBounds[fControlIndices.top()] = bounds; |
| 395 | fControlIndices.pop(); |
| 396 | } |
| 397 | |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 398 | void updateSaveBounds(const Bounds& bounds) { |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 399 | // If we're in a Save block, expand its bounds to cover these bounds too. |
| 400 | if (!fSaveStack.isEmpty()) { |
| 401 | fSaveStack.top().bounds.join(bounds); |
| 402 | } |
| 403 | } |
| 404 | |
mtklein | 131a22b | 2014-08-25 14:16:15 -0700 | [diff] [blame] | 405 | // FIXME: this method could use better bounds |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 406 | Bounds bounds(const DrawText&) const { return fCurrentClipBounds; } |
mtklein | 68199a2 | 2014-08-25 13:49:29 -0700 | [diff] [blame] | 407 | |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 408 | Bounds bounds(const DrawPaint&) const { return fCurrentClipBounds; } |
| 409 | Bounds bounds(const NoOp&) const { return Bounds::MakeEmpty(); } // NoOps don't draw. |
mtklein | 828ce1f | 2014-08-13 12:58:45 -0700 | [diff] [blame] | 410 | |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 411 | Bounds bounds(const DrawRect& op) const { return this->adjustAndMap(op.rect, &op.paint); } |
msarett | 44df651 | 2016-08-25 13:54:30 -0700 | [diff] [blame] | 412 | Bounds bounds(const DrawRegion& op) const { |
| 413 | SkRect rect = SkRect::Make(op.region.getBounds()); |
| 414 | return this->adjustAndMap(rect, &op.paint); |
| 415 | } |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 416 | Bounds bounds(const DrawOval& op) const { return this->adjustAndMap(op.oval, &op.paint); } |
bsalomon | ac3aa24 | 2016-08-19 11:25:19 -0700 | [diff] [blame] | 417 | // Tighter arc bounds? |
| 418 | Bounds bounds(const DrawArc& op) const { return this->adjustAndMap(op.oval, &op.paint); } |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 419 | Bounds bounds(const DrawRRect& op) const { |
mtklein | 62b67ae | 2014-08-18 11:10:37 -0700 | [diff] [blame] | 420 | return this->adjustAndMap(op.rrect.rect(), &op.paint); |
| 421 | } |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 422 | Bounds bounds(const DrawDRRect& op) const { |
mtklein | 62b67ae | 2014-08-18 11:10:37 -0700 | [diff] [blame] | 423 | return this->adjustAndMap(op.outer.rect(), &op.paint); |
| 424 | } |
piotaixr | 6515175 | 2014-10-16 11:58:39 -0700 | [diff] [blame] | 425 | Bounds bounds(const DrawImage& op) const { |
mtklein | da574d1 | 2016-08-01 11:24:03 -0700 | [diff] [blame] | 426 | const SkImage* image = op.image.get(); |
piotaixr | 6515175 | 2014-10-16 11:58:39 -0700 | [diff] [blame] | 427 | SkRect rect = SkRect::MakeXYWH(op.left, op.top, image->width(), image->height()); |
mtklein | 62b67ae | 2014-08-18 11:10:37 -0700 | [diff] [blame] | 428 | |
piotaixr | 6515175 | 2014-10-16 11:58:39 -0700 | [diff] [blame] | 429 | return this->adjustAndMap(rect, op.paint); |
| 430 | } |
msarett | c573a40 | 2016-08-02 08:05:56 -0700 | [diff] [blame] | 431 | Bounds bounds(const DrawImageLattice& op) const { |
| 432 | return this->adjustAndMap(op.dst, op.paint); |
| 433 | } |
piotaixr | 6515175 | 2014-10-16 11:58:39 -0700 | [diff] [blame] | 434 | Bounds bounds(const DrawImageRect& op) const { |
| 435 | return this->adjustAndMap(op.dst, op.paint); |
| 436 | } |
reed | 4c21dc5 | 2015-06-25 12:32:03 -0700 | [diff] [blame] | 437 | Bounds bounds(const DrawImageNine& op) const { |
| 438 | return this->adjustAndMap(op.dst, op.paint); |
| 439 | } |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 440 | Bounds bounds(const DrawPath& op) const { |
mtklein | 62b67ae | 2014-08-18 11:10:37 -0700 | [diff] [blame] | 441 | return op.path.isInverseFillType() ? fCurrentClipBounds |
| 442 | : this->adjustAndMap(op.path.getBounds(), &op.paint); |
| 443 | } |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 444 | Bounds bounds(const DrawPoints& op) const { |
mtklein | 62b67ae | 2014-08-18 11:10:37 -0700 | [diff] [blame] | 445 | SkRect dst; |
| 446 | dst.set(op.pts, op.count); |
| 447 | |
| 448 | // Pad the bounding box a little to make sure hairline points' bounds aren't empty. |
| 449 | SkScalar stroke = SkMaxScalar(op.paint.getStrokeWidth(), 0.01f); |
| 450 | dst.outset(stroke/2, stroke/2); |
| 451 | |
| 452 | return this->adjustAndMap(dst, &op.paint); |
| 453 | } |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 454 | Bounds bounds(const DrawPatch& op) const { |
mtklein | 131a22b | 2014-08-25 14:16:15 -0700 | [diff] [blame] | 455 | SkRect dst; |
| 456 | dst.set(op.cubics, SkPatchUtils::kNumCtrlPts); |
| 457 | return this->adjustAndMap(dst, &op.paint); |
| 458 | } |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 459 | Bounds bounds(const DrawVertices& op) const { |
mtklein | 131a22b | 2014-08-25 14:16:15 -0700 | [diff] [blame] | 460 | SkRect dst; |
| 461 | dst.set(op.vertices, op.vertexCount); |
| 462 | return this->adjustAndMap(dst, &op.paint); |
| 463 | } |
mtklein | 64b4c78 | 2015-07-01 13:56:53 -0700 | [diff] [blame] | 464 | |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 465 | Bounds bounds(const DrawAtlas& op) const { |
| 466 | if (op.cull) { |
reed | 45561a0 | 2016-07-07 12:47:17 -0700 | [diff] [blame] | 467 | // TODO: <reed> can we pass nullptr for the paint? Isn't cull already "correct" |
| 468 | // for the paint (by the caller)? |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 469 | return this->adjustAndMap(*op.cull, op.paint); |
| 470 | } else { |
| 471 | return fCurrentClipBounds; |
| 472 | } |
| 473 | } |
mtklein | 64b4c78 | 2015-07-01 13:56:53 -0700 | [diff] [blame] | 474 | |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 475 | Bounds bounds(const DrawPicture& op) const { |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 476 | SkRect dst = op.picture->cullRect(); |
mtklein | af57903 | 2014-12-01 11:03:37 -0800 | [diff] [blame] | 477 | op.matrix.mapRect(&dst); |
mtklein | 131a22b | 2014-08-25 14:16:15 -0700 | [diff] [blame] | 478 | return this->adjustAndMap(dst, op.paint); |
| 479 | } |
mtklein | 62b67ae | 2014-08-18 11:10:37 -0700 | [diff] [blame] | 480 | |
vjiaoblack | 95302da | 2016-07-21 10:25:54 -0700 | [diff] [blame] | 481 | Bounds bounds(const DrawShadowedPicture& op) const { |
| 482 | SkRect dst = op.picture->cullRect(); |
| 483 | op.matrix.mapRect(&dst); |
| 484 | return this->adjustAndMap(dst, op.paint); |
| 485 | } |
| 486 | |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 487 | Bounds bounds(const DrawPosText& op) const { |
mtklein | 62b67ae | 2014-08-18 11:10:37 -0700 | [diff] [blame] | 488 | const int N = op.paint.countText(op.text, op.byteLength); |
| 489 | if (N == 0) { |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 490 | return Bounds::MakeEmpty(); |
mtklein | 62b67ae | 2014-08-18 11:10:37 -0700 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | SkRect dst; |
mtklein | 937c9c7 | 2014-09-02 15:19:48 -0700 | [diff] [blame] | 494 | dst.set(op.pos, N); |
mtklein | 62b67ae | 2014-08-18 11:10:37 -0700 | [diff] [blame] | 495 | AdjustTextForFontMetrics(&dst, op.paint); |
| 496 | return this->adjustAndMap(dst, &op.paint); |
| 497 | } |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 498 | Bounds bounds(const DrawPosTextH& op) const { |
mtklein | 62b67ae | 2014-08-18 11:10:37 -0700 | [diff] [blame] | 499 | const int N = op.paint.countText(op.text, op.byteLength); |
| 500 | if (N == 0) { |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 501 | return Bounds::MakeEmpty(); |
mtklein | 62b67ae | 2014-08-18 11:10:37 -0700 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | SkScalar left = op.xpos[0], right = op.xpos[0]; |
| 505 | for (int i = 1; i < N; i++) { |
| 506 | left = SkMinScalar(left, op.xpos[i]); |
| 507 | right = SkMaxScalar(right, op.xpos[i]); |
| 508 | } |
| 509 | SkRect dst = { left, op.y, right, op.y }; |
| 510 | AdjustTextForFontMetrics(&dst, op.paint); |
| 511 | return this->adjustAndMap(dst, &op.paint); |
| 512 | } |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 513 | Bounds bounds(const DrawTextOnPath& op) const { |
mtklein | 131a22b | 2014-08-25 14:16:15 -0700 | [diff] [blame] | 514 | SkRect dst = op.path.getBounds(); |
| 515 | |
mtklein | 9a5380d | 2014-12-16 06:31:01 -0800 | [diff] [blame] | 516 | // 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] | 517 | SkRect pad = { 0, 0, 0, 0}; |
| 518 | AdjustTextForFontMetrics(&pad, op.paint); |
mtklein | 9a5380d | 2014-12-16 06:31:01 -0800 | [diff] [blame] | 519 | |
| 520 | // That maximum padding happens to always be the right pad today. |
| 521 | SkASSERT(pad.fLeft == -pad.fRight); |
| 522 | SkASSERT(pad.fTop == -pad.fBottom); |
| 523 | SkASSERT(pad.fRight > pad.fBottom); |
| 524 | dst.outset(pad.fRight, pad.fRight); |
| 525 | |
mtklein | 131a22b | 2014-08-25 14:16:15 -0700 | [diff] [blame] | 526 | return this->adjustAndMap(dst, &op.paint); |
| 527 | } |
| 528 | |
reed | 45561a0 | 2016-07-07 12:47:17 -0700 | [diff] [blame] | 529 | Bounds bounds(const DrawTextRSXform& op) const { |
| 530 | if (op.cull) { |
| 531 | return this->adjustAndMap(*op.cull, nullptr); |
| 532 | } else { |
| 533 | return fCurrentClipBounds; |
| 534 | } |
| 535 | } |
| 536 | |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 537 | Bounds bounds(const DrawTextBlob& op) const { |
mtklein | 131a22b | 2014-08-25 14:16:15 -0700 | [diff] [blame] | 538 | SkRect dst = op.blob->bounds(); |
| 539 | dst.offset(op.x, op.y); |
mtklein | 131a22b | 2014-08-25 14:16:15 -0700 | [diff] [blame] | 540 | return this->adjustAndMap(dst, &op.paint); |
| 541 | } |
mtklein | 62b67ae | 2014-08-18 11:10:37 -0700 | [diff] [blame] | 542 | |
reed | 6be2aa9 | 2014-11-18 11:08:05 -0800 | [diff] [blame] | 543 | Bounds bounds(const DrawDrawable& op) const { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 544 | return this->adjustAndMap(op.worstCaseBounds, nullptr); |
reed | 6be2aa9 | 2014-11-18 11:08:05 -0800 | [diff] [blame] | 545 | } |
| 546 | |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 547 | Bounds bounds(const DrawAnnotation& op) const { |
| 548 | return this->adjustAndMap(op.rect, nullptr); |
| 549 | } |
mtklein | 343a63d | 2016-03-22 11:46:53 -0700 | [diff] [blame] | 550 | |
mtklein | 62b67ae | 2014-08-18 11:10:37 -0700 | [diff] [blame] | 551 | static void AdjustTextForFontMetrics(SkRect* rect, const SkPaint& paint) { |
mtklein | 9a5380d | 2014-12-16 06:31:01 -0800 | [diff] [blame] | 552 | #ifdef SK_DEBUG |
| 553 | SkRect correct = *rect; |
| 554 | #endif |
| 555 | // crbug.com/373785 ~~> xPad = 4x yPad |
| 556 | // crbug.com/424824 ~~> bump yPad from 2x text size to 2.5x |
| 557 | const SkScalar yPad = 2.5f * paint.getTextSize(), |
| 558 | xPad = 4.0f * yPad; |
| 559 | rect->outset(xPad, yPad); |
caryclark | 9a657fa | 2014-08-20 05:24:29 -0700 | [diff] [blame] | 560 | #ifdef SK_DEBUG |
mtklein | 62b67ae | 2014-08-18 11:10:37 -0700 | [diff] [blame] | 561 | SkPaint::FontMetrics metrics; |
| 562 | paint.getFontMetrics(&metrics); |
mtklein | 9a5380d | 2014-12-16 06:31:01 -0800 | [diff] [blame] | 563 | correct.fLeft += metrics.fXMin; |
| 564 | correct.fTop += metrics.fTop; |
| 565 | correct.fRight += metrics.fXMax; |
| 566 | correct.fBottom += metrics.fBottom; |
mtklein | d13291a | 2014-08-21 14:46:49 -0700 | [diff] [blame] | 567 | // See skia:2862 for why we ignore small text sizes. |
mtklein | 9a5380d | 2014-12-16 06:31:01 -0800 | [diff] [blame] | 568 | SkASSERTF(paint.getTextSize() < 0.001f || rect->contains(correct), |
mtklein | ed167ac | 2014-10-29 16:07:10 -0700 | [diff] [blame] | 569 | "%f %f %f %f vs. %f %f %f %f\n", |
mtklein | 9a5380d | 2014-12-16 06:31:01 -0800 | [diff] [blame] | 570 | -xPad, -yPad, +xPad, +yPad, |
mtklein | ed167ac | 2014-10-29 16:07:10 -0700 | [diff] [blame] | 571 | metrics.fXMin, metrics.fTop, metrics.fXMax, metrics.fBottom); |
mtklein | a19afb4 | 2014-08-19 17:47:14 -0700 | [diff] [blame] | 572 | #endif |
mtklein | 62b67ae | 2014-08-18 11:10:37 -0700 | [diff] [blame] | 573 | } |
| 574 | |
mtklein | 479601b | 2014-08-18 08:45:33 -0700 | [diff] [blame] | 575 | // Returns true if rect was meaningfully adjusted for the effects of paint, |
| 576 | // false if the paint could affect the rect in unknown ways. |
| 577 | static bool AdjustForPaint(const SkPaint* paint, SkRect* rect) { |
mtklein | a723b57 | 2014-08-15 11:49:49 -0700 | [diff] [blame] | 578 | if (paint) { |
| 579 | if (paint->canComputeFastBounds()) { |
mtklein | 479601b | 2014-08-18 08:45:33 -0700 | [diff] [blame] | 580 | *rect = paint->computeFastBounds(*rect, rect); |
| 581 | return true; |
mtklein | a723b57 | 2014-08-15 11:49:49 -0700 | [diff] [blame] | 582 | } |
mtklein | 479601b | 2014-08-18 08:45:33 -0700 | [diff] [blame] | 583 | return false; |
| 584 | } |
| 585 | return true; |
| 586 | } |
| 587 | |
mtklein | 8e393bf | 2014-10-01 12:48:58 -0700 | [diff] [blame] | 588 | bool adjustForSaveLayerPaints(SkRect* rect, int savesToIgnore = 0) const { |
| 589 | for (int i = fSaveStack.count() - 1 - savesToIgnore; i >= 0; i--) { |
senorblanco | db64af3 | 2015-12-09 10:11:43 -0800 | [diff] [blame] | 590 | SkMatrix inverse; |
| 591 | if (!fSaveStack[i].ctm.invert(&inverse)) { |
| 592 | return false; |
| 593 | } |
| 594 | inverse.mapRect(rect); |
Mike Klein | 271a030 | 2014-09-23 15:28:38 -0400 | [diff] [blame] | 595 | if (!AdjustForPaint(fSaveStack[i].paint, rect)) { |
| 596 | return false; |
| 597 | } |
senorblanco | db64af3 | 2015-12-09 10:11:43 -0800 | [diff] [blame] | 598 | fSaveStack[i].ctm.mapRect(rect); |
Mike Klein | 271a030 | 2014-09-23 15:28:38 -0400 | [diff] [blame] | 599 | } |
| 600 | return true; |
| 601 | } |
| 602 | |
mtklein | c6ad06a | 2015-08-19 09:51:00 -0700 | [diff] [blame] | 603 | const int fNumRecords; |
mtklein | a723b57 | 2014-08-15 11:49:49 -0700 | [diff] [blame] | 604 | |
robertphillips | 4d52afe | 2014-11-03 08:19:44 -0800 | [diff] [blame] | 605 | // We do not guarantee anything for operations outside of the cull rect |
| 606 | const SkRect fCullRect; |
| 607 | |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 608 | // Conservative identity-space bounds for each op in the SkRecord. |
mtklein | 40732b3 | 2015-10-24 07:45:47 -0700 | [diff] [blame] | 609 | Bounds* fBounds; |
mtklein | a723b57 | 2014-08-15 11:49:49 -0700 | [diff] [blame] | 610 | |
| 611 | // We walk fCurrentOp through the SkRecord, as we go using updateCTM() |
| 612 | // and updateClipBounds() to maintain the exact CTM (fCTM) and conservative |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 613 | // identity-space bounds of the current clip (fCurrentClipBounds). |
mtklein | c6ad06a | 2015-08-19 09:51:00 -0700 | [diff] [blame] | 614 | int fCurrentOp; |
mtklein | e9d2052 | 2015-11-19 12:08:24 -0800 | [diff] [blame] | 615 | SkMatrix fCTM; |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 616 | Bounds fCurrentClipBounds; |
mtklein | a723b57 | 2014-08-15 11:49:49 -0700 | [diff] [blame] | 617 | |
| 618 | // 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] | 619 | SkTDArray<SaveBounds> fSaveStack; |
mtklein | c6ad06a | 2015-08-19 09:51:00 -0700 | [diff] [blame] | 620 | SkTDArray<int> fControlIndices; |
mtklein | 5ad6ee1 | 2014-08-11 08:08:43 -0700 | [diff] [blame] | 621 | }; |
| 622 | |
commit-bot@chromium.org | 27f6b0d | 2014-05-09 14:59:29 +0000 | [diff] [blame] | 623 | } // namespace SkRecords |
mtklein | 5ad6ee1 | 2014-08-11 08:08:43 -0700 | [diff] [blame] | 624 | |
mtklein | 40732b3 | 2015-10-24 07:45:47 -0700 | [diff] [blame] | 625 | void SkRecordFillBounds(const SkRect& cullRect, const SkRecord& record, SkRect bounds[]) { |
| 626 | SkRecords::FillBounds visitor(cullRect, record, bounds); |
mtklein | c6ad06a | 2015-08-19 09:51:00 -0700 | [diff] [blame] | 627 | for (int curOp = 0; curOp < record.count(); curOp++) { |
robertphillips | 4e8e342 | 2014-11-12 06:46:08 -0800 | [diff] [blame] | 628 | visitor.setCurrentOp(curOp); |
mtklein | 343a63d | 2016-03-22 11:46:53 -0700 | [diff] [blame] | 629 | record.visit(curOp, visitor); |
robertphillips | 4e8e342 | 2014-11-12 06:46:08 -0800 | [diff] [blame] | 630 | } |
mtklein | 40732b3 | 2015-10-24 07:45:47 -0700 | [diff] [blame] | 631 | visitor.cleanUp(); |
mtklein | 5ad6ee1 | 2014-08-11 08:08:43 -0700 | [diff] [blame] | 632 | } |
robertphillips | 4e8e342 | 2014-11-12 06:46:08 -0800 | [diff] [blame] | 633 | |