add optional cull-rect to patheffects, so they can do less work if their results
lie outside of the current clip-bounds (the cull rect).
Review URL: https://codereview.appspot.com/7206044
git-svn-id: http://skia.googlecode.com/svn/trunk@7378 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/src/core/SkDraw.cpp b/src/core/SkDraw.cpp
index 45a023b..87f117b 100644
--- a/src/core/SkDraw.cpp
+++ b/src/core/SkDraw.cpp
@@ -122,6 +122,23 @@
memcpy(this, &src, sizeof(*this));
}
+bool SkDraw::computeConservativeLocalClipBounds(SkRect* localBounds) const {
+ if (fRC->isEmpty()) {
+ return false;
+ }
+
+ SkMatrix inverse;
+ if (!fMatrix->invert(&inverse)) {
+ return false;
+ }
+
+ SkIRect devBounds = fRC->getBounds();
+ // outset to have slop for antialasing and hairlines
+ devBounds.outset(1, 1);
+ inverse.mapRect(localBounds, SkRect::Make(devBounds));
+ return true;
+}
+
///////////////////////////////////////////////////////////////////////////////
typedef void (*BitmapXferProc)(void* pixels, size_t bytes, uint32_t data);
@@ -668,7 +685,10 @@
path.moveTo(pts[0]);
path.lineTo(pts[1]);
- if (paint.getPathEffect()->asPoints(&pointData, path, rec, *fMatrix)) {
+ SkRect cullRect = SkRect::Make(fRC->getBounds());
+
+ if (paint.getPathEffect()->asPoints(&pointData, path, rec,
+ *fMatrix, &cullRect)) {
// 'asPoints' managed to find some fast path
SkPaint newP(paint);
@@ -1057,7 +1077,12 @@
}
if (paint->getPathEffect() || paint->getStyle() != SkPaint::kFill_Style) {
- doFill = paint->getFillPath(*pathPtr, &tmpPath);
+ SkRect cullRect;
+ const SkRect* cullRectPtr = NULL;
+ if (this->computeConservativeLocalClipBounds(&cullRect)) {
+ cullRectPtr = &cullRect;
+ }
+ doFill = paint->getFillPath(*pathPtr, &tmpPath, cullRectPtr);
pathPtr = &tmpPath;
}
@@ -2765,3 +2790,4 @@
return true;
}
+
diff --git a/src/core/SkPaint.cpp b/src/core/SkPaint.cpp
index 8f14ba8..71a68ef 100644
--- a/src/core/SkPaint.cpp
+++ b/src/core/SkPaint.cpp
@@ -2145,13 +2145,14 @@
///////////////////////////////////////////////////////////////////////////////
-bool SkPaint::getFillPath(const SkPath& src, SkPath* dst) const {
+bool SkPaint::getFillPath(const SkPath& src, SkPath* dst,
+ const SkRect* cullRect) const {
SkStrokeRec rec(*this);
const SkPath* srcPtr = &src;
SkPath tmpPath;
- if (fPathEffect && fPathEffect->filterPath(&tmpPath, src, &rec)) {
+ if (fPathEffect && fPathEffect->filterPath(&tmpPath, src, &rec, cullRect)) {
srcPtr = &tmpPath;
}
diff --git a/src/core/SkPathEffect.cpp b/src/core/SkPathEffect.cpp
index ebbfd6d..38332a4 100644
--- a/src/core/SkPathEffect.cpp
+++ b/src/core/SkPathEffect.cpp
@@ -19,7 +19,7 @@
}
bool SkPathEffect::asPoints(PointData* results, const SkPath& src,
- const SkStrokeRec&, const SkMatrix&) const {
+ const SkStrokeRec&, const SkMatrix&, const SkRect*) const {
return false;
}
@@ -56,7 +56,7 @@
///////////////////////////////////////////////////////////////////////////////
bool SkComposePathEffect::filterPath(SkPath* dst, const SkPath& src,
- SkStrokeRec* rec) const {
+ SkStrokeRec* rec, const SkRect* cullRect) const {
// we may have failed to unflatten these, so we have to check
if (!fPE0 || !fPE1) {
return false;
@@ -65,16 +65,18 @@
SkPath tmp;
const SkPath* ptr = &src;
- if (fPE1->filterPath(&tmp, src, rec)) {
+ if (fPE1->filterPath(&tmp, src, rec, cullRect)) {
ptr = &tmp;
}
- return fPE0->filterPath(dst, *ptr, rec);
+ return fPE0->filterPath(dst, *ptr, rec, cullRect);
}
///////////////////////////////////////////////////////////////////////////////
bool SkSumPathEffect::filterPath(SkPath* dst, const SkPath& src,
- SkStrokeRec* rec) const {
+ SkStrokeRec* rec, const SkRect* cullRect) const {
// use bit-or so that we always call both, even if the first one succeeds
- return fPE0->filterPath(dst, src, rec) | fPE1->filterPath(dst, src, rec);
+ return fPE0->filterPath(dst, src, rec, cullRect) |
+ fPE1->filterPath(dst, src, rec, cullRect);
}
+
diff --git a/src/core/SkScalerContext.cpp b/src/core/SkScalerContext.cpp
index c16fa76..c35c0db 100644
--- a/src/core/SkScalerContext.cpp
+++ b/src/core/SkScalerContext.cpp
@@ -638,7 +638,7 @@
if (fPathEffect) {
SkPath effectPath;
- if (fPathEffect->filterPath(&effectPath, localPath, &rec)) {
+ if (fPathEffect->filterPath(&effectPath, localPath, &rec, NULL)) {
localPath.swap(effectPath);
}
}
diff --git a/src/effects/Sk1DPathEffect.cpp b/src/effects/Sk1DPathEffect.cpp
index 3ef050c..10a68b9 100644
--- a/src/effects/Sk1DPathEffect.cpp
+++ b/src/effects/Sk1DPathEffect.cpp
@@ -12,7 +12,7 @@
#include "SkPathMeasure.h"
bool Sk1DPathEffect::filterPath(SkPath* dst, const SkPath& src,
- SkStrokeRec*) const {
+ SkStrokeRec*, const SkRect*) const {
SkPathMeasure meas(src, false);
do {
SkScalar length = meas.getLength();
@@ -69,10 +69,10 @@
}
bool SkPath1DPathEffect::filterPath(SkPath* dst, const SkPath& src,
- SkStrokeRec* rec) const {
+ SkStrokeRec* rec, const SkRect* cullRect) const {
if (fAdvance > 0) {
rec->setFillStyle();
- return this->INHERITED::filterPath(dst, src, rec);
+ return this->INHERITED::filterPath(dst, src, rec, cullRect);
}
return false;
}
diff --git a/src/effects/Sk2DPathEffect.cpp b/src/effects/Sk2DPathEffect.cpp
index a4ebe74..dc15f07 100644
--- a/src/effects/Sk2DPathEffect.cpp
+++ b/src/effects/Sk2DPathEffect.cpp
@@ -17,7 +17,7 @@
}
bool Sk2DPathEffect::filterPath(SkPath* dst, const SkPath& src,
- SkStrokeRec*) const {
+ SkStrokeRec*, const SkRect*) const {
if (!fMatrixIsInvertible) {
return false;
}
@@ -80,8 +80,8 @@
///////////////////////////////////////////////////////////////////////////////
bool SkLine2DPathEffect::filterPath(SkPath* dst, const SkPath& src,
- SkStrokeRec* rec) const {
- if (this->INHERITED::filterPath(dst, src, rec)) {
+ SkStrokeRec* rec, const SkRect* cullRect) const {
+ if (this->INHERITED::filterPath(dst, src, rec, cullRect)) {
rec->setStrokeStyle(fWidth);
return true;
}
diff --git a/src/effects/SkCornerPathEffect.cpp b/src/effects/SkCornerPathEffect.cpp
index b63045d..1cf797a 100644
--- a/src/effects/SkCornerPathEffect.cpp
+++ b/src/effects/SkCornerPathEffect.cpp
@@ -31,7 +31,7 @@
}
bool SkCornerPathEffect::filterPath(SkPath* dst, const SkPath& src,
- SkStrokeRec*) const {
+ SkStrokeRec*, const SkRect*) const {
if (0 == fRadius) {
return false;
}
diff --git a/src/effects/SkDashPathEffect.cpp b/src/effects/SkDashPathEffect.cpp
index ef10ca5..d1ce526 100644
--- a/src/effects/SkDashPathEffect.cpp
+++ b/src/effects/SkDashPathEffect.cpp
@@ -88,10 +88,81 @@
sk_free(fIntervals);
}
+static void outset_for_stroke(SkRect* rect, const SkStrokeRec& rec) {
+ SkScalar radius = SkScalarHalf(rec.getWidth());
+ if (0 == radius) {
+ radius = SK_Scalar1; // hairlines
+ }
+ if (SkPaint::kMiter_Join == rec.getJoin()) {
+ radius = SkScalarMul(radius, rec.getMiter());
+ }
+ rect->outset(radius, radius);
+}
+
+// Only handles lines for now. If returns true, dstPath is the new (smaller)
+// path. If returns false, then dstPath parameter is ignored.
+static bool cull_path(const SkPath& srcPath, const SkStrokeRec& rec,
+ const SkRect* cullRect, SkScalar intervalLength,
+ SkPath* dstPath) {
+ if (NULL == cullRect) {
+ return false;
+ }
+
+ SkPoint pts[2];
+ if (!srcPath.isLine(pts)) {
+ return false;
+ }
+
+ SkRect bounds = *cullRect;
+ outset_for_stroke(&bounds, rec);
+
+ SkScalar dx = pts[1].x() - pts[0].x();
+ SkScalar dy = pts[1].y() - pts[0].y();
+
+ // just do horizontal lines for now (lazy)
+ if (dy) {
+ return false;
+ }
+
+ SkScalar minX = pts[0].fX;
+ SkScalar maxX = pts[1].fX;
+
+ if (maxX < bounds.fLeft || minX > bounds.fRight) {
+ return false;
+ }
+
+ if (dx < 0) {
+ SkTSwap(minX, maxX);
+ }
+
+ // Now we actually perform the chop, removing the excess to the left and
+ // right of the bounds (keeping our new line "in phase" with the dash,
+ // hence the (mod intervalLength).
+
+ if (minX < bounds.fLeft) {
+ minX = bounds.fLeft - SkScalarMod(bounds.fLeft - minX,
+ intervalLength);
+ }
+ if (maxX > bounds.fRight) {
+ maxX = bounds.fRight + SkScalarMod(maxX - bounds.fRight,
+ intervalLength);
+ }
+
+ SkASSERT(maxX > minX);
+ if (dx < 0) {
+ SkTSwap(minX, maxX);
+ }
+ pts[0].fX = minX;
+ pts[1].fX = maxX;
+
+ dstPath->moveTo(pts[0]);
+ dstPath->lineTo(pts[1]);
+ return true;
+}
+
class SpecialLineRec {
public:
bool init(const SkPath& src, SkPath* dst, SkStrokeRec* rec,
- SkScalar pathLength,
int intervalCount, SkScalar intervalLength) {
if (rec->isHairlineStyle() || !src.isLine(fPts)) {
return false;
@@ -102,6 +173,8 @@
return false;
}
+ SkScalar pathLength = SkPoint::Distance(fPts[0], fPts[1]);
+
fTangent = fPts[1] - fPts[0];
if (fTangent.isZero()) {
return false;
@@ -156,19 +229,25 @@
};
bool SkDashPathEffect::filterPath(SkPath* dst, const SkPath& src,
- SkStrokeRec* rec) const {
+ SkStrokeRec* rec, const SkRect* cullRect) const {
// we do nothing if the src wants to be filled, or if our dashlength is 0
if (rec->isFillStyle() || fInitialDashLength < 0) {
return false;
}
- SkPathMeasure meas(src, false);
const SkScalar* intervals = fIntervals;
SkScalar dashCount = 0;
+ SkPath cullPathStorage;
+ const SkPath* srcPtr = &src;
+ if (cull_path(src, *rec, cullRect, fIntervalLength, &cullPathStorage)) {
+ srcPtr = &cullPathStorage;
+ }
+
SpecialLineRec lineRec;
- const bool specialLine = lineRec.init(src, dst, rec, meas.getLength(),
- fCount >> 1, fIntervalLength);
+ bool specialLine = lineRec.init(src, dst, rec, fCount >> 1, fIntervalLength);
+
+ SkPathMeasure meas(*srcPtr, false);
do {
bool skipFirstSegment = meas.isClosed();
@@ -256,7 +335,8 @@
bool SkDashPathEffect::asPoints(PointData* results,
const SkPath& src,
const SkStrokeRec& rec,
- const SkMatrix& matrix) const {
+ const SkMatrix& matrix,
+ const SkRect* cullRect) const {
// width < 0 -> fill && width == 0 -> hairline so requiring width > 0 rules both out
if (fInitialDashLength < 0 || 0 >= rec.getWidth()) {
return false;
diff --git a/src/effects/SkDiscretePathEffect.cpp b/src/effects/SkDiscretePathEffect.cpp
index a936be9..2c95208 100644
--- a/src/effects/SkDiscretePathEffect.cpp
+++ b/src/effects/SkDiscretePathEffect.cpp
@@ -26,7 +26,7 @@
}
bool SkDiscretePathEffect::filterPath(SkPath* dst, const SkPath& src,
- SkStrokeRec* rec) const {
+ SkStrokeRec* rec, const SkRect*) const {
bool doFill = rec->isFillStyle();
SkPathMeasure meas(src, doFill);
diff --git a/src/gpu/SkGpuDevice.cpp b/src/gpu/SkGpuDevice.cpp
index 827fc52..3e20e3b 100644
--- a/src/gpu/SkGpuDevice.cpp
+++ b/src/gpu/SkGpuDevice.cpp
@@ -972,7 +972,9 @@
SkStrokeRec stroke(paint);
SkPathEffect* pathEffect = paint.getPathEffect();
- if (pathEffect && pathEffect->filterPath(&effectPath, *pathPtr, &stroke)) {
+ const SkRect* cullRect = NULL; // TODO: what is our bounds?
+ if (pathEffect && pathEffect->filterPath(&effectPath, *pathPtr, &stroke,
+ cullRect)) {
pathPtr = &effectPath;
}