blob: af2037044899b087299baf8230ed5e0605e3aaa5 [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));
96DRAW(DrawOval, drawOval(r.oval, r.paint));
97DRAW(DrawPaint, drawPaint(r.paint));
98DRAW(DrawPath, drawPath(r.path, r.paint));
mtklein9b222a52014-09-18 11:16:31 -070099DRAW(DrawPatch, drawPatch(r.cubics, r.colors, r.texCoords, r.xmode, r.paint));
reedd5fa1a42014-08-09 11:08:05 -0700100DRAW(DrawPicture, drawPicture(r.picture, r.matrix, r.paint));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +0000101DRAW(DrawPoints, drawPoints(r.mode, r.count, r.pts, r.paint));
102DRAW(DrawPosText, drawPosText(r.text, r.byteLength, r.pos, r.paint));
103DRAW(DrawPosTextH, drawPosTextH(r.text, r.byteLength, r.xpos, r.y, r.paint));
104DRAW(DrawRRect, drawRRect(r.rrect, r.paint));
105DRAW(DrawRect, drawRect(r.rect, r.paint));
mtklein7cdc1ee2014-07-07 10:41:04 -0700106DRAW(DrawSprite, drawSprite(shallow_copy(r.bitmap), r.left, r.top, r.paint));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +0000107DRAW(DrawText, drawText(r.text, r.byteLength, r.x, r.y, r.paint));
fmalita00d5c2c2014-08-21 08:53:26 -0700108DRAW(DrawTextBlob, drawTextBlob(r.blob, r.x, r.y, r.paint));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +0000109DRAW(DrawTextOnPath, drawTextOnPath(r.text, r.byteLength, r.path, r.matrix, r.paint));
110DRAW(DrawVertices, drawVertices(r.vmode, r.vertexCount, r.vertices, r.texs, r.colors,
111 r.xmode.get(), r.indices, r.indexCount, r.paint));
mtklein29dfaa82014-09-04 14:12:44 -0700112DRAW(DrawData, drawData(r.data, r.length));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +0000113#undef DRAW
114
mtklein5ad6ee12014-08-11 08:08:43 -0700115
mtklein0a528c12014-10-02 13:03:53 -0700116// This looks silly, I know. Why not just use SkRect::MakeLargest()?
117// In practice, this is well large enough, and it has a few extra advantages:
118// it fits in an SkIRect, and we can munge it a little in both SkRect and
119// SKIRect space without worrying about overflow.
120static const SkRect kUnbounded = { -2e9f, -2e9f, 2e9f, 2e9f };
121
122
mtklein5ad6ee12014-08-11 08:08:43 -0700123// This is an SkRecord visitor that fills an SkBBoxHierarchy.
mtklein828ce1f2014-08-13 12:58:45 -0700124//
125// The interesting part here is how to calculate bounds for ops which don't
126// have intrinsic bounds. What is the bounds of a Save or a Translate?
127//
128// We answer this by thinking about a particular definition of bounds: if I
129// don't execute this op, pixels in this rectangle might draw incorrectly. So
130// the bounds of a Save, a Translate, a Restore, etc. are the union of the
131// bounds of Draw* ops that they might have an effect on. For any given
132// Save/Restore block, the bounds of the Save, the Restore, and any other
133// non-drawing ("control") ops inside are exactly the union of the bounds of
134// the drawing ops inside that block.
135//
136// To implement this, we keep a stack of active Save blocks. As we consume ops
137// inside the Save/Restore block, drawing ops are unioned with the bounds of
138// the block, and control ops are stashed away for later. When we finish the
139// block with a Restore, our bounds are complete, and we go back and fill them
140// in for all the control ops we stashed away.
mtklein5ad6ee12014-08-11 08:08:43 -0700141class FillBounds : SkNoncopyable {
142public:
mtklein828ce1f2014-08-13 12:58:45 -0700143 FillBounds(const SkRecord& record, SkBBoxHierarchy* bbh) : fBounds(record.count()) {
144 // Calculate bounds for all ops. This won't go quite in order, so we'll need
145 // to store the bounds separately then feed them in to the BBH later in order.
mtklein6332f1d2014-08-19 07:09:40 -0700146 fCTM = &SkMatrix::I();
mtklein0a528c12014-10-02 13:03:53 -0700147 fCurrentClipBounds = kUnbounded;
mtklein828ce1f2014-08-13 12:58:45 -0700148 for (fCurrentOp = 0; fCurrentOp < record.count(); fCurrentOp++) {
149 record.visit<void>(fCurrentOp, *this);
150 }
mtklein5ad6ee12014-08-11 08:08:43 -0700151
mtklein828ce1f2014-08-13 12:58:45 -0700152 // If we have any lingering unpaired Saves, simulate restores to make
153 // sure all ops in those Save blocks have their bounds calculated.
154 while (!fSaveStack.isEmpty()) {
155 this->popSaveBlock();
156 }
157
158 // Any control ops not part of any Save/Restore block draw everywhere.
159 while (!fControlIndices.isEmpty()) {
mtklein0a528c12014-10-02 13:03:53 -0700160 this->popControl(kUnbounded);
mtklein828ce1f2014-08-13 12:58:45 -0700161 }
162
163 // Finally feed all stored bounds into the BBH. They'll be returned in this order.
bsalomon49f085d2014-09-05 13:34:00 -0700164 SkASSERT(bbh);
mtklein6bd41962014-10-02 07:41:56 -0700165 for (unsigned i = 0; i < record.count(); i++) {
mtklein828ce1f2014-08-13 12:58:45 -0700166 if (!fBounds[i].isEmpty()) {
mtklein6bd41962014-10-02 07:41:56 -0700167 bbh->insert(i, fBounds[i], true/*ok to defer*/);
mtklein828ce1f2014-08-13 12:58:45 -0700168 }
169 }
170 bbh->flushDeferredInserts();
171 }
mtklein5ad6ee12014-08-11 08:08:43 -0700172
mtkleina723b572014-08-15 11:49:49 -0700173 template <typename T> void operator()(const T& op) {
174 this->updateCTM(op);
175 this->updateClipBounds(op);
176 this->trackBounds(op);
mtklein5ad6ee12014-08-11 08:08:43 -0700177 }
178
179private:
mtklein533eb782014-08-27 10:39:42 -0700180 // In this file, SkRect are in local coordinates, Bounds are translated back to identity space.
181 typedef SkRect Bounds;
182
mtklein828ce1f2014-08-13 12:58:45 -0700183 struct SaveBounds {
mtkleina723b572014-08-15 11:49:49 -0700184 int controlOps; // Number of control ops in this Save block, including the Save.
mtklein533eb782014-08-27 10:39:42 -0700185 Bounds bounds; // Bounds of everything in the block.
mtkleina723b572014-08-15 11:49:49 -0700186 const SkPaint* paint; // Unowned. If set, adjusts the bounds of all ops in this block.
mtklein828ce1f2014-08-13 12:58:45 -0700187 };
188
mtklein8e393bf2014-10-01 12:48:58 -0700189 // Only Restore and SetMatrix change the CTM.
190 template <typename T> void updateCTM(const T&) {}
mtklein6332f1d2014-08-19 07:09:40 -0700191 void updateCTM(const Restore& op) { fCTM = &op.matrix; }
192 void updateCTM(const SetMatrix& op) { fCTM = &op.matrix; }
mtkleina723b572014-08-15 11:49:49 -0700193
mtklein8e393bf2014-10-01 12:48:58 -0700194 // Most ops don't change the clip.
195 template <typename T> void updateClipBounds(const T&) {}
Mike Klein271a0302014-09-23 15:28:38 -0400196
mtklein8e393bf2014-10-01 12:48:58 -0700197 // Clip{Path,RRect,Rect,Region} obviously change the clip. They all know their bounds already.
198 void updateClipBounds(const ClipPath& op) { this->updateClipBoundsForClipOp(op.devBounds); }
199 void updateClipBounds(const ClipRRect& op) { this->updateClipBoundsForClipOp(op.devBounds); }
200 void updateClipBounds(const ClipRect& op) { this->updateClipBoundsForClipOp(op.devBounds); }
201 void updateClipBounds(const ClipRegion& op) { this->updateClipBoundsForClipOp(op.devBounds); }
Mike Klein271a0302014-09-23 15:28:38 -0400202
mtklein8e393bf2014-10-01 12:48:58 -0700203 // The bounds of clip ops need to be adjusted for the paints of saveLayers they're inside.
204 void updateClipBoundsForClipOp(const SkIRect& devBounds) {
205 Bounds clip = SkRect::Make(devBounds);
Mike Klein271a0302014-09-23 15:28:38 -0400206 // We don't call adjustAndMap() because as its last step it would intersect the adjusted
207 // clip bounds with the previous clip, exactly what we can't do when the clip grows.
mtklein0a528c12014-10-02 13:03:53 -0700208 fCurrentClipBounds = this->adjustForSaveLayerPaints(&clip) ? clip : kUnbounded;
Mike Klein271a0302014-09-23 15:28:38 -0400209 }
210
mtklein8e393bf2014-10-01 12:48:58 -0700211 // Restore holds the devBounds for the clip after the {save,saveLayer}/restore block completes.
212 void updateClipBounds(const Restore& op) {
213 // This is just like the clip ops above, but we need to skip the effects (if any) of our
214 // paired saveLayer (if it is one); it has not yet been popped off the save stack. Our
215 // devBounds reflect the state of the world after the saveLayer/restore block is done,
216 // so they are not affected by the saveLayer's paint.
217 const int kSavesToIgnore = 1;
218 Bounds clip = SkRect::Make(op.devBounds);
219 fCurrentClipBounds =
mtklein0a528c12014-10-02 13:03:53 -0700220 this->adjustForSaveLayerPaints(&clip, kSavesToIgnore) ? clip : kUnbounded;
mtklein8e393bf2014-10-01 12:48:58 -0700221 }
222
Mike Klein271a0302014-09-23 15:28:38 -0400223 // We also take advantage of SaveLayer bounds when present to further cut the clip down.
mtkleina723b572014-08-15 11:49:49 -0700224 void updateClipBounds(const SaveLayer& op) {
225 if (op.bounds) {
Mike Klein271a0302014-09-23 15:28:38 -0400226 // adjustAndMap() intersects these layer bounds with the previous clip for us.
227 fCurrentClipBounds = this->adjustAndMap(*op.bounds, op.paint);
mtkleina723b572014-08-15 11:49:49 -0700228 }
229 }
mtklein6cfa73a2014-08-13 13:33:49 -0700230
mtklein828ce1f2014-08-13 12:58:45 -0700231 // The bounds of these ops must be calculated when we hit the Restore
232 // from the bounds of the ops in the same Save block.
mtkleina723b572014-08-15 11:49:49 -0700233 void trackBounds(const Save&) { this->pushSaveBlock(NULL); }
mtkleina723b572014-08-15 11:49:49 -0700234 void trackBounds(const SaveLayer& op) { this->pushSaveBlock(op.paint); }
235 void trackBounds(const Restore&) { fBounds[fCurrentOp] = this->popSaveBlock(); }
mtklein828ce1f2014-08-13 12:58:45 -0700236
mtklein68199a22014-08-25 13:49:29 -0700237 void trackBounds(const SetMatrix&) { this->pushControl(); }
238 void trackBounds(const ClipRect&) { this->pushControl(); }
239 void trackBounds(const ClipRRect&) { this->pushControl(); }
240 void trackBounds(const ClipPath&) { this->pushControl(); }
241 void trackBounds(const ClipRegion&) { this->pushControl(); }
242 void trackBounds(const PushCull&) { this->pushControl(); }
243 void trackBounds(const PopCull&) { this->pushControl(); }
244 void trackBounds(const BeginCommentGroup&) { this->pushControl(); }
245 void trackBounds(const AddComment&) { this->pushControl(); }
246 void trackBounds(const EndCommentGroup&) { this->pushControl(); }
mtklein29dfaa82014-09-04 14:12:44 -0700247 void trackBounds(const DrawData&) { this->pushControl(); }
mtklein828ce1f2014-08-13 12:58:45 -0700248
249 // For all other ops, we can calculate and store the bounds directly now.
250 template <typename T> void trackBounds(const T& op) {
251 fBounds[fCurrentOp] = this->bounds(op);
252 this->updateSaveBounds(fBounds[fCurrentOp]);
mtklein5ad6ee12014-08-11 08:08:43 -0700253 }
254
mtkleina723b572014-08-15 11:49:49 -0700255 void pushSaveBlock(const SkPaint* paint) {
mtklein828ce1f2014-08-13 12:58:45 -0700256 // Starting a new Save block. Push a new entry to represent that.
mtklein533eb782014-08-27 10:39:42 -0700257 SaveBounds sb = { 0, Bounds::MakeEmpty(), paint };
mtklein828ce1f2014-08-13 12:58:45 -0700258 fSaveStack.push(sb);
259 this->pushControl();
260 }
261
mtkleind910f542014-08-22 09:06:34 -0700262 static bool PaintMayAffectTransparentBlack(const SkPaint* paint) {
dneto327f9052014-09-15 10:53:16 -0700263 if (paint) {
264 // FIXME: this is very conservative
265 if (paint->getImageFilter() || paint->getColorFilter()) {
266 return true;
267 }
268
269 // Unusual Xfermodes require us to process a saved layer
270 // even with operations outisde the clip.
271 // For example, DstIn is used by masking layers.
272 // https://code.google.com/p/skia/issues/detail?id=1291
273 // https://crbug.com/401593
274 SkXfermode* xfermode = paint->getXfermode();
275 SkXfermode::Mode mode;
276 // SrcOver is ok, and is also the common case with a NULL xfermode.
277 // So we should make that the fast path and bypass the mode extraction
278 // and test.
279 if (xfermode && xfermode->asMode(&mode)) {
280 switch (mode) {
281 // For each of the following transfer modes, if the source
282 // alpha is zero (our transparent black), the resulting
283 // blended alpha is not necessarily equal to the original
284 // destination alpha.
285 case SkXfermode::kClear_Mode:
286 case SkXfermode::kSrc_Mode:
287 case SkXfermode::kSrcIn_Mode:
288 case SkXfermode::kDstIn_Mode:
289 case SkXfermode::kSrcOut_Mode:
290 case SkXfermode::kDstATop_Mode:
291 case SkXfermode::kModulate_Mode:
292 return true;
293 break;
294 default:
295 break;
296 }
297 }
298 }
299 return false;
mtkleind910f542014-08-22 09:06:34 -0700300 }
301
mtklein533eb782014-08-27 10:39:42 -0700302 Bounds popSaveBlock() {
mtklein828ce1f2014-08-13 12:58:45 -0700303 // We're done the Save block. Apply the block's bounds to all control ops inside it.
304 SaveBounds sb;
305 fSaveStack.pop(&sb);
mtkleind910f542014-08-22 09:06:34 -0700306
307 // If the paint affects transparent black, we can't trust any of our calculated bounds.
mtklein533eb782014-08-27 10:39:42 -0700308 const Bounds& bounds =
mtkleind910f542014-08-22 09:06:34 -0700309 PaintMayAffectTransparentBlack(sb.paint) ? fCurrentClipBounds : sb.bounds;
310
mtklein828ce1f2014-08-13 12:58:45 -0700311 while (sb.controlOps --> 0) {
mtkleind910f542014-08-22 09:06:34 -0700312 this->popControl(bounds);
mtklein828ce1f2014-08-13 12:58:45 -0700313 }
314
315 // This whole Save block may be part another Save block.
mtkleind910f542014-08-22 09:06:34 -0700316 this->updateSaveBounds(bounds);
mtklein828ce1f2014-08-13 12:58:45 -0700317
318 // If called from a real Restore (not a phony one for balance), it'll need the bounds.
mtkleind910f542014-08-22 09:06:34 -0700319 return bounds;
mtklein828ce1f2014-08-13 12:58:45 -0700320 }
321
322 void pushControl() {
323 fControlIndices.push(fCurrentOp);
324 if (!fSaveStack.isEmpty()) {
325 fSaveStack.top().controlOps++;
326 }
327 }
328
mtklein533eb782014-08-27 10:39:42 -0700329 void popControl(const Bounds& bounds) {
mtklein828ce1f2014-08-13 12:58:45 -0700330 fBounds[fControlIndices.top()] = bounds;
331 fControlIndices.pop();
332 }
333
mtklein533eb782014-08-27 10:39:42 -0700334 void updateSaveBounds(const Bounds& bounds) {
mtklein828ce1f2014-08-13 12:58:45 -0700335 // If we're in a Save block, expand its bounds to cover these bounds too.
336 if (!fSaveStack.isEmpty()) {
337 fSaveStack.top().bounds.join(bounds);
338 }
339 }
340
mtklein131a22b2014-08-25 14:16:15 -0700341 // FIXME: this method could use better bounds
mtklein533eb782014-08-27 10:39:42 -0700342 Bounds bounds(const DrawText&) const { return fCurrentClipBounds; }
mtklein68199a22014-08-25 13:49:29 -0700343
mtklein0a528c12014-10-02 13:03:53 -0700344 Bounds bounds(const Clear&) const { return kUnbounded; } // Ignores the clip.
mtklein533eb782014-08-27 10:39:42 -0700345 Bounds bounds(const DrawPaint&) const { return fCurrentClipBounds; }
346 Bounds bounds(const NoOp&) const { return Bounds::MakeEmpty(); } // NoOps don't draw.
mtklein828ce1f2014-08-13 12:58:45 -0700347
mtklein533eb782014-08-27 10:39:42 -0700348 Bounds bounds(const DrawSprite& op) const {
mtklein131a22b2014-08-25 14:16:15 -0700349 const SkBitmap& bm = op.bitmap;
mtklein533eb782014-08-27 10:39:42 -0700350 return Bounds::MakeXYWH(op.left, op.top, bm.width(), bm.height()); // Ignores the matrix.
mtklein131a22b2014-08-25 14:16:15 -0700351 }
352
mtklein533eb782014-08-27 10:39:42 -0700353 Bounds bounds(const DrawRect& op) const { return this->adjustAndMap(op.rect, &op.paint); }
354 Bounds bounds(const DrawOval& op) const { return this->adjustAndMap(op.oval, &op.paint); }
355 Bounds bounds(const DrawRRect& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700356 return this->adjustAndMap(op.rrect.rect(), &op.paint);
357 }
mtklein533eb782014-08-27 10:39:42 -0700358 Bounds bounds(const DrawDRRect& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700359 return this->adjustAndMap(op.outer.rect(), &op.paint);
360 }
361
mtklein533eb782014-08-27 10:39:42 -0700362 Bounds bounds(const DrawBitmapRectToRect& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700363 return this->adjustAndMap(op.dst, op.paint);
364 }
mtklein533eb782014-08-27 10:39:42 -0700365 Bounds bounds(const DrawBitmapNine& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700366 return this->adjustAndMap(op.dst, op.paint);
367 }
mtklein533eb782014-08-27 10:39:42 -0700368 Bounds bounds(const DrawBitmap& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700369 const SkBitmap& bm = op.bitmap;
370 return this->adjustAndMap(SkRect::MakeXYWH(op.left, op.top, bm.width(), bm.height()),
371 op.paint);
372 }
mtklein533eb782014-08-27 10:39:42 -0700373 Bounds bounds(const DrawBitmapMatrix& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700374 const SkBitmap& bm = op.bitmap;
375 SkRect dst = SkRect::MakeWH(bm.width(), bm.height());
376 op.matrix.mapRect(&dst);
377 return this->adjustAndMap(dst, op.paint);
378 }
379
mtklein533eb782014-08-27 10:39:42 -0700380 Bounds bounds(const DrawPath& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700381 return op.path.isInverseFillType() ? fCurrentClipBounds
382 : this->adjustAndMap(op.path.getBounds(), &op.paint);
383 }
mtklein533eb782014-08-27 10:39:42 -0700384 Bounds bounds(const DrawPoints& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700385 SkRect dst;
386 dst.set(op.pts, op.count);
387
388 // Pad the bounding box a little to make sure hairline points' bounds aren't empty.
389 SkScalar stroke = SkMaxScalar(op.paint.getStrokeWidth(), 0.01f);
390 dst.outset(stroke/2, stroke/2);
391
392 return this->adjustAndMap(dst, &op.paint);
393 }
mtklein533eb782014-08-27 10:39:42 -0700394 Bounds bounds(const DrawPatch& op) const {
mtklein131a22b2014-08-25 14:16:15 -0700395 SkRect dst;
396 dst.set(op.cubics, SkPatchUtils::kNumCtrlPts);
397 return this->adjustAndMap(dst, &op.paint);
398 }
mtklein533eb782014-08-27 10:39:42 -0700399 Bounds bounds(const DrawVertices& op) const {
mtklein131a22b2014-08-25 14:16:15 -0700400 SkRect dst;
401 dst.set(op.vertices, op.vertexCount);
402 return this->adjustAndMap(dst, &op.paint);
403 }
404
mtklein533eb782014-08-27 10:39:42 -0700405 Bounds bounds(const DrawPicture& op) const {
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700406 SkRect dst = op.picture->cullRect();
mtklein131a22b2014-08-25 14:16:15 -0700407 if (op.matrix) {
408 op.matrix->mapRect(&dst);
409 }
410 return this->adjustAndMap(dst, op.paint);
411 }
mtklein62b67ae2014-08-18 11:10:37 -0700412
mtklein533eb782014-08-27 10:39:42 -0700413 Bounds bounds(const DrawPosText& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700414 const int N = op.paint.countText(op.text, op.byteLength);
415 if (N == 0) {
mtklein533eb782014-08-27 10:39:42 -0700416 return Bounds::MakeEmpty();
mtklein62b67ae2014-08-18 11:10:37 -0700417 }
418
419 SkRect dst;
mtklein937c9c72014-09-02 15:19:48 -0700420 dst.set(op.pos, N);
mtklein62b67ae2014-08-18 11:10:37 -0700421 AdjustTextForFontMetrics(&dst, op.paint);
422 return this->adjustAndMap(dst, &op.paint);
423 }
mtklein533eb782014-08-27 10:39:42 -0700424 Bounds bounds(const DrawPosTextH& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700425 const int N = op.paint.countText(op.text, op.byteLength);
426 if (N == 0) {
mtklein533eb782014-08-27 10:39:42 -0700427 return Bounds::MakeEmpty();
mtklein62b67ae2014-08-18 11:10:37 -0700428 }
429
430 SkScalar left = op.xpos[0], right = op.xpos[0];
431 for (int i = 1; i < N; i++) {
432 left = SkMinScalar(left, op.xpos[i]);
433 right = SkMaxScalar(right, op.xpos[i]);
434 }
435 SkRect dst = { left, op.y, right, op.y };
436 AdjustTextForFontMetrics(&dst, op.paint);
437 return this->adjustAndMap(dst, &op.paint);
438 }
mtklein533eb782014-08-27 10:39:42 -0700439 Bounds bounds(const DrawTextOnPath& op) const {
mtklein131a22b2014-08-25 14:16:15 -0700440 SkRect dst = op.path.getBounds();
441
442 // Pad all sides by the maximum padding in any direction we'd normally apply.
443 SkRect pad = { 0, 0, 0, 0};
444 AdjustTextForFontMetrics(&pad, op.paint);
445
446 // That maximum padding happens to always be the right pad today.
447 SkASSERT(pad.fLeft == -pad.fRight);
448 SkASSERT(pad.fTop == -pad.fBottom);
449 SkASSERT(pad.fRight > pad.fBottom);
450 dst.outset(pad.fRight, pad.fRight);
451
452 return this->adjustAndMap(dst, &op.paint);
453 }
454
mtklein533eb782014-08-27 10:39:42 -0700455 Bounds bounds(const DrawTextBlob& op) const {
mtklein131a22b2014-08-25 14:16:15 -0700456 SkRect dst = op.blob->bounds();
457 dst.offset(op.x, op.y);
458 // TODO: remove when implicit bounds are plumbed through
459 if (dst.isEmpty()) {
460 return fCurrentClipBounds;
461 }
462 return this->adjustAndMap(dst, &op.paint);
463 }
mtklein62b67ae2014-08-18 11:10:37 -0700464
465 static void AdjustTextForFontMetrics(SkRect* rect, const SkPaint& paint) {
caryclark9a657fa2014-08-20 05:24:29 -0700466#ifdef SK_DEBUG
mtkleina19afb42014-08-19 17:47:14 -0700467 SkRect correct = *rect;
468#endif
mtkleinc8460492014-08-21 16:39:12 -0700469 const SkScalar yPad = 2.0f * paint.getTextSize(), // In practice, this seems to be enough.
mtkleina19afb42014-08-19 17:47:14 -0700470 xPad = 4.0f * yPad; // Hack for very wide Github logo font.
471 rect->outset(xPad, yPad);
caryclark9a657fa2014-08-20 05:24:29 -0700472#ifdef SK_DEBUG
mtklein62b67ae2014-08-18 11:10:37 -0700473 SkPaint::FontMetrics metrics;
474 paint.getFontMetrics(&metrics);
mtkleina19afb42014-08-19 17:47:14 -0700475 correct.fLeft += metrics.fXMin;
476 correct.fTop += metrics.fTop;
477 correct.fRight += metrics.fXMax;
478 correct.fBottom += metrics.fBottom;
mtkleind13291a2014-08-21 14:46:49 -0700479 // See skia:2862 for why we ignore small text sizes.
480 SkASSERTF(paint.getTextSize() < 0.001f || rect->contains(correct),
481 "%f %f %f %f vs. %f %f %f %f\n",
mtkleina19afb42014-08-19 17:47:14 -0700482 -xPad, -yPad, +xPad, +yPad,
483 metrics.fXMin, metrics.fTop, metrics.fXMax, metrics.fBottom);
484#endif
mtklein62b67ae2014-08-18 11:10:37 -0700485 }
486
mtklein479601b2014-08-18 08:45:33 -0700487 // Returns true if rect was meaningfully adjusted for the effects of paint,
488 // false if the paint could affect the rect in unknown ways.
489 static bool AdjustForPaint(const SkPaint* paint, SkRect* rect) {
mtkleina723b572014-08-15 11:49:49 -0700490 if (paint) {
491 if (paint->canComputeFastBounds()) {
mtklein479601b2014-08-18 08:45:33 -0700492 *rect = paint->computeFastBounds(*rect, rect);
493 return true;
mtkleina723b572014-08-15 11:49:49 -0700494 }
mtklein479601b2014-08-18 08:45:33 -0700495 return false;
496 }
497 return true;
498 }
499
mtklein8e393bf2014-10-01 12:48:58 -0700500 bool adjustForSaveLayerPaints(SkRect* rect, int savesToIgnore = 0) const {
501 for (int i = fSaveStack.count() - 1 - savesToIgnore; i >= 0; i--) {
Mike Klein271a0302014-09-23 15:28:38 -0400502 if (!AdjustForPaint(fSaveStack[i].paint, rect)) {
503 return false;
504 }
505 }
506 return true;
507 }
508
mtklein533eb782014-08-27 10:39:42 -0700509 // Adjust rect for all paints that may affect its geometry, then map it to identity space.
510 Bounds adjustAndMap(SkRect rect, const SkPaint* paint) const {
mtklein479601b2014-08-18 08:45:33 -0700511 // Inverted rectangles really confuse our BBHs.
512 rect.sort();
513
514 // Adjust the rect for its own paint.
515 if (!AdjustForPaint(paint, &rect)) {
516 // The paint could do anything to our bounds. The only safe answer is the current clip.
517 return fCurrentClipBounds;
mtkleina723b572014-08-15 11:49:49 -0700518 }
519
520 // Adjust rect for all the paints from the SaveLayers we're inside.
Mike Klein271a0302014-09-23 15:28:38 -0400521 if (!this->adjustForSaveLayerPaints(&rect)) {
522 // Same deal as above.
523 return fCurrentClipBounds;
mtkleina723b572014-08-15 11:49:49 -0700524 }
525
mtklein533eb782014-08-27 10:39:42 -0700526 // Map the rect back to identity space.
mtklein6332f1d2014-08-19 07:09:40 -0700527 fCTM->mapRect(&rect);
mtklein479601b2014-08-18 08:45:33 -0700528
529 // Nothing can draw outside the current clip.
530 // (Only bounded ops call into this method, so oddballs like Clear don't matter here.)
mtklein533eb782014-08-27 10:39:42 -0700531 rect.intersect(fCurrentClipBounds);
532 return rect;
mtkleina723b572014-08-15 11:49:49 -0700533 }
534
mtklein533eb782014-08-27 10:39:42 -0700535 // Conservative identity-space bounds for each op in the SkRecord.
536 SkAutoTMalloc<Bounds> fBounds;
mtkleina723b572014-08-15 11:49:49 -0700537
538 // We walk fCurrentOp through the SkRecord, as we go using updateCTM()
539 // and updateClipBounds() to maintain the exact CTM (fCTM) and conservative
mtklein533eb782014-08-27 10:39:42 -0700540 // identity-space bounds of the current clip (fCurrentClipBounds).
mtklein828ce1f2014-08-13 12:58:45 -0700541 unsigned fCurrentOp;
mtklein6332f1d2014-08-19 07:09:40 -0700542 const SkMatrix* fCTM;
mtklein533eb782014-08-27 10:39:42 -0700543 Bounds fCurrentClipBounds;
mtkleina723b572014-08-15 11:49:49 -0700544
545 // Used to track the bounds of Save/Restore blocks and the control ops inside them.
mtklein828ce1f2014-08-13 12:58:45 -0700546 SkTDArray<SaveBounds> fSaveStack;
547 SkTDArray<unsigned> fControlIndices;
mtklein5ad6ee12014-08-11 08:08:43 -0700548};
549
commit-bot@chromium.org27f6b0d2014-05-09 14:59:29 +0000550} // namespace SkRecords
mtklein5ad6ee12014-08-11 08:08:43 -0700551
552void SkRecordFillBounds(const SkRecord& record, SkBBoxHierarchy* bbh) {
mtklein828ce1f2014-08-13 12:58:45 -0700553 SkRecords::FillBounds(record, bbh);
mtklein5ad6ee12014-08-11 08:08:43 -0700554}