1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/src/gpu/GrOvalRenderer.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,1147 @@ 1.4 +/* 1.5 + * Copyright 2013 Google Inc. 1.6 + * 1.7 + * Use of this source code is governed by a BSD-style license that can be 1.8 + * found in the LICENSE file. 1.9 + */ 1.10 + 1.11 +#include "GrOvalRenderer.h" 1.12 + 1.13 +#include "GrEffect.h" 1.14 +#include "gl/GrGLEffect.h" 1.15 +#include "gl/GrGLSL.h" 1.16 +#include "gl/GrGLVertexEffect.h" 1.17 +#include "GrTBackendEffectFactory.h" 1.18 + 1.19 +#include "GrDrawState.h" 1.20 +#include "GrDrawTarget.h" 1.21 +#include "GrGpu.h" 1.22 + 1.23 +#include "SkRRect.h" 1.24 +#include "SkStrokeRec.h" 1.25 + 1.26 +#include "effects/GrVertexEffect.h" 1.27 + 1.28 +namespace { 1.29 + 1.30 +struct CircleVertex { 1.31 + GrPoint fPos; 1.32 + GrPoint fOffset; 1.33 + SkScalar fOuterRadius; 1.34 + SkScalar fInnerRadius; 1.35 +}; 1.36 + 1.37 +struct EllipseVertex { 1.38 + GrPoint fPos; 1.39 + GrPoint fOffset; 1.40 + GrPoint fOuterRadii; 1.41 + GrPoint fInnerRadii; 1.42 +}; 1.43 + 1.44 +struct DIEllipseVertex { 1.45 + GrPoint fPos; 1.46 + GrPoint fOuterOffset; 1.47 + GrPoint fInnerOffset; 1.48 +}; 1.49 + 1.50 +inline bool circle_stays_circle(const SkMatrix& m) { 1.51 + return m.isSimilarity(); 1.52 +} 1.53 + 1.54 +} 1.55 + 1.56 +/////////////////////////////////////////////////////////////////////////////// 1.57 + 1.58 +/** 1.59 + * The output of this effect is a modulation of the input color and coverage for a circle, 1.60 + * specified as offset_x, offset_y (both from center point), outer radius and inner radius. 1.61 + */ 1.62 + 1.63 +class CircleEdgeEffect : public GrVertexEffect { 1.64 +public: 1.65 + static GrEffectRef* Create(bool stroke) { 1.66 + GR_CREATE_STATIC_EFFECT(gCircleStrokeEdge, CircleEdgeEffect, (true)); 1.67 + GR_CREATE_STATIC_EFFECT(gCircleFillEdge, CircleEdgeEffect, (false)); 1.68 + 1.69 + if (stroke) { 1.70 + gCircleStrokeEdge->ref(); 1.71 + return gCircleStrokeEdge; 1.72 + } else { 1.73 + gCircleFillEdge->ref(); 1.74 + return gCircleFillEdge; 1.75 + } 1.76 + } 1.77 + 1.78 + virtual void getConstantColorComponents(GrColor* color, 1.79 + uint32_t* validFlags) const SK_OVERRIDE { 1.80 + *validFlags = 0; 1.81 + } 1.82 + 1.83 + virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { 1.84 + return GrTBackendEffectFactory<CircleEdgeEffect>::getInstance(); 1.85 + } 1.86 + 1.87 + virtual ~CircleEdgeEffect() {} 1.88 + 1.89 + static const char* Name() { return "CircleEdge"; } 1.90 + 1.91 + inline bool isStroked() const { return fStroke; } 1.92 + 1.93 + class GLEffect : public GrGLVertexEffect { 1.94 + public: 1.95 + GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&) 1.96 + : INHERITED (factory) {} 1.97 + 1.98 + virtual void emitCode(GrGLFullShaderBuilder* builder, 1.99 + const GrDrawEffect& drawEffect, 1.100 + EffectKey key, 1.101 + const char* outputColor, 1.102 + const char* inputColor, 1.103 + const TransformedCoordsArray&, 1.104 + const TextureSamplerArray& samplers) SK_OVERRIDE { 1.105 + const CircleEdgeEffect& circleEffect = drawEffect.castEffect<CircleEdgeEffect>(); 1.106 + const char *vsName, *fsName; 1.107 + builder->addVarying(kVec4f_GrSLType, "CircleEdge", &vsName, &fsName); 1.108 + 1.109 + const SkString* attrName = 1.110 + builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]); 1.111 + builder->vsCodeAppendf("\t%s = %s;\n", vsName, attrName->c_str()); 1.112 + 1.113 + builder->fsCodeAppendf("\tfloat d = length(%s.xy);\n", fsName); 1.114 + builder->fsCodeAppendf("\tfloat edgeAlpha = clamp(%s.z - d, 0.0, 1.0);\n", fsName); 1.115 + if (circleEffect.isStroked()) { 1.116 + builder->fsCodeAppendf("\tfloat innerAlpha = clamp(d - %s.w, 0.0, 1.0);\n", fsName); 1.117 + builder->fsCodeAppend("\tedgeAlpha *= innerAlpha;\n"); 1.118 + } 1.119 + 1.120 + builder->fsCodeAppendf("\t%s = %s;\n", outputColor, 1.121 + (GrGLSLExpr4(inputColor) * GrGLSLExpr1("edgeAlpha")).c_str()); 1.122 + } 1.123 + 1.124 + static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) { 1.125 + const CircleEdgeEffect& circleEffect = drawEffect.castEffect<CircleEdgeEffect>(); 1.126 + 1.127 + return circleEffect.isStroked() ? 0x1 : 0x0; 1.128 + } 1.129 + 1.130 + virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {} 1.131 + 1.132 + private: 1.133 + typedef GrGLVertexEffect INHERITED; 1.134 + }; 1.135 + 1.136 + 1.137 +private: 1.138 + CircleEdgeEffect(bool stroke) : GrVertexEffect() { 1.139 + this->addVertexAttrib(kVec4f_GrSLType); 1.140 + fStroke = stroke; 1.141 + } 1.142 + 1.143 + virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE { 1.144 + const CircleEdgeEffect& cee = CastEffect<CircleEdgeEffect>(other); 1.145 + return cee.fStroke == fStroke; 1.146 + } 1.147 + 1.148 + bool fStroke; 1.149 + 1.150 + GR_DECLARE_EFFECT_TEST; 1.151 + 1.152 + typedef GrVertexEffect INHERITED; 1.153 +}; 1.154 + 1.155 +GR_DEFINE_EFFECT_TEST(CircleEdgeEffect); 1.156 + 1.157 +GrEffectRef* CircleEdgeEffect::TestCreate(SkRandom* random, 1.158 + GrContext* context, 1.159 + const GrDrawTargetCaps&, 1.160 + GrTexture* textures[]) { 1.161 + return CircleEdgeEffect::Create(random->nextBool()); 1.162 +} 1.163 + 1.164 +/////////////////////////////////////////////////////////////////////////////// 1.165 + 1.166 +/** 1.167 + * The output of this effect is a modulation of the input color and coverage for an axis-aligned 1.168 + * ellipse, specified as a 2D offset from center, and the reciprocals of the outer and inner radii, 1.169 + * in both x and y directions. 1.170 + * 1.171 + * We are using an implicit function of x^2/a^2 + y^2/b^2 - 1 = 0. 1.172 + */ 1.173 + 1.174 +class EllipseEdgeEffect : public GrVertexEffect { 1.175 +public: 1.176 + static GrEffectRef* Create(bool stroke) { 1.177 + GR_CREATE_STATIC_EFFECT(gEllipseStrokeEdge, EllipseEdgeEffect, (true)); 1.178 + GR_CREATE_STATIC_EFFECT(gEllipseFillEdge, EllipseEdgeEffect, (false)); 1.179 + 1.180 + if (stroke) { 1.181 + gEllipseStrokeEdge->ref(); 1.182 + return gEllipseStrokeEdge; 1.183 + } else { 1.184 + gEllipseFillEdge->ref(); 1.185 + return gEllipseFillEdge; 1.186 + } 1.187 + } 1.188 + 1.189 + virtual void getConstantColorComponents(GrColor* color, 1.190 + uint32_t* validFlags) const SK_OVERRIDE { 1.191 + *validFlags = 0; 1.192 + } 1.193 + 1.194 + virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { 1.195 + return GrTBackendEffectFactory<EllipseEdgeEffect>::getInstance(); 1.196 + } 1.197 + 1.198 + virtual ~EllipseEdgeEffect() {} 1.199 + 1.200 + static const char* Name() { return "EllipseEdge"; } 1.201 + 1.202 + inline bool isStroked() const { return fStroke; } 1.203 + 1.204 + class GLEffect : public GrGLVertexEffect { 1.205 + public: 1.206 + GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&) 1.207 + : INHERITED (factory) {} 1.208 + 1.209 + virtual void emitCode(GrGLFullShaderBuilder* builder, 1.210 + const GrDrawEffect& drawEffect, 1.211 + EffectKey key, 1.212 + const char* outputColor, 1.213 + const char* inputColor, 1.214 + const TransformedCoordsArray&, 1.215 + const TextureSamplerArray& samplers) SK_OVERRIDE { 1.216 + const EllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<EllipseEdgeEffect>(); 1.217 + 1.218 + const char *vsOffsetName, *fsOffsetName; 1.219 + const char *vsRadiiName, *fsRadiiName; 1.220 + 1.221 + builder->addVarying(kVec2f_GrSLType, "EllipseOffsets", &vsOffsetName, &fsOffsetName); 1.222 + const SkString* attr0Name = 1.223 + builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]); 1.224 + builder->vsCodeAppendf("\t%s = %s;\n", vsOffsetName, attr0Name->c_str()); 1.225 + 1.226 + builder->addVarying(kVec4f_GrSLType, "EllipseRadii", &vsRadiiName, &fsRadiiName); 1.227 + const SkString* attr1Name = 1.228 + builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[1]); 1.229 + builder->vsCodeAppendf("\t%s = %s;\n", vsRadiiName, attr1Name->c_str()); 1.230 + 1.231 + // for outer curve 1.232 + builder->fsCodeAppendf("\tvec2 scaledOffset = %s*%s.xy;\n", fsOffsetName, fsRadiiName); 1.233 + builder->fsCodeAppend("\tfloat test = dot(scaledOffset, scaledOffset) - 1.0;\n"); 1.234 + builder->fsCodeAppendf("\tvec2 grad = 2.0*scaledOffset*%s.xy;\n", fsRadiiName); 1.235 + builder->fsCodeAppend("\tfloat grad_dot = dot(grad, grad);\n"); 1.236 + // we need to clamp the length^2 of the gradiant vector to a non-zero value, because 1.237 + // on the Nexus 4 the undefined result of inversesqrt(0) drops out an entire tile 1.238 + // TODO: restrict this to Adreno-only 1.239 + builder->fsCodeAppend("\tgrad_dot = max(grad_dot, 1.0e-4);\n"); 1.240 + builder->fsCodeAppend("\tfloat invlen = inversesqrt(grad_dot);\n"); 1.241 + builder->fsCodeAppend("\tfloat edgeAlpha = clamp(0.5-test*invlen, 0.0, 1.0);\n"); 1.242 + 1.243 + // for inner curve 1.244 + if (ellipseEffect.isStroked()) { 1.245 + builder->fsCodeAppendf("\tscaledOffset = %s*%s.zw;\n", fsOffsetName, fsRadiiName); 1.246 + builder->fsCodeAppend("\ttest = dot(scaledOffset, scaledOffset) - 1.0;\n"); 1.247 + builder->fsCodeAppendf("\tgrad = 2.0*scaledOffset*%s.zw;\n", fsRadiiName); 1.248 + builder->fsCodeAppend("\tinvlen = inversesqrt(dot(grad, grad));\n"); 1.249 + builder->fsCodeAppend("\tedgeAlpha *= clamp(0.5+test*invlen, 0.0, 1.0);\n"); 1.250 + } 1.251 + 1.252 + builder->fsCodeAppendf("\t%s = %s;\n", outputColor, 1.253 + (GrGLSLExpr4(inputColor) * GrGLSLExpr1("edgeAlpha")).c_str()); 1.254 + } 1.255 + 1.256 + static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) { 1.257 + const EllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<EllipseEdgeEffect>(); 1.258 + 1.259 + return ellipseEffect.isStroked() ? 0x1 : 0x0; 1.260 + } 1.261 + 1.262 + virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE { 1.263 + } 1.264 + 1.265 + private: 1.266 + typedef GrGLVertexEffect INHERITED; 1.267 + }; 1.268 + 1.269 +private: 1.270 + EllipseEdgeEffect(bool stroke) : GrVertexEffect() { 1.271 + this->addVertexAttrib(kVec2f_GrSLType); 1.272 + this->addVertexAttrib(kVec4f_GrSLType); 1.273 + fStroke = stroke; 1.274 + } 1.275 + 1.276 + virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE { 1.277 + const EllipseEdgeEffect& eee = CastEffect<EllipseEdgeEffect>(other); 1.278 + return eee.fStroke == fStroke; 1.279 + } 1.280 + 1.281 + bool fStroke; 1.282 + 1.283 + GR_DECLARE_EFFECT_TEST; 1.284 + 1.285 + typedef GrVertexEffect INHERITED; 1.286 +}; 1.287 + 1.288 +GR_DEFINE_EFFECT_TEST(EllipseEdgeEffect); 1.289 + 1.290 +GrEffectRef* EllipseEdgeEffect::TestCreate(SkRandom* random, 1.291 + GrContext* context, 1.292 + const GrDrawTargetCaps&, 1.293 + GrTexture* textures[]) { 1.294 + return EllipseEdgeEffect::Create(random->nextBool()); 1.295 +} 1.296 + 1.297 +/////////////////////////////////////////////////////////////////////////////// 1.298 + 1.299 +/** 1.300 + * The output of this effect is a modulation of the input color and coverage for an ellipse, 1.301 + * specified as a 2D offset from center for both the outer and inner paths (if stroked). The 1.302 + * implict equation used is for a unit circle (x^2 + y^2 - 1 = 0) and the edge corrected by 1.303 + * using differentials. 1.304 + * 1.305 + * The result is device-independent and can be used with any affine matrix. 1.306 + */ 1.307 + 1.308 +class DIEllipseEdgeEffect : public GrVertexEffect { 1.309 +public: 1.310 + enum Mode { kStroke = 0, kHairline, kFill }; 1.311 + 1.312 + static GrEffectRef* Create(Mode mode) { 1.313 + GR_CREATE_STATIC_EFFECT(gEllipseStrokeEdge, DIEllipseEdgeEffect, (kStroke)); 1.314 + GR_CREATE_STATIC_EFFECT(gEllipseHairlineEdge, DIEllipseEdgeEffect, (kHairline)); 1.315 + GR_CREATE_STATIC_EFFECT(gEllipseFillEdge, DIEllipseEdgeEffect, (kFill)); 1.316 + 1.317 + if (kStroke == mode) { 1.318 + gEllipseStrokeEdge->ref(); 1.319 + return gEllipseStrokeEdge; 1.320 + } else if (kHairline == mode) { 1.321 + gEllipseHairlineEdge->ref(); 1.322 + return gEllipseHairlineEdge; 1.323 + } else { 1.324 + gEllipseFillEdge->ref(); 1.325 + return gEllipseFillEdge; 1.326 + } 1.327 + } 1.328 + 1.329 + virtual void getConstantColorComponents(GrColor* color, 1.330 + uint32_t* validFlags) const SK_OVERRIDE { 1.331 + *validFlags = 0; 1.332 + } 1.333 + 1.334 + virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { 1.335 + return GrTBackendEffectFactory<DIEllipseEdgeEffect>::getInstance(); 1.336 + } 1.337 + 1.338 + virtual ~DIEllipseEdgeEffect() {} 1.339 + 1.340 + static const char* Name() { return "DIEllipseEdge"; } 1.341 + 1.342 + inline Mode getMode() const { return fMode; } 1.343 + 1.344 + class GLEffect : public GrGLVertexEffect { 1.345 + public: 1.346 + GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&) 1.347 + : INHERITED (factory) {} 1.348 + 1.349 + virtual void emitCode(GrGLFullShaderBuilder* builder, 1.350 + const GrDrawEffect& drawEffect, 1.351 + EffectKey key, 1.352 + const char* outputColor, 1.353 + const char* inputColor, 1.354 + const TransformedCoordsArray&, 1.355 + const TextureSamplerArray& samplers) SK_OVERRIDE { 1.356 + const DIEllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<DIEllipseEdgeEffect>(); 1.357 + 1.358 + SkAssertResult(builder->enableFeature( 1.359 + GrGLShaderBuilder::kStandardDerivatives_GLSLFeature)); 1.360 + 1.361 + const char *vsOffsetName0, *fsOffsetName0; 1.362 + builder->addVarying(kVec2f_GrSLType, "EllipseOffsets0", 1.363 + &vsOffsetName0, &fsOffsetName0); 1.364 + const SkString* attr0Name = 1.365 + builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]); 1.366 + builder->vsCodeAppendf("\t%s = %s;\n", vsOffsetName0, attr0Name->c_str()); 1.367 + const char *vsOffsetName1, *fsOffsetName1; 1.368 + builder->addVarying(kVec2f_GrSLType, "EllipseOffsets1", 1.369 + &vsOffsetName1, &fsOffsetName1); 1.370 + const SkString* attr1Name = 1.371 + builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[1]); 1.372 + builder->vsCodeAppendf("\t%s = %s;\n", vsOffsetName1, attr1Name->c_str()); 1.373 + 1.374 + // for outer curve 1.375 + builder->fsCodeAppendf("\tvec2 scaledOffset = %s.xy;\n", fsOffsetName0); 1.376 + builder->fsCodeAppend("\tfloat test = dot(scaledOffset, scaledOffset) - 1.0;\n"); 1.377 + builder->fsCodeAppendf("\tvec2 duvdx = dFdx(%s);\n", fsOffsetName0); 1.378 + builder->fsCodeAppendf("\tvec2 duvdy = dFdy(%s);\n", fsOffsetName0); 1.379 + builder->fsCodeAppendf("\tvec2 grad = vec2(2.0*%s.x*duvdx.x + 2.0*%s.y*duvdx.y,\n" 1.380 + "\t 2.0*%s.x*duvdy.x + 2.0*%s.y*duvdy.y);\n", 1.381 + fsOffsetName0, fsOffsetName0, fsOffsetName0, fsOffsetName0); 1.382 + 1.383 + builder->fsCodeAppend("\tfloat grad_dot = dot(grad, grad);\n"); 1.384 + // we need to clamp the length^2 of the gradiant vector to a non-zero value, because 1.385 + // on the Nexus 4 the undefined result of inversesqrt(0) drops out an entire tile 1.386 + // TODO: restrict this to Adreno-only 1.387 + builder->fsCodeAppend("\tgrad_dot = max(grad_dot, 1.0e-4);\n"); 1.388 + builder->fsCodeAppend("\tfloat invlen = inversesqrt(grad_dot);\n"); 1.389 + if (kHairline == ellipseEffect.getMode()) { 1.390 + // can probably do this with one step 1.391 + builder->fsCodeAppend("\tfloat edgeAlpha = clamp(1.0-test*invlen, 0.0, 1.0);\n"); 1.392 + builder->fsCodeAppend("\tedgeAlpha *= clamp(1.0+test*invlen, 0.0, 1.0);\n"); 1.393 + } else { 1.394 + builder->fsCodeAppend("\tfloat edgeAlpha = clamp(0.5-test*invlen, 0.0, 1.0);\n"); 1.395 + } 1.396 + 1.397 + // for inner curve 1.398 + if (kStroke == ellipseEffect.getMode()) { 1.399 + builder->fsCodeAppendf("\tscaledOffset = %s.xy;\n", fsOffsetName1); 1.400 + builder->fsCodeAppend("\ttest = dot(scaledOffset, scaledOffset) - 1.0;\n"); 1.401 + builder->fsCodeAppendf("\tduvdx = dFdx(%s);\n", fsOffsetName1); 1.402 + builder->fsCodeAppendf("\tduvdy = dFdy(%s);\n", fsOffsetName1); 1.403 + builder->fsCodeAppendf("\tgrad = vec2(2.0*%s.x*duvdx.x + 2.0*%s.y*duvdx.y,\n" 1.404 + "\t 2.0*%s.x*duvdy.x + 2.0*%s.y*duvdy.y);\n", 1.405 + fsOffsetName1, fsOffsetName1, fsOffsetName1, fsOffsetName1); 1.406 + builder->fsCodeAppend("\tinvlen = inversesqrt(dot(grad, grad));\n"); 1.407 + builder->fsCodeAppend("\tedgeAlpha *= clamp(0.5+test*invlen, 0.0, 1.0);\n"); 1.408 + } 1.409 + 1.410 + builder->fsCodeAppendf("\t%s = %s;\n", outputColor, 1.411 + (GrGLSLExpr4(inputColor) * GrGLSLExpr1("edgeAlpha")).c_str()); 1.412 + } 1.413 + 1.414 + static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) { 1.415 + const DIEllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<DIEllipseEdgeEffect>(); 1.416 + 1.417 + return ellipseEffect.getMode(); 1.418 + } 1.419 + 1.420 + virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE { 1.421 + } 1.422 + 1.423 + private: 1.424 + typedef GrGLVertexEffect INHERITED; 1.425 + }; 1.426 + 1.427 +private: 1.428 + DIEllipseEdgeEffect(Mode mode) : GrVertexEffect() { 1.429 + this->addVertexAttrib(kVec2f_GrSLType); 1.430 + this->addVertexAttrib(kVec2f_GrSLType); 1.431 + fMode = mode; 1.432 + } 1.433 + 1.434 + virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE { 1.435 + const DIEllipseEdgeEffect& eee = CastEffect<DIEllipseEdgeEffect>(other); 1.436 + return eee.fMode == fMode; 1.437 + } 1.438 + 1.439 + Mode fMode; 1.440 + 1.441 + GR_DECLARE_EFFECT_TEST; 1.442 + 1.443 + typedef GrVertexEffect INHERITED; 1.444 +}; 1.445 + 1.446 +GR_DEFINE_EFFECT_TEST(DIEllipseEdgeEffect); 1.447 + 1.448 +GrEffectRef* DIEllipseEdgeEffect::TestCreate(SkRandom* random, 1.449 + GrContext* context, 1.450 + const GrDrawTargetCaps&, 1.451 + GrTexture* textures[]) { 1.452 + return DIEllipseEdgeEffect::Create((Mode)(random->nextRangeU(0,2))); 1.453 +} 1.454 + 1.455 +/////////////////////////////////////////////////////////////////////////////// 1.456 + 1.457 +void GrOvalRenderer::reset() { 1.458 + SkSafeSetNull(fRRectIndexBuffer); 1.459 +} 1.460 + 1.461 +bool GrOvalRenderer::drawOval(GrDrawTarget* target, const GrContext* context, bool useAA, 1.462 + const SkRect& oval, const SkStrokeRec& stroke) 1.463 +{ 1.464 + bool useCoverageAA = useAA && 1.465 + !target->getDrawState().getRenderTarget()->isMultisampled() && 1.466 + !target->shouldDisableCoverageAAForBlend(); 1.467 + 1.468 + if (!useCoverageAA) { 1.469 + return false; 1.470 + } 1.471 + 1.472 + const SkMatrix& vm = context->getMatrix(); 1.473 + 1.474 + // we can draw circles 1.475 + if (SkScalarNearlyEqual(oval.width(), oval.height()) 1.476 + && circle_stays_circle(vm)) { 1.477 + this->drawCircle(target, useCoverageAA, oval, stroke); 1.478 + // if we have shader derivative support, render as device-independent 1.479 + } else if (target->caps()->shaderDerivativeSupport()) { 1.480 + return this->drawDIEllipse(target, useCoverageAA, oval, stroke); 1.481 + // otherwise axis-aligned ellipses only 1.482 + } else if (vm.rectStaysRect()) { 1.483 + return this->drawEllipse(target, useCoverageAA, oval, stroke); 1.484 + } else { 1.485 + return false; 1.486 + } 1.487 + 1.488 + return true; 1.489 +} 1.490 + 1.491 +/////////////////////////////////////////////////////////////////////////////// 1.492 + 1.493 +// position + edge 1.494 +extern const GrVertexAttrib gCircleVertexAttribs[] = { 1.495 + {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding}, 1.496 + {kVec4f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding} 1.497 +}; 1.498 + 1.499 +void GrOvalRenderer::drawCircle(GrDrawTarget* target, 1.500 + bool useCoverageAA, 1.501 + const SkRect& circle, 1.502 + const SkStrokeRec& stroke) 1.503 +{ 1.504 + GrDrawState* drawState = target->drawState(); 1.505 + 1.506 + const SkMatrix& vm = drawState->getViewMatrix(); 1.507 + GrPoint center = GrPoint::Make(circle.centerX(), circle.centerY()); 1.508 + vm.mapPoints(¢er, 1); 1.509 + SkScalar radius = vm.mapRadius(SkScalarHalf(circle.width())); 1.510 + SkScalar strokeWidth = vm.mapRadius(stroke.getWidth()); 1.511 + 1.512 + GrDrawState::AutoViewMatrixRestore avmr; 1.513 + if (!avmr.setIdentity(drawState)) { 1.514 + return; 1.515 + } 1.516 + 1.517 + drawState->setVertexAttribs<gCircleVertexAttribs>(SK_ARRAY_COUNT(gCircleVertexAttribs)); 1.518 + SkASSERT(sizeof(CircleVertex) == drawState->getVertexSize()); 1.519 + 1.520 + GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0); 1.521 + if (!geo.succeeded()) { 1.522 + GrPrintf("Failed to get space for vertices!\n"); 1.523 + return; 1.524 + } 1.525 + 1.526 + CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices()); 1.527 + 1.528 + SkStrokeRec::Style style = stroke.getStyle(); 1.529 + bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style); 1.530 + 1.531 + SkScalar innerRadius = 0.0f; 1.532 + SkScalar outerRadius = radius; 1.533 + SkScalar halfWidth = 0; 1.534 + if (style != SkStrokeRec::kFill_Style) { 1.535 + if (SkScalarNearlyZero(strokeWidth)) { 1.536 + halfWidth = SK_ScalarHalf; 1.537 + } else { 1.538 + halfWidth = SkScalarHalf(strokeWidth); 1.539 + } 1.540 + 1.541 + outerRadius += halfWidth; 1.542 + if (isStroked) { 1.543 + innerRadius = radius - halfWidth; 1.544 + } 1.545 + } 1.546 + 1.547 + GrEffectRef* effect = CircleEdgeEffect::Create(isStroked && innerRadius > 0); 1.548 + static const int kCircleEdgeAttrIndex = 1; 1.549 + drawState->addCoverageEffect(effect, kCircleEdgeAttrIndex)->unref(); 1.550 + 1.551 + // The radii are outset for two reasons. First, it allows the shader to simply perform 1.552 + // clamp(distance-to-center - radius, 0, 1). Second, the outer radius is used to compute the 1.553 + // verts of the bounding box that is rendered and the outset ensures the box will cover all 1.554 + // pixels partially covered by the circle. 1.555 + outerRadius += SK_ScalarHalf; 1.556 + innerRadius -= SK_ScalarHalf; 1.557 + 1.558 + SkRect bounds = SkRect::MakeLTRB( 1.559 + center.fX - outerRadius, 1.560 + center.fY - outerRadius, 1.561 + center.fX + outerRadius, 1.562 + center.fY + outerRadius 1.563 + ); 1.564 + 1.565 + verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop); 1.566 + verts[0].fOffset = SkPoint::Make(-outerRadius, -outerRadius); 1.567 + verts[0].fOuterRadius = outerRadius; 1.568 + verts[0].fInnerRadius = innerRadius; 1.569 + 1.570 + verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop); 1.571 + verts[1].fOffset = SkPoint::Make(outerRadius, -outerRadius); 1.572 + verts[1].fOuterRadius = outerRadius; 1.573 + verts[1].fInnerRadius = innerRadius; 1.574 + 1.575 + verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom); 1.576 + verts[2].fOffset = SkPoint::Make(-outerRadius, outerRadius); 1.577 + verts[2].fOuterRadius = outerRadius; 1.578 + verts[2].fInnerRadius = innerRadius; 1.579 + 1.580 + verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom); 1.581 + verts[3].fOffset = SkPoint::Make(outerRadius, outerRadius); 1.582 + verts[3].fOuterRadius = outerRadius; 1.583 + verts[3].fInnerRadius = innerRadius; 1.584 + 1.585 + target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds); 1.586 +} 1.587 + 1.588 +/////////////////////////////////////////////////////////////////////////////// 1.589 + 1.590 +// position + offset + 1/radii 1.591 +extern const GrVertexAttrib gEllipseVertexAttribs[] = { 1.592 + {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding}, 1.593 + {kVec2f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding}, 1.594 + {kVec4f_GrVertexAttribType, 2*sizeof(GrPoint), kEffect_GrVertexAttribBinding} 1.595 +}; 1.596 + 1.597 +// position + offsets 1.598 +extern const GrVertexAttrib gDIEllipseVertexAttribs[] = { 1.599 + {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding}, 1.600 + {kVec2f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding}, 1.601 + {kVec2f_GrVertexAttribType, 2*sizeof(GrPoint), kEffect_GrVertexAttribBinding}, 1.602 +}; 1.603 + 1.604 +bool GrOvalRenderer::drawEllipse(GrDrawTarget* target, 1.605 + bool useCoverageAA, 1.606 + const SkRect& ellipse, 1.607 + const SkStrokeRec& stroke) 1.608 +{ 1.609 + GrDrawState* drawState = target->drawState(); 1.610 +#ifdef SK_DEBUG 1.611 + { 1.612 + // we should have checked for this previously 1.613 + bool isAxisAlignedEllipse = drawState->getViewMatrix().rectStaysRect(); 1.614 + SkASSERT(useCoverageAA && isAxisAlignedEllipse); 1.615 + } 1.616 +#endif 1.617 + 1.618 + // do any matrix crunching before we reset the draw state for device coords 1.619 + const SkMatrix& vm = drawState->getViewMatrix(); 1.620 + GrPoint center = GrPoint::Make(ellipse.centerX(), ellipse.centerY()); 1.621 + vm.mapPoints(¢er, 1); 1.622 + SkScalar ellipseXRadius = SkScalarHalf(ellipse.width()); 1.623 + SkScalar ellipseYRadius = SkScalarHalf(ellipse.height()); 1.624 + SkScalar xRadius = SkScalarAbs(vm[SkMatrix::kMScaleX]*ellipseXRadius + 1.625 + vm[SkMatrix::kMSkewY]*ellipseYRadius); 1.626 + SkScalar yRadius = SkScalarAbs(vm[SkMatrix::kMSkewX]*ellipseXRadius + 1.627 + vm[SkMatrix::kMScaleY]*ellipseYRadius); 1.628 + 1.629 + // do (potentially) anisotropic mapping of stroke 1.630 + SkVector scaledStroke; 1.631 + SkScalar strokeWidth = stroke.getWidth(); 1.632 + scaledStroke.fX = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMScaleX] + vm[SkMatrix::kMSkewY])); 1.633 + scaledStroke.fY = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMSkewX] + vm[SkMatrix::kMScaleY])); 1.634 + 1.635 + SkStrokeRec::Style style = stroke.getStyle(); 1.636 + bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style); 1.637 + 1.638 + SkScalar innerXRadius = 0; 1.639 + SkScalar innerYRadius = 0; 1.640 + if (SkStrokeRec::kFill_Style != style) { 1.641 + if (SkScalarNearlyZero(scaledStroke.length())) { 1.642 + scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf); 1.643 + } else { 1.644 + scaledStroke.scale(SK_ScalarHalf); 1.645 + } 1.646 + 1.647 + // we only handle thick strokes for near-circular ellipses 1.648 + if (scaledStroke.length() > SK_ScalarHalf && 1.649 + (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) { 1.650 + return false; 1.651 + } 1.652 + 1.653 + // we don't handle it if curvature of the stroke is less than curvature of the ellipse 1.654 + if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius || 1.655 + scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) { 1.656 + return false; 1.657 + } 1.658 + 1.659 + // this is legit only if scale & translation (which should be the case at the moment) 1.660 + if (isStroked) { 1.661 + innerXRadius = xRadius - scaledStroke.fX; 1.662 + innerYRadius = yRadius - scaledStroke.fY; 1.663 + } 1.664 + 1.665 + xRadius += scaledStroke.fX; 1.666 + yRadius += scaledStroke.fY; 1.667 + } 1.668 + 1.669 + GrDrawState::AutoViewMatrixRestore avmr; 1.670 + if (!avmr.setIdentity(drawState)) { 1.671 + return false; 1.672 + } 1.673 + 1.674 + drawState->setVertexAttribs<gEllipseVertexAttribs>(SK_ARRAY_COUNT(gEllipseVertexAttribs)); 1.675 + SkASSERT(sizeof(EllipseVertex) == drawState->getVertexSize()); 1.676 + 1.677 + GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0); 1.678 + if (!geo.succeeded()) { 1.679 + GrPrintf("Failed to get space for vertices!\n"); 1.680 + return false; 1.681 + } 1.682 + 1.683 + EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices()); 1.684 + 1.685 + GrEffectRef* effect = EllipseEdgeEffect::Create(isStroked && 1.686 + innerXRadius > 0 && innerYRadius > 0); 1.687 + 1.688 + static const int kEllipseCenterAttrIndex = 1; 1.689 + static const int kEllipseEdgeAttrIndex = 2; 1.690 + drawState->addCoverageEffect(effect, kEllipseCenterAttrIndex, kEllipseEdgeAttrIndex)->unref(); 1.691 + 1.692 + // Compute the reciprocals of the radii here to save time in the shader 1.693 + SkScalar xRadRecip = SkScalarInvert(xRadius); 1.694 + SkScalar yRadRecip = SkScalarInvert(yRadius); 1.695 + SkScalar xInnerRadRecip = SkScalarInvert(innerXRadius); 1.696 + SkScalar yInnerRadRecip = SkScalarInvert(innerYRadius); 1.697 + 1.698 + // We've extended the outer x radius out half a pixel to antialias. 1.699 + // This will also expand the rect so all the pixels will be captured. 1.700 + // TODO: Consider if we should use sqrt(2)/2 instead 1.701 + xRadius += SK_ScalarHalf; 1.702 + yRadius += SK_ScalarHalf; 1.703 + 1.704 + SkRect bounds = SkRect::MakeLTRB( 1.705 + center.fX - xRadius, 1.706 + center.fY - yRadius, 1.707 + center.fX + xRadius, 1.708 + center.fY + yRadius 1.709 + ); 1.710 + 1.711 + verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop); 1.712 + verts[0].fOffset = SkPoint::Make(-xRadius, -yRadius); 1.713 + verts[0].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); 1.714 + verts[0].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); 1.715 + 1.716 + verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop); 1.717 + verts[1].fOffset = SkPoint::Make(xRadius, -yRadius); 1.718 + verts[1].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); 1.719 + verts[1].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); 1.720 + 1.721 + verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom); 1.722 + verts[2].fOffset = SkPoint::Make(-xRadius, yRadius); 1.723 + verts[2].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); 1.724 + verts[2].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); 1.725 + 1.726 + verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom); 1.727 + verts[3].fOffset = SkPoint::Make(xRadius, yRadius); 1.728 + verts[3].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); 1.729 + verts[3].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); 1.730 + 1.731 + target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds); 1.732 + 1.733 + return true; 1.734 +} 1.735 + 1.736 +bool GrOvalRenderer::drawDIEllipse(GrDrawTarget* target, 1.737 + bool useCoverageAA, 1.738 + const SkRect& ellipse, 1.739 + const SkStrokeRec& stroke) 1.740 +{ 1.741 + GrDrawState* drawState = target->drawState(); 1.742 + const SkMatrix& vm = drawState->getViewMatrix(); 1.743 + 1.744 + GrPoint center = GrPoint::Make(ellipse.centerX(), ellipse.centerY()); 1.745 + SkScalar xRadius = SkScalarHalf(ellipse.width()); 1.746 + SkScalar yRadius = SkScalarHalf(ellipse.height()); 1.747 + 1.748 + SkStrokeRec::Style style = stroke.getStyle(); 1.749 + DIEllipseEdgeEffect::Mode mode = (SkStrokeRec::kStroke_Style == style) ? 1.750 + DIEllipseEdgeEffect::kStroke : 1.751 + (SkStrokeRec::kHairline_Style == style) ? 1.752 + DIEllipseEdgeEffect::kHairline : DIEllipseEdgeEffect::kFill; 1.753 + 1.754 + SkScalar innerXRadius = 0; 1.755 + SkScalar innerYRadius = 0; 1.756 + if (SkStrokeRec::kFill_Style != style && SkStrokeRec::kHairline_Style != style) { 1.757 + SkScalar strokeWidth = stroke.getWidth(); 1.758 + 1.759 + if (SkScalarNearlyZero(strokeWidth)) { 1.760 + strokeWidth = SK_ScalarHalf; 1.761 + } else { 1.762 + strokeWidth *= SK_ScalarHalf; 1.763 + } 1.764 + 1.765 + // we only handle thick strokes for near-circular ellipses 1.766 + if (strokeWidth > SK_ScalarHalf && 1.767 + (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) { 1.768 + return false; 1.769 + } 1.770 + 1.771 + // we don't handle it if curvature of the stroke is less than curvature of the ellipse 1.772 + if (strokeWidth*(yRadius*yRadius) < (strokeWidth*strokeWidth)*xRadius || 1.773 + strokeWidth*(xRadius*xRadius) < (strokeWidth*strokeWidth)*yRadius) { 1.774 + return false; 1.775 + } 1.776 + 1.777 + // set inner radius (if needed) 1.778 + if (SkStrokeRec::kStroke_Style == style) { 1.779 + innerXRadius = xRadius - strokeWidth; 1.780 + innerYRadius = yRadius - strokeWidth; 1.781 + } 1.782 + 1.783 + xRadius += strokeWidth; 1.784 + yRadius += strokeWidth; 1.785 + } 1.786 + if (DIEllipseEdgeEffect::kStroke == mode) { 1.787 + mode = (innerXRadius > 0 && innerYRadius > 0) ? DIEllipseEdgeEffect::kStroke : 1.788 + DIEllipseEdgeEffect::kFill; 1.789 + } 1.790 + SkScalar innerRatioX = SkScalarDiv(xRadius, innerXRadius); 1.791 + SkScalar innerRatioY = SkScalarDiv(yRadius, innerYRadius); 1.792 + 1.793 + drawState->setVertexAttribs<gDIEllipseVertexAttribs>(SK_ARRAY_COUNT(gDIEllipseVertexAttribs)); 1.794 + SkASSERT(sizeof(DIEllipseVertex) == drawState->getVertexSize()); 1.795 + 1.796 + GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0); 1.797 + if (!geo.succeeded()) { 1.798 + GrPrintf("Failed to get space for vertices!\n"); 1.799 + return false; 1.800 + } 1.801 + 1.802 + DIEllipseVertex* verts = reinterpret_cast<DIEllipseVertex*>(geo.vertices()); 1.803 + 1.804 + GrEffectRef* effect = DIEllipseEdgeEffect::Create(mode); 1.805 + 1.806 + static const int kEllipseOuterOffsetAttrIndex = 1; 1.807 + static const int kEllipseInnerOffsetAttrIndex = 2; 1.808 + drawState->addCoverageEffect(effect, kEllipseOuterOffsetAttrIndex, 1.809 + kEllipseInnerOffsetAttrIndex)->unref(); 1.810 + 1.811 + // This expands the outer rect so that after CTM we end up with a half-pixel border 1.812 + SkScalar a = vm[SkMatrix::kMScaleX]; 1.813 + SkScalar b = vm[SkMatrix::kMSkewX]; 1.814 + SkScalar c = vm[SkMatrix::kMSkewY]; 1.815 + SkScalar d = vm[SkMatrix::kMScaleY]; 1.816 + SkScalar geoDx = SkScalarDiv(SK_ScalarHalf, SkScalarSqrt(a*a + c*c)); 1.817 + SkScalar geoDy = SkScalarDiv(SK_ScalarHalf, SkScalarSqrt(b*b + d*d)); 1.818 + // This adjusts the "radius" to include the half-pixel border 1.819 + SkScalar offsetDx = SkScalarDiv(geoDx, xRadius); 1.820 + SkScalar offsetDy = SkScalarDiv(geoDy, yRadius); 1.821 + 1.822 + SkRect bounds = SkRect::MakeLTRB( 1.823 + center.fX - xRadius - geoDx, 1.824 + center.fY - yRadius - geoDy, 1.825 + center.fX + xRadius + geoDx, 1.826 + center.fY + yRadius + geoDy 1.827 + ); 1.828 + 1.829 + verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop); 1.830 + verts[0].fOuterOffset = SkPoint::Make(-1.0f - offsetDx, -1.0f - offsetDy); 1.831 + verts[0].fInnerOffset = SkPoint::Make(-innerRatioX - offsetDx, -innerRatioY - offsetDy); 1.832 + 1.833 + verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop); 1.834 + verts[1].fOuterOffset = SkPoint::Make(1.0f + offsetDx, -1.0f - offsetDy); 1.835 + verts[1].fInnerOffset = SkPoint::Make(innerRatioX + offsetDx, -innerRatioY - offsetDy); 1.836 + 1.837 + verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom); 1.838 + verts[2].fOuterOffset = SkPoint::Make(-1.0f - offsetDx, 1.0f + offsetDy); 1.839 + verts[2].fInnerOffset = SkPoint::Make(-innerRatioX - offsetDx, innerRatioY + offsetDy); 1.840 + 1.841 + verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom); 1.842 + verts[3].fOuterOffset = SkPoint::Make(1.0f + offsetDx, 1.0f + offsetDy); 1.843 + verts[3].fInnerOffset = SkPoint::Make(innerRatioX + offsetDx, innerRatioY + offsetDy); 1.844 + 1.845 + target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds); 1.846 + 1.847 + return true; 1.848 +} 1.849 + 1.850 +/////////////////////////////////////////////////////////////////////////////// 1.851 + 1.852 +static const uint16_t gRRectIndices[] = { 1.853 + // corners 1.854 + 0, 1, 5, 0, 5, 4, 1.855 + 2, 3, 7, 2, 7, 6, 1.856 + 8, 9, 13, 8, 13, 12, 1.857 + 10, 11, 15, 10, 15, 14, 1.858 + 1.859 + // edges 1.860 + 1, 2, 6, 1, 6, 5, 1.861 + 4, 5, 9, 4, 9, 8, 1.862 + 6, 7, 11, 6, 11, 10, 1.863 + 9, 10, 14, 9, 14, 13, 1.864 + 1.865 + // center 1.866 + // we place this at the end so that we can ignore these indices when rendering stroke-only 1.867 + 5, 6, 10, 5, 10, 9 1.868 +}; 1.869 + 1.870 + 1.871 +GrIndexBuffer* GrOvalRenderer::rRectIndexBuffer(GrGpu* gpu) { 1.872 + if (NULL == fRRectIndexBuffer) { 1.873 + fRRectIndexBuffer = 1.874 + gpu->createIndexBuffer(sizeof(gRRectIndices), false); 1.875 + if (NULL != fRRectIndexBuffer) { 1.876 +#ifdef SK_DEBUG 1.877 + bool updated = 1.878 +#endif 1.879 + fRRectIndexBuffer->updateData(gRRectIndices, 1.880 + sizeof(gRRectIndices)); 1.881 + GR_DEBUGASSERT(updated); 1.882 + } 1.883 + } 1.884 + return fRRectIndexBuffer; 1.885 +} 1.886 + 1.887 +bool GrOvalRenderer::drawSimpleRRect(GrDrawTarget* target, GrContext* context, bool useAA, 1.888 + const SkRRect& rrect, const SkStrokeRec& stroke) 1.889 +{ 1.890 + bool useCoverageAA = useAA && 1.891 + !target->getDrawState().getRenderTarget()->isMultisampled() && 1.892 + !target->shouldDisableCoverageAAForBlend(); 1.893 + 1.894 + // only anti-aliased rrects for now 1.895 + if (!useCoverageAA) { 1.896 + return false; 1.897 + } 1.898 + 1.899 + const SkMatrix& vm = context->getMatrix(); 1.900 +#ifdef SK_DEBUG 1.901 + { 1.902 + // we should have checked for this previously 1.903 + SkASSERT(useCoverageAA && vm.rectStaysRect() && rrect.isSimple()); 1.904 + } 1.905 +#endif 1.906 + 1.907 + // do any matrix crunching before we reset the draw state for device coords 1.908 + const SkRect& rrectBounds = rrect.getBounds(); 1.909 + SkRect bounds; 1.910 + vm.mapRect(&bounds, rrectBounds); 1.911 + 1.912 + SkVector radii = rrect.getSimpleRadii(); 1.913 + SkScalar xRadius = SkScalarAbs(vm[SkMatrix::kMScaleX]*radii.fX + 1.914 + vm[SkMatrix::kMSkewY]*radii.fY); 1.915 + SkScalar yRadius = SkScalarAbs(vm[SkMatrix::kMSkewX]*radii.fX + 1.916 + vm[SkMatrix::kMScaleY]*radii.fY); 1.917 + 1.918 + // if hairline stroke is greater than radius, we don't handle that right now 1.919 + SkStrokeRec::Style style = stroke.getStyle(); 1.920 + if (SkStrokeRec::kHairline_Style == style && 1.921 + (SK_ScalarHalf > xRadius || SK_ScalarHalf > yRadius)) { 1.922 + return false; 1.923 + } 1.924 + 1.925 + // do (potentially) anisotropic mapping of stroke 1.926 + SkVector scaledStroke; 1.927 + SkScalar strokeWidth = stroke.getWidth(); 1.928 + scaledStroke.fX = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMScaleX] + vm[SkMatrix::kMSkewY])); 1.929 + scaledStroke.fY = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMSkewX] + vm[SkMatrix::kMScaleY])); 1.930 + 1.931 + // if half of strokewidth is greater than radius, we don't handle that right now 1.932 + if (SK_ScalarHalf*scaledStroke.fX > xRadius || SK_ScalarHalf*scaledStroke.fY > yRadius) { 1.933 + return false; 1.934 + } 1.935 + 1.936 + // reset to device coordinates 1.937 + GrDrawState* drawState = target->drawState(); 1.938 + GrDrawState::AutoViewMatrixRestore avmr; 1.939 + if (!avmr.setIdentity(drawState)) { 1.940 + return false; 1.941 + } 1.942 + 1.943 + bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style); 1.944 + 1.945 + GrIndexBuffer* indexBuffer = this->rRectIndexBuffer(context->getGpu()); 1.946 + if (NULL == indexBuffer) { 1.947 + GrPrintf("Failed to create index buffer!\n"); 1.948 + return false; 1.949 + } 1.950 + 1.951 + // if the corners are circles, use the circle renderer 1.952 + if ((!isStroked || scaledStroke.fX == scaledStroke.fY) && xRadius == yRadius) { 1.953 + drawState->setVertexAttribs<gCircleVertexAttribs>(SK_ARRAY_COUNT(gCircleVertexAttribs)); 1.954 + SkASSERT(sizeof(CircleVertex) == drawState->getVertexSize()); 1.955 + 1.956 + GrDrawTarget::AutoReleaseGeometry geo(target, 16, 0); 1.957 + if (!geo.succeeded()) { 1.958 + GrPrintf("Failed to get space for vertices!\n"); 1.959 + return false; 1.960 + } 1.961 + CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices()); 1.962 + 1.963 + SkScalar innerRadius = 0.0f; 1.964 + SkScalar outerRadius = xRadius; 1.965 + SkScalar halfWidth = 0; 1.966 + if (style != SkStrokeRec::kFill_Style) { 1.967 + if (SkScalarNearlyZero(scaledStroke.fX)) { 1.968 + halfWidth = SK_ScalarHalf; 1.969 + } else { 1.970 + halfWidth = SkScalarHalf(scaledStroke.fX); 1.971 + } 1.972 + 1.973 + if (isStroked) { 1.974 + innerRadius = xRadius - halfWidth; 1.975 + } 1.976 + outerRadius += halfWidth; 1.977 + bounds.outset(halfWidth, halfWidth); 1.978 + } 1.979 + 1.980 + isStroked = (isStroked && innerRadius >= 0); 1.981 + 1.982 + GrEffectRef* effect = CircleEdgeEffect::Create(isStroked); 1.983 + static const int kCircleEdgeAttrIndex = 1; 1.984 + drawState->addCoverageEffect(effect, kCircleEdgeAttrIndex)->unref(); 1.985 + 1.986 + // The radii are outset for two reasons. First, it allows the shader to simply perform 1.987 + // clamp(distance-to-center - radius, 0, 1). Second, the outer radius is used to compute the 1.988 + // verts of the bounding box that is rendered and the outset ensures the box will cover all 1.989 + // pixels partially covered by the circle. 1.990 + outerRadius += SK_ScalarHalf; 1.991 + innerRadius -= SK_ScalarHalf; 1.992 + 1.993 + // Expand the rect so all the pixels will be captured. 1.994 + bounds.outset(SK_ScalarHalf, SK_ScalarHalf); 1.995 + 1.996 + SkScalar yCoords[4] = { 1.997 + bounds.fTop, 1.998 + bounds.fTop + outerRadius, 1.999 + bounds.fBottom - outerRadius, 1.1000 + bounds.fBottom 1.1001 + }; 1.1002 + SkScalar yOuterRadii[4] = { 1.1003 + -outerRadius, 1.1004 + 0, 1.1005 + 0, 1.1006 + outerRadius 1.1007 + }; 1.1008 + for (int i = 0; i < 4; ++i) { 1.1009 + verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]); 1.1010 + verts->fOffset = SkPoint::Make(-outerRadius, yOuterRadii[i]); 1.1011 + verts->fOuterRadius = outerRadius; 1.1012 + verts->fInnerRadius = innerRadius; 1.1013 + verts++; 1.1014 + 1.1015 + verts->fPos = SkPoint::Make(bounds.fLeft + outerRadius, yCoords[i]); 1.1016 + verts->fOffset = SkPoint::Make(0, yOuterRadii[i]); 1.1017 + verts->fOuterRadius = outerRadius; 1.1018 + verts->fInnerRadius = innerRadius; 1.1019 + verts++; 1.1020 + 1.1021 + verts->fPos = SkPoint::Make(bounds.fRight - outerRadius, yCoords[i]); 1.1022 + verts->fOffset = SkPoint::Make(0, yOuterRadii[i]); 1.1023 + verts->fOuterRadius = outerRadius; 1.1024 + verts->fInnerRadius = innerRadius; 1.1025 + verts++; 1.1026 + 1.1027 + verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]); 1.1028 + verts->fOffset = SkPoint::Make(outerRadius, yOuterRadii[i]); 1.1029 + verts->fOuterRadius = outerRadius; 1.1030 + verts->fInnerRadius = innerRadius; 1.1031 + verts++; 1.1032 + } 1.1033 + 1.1034 + // drop out the middle quad if we're stroked 1.1035 + int indexCnt = isStroked ? GR_ARRAY_COUNT(gRRectIndices)-6 : GR_ARRAY_COUNT(gRRectIndices); 1.1036 + target->setIndexSourceToBuffer(indexBuffer); 1.1037 + target->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, 16, indexCnt, &bounds); 1.1038 + 1.1039 + // otherwise we use the ellipse renderer 1.1040 + } else { 1.1041 + drawState->setVertexAttribs<gEllipseVertexAttribs>(SK_ARRAY_COUNT(gEllipseVertexAttribs)); 1.1042 + SkASSERT(sizeof(EllipseVertex) == drawState->getVertexSize()); 1.1043 + 1.1044 + SkScalar innerXRadius = 0.0f; 1.1045 + SkScalar innerYRadius = 0.0f; 1.1046 + if (SkStrokeRec::kFill_Style != style) { 1.1047 + if (SkScalarNearlyZero(scaledStroke.length())) { 1.1048 + scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf); 1.1049 + } else { 1.1050 + scaledStroke.scale(SK_ScalarHalf); 1.1051 + } 1.1052 + 1.1053 + // we only handle thick strokes for near-circular ellipses 1.1054 + if (scaledStroke.length() > SK_ScalarHalf && 1.1055 + (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) { 1.1056 + return false; 1.1057 + } 1.1058 + 1.1059 + // we don't handle it if curvature of the stroke is less than curvature of the ellipse 1.1060 + if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius || 1.1061 + scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) { 1.1062 + return false; 1.1063 + } 1.1064 + 1.1065 + // this is legit only if scale & translation (which should be the case at the moment) 1.1066 + if (isStroked) { 1.1067 + innerXRadius = xRadius - scaledStroke.fX; 1.1068 + innerYRadius = yRadius - scaledStroke.fY; 1.1069 + } 1.1070 + 1.1071 + xRadius += scaledStroke.fX; 1.1072 + yRadius += scaledStroke.fY; 1.1073 + bounds.outset(scaledStroke.fX, scaledStroke.fY); 1.1074 + } 1.1075 + 1.1076 + isStroked = (isStroked && innerXRadius >= 0 && innerYRadius >= 0); 1.1077 + 1.1078 + GrDrawTarget::AutoReleaseGeometry geo(target, 16, 0); 1.1079 + if (!geo.succeeded()) { 1.1080 + GrPrintf("Failed to get space for vertices!\n"); 1.1081 + return false; 1.1082 + } 1.1083 + EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices()); 1.1084 + 1.1085 + GrEffectRef* effect = EllipseEdgeEffect::Create(isStroked); 1.1086 + static const int kEllipseOffsetAttrIndex = 1; 1.1087 + static const int kEllipseRadiiAttrIndex = 2; 1.1088 + drawState->addCoverageEffect(effect, 1.1089 + kEllipseOffsetAttrIndex, kEllipseRadiiAttrIndex)->unref(); 1.1090 + 1.1091 + // Compute the reciprocals of the radii here to save time in the shader 1.1092 + SkScalar xRadRecip = SkScalarInvert(xRadius); 1.1093 + SkScalar yRadRecip = SkScalarInvert(yRadius); 1.1094 + SkScalar xInnerRadRecip = SkScalarInvert(innerXRadius); 1.1095 + SkScalar yInnerRadRecip = SkScalarInvert(innerYRadius); 1.1096 + 1.1097 + // Extend the radii out half a pixel to antialias. 1.1098 + SkScalar xOuterRadius = xRadius + SK_ScalarHalf; 1.1099 + SkScalar yOuterRadius = yRadius + SK_ScalarHalf; 1.1100 + 1.1101 + // Expand the rect so all the pixels will be captured. 1.1102 + bounds.outset(SK_ScalarHalf, SK_ScalarHalf); 1.1103 + 1.1104 + SkScalar yCoords[4] = { 1.1105 + bounds.fTop, 1.1106 + bounds.fTop + yOuterRadius, 1.1107 + bounds.fBottom - yOuterRadius, 1.1108 + bounds.fBottom 1.1109 + }; 1.1110 + SkScalar yOuterOffsets[4] = { 1.1111 + yOuterRadius, 1.1112 + SK_ScalarNearlyZero, // we're using inversesqrt() in the shader, so can't be exactly 0 1.1113 + SK_ScalarNearlyZero, 1.1114 + yOuterRadius 1.1115 + }; 1.1116 + 1.1117 + for (int i = 0; i < 4; ++i) { 1.1118 + verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]); 1.1119 + verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]); 1.1120 + verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); 1.1121 + verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); 1.1122 + verts++; 1.1123 + 1.1124 + verts->fPos = SkPoint::Make(bounds.fLeft + xOuterRadius, yCoords[i]); 1.1125 + verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]); 1.1126 + verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); 1.1127 + verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); 1.1128 + verts++; 1.1129 + 1.1130 + verts->fPos = SkPoint::Make(bounds.fRight - xOuterRadius, yCoords[i]); 1.1131 + verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]); 1.1132 + verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); 1.1133 + verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); 1.1134 + verts++; 1.1135 + 1.1136 + verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]); 1.1137 + verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]); 1.1138 + verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); 1.1139 + verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); 1.1140 + verts++; 1.1141 + } 1.1142 + 1.1143 + // drop out the middle quad if we're stroked 1.1144 + int indexCnt = isStroked ? GR_ARRAY_COUNT(gRRectIndices)-6 : GR_ARRAY_COUNT(gRRectIndices); 1.1145 + target->setIndexSourceToBuffer(indexBuffer); 1.1146 + target->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, 16, indexCnt, &bounds); 1.1147 + } 1.1148 + 1.1149 + return true; 1.1150 +}