blob: 5981245c018700e8ed969e7abc75e1d76c63b7ba [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,
13 const SkBBoxHierarchy* bbh,
14 SkDrawPictureCallback* callback) {
Mike Kleinc11530e2014-06-24 11:29:06 -040015 SkAutoCanvasRestore saveRestore(canvas, true /*save now, restore at exit*/);
mtklein5ad6ee12014-08-11 08:08:43 -070016
bsalomon49f085d2014-09-05 13:34:00 -070017 if (bbh) {
mtklein5ad6ee12014-08-11 08:08:43 -070018 // Draw only ops that affect pixels in the canvas's current clip.
mtklein3e8232b2014-08-18 13:39:11 -070019 // The SkRecord and BBH were recorded in identity space. This canvas
20 // is not necessarily in that same space. getClipBounds() returns us
21 // this canvas' clip bounds transformed back into identity space, which
22 // lets us query the BBH.
mtklein533eb782014-08-27 10:39:42 -070023 SkRect query = { 0, 0, 0, 0 };
24 (void)canvas->getClipBounds(&query);
mtklein3e8232b2014-08-18 13:39:11 -070025
mtklein6bd41962014-10-02 07:41:56 -070026 SkTDArray<unsigned> ops;
mtkleina723b572014-08-15 11:49:49 -070027 bbh->search(query, &ops);
mtklein5ad6ee12014-08-11 08:08:43 -070028
mtklein5ad6ee12014-08-11 08:08:43 -070029 SkRecords::Draw draw(canvas);
30 for (int i = 0; i < ops.count(); i++) {
bsalomon49f085d2014-09-05 13:34:00 -070031 if (callback && callback->abortDrawing()) {
mtklein5ad6ee12014-08-11 08:08:43 -070032 return;
33 }
mtklein6bd41962014-10-02 07:41:56 -070034 record.visit<void>(ops[i], draw);
mtklein5ad6ee12014-08-11 08:08:43 -070035 }
36 } else {
37 // Draw all ops.
mtklein00f30bd2014-09-02 12:03:31 -070038 SkRecords::Draw draw(canvas);
39 for (unsigned i = 0; i < record.count(); i++) {
bsalomon49f085d2014-09-05 13:34:00 -070040 if (callback && callback->abortDrawing()) {
mtklein5ad6ee12014-08-11 08:08:43 -070041 return;
42 }
mtklein00f30bd2014-09-02 12:03:31 -070043 record.visit<void>(i, draw);
mtklein5ad6ee12014-08-11 08:08:43 -070044 }
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000045 }
commit-bot@chromium.org27f6b0d2014-05-09 14:59:29 +000046}
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000047
mtklein00f30bd2014-09-02 12:03:31 -070048void SkRecordPartialDraw(const SkRecord& record,
49 SkCanvas* canvas,
50 const SkRect& clearRect,
robertphillips4815fe52014-09-16 10:32:43 -070051 unsigned start, unsigned stop,
52 const SkMatrix& initialCTM) {
mtklein00f30bd2014-09-02 12:03:31 -070053 SkAutoCanvasRestore saveRestore(canvas, true /*save now, restore at exit*/);
54
55 stop = SkTMin(stop, record.count());
robertphillips4815fe52014-09-16 10:32:43 -070056 SkRecords::PartialDraw draw(canvas, clearRect, initialCTM);
mtklein00f30bd2014-09-02 12:03:31 -070057 for (unsigned i = start; i < stop; i++) {
58 record.visit<void>(i, draw);
59 }
60}
61
commit-bot@chromium.org27f6b0d2014-05-09 14:59:29 +000062namespace SkRecords {
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000063
mtklein7cdc1ee2014-07-07 10:41:04 -070064// FIXME: SkBitmaps are stateful, so we need to copy them to play back in multiple threads.
65static SkBitmap shallow_copy(const SkBitmap& bitmap) {
66 return bitmap;
67}
68
commit-bot@chromium.org2e0c32a2014-04-28 16:19:45 +000069// NoOps draw nothing.
commit-bot@chromium.org27f6b0d2014-05-09 14:59:29 +000070template <> void Draw::draw(const NoOp&) {}
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000071
commit-bot@chromium.org27f6b0d2014-05-09 14:59:29 +000072#define DRAW(T, call) template <> void Draw::draw(const T& r) { fCanvas->call; }
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000073DRAW(Restore, restore());
Florin Malita5f6102d2014-06-30 10:13:28 -040074DRAW(Save, save());
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000075DRAW(SaveLayer, saveLayer(r.bounds, r.paint, r.flags));
76DRAW(PopCull, popCull());
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000077DRAW(PushCull, pushCull(r.rect));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000078DRAW(Clear, clear(r.color));
commit-bot@chromium.org99bd7d82014-05-19 15:51:12 +000079DRAW(SetMatrix, setMatrix(SkMatrix::Concat(fInitialCTM, r.matrix)));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000080
81DRAW(ClipPath, clipPath(r.path, r.op, r.doAA));
82DRAW(ClipRRect, clipRRect(r.rrect, r.op, r.doAA));
83DRAW(ClipRect, clipRect(r.rect, r.op, r.doAA));
84DRAW(ClipRegion, clipRegion(r.region, r.op));
85
mtklein5f0e8222014-08-22 11:44:26 -070086DRAW(BeginCommentGroup, beginCommentGroup(r.description));
87DRAW(AddComment, addComment(r.key, r.value));
88DRAW(EndCommentGroup, endCommentGroup());
89
mtklein7cdc1ee2014-07-07 10:41:04 -070090DRAW(DrawBitmap, drawBitmap(shallow_copy(r.bitmap), r.left, r.top, r.paint));
91DRAW(DrawBitmapMatrix, drawBitmapMatrix(shallow_copy(r.bitmap), r.matrix, r.paint));
92DRAW(DrawBitmapNine, drawBitmapNine(shallow_copy(r.bitmap), r.center, r.dst, r.paint));
93DRAW(DrawBitmapRectToRect,
94 drawBitmapRectToRect(shallow_copy(r.bitmap), r.src, r.dst, r.paint, r.flags));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000095DRAW(DrawDRRect, drawDRRect(r.outer, r.inner, r.paint));
piotaixr65151752014-10-16 11:58:39 -070096DRAW(DrawImage, drawImage(r.image, r.left, r.top, r.paint));
97DRAW(DrawImageRect, drawImageRect(r.image, r.src, r.dst, r.paint));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000098DRAW(DrawOval, drawOval(r.oval, r.paint));
99DRAW(DrawPaint, drawPaint(r.paint));
100DRAW(DrawPath, drawPath(r.path, r.paint));
mtklein9b222a52014-09-18 11:16:31 -0700101DRAW(DrawPatch, drawPatch(r.cubics, r.colors, r.texCoords, r.xmode, r.paint));
reedd5fa1a42014-08-09 11:08:05 -0700102DRAW(DrawPicture, drawPicture(r.picture, r.matrix, r.paint));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +0000103DRAW(DrawPoints, drawPoints(r.mode, r.count, r.pts, r.paint));
104DRAW(DrawPosText, drawPosText(r.text, r.byteLength, r.pos, r.paint));
105DRAW(DrawPosTextH, drawPosTextH(r.text, r.byteLength, r.xpos, r.y, r.paint));
106DRAW(DrawRRect, drawRRect(r.rrect, r.paint));
107DRAW(DrawRect, drawRect(r.rect, r.paint));
mtklein7cdc1ee2014-07-07 10:41:04 -0700108DRAW(DrawSprite, drawSprite(shallow_copy(r.bitmap), r.left, r.top, r.paint));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +0000109DRAW(DrawText, drawText(r.text, r.byteLength, r.x, r.y, r.paint));
fmalita00d5c2c2014-08-21 08:53:26 -0700110DRAW(DrawTextBlob, drawTextBlob(r.blob, r.x, r.y, r.paint));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +0000111DRAW(DrawTextOnPath, drawTextOnPath(r.text, r.byteLength, r.path, r.matrix, r.paint));
112DRAW(DrawVertices, drawVertices(r.vmode, r.vertexCount, r.vertices, r.texs, r.colors,
113 r.xmode.get(), r.indices, r.indexCount, r.paint));
mtklein29dfaa82014-09-04 14:12:44 -0700114DRAW(DrawData, drawData(r.data, r.length));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +0000115#undef DRAW
116
mtklein5ad6ee12014-08-11 08:08:43 -0700117
mtklein0a528c12014-10-02 13:03:53 -0700118// This looks silly, I know. Why not just use SkRect::MakeLargest()?
119// In practice, this is well large enough, and it has a few extra advantages:
120// it fits in an SkIRect, and we can munge it a little in both SkRect and
121// SKIRect space without worrying about overflow.
122static const SkRect kUnbounded = { -2e9f, -2e9f, 2e9f, 2e9f };
123
124
mtklein5ad6ee12014-08-11 08:08:43 -0700125// This is an SkRecord visitor that fills an SkBBoxHierarchy.
mtklein828ce1f2014-08-13 12:58:45 -0700126//
127// The interesting part here is how to calculate bounds for ops which don't
128// have intrinsic bounds. What is the bounds of a Save or a Translate?
129//
130// We answer this by thinking about a particular definition of bounds: if I
131// don't execute this op, pixels in this rectangle might draw incorrectly. So
132// the bounds of a Save, a Translate, a Restore, etc. are the union of the
133// bounds of Draw* ops that they might have an effect on. For any given
134// Save/Restore block, the bounds of the Save, the Restore, and any other
135// non-drawing ("control") ops inside are exactly the union of the bounds of
136// the drawing ops inside that block.
137//
138// To implement this, we keep a stack of active Save blocks. As we consume ops
139// inside the Save/Restore block, drawing ops are unioned with the bounds of
140// the block, and control ops are stashed away for later. When we finish the
141// block with a Restore, our bounds are complete, and we go back and fill them
142// in for all the control ops we stashed away.
mtklein5ad6ee12014-08-11 08:08:43 -0700143class FillBounds : SkNoncopyable {
144public:
mtklein828ce1f2014-08-13 12:58:45 -0700145 FillBounds(const SkRecord& record, SkBBoxHierarchy* bbh) : fBounds(record.count()) {
146 // Calculate bounds for all ops. This won't go quite in order, so we'll need
147 // to store the bounds separately then feed them in to the BBH later in order.
mtklein6332f1d2014-08-19 07:09:40 -0700148 fCTM = &SkMatrix::I();
mtklein0a528c12014-10-02 13:03:53 -0700149 fCurrentClipBounds = kUnbounded;
mtklein828ce1f2014-08-13 12:58:45 -0700150 for (fCurrentOp = 0; fCurrentOp < record.count(); fCurrentOp++) {
151 record.visit<void>(fCurrentOp, *this);
152 }
mtklein5ad6ee12014-08-11 08:08:43 -0700153
mtklein828ce1f2014-08-13 12:58:45 -0700154 // If we have any lingering unpaired Saves, simulate restores to make
155 // sure all ops in those Save blocks have their bounds calculated.
156 while (!fSaveStack.isEmpty()) {
157 this->popSaveBlock();
158 }
159
160 // Any control ops not part of any Save/Restore block draw everywhere.
161 while (!fControlIndices.isEmpty()) {
mtklein0a528c12014-10-02 13:03:53 -0700162 this->popControl(kUnbounded);
mtklein828ce1f2014-08-13 12:58:45 -0700163 }
164
165 // Finally feed all stored bounds into the BBH. They'll be returned in this order.
bsalomon49f085d2014-09-05 13:34:00 -0700166 SkASSERT(bbh);
mtklein4477c3c2014-10-27 10:27:10 -0700167 bbh->insert(&fBounds, record.count());
mtklein828ce1f2014-08-13 12:58:45 -0700168 }
mtklein5ad6ee12014-08-11 08:08:43 -0700169
mtkleina723b572014-08-15 11:49:49 -0700170 template <typename T> void operator()(const T& op) {
171 this->updateCTM(op);
172 this->updateClipBounds(op);
173 this->trackBounds(op);
mtklein5ad6ee12014-08-11 08:08:43 -0700174 }
175
176private:
mtklein533eb782014-08-27 10:39:42 -0700177 // In this file, SkRect are in local coordinates, Bounds are translated back to identity space.
178 typedef SkRect Bounds;
179
mtklein828ce1f2014-08-13 12:58:45 -0700180 struct SaveBounds {
mtkleina723b572014-08-15 11:49:49 -0700181 int controlOps; // Number of control ops in this Save block, including the Save.
mtklein533eb782014-08-27 10:39:42 -0700182 Bounds bounds; // Bounds of everything in the block.
mtkleina723b572014-08-15 11:49:49 -0700183 const SkPaint* paint; // Unowned. If set, adjusts the bounds of all ops in this block.
mtklein828ce1f2014-08-13 12:58:45 -0700184 };
185
mtklein8e393bf2014-10-01 12:48:58 -0700186 // Only Restore and SetMatrix change the CTM.
187 template <typename T> void updateCTM(const T&) {}
mtklein6332f1d2014-08-19 07:09:40 -0700188 void updateCTM(const Restore& op) { fCTM = &op.matrix; }
189 void updateCTM(const SetMatrix& op) { fCTM = &op.matrix; }
mtkleina723b572014-08-15 11:49:49 -0700190
mtklein8e393bf2014-10-01 12:48:58 -0700191 // Most ops don't change the clip.
192 template <typename T> void updateClipBounds(const T&) {}
Mike Klein271a0302014-09-23 15:28:38 -0400193
mtklein8e393bf2014-10-01 12:48:58 -0700194 // Clip{Path,RRect,Rect,Region} obviously change the clip. They all know their bounds already.
195 void updateClipBounds(const ClipPath& op) { this->updateClipBoundsForClipOp(op.devBounds); }
196 void updateClipBounds(const ClipRRect& op) { this->updateClipBoundsForClipOp(op.devBounds); }
197 void updateClipBounds(const ClipRect& op) { this->updateClipBoundsForClipOp(op.devBounds); }
198 void updateClipBounds(const ClipRegion& op) { this->updateClipBoundsForClipOp(op.devBounds); }
Mike Klein271a0302014-09-23 15:28:38 -0400199
mtklein8e393bf2014-10-01 12:48:58 -0700200 // The bounds of clip ops need to be adjusted for the paints of saveLayers they're inside.
201 void updateClipBoundsForClipOp(const SkIRect& devBounds) {
202 Bounds clip = SkRect::Make(devBounds);
Mike Klein271a0302014-09-23 15:28:38 -0400203 // We don't call adjustAndMap() because as its last step it would intersect the adjusted
204 // clip bounds with the previous clip, exactly what we can't do when the clip grows.
mtklein0a528c12014-10-02 13:03:53 -0700205 fCurrentClipBounds = this->adjustForSaveLayerPaints(&clip) ? clip : kUnbounded;
Mike Klein271a0302014-09-23 15:28:38 -0400206 }
207
mtklein8e393bf2014-10-01 12:48:58 -0700208 // Restore holds the devBounds for the clip after the {save,saveLayer}/restore block completes.
209 void updateClipBounds(const Restore& op) {
210 // This is just like the clip ops above, but we need to skip the effects (if any) of our
211 // paired saveLayer (if it is one); it has not yet been popped off the save stack. Our
212 // devBounds reflect the state of the world after the saveLayer/restore block is done,
213 // so they are not affected by the saveLayer's paint.
214 const int kSavesToIgnore = 1;
215 Bounds clip = SkRect::Make(op.devBounds);
216 fCurrentClipBounds =
mtklein0a528c12014-10-02 13:03:53 -0700217 this->adjustForSaveLayerPaints(&clip, kSavesToIgnore) ? clip : kUnbounded;
mtklein8e393bf2014-10-01 12:48:58 -0700218 }
219
Mike Klein271a0302014-09-23 15:28:38 -0400220 // We also take advantage of SaveLayer bounds when present to further cut the clip down.
mtkleina723b572014-08-15 11:49:49 -0700221 void updateClipBounds(const SaveLayer& op) {
222 if (op.bounds) {
Mike Klein271a0302014-09-23 15:28:38 -0400223 // adjustAndMap() intersects these layer bounds with the previous clip for us.
224 fCurrentClipBounds = this->adjustAndMap(*op.bounds, op.paint);
mtkleina723b572014-08-15 11:49:49 -0700225 }
226 }
mtklein6cfa73a2014-08-13 13:33:49 -0700227
mtklein828ce1f2014-08-13 12:58:45 -0700228 // The bounds of these ops must be calculated when we hit the Restore
229 // from the bounds of the ops in the same Save block.
mtkleina723b572014-08-15 11:49:49 -0700230 void trackBounds(const Save&) { this->pushSaveBlock(NULL); }
mtkleina723b572014-08-15 11:49:49 -0700231 void trackBounds(const SaveLayer& op) { this->pushSaveBlock(op.paint); }
232 void trackBounds(const Restore&) { fBounds[fCurrentOp] = this->popSaveBlock(); }
mtklein828ce1f2014-08-13 12:58:45 -0700233
mtklein68199a22014-08-25 13:49:29 -0700234 void trackBounds(const SetMatrix&) { this->pushControl(); }
235 void trackBounds(const ClipRect&) { this->pushControl(); }
236 void trackBounds(const ClipRRect&) { this->pushControl(); }
237 void trackBounds(const ClipPath&) { this->pushControl(); }
238 void trackBounds(const ClipRegion&) { this->pushControl(); }
239 void trackBounds(const PushCull&) { this->pushControl(); }
240 void trackBounds(const PopCull&) { this->pushControl(); }
241 void trackBounds(const BeginCommentGroup&) { this->pushControl(); }
242 void trackBounds(const AddComment&) { this->pushControl(); }
243 void trackBounds(const EndCommentGroup&) { this->pushControl(); }
mtklein29dfaa82014-09-04 14:12:44 -0700244 void trackBounds(const DrawData&) { this->pushControl(); }
mtklein828ce1f2014-08-13 12:58:45 -0700245
246 // For all other ops, we can calculate and store the bounds directly now.
247 template <typename T> void trackBounds(const T& op) {
248 fBounds[fCurrentOp] = this->bounds(op);
249 this->updateSaveBounds(fBounds[fCurrentOp]);
mtklein5ad6ee12014-08-11 08:08:43 -0700250 }
251
mtkleina723b572014-08-15 11:49:49 -0700252 void pushSaveBlock(const SkPaint* paint) {
mtklein828ce1f2014-08-13 12:58:45 -0700253 // Starting a new Save block. Push a new entry to represent that.
mtklein533eb782014-08-27 10:39:42 -0700254 SaveBounds sb = { 0, Bounds::MakeEmpty(), paint };
mtklein828ce1f2014-08-13 12:58:45 -0700255 fSaveStack.push(sb);
256 this->pushControl();
257 }
258
mtkleind910f542014-08-22 09:06:34 -0700259 static bool PaintMayAffectTransparentBlack(const SkPaint* paint) {
dneto327f9052014-09-15 10:53:16 -0700260 if (paint) {
261 // FIXME: this is very conservative
262 if (paint->getImageFilter() || paint->getColorFilter()) {
263 return true;
264 }
265
266 // Unusual Xfermodes require us to process a saved layer
267 // even with operations outisde the clip.
268 // For example, DstIn is used by masking layers.
269 // https://code.google.com/p/skia/issues/detail?id=1291
270 // https://crbug.com/401593
271 SkXfermode* xfermode = paint->getXfermode();
272 SkXfermode::Mode mode;
273 // SrcOver is ok, and is also the common case with a NULL xfermode.
274 // So we should make that the fast path and bypass the mode extraction
275 // and test.
276 if (xfermode && xfermode->asMode(&mode)) {
277 switch (mode) {
278 // For each of the following transfer modes, if the source
279 // alpha is zero (our transparent black), the resulting
280 // blended alpha is not necessarily equal to the original
281 // destination alpha.
282 case SkXfermode::kClear_Mode:
283 case SkXfermode::kSrc_Mode:
284 case SkXfermode::kSrcIn_Mode:
285 case SkXfermode::kDstIn_Mode:
286 case SkXfermode::kSrcOut_Mode:
287 case SkXfermode::kDstATop_Mode:
288 case SkXfermode::kModulate_Mode:
289 return true;
290 break;
291 default:
292 break;
293 }
294 }
295 }
296 return false;
mtkleind910f542014-08-22 09:06:34 -0700297 }
298
mtklein533eb782014-08-27 10:39:42 -0700299 Bounds popSaveBlock() {
mtklein828ce1f2014-08-13 12:58:45 -0700300 // We're done the Save block. Apply the block's bounds to all control ops inside it.
301 SaveBounds sb;
302 fSaveStack.pop(&sb);
mtkleind910f542014-08-22 09:06:34 -0700303
304 // If the paint affects transparent black, we can't trust any of our calculated bounds.
mtklein533eb782014-08-27 10:39:42 -0700305 const Bounds& bounds =
mtkleind910f542014-08-22 09:06:34 -0700306 PaintMayAffectTransparentBlack(sb.paint) ? fCurrentClipBounds : sb.bounds;
307
mtklein828ce1f2014-08-13 12:58:45 -0700308 while (sb.controlOps --> 0) {
mtkleind910f542014-08-22 09:06:34 -0700309 this->popControl(bounds);
mtklein828ce1f2014-08-13 12:58:45 -0700310 }
311
312 // This whole Save block may be part another Save block.
mtkleind910f542014-08-22 09:06:34 -0700313 this->updateSaveBounds(bounds);
mtklein828ce1f2014-08-13 12:58:45 -0700314
315 // If called from a real Restore (not a phony one for balance), it'll need the bounds.
mtkleind910f542014-08-22 09:06:34 -0700316 return bounds;
mtklein828ce1f2014-08-13 12:58:45 -0700317 }
318
319 void pushControl() {
320 fControlIndices.push(fCurrentOp);
321 if (!fSaveStack.isEmpty()) {
322 fSaveStack.top().controlOps++;
323 }
324 }
325
mtklein533eb782014-08-27 10:39:42 -0700326 void popControl(const Bounds& bounds) {
mtklein828ce1f2014-08-13 12:58:45 -0700327 fBounds[fControlIndices.top()] = bounds;
328 fControlIndices.pop();
329 }
330
mtklein533eb782014-08-27 10:39:42 -0700331 void updateSaveBounds(const Bounds& bounds) {
mtklein828ce1f2014-08-13 12:58:45 -0700332 // If we're in a Save block, expand its bounds to cover these bounds too.
333 if (!fSaveStack.isEmpty()) {
334 fSaveStack.top().bounds.join(bounds);
335 }
336 }
337
mtklein131a22b2014-08-25 14:16:15 -0700338 // FIXME: this method could use better bounds
mtklein533eb782014-08-27 10:39:42 -0700339 Bounds bounds(const DrawText&) const { return fCurrentClipBounds; }
mtklein68199a22014-08-25 13:49:29 -0700340
mtklein0a528c12014-10-02 13:03:53 -0700341 Bounds bounds(const Clear&) const { return kUnbounded; } // Ignores the clip.
mtklein533eb782014-08-27 10:39:42 -0700342 Bounds bounds(const DrawPaint&) const { return fCurrentClipBounds; }
343 Bounds bounds(const NoOp&) const { return Bounds::MakeEmpty(); } // NoOps don't draw.
mtklein828ce1f2014-08-13 12:58:45 -0700344
mtklein533eb782014-08-27 10:39:42 -0700345 Bounds bounds(const DrawSprite& op) const {
mtklein131a22b2014-08-25 14:16:15 -0700346 const SkBitmap& bm = op.bitmap;
mtklein533eb782014-08-27 10:39:42 -0700347 return Bounds::MakeXYWH(op.left, op.top, bm.width(), bm.height()); // Ignores the matrix.
mtklein131a22b2014-08-25 14:16:15 -0700348 }
349
mtklein533eb782014-08-27 10:39:42 -0700350 Bounds bounds(const DrawRect& op) const { return this->adjustAndMap(op.rect, &op.paint); }
351 Bounds bounds(const DrawOval& op) const { return this->adjustAndMap(op.oval, &op.paint); }
352 Bounds bounds(const DrawRRect& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700353 return this->adjustAndMap(op.rrect.rect(), &op.paint);
354 }
mtklein533eb782014-08-27 10:39:42 -0700355 Bounds bounds(const DrawDRRect& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700356 return this->adjustAndMap(op.outer.rect(), &op.paint);
357 }
piotaixr65151752014-10-16 11:58:39 -0700358 Bounds bounds(const DrawImage& op) const {
359 const SkImage* image = op.image;
360 SkRect rect = SkRect::MakeXYWH(op.left, op.top, image->width(), image->height());
mtklein62b67ae2014-08-18 11:10:37 -0700361
piotaixr65151752014-10-16 11:58:39 -0700362 return this->adjustAndMap(rect, op.paint);
363 }
364 Bounds bounds(const DrawImageRect& op) const {
365 return this->adjustAndMap(op.dst, op.paint);
366 }
mtklein533eb782014-08-27 10:39:42 -0700367 Bounds bounds(const DrawBitmapRectToRect& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700368 return this->adjustAndMap(op.dst, op.paint);
369 }
mtklein533eb782014-08-27 10:39:42 -0700370 Bounds bounds(const DrawBitmapNine& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700371 return this->adjustAndMap(op.dst, op.paint);
372 }
mtklein533eb782014-08-27 10:39:42 -0700373 Bounds bounds(const DrawBitmap& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700374 const SkBitmap& bm = op.bitmap;
375 return this->adjustAndMap(SkRect::MakeXYWH(op.left, op.top, bm.width(), bm.height()),
376 op.paint);
377 }
mtklein533eb782014-08-27 10:39:42 -0700378 Bounds bounds(const DrawBitmapMatrix& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700379 const SkBitmap& bm = op.bitmap;
380 SkRect dst = SkRect::MakeWH(bm.width(), bm.height());
381 op.matrix.mapRect(&dst);
382 return this->adjustAndMap(dst, op.paint);
383 }
384
mtklein533eb782014-08-27 10:39:42 -0700385 Bounds bounds(const DrawPath& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700386 return op.path.isInverseFillType() ? fCurrentClipBounds
387 : this->adjustAndMap(op.path.getBounds(), &op.paint);
388 }
mtklein533eb782014-08-27 10:39:42 -0700389 Bounds bounds(const DrawPoints& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700390 SkRect dst;
391 dst.set(op.pts, op.count);
392
393 // Pad the bounding box a little to make sure hairline points' bounds aren't empty.
394 SkScalar stroke = SkMaxScalar(op.paint.getStrokeWidth(), 0.01f);
395 dst.outset(stroke/2, stroke/2);
396
397 return this->adjustAndMap(dst, &op.paint);
398 }
mtklein533eb782014-08-27 10:39:42 -0700399 Bounds bounds(const DrawPatch& op) const {
mtklein131a22b2014-08-25 14:16:15 -0700400 SkRect dst;
401 dst.set(op.cubics, SkPatchUtils::kNumCtrlPts);
402 return this->adjustAndMap(dst, &op.paint);
403 }
mtklein533eb782014-08-27 10:39:42 -0700404 Bounds bounds(const DrawVertices& op) const {
mtklein131a22b2014-08-25 14:16:15 -0700405 SkRect dst;
406 dst.set(op.vertices, op.vertexCount);
407 return this->adjustAndMap(dst, &op.paint);
408 }
409
mtklein533eb782014-08-27 10:39:42 -0700410 Bounds bounds(const DrawPicture& op) const {
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700411 SkRect dst = op.picture->cullRect();
mtklein131a22b2014-08-25 14:16:15 -0700412 if (op.matrix) {
413 op.matrix->mapRect(&dst);
414 }
415 return this->adjustAndMap(dst, op.paint);
416 }
mtklein62b67ae2014-08-18 11:10:37 -0700417
mtklein533eb782014-08-27 10:39:42 -0700418 Bounds bounds(const DrawPosText& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700419 const int N = op.paint.countText(op.text, op.byteLength);
420 if (N == 0) {
mtklein533eb782014-08-27 10:39:42 -0700421 return Bounds::MakeEmpty();
mtklein62b67ae2014-08-18 11:10:37 -0700422 }
423
424 SkRect dst;
mtklein937c9c72014-09-02 15:19:48 -0700425 dst.set(op.pos, N);
mtklein62b67ae2014-08-18 11:10:37 -0700426 AdjustTextForFontMetrics(&dst, op.paint);
427 return this->adjustAndMap(dst, &op.paint);
428 }
mtklein533eb782014-08-27 10:39:42 -0700429 Bounds bounds(const DrawPosTextH& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700430 const int N = op.paint.countText(op.text, op.byteLength);
431 if (N == 0) {
mtklein533eb782014-08-27 10:39:42 -0700432 return Bounds::MakeEmpty();
mtklein62b67ae2014-08-18 11:10:37 -0700433 }
434
435 SkScalar left = op.xpos[0], right = op.xpos[0];
436 for (int i = 1; i < N; i++) {
437 left = SkMinScalar(left, op.xpos[i]);
438 right = SkMaxScalar(right, op.xpos[i]);
439 }
440 SkRect dst = { left, op.y, right, op.y };
441 AdjustTextForFontMetrics(&dst, op.paint);
442 return this->adjustAndMap(dst, &op.paint);
443 }
mtklein533eb782014-08-27 10:39:42 -0700444 Bounds bounds(const DrawTextOnPath& op) const {
mtklein131a22b2014-08-25 14:16:15 -0700445 SkRect dst = op.path.getBounds();
446
mtkleined167ac2014-10-29 16:07:10 -0700447 // Pad all sides by the maximum padding in any direction we'd normally apply.
mtklein131a22b2014-08-25 14:16:15 -0700448 SkRect pad = { 0, 0, 0, 0};
449 AdjustTextForFontMetrics(&pad, op.paint);
mtkleined167ac2014-10-29 16:07:10 -0700450
451 // That maximum padding happens to always be the right pad today.
452 SkASSERT(pad.fLeft == -pad.fRight);
453 SkASSERT(pad.fTop == -pad.fBottom);
454 SkASSERT(pad.fRight > pad.fBottom);
455 dst.outset(pad.fRight, pad.fRight);
mtklein131a22b2014-08-25 14:16:15 -0700456
457 return this->adjustAndMap(dst, &op.paint);
458 }
459
mtklein533eb782014-08-27 10:39:42 -0700460 Bounds bounds(const DrawTextBlob& op) const {
mtklein131a22b2014-08-25 14:16:15 -0700461 SkRect dst = op.blob->bounds();
462 dst.offset(op.x, op.y);
mtklein131a22b2014-08-25 14:16:15 -0700463 return this->adjustAndMap(dst, &op.paint);
464 }
mtklein62b67ae2014-08-18 11:10:37 -0700465
466 static void AdjustTextForFontMetrics(SkRect* rect, const SkPaint& paint) {
mtkleined167ac2014-10-29 16:07:10 -0700467#ifdef SK_DEBUG
468 SkRect correct = *rect;
469#endif
470 // crbug.com/373785 ~~> xPad = 4x yPad
471 // crbug.com/424824 ~~> bump yPad from 2x text size to 2.5x
472 const SkScalar yPad = 2.5f * paint.getTextSize(),
473 xPad = 4.0f * yPad;
474 rect->outset(xPad, yPad);
caryclark9a657fa2014-08-20 05:24:29 -0700475#ifdef SK_DEBUG
mtklein62b67ae2014-08-18 11:10:37 -0700476 SkPaint::FontMetrics metrics;
477 paint.getFontMetrics(&metrics);
mtkleined167ac2014-10-29 16:07:10 -0700478 correct.fLeft += metrics.fXMin;
479 correct.fTop += metrics.fTop;
480 correct.fRight += metrics.fXMax;
481 correct.fBottom += metrics.fBottom;
mtkleind13291a2014-08-21 14:46:49 -0700482 // See skia:2862 for why we ignore small text sizes.
mtkleined167ac2014-10-29 16:07:10 -0700483 SkASSERTF(paint.getTextSize() < 0.001f || rect->contains(correct),
484 "%f %f %f %f vs. %f %f %f %f\n",
485 -xPad, -yPad, +xPad, +yPad,
486 metrics.fXMin, metrics.fTop, metrics.fXMax, metrics.fBottom);
mtkleina19afb42014-08-19 17:47:14 -0700487#endif
mtklein62b67ae2014-08-18 11:10:37 -0700488 }
489
mtklein479601b2014-08-18 08:45:33 -0700490 // Returns true if rect was meaningfully adjusted for the effects of paint,
491 // false if the paint could affect the rect in unknown ways.
492 static bool AdjustForPaint(const SkPaint* paint, SkRect* rect) {
mtkleina723b572014-08-15 11:49:49 -0700493 if (paint) {
494 if (paint->canComputeFastBounds()) {
mtklein479601b2014-08-18 08:45:33 -0700495 *rect = paint->computeFastBounds(*rect, rect);
496 return true;
mtkleina723b572014-08-15 11:49:49 -0700497 }
mtklein479601b2014-08-18 08:45:33 -0700498 return false;
499 }
500 return true;
501 }
502
mtklein8e393bf2014-10-01 12:48:58 -0700503 bool adjustForSaveLayerPaints(SkRect* rect, int savesToIgnore = 0) const {
504 for (int i = fSaveStack.count() - 1 - savesToIgnore; i >= 0; i--) {
Mike Klein271a0302014-09-23 15:28:38 -0400505 if (!AdjustForPaint(fSaveStack[i].paint, rect)) {
506 return false;
507 }
508 }
509 return true;
510 }
511
mtklein533eb782014-08-27 10:39:42 -0700512 // Adjust rect for all paints that may affect its geometry, then map it to identity space.
513 Bounds adjustAndMap(SkRect rect, const SkPaint* paint) const {
mtklein479601b2014-08-18 08:45:33 -0700514 // Inverted rectangles really confuse our BBHs.
515 rect.sort();
516
517 // Adjust the rect for its own paint.
518 if (!AdjustForPaint(paint, &rect)) {
519 // The paint could do anything to our bounds. The only safe answer is the current clip.
520 return fCurrentClipBounds;
mtkleina723b572014-08-15 11:49:49 -0700521 }
522
523 // Adjust rect for all the paints from the SaveLayers we're inside.
Mike Klein271a0302014-09-23 15:28:38 -0400524 if (!this->adjustForSaveLayerPaints(&rect)) {
525 // Same deal as above.
526 return fCurrentClipBounds;
mtkleina723b572014-08-15 11:49:49 -0700527 }
528
mtklein533eb782014-08-27 10:39:42 -0700529 // Map the rect back to identity space.
mtklein6332f1d2014-08-19 07:09:40 -0700530 fCTM->mapRect(&rect);
mtklein479601b2014-08-18 08:45:33 -0700531
532 // Nothing can draw outside the current clip.
533 // (Only bounded ops call into this method, so oddballs like Clear don't matter here.)
mtklein533eb782014-08-27 10:39:42 -0700534 rect.intersect(fCurrentClipBounds);
535 return rect;
mtkleina723b572014-08-15 11:49:49 -0700536 }
537
mtklein533eb782014-08-27 10:39:42 -0700538 // Conservative identity-space bounds for each op in the SkRecord.
539 SkAutoTMalloc<Bounds> fBounds;
mtkleina723b572014-08-15 11:49:49 -0700540
541 // We walk fCurrentOp through the SkRecord, as we go using updateCTM()
542 // and updateClipBounds() to maintain the exact CTM (fCTM) and conservative
mtklein533eb782014-08-27 10:39:42 -0700543 // identity-space bounds of the current clip (fCurrentClipBounds).
mtklein828ce1f2014-08-13 12:58:45 -0700544 unsigned fCurrentOp;
mtklein6332f1d2014-08-19 07:09:40 -0700545 const SkMatrix* fCTM;
mtklein533eb782014-08-27 10:39:42 -0700546 Bounds fCurrentClipBounds;
mtkleina723b572014-08-15 11:49:49 -0700547
548 // Used to track the bounds of Save/Restore blocks and the control ops inside them.
mtklein828ce1f2014-08-13 12:58:45 -0700549 SkTDArray<SaveBounds> fSaveStack;
550 SkTDArray<unsigned> fControlIndices;
mtklein5ad6ee12014-08-11 08:08:43 -0700551};
552
commit-bot@chromium.org27f6b0d2014-05-09 14:59:29 +0000553} // namespace SkRecords
mtklein5ad6ee12014-08-11 08:08:43 -0700554
555void SkRecordFillBounds(const SkRecord& record, SkBBoxHierarchy* bbh) {
mtklein828ce1f2014-08-13 12:58:45 -0700556 SkRecords::FillBounds(record, bbh);
mtklein5ad6ee12014-08-11 08:08:43 -0700557}