blob: e35e5a0a9d37d37e5c232067108a20680d03c511 [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.
bsalomon49f085d2014-09-05 13:34:00 -0700171 SkASSERT(bbh);
robertphillips4e8e3422014-11-12 06:46:08 -0800172 bbh->insert(&fBounds, fNumRecords);
mtklein828ce1f2014-08-13 12:58:45 -0700173 }
mtklein5ad6ee12014-08-11 08:08:43 -0700174
mtkleina723b572014-08-15 11:49:49 -0700175 template <typename T> void operator()(const T& op) {
176 this->updateCTM(op);
177 this->updateClipBounds(op);
178 this->trackBounds(op);
mtklein5ad6ee12014-08-11 08:08:43 -0700179 }
180
mtklein533eb782014-08-27 10:39:42 -0700181 // In this file, SkRect are in local coordinates, Bounds are translated back to identity space.
182 typedef SkRect Bounds;
183
robertphillips4e8e3422014-11-12 06:46:08 -0800184 unsigned currentOp() const { return fCurrentOp; }
185 const SkMatrix& ctm() const { return *fCTM; }
robertphillips4e8e3422014-11-12 06:46:08 -0800186 const Bounds& getBounds(unsigned index) const { return fBounds[index]; }
187
188 // Adjust rect for all paints that may affect its geometry, then map it to identity space.
189 Bounds adjustAndMap(SkRect rect, const SkPaint* paint) const {
190 // Inverted rectangles really confuse our BBHs.
191 rect.sort();
192
193 // Adjust the rect for its own paint.
194 if (!AdjustForPaint(paint, &rect)) {
195 // The paint could do anything to our bounds. The only safe answer is the current clip.
196 return fCurrentClipBounds;
197 }
198
199 // Adjust rect for all the paints from the SaveLayers we're inside.
200 if (!this->adjustForSaveLayerPaints(&rect)) {
201 // Same deal as above.
202 return fCurrentClipBounds;
203 }
204
205 // Map the rect back to identity space.
206 fCTM->mapRect(&rect);
207
208 // Nothing can draw outside the current clip.
209 // (Only bounded ops call into this method, so oddballs like Clear don't matter here.)
210 rect.intersect(fCurrentClipBounds);
211 return rect;
212 }
213
214private:
mtklein828ce1f2014-08-13 12:58:45 -0700215 struct SaveBounds {
mtkleina723b572014-08-15 11:49:49 -0700216 int controlOps; // Number of control ops in this Save block, including the Save.
mtklein533eb782014-08-27 10:39:42 -0700217 Bounds bounds; // Bounds of everything in the block.
mtkleina723b572014-08-15 11:49:49 -0700218 const SkPaint* paint; // Unowned. If set, adjusts the bounds of all ops in this block.
mtklein828ce1f2014-08-13 12:58:45 -0700219 };
220
mtklein8e393bf2014-10-01 12:48:58 -0700221 // Only Restore and SetMatrix change the CTM.
222 template <typename T> void updateCTM(const T&) {}
mtklein6332f1d2014-08-19 07:09:40 -0700223 void updateCTM(const Restore& op) { fCTM = &op.matrix; }
224 void updateCTM(const SetMatrix& op) { fCTM = &op.matrix; }
mtkleina723b572014-08-15 11:49:49 -0700225
mtklein8e393bf2014-10-01 12:48:58 -0700226 // Most ops don't change the clip.
227 template <typename T> void updateClipBounds(const T&) {}
Mike Klein271a0302014-09-23 15:28:38 -0400228
mtklein8e393bf2014-10-01 12:48:58 -0700229 // Clip{Path,RRect,Rect,Region} obviously change the clip. They all know their bounds already.
230 void updateClipBounds(const ClipPath& op) { this->updateClipBoundsForClipOp(op.devBounds); }
231 void updateClipBounds(const ClipRRect& op) { this->updateClipBoundsForClipOp(op.devBounds); }
232 void updateClipBounds(const ClipRect& op) { this->updateClipBoundsForClipOp(op.devBounds); }
233 void updateClipBounds(const ClipRegion& op) { this->updateClipBoundsForClipOp(op.devBounds); }
Mike Klein271a0302014-09-23 15:28:38 -0400234
mtklein8e393bf2014-10-01 12:48:58 -0700235 // The bounds of clip ops need to be adjusted for the paints of saveLayers they're inside.
236 void updateClipBoundsForClipOp(const SkIRect& devBounds) {
237 Bounds clip = SkRect::Make(devBounds);
Mike Klein271a0302014-09-23 15:28:38 -0400238 // We don't call adjustAndMap() because as its last step it would intersect the adjusted
239 // clip bounds with the previous clip, exactly what we can't do when the clip grows.
robertphillips4d52afe2014-11-03 08:19:44 -0800240 fCurrentClipBounds = this->adjustForSaveLayerPaints(&clip) ? clip : fCullRect;
Mike Klein271a0302014-09-23 15:28:38 -0400241 }
242
mtklein8e393bf2014-10-01 12:48:58 -0700243 // Restore holds the devBounds for the clip after the {save,saveLayer}/restore block completes.
244 void updateClipBounds(const Restore& op) {
245 // This is just like the clip ops above, but we need to skip the effects (if any) of our
246 // paired saveLayer (if it is one); it has not yet been popped off the save stack. Our
247 // devBounds reflect the state of the world after the saveLayer/restore block is done,
248 // so they are not affected by the saveLayer's paint.
249 const int kSavesToIgnore = 1;
250 Bounds clip = SkRect::Make(op.devBounds);
251 fCurrentClipBounds =
robertphillips4d52afe2014-11-03 08:19:44 -0800252 this->adjustForSaveLayerPaints(&clip, kSavesToIgnore) ? clip : fCullRect;
mtklein8e393bf2014-10-01 12:48:58 -0700253 }
254
Mike Klein271a0302014-09-23 15:28:38 -0400255 // We also take advantage of SaveLayer bounds when present to further cut the clip down.
mtkleina723b572014-08-15 11:49:49 -0700256 void updateClipBounds(const SaveLayer& op) {
257 if (op.bounds) {
Mike Klein271a0302014-09-23 15:28:38 -0400258 // adjustAndMap() intersects these layer bounds with the previous clip for us.
259 fCurrentClipBounds = this->adjustAndMap(*op.bounds, op.paint);
mtkleina723b572014-08-15 11:49:49 -0700260 }
261 }
mtklein6cfa73a2014-08-13 13:33:49 -0700262
mtklein828ce1f2014-08-13 12:58:45 -0700263 // The bounds of these ops must be calculated when we hit the Restore
264 // from the bounds of the ops in the same Save block.
mtkleina723b572014-08-15 11:49:49 -0700265 void trackBounds(const Save&) { this->pushSaveBlock(NULL); }
mtkleina723b572014-08-15 11:49:49 -0700266 void trackBounds(const SaveLayer& op) { this->pushSaveBlock(op.paint); }
267 void trackBounds(const Restore&) { fBounds[fCurrentOp] = this->popSaveBlock(); }
mtklein828ce1f2014-08-13 12:58:45 -0700268
mtklein68199a22014-08-25 13:49:29 -0700269 void trackBounds(const SetMatrix&) { this->pushControl(); }
270 void trackBounds(const ClipRect&) { this->pushControl(); }
271 void trackBounds(const ClipRRect&) { this->pushControl(); }
272 void trackBounds(const ClipPath&) { this->pushControl(); }
273 void trackBounds(const ClipRegion&) { this->pushControl(); }
274 void trackBounds(const PushCull&) { this->pushControl(); }
275 void trackBounds(const PopCull&) { this->pushControl(); }
276 void trackBounds(const BeginCommentGroup&) { this->pushControl(); }
277 void trackBounds(const AddComment&) { this->pushControl(); }
278 void trackBounds(const EndCommentGroup&) { this->pushControl(); }
mtklein29dfaa82014-09-04 14:12:44 -0700279 void trackBounds(const DrawData&) { this->pushControl(); }
mtklein828ce1f2014-08-13 12:58:45 -0700280
281 // For all other ops, we can calculate and store the bounds directly now.
282 template <typename T> void trackBounds(const T& op) {
283 fBounds[fCurrentOp] = this->bounds(op);
284 this->updateSaveBounds(fBounds[fCurrentOp]);
mtklein5ad6ee12014-08-11 08:08:43 -0700285 }
286
mtkleina723b572014-08-15 11:49:49 -0700287 void pushSaveBlock(const SkPaint* paint) {
mtklein828ce1f2014-08-13 12:58:45 -0700288 // Starting a new Save block. Push a new entry to represent that.
robertphillips4d52afe2014-11-03 08:19:44 -0800289 SaveBounds sb;
290 sb.controlOps = 0;
291 // If the paint affects transparent black, the bound shouldn't be smaller
292 // than the current clip bounds.
293 sb.bounds =
294 PaintMayAffectTransparentBlack(paint) ? fCurrentClipBounds : Bounds::MakeEmpty();
295 sb.paint = paint;
296
mtklein828ce1f2014-08-13 12:58:45 -0700297 fSaveStack.push(sb);
298 this->pushControl();
299 }
300
mtkleind910f542014-08-22 09:06:34 -0700301 static bool PaintMayAffectTransparentBlack(const SkPaint* paint) {
dneto327f9052014-09-15 10:53:16 -0700302 if (paint) {
303 // FIXME: this is very conservative
304 if (paint->getImageFilter() || paint->getColorFilter()) {
305 return true;
306 }
307
308 // Unusual Xfermodes require us to process a saved layer
309 // even with operations outisde the clip.
310 // For example, DstIn is used by masking layers.
311 // https://code.google.com/p/skia/issues/detail?id=1291
312 // https://crbug.com/401593
313 SkXfermode* xfermode = paint->getXfermode();
314 SkXfermode::Mode mode;
315 // SrcOver is ok, and is also the common case with a NULL xfermode.
316 // So we should make that the fast path and bypass the mode extraction
317 // and test.
318 if (xfermode && xfermode->asMode(&mode)) {
319 switch (mode) {
320 // For each of the following transfer modes, if the source
321 // alpha is zero (our transparent black), the resulting
322 // blended alpha is not necessarily equal to the original
323 // destination alpha.
324 case SkXfermode::kClear_Mode:
325 case SkXfermode::kSrc_Mode:
326 case SkXfermode::kSrcIn_Mode:
327 case SkXfermode::kDstIn_Mode:
328 case SkXfermode::kSrcOut_Mode:
329 case SkXfermode::kDstATop_Mode:
330 case SkXfermode::kModulate_Mode:
331 return true;
332 break;
333 default:
334 break;
335 }
336 }
337 }
338 return false;
mtkleind910f542014-08-22 09:06:34 -0700339 }
340
mtklein533eb782014-08-27 10:39:42 -0700341 Bounds popSaveBlock() {
mtklein828ce1f2014-08-13 12:58:45 -0700342 // We're done the Save block. Apply the block's bounds to all control ops inside it.
343 SaveBounds sb;
344 fSaveStack.pop(&sb);
mtkleind910f542014-08-22 09:06:34 -0700345
mtklein828ce1f2014-08-13 12:58:45 -0700346 while (sb.controlOps --> 0) {
robertphillips4d52afe2014-11-03 08:19:44 -0800347 this->popControl(sb.bounds);
mtklein828ce1f2014-08-13 12:58:45 -0700348 }
349
350 // This whole Save block may be part another Save block.
robertphillips4d52afe2014-11-03 08:19:44 -0800351 this->updateSaveBounds(sb.bounds);
mtklein828ce1f2014-08-13 12:58:45 -0700352
353 // If called from a real Restore (not a phony one for balance), it'll need the bounds.
robertphillips4d52afe2014-11-03 08:19:44 -0800354 return sb.bounds;
mtklein828ce1f2014-08-13 12:58:45 -0700355 }
356
357 void pushControl() {
358 fControlIndices.push(fCurrentOp);
359 if (!fSaveStack.isEmpty()) {
360 fSaveStack.top().controlOps++;
361 }
362 }
363
mtklein533eb782014-08-27 10:39:42 -0700364 void popControl(const Bounds& bounds) {
mtklein828ce1f2014-08-13 12:58:45 -0700365 fBounds[fControlIndices.top()] = bounds;
366 fControlIndices.pop();
367 }
368
mtklein533eb782014-08-27 10:39:42 -0700369 void updateSaveBounds(const Bounds& bounds) {
mtklein828ce1f2014-08-13 12:58:45 -0700370 // If we're in a Save block, expand its bounds to cover these bounds too.
371 if (!fSaveStack.isEmpty()) {
372 fSaveStack.top().bounds.join(bounds);
373 }
374 }
375
mtklein131a22b2014-08-25 14:16:15 -0700376 // FIXME: this method could use better bounds
mtklein533eb782014-08-27 10:39:42 -0700377 Bounds bounds(const DrawText&) const { return fCurrentClipBounds; }
mtklein68199a22014-08-25 13:49:29 -0700378
robertphillips4d52afe2014-11-03 08:19:44 -0800379 Bounds bounds(const Clear&) const { return fCullRect; } // Ignores the clip.
mtklein533eb782014-08-27 10:39:42 -0700380 Bounds bounds(const DrawPaint&) const { return fCurrentClipBounds; }
381 Bounds bounds(const NoOp&) const { return Bounds::MakeEmpty(); } // NoOps don't draw.
mtklein828ce1f2014-08-13 12:58:45 -0700382
mtklein533eb782014-08-27 10:39:42 -0700383 Bounds bounds(const DrawSprite& op) const {
mtklein131a22b2014-08-25 14:16:15 -0700384 const SkBitmap& bm = op.bitmap;
mtklein533eb782014-08-27 10:39:42 -0700385 return Bounds::MakeXYWH(op.left, op.top, bm.width(), bm.height()); // Ignores the matrix.
mtklein131a22b2014-08-25 14:16:15 -0700386 }
387
mtklein533eb782014-08-27 10:39:42 -0700388 Bounds bounds(const DrawRect& op) const { return this->adjustAndMap(op.rect, &op.paint); }
389 Bounds bounds(const DrawOval& op) const { return this->adjustAndMap(op.oval, &op.paint); }
390 Bounds bounds(const DrawRRect& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700391 return this->adjustAndMap(op.rrect.rect(), &op.paint);
392 }
mtklein533eb782014-08-27 10:39:42 -0700393 Bounds bounds(const DrawDRRect& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700394 return this->adjustAndMap(op.outer.rect(), &op.paint);
395 }
piotaixr65151752014-10-16 11:58:39 -0700396 Bounds bounds(const DrawImage& op) const {
397 const SkImage* image = op.image;
398 SkRect rect = SkRect::MakeXYWH(op.left, op.top, image->width(), image->height());
mtklein62b67ae2014-08-18 11:10:37 -0700399
piotaixr65151752014-10-16 11:58:39 -0700400 return this->adjustAndMap(rect, op.paint);
401 }
402 Bounds bounds(const DrawImageRect& op) const {
403 return this->adjustAndMap(op.dst, op.paint);
404 }
mtklein533eb782014-08-27 10:39:42 -0700405 Bounds bounds(const DrawBitmapRectToRect& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700406 return this->adjustAndMap(op.dst, op.paint);
407 }
mtklein533eb782014-08-27 10:39:42 -0700408 Bounds bounds(const DrawBitmapNine& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700409 return this->adjustAndMap(op.dst, op.paint);
410 }
mtklein533eb782014-08-27 10:39:42 -0700411 Bounds bounds(const DrawBitmap& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700412 const SkBitmap& bm = op.bitmap;
413 return this->adjustAndMap(SkRect::MakeXYWH(op.left, op.top, bm.width(), bm.height()),
414 op.paint);
415 }
mtklein533eb782014-08-27 10:39:42 -0700416 Bounds bounds(const DrawBitmapMatrix& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700417 const SkBitmap& bm = op.bitmap;
418 SkRect dst = SkRect::MakeWH(bm.width(), bm.height());
419 op.matrix.mapRect(&dst);
420 return this->adjustAndMap(dst, op.paint);
421 }
422
mtklein533eb782014-08-27 10:39:42 -0700423 Bounds bounds(const DrawPath& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700424 return op.path.isInverseFillType() ? fCurrentClipBounds
425 : this->adjustAndMap(op.path.getBounds(), &op.paint);
426 }
mtklein533eb782014-08-27 10:39:42 -0700427 Bounds bounds(const DrawPoints& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700428 SkRect dst;
429 dst.set(op.pts, op.count);
430
431 // Pad the bounding box a little to make sure hairline points' bounds aren't empty.
432 SkScalar stroke = SkMaxScalar(op.paint.getStrokeWidth(), 0.01f);
433 dst.outset(stroke/2, stroke/2);
434
435 return this->adjustAndMap(dst, &op.paint);
436 }
mtklein533eb782014-08-27 10:39:42 -0700437 Bounds bounds(const DrawPatch& op) const {
mtklein131a22b2014-08-25 14:16:15 -0700438 SkRect dst;
439 dst.set(op.cubics, SkPatchUtils::kNumCtrlPts);
440 return this->adjustAndMap(dst, &op.paint);
441 }
mtklein533eb782014-08-27 10:39:42 -0700442 Bounds bounds(const DrawVertices& op) const {
mtklein131a22b2014-08-25 14:16:15 -0700443 SkRect dst;
444 dst.set(op.vertices, op.vertexCount);
445 return this->adjustAndMap(dst, &op.paint);
446 }
447
mtklein533eb782014-08-27 10:39:42 -0700448 Bounds bounds(const DrawPicture& op) const {
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700449 SkRect dst = op.picture->cullRect();
mtklein131a22b2014-08-25 14:16:15 -0700450 if (op.matrix) {
451 op.matrix->mapRect(&dst);
452 }
453 return this->adjustAndMap(dst, op.paint);
454 }
mtklein62b67ae2014-08-18 11:10:37 -0700455
mtklein533eb782014-08-27 10:39:42 -0700456 Bounds bounds(const DrawPosText& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700457 const int N = op.paint.countText(op.text, op.byteLength);
458 if (N == 0) {
mtklein533eb782014-08-27 10:39:42 -0700459 return Bounds::MakeEmpty();
mtklein62b67ae2014-08-18 11:10:37 -0700460 }
461
462 SkRect dst;
mtklein937c9c72014-09-02 15:19:48 -0700463 dst.set(op.pos, N);
mtklein62b67ae2014-08-18 11:10:37 -0700464 AdjustTextForFontMetrics(&dst, op.paint);
465 return this->adjustAndMap(dst, &op.paint);
466 }
mtklein533eb782014-08-27 10:39:42 -0700467 Bounds bounds(const DrawPosTextH& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700468 const int N = op.paint.countText(op.text, op.byteLength);
469 if (N == 0) {
mtklein533eb782014-08-27 10:39:42 -0700470 return Bounds::MakeEmpty();
mtklein62b67ae2014-08-18 11:10:37 -0700471 }
472
473 SkScalar left = op.xpos[0], right = op.xpos[0];
474 for (int i = 1; i < N; i++) {
475 left = SkMinScalar(left, op.xpos[i]);
476 right = SkMaxScalar(right, op.xpos[i]);
477 }
478 SkRect dst = { left, op.y, right, op.y };
479 AdjustTextForFontMetrics(&dst, op.paint);
480 return this->adjustAndMap(dst, &op.paint);
481 }
mtklein533eb782014-08-27 10:39:42 -0700482 Bounds bounds(const DrawTextOnPath& op) const {
mtklein131a22b2014-08-25 14:16:15 -0700483 SkRect dst = op.path.getBounds();
484
mtkleined167ac2014-10-29 16:07:10 -0700485 // Pad all sides by the maximum padding in any direction we'd normally apply.
mtklein131a22b2014-08-25 14:16:15 -0700486 SkRect pad = { 0, 0, 0, 0};
487 AdjustTextForFontMetrics(&pad, op.paint);
mtkleined167ac2014-10-29 16:07:10 -0700488
489 // That maximum padding happens to always be the right pad today.
490 SkASSERT(pad.fLeft == -pad.fRight);
491 SkASSERT(pad.fTop == -pad.fBottom);
492 SkASSERT(pad.fRight > pad.fBottom);
493 dst.outset(pad.fRight, pad.fRight);
mtklein131a22b2014-08-25 14:16:15 -0700494
495 return this->adjustAndMap(dst, &op.paint);
496 }
497
mtklein533eb782014-08-27 10:39:42 -0700498 Bounds bounds(const DrawTextBlob& op) const {
mtklein131a22b2014-08-25 14:16:15 -0700499 SkRect dst = op.blob->bounds();
500 dst.offset(op.x, op.y);
mtklein131a22b2014-08-25 14:16:15 -0700501 return this->adjustAndMap(dst, &op.paint);
502 }
mtklein62b67ae2014-08-18 11:10:37 -0700503
504 static void AdjustTextForFontMetrics(SkRect* rect, const SkPaint& paint) {
mtkleined167ac2014-10-29 16:07:10 -0700505#ifdef SK_DEBUG
506 SkRect correct = *rect;
507#endif
508 // crbug.com/373785 ~~> xPad = 4x yPad
509 // crbug.com/424824 ~~> bump yPad from 2x text size to 2.5x
510 const SkScalar yPad = 2.5f * paint.getTextSize(),
511 xPad = 4.0f * yPad;
512 rect->outset(xPad, yPad);
caryclark9a657fa2014-08-20 05:24:29 -0700513#ifdef SK_DEBUG
mtklein62b67ae2014-08-18 11:10:37 -0700514 SkPaint::FontMetrics metrics;
515 paint.getFontMetrics(&metrics);
mtkleined167ac2014-10-29 16:07:10 -0700516 correct.fLeft += metrics.fXMin;
517 correct.fTop += metrics.fTop;
518 correct.fRight += metrics.fXMax;
519 correct.fBottom += metrics.fBottom;
mtkleind13291a2014-08-21 14:46:49 -0700520 // See skia:2862 for why we ignore small text sizes.
mtkleined167ac2014-10-29 16:07:10 -0700521 SkASSERTF(paint.getTextSize() < 0.001f || rect->contains(correct),
522 "%f %f %f %f vs. %f %f %f %f\n",
523 -xPad, -yPad, +xPad, +yPad,
524 metrics.fXMin, metrics.fTop, metrics.fXMax, metrics.fBottom);
mtkleina19afb42014-08-19 17:47:14 -0700525#endif
mtklein62b67ae2014-08-18 11:10:37 -0700526 }
527
mtklein479601b2014-08-18 08:45:33 -0700528 // Returns true if rect was meaningfully adjusted for the effects of paint,
529 // false if the paint could affect the rect in unknown ways.
530 static bool AdjustForPaint(const SkPaint* paint, SkRect* rect) {
mtkleina723b572014-08-15 11:49:49 -0700531 if (paint) {
532 if (paint->canComputeFastBounds()) {
mtklein479601b2014-08-18 08:45:33 -0700533 *rect = paint->computeFastBounds(*rect, rect);
534 return true;
mtkleina723b572014-08-15 11:49:49 -0700535 }
mtklein479601b2014-08-18 08:45:33 -0700536 return false;
537 }
538 return true;
539 }
540
mtklein8e393bf2014-10-01 12:48:58 -0700541 bool adjustForSaveLayerPaints(SkRect* rect, int savesToIgnore = 0) const {
542 for (int i = fSaveStack.count() - 1 - savesToIgnore; i >= 0; i--) {
Mike Klein271a0302014-09-23 15:28:38 -0400543 if (!AdjustForPaint(fSaveStack[i].paint, rect)) {
544 return false;
545 }
546 }
547 return true;
548 }
549
robertphillips4e8e3422014-11-12 06:46:08 -0800550 const unsigned fNumRecords;
mtkleina723b572014-08-15 11:49:49 -0700551
robertphillips4d52afe2014-11-03 08:19:44 -0800552 // We do not guarantee anything for operations outside of the cull rect
553 const SkRect fCullRect;
554
mtklein533eb782014-08-27 10:39:42 -0700555 // Conservative identity-space bounds for each op in the SkRecord.
556 SkAutoTMalloc<Bounds> fBounds;
mtkleina723b572014-08-15 11:49:49 -0700557
558 // We walk fCurrentOp through the SkRecord, as we go using updateCTM()
559 // and updateClipBounds() to maintain the exact CTM (fCTM) and conservative
mtklein533eb782014-08-27 10:39:42 -0700560 // identity-space bounds of the current clip (fCurrentClipBounds).
mtklein828ce1f2014-08-13 12:58:45 -0700561 unsigned fCurrentOp;
mtklein6332f1d2014-08-19 07:09:40 -0700562 const SkMatrix* fCTM;
mtklein533eb782014-08-27 10:39:42 -0700563 Bounds fCurrentClipBounds;
mtkleina723b572014-08-15 11:49:49 -0700564
565 // Used to track the bounds of Save/Restore blocks and the control ops inside them.
mtklein828ce1f2014-08-13 12:58:45 -0700566 SkTDArray<SaveBounds> fSaveStack;
567 SkTDArray<unsigned> fControlIndices;
mtklein5ad6ee12014-08-11 08:08:43 -0700568};
569
robertphillips4e8e3422014-11-12 06:46:08 -0800570// SkRecord visitor to gather saveLayer/restore information.
571class CollectLayers : SkNoncopyable {
572public:
robertphillips82365912014-11-12 09:32:34 -0800573 CollectLayers(const SkRect& cullRect, const SkRecord& record, SkLayerInfo* accelData)
robertphillips4e8e3422014-11-12 06:46:08 -0800574 : fSaveLayersInStack(0)
575 , fAccelData(accelData)
576 , fFillBounds(cullRect, record) {
577 }
578
579 void setCurrentOp(unsigned currentOp) { fFillBounds.setCurrentOp(currentOp); }
580
581 void cleanUp(SkBBoxHierarchy* bbh) {
582 // fFillBounds must perform its cleanUp first so that all the bounding
583 // boxes associated with unbalanced restores are updated (prior to
584 // fetching their bound in popSaveLayerInfo).
585 fFillBounds.cleanUp(bbh);
586
587 while (!fSaveLayerStack.isEmpty()) {
588 this->popSaveLayerInfo();
589 }
590 }
591
592 template <typename T> void operator()(const T& op) {
593 fFillBounds(op);
594 this->trackSaveLayers(op);
595 }
596
597private:
598 struct SaveLayerInfo {
599 SaveLayerInfo() { }
robertphillips74576eb2014-11-12 07:25:02 -0800600 SaveLayerInfo(int opIndex, bool isSaveLayer, const SkPaint* paint)
robertphillips4e8e3422014-11-12 06:46:08 -0800601 : fStartIndex(opIndex)
602 , fIsSaveLayer(isSaveLayer)
603 , fHasNestedSaveLayer(false)
robertphillips74576eb2014-11-12 07:25:02 -0800604 , fPaint(paint) {
robertphillips4e8e3422014-11-12 06:46:08 -0800605 }
606
607 int fStartIndex;
608 bool fIsSaveLayer;
609 bool fHasNestedSaveLayer;
610 const SkPaint* fPaint;
robertphillips4e8e3422014-11-12 06:46:08 -0800611 };
612
613 template <typename T> void trackSaveLayers(const T& op) {
614 /* most ops aren't involved in saveLayers */
615 }
616 void trackSaveLayers(const Save& s) { this->pushSaveLayerInfo(false, NULL); }
617 void trackSaveLayers(const SaveLayer& sl) { this->pushSaveLayerInfo(true, sl.paint); }
618 void trackSaveLayers(const Restore& r) { this->popSaveLayerInfo(); }
619
620 void trackSaveLayers(const DrawPicture& dp) {
621 // For sub-pictures, we wrap their layer information within the parent
622 // picture's rendering hierarchy
robertphillips82365912014-11-12 09:32:34 -0800623 SkPicture::AccelData::Key key = SkLayerInfo::ComputeKey();
robertphillips4e8e3422014-11-12 06:46:08 -0800624
robertphillips82365912014-11-12 09:32:34 -0800625 const SkLayerInfo* childData =
626 static_cast<const SkLayerInfo*>(dp.picture->EXPERIMENTAL_getAccelData(key));
robertphillips4e8e3422014-11-12 06:46:08 -0800627 if (!childData) {
628 // If the child layer hasn't been generated with saveLayer data we
629 // assume the worst (i.e., that it does contain layers which nest
630 // inside existing layers). Layers within sub-pictures that don't
631 // have saveLayer data cannot be hoisted.
632 // TODO: could the analysis data be use to fine tune this?
633 this->updateStackForSaveLayer();
634 return;
635 }
636
robertphillips82365912014-11-12 09:32:34 -0800637 for (int i = 0; i < childData->numBlocks(); ++i) {
638 const SkLayerInfo::BlockInfo& src = childData->block(i);
robertphillips4e8e3422014-11-12 06:46:08 -0800639
robertphillips74576eb2014-11-12 07:25:02 -0800640 FillBounds::Bounds newBound = fFillBounds.adjustAndMap(src.fBounds, dp.paint);
641 if (newBound.isEmpty()) {
robertphillips4e8e3422014-11-12 06:46:08 -0800642 continue;
643 }
644
645 this->updateStackForSaveLayer();
646
robertphillips82365912014-11-12 09:32:34 -0800647 SkLayerInfo::BlockInfo& dst = fAccelData->addBlock();
robertphillips4e8e3422014-11-12 06:46:08 -0800648
649 // If src.fPicture is NULL the layer is in dp.picture; otherwise
650 // it belongs to a sub-picture.
651 dst.fPicture = src.fPicture ? src.fPicture : static_cast<const SkPicture*>(dp.picture);
652 dst.fPicture->ref();
robertphillips74576eb2014-11-12 07:25:02 -0800653 dst.fBounds = newBound;
robertphillips4e8e3422014-11-12 06:46:08 -0800654 dst.fLocalMat = src.fLocalMat;
655 dst.fPreMat = src.fPreMat;
656 dst.fPreMat.postConcat(fFillBounds.ctm());
657 if (src.fPaint) {
658 dst.fPaint = SkNEW_ARGS(SkPaint, (*src.fPaint));
659 }
660 dst.fSaveLayerOpID = src.fSaveLayerOpID;
661 dst.fRestoreOpID = src.fRestoreOpID;
662 dst.fHasNestedLayers = src.fHasNestedLayers;
663 dst.fIsNested = fSaveLayersInStack > 0 || src.fIsNested;
664 }
665 }
666
667 // Inform all the saveLayers already on the stack that they now have a
668 // nested saveLayer inside them
669 void updateStackForSaveLayer() {
670 for (int index = fSaveLayerStack.count() - 1; index >= 0; --index) {
671 if (fSaveLayerStack[index].fHasNestedSaveLayer) {
672 break;
673 }
674 fSaveLayerStack[index].fHasNestedSaveLayer = true;
675 if (fSaveLayerStack[index].fIsSaveLayer) {
676 break;
677 }
678 }
679 }
680
681 void pushSaveLayerInfo(bool isSaveLayer, const SkPaint* paint) {
682 if (isSaveLayer) {
683 this->updateStackForSaveLayer();
684 ++fSaveLayersInStack;
685 }
686
robertphillips74576eb2014-11-12 07:25:02 -0800687 fSaveLayerStack.push(SaveLayerInfo(fFillBounds.currentOp(), isSaveLayer, paint));
robertphillips4e8e3422014-11-12 06:46:08 -0800688 }
689
690 void popSaveLayerInfo() {
691 if (fSaveLayerStack.count() <= 0) {
692 SkASSERT(false);
693 return;
694 }
695
696 SaveLayerInfo sli;
697 fSaveLayerStack.pop(&sli);
698
699 if (!sli.fIsSaveLayer) {
700 return;
701 }
702
703 --fSaveLayersInStack;
704
robertphillips82365912014-11-12 09:32:34 -0800705 SkLayerInfo::BlockInfo& block = fAccelData->addBlock();
robertphillips4e8e3422014-11-12 06:46:08 -0800706
robertphillips82365912014-11-12 09:32:34 -0800707 SkASSERT(NULL == block.fPicture); // This layer is in the top-most picture
robertphillips4e8e3422014-11-12 06:46:08 -0800708
robertphillips82365912014-11-12 09:32:34 -0800709 block.fBounds = fFillBounds.getBounds(sli.fStartIndex);
710 block.fLocalMat = fFillBounds.ctm();
711 block.fPreMat = SkMatrix::I();
robertphillips4e8e3422014-11-12 06:46:08 -0800712 if (sli.fPaint) {
robertphillips82365912014-11-12 09:32:34 -0800713 block.fPaint = SkNEW_ARGS(SkPaint, (*sli.fPaint));
robertphillips4e8e3422014-11-12 06:46:08 -0800714 }
robertphillips82365912014-11-12 09:32:34 -0800715 block.fSaveLayerOpID = sli.fStartIndex;
716 block.fRestoreOpID = fFillBounds.currentOp();
717 block.fHasNestedLayers = sli.fHasNestedSaveLayer;
718 block.fIsNested = fSaveLayersInStack > 0;
robertphillips4e8e3422014-11-12 06:46:08 -0800719 }
720
721 // Used to collect saveLayer information for layer hoisting
722 int fSaveLayersInStack;
723 SkTDArray<SaveLayerInfo> fSaveLayerStack;
robertphillips82365912014-11-12 09:32:34 -0800724 SkLayerInfo* fAccelData;
robertphillips4e8e3422014-11-12 06:46:08 -0800725
726 SkRecords::FillBounds fFillBounds;
727};
robertphillips4e8e3422014-11-12 06:46:08 -0800728
commit-bot@chromium.org27f6b0d2014-05-09 14:59:29 +0000729} // namespace SkRecords
mtklein5ad6ee12014-08-11 08:08:43 -0700730
robertphillips4d52afe2014-11-03 08:19:44 -0800731void SkRecordFillBounds(const SkRect& cullRect, const SkRecord& record, SkBBoxHierarchy* bbh) {
robertphillips4e8e3422014-11-12 06:46:08 -0800732 SkRecords::FillBounds visitor(cullRect, record);
733
734 for (unsigned curOp = 0; curOp < record.count(); curOp++) {
735 visitor.setCurrentOp(curOp);
736 record.visit<void>(curOp, visitor);
737 }
738
739 visitor.cleanUp(bbh);
mtklein5ad6ee12014-08-11 08:08:43 -0700740}
robertphillips4e8e3422014-11-12 06:46:08 -0800741
robertphillips4e8e3422014-11-12 06:46:08 -0800742void SkRecordComputeLayers(const SkRect& cullRect, const SkRecord& record,
robertphillips82365912014-11-12 09:32:34 -0800743 SkBBoxHierarchy* bbh, SkLayerInfo* data) {
robertphillips4e8e3422014-11-12 06:46:08 -0800744 SkRecords::CollectLayers visitor(cullRect, record, data);
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);
752}
robertphillips4e8e3422014-11-12 06:46:08 -0800753