blob: 9b08426670fc4bfc2410fb1cb1aa1cba091e3916 [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,
reed1bdfd3f2014-11-24 14:41:51 -080014 SkPicture const* const drawablePicts[],
reed3cb38402015-02-06 08:36:15 -080015 SkDrawable* const drawables[],
reed1bdfd3f2014-11-24 14:41:51 -080016 int drawableCount,
mtklein5ad6ee12014-08-11 08:08:43 -070017 const SkBBoxHierarchy* bbh,
robertphillips783fe162015-01-07 07:28:41 -080018 SkPicture::AbortCallback* 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.
mtklein7cc1a342014-11-20 08:01:09 -080027 SkRect query;
28 if (!canvas->getClipBounds(&query)) {
mtklein49aabde2015-01-05 07:02:45 -080029 query.setEmpty();
mtklein7cc1a342014-11-20 08:01:09 -080030 }
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
reed1bdfd3f2014-11-24 14:41:51 -080035 SkRecords::Draw draw(canvas, drawablePicts, drawables, drawableCount);
mtklein5ad6ee12014-08-11 08:08:43 -070036 for (int i = 0; i < ops.count(); i++) {
robertphillips783fe162015-01-07 07:28:41 -080037 if (callback && callback->abort()) {
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.
reed1bdfd3f2014-11-24 14:41:51 -080047 SkRecords::Draw draw(canvas, drawablePicts, drawables, drawableCount);
mtklein00f30bd2014-09-02 12:03:31 -070048 for (unsigned i = 0; i < record.count(); i++) {
robertphillips783fe162015-01-07 07:28:41 -080049 if (callback && callback->abort()) {
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
reed6be2aa92014-11-18 11:08:05 -080060void SkRecordPartialDraw(const SkRecord& record, SkCanvas* canvas,
61 SkPicture const* const drawablePicts[], int drawableCount,
robertphillips4815fe52014-09-16 10:32:43 -070062 unsigned start, unsigned stop,
63 const SkMatrix& initialCTM) {
mtklein00f30bd2014-09-02 12:03:31 -070064 SkAutoCanvasRestore saveRestore(canvas, true /*save now, restore at exit*/);
65
66 stop = SkTMin(stop, record.count());
reed8eddfb52014-12-04 07:50:14 -080067 SkRecords::Draw draw(canvas, drawablePicts, NULL, drawableCount, &initialCTM);
mtklein00f30bd2014-09-02 12:03:31 -070068 for (unsigned i = start; i < stop; i++) {
69 record.visit<void>(i, draw);
70 }
71}
72
commit-bot@chromium.org27f6b0d2014-05-09 14:59:29 +000073namespace SkRecords {
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000074
commit-bot@chromium.org2e0c32a2014-04-28 16:19:45 +000075// NoOps draw nothing.
commit-bot@chromium.org27f6b0d2014-05-09 14:59:29 +000076template <> void Draw::draw(const NoOp&) {}
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000077
commit-bot@chromium.org27f6b0d2014-05-09 14:59:29 +000078#define DRAW(T, call) template <> void Draw::draw(const T& r) { fCanvas->call; }
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000079DRAW(Restore, restore());
Florin Malita5f6102d2014-06-30 10:13:28 -040080DRAW(Save, save());
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000081DRAW(SaveLayer, saveLayer(r.bounds, r.paint, r.flags));
commit-bot@chromium.org99bd7d82014-05-19 15:51:12 +000082DRAW(SetMatrix, setMatrix(SkMatrix::Concat(fInitialCTM, r.matrix)));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000083
mtkleincdeeb092014-11-20 09:14:28 -080084DRAW(ClipPath, clipPath(r.path, r.opAA.op, r.opAA.aa));
85DRAW(ClipRRect, clipRRect(r.rrect, r.opAA.op, r.opAA.aa));
86DRAW(ClipRect, clipRect(r.rect, r.opAA.op, r.opAA.aa));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000087DRAW(ClipRegion, clipRegion(r.region, r.op));
88
mtklein5f0e8222014-08-22 11:44:26 -070089DRAW(BeginCommentGroup, beginCommentGroup(r.description));
90DRAW(AddComment, addComment(r.key, r.value));
91DRAW(EndCommentGroup, endCommentGroup());
92
mtkleinf55c3142014-12-11 12:43:04 -080093DRAW(DrawBitmap, drawBitmap(r.bitmap.shallowCopy(), r.left, r.top, r.paint));
94DRAW(DrawBitmapNine, drawBitmapNine(r.bitmap.shallowCopy(), r.center, r.dst, r.paint));
mtklein7cdc1ee2014-07-07 10:41:04 -070095DRAW(DrawBitmapRectToRect,
mtkleinf55c3142014-12-11 12:43:04 -080096 drawBitmapRectToRect(r.bitmap.shallowCopy(), r.src, r.dst, r.paint,
mtklein42ddcd42014-11-21 08:48:35 -080097 SkCanvas::kNone_DrawBitmapRectFlag));
98DRAW(DrawBitmapRectToRectBleed,
mtkleinf55c3142014-12-11 12:43:04 -080099 drawBitmapRectToRect(r.bitmap.shallowCopy(), r.src, r.dst, r.paint,
mtklein42ddcd42014-11-21 08:48:35 -0800100 SkCanvas::kBleed_DrawBitmapRectFlag));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +0000101DRAW(DrawDRRect, drawDRRect(r.outer, r.inner, r.paint));
piotaixr65151752014-10-16 11:58:39 -0700102DRAW(DrawImage, drawImage(r.image, r.left, r.top, r.paint));
103DRAW(DrawImageRect, drawImageRect(r.image, r.src, r.dst, r.paint));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +0000104DRAW(DrawOval, drawOval(r.oval, r.paint));
105DRAW(DrawPaint, drawPaint(r.paint));
106DRAW(DrawPath, drawPath(r.path, r.paint));
mtklein9b222a52014-09-18 11:16:31 -0700107DRAW(DrawPatch, drawPatch(r.cubics, r.colors, r.texCoords, r.xmode, r.paint));
mtkleinaf579032014-12-01 11:03:37 -0800108DRAW(DrawPicture, drawPicture(r.picture, &r.matrix, r.paint));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +0000109DRAW(DrawPoints, drawPoints(r.mode, r.count, r.pts, r.paint));
110DRAW(DrawPosText, drawPosText(r.text, r.byteLength, r.pos, r.paint));
111DRAW(DrawPosTextH, drawPosTextH(r.text, r.byteLength, r.xpos, r.y, r.paint));
112DRAW(DrawRRect, drawRRect(r.rrect, r.paint));
113DRAW(DrawRect, drawRect(r.rect, r.paint));
mtkleinf55c3142014-12-11 12:43:04 -0800114DRAW(DrawSprite, drawSprite(r.bitmap.shallowCopy(), r.left, r.top, r.paint));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +0000115DRAW(DrawText, drawText(r.text, r.byteLength, r.x, r.y, r.paint));
fmalita00d5c2c2014-08-21 08:53:26 -0700116DRAW(DrawTextBlob, drawTextBlob(r.blob, r.x, r.y, r.paint));
mtkleinaf579032014-12-01 11:03:37 -0800117DRAW(DrawTextOnPath, drawTextOnPath(r.text, r.byteLength, r.path, &r.matrix, r.paint));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +0000118DRAW(DrawVertices, drawVertices(r.vmode, r.vertexCount, r.vertices, r.texs, r.colors,
119 r.xmode.get(), r.indices, r.indexCount, r.paint));
120#undef DRAW
121
reed6be2aa92014-11-18 11:08:05 -0800122template <> void Draw::draw(const DrawDrawable& r) {
123 SkASSERT(r.index >= 0);
124 SkASSERT(r.index < fDrawableCount);
reed1bdfd3f2014-11-24 14:41:51 -0800125 if (fDrawables) {
126 SkASSERT(NULL == fDrawablePicts);
reed3cb38402015-02-06 08:36:15 -0800127 fCanvas->drawDrawable(fDrawables[r.index]);
reed1bdfd3f2014-11-24 14:41:51 -0800128 } else {
129 fCanvas->drawPicture(fDrawablePicts[r.index]);
130 }
reed6be2aa92014-11-18 11:08:05 -0800131}
132
mtklein5ad6ee12014-08-11 08:08:43 -0700133// This is an SkRecord visitor that fills an SkBBoxHierarchy.
mtklein828ce1f2014-08-13 12:58:45 -0700134//
135// The interesting part here is how to calculate bounds for ops which don't
136// have intrinsic bounds. What is the bounds of a Save or a Translate?
137//
138// We answer this by thinking about a particular definition of bounds: if I
139// don't execute this op, pixels in this rectangle might draw incorrectly. So
140// the bounds of a Save, a Translate, a Restore, etc. are the union of the
141// bounds of Draw* ops that they might have an effect on. For any given
142// Save/Restore block, the bounds of the Save, the Restore, and any other
143// non-drawing ("control") ops inside are exactly the union of the bounds of
144// the drawing ops inside that block.
145//
146// To implement this, we keep a stack of active Save blocks. As we consume ops
147// inside the Save/Restore block, drawing ops are unioned with the bounds of
148// the block, and control ops are stashed away for later. When we finish the
149// block with a Restore, our bounds are complete, and we go back and fill them
150// in for all the control ops we stashed away.
mtklein5ad6ee12014-08-11 08:08:43 -0700151class FillBounds : SkNoncopyable {
152public:
robertphillips4e8e3422014-11-12 06:46:08 -0800153 FillBounds(const SkRect& cullRect, const SkRecord& record)
154 : fNumRecords(record.count())
155 , fCullRect(cullRect)
reed1bdfd3f2014-11-24 14:41:51 -0800156 , fBounds(record.count())
157 {
mtklein828ce1f2014-08-13 12:58:45 -0700158 // Calculate bounds for all ops. This won't go quite in order, so we'll need
159 // to store the bounds separately then feed them in to the BBH later in order.
mtklein6332f1d2014-08-19 07:09:40 -0700160 fCTM = &SkMatrix::I();
robertphillips4d52afe2014-11-03 08:19:44 -0800161 fCurrentClipBounds = fCullRect;
robertphillips4e8e3422014-11-12 06:46:08 -0800162 }
mtklein5ad6ee12014-08-11 08:08:43 -0700163
robertphillips4e8e3422014-11-12 06:46:08 -0800164 void setCurrentOp(unsigned currentOp) { fCurrentOp = currentOp; }
165
166 void cleanUp(SkBBoxHierarchy* bbh) {
mtklein828ce1f2014-08-13 12:58:45 -0700167 // If we have any lingering unpaired Saves, simulate restores to make
168 // sure all ops in those Save blocks have their bounds calculated.
169 while (!fSaveStack.isEmpty()) {
170 this->popSaveBlock();
171 }
172
173 // Any control ops not part of any Save/Restore block draw everywhere.
174 while (!fControlIndices.isEmpty()) {
robertphillips4d52afe2014-11-03 08:19:44 -0800175 this->popControl(fCullRect);
mtklein828ce1f2014-08-13 12:58:45 -0700176 }
177
178 // Finally feed all stored bounds into the BBH. They'll be returned in this order.
robertphillips89108792014-11-17 08:16:15 -0800179 if (bbh) {
mtkleinbfd5bff2015-02-10 13:44:27 -0800180 bbh->insert(fBounds.get(), fNumRecords);
robertphillips89108792014-11-17 08:16:15 -0800181 }
mtklein828ce1f2014-08-13 12:58:45 -0700182 }
mtklein5ad6ee12014-08-11 08:08:43 -0700183
mtkleina723b572014-08-15 11:49:49 -0700184 template <typename T> void operator()(const T& op) {
185 this->updateCTM(op);
186 this->updateClipBounds(op);
187 this->trackBounds(op);
mtklein5ad6ee12014-08-11 08:08:43 -0700188 }
189
mtklein533eb782014-08-27 10:39:42 -0700190 // In this file, SkRect are in local coordinates, Bounds are translated back to identity space.
191 typedef SkRect Bounds;
192
robertphillips4e8e3422014-11-12 06:46:08 -0800193 unsigned currentOp() const { return fCurrentOp; }
194 const SkMatrix& ctm() const { return *fCTM; }
robertphillips4e8e3422014-11-12 06:46:08 -0800195 const Bounds& getBounds(unsigned index) const { return fBounds[index]; }
196
197 // Adjust rect for all paints that may affect its geometry, then map it to identity space.
198 Bounds adjustAndMap(SkRect rect, const SkPaint* paint) const {
199 // Inverted rectangles really confuse our BBHs.
200 rect.sort();
201
202 // Adjust the rect for its own paint.
203 if (!AdjustForPaint(paint, &rect)) {
204 // The paint could do anything to our bounds. The only safe answer is the current clip.
205 return fCurrentClipBounds;
206 }
207
208 // Adjust rect for all the paints from the SaveLayers we're inside.
209 if (!this->adjustForSaveLayerPaints(&rect)) {
210 // Same deal as above.
211 return fCurrentClipBounds;
212 }
213
214 // Map the rect back to identity space.
215 fCTM->mapRect(&rect);
216
217 // Nothing can draw outside the current clip.
robertphillipsc187a3c2014-12-30 13:53:51 -0800218 if (!rect.intersect(fCurrentClipBounds)) {
219 return Bounds::MakeEmpty();
220 }
221
robertphillips4e8e3422014-11-12 06:46:08 -0800222 return rect;
223 }
224
225private:
mtklein828ce1f2014-08-13 12:58:45 -0700226 struct SaveBounds {
mtkleina723b572014-08-15 11:49:49 -0700227 int controlOps; // Number of control ops in this Save block, including the Save.
mtklein533eb782014-08-27 10:39:42 -0700228 Bounds bounds; // Bounds of everything in the block.
mtkleina723b572014-08-15 11:49:49 -0700229 const SkPaint* paint; // Unowned. If set, adjusts the bounds of all ops in this block.
mtklein828ce1f2014-08-13 12:58:45 -0700230 };
231
mtklein8e393bf2014-10-01 12:48:58 -0700232 // Only Restore and SetMatrix change the CTM.
233 template <typename T> void updateCTM(const T&) {}
mtklein6332f1d2014-08-19 07:09:40 -0700234 void updateCTM(const Restore& op) { fCTM = &op.matrix; }
235 void updateCTM(const SetMatrix& op) { fCTM = &op.matrix; }
mtkleina723b572014-08-15 11:49:49 -0700236
mtklein8e393bf2014-10-01 12:48:58 -0700237 // Most ops don't change the clip.
238 template <typename T> void updateClipBounds(const T&) {}
Mike Klein271a0302014-09-23 15:28:38 -0400239
mtklein8e393bf2014-10-01 12:48:58 -0700240 // Clip{Path,RRect,Rect,Region} obviously change the clip. They all know their bounds already.
241 void updateClipBounds(const ClipPath& op) { this->updateClipBoundsForClipOp(op.devBounds); }
242 void updateClipBounds(const ClipRRect& op) { this->updateClipBoundsForClipOp(op.devBounds); }
243 void updateClipBounds(const ClipRect& op) { this->updateClipBoundsForClipOp(op.devBounds); }
244 void updateClipBounds(const ClipRegion& op) { this->updateClipBoundsForClipOp(op.devBounds); }
Mike Klein271a0302014-09-23 15:28:38 -0400245
mtklein8e393bf2014-10-01 12:48:58 -0700246 // The bounds of clip ops need to be adjusted for the paints of saveLayers they're inside.
247 void updateClipBoundsForClipOp(const SkIRect& devBounds) {
248 Bounds clip = SkRect::Make(devBounds);
Mike Klein271a0302014-09-23 15:28:38 -0400249 // We don't call adjustAndMap() because as its last step it would intersect the adjusted
250 // clip bounds with the previous clip, exactly what we can't do when the clip grows.
robertphillips4d52afe2014-11-03 08:19:44 -0800251 fCurrentClipBounds = this->adjustForSaveLayerPaints(&clip) ? clip : fCullRect;
Mike Klein271a0302014-09-23 15:28:38 -0400252 }
253
mtklein8e393bf2014-10-01 12:48:58 -0700254 // Restore holds the devBounds for the clip after the {save,saveLayer}/restore block completes.
255 void updateClipBounds(const Restore& op) {
256 // This is just like the clip ops above, but we need to skip the effects (if any) of our
257 // paired saveLayer (if it is one); it has not yet been popped off the save stack. Our
258 // devBounds reflect the state of the world after the saveLayer/restore block is done,
259 // so they are not affected by the saveLayer's paint.
260 const int kSavesToIgnore = 1;
261 Bounds clip = SkRect::Make(op.devBounds);
262 fCurrentClipBounds =
robertphillips4d52afe2014-11-03 08:19:44 -0800263 this->adjustForSaveLayerPaints(&clip, kSavesToIgnore) ? clip : fCullRect;
mtklein8e393bf2014-10-01 12:48:58 -0700264 }
265
Mike Klein271a0302014-09-23 15:28:38 -0400266 // We also take advantage of SaveLayer bounds when present to further cut the clip down.
mtkleina723b572014-08-15 11:49:49 -0700267 void updateClipBounds(const SaveLayer& op) {
268 if (op.bounds) {
Mike Klein271a0302014-09-23 15:28:38 -0400269 // adjustAndMap() intersects these layer bounds with the previous clip for us.
270 fCurrentClipBounds = this->adjustAndMap(*op.bounds, op.paint);
mtkleina723b572014-08-15 11:49:49 -0700271 }
272 }
mtklein6cfa73a2014-08-13 13:33:49 -0700273
mtklein828ce1f2014-08-13 12:58:45 -0700274 // The bounds of these ops must be calculated when we hit the Restore
275 // from the bounds of the ops in the same Save block.
mtkleina723b572014-08-15 11:49:49 -0700276 void trackBounds(const Save&) { this->pushSaveBlock(NULL); }
mtkleina723b572014-08-15 11:49:49 -0700277 void trackBounds(const SaveLayer& op) { this->pushSaveBlock(op.paint); }
278 void trackBounds(const Restore&) { fBounds[fCurrentOp] = this->popSaveBlock(); }
mtklein828ce1f2014-08-13 12:58:45 -0700279
mtklein68199a22014-08-25 13:49:29 -0700280 void trackBounds(const SetMatrix&) { this->pushControl(); }
281 void trackBounds(const ClipRect&) { this->pushControl(); }
282 void trackBounds(const ClipRRect&) { this->pushControl(); }
283 void trackBounds(const ClipPath&) { this->pushControl(); }
284 void trackBounds(const ClipRegion&) { this->pushControl(); }
mtklein68199a22014-08-25 13:49:29 -0700285 void trackBounds(const BeginCommentGroup&) { this->pushControl(); }
286 void trackBounds(const AddComment&) { this->pushControl(); }
287 void trackBounds(const EndCommentGroup&) { this->pushControl(); }
mtklein828ce1f2014-08-13 12:58:45 -0700288
289 // For all other ops, we can calculate and store the bounds directly now.
290 template <typename T> void trackBounds(const T& op) {
291 fBounds[fCurrentOp] = this->bounds(op);
292 this->updateSaveBounds(fBounds[fCurrentOp]);
mtklein5ad6ee12014-08-11 08:08:43 -0700293 }
294
mtkleina723b572014-08-15 11:49:49 -0700295 void pushSaveBlock(const SkPaint* paint) {
mtklein828ce1f2014-08-13 12:58:45 -0700296 // Starting a new Save block. Push a new entry to represent that.
robertphillips4d52afe2014-11-03 08:19:44 -0800297 SaveBounds sb;
298 sb.controlOps = 0;
299 // If the paint affects transparent black, the bound shouldn't be smaller
300 // than the current clip bounds.
301 sb.bounds =
302 PaintMayAffectTransparentBlack(paint) ? fCurrentClipBounds : Bounds::MakeEmpty();
303 sb.paint = paint;
304
mtklein828ce1f2014-08-13 12:58:45 -0700305 fSaveStack.push(sb);
306 this->pushControl();
307 }
308
mtkleind910f542014-08-22 09:06:34 -0700309 static bool PaintMayAffectTransparentBlack(const SkPaint* paint) {
dneto327f9052014-09-15 10:53:16 -0700310 if (paint) {
311 // FIXME: this is very conservative
312 if (paint->getImageFilter() || paint->getColorFilter()) {
313 return true;
314 }
315
316 // Unusual Xfermodes require us to process a saved layer
317 // even with operations outisde the clip.
318 // For example, DstIn is used by masking layers.
319 // https://code.google.com/p/skia/issues/detail?id=1291
320 // https://crbug.com/401593
321 SkXfermode* xfermode = paint->getXfermode();
322 SkXfermode::Mode mode;
323 // SrcOver is ok, and is also the common case with a NULL xfermode.
324 // So we should make that the fast path and bypass the mode extraction
325 // and test.
326 if (xfermode && xfermode->asMode(&mode)) {
327 switch (mode) {
328 // For each of the following transfer modes, if the source
329 // alpha is zero (our transparent black), the resulting
330 // blended alpha is not necessarily equal to the original
331 // destination alpha.
332 case SkXfermode::kClear_Mode:
333 case SkXfermode::kSrc_Mode:
334 case SkXfermode::kSrcIn_Mode:
335 case SkXfermode::kDstIn_Mode:
336 case SkXfermode::kSrcOut_Mode:
337 case SkXfermode::kDstATop_Mode:
338 case SkXfermode::kModulate_Mode:
339 return true;
340 break;
341 default:
342 break;
343 }
344 }
345 }
346 return false;
mtkleind910f542014-08-22 09:06:34 -0700347 }
348
mtklein533eb782014-08-27 10:39:42 -0700349 Bounds popSaveBlock() {
mtklein828ce1f2014-08-13 12:58:45 -0700350 // We're done the Save block. Apply the block's bounds to all control ops inside it.
351 SaveBounds sb;
352 fSaveStack.pop(&sb);
mtkleind910f542014-08-22 09:06:34 -0700353
mtklein828ce1f2014-08-13 12:58:45 -0700354 while (sb.controlOps --> 0) {
robertphillips4d52afe2014-11-03 08:19:44 -0800355 this->popControl(sb.bounds);
mtklein828ce1f2014-08-13 12:58:45 -0700356 }
357
358 // This whole Save block may be part another Save block.
robertphillips4d52afe2014-11-03 08:19:44 -0800359 this->updateSaveBounds(sb.bounds);
mtklein828ce1f2014-08-13 12:58:45 -0700360
361 // If called from a real Restore (not a phony one for balance), it'll need the bounds.
robertphillips4d52afe2014-11-03 08:19:44 -0800362 return sb.bounds;
mtklein828ce1f2014-08-13 12:58:45 -0700363 }
364
365 void pushControl() {
366 fControlIndices.push(fCurrentOp);
367 if (!fSaveStack.isEmpty()) {
368 fSaveStack.top().controlOps++;
369 }
370 }
371
mtklein533eb782014-08-27 10:39:42 -0700372 void popControl(const Bounds& bounds) {
mtklein828ce1f2014-08-13 12:58:45 -0700373 fBounds[fControlIndices.top()] = bounds;
374 fControlIndices.pop();
375 }
376
mtklein533eb782014-08-27 10:39:42 -0700377 void updateSaveBounds(const Bounds& bounds) {
mtklein828ce1f2014-08-13 12:58:45 -0700378 // If we're in a Save block, expand its bounds to cover these bounds too.
379 if (!fSaveStack.isEmpty()) {
380 fSaveStack.top().bounds.join(bounds);
381 }
382 }
383
mtklein131a22b2014-08-25 14:16:15 -0700384 // FIXME: this method could use better bounds
mtklein533eb782014-08-27 10:39:42 -0700385 Bounds bounds(const DrawText&) const { return fCurrentClipBounds; }
mtklein68199a22014-08-25 13:49:29 -0700386
mtklein533eb782014-08-27 10:39:42 -0700387 Bounds bounds(const DrawPaint&) const { return fCurrentClipBounds; }
388 Bounds bounds(const NoOp&) const { return Bounds::MakeEmpty(); } // NoOps don't draw.
mtklein828ce1f2014-08-13 12:58:45 -0700389
mtkleinaf579032014-12-01 11:03:37 -0800390 Bounds bounds(const DrawSprite& op) const { // Ignores the matrix.
391 return Bounds::MakeXYWH(op.left, op.top, op.bitmap.width(), op.bitmap.height());
mtklein131a22b2014-08-25 14:16:15 -0700392 }
393
mtklein533eb782014-08-27 10:39:42 -0700394 Bounds bounds(const DrawRect& op) const { return this->adjustAndMap(op.rect, &op.paint); }
395 Bounds bounds(const DrawOval& op) const { return this->adjustAndMap(op.oval, &op.paint); }
396 Bounds bounds(const DrawRRect& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700397 return this->adjustAndMap(op.rrect.rect(), &op.paint);
398 }
mtklein533eb782014-08-27 10:39:42 -0700399 Bounds bounds(const DrawDRRect& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700400 return this->adjustAndMap(op.outer.rect(), &op.paint);
401 }
piotaixr65151752014-10-16 11:58:39 -0700402 Bounds bounds(const DrawImage& op) const {
403 const SkImage* image = op.image;
404 SkRect rect = SkRect::MakeXYWH(op.left, op.top, image->width(), image->height());
mtklein62b67ae2014-08-18 11:10:37 -0700405
piotaixr65151752014-10-16 11:58:39 -0700406 return this->adjustAndMap(rect, op.paint);
407 }
408 Bounds bounds(const DrawImageRect& op) const {
409 return this->adjustAndMap(op.dst, op.paint);
410 }
mtklein533eb782014-08-27 10:39:42 -0700411 Bounds bounds(const DrawBitmapRectToRect& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700412 return this->adjustAndMap(op.dst, op.paint);
413 }
mtklein42ddcd42014-11-21 08:48:35 -0800414 Bounds bounds(const DrawBitmapRectToRectBleed& op) const {
415 return this->adjustAndMap(op.dst, op.paint);
416 }
mtklein533eb782014-08-27 10:39:42 -0700417 Bounds bounds(const DrawBitmapNine& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700418 return this->adjustAndMap(op.dst, op.paint);
419 }
mtklein533eb782014-08-27 10:39:42 -0700420 Bounds bounds(const DrawBitmap& op) const {
mtkleinaf579032014-12-01 11:03:37 -0800421 return this->adjustAndMap(
422 SkRect::MakeXYWH(op.left, op.top, op.bitmap.width(), op.bitmap.height()),
423 op.paint);
mtklein62b67ae2014-08-18 11:10:37 -0700424 }
mtklein62b67ae2014-08-18 11:10:37 -0700425
mtklein533eb782014-08-27 10:39:42 -0700426 Bounds bounds(const DrawPath& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700427 return op.path.isInverseFillType() ? fCurrentClipBounds
428 : this->adjustAndMap(op.path.getBounds(), &op.paint);
429 }
mtklein533eb782014-08-27 10:39:42 -0700430 Bounds bounds(const DrawPoints& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700431 SkRect dst;
432 dst.set(op.pts, op.count);
433
434 // Pad the bounding box a little to make sure hairline points' bounds aren't empty.
435 SkScalar stroke = SkMaxScalar(op.paint.getStrokeWidth(), 0.01f);
436 dst.outset(stroke/2, stroke/2);
437
438 return this->adjustAndMap(dst, &op.paint);
439 }
mtklein533eb782014-08-27 10:39:42 -0700440 Bounds bounds(const DrawPatch& op) const {
mtklein131a22b2014-08-25 14:16:15 -0700441 SkRect dst;
442 dst.set(op.cubics, SkPatchUtils::kNumCtrlPts);
443 return this->adjustAndMap(dst, &op.paint);
444 }
mtklein533eb782014-08-27 10:39:42 -0700445 Bounds bounds(const DrawVertices& op) const {
mtklein131a22b2014-08-25 14:16:15 -0700446 SkRect dst;
447 dst.set(op.vertices, op.vertexCount);
448 return this->adjustAndMap(dst, &op.paint);
449 }
450
mtklein533eb782014-08-27 10:39:42 -0700451 Bounds bounds(const DrawPicture& op) const {
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700452 SkRect dst = op.picture->cullRect();
mtkleinaf579032014-12-01 11:03:37 -0800453 op.matrix.mapRect(&dst);
mtklein131a22b2014-08-25 14:16:15 -0700454 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
mtklein9a5380d2014-12-16 06:31:01 -0800486 // 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);
mtklein9a5380d2014-12-16 06:31:01 -0800489
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);
495
mtklein131a22b2014-08-25 14:16:15 -0700496 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
reed6be2aa92014-11-18 11:08:05 -0800505 Bounds bounds(const DrawDrawable& op) const {
506 return this->adjustAndMap(op.worstCaseBounds, NULL);
507 }
508
mtklein62b67ae2014-08-18 11:10:37 -0700509 static void AdjustTextForFontMetrics(SkRect* rect, const SkPaint& paint) {
mtklein9a5380d2014-12-16 06:31:01 -0800510#ifdef SK_DEBUG
511 SkRect correct = *rect;
512#endif
513 // crbug.com/373785 ~~> xPad = 4x yPad
514 // crbug.com/424824 ~~> bump yPad from 2x text size to 2.5x
515 const SkScalar yPad = 2.5f * paint.getTextSize(),
516 xPad = 4.0f * yPad;
517 rect->outset(xPad, yPad);
caryclark9a657fa2014-08-20 05:24:29 -0700518#ifdef SK_DEBUG
mtklein62b67ae2014-08-18 11:10:37 -0700519 SkPaint::FontMetrics metrics;
520 paint.getFontMetrics(&metrics);
mtklein9a5380d2014-12-16 06:31:01 -0800521 correct.fLeft += metrics.fXMin;
522 correct.fTop += metrics.fTop;
523 correct.fRight += metrics.fXMax;
524 correct.fBottom += metrics.fBottom;
mtkleind13291a2014-08-21 14:46:49 -0700525 // See skia:2862 for why we ignore small text sizes.
mtklein9a5380d2014-12-16 06:31:01 -0800526 SkASSERTF(paint.getTextSize() < 0.001f || rect->contains(correct),
mtkleined167ac2014-10-29 16:07:10 -0700527 "%f %f %f %f vs. %f %f %f %f\n",
mtklein9a5380d2014-12-16 06:31:01 -0800528 -xPad, -yPad, +xPad, +yPad,
mtkleined167ac2014-10-29 16:07:10 -0700529 metrics.fXMin, metrics.fTop, metrics.fXMax, metrics.fBottom);
mtkleina19afb42014-08-19 17:47:14 -0700530#endif
mtklein62b67ae2014-08-18 11:10:37 -0700531 }
532
mtklein479601b2014-08-18 08:45:33 -0700533 // Returns true if rect was meaningfully adjusted for the effects of paint,
534 // false if the paint could affect the rect in unknown ways.
535 static bool AdjustForPaint(const SkPaint* paint, SkRect* rect) {
mtkleina723b572014-08-15 11:49:49 -0700536 if (paint) {
537 if (paint->canComputeFastBounds()) {
mtklein479601b2014-08-18 08:45:33 -0700538 *rect = paint->computeFastBounds(*rect, rect);
539 return true;
mtkleina723b572014-08-15 11:49:49 -0700540 }
mtklein479601b2014-08-18 08:45:33 -0700541 return false;
542 }
543 return true;
544 }
545
mtklein8e393bf2014-10-01 12:48:58 -0700546 bool adjustForSaveLayerPaints(SkRect* rect, int savesToIgnore = 0) const {
547 for (int i = fSaveStack.count() - 1 - savesToIgnore; i >= 0; i--) {
Mike Klein271a0302014-09-23 15:28:38 -0400548 if (!AdjustForPaint(fSaveStack[i].paint, rect)) {
549 return false;
550 }
551 }
552 return true;
553 }
554
robertphillips4e8e3422014-11-12 06:46:08 -0800555 const unsigned fNumRecords;
mtkleina723b572014-08-15 11:49:49 -0700556
robertphillips4d52afe2014-11-03 08:19:44 -0800557 // We do not guarantee anything for operations outside of the cull rect
558 const SkRect fCullRect;
559
mtklein533eb782014-08-27 10:39:42 -0700560 // Conservative identity-space bounds for each op in the SkRecord.
561 SkAutoTMalloc<Bounds> fBounds;
mtkleina723b572014-08-15 11:49:49 -0700562
563 // We walk fCurrentOp through the SkRecord, as we go using updateCTM()
564 // and updateClipBounds() to maintain the exact CTM (fCTM) and conservative
mtklein533eb782014-08-27 10:39:42 -0700565 // identity-space bounds of the current clip (fCurrentClipBounds).
mtklein828ce1f2014-08-13 12:58:45 -0700566 unsigned fCurrentOp;
mtklein6332f1d2014-08-19 07:09:40 -0700567 const SkMatrix* fCTM;
mtklein533eb782014-08-27 10:39:42 -0700568 Bounds fCurrentClipBounds;
mtkleina723b572014-08-15 11:49:49 -0700569
570 // Used to track the bounds of Save/Restore blocks and the control ops inside them.
mtklein828ce1f2014-08-13 12:58:45 -0700571 SkTDArray<SaveBounds> fSaveStack;
572 SkTDArray<unsigned> fControlIndices;
mtklein5ad6ee12014-08-11 08:08:43 -0700573};
574
robertphillips4e8e3422014-11-12 06:46:08 -0800575// SkRecord visitor to gather saveLayer/restore information.
576class CollectLayers : SkNoncopyable {
577public:
reed1bdfd3f2014-11-24 14:41:51 -0800578 CollectLayers(const SkRect& cullRect, const SkRecord& record,
579 const SkPicture::SnapshotArray* pictList, SkLayerInfo* accelData)
robertphillips4e8e3422014-11-12 06:46:08 -0800580 : fSaveLayersInStack(0)
581 , fAccelData(accelData)
reed1bdfd3f2014-11-24 14:41:51 -0800582 , fPictList(pictList)
583 , fFillBounds(cullRect, record)
584 {}
robertphillips4e8e3422014-11-12 06:46:08 -0800585
586 void setCurrentOp(unsigned currentOp) { fFillBounds.setCurrentOp(currentOp); }
587
588 void cleanUp(SkBBoxHierarchy* bbh) {
589 // fFillBounds must perform its cleanUp first so that all the bounding
590 // boxes associated with unbalanced restores are updated (prior to
591 // fetching their bound in popSaveLayerInfo).
592 fFillBounds.cleanUp(bbh);
593
594 while (!fSaveLayerStack.isEmpty()) {
595 this->popSaveLayerInfo();
596 }
597 }
598
599 template <typename T> void operator()(const T& op) {
600 fFillBounds(op);
601 this->trackSaveLayers(op);
602 }
603
604private:
605 struct SaveLayerInfo {
606 SaveLayerInfo() { }
robertphillips478dd722014-12-16 08:25:55 -0800607 SaveLayerInfo(int opIndex, bool isSaveLayer, const SkRect* bounds, const SkPaint* paint)
robertphillips4e8e3422014-11-12 06:46:08 -0800608 : fStartIndex(opIndex)
609 , fIsSaveLayer(isSaveLayer)
610 , fHasNestedSaveLayer(false)
robertphillips478dd722014-12-16 08:25:55 -0800611 , fBounds(bounds ? *bounds : SkRect::MakeEmpty())
robertphillips74576eb2014-11-12 07:25:02 -0800612 , fPaint(paint) {
robertphillips4e8e3422014-11-12 06:46:08 -0800613 }
614
615 int fStartIndex;
616 bool fIsSaveLayer;
617 bool fHasNestedSaveLayer;
robertphillips478dd722014-12-16 08:25:55 -0800618 SkRect fBounds;
robertphillips4e8e3422014-11-12 06:46:08 -0800619 const SkPaint* fPaint;
robertphillips4e8e3422014-11-12 06:46:08 -0800620 };
621
622 template <typename T> void trackSaveLayers(const T& op) {
623 /* most ops aren't involved in saveLayers */
624 }
robertphillips478dd722014-12-16 08:25:55 -0800625 void trackSaveLayers(const Save& s) { this->pushSaveLayerInfo(false, NULL, NULL); }
626 void trackSaveLayers(const SaveLayer& sl) { this->pushSaveLayerInfo(true, sl.bounds, sl.paint); }
robertphillips4e8e3422014-11-12 06:46:08 -0800627 void trackSaveLayers(const Restore& r) { this->popSaveLayerInfo(); }
628
reed1bdfd3f2014-11-24 14:41:51 -0800629 void trackSaveLayersForPicture(const SkPicture* picture, const SkPaint* paint) {
robertphillips4e8e3422014-11-12 06:46:08 -0800630 // For sub-pictures, we wrap their layer information within the parent
631 // picture's rendering hierarchy
robertphillips82365912014-11-12 09:32:34 -0800632 SkPicture::AccelData::Key key = SkLayerInfo::ComputeKey();
robertphillips4e8e3422014-11-12 06:46:08 -0800633
robertphillips82365912014-11-12 09:32:34 -0800634 const SkLayerInfo* childData =
reed1bdfd3f2014-11-24 14:41:51 -0800635 static_cast<const SkLayerInfo*>(picture->EXPERIMENTAL_getAccelData(key));
robertphillips4e8e3422014-11-12 06:46:08 -0800636 if (!childData) {
637 // If the child layer hasn't been generated with saveLayer data we
638 // assume the worst (i.e., that it does contain layers which nest
639 // inside existing layers). Layers within sub-pictures that don't
640 // have saveLayer data cannot be hoisted.
641 // TODO: could the analysis data be use to fine tune this?
642 this->updateStackForSaveLayer();
643 return;
644 }
645
robertphillips82365912014-11-12 09:32:34 -0800646 for (int i = 0; i < childData->numBlocks(); ++i) {
647 const SkLayerInfo::BlockInfo& src = childData->block(i);
robertphillips4e8e3422014-11-12 06:46:08 -0800648
reed1bdfd3f2014-11-24 14:41:51 -0800649 FillBounds::Bounds newBound = fFillBounds.adjustAndMap(src.fBounds, paint);
robertphillips74576eb2014-11-12 07:25:02 -0800650 if (newBound.isEmpty()) {
robertphillips4e8e3422014-11-12 06:46:08 -0800651 continue;
652 }
653
654 this->updateStackForSaveLayer();
655
robertphillips82365912014-11-12 09:32:34 -0800656 SkLayerInfo::BlockInfo& dst = fAccelData->addBlock();
robertphillips4e8e3422014-11-12 06:46:08 -0800657
658 // If src.fPicture is NULL the layer is in dp.picture; otherwise
659 // it belongs to a sub-picture.
reed1bdfd3f2014-11-24 14:41:51 -0800660 dst.fPicture = src.fPicture ? src.fPicture : picture;
robertphillips4e8e3422014-11-12 06:46:08 -0800661 dst.fPicture->ref();
robertphillips74576eb2014-11-12 07:25:02 -0800662 dst.fBounds = newBound;
robertphillips478dd722014-12-16 08:25:55 -0800663 dst.fSrcBounds = src.fSrcBounds;
robertphillips4e8e3422014-11-12 06:46:08 -0800664 dst.fLocalMat = src.fLocalMat;
665 dst.fPreMat = src.fPreMat;
666 dst.fPreMat.postConcat(fFillBounds.ctm());
667 if (src.fPaint) {
668 dst.fPaint = SkNEW_ARGS(SkPaint, (*src.fPaint));
669 }
670 dst.fSaveLayerOpID = src.fSaveLayerOpID;
671 dst.fRestoreOpID = src.fRestoreOpID;
672 dst.fHasNestedLayers = src.fHasNestedLayers;
673 dst.fIsNested = fSaveLayersInStack > 0 || src.fIsNested;
robertphillips01d6e5f2014-12-01 09:09:27 -0800674
675 // Store 'saveLayer ops from enclosing picture' + drawPict op + 'ops from sub-picture'
676 dst.fKeySize = fSaveLayerOpStack.count() + src.fKeySize + 1;
robertphillipse99d4992014-12-03 07:33:57 -0800677 dst.fKey = SkNEW_ARRAY(unsigned, dst.fKeySize);
678 memcpy(dst.fKey, fSaveLayerOpStack.begin(), fSaveLayerOpStack.count() * sizeof(unsigned));
robertphillips01d6e5f2014-12-01 09:09:27 -0800679 dst.fKey[fSaveLayerOpStack.count()] = fFillBounds.currentOp();
robertphillipse99d4992014-12-03 07:33:57 -0800680 memcpy(&dst.fKey[fSaveLayerOpStack.count()+1], src.fKey, src.fKeySize * sizeof(unsigned));
robertphillips4e8e3422014-11-12 06:46:08 -0800681 }
682 }
683
reed1bdfd3f2014-11-24 14:41:51 -0800684 void trackSaveLayers(const DrawPicture& dp) {
685 this->trackSaveLayersForPicture(dp.picture, dp.paint);
686 }
687
688 void trackSaveLayers(const DrawDrawable& dp) {
689 SkASSERT(fPictList);
690 SkASSERT(dp.index >= 0 && dp.index < fPictList->count());
691 const SkPaint* paint = NULL; // drawables don't get a side-car paint
692 this->trackSaveLayersForPicture(fPictList->begin()[dp.index], paint);
693 }
694
robertphillips4e8e3422014-11-12 06:46:08 -0800695 // Inform all the saveLayers already on the stack that they now have a
696 // nested saveLayer inside them
697 void updateStackForSaveLayer() {
698 for (int index = fSaveLayerStack.count() - 1; index >= 0; --index) {
699 if (fSaveLayerStack[index].fHasNestedSaveLayer) {
700 break;
701 }
702 fSaveLayerStack[index].fHasNestedSaveLayer = true;
703 if (fSaveLayerStack[index].fIsSaveLayer) {
704 break;
705 }
706 }
707 }
708
robertphillips478dd722014-12-16 08:25:55 -0800709 void pushSaveLayerInfo(bool isSaveLayer, const SkRect* bounds, const SkPaint* paint) {
robertphillips4e8e3422014-11-12 06:46:08 -0800710 if (isSaveLayer) {
711 this->updateStackForSaveLayer();
712 ++fSaveLayersInStack;
robertphillips01d6e5f2014-12-01 09:09:27 -0800713 fSaveLayerOpStack.push(fFillBounds.currentOp());
robertphillips4e8e3422014-11-12 06:46:08 -0800714 }
715
robertphillips478dd722014-12-16 08:25:55 -0800716 fSaveLayerStack.push(SaveLayerInfo(fFillBounds.currentOp(), isSaveLayer, bounds, paint));
robertphillips4e8e3422014-11-12 06:46:08 -0800717 }
718
719 void popSaveLayerInfo() {
720 if (fSaveLayerStack.count() <= 0) {
721 SkASSERT(false);
722 return;
723 }
724
robertphillips01d6e5f2014-12-01 09:09:27 -0800725 SkASSERT(fSaveLayersInStack == fSaveLayerOpStack.count());
726
robertphillips4e8e3422014-11-12 06:46:08 -0800727 SaveLayerInfo sli;
728 fSaveLayerStack.pop(&sli);
729
730 if (!sli.fIsSaveLayer) {
731 return;
732 }
733
734 --fSaveLayersInStack;
735
robertphillips82365912014-11-12 09:32:34 -0800736 SkLayerInfo::BlockInfo& block = fAccelData->addBlock();
robertphillips4e8e3422014-11-12 06:46:08 -0800737
robertphillips82365912014-11-12 09:32:34 -0800738 SkASSERT(NULL == block.fPicture); // This layer is in the top-most picture
robertphillips4e8e3422014-11-12 06:46:08 -0800739
robertphillips82365912014-11-12 09:32:34 -0800740 block.fBounds = fFillBounds.getBounds(sli.fStartIndex);
741 block.fLocalMat = fFillBounds.ctm();
742 block.fPreMat = SkMatrix::I();
robertphillips4e8e3422014-11-12 06:46:08 -0800743 if (sli.fPaint) {
robertphillips82365912014-11-12 09:32:34 -0800744 block.fPaint = SkNEW_ARGS(SkPaint, (*sli.fPaint));
robertphillips4e8e3422014-11-12 06:46:08 -0800745 }
robertphillips478dd722014-12-16 08:25:55 -0800746
747 block.fSrcBounds = sli.fBounds;
robertphillips82365912014-11-12 09:32:34 -0800748 block.fSaveLayerOpID = sli.fStartIndex;
749 block.fRestoreOpID = fFillBounds.currentOp();
750 block.fHasNestedLayers = sli.fHasNestedSaveLayer;
751 block.fIsNested = fSaveLayersInStack > 0;
robertphillips01d6e5f2014-12-01 09:09:27 -0800752
753 block.fKeySize = fSaveLayerOpStack.count();
robertphillipse99d4992014-12-03 07:33:57 -0800754 block.fKey = SkNEW_ARRAY(unsigned, block.fKeySize);
755 memcpy(block.fKey, fSaveLayerOpStack.begin(), block.fKeySize * sizeof(unsigned));
robertphillips01d6e5f2014-12-01 09:09:27 -0800756
757 fSaveLayerOpStack.pop();
robertphillips4e8e3422014-11-12 06:46:08 -0800758 }
759
760 // Used to collect saveLayer information for layer hoisting
robertphillips01d6e5f2014-12-01 09:09:27 -0800761 int fSaveLayersInStack;
robertphillips4e8e3422014-11-12 06:46:08 -0800762 SkTDArray<SaveLayerInfo> fSaveLayerStack;
robertphillips01d6e5f2014-12-01 09:09:27 -0800763 // The op code indices of all the currently active saveLayers
robertphillipse99d4992014-12-03 07:33:57 -0800764 SkTDArray<unsigned> fSaveLayerOpStack;
robertphillips01d6e5f2014-12-01 09:09:27 -0800765 SkLayerInfo* fAccelData;
reed1bdfd3f2014-11-24 14:41:51 -0800766 const SkPicture::SnapshotArray* fPictList;
robertphillips4e8e3422014-11-12 06:46:08 -0800767
768 SkRecords::FillBounds fFillBounds;
769};
robertphillips4e8e3422014-11-12 06:46:08 -0800770
commit-bot@chromium.org27f6b0d2014-05-09 14:59:29 +0000771} // namespace SkRecords
mtklein5ad6ee12014-08-11 08:08:43 -0700772
robertphillips4d52afe2014-11-03 08:19:44 -0800773void SkRecordFillBounds(const SkRect& cullRect, const SkRecord& record, SkBBoxHierarchy* bbh) {
robertphillips4e8e3422014-11-12 06:46:08 -0800774 SkRecords::FillBounds visitor(cullRect, record);
775
776 for (unsigned curOp = 0; curOp < record.count(); curOp++) {
777 visitor.setCurrentOp(curOp);
778 record.visit<void>(curOp, visitor);
779 }
780
781 visitor.cleanUp(bbh);
mtklein5ad6ee12014-08-11 08:08:43 -0700782}
robertphillips4e8e3422014-11-12 06:46:08 -0800783
robertphillips4e8e3422014-11-12 06:46:08 -0800784void SkRecordComputeLayers(const SkRect& cullRect, const SkRecord& record,
reed1bdfd3f2014-11-24 14:41:51 -0800785 const SkPicture::SnapshotArray* pictList, SkBBoxHierarchy* bbh,
786 SkLayerInfo* data) {
787 SkRecords::CollectLayers visitor(cullRect, record, pictList, data);
robertphillips4e8e3422014-11-12 06:46:08 -0800788
789 for (unsigned curOp = 0; curOp < record.count(); curOp++) {
790 visitor.setCurrentOp(curOp);
791 record.visit<void>(curOp, visitor);
792 }
793
794 visitor.cleanUp(bbh);
795}
robertphillips4e8e3422014-11-12 06:46:08 -0800796