• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
8 #include "include/core/SkBBHFactory.h"
9 #include "include/core/SkImage.h"
10 #include "src/core/SkCanvasPriv.h"
11 #include "src/core/SkColorFilterBase.h"
12 #include "src/core/SkImageFilter_Base.h"
13 #include "src/core/SkRecordDraw.h"
14 #include "src/utils/SkPatchUtils.h"
15 
SkRecordDraw(const SkRecord & record,SkCanvas * canvas,SkPicture const * const drawablePicts[],SkDrawable * const drawables[],int drawableCount,const SkBBoxHierarchy * bbh,SkPicture::AbortCallback * callback)16 void SkRecordDraw(const SkRecord& record,
17                   SkCanvas* canvas,
18                   SkPicture const* const drawablePicts[],
19                   SkDrawable* const drawables[],
20                   int drawableCount,
21                   const SkBBoxHierarchy* bbh,
22                   SkPicture::AbortCallback* callback) {
23     SkAutoCanvasRestore saveRestore(canvas, true /*save now, restore at exit*/);
24 
25     if (bbh) {
26         // Draw only ops that affect pixels in the canvas's current clip.
27         // The SkRecord and BBH were recorded in identity space.  This canvas
28         // is not necessarily in that same space.  getLocalClipBounds() returns us
29         // this canvas' clip bounds transformed back into identity space, which
30         // lets us query the BBH.
31         SkRect query = canvas->getLocalClipBounds();
32 
33         std::vector<int> ops;
34         bbh->search(query, &ops);
35 
36         SkRecords::Draw draw(canvas, drawablePicts, drawables, drawableCount);
37         for (int i = 0; i < (int)ops.size(); i++) {
38             if (callback && callback->abort()) {
39                 return;
40             }
41             // This visit call uses the SkRecords::Draw::operator() to call
42             // methods on the |canvas|, wrapped by methods defined with the
43             // DRAW() macro.
44             record.visit(ops[i], draw);
45         }
46     } else {
47         // Draw all ops.
48         SkRecords::Draw draw(canvas, drawablePicts, drawables, drawableCount);
49         for (int i = 0; i < record.count(); i++) {
50             if (callback && callback->abort()) {
51                 return;
52             }
53             // This visit call uses the SkRecords::Draw::operator() to call
54             // methods on the |canvas|, wrapped by methods defined with the
55             // DRAW() macro.
56             record.visit(i, draw);
57         }
58     }
59 }
60 
SkRecordPartialDraw(const SkRecord & record,SkCanvas * canvas,SkPicture const * const drawablePicts[],int drawableCount,int start,int stop,const SkM44 & initialCTM)61 void SkRecordPartialDraw(const SkRecord& record, SkCanvas* canvas,
62                          SkPicture const* const drawablePicts[], int drawableCount,
63                          int start, int stop,
64                          const SkM44& initialCTM) {
65     SkAutoCanvasRestore saveRestore(canvas, true /*save now, restore at exit*/);
66 
67     stop = std::min(stop, record.count());
68     SkRecords::Draw draw(canvas, drawablePicts, nullptr, drawableCount, &initialCTM);
69     for (int i = start; i < stop; i++) {
70         record.visit(i, draw);
71     }
72 }
73 
74 namespace SkRecords {
75 
76 // NoOps draw nothing.
draw(const NoOp &)77 template <> void Draw::draw(const NoOp&) {}
78 
79 #define DRAW(T, call) template <> void Draw::draw(const T& r) { fCanvas->call; }
80 DRAW(Flush, flush());
81 DRAW(Restore, restore());
82 DRAW(Save, save());
83 DRAW(SaveLayer, saveLayer(SkCanvasPriv::ScaledBackdropLayer(r.bounds,
84                                                             r.paint,
85                                                             r.backdrop.get(),
86                                                             r.backdropScale,
87                                                             r.saveLayerFlags)));
88 
draw(const SaveBehind & r)89 template <> void Draw::draw(const SaveBehind& r) {
90     SkCanvasPriv::SaveBehind(fCanvas, r.subset);
91 }
92 
draw(const DrawBehind & r)93 template <> void Draw::draw(const DrawBehind& r) {
94     SkCanvasPriv::DrawBehind(fCanvas, r.paint);
95 }
96 
97 DRAW(SetMatrix, setMatrix(fInitialCTM.asM33() * r.matrix));
98 DRAW(SetM44, setMatrix(fInitialCTM * r.matrix));
99 DRAW(Concat44, concat(r.matrix));
100 DRAW(Concat, concat(r.matrix));
101 DRAW(Translate, translate(r.dx, r.dy));
102 DRAW(Scale, scale(r.sx, r.sy));
103 
104 DRAW(ClipPath, clipPath(r.path, r.opAA.op(), r.opAA.aa()));
105 DRAW(ClipRRect, clipRRect(r.rrect, r.opAA.op(), r.opAA.aa()));
106 DRAW(ClipRect, clipRect(r.rect, r.opAA.op(), r.opAA.aa()));
107 DRAW(ClipRegion, clipRegion(r.region, r.op));
108 DRAW(ClipShader, clipShader(r.shader, r.op));
109 
draw(const ResetClip & r)110 template <> void Draw::draw(const ResetClip& r) {
111     SkCanvasPriv::ResetClip(fCanvas);
112 }
113 
114 DRAW(DrawArc, drawArc(r.oval, r.startAngle, r.sweepAngle, r.useCenter, r.paint));
115 DRAW(DrawDRRect, drawDRRect(r.outer, r.inner, r.paint));
116 DRAW(DrawImage, drawImage(r.image.get(), r.left, r.top, r.sampling, r.paint));
117 
draw(const DrawImageLattice & r)118 template <> void Draw::draw(const DrawImageLattice& r) {
119     SkCanvas::Lattice lattice;
120     lattice.fXCount = r.xCount;
121     lattice.fXDivs = r.xDivs;
122     lattice.fYCount = r.yCount;
123     lattice.fYDivs = r.yDivs;
124     lattice.fRectTypes = (0 == r.flagCount) ? nullptr : r.flags;
125     lattice.fColors = (0 == r.flagCount) ? nullptr : r.colors;
126     lattice.fBounds = &r.src;
127     fCanvas->drawImageLattice(r.image.get(), lattice, r.dst, r.filter, r.paint);
128 }
129 
130 DRAW(DrawImageRect, drawImageRect(r.image.get(), r.src, r.dst, r.sampling, r.paint, r.constraint));
131 DRAW(DrawOval, drawOval(r.oval, r.paint));
132 DRAW(DrawPaint, drawPaint(r.paint));
133 DRAW(DrawPath, drawPath(r.path, r.paint));
134 DRAW(DrawPatch, drawPatch(r.cubics, r.colors, r.texCoords, r.bmode, r.paint));
135 DRAW(DrawPicture, drawPicture(r.picture.get(), &r.matrix, r.paint));
136 DRAW(DrawPoints, drawPoints(r.mode, r.count, r.pts, r.paint));
137 DRAW(DrawRRect, drawRRect(r.rrect, r.paint));
138 DRAW(DrawRect, drawRect(r.rect, r.paint));
139 DRAW(DrawRegion, drawRegion(r.region, r.paint));
140 DRAW(DrawTextBlob, drawTextBlob(r.blob.get(), r.x, r.y, r.paint));
141 DRAW(DrawAtlas, drawAtlas(r.atlas.get(), r.xforms, r.texs, r.colors, r.count, r.mode, r.sampling,
142                           r.cull, r.paint));
143 DRAW(DrawVertices, drawVertices(r.vertices, r.bmode, r.paint));
144 DRAW(DrawShadowRec, private_draw_shadow_rec(r.path, r.rec));
145 DRAW(DrawAnnotation, drawAnnotation(r.rect, r.key.c_str(), r.value.get()));
146 
147 DRAW(DrawEdgeAAQuad, experimental_DrawEdgeAAQuad(
148         r.rect, r.clip, r.aa, r.color, r.mode));
149 DRAW(DrawEdgeAAImageSet, experimental_DrawEdgeAAImageSet(
150         r.set.get(), r.count, r.dstClips, r.preViewMatrices, r.sampling, r.paint, r.constraint));
151 
152 #undef DRAW
153 
draw(const DrawDrawable & r)154 template <> void Draw::draw(const DrawDrawable& r) {
155     SkASSERT(r.index >= 0);
156     SkASSERT(r.index < fDrawableCount);
157     if (fDrawables) {
158         SkASSERT(nullptr == fDrawablePicts);
159         fCanvas->drawDrawable(fDrawables[r.index], r.matrix);
160     } else {
161         fCanvas->drawPicture(fDrawablePicts[r.index], r.matrix, nullptr);
162     }
163 }
164 
165 // This is an SkRecord visitor that fills an SkBBoxHierarchy.
166 //
167 // The interesting part here is how to calculate bounds for ops which don't
168 // have intrinsic bounds.  What is the bounds of a Save or a Translate?
169 //
170 // We answer this by thinking about a particular definition of bounds: if I
171 // don't execute this op, pixels in this rectangle might draw incorrectly.  So
172 // the bounds of a Save, a Translate, a Restore, etc. are the union of the
173 // bounds of Draw* ops that they might have an effect on.  For any given
174 // Save/Restore block, the bounds of the Save, the Restore, and any other
175 // non-drawing ("control") ops inside are exactly the union of the bounds of
176 // the drawing ops inside that block.
177 //
178 // To implement this, we keep a stack of active Save blocks.  As we consume ops
179 // inside the Save/Restore block, drawing ops are unioned with the bounds of
180 // the block, and control ops are stashed away for later.  When we finish the
181 // block with a Restore, our bounds are complete, and we go back and fill them
182 // in for all the control ops we stashed away.
183 class FillBounds : SkNoncopyable {
184 public:
FillBounds(const SkRect & cullRect,const SkRecord & record,SkRect bounds[],SkBBoxHierarchy::Metadata meta[])185     FillBounds(const SkRect& cullRect, const SkRecord& record,
186                SkRect bounds[], SkBBoxHierarchy::Metadata meta[])
187         : fCullRect(cullRect)
188         , fBounds(bounds)
189         , fMeta(meta) {
190         fCTM = SkMatrix::I();
191 
192         // We push an extra save block to track the bounds of any top-level control operations.
193         fSaveStack.push_back({ 0, Bounds::MakeEmpty(), nullptr, fCTM });
194     }
195 
~FillBounds()196     ~FillBounds() {
197         // If we have any lingering unpaired Saves, simulate restores to make
198         // sure all ops in those Save blocks have their bounds calculated.
199         while (!fSaveStack.isEmpty()) {
200             this->popSaveBlock();
201         }
202 
203         // Any control ops not part of any Save/Restore block draw everywhere.
204         while (!fControlIndices.isEmpty()) {
205             this->popControl(fCullRect);
206         }
207     }
208 
setCurrentOp(int currentOp)209     void setCurrentOp(int currentOp) { fCurrentOp = currentOp; }
210 
211 
operator ()(const T & op)212     template <typename T> void operator()(const T& op) {
213         this->updateCTM(op);
214         this->trackBounds(op);
215     }
216 
217     // In this file, SkRect are in local coordinates, Bounds are translated back to identity space.
218     typedef SkRect Bounds;
219 
220     // Adjust rect for all paints that may affect its geometry, then map it to identity space.
adjustAndMap(SkRect rect,const SkPaint * paint) const221     Bounds adjustAndMap(SkRect rect, const SkPaint* paint) const {
222         // Inverted rectangles really confuse our BBHs.
223         rect.sort();
224 
225         // Adjust the rect for its own paint.
226         if (!AdjustForPaint(paint, &rect)) {
227             // The paint could do anything to our bounds.  The only safe answer is the cull.
228             return fCullRect;
229         }
230 
231         // Adjust rect for all the paints from the SaveLayers we're inside.
232         if (!this->adjustForSaveLayerPaints(&rect)) {
233             // Same deal as above.
234             return fCullRect;
235         }
236 
237         // Map the rect back to identity space.
238         fCTM.mapRect(&rect);
239 
240         // Nothing can draw outside the cull rect.
241         if (!rect.intersect(fCullRect)) {
242             return Bounds::MakeEmpty();
243         }
244 
245         return rect;
246     }
247 
248 private:
249     struct SaveBounds {
250         int controlOps;        // Number of control ops in this Save block, including the Save.
251         Bounds bounds;         // Bounds of everything in the block.
252         const SkPaint* paint;  // Unowned.  If set, adjusts the bounds of all ops in this block.
253         SkMatrix ctm;
254     };
255 
256     // Only Restore, SetMatrix, Concat, and Translate change the CTM.
updateCTM(const T &)257     template <typename T> void updateCTM(const T&) {}
updateCTM(const Restore & op)258     void updateCTM(const Restore& op)   { fCTM = op.matrix; }
updateCTM(const SetMatrix & op)259     void updateCTM(const SetMatrix& op) { fCTM = op.matrix; }
updateCTM(const SetM44 & op)260     void updateCTM(const SetM44& op)    { fCTM = op.matrix.asM33(); }
updateCTM(const Concat44 & op)261     void updateCTM(const Concat44& op)  { fCTM.preConcat(op.matrix.asM33()); }
updateCTM(const Concat & op)262     void updateCTM(const Concat& op)    { fCTM.preConcat(op.matrix); }
updateCTM(const Scale & op)263     void updateCTM(const Scale& op)     { fCTM.preScale(op.sx, op.sy); }
updateCTM(const Translate & op)264     void updateCTM(const Translate& op) { fCTM.preTranslate(op.dx, op.dy); }
265 
266     // The bounds of these ops must be calculated when we hit the Restore
267     // from the bounds of the ops in the same Save block.
trackBounds(const Save &)268     void trackBounds(const Save&)          { this->pushSaveBlock(nullptr); }
trackBounds(const SaveLayer & op)269     void trackBounds(const SaveLayer& op)  { this->pushSaveBlock(op.paint); }
trackBounds(const SaveBehind &)270     void trackBounds(const SaveBehind&)    { this->pushSaveBlock(nullptr); }
trackBounds(const Restore &)271     void trackBounds(const Restore&) {
272         const bool isSaveLayer = fSaveStack.top().paint != nullptr;
273         fBounds[fCurrentOp] = this->popSaveBlock();
274         fMeta  [fCurrentOp].isDraw = isSaveLayer;
275     }
276 
trackBounds(const SetMatrix &)277     void trackBounds(const SetMatrix&)         { this->pushControl(); }
trackBounds(const SetM44 &)278     void trackBounds(const SetM44&)            { this->pushControl(); }
trackBounds(const Concat &)279     void trackBounds(const Concat&)            { this->pushControl(); }
trackBounds(const Concat44 &)280     void trackBounds(const Concat44&)          { this->pushControl(); }
trackBounds(const Scale &)281     void trackBounds(const Scale&)             { this->pushControl(); }
trackBounds(const Translate &)282     void trackBounds(const Translate&)         { this->pushControl(); }
trackBounds(const ClipRect &)283     void trackBounds(const ClipRect&)          { this->pushControl(); }
trackBounds(const ClipRRect &)284     void trackBounds(const ClipRRect&)         { this->pushControl(); }
trackBounds(const ClipPath &)285     void trackBounds(const ClipPath&)          { this->pushControl(); }
trackBounds(const ClipRegion &)286     void trackBounds(const ClipRegion&)        { this->pushControl(); }
trackBounds(const ClipShader &)287     void trackBounds(const ClipShader&)        { this->pushControl(); }
trackBounds(const ResetClip &)288     void trackBounds(const ResetClip&)         { this->pushControl(); }
289 
290 
291     // For all other ops, we can calculate and store the bounds directly now.
trackBounds(const T & op)292     template <typename T> void trackBounds(const T& op) {
293         fBounds[fCurrentOp] = this->bounds(op);
294         fMeta  [fCurrentOp].isDraw = true;
295         this->updateSaveBounds(fBounds[fCurrentOp]);
296     }
297 
pushSaveBlock(const SkPaint * paint)298     void pushSaveBlock(const SkPaint* paint) {
299         // Starting a new Save block.  Push a new entry to represent that.
300         SaveBounds sb;
301         sb.controlOps = 0;
302         // If the paint affects transparent black,
303         // the bound shouldn't be smaller than the cull.
304         sb.bounds =
305             PaintMayAffectTransparentBlack(paint) ? fCullRect : Bounds::MakeEmpty();
306         sb.paint = paint;
307         sb.ctm = this->fCTM;
308 
309         fSaveStack.push_back(sb);
310         this->pushControl();
311     }
312 
PaintMayAffectTransparentBlack(const SkPaint * paint)313     static bool PaintMayAffectTransparentBlack(const SkPaint* paint) {
314         if (paint) {
315             // FIXME: this is very conservative
316             if ((paint->getImageFilter() &&
317                  as_IFB(paint->getImageFilter())->affectsTransparentBlack()) ||
318                 (paint->getColorFilter() &&
319                  as_CFB(paint->getColorFilter())->affectsTransparentBlack())) {
320                 return true;
321             }
322             const auto bm = paint->asBlendMode();
323             if (!bm) {
324                 return true;    // can we query other blenders for this?
325             }
326 
327             // Unusual blendmodes require us to process a saved layer
328             // even with operations outisde the clip.
329             // For example, DstIn is used by masking layers.
330             // https://code.google.com/p/skia/issues/detail?id=1291
331             // https://crbug.com/401593
332             switch (bm.value()) {
333                 // For each of the following transfer modes, if the source
334                 // alpha is zero (our transparent black), the resulting
335                 // blended alpha is not necessarily equal to the original
336                 // destination alpha.
337                 case SkBlendMode::kClear:
338                 case SkBlendMode::kSrc:
339                 case SkBlendMode::kSrcIn:
340                 case SkBlendMode::kDstIn:
341                 case SkBlendMode::kSrcOut:
342                 case SkBlendMode::kDstATop:
343                 case SkBlendMode::kModulate:
344                     return true;
345                     break;
346                 default:
347                     break;
348             }
349         }
350         return false;
351     }
352 
popSaveBlock()353     Bounds popSaveBlock() {
354         // We're done the Save block.  Apply the block's bounds to all control ops inside it.
355         SaveBounds sb;
356         fSaveStack.pop(&sb);
357 
358         while (sb.controlOps --> 0) {
359             this->popControl(sb.bounds);
360         }
361 
362         // This whole Save block may be part another Save block.
363         this->updateSaveBounds(sb.bounds);
364 
365         // If called from a real Restore (not a phony one for balance), it'll need the bounds.
366         return sb.bounds;
367     }
368 
pushControl()369     void pushControl() {
370         fControlIndices.push_back(fCurrentOp);
371         if (!fSaveStack.isEmpty()) {
372             fSaveStack.top().controlOps++;
373         }
374     }
375 
popControl(const Bounds & bounds)376     void popControl(const Bounds& bounds) {
377         fBounds[fControlIndices.top()] = bounds;
378         fMeta  [fControlIndices.top()].isDraw = false;
379         fControlIndices.pop();
380     }
381 
updateSaveBounds(const Bounds & bounds)382     void updateSaveBounds(const Bounds& bounds) {
383         // If we're in a Save block, expand its bounds to cover these bounds too.
384         if (!fSaveStack.isEmpty()) {
385             fSaveStack.top().bounds.join(bounds);
386         }
387     }
388 
bounds(const Flush &) const389     Bounds bounds(const Flush&) const { return fCullRect; }
390 
bounds(const DrawPaint &) const391     Bounds bounds(const DrawPaint&) const { return fCullRect; }
bounds(const DrawBehind &) const392     Bounds bounds(const DrawBehind&) const { return fCullRect; }
bounds(const NoOp &) const393     Bounds bounds(const NoOp&)  const { return Bounds::MakeEmpty(); }    // NoOps don't draw.
394 
bounds(const DrawRect & op) const395     Bounds bounds(const DrawRect& op) const { return this->adjustAndMap(op.rect, &op.paint); }
bounds(const DrawRegion & op) const396     Bounds bounds(const DrawRegion& op) const {
397         SkRect rect = SkRect::Make(op.region.getBounds());
398         return this->adjustAndMap(rect, &op.paint);
399     }
bounds(const DrawOval & op) const400     Bounds bounds(const DrawOval& op) const { return this->adjustAndMap(op.oval, &op.paint); }
401     // Tighter arc bounds?
bounds(const DrawArc & op) const402     Bounds bounds(const DrawArc& op) const { return this->adjustAndMap(op.oval, &op.paint); }
bounds(const DrawRRect & op) const403     Bounds bounds(const DrawRRect& op) const {
404         return this->adjustAndMap(op.rrect.rect(), &op.paint);
405     }
bounds(const DrawDRRect & op) const406     Bounds bounds(const DrawDRRect& op) const {
407         return this->adjustAndMap(op.outer.rect(), &op.paint);
408     }
bounds(const DrawImage & op) const409     Bounds bounds(const DrawImage& op) const {
410         const SkImage* image = op.image.get();
411         SkRect rect = SkRect::MakeXYWH(op.left, op.top, image->width(), image->height());
412 
413         return this->adjustAndMap(rect, op.paint);
414     }
bounds(const DrawImageLattice & op) const415     Bounds bounds(const DrawImageLattice& op) const {
416         return this->adjustAndMap(op.dst, op.paint);
417     }
bounds(const DrawImageRect & op) const418     Bounds bounds(const DrawImageRect& op) const {
419         return this->adjustAndMap(op.dst, op.paint);
420     }
bounds(const DrawPath & op) const421     Bounds bounds(const DrawPath& op) const {
422         return op.path.isInverseFillType() ? fCullRect
423                                            : this->adjustAndMap(op.path.getBounds(), &op.paint);
424     }
bounds(const DrawPoints & op) const425     Bounds bounds(const DrawPoints& op) const {
426         SkRect dst;
427         dst.setBounds(op.pts, op.count);
428 
429         // Pad the bounding box a little to make sure hairline points' bounds aren't empty.
430         SkScalar stroke = std::max(op.paint.getStrokeWidth(), 0.01f);
431         dst.outset(stroke/2, stroke/2);
432 
433         return this->adjustAndMap(dst, &op.paint);
434     }
bounds(const DrawPatch & op) const435     Bounds bounds(const DrawPatch& op) const {
436         SkRect dst;
437         dst.setBounds(op.cubics, SkPatchUtils::kNumCtrlPts);
438         return this->adjustAndMap(dst, &op.paint);
439     }
bounds(const DrawVertices & op) const440     Bounds bounds(const DrawVertices& op) const {
441         return this->adjustAndMap(op.vertices->bounds(), &op.paint);
442     }
443 
bounds(const DrawAtlas & op) const444     Bounds bounds(const DrawAtlas& op) const {
445         if (op.cull) {
446             // TODO: <reed> can we pass nullptr for the paint? Isn't cull already "correct"
447             // for the paint (by the caller)?
448             return this->adjustAndMap(*op.cull, op.paint);
449         } else {
450             return fCullRect;
451         }
452     }
453 
bounds(const DrawShadowRec & op) const454     Bounds bounds(const DrawShadowRec& op) const {
455         SkRect bounds;
456         SkDrawShadowMetrics::GetLocalBounds(op.path, op.rec, fCTM, &bounds);
457         return this->adjustAndMap(bounds, nullptr);
458     }
459 
bounds(const DrawPicture & op) const460     Bounds bounds(const DrawPicture& op) const {
461         SkRect dst = op.picture->cullRect();
462         op.matrix.mapRect(&dst);
463         return this->adjustAndMap(dst, op.paint);
464     }
465 
bounds(const DrawTextBlob & op) const466     Bounds bounds(const DrawTextBlob& op) const {
467         SkRect dst = op.blob->bounds();
468         dst.offset(op.x, op.y);
469         return this->adjustAndMap(dst, &op.paint);
470     }
471 
bounds(const DrawDrawable & op) const472     Bounds bounds(const DrawDrawable& op) const {
473         return this->adjustAndMap(op.worstCaseBounds, nullptr);
474     }
475 
bounds(const DrawAnnotation & op) const476     Bounds bounds(const DrawAnnotation& op) const {
477         return this->adjustAndMap(op.rect, nullptr);
478     }
bounds(const DrawEdgeAAQuad & op) const479     Bounds bounds(const DrawEdgeAAQuad& op) const {
480         SkRect bounds = op.rect;
481         if (op.clip) {
482             bounds.setBounds(op.clip, 4);
483         }
484         return this->adjustAndMap(bounds, nullptr);
485     }
bounds(const DrawEdgeAAImageSet & op) const486     Bounds bounds(const DrawEdgeAAImageSet& op) const {
487         SkRect rect = SkRect::MakeEmpty();
488         int clipIndex = 0;
489         for (int i = 0; i < op.count; ++i) {
490             SkRect entryBounds = op.set[i].fDstRect;
491             if (op.set[i].fHasClip) {
492                 entryBounds.setBounds(op.dstClips + clipIndex, 4);
493                 clipIndex += 4;
494             }
495             if (op.set[i].fMatrixIndex >= 0) {
496                 op.preViewMatrices[op.set[i].fMatrixIndex].mapRect(&entryBounds);
497             }
498             rect.join(this->adjustAndMap(entryBounds, nullptr));
499         }
500         return rect;
501     }
502 
503     // Returns true if rect was meaningfully adjusted for the effects of paint,
504     // false if the paint could affect the rect in unknown ways.
AdjustForPaint(const SkPaint * paint,SkRect * rect)505     static bool AdjustForPaint(const SkPaint* paint, SkRect* rect) {
506         if (paint) {
507             if (paint->canComputeFastBounds()) {
508                 *rect = paint->computeFastBounds(*rect, rect);
509                 return true;
510             }
511             return false;
512         }
513         return true;
514     }
515 
adjustForSaveLayerPaints(SkRect * rect,int savesToIgnore=0) const516     bool adjustForSaveLayerPaints(SkRect* rect, int savesToIgnore = 0) const {
517         for (int i = fSaveStack.count() - 1 - savesToIgnore; i >= 0; i--) {
518             SkMatrix inverse;
519             if (!fSaveStack[i].ctm.invert(&inverse)) {
520                 return false;
521             }
522             inverse.mapRect(rect);
523             if (!AdjustForPaint(fSaveStack[i].paint, rect)) {
524                 return false;
525             }
526             fSaveStack[i].ctm.mapRect(rect);
527         }
528         return true;
529     }
530 
531     // We do not guarantee anything for operations outside of the cull rect
532     const SkRect fCullRect;
533 
534     // Conservative identity-space bounds for each op in the SkRecord.
535     Bounds* fBounds;
536 
537     // Parallel array to fBounds, holding metadata for each bounds rect.
538     SkBBoxHierarchy::Metadata* fMeta;
539 
540     // We walk fCurrentOp through the SkRecord,
541     // as we go using updateCTM() to maintain the exact CTM (fCTM).
542     int fCurrentOp;
543     SkMatrix fCTM;
544 
545     // Used to track the bounds of Save/Restore blocks and the control ops inside them.
546     SkTDArray<SaveBounds> fSaveStack;
547     SkTDArray<int>   fControlIndices;
548 };
549 
550 }  // namespace SkRecords
551 
SkRecordFillBounds(const SkRect & cullRect,const SkRecord & record,SkRect bounds[],SkBBoxHierarchy::Metadata meta[])552 void SkRecordFillBounds(const SkRect& cullRect, const SkRecord& record,
553                         SkRect bounds[], SkBBoxHierarchy::Metadata meta[]) {
554     {
555         SkRecords::FillBounds visitor(cullRect, record, bounds, meta);
556         for (int i = 0; i < record.count(); i++) {
557             visitor.setCurrentOp(i);
558             record.visit(i, visitor);
559         }
560     }
561 }
562