Do circular clipping in normalized space
BUG=skia:426217
Review URL: https://codereview.chromium.org/1110173002
diff --git a/gm/circularclips.cpp b/gm/circularclips.cpp
index 4a98de4..15f583f 100644
--- a/gm/circularclips.cpp
+++ b/gm/circularclips.cpp
@@ -49,6 +49,20 @@
SkPaint fillPaint;
+ // Giant background circular clips (AA, non-inverted, replace/isect)
+ fillPaint.setColor(0x80808080);
+ canvas->save();
+ canvas->scale(10, 10);
+ canvas->translate(-((fX1 + fX2)/2 - fR), -(fY - 2*fR/3));
+ canvas->clipPath(fCircle1, SkRegion::kReplace_Op, true);
+ canvas->clipPath(fCircle2, SkRegion::kIntersect_Op, true);
+
+ canvas->drawRect(rect, fillPaint);
+
+ canvas->restore();
+
+ fillPaint.setColor(0xFF000000);
+
for (size_t i = 0; i < 4; i++) {
fCircle1.toggleInverseFillType();
if (i % 2 == 0) {
diff --git a/src/gpu/effects/GrOvalEffect.cpp b/src/gpu/effects/GrOvalEffect.cpp
index bce3c01..69f6326 100644
--- a/src/gpu/effects/GrOvalEffect.cpp
+++ b/src/gpu/effects/GrOvalEffect.cpp
@@ -128,10 +128,10 @@
const TextureSamplerArray& samplers) {
const CircleEffect& ce = fp.cast<CircleEffect>();
const char *circleName;
- // The circle uniform is (center.x, center.y, radius + 0.5) for regular fills and
- // (... ,radius - 0.5) for inverse fills.
+ // The circle uniform is (center.x, center.y, radius + 0.5, 1 / (radius + 0.5)) for regular
+ // fills and (..., radius - 0.5, 1 / (radius - 0.5)) for inverse fills.
fCircleUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
- kVec3f_GrSLType, kDefault_GrSLPrecision,
+ kVec4f_GrSLType, kDefault_GrSLPrecision,
"circle",
&circleName);
@@ -139,12 +139,16 @@
const char* fragmentPos = fsBuilder->fragmentPosition();
SkASSERT(kHairlineAA_GrProcessorEdgeType != ce.getEdgeType());
+ // TODO: Right now the distance to circle caclulation is performed in a space normalized to the
+ // radius and then denormalized. This is to prevent overflow on devices that have a "real"
+ // mediump. It'd be nice to only to this on mediump devices but we currently don't have the
+ // caps here.
if (GrProcessorEdgeTypeIsInverseFill(ce.getEdgeType())) {
- fsBuilder->codeAppendf("\t\tfloat d = length(%s.xy - %s.xy) - %s.z;\n",
- circleName, fragmentPos, circleName);
+ fsBuilder->codeAppendf("\t\tfloat d = (length((%s.xy - %s.xy) * %s.w) - 1.0) * %s.z;\n",
+ circleName, fragmentPos, circleName, circleName);
} else {
- fsBuilder->codeAppendf("\t\tfloat d = %s.z - length(%s.xy - %s.xy);\n",
- circleName, fragmentPos, circleName);
+ fsBuilder->codeAppendf("\t\tfloat d = (1.0 - length((%s.xy - %s.xy) * %s.w)) * %s.z;\n",
+ circleName, fragmentPos, circleName, circleName);
}
if (GrProcessorEdgeTypeIsAA(ce.getEdgeType())) {
fsBuilder->codeAppend("\t\td = clamp(d, 0.0, 1.0);\n");
@@ -171,7 +175,8 @@
} else {
radius += 0.5f;
}
- pdman.set3f(fCircleUniform, ce.getCenter().fX, ce.getCenter().fY, radius);
+ pdman.set4f(fCircleUniform, ce.getCenter().fX, ce.getCenter().fY, radius,
+ SkScalarInvert(radius));
fPrevCenter = ce.getCenter();
fPrevRadius = ce.getRadius();
}