Chromium Code Reviews| Index: src/gpu/effects/GrRRectEffect.cpp |
| diff --git a/src/gpu/effects/GrRRectEffect.cpp b/src/gpu/effects/GrRRectEffect.cpp |
| index 3b7cecaff0461a0e34182190146182c4c9903718..60faee9611c8423833b85d5f6a8d0941287e6738 100644 |
| --- a/src/gpu/effects/GrRRectEffect.cpp |
| +++ b/src/gpu/effects/GrRRectEffect.cpp |
| @@ -17,17 +17,27 @@ class GLRRectEffect; |
| class RRectEffect : public GrEffect { |
| public: |
| - // This effect only supports circular corner rrects where all corners have the same radius |
| - // which must be <= kRadiusMin. |
| + // This effect only supports circular corner rrects where the radius is >= kRadiusMin. |
| static const SkScalar kRadiusMin; |
| - static GrEffectRef* Create(const SkRRect&); |
| + /// The types of circular corner rrects supported |
| + enum Type { |
| + kCircleCorner_Type, //<! All four corners have the same circular radius. |
| + kLeftCircleTab_Type, //<! The left side has circular corners and the right is a rect. |
| + kTopCircleTab_Type, //<! etc |
| + kRightCircleTab_Type, |
| + kBottomCircleTab_Type, |
| + }; |
| + |
| + static GrEffectRef* Create(const SkRRect&, Type); |
| virtual ~RRectEffect() {}; |
| static const char* Name() { return "RRect"; } |
| const SkRRect& getRRect() const { return fRRect; } |
| + Type getType() const { return fType; } |
| + |
| typedef GLRRectEffect GLEffect; |
| virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE; |
| @@ -35,10 +45,11 @@ public: |
| virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE; |
| private: |
| - RRectEffect(const SkRRect&); |
| + RRectEffect(const SkRRect&, Type); |
| virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE; |
| + Type fType; |
| SkRRect fRRect; |
| GR_DECLARE_EFFECT_TEST; |
| @@ -48,8 +59,8 @@ private: |
| const SkScalar RRectEffect::kRadiusMin = 0.5f; |
| -GrEffectRef* RRectEffect::Create(const SkRRect& rrect) { |
| - return CreateEffectRef(AutoEffectUnref(SkNEW_ARGS(RRectEffect, (rrect)))); |
| +GrEffectRef* RRectEffect::Create(const SkRRect& rrect, Type type) { |
| + return CreateEffectRef(AutoEffectUnref(SkNEW_ARGS(RRectEffect, (rrect, type)))); |
| } |
| void RRectEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const { |
| @@ -60,13 +71,15 @@ const GrBackendEffectFactory& RRectEffect::getFactory() const { |
| return GrTBackendEffectFactory<RRectEffect>::getInstance(); |
| } |
| -RRectEffect::RRectEffect(const SkRRect& rrect) |
| - : fRRect(rrect) { |
| +RRectEffect::RRectEffect(const SkRRect& rrect, Type type) |
| + : fRRect(rrect) |
| + , fType(type) { |
| 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 fRRect == rre.fRRect; |
| } |
| @@ -101,7 +114,7 @@ public: |
| const TransformedCoordsArray&, |
| const TextureSamplerArray&) SK_OVERRIDE; |
| - static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&) { return 0; } |
| + static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&); |
| virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE; |
| @@ -125,10 +138,13 @@ void GLRRectEffect::emitCode(GrGLShaderBuilder* builder, |
| const char* inputColor, |
| const TransformedCoordsArray&, |
| const TextureSamplerArray& samplers) { |
| + const RRectEffect& rre = drawEffect.castEffect<RRectEffect>(); |
| const char *rectName; |
| const char *radiusPlusHalfName; |
| // The inner rect is the rrect bounds inset by the radius. Its top, left, right, and bottom |
| - // edges correspond to components x, y, z, and w, respectively. |
| + // edges correspond to components x, y, z, and w, respectively. When one side of the rrect has |
| + // rectangular corners, that side's value corresponds to the rect edge's value outset by half a |
| + // pixel. |
| fInnerRectUniform = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
| kVec4f_GrSLType, |
| "innerRect", |
| @@ -149,25 +165,103 @@ void GLRRectEffect::emitCode(GrGLShaderBuilder* builder, |
| // 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"); |
| - builder->fsCodeAppendf("\t\tfloat alpha = clamp(%s - length(dxy), 0.0, 1.0);\n", |
| - radiusPlusHalfName); |
| + // |
| + // 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.getType()) { |
| + case RRectEffect::kCircleCorner_Type: |
| + 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::kLeftCircleTab_Type: |
| + 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"); |
|
robertphillips
2014/02/21 19:26:36
\n?
bsalomon
2014/02/21 21:20:45
Done. x4
|
| + builder->fsCodeAppendf("\t\tfloat rightAlpha = clamp(%s.z - %s.x, 0.0, 1.0);\n", rectName, fragmentPos); |
| + builder->fsCodeAppendf("\t\tfloat alpha = rightAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n", |
| + radiusPlusHalfName); |
| + break; |
| + case RRectEffect::kTopCircleTab_Type: |
| + 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"); |
|
robertphillips
2014/02/21 19:26:36
\n?
|
| + builder->fsCodeAppendf("\t\tfloat bottomAlpha = clamp(%s.w - %s.y, 0.0, 1.0);\n", rectName, fragmentPos); |
| + builder->fsCodeAppendf("\t\tfloat alpha = bottomAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n", |
| + radiusPlusHalfName); |
| + break; |
| + case RRectEffect::kRightCircleTab_Type: |
| + 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"); |
|
robertphillips
2014/02/21 19:26:36
\n?
|
| + builder->fsCodeAppendf("\t\tfloat leftAlpha = clamp(%s.x - %s.x, 0.0, 1.0);\n", fragmentPos, rectName); |
| + builder->fsCodeAppendf("\t\tfloat alpha = leftAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n", |
| + radiusPlusHalfName); |
| + break; |
| + case RRectEffect::kBottomCircleTab_Type: |
| + 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"); |
|
robertphillips
2014/02/21 19:26:36
\n?
|
| + builder->fsCodeAppendf("\t\tfloat topAlpha = clamp(%s.y - %s.y, 0.0, 1.0);\n", fragmentPos, rectName); |
| + builder->fsCodeAppendf("\t\tfloat alpha = topAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n", |
| + radiusPlusHalfName); |
| + break; |
| + } |
| builder->fsCodeAppendf("\t\t%s = %s;\n", outputColor, |
| (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_str()); |
| } |
| +GrGLEffect::EffectKey GLRRectEffect::GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) { |
| + const RRectEffect& rre = drawEffect.castEffect<RRectEffect>(); |
| + return rre.getType(); |
| +} |
| + |
| void GLRRectEffect::setData(const GrGLUniformManager& uman, const GrDrawEffect& drawEffect) { |
| const RRectEffect& rre = drawEffect.castEffect<RRectEffect>(); |
| const SkRRect& rrect = rre.getRRect(); |
| if (rrect != fPrevRRect) { |
| - SkASSERT(rrect.isSimpleCircular()); |
| SkRect rect = rrect.getBounds(); |
| - SkScalar radius = rrect.getSimpleRadii().fX; |
| - SkASSERT(radius >= RRectEffect::kRadiusMin); |
| - rect.inset(radius, radius); |
| + SkScalar radius; |
| + switch (rre.getType()) { |
| + case RRectEffect::kCircleCorner_Type: |
| + SkASSERT(rrect.isSimpleCircular()); |
| + radius = rrect.getSimpleRadii().fX; |
| + SkASSERT(radius >= RRectEffect::kRadiusMin); |
| + rect.inset(radius, radius); |
| + break; |
| + case RRectEffect::kLeftCircleTab_Type: |
| + radius = rrect.radii(SkRRect::kUpperLeft_Corner).fX; |
| + rect.fLeft += radius; |
| + rect.fTop += radius; |
| + rect.fRight += 0.5f; |
| + rect.fBottom -= radius; |
| + break; |
| + case RRectEffect::kTopCircleTab_Type: |
| + radius = rrect.radii(SkRRect::kUpperLeft_Corner).fX; |
| + rect.fLeft += radius; |
| + rect.fTop += radius; |
| + rect.fRight -= radius; |
| + rect.fBottom += 0.5f; |
| + break; |
| + case RRectEffect::kRightCircleTab_Type: |
| + radius = rrect.radii(SkRRect::kUpperRight_Corner).fX; |
| + rect.fLeft -= 0.5f; |
| + rect.fTop += radius; |
| + rect.fRight -= radius; |
| + rect.fBottom -= radius; |
| + break; |
| + case RRectEffect::kBottomCircleTab_Type: |
| + radius = rrect.radii(SkRRect::kLowerLeft_Corner).fX; |
| + rect.fLeft += radius; |
| + rect.fTop -= 0.5f; |
| + rect.fRight -= radius; |
| + rect.fBottom -= radius; |
| + break; |
| + } |
| uman.set4f(fInnerRectUniform, rect.fLeft, rect.fTop, rect.fRight, rect.fBottom); |
| uman.set1f(fRadiusPlusHalfUniform, radius + 0.5f); |
| fPrevRRect = rrect; |
| @@ -177,13 +271,63 @@ void GLRRectEffect::setData(const GrGLUniformManager& uman, const GrDrawEffect& |
| ////////////////////////////////////////////////////////////////////////////// |
| GrEffectRef* GrRRectEffect::Create(const SkRRect& rrect) { |
| - if (!rrect.isSimpleCircular()) { |
| + RRectEffect::Type type; |
| + if (rrect.isSimpleCircular()) { |
| + if (rrect.getSimpleRadii().fX < RRectEffect::kRadiusMin) { |
| + return NULL; |
| + } |
| + type = RRectEffect::kCircleCorner_Type; |
| + } else if (rrect.isComplex()) { |
| + // Check for the "tab" cases - two adjacent circular corners and two square corners. |
| + SkScalar radius; |
| + int circleCornerBitfield = 0; |
| + for (int c = 0; c < 4; ++c) { |
| + const SkVector& r = rrect.radii((SkRRect::Corner)c); |
| + SkASSERT((0 == r.fX) == (0 == r.fY)); |
| + if (0 == r.fX) { |
| + continue; |
| + } |
| + if (r.fX != r.fY) { |
| + circleCornerBitfield = 0; |
|
robertphillips
2014/02/21 19:26:36
just return NULL here?
bsalomon
2014/02/21 21:20:45
Done. x3
|
| + break; |
| + } |
| + if (!circleCornerBitfield) { |
| + radius = r.fX; |
| + if (radius < RRectEffect::kRadiusMin) { |
|
robertphillips
2014/02/21 19:26:36
can't we just return NULL here?
|
| + break; |
| + } |
| + circleCornerBitfield = 1 << c; |
| + } else { |
| + if (r.fX != radius) { |
| + circleCornerBitfield = 0; |
| + break; |
| + } |
| + circleCornerBitfield |= 1 << c; |
| + } |
| + } |
| + |
| + GR_STATIC_ASSERT(SkRRect::kUpperLeft_Corner == 0); |
| + GR_STATIC_ASSERT(SkRRect::kUpperRight_Corner == 1); |
| + GR_STATIC_ASSERT(SkRRect::kLowerRight_Corner == 2); |
| + GR_STATIC_ASSERT(SkRRect::kLowerLeft_Corner == 3); |
| + switch (circleCornerBitfield) { |
| + case 3: |
| + type = RRectEffect::kTopCircleTab_Type; |
| + break; |
| + case 6: |
| + type = RRectEffect::kRightCircleTab_Type; |
| + break; |
| + case 9: |
| + type = RRectEffect::kLeftCircleTab_Type; |
| + break; |
| + case 12: |
| + type = RRectEffect::kBottomCircleTab_Type; |
| + break; |
| + default: |
| + return NULL; |
| + } |
| + } else { |
| return NULL; |
| } |
| - |
| - if (rrect.getSimpleRadii().fX < RRectEffect::kRadiusMin) { |
| - return NULL; |
| - } |
| - |
| - return RRectEffect::Create(rrect); |
| + return RRectEffect::Create(rrect, type); |
| } |