diff options
-rw-r--r-- | gm/rrects.cpp | 121 | ||||
-rw-r--r-- | include/core/SkRRect.h | 7 | ||||
-rw-r--r-- | src/gpu/GrClipMaskManager.cpp | 2 | ||||
-rw-r--r-- | src/gpu/effects/GrRRectEffect.cpp | 86 | ||||
-rw-r--r-- | src/gpu/effects/GrRRectEffect.h | 11 |
5 files changed, 161 insertions, 66 deletions
diff --git a/gm/rrects.cpp b/gm/rrects.cpp index 35120c119e..4a1e29762b 100644 --- a/gm/rrects.cpp +++ b/gm/rrects.cpp @@ -6,7 +6,10 @@ */ #include "gm.h" +#include "GrTest.h" +#include "SkDevice.h" #include "SkRRect.h" +#include "effects/GrRRectEffect.h" namespace skiagm { @@ -14,51 +17,115 @@ namespace skiagm { class RRectGM : public GM { public: - RRectGM(bool doAA, bool doClip) : fDoAA(doAA), fDoClip(doClip) { + enum Type { + kBW_Draw_Type, + kAA_Draw_Type, + kBW_Clip_Type, + kAA_Clip_Type, + kEffect_Type, + }; + RRectGM(Type type) : fType(type) { this->setBGColor(0xFFDDDDDD); this->setUpRRects(); } protected: - SkString onShortName() { + SkString onShortName() SK_OVERRIDE { SkString name("rrect"); - if (fDoClip) { - name.append("_clip"); + switch (fType) { + case kBW_Draw_Type: + name.append("_draw_bw"); + break; + case kAA_Draw_Type: + name.append("_draw_aa"); + break; + case kBW_Clip_Type: + name.append("_clip_bw"); + break; + case kAA_Clip_Type: + name.append("_clip_aa"); + break; + case kEffect_Type: + name.append("_effect"); + break; } - if (fDoAA) { - name.append("_aa"); - } else { - name.append("_bw"); - } - return name; } - virtual SkISize onISize() { return make_isize(kImageWidth, kImageHeight); } + virtual SkISize onISize() SK_OVERRIDE { return make_isize(kImageWidth, kImageHeight); } - virtual void onDraw(SkCanvas* canvas) { + virtual uint32_t onGetFlags() const SK_OVERRIDE { + if (kEffect_Type == fType) { + return kGPUOnly_Flag; + } else { + return 0; + } + } - SkPaint paint; - // when clipping the AA is pushed into the clip operation - paint.setAntiAlias(fDoClip ? false : fDoAA); + virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { + SkBaseDevice* device = canvas->getTopDevice(); + GrContext* context = NULL; + GrRenderTarget* rt = device->accessRenderTarget(); + if (NULL != rt) { + context = rt->getContext(); + } + if (kEffect_Type == fType && NULL == context) { + return; + } + SkPaint paint; + if (kAA_Draw_Type == fType) { + paint.setAntiAlias(true); + } + static const SkRect kMaxTileBound = SkRect::MakeWH(SkIntToScalar(kTileX), SkIntToScalar(kTileY)); int curRRect = 0; + int numRRects = kNumRRects; + if (kEffect_Type == fType) { + numRRects *= GrRRectEffect::kEdgeTypeCnt; + } for (int y = 1; y < kImageHeight; y += kTileY) { for (int x = 1; x < kImageWidth; x += kTileX) { - if (curRRect >= kNumRRects) { + if (curRRect >= numRRects) { break; } - SkASSERT(kMaxTileBound.contains(fRRects[curRRect].getBounds())); + int rrectIdx = curRRect % kNumRRects; + SkASSERT(kMaxTileBound.contains(fRRects[rrectIdx].getBounds())); canvas->save(); canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); - if (fDoClip) { - canvas->clipRRect(fRRects[curRRect], SkRegion::kReplace_Op, fDoAA); + if (kEffect_Type == fType) { + GrTestTarget tt; + context->getTestTarget(&tt); + if (NULL == tt.target()) { + SkDEBUGFAIL("Couldn't get Gr test target."); + return; + } + GrDrawState* drawState = tt.target()->drawState(); + + SkRRect rrect = fRRects[rrectIdx]; + rrect.offset(SkIntToScalar(x), SkIntToScalar(y)); + GrRRectEffect::EdgeType edgeType = (GrRRectEffect::EdgeType) + (curRRect / kNumRRects); + SkAutoTUnref<GrEffectRef> effect(GrRRectEffect::Create(edgeType, rrect)); + if (effect) { + drawState->addCoverageEffect(effect); + drawState->setIdentityViewMatrix(); + drawState->setRenderTarget(rt); + drawState->setColor(0xff000000); + + SkRect bounds = rrect.getBounds(); + bounds.outset(2.f, 2.f); + + tt.target()->drawSimpleRect(bounds); + } + } else if (kBW_Clip_Type == fType || kAA_Clip_Type == fType) { + bool aaClip = (kAA_Clip_Type == fType); + canvas->clipRRect(fRRects[rrectIdx], SkRegion::kReplace_Op, aaClip); canvas->drawRect(kMaxTileBound, paint); } else { - canvas->drawRRect(fRRects[curRRect], paint); + canvas->drawRRect(fRRects[rrectIdx], paint); } ++curRRect; canvas->restore(); @@ -88,9 +155,8 @@ protected: } private: - bool fDoAA; - bool fDoClip; // use clipRRect & drawRect instead of drawRRect - + Type fType; + static const int kImageWidth = 640; static const int kImageHeight = 480; @@ -151,9 +217,10 @@ const SkVector RRectGM::gRadii[kNumComplexCases][4] = { /////////////////////////////////////////////////////////////////////////////// -DEF_GM( return new RRectGM(false, false); ) -DEF_GM( return new RRectGM(true, false); ) -DEF_GM( return new RRectGM(false, true); ) -DEF_GM( return new RRectGM(true, true); ) +DEF_GM( return new RRectGM(RRectGM::kAA_Draw_Type); ) +DEF_GM( return new RRectGM(RRectGM::kBW_Draw_Type); ) +DEF_GM( return new RRectGM(RRectGM::kAA_Clip_Type); ) +DEF_GM( return new RRectGM(RRectGM::kBW_Clip_Type); ) +DEF_GM( return new RRectGM(RRectGM::kEffect_Type); ) } diff --git a/include/core/SkRRect.h b/include/core/SkRRect.h index 141a3d27af..257c8236e2 100644 --- a/include/core/SkRRect.h +++ b/include/core/SkRRect.h @@ -224,6 +224,13 @@ public: void outset(SkScalar dx, SkScalar dy) { this->inset(-dx, -dy, this); } + + /** + * Translate the rrect by (dx, dy). + */ + void offset(SkScalar dx, SkScalar dy) { + fRect.offset(dx, dy); + } /** * Returns true if 'rect' is wholy inside the RR, and both diff --git a/src/gpu/GrClipMaskManager.cpp b/src/gpu/GrClipMaskManager.cpp index 90106fa7e0..9b95d33c5a 100644 --- a/src/gpu/GrClipMaskManager.cpp +++ b/src/gpu/GrClipMaskManager.cpp @@ -192,7 +192,7 @@ bool GrClipMaskManager::setupClipping(const GrClipData* clipDataIn, } } else if (isAA && SkClipStack::Element::kRRect_Type == type && !rt->isMultisampled()) { const SkRRect& rrect = elements.tail()->getRRect(); - effect.reset(GrRRectEffect::Create(rrect)); + effect.reset(GrRRectEffect::Create(GrRRectEffect::kFillAA_EdgeType, rrect)); } else if (isAA && SkClipStack::Element::kRect_Type == type && !rt->isMultisampled()) { // We only handle AA/non-MSAA rects here. Coverage effect AA isn't MSAA friendly and // non-AA rect clips are handled by the scissor. diff --git a/src/gpu/effects/GrRRectEffect.cpp b/src/gpu/effects/GrRRectEffect.cpp index 7a6554b88f..583e385869 100644 --- a/src/gpu/effects/GrRRectEffect.cpp +++ b/src/gpu/effects/GrRRectEffect.cpp @@ -13,30 +13,34 @@ #include "SkRRect.h" +using namespace GrRRectEffect; + class GLRRectEffect; class RRectEffect : public GrEffect { public: // This effect only supports circular corner rrects where the radius is >= kRadiusMin. static const SkScalar kRadiusMin; - + /// 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, + enum RRectType { + kCircleCorner_RRectType, //<! All four corners have the same circular radius. + kLeftCircleTab_RRectType, //<! The left side has circular corners, the right is a rect. + kTopCircleTab_RRectType, //<! etc + kRightCircleTab_RRectType, + kBottomCircleTab_RRectType, }; - static GrEffectRef* Create(const SkRRect&, Type); + static GrEffectRef* Create(EdgeType, const SkRRect&, RRectType); virtual ~RRectEffect() {}; static const char* Name() { return "RRect"; } const SkRRect& getRRect() const { return fRRect; } - Type getType() const { return fType; } + RRectType getType() const { return fRRectType; } + + EdgeType getEdgeType() const { return fEdgeType; } typedef GLRRectEffect GLEffect; @@ -45,12 +49,13 @@ public: virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE; private: - RRectEffect(const SkRRect&, Type); + RRectEffect(EdgeType, const SkRRect&, RRectType); virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE; - SkRRect fRRect; - Type fType; + SkRRect fRRect; + EdgeType fEdgeType; + RRectType fRRectType; GR_DECLARE_EFFECT_TEST; @@ -59,8 +64,8 @@ private: const SkScalar RRectEffect::kRadiusMin = 0.5f; -GrEffectRef* RRectEffect::Create(const SkRRect& rrect, Type type) { - return CreateEffectRef(AutoEffectUnref(SkNEW_ARGS(RRectEffect, (rrect, type)))); +GrEffectRef* RRectEffect::Create(EdgeType edgeType, const SkRRect& rrect, RRectType rrtype) { + return CreateEffectRef(AutoEffectUnref(SkNEW_ARGS(RRectEffect, (edgeType, rrect, rrtype)))); } void RRectEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const { @@ -71,16 +76,17 @@ const GrBackendEffectFactory& RRectEffect::getFactory() const { return GrTBackendEffectFactory<RRectEffect>::getInstance(); } -RRectEffect::RRectEffect(const SkRRect& rrect, Type type) +RRectEffect::RRectEffect(EdgeType edgeType, const SkRRect& rrect, RRectType rrtype) : fRRect(rrect) - , fType(type) { + , fEdgeType(edgeType) + , fRRectType(rrtype) { 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; + return fEdgeType == rre.fEdgeType && fRRect == rre.fRRect; } ////////////////////////////////////////////////////////////////////////////// @@ -94,10 +100,11 @@ GrEffectRef* RRectEffect::TestCreate(SkRandom* random, SkScalar w = random->nextRangeScalar(20.f, 1000.f); SkScalar h = random->nextRangeScalar(20.f, 1000.f); SkScalar r = random->nextRangeF(kRadiusMin, 9.f); + EdgeType et = (EdgeType) random->nextULessThan(kEdgeTypeCnt); SkRRect rrect; rrect.setRectXY(SkRect::MakeWH(w, h), r, r); - return GrRRectEffect::Create(rrect); + return GrRRectEffect::Create(et, rrect); } ////////////////////////////////////////////////////////////////////////////// @@ -170,14 +177,14 @@ void GLRRectEffect::emitCode(GrGLShaderBuilder* builder, // 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: + case RRectEffect::kCircleCorner_RRectType: 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: + case RRectEffect::kLeftCircleTab_RRectType: 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"); @@ -186,7 +193,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::kTopCircleTab_Type: + case RRectEffect::kTopCircleTab_RRectType: 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"); @@ -195,7 +202,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::kRightCircleTab_Type: + case RRectEffect::kRightCircleTab_RRectType: 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"); @@ -204,7 +211,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::kBottomCircleTab_Type: + case RRectEffect::kBottomCircleTab_RRectType: 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"); @@ -214,6 +221,10 @@ void GLRRectEffect::emitCode(GrGLShaderBuilder* builder, radiusPlusHalfName); break; } + + if (kInverseFillAA_EdgeType == rre.getEdgeType()) { + builder->fsCodeAppend("\t\talpha = 1.0 - alpha;\n"); + } builder->fsCodeAppendf("\t\t%s = %s;\n", outputColor, (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_str()); @@ -221,7 +232,8 @@ void GLRRectEffect::emitCode(GrGLShaderBuilder* builder, GrGLEffect::EffectKey GLRRectEffect::GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) { const RRectEffect& rre = drawEffect.castEffect<RRectEffect>(); - return rre.getType(); + GR_STATIC_ASSERT(kEdgeTypeCnt <= 4); + return (rre.getType() << 2) | rre.getEdgeType(); } void GLRRectEffect::setData(const GrGLUniformManager& uman, const GrDrawEffect& drawEffect) { @@ -231,34 +243,34 @@ void GLRRectEffect::setData(const GrGLUniformManager& uman, const GrDrawEffect& SkRect rect = rrect.getBounds(); SkScalar radius = 0; switch (rre.getType()) { - case RRectEffect::kCircleCorner_Type: + case RRectEffect::kCircleCorner_RRectType: SkASSERT(rrect.isSimpleCircular()); radius = rrect.getSimpleRadii().fX; SkASSERT(radius >= RRectEffect::kRadiusMin); rect.inset(radius, radius); break; - case RRectEffect::kLeftCircleTab_Type: + case RRectEffect::kLeftCircleTab_RRectType: 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: + case RRectEffect::kTopCircleTab_RRectType: 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: + case RRectEffect::kRightCircleTab_RRectType: 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: + case RRectEffect::kBottomCircleTab_RRectType: radius = rrect.radii(SkRRect::kLowerLeft_Corner).fX; rect.fLeft += radius; rect.fTop -= 0.5f; @@ -274,13 +286,13 @@ void GLRRectEffect::setData(const GrGLUniformManager& uman, const GrDrawEffect& ////////////////////////////////////////////////////////////////////////////// -GrEffectRef* GrRRectEffect::Create(const SkRRect& rrect) { - RRectEffect::Type type; +GrEffectRef* GrRRectEffect::Create(EdgeType edgeType, const SkRRect& rrect) { + RRectEffect::RRectType rrtype; if (rrect.isSimpleCircular()) { if (rrect.getSimpleRadii().fX < RRectEffect::kRadiusMin) { return NULL; } - type = RRectEffect::kCircleCorner_Type; + rrtype = RRectEffect::kCircleCorner_RRectType; } else if (rrect.isComplex()) { // Check for the "tab" cases - two adjacent circular corners and two square corners. SkScalar radius = 0; @@ -314,16 +326,16 @@ GrEffectRef* GrRRectEffect::Create(const SkRRect& rrect) { GR_STATIC_ASSERT(SkRRect::kLowerLeft_Corner == 3); switch (circleCornerBitfield) { case 3: - type = RRectEffect::kTopCircleTab_Type; + rrtype = RRectEffect::kTopCircleTab_RRectType; break; case 6: - type = RRectEffect::kRightCircleTab_Type; + rrtype = RRectEffect::kRightCircleTab_RRectType; break; case 9: - type = RRectEffect::kLeftCircleTab_Type; + rrtype = RRectEffect::kLeftCircleTab_RRectType; break; case 12: - type = RRectEffect::kBottomCircleTab_Type; + rrtype = RRectEffect::kBottomCircleTab_RRectType; break; default: return NULL; @@ -331,5 +343,5 @@ GrEffectRef* GrRRectEffect::Create(const SkRRect& rrect) { } else { return NULL; } - return RRectEffect::Create(rrect, type); + return RRectEffect::Create(edgeType, rrect, rrtype); } diff --git a/src/gpu/effects/GrRRectEffect.h b/src/gpu/effects/GrRRectEffect.h index 45dac0154f..7df7e6f076 100644 --- a/src/gpu/effects/GrRRectEffect.h +++ b/src/gpu/effects/GrRRectEffect.h @@ -14,11 +14,20 @@ class GrEffectRef; class SkRRect; namespace GrRRectEffect { + enum EdgeType { + kFillAA_EdgeType, + kInverseFillAA_EdgeType, + + kLastEdgeType = kInverseFillAA_EdgeType, + }; + + static const int kEdgeTypeCnt = kLastEdgeType + 1; + /** * Creates an effect that performs anti-aliased clipping against a SkRRect. It doesn't support * all varieties of SkRRect so the caller must check for a NULL return. */ - GrEffectRef* Create(const SkRRect&); + GrEffectRef* Create(EdgeType, const SkRRect&); }; #endif |