blob: 65455864991888d723ccd696d89432debd48c667 [file] [log] [blame]
commit-bot@chromium.orgc4b21e62014-04-11 18:33:31 +00001/*
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.org506db0b2014-04-08 23:31:35 +00008#include "SkRecordDraw.h"
mtklein131a22b2014-08-25 14:16:15 -07009#include "SkPatchUtils.h"
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000010
mtklein5ad6ee12014-08-11 08:08:43 -070011void SkRecordDraw(const SkRecord& record,
12 SkCanvas* canvas,
reed1bdfd3f2014-11-24 14:41:51 -080013 SkPicture const* const drawablePicts[],
reed3cb38402015-02-06 08:36:15 -080014 SkDrawable* const drawables[],
reed1bdfd3f2014-11-24 14:41:51 -080015 int drawableCount,
mtklein5ad6ee12014-08-11 08:08:43 -070016 const SkBBoxHierarchy* bbh,
robertphillips783fe162015-01-07 07:28:41 -080017 SkPicture::AbortCallback* callback) {
Mike Kleinc11530e2014-06-24 11:29:06 -040018 SkAutoCanvasRestore saveRestore(canvas, true /*save now, restore at exit*/);
mtklein5ad6ee12014-08-11 08:08:43 -070019
bsalomon49f085d2014-09-05 13:34:00 -070020 if (bbh) {
mtklein5ad6ee12014-08-11 08:08:43 -070021 // Draw only ops that affect pixels in the canvas's current clip.
mtklein3e8232b2014-08-18 13:39:11 -070022 // 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.
mtklein7cc1a342014-11-20 08:01:09 -080026 SkRect query;
27 if (!canvas->getClipBounds(&query)) {
mtklein49aabde2015-01-05 07:02:45 -080028 query.setEmpty();
mtklein7cc1a342014-11-20 08:01:09 -080029 }
mtklein3e8232b2014-08-18 13:39:11 -070030
mtkleinc6ad06a2015-08-19 09:51:00 -070031 SkTDArray<int> ops;
mtkleina723b572014-08-15 11:49:49 -070032 bbh->search(query, &ops);
mtklein5ad6ee12014-08-11 08:08:43 -070033
reed1bdfd3f2014-11-24 14:41:51 -080034 SkRecords::Draw draw(canvas, drawablePicts, drawables, drawableCount);
mtklein5ad6ee12014-08-11 08:08:43 -070035 for (int i = 0; i < ops.count(); i++) {
robertphillips783fe162015-01-07 07:28:41 -080036 if (callback && callback->abort()) {
mtklein5ad6ee12014-08-11 08:08:43 -070037 return;
38 }
danakjd239d422014-11-03 12:43:30 -080039 // This visit call uses the SkRecords::Draw::operator() to call
40 // methods on the |canvas|, wrapped by methods defined with the
41 // DRAW() macro.
mtklein343a63d2016-03-22 11:46:53 -070042 record.visit(ops[i], draw);
mtklein5ad6ee12014-08-11 08:08:43 -070043 }
44 } else {
45 // Draw all ops.
reed1bdfd3f2014-11-24 14:41:51 -080046 SkRecords::Draw draw(canvas, drawablePicts, drawables, drawableCount);
mtkleinc6ad06a2015-08-19 09:51:00 -070047 for (int i = 0; i < record.count(); i++) {
robertphillips783fe162015-01-07 07:28:41 -080048 if (callback && callback->abort()) {
mtklein5ad6ee12014-08-11 08:08:43 -070049 return;
50 }
danakjd239d422014-11-03 12:43:30 -080051 // This visit call uses the SkRecords::Draw::operator() to call
52 // methods on the |canvas|, wrapped by methods defined with the
53 // DRAW() macro.
mtklein343a63d2016-03-22 11:46:53 -070054 record.visit(i, draw);
mtklein5ad6ee12014-08-11 08:08:43 -070055 }
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000056 }
commit-bot@chromium.org27f6b0d2014-05-09 14:59:29 +000057}
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000058
reed6be2aa92014-11-18 11:08:05 -080059void SkRecordPartialDraw(const SkRecord& record, SkCanvas* canvas,
60 SkPicture const* const drawablePicts[], int drawableCount,
mtkleinc6ad06a2015-08-19 09:51:00 -070061 int start, int stop,
robertphillips4815fe52014-09-16 10:32:43 -070062 const SkMatrix& initialCTM) {
mtklein00f30bd2014-09-02 12:03:31 -070063 SkAutoCanvasRestore saveRestore(canvas, true /*save now, restore at exit*/);
64
65 stop = SkTMin(stop, record.count());
halcanary96fcdcc2015-08-27 07:41:13 -070066 SkRecords::Draw draw(canvas, drawablePicts, nullptr, drawableCount, &initialCTM);
mtkleinc6ad06a2015-08-19 09:51:00 -070067 for (int i = start; i < stop; i++) {
mtklein343a63d2016-03-22 11:46:53 -070068 record.visit(i, draw);
mtklein00f30bd2014-09-02 12:03:31 -070069 }
70}
71
commit-bot@chromium.org27f6b0d2014-05-09 14:59:29 +000072namespace SkRecords {
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000073
commit-bot@chromium.org2e0c32a2014-04-28 16:19:45 +000074// NoOps draw nothing.
commit-bot@chromium.org27f6b0d2014-05-09 14:59:29 +000075template <> void Draw::draw(const NoOp&) {}
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000076
commit-bot@chromium.org27f6b0d2014-05-09 14:59:29 +000077#define DRAW(T, call) template <> void Draw::draw(const T& r) { fCanvas->call; }
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000078DRAW(Restore, restore());
Florin Malita5f6102d2014-06-30 10:13:28 -040079DRAW(Save, save());
mtkleinda574d12016-08-01 11:24:03 -070080DRAW(SaveLayer, saveLayer(SkCanvas::SaveLayerRec(r.bounds,
81 r.paint,
82 r.backdrop.get(),
83 r.saveLayerFlags)));
commit-bot@chromium.org99bd7d82014-05-19 15:51:12 +000084DRAW(SetMatrix, setMatrix(SkMatrix::Concat(fInitialCTM, r.matrix)));
mtkleine9d20522015-11-19 12:08:24 -080085DRAW(Concat, concat(r.matrix));
mtkleincbdf0072016-08-19 09:05:27 -070086DRAW(Translate, translate(r.dx, r.dy));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000087
mtkleincdeeb092014-11-20 09:14:28 -080088DRAW(ClipPath, clipPath(r.path, r.opAA.op, r.opAA.aa));
89DRAW(ClipRRect, clipRRect(r.rrect, r.opAA.op, r.opAA.aa));
90DRAW(ClipRect, clipRect(r.rect, r.opAA.op, r.opAA.aa));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000091DRAW(ClipRegion, clipRegion(r.region, r.op));
92
vjiaoblack95302da2016-07-21 10:25:54 -070093#ifdef SK_EXPERIMENTAL_SHADOWING
vjiaoblacke5de1302016-07-13 14:05:28 -070094DRAW(TranslateZ, SkCanvas::translateZ(r.z));
vjiaoblack95302da2016-07-21 10:25:54 -070095#else
96template <> void Draw::draw(const TranslateZ& r) { }
97#endif
vjiaoblacke5de1302016-07-13 14:05:28 -070098
bsalomonac3aa242016-08-19 11:25:19 -070099DRAW(DrawArc, drawArc(r.oval, r.startAngle, r.sweepAngle, r.useCenter, r.paint));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +0000100DRAW(DrawDRRect, drawDRRect(r.outer, r.inner, r.paint));
mtkleinda574d12016-08-01 11:24:03 -0700101DRAW(DrawImage, drawImage(r.image.get(), r.left, r.top, r.paint));
msarettc573a402016-08-02 08:05:56 -0700102
103template <> 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;
109 fCanvas->drawImageLattice(r.image.get(), lattice, r.dst, r.paint);
110}
111
mtkleinda574d12016-08-01 11:24:03 -0700112DRAW(DrawImageRect, legacy_drawImageRect(r.image.get(), r.src, r.dst, r.paint, r.constraint));
113DRAW(DrawImageNine, drawImageNine(r.image.get(), r.center, r.dst, r.paint));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +0000114DRAW(DrawOval, drawOval(r.oval, r.paint));
115DRAW(DrawPaint, drawPaint(r.paint));
116DRAW(DrawPath, drawPath(r.path, r.paint));
mtklein9b222a52014-09-18 11:16:31 -0700117DRAW(DrawPatch, drawPatch(r.cubics, r.colors, r.texCoords, r.xmode, r.paint));
mtkleinda574d12016-08-01 11:24:03 -0700118DRAW(DrawPicture, drawPicture(r.picture.get(), &r.matrix, r.paint));
vjiaoblack95302da2016-07-21 10:25:54 -0700119
120#ifdef SK_EXPERIMENTAL_SHADOWING
vjiaoblacke6f5d562016-08-25 06:30:23 -0700121DRAW(DrawShadowedPicture, drawShadowedPicture(r.picture.get(), &r.matrix, r.paint, r.params));
vjiaoblack95302da2016-07-21 10:25:54 -0700122#else
123template <> void Draw::draw(const DrawShadowedPicture& r) { }
124#endif
125
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +0000126DRAW(DrawPoints, drawPoints(r.mode, r.count, r.pts, r.paint));
127DRAW(DrawPosText, drawPosText(r.text, r.byteLength, r.pos, r.paint));
128DRAW(DrawPosTextH, drawPosTextH(r.text, r.byteLength, r.xpos, r.y, r.paint));
129DRAW(DrawRRect, drawRRect(r.rrect, r.paint));
130DRAW(DrawRect, drawRect(r.rect, r.paint));
msarett44df6512016-08-25 13:54:30 -0700131DRAW(DrawRegion, drawRegion(r.region, r.paint));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +0000132DRAW(DrawText, drawText(r.text, r.byteLength, r.x, r.y, r.paint));
mtkleinda574d12016-08-01 11:24:03 -0700133DRAW(DrawTextBlob, drawTextBlob(r.blob.get(), r.x, r.y, r.paint));
mtkleinaf579032014-12-01 11:03:37 -0800134DRAW(DrawTextOnPath, drawTextOnPath(r.text, r.byteLength, r.path, &r.matrix, r.paint));
reed45561a02016-07-07 12:47:17 -0700135DRAW(DrawTextRSXform, drawTextRSXform(r.text, r.byteLength, r.xforms, r.cull, r.paint));
mtkleinda574d12016-08-01 11:24:03 -0700136DRAW(DrawAtlas, drawAtlas(r.atlas.get(),
137 r.xforms, r.texs, r.colors, r.count, r.mode, r.cull, r.paint));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +0000138DRAW(DrawVertices, drawVertices(r.vmode, r.vertexCount, r.vertices, r.texs, r.colors,
mtklein449d9b72015-09-28 10:33:02 -0700139 r.xmode, r.indices, r.indexCount, r.paint));
mtkleinda574d12016-08-01 11:24:03 -0700140DRAW(DrawAnnotation, drawAnnotation(r.rect, r.key.c_str(), r.value.get()));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +0000141#undef DRAW
142
reed6be2aa92014-11-18 11:08:05 -0800143template <> void Draw::draw(const DrawDrawable& r) {
144 SkASSERT(r.index >= 0);
145 SkASSERT(r.index < fDrawableCount);
reed1bdfd3f2014-11-24 14:41:51 -0800146 if (fDrawables) {
halcanary96fcdcc2015-08-27 07:41:13 -0700147 SkASSERT(nullptr == fDrawablePicts);
reeda8db7282015-07-07 10:22:31 -0700148 fCanvas->drawDrawable(fDrawables[r.index], r.matrix);
reed1bdfd3f2014-11-24 14:41:51 -0800149 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700150 fCanvas->drawPicture(fDrawablePicts[r.index], r.matrix, nullptr);
reed1bdfd3f2014-11-24 14:41:51 -0800151 }
reed6be2aa92014-11-18 11:08:05 -0800152}
153
mtklein5ad6ee12014-08-11 08:08:43 -0700154// This is an SkRecord visitor that fills an SkBBoxHierarchy.
mtklein828ce1f2014-08-13 12:58:45 -0700155//
156// The interesting part here is how to calculate bounds for ops which don't
157// have intrinsic bounds. What is the bounds of a Save or a Translate?
158//
159// We answer this by thinking about a particular definition of bounds: if I
160// don't execute this op, pixels in this rectangle might draw incorrectly. So
161// the bounds of a Save, a Translate, a Restore, etc. are the union of the
162// bounds of Draw* ops that they might have an effect on. For any given
163// Save/Restore block, the bounds of the Save, the Restore, and any other
164// non-drawing ("control") ops inside are exactly the union of the bounds of
165// the drawing ops inside that block.
166//
167// To implement this, we keep a stack of active Save blocks. As we consume ops
168// inside the Save/Restore block, drawing ops are unioned with the bounds of
169// the block, and control ops are stashed away for later. When we finish the
170// block with a Restore, our bounds are complete, and we go back and fill them
171// in for all the control ops we stashed away.
mtklein5ad6ee12014-08-11 08:08:43 -0700172class FillBounds : SkNoncopyable {
173public:
mtklein40732b32015-10-24 07:45:47 -0700174 FillBounds(const SkRect& cullRect, const SkRecord& record, SkRect bounds[])
robertphillips4e8e3422014-11-12 06:46:08 -0800175 : fNumRecords(record.count())
176 , fCullRect(cullRect)
mtklein40732b32015-10-24 07:45:47 -0700177 , fBounds(bounds) {
mtkleine9d20522015-11-19 12:08:24 -0800178 fCTM = SkMatrix::I();
robertphillips4d52afe2014-11-03 08:19:44 -0800179 fCurrentClipBounds = fCullRect;
robertphillips4e8e3422014-11-12 06:46:08 -0800180 }
mtklein5ad6ee12014-08-11 08:08:43 -0700181
mtklein40732b32015-10-24 07:45:47 -0700182 void cleanUp() {
mtklein828ce1f2014-08-13 12:58:45 -0700183 // If we have any lingering unpaired Saves, simulate restores to make
184 // sure all ops in those Save blocks have their bounds calculated.
185 while (!fSaveStack.isEmpty()) {
186 this->popSaveBlock();
187 }
188
189 // Any control ops not part of any Save/Restore block draw everywhere.
190 while (!fControlIndices.isEmpty()) {
robertphillips4d52afe2014-11-03 08:19:44 -0800191 this->popControl(fCullRect);
mtklein828ce1f2014-08-13 12:58:45 -0700192 }
mtklein828ce1f2014-08-13 12:58:45 -0700193 }
mtklein5ad6ee12014-08-11 08:08:43 -0700194
mtklein40732b32015-10-24 07:45:47 -0700195 void setCurrentOp(int currentOp) { fCurrentOp = currentOp; }
196
197
mtkleina723b572014-08-15 11:49:49 -0700198 template <typename T> void operator()(const T& op) {
199 this->updateCTM(op);
200 this->updateClipBounds(op);
201 this->trackBounds(op);
mtklein5ad6ee12014-08-11 08:08:43 -0700202 }
203
mtklein533eb782014-08-27 10:39:42 -0700204 // In this file, SkRect are in local coordinates, Bounds are translated back to identity space.
205 typedef SkRect Bounds;
206
mtkleinc6ad06a2015-08-19 09:51:00 -0700207 int currentOp() const { return fCurrentOp; }
mtkleine9d20522015-11-19 12:08:24 -0800208 const SkMatrix& ctm() const { return fCTM; }
mtkleinc6ad06a2015-08-19 09:51:00 -0700209 const Bounds& getBounds(int index) const { return fBounds[index]; }
robertphillips4e8e3422014-11-12 06:46:08 -0800210
211 // Adjust rect for all paints that may affect its geometry, then map it to identity space.
212 Bounds adjustAndMap(SkRect rect, const SkPaint* paint) const {
213 // Inverted rectangles really confuse our BBHs.
214 rect.sort();
215
216 // Adjust the rect for its own paint.
217 if (!AdjustForPaint(paint, &rect)) {
218 // The paint could do anything to our bounds. The only safe answer is the current clip.
219 return fCurrentClipBounds;
220 }
221
222 // Adjust rect for all the paints from the SaveLayers we're inside.
223 if (!this->adjustForSaveLayerPaints(&rect)) {
224 // Same deal as above.
225 return fCurrentClipBounds;
226 }
227
228 // Map the rect back to identity space.
mtkleine9d20522015-11-19 12:08:24 -0800229 fCTM.mapRect(&rect);
robertphillips4e8e3422014-11-12 06:46:08 -0800230
231 // Nothing can draw outside the current clip.
robertphillipsc187a3c2014-12-30 13:53:51 -0800232 if (!rect.intersect(fCurrentClipBounds)) {
233 return Bounds::MakeEmpty();
234 }
235
robertphillips4e8e3422014-11-12 06:46:08 -0800236 return rect;
237 }
238
239private:
mtklein828ce1f2014-08-13 12:58:45 -0700240 struct SaveBounds {
mtkleina723b572014-08-15 11:49:49 -0700241 int controlOps; // Number of control ops in this Save block, including the Save.
mtklein533eb782014-08-27 10:39:42 -0700242 Bounds bounds; // Bounds of everything in the block.
mtkleina723b572014-08-15 11:49:49 -0700243 const SkPaint* paint; // Unowned. If set, adjusts the bounds of all ops in this block.
senorblancodb64af32015-12-09 10:11:43 -0800244 SkMatrix ctm;
mtklein828ce1f2014-08-13 12:58:45 -0700245 };
246
mtkleincbdf0072016-08-19 09:05:27 -0700247 // Only Restore, SetMatrix, Concat, and Translate change the CTM.
mtklein8e393bf2014-10-01 12:48:58 -0700248 template <typename T> void updateCTM(const T&) {}
mtkleine9d20522015-11-19 12:08:24 -0800249 void updateCTM(const Restore& op) { fCTM = op.matrix; }
250 void updateCTM(const SetMatrix& op) { fCTM = op.matrix; }
251 void updateCTM(const Concat& op) { fCTM.preConcat(op.matrix); }
mtkleincbdf0072016-08-19 09:05:27 -0700252 void updateCTM(const Translate& op) { fCTM.preTranslate(op.dx, op.dy); }
mtkleina723b572014-08-15 11:49:49 -0700253
mtklein8e393bf2014-10-01 12:48:58 -0700254 // Most ops don't change the clip.
255 template <typename T> void updateClipBounds(const T&) {}
Mike Klein271a0302014-09-23 15:28:38 -0400256
mtklein8e393bf2014-10-01 12:48:58 -0700257 // Clip{Path,RRect,Rect,Region} obviously change the clip. They all know their bounds already.
258 void updateClipBounds(const ClipPath& op) { this->updateClipBoundsForClipOp(op.devBounds); }
259 void updateClipBounds(const ClipRRect& op) { this->updateClipBoundsForClipOp(op.devBounds); }
260 void updateClipBounds(const ClipRect& op) { this->updateClipBoundsForClipOp(op.devBounds); }
261 void updateClipBounds(const ClipRegion& op) { this->updateClipBoundsForClipOp(op.devBounds); }
Mike Klein271a0302014-09-23 15:28:38 -0400262
mtklein8e393bf2014-10-01 12:48:58 -0700263 // The bounds of clip ops need to be adjusted for the paints of saveLayers they're inside.
264 void updateClipBoundsForClipOp(const SkIRect& devBounds) {
265 Bounds clip = SkRect::Make(devBounds);
Mike Klein271a0302014-09-23 15:28:38 -0400266 // We don't call adjustAndMap() because as its last step it would intersect the adjusted
267 // clip bounds with the previous clip, exactly what we can't do when the clip grows.
schenney23d85932015-03-06 16:20:28 -0800268 if (this->adjustForSaveLayerPaints(&clip)) {
269 fCurrentClipBounds = clip.intersect(fCullRect) ? clip : Bounds::MakeEmpty();
270 } else {
271 fCurrentClipBounds = fCullRect;
272 }
Mike Klein271a0302014-09-23 15:28:38 -0400273 }
274
mtklein8e393bf2014-10-01 12:48:58 -0700275 // Restore holds the devBounds for the clip after the {save,saveLayer}/restore block completes.
276 void updateClipBounds(const Restore& op) {
277 // This is just like the clip ops above, but we need to skip the effects (if any) of our
278 // paired saveLayer (if it is one); it has not yet been popped off the save stack. Our
279 // devBounds reflect the state of the world after the saveLayer/restore block is done,
280 // so they are not affected by the saveLayer's paint.
281 const int kSavesToIgnore = 1;
282 Bounds clip = SkRect::Make(op.devBounds);
schenney23d85932015-03-06 16:20:28 -0800283 if (this->adjustForSaveLayerPaints(&clip, kSavesToIgnore)) {
284 fCurrentClipBounds = clip.intersect(fCullRect) ? clip : Bounds::MakeEmpty();
285 } else {
286 fCurrentClipBounds = fCullRect;
287 }
mtklein8e393bf2014-10-01 12:48:58 -0700288 }
289
Mike Klein271a0302014-09-23 15:28:38 -0400290 // We also take advantage of SaveLayer bounds when present to further cut the clip down.
mtkleina723b572014-08-15 11:49:49 -0700291 void updateClipBounds(const SaveLayer& op) {
292 if (op.bounds) {
Mike Klein271a0302014-09-23 15:28:38 -0400293 // adjustAndMap() intersects these layer bounds with the previous clip for us.
294 fCurrentClipBounds = this->adjustAndMap(*op.bounds, op.paint);
mtkleina723b572014-08-15 11:49:49 -0700295 }
296 }
mtklein6cfa73a2014-08-13 13:33:49 -0700297
mtklein828ce1f2014-08-13 12:58:45 -0700298 // The bounds of these ops must be calculated when we hit the Restore
299 // from the bounds of the ops in the same Save block.
halcanary96fcdcc2015-08-27 07:41:13 -0700300 void trackBounds(const Save&) { this->pushSaveBlock(nullptr); }
mtkleina723b572014-08-15 11:49:49 -0700301 void trackBounds(const SaveLayer& op) { this->pushSaveBlock(op.paint); }
302 void trackBounds(const Restore&) { fBounds[fCurrentOp] = this->popSaveBlock(); }
mtklein828ce1f2014-08-13 12:58:45 -0700303
mtklein68199a22014-08-25 13:49:29 -0700304 void trackBounds(const SetMatrix&) { this->pushControl(); }
mtkleine9d20522015-11-19 12:08:24 -0800305 void trackBounds(const Concat&) { this->pushControl(); }
mtkleincbdf0072016-08-19 09:05:27 -0700306 void trackBounds(const Translate&) { this->pushControl(); }
307 void trackBounds(const TranslateZ&) { this->pushControl(); }
mtklein68199a22014-08-25 13:49:29 -0700308 void trackBounds(const ClipRect&) { this->pushControl(); }
309 void trackBounds(const ClipRRect&) { this->pushControl(); }
310 void trackBounds(const ClipPath&) { this->pushControl(); }
311 void trackBounds(const ClipRegion&) { this->pushControl(); }
mtklein828ce1f2014-08-13 12:58:45 -0700312
vjiaoblacke5de1302016-07-13 14:05:28 -0700313
mtklein828ce1f2014-08-13 12:58:45 -0700314 // For all other ops, we can calculate and store the bounds directly now.
315 template <typename T> void trackBounds(const T& op) {
316 fBounds[fCurrentOp] = this->bounds(op);
317 this->updateSaveBounds(fBounds[fCurrentOp]);
mtklein5ad6ee12014-08-11 08:08:43 -0700318 }
319
mtkleina723b572014-08-15 11:49:49 -0700320 void pushSaveBlock(const SkPaint* paint) {
mtklein828ce1f2014-08-13 12:58:45 -0700321 // Starting a new Save block. Push a new entry to represent that.
robertphillips4d52afe2014-11-03 08:19:44 -0800322 SaveBounds sb;
323 sb.controlOps = 0;
324 // If the paint affects transparent black, the bound shouldn't be smaller
325 // than the current clip bounds.
326 sb.bounds =
327 PaintMayAffectTransparentBlack(paint) ? fCurrentClipBounds : Bounds::MakeEmpty();
328 sb.paint = paint;
senorblancodb64af32015-12-09 10:11:43 -0800329 sb.ctm = this->fCTM;
robertphillips4d52afe2014-11-03 08:19:44 -0800330
mtklein828ce1f2014-08-13 12:58:45 -0700331 fSaveStack.push(sb);
332 this->pushControl();
333 }
334
mtkleind910f542014-08-22 09:06:34 -0700335 static bool PaintMayAffectTransparentBlack(const SkPaint* paint) {
dneto327f9052014-09-15 10:53:16 -0700336 if (paint) {
337 // FIXME: this is very conservative
338 if (paint->getImageFilter() || paint->getColorFilter()) {
339 return true;
340 }
341
342 // Unusual Xfermodes require us to process a saved layer
343 // even with operations outisde the clip.
344 // For example, DstIn is used by masking layers.
345 // https://code.google.com/p/skia/issues/detail?id=1291
346 // https://crbug.com/401593
347 SkXfermode* xfermode = paint->getXfermode();
348 SkXfermode::Mode mode;
halcanary96fcdcc2015-08-27 07:41:13 -0700349 // SrcOver is ok, and is also the common case with a nullptr xfermode.
dneto327f9052014-09-15 10:53:16 -0700350 // So we should make that the fast path and bypass the mode extraction
351 // and test.
352 if (xfermode && xfermode->asMode(&mode)) {
353 switch (mode) {
354 // For each of the following transfer modes, if the source
355 // alpha is zero (our transparent black), the resulting
356 // blended alpha is not necessarily equal to the original
357 // destination alpha.
358 case SkXfermode::kClear_Mode:
359 case SkXfermode::kSrc_Mode:
360 case SkXfermode::kSrcIn_Mode:
361 case SkXfermode::kDstIn_Mode:
362 case SkXfermode::kSrcOut_Mode:
363 case SkXfermode::kDstATop_Mode:
364 case SkXfermode::kModulate_Mode:
365 return true;
366 break;
367 default:
368 break;
369 }
370 }
371 }
372 return false;
mtkleind910f542014-08-22 09:06:34 -0700373 }
374
mtklein533eb782014-08-27 10:39:42 -0700375 Bounds popSaveBlock() {
mtklein828ce1f2014-08-13 12:58:45 -0700376 // We're done the Save block. Apply the block's bounds to all control ops inside it.
377 SaveBounds sb;
378 fSaveStack.pop(&sb);
mtkleind910f542014-08-22 09:06:34 -0700379
mtklein828ce1f2014-08-13 12:58:45 -0700380 while (sb.controlOps --> 0) {
robertphillips4d52afe2014-11-03 08:19:44 -0800381 this->popControl(sb.bounds);
mtklein828ce1f2014-08-13 12:58:45 -0700382 }
383
384 // This whole Save block may be part another Save block.
robertphillips4d52afe2014-11-03 08:19:44 -0800385 this->updateSaveBounds(sb.bounds);
mtklein828ce1f2014-08-13 12:58:45 -0700386
387 // If called from a real Restore (not a phony one for balance), it'll need the bounds.
robertphillips4d52afe2014-11-03 08:19:44 -0800388 return sb.bounds;
mtklein828ce1f2014-08-13 12:58:45 -0700389 }
390
391 void pushControl() {
392 fControlIndices.push(fCurrentOp);
393 if (!fSaveStack.isEmpty()) {
394 fSaveStack.top().controlOps++;
395 }
396 }
397
mtklein533eb782014-08-27 10:39:42 -0700398 void popControl(const Bounds& bounds) {
mtklein828ce1f2014-08-13 12:58:45 -0700399 fBounds[fControlIndices.top()] = bounds;
400 fControlIndices.pop();
401 }
402
mtklein533eb782014-08-27 10:39:42 -0700403 void updateSaveBounds(const Bounds& bounds) {
mtklein828ce1f2014-08-13 12:58:45 -0700404 // If we're in a Save block, expand its bounds to cover these bounds too.
405 if (!fSaveStack.isEmpty()) {
406 fSaveStack.top().bounds.join(bounds);
407 }
408 }
409
mtklein131a22b2014-08-25 14:16:15 -0700410 // FIXME: this method could use better bounds
mtklein533eb782014-08-27 10:39:42 -0700411 Bounds bounds(const DrawText&) const { return fCurrentClipBounds; }
mtklein68199a22014-08-25 13:49:29 -0700412
mtklein533eb782014-08-27 10:39:42 -0700413 Bounds bounds(const DrawPaint&) const { return fCurrentClipBounds; }
414 Bounds bounds(const NoOp&) const { return Bounds::MakeEmpty(); } // NoOps don't draw.
mtklein828ce1f2014-08-13 12:58:45 -0700415
mtklein533eb782014-08-27 10:39:42 -0700416 Bounds bounds(const DrawRect& op) const { return this->adjustAndMap(op.rect, &op.paint); }
msarett44df6512016-08-25 13:54:30 -0700417 Bounds bounds(const DrawRegion& op) const {
418 SkRect rect = SkRect::Make(op.region.getBounds());
419 return this->adjustAndMap(rect, &op.paint);
420 }
mtklein533eb782014-08-27 10:39:42 -0700421 Bounds bounds(const DrawOval& op) const { return this->adjustAndMap(op.oval, &op.paint); }
bsalomonac3aa242016-08-19 11:25:19 -0700422 // Tighter arc bounds?
423 Bounds bounds(const DrawArc& op) const { return this->adjustAndMap(op.oval, &op.paint); }
mtklein533eb782014-08-27 10:39:42 -0700424 Bounds bounds(const DrawRRect& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700425 return this->adjustAndMap(op.rrect.rect(), &op.paint);
426 }
mtklein533eb782014-08-27 10:39:42 -0700427 Bounds bounds(const DrawDRRect& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700428 return this->adjustAndMap(op.outer.rect(), &op.paint);
429 }
piotaixr65151752014-10-16 11:58:39 -0700430 Bounds bounds(const DrawImage& op) const {
mtkleinda574d12016-08-01 11:24:03 -0700431 const SkImage* image = op.image.get();
piotaixr65151752014-10-16 11:58:39 -0700432 SkRect rect = SkRect::MakeXYWH(op.left, op.top, image->width(), image->height());
mtklein62b67ae2014-08-18 11:10:37 -0700433
piotaixr65151752014-10-16 11:58:39 -0700434 return this->adjustAndMap(rect, op.paint);
435 }
msarettc573a402016-08-02 08:05:56 -0700436 Bounds bounds(const DrawImageLattice& op) const {
437 return this->adjustAndMap(op.dst, op.paint);
438 }
piotaixr65151752014-10-16 11:58:39 -0700439 Bounds bounds(const DrawImageRect& op) const {
440 return this->adjustAndMap(op.dst, op.paint);
441 }
reed4c21dc52015-06-25 12:32:03 -0700442 Bounds bounds(const DrawImageNine& op) const {
443 return this->adjustAndMap(op.dst, op.paint);
444 }
mtklein533eb782014-08-27 10:39:42 -0700445 Bounds bounds(const DrawPath& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700446 return op.path.isInverseFillType() ? fCurrentClipBounds
447 : this->adjustAndMap(op.path.getBounds(), &op.paint);
448 }
mtklein533eb782014-08-27 10:39:42 -0700449 Bounds bounds(const DrawPoints& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700450 SkRect dst;
451 dst.set(op.pts, op.count);
452
453 // Pad the bounding box a little to make sure hairline points' bounds aren't empty.
454 SkScalar stroke = SkMaxScalar(op.paint.getStrokeWidth(), 0.01f);
455 dst.outset(stroke/2, stroke/2);
456
457 return this->adjustAndMap(dst, &op.paint);
458 }
mtklein533eb782014-08-27 10:39:42 -0700459 Bounds bounds(const DrawPatch& op) const {
mtklein131a22b2014-08-25 14:16:15 -0700460 SkRect dst;
461 dst.set(op.cubics, SkPatchUtils::kNumCtrlPts);
462 return this->adjustAndMap(dst, &op.paint);
463 }
mtklein533eb782014-08-27 10:39:42 -0700464 Bounds bounds(const DrawVertices& op) const {
mtklein131a22b2014-08-25 14:16:15 -0700465 SkRect dst;
466 dst.set(op.vertices, op.vertexCount);
467 return this->adjustAndMap(dst, &op.paint);
468 }
mtklein64b4c782015-07-01 13:56:53 -0700469
reed71c3c762015-06-24 10:29:17 -0700470 Bounds bounds(const DrawAtlas& op) const {
471 if (op.cull) {
reed45561a02016-07-07 12:47:17 -0700472 // TODO: <reed> can we pass nullptr for the paint? Isn't cull already "correct"
473 // for the paint (by the caller)?
reed71c3c762015-06-24 10:29:17 -0700474 return this->adjustAndMap(*op.cull, op.paint);
475 } else {
476 return fCurrentClipBounds;
477 }
478 }
mtklein64b4c782015-07-01 13:56:53 -0700479
mtklein533eb782014-08-27 10:39:42 -0700480 Bounds bounds(const DrawPicture& op) const {
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700481 SkRect dst = op.picture->cullRect();
mtkleinaf579032014-12-01 11:03:37 -0800482 op.matrix.mapRect(&dst);
mtklein131a22b2014-08-25 14:16:15 -0700483 return this->adjustAndMap(dst, op.paint);
484 }
mtklein62b67ae2014-08-18 11:10:37 -0700485
vjiaoblack95302da2016-07-21 10:25:54 -0700486 Bounds bounds(const DrawShadowedPicture& op) const {
487 SkRect dst = op.picture->cullRect();
488 op.matrix.mapRect(&dst);
489 return this->adjustAndMap(dst, op.paint);
490 }
491
mtklein533eb782014-08-27 10:39:42 -0700492 Bounds bounds(const DrawPosText& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700493 const int N = op.paint.countText(op.text, op.byteLength);
494 if (N == 0) {
mtklein533eb782014-08-27 10:39:42 -0700495 return Bounds::MakeEmpty();
mtklein62b67ae2014-08-18 11:10:37 -0700496 }
497
498 SkRect dst;
mtklein937c9c72014-09-02 15:19:48 -0700499 dst.set(op.pos, N);
mtklein62b67ae2014-08-18 11:10:37 -0700500 AdjustTextForFontMetrics(&dst, op.paint);
501 return this->adjustAndMap(dst, &op.paint);
502 }
mtklein533eb782014-08-27 10:39:42 -0700503 Bounds bounds(const DrawPosTextH& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700504 const int N = op.paint.countText(op.text, op.byteLength);
505 if (N == 0) {
mtklein533eb782014-08-27 10:39:42 -0700506 return Bounds::MakeEmpty();
mtklein62b67ae2014-08-18 11:10:37 -0700507 }
508
509 SkScalar left = op.xpos[0], right = op.xpos[0];
510 for (int i = 1; i < N; i++) {
511 left = SkMinScalar(left, op.xpos[i]);
512 right = SkMaxScalar(right, op.xpos[i]);
513 }
514 SkRect dst = { left, op.y, right, op.y };
515 AdjustTextForFontMetrics(&dst, op.paint);
516 return this->adjustAndMap(dst, &op.paint);
517 }
mtklein533eb782014-08-27 10:39:42 -0700518 Bounds bounds(const DrawTextOnPath& op) const {
mtklein131a22b2014-08-25 14:16:15 -0700519 SkRect dst = op.path.getBounds();
520
mtklein9a5380d2014-12-16 06:31:01 -0800521 // Pad all sides by the maximum padding in any direction we'd normally apply.
mtklein131a22b2014-08-25 14:16:15 -0700522 SkRect pad = { 0, 0, 0, 0};
523 AdjustTextForFontMetrics(&pad, op.paint);
mtklein9a5380d2014-12-16 06:31:01 -0800524
525 // That maximum padding happens to always be the right pad today.
526 SkASSERT(pad.fLeft == -pad.fRight);
527 SkASSERT(pad.fTop == -pad.fBottom);
528 SkASSERT(pad.fRight > pad.fBottom);
529 dst.outset(pad.fRight, pad.fRight);
530
mtklein131a22b2014-08-25 14:16:15 -0700531 return this->adjustAndMap(dst, &op.paint);
532 }
533
reed45561a02016-07-07 12:47:17 -0700534 Bounds bounds(const DrawTextRSXform& op) const {
535 if (op.cull) {
536 return this->adjustAndMap(*op.cull, nullptr);
537 } else {
538 return fCurrentClipBounds;
539 }
540 }
541
mtklein533eb782014-08-27 10:39:42 -0700542 Bounds bounds(const DrawTextBlob& op) const {
mtklein131a22b2014-08-25 14:16:15 -0700543 SkRect dst = op.blob->bounds();
544 dst.offset(op.x, op.y);
mtklein131a22b2014-08-25 14:16:15 -0700545 return this->adjustAndMap(dst, &op.paint);
546 }
mtklein62b67ae2014-08-18 11:10:37 -0700547
reed6be2aa92014-11-18 11:08:05 -0800548 Bounds bounds(const DrawDrawable& op) const {
halcanary96fcdcc2015-08-27 07:41:13 -0700549 return this->adjustAndMap(op.worstCaseBounds, nullptr);
reed6be2aa92014-11-18 11:08:05 -0800550 }
551
reedf70b5312016-03-04 16:36:20 -0800552 Bounds bounds(const DrawAnnotation& op) const {
553 return this->adjustAndMap(op.rect, nullptr);
554 }
mtklein343a63d2016-03-22 11:46:53 -0700555
mtklein62b67ae2014-08-18 11:10:37 -0700556 static void AdjustTextForFontMetrics(SkRect* rect, const SkPaint& paint) {
mtklein9a5380d2014-12-16 06:31:01 -0800557#ifdef SK_DEBUG
558 SkRect correct = *rect;
559#endif
560 // crbug.com/373785 ~~> xPad = 4x yPad
561 // crbug.com/424824 ~~> bump yPad from 2x text size to 2.5x
562 const SkScalar yPad = 2.5f * paint.getTextSize(),
563 xPad = 4.0f * yPad;
564 rect->outset(xPad, yPad);
caryclark9a657fa2014-08-20 05:24:29 -0700565#ifdef SK_DEBUG
mtklein62b67ae2014-08-18 11:10:37 -0700566 SkPaint::FontMetrics metrics;
567 paint.getFontMetrics(&metrics);
mtklein9a5380d2014-12-16 06:31:01 -0800568 correct.fLeft += metrics.fXMin;
569 correct.fTop += metrics.fTop;
570 correct.fRight += metrics.fXMax;
571 correct.fBottom += metrics.fBottom;
mtkleind13291a2014-08-21 14:46:49 -0700572 // See skia:2862 for why we ignore small text sizes.
mtklein9a5380d2014-12-16 06:31:01 -0800573 SkASSERTF(paint.getTextSize() < 0.001f || rect->contains(correct),
mtkleined167ac2014-10-29 16:07:10 -0700574 "%f %f %f %f vs. %f %f %f %f\n",
mtklein9a5380d2014-12-16 06:31:01 -0800575 -xPad, -yPad, +xPad, +yPad,
mtkleined167ac2014-10-29 16:07:10 -0700576 metrics.fXMin, metrics.fTop, metrics.fXMax, metrics.fBottom);
mtkleina19afb42014-08-19 17:47:14 -0700577#endif
mtklein62b67ae2014-08-18 11:10:37 -0700578 }
579
mtklein479601b2014-08-18 08:45:33 -0700580 // Returns true if rect was meaningfully adjusted for the effects of paint,
581 // false if the paint could affect the rect in unknown ways.
582 static bool AdjustForPaint(const SkPaint* paint, SkRect* rect) {
mtkleina723b572014-08-15 11:49:49 -0700583 if (paint) {
584 if (paint->canComputeFastBounds()) {
mtklein479601b2014-08-18 08:45:33 -0700585 *rect = paint->computeFastBounds(*rect, rect);
586 return true;
mtkleina723b572014-08-15 11:49:49 -0700587 }
mtklein479601b2014-08-18 08:45:33 -0700588 return false;
589 }
590 return true;
591 }
592
mtklein8e393bf2014-10-01 12:48:58 -0700593 bool adjustForSaveLayerPaints(SkRect* rect, int savesToIgnore = 0) const {
594 for (int i = fSaveStack.count() - 1 - savesToIgnore; i >= 0; i--) {
senorblancodb64af32015-12-09 10:11:43 -0800595 SkMatrix inverse;
596 if (!fSaveStack[i].ctm.invert(&inverse)) {
597 return false;
598 }
599 inverse.mapRect(rect);
Mike Klein271a0302014-09-23 15:28:38 -0400600 if (!AdjustForPaint(fSaveStack[i].paint, rect)) {
601 return false;
602 }
senorblancodb64af32015-12-09 10:11:43 -0800603 fSaveStack[i].ctm.mapRect(rect);
Mike Klein271a0302014-09-23 15:28:38 -0400604 }
605 return true;
606 }
607
mtkleinc6ad06a2015-08-19 09:51:00 -0700608 const int fNumRecords;
mtkleina723b572014-08-15 11:49:49 -0700609
robertphillips4d52afe2014-11-03 08:19:44 -0800610 // We do not guarantee anything for operations outside of the cull rect
611 const SkRect fCullRect;
612
mtklein533eb782014-08-27 10:39:42 -0700613 // Conservative identity-space bounds for each op in the SkRecord.
mtklein40732b32015-10-24 07:45:47 -0700614 Bounds* fBounds;
mtkleina723b572014-08-15 11:49:49 -0700615
616 // We walk fCurrentOp through the SkRecord, as we go using updateCTM()
617 // and updateClipBounds() to maintain the exact CTM (fCTM) and conservative
mtklein533eb782014-08-27 10:39:42 -0700618 // identity-space bounds of the current clip (fCurrentClipBounds).
mtkleinc6ad06a2015-08-19 09:51:00 -0700619 int fCurrentOp;
mtkleine9d20522015-11-19 12:08:24 -0800620 SkMatrix fCTM;
mtklein533eb782014-08-27 10:39:42 -0700621 Bounds fCurrentClipBounds;
mtkleina723b572014-08-15 11:49:49 -0700622
623 // Used to track the bounds of Save/Restore blocks and the control ops inside them.
mtklein828ce1f2014-08-13 12:58:45 -0700624 SkTDArray<SaveBounds> fSaveStack;
mtkleinc6ad06a2015-08-19 09:51:00 -0700625 SkTDArray<int> fControlIndices;
mtklein5ad6ee12014-08-11 08:08:43 -0700626};
627
commit-bot@chromium.org27f6b0d2014-05-09 14:59:29 +0000628} // namespace SkRecords
mtklein5ad6ee12014-08-11 08:08:43 -0700629
mtklein40732b32015-10-24 07:45:47 -0700630void SkRecordFillBounds(const SkRect& cullRect, const SkRecord& record, SkRect bounds[]) {
631 SkRecords::FillBounds visitor(cullRect, record, bounds);
mtkleinc6ad06a2015-08-19 09:51:00 -0700632 for (int curOp = 0; curOp < record.count(); curOp++) {
robertphillips4e8e3422014-11-12 06:46:08 -0800633 visitor.setCurrentOp(curOp);
mtklein343a63d2016-03-22 11:46:53 -0700634 record.visit(curOp, visitor);
robertphillips4e8e3422014-11-12 06:46:08 -0800635 }
mtklein40732b32015-10-24 07:45:47 -0700636 visitor.cleanUp();
mtklein5ad6ee12014-08-11 08:08:43 -0700637}
robertphillips4e8e3422014-11-12 06:46:08 -0800638