diff options
author | commit-bot@chromium.org <commit-bot@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2014-03-12 15:32:50 +0000 |
---|---|---|
committer | commit-bot@chromium.org <commit-bot@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2014-03-12 15:32:50 +0000 |
commit | 4355f21dbc29bcb479a3806444dd22916e22771e (patch) | |
tree | c8ae4bdff1fceb9e9ca55595336fbeafefa3aaa9 /src | |
parent | 069a55a7fe2c3ae6f5049843b3fc1a167b51215c (diff) |
Add elliptical-corner rrect clip effect. Currently only handles the case where all corners have the same pair of radii.
BUG=skia:2181
R=jvanverth@google.com
Author: bsalomon@google.com
Review URL: https://codereview.chromium.org/194603004
git-svn-id: http://skia.googlecode.com/svn/trunk@13766 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'src')
-rw-r--r-- | src/gpu/effects/GrRRectEffect.cpp | 347 |
1 files changed, 277 insertions, 70 deletions
diff --git a/src/gpu/effects/GrRRectEffect.cpp b/src/gpu/effects/GrRRectEffect.cpp index 1bbf2f7976..1838cf0287 100644 --- a/src/gpu/effects/GrRRectEffect.cpp +++ b/src/gpu/effects/GrRRectEffect.cpp @@ -13,11 +13,9 @@ #include "SkRRect.h" -using namespace GrRRectEffect; +class GLCircularRRectEffect; -class GLRRectEffect; - -class RRectEffect : public GrEffect { +class CircularRRectEffect : public GrEffect { public: // This effect only supports circular corner rrects where the radius is >= kRadiusMin. static const SkScalar kRadiusMin; @@ -42,8 +40,8 @@ public: // be square). static GrEffectRef* Create(GrEffectEdgeType, uint32_t circularCornerFlags, const SkRRect&); - virtual ~RRectEffect() {}; - static const char* Name() { return "RRect"; } + virtual ~CircularRRectEffect() {}; + static const char* Name() { return "CircularRRect"; } const SkRRect& getRRect() const { return fRRect; } @@ -51,14 +49,14 @@ public: GrEffectEdgeType getEdgeType() const { return fEdgeType; } - typedef GLRRectEffect GLEffect; + typedef GLCircularRRectEffect GLEffect; virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE; virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE; private: - RRectEffect(GrEffectEdgeType, uint32_t circularCornerFlags, const SkRRect&); + CircularRRectEffect(GrEffectEdgeType, uint32_t circularCornerFlags, const SkRRect&); virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE; @@ -71,25 +69,25 @@ private: typedef GrEffect INHERITED; }; -const SkScalar RRectEffect::kRadiusMin = 0.5f; +const SkScalar CircularRRectEffect::kRadiusMin = 0.5f; -GrEffectRef* RRectEffect::Create(GrEffectEdgeType edgeType, +GrEffectRef* CircularRRectEffect::Create(GrEffectEdgeType edgeType, uint32_t circularCornerFlags, const SkRRect& rrect) { SkASSERT(kFillAA_GrEffectEdgeType == edgeType || kInverseFillAA_GrEffectEdgeType == edgeType); - return CreateEffectRef(AutoEffectUnref(SkNEW_ARGS(RRectEffect, + return CreateEffectRef(AutoEffectUnref(SkNEW_ARGS(CircularRRectEffect, (edgeType, circularCornerFlags, rrect)))); } -void RRectEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const { +void CircularRRectEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const { *validFlags = 0; } -const GrBackendEffectFactory& RRectEffect::getFactory() const { - return GrTBackendEffectFactory<RRectEffect>::getInstance(); +const GrBackendEffectFactory& CircularRRectEffect::getFactory() const { + return GrTBackendEffectFactory<CircularRRectEffect>::getInstance(); } -RRectEffect::RRectEffect(GrEffectEdgeType edgeType, uint32_t circularCornerFlags, +CircularRRectEffect::CircularRRectEffect(GrEffectEdgeType edgeType, uint32_t circularCornerFlags, const SkRRect& rrect) : fRRect(rrect) , fEdgeType(edgeType) @@ -97,17 +95,17 @@ RRectEffect::RRectEffect(GrEffectEdgeType edgeType, uint32_t circularCornerFlags this->setWillReadFragmentPosition(); } -bool RRectEffect::onIsEqual(const GrEffect& other) const { - const RRectEffect& rre = CastEffect<RRectEffect>(other); - // type is derived from fRRect, so no need to check it. - return fEdgeType == rre.fEdgeType && fRRect == rre.fRRect; +bool CircularRRectEffect::onIsEqual(const GrEffect& other) const { + const CircularRRectEffect& crre = CastEffect<CircularRRectEffect>(other); + // The corner flags are derived from fRRect, so no need to check them. + return fEdgeType == crre.fEdgeType && fRRect == crre.fRRect; } ////////////////////////////////////////////////////////////////////////////// -GR_DEFINE_EFFECT_TEST(RRectEffect); +GR_DEFINE_EFFECT_TEST(CircularRRectEffect); -GrEffectRef* RRectEffect::TestCreate(SkRandom* random, +GrEffectRef* CircularRRectEffect::TestCreate(SkRandom* random, GrContext*, const GrDrawTargetCaps& caps, GrTexture*[]) { @@ -126,9 +124,9 @@ GrEffectRef* RRectEffect::TestCreate(SkRandom* random, ////////////////////////////////////////////////////////////////////////////// -class GLRRectEffect : public GrGLEffect { +class GLCircularRRectEffect : public GrGLEffect { public: - GLRRectEffect(const GrBackendEffectFactory&, const GrDrawEffect&); + GLCircularRRectEffect(const GrBackendEffectFactory&, const GrDrawEffect&); virtual void emitCode(GrGLShaderBuilder* builder, const GrDrawEffect& drawEffect, @@ -149,20 +147,20 @@ private: typedef GrGLEffect INHERITED; }; -GLRRectEffect::GLRRectEffect(const GrBackendEffectFactory& factory, +GLCircularRRectEffect::GLCircularRRectEffect(const GrBackendEffectFactory& factory, const GrDrawEffect& drawEffect) : INHERITED (factory) { fPrevRRect.setEmpty(); } -void GLRRectEffect::emitCode(GrGLShaderBuilder* builder, +void GLCircularRRectEffect::emitCode(GrGLShaderBuilder* builder, const GrDrawEffect& drawEffect, EffectKey key, const char* outputColor, const char* inputColor, const TransformedCoordsArray&, const TextureSamplerArray& samplers) { - const RRectEffect& rre = drawEffect.castEffect<RRectEffect>(); + const CircularRRectEffect& crre = drawEffect.castEffect<CircularRRectEffect>(); const char *rectName; const char *radiusPlusHalfName; // The inner rect is the rrect bounds inset by the radius. Its left, top, right, and bottom @@ -193,16 +191,17 @@ void GLRRectEffect::emitCode(GrGLShaderBuilder* builder, // For the cases where one half of the rrect is rectangular we drop one of the x or y // computations, compute a separate rect edge alpha for the rect side, and mul the two computed // alphas together. - switch (rre.getCircularCornerFlags()) { - case RRectEffect::kAll_CornerFlags: + switch (crre.getCircularCornerFlags()) { + case CircularRRectEffect::kAll_CornerFlags: builder->fsCodeAppendf("\t\tvec2 dxy0 = %s.xy - %s.xy;\n", rectName, fragmentPos); builder->fsCodeAppendf("\t\tvec2 dxy1 = %s.xy - %s.zw;\n", fragmentPos, rectName); builder->fsCodeAppend("\t\tvec2 dxy = max(max(dxy0, dxy1), 0.0);\n"); builder->fsCodeAppendf("\t\tfloat alpha = clamp(%s - length(dxy), 0.0, 1.0);\n", radiusPlusHalfName); break; - case RRectEffect::kTopLeft_CornerFlag: - builder->fsCodeAppendf("\t\tvec2 dxy = max(%s.xy - %s.xy, 0.0);\n", rectName, fragmentPos); + case CircularRRectEffect::kTopLeft_CornerFlag: + builder->fsCodeAppendf("\t\tvec2 dxy = max(%s.xy - %s.xy, 0.0);\n", + rectName, fragmentPos); builder->fsCodeAppendf("\t\tfloat rightAlpha = clamp(%s.z - %s.x, 0.0, 1.0);\n", rectName, fragmentPos); builder->fsCodeAppendf("\t\tfloat bottomAlpha = clamp(%s.w - %s.y, 0.0, 1.0);\n", @@ -210,7 +209,7 @@ void GLRRectEffect::emitCode(GrGLShaderBuilder* builder, builder->fsCodeAppendf("\t\tfloat alpha = bottomAlpha * rightAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n", radiusPlusHalfName); break; - case RRectEffect::kTopRight_CornerFlag: + case CircularRRectEffect::kTopRight_CornerFlag: builder->fsCodeAppendf("\t\tvec2 dxy = max(vec2(%s.x - %s.z, %s.y - %s.y), 0.0);\n", fragmentPos, rectName, rectName, fragmentPos); builder->fsCodeAppendf("\t\tfloat leftAlpha = clamp(%s.x - %s.x, 0.0, 1.0);\n", @@ -220,7 +219,7 @@ void GLRRectEffect::emitCode(GrGLShaderBuilder* builder, builder->fsCodeAppendf("\t\tfloat alpha = bottomAlpha * leftAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n", radiusPlusHalfName); break; - case RRectEffect::kBottomRight_CornerFlag: + case CircularRRectEffect::kBottomRight_CornerFlag: builder->fsCodeAppendf("\t\tvec2 dxy = max(%s.xy - %s.zw, 0.0);\n", fragmentPos, rectName); builder->fsCodeAppendf("\t\tfloat leftAlpha = clamp(%s.x - %s.x, 0.0, 1.0);\n", @@ -230,7 +229,7 @@ void GLRRectEffect::emitCode(GrGLShaderBuilder* builder, builder->fsCodeAppendf("\t\tfloat alpha = topAlpha * leftAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n", radiusPlusHalfName); break; - case RRectEffect::kBottomLeft_CornerFlag: + case CircularRRectEffect::kBottomLeft_CornerFlag: builder->fsCodeAppendf("\t\tvec2 dxy = max(vec2(%s.x - %s.x, %s.y - %s.w), 0.0);\n", rectName, fragmentPos, fragmentPos, rectName); builder->fsCodeAppendf("\t\tfloat rightAlpha = clamp(%s.z - %s.x, 0.0, 1.0);\n", @@ -240,7 +239,7 @@ void GLRRectEffect::emitCode(GrGLShaderBuilder* builder, builder->fsCodeAppendf("\t\tfloat alpha = topAlpha * rightAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n", radiusPlusHalfName); break; - case RRectEffect::kLeft_CornerFlags: + case CircularRRectEffect::kLeft_CornerFlags: builder->fsCodeAppendf("\t\tvec2 dxy0 = %s.xy - %s.xy;\n", rectName, fragmentPos); builder->fsCodeAppendf("\t\tfloat dy1 = %s.y - %s.w;\n", fragmentPos, rectName); builder->fsCodeAppend("\t\tvec2 dxy = max(vec2(dxy0.x, max(dxy0.y, dy1)), 0.0);\n"); @@ -249,7 +248,7 @@ void GLRRectEffect::emitCode(GrGLShaderBuilder* builder, builder->fsCodeAppendf("\t\tfloat alpha = rightAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n", radiusPlusHalfName); break; - case RRectEffect::kTop_CornerFlags: + case CircularRRectEffect::kTop_CornerFlags: builder->fsCodeAppendf("\t\tvec2 dxy0 = %s.xy - %s.xy;\n", rectName, fragmentPos); builder->fsCodeAppendf("\t\tfloat dx1 = %s.x - %s.z;\n", fragmentPos, rectName); builder->fsCodeAppend("\t\tvec2 dxy = max(vec2(max(dxy0.x, dx1), dxy0.y), 0.0);\n"); @@ -258,7 +257,7 @@ void GLRRectEffect::emitCode(GrGLShaderBuilder* builder, builder->fsCodeAppendf("\t\tfloat alpha = bottomAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n", radiusPlusHalfName); break; - case RRectEffect::kRight_CornerFlags: + case CircularRRectEffect::kRight_CornerFlags: builder->fsCodeAppendf("\t\tfloat dy0 = %s.y - %s.y;\n", rectName, fragmentPos); builder->fsCodeAppendf("\t\tvec2 dxy1 = %s.xy - %s.zw;\n", fragmentPos, rectName); builder->fsCodeAppend("\t\tvec2 dxy = max(vec2(dxy1.x, max(dy0, dxy1.y)), 0.0);\n"); @@ -267,7 +266,7 @@ void GLRRectEffect::emitCode(GrGLShaderBuilder* builder, builder->fsCodeAppendf("\t\tfloat alpha = leftAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n", radiusPlusHalfName); break; - case RRectEffect::kBottom_CornerFlags: + case CircularRRectEffect::kBottom_CornerFlags: builder->fsCodeAppendf("\t\tfloat dx0 = %s.x - %s.x;\n", rectName, fragmentPos); builder->fsCodeAppendf("\t\tvec2 dxy1 = %s.xy - %s.zw;\n", fragmentPos, rectName); builder->fsCodeAppend("\t\tvec2 dxy = max(vec2(max(dx0, dxy1.x), dxy1.y), 0.0);\n"); @@ -278,7 +277,7 @@ void GLRRectEffect::emitCode(GrGLShaderBuilder* builder, break; } - if (kInverseFillAA_GrEffectEdgeType == rre.getEdgeType()) { + if (kInverseFillAA_GrEffectEdgeType == crre.getEdgeType()) { builder->fsCodeAppend("\t\talpha = 1.0 - alpha;\n"); } @@ -286,75 +285,77 @@ void GLRRectEffect::emitCode(GrGLShaderBuilder* builder, (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_str()); } -GrGLEffect::EffectKey GLRRectEffect::GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) { - const RRectEffect& rre = drawEffect.castEffect<RRectEffect>(); +GrGLEffect::EffectKey GLCircularRRectEffect::GenKey(const GrDrawEffect& drawEffect, + const GrGLCaps&) { + const CircularRRectEffect& crre = drawEffect.castEffect<CircularRRectEffect>(); GR_STATIC_ASSERT(kGrEffectEdgeTypeCnt <= 8); - return (rre.getCircularCornerFlags() << 3) | rre.getEdgeType(); + return (crre.getCircularCornerFlags() << 3) | crre.getEdgeType(); } -void GLRRectEffect::setData(const GrGLUniformManager& uman, const GrDrawEffect& drawEffect) { - const RRectEffect& rre = drawEffect.castEffect<RRectEffect>(); - const SkRRect& rrect = rre.getRRect(); +void GLCircularRRectEffect::setData(const GrGLUniformManager& uman, + const GrDrawEffect& drawEffect) { + const CircularRRectEffect& crre = drawEffect.castEffect<CircularRRectEffect>(); + const SkRRect& rrect = crre.getRRect(); if (rrect != fPrevRRect) { SkRect rect = rrect.getBounds(); SkScalar radius = 0; - switch (rre.getCircularCornerFlags()) { - case RRectEffect::kAll_CornerFlags: + switch (crre.getCircularCornerFlags()) { + case CircularRRectEffect::kAll_CornerFlags: SkASSERT(rrect.isSimpleCircular()); radius = rrect.getSimpleRadii().fX; - SkASSERT(radius >= RRectEffect::kRadiusMin); + SkASSERT(radius >= CircularRRectEffect::kRadiusMin); rect.inset(radius, radius); break; - case RRectEffect::kTopLeft_CornerFlag: + case CircularRRectEffect::kTopLeft_CornerFlag: radius = rrect.radii(SkRRect::kUpperLeft_Corner).fX; rect.fLeft += radius; rect.fTop += radius; rect.fRight += 0.5f; rect.fBottom += 0.5f; break; - case RRectEffect::kTopRight_CornerFlag: + case CircularRRectEffect::kTopRight_CornerFlag: radius = rrect.radii(SkRRect::kUpperRight_Corner).fX; rect.fLeft -= 0.5f; rect.fTop += radius; rect.fRight -= radius; rect.fBottom += 0.5f; break; - case RRectEffect::kBottomRight_CornerFlag: + case CircularRRectEffect::kBottomRight_CornerFlag: radius = rrect.radii(SkRRect::kLowerRight_Corner).fX; rect.fLeft -= 0.5f; rect.fTop -= 0.5f; rect.fRight -= radius; rect.fBottom -= radius; break; - case RRectEffect::kBottomLeft_CornerFlag: + case CircularRRectEffect::kBottomLeft_CornerFlag: radius = rrect.radii(SkRRect::kLowerLeft_Corner).fX; rect.fLeft += radius; rect.fTop -= 0.5f; rect.fRight += 0.5f; rect.fBottom -= radius; break; - case RRectEffect::kLeft_CornerFlags: + case CircularRRectEffect::kLeft_CornerFlags: radius = rrect.radii(SkRRect::kUpperLeft_Corner).fX; rect.fLeft += radius; rect.fTop += radius; rect.fRight += 0.5f; rect.fBottom -= radius; break; - case RRectEffect::kTop_CornerFlags: + case CircularRRectEffect::kTop_CornerFlags: radius = rrect.radii(SkRRect::kUpperLeft_Corner).fX; rect.fLeft += radius; rect.fTop += radius; rect.fRight -= radius; rect.fBottom += 0.5f; break; - case RRectEffect::kRight_CornerFlags: + case CircularRRectEffect::kRight_CornerFlags: radius = rrect.radii(SkRRect::kUpperRight_Corner).fX; rect.fLeft -= 0.5f; rect.fTop += radius; rect.fRight -= radius; rect.fBottom -= radius; break; - case RRectEffect::kBottom_CornerFlags: + case CircularRRectEffect::kBottom_CornerFlags: radius = rrect.radii(SkRRect::kLowerLeft_Corner).fX; rect.fLeft += radius; rect.fTop -= 0.5f; @@ -372,16 +373,222 @@ void GLRRectEffect::setData(const GrGLUniformManager& uman, const GrDrawEffect& ////////////////////////////////////////////////////////////////////////////// +class GLEllipticalRRectEffect; + +/** + * Currently this effect only supports "simple" elliptical round rects (i.e. + * the corners all have a common x/y radii pair). + */ +class EllipticalRRectEffect : public GrEffect { +public: + // This effect only supports rrects where the radii are >= kRadiusMin + static const SkScalar kRadiusMin; + + static GrEffectRef* Create(GrEffectEdgeType, const SkRRect&); + + virtual ~EllipticalRRectEffect() {}; + static const char* Name() { return "EllipticalRRect"; } + + const SkRRect& getRRect() const { return fRRect; } + + GrEffectEdgeType getEdgeType() const { return fEdgeType; } + + typedef GLEllipticalRRectEffect GLEffect; + + virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE; + + virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE; + +private: + EllipticalRRectEffect(GrEffectEdgeType, const SkRRect&); + + virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE; + + SkRRect fRRect; + GrEffectEdgeType fEdgeType; + + GR_DECLARE_EFFECT_TEST; + + typedef GrEffect INHERITED; +}; + +const SkScalar EllipticalRRectEffect::kRadiusMin = 0.5f; + +GrEffectRef* EllipticalRRectEffect::Create(GrEffectEdgeType edgeType, + const SkRRect& rrect) { + SkASSERT(kFillAA_GrEffectEdgeType == edgeType || kInverseFillAA_GrEffectEdgeType == edgeType); + return CreateEffectRef(AutoEffectUnref(SkNEW_ARGS(EllipticalRRectEffect, (edgeType, rrect)))); +} + +void EllipticalRRectEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const { + *validFlags = 0; +} + +const GrBackendEffectFactory& EllipticalRRectEffect::getFactory() const { + return GrTBackendEffectFactory<EllipticalRRectEffect>::getInstance(); +} + +EllipticalRRectEffect::EllipticalRRectEffect(GrEffectEdgeType edgeType, const SkRRect& rrect) + : fRRect(rrect) + , fEdgeType(edgeType){ + this->setWillReadFragmentPosition(); +} + +bool EllipticalRRectEffect::onIsEqual(const GrEffect& other) const { + const EllipticalRRectEffect& erre = CastEffect<EllipticalRRectEffect>(other); + return fEdgeType == erre.fEdgeType && fRRect == erre.fRRect; +} + +////////////////////////////////////////////////////////////////////////////// + +GR_DEFINE_EFFECT_TEST(EllipticalRRectEffect); + +GrEffectRef* EllipticalRRectEffect::TestCreate(SkRandom* random, + GrContext*, + const GrDrawTargetCaps& caps, + GrTexture*[]) { + SkScalar w = random->nextRangeScalar(20.f, 1000.f); + SkScalar h = random->nextRangeScalar(20.f, 1000.f); + SkScalar rx = random->nextRangeF(kRadiusMin, 9.f); + SkScalar ry = random->nextRangeF(kRadiusMin, 9.f); + SkRRect rrect; + rrect.setRectXY(SkRect::MakeWH(w, h), rx, ry); + GrEffectRef* effect; + do { + GrEffectEdgeType et = (GrEffectEdgeType)random->nextULessThan(kGrEffectEdgeTypeCnt); + effect = EllipticalRRectEffect::Create(et, rrect); + } while (NULL == effect); + return effect; +} + +////////////////////////////////////////////////////////////////////////////// + +class GLEllipticalRRectEffect : public GrGLEffect { +public: + GLEllipticalRRectEffect(const GrBackendEffectFactory&, const GrDrawEffect&); + + virtual void emitCode(GrGLShaderBuilder* builder, + const GrDrawEffect& drawEffect, + EffectKey key, + const char* outputColor, + const char* inputColor, + const TransformedCoordsArray&, + const TextureSamplerArray&) SK_OVERRIDE; + + static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&); + + virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE; + +private: + GrGLUniformManager::UniformHandle fInnerRectUniform; + GrGLUniformManager::UniformHandle fInvRadiusXYSqdUniform; + SkRRect fPrevRRect; + typedef GrGLEffect INHERITED; +}; + +GLEllipticalRRectEffect::GLEllipticalRRectEffect(const GrBackendEffectFactory& factory, + const GrDrawEffect& drawEffect) + : INHERITED (factory) { + fPrevRRect.setEmpty(); +} + +void GLEllipticalRRectEffect::emitCode(GrGLShaderBuilder* builder, + const GrDrawEffect& drawEffect, + EffectKey key, + const char* outputColor, + const char* inputColor, + const TransformedCoordsArray&, + const TextureSamplerArray& samplers) { + const EllipticalRRectEffect& erre = drawEffect.castEffect<EllipticalRRectEffect>(); + const char *rectName; + const char *invRadiusXYSqdName; + // The inner rect is the rrect bounds inset by the x/y radii + fInnerRectUniform = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, + kVec4f_GrSLType, + "innerRect", + &rectName); + fInvRadiusXYSqdUniform = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, + kVec2f_GrSLType, + "invRadiusXY", + &invRadiusXYSqdName); + const char* fragmentPos = builder->fragmentPosition(); + // At each quarter-ellipse corner we compute a vector that is the offset of the fragment pos + // to the ellipse center. The vector is pinned in x and y to be in the quarter-plane relevant + // to that corner. This means that points near the interior near the rrect top edge will have + // a vector that points straight up for both the TL left and TR corners. Computing an + // alpha from this vector at either the TR or TL corner will give the correct result. Similarly, + // fragments near the other three edges will get the correct AA. Fragments in the interior of + // the rrect will have a (0,0) vector at all four corners. So long as the radii > 0.5 they will + // correctly produce an alpha value of 1 at all four corners. We take the min of all the alphas. + // The code below is a simplified version of the above that performs maxs on the vector + // components before computing distances and alpha values so that only one distance computation + // need be computed to determine the min alpha. + builder->fsCodeAppendf("\t\tvec2 dxy0 = %s.xy - %s.xy;\n", rectName, fragmentPos); + builder->fsCodeAppendf("\t\tvec2 dxy1 = %s.xy - %s.zw;\n", fragmentPos, rectName); + builder->fsCodeAppend("\t\tvec2 dxy = max(max(dxy0, dxy1), 0.0);\n"); + // Z is the x/y offsets divided by squared radii. + builder->fsCodeAppendf("\t\tvec2 Z = dxy * %s;\n", invRadiusXYSqdName); + // implicit is the evaluation of (x/a)^2 + (y/b)^2 - 1. + builder->fsCodeAppend("\t\tfloat implicit = dot(Z, dxy) - 1.0;\n"); + // grad_dot is the squared length of the gradient of the implicit. + builder->fsCodeAppendf("\t\tfloat grad_dot = 4.0 * dot(Z, Z);\n"); + builder->fsCodeAppend("\t\tgrad_dot = max(grad_dot, 1.0e-4);\n"); + builder->fsCodeAppendf("\t\tfloat approx_dist = implicit * inversesqrt(grad_dot);\n"); + + if (kFillAA_GrEffectEdgeType == erre.getEdgeType()) { + builder->fsCodeAppend("\t\tfloat alpha = clamp(0.5 - approx_dist, 0.0, 1.0);\n"); + } else { + builder->fsCodeAppend("\t\tfloat alpha = clamp(0.5 + approx_dist, 0.0, 1.0);\n"); + } + + builder->fsCodeAppendf("\t\t%s = %s;\n", outputColor, + (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_str()); +} + +GrGLEffect::EffectKey GLEllipticalRRectEffect::GenKey(const GrDrawEffect& drawEffect, + const GrGLCaps&) { + const EllipticalRRectEffect& erre = drawEffect.castEffect<EllipticalRRectEffect>(); + return erre.getEdgeType(); +} + +void GLEllipticalRRectEffect::setData(const GrGLUniformManager& uman, + const GrDrawEffect& drawEffect) { + const EllipticalRRectEffect& erre = drawEffect.castEffect<EllipticalRRectEffect>(); + const SkRRect& rrect = erre.getRRect(); + if (rrect != fPrevRRect) { + SkRect rect = rrect.getBounds(); + SkASSERT(rrect.isSimple()); + const SkVector& radius = rrect.getSimpleRadii(); + SkASSERT(radius.fX >= EllipticalRRectEffect::kRadiusMin); + SkASSERT(radius.fY >= EllipticalRRectEffect::kRadiusMin); + rect.inset(radius.fX, radius.fY); + uman.set4f(fInnerRectUniform, rect.fLeft, rect.fTop, rect.fRight, rect.fBottom); + uman.set2f(fInvRadiusXYSqdUniform, 1.f / (radius.fX * radius.fX), + 1.f / (radius.fY * radius.fY)); + fPrevRRect = rrect; + } +} + +////////////////////////////////////////////////////////////////////////////// + GrEffectRef* GrRRectEffect::Create(GrEffectEdgeType edgeType, const SkRRect& rrect) { if (kFillAA_GrEffectEdgeType != edgeType && kInverseFillAA_GrEffectEdgeType != edgeType) { return NULL; } uint32_t cornerFlags; - if (rrect.isSimpleCircular()) { - if (rrect.getSimpleRadii().fX < RRectEffect::kRadiusMin) { - return NULL; + if (rrect.isSimple()) { + if (rrect.getSimpleRadii().fX == rrect.getSimpleRadii().fY) { + if (rrect.getSimpleRadii().fX < CircularRRectEffect::kRadiusMin) { + return NULL; + } + cornerFlags = CircularRRectEffect::kAll_CornerFlags; + } else { + if (rrect.getSimpleRadii().fX < EllipticalRRectEffect::kRadiusMin || + rrect.getSimpleRadii().fY < EllipticalRRectEffect::kRadiusMin) { + return NULL; + } + return EllipticalRRectEffect::Create(edgeType, rrect); } - cornerFlags = RRectEffect::kAll_CornerFlags; } else if (rrect.isComplex()) { // Check for the "tab" cases - two adjacent circular corners and two square corners. SkScalar radius = 0; @@ -397,7 +604,7 @@ GrEffectRef* GrRRectEffect::Create(GrEffectEdgeType edgeType, const SkRRect& rre } if (!cornerFlags) { radius = r.fX; - if (radius < RRectEffect::kRadiusMin) { + if (radius < CircularRRectEffect::kRadiusMin) { return NULL; } cornerFlags = 1 << c; @@ -410,15 +617,15 @@ GrEffectRef* GrRRectEffect::Create(GrEffectEdgeType edgeType, const SkRRect& rre } switch (cornerFlags) { - case RRectEffect::kTopLeft_CornerFlag: - case RRectEffect::kTopRight_CornerFlag: - case RRectEffect::kBottomRight_CornerFlag: - case RRectEffect::kBottomLeft_CornerFlag: - case RRectEffect::kLeft_CornerFlags: - case RRectEffect::kTop_CornerFlags: - case RRectEffect::kRight_CornerFlags: - case RRectEffect::kBottom_CornerFlags: - case RRectEffect::kAll_CornerFlags: + case CircularRRectEffect::kTopLeft_CornerFlag: + case CircularRRectEffect::kTopRight_CornerFlag: + case CircularRRectEffect::kBottomRight_CornerFlag: + case CircularRRectEffect::kBottomLeft_CornerFlag: + case CircularRRectEffect::kLeft_CornerFlags: + case CircularRRectEffect::kTop_CornerFlags: + case CircularRRectEffect::kRight_CornerFlags: + case CircularRRectEffect::kBottom_CornerFlags: + case CircularRRectEffect::kAll_CornerFlags: break; default: return NULL; @@ -426,5 +633,5 @@ GrEffectRef* GrRRectEffect::Create(GrEffectEdgeType edgeType, const SkRRect& rre } else { return NULL; } - return RRectEffect::Create(edgeType, cornerFlags, rrect); + return CircularRRectEffect::Create(edgeType, cornerFlags, rrect); } |