Index: src/core/SkRecordDraw.cpp |
diff --git a/src/core/SkRecordDraw.cpp b/src/core/SkRecordDraw.cpp |
index ad1327016b82de3351ba410b4c4238a34218f452..51674905ab0b00bc5b33868084a9cfe1d6317430 100644 |
--- a/src/core/SkRecordDraw.cpp |
+++ b/src/core/SkRecordDraw.cpp |
@@ -124,433 +124,385 @@ DRAW(DrawData, drawData(r.data, r.length)); |
static const SkRect kUnbounded = { -2e9f, -2e9f, 2e9f, 2e9f }; |
-// This is an SkRecord visitor that fills an SkBBoxHierarchy. |
-// |
-// The interesting part here is how to calculate bounds for ops which don't |
-// have intrinsic bounds. What is the bounds of a Save or a Translate? |
-// |
-// We answer this by thinking about a particular definition of bounds: if I |
-// don't execute this op, pixels in this rectangle might draw incorrectly. So |
-// the bounds of a Save, a Translate, a Restore, etc. are the union of the |
-// bounds of Draw* ops that they might have an effect on. For any given |
-// Save/Restore block, the bounds of the Save, the Restore, and any other |
-// non-drawing ("control") ops inside are exactly the union of the bounds of |
-// the drawing ops inside that block. |
-// |
-// To implement this, we keep a stack of active Save blocks. As we consume ops |
-// inside the Save/Restore block, drawing ops are unioned with the bounds of |
-// the block, and control ops are stashed away for later. When we finish the |
-// block with a Restore, our bounds are complete, and we go back and fill them |
-// in for all the control ops we stashed away. |
-class FillBounds : SkNoncopyable { |
-public: |
- FillBounds(const SkRecord& record, SkBBoxHierarchy* bbh) : fBounds(record.count()) { |
- // Calculate bounds for all ops. This won't go quite in order, so we'll need |
- // to store the bounds separately then feed them in to the BBH later in order. |
- fCTM = &SkMatrix::I(); |
- fCurrentClipBounds = kUnbounded; |
- for (fCurrentOp = 0; fCurrentOp < record.count(); fCurrentOp++) { |
- record.visit<void>(fCurrentOp, *this); |
- } |
- |
- // If we have any lingering unpaired Saves, simulate restores to make |
- // sure all ops in those Save blocks have their bounds calculated. |
- while (!fSaveStack.isEmpty()) { |
- this->popSaveBlock(); |
- } |
- |
- // Any control ops not part of any Save/Restore block draw everywhere. |
- while (!fControlIndices.isEmpty()) { |
- this->popControl(kUnbounded); |
- } |
- |
- // Finally feed all stored bounds into the BBH. They'll be returned in this order. |
- SkASSERT(bbh); |
- bbh->insert(&fBounds, record.count()); |
+FillBounds::FillBounds(const SkRecord& record, SkBBoxHierarchy* bbh) : fBounds(record.count()) { |
+ // Calculate bounds for all ops. This won't go quite in order, so we'll need |
+ // to store the bounds separately then feed them in to the BBH later in order. |
+ fCTM = &SkMatrix::I(); |
+ fCurrentClipBounds = kUnbounded; |
+ for (fCurrentOp = 0; fCurrentOp < record.count(); fCurrentOp++) { |
+ record.visit<void>(fCurrentOp, *this); |
} |
- template <typename T> void operator()(const T& op) { |
- this->updateCTM(op); |
- this->updateClipBounds(op); |
- this->trackBounds(op); |
+ // If we have any lingering unpaired Saves, simulate restores to make |
+ // sure all ops in those Save blocks have their bounds calculated. |
+ while (!fSaveStack.isEmpty()) { |
+ this->popSaveBlock(); |
} |
-private: |
- // In this file, SkRect are in local coordinates, Bounds are translated back to identity space. |
- typedef SkRect Bounds; |
- |
- struct SaveBounds { |
- int controlOps; // Number of control ops in this Save block, including the Save. |
- Bounds bounds; // Bounds of everything in the block. |
- const SkPaint* paint; // Unowned. If set, adjusts the bounds of all ops in this block. |
- }; |
- |
- // Only Restore and SetMatrix change the CTM. |
- template <typename T> void updateCTM(const T&) {} |
- void updateCTM(const Restore& op) { fCTM = &op.matrix; } |
- void updateCTM(const SetMatrix& op) { fCTM = &op.matrix; } |
- |
- // Most ops don't change the clip. |
- template <typename T> void updateClipBounds(const T&) {} |
- |
- // Clip{Path,RRect,Rect,Region} obviously change the clip. They all know their bounds already. |
- void updateClipBounds(const ClipPath& op) { this->updateClipBoundsForClipOp(op.devBounds); } |
- void updateClipBounds(const ClipRRect& op) { this->updateClipBoundsForClipOp(op.devBounds); } |
- void updateClipBounds(const ClipRect& op) { this->updateClipBoundsForClipOp(op.devBounds); } |
- void updateClipBounds(const ClipRegion& op) { this->updateClipBoundsForClipOp(op.devBounds); } |
- |
- // The bounds of clip ops need to be adjusted for the paints of saveLayers they're inside. |
- void updateClipBoundsForClipOp(const SkIRect& devBounds) { |
- Bounds clip = SkRect::Make(devBounds); |
- // We don't call adjustAndMap() because as its last step it would intersect the adjusted |
- // clip bounds with the previous clip, exactly what we can't do when the clip grows. |
- fCurrentClipBounds = this->adjustForSaveLayerPaints(&clip) ? clip : kUnbounded; |
+ // Any control ops not part of any Save/Restore block draw everywhere. |
+ while (!fControlIndices.isEmpty()) { |
+ this->popControl(kUnbounded); |
} |
- // Restore holds the devBounds for the clip after the {save,saveLayer}/restore block completes. |
- void updateClipBounds(const Restore& op) { |
- // This is just like the clip ops above, but we need to skip the effects (if any) of our |
- // paired saveLayer (if it is one); it has not yet been popped off the save stack. Our |
- // devBounds reflect the state of the world after the saveLayer/restore block is done, |
- // so they are not affected by the saveLayer's paint. |
- const int kSavesToIgnore = 1; |
- Bounds clip = SkRect::Make(op.devBounds); |
- fCurrentClipBounds = |
- this->adjustForSaveLayerPaints(&clip, kSavesToIgnore) ? clip : kUnbounded; |
+ // Finally feed all stored bounds into the BBH. They'll be returned in this order. |
+ // TODO: resume use of the assert once saveLayer collection is moved into SkPicture ctor |
+ //SkASSERT(bbh); |
+ if (bbh) { |
mtklein
2014/11/03 15:56:46
Is this an indication that we really want this to
robertphillips
2014/11/03 16:49:18
Ultimately, I think that whenever we see the colle
|
+ bbh->insert(&fBounds, record.count()); |
} |
+} |
- // We also take advantage of SaveLayer bounds when present to further cut the clip down. |
- void updateClipBounds(const SaveLayer& op) { |
- if (op.bounds) { |
- // adjustAndMap() intersects these layer bounds with the previous clip for us. |
- fCurrentClipBounds = this->adjustAndMap(*op.bounds, op.paint); |
- } |
- } |
+// Only Restore and SetMatrix change the CTM. |
+template <typename T> void FillBounds::updateCTM(const T&) {} |
+template <> void FillBounds::updateCTM(const Restore& op) { fCTM = &op.matrix; } |
+template <> void FillBounds::updateCTM(const SetMatrix& op) { fCTM = &op.matrix; } |
+ |
+// Most ops don't change the clip. |
+template <typename T> void FillBounds::updateClipBounds(const T&) {} |
+ |
+// Clip{Path,RRect,Rect,Region} obviously change the clip. They all know their bounds already. |
+template <> void FillBounds::updateClipBounds(const ClipPath& op) { this->updateClipBoundsForClipOp(op.devBounds); } |
+template <> void FillBounds::updateClipBounds(const ClipRRect& op) { this->updateClipBoundsForClipOp(op.devBounds); } |
+template <> void FillBounds::updateClipBounds(const ClipRect& op) { this->updateClipBoundsForClipOp(op.devBounds); } |
+template <> void FillBounds::updateClipBounds(const ClipRegion& op) { this->updateClipBoundsForClipOp(op.devBounds); } |
+ |
+// The bounds of clip ops need to be adjusted for the paints of saveLayers they're inside. |
+void FillBounds::updateClipBoundsForClipOp(const SkIRect& devBounds) { |
+ Bounds clip = SkRect::Make(devBounds); |
+ // We don't call adjustAndMap() because as its last step it would intersect the adjusted |
+ // clip bounds with the previous clip, exactly what we can't do when the clip grows. |
+ fCurrentClipBounds = this->adjustForSaveLayerPaints(&clip) ? clip : kUnbounded; |
+} |
- // The bounds of these ops must be calculated when we hit the Restore |
- // from the bounds of the ops in the same Save block. |
- void trackBounds(const Save&) { this->pushSaveBlock(NULL); } |
- void trackBounds(const SaveLayer& op) { this->pushSaveBlock(op.paint); } |
- void trackBounds(const Restore&) { fBounds[fCurrentOp] = this->popSaveBlock(); } |
- |
- void trackBounds(const SetMatrix&) { this->pushControl(); } |
- void trackBounds(const ClipRect&) { this->pushControl(); } |
- void trackBounds(const ClipRRect&) { this->pushControl(); } |
- void trackBounds(const ClipPath&) { this->pushControl(); } |
- void trackBounds(const ClipRegion&) { this->pushControl(); } |
- void trackBounds(const PushCull&) { this->pushControl(); } |
- void trackBounds(const PopCull&) { this->pushControl(); } |
- void trackBounds(const BeginCommentGroup&) { this->pushControl(); } |
- void trackBounds(const AddComment&) { this->pushControl(); } |
- void trackBounds(const EndCommentGroup&) { this->pushControl(); } |
- void trackBounds(const DrawData&) { this->pushControl(); } |
- |
- // For all other ops, we can calculate and store the bounds directly now. |
- template <typename T> void trackBounds(const T& op) { |
- fBounds[fCurrentOp] = this->bounds(op); |
- this->updateSaveBounds(fBounds[fCurrentOp]); |
- } |
+// Restore holds the devBounds for the clip after the {save,saveLayer}/restore block completes. |
+template <> void FillBounds::updateClipBounds(const Restore& op) { |
+ // This is just like the clip ops above, but we need to skip the effects (if any) of our |
+ // paired saveLayer (if it is one); it has not yet been popped off the save stack. Our |
+ // devBounds reflect the state of the world after the saveLayer/restore block is done, |
+ // so they are not affected by the saveLayer's paint. |
+ const int kSavesToIgnore = 1; |
+ Bounds clip = SkRect::Make(op.devBounds); |
+ fCurrentClipBounds = |
+ this->adjustForSaveLayerPaints(&clip, kSavesToIgnore) ? clip : kUnbounded; |
+} |
- void pushSaveBlock(const SkPaint* paint) { |
- // Starting a new Save block. Push a new entry to represent that. |
- SaveBounds sb = { 0, Bounds::MakeEmpty(), paint }; |
- fSaveStack.push(sb); |
- this->pushControl(); |
+// We also take advantage of SaveLayer bounds when present to further cut the clip down. |
+template <> void FillBounds::updateClipBounds(const SaveLayer& op) { |
+ if (op.bounds) { |
+ // adjustAndMap() intersects these layer bounds with the previous clip for us. |
+ fCurrentClipBounds = this->adjustAndMap(*op.bounds, op.paint); |
} |
+} |
- static bool PaintMayAffectTransparentBlack(const SkPaint* paint) { |
- if (paint) { |
- // FIXME: this is very conservative |
- if (paint->getImageFilter() || paint->getColorFilter()) { |
- return true; |
- } |
+// The bounds of these ops must be calculated when we hit the Restore |
+// from the bounds of the ops in the same Save block. |
+template <> void FillBounds::trackBounds(const Save&) { this->pushSaveBlock(NULL); } |
+template <> void FillBounds::trackBounds(const SaveLayer& op) { this->pushSaveBlock(op.paint); } |
+template <> void FillBounds::trackBounds(const Restore&) { fBounds[fCurrentOp] = this->popSaveBlock(); } |
+ |
+template <> void FillBounds::trackBounds(const SetMatrix&) { this->pushControl(); } |
+template <> void FillBounds::trackBounds(const ClipRect&) { this->pushControl(); } |
+template <> void FillBounds::trackBounds(const ClipRRect&) { this->pushControl(); } |
+template <> void FillBounds::trackBounds(const ClipPath&) { this->pushControl(); } |
+template <> void FillBounds::trackBounds(const ClipRegion&) { this->pushControl(); } |
+template <> void FillBounds::trackBounds(const PushCull&) { this->pushControl(); } |
+template <> void FillBounds::trackBounds(const PopCull&) { this->pushControl(); } |
+template <> void FillBounds::trackBounds(const BeginCommentGroup&) { this->pushControl(); } |
+template <> void FillBounds::trackBounds(const AddComment&) { this->pushControl(); } |
+template <> void FillBounds::trackBounds(const EndCommentGroup&) { this->pushControl(); } |
+template <> void FillBounds::trackBounds(const DrawData&) { this->pushControl(); } |
+ |
+// For all other ops, we can calculate and store the bounds directly now. |
+template <typename T> void FillBounds::trackBounds(const T& op) { |
+ fBounds[fCurrentOp] = this->bounds(op); |
+ this->updateSaveBounds(fBounds[fCurrentOp]); |
+} |
+ |
+void FillBounds::pushSaveBlock(const SkPaint* paint) { |
+ // Starting a new Save block. Push a new entry to represent that. |
+ SaveBounds sb = { 0, Bounds::MakeEmpty(), paint }; |
+ fSaveStack.push(sb); |
+ this->pushControl(); |
+} |
+ |
+bool FillBounds::PaintMayAffectTransparentBlack(const SkPaint* paint) { |
+ if (paint) { |
+ // FIXME: this is very conservative |
+ if (paint->getImageFilter() || paint->getColorFilter()) { |
+ return true; |
+ } |
- // Unusual Xfermodes require us to process a saved layer |
- // even with operations outisde the clip. |
- // For example, DstIn is used by masking layers. |
- // https://code.google.com/p/skia/issues/detail?id=1291 |
- // https://crbug.com/401593 |
- SkXfermode* xfermode = paint->getXfermode(); |
- SkXfermode::Mode mode; |
- // SrcOver is ok, and is also the common case with a NULL xfermode. |
- // So we should make that the fast path and bypass the mode extraction |
- // and test. |
- if (xfermode && xfermode->asMode(&mode)) { |
- switch (mode) { |
- // For each of the following transfer modes, if the source |
- // alpha is zero (our transparent black), the resulting |
- // blended alpha is not necessarily equal to the original |
- // destination alpha. |
- case SkXfermode::kClear_Mode: |
- case SkXfermode::kSrc_Mode: |
- case SkXfermode::kSrcIn_Mode: |
- case SkXfermode::kDstIn_Mode: |
- case SkXfermode::kSrcOut_Mode: |
- case SkXfermode::kDstATop_Mode: |
- case SkXfermode::kModulate_Mode: |
- return true; |
- break; |
- default: |
- break; |
- } |
+ // Unusual Xfermodes require us to process a saved layer |
+ // even with operations outisde the clip. |
+ // For example, DstIn is used by masking layers. |
+ // https://code.google.com/p/skia/issues/detail?id=1291 |
+ // https://crbug.com/401593 |
+ SkXfermode* xfermode = paint->getXfermode(); |
+ SkXfermode::Mode mode; |
+ // SrcOver is ok, and is also the common case with a NULL xfermode. |
+ // So we should make that the fast path and bypass the mode extraction |
+ // and test. |
+ if (xfermode && xfermode->asMode(&mode)) { |
+ switch (mode) { |
+ // For each of the following transfer modes, if the source |
+ // alpha is zero (our transparent black), the resulting |
+ // blended alpha is not necessarily equal to the original |
+ // destination alpha. |
+ case SkXfermode::kClear_Mode: |
+ case SkXfermode::kSrc_Mode: |
+ case SkXfermode::kSrcIn_Mode: |
+ case SkXfermode::kDstIn_Mode: |
+ case SkXfermode::kSrcOut_Mode: |
+ case SkXfermode::kDstATop_Mode: |
+ case SkXfermode::kModulate_Mode: |
+ return true; |
+ break; |
+ default: |
+ break; |
} |
} |
- return false; |
} |
+ return false; |
+} |
- Bounds popSaveBlock() { |
- // We're done the Save block. Apply the block's bounds to all control ops inside it. |
- SaveBounds sb; |
- fSaveStack.pop(&sb); |
+FillBounds::Bounds FillBounds::popSaveBlock() { |
+ // We're done the Save block. Apply the block's bounds to all control ops inside it. |
+ SaveBounds sb; |
+ fSaveStack.pop(&sb); |
- // If the paint affects transparent black, we can't trust any of our calculated bounds. |
- const Bounds& bounds = |
- PaintMayAffectTransparentBlack(sb.paint) ? fCurrentClipBounds : sb.bounds; |
+ // If the paint affects transparent black, we can't trust any of our calculated bounds. |
+ const Bounds& bounds = |
+ PaintMayAffectTransparentBlack(sb.paint) ? fCurrentClipBounds : sb.bounds; |
- while (sb.controlOps --> 0) { |
- this->popControl(bounds); |
- } |
+ while (sb.controlOps --> 0) { |
+ this->popControl(bounds); |
+ } |
- // This whole Save block may be part another Save block. |
- this->updateSaveBounds(bounds); |
+ // This whole Save block may be part another Save block. |
+ this->updateSaveBounds(bounds); |
- // If called from a real Restore (not a phony one for balance), it'll need the bounds. |
- return bounds; |
- } |
+ // If called from a real Restore (not a phony one for balance), it'll need the bounds. |
+ return bounds; |
+} |
- void pushControl() { |
- fControlIndices.push(fCurrentOp); |
- if (!fSaveStack.isEmpty()) { |
- fSaveStack.top().controlOps++; |
- } |
+void FillBounds::pushControl() { |
+ fControlIndices.push(fCurrentOp); |
+ if (!fSaveStack.isEmpty()) { |
+ fSaveStack.top().controlOps++; |
} |
+} |
- void popControl(const Bounds& bounds) { |
- fBounds[fControlIndices.top()] = bounds; |
- fControlIndices.pop(); |
- } |
+void FillBounds::popControl(const Bounds& bounds) { |
+ fBounds[fControlIndices.top()] = bounds; |
+ fControlIndices.pop(); |
+} |
- void updateSaveBounds(const Bounds& bounds) { |
- // If we're in a Save block, expand its bounds to cover these bounds too. |
- if (!fSaveStack.isEmpty()) { |
- fSaveStack.top().bounds.join(bounds); |
- } |
+void FillBounds::updateSaveBounds(const Bounds& bounds) { |
+ // If we're in a Save block, expand its bounds to cover these bounds too. |
+ if (!fSaveStack.isEmpty()) { |
+ fSaveStack.top().bounds.join(bounds); |
} |
+} |
- // FIXME: this method could use better bounds |
- Bounds bounds(const DrawText&) const { return fCurrentClipBounds; } |
+// FIXME: this method could use better bounds |
+template <> FillBounds::Bounds FillBounds::bounds(const DrawText&) const { return fCurrentClipBounds; } |
- Bounds bounds(const Clear&) const { return kUnbounded; } // Ignores the clip. |
- Bounds bounds(const DrawPaint&) const { return fCurrentClipBounds; } |
- Bounds bounds(const NoOp&) const { return Bounds::MakeEmpty(); } // NoOps don't draw. |
+template <> FillBounds::Bounds FillBounds::bounds(const Clear&) const { return kUnbounded; } // Ignores the clip. |
+template <> FillBounds::Bounds FillBounds::bounds(const DrawPaint&) const { return fCurrentClipBounds; } |
+template <> FillBounds::Bounds FillBounds::bounds(const NoOp&) const { return Bounds::MakeEmpty(); } // NoOps don't draw. |
- Bounds bounds(const DrawSprite& op) const { |
- const SkBitmap& bm = op.bitmap; |
- return Bounds::MakeXYWH(op.left, op.top, bm.width(), bm.height()); // Ignores the matrix. |
- } |
+template <> FillBounds::Bounds FillBounds::bounds(const DrawSprite& op) const { |
+ const SkBitmap& bm = op.bitmap; |
+ return Bounds::MakeXYWH(op.left, op.top, bm.width(), bm.height()); // Ignores the matrix. |
+} |
- Bounds bounds(const DrawRect& op) const { return this->adjustAndMap(op.rect, &op.paint); } |
- Bounds bounds(const DrawOval& op) const { return this->adjustAndMap(op.oval, &op.paint); } |
- Bounds bounds(const DrawRRect& op) const { |
- return this->adjustAndMap(op.rrect.rect(), &op.paint); |
- } |
- Bounds bounds(const DrawDRRect& op) const { |
- return this->adjustAndMap(op.outer.rect(), &op.paint); |
- } |
- Bounds bounds(const DrawImage& op) const { |
- const SkImage* image = op.image; |
- SkRect rect = SkRect::MakeXYWH(op.left, op.top, image->width(), image->height()); |
+template <> FillBounds::Bounds FillBounds::bounds(const DrawRect& op) const { return this->adjustAndMap(op.rect, &op.paint); } |
+template <> FillBounds::Bounds FillBounds::bounds(const DrawOval& op) const { return this->adjustAndMap(op.oval, &op.paint); } |
+template <> FillBounds::Bounds FillBounds::bounds(const DrawRRect& op) const { |
+ return this->adjustAndMap(op.rrect.rect(), &op.paint); |
+} |
+template <> FillBounds::Bounds FillBounds::bounds(const DrawDRRect& op) const { |
+ return this->adjustAndMap(op.outer.rect(), &op.paint); |
+} |
+template <> FillBounds::Bounds FillBounds::bounds(const DrawImage& op) const { |
+ const SkImage* image = op.image; |
+ SkRect rect = SkRect::MakeXYWH(op.left, op.top, image->width(), image->height()); |
- return this->adjustAndMap(rect, op.paint); |
- } |
- Bounds bounds(const DrawImageRect& op) const { |
- return this->adjustAndMap(op.dst, op.paint); |
- } |
- Bounds bounds(const DrawBitmapRectToRect& op) const { |
- return this->adjustAndMap(op.dst, op.paint); |
- } |
- Bounds bounds(const DrawBitmapNine& op) const { |
- return this->adjustAndMap(op.dst, op.paint); |
- } |
- Bounds bounds(const DrawBitmap& op) const { |
- const SkBitmap& bm = op.bitmap; |
- return this->adjustAndMap(SkRect::MakeXYWH(op.left, op.top, bm.width(), bm.height()), |
- op.paint); |
- } |
- Bounds bounds(const DrawBitmapMatrix& op) const { |
- const SkBitmap& bm = op.bitmap; |
- SkRect dst = SkRect::MakeWH(bm.width(), bm.height()); |
- op.matrix.mapRect(&dst); |
- return this->adjustAndMap(dst, op.paint); |
- } |
+ return this->adjustAndMap(rect, op.paint); |
+} |
+template <> FillBounds::Bounds FillBounds::bounds(const DrawImageRect& op) const { |
+ return this->adjustAndMap(op.dst, op.paint); |
+} |
+template <> FillBounds::Bounds FillBounds::bounds(const DrawBitmapRectToRect& op) const { |
+ return this->adjustAndMap(op.dst, op.paint); |
+} |
+template <> FillBounds::Bounds FillBounds::bounds(const DrawBitmapNine& op) const { |
+ return this->adjustAndMap(op.dst, op.paint); |
+} |
+template <> FillBounds::Bounds FillBounds::bounds(const DrawBitmap& op) const { |
+ const SkBitmap& bm = op.bitmap; |
+ return this->adjustAndMap(SkRect::MakeXYWH(op.left, op.top, bm.width(), bm.height()), |
+ op.paint); |
+} |
+template <> FillBounds::Bounds FillBounds::bounds(const DrawBitmapMatrix& op) const { |
+ const SkBitmap& bm = op.bitmap; |
+ SkRect dst = SkRect::MakeWH(bm.width(), bm.height()); |
+ op.matrix.mapRect(&dst); |
+ return this->adjustAndMap(dst, op.paint); |
+} |
- Bounds bounds(const DrawPath& op) const { |
- return op.path.isInverseFillType() ? fCurrentClipBounds |
- : this->adjustAndMap(op.path.getBounds(), &op.paint); |
- } |
- Bounds bounds(const DrawPoints& op) const { |
- SkRect dst; |
- dst.set(op.pts, op.count); |
+template <> FillBounds::Bounds FillBounds::bounds(const DrawPath& op) const { |
+ return op.path.isInverseFillType() ? fCurrentClipBounds |
+ : this->adjustAndMap(op.path.getBounds(), &op.paint); |
+} |
+template <> FillBounds::Bounds FillBounds::bounds(const DrawPoints& op) const { |
+ SkRect dst; |
+ dst.set(op.pts, op.count); |
- // Pad the bounding box a little to make sure hairline points' bounds aren't empty. |
- SkScalar stroke = SkMaxScalar(op.paint.getStrokeWidth(), 0.01f); |
- dst.outset(stroke/2, stroke/2); |
+ // Pad the bounding box a little to make sure hairline points' bounds aren't empty. |
+ SkScalar stroke = SkMaxScalar(op.paint.getStrokeWidth(), 0.01f); |
+ dst.outset(stroke/2, stroke/2); |
- return this->adjustAndMap(dst, &op.paint); |
- } |
- Bounds bounds(const DrawPatch& op) const { |
- SkRect dst; |
- dst.set(op.cubics, SkPatchUtils::kNumCtrlPts); |
- return this->adjustAndMap(dst, &op.paint); |
- } |
- Bounds bounds(const DrawVertices& op) const { |
- SkRect dst; |
- dst.set(op.vertices, op.vertexCount); |
- return this->adjustAndMap(dst, &op.paint); |
- } |
+ return this->adjustAndMap(dst, &op.paint); |
+} |
+template <> FillBounds::Bounds FillBounds::bounds(const DrawPatch& op) const { |
+ SkRect dst; |
+ dst.set(op.cubics, SkPatchUtils::kNumCtrlPts); |
+ return this->adjustAndMap(dst, &op.paint); |
+} |
+template <> FillBounds::Bounds FillBounds::bounds(const DrawVertices& op) const { |
+ SkRect dst; |
+ dst.set(op.vertices, op.vertexCount); |
+ return this->adjustAndMap(dst, &op.paint); |
+} |
- Bounds bounds(const DrawPicture& op) const { |
- SkRect dst = op.picture->cullRect(); |
- if (op.matrix) { |
- op.matrix->mapRect(&dst); |
- } |
- return this->adjustAndMap(dst, op.paint); |
+template <> FillBounds::Bounds FillBounds::bounds(const DrawPicture& op) const { |
+ SkRect dst = op.picture->cullRect(); |
+ if (op.matrix) { |
+ op.matrix->mapRect(&dst); |
} |
+ return this->adjustAndMap(dst, op.paint); |
+} |
- Bounds bounds(const DrawPosText& op) const { |
- const int N = op.paint.countText(op.text, op.byteLength); |
- if (N == 0) { |
- return Bounds::MakeEmpty(); |
- } |
+template <> FillBounds::Bounds FillBounds::bounds(const DrawPosText& op) const { |
+ const int N = op.paint.countText(op.text, op.byteLength); |
+ if (N == 0) { |
+ return Bounds::MakeEmpty(); |
+ } |
- SkRect dst; |
- dst.set(op.pos, N); |
- AdjustTextForFontMetrics(&dst, op.paint); |
- return this->adjustAndMap(dst, &op.paint); |
+ SkRect dst; |
+ dst.set(op.pos, N); |
+ AdjustTextForFontMetrics(&dst, op.paint); |
+ return this->adjustAndMap(dst, &op.paint); |
+} |
+template <> FillBounds::Bounds FillBounds::bounds(const DrawPosTextH& op) const { |
+ const int N = op.paint.countText(op.text, op.byteLength); |
+ if (N == 0) { |
+ return Bounds::MakeEmpty(); |
} |
- Bounds bounds(const DrawPosTextH& op) const { |
- const int N = op.paint.countText(op.text, op.byteLength); |
- if (N == 0) { |
- return Bounds::MakeEmpty(); |
- } |
- SkScalar left = op.xpos[0], right = op.xpos[0]; |
- for (int i = 1; i < N; i++) { |
- left = SkMinScalar(left, op.xpos[i]); |
- right = SkMaxScalar(right, op.xpos[i]); |
- } |
- SkRect dst = { left, op.y, right, op.y }; |
- AdjustTextForFontMetrics(&dst, op.paint); |
- return this->adjustAndMap(dst, &op.paint); |
+ SkScalar left = op.xpos[0], right = op.xpos[0]; |
+ for (int i = 1; i < N; i++) { |
+ left = SkMinScalar(left, op.xpos[i]); |
+ right = SkMaxScalar(right, op.xpos[i]); |
} |
- Bounds bounds(const DrawTextOnPath& op) const { |
- SkRect dst = op.path.getBounds(); |
+ SkRect dst = { left, op.y, right, op.y }; |
+ AdjustTextForFontMetrics(&dst, op.paint); |
+ return this->adjustAndMap(dst, &op.paint); |
+} |
+template <> FillBounds::Bounds FillBounds::bounds(const DrawTextOnPath& op) const { |
+ SkRect dst = op.path.getBounds(); |
- // Pad all sides by the maximum padding in any direction we'd normally apply. |
- SkRect pad = { 0, 0, 0, 0}; |
- AdjustTextForFontMetrics(&pad, op.paint); |
+ // Pad all sides by the maximum padding in any direction we'd normally apply. |
+ SkRect pad = { 0, 0, 0, 0}; |
+ AdjustTextForFontMetrics(&pad, op.paint); |
- // That maximum padding happens to always be the right pad today. |
- SkASSERT(pad.fLeft == -pad.fRight); |
- SkASSERT(pad.fTop == -pad.fBottom); |
- SkASSERT(pad.fRight > pad.fBottom); |
- dst.outset(pad.fRight, pad.fRight); |
+ // That maximum padding happens to always be the right pad today. |
+ SkASSERT(pad.fLeft == -pad.fRight); |
+ SkASSERT(pad.fTop == -pad.fBottom); |
+ SkASSERT(pad.fRight > pad.fBottom); |
+ dst.outset(pad.fRight, pad.fRight); |
- return this->adjustAndMap(dst, &op.paint); |
- } |
+ return this->adjustAndMap(dst, &op.paint); |
+} |
- Bounds bounds(const DrawTextBlob& op) const { |
- SkRect dst = op.blob->bounds(); |
- dst.offset(op.x, op.y); |
- return this->adjustAndMap(dst, &op.paint); |
- } |
+template <> FillBounds::Bounds FillBounds::bounds(const DrawTextBlob& op) const { |
+ SkRect dst = op.blob->bounds(); |
+ dst.offset(op.x, op.y); |
+ return this->adjustAndMap(dst, &op.paint); |
+} |
- static void AdjustTextForFontMetrics(SkRect* rect, const SkPaint& paint) { |
+void FillBounds::AdjustTextForFontMetrics(SkRect* rect, const SkPaint& paint) { |
#ifdef SK_DEBUG |
- SkRect correct = *rect; |
+ SkRect correct = *rect; |
#endif |
- // crbug.com/373785 ~~> xPad = 4x yPad |
- // crbug.com/424824 ~~> bump yPad from 2x text size to 2.5x |
- const SkScalar yPad = 2.5f * paint.getTextSize(), |
- xPad = 4.0f * yPad; |
- rect->outset(xPad, yPad); |
+ // crbug.com/373785 ~~> xPad = 4x yPad |
+ // crbug.com/424824 ~~> bump yPad from 2x text size to 2.5x |
+ const SkScalar yPad = 2.5f * paint.getTextSize(), |
+ xPad = 4.0f * yPad; |
+ rect->outset(xPad, yPad); |
#ifdef SK_DEBUG |
- SkPaint::FontMetrics metrics; |
- paint.getFontMetrics(&metrics); |
- correct.fLeft += metrics.fXMin; |
- correct.fTop += metrics.fTop; |
- correct.fRight += metrics.fXMax; |
- correct.fBottom += metrics.fBottom; |
- // See skia:2862 for why we ignore small text sizes. |
- SkASSERTF(paint.getTextSize() < 0.001f || rect->contains(correct), |
- "%f %f %f %f vs. %f %f %f %f\n", |
- -xPad, -yPad, +xPad, +yPad, |
- metrics.fXMin, metrics.fTop, metrics.fXMax, metrics.fBottom); |
+ SkPaint::FontMetrics metrics; |
+ paint.getFontMetrics(&metrics); |
+ correct.fLeft += metrics.fXMin; |
+ correct.fTop += metrics.fTop; |
+ correct.fRight += metrics.fXMax; |
+ correct.fBottom += metrics.fBottom; |
+ // See skia:2862 for why we ignore small text sizes. |
+ SkASSERTF(paint.getTextSize() < 0.001f || rect->contains(correct), |
+ "%f %f %f %f vs. %f %f %f %f\n", |
+ -xPad, -yPad, +xPad, +yPad, |
+ metrics.fXMin, metrics.fTop, metrics.fXMax, metrics.fBottom); |
#endif |
- } |
+} |
- // Returns true if rect was meaningfully adjusted for the effects of paint, |
- // false if the paint could affect the rect in unknown ways. |
- static bool AdjustForPaint(const SkPaint* paint, SkRect* rect) { |
- if (paint) { |
- if (paint->canComputeFastBounds()) { |
- *rect = paint->computeFastBounds(*rect, rect); |
- return true; |
- } |
- return false; |
+// Returns true if rect was meaningfully adjusted for the effects of paint, |
+// false if the paint could affect the rect in unknown ways. |
+bool FillBounds::AdjustForPaint(const SkPaint* paint, SkRect* rect) { |
+ if (paint) { |
+ if (paint->canComputeFastBounds()) { |
+ *rect = paint->computeFastBounds(*rect, rect); |
+ return true; |
} |
- return true; |
+ return false; |
} |
+ return true; |
+} |
- bool adjustForSaveLayerPaints(SkRect* rect, int savesToIgnore = 0) const { |
- for (int i = fSaveStack.count() - 1 - savesToIgnore; i >= 0; i--) { |
- if (!AdjustForPaint(fSaveStack[i].paint, rect)) { |
- return false; |
- } |
+bool FillBounds::adjustForSaveLayerPaints(SkRect* rect, int savesToIgnore) const { |
+ for (int i = fSaveStack.count() - 1 - savesToIgnore; i >= 0; i--) { |
+ if (!AdjustForPaint(fSaveStack[i].paint, rect)) { |
+ return false; |
} |
- return true; |
} |
+ return true; |
+} |
- // Adjust rect for all paints that may affect its geometry, then map it to identity space. |
- Bounds adjustAndMap(SkRect rect, const SkPaint* paint) const { |
- // Inverted rectangles really confuse our BBHs. |
- rect.sort(); |
- |
- // Adjust the rect for its own paint. |
- if (!AdjustForPaint(paint, &rect)) { |
- // The paint could do anything to our bounds. The only safe answer is the current clip. |
- return fCurrentClipBounds; |
- } |
- |
- // Adjust rect for all the paints from the SaveLayers we're inside. |
- if (!this->adjustForSaveLayerPaints(&rect)) { |
- // Same deal as above. |
- return fCurrentClipBounds; |
- } |
- |
- // Map the rect back to identity space. |
- fCTM->mapRect(&rect); |
+// Adjust rect for all paints that may affect its geometry, then map it to identity space. |
+FillBounds::Bounds FillBounds::adjustAndMap(SkRect rect, const SkPaint* paint) const { |
+ // Inverted rectangles really confuse our BBHs. |
+ rect.sort(); |
- // Nothing can draw outside the current clip. |
- // (Only bounded ops call into this method, so oddballs like Clear don't matter here.) |
- rect.intersect(fCurrentClipBounds); |
- return rect; |
+ // Adjust the rect for its own paint. |
+ if (!AdjustForPaint(paint, &rect)) { |
+ // The paint could do anything to our bounds. The only safe answer is the current clip. |
+ return fCurrentClipBounds; |
} |
- // Conservative identity-space bounds for each op in the SkRecord. |
- SkAutoTMalloc<Bounds> fBounds; |
+ // Adjust rect for all the paints from the SaveLayers we're inside. |
+ if (!this->adjustForSaveLayerPaints(&rect)) { |
+ // Same deal as above. |
+ return fCurrentClipBounds; |
+ } |
- // We walk fCurrentOp through the SkRecord, as we go using updateCTM() |
- // and updateClipBounds() to maintain the exact CTM (fCTM) and conservative |
- // identity-space bounds of the current clip (fCurrentClipBounds). |
- unsigned fCurrentOp; |
- const SkMatrix* fCTM; |
- Bounds fCurrentClipBounds; |
+ // Map the rect back to identity space. |
+ fCTM->mapRect(&rect); |
- // Used to track the bounds of Save/Restore blocks and the control ops inside them. |
- SkTDArray<SaveBounds> fSaveStack; |
- SkTDArray<unsigned> fControlIndices; |
-}; |
+ // Nothing can draw outside the current clip. |
+ // (Only bounded ops call into this method, so oddballs like Clear don't matter here.) |
+ rect.intersect(fCurrentClipBounds); |
+ return rect; |
+} |
} // namespace SkRecords |