blob: 6a2a5e3677b4395b980f6e0b23b110ddafc5fd89 [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[],
15 SkCanvasDrawable* const drawables[],
16 int drawableCount,
mtklein5ad6ee12014-08-11 08:08:43 -070017 const SkBBoxHierarchy* bbh,
18 SkDrawPictureCallback* callback) {
Mike Kleinc11530e2014-06-24 11:29:06 -040019 SkAutoCanvasRestore saveRestore(canvas, true /*save now, restore at exit*/);
mtklein5ad6ee12014-08-11 08:08:43 -070020
bsalomon49f085d2014-09-05 13:34:00 -070021 if (bbh) {
mtklein5ad6ee12014-08-11 08:08:43 -070022 // Draw only ops that affect pixels in the canvas's current clip.
mtklein3e8232b2014-08-18 13:39:11 -070023 // The SkRecord and BBH were recorded in identity space. This canvas
24 // is not necessarily in that same space. getClipBounds() returns us
25 // this canvas' clip bounds transformed back into identity space, which
26 // lets us query the BBH.
mtklein7cc1a342014-11-20 08:01:09 -080027 SkRect query;
28 if (!canvas->getClipBounds(&query)) {
29 // We want to make sure our query rectangle is never totally empty.
30 // Clear ignores the clip, so it must draw even if the clip is logically empty.
31 query = SkRect::MakeWH(SK_ScalarNearlyZero, SK_ScalarNearlyZero);
32 }
mtklein3e8232b2014-08-18 13:39:11 -070033
mtklein6bd41962014-10-02 07:41:56 -070034 SkTDArray<unsigned> ops;
mtkleina723b572014-08-15 11:49:49 -070035 bbh->search(query, &ops);
mtklein5ad6ee12014-08-11 08:08:43 -070036
reed1bdfd3f2014-11-24 14:41:51 -080037 SkRecords::Draw draw(canvas, drawablePicts, drawables, drawableCount);
mtklein5ad6ee12014-08-11 08:08:43 -070038 for (int i = 0; i < ops.count(); i++) {
bsalomon49f085d2014-09-05 13:34:00 -070039 if (callback && callback->abortDrawing()) {
mtklein5ad6ee12014-08-11 08:08:43 -070040 return;
41 }
danakjd239d422014-11-03 12:43:30 -080042 // This visit call uses the SkRecords::Draw::operator() to call
43 // methods on the |canvas|, wrapped by methods defined with the
44 // DRAW() macro.
mtklein6bd41962014-10-02 07:41:56 -070045 record.visit<void>(ops[i], draw);
mtklein5ad6ee12014-08-11 08:08:43 -070046 }
47 } else {
48 // Draw all ops.
reed1bdfd3f2014-11-24 14:41:51 -080049 SkRecords::Draw draw(canvas, drawablePicts, drawables, drawableCount);
mtklein00f30bd2014-09-02 12:03:31 -070050 for (unsigned i = 0; i < record.count(); i++) {
bsalomon49f085d2014-09-05 13:34:00 -070051 if (callback && callback->abortDrawing()) {
mtklein5ad6ee12014-08-11 08:08:43 -070052 return;
53 }
danakjd239d422014-11-03 12:43:30 -080054 // This visit call uses the SkRecords::Draw::operator() to call
55 // methods on the |canvas|, wrapped by methods defined with the
56 // DRAW() macro.
mtklein00f30bd2014-09-02 12:03:31 -070057 record.visit<void>(i, draw);
mtklein5ad6ee12014-08-11 08:08:43 -070058 }
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000059 }
commit-bot@chromium.org27f6b0d2014-05-09 14:59:29 +000060}
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000061
reed6be2aa92014-11-18 11:08:05 -080062void SkRecordPartialDraw(const SkRecord& record, SkCanvas* canvas,
63 SkPicture const* const drawablePicts[], int drawableCount,
robertphillips4815fe52014-09-16 10:32:43 -070064 unsigned start, unsigned stop,
65 const SkMatrix& initialCTM) {
mtklein00f30bd2014-09-02 12:03:31 -070066 SkAutoCanvasRestore saveRestore(canvas, true /*save now, restore at exit*/);
67
68 stop = SkTMin(stop, record.count());
reed8eddfb52014-12-04 07:50:14 -080069 SkRecords::Draw draw(canvas, drawablePicts, NULL, drawableCount, &initialCTM);
mtklein00f30bd2014-09-02 12:03:31 -070070 for (unsigned i = start; i < stop; i++) {
71 record.visit<void>(i, draw);
72 }
73}
74
commit-bot@chromium.org27f6b0d2014-05-09 14:59:29 +000075namespace SkRecords {
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000076
commit-bot@chromium.org2e0c32a2014-04-28 16:19:45 +000077// NoOps draw nothing.
commit-bot@chromium.org27f6b0d2014-05-09 14:59:29 +000078template <> void Draw::draw(const NoOp&) {}
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000079
commit-bot@chromium.org27f6b0d2014-05-09 14:59:29 +000080#define DRAW(T, call) template <> void Draw::draw(const T& r) { fCanvas->call; }
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000081DRAW(Restore, restore());
Florin Malita5f6102d2014-06-30 10:13:28 -040082DRAW(Save, save());
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000083DRAW(SaveLayer, saveLayer(r.bounds, r.paint, r.flags));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000084DRAW(Clear, clear(r.color));
commit-bot@chromium.org99bd7d82014-05-19 15:51:12 +000085DRAW(SetMatrix, setMatrix(SkMatrix::Concat(fInitialCTM, r.matrix)));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000086
mtkleincdeeb092014-11-20 09:14:28 -080087DRAW(ClipPath, clipPath(r.path, r.opAA.op, r.opAA.aa));
88DRAW(ClipRRect, clipRRect(r.rrect, r.opAA.op, r.opAA.aa));
89DRAW(ClipRect, clipRect(r.rect, r.opAA.op, r.opAA.aa));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000090DRAW(ClipRegion, clipRegion(r.region, r.op));
91
mtklein5f0e8222014-08-22 11:44:26 -070092DRAW(BeginCommentGroup, beginCommentGroup(r.description));
93DRAW(AddComment, addComment(r.key, r.value));
94DRAW(EndCommentGroup, endCommentGroup());
95
mtkleinf55c3142014-12-11 12:43:04 -080096DRAW(DrawBitmap, drawBitmap(r.bitmap.shallowCopy(), r.left, r.top, r.paint));
97DRAW(DrawBitmapNine, drawBitmapNine(r.bitmap.shallowCopy(), r.center, r.dst, r.paint));
mtklein7cdc1ee2014-07-07 10:41:04 -070098DRAW(DrawBitmapRectToRect,
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::kNone_DrawBitmapRectFlag));
101DRAW(DrawBitmapRectToRectBleed,
mtkleinf55c3142014-12-11 12:43:04 -0800102 drawBitmapRectToRect(r.bitmap.shallowCopy(), r.src, r.dst, r.paint,
mtklein42ddcd42014-11-21 08:48:35 -0800103 SkCanvas::kBleed_DrawBitmapRectFlag));
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));
mtkleinaf579032014-12-01 11:03:37 -0800111DRAW(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));
mtkleinf55c3142014-12-11 12:43:04 -0800117DRAW(DrawSprite, drawSprite(r.bitmap.shallowCopy(), 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));
mtkleinaf579032014-12-01 11:03:37 -0800120DRAW(DrawTextOnPath, drawTextOnPath(r.text, r.byteLength, r.path, &r.matrix, r.paint));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +0000121DRAW(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
reed6be2aa92014-11-18 11:08:05 -0800126template <> void Draw::draw(const DrawDrawable& r) {
127 SkASSERT(r.index >= 0);
128 SkASSERT(r.index < fDrawableCount);
reed1bdfd3f2014-11-24 14:41:51 -0800129 if (fDrawables) {
130 SkASSERT(NULL == fDrawablePicts);
131 fCanvas->EXPERIMENTAL_drawDrawable(fDrawables[r.index]);
132 } else {
133 fCanvas->drawPicture(fDrawablePicts[r.index]);
134 }
reed6be2aa92014-11-18 11:08:05 -0800135}
136
mtklein5ad6ee12014-08-11 08:08:43 -0700137// This is an SkRecord visitor that fills an SkBBoxHierarchy.
mtklein828ce1f2014-08-13 12:58:45 -0700138//
139// The interesting part here is how to calculate bounds for ops which don't
140// have intrinsic bounds. What is the bounds of a Save or a Translate?
141//
142// We answer this by thinking about a particular definition of bounds: if I
143// don't execute this op, pixels in this rectangle might draw incorrectly. So
144// the bounds of a Save, a Translate, a Restore, etc. are the union of the
145// bounds of Draw* ops that they might have an effect on. For any given
146// Save/Restore block, the bounds of the Save, the Restore, and any other
147// non-drawing ("control") ops inside are exactly the union of the bounds of
148// the drawing ops inside that block.
149//
150// To implement this, we keep a stack of active Save blocks. As we consume ops
151// inside the Save/Restore block, drawing ops are unioned with the bounds of
152// the block, and control ops are stashed away for later. When we finish the
153// block with a Restore, our bounds are complete, and we go back and fill them
154// in for all the control ops we stashed away.
mtklein5ad6ee12014-08-11 08:08:43 -0700155class FillBounds : SkNoncopyable {
156public:
robertphillips4e8e3422014-11-12 06:46:08 -0800157 FillBounds(const SkRect& cullRect, const SkRecord& record)
158 : fNumRecords(record.count())
159 , fCullRect(cullRect)
reed1bdfd3f2014-11-24 14:41:51 -0800160 , fBounds(record.count())
161 {
mtklein828ce1f2014-08-13 12:58:45 -0700162 // Calculate bounds for all ops. This won't go quite in order, so we'll need
163 // to store the bounds separately then feed them in to the BBH later in order.
mtklein6332f1d2014-08-19 07:09:40 -0700164 fCTM = &SkMatrix::I();
robertphillips4d52afe2014-11-03 08:19:44 -0800165 fCurrentClipBounds = fCullRect;
robertphillips4e8e3422014-11-12 06:46:08 -0800166 }
mtklein5ad6ee12014-08-11 08:08:43 -0700167
robertphillips4e8e3422014-11-12 06:46:08 -0800168 void setCurrentOp(unsigned currentOp) { fCurrentOp = currentOp; }
169
170 void cleanUp(SkBBoxHierarchy* bbh) {
mtklein828ce1f2014-08-13 12:58:45 -0700171 // If we have any lingering unpaired Saves, simulate restores to make
172 // sure all ops in those Save blocks have their bounds calculated.
173 while (!fSaveStack.isEmpty()) {
174 this->popSaveBlock();
175 }
176
177 // Any control ops not part of any Save/Restore block draw everywhere.
178 while (!fControlIndices.isEmpty()) {
robertphillips4d52afe2014-11-03 08:19:44 -0800179 this->popControl(fCullRect);
mtklein828ce1f2014-08-13 12:58:45 -0700180 }
181
182 // Finally feed all stored bounds into the BBH. They'll be returned in this order.
robertphillips89108792014-11-17 08:16:15 -0800183 if (bbh) {
184 bbh->insert(&fBounds, fNumRecords);
185 }
mtklein828ce1f2014-08-13 12:58:45 -0700186 }
mtklein5ad6ee12014-08-11 08:08:43 -0700187
mtkleina723b572014-08-15 11:49:49 -0700188 template <typename T> void operator()(const T& op) {
189 this->updateCTM(op);
190 this->updateClipBounds(op);
191 this->trackBounds(op);
mtklein5ad6ee12014-08-11 08:08:43 -0700192 }
193
mtklein533eb782014-08-27 10:39:42 -0700194 // In this file, SkRect are in local coordinates, Bounds are translated back to identity space.
195 typedef SkRect Bounds;
196
robertphillips4e8e3422014-11-12 06:46:08 -0800197 unsigned currentOp() const { return fCurrentOp; }
198 const SkMatrix& ctm() const { return *fCTM; }
robertphillips4e8e3422014-11-12 06:46:08 -0800199 const Bounds& getBounds(unsigned index) const { return fBounds[index]; }
200
201 // Adjust rect for all paints that may affect its geometry, then map it to identity space.
202 Bounds adjustAndMap(SkRect rect, const SkPaint* paint) const {
203 // Inverted rectangles really confuse our BBHs.
204 rect.sort();
205
206 // Adjust the rect for its own paint.
207 if (!AdjustForPaint(paint, &rect)) {
208 // The paint could do anything to our bounds. The only safe answer is the current clip.
209 return fCurrentClipBounds;
210 }
211
212 // Adjust rect for all the paints from the SaveLayers we're inside.
213 if (!this->adjustForSaveLayerPaints(&rect)) {
214 // Same deal as above.
215 return fCurrentClipBounds;
216 }
217
218 // Map the rect back to identity space.
219 fCTM->mapRect(&rect);
220
221 // Nothing can draw outside the current clip.
222 // (Only bounded ops call into this method, so oddballs like Clear don't matter here.)
223 rect.intersect(fCurrentClipBounds);
224 return rect;
225 }
226
227private:
mtklein828ce1f2014-08-13 12:58:45 -0700228 struct SaveBounds {
mtkleina723b572014-08-15 11:49:49 -0700229 int controlOps; // Number of control ops in this Save block, including the Save.
mtklein533eb782014-08-27 10:39:42 -0700230 Bounds bounds; // Bounds of everything in the block.
mtkleina723b572014-08-15 11:49:49 -0700231 const SkPaint* paint; // Unowned. If set, adjusts the bounds of all ops in this block.
mtklein828ce1f2014-08-13 12:58:45 -0700232 };
233
mtklein8e393bf2014-10-01 12:48:58 -0700234 // Only Restore and SetMatrix change the CTM.
235 template <typename T> void updateCTM(const T&) {}
mtklein6332f1d2014-08-19 07:09:40 -0700236 void updateCTM(const Restore& op) { fCTM = &op.matrix; }
237 void updateCTM(const SetMatrix& op) { fCTM = &op.matrix; }
mtkleina723b572014-08-15 11:49:49 -0700238
mtklein8e393bf2014-10-01 12:48:58 -0700239 // Most ops don't change the clip.
240 template <typename T> void updateClipBounds(const T&) {}
Mike Klein271a0302014-09-23 15:28:38 -0400241
mtklein8e393bf2014-10-01 12:48:58 -0700242 // Clip{Path,RRect,Rect,Region} obviously change the clip. They all know their bounds already.
243 void updateClipBounds(const ClipPath& op) { this->updateClipBoundsForClipOp(op.devBounds); }
244 void updateClipBounds(const ClipRRect& op) { this->updateClipBoundsForClipOp(op.devBounds); }
245 void updateClipBounds(const ClipRect& op) { this->updateClipBoundsForClipOp(op.devBounds); }
246 void updateClipBounds(const ClipRegion& op) { this->updateClipBoundsForClipOp(op.devBounds); }
Mike Klein271a0302014-09-23 15:28:38 -0400247
mtklein8e393bf2014-10-01 12:48:58 -0700248 // The bounds of clip ops need to be adjusted for the paints of saveLayers they're inside.
249 void updateClipBoundsForClipOp(const SkIRect& devBounds) {
250 Bounds clip = SkRect::Make(devBounds);
Mike Klein271a0302014-09-23 15:28:38 -0400251 // We don't call adjustAndMap() because as its last step it would intersect the adjusted
252 // clip bounds with the previous clip, exactly what we can't do when the clip grows.
robertphillips4d52afe2014-11-03 08:19:44 -0800253 fCurrentClipBounds = this->adjustForSaveLayerPaints(&clip) ? clip : fCullRect;
Mike Klein271a0302014-09-23 15:28:38 -0400254 }
255
mtklein8e393bf2014-10-01 12:48:58 -0700256 // Restore holds the devBounds for the clip after the {save,saveLayer}/restore block completes.
257 void updateClipBounds(const Restore& op) {
258 // This is just like the clip ops above, but we need to skip the effects (if any) of our
259 // paired saveLayer (if it is one); it has not yet been popped off the save stack. Our
260 // devBounds reflect the state of the world after the saveLayer/restore block is done,
261 // so they are not affected by the saveLayer's paint.
262 const int kSavesToIgnore = 1;
263 Bounds clip = SkRect::Make(op.devBounds);
264 fCurrentClipBounds =
robertphillips4d52afe2014-11-03 08:19:44 -0800265 this->adjustForSaveLayerPaints(&clip, kSavesToIgnore) ? clip : fCullRect;
mtklein8e393bf2014-10-01 12:48:58 -0700266 }
267
Mike Klein271a0302014-09-23 15:28:38 -0400268 // We also take advantage of SaveLayer bounds when present to further cut the clip down.
mtkleina723b572014-08-15 11:49:49 -0700269 void updateClipBounds(const SaveLayer& op) {
270 if (op.bounds) {
Mike Klein271a0302014-09-23 15:28:38 -0400271 // adjustAndMap() intersects these layer bounds with the previous clip for us.
272 fCurrentClipBounds = this->adjustAndMap(*op.bounds, op.paint);
mtkleina723b572014-08-15 11:49:49 -0700273 }
274 }
mtklein6cfa73a2014-08-13 13:33:49 -0700275
mtklein828ce1f2014-08-13 12:58:45 -0700276 // The bounds of these ops must be calculated when we hit the Restore
277 // from the bounds of the ops in the same Save block.
mtkleina723b572014-08-15 11:49:49 -0700278 void trackBounds(const Save&) { this->pushSaveBlock(NULL); }
mtkleina723b572014-08-15 11:49:49 -0700279 void trackBounds(const SaveLayer& op) { this->pushSaveBlock(op.paint); }
280 void trackBounds(const Restore&) { fBounds[fCurrentOp] = this->popSaveBlock(); }
mtklein828ce1f2014-08-13 12:58:45 -0700281
mtklein68199a22014-08-25 13:49:29 -0700282 void trackBounds(const SetMatrix&) { this->pushControl(); }
283 void trackBounds(const ClipRect&) { this->pushControl(); }
284 void trackBounds(const ClipRRect&) { this->pushControl(); }
285 void trackBounds(const ClipPath&) { this->pushControl(); }
286 void trackBounds(const ClipRegion&) { this->pushControl(); }
mtklein68199a22014-08-25 13:49:29 -0700287 void trackBounds(const BeginCommentGroup&) { this->pushControl(); }
288 void trackBounds(const AddComment&) { this->pushControl(); }
289 void trackBounds(const EndCommentGroup&) { this->pushControl(); }
mtklein29dfaa82014-09-04 14:12:44 -0700290 void trackBounds(const DrawData&) { this->pushControl(); }
mtklein828ce1f2014-08-13 12:58:45 -0700291
292 // For all other ops, we can calculate and store the bounds directly now.
293 template <typename T> void trackBounds(const T& op) {
294 fBounds[fCurrentOp] = this->bounds(op);
295 this->updateSaveBounds(fBounds[fCurrentOp]);
mtklein5ad6ee12014-08-11 08:08:43 -0700296 }
297
mtkleina723b572014-08-15 11:49:49 -0700298 void pushSaveBlock(const SkPaint* paint) {
mtklein828ce1f2014-08-13 12:58:45 -0700299 // Starting a new Save block. Push a new entry to represent that.
robertphillips4d52afe2014-11-03 08:19:44 -0800300 SaveBounds sb;
301 sb.controlOps = 0;
302 // If the paint affects transparent black, the bound shouldn't be smaller
303 // than the current clip bounds.
304 sb.bounds =
305 PaintMayAffectTransparentBlack(paint) ? fCurrentClipBounds : Bounds::MakeEmpty();
306 sb.paint = paint;
307
mtklein828ce1f2014-08-13 12:58:45 -0700308 fSaveStack.push(sb);
309 this->pushControl();
310 }
311
mtkleind910f542014-08-22 09:06:34 -0700312 static bool PaintMayAffectTransparentBlack(const SkPaint* paint) {
dneto327f9052014-09-15 10:53:16 -0700313 if (paint) {
314 // FIXME: this is very conservative
315 if (paint->getImageFilter() || paint->getColorFilter()) {
316 return true;
317 }
318
319 // Unusual Xfermodes require us to process a saved layer
320 // even with operations outisde the clip.
321 // For example, DstIn is used by masking layers.
322 // https://code.google.com/p/skia/issues/detail?id=1291
323 // https://crbug.com/401593
324 SkXfermode* xfermode = paint->getXfermode();
325 SkXfermode::Mode mode;
326 // SrcOver is ok, and is also the common case with a NULL xfermode.
327 // So we should make that the fast path and bypass the mode extraction
328 // and test.
329 if (xfermode && xfermode->asMode(&mode)) {
330 switch (mode) {
331 // For each of the following transfer modes, if the source
332 // alpha is zero (our transparent black), the resulting
333 // blended alpha is not necessarily equal to the original
334 // destination alpha.
335 case SkXfermode::kClear_Mode:
336 case SkXfermode::kSrc_Mode:
337 case SkXfermode::kSrcIn_Mode:
338 case SkXfermode::kDstIn_Mode:
339 case SkXfermode::kSrcOut_Mode:
340 case SkXfermode::kDstATop_Mode:
341 case SkXfermode::kModulate_Mode:
342 return true;
343 break;
344 default:
345 break;
346 }
347 }
348 }
349 return false;
mtkleind910f542014-08-22 09:06:34 -0700350 }
351
mtklein533eb782014-08-27 10:39:42 -0700352 Bounds popSaveBlock() {
mtklein828ce1f2014-08-13 12:58:45 -0700353 // We're done the Save block. Apply the block's bounds to all control ops inside it.
354 SaveBounds sb;
355 fSaveStack.pop(&sb);
mtkleind910f542014-08-22 09:06:34 -0700356
mtklein828ce1f2014-08-13 12:58:45 -0700357 while (sb.controlOps --> 0) {
robertphillips4d52afe2014-11-03 08:19:44 -0800358 this->popControl(sb.bounds);
mtklein828ce1f2014-08-13 12:58:45 -0700359 }
360
361 // This whole Save block may be part another Save block.
robertphillips4d52afe2014-11-03 08:19:44 -0800362 this->updateSaveBounds(sb.bounds);
mtklein828ce1f2014-08-13 12:58:45 -0700363
364 // If called from a real Restore (not a phony one for balance), it'll need the bounds.
robertphillips4d52afe2014-11-03 08:19:44 -0800365 return sb.bounds;
mtklein828ce1f2014-08-13 12:58:45 -0700366 }
367
368 void pushControl() {
369 fControlIndices.push(fCurrentOp);
370 if (!fSaveStack.isEmpty()) {
371 fSaveStack.top().controlOps++;
372 }
373 }
374
mtklein533eb782014-08-27 10:39:42 -0700375 void popControl(const Bounds& bounds) {
mtklein828ce1f2014-08-13 12:58:45 -0700376 fBounds[fControlIndices.top()] = bounds;
377 fControlIndices.pop();
378 }
379
mtklein533eb782014-08-27 10:39:42 -0700380 void updateSaveBounds(const Bounds& bounds) {
mtklein828ce1f2014-08-13 12:58:45 -0700381 // If we're in a Save block, expand its bounds to cover these bounds too.
382 if (!fSaveStack.isEmpty()) {
383 fSaveStack.top().bounds.join(bounds);
384 }
385 }
386
mtklein131a22b2014-08-25 14:16:15 -0700387 // FIXME: this method could use better bounds
mtklein533eb782014-08-27 10:39:42 -0700388 Bounds bounds(const DrawText&) const { return fCurrentClipBounds; }
mtklein68199a22014-08-25 13:49:29 -0700389
robertphillips4d52afe2014-11-03 08:19:44 -0800390 Bounds bounds(const Clear&) const { return fCullRect; } // Ignores the clip.
mtklein533eb782014-08-27 10:39:42 -0700391 Bounds bounds(const DrawPaint&) const { return fCurrentClipBounds; }
392 Bounds bounds(const NoOp&) const { return Bounds::MakeEmpty(); } // NoOps don't draw.
mtklein828ce1f2014-08-13 12:58:45 -0700393
mtkleinaf579032014-12-01 11:03:37 -0800394 Bounds bounds(const DrawSprite& op) const { // Ignores the matrix.
395 return Bounds::MakeXYWH(op.left, op.top, op.bitmap.width(), op.bitmap.height());
mtklein131a22b2014-08-25 14:16:15 -0700396 }
397
mtklein533eb782014-08-27 10:39:42 -0700398 Bounds bounds(const DrawRect& op) const { return this->adjustAndMap(op.rect, &op.paint); }
399 Bounds bounds(const DrawOval& op) const { return this->adjustAndMap(op.oval, &op.paint); }
400 Bounds bounds(const DrawRRect& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700401 return this->adjustAndMap(op.rrect.rect(), &op.paint);
402 }
mtklein533eb782014-08-27 10:39:42 -0700403 Bounds bounds(const DrawDRRect& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700404 return this->adjustAndMap(op.outer.rect(), &op.paint);
405 }
piotaixr65151752014-10-16 11:58:39 -0700406 Bounds bounds(const DrawImage& op) const {
407 const SkImage* image = op.image;
408 SkRect rect = SkRect::MakeXYWH(op.left, op.top, image->width(), image->height());
mtklein62b67ae2014-08-18 11:10:37 -0700409
piotaixr65151752014-10-16 11:58:39 -0700410 return this->adjustAndMap(rect, op.paint);
411 }
412 Bounds bounds(const DrawImageRect& op) const {
413 return this->adjustAndMap(op.dst, op.paint);
414 }
mtklein533eb782014-08-27 10:39:42 -0700415 Bounds bounds(const DrawBitmapRectToRect& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700416 return this->adjustAndMap(op.dst, op.paint);
417 }
mtklein42ddcd42014-11-21 08:48:35 -0800418 Bounds bounds(const DrawBitmapRectToRectBleed& op) const {
419 return this->adjustAndMap(op.dst, op.paint);
420 }
mtklein533eb782014-08-27 10:39:42 -0700421 Bounds bounds(const DrawBitmapNine& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700422 return this->adjustAndMap(op.dst, op.paint);
423 }
mtklein533eb782014-08-27 10:39:42 -0700424 Bounds bounds(const DrawBitmap& op) const {
mtkleinaf579032014-12-01 11:03:37 -0800425 return this->adjustAndMap(
426 SkRect::MakeXYWH(op.left, op.top, op.bitmap.width(), op.bitmap.height()),
427 op.paint);
mtklein62b67ae2014-08-18 11:10:37 -0700428 }
mtklein62b67ae2014-08-18 11:10:37 -0700429
mtklein533eb782014-08-27 10:39:42 -0700430 Bounds bounds(const DrawPath& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700431 return op.path.isInverseFillType() ? fCurrentClipBounds
432 : this->adjustAndMap(op.path.getBounds(), &op.paint);
433 }
mtklein533eb782014-08-27 10:39:42 -0700434 Bounds bounds(const DrawPoints& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700435 SkRect dst;
436 dst.set(op.pts, op.count);
437
438 // Pad the bounding box a little to make sure hairline points' bounds aren't empty.
439 SkScalar stroke = SkMaxScalar(op.paint.getStrokeWidth(), 0.01f);
440 dst.outset(stroke/2, stroke/2);
441
442 return this->adjustAndMap(dst, &op.paint);
443 }
mtklein533eb782014-08-27 10:39:42 -0700444 Bounds bounds(const DrawPatch& op) const {
mtklein131a22b2014-08-25 14:16:15 -0700445 SkRect dst;
446 dst.set(op.cubics, SkPatchUtils::kNumCtrlPts);
447 return this->adjustAndMap(dst, &op.paint);
448 }
mtklein533eb782014-08-27 10:39:42 -0700449 Bounds bounds(const DrawVertices& op) const {
mtklein131a22b2014-08-25 14:16:15 -0700450 SkRect dst;
451 dst.set(op.vertices, op.vertexCount);
452 return this->adjustAndMap(dst, &op.paint);
453 }
454
mtklein533eb782014-08-27 10:39:42 -0700455 Bounds bounds(const DrawPicture& op) const {
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700456 SkRect dst = op.picture->cullRect();
mtkleinaf579032014-12-01 11:03:37 -0800457 op.matrix.mapRect(&dst);
mtklein131a22b2014-08-25 14:16:15 -0700458 return this->adjustAndMap(dst, op.paint);
459 }
mtklein62b67ae2014-08-18 11:10:37 -0700460
mtklein533eb782014-08-27 10:39:42 -0700461 Bounds bounds(const DrawPosText& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700462 const int N = op.paint.countText(op.text, op.byteLength);
463 if (N == 0) {
mtklein533eb782014-08-27 10:39:42 -0700464 return Bounds::MakeEmpty();
mtklein62b67ae2014-08-18 11:10:37 -0700465 }
466
467 SkRect dst;
mtklein937c9c72014-09-02 15:19:48 -0700468 dst.set(op.pos, N);
mtklein62b67ae2014-08-18 11:10:37 -0700469 AdjustTextForFontMetrics(&dst, op.paint);
470 return this->adjustAndMap(dst, &op.paint);
471 }
mtklein533eb782014-08-27 10:39:42 -0700472 Bounds bounds(const DrawPosTextH& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700473 const int N = op.paint.countText(op.text, op.byteLength);
474 if (N == 0) {
mtklein533eb782014-08-27 10:39:42 -0700475 return Bounds::MakeEmpty();
mtklein62b67ae2014-08-18 11:10:37 -0700476 }
477
478 SkScalar left = op.xpos[0], right = op.xpos[0];
479 for (int i = 1; i < N; i++) {
480 left = SkMinScalar(left, op.xpos[i]);
481 right = SkMaxScalar(right, op.xpos[i]);
482 }
483 SkRect dst = { left, op.y, right, op.y };
484 AdjustTextForFontMetrics(&dst, op.paint);
485 return this->adjustAndMap(dst, &op.paint);
486 }
mtklein533eb782014-08-27 10:39:42 -0700487 Bounds bounds(const DrawTextOnPath& op) const {
mtklein131a22b2014-08-25 14:16:15 -0700488 SkRect dst = op.path.getBounds();
489
mtklein9a5380d2014-12-16 06:31:01 -0800490 // Pad all sides by the maximum padding in any direction we'd normally apply.
mtklein131a22b2014-08-25 14:16:15 -0700491 SkRect pad = { 0, 0, 0, 0};
492 AdjustTextForFontMetrics(&pad, op.paint);
mtklein9a5380d2014-12-16 06:31:01 -0800493
494 // That maximum padding happens to always be the right pad today.
495 SkASSERT(pad.fLeft == -pad.fRight);
496 SkASSERT(pad.fTop == -pad.fBottom);
497 SkASSERT(pad.fRight > pad.fBottom);
498 dst.outset(pad.fRight, pad.fRight);
499
mtklein131a22b2014-08-25 14:16:15 -0700500 return this->adjustAndMap(dst, &op.paint);
501 }
502
mtklein533eb782014-08-27 10:39:42 -0700503 Bounds bounds(const DrawTextBlob& op) const {
mtklein131a22b2014-08-25 14:16:15 -0700504 SkRect dst = op.blob->bounds();
505 dst.offset(op.x, op.y);
mtklein131a22b2014-08-25 14:16:15 -0700506 return this->adjustAndMap(dst, &op.paint);
507 }
mtklein62b67ae2014-08-18 11:10:37 -0700508
reed6be2aa92014-11-18 11:08:05 -0800509 Bounds bounds(const DrawDrawable& op) const {
510 return this->adjustAndMap(op.worstCaseBounds, NULL);
511 }
512
mtklein62b67ae2014-08-18 11:10:37 -0700513 static void AdjustTextForFontMetrics(SkRect* rect, const SkPaint& paint) {
mtklein9a5380d2014-12-16 06:31:01 -0800514#ifdef SK_DEBUG
515 SkRect correct = *rect;
516#endif
517 // crbug.com/373785 ~~> xPad = 4x yPad
518 // crbug.com/424824 ~~> bump yPad from 2x text size to 2.5x
519 const SkScalar yPad = 2.5f * paint.getTextSize(),
520 xPad = 4.0f * yPad;
521 rect->outset(xPad, yPad);
caryclark9a657fa2014-08-20 05:24:29 -0700522#ifdef SK_DEBUG
mtklein62b67ae2014-08-18 11:10:37 -0700523 SkPaint::FontMetrics metrics;
524 paint.getFontMetrics(&metrics);
mtklein9a5380d2014-12-16 06:31:01 -0800525 correct.fLeft += metrics.fXMin;
526 correct.fTop += metrics.fTop;
527 correct.fRight += metrics.fXMax;
528 correct.fBottom += metrics.fBottom;
mtkleind13291a2014-08-21 14:46:49 -0700529 // See skia:2862 for why we ignore small text sizes.
mtklein9a5380d2014-12-16 06:31:01 -0800530 SkASSERTF(paint.getTextSize() < 0.001f || rect->contains(correct),
mtkleined167ac2014-10-29 16:07:10 -0700531 "%f %f %f %f vs. %f %f %f %f\n",
mtklein9a5380d2014-12-16 06:31:01 -0800532 -xPad, -yPad, +xPad, +yPad,
mtkleined167ac2014-10-29 16:07:10 -0700533 metrics.fXMin, metrics.fTop, metrics.fXMax, metrics.fBottom);
mtkleina19afb42014-08-19 17:47:14 -0700534#endif
mtklein62b67ae2014-08-18 11:10:37 -0700535 }
536
mtklein479601b2014-08-18 08:45:33 -0700537 // Returns true if rect was meaningfully adjusted for the effects of paint,
538 // false if the paint could affect the rect in unknown ways.
539 static bool AdjustForPaint(const SkPaint* paint, SkRect* rect) {
mtkleina723b572014-08-15 11:49:49 -0700540 if (paint) {
541 if (paint->canComputeFastBounds()) {
mtklein479601b2014-08-18 08:45:33 -0700542 *rect = paint->computeFastBounds(*rect, rect);
543 return true;
mtkleina723b572014-08-15 11:49:49 -0700544 }
mtklein479601b2014-08-18 08:45:33 -0700545 return false;
546 }
547 return true;
548 }
549
mtklein8e393bf2014-10-01 12:48:58 -0700550 bool adjustForSaveLayerPaints(SkRect* rect, int savesToIgnore = 0) const {
551 for (int i = fSaveStack.count() - 1 - savesToIgnore; i >= 0; i--) {
Mike Klein271a0302014-09-23 15:28:38 -0400552 if (!AdjustForPaint(fSaveStack[i].paint, rect)) {
553 return false;
554 }
555 }
556 return true;
557 }
558
robertphillips4e8e3422014-11-12 06:46:08 -0800559 const unsigned fNumRecords;
mtkleina723b572014-08-15 11:49:49 -0700560
robertphillips4d52afe2014-11-03 08:19:44 -0800561 // We do not guarantee anything for operations outside of the cull rect
562 const SkRect fCullRect;
563
mtklein533eb782014-08-27 10:39:42 -0700564 // Conservative identity-space bounds for each op in the SkRecord.
565 SkAutoTMalloc<Bounds> fBounds;
mtkleina723b572014-08-15 11:49:49 -0700566
567 // We walk fCurrentOp through the SkRecord, as we go using updateCTM()
568 // and updateClipBounds() to maintain the exact CTM (fCTM) and conservative
mtklein533eb782014-08-27 10:39:42 -0700569 // identity-space bounds of the current clip (fCurrentClipBounds).
mtklein828ce1f2014-08-13 12:58:45 -0700570 unsigned fCurrentOp;
mtklein6332f1d2014-08-19 07:09:40 -0700571 const SkMatrix* fCTM;
mtklein533eb782014-08-27 10:39:42 -0700572 Bounds fCurrentClipBounds;
mtkleina723b572014-08-15 11:49:49 -0700573
574 // Used to track the bounds of Save/Restore blocks and the control ops inside them.
mtklein828ce1f2014-08-13 12:58:45 -0700575 SkTDArray<SaveBounds> fSaveStack;
576 SkTDArray<unsigned> fControlIndices;
mtklein5ad6ee12014-08-11 08:08:43 -0700577};
578
robertphillips4e8e3422014-11-12 06:46:08 -0800579// SkRecord visitor to gather saveLayer/restore information.
580class CollectLayers : SkNoncopyable {
581public:
reed1bdfd3f2014-11-24 14:41:51 -0800582 CollectLayers(const SkRect& cullRect, const SkRecord& record,
583 const SkPicture::SnapshotArray* pictList, SkLayerInfo* accelData)
robertphillips4e8e3422014-11-12 06:46:08 -0800584 : fSaveLayersInStack(0)
585 , fAccelData(accelData)
reed1bdfd3f2014-11-24 14:41:51 -0800586 , fPictList(pictList)
587 , fFillBounds(cullRect, record)
588 {}
robertphillips4e8e3422014-11-12 06:46:08 -0800589
590 void setCurrentOp(unsigned currentOp) { fFillBounds.setCurrentOp(currentOp); }
591
592 void cleanUp(SkBBoxHierarchy* bbh) {
593 // fFillBounds must perform its cleanUp first so that all the bounding
594 // boxes associated with unbalanced restores are updated (prior to
595 // fetching their bound in popSaveLayerInfo).
596 fFillBounds.cleanUp(bbh);
597
598 while (!fSaveLayerStack.isEmpty()) {
599 this->popSaveLayerInfo();
600 }
601 }
602
603 template <typename T> void operator()(const T& op) {
604 fFillBounds(op);
605 this->trackSaveLayers(op);
606 }
607
608private:
609 struct SaveLayerInfo {
610 SaveLayerInfo() { }
robertphillips478dd722014-12-16 08:25:55 -0800611 SaveLayerInfo(int opIndex, bool isSaveLayer, const SkRect* bounds, const SkPaint* paint)
robertphillips4e8e3422014-11-12 06:46:08 -0800612 : fStartIndex(opIndex)
613 , fIsSaveLayer(isSaveLayer)
614 , fHasNestedSaveLayer(false)
robertphillips478dd722014-12-16 08:25:55 -0800615 , fBounds(bounds ? *bounds : SkRect::MakeEmpty())
robertphillips74576eb2014-11-12 07:25:02 -0800616 , fPaint(paint) {
robertphillips4e8e3422014-11-12 06:46:08 -0800617 }
618
619 int fStartIndex;
620 bool fIsSaveLayer;
621 bool fHasNestedSaveLayer;
robertphillips478dd722014-12-16 08:25:55 -0800622 SkRect fBounds;
robertphillips4e8e3422014-11-12 06:46:08 -0800623 const SkPaint* fPaint;
robertphillips4e8e3422014-11-12 06:46:08 -0800624 };
625
626 template <typename T> void trackSaveLayers(const T& op) {
627 /* most ops aren't involved in saveLayers */
628 }
robertphillips478dd722014-12-16 08:25:55 -0800629 void trackSaveLayers(const Save& s) { this->pushSaveLayerInfo(false, NULL, NULL); }
630 void trackSaveLayers(const SaveLayer& sl) { this->pushSaveLayerInfo(true, sl.bounds, sl.paint); }
robertphillips4e8e3422014-11-12 06:46:08 -0800631 void trackSaveLayers(const Restore& r) { this->popSaveLayerInfo(); }
632
reed1bdfd3f2014-11-24 14:41:51 -0800633 void trackSaveLayersForPicture(const SkPicture* picture, const SkPaint* paint) {
robertphillips4e8e3422014-11-12 06:46:08 -0800634 // For sub-pictures, we wrap their layer information within the parent
635 // picture's rendering hierarchy
robertphillips82365912014-11-12 09:32:34 -0800636 SkPicture::AccelData::Key key = SkLayerInfo::ComputeKey();
robertphillips4e8e3422014-11-12 06:46:08 -0800637
robertphillips82365912014-11-12 09:32:34 -0800638 const SkLayerInfo* childData =
reed1bdfd3f2014-11-24 14:41:51 -0800639 static_cast<const SkLayerInfo*>(picture->EXPERIMENTAL_getAccelData(key));
robertphillips4e8e3422014-11-12 06:46:08 -0800640 if (!childData) {
641 // If the child layer hasn't been generated with saveLayer data we
642 // assume the worst (i.e., that it does contain layers which nest
643 // inside existing layers). Layers within sub-pictures that don't
644 // have saveLayer data cannot be hoisted.
645 // TODO: could the analysis data be use to fine tune this?
646 this->updateStackForSaveLayer();
647 return;
648 }
649
robertphillips82365912014-11-12 09:32:34 -0800650 for (int i = 0; i < childData->numBlocks(); ++i) {
651 const SkLayerInfo::BlockInfo& src = childData->block(i);
robertphillips4e8e3422014-11-12 06:46:08 -0800652
reed1bdfd3f2014-11-24 14:41:51 -0800653 FillBounds::Bounds newBound = fFillBounds.adjustAndMap(src.fBounds, paint);
robertphillips74576eb2014-11-12 07:25:02 -0800654 if (newBound.isEmpty()) {
robertphillips4e8e3422014-11-12 06:46:08 -0800655 continue;
656 }
657
658 this->updateStackForSaveLayer();
659
robertphillips82365912014-11-12 09:32:34 -0800660 SkLayerInfo::BlockInfo& dst = fAccelData->addBlock();
robertphillips4e8e3422014-11-12 06:46:08 -0800661
662 // If src.fPicture is NULL the layer is in dp.picture; otherwise
663 // it belongs to a sub-picture.
reed1bdfd3f2014-11-24 14:41:51 -0800664 dst.fPicture = src.fPicture ? src.fPicture : picture;
robertphillips4e8e3422014-11-12 06:46:08 -0800665 dst.fPicture->ref();
robertphillips74576eb2014-11-12 07:25:02 -0800666 dst.fBounds = newBound;
robertphillips478dd722014-12-16 08:25:55 -0800667 dst.fSrcBounds = src.fSrcBounds;
robertphillips4e8e3422014-11-12 06:46:08 -0800668 dst.fLocalMat = src.fLocalMat;
669 dst.fPreMat = src.fPreMat;
670 dst.fPreMat.postConcat(fFillBounds.ctm());
671 if (src.fPaint) {
672 dst.fPaint = SkNEW_ARGS(SkPaint, (*src.fPaint));
673 }
674 dst.fSaveLayerOpID = src.fSaveLayerOpID;
675 dst.fRestoreOpID = src.fRestoreOpID;
676 dst.fHasNestedLayers = src.fHasNestedLayers;
677 dst.fIsNested = fSaveLayersInStack > 0 || src.fIsNested;
robertphillips01d6e5f2014-12-01 09:09:27 -0800678
679 // Store 'saveLayer ops from enclosing picture' + drawPict op + 'ops from sub-picture'
680 dst.fKeySize = fSaveLayerOpStack.count() + src.fKeySize + 1;
robertphillipse99d4992014-12-03 07:33:57 -0800681 dst.fKey = SkNEW_ARRAY(unsigned, dst.fKeySize);
682 memcpy(dst.fKey, fSaveLayerOpStack.begin(), fSaveLayerOpStack.count() * sizeof(unsigned));
robertphillips01d6e5f2014-12-01 09:09:27 -0800683 dst.fKey[fSaveLayerOpStack.count()] = fFillBounds.currentOp();
robertphillipse99d4992014-12-03 07:33:57 -0800684 memcpy(&dst.fKey[fSaveLayerOpStack.count()+1], src.fKey, src.fKeySize * sizeof(unsigned));
robertphillips4e8e3422014-11-12 06:46:08 -0800685 }
686 }
687
reed1bdfd3f2014-11-24 14:41:51 -0800688 void trackSaveLayers(const DrawPicture& dp) {
689 this->trackSaveLayersForPicture(dp.picture, dp.paint);
690 }
691
692 void trackSaveLayers(const DrawDrawable& dp) {
693 SkASSERT(fPictList);
694 SkASSERT(dp.index >= 0 && dp.index < fPictList->count());
695 const SkPaint* paint = NULL; // drawables don't get a side-car paint
696 this->trackSaveLayersForPicture(fPictList->begin()[dp.index], paint);
697 }
698
robertphillips4e8e3422014-11-12 06:46:08 -0800699 // Inform all the saveLayers already on the stack that they now have a
700 // nested saveLayer inside them
701 void updateStackForSaveLayer() {
702 for (int index = fSaveLayerStack.count() - 1; index >= 0; --index) {
703 if (fSaveLayerStack[index].fHasNestedSaveLayer) {
704 break;
705 }
706 fSaveLayerStack[index].fHasNestedSaveLayer = true;
707 if (fSaveLayerStack[index].fIsSaveLayer) {
708 break;
709 }
710 }
711 }
712
robertphillips478dd722014-12-16 08:25:55 -0800713 void pushSaveLayerInfo(bool isSaveLayer, const SkRect* bounds, const SkPaint* paint) {
robertphillips4e8e3422014-11-12 06:46:08 -0800714 if (isSaveLayer) {
715 this->updateStackForSaveLayer();
716 ++fSaveLayersInStack;
robertphillips01d6e5f2014-12-01 09:09:27 -0800717 fSaveLayerOpStack.push(fFillBounds.currentOp());
robertphillips4e8e3422014-11-12 06:46:08 -0800718 }
719
robertphillips478dd722014-12-16 08:25:55 -0800720 fSaveLayerStack.push(SaveLayerInfo(fFillBounds.currentOp(), isSaveLayer, bounds, paint));
robertphillips4e8e3422014-11-12 06:46:08 -0800721 }
722
723 void popSaveLayerInfo() {
724 if (fSaveLayerStack.count() <= 0) {
725 SkASSERT(false);
726 return;
727 }
728
robertphillips01d6e5f2014-12-01 09:09:27 -0800729 SkASSERT(fSaveLayersInStack == fSaveLayerOpStack.count());
730
robertphillips4e8e3422014-11-12 06:46:08 -0800731 SaveLayerInfo sli;
732 fSaveLayerStack.pop(&sli);
733
734 if (!sli.fIsSaveLayer) {
735 return;
736 }
737
738 --fSaveLayersInStack;
739
robertphillips82365912014-11-12 09:32:34 -0800740 SkLayerInfo::BlockInfo& block = fAccelData->addBlock();
robertphillips4e8e3422014-11-12 06:46:08 -0800741
robertphillips82365912014-11-12 09:32:34 -0800742 SkASSERT(NULL == block.fPicture); // This layer is in the top-most picture
robertphillips4e8e3422014-11-12 06:46:08 -0800743
robertphillips82365912014-11-12 09:32:34 -0800744 block.fBounds = fFillBounds.getBounds(sli.fStartIndex);
745 block.fLocalMat = fFillBounds.ctm();
746 block.fPreMat = SkMatrix::I();
robertphillips4e8e3422014-11-12 06:46:08 -0800747 if (sli.fPaint) {
robertphillips82365912014-11-12 09:32:34 -0800748 block.fPaint = SkNEW_ARGS(SkPaint, (*sli.fPaint));
robertphillips4e8e3422014-11-12 06:46:08 -0800749 }
robertphillips478dd722014-12-16 08:25:55 -0800750
751 block.fSrcBounds = sli.fBounds;
robertphillips82365912014-11-12 09:32:34 -0800752 block.fSaveLayerOpID = sli.fStartIndex;
753 block.fRestoreOpID = fFillBounds.currentOp();
754 block.fHasNestedLayers = sli.fHasNestedSaveLayer;
755 block.fIsNested = fSaveLayersInStack > 0;
robertphillips01d6e5f2014-12-01 09:09:27 -0800756
757 block.fKeySize = fSaveLayerOpStack.count();
robertphillipse99d4992014-12-03 07:33:57 -0800758 block.fKey = SkNEW_ARRAY(unsigned, block.fKeySize);
759 memcpy(block.fKey, fSaveLayerOpStack.begin(), block.fKeySize * sizeof(unsigned));
robertphillips01d6e5f2014-12-01 09:09:27 -0800760
761 fSaveLayerOpStack.pop();
robertphillips4e8e3422014-11-12 06:46:08 -0800762 }
763
764 // Used to collect saveLayer information for layer hoisting
robertphillips01d6e5f2014-12-01 09:09:27 -0800765 int fSaveLayersInStack;
robertphillips4e8e3422014-11-12 06:46:08 -0800766 SkTDArray<SaveLayerInfo> fSaveLayerStack;
robertphillips01d6e5f2014-12-01 09:09:27 -0800767 // The op code indices of all the currently active saveLayers
robertphillipse99d4992014-12-03 07:33:57 -0800768 SkTDArray<unsigned> fSaveLayerOpStack;
robertphillips01d6e5f2014-12-01 09:09:27 -0800769 SkLayerInfo* fAccelData;
reed1bdfd3f2014-11-24 14:41:51 -0800770 const SkPicture::SnapshotArray* fPictList;
robertphillips4e8e3422014-11-12 06:46:08 -0800771
772 SkRecords::FillBounds fFillBounds;
773};
robertphillips4e8e3422014-11-12 06:46:08 -0800774
commit-bot@chromium.org27f6b0d2014-05-09 14:59:29 +0000775} // namespace SkRecords
mtklein5ad6ee12014-08-11 08:08:43 -0700776
robertphillips4d52afe2014-11-03 08:19:44 -0800777void SkRecordFillBounds(const SkRect& cullRect, const SkRecord& record, SkBBoxHierarchy* bbh) {
robertphillips4e8e3422014-11-12 06:46:08 -0800778 SkRecords::FillBounds visitor(cullRect, record);
779
780 for (unsigned curOp = 0; curOp < record.count(); curOp++) {
781 visitor.setCurrentOp(curOp);
782 record.visit<void>(curOp, visitor);
783 }
784
785 visitor.cleanUp(bbh);
mtklein5ad6ee12014-08-11 08:08:43 -0700786}
robertphillips4e8e3422014-11-12 06:46:08 -0800787
robertphillips4e8e3422014-11-12 06:46:08 -0800788void SkRecordComputeLayers(const SkRect& cullRect, const SkRecord& record,
reed1bdfd3f2014-11-24 14:41:51 -0800789 const SkPicture::SnapshotArray* pictList, SkBBoxHierarchy* bbh,
790 SkLayerInfo* data) {
791 SkRecords::CollectLayers visitor(cullRect, record, pictList, data);
robertphillips4e8e3422014-11-12 06:46:08 -0800792
793 for (unsigned curOp = 0; curOp < record.count(); curOp++) {
794 visitor.setCurrentOp(curOp);
795 record.visit<void>(curOp, visitor);
796 }
797
798 visitor.cleanUp(bbh);
799}
robertphillips4e8e3422014-11-12 06:46:08 -0800800