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
draw(const DrawBehind & r)91 template <> void Draw::draw(const DrawBehind& r) {
92 SkCanvasPriv::DrawBehind(fCanvas, r.paint);
93 }
94
95 DRAW(SetMatrix, setMatrix(SkMatrix::Concat(fInitialCTM, r.matrix)));
96 DRAW(Concat, concat(r.matrix));
97 DRAW(Translate, translate(r.dx, r.dy));
98
99 DRAW(ClipPath, clipPath(r.path, r.opAA.op(), r.opAA.aa()));
100 DRAW(ClipRRect, clipRRect(r.rrect, r.opAA.op(), r.opAA.aa()));
101 DRAW(ClipRect, clipRect(r.rect, r.opAA.op(), r.opAA.aa()));
102 DRAW(ClipRegion, clipRegion(r.region, r.op));
103
104 DRAW(DrawArc, drawArc(r.oval, r.startAngle, r.sweepAngle, r.useCenter, r.paint));
105 DRAW(DrawDRRect, drawDRRect(r.outer, r.inner, r.paint));
106 DRAW(DrawImage, drawImage(r.image.get(), r.left, r.top, r.paint));
107
draw(const DrawImageLattice & r)108 template <> void Draw::draw(const DrawImageLattice& r) {
109 SkCanvas::Lattice lattice;
110 lattice.fXCount = r.xCount;
111 lattice.fXDivs = r.xDivs;
112 lattice.fYCount = r.yCount;
113 lattice.fYDivs = r.yDivs;
114 lattice.fRectTypes = (0 == r.flagCount) ? nullptr : r.flags;
115 lattice.fColors = (0 == r.flagCount) ? nullptr : r.colors;
116 lattice.fBounds = &r.src;
117 fCanvas->drawImageLattice(r.image.get(), lattice, r.dst, r.paint);
118 }
119
120 DRAW(DrawImageRect, legacy_drawImageRect(r.image.get(), r.src, r.dst, r.paint, r.constraint));
121 DRAW(DrawImageNine, drawImageNine(r.image.get(), r.center, r.dst, r.paint));
122 DRAW(DrawImageSet, experimental_DrawImageSetV1(r.set.get(), r.count, r.quality, r.mode));
123 DRAW(DrawOval, drawOval(r.oval, r.paint));
124 DRAW(DrawPaint, drawPaint(r.paint));
125 DRAW(DrawPath, drawPath(r.path, r.paint));
126 DRAW(DrawPatch, drawPatch(r.cubics, r.colors, r.texCoords, r.bmode, r.paint));
127 DRAW(DrawPicture, drawPicture(r.picture.get(), &r.matrix, r.paint));
128 DRAW(DrawPoints, drawPoints(r.mode, r.count, r.pts, r.paint));
129 DRAW(DrawRRect, drawRRect(r.rrect, r.paint));
130 DRAW(DrawRect, drawRect(r.rect, r.paint));
131 DRAW(DrawEdgeAARect, experimental_DrawEdgeAARectV1(r.rect, r.aa, r.color, r.mode));
132 DRAW(DrawRegion, drawRegion(r.region, r.paint));
133 DRAW(DrawTextBlob, drawTextBlob(r.blob.get(), r.x, r.y, r.paint));
134 DRAW(DrawAtlas, drawAtlas(r.atlas.get(),
135 r.xforms, r.texs, r.colors, r.count, r.mode, r.cull, r.paint));
136 DRAW(DrawVertices, drawVertices(r.vertices, r.bones, r.boneCount, r.bmode, r.paint));
137 DRAW(DrawShadowRec, private_draw_shadow_rec(r.path, r.rec));
138 DRAW(DrawAnnotation, drawAnnotation(r.rect, r.key.c_str(), r.value.get()));
139 #undef DRAW
140
draw(const DrawDrawable & r)141 template <> void Draw::draw(const DrawDrawable& r) {
142 SkASSERT(r.index >= 0);
143 SkASSERT(r.index < fDrawableCount);
144 if (fDrawables) {
145 SkASSERT(nullptr == fDrawablePicts);
146 fCanvas->drawDrawable(fDrawables[r.index], r.matrix);
147 } else {
148 fCanvas->drawPicture(fDrawablePicts[r.index], r.matrix, nullptr);
149 }
150 }
151
152 // This is an SkRecord visitor that fills an SkBBoxHierarchy.
153 //
154 // The interesting part here is how to calculate bounds for ops which don't
155 // have intrinsic bounds. What is the bounds of a Save or a Translate?
156 //
157 // We answer this by thinking about a particular definition of bounds: if I
158 // don't execute this op, pixels in this rectangle might draw incorrectly. So
159 // the bounds of a Save, a Translate, a Restore, etc. are the union of the
160 // bounds of Draw* ops that they might have an effect on. For any given
161 // Save/Restore block, the bounds of the Save, the Restore, and any other
162 // non-drawing ("control") ops inside are exactly the union of the bounds of
163 // the drawing ops inside that block.
164 //
165 // To implement this, we keep a stack of active Save blocks. As we consume ops
166 // inside the Save/Restore block, drawing ops are unioned with the bounds of
167 // the block, and control ops are stashed away for later. When we finish the
168 // block with a Restore, our bounds are complete, and we go back and fill them
169 // in for all the control ops we stashed away.
170 class FillBounds : SkNoncopyable {
171 public:
FillBounds(const SkRect & cullRect,const SkRecord & record,SkRect bounds[])172 FillBounds(const SkRect& cullRect, const SkRecord& record, SkRect bounds[])
173 : fNumRecords(record.count())
174 , fCullRect(cullRect)
175 , fBounds(bounds) {
176 fCTM = SkMatrix::I();
177
178 // We push an extra save block to track the bounds of any top-level control operations.
179 fSaveStack.push_back({ 0, Bounds::MakeEmpty(), nullptr, fCTM });
180 }
181
cleanUp()182 void cleanUp() {
183 // If we have any lingering unpaired Saves, simulate restores to make
184 // sure all ops in those Save blocks have their bounds calculated.
185 while (!fSaveStack.isEmpty()) {
186 this->popSaveBlock();
187 }
188
189 // Any control ops not part of any Save/Restore block draw everywhere.
190 while (!fControlIndices.isEmpty()) {
191 this->popControl(fCullRect);
192 }
193 }
194
setCurrentOp(int currentOp)195 void setCurrentOp(int currentOp) { fCurrentOp = currentOp; }
196
197
operator ()(const T & op)198 template <typename T> void operator()(const T& op) {
199 this->updateCTM(op);
200 this->trackBounds(op);
201 }
202
203 // In this file, SkRect are in local coordinates, Bounds are translated back to identity space.
204 typedef SkRect Bounds;
205
currentOp() const206 int currentOp() const { return fCurrentOp; }
ctm() const207 const SkMatrix& ctm() const { return fCTM; }
getBounds(int index) const208 const Bounds& getBounds(int index) const { return fBounds[index]; }
209
210 // Adjust rect for all paints that may affect its geometry, then map it to identity space.
adjustAndMap(SkRect rect,const SkPaint * paint) const211 Bounds adjustAndMap(SkRect rect, const SkPaint* paint) const {
212 // Inverted rectangles really confuse our BBHs.
213 rect.sort();
214
215 // Adjust the rect for its own paint.
216 if (!AdjustForPaint(paint, &rect)) {
217 // The paint could do anything to our bounds. The only safe answer is the cull.
218 return fCullRect;
219 }
220
221 // Adjust rect for all the paints from the SaveLayers we're inside.
222 if (!this->adjustForSaveLayerPaints(&rect)) {
223 // Same deal as above.
224 return fCullRect;
225 }
226
227 // Map the rect back to identity space.
228 fCTM.mapRect(&rect);
229
230 // Nothing can draw outside the cull rect.
231 if (!rect.intersect(fCullRect)) {
232 return Bounds::MakeEmpty();
233 }
234
235 return rect;
236 }
237
238 private:
239 struct SaveBounds {
240 int controlOps; // Number of control ops in this Save block, including the Save.
241 Bounds bounds; // Bounds of everything in the block.
242 const SkPaint* paint; // Unowned. If set, adjusts the bounds of all ops in this block.
243 SkMatrix ctm;
244 };
245
246 // Only Restore, SetMatrix, Concat, and Translate change the CTM.
updateCTM(const T &)247 template <typename T> void updateCTM(const T&) {}
updateCTM(const Restore & op)248 void updateCTM(const Restore& op) { fCTM = op.matrix; }
updateCTM(const SetMatrix & op)249 void updateCTM(const SetMatrix& op) { fCTM = op.matrix; }
updateCTM(const Concat & op)250 void updateCTM(const Concat& op) { fCTM.preConcat(op.matrix); }
updateCTM(const Translate & op)251 void updateCTM(const Translate& op) { fCTM.preTranslate(op.dx, op.dy); }
252
253 // The bounds of these ops must be calculated when we hit the Restore
254 // from the bounds of the ops in the same Save block.
trackBounds(const Save &)255 void trackBounds(const Save&) { this->pushSaveBlock(nullptr); }
trackBounds(const SaveLayer & op)256 void trackBounds(const SaveLayer& op) { this->pushSaveBlock(op.paint); }
trackBounds(const SaveBehind &)257 void trackBounds(const SaveBehind&) { this->pushSaveBlock(nullptr); }
trackBounds(const Restore &)258 void trackBounds(const Restore&) { fBounds[fCurrentOp] = this->popSaveBlock(); }
259
trackBounds(const SetMatrix &)260 void trackBounds(const SetMatrix&) { this->pushControl(); }
trackBounds(const Concat &)261 void trackBounds(const Concat&) { this->pushControl(); }
trackBounds(const Translate &)262 void trackBounds(const Translate&) { this->pushControl(); }
trackBounds(const ClipRect &)263 void trackBounds(const ClipRect&) { this->pushControl(); }
trackBounds(const ClipRRect &)264 void trackBounds(const ClipRRect&) { this->pushControl(); }
trackBounds(const ClipPath &)265 void trackBounds(const ClipPath&) { this->pushControl(); }
trackBounds(const ClipRegion &)266 void trackBounds(const ClipRegion&) { this->pushControl(); }
267
268
269 // For all other ops, we can calculate and store the bounds directly now.
trackBounds(const T & op)270 template <typename T> void trackBounds(const T& op) {
271 fBounds[fCurrentOp] = this->bounds(op);
272 this->updateSaveBounds(fBounds[fCurrentOp]);
273 }
274
pushSaveBlock(const SkPaint * paint)275 void pushSaveBlock(const SkPaint* paint) {
276 // Starting a new Save block. Push a new entry to represent that.
277 SaveBounds sb;
278 sb.controlOps = 0;
279 // If the paint affects transparent black,
280 // the bound shouldn't be smaller than the cull.
281 sb.bounds =
282 PaintMayAffectTransparentBlack(paint) ? fCullRect : Bounds::MakeEmpty();
283 sb.paint = paint;
284 sb.ctm = this->fCTM;
285
286 fSaveStack.push_back(sb);
287 this->pushControl();
288 }
289
PaintMayAffectTransparentBlack(const SkPaint * paint)290 static bool PaintMayAffectTransparentBlack(const SkPaint* paint) {
291 if (paint) {
292 // FIXME: this is very conservative
293 if (paint->getImageFilter() || paint->getColorFilter()) {
294 return true;
295 }
296
297 // Unusual blendmodes require us to process a saved layer
298 // even with operations outisde the clip.
299 // For example, DstIn is used by masking layers.
300 // https://code.google.com/p/skia/issues/detail?id=1291
301 // https://crbug.com/401593
302 switch (paint->getBlendMode()) {
303 // For each of the following transfer modes, if the source
304 // alpha is zero (our transparent black), the resulting
305 // blended alpha is not necessarily equal to the original
306 // destination alpha.
307 case SkBlendMode::kClear:
308 case SkBlendMode::kSrc:
309 case SkBlendMode::kSrcIn:
310 case SkBlendMode::kDstIn:
311 case SkBlendMode::kSrcOut:
312 case SkBlendMode::kDstATop:
313 case SkBlendMode::kModulate:
314 return true;
315 break;
316 default:
317 break;
318 }
319 }
320 return false;
321 }
322
popSaveBlock()323 Bounds popSaveBlock() {
324 // We're done the Save block. Apply the block's bounds to all control ops inside it.
325 SaveBounds sb;
326 fSaveStack.pop(&sb);
327
328 while (sb.controlOps --> 0) {
329 this->popControl(sb.bounds);
330 }
331
332 // This whole Save block may be part another Save block.
333 this->updateSaveBounds(sb.bounds);
334
335 // If called from a real Restore (not a phony one for balance), it'll need the bounds.
336 return sb.bounds;
337 }
338
pushControl()339 void pushControl() {
340 fControlIndices.push_back(fCurrentOp);
341 if (!fSaveStack.isEmpty()) {
342 fSaveStack.top().controlOps++;
343 }
344 }
345
popControl(const Bounds & bounds)346 void popControl(const Bounds& bounds) {
347 fBounds[fControlIndices.top()] = bounds;
348 fControlIndices.pop();
349 }
350
updateSaveBounds(const Bounds & bounds)351 void updateSaveBounds(const Bounds& bounds) {
352 // If we're in a Save block, expand its bounds to cover these bounds too.
353 if (!fSaveStack.isEmpty()) {
354 fSaveStack.top().bounds.join(bounds);
355 }
356 }
357
bounds(const Flush &) const358 Bounds bounds(const Flush&) const { return fCullRect; }
359
bounds(const DrawPaint &) const360 Bounds bounds(const DrawPaint&) const { return fCullRect; }
bounds(const DrawBehind &) const361 Bounds bounds(const DrawBehind&) const { return fCullRect; }
bounds(const NoOp &) const362 Bounds bounds(const NoOp&) const { return Bounds::MakeEmpty(); } // NoOps don't draw.
363
bounds(const DrawRect & op) const364 Bounds bounds(const DrawRect& op) const { return this->adjustAndMap(op.rect, &op.paint); }
bounds(const DrawEdgeAARect & op) const365 Bounds bounds(const DrawEdgeAARect& op) const { return this->adjustAndMap(op.rect, nullptr); }
366
bounds(const DrawRegion & op) const367 Bounds bounds(const DrawRegion& op) const {
368 SkRect rect = SkRect::Make(op.region.getBounds());
369 return this->adjustAndMap(rect, &op.paint);
370 }
bounds(const DrawOval & op) const371 Bounds bounds(const DrawOval& op) const { return this->adjustAndMap(op.oval, &op.paint); }
372 // Tighter arc bounds?
bounds(const DrawArc & op) const373 Bounds bounds(const DrawArc& op) const { return this->adjustAndMap(op.oval, &op.paint); }
bounds(const DrawRRect & op) const374 Bounds bounds(const DrawRRect& op) const {
375 return this->adjustAndMap(op.rrect.rect(), &op.paint);
376 }
bounds(const DrawDRRect & op) const377 Bounds bounds(const DrawDRRect& op) const {
378 return this->adjustAndMap(op.outer.rect(), &op.paint);
379 }
bounds(const DrawImage & op) const380 Bounds bounds(const DrawImage& op) const {
381 const SkImage* image = op.image.get();
382 SkRect rect = SkRect::MakeXYWH(op.left, op.top, image->width(), image->height());
383
384 return this->adjustAndMap(rect, op.paint);
385 }
bounds(const DrawImageLattice & op) const386 Bounds bounds(const DrawImageLattice& op) const {
387 return this->adjustAndMap(op.dst, op.paint);
388 }
bounds(const DrawImageRect & op) const389 Bounds bounds(const DrawImageRect& op) const {
390 return this->adjustAndMap(op.dst, op.paint);
391 }
bounds(const DrawImageNine & op) const392 Bounds bounds(const DrawImageNine& op) const {
393 return this->adjustAndMap(op.dst, op.paint);
394 }
bounds(const DrawImageSet & op) const395 Bounds bounds(const DrawImageSet& op) const {
396 SkRect rect = SkRect::MakeEmpty();
397 for (int i = 0; i < op.count; ++i) {
398 rect.join(this->adjustAndMap(op.set[i].fDstRect, nullptr));
399 }
400 return rect;
401 }
bounds(const DrawPath & op) const402 Bounds bounds(const DrawPath& op) const {
403 return op.path.isInverseFillType() ? fCullRect
404 : this->adjustAndMap(op.path.getBounds(), &op.paint);
405 }
bounds(const DrawPoints & op) const406 Bounds bounds(const DrawPoints& op) const {
407 SkRect dst;
408 dst.set(op.pts, op.count);
409
410 // Pad the bounding box a little to make sure hairline points' bounds aren't empty.
411 SkScalar stroke = SkMaxScalar(op.paint.getStrokeWidth(), 0.01f);
412 dst.outset(stroke/2, stroke/2);
413
414 return this->adjustAndMap(dst, &op.paint);
415 }
bounds(const DrawPatch & op) const416 Bounds bounds(const DrawPatch& op) const {
417 SkRect dst;
418 dst.set(op.cubics, SkPatchUtils::kNumCtrlPts);
419 return this->adjustAndMap(dst, &op.paint);
420 }
bounds(const DrawVertices & op) const421 Bounds bounds(const DrawVertices& op) const {
422 return this->adjustAndMap(op.vertices->bounds(), &op.paint);
423 }
424
bounds(const DrawAtlas & op) const425 Bounds bounds(const DrawAtlas& op) const {
426 if (op.cull) {
427 // TODO: <reed> can we pass nullptr for the paint? Isn't cull already "correct"
428 // for the paint (by the caller)?
429 return this->adjustAndMap(*op.cull, op.paint);
430 } else {
431 return fCullRect;
432 }
433 }
434
bounds(const DrawShadowRec & op) const435 Bounds bounds(const DrawShadowRec& op) const {
436 SkRect bounds;
437 SkDrawShadowMetrics::GetLocalBounds(op.path, op.rec, fCTM, &bounds);
438 return this->adjustAndMap(bounds, nullptr);
439 }
440
bounds(const DrawPicture & op) const441 Bounds bounds(const DrawPicture& op) const {
442 SkRect dst = op.picture->cullRect();
443 op.matrix.mapRect(&dst);
444 return this->adjustAndMap(dst, op.paint);
445 }
446
bounds(const DrawTextBlob & op) const447 Bounds bounds(const DrawTextBlob& op) const {
448 SkRect dst = op.blob->bounds();
449 dst.offset(op.x, op.y);
450 return this->adjustAndMap(dst, &op.paint);
451 }
452
bounds(const DrawDrawable & op) const453 Bounds bounds(const DrawDrawable& op) const {
454 return this->adjustAndMap(op.worstCaseBounds, nullptr);
455 }
456
bounds(const DrawAnnotation & op) const457 Bounds bounds(const DrawAnnotation& op) const {
458 return this->adjustAndMap(op.rect, nullptr);
459 }
460
461 // Returns true if rect was meaningfully adjusted for the effects of paint,
462 // false if the paint could affect the rect in unknown ways.
AdjustForPaint(const SkPaint * paint,SkRect * rect)463 static bool AdjustForPaint(const SkPaint* paint, SkRect* rect) {
464 if (paint) {
465 if (paint->canComputeFastBounds()) {
466 *rect = paint->computeFastBounds(*rect, rect);
467 return true;
468 }
469 return false;
470 }
471 return true;
472 }
473
adjustForSaveLayerPaints(SkRect * rect,int savesToIgnore=0) const474 bool adjustForSaveLayerPaints(SkRect* rect, int savesToIgnore = 0) const {
475 for (int i = fSaveStack.count() - 1 - savesToIgnore; i >= 0; i--) {
476 SkMatrix inverse;
477 if (!fSaveStack[i].ctm.invert(&inverse)) {
478 return false;
479 }
480 inverse.mapRect(rect);
481 if (!AdjustForPaint(fSaveStack[i].paint, rect)) {
482 return false;
483 }
484 fSaveStack[i].ctm.mapRect(rect);
485 }
486 return true;
487 }
488
489 const int fNumRecords;
490
491 // We do not guarantee anything for operations outside of the cull rect
492 const SkRect fCullRect;
493
494 // Conservative identity-space bounds for each op in the SkRecord.
495 Bounds* fBounds;
496
497 // We walk fCurrentOp through the SkRecord,
498 // as we go using updateCTM() to maintain the exact CTM (fCTM).
499 int fCurrentOp;
500 SkMatrix fCTM;
501
502 // Used to track the bounds of Save/Restore blocks and the control ops inside them.
503 SkTDArray<SaveBounds> fSaveStack;
504 SkTDArray<int> fControlIndices;
505 };
506
507 } // namespace SkRecords
508
SkRecordFillBounds(const SkRect & cullRect,const SkRecord & record,SkRect bounds[])509 void SkRecordFillBounds(const SkRect& cullRect, const SkRecord& record, SkRect bounds[]) {
510 SkRecords::FillBounds visitor(cullRect, record, bounds);
511 for (int curOp = 0; curOp < record.count(); curOp++) {
512 visitor.setCurrentOp(curOp);
513 record.visit(curOp, visitor);
514 }
515 visitor.cleanUp();
516 }
517
518