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