blob: 2a839a95762fa07ec070b8e4c956986e32bf8c53 [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
robertphillips4e8e3422014-11-12 06:46:08 -080011#if SK_SUPPORT_GPU
12#include "GrPictureUtils.h"
13#endif
14
mtklein5ad6ee12014-08-11 08:08:43 -070015void SkRecordDraw(const SkRecord& record,
16 SkCanvas* canvas,
17 const SkBBoxHierarchy* bbh,
18 SkDrawPictureCallback* callback) {
Mike Kleinc11530e2014-06-24 11:29:06 -040019 SkAutoCanvasRestore saveRestore(canvas, true /*save now, restore at exit*/);
mtklein5ad6ee12014-08-11 08:08:43 -070020
bsalomon49f085d2014-09-05 13:34:00 -070021 if (bbh) {
mtklein5ad6ee12014-08-11 08:08:43 -070022 // Draw only ops that affect pixels in the canvas's current clip.
mtklein3e8232b2014-08-18 13:39:11 -070023 // The SkRecord and BBH were recorded in identity space. This canvas
24 // is not necessarily in that same space. getClipBounds() returns us
25 // this canvas' clip bounds transformed back into identity space, which
26 // lets us query the BBH.
junova41d3c32014-10-30 11:44:19 -070027 SkRect query;
28 if (!canvas->getClipBounds(&query)) {
29 return;
30 }
mtklein3e8232b2014-08-18 13:39:11 -070031
mtklein6bd41962014-10-02 07:41:56 -070032 SkTDArray<unsigned> ops;
mtkleina723b572014-08-15 11:49:49 -070033 bbh->search(query, &ops);
mtklein5ad6ee12014-08-11 08:08:43 -070034
mtklein5ad6ee12014-08-11 08:08:43 -070035 SkRecords::Draw draw(canvas);
36 for (int i = 0; i < ops.count(); i++) {
bsalomon49f085d2014-09-05 13:34:00 -070037 if (callback && callback->abortDrawing()) {
mtklein5ad6ee12014-08-11 08:08:43 -070038 return;
39 }
danakjd239d422014-11-03 12:43:30 -080040 // This visit call uses the SkRecords::Draw::operator() to call
41 // methods on the |canvas|, wrapped by methods defined with the
42 // DRAW() macro.
mtklein6bd41962014-10-02 07:41:56 -070043 record.visit<void>(ops[i], draw);
mtklein5ad6ee12014-08-11 08:08:43 -070044 }
45 } else {
46 // Draw all ops.
mtklein00f30bd2014-09-02 12:03:31 -070047 SkRecords::Draw draw(canvas);
48 for (unsigned i = 0; i < record.count(); i++) {
bsalomon49f085d2014-09-05 13:34:00 -070049 if (callback && callback->abortDrawing()) {
mtklein5ad6ee12014-08-11 08:08:43 -070050 return;
51 }
danakjd239d422014-11-03 12:43:30 -080052 // This visit call uses the SkRecords::Draw::operator() to call
53 // methods on the |canvas|, wrapped by methods defined with the
54 // DRAW() macro.
mtklein00f30bd2014-09-02 12:03:31 -070055 record.visit<void>(i, draw);
mtklein5ad6ee12014-08-11 08:08:43 -070056 }
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000057 }
commit-bot@chromium.org27f6b0d2014-05-09 14:59:29 +000058}
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000059
mtklein00f30bd2014-09-02 12:03:31 -070060void SkRecordPartialDraw(const SkRecord& record,
61 SkCanvas* canvas,
62 const SkRect& clearRect,
robertphillips4815fe52014-09-16 10:32:43 -070063 unsigned start, unsigned stop,
64 const SkMatrix& initialCTM) {
mtklein00f30bd2014-09-02 12:03:31 -070065 SkAutoCanvasRestore saveRestore(canvas, true /*save now, restore at exit*/);
66
67 stop = SkTMin(stop, record.count());
robertphillips4815fe52014-09-16 10:32:43 -070068 SkRecords::PartialDraw draw(canvas, clearRect, initialCTM);
mtklein00f30bd2014-09-02 12:03:31 -070069 for (unsigned i = start; i < stop; i++) {
70 record.visit<void>(i, draw);
71 }
72}
73
commit-bot@chromium.org27f6b0d2014-05-09 14:59:29 +000074namespace SkRecords {
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000075
mtklein7cdc1ee2014-07-07 10:41:04 -070076// FIXME: SkBitmaps are stateful, so we need to copy them to play back in multiple threads.
77static SkBitmap shallow_copy(const SkBitmap& bitmap) {
78 return bitmap;
79}
80
commit-bot@chromium.org2e0c32a2014-04-28 16:19:45 +000081// NoOps draw nothing.
commit-bot@chromium.org27f6b0d2014-05-09 14:59:29 +000082template <> void Draw::draw(const NoOp&) {}
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000083
commit-bot@chromium.org27f6b0d2014-05-09 14:59:29 +000084#define DRAW(T, call) template <> void Draw::draw(const T& r) { fCanvas->call; }
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000085DRAW(Restore, restore());
Florin Malita5f6102d2014-06-30 10:13:28 -040086DRAW(Save, save());
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000087DRAW(SaveLayer, saveLayer(r.bounds, r.paint, r.flags));
88DRAW(PopCull, popCull());
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000089DRAW(PushCull, pushCull(r.rect));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000090DRAW(Clear, clear(r.color));
commit-bot@chromium.org99bd7d82014-05-19 15:51:12 +000091DRAW(SetMatrix, setMatrix(SkMatrix::Concat(fInitialCTM, r.matrix)));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000092
93DRAW(ClipPath, clipPath(r.path, r.op, r.doAA));
94DRAW(ClipRRect, clipRRect(r.rrect, r.op, r.doAA));
95DRAW(ClipRect, clipRect(r.rect, r.op, r.doAA));
96DRAW(ClipRegion, clipRegion(r.region, r.op));
97
mtklein5f0e8222014-08-22 11:44:26 -070098DRAW(BeginCommentGroup, beginCommentGroup(r.description));
99DRAW(AddComment, addComment(r.key, r.value));
100DRAW(EndCommentGroup, endCommentGroup());
101
mtklein7cdc1ee2014-07-07 10:41:04 -0700102DRAW(DrawBitmap, drawBitmap(shallow_copy(r.bitmap), r.left, r.top, r.paint));
103DRAW(DrawBitmapMatrix, drawBitmapMatrix(shallow_copy(r.bitmap), r.matrix, r.paint));
104DRAW(DrawBitmapNine, drawBitmapNine(shallow_copy(r.bitmap), r.center, r.dst, r.paint));
105DRAW(DrawBitmapRectToRect,
106 drawBitmapRectToRect(shallow_copy(r.bitmap), r.src, r.dst, r.paint, r.flags));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +0000107DRAW(DrawDRRect, drawDRRect(r.outer, r.inner, r.paint));
piotaixr65151752014-10-16 11:58:39 -0700108DRAW(DrawImage, drawImage(r.image, r.left, r.top, r.paint));
109DRAW(DrawImageRect, drawImageRect(r.image, r.src, r.dst, r.paint));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +0000110DRAW(DrawOval, drawOval(r.oval, r.paint));
111DRAW(DrawPaint, drawPaint(r.paint));
112DRAW(DrawPath, drawPath(r.path, r.paint));
mtklein9b222a52014-09-18 11:16:31 -0700113DRAW(DrawPatch, drawPatch(r.cubics, r.colors, r.texCoords, r.xmode, r.paint));
reedd5fa1a42014-08-09 11:08:05 -0700114DRAW(DrawPicture, drawPicture(r.picture, r.matrix, r.paint));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +0000115DRAW(DrawPoints, drawPoints(r.mode, r.count, r.pts, r.paint));
116DRAW(DrawPosText, drawPosText(r.text, r.byteLength, r.pos, r.paint));
117DRAW(DrawPosTextH, drawPosTextH(r.text, r.byteLength, r.xpos, r.y, r.paint));
118DRAW(DrawRRect, drawRRect(r.rrect, r.paint));
119DRAW(DrawRect, drawRect(r.rect, r.paint));
mtklein7cdc1ee2014-07-07 10:41:04 -0700120DRAW(DrawSprite, drawSprite(shallow_copy(r.bitmap), r.left, r.top, r.paint));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +0000121DRAW(DrawText, drawText(r.text, r.byteLength, r.x, r.y, r.paint));
fmalita00d5c2c2014-08-21 08:53:26 -0700122DRAW(DrawTextBlob, drawTextBlob(r.blob, r.x, r.y, r.paint));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +0000123DRAW(DrawTextOnPath, drawTextOnPath(r.text, r.byteLength, r.path, r.matrix, r.paint));
124DRAW(DrawVertices, drawVertices(r.vmode, r.vertexCount, r.vertices, r.texs, r.colors,
125 r.xmode.get(), r.indices, r.indexCount, r.paint));
mtklein29dfaa82014-09-04 14:12:44 -0700126DRAW(DrawData, drawData(r.data, r.length));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +0000127#undef DRAW
128
mtklein5ad6ee12014-08-11 08:08:43 -0700129// This is an SkRecord visitor that fills an SkBBoxHierarchy.
mtklein828ce1f2014-08-13 12:58:45 -0700130//
131// The interesting part here is how to calculate bounds for ops which don't
132// have intrinsic bounds. What is the bounds of a Save or a Translate?
133//
134// We answer this by thinking about a particular definition of bounds: if I
135// don't execute this op, pixels in this rectangle might draw incorrectly. So
136// the bounds of a Save, a Translate, a Restore, etc. are the union of the
137// bounds of Draw* ops that they might have an effect on. For any given
138// Save/Restore block, the bounds of the Save, the Restore, and any other
139// non-drawing ("control") ops inside are exactly the union of the bounds of
140// the drawing ops inside that block.
141//
142// To implement this, we keep a stack of active Save blocks. As we consume ops
143// inside the Save/Restore block, drawing ops are unioned with the bounds of
144// the block, and control ops are stashed away for later. When we finish the
145// block with a Restore, our bounds are complete, and we go back and fill them
146// in for all the control ops we stashed away.
mtklein5ad6ee12014-08-11 08:08:43 -0700147class FillBounds : SkNoncopyable {
148public:
robertphillips4e8e3422014-11-12 06:46:08 -0800149 FillBounds(const SkRect& cullRect, const SkRecord& record)
150 : fNumRecords(record.count())
151 , fCullRect(cullRect)
robertphillips4d52afe2014-11-03 08:19:44 -0800152 , fBounds(record.count()) {
mtklein828ce1f2014-08-13 12:58:45 -0700153 // Calculate bounds for all ops. This won't go quite in order, so we'll need
154 // to store the bounds separately then feed them in to the BBH later in order.
mtklein6332f1d2014-08-19 07:09:40 -0700155 fCTM = &SkMatrix::I();
robertphillips4d52afe2014-11-03 08:19:44 -0800156 fCurrentClipBounds = fCullRect;
robertphillips4e8e3422014-11-12 06:46:08 -0800157 }
mtklein5ad6ee12014-08-11 08:08:43 -0700158
robertphillips4e8e3422014-11-12 06:46:08 -0800159 void setCurrentOp(unsigned currentOp) { fCurrentOp = currentOp; }
160
161 void cleanUp(SkBBoxHierarchy* bbh) {
mtklein828ce1f2014-08-13 12:58:45 -0700162 // If we have any lingering unpaired Saves, simulate restores to make
163 // sure all ops in those Save blocks have their bounds calculated.
164 while (!fSaveStack.isEmpty()) {
165 this->popSaveBlock();
166 }
167
168 // Any control ops not part of any Save/Restore block draw everywhere.
169 while (!fControlIndices.isEmpty()) {
robertphillips4d52afe2014-11-03 08:19:44 -0800170 this->popControl(fCullRect);
mtklein828ce1f2014-08-13 12:58:45 -0700171 }
172
173 // Finally feed all stored bounds into the BBH. They'll be returned in this order.
bsalomon49f085d2014-09-05 13:34:00 -0700174 SkASSERT(bbh);
robertphillips4e8e3422014-11-12 06:46:08 -0800175 bbh->insert(&fBounds, fNumRecords);
mtklein828ce1f2014-08-13 12:58:45 -0700176 }
mtklein5ad6ee12014-08-11 08:08:43 -0700177
mtkleina723b572014-08-15 11:49:49 -0700178 template <typename T> void operator()(const T& op) {
179 this->updateCTM(op);
180 this->updateClipBounds(op);
181 this->trackBounds(op);
mtklein5ad6ee12014-08-11 08:08:43 -0700182 }
183
mtklein533eb782014-08-27 10:39:42 -0700184 // In this file, SkRect are in local coordinates, Bounds are translated back to identity space.
185 typedef SkRect Bounds;
186
robertphillips4e8e3422014-11-12 06:46:08 -0800187 unsigned currentOp() const { return fCurrentOp; }
188 const SkMatrix& ctm() const { return *fCTM; }
189 const Bounds& currentClipBounds() const { return fCurrentClipBounds; }
190 const Bounds& getBounds(unsigned index) const { return fBounds[index]; }
191
192 // Adjust rect for all paints that may affect its geometry, then map it to identity space.
193 Bounds adjustAndMap(SkRect rect, const SkPaint* paint) const {
194 // Inverted rectangles really confuse our BBHs.
195 rect.sort();
196
197 // Adjust the rect for its own paint.
198 if (!AdjustForPaint(paint, &rect)) {
199 // The paint could do anything to our bounds. The only safe answer is the current clip.
200 return fCurrentClipBounds;
201 }
202
203 // Adjust rect for all the paints from the SaveLayers we're inside.
204 if (!this->adjustForSaveLayerPaints(&rect)) {
205 // Same deal as above.
206 return fCurrentClipBounds;
207 }
208
209 // Map the rect back to identity space.
210 fCTM->mapRect(&rect);
211
212 // Nothing can draw outside the current clip.
213 // (Only bounded ops call into this method, so oddballs like Clear don't matter here.)
214 rect.intersect(fCurrentClipBounds);
215 return rect;
216 }
217
218private:
mtklein828ce1f2014-08-13 12:58:45 -0700219 struct SaveBounds {
mtkleina723b572014-08-15 11:49:49 -0700220 int controlOps; // Number of control ops in this Save block, including the Save.
mtklein533eb782014-08-27 10:39:42 -0700221 Bounds bounds; // Bounds of everything in the block.
mtkleina723b572014-08-15 11:49:49 -0700222 const SkPaint* paint; // Unowned. If set, adjusts the bounds of all ops in this block.
mtklein828ce1f2014-08-13 12:58:45 -0700223 };
224
mtklein8e393bf2014-10-01 12:48:58 -0700225 // Only Restore and SetMatrix change the CTM.
226 template <typename T> void updateCTM(const T&) {}
mtklein6332f1d2014-08-19 07:09:40 -0700227 void updateCTM(const Restore& op) { fCTM = &op.matrix; }
228 void updateCTM(const SetMatrix& op) { fCTM = &op.matrix; }
mtkleina723b572014-08-15 11:49:49 -0700229
mtklein8e393bf2014-10-01 12:48:58 -0700230 // Most ops don't change the clip.
231 template <typename T> void updateClipBounds(const T&) {}
Mike Klein271a0302014-09-23 15:28:38 -0400232
mtklein8e393bf2014-10-01 12:48:58 -0700233 // Clip{Path,RRect,Rect,Region} obviously change the clip. They all know their bounds already.
234 void updateClipBounds(const ClipPath& op) { this->updateClipBoundsForClipOp(op.devBounds); }
235 void updateClipBounds(const ClipRRect& op) { this->updateClipBoundsForClipOp(op.devBounds); }
236 void updateClipBounds(const ClipRect& op) { this->updateClipBoundsForClipOp(op.devBounds); }
237 void updateClipBounds(const ClipRegion& op) { this->updateClipBoundsForClipOp(op.devBounds); }
Mike Klein271a0302014-09-23 15:28:38 -0400238
mtklein8e393bf2014-10-01 12:48:58 -0700239 // The bounds of clip ops need to be adjusted for the paints of saveLayers they're inside.
240 void updateClipBoundsForClipOp(const SkIRect& devBounds) {
241 Bounds clip = SkRect::Make(devBounds);
Mike Klein271a0302014-09-23 15:28:38 -0400242 // We don't call adjustAndMap() because as its last step it would intersect the adjusted
243 // clip bounds with the previous clip, exactly what we can't do when the clip grows.
robertphillips4d52afe2014-11-03 08:19:44 -0800244 fCurrentClipBounds = this->adjustForSaveLayerPaints(&clip) ? clip : fCullRect;
Mike Klein271a0302014-09-23 15:28:38 -0400245 }
246
mtklein8e393bf2014-10-01 12:48:58 -0700247 // Restore holds the devBounds for the clip after the {save,saveLayer}/restore block completes.
248 void updateClipBounds(const Restore& op) {
249 // This is just like the clip ops above, but we need to skip the effects (if any) of our
250 // paired saveLayer (if it is one); it has not yet been popped off the save stack. Our
251 // devBounds reflect the state of the world after the saveLayer/restore block is done,
252 // so they are not affected by the saveLayer's paint.
253 const int kSavesToIgnore = 1;
254 Bounds clip = SkRect::Make(op.devBounds);
255 fCurrentClipBounds =
robertphillips4d52afe2014-11-03 08:19:44 -0800256 this->adjustForSaveLayerPaints(&clip, kSavesToIgnore) ? clip : fCullRect;
mtklein8e393bf2014-10-01 12:48:58 -0700257 }
258
Mike Klein271a0302014-09-23 15:28:38 -0400259 // We also take advantage of SaveLayer bounds when present to further cut the clip down.
mtkleina723b572014-08-15 11:49:49 -0700260 void updateClipBounds(const SaveLayer& op) {
261 if (op.bounds) {
Mike Klein271a0302014-09-23 15:28:38 -0400262 // adjustAndMap() intersects these layer bounds with the previous clip for us.
263 fCurrentClipBounds = this->adjustAndMap(*op.bounds, op.paint);
mtkleina723b572014-08-15 11:49:49 -0700264 }
265 }
mtklein6cfa73a2014-08-13 13:33:49 -0700266
mtklein828ce1f2014-08-13 12:58:45 -0700267 // The bounds of these ops must be calculated when we hit the Restore
268 // from the bounds of the ops in the same Save block.
mtkleina723b572014-08-15 11:49:49 -0700269 void trackBounds(const Save&) { this->pushSaveBlock(NULL); }
mtkleina723b572014-08-15 11:49:49 -0700270 void trackBounds(const SaveLayer& op) { this->pushSaveBlock(op.paint); }
271 void trackBounds(const Restore&) { fBounds[fCurrentOp] = this->popSaveBlock(); }
mtklein828ce1f2014-08-13 12:58:45 -0700272
mtklein68199a22014-08-25 13:49:29 -0700273 void trackBounds(const SetMatrix&) { this->pushControl(); }
274 void trackBounds(const ClipRect&) { this->pushControl(); }
275 void trackBounds(const ClipRRect&) { this->pushControl(); }
276 void trackBounds(const ClipPath&) { this->pushControl(); }
277 void trackBounds(const ClipRegion&) { this->pushControl(); }
278 void trackBounds(const PushCull&) { this->pushControl(); }
279 void trackBounds(const PopCull&) { this->pushControl(); }
280 void trackBounds(const BeginCommentGroup&) { this->pushControl(); }
281 void trackBounds(const AddComment&) { this->pushControl(); }
282 void trackBounds(const EndCommentGroup&) { this->pushControl(); }
mtklein29dfaa82014-09-04 14:12:44 -0700283 void trackBounds(const DrawData&) { this->pushControl(); }
mtklein828ce1f2014-08-13 12:58:45 -0700284
285 // For all other ops, we can calculate and store the bounds directly now.
286 template <typename T> void trackBounds(const T& op) {
287 fBounds[fCurrentOp] = this->bounds(op);
288 this->updateSaveBounds(fBounds[fCurrentOp]);
mtklein5ad6ee12014-08-11 08:08:43 -0700289 }
290
mtkleina723b572014-08-15 11:49:49 -0700291 void pushSaveBlock(const SkPaint* paint) {
mtklein828ce1f2014-08-13 12:58:45 -0700292 // Starting a new Save block. Push a new entry to represent that.
robertphillips4d52afe2014-11-03 08:19:44 -0800293 SaveBounds sb;
294 sb.controlOps = 0;
295 // If the paint affects transparent black, the bound shouldn't be smaller
296 // than the current clip bounds.
297 sb.bounds =
298 PaintMayAffectTransparentBlack(paint) ? fCurrentClipBounds : Bounds::MakeEmpty();
299 sb.paint = paint;
300
mtklein828ce1f2014-08-13 12:58:45 -0700301 fSaveStack.push(sb);
302 this->pushControl();
303 }
304
mtkleind910f542014-08-22 09:06:34 -0700305 static bool PaintMayAffectTransparentBlack(const SkPaint* paint) {
dneto327f9052014-09-15 10:53:16 -0700306 if (paint) {
307 // FIXME: this is very conservative
308 if (paint->getImageFilter() || paint->getColorFilter()) {
309 return true;
310 }
311
312 // Unusual Xfermodes require us to process a saved layer
313 // even with operations outisde the clip.
314 // For example, DstIn is used by masking layers.
315 // https://code.google.com/p/skia/issues/detail?id=1291
316 // https://crbug.com/401593
317 SkXfermode* xfermode = paint->getXfermode();
318 SkXfermode::Mode mode;
319 // SrcOver is ok, and is also the common case with a NULL xfermode.
320 // So we should make that the fast path and bypass the mode extraction
321 // and test.
322 if (xfermode && xfermode->asMode(&mode)) {
323 switch (mode) {
324 // For each of the following transfer modes, if the source
325 // alpha is zero (our transparent black), the resulting
326 // blended alpha is not necessarily equal to the original
327 // destination alpha.
328 case SkXfermode::kClear_Mode:
329 case SkXfermode::kSrc_Mode:
330 case SkXfermode::kSrcIn_Mode:
331 case SkXfermode::kDstIn_Mode:
332 case SkXfermode::kSrcOut_Mode:
333 case SkXfermode::kDstATop_Mode:
334 case SkXfermode::kModulate_Mode:
335 return true;
336 break;
337 default:
338 break;
339 }
340 }
341 }
342 return false;
mtkleind910f542014-08-22 09:06:34 -0700343 }
344
mtklein533eb782014-08-27 10:39:42 -0700345 Bounds popSaveBlock() {
mtklein828ce1f2014-08-13 12:58:45 -0700346 // We're done the Save block. Apply the block's bounds to all control ops inside it.
347 SaveBounds sb;
348 fSaveStack.pop(&sb);
mtkleind910f542014-08-22 09:06:34 -0700349
mtklein828ce1f2014-08-13 12:58:45 -0700350 while (sb.controlOps --> 0) {
robertphillips4d52afe2014-11-03 08:19:44 -0800351 this->popControl(sb.bounds);
mtklein828ce1f2014-08-13 12:58:45 -0700352 }
353
354 // This whole Save block may be part another Save block.
robertphillips4d52afe2014-11-03 08:19:44 -0800355 this->updateSaveBounds(sb.bounds);
mtklein828ce1f2014-08-13 12:58:45 -0700356
357 // If called from a real Restore (not a phony one for balance), it'll need the bounds.
robertphillips4d52afe2014-11-03 08:19:44 -0800358 return sb.bounds;
mtklein828ce1f2014-08-13 12:58:45 -0700359 }
360
361 void pushControl() {
362 fControlIndices.push(fCurrentOp);
363 if (!fSaveStack.isEmpty()) {
364 fSaveStack.top().controlOps++;
365 }
366 }
367
mtklein533eb782014-08-27 10:39:42 -0700368 void popControl(const Bounds& bounds) {
mtklein828ce1f2014-08-13 12:58:45 -0700369 fBounds[fControlIndices.top()] = bounds;
370 fControlIndices.pop();
371 }
372
mtklein533eb782014-08-27 10:39:42 -0700373 void updateSaveBounds(const Bounds& bounds) {
mtklein828ce1f2014-08-13 12:58:45 -0700374 // If we're in a Save block, expand its bounds to cover these bounds too.
375 if (!fSaveStack.isEmpty()) {
376 fSaveStack.top().bounds.join(bounds);
377 }
378 }
379
mtklein131a22b2014-08-25 14:16:15 -0700380 // FIXME: this method could use better bounds
mtklein533eb782014-08-27 10:39:42 -0700381 Bounds bounds(const DrawText&) const { return fCurrentClipBounds; }
mtklein68199a22014-08-25 13:49:29 -0700382
robertphillips4d52afe2014-11-03 08:19:44 -0800383 Bounds bounds(const Clear&) const { return fCullRect; } // Ignores the clip.
mtklein533eb782014-08-27 10:39:42 -0700384 Bounds bounds(const DrawPaint&) const { return fCurrentClipBounds; }
385 Bounds bounds(const NoOp&) const { return Bounds::MakeEmpty(); } // NoOps don't draw.
mtklein828ce1f2014-08-13 12:58:45 -0700386
mtklein533eb782014-08-27 10:39:42 -0700387 Bounds bounds(const DrawSprite& op) const {
mtklein131a22b2014-08-25 14:16:15 -0700388 const SkBitmap& bm = op.bitmap;
mtklein533eb782014-08-27 10:39:42 -0700389 return Bounds::MakeXYWH(op.left, op.top, bm.width(), bm.height()); // Ignores the matrix.
mtklein131a22b2014-08-25 14:16:15 -0700390 }
391
mtklein533eb782014-08-27 10:39:42 -0700392 Bounds bounds(const DrawRect& op) const { return this->adjustAndMap(op.rect, &op.paint); }
393 Bounds bounds(const DrawOval& op) const { return this->adjustAndMap(op.oval, &op.paint); }
394 Bounds bounds(const DrawRRect& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700395 return this->adjustAndMap(op.rrect.rect(), &op.paint);
396 }
mtklein533eb782014-08-27 10:39:42 -0700397 Bounds bounds(const DrawDRRect& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700398 return this->adjustAndMap(op.outer.rect(), &op.paint);
399 }
piotaixr65151752014-10-16 11:58:39 -0700400 Bounds bounds(const DrawImage& op) const {
401 const SkImage* image = op.image;
402 SkRect rect = SkRect::MakeXYWH(op.left, op.top, image->width(), image->height());
mtklein62b67ae2014-08-18 11:10:37 -0700403
piotaixr65151752014-10-16 11:58:39 -0700404 return this->adjustAndMap(rect, op.paint);
405 }
406 Bounds bounds(const DrawImageRect& op) const {
407 return this->adjustAndMap(op.dst, op.paint);
408 }
mtklein533eb782014-08-27 10:39:42 -0700409 Bounds bounds(const DrawBitmapRectToRect& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700410 return this->adjustAndMap(op.dst, op.paint);
411 }
mtklein533eb782014-08-27 10:39:42 -0700412 Bounds bounds(const DrawBitmapNine& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700413 return this->adjustAndMap(op.dst, op.paint);
414 }
mtklein533eb782014-08-27 10:39:42 -0700415 Bounds bounds(const DrawBitmap& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700416 const SkBitmap& bm = op.bitmap;
417 return this->adjustAndMap(SkRect::MakeXYWH(op.left, op.top, bm.width(), bm.height()),
418 op.paint);
419 }
mtklein533eb782014-08-27 10:39:42 -0700420 Bounds bounds(const DrawBitmapMatrix& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700421 const SkBitmap& bm = op.bitmap;
422 SkRect dst = SkRect::MakeWH(bm.width(), bm.height());
423 op.matrix.mapRect(&dst);
424 return this->adjustAndMap(dst, op.paint);
425 }
426
mtklein533eb782014-08-27 10:39:42 -0700427 Bounds bounds(const DrawPath& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700428 return op.path.isInverseFillType() ? fCurrentClipBounds
429 : this->adjustAndMap(op.path.getBounds(), &op.paint);
430 }
mtklein533eb782014-08-27 10:39:42 -0700431 Bounds bounds(const DrawPoints& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700432 SkRect dst;
433 dst.set(op.pts, op.count);
434
435 // Pad the bounding box a little to make sure hairline points' bounds aren't empty.
436 SkScalar stroke = SkMaxScalar(op.paint.getStrokeWidth(), 0.01f);
437 dst.outset(stroke/2, stroke/2);
438
439 return this->adjustAndMap(dst, &op.paint);
440 }
mtklein533eb782014-08-27 10:39:42 -0700441 Bounds bounds(const DrawPatch& op) const {
mtklein131a22b2014-08-25 14:16:15 -0700442 SkRect dst;
443 dst.set(op.cubics, SkPatchUtils::kNumCtrlPts);
444 return this->adjustAndMap(dst, &op.paint);
445 }
mtklein533eb782014-08-27 10:39:42 -0700446 Bounds bounds(const DrawVertices& op) const {
mtklein131a22b2014-08-25 14:16:15 -0700447 SkRect dst;
448 dst.set(op.vertices, op.vertexCount);
449 return this->adjustAndMap(dst, &op.paint);
450 }
451
mtklein533eb782014-08-27 10:39:42 -0700452 Bounds bounds(const DrawPicture& op) const {
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700453 SkRect dst = op.picture->cullRect();
mtklein131a22b2014-08-25 14:16:15 -0700454 if (op.matrix) {
455 op.matrix->mapRect(&dst);
456 }
457 return this->adjustAndMap(dst, op.paint);
458 }
mtklein62b67ae2014-08-18 11:10:37 -0700459
mtklein533eb782014-08-27 10:39:42 -0700460 Bounds bounds(const DrawPosText& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700461 const int N = op.paint.countText(op.text, op.byteLength);
462 if (N == 0) {
mtklein533eb782014-08-27 10:39:42 -0700463 return Bounds::MakeEmpty();
mtklein62b67ae2014-08-18 11:10:37 -0700464 }
465
466 SkRect dst;
mtklein937c9c72014-09-02 15:19:48 -0700467 dst.set(op.pos, N);
mtklein62b67ae2014-08-18 11:10:37 -0700468 AdjustTextForFontMetrics(&dst, op.paint);
469 return this->adjustAndMap(dst, &op.paint);
470 }
mtklein533eb782014-08-27 10:39:42 -0700471 Bounds bounds(const DrawPosTextH& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700472 const int N = op.paint.countText(op.text, op.byteLength);
473 if (N == 0) {
mtklein533eb782014-08-27 10:39:42 -0700474 return Bounds::MakeEmpty();
mtklein62b67ae2014-08-18 11:10:37 -0700475 }
476
477 SkScalar left = op.xpos[0], right = op.xpos[0];
478 for (int i = 1; i < N; i++) {
479 left = SkMinScalar(left, op.xpos[i]);
480 right = SkMaxScalar(right, op.xpos[i]);
481 }
482 SkRect dst = { left, op.y, right, op.y };
483 AdjustTextForFontMetrics(&dst, op.paint);
484 return this->adjustAndMap(dst, &op.paint);
485 }
mtklein533eb782014-08-27 10:39:42 -0700486 Bounds bounds(const DrawTextOnPath& op) const {
mtklein131a22b2014-08-25 14:16:15 -0700487 SkRect dst = op.path.getBounds();
488
mtkleined167ac2014-10-29 16:07:10 -0700489 // Pad all sides by the maximum padding in any direction we'd normally apply.
mtklein131a22b2014-08-25 14:16:15 -0700490 SkRect pad = { 0, 0, 0, 0};
491 AdjustTextForFontMetrics(&pad, op.paint);
mtkleined167ac2014-10-29 16:07:10 -0700492
493 // That maximum padding happens to always be the right pad today.
494 SkASSERT(pad.fLeft == -pad.fRight);
495 SkASSERT(pad.fTop == -pad.fBottom);
496 SkASSERT(pad.fRight > pad.fBottom);
497 dst.outset(pad.fRight, pad.fRight);
mtklein131a22b2014-08-25 14:16:15 -0700498
499 return this->adjustAndMap(dst, &op.paint);
500 }
501
mtklein533eb782014-08-27 10:39:42 -0700502 Bounds bounds(const DrawTextBlob& op) const {
mtklein131a22b2014-08-25 14:16:15 -0700503 SkRect dst = op.blob->bounds();
504 dst.offset(op.x, op.y);
mtklein131a22b2014-08-25 14:16:15 -0700505 return this->adjustAndMap(dst, &op.paint);
506 }
mtklein62b67ae2014-08-18 11:10:37 -0700507
508 static void AdjustTextForFontMetrics(SkRect* rect, const SkPaint& paint) {
mtkleined167ac2014-10-29 16:07:10 -0700509#ifdef SK_DEBUG
510 SkRect correct = *rect;
511#endif
512 // crbug.com/373785 ~~> xPad = 4x yPad
513 // crbug.com/424824 ~~> bump yPad from 2x text size to 2.5x
514 const SkScalar yPad = 2.5f * paint.getTextSize(),
515 xPad = 4.0f * yPad;
516 rect->outset(xPad, yPad);
caryclark9a657fa2014-08-20 05:24:29 -0700517#ifdef SK_DEBUG
mtklein62b67ae2014-08-18 11:10:37 -0700518 SkPaint::FontMetrics metrics;
519 paint.getFontMetrics(&metrics);
mtkleined167ac2014-10-29 16:07:10 -0700520 correct.fLeft += metrics.fXMin;
521 correct.fTop += metrics.fTop;
522 correct.fRight += metrics.fXMax;
523 correct.fBottom += metrics.fBottom;
mtkleind13291a2014-08-21 14:46:49 -0700524 // See skia:2862 for why we ignore small text sizes.
mtkleined167ac2014-10-29 16:07:10 -0700525 SkASSERTF(paint.getTextSize() < 0.001f || rect->contains(correct),
526 "%f %f %f %f vs. %f %f %f %f\n",
527 -xPad, -yPad, +xPad, +yPad,
528 metrics.fXMin, metrics.fTop, metrics.fXMax, metrics.fBottom);
mtkleina19afb42014-08-19 17:47:14 -0700529#endif
mtklein62b67ae2014-08-18 11:10:37 -0700530 }
531
mtklein479601b2014-08-18 08:45:33 -0700532 // Returns true if rect was meaningfully adjusted for the effects of paint,
533 // false if the paint could affect the rect in unknown ways.
534 static bool AdjustForPaint(const SkPaint* paint, SkRect* rect) {
mtkleina723b572014-08-15 11:49:49 -0700535 if (paint) {
536 if (paint->canComputeFastBounds()) {
mtklein479601b2014-08-18 08:45:33 -0700537 *rect = paint->computeFastBounds(*rect, rect);
538 return true;
mtkleina723b572014-08-15 11:49:49 -0700539 }
mtklein479601b2014-08-18 08:45:33 -0700540 return false;
541 }
542 return true;
543 }
544
mtklein8e393bf2014-10-01 12:48:58 -0700545 bool adjustForSaveLayerPaints(SkRect* rect, int savesToIgnore = 0) const {
546 for (int i = fSaveStack.count() - 1 - savesToIgnore; i >= 0; i--) {
Mike Klein271a0302014-09-23 15:28:38 -0400547 if (!AdjustForPaint(fSaveStack[i].paint, rect)) {
548 return false;
549 }
550 }
551 return true;
552 }
553
robertphillips4e8e3422014-11-12 06:46:08 -0800554 const unsigned fNumRecords;
mtkleina723b572014-08-15 11:49:49 -0700555
robertphillips4d52afe2014-11-03 08:19:44 -0800556 // We do not guarantee anything for operations outside of the cull rect
557 const SkRect fCullRect;
558
mtklein533eb782014-08-27 10:39:42 -0700559 // Conservative identity-space bounds for each op in the SkRecord.
560 SkAutoTMalloc<Bounds> fBounds;
mtkleina723b572014-08-15 11:49:49 -0700561
562 // We walk fCurrentOp through the SkRecord, as we go using updateCTM()
563 // and updateClipBounds() to maintain the exact CTM (fCTM) and conservative
mtklein533eb782014-08-27 10:39:42 -0700564 // identity-space bounds of the current clip (fCurrentClipBounds).
mtklein828ce1f2014-08-13 12:58:45 -0700565 unsigned fCurrentOp;
mtklein6332f1d2014-08-19 07:09:40 -0700566 const SkMatrix* fCTM;
mtklein533eb782014-08-27 10:39:42 -0700567 Bounds fCurrentClipBounds;
mtkleina723b572014-08-15 11:49:49 -0700568
569 // Used to track the bounds of Save/Restore blocks and the control ops inside them.
mtklein828ce1f2014-08-13 12:58:45 -0700570 SkTDArray<SaveBounds> fSaveStack;
571 SkTDArray<unsigned> fControlIndices;
mtklein5ad6ee12014-08-11 08:08:43 -0700572};
573
robertphillips4e8e3422014-11-12 06:46:08 -0800574#if SK_SUPPORT_GPU
575// SkRecord visitor to gather saveLayer/restore information.
576class CollectLayers : SkNoncopyable {
577public:
578 CollectLayers(const SkRect& cullRect, const SkRecord& record, GrAccelData* accelData)
579 : fSaveLayersInStack(0)
580 , fAccelData(accelData)
581 , fFillBounds(cullRect, record) {
582 }
583
584 void setCurrentOp(unsigned currentOp) { fFillBounds.setCurrentOp(currentOp); }
585
586 void cleanUp(SkBBoxHierarchy* bbh) {
587 // fFillBounds must perform its cleanUp first so that all the bounding
588 // boxes associated with unbalanced restores are updated (prior to
589 // fetching their bound in popSaveLayerInfo).
590 fFillBounds.cleanUp(bbh);
591
592 while (!fSaveLayerStack.isEmpty()) {
593 this->popSaveLayerInfo();
594 }
595 }
596
597 template <typename T> void operator()(const T& op) {
598 fFillBounds(op);
599 this->trackSaveLayers(op);
600 }
601
602private:
603 struct SaveLayerInfo {
604 SaveLayerInfo() { }
605 SaveLayerInfo(int opIndex, bool isSaveLayer, const SkPaint* paint,
606 const FillBounds::Bounds& clipBound)
607 : fStartIndex(opIndex)
608 , fIsSaveLayer(isSaveLayer)
609 , fHasNestedSaveLayer(false)
610 , fPaint(paint)
611 , fClipBound(clipBound) {
612 }
613
614 int fStartIndex;
615 bool fIsSaveLayer;
616 bool fHasNestedSaveLayer;
617 const SkPaint* fPaint;
618 FillBounds::Bounds fClipBound;
619 };
620
621 template <typename T> void trackSaveLayers(const T& op) {
622 /* most ops aren't involved in saveLayers */
623 }
624 void trackSaveLayers(const Save& s) { this->pushSaveLayerInfo(false, NULL); }
625 void trackSaveLayers(const SaveLayer& sl) { this->pushSaveLayerInfo(true, sl.paint); }
626 void trackSaveLayers(const Restore& r) { this->popSaveLayerInfo(); }
627
628 void trackSaveLayers(const DrawPicture& dp) {
629 // For sub-pictures, we wrap their layer information within the parent
630 // picture's rendering hierarchy
631 SkPicture::AccelData::Key key = GrAccelData::ComputeAccelDataKey();
632
633 const GrAccelData* childData =
634 static_cast<const GrAccelData*>(dp.picture->EXPERIMENTAL_getAccelData(key));
635 if (!childData) {
636 // If the child layer hasn't been generated with saveLayer data we
637 // assume the worst (i.e., that it does contain layers which nest
638 // inside existing layers). Layers within sub-pictures that don't
639 // have saveLayer data cannot be hoisted.
640 // TODO: could the analysis data be use to fine tune this?
641 this->updateStackForSaveLayer();
642 return;
643 }
644
645 for (int i = 0; i < childData->numSaveLayers(); ++i) {
646 const GrAccelData::SaveLayerInfo& src = childData->saveLayerInfo(i);
647
648 FillBounds::Bounds newClip(fFillBounds.currentClipBounds());
649
650 if (!newClip.intersect(fFillBounds.adjustAndMap(src.fBounds, dp.paint))) {
651 continue;
652 }
653
654 this->updateStackForSaveLayer();
655
656 GrAccelData::SaveLayerInfo& dst = fAccelData->addSaveLayerInfo();
657
658 // If src.fPicture is NULL the layer is in dp.picture; otherwise
659 // it belongs to a sub-picture.
660 dst.fPicture = src.fPicture ? src.fPicture : static_cast<const SkPicture*>(dp.picture);
661 dst.fPicture->ref();
662 dst.fBounds = newClip;
663 dst.fLocalMat = src.fLocalMat;
664 dst.fPreMat = src.fPreMat;
665 dst.fPreMat.postConcat(fFillBounds.ctm());
666 if (src.fPaint) {
667 dst.fPaint = SkNEW_ARGS(SkPaint, (*src.fPaint));
668 }
669 dst.fSaveLayerOpID = src.fSaveLayerOpID;
670 dst.fRestoreOpID = src.fRestoreOpID;
671 dst.fHasNestedLayers = src.fHasNestedLayers;
672 dst.fIsNested = fSaveLayersInStack > 0 || src.fIsNested;
673 }
674 }
675
676 // Inform all the saveLayers already on the stack that they now have a
677 // nested saveLayer inside them
678 void updateStackForSaveLayer() {
679 for (int index = fSaveLayerStack.count() - 1; index >= 0; --index) {
680 if (fSaveLayerStack[index].fHasNestedSaveLayer) {
681 break;
682 }
683 fSaveLayerStack[index].fHasNestedSaveLayer = true;
684 if (fSaveLayerStack[index].fIsSaveLayer) {
685 break;
686 }
687 }
688 }
689
690 void pushSaveLayerInfo(bool isSaveLayer, const SkPaint* paint) {
691 if (isSaveLayer) {
692 this->updateStackForSaveLayer();
693 ++fSaveLayersInStack;
694 }
695
696 fSaveLayerStack.push(SaveLayerInfo(fFillBounds.currentOp(), isSaveLayer, paint,
697 fFillBounds.currentClipBounds()));
698 }
699
700 void popSaveLayerInfo() {
701 if (fSaveLayerStack.count() <= 0) {
702 SkASSERT(false);
703 return;
704 }
705
706 SaveLayerInfo sli;
707 fSaveLayerStack.pop(&sli);
708
709 if (!sli.fIsSaveLayer) {
710 return;
711 }
712
713 --fSaveLayersInStack;
714
715 GrAccelData::SaveLayerInfo& slInfo = fAccelData->addSaveLayerInfo();
716
717 SkASSERT(NULL == slInfo.fPicture); // This layer is in the top-most picture
718
719 slInfo.fBounds = fFillBounds.getBounds(sli.fStartIndex);
720 slInfo.fBounds.intersect(sli.fClipBound);
721 slInfo.fLocalMat = fFillBounds.ctm();
722 slInfo.fPreMat = SkMatrix::I();
723 if (sli.fPaint) {
724 slInfo.fPaint = SkNEW_ARGS(SkPaint, (*sli.fPaint));
725 }
726 slInfo.fSaveLayerOpID = sli.fStartIndex;
727 slInfo.fRestoreOpID = fFillBounds.currentOp();
728 slInfo.fHasNestedLayers = sli.fHasNestedSaveLayer;
729 slInfo.fIsNested = fSaveLayersInStack > 0;
730 }
731
732 // Used to collect saveLayer information for layer hoisting
733 int fSaveLayersInStack;
734 SkTDArray<SaveLayerInfo> fSaveLayerStack;
735 GrAccelData* fAccelData;
736
737 SkRecords::FillBounds fFillBounds;
738};
739#endif
740
commit-bot@chromium.org27f6b0d2014-05-09 14:59:29 +0000741} // namespace SkRecords
mtklein5ad6ee12014-08-11 08:08:43 -0700742
robertphillips4d52afe2014-11-03 08:19:44 -0800743void SkRecordFillBounds(const SkRect& cullRect, const SkRecord& record, SkBBoxHierarchy* bbh) {
robertphillips4e8e3422014-11-12 06:46:08 -0800744 SkRecords::FillBounds visitor(cullRect, record);
745
746 for (unsigned curOp = 0; curOp < record.count(); curOp++) {
747 visitor.setCurrentOp(curOp);
748 record.visit<void>(curOp, visitor);
749 }
750
751 visitor.cleanUp(bbh);
mtklein5ad6ee12014-08-11 08:08:43 -0700752}
robertphillips4e8e3422014-11-12 06:46:08 -0800753
754#if SK_SUPPORT_GPU
755void SkRecordComputeLayers(const SkRect& cullRect, const SkRecord& record,
756 SkBBoxHierarchy* bbh, GrAccelData* data) {
757 SkRecords::CollectLayers visitor(cullRect, record, data);
758
759 for (unsigned curOp = 0; curOp < record.count(); curOp++) {
760 visitor.setCurrentOp(curOp);
761 record.visit<void>(curOp, visitor);
762 }
763
764 visitor.cleanUp(bbh);
765}
766#endif
767