Unify implementations of stroking radius calculationsns
GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1928133002
Review-Url: https://codereview.chromium.org/1928133002
diff --git a/include/core/SkStrokeRec.h b/include/core/SkStrokeRec.h
index 22981a5..9a49a3d 100644
--- a/include/core/SkStrokeRec.h
+++ b/include/core/SkStrokeRec.h
@@ -99,6 +99,22 @@
void applyToPaint(SkPaint* paint) const;
/**
+ * Gives a conservative value for the outset that should applied to a
+ * geometries bounds to account for any inflation due to applying this
+ * strokeRec to the geometry.
+ */
+ SkScalar getInflationRadius() const;
+
+ /**
+ * Equivalent to:
+ * SkStrokeRec rec(paint, style);
+ * rec.getInflationRadius();
+ * This does not account for other effects on the paint (i.e. path
+ * effect).
+ */
+ static SkScalar GetInflationRadius(const SkPaint&, SkPaint::Style);
+
+ /**
* Compare if two SkStrokeRecs have an equal effect on a path.
* Equal SkStrokeRecs produce equal paths. Equality of produced
* paths does not take the ResScale parameter into account.
diff --git a/src/core/SkPaint.cpp b/src/core/SkPaint.cpp
index d28168a..51bfd54 100644
--- a/src/core/SkPaint.cpp
+++ b/src/core/SkPaint.cpp
@@ -2025,22 +2025,8 @@
src = &tmpSrc;
}
- if (kFill_Style != style) {
- // since we're stroked, outset the rect by the radius (and join type)
- SkScalar radius = SkScalarHalf(this->getStrokeWidth());
- if (0 == radius) { // hairline
- radius = SK_Scalar1;
- } else if (this->getStrokeJoin() == SkPaint::kMiter_Join) {
- SkScalar scale = this->getStrokeMiter();
- if (scale > SK_Scalar1) {
- radius = SkScalarMul(radius, scale);
- }
- }
- storage->set(src->fLeft - radius, src->fTop - radius,
- src->fRight + radius, src->fBottom + radius);
- } else {
- *storage = *src;
- }
+ SkScalar radius = SkStrokeRec::GetInflationRadius(*this, style);
+ *storage = src->makeOutset(radius, radius);
if (this->getMaskFilter()) {
this->getMaskFilter()->computeFastBounds(*storage, storage);
diff --git a/src/core/SkStrokeRec.cpp b/src/core/SkStrokeRec.cpp
index 6aed573..f3cca16 100644
--- a/src/core/SkStrokeRec.cpp
+++ b/src/core/SkStrokeRec.cpp
@@ -134,3 +134,31 @@
paint->setStrokeCap((SkPaint::Cap)fCap);
paint->setStrokeJoin((SkPaint::Join)fJoin);
}
+
+static inline SkScalar get_inflation_bounds(SkPaint::Join join,
+ SkScalar strokeWidth,
+ SkScalar miterLimit) {
+ if (strokeWidth < 0) { // fill
+ return 0;
+ } else if (0 == strokeWidth) {
+ return SK_Scalar1;
+ }
+ // since we're stroked, outset the rect by the radius (and join type)
+ SkScalar radius = SkScalarHalf(strokeWidth);
+ if (SkPaint::kMiter_Join == join) {
+ if (miterLimit > SK_Scalar1) {
+ radius = SkScalarMul(miterLimit, radius);
+ }
+ }
+ return radius;
+}
+
+SkScalar SkStrokeRec::getInflationRadius() const {
+ return get_inflation_bounds((SkPaint::Join)fJoin, fWidth, fMiterLimit);
+}
+
+SkScalar SkStrokeRec::GetInflationRadius(const SkPaint& paint, SkPaint::Style style) {
+ SkScalar width = SkPaint::kFill_Style == style ? -SK_Scalar1 : paint.getStrokeWidth();
+ return get_inflation_bounds(paint.getStrokeJoin(), width, paint.getStrokeMiter());
+
+}
diff --git a/src/gpu/batches/GrTessellatingPathRenderer.cpp b/src/gpu/batches/GrTessellatingPathRenderer.cpp
index 39aa50c..2c8520b 100644
--- a/src/gpu/batches/GrTessellatingPathRenderer.cpp
+++ b/src/gpu/batches/GrTessellatingPathRenderer.cpp
@@ -258,16 +258,8 @@
} else {
fBounds = path.getBounds();
}
- if (!stroke.isFillStyle()) {
- SkScalar radius = SkScalarHalf(stroke.getWidth());
- if (stroke.getJoin() == SkPaint::kMiter_Join) {
- SkScalar scale = stroke.getMiter();
- if (scale > SK_Scalar1) {
- radius = SkScalarMul(radius, scale);
- }
- }
- fBounds.outset(radius, radius);
- }
+ SkScalar radius = stroke.getInflationRadius();
+ fBounds.outset(radius, radius);
viewMatrix.mapRect(&fBounds);
}