michael@0: /* michael@0: * Copyright 2013 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #include "GrOvalRenderer.h" michael@0: michael@0: #include "GrEffect.h" michael@0: #include "gl/GrGLEffect.h" michael@0: #include "gl/GrGLSL.h" michael@0: #include "gl/GrGLVertexEffect.h" michael@0: #include "GrTBackendEffectFactory.h" michael@0: michael@0: #include "GrDrawState.h" michael@0: #include "GrDrawTarget.h" michael@0: #include "GrGpu.h" michael@0: michael@0: #include "SkRRect.h" michael@0: #include "SkStrokeRec.h" michael@0: michael@0: #include "effects/GrVertexEffect.h" michael@0: michael@0: namespace { michael@0: michael@0: struct CircleVertex { michael@0: GrPoint fPos; michael@0: GrPoint fOffset; michael@0: SkScalar fOuterRadius; michael@0: SkScalar fInnerRadius; michael@0: }; michael@0: michael@0: struct EllipseVertex { michael@0: GrPoint fPos; michael@0: GrPoint fOffset; michael@0: GrPoint fOuterRadii; michael@0: GrPoint fInnerRadii; michael@0: }; michael@0: michael@0: struct DIEllipseVertex { michael@0: GrPoint fPos; michael@0: GrPoint fOuterOffset; michael@0: GrPoint fInnerOffset; michael@0: }; michael@0: michael@0: inline bool circle_stays_circle(const SkMatrix& m) { michael@0: return m.isSimilarity(); michael@0: } michael@0: michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: /** michael@0: * The output of this effect is a modulation of the input color and coverage for a circle, michael@0: * specified as offset_x, offset_y (both from center point), outer radius and inner radius. michael@0: */ michael@0: michael@0: class CircleEdgeEffect : public GrVertexEffect { michael@0: public: michael@0: static GrEffectRef* Create(bool stroke) { michael@0: GR_CREATE_STATIC_EFFECT(gCircleStrokeEdge, CircleEdgeEffect, (true)); michael@0: GR_CREATE_STATIC_EFFECT(gCircleFillEdge, CircleEdgeEffect, (false)); michael@0: michael@0: if (stroke) { michael@0: gCircleStrokeEdge->ref(); michael@0: return gCircleStrokeEdge; michael@0: } else { michael@0: gCircleFillEdge->ref(); michael@0: return gCircleFillEdge; michael@0: } michael@0: } michael@0: michael@0: virtual void getConstantColorComponents(GrColor* color, michael@0: uint32_t* validFlags) const SK_OVERRIDE { michael@0: *validFlags = 0; michael@0: } michael@0: michael@0: virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { michael@0: return GrTBackendEffectFactory::getInstance(); michael@0: } michael@0: michael@0: virtual ~CircleEdgeEffect() {} michael@0: michael@0: static const char* Name() { return "CircleEdge"; } michael@0: michael@0: inline bool isStroked() const { return fStroke; } michael@0: michael@0: class GLEffect : public GrGLVertexEffect { michael@0: public: michael@0: GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&) michael@0: : INHERITED (factory) {} michael@0: michael@0: virtual void emitCode(GrGLFullShaderBuilder* builder, michael@0: const GrDrawEffect& drawEffect, michael@0: EffectKey key, michael@0: const char* outputColor, michael@0: const char* inputColor, michael@0: const TransformedCoordsArray&, michael@0: const TextureSamplerArray& samplers) SK_OVERRIDE { michael@0: const CircleEdgeEffect& circleEffect = drawEffect.castEffect(); michael@0: const char *vsName, *fsName; michael@0: builder->addVarying(kVec4f_GrSLType, "CircleEdge", &vsName, &fsName); michael@0: michael@0: const SkString* attrName = michael@0: builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]); michael@0: builder->vsCodeAppendf("\t%s = %s;\n", vsName, attrName->c_str()); michael@0: michael@0: builder->fsCodeAppendf("\tfloat d = length(%s.xy);\n", fsName); michael@0: builder->fsCodeAppendf("\tfloat edgeAlpha = clamp(%s.z - d, 0.0, 1.0);\n", fsName); michael@0: if (circleEffect.isStroked()) { michael@0: builder->fsCodeAppendf("\tfloat innerAlpha = clamp(d - %s.w, 0.0, 1.0);\n", fsName); michael@0: builder->fsCodeAppend("\tedgeAlpha *= innerAlpha;\n"); michael@0: } michael@0: michael@0: builder->fsCodeAppendf("\t%s = %s;\n", outputColor, michael@0: (GrGLSLExpr4(inputColor) * GrGLSLExpr1("edgeAlpha")).c_str()); michael@0: } michael@0: michael@0: static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) { michael@0: const CircleEdgeEffect& circleEffect = drawEffect.castEffect(); michael@0: michael@0: return circleEffect.isStroked() ? 0x1 : 0x0; michael@0: } michael@0: michael@0: virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {} michael@0: michael@0: private: michael@0: typedef GrGLVertexEffect INHERITED; michael@0: }; michael@0: michael@0: michael@0: private: michael@0: CircleEdgeEffect(bool stroke) : GrVertexEffect() { michael@0: this->addVertexAttrib(kVec4f_GrSLType); michael@0: fStroke = stroke; michael@0: } michael@0: michael@0: virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE { michael@0: const CircleEdgeEffect& cee = CastEffect(other); michael@0: return cee.fStroke == fStroke; michael@0: } michael@0: michael@0: bool fStroke; michael@0: michael@0: GR_DECLARE_EFFECT_TEST; michael@0: michael@0: typedef GrVertexEffect INHERITED; michael@0: }; michael@0: michael@0: GR_DEFINE_EFFECT_TEST(CircleEdgeEffect); michael@0: michael@0: GrEffectRef* CircleEdgeEffect::TestCreate(SkRandom* random, michael@0: GrContext* context, michael@0: const GrDrawTargetCaps&, michael@0: GrTexture* textures[]) { michael@0: return CircleEdgeEffect::Create(random->nextBool()); michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: /** michael@0: * The output of this effect is a modulation of the input color and coverage for an axis-aligned michael@0: * ellipse, specified as a 2D offset from center, and the reciprocals of the outer and inner radii, michael@0: * in both x and y directions. michael@0: * michael@0: * We are using an implicit function of x^2/a^2 + y^2/b^2 - 1 = 0. michael@0: */ michael@0: michael@0: class EllipseEdgeEffect : public GrVertexEffect { michael@0: public: michael@0: static GrEffectRef* Create(bool stroke) { michael@0: GR_CREATE_STATIC_EFFECT(gEllipseStrokeEdge, EllipseEdgeEffect, (true)); michael@0: GR_CREATE_STATIC_EFFECT(gEllipseFillEdge, EllipseEdgeEffect, (false)); michael@0: michael@0: if (stroke) { michael@0: gEllipseStrokeEdge->ref(); michael@0: return gEllipseStrokeEdge; michael@0: } else { michael@0: gEllipseFillEdge->ref(); michael@0: return gEllipseFillEdge; michael@0: } michael@0: } michael@0: michael@0: virtual void getConstantColorComponents(GrColor* color, michael@0: uint32_t* validFlags) const SK_OVERRIDE { michael@0: *validFlags = 0; michael@0: } michael@0: michael@0: virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { michael@0: return GrTBackendEffectFactory::getInstance(); michael@0: } michael@0: michael@0: virtual ~EllipseEdgeEffect() {} michael@0: michael@0: static const char* Name() { return "EllipseEdge"; } michael@0: michael@0: inline bool isStroked() const { return fStroke; } michael@0: michael@0: class GLEffect : public GrGLVertexEffect { michael@0: public: michael@0: GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&) michael@0: : INHERITED (factory) {} michael@0: michael@0: virtual void emitCode(GrGLFullShaderBuilder* builder, michael@0: const GrDrawEffect& drawEffect, michael@0: EffectKey key, michael@0: const char* outputColor, michael@0: const char* inputColor, michael@0: const TransformedCoordsArray&, michael@0: const TextureSamplerArray& samplers) SK_OVERRIDE { michael@0: const EllipseEdgeEffect& ellipseEffect = drawEffect.castEffect(); michael@0: michael@0: const char *vsOffsetName, *fsOffsetName; michael@0: const char *vsRadiiName, *fsRadiiName; michael@0: michael@0: builder->addVarying(kVec2f_GrSLType, "EllipseOffsets", &vsOffsetName, &fsOffsetName); michael@0: const SkString* attr0Name = michael@0: builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]); michael@0: builder->vsCodeAppendf("\t%s = %s;\n", vsOffsetName, attr0Name->c_str()); michael@0: michael@0: builder->addVarying(kVec4f_GrSLType, "EllipseRadii", &vsRadiiName, &fsRadiiName); michael@0: const SkString* attr1Name = michael@0: builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[1]); michael@0: builder->vsCodeAppendf("\t%s = %s;\n", vsRadiiName, attr1Name->c_str()); michael@0: michael@0: // for outer curve michael@0: builder->fsCodeAppendf("\tvec2 scaledOffset = %s*%s.xy;\n", fsOffsetName, fsRadiiName); michael@0: builder->fsCodeAppend("\tfloat test = dot(scaledOffset, scaledOffset) - 1.0;\n"); michael@0: builder->fsCodeAppendf("\tvec2 grad = 2.0*scaledOffset*%s.xy;\n", fsRadiiName); michael@0: builder->fsCodeAppend("\tfloat grad_dot = dot(grad, grad);\n"); michael@0: // we need to clamp the length^2 of the gradiant vector to a non-zero value, because michael@0: // on the Nexus 4 the undefined result of inversesqrt(0) drops out an entire tile michael@0: // TODO: restrict this to Adreno-only michael@0: builder->fsCodeAppend("\tgrad_dot = max(grad_dot, 1.0e-4);\n"); michael@0: builder->fsCodeAppend("\tfloat invlen = inversesqrt(grad_dot);\n"); michael@0: builder->fsCodeAppend("\tfloat edgeAlpha = clamp(0.5-test*invlen, 0.0, 1.0);\n"); michael@0: michael@0: // for inner curve michael@0: if (ellipseEffect.isStroked()) { michael@0: builder->fsCodeAppendf("\tscaledOffset = %s*%s.zw;\n", fsOffsetName, fsRadiiName); michael@0: builder->fsCodeAppend("\ttest = dot(scaledOffset, scaledOffset) - 1.0;\n"); michael@0: builder->fsCodeAppendf("\tgrad = 2.0*scaledOffset*%s.zw;\n", fsRadiiName); michael@0: builder->fsCodeAppend("\tinvlen = inversesqrt(dot(grad, grad));\n"); michael@0: builder->fsCodeAppend("\tedgeAlpha *= clamp(0.5+test*invlen, 0.0, 1.0);\n"); michael@0: } michael@0: michael@0: builder->fsCodeAppendf("\t%s = %s;\n", outputColor, michael@0: (GrGLSLExpr4(inputColor) * GrGLSLExpr1("edgeAlpha")).c_str()); michael@0: } michael@0: michael@0: static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) { michael@0: const EllipseEdgeEffect& ellipseEffect = drawEffect.castEffect(); michael@0: michael@0: return ellipseEffect.isStroked() ? 0x1 : 0x0; michael@0: } michael@0: michael@0: virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE { michael@0: } michael@0: michael@0: private: michael@0: typedef GrGLVertexEffect INHERITED; michael@0: }; michael@0: michael@0: private: michael@0: EllipseEdgeEffect(bool stroke) : GrVertexEffect() { michael@0: this->addVertexAttrib(kVec2f_GrSLType); michael@0: this->addVertexAttrib(kVec4f_GrSLType); michael@0: fStroke = stroke; michael@0: } michael@0: michael@0: virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE { michael@0: const EllipseEdgeEffect& eee = CastEffect(other); michael@0: return eee.fStroke == fStroke; michael@0: } michael@0: michael@0: bool fStroke; michael@0: michael@0: GR_DECLARE_EFFECT_TEST; michael@0: michael@0: typedef GrVertexEffect INHERITED; michael@0: }; michael@0: michael@0: GR_DEFINE_EFFECT_TEST(EllipseEdgeEffect); michael@0: michael@0: GrEffectRef* EllipseEdgeEffect::TestCreate(SkRandom* random, michael@0: GrContext* context, michael@0: const GrDrawTargetCaps&, michael@0: GrTexture* textures[]) { michael@0: return EllipseEdgeEffect::Create(random->nextBool()); michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: /** michael@0: * The output of this effect is a modulation of the input color and coverage for an ellipse, michael@0: * specified as a 2D offset from center for both the outer and inner paths (if stroked). The michael@0: * implict equation used is for a unit circle (x^2 + y^2 - 1 = 0) and the edge corrected by michael@0: * using differentials. michael@0: * michael@0: * The result is device-independent and can be used with any affine matrix. michael@0: */ michael@0: michael@0: class DIEllipseEdgeEffect : public GrVertexEffect { michael@0: public: michael@0: enum Mode { kStroke = 0, kHairline, kFill }; michael@0: michael@0: static GrEffectRef* Create(Mode mode) { michael@0: GR_CREATE_STATIC_EFFECT(gEllipseStrokeEdge, DIEllipseEdgeEffect, (kStroke)); michael@0: GR_CREATE_STATIC_EFFECT(gEllipseHairlineEdge, DIEllipseEdgeEffect, (kHairline)); michael@0: GR_CREATE_STATIC_EFFECT(gEllipseFillEdge, DIEllipseEdgeEffect, (kFill)); michael@0: michael@0: if (kStroke == mode) { michael@0: gEllipseStrokeEdge->ref(); michael@0: return gEllipseStrokeEdge; michael@0: } else if (kHairline == mode) { michael@0: gEllipseHairlineEdge->ref(); michael@0: return gEllipseHairlineEdge; michael@0: } else { michael@0: gEllipseFillEdge->ref(); michael@0: return gEllipseFillEdge; michael@0: } michael@0: } michael@0: michael@0: virtual void getConstantColorComponents(GrColor* color, michael@0: uint32_t* validFlags) const SK_OVERRIDE { michael@0: *validFlags = 0; michael@0: } michael@0: michael@0: virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { michael@0: return GrTBackendEffectFactory::getInstance(); michael@0: } michael@0: michael@0: virtual ~DIEllipseEdgeEffect() {} michael@0: michael@0: static const char* Name() { return "DIEllipseEdge"; } michael@0: michael@0: inline Mode getMode() const { return fMode; } michael@0: michael@0: class GLEffect : public GrGLVertexEffect { michael@0: public: michael@0: GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&) michael@0: : INHERITED (factory) {} michael@0: michael@0: virtual void emitCode(GrGLFullShaderBuilder* builder, michael@0: const GrDrawEffect& drawEffect, michael@0: EffectKey key, michael@0: const char* outputColor, michael@0: const char* inputColor, michael@0: const TransformedCoordsArray&, michael@0: const TextureSamplerArray& samplers) SK_OVERRIDE { michael@0: const DIEllipseEdgeEffect& ellipseEffect = drawEffect.castEffect(); michael@0: michael@0: SkAssertResult(builder->enableFeature( michael@0: GrGLShaderBuilder::kStandardDerivatives_GLSLFeature)); michael@0: michael@0: const char *vsOffsetName0, *fsOffsetName0; michael@0: builder->addVarying(kVec2f_GrSLType, "EllipseOffsets0", michael@0: &vsOffsetName0, &fsOffsetName0); michael@0: const SkString* attr0Name = michael@0: builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]); michael@0: builder->vsCodeAppendf("\t%s = %s;\n", vsOffsetName0, attr0Name->c_str()); michael@0: const char *vsOffsetName1, *fsOffsetName1; michael@0: builder->addVarying(kVec2f_GrSLType, "EllipseOffsets1", michael@0: &vsOffsetName1, &fsOffsetName1); michael@0: const SkString* attr1Name = michael@0: builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[1]); michael@0: builder->vsCodeAppendf("\t%s = %s;\n", vsOffsetName1, attr1Name->c_str()); michael@0: michael@0: // for outer curve michael@0: builder->fsCodeAppendf("\tvec2 scaledOffset = %s.xy;\n", fsOffsetName0); michael@0: builder->fsCodeAppend("\tfloat test = dot(scaledOffset, scaledOffset) - 1.0;\n"); michael@0: builder->fsCodeAppendf("\tvec2 duvdx = dFdx(%s);\n", fsOffsetName0); michael@0: builder->fsCodeAppendf("\tvec2 duvdy = dFdy(%s);\n", fsOffsetName0); michael@0: builder->fsCodeAppendf("\tvec2 grad = vec2(2.0*%s.x*duvdx.x + 2.0*%s.y*duvdx.y,\n" michael@0: "\t 2.0*%s.x*duvdy.x + 2.0*%s.y*duvdy.y);\n", michael@0: fsOffsetName0, fsOffsetName0, fsOffsetName0, fsOffsetName0); michael@0: michael@0: builder->fsCodeAppend("\tfloat grad_dot = dot(grad, grad);\n"); michael@0: // we need to clamp the length^2 of the gradiant vector to a non-zero value, because michael@0: // on the Nexus 4 the undefined result of inversesqrt(0) drops out an entire tile michael@0: // TODO: restrict this to Adreno-only michael@0: builder->fsCodeAppend("\tgrad_dot = max(grad_dot, 1.0e-4);\n"); michael@0: builder->fsCodeAppend("\tfloat invlen = inversesqrt(grad_dot);\n"); michael@0: if (kHairline == ellipseEffect.getMode()) { michael@0: // can probably do this with one step michael@0: builder->fsCodeAppend("\tfloat edgeAlpha = clamp(1.0-test*invlen, 0.0, 1.0);\n"); michael@0: builder->fsCodeAppend("\tedgeAlpha *= clamp(1.0+test*invlen, 0.0, 1.0);\n"); michael@0: } else { michael@0: builder->fsCodeAppend("\tfloat edgeAlpha = clamp(0.5-test*invlen, 0.0, 1.0);\n"); michael@0: } michael@0: michael@0: // for inner curve michael@0: if (kStroke == ellipseEffect.getMode()) { michael@0: builder->fsCodeAppendf("\tscaledOffset = %s.xy;\n", fsOffsetName1); michael@0: builder->fsCodeAppend("\ttest = dot(scaledOffset, scaledOffset) - 1.0;\n"); michael@0: builder->fsCodeAppendf("\tduvdx = dFdx(%s);\n", fsOffsetName1); michael@0: builder->fsCodeAppendf("\tduvdy = dFdy(%s);\n", fsOffsetName1); michael@0: builder->fsCodeAppendf("\tgrad = vec2(2.0*%s.x*duvdx.x + 2.0*%s.y*duvdx.y,\n" michael@0: "\t 2.0*%s.x*duvdy.x + 2.0*%s.y*duvdy.y);\n", michael@0: fsOffsetName1, fsOffsetName1, fsOffsetName1, fsOffsetName1); michael@0: builder->fsCodeAppend("\tinvlen = inversesqrt(dot(grad, grad));\n"); michael@0: builder->fsCodeAppend("\tedgeAlpha *= clamp(0.5+test*invlen, 0.0, 1.0);\n"); michael@0: } michael@0: michael@0: builder->fsCodeAppendf("\t%s = %s;\n", outputColor, michael@0: (GrGLSLExpr4(inputColor) * GrGLSLExpr1("edgeAlpha")).c_str()); michael@0: } michael@0: michael@0: static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) { michael@0: const DIEllipseEdgeEffect& ellipseEffect = drawEffect.castEffect(); michael@0: michael@0: return ellipseEffect.getMode(); michael@0: } michael@0: michael@0: virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE { michael@0: } michael@0: michael@0: private: michael@0: typedef GrGLVertexEffect INHERITED; michael@0: }; michael@0: michael@0: private: michael@0: DIEllipseEdgeEffect(Mode mode) : GrVertexEffect() { michael@0: this->addVertexAttrib(kVec2f_GrSLType); michael@0: this->addVertexAttrib(kVec2f_GrSLType); michael@0: fMode = mode; michael@0: } michael@0: michael@0: virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE { michael@0: const DIEllipseEdgeEffect& eee = CastEffect(other); michael@0: return eee.fMode == fMode; michael@0: } michael@0: michael@0: Mode fMode; michael@0: michael@0: GR_DECLARE_EFFECT_TEST; michael@0: michael@0: typedef GrVertexEffect INHERITED; michael@0: }; michael@0: michael@0: GR_DEFINE_EFFECT_TEST(DIEllipseEdgeEffect); michael@0: michael@0: GrEffectRef* DIEllipseEdgeEffect::TestCreate(SkRandom* random, michael@0: GrContext* context, michael@0: const GrDrawTargetCaps&, michael@0: GrTexture* textures[]) { michael@0: return DIEllipseEdgeEffect::Create((Mode)(random->nextRangeU(0,2))); michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: void GrOvalRenderer::reset() { michael@0: SkSafeSetNull(fRRectIndexBuffer); michael@0: } michael@0: michael@0: bool GrOvalRenderer::drawOval(GrDrawTarget* target, const GrContext* context, bool useAA, michael@0: const SkRect& oval, const SkStrokeRec& stroke) michael@0: { michael@0: bool useCoverageAA = useAA && michael@0: !target->getDrawState().getRenderTarget()->isMultisampled() && michael@0: !target->shouldDisableCoverageAAForBlend(); michael@0: michael@0: if (!useCoverageAA) { michael@0: return false; michael@0: } michael@0: michael@0: const SkMatrix& vm = context->getMatrix(); michael@0: michael@0: // we can draw circles michael@0: if (SkScalarNearlyEqual(oval.width(), oval.height()) michael@0: && circle_stays_circle(vm)) { michael@0: this->drawCircle(target, useCoverageAA, oval, stroke); michael@0: // if we have shader derivative support, render as device-independent michael@0: } else if (target->caps()->shaderDerivativeSupport()) { michael@0: return this->drawDIEllipse(target, useCoverageAA, oval, stroke); michael@0: // otherwise axis-aligned ellipses only michael@0: } else if (vm.rectStaysRect()) { michael@0: return this->drawEllipse(target, useCoverageAA, oval, stroke); michael@0: } else { michael@0: return false; michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: // position + edge michael@0: extern const GrVertexAttrib gCircleVertexAttribs[] = { michael@0: {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding}, michael@0: {kVec4f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding} michael@0: }; michael@0: michael@0: void GrOvalRenderer::drawCircle(GrDrawTarget* target, michael@0: bool useCoverageAA, michael@0: const SkRect& circle, michael@0: const SkStrokeRec& stroke) michael@0: { michael@0: GrDrawState* drawState = target->drawState(); michael@0: michael@0: const SkMatrix& vm = drawState->getViewMatrix(); michael@0: GrPoint center = GrPoint::Make(circle.centerX(), circle.centerY()); michael@0: vm.mapPoints(¢er, 1); michael@0: SkScalar radius = vm.mapRadius(SkScalarHalf(circle.width())); michael@0: SkScalar strokeWidth = vm.mapRadius(stroke.getWidth()); michael@0: michael@0: GrDrawState::AutoViewMatrixRestore avmr; michael@0: if (!avmr.setIdentity(drawState)) { michael@0: return; michael@0: } michael@0: michael@0: drawState->setVertexAttribs(SK_ARRAY_COUNT(gCircleVertexAttribs)); michael@0: SkASSERT(sizeof(CircleVertex) == drawState->getVertexSize()); michael@0: michael@0: GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0); michael@0: if (!geo.succeeded()) { michael@0: GrPrintf("Failed to get space for vertices!\n"); michael@0: return; michael@0: } michael@0: michael@0: CircleVertex* verts = reinterpret_cast(geo.vertices()); michael@0: michael@0: SkStrokeRec::Style style = stroke.getStyle(); michael@0: bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style); michael@0: michael@0: SkScalar innerRadius = 0.0f; michael@0: SkScalar outerRadius = radius; michael@0: SkScalar halfWidth = 0; michael@0: if (style != SkStrokeRec::kFill_Style) { michael@0: if (SkScalarNearlyZero(strokeWidth)) { michael@0: halfWidth = SK_ScalarHalf; michael@0: } else { michael@0: halfWidth = SkScalarHalf(strokeWidth); michael@0: } michael@0: michael@0: outerRadius += halfWidth; michael@0: if (isStroked) { michael@0: innerRadius = radius - halfWidth; michael@0: } michael@0: } michael@0: michael@0: GrEffectRef* effect = CircleEdgeEffect::Create(isStroked && innerRadius > 0); michael@0: static const int kCircleEdgeAttrIndex = 1; michael@0: drawState->addCoverageEffect(effect, kCircleEdgeAttrIndex)->unref(); michael@0: michael@0: // The radii are outset for two reasons. First, it allows the shader to simply perform michael@0: // clamp(distance-to-center - radius, 0, 1). Second, the outer radius is used to compute the michael@0: // verts of the bounding box that is rendered and the outset ensures the box will cover all michael@0: // pixels partially covered by the circle. michael@0: outerRadius += SK_ScalarHalf; michael@0: innerRadius -= SK_ScalarHalf; michael@0: michael@0: SkRect bounds = SkRect::MakeLTRB( michael@0: center.fX - outerRadius, michael@0: center.fY - outerRadius, michael@0: center.fX + outerRadius, michael@0: center.fY + outerRadius michael@0: ); michael@0: michael@0: verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop); michael@0: verts[0].fOffset = SkPoint::Make(-outerRadius, -outerRadius); michael@0: verts[0].fOuterRadius = outerRadius; michael@0: verts[0].fInnerRadius = innerRadius; michael@0: michael@0: verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop); michael@0: verts[1].fOffset = SkPoint::Make(outerRadius, -outerRadius); michael@0: verts[1].fOuterRadius = outerRadius; michael@0: verts[1].fInnerRadius = innerRadius; michael@0: michael@0: verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom); michael@0: verts[2].fOffset = SkPoint::Make(-outerRadius, outerRadius); michael@0: verts[2].fOuterRadius = outerRadius; michael@0: verts[2].fInnerRadius = innerRadius; michael@0: michael@0: verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom); michael@0: verts[3].fOffset = SkPoint::Make(outerRadius, outerRadius); michael@0: verts[3].fOuterRadius = outerRadius; michael@0: verts[3].fInnerRadius = innerRadius; michael@0: michael@0: target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds); michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: // position + offset + 1/radii michael@0: extern const GrVertexAttrib gEllipseVertexAttribs[] = { michael@0: {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding}, michael@0: {kVec2f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding}, michael@0: {kVec4f_GrVertexAttribType, 2*sizeof(GrPoint), kEffect_GrVertexAttribBinding} michael@0: }; michael@0: michael@0: // position + offsets michael@0: extern const GrVertexAttrib gDIEllipseVertexAttribs[] = { michael@0: {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding}, michael@0: {kVec2f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding}, michael@0: {kVec2f_GrVertexAttribType, 2*sizeof(GrPoint), kEffect_GrVertexAttribBinding}, michael@0: }; michael@0: michael@0: bool GrOvalRenderer::drawEllipse(GrDrawTarget* target, michael@0: bool useCoverageAA, michael@0: const SkRect& ellipse, michael@0: const SkStrokeRec& stroke) michael@0: { michael@0: GrDrawState* drawState = target->drawState(); michael@0: #ifdef SK_DEBUG michael@0: { michael@0: // we should have checked for this previously michael@0: bool isAxisAlignedEllipse = drawState->getViewMatrix().rectStaysRect(); michael@0: SkASSERT(useCoverageAA && isAxisAlignedEllipse); michael@0: } michael@0: #endif michael@0: michael@0: // do any matrix crunching before we reset the draw state for device coords michael@0: const SkMatrix& vm = drawState->getViewMatrix(); michael@0: GrPoint center = GrPoint::Make(ellipse.centerX(), ellipse.centerY()); michael@0: vm.mapPoints(¢er, 1); michael@0: SkScalar ellipseXRadius = SkScalarHalf(ellipse.width()); michael@0: SkScalar ellipseYRadius = SkScalarHalf(ellipse.height()); michael@0: SkScalar xRadius = SkScalarAbs(vm[SkMatrix::kMScaleX]*ellipseXRadius + michael@0: vm[SkMatrix::kMSkewY]*ellipseYRadius); michael@0: SkScalar yRadius = SkScalarAbs(vm[SkMatrix::kMSkewX]*ellipseXRadius + michael@0: vm[SkMatrix::kMScaleY]*ellipseYRadius); michael@0: michael@0: // do (potentially) anisotropic mapping of stroke michael@0: SkVector scaledStroke; michael@0: SkScalar strokeWidth = stroke.getWidth(); michael@0: scaledStroke.fX = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMScaleX] + vm[SkMatrix::kMSkewY])); michael@0: scaledStroke.fY = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMSkewX] + vm[SkMatrix::kMScaleY])); michael@0: michael@0: SkStrokeRec::Style style = stroke.getStyle(); michael@0: bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style); michael@0: michael@0: SkScalar innerXRadius = 0; michael@0: SkScalar innerYRadius = 0; michael@0: if (SkStrokeRec::kFill_Style != style) { michael@0: if (SkScalarNearlyZero(scaledStroke.length())) { michael@0: scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf); michael@0: } else { michael@0: scaledStroke.scale(SK_ScalarHalf); michael@0: } michael@0: michael@0: // we only handle thick strokes for near-circular ellipses michael@0: if (scaledStroke.length() > SK_ScalarHalf && michael@0: (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) { michael@0: return false; michael@0: } michael@0: michael@0: // we don't handle it if curvature of the stroke is less than curvature of the ellipse michael@0: if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius || michael@0: scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) { michael@0: return false; michael@0: } michael@0: michael@0: // this is legit only if scale & translation (which should be the case at the moment) michael@0: if (isStroked) { michael@0: innerXRadius = xRadius - scaledStroke.fX; michael@0: innerYRadius = yRadius - scaledStroke.fY; michael@0: } michael@0: michael@0: xRadius += scaledStroke.fX; michael@0: yRadius += scaledStroke.fY; michael@0: } michael@0: michael@0: GrDrawState::AutoViewMatrixRestore avmr; michael@0: if (!avmr.setIdentity(drawState)) { michael@0: return false; michael@0: } michael@0: michael@0: drawState->setVertexAttribs(SK_ARRAY_COUNT(gEllipseVertexAttribs)); michael@0: SkASSERT(sizeof(EllipseVertex) == drawState->getVertexSize()); michael@0: michael@0: GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0); michael@0: if (!geo.succeeded()) { michael@0: GrPrintf("Failed to get space for vertices!\n"); michael@0: return false; michael@0: } michael@0: michael@0: EllipseVertex* verts = reinterpret_cast(geo.vertices()); michael@0: michael@0: GrEffectRef* effect = EllipseEdgeEffect::Create(isStroked && michael@0: innerXRadius > 0 && innerYRadius > 0); michael@0: michael@0: static const int kEllipseCenterAttrIndex = 1; michael@0: static const int kEllipseEdgeAttrIndex = 2; michael@0: drawState->addCoverageEffect(effect, kEllipseCenterAttrIndex, kEllipseEdgeAttrIndex)->unref(); michael@0: michael@0: // Compute the reciprocals of the radii here to save time in the shader michael@0: SkScalar xRadRecip = SkScalarInvert(xRadius); michael@0: SkScalar yRadRecip = SkScalarInvert(yRadius); michael@0: SkScalar xInnerRadRecip = SkScalarInvert(innerXRadius); michael@0: SkScalar yInnerRadRecip = SkScalarInvert(innerYRadius); michael@0: michael@0: // We've extended the outer x radius out half a pixel to antialias. michael@0: // This will also expand the rect so all the pixels will be captured. michael@0: // TODO: Consider if we should use sqrt(2)/2 instead michael@0: xRadius += SK_ScalarHalf; michael@0: yRadius += SK_ScalarHalf; michael@0: michael@0: SkRect bounds = SkRect::MakeLTRB( michael@0: center.fX - xRadius, michael@0: center.fY - yRadius, michael@0: center.fX + xRadius, michael@0: center.fY + yRadius michael@0: ); michael@0: michael@0: verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop); michael@0: verts[0].fOffset = SkPoint::Make(-xRadius, -yRadius); michael@0: verts[0].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); michael@0: verts[0].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); michael@0: michael@0: verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop); michael@0: verts[1].fOffset = SkPoint::Make(xRadius, -yRadius); michael@0: verts[1].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); michael@0: verts[1].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); michael@0: michael@0: verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom); michael@0: verts[2].fOffset = SkPoint::Make(-xRadius, yRadius); michael@0: verts[2].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); michael@0: verts[2].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); michael@0: michael@0: verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom); michael@0: verts[3].fOffset = SkPoint::Make(xRadius, yRadius); michael@0: verts[3].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); michael@0: verts[3].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); michael@0: michael@0: target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: bool GrOvalRenderer::drawDIEllipse(GrDrawTarget* target, michael@0: bool useCoverageAA, michael@0: const SkRect& ellipse, michael@0: const SkStrokeRec& stroke) michael@0: { michael@0: GrDrawState* drawState = target->drawState(); michael@0: const SkMatrix& vm = drawState->getViewMatrix(); michael@0: michael@0: GrPoint center = GrPoint::Make(ellipse.centerX(), ellipse.centerY()); michael@0: SkScalar xRadius = SkScalarHalf(ellipse.width()); michael@0: SkScalar yRadius = SkScalarHalf(ellipse.height()); michael@0: michael@0: SkStrokeRec::Style style = stroke.getStyle(); michael@0: DIEllipseEdgeEffect::Mode mode = (SkStrokeRec::kStroke_Style == style) ? michael@0: DIEllipseEdgeEffect::kStroke : michael@0: (SkStrokeRec::kHairline_Style == style) ? michael@0: DIEllipseEdgeEffect::kHairline : DIEllipseEdgeEffect::kFill; michael@0: michael@0: SkScalar innerXRadius = 0; michael@0: SkScalar innerYRadius = 0; michael@0: if (SkStrokeRec::kFill_Style != style && SkStrokeRec::kHairline_Style != style) { michael@0: SkScalar strokeWidth = stroke.getWidth(); michael@0: michael@0: if (SkScalarNearlyZero(strokeWidth)) { michael@0: strokeWidth = SK_ScalarHalf; michael@0: } else { michael@0: strokeWidth *= SK_ScalarHalf; michael@0: } michael@0: michael@0: // we only handle thick strokes for near-circular ellipses michael@0: if (strokeWidth > SK_ScalarHalf && michael@0: (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) { michael@0: return false; michael@0: } michael@0: michael@0: // we don't handle it if curvature of the stroke is less than curvature of the ellipse michael@0: if (strokeWidth*(yRadius*yRadius) < (strokeWidth*strokeWidth)*xRadius || michael@0: strokeWidth*(xRadius*xRadius) < (strokeWidth*strokeWidth)*yRadius) { michael@0: return false; michael@0: } michael@0: michael@0: // set inner radius (if needed) michael@0: if (SkStrokeRec::kStroke_Style == style) { michael@0: innerXRadius = xRadius - strokeWidth; michael@0: innerYRadius = yRadius - strokeWidth; michael@0: } michael@0: michael@0: xRadius += strokeWidth; michael@0: yRadius += strokeWidth; michael@0: } michael@0: if (DIEllipseEdgeEffect::kStroke == mode) { michael@0: mode = (innerXRadius > 0 && innerYRadius > 0) ? DIEllipseEdgeEffect::kStroke : michael@0: DIEllipseEdgeEffect::kFill; michael@0: } michael@0: SkScalar innerRatioX = SkScalarDiv(xRadius, innerXRadius); michael@0: SkScalar innerRatioY = SkScalarDiv(yRadius, innerYRadius); michael@0: michael@0: drawState->setVertexAttribs(SK_ARRAY_COUNT(gDIEllipseVertexAttribs)); michael@0: SkASSERT(sizeof(DIEllipseVertex) == drawState->getVertexSize()); michael@0: michael@0: GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0); michael@0: if (!geo.succeeded()) { michael@0: GrPrintf("Failed to get space for vertices!\n"); michael@0: return false; michael@0: } michael@0: michael@0: DIEllipseVertex* verts = reinterpret_cast(geo.vertices()); michael@0: michael@0: GrEffectRef* effect = DIEllipseEdgeEffect::Create(mode); michael@0: michael@0: static const int kEllipseOuterOffsetAttrIndex = 1; michael@0: static const int kEllipseInnerOffsetAttrIndex = 2; michael@0: drawState->addCoverageEffect(effect, kEllipseOuterOffsetAttrIndex, michael@0: kEllipseInnerOffsetAttrIndex)->unref(); michael@0: michael@0: // This expands the outer rect so that after CTM we end up with a half-pixel border michael@0: SkScalar a = vm[SkMatrix::kMScaleX]; michael@0: SkScalar b = vm[SkMatrix::kMSkewX]; michael@0: SkScalar c = vm[SkMatrix::kMSkewY]; michael@0: SkScalar d = vm[SkMatrix::kMScaleY]; michael@0: SkScalar geoDx = SkScalarDiv(SK_ScalarHalf, SkScalarSqrt(a*a + c*c)); michael@0: SkScalar geoDy = SkScalarDiv(SK_ScalarHalf, SkScalarSqrt(b*b + d*d)); michael@0: // This adjusts the "radius" to include the half-pixel border michael@0: SkScalar offsetDx = SkScalarDiv(geoDx, xRadius); michael@0: SkScalar offsetDy = SkScalarDiv(geoDy, yRadius); michael@0: michael@0: SkRect bounds = SkRect::MakeLTRB( michael@0: center.fX - xRadius - geoDx, michael@0: center.fY - yRadius - geoDy, michael@0: center.fX + xRadius + geoDx, michael@0: center.fY + yRadius + geoDy michael@0: ); michael@0: michael@0: verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop); michael@0: verts[0].fOuterOffset = SkPoint::Make(-1.0f - offsetDx, -1.0f - offsetDy); michael@0: verts[0].fInnerOffset = SkPoint::Make(-innerRatioX - offsetDx, -innerRatioY - offsetDy); michael@0: michael@0: verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop); michael@0: verts[1].fOuterOffset = SkPoint::Make(1.0f + offsetDx, -1.0f - offsetDy); michael@0: verts[1].fInnerOffset = SkPoint::Make(innerRatioX + offsetDx, -innerRatioY - offsetDy); michael@0: michael@0: verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom); michael@0: verts[2].fOuterOffset = SkPoint::Make(-1.0f - offsetDx, 1.0f + offsetDy); michael@0: verts[2].fInnerOffset = SkPoint::Make(-innerRatioX - offsetDx, innerRatioY + offsetDy); michael@0: michael@0: verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom); michael@0: verts[3].fOuterOffset = SkPoint::Make(1.0f + offsetDx, 1.0f + offsetDy); michael@0: verts[3].fInnerOffset = SkPoint::Make(innerRatioX + offsetDx, innerRatioY + offsetDy); michael@0: michael@0: target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: static const uint16_t gRRectIndices[] = { michael@0: // corners michael@0: 0, 1, 5, 0, 5, 4, michael@0: 2, 3, 7, 2, 7, 6, michael@0: 8, 9, 13, 8, 13, 12, michael@0: 10, 11, 15, 10, 15, 14, michael@0: michael@0: // edges michael@0: 1, 2, 6, 1, 6, 5, michael@0: 4, 5, 9, 4, 9, 8, michael@0: 6, 7, 11, 6, 11, 10, michael@0: 9, 10, 14, 9, 14, 13, michael@0: michael@0: // center michael@0: // we place this at the end so that we can ignore these indices when rendering stroke-only michael@0: 5, 6, 10, 5, 10, 9 michael@0: }; michael@0: michael@0: michael@0: GrIndexBuffer* GrOvalRenderer::rRectIndexBuffer(GrGpu* gpu) { michael@0: if (NULL == fRRectIndexBuffer) { michael@0: fRRectIndexBuffer = michael@0: gpu->createIndexBuffer(sizeof(gRRectIndices), false); michael@0: if (NULL != fRRectIndexBuffer) { michael@0: #ifdef SK_DEBUG michael@0: bool updated = michael@0: #endif michael@0: fRRectIndexBuffer->updateData(gRRectIndices, michael@0: sizeof(gRRectIndices)); michael@0: GR_DEBUGASSERT(updated); michael@0: } michael@0: } michael@0: return fRRectIndexBuffer; michael@0: } michael@0: michael@0: bool GrOvalRenderer::drawSimpleRRect(GrDrawTarget* target, GrContext* context, bool useAA, michael@0: const SkRRect& rrect, const SkStrokeRec& stroke) michael@0: { michael@0: bool useCoverageAA = useAA && michael@0: !target->getDrawState().getRenderTarget()->isMultisampled() && michael@0: !target->shouldDisableCoverageAAForBlend(); michael@0: michael@0: // only anti-aliased rrects for now michael@0: if (!useCoverageAA) { michael@0: return false; michael@0: } michael@0: michael@0: const SkMatrix& vm = context->getMatrix(); michael@0: #ifdef SK_DEBUG michael@0: { michael@0: // we should have checked for this previously michael@0: SkASSERT(useCoverageAA && vm.rectStaysRect() && rrect.isSimple()); michael@0: } michael@0: #endif michael@0: michael@0: // do any matrix crunching before we reset the draw state for device coords michael@0: const SkRect& rrectBounds = rrect.getBounds(); michael@0: SkRect bounds; michael@0: vm.mapRect(&bounds, rrectBounds); michael@0: michael@0: SkVector radii = rrect.getSimpleRadii(); michael@0: SkScalar xRadius = SkScalarAbs(vm[SkMatrix::kMScaleX]*radii.fX + michael@0: vm[SkMatrix::kMSkewY]*radii.fY); michael@0: SkScalar yRadius = SkScalarAbs(vm[SkMatrix::kMSkewX]*radii.fX + michael@0: vm[SkMatrix::kMScaleY]*radii.fY); michael@0: michael@0: // if hairline stroke is greater than radius, we don't handle that right now michael@0: SkStrokeRec::Style style = stroke.getStyle(); michael@0: if (SkStrokeRec::kHairline_Style == style && michael@0: (SK_ScalarHalf > xRadius || SK_ScalarHalf > yRadius)) { michael@0: return false; michael@0: } michael@0: michael@0: // do (potentially) anisotropic mapping of stroke michael@0: SkVector scaledStroke; michael@0: SkScalar strokeWidth = stroke.getWidth(); michael@0: scaledStroke.fX = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMScaleX] + vm[SkMatrix::kMSkewY])); michael@0: scaledStroke.fY = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMSkewX] + vm[SkMatrix::kMScaleY])); michael@0: michael@0: // if half of strokewidth is greater than radius, we don't handle that right now michael@0: if (SK_ScalarHalf*scaledStroke.fX > xRadius || SK_ScalarHalf*scaledStroke.fY > yRadius) { michael@0: return false; michael@0: } michael@0: michael@0: // reset to device coordinates michael@0: GrDrawState* drawState = target->drawState(); michael@0: GrDrawState::AutoViewMatrixRestore avmr; michael@0: if (!avmr.setIdentity(drawState)) { michael@0: return false; michael@0: } michael@0: michael@0: bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style); michael@0: michael@0: GrIndexBuffer* indexBuffer = this->rRectIndexBuffer(context->getGpu()); michael@0: if (NULL == indexBuffer) { michael@0: GrPrintf("Failed to create index buffer!\n"); michael@0: return false; michael@0: } michael@0: michael@0: // if the corners are circles, use the circle renderer michael@0: if ((!isStroked || scaledStroke.fX == scaledStroke.fY) && xRadius == yRadius) { michael@0: drawState->setVertexAttribs(SK_ARRAY_COUNT(gCircleVertexAttribs)); michael@0: SkASSERT(sizeof(CircleVertex) == drawState->getVertexSize()); michael@0: michael@0: GrDrawTarget::AutoReleaseGeometry geo(target, 16, 0); michael@0: if (!geo.succeeded()) { michael@0: GrPrintf("Failed to get space for vertices!\n"); michael@0: return false; michael@0: } michael@0: CircleVertex* verts = reinterpret_cast(geo.vertices()); michael@0: michael@0: SkScalar innerRadius = 0.0f; michael@0: SkScalar outerRadius = xRadius; michael@0: SkScalar halfWidth = 0; michael@0: if (style != SkStrokeRec::kFill_Style) { michael@0: if (SkScalarNearlyZero(scaledStroke.fX)) { michael@0: halfWidth = SK_ScalarHalf; michael@0: } else { michael@0: halfWidth = SkScalarHalf(scaledStroke.fX); michael@0: } michael@0: michael@0: if (isStroked) { michael@0: innerRadius = xRadius - halfWidth; michael@0: } michael@0: outerRadius += halfWidth; michael@0: bounds.outset(halfWidth, halfWidth); michael@0: } michael@0: michael@0: isStroked = (isStroked && innerRadius >= 0); michael@0: michael@0: GrEffectRef* effect = CircleEdgeEffect::Create(isStroked); michael@0: static const int kCircleEdgeAttrIndex = 1; michael@0: drawState->addCoverageEffect(effect, kCircleEdgeAttrIndex)->unref(); michael@0: michael@0: // The radii are outset for two reasons. First, it allows the shader to simply perform michael@0: // clamp(distance-to-center - radius, 0, 1). Second, the outer radius is used to compute the michael@0: // verts of the bounding box that is rendered and the outset ensures the box will cover all michael@0: // pixels partially covered by the circle. michael@0: outerRadius += SK_ScalarHalf; michael@0: innerRadius -= SK_ScalarHalf; michael@0: michael@0: // Expand the rect so all the pixels will be captured. michael@0: bounds.outset(SK_ScalarHalf, SK_ScalarHalf); michael@0: michael@0: SkScalar yCoords[4] = { michael@0: bounds.fTop, michael@0: bounds.fTop + outerRadius, michael@0: bounds.fBottom - outerRadius, michael@0: bounds.fBottom michael@0: }; michael@0: SkScalar yOuterRadii[4] = { michael@0: -outerRadius, michael@0: 0, michael@0: 0, michael@0: outerRadius michael@0: }; michael@0: for (int i = 0; i < 4; ++i) { michael@0: verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]); michael@0: verts->fOffset = SkPoint::Make(-outerRadius, yOuterRadii[i]); michael@0: verts->fOuterRadius = outerRadius; michael@0: verts->fInnerRadius = innerRadius; michael@0: verts++; michael@0: michael@0: verts->fPos = SkPoint::Make(bounds.fLeft + outerRadius, yCoords[i]); michael@0: verts->fOffset = SkPoint::Make(0, yOuterRadii[i]); michael@0: verts->fOuterRadius = outerRadius; michael@0: verts->fInnerRadius = innerRadius; michael@0: verts++; michael@0: michael@0: verts->fPos = SkPoint::Make(bounds.fRight - outerRadius, yCoords[i]); michael@0: verts->fOffset = SkPoint::Make(0, yOuterRadii[i]); michael@0: verts->fOuterRadius = outerRadius; michael@0: verts->fInnerRadius = innerRadius; michael@0: verts++; michael@0: michael@0: verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]); michael@0: verts->fOffset = SkPoint::Make(outerRadius, yOuterRadii[i]); michael@0: verts->fOuterRadius = outerRadius; michael@0: verts->fInnerRadius = innerRadius; michael@0: verts++; michael@0: } michael@0: michael@0: // drop out the middle quad if we're stroked michael@0: int indexCnt = isStroked ? GR_ARRAY_COUNT(gRRectIndices)-6 : GR_ARRAY_COUNT(gRRectIndices); michael@0: target->setIndexSourceToBuffer(indexBuffer); michael@0: target->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, 16, indexCnt, &bounds); michael@0: michael@0: // otherwise we use the ellipse renderer michael@0: } else { michael@0: drawState->setVertexAttribs(SK_ARRAY_COUNT(gEllipseVertexAttribs)); michael@0: SkASSERT(sizeof(EllipseVertex) == drawState->getVertexSize()); michael@0: michael@0: SkScalar innerXRadius = 0.0f; michael@0: SkScalar innerYRadius = 0.0f; michael@0: if (SkStrokeRec::kFill_Style != style) { michael@0: if (SkScalarNearlyZero(scaledStroke.length())) { michael@0: scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf); michael@0: } else { michael@0: scaledStroke.scale(SK_ScalarHalf); michael@0: } michael@0: michael@0: // we only handle thick strokes for near-circular ellipses michael@0: if (scaledStroke.length() > SK_ScalarHalf && michael@0: (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) { michael@0: return false; michael@0: } michael@0: michael@0: // we don't handle it if curvature of the stroke is less than curvature of the ellipse michael@0: if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius || michael@0: scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) { michael@0: return false; michael@0: } michael@0: michael@0: // this is legit only if scale & translation (which should be the case at the moment) michael@0: if (isStroked) { michael@0: innerXRadius = xRadius - scaledStroke.fX; michael@0: innerYRadius = yRadius - scaledStroke.fY; michael@0: } michael@0: michael@0: xRadius += scaledStroke.fX; michael@0: yRadius += scaledStroke.fY; michael@0: bounds.outset(scaledStroke.fX, scaledStroke.fY); michael@0: } michael@0: michael@0: isStroked = (isStroked && innerXRadius >= 0 && innerYRadius >= 0); michael@0: michael@0: GrDrawTarget::AutoReleaseGeometry geo(target, 16, 0); michael@0: if (!geo.succeeded()) { michael@0: GrPrintf("Failed to get space for vertices!\n"); michael@0: return false; michael@0: } michael@0: EllipseVertex* verts = reinterpret_cast(geo.vertices()); michael@0: michael@0: GrEffectRef* effect = EllipseEdgeEffect::Create(isStroked); michael@0: static const int kEllipseOffsetAttrIndex = 1; michael@0: static const int kEllipseRadiiAttrIndex = 2; michael@0: drawState->addCoverageEffect(effect, michael@0: kEllipseOffsetAttrIndex, kEllipseRadiiAttrIndex)->unref(); michael@0: michael@0: // Compute the reciprocals of the radii here to save time in the shader michael@0: SkScalar xRadRecip = SkScalarInvert(xRadius); michael@0: SkScalar yRadRecip = SkScalarInvert(yRadius); michael@0: SkScalar xInnerRadRecip = SkScalarInvert(innerXRadius); michael@0: SkScalar yInnerRadRecip = SkScalarInvert(innerYRadius); michael@0: michael@0: // Extend the radii out half a pixel to antialias. michael@0: SkScalar xOuterRadius = xRadius + SK_ScalarHalf; michael@0: SkScalar yOuterRadius = yRadius + SK_ScalarHalf; michael@0: michael@0: // Expand the rect so all the pixels will be captured. michael@0: bounds.outset(SK_ScalarHalf, SK_ScalarHalf); michael@0: michael@0: SkScalar yCoords[4] = { michael@0: bounds.fTop, michael@0: bounds.fTop + yOuterRadius, michael@0: bounds.fBottom - yOuterRadius, michael@0: bounds.fBottom michael@0: }; michael@0: SkScalar yOuterOffsets[4] = { michael@0: yOuterRadius, michael@0: SK_ScalarNearlyZero, // we're using inversesqrt() in the shader, so can't be exactly 0 michael@0: SK_ScalarNearlyZero, michael@0: yOuterRadius michael@0: }; michael@0: michael@0: for (int i = 0; i < 4; ++i) { michael@0: verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]); michael@0: verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]); michael@0: verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); michael@0: verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); michael@0: verts++; michael@0: michael@0: verts->fPos = SkPoint::Make(bounds.fLeft + xOuterRadius, yCoords[i]); michael@0: verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]); michael@0: verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); michael@0: verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); michael@0: verts++; michael@0: michael@0: verts->fPos = SkPoint::Make(bounds.fRight - xOuterRadius, yCoords[i]); michael@0: verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]); michael@0: verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); michael@0: verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); michael@0: verts++; michael@0: michael@0: verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]); michael@0: verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]); michael@0: verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); michael@0: verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); michael@0: verts++; michael@0: } michael@0: michael@0: // drop out the middle quad if we're stroked michael@0: int indexCnt = isStroked ? GR_ARRAY_COUNT(gRRectIndices)-6 : GR_ARRAY_COUNT(gRRectIndices); michael@0: target->setIndexSourceToBuffer(indexBuffer); michael@0: target->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, 16, indexCnt, &bounds); michael@0: } michael@0: michael@0: return true; michael@0: }