blob: 77235fda415577eb1c6288c8de87cdd9d91a4492 [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
robertphillips82365912014-11-12 09:32:34 -08008#include "SkLayerInfo.h"
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +00009#include "SkRecordDraw.h"
mtklein131a22b2014-08-25 14:16:15 -070010#include "SkPatchUtils.h"
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000011
mtklein5ad6ee12014-08-11 08:08:43 -070012void SkRecordDraw(const SkRecord& record,
13 SkCanvas* canvas,
14 const SkBBoxHierarchy* bbh,
15 SkDrawPictureCallback* callback) {
Mike Kleinc11530e2014-06-24 11:29:06 -040016 SkAutoCanvasRestore saveRestore(canvas, true /*save now, restore at exit*/);
mtklein5ad6ee12014-08-11 08:08:43 -070017
bsalomon49f085d2014-09-05 13:34:00 -070018 if (bbh) {
mtklein5ad6ee12014-08-11 08:08:43 -070019 // Draw only ops that affect pixels in the canvas's current clip.
mtklein3e8232b2014-08-18 13:39:11 -070020 // The SkRecord and BBH were recorded in identity space. This canvas
21 // is not necessarily in that same space. getClipBounds() returns us
22 // this canvas' clip bounds transformed back into identity space, which
23 // lets us query the BBH.
junova41d3c32014-10-30 11:44:19 -070024 SkRect query;
25 if (!canvas->getClipBounds(&query)) {
26 return;
27 }
mtklein3e8232b2014-08-18 13:39:11 -070028
mtklein6bd41962014-10-02 07:41:56 -070029 SkTDArray<unsigned> ops;
mtkleina723b572014-08-15 11:49:49 -070030 bbh->search(query, &ops);
mtklein5ad6ee12014-08-11 08:08:43 -070031
mtklein5ad6ee12014-08-11 08:08:43 -070032 SkRecords::Draw draw(canvas);
33 for (int i = 0; i < ops.count(); i++) {
bsalomon49f085d2014-09-05 13:34:00 -070034 if (callback && callback->abortDrawing()) {
mtklein5ad6ee12014-08-11 08:08:43 -070035 return;
36 }
danakjd239d422014-11-03 12:43:30 -080037 // This visit call uses the SkRecords::Draw::operator() to call
38 // methods on the |canvas|, wrapped by methods defined with the
39 // DRAW() macro.
mtklein6bd41962014-10-02 07:41:56 -070040 record.visit<void>(ops[i], draw);
mtklein5ad6ee12014-08-11 08:08:43 -070041 }
42 } else {
43 // Draw all ops.
mtklein00f30bd2014-09-02 12:03:31 -070044 SkRecords::Draw draw(canvas);
45 for (unsigned i = 0; i < record.count(); i++) {
bsalomon49f085d2014-09-05 13:34:00 -070046 if (callback && callback->abortDrawing()) {
mtklein5ad6ee12014-08-11 08:08:43 -070047 return;
48 }
danakjd239d422014-11-03 12:43:30 -080049 // This visit call uses the SkRecords::Draw::operator() to call
50 // methods on the |canvas|, wrapped by methods defined with the
51 // DRAW() macro.
mtklein00f30bd2014-09-02 12:03:31 -070052 record.visit<void>(i, draw);
mtklein5ad6ee12014-08-11 08:08:43 -070053 }
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000054 }
commit-bot@chromium.org27f6b0d2014-05-09 14:59:29 +000055}
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000056
mtklein00f30bd2014-09-02 12:03:31 -070057void SkRecordPartialDraw(const SkRecord& record,
58 SkCanvas* canvas,
59 const SkRect& clearRect,
robertphillips4815fe52014-09-16 10:32:43 -070060 unsigned start, unsigned stop,
61 const SkMatrix& initialCTM) {
mtklein00f30bd2014-09-02 12:03:31 -070062 SkAutoCanvasRestore saveRestore(canvas, true /*save now, restore at exit*/);
63
64 stop = SkTMin(stop, record.count());
robertphillips4815fe52014-09-16 10:32:43 -070065 SkRecords::PartialDraw draw(canvas, clearRect, initialCTM);
mtklein00f30bd2014-09-02 12:03:31 -070066 for (unsigned i = start; i < stop; i++) {
67 record.visit<void>(i, draw);
68 }
69}
70
commit-bot@chromium.org27f6b0d2014-05-09 14:59:29 +000071namespace SkRecords {
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000072
mtklein7cdc1ee2014-07-07 10:41:04 -070073// FIXME: SkBitmaps are stateful, so we need to copy them to play back in multiple threads.
74static SkBitmap shallow_copy(const SkBitmap& bitmap) {
75 return bitmap;
76}
77
commit-bot@chromium.org2e0c32a2014-04-28 16:19:45 +000078// NoOps draw nothing.
commit-bot@chromium.org27f6b0d2014-05-09 14:59:29 +000079template <> void Draw::draw(const NoOp&) {}
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000080
commit-bot@chromium.org27f6b0d2014-05-09 14:59:29 +000081#define DRAW(T, call) template <> void Draw::draw(const T& r) { fCanvas->call; }
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000082DRAW(Restore, restore());
Florin Malita5f6102d2014-06-30 10:13:28 -040083DRAW(Save, save());
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000084DRAW(SaveLayer, saveLayer(r.bounds, r.paint, r.flags));
85DRAW(PopCull, popCull());
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000086DRAW(PushCull, pushCull(r.rect));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000087DRAW(Clear, clear(r.color));
commit-bot@chromium.org99bd7d82014-05-19 15:51:12 +000088DRAW(SetMatrix, setMatrix(SkMatrix::Concat(fInitialCTM, r.matrix)));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000089
90DRAW(ClipPath, clipPath(r.path, r.op, r.doAA));
91DRAW(ClipRRect, clipRRect(r.rrect, r.op, r.doAA));
92DRAW(ClipRect, clipRect(r.rect, r.op, r.doAA));
93DRAW(ClipRegion, clipRegion(r.region, r.op));
94
mtklein5f0e8222014-08-22 11:44:26 -070095DRAW(BeginCommentGroup, beginCommentGroup(r.description));
96DRAW(AddComment, addComment(r.key, r.value));
97DRAW(EndCommentGroup, endCommentGroup());
98
mtklein7cdc1ee2014-07-07 10:41:04 -070099DRAW(DrawBitmap, drawBitmap(shallow_copy(r.bitmap), r.left, r.top, r.paint));
100DRAW(DrawBitmapMatrix, drawBitmapMatrix(shallow_copy(r.bitmap), r.matrix, r.paint));
101DRAW(DrawBitmapNine, drawBitmapNine(shallow_copy(r.bitmap), r.center, r.dst, r.paint));
102DRAW(DrawBitmapRectToRect,
103 drawBitmapRectToRect(shallow_copy(r.bitmap), r.src, r.dst, r.paint, r.flags));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +0000104DRAW(DrawDRRect, drawDRRect(r.outer, r.inner, r.paint));
piotaixr65151752014-10-16 11:58:39 -0700105DRAW(DrawImage, drawImage(r.image, r.left, r.top, r.paint));
106DRAW(DrawImageRect, drawImageRect(r.image, r.src, r.dst, r.paint));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +0000107DRAW(DrawOval, drawOval(r.oval, r.paint));
108DRAW(DrawPaint, drawPaint(r.paint));
109DRAW(DrawPath, drawPath(r.path, r.paint));
mtklein9b222a52014-09-18 11:16:31 -0700110DRAW(DrawPatch, drawPatch(r.cubics, r.colors, r.texCoords, r.xmode, r.paint));
reedd5fa1a42014-08-09 11:08:05 -0700111DRAW(DrawPicture, drawPicture(r.picture, r.matrix, r.paint));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +0000112DRAW(DrawPoints, drawPoints(r.mode, r.count, r.pts, r.paint));
113DRAW(DrawPosText, drawPosText(r.text, r.byteLength, r.pos, r.paint));
114DRAW(DrawPosTextH, drawPosTextH(r.text, r.byteLength, r.xpos, r.y, r.paint));
115DRAW(DrawRRect, drawRRect(r.rrect, r.paint));
116DRAW(DrawRect, drawRect(r.rect, r.paint));
mtklein7cdc1ee2014-07-07 10:41:04 -0700117DRAW(DrawSprite, drawSprite(shallow_copy(r.bitmap), r.left, r.top, r.paint));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +0000118DRAW(DrawText, drawText(r.text, r.byteLength, r.x, r.y, r.paint));
fmalita00d5c2c2014-08-21 08:53:26 -0700119DRAW(DrawTextBlob, drawTextBlob(r.blob, r.x, r.y, r.paint));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +0000120DRAW(DrawTextOnPath, drawTextOnPath(r.text, r.byteLength, r.path, r.matrix, r.paint));
121DRAW(DrawVertices, drawVertices(r.vmode, r.vertexCount, r.vertices, r.texs, r.colors,
122 r.xmode.get(), r.indices, r.indexCount, r.paint));
mtklein29dfaa82014-09-04 14:12:44 -0700123DRAW(DrawData, drawData(r.data, r.length));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +0000124#undef DRAW
125
mtklein5ad6ee12014-08-11 08:08:43 -0700126// This is an SkRecord visitor that fills an SkBBoxHierarchy.
mtklein828ce1f2014-08-13 12:58:45 -0700127//
128// The interesting part here is how to calculate bounds for ops which don't
129// have intrinsic bounds. What is the bounds of a Save or a Translate?
130//
131// We answer this by thinking about a particular definition of bounds: if I
132// don't execute this op, pixels in this rectangle might draw incorrectly. So
133// the bounds of a Save, a Translate, a Restore, etc. are the union of the
134// bounds of Draw* ops that they might have an effect on. For any given
135// Save/Restore block, the bounds of the Save, the Restore, and any other
136// non-drawing ("control") ops inside are exactly the union of the bounds of
137// the drawing ops inside that block.
138//
139// To implement this, we keep a stack of active Save blocks. As we consume ops
140// inside the Save/Restore block, drawing ops are unioned with the bounds of
141// the block, and control ops are stashed away for later. When we finish the
142// block with a Restore, our bounds are complete, and we go back and fill them
143// in for all the control ops we stashed away.
mtklein5ad6ee12014-08-11 08:08:43 -0700144class FillBounds : SkNoncopyable {
145public:
robertphillips4e8e3422014-11-12 06:46:08 -0800146 FillBounds(const SkRect& cullRect, const SkRecord& record)
147 : fNumRecords(record.count())
148 , fCullRect(cullRect)
robertphillips4d52afe2014-11-03 08:19:44 -0800149 , fBounds(record.count()) {
mtklein828ce1f2014-08-13 12:58:45 -0700150 // Calculate bounds for all ops. This won't go quite in order, so we'll need
151 // to store the bounds separately then feed them in to the BBH later in order.
mtklein6332f1d2014-08-19 07:09:40 -0700152 fCTM = &SkMatrix::I();
robertphillips4d52afe2014-11-03 08:19:44 -0800153 fCurrentClipBounds = fCullRect;
robertphillips4e8e3422014-11-12 06:46:08 -0800154 }
mtklein5ad6ee12014-08-11 08:08:43 -0700155
robertphillips4e8e3422014-11-12 06:46:08 -0800156 void setCurrentOp(unsigned currentOp) { fCurrentOp = currentOp; }
157
158 void cleanUp(SkBBoxHierarchy* bbh) {
mtklein828ce1f2014-08-13 12:58:45 -0700159 // If we have any lingering unpaired Saves, simulate restores to make
160 // sure all ops in those Save blocks have their bounds calculated.
161 while (!fSaveStack.isEmpty()) {
162 this->popSaveBlock();
163 }
164
165 // Any control ops not part of any Save/Restore block draw everywhere.
166 while (!fControlIndices.isEmpty()) {
robertphillips4d52afe2014-11-03 08:19:44 -0800167 this->popControl(fCullRect);
mtklein828ce1f2014-08-13 12:58:45 -0700168 }
169
170 // Finally feed all stored bounds into the BBH. They'll be returned in this order.
robertphillips89108792014-11-17 08:16:15 -0800171 if (bbh) {
172 bbh->insert(&fBounds, fNumRecords);
173 }
mtklein828ce1f2014-08-13 12:58:45 -0700174 }
mtklein5ad6ee12014-08-11 08:08:43 -0700175
mtkleina723b572014-08-15 11:49:49 -0700176 template <typename T> void operator()(const T& op) {
177 this->updateCTM(op);
178 this->updateClipBounds(op);
179 this->trackBounds(op);
mtklein5ad6ee12014-08-11 08:08:43 -0700180 }
181
mtklein533eb782014-08-27 10:39:42 -0700182 // In this file, SkRect are in local coordinates, Bounds are translated back to identity space.
183 typedef SkRect Bounds;
184
robertphillips4e8e3422014-11-12 06:46:08 -0800185 unsigned currentOp() const { return fCurrentOp; }
186 const SkMatrix& ctm() const { return *fCTM; }
robertphillips4e8e3422014-11-12 06:46:08 -0800187 const Bounds& getBounds(unsigned index) const { return fBounds[index]; }
188
189 // Adjust rect for all paints that may affect its geometry, then map it to identity space.
190 Bounds adjustAndMap(SkRect rect, const SkPaint* paint) const {
191 // Inverted rectangles really confuse our BBHs.
192 rect.sort();
193
194 // Adjust the rect for its own paint.
195 if (!AdjustForPaint(paint, &rect)) {
196 // The paint could do anything to our bounds. The only safe answer is the current clip.
197 return fCurrentClipBounds;
198 }
199
200 // Adjust rect for all the paints from the SaveLayers we're inside.
201 if (!this->adjustForSaveLayerPaints(&rect)) {
202 // Same deal as above.
203 return fCurrentClipBounds;
204 }
205
206 // Map the rect back to identity space.
207 fCTM->mapRect(&rect);
208
209 // Nothing can draw outside the current clip.
210 // (Only bounded ops call into this method, so oddballs like Clear don't matter here.)
211 rect.intersect(fCurrentClipBounds);
212 return rect;
213 }
214
215private:
mtklein828ce1f2014-08-13 12:58:45 -0700216 struct SaveBounds {
mtkleina723b572014-08-15 11:49:49 -0700217 int controlOps; // Number of control ops in this Save block, including the Save.
mtklein533eb782014-08-27 10:39:42 -0700218 Bounds bounds; // Bounds of everything in the block.
mtkleina723b572014-08-15 11:49:49 -0700219 const SkPaint* paint; // Unowned. If set, adjusts the bounds of all ops in this block.
mtklein828ce1f2014-08-13 12:58:45 -0700220 };
221
mtklein8e393bf2014-10-01 12:48:58 -0700222 // Only Restore and SetMatrix change the CTM.
223 template <typename T> void updateCTM(const T&) {}
mtklein6332f1d2014-08-19 07:09:40 -0700224 void updateCTM(const Restore& op) { fCTM = &op.matrix; }
225 void updateCTM(const SetMatrix& op) { fCTM = &op.matrix; }
mtkleina723b572014-08-15 11:49:49 -0700226
mtklein8e393bf2014-10-01 12:48:58 -0700227 // Most ops don't change the clip.
228 template <typename T> void updateClipBounds(const T&) {}
Mike Klein271a0302014-09-23 15:28:38 -0400229
mtklein8e393bf2014-10-01 12:48:58 -0700230 // Clip{Path,RRect,Rect,Region} obviously change the clip. They all know their bounds already.
231 void updateClipBounds(const ClipPath& op) { this->updateClipBoundsForClipOp(op.devBounds); }
232 void updateClipBounds(const ClipRRect& op) { this->updateClipBoundsForClipOp(op.devBounds); }
233 void updateClipBounds(const ClipRect& op) { this->updateClipBoundsForClipOp(op.devBounds); }
234 void updateClipBounds(const ClipRegion& op) { this->updateClipBoundsForClipOp(op.devBounds); }
Mike Klein271a0302014-09-23 15:28:38 -0400235
mtklein8e393bf2014-10-01 12:48:58 -0700236 // The bounds of clip ops need to be adjusted for the paints of saveLayers they're inside.
237 void updateClipBoundsForClipOp(const SkIRect& devBounds) {
238 Bounds clip = SkRect::Make(devBounds);
Mike Klein271a0302014-09-23 15:28:38 -0400239 // We don't call adjustAndMap() because as its last step it would intersect the adjusted
240 // clip bounds with the previous clip, exactly what we can't do when the clip grows.
robertphillips4d52afe2014-11-03 08:19:44 -0800241 fCurrentClipBounds = this->adjustForSaveLayerPaints(&clip) ? clip : fCullRect;
Mike Klein271a0302014-09-23 15:28:38 -0400242 }
243
mtklein8e393bf2014-10-01 12:48:58 -0700244 // Restore holds the devBounds for the clip after the {save,saveLayer}/restore block completes.
245 void updateClipBounds(const Restore& op) {
246 // This is just like the clip ops above, but we need to skip the effects (if any) of our
247 // paired saveLayer (if it is one); it has not yet been popped off the save stack. Our
248 // devBounds reflect the state of the world after the saveLayer/restore block is done,
249 // so they are not affected by the saveLayer's paint.
250 const int kSavesToIgnore = 1;
251 Bounds clip = SkRect::Make(op.devBounds);
252 fCurrentClipBounds =
robertphillips4d52afe2014-11-03 08:19:44 -0800253 this->adjustForSaveLayerPaints(&clip, kSavesToIgnore) ? clip : fCullRect;
mtklein8e393bf2014-10-01 12:48:58 -0700254 }
255
Mike Klein271a0302014-09-23 15:28:38 -0400256 // We also take advantage of SaveLayer bounds when present to further cut the clip down.
mtkleina723b572014-08-15 11:49:49 -0700257 void updateClipBounds(const SaveLayer& op) {
258 if (op.bounds) {
Mike Klein271a0302014-09-23 15:28:38 -0400259 // adjustAndMap() intersects these layer bounds with the previous clip for us.
260 fCurrentClipBounds = this->adjustAndMap(*op.bounds, op.paint);
mtkleina723b572014-08-15 11:49:49 -0700261 }
262 }
mtklein6cfa73a2014-08-13 13:33:49 -0700263
mtklein828ce1f2014-08-13 12:58:45 -0700264 // The bounds of these ops must be calculated when we hit the Restore
265 // from the bounds of the ops in the same Save block.
mtkleina723b572014-08-15 11:49:49 -0700266 void trackBounds(const Save&) { this->pushSaveBlock(NULL); }
mtkleina723b572014-08-15 11:49:49 -0700267 void trackBounds(const SaveLayer& op) { this->pushSaveBlock(op.paint); }
268 void trackBounds(const Restore&) { fBounds[fCurrentOp] = this->popSaveBlock(); }
mtklein828ce1f2014-08-13 12:58:45 -0700269
mtklein68199a22014-08-25 13:49:29 -0700270 void trackBounds(const SetMatrix&) { this->pushControl(); }
271 void trackBounds(const ClipRect&) { this->pushControl(); }
272 void trackBounds(const ClipRRect&) { this->pushControl(); }
273 void trackBounds(const ClipPath&) { this->pushControl(); }
274 void trackBounds(const ClipRegion&) { this->pushControl(); }
275 void trackBounds(const PushCull&) { this->pushControl(); }
276 void trackBounds(const PopCull&) { this->pushControl(); }
277 void trackBounds(const BeginCommentGroup&) { this->pushControl(); }
278 void trackBounds(const AddComment&) { this->pushControl(); }
279 void trackBounds(const EndCommentGroup&) { this->pushControl(); }
mtklein29dfaa82014-09-04 14:12:44 -0700280 void trackBounds(const DrawData&) { this->pushControl(); }
mtklein828ce1f2014-08-13 12:58:45 -0700281
282 // For all other ops, we can calculate and store the bounds directly now.
283 template <typename T> void trackBounds(const T& op) {
284 fBounds[fCurrentOp] = this->bounds(op);
285 this->updateSaveBounds(fBounds[fCurrentOp]);
mtklein5ad6ee12014-08-11 08:08:43 -0700286 }
287
mtkleina723b572014-08-15 11:49:49 -0700288 void pushSaveBlock(const SkPaint* paint) {
mtklein828ce1f2014-08-13 12:58:45 -0700289 // Starting a new Save block. Push a new entry to represent that.
robertphillips4d52afe2014-11-03 08:19:44 -0800290 SaveBounds sb;
291 sb.controlOps = 0;
292 // If the paint affects transparent black, the bound shouldn't be smaller
293 // than the current clip bounds.
294 sb.bounds =
295 PaintMayAffectTransparentBlack(paint) ? fCurrentClipBounds : Bounds::MakeEmpty();
296 sb.paint = paint;
297
mtklein828ce1f2014-08-13 12:58:45 -0700298 fSaveStack.push(sb);
299 this->pushControl();
300 }
301
mtkleind910f542014-08-22 09:06:34 -0700302 static bool PaintMayAffectTransparentBlack(const SkPaint* paint) {
dneto327f9052014-09-15 10:53:16 -0700303 if (paint) {
304 // FIXME: this is very conservative
305 if (paint->getImageFilter() || paint->getColorFilter()) {
306 return true;
307 }
308
309 // Unusual Xfermodes require us to process a saved layer
310 // even with operations outisde the clip.
311 // For example, DstIn is used by masking layers.
312 // https://code.google.com/p/skia/issues/detail?id=1291
313 // https://crbug.com/401593
314 SkXfermode* xfermode = paint->getXfermode();
315 SkXfermode::Mode mode;
316 // SrcOver is ok, and is also the common case with a NULL xfermode.
317 // So we should make that the fast path and bypass the mode extraction
318 // and test.
319 if (xfermode && xfermode->asMode(&mode)) {
320 switch (mode) {
321 // For each of the following transfer modes, if the source
322 // alpha is zero (our transparent black), the resulting
323 // blended alpha is not necessarily equal to the original
324 // destination alpha.
325 case SkXfermode::kClear_Mode:
326 case SkXfermode::kSrc_Mode:
327 case SkXfermode::kSrcIn_Mode:
328 case SkXfermode::kDstIn_Mode:
329 case SkXfermode::kSrcOut_Mode:
330 case SkXfermode::kDstATop_Mode:
331 case SkXfermode::kModulate_Mode:
332 return true;
333 break;
334 default:
335 break;
336 }
337 }
338 }
339 return false;
mtkleind910f542014-08-22 09:06:34 -0700340 }
341
mtklein533eb782014-08-27 10:39:42 -0700342 Bounds popSaveBlock() {
mtklein828ce1f2014-08-13 12:58:45 -0700343 // We're done the Save block. Apply the block's bounds to all control ops inside it.
344 SaveBounds sb;
345 fSaveStack.pop(&sb);
mtkleind910f542014-08-22 09:06:34 -0700346
mtklein828ce1f2014-08-13 12:58:45 -0700347 while (sb.controlOps --> 0) {
robertphillips4d52afe2014-11-03 08:19:44 -0800348 this->popControl(sb.bounds);
mtklein828ce1f2014-08-13 12:58:45 -0700349 }
350
351 // This whole Save block may be part another Save block.
robertphillips4d52afe2014-11-03 08:19:44 -0800352 this->updateSaveBounds(sb.bounds);
mtklein828ce1f2014-08-13 12:58:45 -0700353
354 // If called from a real Restore (not a phony one for balance), it'll need the bounds.
robertphillips4d52afe2014-11-03 08:19:44 -0800355 return sb.bounds;
mtklein828ce1f2014-08-13 12:58:45 -0700356 }
357
358 void pushControl() {
359 fControlIndices.push(fCurrentOp);
360 if (!fSaveStack.isEmpty()) {
361 fSaveStack.top().controlOps++;
362 }
363 }
364
mtklein533eb782014-08-27 10:39:42 -0700365 void popControl(const Bounds& bounds) {
mtklein828ce1f2014-08-13 12:58:45 -0700366 fBounds[fControlIndices.top()] = bounds;
367 fControlIndices.pop();
368 }
369
mtklein533eb782014-08-27 10:39:42 -0700370 void updateSaveBounds(const Bounds& bounds) {
mtklein828ce1f2014-08-13 12:58:45 -0700371 // If we're in a Save block, expand its bounds to cover these bounds too.
372 if (!fSaveStack.isEmpty()) {
373 fSaveStack.top().bounds.join(bounds);
374 }
375 }
376
mtklein131a22b2014-08-25 14:16:15 -0700377 // FIXME: this method could use better bounds
mtklein533eb782014-08-27 10:39:42 -0700378 Bounds bounds(const DrawText&) const { return fCurrentClipBounds; }
mtklein68199a22014-08-25 13:49:29 -0700379
robertphillips4d52afe2014-11-03 08:19:44 -0800380 Bounds bounds(const Clear&) const { return fCullRect; } // Ignores the clip.
mtklein533eb782014-08-27 10:39:42 -0700381 Bounds bounds(const DrawPaint&) const { return fCurrentClipBounds; }
382 Bounds bounds(const NoOp&) const { return Bounds::MakeEmpty(); } // NoOps don't draw.
mtklein828ce1f2014-08-13 12:58:45 -0700383
mtklein533eb782014-08-27 10:39:42 -0700384 Bounds bounds(const DrawSprite& op) const {
mtklein131a22b2014-08-25 14:16:15 -0700385 const SkBitmap& bm = op.bitmap;
mtklein533eb782014-08-27 10:39:42 -0700386 return Bounds::MakeXYWH(op.left, op.top, bm.width(), bm.height()); // Ignores the matrix.
mtklein131a22b2014-08-25 14:16:15 -0700387 }
388
mtklein533eb782014-08-27 10:39:42 -0700389 Bounds bounds(const DrawRect& op) const { return this->adjustAndMap(op.rect, &op.paint); }
390 Bounds bounds(const DrawOval& op) const { return this->adjustAndMap(op.oval, &op.paint); }
391 Bounds bounds(const DrawRRect& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700392 return this->adjustAndMap(op.rrect.rect(), &op.paint);
393 }
mtklein533eb782014-08-27 10:39:42 -0700394 Bounds bounds(const DrawDRRect& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700395 return this->adjustAndMap(op.outer.rect(), &op.paint);
396 }
piotaixr65151752014-10-16 11:58:39 -0700397 Bounds bounds(const DrawImage& op) const {
398 const SkImage* image = op.image;
399 SkRect rect = SkRect::MakeXYWH(op.left, op.top, image->width(), image->height());
mtklein62b67ae2014-08-18 11:10:37 -0700400
piotaixr65151752014-10-16 11:58:39 -0700401 return this->adjustAndMap(rect, op.paint);
402 }
403 Bounds bounds(const DrawImageRect& op) const {
404 return this->adjustAndMap(op.dst, op.paint);
405 }
mtklein533eb782014-08-27 10:39:42 -0700406 Bounds bounds(const DrawBitmapRectToRect& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700407 return this->adjustAndMap(op.dst, op.paint);
408 }
mtklein533eb782014-08-27 10:39:42 -0700409 Bounds bounds(const DrawBitmapNine& 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 DrawBitmap& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700413 const SkBitmap& bm = op.bitmap;
414 return this->adjustAndMap(SkRect::MakeXYWH(op.left, op.top, bm.width(), bm.height()),
415 op.paint);
416 }
mtklein533eb782014-08-27 10:39:42 -0700417 Bounds bounds(const DrawBitmapMatrix& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700418 const SkBitmap& bm = op.bitmap;
419 SkRect dst = SkRect::MakeWH(bm.width(), bm.height());
420 op.matrix.mapRect(&dst);
421 return this->adjustAndMap(dst, op.paint);
422 }
423
mtklein533eb782014-08-27 10:39:42 -0700424 Bounds bounds(const DrawPath& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700425 return op.path.isInverseFillType() ? fCurrentClipBounds
426 : this->adjustAndMap(op.path.getBounds(), &op.paint);
427 }
mtklein533eb782014-08-27 10:39:42 -0700428 Bounds bounds(const DrawPoints& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700429 SkRect dst;
430 dst.set(op.pts, op.count);
431
432 // Pad the bounding box a little to make sure hairline points' bounds aren't empty.
433 SkScalar stroke = SkMaxScalar(op.paint.getStrokeWidth(), 0.01f);
434 dst.outset(stroke/2, stroke/2);
435
436 return this->adjustAndMap(dst, &op.paint);
437 }
mtklein533eb782014-08-27 10:39:42 -0700438 Bounds bounds(const DrawPatch& op) const {
mtklein131a22b2014-08-25 14:16:15 -0700439 SkRect dst;
440 dst.set(op.cubics, SkPatchUtils::kNumCtrlPts);
441 return this->adjustAndMap(dst, &op.paint);
442 }
mtklein533eb782014-08-27 10:39:42 -0700443 Bounds bounds(const DrawVertices& op) const {
mtklein131a22b2014-08-25 14:16:15 -0700444 SkRect dst;
445 dst.set(op.vertices, op.vertexCount);
446 return this->adjustAndMap(dst, &op.paint);
447 }
448
mtklein533eb782014-08-27 10:39:42 -0700449 Bounds bounds(const DrawPicture& op) const {
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700450 SkRect dst = op.picture->cullRect();
mtklein131a22b2014-08-25 14:16:15 -0700451 if (op.matrix) {
452 op.matrix->mapRect(&dst);
453 }
454 return this->adjustAndMap(dst, op.paint);
455 }
mtklein62b67ae2014-08-18 11:10:37 -0700456
mtklein533eb782014-08-27 10:39:42 -0700457 Bounds bounds(const DrawPosText& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700458 const int N = op.paint.countText(op.text, op.byteLength);
459 if (N == 0) {
mtklein533eb782014-08-27 10:39:42 -0700460 return Bounds::MakeEmpty();
mtklein62b67ae2014-08-18 11:10:37 -0700461 }
462
463 SkRect dst;
mtklein937c9c72014-09-02 15:19:48 -0700464 dst.set(op.pos, N);
mtklein62b67ae2014-08-18 11:10:37 -0700465 AdjustTextForFontMetrics(&dst, op.paint);
466 return this->adjustAndMap(dst, &op.paint);
467 }
mtklein533eb782014-08-27 10:39:42 -0700468 Bounds bounds(const DrawPosTextH& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700469 const int N = op.paint.countText(op.text, op.byteLength);
470 if (N == 0) {
mtklein533eb782014-08-27 10:39:42 -0700471 return Bounds::MakeEmpty();
mtklein62b67ae2014-08-18 11:10:37 -0700472 }
473
474 SkScalar left = op.xpos[0], right = op.xpos[0];
475 for (int i = 1; i < N; i++) {
476 left = SkMinScalar(left, op.xpos[i]);
477 right = SkMaxScalar(right, op.xpos[i]);
478 }
479 SkRect dst = { left, op.y, right, op.y };
480 AdjustTextForFontMetrics(&dst, op.paint);
481 return this->adjustAndMap(dst, &op.paint);
482 }
mtklein533eb782014-08-27 10:39:42 -0700483 Bounds bounds(const DrawTextOnPath& op) const {
mtklein131a22b2014-08-25 14:16:15 -0700484 SkRect dst = op.path.getBounds();
485
mtkleined167ac2014-10-29 16:07:10 -0700486 // Pad all sides by the maximum padding in any direction we'd normally apply.
mtklein131a22b2014-08-25 14:16:15 -0700487 SkRect pad = { 0, 0, 0, 0};
488 AdjustTextForFontMetrics(&pad, op.paint);
mtkleined167ac2014-10-29 16:07:10 -0700489
490 // That maximum padding happens to always be the right pad today.
491 SkASSERT(pad.fLeft == -pad.fRight);
492 SkASSERT(pad.fTop == -pad.fBottom);
493 SkASSERT(pad.fRight > pad.fBottom);
494 dst.outset(pad.fRight, pad.fRight);
mtklein131a22b2014-08-25 14:16:15 -0700495
496 return this->adjustAndMap(dst, &op.paint);
497 }
498
mtklein533eb782014-08-27 10:39:42 -0700499 Bounds bounds(const DrawTextBlob& op) const {
mtklein131a22b2014-08-25 14:16:15 -0700500 SkRect dst = op.blob->bounds();
501 dst.offset(op.x, op.y);
mtklein131a22b2014-08-25 14:16:15 -0700502 return this->adjustAndMap(dst, &op.paint);
503 }
mtklein62b67ae2014-08-18 11:10:37 -0700504
505 static void AdjustTextForFontMetrics(SkRect* rect, const SkPaint& paint) {
mtkleined167ac2014-10-29 16:07:10 -0700506#ifdef SK_DEBUG
507 SkRect correct = *rect;
508#endif
509 // crbug.com/373785 ~~> xPad = 4x yPad
510 // crbug.com/424824 ~~> bump yPad from 2x text size to 2.5x
511 const SkScalar yPad = 2.5f * paint.getTextSize(),
512 xPad = 4.0f * yPad;
513 rect->outset(xPad, yPad);
caryclark9a657fa2014-08-20 05:24:29 -0700514#ifdef SK_DEBUG
mtklein62b67ae2014-08-18 11:10:37 -0700515 SkPaint::FontMetrics metrics;
516 paint.getFontMetrics(&metrics);
mtkleined167ac2014-10-29 16:07:10 -0700517 correct.fLeft += metrics.fXMin;
518 correct.fTop += metrics.fTop;
519 correct.fRight += metrics.fXMax;
520 correct.fBottom += metrics.fBottom;
mtkleind13291a2014-08-21 14:46:49 -0700521 // See skia:2862 for why we ignore small text sizes.
mtkleined167ac2014-10-29 16:07:10 -0700522 SkASSERTF(paint.getTextSize() < 0.001f || rect->contains(correct),
523 "%f %f %f %f vs. %f %f %f %f\n",
524 -xPad, -yPad, +xPad, +yPad,
525 metrics.fXMin, metrics.fTop, metrics.fXMax, metrics.fBottom);
mtkleina19afb42014-08-19 17:47:14 -0700526#endif
mtklein62b67ae2014-08-18 11:10:37 -0700527 }
528
mtklein479601b2014-08-18 08:45:33 -0700529 // Returns true if rect was meaningfully adjusted for the effects of paint,
530 // false if the paint could affect the rect in unknown ways.
531 static bool AdjustForPaint(const SkPaint* paint, SkRect* rect) {
mtkleina723b572014-08-15 11:49:49 -0700532 if (paint) {
533 if (paint->canComputeFastBounds()) {
mtklein479601b2014-08-18 08:45:33 -0700534 *rect = paint->computeFastBounds(*rect, rect);
535 return true;
mtkleina723b572014-08-15 11:49:49 -0700536 }
mtklein479601b2014-08-18 08:45:33 -0700537 return false;
538 }
539 return true;
540 }
541
mtklein8e393bf2014-10-01 12:48:58 -0700542 bool adjustForSaveLayerPaints(SkRect* rect, int savesToIgnore = 0) const {
543 for (int i = fSaveStack.count() - 1 - savesToIgnore; i >= 0; i--) {
Mike Klein271a0302014-09-23 15:28:38 -0400544 if (!AdjustForPaint(fSaveStack[i].paint, rect)) {
545 return false;
546 }
547 }
548 return true;
549 }
550
robertphillips4e8e3422014-11-12 06:46:08 -0800551 const unsigned fNumRecords;
mtkleina723b572014-08-15 11:49:49 -0700552
robertphillips4d52afe2014-11-03 08:19:44 -0800553 // We do not guarantee anything for operations outside of the cull rect
554 const SkRect fCullRect;
555
mtklein533eb782014-08-27 10:39:42 -0700556 // Conservative identity-space bounds for each op in the SkRecord.
557 SkAutoTMalloc<Bounds> fBounds;
mtkleina723b572014-08-15 11:49:49 -0700558
559 // We walk fCurrentOp through the SkRecord, as we go using updateCTM()
560 // and updateClipBounds() to maintain the exact CTM (fCTM) and conservative
mtklein533eb782014-08-27 10:39:42 -0700561 // identity-space bounds of the current clip (fCurrentClipBounds).
mtklein828ce1f2014-08-13 12:58:45 -0700562 unsigned fCurrentOp;
mtklein6332f1d2014-08-19 07:09:40 -0700563 const SkMatrix* fCTM;
mtklein533eb782014-08-27 10:39:42 -0700564 Bounds fCurrentClipBounds;
mtkleina723b572014-08-15 11:49:49 -0700565
566 // Used to track the bounds of Save/Restore blocks and the control ops inside them.
mtklein828ce1f2014-08-13 12:58:45 -0700567 SkTDArray<SaveBounds> fSaveStack;
568 SkTDArray<unsigned> fControlIndices;
mtklein5ad6ee12014-08-11 08:08:43 -0700569};
570
robertphillips4e8e3422014-11-12 06:46:08 -0800571// SkRecord visitor to gather saveLayer/restore information.
572class CollectLayers : SkNoncopyable {
573public:
robertphillips82365912014-11-12 09:32:34 -0800574 CollectLayers(const SkRect& cullRect, const SkRecord& record, SkLayerInfo* accelData)
robertphillips4e8e3422014-11-12 06:46:08 -0800575 : fSaveLayersInStack(0)
576 , fAccelData(accelData)
577 , fFillBounds(cullRect, record) {
578 }
579
580 void setCurrentOp(unsigned currentOp) { fFillBounds.setCurrentOp(currentOp); }
581
582 void cleanUp(SkBBoxHierarchy* bbh) {
583 // fFillBounds must perform its cleanUp first so that all the bounding
584 // boxes associated with unbalanced restores are updated (prior to
585 // fetching their bound in popSaveLayerInfo).
586 fFillBounds.cleanUp(bbh);
587
588 while (!fSaveLayerStack.isEmpty()) {
589 this->popSaveLayerInfo();
590 }
591 }
592
593 template <typename T> void operator()(const T& op) {
594 fFillBounds(op);
595 this->trackSaveLayers(op);
596 }
597
598private:
599 struct SaveLayerInfo {
600 SaveLayerInfo() { }
robertphillips74576eb2014-11-12 07:25:02 -0800601 SaveLayerInfo(int opIndex, bool isSaveLayer, const SkPaint* paint)
robertphillips4e8e3422014-11-12 06:46:08 -0800602 : fStartIndex(opIndex)
603 , fIsSaveLayer(isSaveLayer)
604 , fHasNestedSaveLayer(false)
robertphillips74576eb2014-11-12 07:25:02 -0800605 , fPaint(paint) {
robertphillips4e8e3422014-11-12 06:46:08 -0800606 }
607
608 int fStartIndex;
609 bool fIsSaveLayer;
610 bool fHasNestedSaveLayer;
611 const SkPaint* fPaint;
robertphillips4e8e3422014-11-12 06:46:08 -0800612 };
613
614 template <typename T> void trackSaveLayers(const T& op) {
615 /* most ops aren't involved in saveLayers */
616 }
617 void trackSaveLayers(const Save& s) { this->pushSaveLayerInfo(false, NULL); }
618 void trackSaveLayers(const SaveLayer& sl) { this->pushSaveLayerInfo(true, sl.paint); }
619 void trackSaveLayers(const Restore& r) { this->popSaveLayerInfo(); }
620
621 void trackSaveLayers(const DrawPicture& dp) {
622 // For sub-pictures, we wrap their layer information within the parent
623 // picture's rendering hierarchy
robertphillips82365912014-11-12 09:32:34 -0800624 SkPicture::AccelData::Key key = SkLayerInfo::ComputeKey();
robertphillips4e8e3422014-11-12 06:46:08 -0800625
robertphillips82365912014-11-12 09:32:34 -0800626 const SkLayerInfo* childData =
627 static_cast<const SkLayerInfo*>(dp.picture->EXPERIMENTAL_getAccelData(key));
robertphillips4e8e3422014-11-12 06:46:08 -0800628 if (!childData) {
629 // If the child layer hasn't been generated with saveLayer data we
630 // assume the worst (i.e., that it does contain layers which nest
631 // inside existing layers). Layers within sub-pictures that don't
632 // have saveLayer data cannot be hoisted.
633 // TODO: could the analysis data be use to fine tune this?
634 this->updateStackForSaveLayer();
635 return;
636 }
637
robertphillips82365912014-11-12 09:32:34 -0800638 for (int i = 0; i < childData->numBlocks(); ++i) {
639 const SkLayerInfo::BlockInfo& src = childData->block(i);
robertphillips4e8e3422014-11-12 06:46:08 -0800640
robertphillips74576eb2014-11-12 07:25:02 -0800641 FillBounds::Bounds newBound = fFillBounds.adjustAndMap(src.fBounds, dp.paint);
642 if (newBound.isEmpty()) {
robertphillips4e8e3422014-11-12 06:46:08 -0800643 continue;
644 }
645
646 this->updateStackForSaveLayer();
647
robertphillips82365912014-11-12 09:32:34 -0800648 SkLayerInfo::BlockInfo& dst = fAccelData->addBlock();
robertphillips4e8e3422014-11-12 06:46:08 -0800649
650 // If src.fPicture is NULL the layer is in dp.picture; otherwise
651 // it belongs to a sub-picture.
652 dst.fPicture = src.fPicture ? src.fPicture : static_cast<const SkPicture*>(dp.picture);
653 dst.fPicture->ref();
robertphillips74576eb2014-11-12 07:25:02 -0800654 dst.fBounds = newBound;
robertphillips4e8e3422014-11-12 06:46:08 -0800655 dst.fLocalMat = src.fLocalMat;
656 dst.fPreMat = src.fPreMat;
657 dst.fPreMat.postConcat(fFillBounds.ctm());
658 if (src.fPaint) {
659 dst.fPaint = SkNEW_ARGS(SkPaint, (*src.fPaint));
660 }
661 dst.fSaveLayerOpID = src.fSaveLayerOpID;
662 dst.fRestoreOpID = src.fRestoreOpID;
663 dst.fHasNestedLayers = src.fHasNestedLayers;
664 dst.fIsNested = fSaveLayersInStack > 0 || src.fIsNested;
665 }
666 }
667
668 // Inform all the saveLayers already on the stack that they now have a
669 // nested saveLayer inside them
670 void updateStackForSaveLayer() {
671 for (int index = fSaveLayerStack.count() - 1; index >= 0; --index) {
672 if (fSaveLayerStack[index].fHasNestedSaveLayer) {
673 break;
674 }
675 fSaveLayerStack[index].fHasNestedSaveLayer = true;
676 if (fSaveLayerStack[index].fIsSaveLayer) {
677 break;
678 }
679 }
680 }
681
682 void pushSaveLayerInfo(bool isSaveLayer, const SkPaint* paint) {
683 if (isSaveLayer) {
684 this->updateStackForSaveLayer();
685 ++fSaveLayersInStack;
686 }
687
robertphillips74576eb2014-11-12 07:25:02 -0800688 fSaveLayerStack.push(SaveLayerInfo(fFillBounds.currentOp(), isSaveLayer, paint));
robertphillips4e8e3422014-11-12 06:46:08 -0800689 }
690
691 void popSaveLayerInfo() {
692 if (fSaveLayerStack.count() <= 0) {
693 SkASSERT(false);
694 return;
695 }
696
697 SaveLayerInfo sli;
698 fSaveLayerStack.pop(&sli);
699
700 if (!sli.fIsSaveLayer) {
701 return;
702 }
703
704 --fSaveLayersInStack;
705
robertphillips82365912014-11-12 09:32:34 -0800706 SkLayerInfo::BlockInfo& block = fAccelData->addBlock();
robertphillips4e8e3422014-11-12 06:46:08 -0800707
robertphillips82365912014-11-12 09:32:34 -0800708 SkASSERT(NULL == block.fPicture); // This layer is in the top-most picture
robertphillips4e8e3422014-11-12 06:46:08 -0800709
robertphillips82365912014-11-12 09:32:34 -0800710 block.fBounds = fFillBounds.getBounds(sli.fStartIndex);
711 block.fLocalMat = fFillBounds.ctm();
712 block.fPreMat = SkMatrix::I();
robertphillips4e8e3422014-11-12 06:46:08 -0800713 if (sli.fPaint) {
robertphillips82365912014-11-12 09:32:34 -0800714 block.fPaint = SkNEW_ARGS(SkPaint, (*sli.fPaint));
robertphillips4e8e3422014-11-12 06:46:08 -0800715 }
robertphillips82365912014-11-12 09:32:34 -0800716 block.fSaveLayerOpID = sli.fStartIndex;
717 block.fRestoreOpID = fFillBounds.currentOp();
718 block.fHasNestedLayers = sli.fHasNestedSaveLayer;
719 block.fIsNested = fSaveLayersInStack > 0;
robertphillips4e8e3422014-11-12 06:46:08 -0800720 }
721
722 // Used to collect saveLayer information for layer hoisting
723 int fSaveLayersInStack;
724 SkTDArray<SaveLayerInfo> fSaveLayerStack;
robertphillips82365912014-11-12 09:32:34 -0800725 SkLayerInfo* fAccelData;
robertphillips4e8e3422014-11-12 06:46:08 -0800726
727 SkRecords::FillBounds fFillBounds;
728};
robertphillips4e8e3422014-11-12 06:46:08 -0800729
commit-bot@chromium.org27f6b0d2014-05-09 14:59:29 +0000730} // namespace SkRecords
mtklein5ad6ee12014-08-11 08:08:43 -0700731
robertphillips4d52afe2014-11-03 08:19:44 -0800732void SkRecordFillBounds(const SkRect& cullRect, const SkRecord& record, SkBBoxHierarchy* bbh) {
robertphillips4e8e3422014-11-12 06:46:08 -0800733 SkRecords::FillBounds visitor(cullRect, record);
734
735 for (unsigned curOp = 0; curOp < record.count(); curOp++) {
736 visitor.setCurrentOp(curOp);
737 record.visit<void>(curOp, visitor);
738 }
739
740 visitor.cleanUp(bbh);
mtklein5ad6ee12014-08-11 08:08:43 -0700741}
robertphillips4e8e3422014-11-12 06:46:08 -0800742
robertphillips4e8e3422014-11-12 06:46:08 -0800743void SkRecordComputeLayers(const SkRect& cullRect, const SkRecord& record,
robertphillips82365912014-11-12 09:32:34 -0800744 SkBBoxHierarchy* bbh, SkLayerInfo* data) {
robertphillips4e8e3422014-11-12 06:46:08 -0800745 SkRecords::CollectLayers visitor(cullRect, record, data);
746
747 for (unsigned curOp = 0; curOp < record.count(); curOp++) {
748 visitor.setCurrentOp(curOp);
749 record.visit<void>(curOp, visitor);
750 }
751
752 visitor.cleanUp(bbh);
753}
robertphillips4e8e3422014-11-12 06:46:08 -0800754