blob: b9bf92c0bad77b9a73ff7711d7e24b905e822491 [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
mtkleinc6ad06a2015-08-19 09:51:00 -070032 SkTDArray<int> 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);
mtkleinc6ad06a2015-08-19 09:51:00 -070048 for (int 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,
mtkleinc6ad06a2015-08-19 09:51:00 -070062 int start, int stop,
robertphillips4815fe52014-09-16 10:32:43 -070063 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());
halcanary96fcdcc2015-08-27 07:41:13 -070067 SkRecords::Draw draw(canvas, drawablePicts, nullptr, drawableCount, &initialCTM);
mtkleinc6ad06a2015-08-19 09:51:00 -070068 for (int i = start; i < stop; i++) {
mtklein00f30bd2014-09-02 12:03:31 -070069 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)));
mtkleine9d20522015-11-19 12:08:24 -080083DRAW(Concat, concat(r.matrix));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000084
mtkleincdeeb092014-11-20 09:14:28 -080085DRAW(ClipPath, clipPath(r.path, r.opAA.op, r.opAA.aa));
86DRAW(ClipRRect, clipRRect(r.rrect, r.opAA.op, r.opAA.aa));
87DRAW(ClipRect, clipRect(r.rect, r.opAA.op, r.opAA.aa));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000088DRAW(ClipRegion, clipRegion(r.region, r.op));
89
mtkleinf55c3142014-12-11 12:43:04 -080090DRAW(DrawBitmap, drawBitmap(r.bitmap.shallowCopy(), r.left, r.top, r.paint));
91DRAW(DrawBitmapNine, drawBitmapNine(r.bitmap.shallowCopy(), r.center, r.dst, r.paint));
reeda5517e22015-07-14 10:54:12 -070092DRAW(DrawBitmapRect,
reede47829b2015-08-06 10:02:53 -070093 legacy_drawBitmapRect(r.bitmap.shallowCopy(), r.src, r.dst, r.paint,
reeda5517e22015-07-14 10:54:12 -070094 SkCanvas::kStrict_SrcRectConstraint));
95DRAW(DrawBitmapRectFast,
reede47829b2015-08-06 10:02:53 -070096 legacy_drawBitmapRect(r.bitmap.shallowCopy(), r.src, r.dst, r.paint,
reeda5517e22015-07-14 10:54:12 -070097 SkCanvas::kFast_SrcRectConstraint));
98DRAW(DrawBitmapRectFixedSize,
reede47829b2015-08-06 10:02:53 -070099 legacy_drawBitmapRect(r.bitmap.shallowCopy(), &r.src, r.dst, &r.paint, r.constraint));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +0000100DRAW(DrawDRRect, drawDRRect(r.outer, r.inner, r.paint));
piotaixr65151752014-10-16 11:58:39 -0700101DRAW(DrawImage, drawImage(r.image, r.left, r.top, r.paint));
reede47829b2015-08-06 10:02:53 -0700102DRAW(DrawImageRect, legacy_drawImageRect(r.image, r.src, r.dst, r.paint, r.constraint));
reed4c21dc52015-06-25 12:32:03 -0700103DRAW(DrawImageNine, drawImageNine(r.image, r.center, 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));
reed71c3c762015-06-24 10:29:17 -0700118DRAW(DrawAtlas, drawAtlas(r.atlas, r.xforms, r.texs, r.colors, r.count, r.mode, r.cull, r.paint));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +0000119DRAW(DrawVertices, drawVertices(r.vmode, r.vertexCount, r.vertices, r.texs, r.colors,
mtklein449d9b72015-09-28 10:33:02 -0700120 r.xmode, r.indices, r.indexCount, r.paint));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +0000121#undef DRAW
122
reed6be2aa92014-11-18 11:08:05 -0800123template <> void Draw::draw(const DrawDrawable& r) {
124 SkASSERT(r.index >= 0);
125 SkASSERT(r.index < fDrawableCount);
reed1bdfd3f2014-11-24 14:41:51 -0800126 if (fDrawables) {
halcanary96fcdcc2015-08-27 07:41:13 -0700127 SkASSERT(nullptr == fDrawablePicts);
reeda8db7282015-07-07 10:22:31 -0700128 fCanvas->drawDrawable(fDrawables[r.index], r.matrix);
reed1bdfd3f2014-11-24 14:41:51 -0800129 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700130 fCanvas->drawPicture(fDrawablePicts[r.index], r.matrix, nullptr);
reed1bdfd3f2014-11-24 14:41:51 -0800131 }
reed6be2aa92014-11-18 11:08:05 -0800132}
133
mtklein5ad6ee12014-08-11 08:08:43 -0700134// This is an SkRecord visitor that fills an SkBBoxHierarchy.
mtklein828ce1f2014-08-13 12:58:45 -0700135//
136// The interesting part here is how to calculate bounds for ops which don't
137// have intrinsic bounds. What is the bounds of a Save or a Translate?
138//
139// We answer this by thinking about a particular definition of bounds: if I
140// don't execute this op, pixels in this rectangle might draw incorrectly. So
141// the bounds of a Save, a Translate, a Restore, etc. are the union of the
142// bounds of Draw* ops that they might have an effect on. For any given
143// Save/Restore block, the bounds of the Save, the Restore, and any other
144// non-drawing ("control") ops inside are exactly the union of the bounds of
145// the drawing ops inside that block.
146//
147// To implement this, we keep a stack of active Save blocks. As we consume ops
148// inside the Save/Restore block, drawing ops are unioned with the bounds of
149// the block, and control ops are stashed away for later. When we finish the
150// block with a Restore, our bounds are complete, and we go back and fill them
151// in for all the control ops we stashed away.
mtklein5ad6ee12014-08-11 08:08:43 -0700152class FillBounds : SkNoncopyable {
153public:
mtklein40732b32015-10-24 07:45:47 -0700154 FillBounds(const SkRect& cullRect, const SkRecord& record, SkRect bounds[])
robertphillips4e8e3422014-11-12 06:46:08 -0800155 : fNumRecords(record.count())
156 , fCullRect(cullRect)
mtklein40732b32015-10-24 07:45:47 -0700157 , fBounds(bounds) {
mtkleine9d20522015-11-19 12:08:24 -0800158 fCTM = SkMatrix::I();
robertphillips4d52afe2014-11-03 08:19:44 -0800159 fCurrentClipBounds = fCullRect;
robertphillips4e8e3422014-11-12 06:46:08 -0800160 }
mtklein5ad6ee12014-08-11 08:08:43 -0700161
mtklein40732b32015-10-24 07:45:47 -0700162 void cleanUp() {
mtklein828ce1f2014-08-13 12:58:45 -0700163 // If we have any lingering unpaired Saves, simulate restores to make
164 // sure all ops in those Save blocks have their bounds calculated.
165 while (!fSaveStack.isEmpty()) {
166 this->popSaveBlock();
167 }
168
169 // Any control ops not part of any Save/Restore block draw everywhere.
170 while (!fControlIndices.isEmpty()) {
robertphillips4d52afe2014-11-03 08:19:44 -0800171 this->popControl(fCullRect);
mtklein828ce1f2014-08-13 12:58:45 -0700172 }
mtklein828ce1f2014-08-13 12:58:45 -0700173 }
mtklein5ad6ee12014-08-11 08:08:43 -0700174
mtklein40732b32015-10-24 07:45:47 -0700175 void setCurrentOp(int currentOp) { fCurrentOp = currentOp; }
176
177
mtkleina723b572014-08-15 11:49:49 -0700178 template <typename T> void operator()(const T& op) {
179 this->updateCTM(op);
180 this->updateClipBounds(op);
181 this->trackBounds(op);
mtklein5ad6ee12014-08-11 08:08:43 -0700182 }
183
mtklein533eb782014-08-27 10:39:42 -0700184 // In this file, SkRect are in local coordinates, Bounds are translated back to identity space.
185 typedef SkRect Bounds;
186
mtkleinc6ad06a2015-08-19 09:51:00 -0700187 int currentOp() const { return fCurrentOp; }
mtkleine9d20522015-11-19 12:08:24 -0800188 const SkMatrix& ctm() const { return fCTM; }
mtkleinc6ad06a2015-08-19 09:51:00 -0700189 const Bounds& getBounds(int index) const { return fBounds[index]; }
robertphillips4e8e3422014-11-12 06:46:08 -0800190
191 // Adjust rect for all paints that may affect its geometry, then map it to identity space.
192 Bounds adjustAndMap(SkRect rect, const SkPaint* paint) const {
193 // Inverted rectangles really confuse our BBHs.
194 rect.sort();
195
196 // Adjust the rect for its own paint.
197 if (!AdjustForPaint(paint, &rect)) {
198 // The paint could do anything to our bounds. The only safe answer is the current clip.
199 return fCurrentClipBounds;
200 }
201
202 // Adjust rect for all the paints from the SaveLayers we're inside.
203 if (!this->adjustForSaveLayerPaints(&rect)) {
204 // Same deal as above.
205 return fCurrentClipBounds;
206 }
207
208 // Map the rect back to identity space.
mtkleine9d20522015-11-19 12:08:24 -0800209 fCTM.mapRect(&rect);
robertphillips4e8e3422014-11-12 06:46:08 -0800210
211 // Nothing can draw outside the current clip.
robertphillipsc187a3c2014-12-30 13:53:51 -0800212 if (!rect.intersect(fCurrentClipBounds)) {
213 return Bounds::MakeEmpty();
214 }
215
robertphillips4e8e3422014-11-12 06:46:08 -0800216 return rect;
217 }
218
219private:
mtklein828ce1f2014-08-13 12:58:45 -0700220 struct SaveBounds {
mtkleina723b572014-08-15 11:49:49 -0700221 int controlOps; // Number of control ops in this Save block, including the Save.
mtklein533eb782014-08-27 10:39:42 -0700222 Bounds bounds; // Bounds of everything in the block.
mtkleina723b572014-08-15 11:49:49 -0700223 const SkPaint* paint; // Unowned. If set, adjusts the bounds of all ops in this block.
mtklein828ce1f2014-08-13 12:58:45 -0700224 };
225
mtkleine9d20522015-11-19 12:08:24 -0800226 // Only Restore, SetMatrix, and Concat change the CTM.
mtklein8e393bf2014-10-01 12:48:58 -0700227 template <typename T> void updateCTM(const T&) {}
mtkleine9d20522015-11-19 12:08:24 -0800228 void updateCTM(const Restore& op) { fCTM = op.matrix; }
229 void updateCTM(const SetMatrix& op) { fCTM = op.matrix; }
230 void updateCTM(const Concat& op) { fCTM.preConcat(op.matrix); }
mtkleina723b572014-08-15 11:49:49 -0700231
mtklein8e393bf2014-10-01 12:48:58 -0700232 // Most ops don't change the clip.
233 template <typename T> void updateClipBounds(const T&) {}
Mike Klein271a0302014-09-23 15:28:38 -0400234
mtklein8e393bf2014-10-01 12:48:58 -0700235 // Clip{Path,RRect,Rect,Region} obviously change the clip. They all know their bounds already.
236 void updateClipBounds(const ClipPath& op) { this->updateClipBoundsForClipOp(op.devBounds); }
237 void updateClipBounds(const ClipRRect& op) { this->updateClipBoundsForClipOp(op.devBounds); }
238 void updateClipBounds(const ClipRect& op) { this->updateClipBoundsForClipOp(op.devBounds); }
239 void updateClipBounds(const ClipRegion& op) { this->updateClipBoundsForClipOp(op.devBounds); }
Mike Klein271a0302014-09-23 15:28:38 -0400240
mtklein8e393bf2014-10-01 12:48:58 -0700241 // The bounds of clip ops need to be adjusted for the paints of saveLayers they're inside.
242 void updateClipBoundsForClipOp(const SkIRect& devBounds) {
243 Bounds clip = SkRect::Make(devBounds);
Mike Klein271a0302014-09-23 15:28:38 -0400244 // We don't call adjustAndMap() because as its last step it would intersect the adjusted
245 // clip bounds with the previous clip, exactly what we can't do when the clip grows.
schenney23d85932015-03-06 16:20:28 -0800246 if (this->adjustForSaveLayerPaints(&clip)) {
247 fCurrentClipBounds = clip.intersect(fCullRect) ? clip : Bounds::MakeEmpty();
248 } else {
249 fCurrentClipBounds = fCullRect;
250 }
Mike Klein271a0302014-09-23 15:28:38 -0400251 }
252
mtklein8e393bf2014-10-01 12:48:58 -0700253 // Restore holds the devBounds for the clip after the {save,saveLayer}/restore block completes.
254 void updateClipBounds(const Restore& op) {
255 // This is just like the clip ops above, but we need to skip the effects (if any) of our
256 // paired saveLayer (if it is one); it has not yet been popped off the save stack. Our
257 // devBounds reflect the state of the world after the saveLayer/restore block is done,
258 // so they are not affected by the saveLayer's paint.
259 const int kSavesToIgnore = 1;
260 Bounds clip = SkRect::Make(op.devBounds);
schenney23d85932015-03-06 16:20:28 -0800261 if (this->adjustForSaveLayerPaints(&clip, kSavesToIgnore)) {
262 fCurrentClipBounds = clip.intersect(fCullRect) ? clip : Bounds::MakeEmpty();
263 } else {
264 fCurrentClipBounds = fCullRect;
265 }
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.
halcanary96fcdcc2015-08-27 07:41:13 -0700278 void trackBounds(const Save&) { this->pushSaveBlock(nullptr); }
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(); }
mtkleine9d20522015-11-19 12:08:24 -0800283 void trackBounds(const Concat&) { this->pushControl(); }
mtklein68199a22014-08-25 13:49:29 -0700284 void trackBounds(const ClipRect&) { this->pushControl(); }
285 void trackBounds(const ClipRRect&) { this->pushControl(); }
286 void trackBounds(const ClipPath&) { this->pushControl(); }
287 void trackBounds(const ClipRegion&) { 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;
halcanary96fcdcc2015-08-27 07:41:13 -0700323 // SrcOver is ok, and is also the common case with a nullptr xfermode.
dneto327f9052014-09-15 10:53:16 -0700324 // 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
schenney23d85932015-03-06 16:20:28 -0800390 Bounds bounds(const DrawSprite& op) const { // Ignores the matrix, but respects the clip.
391 SkRect rect = Bounds::MakeXYWH(op.left, op.top, op.bitmap.width(), op.bitmap.height());
392 if (!rect.intersect(fCurrentClipBounds)) {
393 return Bounds::MakeEmpty();
394 }
395 return rect;
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 }
reed4c21dc52015-06-25 12:32:03 -0700415 Bounds bounds(const DrawImageNine& op) const {
416 return this->adjustAndMap(op.dst, op.paint);
417 }
reeda5517e22015-07-14 10:54:12 -0700418 Bounds bounds(const DrawBitmapRect& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700419 return this->adjustAndMap(op.dst, op.paint);
420 }
reeda5517e22015-07-14 10:54:12 -0700421 Bounds bounds(const DrawBitmapRectFast& op) const {
mtklein42ddcd42014-11-21 08:48:35 -0800422 return this->adjustAndMap(op.dst, op.paint);
423 }
reeda5517e22015-07-14 10:54:12 -0700424 Bounds bounds(const DrawBitmapRectFixedSize& op) const {
mtklein64b4c782015-07-01 13:56:53 -0700425 return this->adjustAndMap(op.dst, &op.paint);
426 }
mtklein533eb782014-08-27 10:39:42 -0700427 Bounds bounds(const DrawBitmapNine& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700428 return this->adjustAndMap(op.dst, op.paint);
429 }
mtklein533eb782014-08-27 10:39:42 -0700430 Bounds bounds(const DrawBitmap& op) const {
mtkleinaf579032014-12-01 11:03:37 -0800431 return this->adjustAndMap(
432 SkRect::MakeXYWH(op.left, op.top, op.bitmap.width(), op.bitmap.height()),
433 op.paint);
mtklein62b67ae2014-08-18 11:10:37 -0700434 }
mtklein62b67ae2014-08-18 11:10:37 -0700435
mtklein533eb782014-08-27 10:39:42 -0700436 Bounds bounds(const DrawPath& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700437 return op.path.isInverseFillType() ? fCurrentClipBounds
438 : this->adjustAndMap(op.path.getBounds(), &op.paint);
439 }
mtklein533eb782014-08-27 10:39:42 -0700440 Bounds bounds(const DrawPoints& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700441 SkRect dst;
442 dst.set(op.pts, op.count);
443
444 // Pad the bounding box a little to make sure hairline points' bounds aren't empty.
445 SkScalar stroke = SkMaxScalar(op.paint.getStrokeWidth(), 0.01f);
446 dst.outset(stroke/2, stroke/2);
447
448 return this->adjustAndMap(dst, &op.paint);
449 }
mtklein533eb782014-08-27 10:39:42 -0700450 Bounds bounds(const DrawPatch& op) const {
mtklein131a22b2014-08-25 14:16:15 -0700451 SkRect dst;
452 dst.set(op.cubics, SkPatchUtils::kNumCtrlPts);
453 return this->adjustAndMap(dst, &op.paint);
454 }
mtklein533eb782014-08-27 10:39:42 -0700455 Bounds bounds(const DrawVertices& op) const {
mtklein131a22b2014-08-25 14:16:15 -0700456 SkRect dst;
457 dst.set(op.vertices, op.vertexCount);
458 return this->adjustAndMap(dst, &op.paint);
459 }
mtklein64b4c782015-07-01 13:56:53 -0700460
reed71c3c762015-06-24 10:29:17 -0700461 Bounds bounds(const DrawAtlas& op) const {
462 if (op.cull) {
463 return this->adjustAndMap(*op.cull, op.paint);
464 } else {
465 return fCurrentClipBounds;
466 }
467 }
mtklein64b4c782015-07-01 13:56:53 -0700468
mtklein533eb782014-08-27 10:39:42 -0700469 Bounds bounds(const DrawPicture& op) const {
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700470 SkRect dst = op.picture->cullRect();
mtkleinaf579032014-12-01 11:03:37 -0800471 op.matrix.mapRect(&dst);
mtklein131a22b2014-08-25 14:16:15 -0700472 return this->adjustAndMap(dst, op.paint);
473 }
mtklein62b67ae2014-08-18 11:10:37 -0700474
mtklein533eb782014-08-27 10:39:42 -0700475 Bounds bounds(const DrawPosText& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700476 const int N = op.paint.countText(op.text, op.byteLength);
477 if (N == 0) {
mtklein533eb782014-08-27 10:39:42 -0700478 return Bounds::MakeEmpty();
mtklein62b67ae2014-08-18 11:10:37 -0700479 }
480
481 SkRect dst;
mtklein937c9c72014-09-02 15:19:48 -0700482 dst.set(op.pos, N);
mtklein62b67ae2014-08-18 11:10:37 -0700483 AdjustTextForFontMetrics(&dst, op.paint);
484 return this->adjustAndMap(dst, &op.paint);
485 }
mtklein533eb782014-08-27 10:39:42 -0700486 Bounds bounds(const DrawPosTextH& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700487 const int N = op.paint.countText(op.text, op.byteLength);
488 if (N == 0) {
mtklein533eb782014-08-27 10:39:42 -0700489 return Bounds::MakeEmpty();
mtklein62b67ae2014-08-18 11:10:37 -0700490 }
491
492 SkScalar left = op.xpos[0], right = op.xpos[0];
493 for (int i = 1; i < N; i++) {
494 left = SkMinScalar(left, op.xpos[i]);
495 right = SkMaxScalar(right, op.xpos[i]);
496 }
497 SkRect dst = { left, op.y, right, op.y };
498 AdjustTextForFontMetrics(&dst, op.paint);
499 return this->adjustAndMap(dst, &op.paint);
500 }
mtklein533eb782014-08-27 10:39:42 -0700501 Bounds bounds(const DrawTextOnPath& op) const {
mtklein131a22b2014-08-25 14:16:15 -0700502 SkRect dst = op.path.getBounds();
503
mtklein9a5380d2014-12-16 06:31:01 -0800504 // Pad all sides by the maximum padding in any direction we'd normally apply.
mtklein131a22b2014-08-25 14:16:15 -0700505 SkRect pad = { 0, 0, 0, 0};
506 AdjustTextForFontMetrics(&pad, op.paint);
mtklein9a5380d2014-12-16 06:31:01 -0800507
508 // That maximum padding happens to always be the right pad today.
509 SkASSERT(pad.fLeft == -pad.fRight);
510 SkASSERT(pad.fTop == -pad.fBottom);
511 SkASSERT(pad.fRight > pad.fBottom);
512 dst.outset(pad.fRight, pad.fRight);
513
mtklein131a22b2014-08-25 14:16:15 -0700514 return this->adjustAndMap(dst, &op.paint);
515 }
516
mtklein533eb782014-08-27 10:39:42 -0700517 Bounds bounds(const DrawTextBlob& op) const {
mtklein131a22b2014-08-25 14:16:15 -0700518 SkRect dst = op.blob->bounds();
519 dst.offset(op.x, op.y);
mtklein131a22b2014-08-25 14:16:15 -0700520 return this->adjustAndMap(dst, &op.paint);
521 }
mtklein62b67ae2014-08-18 11:10:37 -0700522
reed6be2aa92014-11-18 11:08:05 -0800523 Bounds bounds(const DrawDrawable& op) const {
halcanary96fcdcc2015-08-27 07:41:13 -0700524 return this->adjustAndMap(op.worstCaseBounds, nullptr);
reed6be2aa92014-11-18 11:08:05 -0800525 }
526
mtklein62b67ae2014-08-18 11:10:37 -0700527 static void AdjustTextForFontMetrics(SkRect* rect, const SkPaint& paint) {
mtklein9a5380d2014-12-16 06:31:01 -0800528#ifdef SK_DEBUG
529 SkRect correct = *rect;
530#endif
531 // crbug.com/373785 ~~> xPad = 4x yPad
532 // crbug.com/424824 ~~> bump yPad from 2x text size to 2.5x
533 const SkScalar yPad = 2.5f * paint.getTextSize(),
534 xPad = 4.0f * yPad;
535 rect->outset(xPad, yPad);
caryclark9a657fa2014-08-20 05:24:29 -0700536#ifdef SK_DEBUG
mtklein62b67ae2014-08-18 11:10:37 -0700537 SkPaint::FontMetrics metrics;
538 paint.getFontMetrics(&metrics);
mtklein9a5380d2014-12-16 06:31:01 -0800539 correct.fLeft += metrics.fXMin;
540 correct.fTop += metrics.fTop;
541 correct.fRight += metrics.fXMax;
542 correct.fBottom += metrics.fBottom;
mtkleind13291a2014-08-21 14:46:49 -0700543 // See skia:2862 for why we ignore small text sizes.
mtklein9a5380d2014-12-16 06:31:01 -0800544 SkASSERTF(paint.getTextSize() < 0.001f || rect->contains(correct),
mtkleined167ac2014-10-29 16:07:10 -0700545 "%f %f %f %f vs. %f %f %f %f\n",
mtklein9a5380d2014-12-16 06:31:01 -0800546 -xPad, -yPad, +xPad, +yPad,
mtkleined167ac2014-10-29 16:07:10 -0700547 metrics.fXMin, metrics.fTop, metrics.fXMax, metrics.fBottom);
mtkleina19afb42014-08-19 17:47:14 -0700548#endif
mtklein62b67ae2014-08-18 11:10:37 -0700549 }
550
mtklein479601b2014-08-18 08:45:33 -0700551 // Returns true if rect was meaningfully adjusted for the effects of paint,
552 // false if the paint could affect the rect in unknown ways.
553 static bool AdjustForPaint(const SkPaint* paint, SkRect* rect) {
mtkleina723b572014-08-15 11:49:49 -0700554 if (paint) {
555 if (paint->canComputeFastBounds()) {
mtklein479601b2014-08-18 08:45:33 -0700556 *rect = paint->computeFastBounds(*rect, rect);
557 return true;
mtkleina723b572014-08-15 11:49:49 -0700558 }
mtklein479601b2014-08-18 08:45:33 -0700559 return false;
560 }
561 return true;
562 }
563
mtklein8e393bf2014-10-01 12:48:58 -0700564 bool adjustForSaveLayerPaints(SkRect* rect, int savesToIgnore = 0) const {
565 for (int i = fSaveStack.count() - 1 - savesToIgnore; i >= 0; i--) {
Mike Klein271a0302014-09-23 15:28:38 -0400566 if (!AdjustForPaint(fSaveStack[i].paint, rect)) {
567 return false;
568 }
569 }
570 return true;
571 }
572
mtkleinc6ad06a2015-08-19 09:51:00 -0700573 const int fNumRecords;
mtkleina723b572014-08-15 11:49:49 -0700574
robertphillips4d52afe2014-11-03 08:19:44 -0800575 // We do not guarantee anything for operations outside of the cull rect
576 const SkRect fCullRect;
577
mtklein533eb782014-08-27 10:39:42 -0700578 // Conservative identity-space bounds for each op in the SkRecord.
mtklein40732b32015-10-24 07:45:47 -0700579 Bounds* fBounds;
mtkleina723b572014-08-15 11:49:49 -0700580
581 // We walk fCurrentOp through the SkRecord, as we go using updateCTM()
582 // and updateClipBounds() to maintain the exact CTM (fCTM) and conservative
mtklein533eb782014-08-27 10:39:42 -0700583 // identity-space bounds of the current clip (fCurrentClipBounds).
mtkleinc6ad06a2015-08-19 09:51:00 -0700584 int fCurrentOp;
mtkleine9d20522015-11-19 12:08:24 -0800585 SkMatrix fCTM;
mtklein533eb782014-08-27 10:39:42 -0700586 Bounds fCurrentClipBounds;
mtkleina723b572014-08-15 11:49:49 -0700587
588 // Used to track the bounds of Save/Restore blocks and the control ops inside them.
mtklein828ce1f2014-08-13 12:58:45 -0700589 SkTDArray<SaveBounds> fSaveStack;
mtkleinc6ad06a2015-08-19 09:51:00 -0700590 SkTDArray<int> fControlIndices;
mtklein5ad6ee12014-08-11 08:08:43 -0700591};
592
robertphillips4e8e3422014-11-12 06:46:08 -0800593// SkRecord visitor to gather saveLayer/restore information.
594class CollectLayers : SkNoncopyable {
595public:
mtklein40732b32015-10-24 07:45:47 -0700596 CollectLayers(const SkRect& cullRect, const SkRecord& record, SkRect bounds[],
mtklein9db912c2015-05-19 11:11:26 -0700597 const SkBigPicture::SnapshotArray* pictList, SkLayerInfo* accelData)
robertphillips4e8e3422014-11-12 06:46:08 -0800598 : fSaveLayersInStack(0)
599 , fAccelData(accelData)
reed1bdfd3f2014-11-24 14:41:51 -0800600 , fPictList(pictList)
mtklein40732b32015-10-24 07:45:47 -0700601 , fFillBounds(cullRect, record, bounds)
reed1bdfd3f2014-11-24 14:41:51 -0800602 {}
robertphillips4e8e3422014-11-12 06:46:08 -0800603
mtklein40732b32015-10-24 07:45:47 -0700604 void cleanUp() {
robertphillips4e8e3422014-11-12 06:46:08 -0800605 // fFillBounds must perform its cleanUp first so that all the bounding
606 // boxes associated with unbalanced restores are updated (prior to
607 // fetching their bound in popSaveLayerInfo).
mtklein40732b32015-10-24 07:45:47 -0700608 fFillBounds.cleanUp();
robertphillips4e8e3422014-11-12 06:46:08 -0800609 while (!fSaveLayerStack.isEmpty()) {
610 this->popSaveLayerInfo();
611 }
612 }
613
mtklein40732b32015-10-24 07:45:47 -0700614 void setCurrentOp(int currentOp) { fFillBounds.setCurrentOp(currentOp); }
615
616
robertphillips4e8e3422014-11-12 06:46:08 -0800617 template <typename T> void operator()(const T& op) {
618 fFillBounds(op);
619 this->trackSaveLayers(op);
620 }
621
622private:
623 struct SaveLayerInfo {
624 SaveLayerInfo() { }
robertphillips478dd722014-12-16 08:25:55 -0800625 SaveLayerInfo(int opIndex, bool isSaveLayer, const SkRect* bounds, const SkPaint* paint)
robertphillips4e8e3422014-11-12 06:46:08 -0800626 : fStartIndex(opIndex)
627 , fIsSaveLayer(isSaveLayer)
628 , fHasNestedSaveLayer(false)
robertphillips478dd722014-12-16 08:25:55 -0800629 , fBounds(bounds ? *bounds : SkRect::MakeEmpty())
robertphillips74576eb2014-11-12 07:25:02 -0800630 , fPaint(paint) {
robertphillips4e8e3422014-11-12 06:46:08 -0800631 }
632
633 int fStartIndex;
634 bool fIsSaveLayer;
635 bool fHasNestedSaveLayer;
robertphillips478dd722014-12-16 08:25:55 -0800636 SkRect fBounds;
robertphillips4e8e3422014-11-12 06:46:08 -0800637 const SkPaint* fPaint;
robertphillips4e8e3422014-11-12 06:46:08 -0800638 };
639
640 template <typename T> void trackSaveLayers(const T& op) {
641 /* most ops aren't involved in saveLayers */
642 }
halcanary96fcdcc2015-08-27 07:41:13 -0700643 void trackSaveLayers(const Save& s) { this->pushSaveLayerInfo(false, nullptr, nullptr); }
robertphillips478dd722014-12-16 08:25:55 -0800644 void trackSaveLayers(const SaveLayer& sl) { this->pushSaveLayerInfo(true, sl.bounds, sl.paint); }
robertphillips4e8e3422014-11-12 06:46:08 -0800645 void trackSaveLayers(const Restore& r) { this->popSaveLayerInfo(); }
646
reed1bdfd3f2014-11-24 14:41:51 -0800647 void trackSaveLayersForPicture(const SkPicture* picture, const SkPaint* paint) {
robertphillips4e8e3422014-11-12 06:46:08 -0800648 // For sub-pictures, we wrap their layer information within the parent
649 // picture's rendering hierarchy
halcanary96fcdcc2015-08-27 07:41:13 -0700650 const SkLayerInfo* childData = nullptr;
mtklein9db912c2015-05-19 11:11:26 -0700651 if (const SkBigPicture* bp = picture->asSkBigPicture()) {
652 childData = static_cast<const SkLayerInfo*>(bp->accelData());
653 }
robertphillips4e8e3422014-11-12 06:46:08 -0800654 if (!childData) {
655 // If the child layer hasn't been generated with saveLayer data we
656 // assume the worst (i.e., that it does contain layers which nest
657 // inside existing layers). Layers within sub-pictures that don't
658 // have saveLayer data cannot be hoisted.
659 // TODO: could the analysis data be use to fine tune this?
660 this->updateStackForSaveLayer();
661 return;
662 }
663
robertphillips82365912014-11-12 09:32:34 -0800664 for (int i = 0; i < childData->numBlocks(); ++i) {
665 const SkLayerInfo::BlockInfo& src = childData->block(i);
robertphillips4e8e3422014-11-12 06:46:08 -0800666
reed1bdfd3f2014-11-24 14:41:51 -0800667 FillBounds::Bounds newBound = fFillBounds.adjustAndMap(src.fBounds, paint);
robertphillips74576eb2014-11-12 07:25:02 -0800668 if (newBound.isEmpty()) {
robertphillips4e8e3422014-11-12 06:46:08 -0800669 continue;
670 }
671
672 this->updateStackForSaveLayer();
673
robertphillips82365912014-11-12 09:32:34 -0800674 SkLayerInfo::BlockInfo& dst = fAccelData->addBlock();
robertphillips4e8e3422014-11-12 06:46:08 -0800675
halcanary96fcdcc2015-08-27 07:41:13 -0700676 // If src.fPicture is nullptr the layer is in dp.picture; otherwise
robertphillips4e8e3422014-11-12 06:46:08 -0800677 // it belongs to a sub-picture.
reed1bdfd3f2014-11-24 14:41:51 -0800678 dst.fPicture = src.fPicture ? src.fPicture : picture;
robertphillips4e8e3422014-11-12 06:46:08 -0800679 dst.fPicture->ref();
robertphillips74576eb2014-11-12 07:25:02 -0800680 dst.fBounds = newBound;
robertphillips478dd722014-12-16 08:25:55 -0800681 dst.fSrcBounds = src.fSrcBounds;
robertphillips4e8e3422014-11-12 06:46:08 -0800682 dst.fLocalMat = src.fLocalMat;
683 dst.fPreMat = src.fPreMat;
684 dst.fPreMat.postConcat(fFillBounds.ctm());
685 if (src.fPaint) {
halcanary385fe4d2015-08-26 13:07:48 -0700686 dst.fPaint = new SkPaint(*src.fPaint);
robertphillips4e8e3422014-11-12 06:46:08 -0800687 }
688 dst.fSaveLayerOpID = src.fSaveLayerOpID;
689 dst.fRestoreOpID = src.fRestoreOpID;
690 dst.fHasNestedLayers = src.fHasNestedLayers;
691 dst.fIsNested = fSaveLayersInStack > 0 || src.fIsNested;
robertphillips01d6e5f2014-12-01 09:09:27 -0800692
693 // Store 'saveLayer ops from enclosing picture' + drawPict op + 'ops from sub-picture'
694 dst.fKeySize = fSaveLayerOpStack.count() + src.fKeySize + 1;
halcanary385fe4d2015-08-26 13:07:48 -0700695 dst.fKey = new int[dst.fKeySize];
mtklein02046c52015-12-09 10:02:14 -0800696 sk_careful_memcpy(dst.fKey, fSaveLayerOpStack.begin(),
697 fSaveLayerOpStack.count() * sizeof(int));
robertphillips01d6e5f2014-12-01 09:09:27 -0800698 dst.fKey[fSaveLayerOpStack.count()] = fFillBounds.currentOp();
mtkleinc6ad06a2015-08-19 09:51:00 -0700699 memcpy(&dst.fKey[fSaveLayerOpStack.count()+1], src.fKey, src.fKeySize * sizeof(int));
robertphillips4e8e3422014-11-12 06:46:08 -0800700 }
701 }
702
reed1bdfd3f2014-11-24 14:41:51 -0800703 void trackSaveLayers(const DrawPicture& dp) {
704 this->trackSaveLayersForPicture(dp.picture, dp.paint);
705 }
706
707 void trackSaveLayers(const DrawDrawable& dp) {
708 SkASSERT(fPictList);
709 SkASSERT(dp.index >= 0 && dp.index < fPictList->count());
halcanary96fcdcc2015-08-27 07:41:13 -0700710 const SkPaint* paint = nullptr; // drawables don't get a side-car paint
reed1bdfd3f2014-11-24 14:41:51 -0800711 this->trackSaveLayersForPicture(fPictList->begin()[dp.index], paint);
712 }
713
robertphillips4e8e3422014-11-12 06:46:08 -0800714 // Inform all the saveLayers already on the stack that they now have a
715 // nested saveLayer inside them
716 void updateStackForSaveLayer() {
717 for (int index = fSaveLayerStack.count() - 1; index >= 0; --index) {
718 if (fSaveLayerStack[index].fHasNestedSaveLayer) {
719 break;
720 }
721 fSaveLayerStack[index].fHasNestedSaveLayer = true;
722 if (fSaveLayerStack[index].fIsSaveLayer) {
723 break;
724 }
725 }
726 }
727
robertphillips478dd722014-12-16 08:25:55 -0800728 void pushSaveLayerInfo(bool isSaveLayer, const SkRect* bounds, const SkPaint* paint) {
robertphillips4e8e3422014-11-12 06:46:08 -0800729 if (isSaveLayer) {
730 this->updateStackForSaveLayer();
731 ++fSaveLayersInStack;
robertphillips01d6e5f2014-12-01 09:09:27 -0800732 fSaveLayerOpStack.push(fFillBounds.currentOp());
robertphillips4e8e3422014-11-12 06:46:08 -0800733 }
734
robertphillips478dd722014-12-16 08:25:55 -0800735 fSaveLayerStack.push(SaveLayerInfo(fFillBounds.currentOp(), isSaveLayer, bounds, paint));
robertphillips4e8e3422014-11-12 06:46:08 -0800736 }
737
738 void popSaveLayerInfo() {
739 if (fSaveLayerStack.count() <= 0) {
740 SkASSERT(false);
741 return;
742 }
743
robertphillips01d6e5f2014-12-01 09:09:27 -0800744 SkASSERT(fSaveLayersInStack == fSaveLayerOpStack.count());
745
robertphillips4e8e3422014-11-12 06:46:08 -0800746 SaveLayerInfo sli;
747 fSaveLayerStack.pop(&sli);
748
749 if (!sli.fIsSaveLayer) {
750 return;
751 }
752
753 --fSaveLayersInStack;
754
robertphillips82365912014-11-12 09:32:34 -0800755 SkLayerInfo::BlockInfo& block = fAccelData->addBlock();
robertphillips4e8e3422014-11-12 06:46:08 -0800756
halcanary96fcdcc2015-08-27 07:41:13 -0700757 SkASSERT(nullptr == block.fPicture); // This layer is in the top-most picture
robertphillips4e8e3422014-11-12 06:46:08 -0800758
robertphillips82365912014-11-12 09:32:34 -0800759 block.fBounds = fFillBounds.getBounds(sli.fStartIndex);
760 block.fLocalMat = fFillBounds.ctm();
761 block.fPreMat = SkMatrix::I();
robertphillips4e8e3422014-11-12 06:46:08 -0800762 if (sli.fPaint) {
halcanary385fe4d2015-08-26 13:07:48 -0700763 block.fPaint = new SkPaint(*sli.fPaint);
robertphillips4e8e3422014-11-12 06:46:08 -0800764 }
robertphillips478dd722014-12-16 08:25:55 -0800765
766 block.fSrcBounds = sli.fBounds;
robertphillips82365912014-11-12 09:32:34 -0800767 block.fSaveLayerOpID = sli.fStartIndex;
768 block.fRestoreOpID = fFillBounds.currentOp();
769 block.fHasNestedLayers = sli.fHasNestedSaveLayer;
770 block.fIsNested = fSaveLayersInStack > 0;
robertphillips01d6e5f2014-12-01 09:09:27 -0800771
772 block.fKeySize = fSaveLayerOpStack.count();
halcanary385fe4d2015-08-26 13:07:48 -0700773 block.fKey = new int[block.fKeySize];
mtkleinc6ad06a2015-08-19 09:51:00 -0700774 memcpy(block.fKey, fSaveLayerOpStack.begin(), block.fKeySize * sizeof(int));
robertphillips01d6e5f2014-12-01 09:09:27 -0800775
776 fSaveLayerOpStack.pop();
robertphillips4e8e3422014-11-12 06:46:08 -0800777 }
778
779 // Used to collect saveLayer information for layer hoisting
robertphillips01d6e5f2014-12-01 09:09:27 -0800780 int fSaveLayersInStack;
robertphillips4e8e3422014-11-12 06:46:08 -0800781 SkTDArray<SaveLayerInfo> fSaveLayerStack;
robertphillips01d6e5f2014-12-01 09:09:27 -0800782 // The op code indices of all the currently active saveLayers
mtkleinc6ad06a2015-08-19 09:51:00 -0700783 SkTDArray<int> fSaveLayerOpStack;
robertphillips01d6e5f2014-12-01 09:09:27 -0800784 SkLayerInfo* fAccelData;
mtklein9db912c2015-05-19 11:11:26 -0700785 const SkBigPicture::SnapshotArray* fPictList;
robertphillips4e8e3422014-11-12 06:46:08 -0800786
787 SkRecords::FillBounds fFillBounds;
788};
robertphillips4e8e3422014-11-12 06:46:08 -0800789
commit-bot@chromium.org27f6b0d2014-05-09 14:59:29 +0000790} // namespace SkRecords
mtklein5ad6ee12014-08-11 08:08:43 -0700791
mtklein40732b32015-10-24 07:45:47 -0700792void SkRecordFillBounds(const SkRect& cullRect, const SkRecord& record, SkRect bounds[]) {
793 SkRecords::FillBounds visitor(cullRect, record, bounds);
mtkleinc6ad06a2015-08-19 09:51:00 -0700794 for (int curOp = 0; curOp < record.count(); curOp++) {
robertphillips4e8e3422014-11-12 06:46:08 -0800795 visitor.setCurrentOp(curOp);
796 record.visit<void>(curOp, visitor);
797 }
mtklein40732b32015-10-24 07:45:47 -0700798 visitor.cleanUp();
mtklein5ad6ee12014-08-11 08:08:43 -0700799}
robertphillips4e8e3422014-11-12 06:46:08 -0800800
mtklein40732b32015-10-24 07:45:47 -0700801void SkRecordComputeLayers(const SkRect& cullRect, const SkRecord& record, SkRect bounds[],
802 const SkBigPicture::SnapshotArray* pictList, SkLayerInfo* data) {
803 SkRecords::CollectLayers visitor(cullRect, record, bounds, pictList, data);
mtkleinc6ad06a2015-08-19 09:51:00 -0700804 for (int curOp = 0; curOp < record.count(); curOp++) {
robertphillips4e8e3422014-11-12 06:46:08 -0800805 visitor.setCurrentOp(curOp);
806 record.visit<void>(curOp, visitor);
807 }
mtklein40732b32015-10-24 07:45:47 -0700808 visitor.cleanUp();
robertphillips4e8e3422014-11-12 06:46:08 -0800809}
robertphillips4e8e3422014-11-12 06:46:08 -0800810