gfx/skia/trunk/src/gpu/GrOvalRenderer.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /*
michael@0 2 * Copyright 2013 Google Inc.
michael@0 3 *
michael@0 4 * Use of this source code is governed by a BSD-style license that can be
michael@0 5 * found in the LICENSE file.
michael@0 6 */
michael@0 7
michael@0 8 #include "GrOvalRenderer.h"
michael@0 9
michael@0 10 #include "GrEffect.h"
michael@0 11 #include "gl/GrGLEffect.h"
michael@0 12 #include "gl/GrGLSL.h"
michael@0 13 #include "gl/GrGLVertexEffect.h"
michael@0 14 #include "GrTBackendEffectFactory.h"
michael@0 15
michael@0 16 #include "GrDrawState.h"
michael@0 17 #include "GrDrawTarget.h"
michael@0 18 #include "GrGpu.h"
michael@0 19
michael@0 20 #include "SkRRect.h"
michael@0 21 #include "SkStrokeRec.h"
michael@0 22
michael@0 23 #include "effects/GrVertexEffect.h"
michael@0 24
michael@0 25 namespace {
michael@0 26
michael@0 27 struct CircleVertex {
michael@0 28 GrPoint fPos;
michael@0 29 GrPoint fOffset;
michael@0 30 SkScalar fOuterRadius;
michael@0 31 SkScalar fInnerRadius;
michael@0 32 };
michael@0 33
michael@0 34 struct EllipseVertex {
michael@0 35 GrPoint fPos;
michael@0 36 GrPoint fOffset;
michael@0 37 GrPoint fOuterRadii;
michael@0 38 GrPoint fInnerRadii;
michael@0 39 };
michael@0 40
michael@0 41 struct DIEllipseVertex {
michael@0 42 GrPoint fPos;
michael@0 43 GrPoint fOuterOffset;
michael@0 44 GrPoint fInnerOffset;
michael@0 45 };
michael@0 46
michael@0 47 inline bool circle_stays_circle(const SkMatrix& m) {
michael@0 48 return m.isSimilarity();
michael@0 49 }
michael@0 50
michael@0 51 }
michael@0 52
michael@0 53 ///////////////////////////////////////////////////////////////////////////////
michael@0 54
michael@0 55 /**
michael@0 56 * The output of this effect is a modulation of the input color and coverage for a circle,
michael@0 57 * specified as offset_x, offset_y (both from center point), outer radius and inner radius.
michael@0 58 */
michael@0 59
michael@0 60 class CircleEdgeEffect : public GrVertexEffect {
michael@0 61 public:
michael@0 62 static GrEffectRef* Create(bool stroke) {
michael@0 63 GR_CREATE_STATIC_EFFECT(gCircleStrokeEdge, CircleEdgeEffect, (true));
michael@0 64 GR_CREATE_STATIC_EFFECT(gCircleFillEdge, CircleEdgeEffect, (false));
michael@0 65
michael@0 66 if (stroke) {
michael@0 67 gCircleStrokeEdge->ref();
michael@0 68 return gCircleStrokeEdge;
michael@0 69 } else {
michael@0 70 gCircleFillEdge->ref();
michael@0 71 return gCircleFillEdge;
michael@0 72 }
michael@0 73 }
michael@0 74
michael@0 75 virtual void getConstantColorComponents(GrColor* color,
michael@0 76 uint32_t* validFlags) const SK_OVERRIDE {
michael@0 77 *validFlags = 0;
michael@0 78 }
michael@0 79
michael@0 80 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
michael@0 81 return GrTBackendEffectFactory<CircleEdgeEffect>::getInstance();
michael@0 82 }
michael@0 83
michael@0 84 virtual ~CircleEdgeEffect() {}
michael@0 85
michael@0 86 static const char* Name() { return "CircleEdge"; }
michael@0 87
michael@0 88 inline bool isStroked() const { return fStroke; }
michael@0 89
michael@0 90 class GLEffect : public GrGLVertexEffect {
michael@0 91 public:
michael@0 92 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
michael@0 93 : INHERITED (factory) {}
michael@0 94
michael@0 95 virtual void emitCode(GrGLFullShaderBuilder* builder,
michael@0 96 const GrDrawEffect& drawEffect,
michael@0 97 EffectKey key,
michael@0 98 const char* outputColor,
michael@0 99 const char* inputColor,
michael@0 100 const TransformedCoordsArray&,
michael@0 101 const TextureSamplerArray& samplers) SK_OVERRIDE {
michael@0 102 const CircleEdgeEffect& circleEffect = drawEffect.castEffect<CircleEdgeEffect>();
michael@0 103 const char *vsName, *fsName;
michael@0 104 builder->addVarying(kVec4f_GrSLType, "CircleEdge", &vsName, &fsName);
michael@0 105
michael@0 106 const SkString* attrName =
michael@0 107 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]);
michael@0 108 builder->vsCodeAppendf("\t%s = %s;\n", vsName, attrName->c_str());
michael@0 109
michael@0 110 builder->fsCodeAppendf("\tfloat d = length(%s.xy);\n", fsName);
michael@0 111 builder->fsCodeAppendf("\tfloat edgeAlpha = clamp(%s.z - d, 0.0, 1.0);\n", fsName);
michael@0 112 if (circleEffect.isStroked()) {
michael@0 113 builder->fsCodeAppendf("\tfloat innerAlpha = clamp(d - %s.w, 0.0, 1.0);\n", fsName);
michael@0 114 builder->fsCodeAppend("\tedgeAlpha *= innerAlpha;\n");
michael@0 115 }
michael@0 116
michael@0 117 builder->fsCodeAppendf("\t%s = %s;\n", outputColor,
michael@0 118 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("edgeAlpha")).c_str());
michael@0 119 }
michael@0 120
michael@0 121 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
michael@0 122 const CircleEdgeEffect& circleEffect = drawEffect.castEffect<CircleEdgeEffect>();
michael@0 123
michael@0 124 return circleEffect.isStroked() ? 0x1 : 0x0;
michael@0 125 }
michael@0 126
michael@0 127 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {}
michael@0 128
michael@0 129 private:
michael@0 130 typedef GrGLVertexEffect INHERITED;
michael@0 131 };
michael@0 132
michael@0 133
michael@0 134 private:
michael@0 135 CircleEdgeEffect(bool stroke) : GrVertexEffect() {
michael@0 136 this->addVertexAttrib(kVec4f_GrSLType);
michael@0 137 fStroke = stroke;
michael@0 138 }
michael@0 139
michael@0 140 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
michael@0 141 const CircleEdgeEffect& cee = CastEffect<CircleEdgeEffect>(other);
michael@0 142 return cee.fStroke == fStroke;
michael@0 143 }
michael@0 144
michael@0 145 bool fStroke;
michael@0 146
michael@0 147 GR_DECLARE_EFFECT_TEST;
michael@0 148
michael@0 149 typedef GrVertexEffect INHERITED;
michael@0 150 };
michael@0 151
michael@0 152 GR_DEFINE_EFFECT_TEST(CircleEdgeEffect);
michael@0 153
michael@0 154 GrEffectRef* CircleEdgeEffect::TestCreate(SkRandom* random,
michael@0 155 GrContext* context,
michael@0 156 const GrDrawTargetCaps&,
michael@0 157 GrTexture* textures[]) {
michael@0 158 return CircleEdgeEffect::Create(random->nextBool());
michael@0 159 }
michael@0 160
michael@0 161 ///////////////////////////////////////////////////////////////////////////////
michael@0 162
michael@0 163 /**
michael@0 164 * The output of this effect is a modulation of the input color and coverage for an axis-aligned
michael@0 165 * ellipse, specified as a 2D offset from center, and the reciprocals of the outer and inner radii,
michael@0 166 * in both x and y directions.
michael@0 167 *
michael@0 168 * We are using an implicit function of x^2/a^2 + y^2/b^2 - 1 = 0.
michael@0 169 */
michael@0 170
michael@0 171 class EllipseEdgeEffect : public GrVertexEffect {
michael@0 172 public:
michael@0 173 static GrEffectRef* Create(bool stroke) {
michael@0 174 GR_CREATE_STATIC_EFFECT(gEllipseStrokeEdge, EllipseEdgeEffect, (true));
michael@0 175 GR_CREATE_STATIC_EFFECT(gEllipseFillEdge, EllipseEdgeEffect, (false));
michael@0 176
michael@0 177 if (stroke) {
michael@0 178 gEllipseStrokeEdge->ref();
michael@0 179 return gEllipseStrokeEdge;
michael@0 180 } else {
michael@0 181 gEllipseFillEdge->ref();
michael@0 182 return gEllipseFillEdge;
michael@0 183 }
michael@0 184 }
michael@0 185
michael@0 186 virtual void getConstantColorComponents(GrColor* color,
michael@0 187 uint32_t* validFlags) const SK_OVERRIDE {
michael@0 188 *validFlags = 0;
michael@0 189 }
michael@0 190
michael@0 191 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
michael@0 192 return GrTBackendEffectFactory<EllipseEdgeEffect>::getInstance();
michael@0 193 }
michael@0 194
michael@0 195 virtual ~EllipseEdgeEffect() {}
michael@0 196
michael@0 197 static const char* Name() { return "EllipseEdge"; }
michael@0 198
michael@0 199 inline bool isStroked() const { return fStroke; }
michael@0 200
michael@0 201 class GLEffect : public GrGLVertexEffect {
michael@0 202 public:
michael@0 203 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
michael@0 204 : INHERITED (factory) {}
michael@0 205
michael@0 206 virtual void emitCode(GrGLFullShaderBuilder* builder,
michael@0 207 const GrDrawEffect& drawEffect,
michael@0 208 EffectKey key,
michael@0 209 const char* outputColor,
michael@0 210 const char* inputColor,
michael@0 211 const TransformedCoordsArray&,
michael@0 212 const TextureSamplerArray& samplers) SK_OVERRIDE {
michael@0 213 const EllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<EllipseEdgeEffect>();
michael@0 214
michael@0 215 const char *vsOffsetName, *fsOffsetName;
michael@0 216 const char *vsRadiiName, *fsRadiiName;
michael@0 217
michael@0 218 builder->addVarying(kVec2f_GrSLType, "EllipseOffsets", &vsOffsetName, &fsOffsetName);
michael@0 219 const SkString* attr0Name =
michael@0 220 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]);
michael@0 221 builder->vsCodeAppendf("\t%s = %s;\n", vsOffsetName, attr0Name->c_str());
michael@0 222
michael@0 223 builder->addVarying(kVec4f_GrSLType, "EllipseRadii", &vsRadiiName, &fsRadiiName);
michael@0 224 const SkString* attr1Name =
michael@0 225 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[1]);
michael@0 226 builder->vsCodeAppendf("\t%s = %s;\n", vsRadiiName, attr1Name->c_str());
michael@0 227
michael@0 228 // for outer curve
michael@0 229 builder->fsCodeAppendf("\tvec2 scaledOffset = %s*%s.xy;\n", fsOffsetName, fsRadiiName);
michael@0 230 builder->fsCodeAppend("\tfloat test = dot(scaledOffset, scaledOffset) - 1.0;\n");
michael@0 231 builder->fsCodeAppendf("\tvec2 grad = 2.0*scaledOffset*%s.xy;\n", fsRadiiName);
michael@0 232 builder->fsCodeAppend("\tfloat grad_dot = dot(grad, grad);\n");
michael@0 233 // we need to clamp the length^2 of the gradiant vector to a non-zero value, because
michael@0 234 // on the Nexus 4 the undefined result of inversesqrt(0) drops out an entire tile
michael@0 235 // TODO: restrict this to Adreno-only
michael@0 236 builder->fsCodeAppend("\tgrad_dot = max(grad_dot, 1.0e-4);\n");
michael@0 237 builder->fsCodeAppend("\tfloat invlen = inversesqrt(grad_dot);\n");
michael@0 238 builder->fsCodeAppend("\tfloat edgeAlpha = clamp(0.5-test*invlen, 0.0, 1.0);\n");
michael@0 239
michael@0 240 // for inner curve
michael@0 241 if (ellipseEffect.isStroked()) {
michael@0 242 builder->fsCodeAppendf("\tscaledOffset = %s*%s.zw;\n", fsOffsetName, fsRadiiName);
michael@0 243 builder->fsCodeAppend("\ttest = dot(scaledOffset, scaledOffset) - 1.0;\n");
michael@0 244 builder->fsCodeAppendf("\tgrad = 2.0*scaledOffset*%s.zw;\n", fsRadiiName);
michael@0 245 builder->fsCodeAppend("\tinvlen = inversesqrt(dot(grad, grad));\n");
michael@0 246 builder->fsCodeAppend("\tedgeAlpha *= clamp(0.5+test*invlen, 0.0, 1.0);\n");
michael@0 247 }
michael@0 248
michael@0 249 builder->fsCodeAppendf("\t%s = %s;\n", outputColor,
michael@0 250 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("edgeAlpha")).c_str());
michael@0 251 }
michael@0 252
michael@0 253 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
michael@0 254 const EllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<EllipseEdgeEffect>();
michael@0 255
michael@0 256 return ellipseEffect.isStroked() ? 0x1 : 0x0;
michael@0 257 }
michael@0 258
michael@0 259 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {
michael@0 260 }
michael@0 261
michael@0 262 private:
michael@0 263 typedef GrGLVertexEffect INHERITED;
michael@0 264 };
michael@0 265
michael@0 266 private:
michael@0 267 EllipseEdgeEffect(bool stroke) : GrVertexEffect() {
michael@0 268 this->addVertexAttrib(kVec2f_GrSLType);
michael@0 269 this->addVertexAttrib(kVec4f_GrSLType);
michael@0 270 fStroke = stroke;
michael@0 271 }
michael@0 272
michael@0 273 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
michael@0 274 const EllipseEdgeEffect& eee = CastEffect<EllipseEdgeEffect>(other);
michael@0 275 return eee.fStroke == fStroke;
michael@0 276 }
michael@0 277
michael@0 278 bool fStroke;
michael@0 279
michael@0 280 GR_DECLARE_EFFECT_TEST;
michael@0 281
michael@0 282 typedef GrVertexEffect INHERITED;
michael@0 283 };
michael@0 284
michael@0 285 GR_DEFINE_EFFECT_TEST(EllipseEdgeEffect);
michael@0 286
michael@0 287 GrEffectRef* EllipseEdgeEffect::TestCreate(SkRandom* random,
michael@0 288 GrContext* context,
michael@0 289 const GrDrawTargetCaps&,
michael@0 290 GrTexture* textures[]) {
michael@0 291 return EllipseEdgeEffect::Create(random->nextBool());
michael@0 292 }
michael@0 293
michael@0 294 ///////////////////////////////////////////////////////////////////////////////
michael@0 295
michael@0 296 /**
michael@0 297 * The output of this effect is a modulation of the input color and coverage for an ellipse,
michael@0 298 * specified as a 2D offset from center for both the outer and inner paths (if stroked). The
michael@0 299 * implict equation used is for a unit circle (x^2 + y^2 - 1 = 0) and the edge corrected by
michael@0 300 * using differentials.
michael@0 301 *
michael@0 302 * The result is device-independent and can be used with any affine matrix.
michael@0 303 */
michael@0 304
michael@0 305 class DIEllipseEdgeEffect : public GrVertexEffect {
michael@0 306 public:
michael@0 307 enum Mode { kStroke = 0, kHairline, kFill };
michael@0 308
michael@0 309 static GrEffectRef* Create(Mode mode) {
michael@0 310 GR_CREATE_STATIC_EFFECT(gEllipseStrokeEdge, DIEllipseEdgeEffect, (kStroke));
michael@0 311 GR_CREATE_STATIC_EFFECT(gEllipseHairlineEdge, DIEllipseEdgeEffect, (kHairline));
michael@0 312 GR_CREATE_STATIC_EFFECT(gEllipseFillEdge, DIEllipseEdgeEffect, (kFill));
michael@0 313
michael@0 314 if (kStroke == mode) {
michael@0 315 gEllipseStrokeEdge->ref();
michael@0 316 return gEllipseStrokeEdge;
michael@0 317 } else if (kHairline == mode) {
michael@0 318 gEllipseHairlineEdge->ref();
michael@0 319 return gEllipseHairlineEdge;
michael@0 320 } else {
michael@0 321 gEllipseFillEdge->ref();
michael@0 322 return gEllipseFillEdge;
michael@0 323 }
michael@0 324 }
michael@0 325
michael@0 326 virtual void getConstantColorComponents(GrColor* color,
michael@0 327 uint32_t* validFlags) const SK_OVERRIDE {
michael@0 328 *validFlags = 0;
michael@0 329 }
michael@0 330
michael@0 331 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
michael@0 332 return GrTBackendEffectFactory<DIEllipseEdgeEffect>::getInstance();
michael@0 333 }
michael@0 334
michael@0 335 virtual ~DIEllipseEdgeEffect() {}
michael@0 336
michael@0 337 static const char* Name() { return "DIEllipseEdge"; }
michael@0 338
michael@0 339 inline Mode getMode() const { return fMode; }
michael@0 340
michael@0 341 class GLEffect : public GrGLVertexEffect {
michael@0 342 public:
michael@0 343 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
michael@0 344 : INHERITED (factory) {}
michael@0 345
michael@0 346 virtual void emitCode(GrGLFullShaderBuilder* builder,
michael@0 347 const GrDrawEffect& drawEffect,
michael@0 348 EffectKey key,
michael@0 349 const char* outputColor,
michael@0 350 const char* inputColor,
michael@0 351 const TransformedCoordsArray&,
michael@0 352 const TextureSamplerArray& samplers) SK_OVERRIDE {
michael@0 353 const DIEllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<DIEllipseEdgeEffect>();
michael@0 354
michael@0 355 SkAssertResult(builder->enableFeature(
michael@0 356 GrGLShaderBuilder::kStandardDerivatives_GLSLFeature));
michael@0 357
michael@0 358 const char *vsOffsetName0, *fsOffsetName0;
michael@0 359 builder->addVarying(kVec2f_GrSLType, "EllipseOffsets0",
michael@0 360 &vsOffsetName0, &fsOffsetName0);
michael@0 361 const SkString* attr0Name =
michael@0 362 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]);
michael@0 363 builder->vsCodeAppendf("\t%s = %s;\n", vsOffsetName0, attr0Name->c_str());
michael@0 364 const char *vsOffsetName1, *fsOffsetName1;
michael@0 365 builder->addVarying(kVec2f_GrSLType, "EllipseOffsets1",
michael@0 366 &vsOffsetName1, &fsOffsetName1);
michael@0 367 const SkString* attr1Name =
michael@0 368 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[1]);
michael@0 369 builder->vsCodeAppendf("\t%s = %s;\n", vsOffsetName1, attr1Name->c_str());
michael@0 370
michael@0 371 // for outer curve
michael@0 372 builder->fsCodeAppendf("\tvec2 scaledOffset = %s.xy;\n", fsOffsetName0);
michael@0 373 builder->fsCodeAppend("\tfloat test = dot(scaledOffset, scaledOffset) - 1.0;\n");
michael@0 374 builder->fsCodeAppendf("\tvec2 duvdx = dFdx(%s);\n", fsOffsetName0);
michael@0 375 builder->fsCodeAppendf("\tvec2 duvdy = dFdy(%s);\n", fsOffsetName0);
michael@0 376 builder->fsCodeAppendf("\tvec2 grad = vec2(2.0*%s.x*duvdx.x + 2.0*%s.y*duvdx.y,\n"
michael@0 377 "\t 2.0*%s.x*duvdy.x + 2.0*%s.y*duvdy.y);\n",
michael@0 378 fsOffsetName0, fsOffsetName0, fsOffsetName0, fsOffsetName0);
michael@0 379
michael@0 380 builder->fsCodeAppend("\tfloat grad_dot = dot(grad, grad);\n");
michael@0 381 // we need to clamp the length^2 of the gradiant vector to a non-zero value, because
michael@0 382 // on the Nexus 4 the undefined result of inversesqrt(0) drops out an entire tile
michael@0 383 // TODO: restrict this to Adreno-only
michael@0 384 builder->fsCodeAppend("\tgrad_dot = max(grad_dot, 1.0e-4);\n");
michael@0 385 builder->fsCodeAppend("\tfloat invlen = inversesqrt(grad_dot);\n");
michael@0 386 if (kHairline == ellipseEffect.getMode()) {
michael@0 387 // can probably do this with one step
michael@0 388 builder->fsCodeAppend("\tfloat edgeAlpha = clamp(1.0-test*invlen, 0.0, 1.0);\n");
michael@0 389 builder->fsCodeAppend("\tedgeAlpha *= clamp(1.0+test*invlen, 0.0, 1.0);\n");
michael@0 390 } else {
michael@0 391 builder->fsCodeAppend("\tfloat edgeAlpha = clamp(0.5-test*invlen, 0.0, 1.0);\n");
michael@0 392 }
michael@0 393
michael@0 394 // for inner curve
michael@0 395 if (kStroke == ellipseEffect.getMode()) {
michael@0 396 builder->fsCodeAppendf("\tscaledOffset = %s.xy;\n", fsOffsetName1);
michael@0 397 builder->fsCodeAppend("\ttest = dot(scaledOffset, scaledOffset) - 1.0;\n");
michael@0 398 builder->fsCodeAppendf("\tduvdx = dFdx(%s);\n", fsOffsetName1);
michael@0 399 builder->fsCodeAppendf("\tduvdy = dFdy(%s);\n", fsOffsetName1);
michael@0 400 builder->fsCodeAppendf("\tgrad = vec2(2.0*%s.x*duvdx.x + 2.0*%s.y*duvdx.y,\n"
michael@0 401 "\t 2.0*%s.x*duvdy.x + 2.0*%s.y*duvdy.y);\n",
michael@0 402 fsOffsetName1, fsOffsetName1, fsOffsetName1, fsOffsetName1);
michael@0 403 builder->fsCodeAppend("\tinvlen = inversesqrt(dot(grad, grad));\n");
michael@0 404 builder->fsCodeAppend("\tedgeAlpha *= clamp(0.5+test*invlen, 0.0, 1.0);\n");
michael@0 405 }
michael@0 406
michael@0 407 builder->fsCodeAppendf("\t%s = %s;\n", outputColor,
michael@0 408 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("edgeAlpha")).c_str());
michael@0 409 }
michael@0 410
michael@0 411 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
michael@0 412 const DIEllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<DIEllipseEdgeEffect>();
michael@0 413
michael@0 414 return ellipseEffect.getMode();
michael@0 415 }
michael@0 416
michael@0 417 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {
michael@0 418 }
michael@0 419
michael@0 420 private:
michael@0 421 typedef GrGLVertexEffect INHERITED;
michael@0 422 };
michael@0 423
michael@0 424 private:
michael@0 425 DIEllipseEdgeEffect(Mode mode) : GrVertexEffect() {
michael@0 426 this->addVertexAttrib(kVec2f_GrSLType);
michael@0 427 this->addVertexAttrib(kVec2f_GrSLType);
michael@0 428 fMode = mode;
michael@0 429 }
michael@0 430
michael@0 431 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
michael@0 432 const DIEllipseEdgeEffect& eee = CastEffect<DIEllipseEdgeEffect>(other);
michael@0 433 return eee.fMode == fMode;
michael@0 434 }
michael@0 435
michael@0 436 Mode fMode;
michael@0 437
michael@0 438 GR_DECLARE_EFFECT_TEST;
michael@0 439
michael@0 440 typedef GrVertexEffect INHERITED;
michael@0 441 };
michael@0 442
michael@0 443 GR_DEFINE_EFFECT_TEST(DIEllipseEdgeEffect);
michael@0 444
michael@0 445 GrEffectRef* DIEllipseEdgeEffect::TestCreate(SkRandom* random,
michael@0 446 GrContext* context,
michael@0 447 const GrDrawTargetCaps&,
michael@0 448 GrTexture* textures[]) {
michael@0 449 return DIEllipseEdgeEffect::Create((Mode)(random->nextRangeU(0,2)));
michael@0 450 }
michael@0 451
michael@0 452 ///////////////////////////////////////////////////////////////////////////////
michael@0 453
michael@0 454 void GrOvalRenderer::reset() {
michael@0 455 SkSafeSetNull(fRRectIndexBuffer);
michael@0 456 }
michael@0 457
michael@0 458 bool GrOvalRenderer::drawOval(GrDrawTarget* target, const GrContext* context, bool useAA,
michael@0 459 const SkRect& oval, const SkStrokeRec& stroke)
michael@0 460 {
michael@0 461 bool useCoverageAA = useAA &&
michael@0 462 !target->getDrawState().getRenderTarget()->isMultisampled() &&
michael@0 463 !target->shouldDisableCoverageAAForBlend();
michael@0 464
michael@0 465 if (!useCoverageAA) {
michael@0 466 return false;
michael@0 467 }
michael@0 468
michael@0 469 const SkMatrix& vm = context->getMatrix();
michael@0 470
michael@0 471 // we can draw circles
michael@0 472 if (SkScalarNearlyEqual(oval.width(), oval.height())
michael@0 473 && circle_stays_circle(vm)) {
michael@0 474 this->drawCircle(target, useCoverageAA, oval, stroke);
michael@0 475 // if we have shader derivative support, render as device-independent
michael@0 476 } else if (target->caps()->shaderDerivativeSupport()) {
michael@0 477 return this->drawDIEllipse(target, useCoverageAA, oval, stroke);
michael@0 478 // otherwise axis-aligned ellipses only
michael@0 479 } else if (vm.rectStaysRect()) {
michael@0 480 return this->drawEllipse(target, useCoverageAA, oval, stroke);
michael@0 481 } else {
michael@0 482 return false;
michael@0 483 }
michael@0 484
michael@0 485 return true;
michael@0 486 }
michael@0 487
michael@0 488 ///////////////////////////////////////////////////////////////////////////////
michael@0 489
michael@0 490 // position + edge
michael@0 491 extern const GrVertexAttrib gCircleVertexAttribs[] = {
michael@0 492 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
michael@0 493 {kVec4f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding}
michael@0 494 };
michael@0 495
michael@0 496 void GrOvalRenderer::drawCircle(GrDrawTarget* target,
michael@0 497 bool useCoverageAA,
michael@0 498 const SkRect& circle,
michael@0 499 const SkStrokeRec& stroke)
michael@0 500 {
michael@0 501 GrDrawState* drawState = target->drawState();
michael@0 502
michael@0 503 const SkMatrix& vm = drawState->getViewMatrix();
michael@0 504 GrPoint center = GrPoint::Make(circle.centerX(), circle.centerY());
michael@0 505 vm.mapPoints(&center, 1);
michael@0 506 SkScalar radius = vm.mapRadius(SkScalarHalf(circle.width()));
michael@0 507 SkScalar strokeWidth = vm.mapRadius(stroke.getWidth());
michael@0 508
michael@0 509 GrDrawState::AutoViewMatrixRestore avmr;
michael@0 510 if (!avmr.setIdentity(drawState)) {
michael@0 511 return;
michael@0 512 }
michael@0 513
michael@0 514 drawState->setVertexAttribs<gCircleVertexAttribs>(SK_ARRAY_COUNT(gCircleVertexAttribs));
michael@0 515 SkASSERT(sizeof(CircleVertex) == drawState->getVertexSize());
michael@0 516
michael@0 517 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
michael@0 518 if (!geo.succeeded()) {
michael@0 519 GrPrintf("Failed to get space for vertices!\n");
michael@0 520 return;
michael@0 521 }
michael@0 522
michael@0 523 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
michael@0 524
michael@0 525 SkStrokeRec::Style style = stroke.getStyle();
michael@0 526 bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style);
michael@0 527
michael@0 528 SkScalar innerRadius = 0.0f;
michael@0 529 SkScalar outerRadius = radius;
michael@0 530 SkScalar halfWidth = 0;
michael@0 531 if (style != SkStrokeRec::kFill_Style) {
michael@0 532 if (SkScalarNearlyZero(strokeWidth)) {
michael@0 533 halfWidth = SK_ScalarHalf;
michael@0 534 } else {
michael@0 535 halfWidth = SkScalarHalf(strokeWidth);
michael@0 536 }
michael@0 537
michael@0 538 outerRadius += halfWidth;
michael@0 539 if (isStroked) {
michael@0 540 innerRadius = radius - halfWidth;
michael@0 541 }
michael@0 542 }
michael@0 543
michael@0 544 GrEffectRef* effect = CircleEdgeEffect::Create(isStroked && innerRadius > 0);
michael@0 545 static const int kCircleEdgeAttrIndex = 1;
michael@0 546 drawState->addCoverageEffect(effect, kCircleEdgeAttrIndex)->unref();
michael@0 547
michael@0 548 // The radii are outset for two reasons. First, it allows the shader to simply perform
michael@0 549 // clamp(distance-to-center - radius, 0, 1). Second, the outer radius is used to compute the
michael@0 550 // verts of the bounding box that is rendered and the outset ensures the box will cover all
michael@0 551 // pixels partially covered by the circle.
michael@0 552 outerRadius += SK_ScalarHalf;
michael@0 553 innerRadius -= SK_ScalarHalf;
michael@0 554
michael@0 555 SkRect bounds = SkRect::MakeLTRB(
michael@0 556 center.fX - outerRadius,
michael@0 557 center.fY - outerRadius,
michael@0 558 center.fX + outerRadius,
michael@0 559 center.fY + outerRadius
michael@0 560 );
michael@0 561
michael@0 562 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
michael@0 563 verts[0].fOffset = SkPoint::Make(-outerRadius, -outerRadius);
michael@0 564 verts[0].fOuterRadius = outerRadius;
michael@0 565 verts[0].fInnerRadius = innerRadius;
michael@0 566
michael@0 567 verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
michael@0 568 verts[1].fOffset = SkPoint::Make(outerRadius, -outerRadius);
michael@0 569 verts[1].fOuterRadius = outerRadius;
michael@0 570 verts[1].fInnerRadius = innerRadius;
michael@0 571
michael@0 572 verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
michael@0 573 verts[2].fOffset = SkPoint::Make(-outerRadius, outerRadius);
michael@0 574 verts[2].fOuterRadius = outerRadius;
michael@0 575 verts[2].fInnerRadius = innerRadius;
michael@0 576
michael@0 577 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
michael@0 578 verts[3].fOffset = SkPoint::Make(outerRadius, outerRadius);
michael@0 579 verts[3].fOuterRadius = outerRadius;
michael@0 580 verts[3].fInnerRadius = innerRadius;
michael@0 581
michael@0 582 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds);
michael@0 583 }
michael@0 584
michael@0 585 ///////////////////////////////////////////////////////////////////////////////
michael@0 586
michael@0 587 // position + offset + 1/radii
michael@0 588 extern const GrVertexAttrib gEllipseVertexAttribs[] = {
michael@0 589 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
michael@0 590 {kVec2f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding},
michael@0 591 {kVec4f_GrVertexAttribType, 2*sizeof(GrPoint), kEffect_GrVertexAttribBinding}
michael@0 592 };
michael@0 593
michael@0 594 // position + offsets
michael@0 595 extern const GrVertexAttrib gDIEllipseVertexAttribs[] = {
michael@0 596 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
michael@0 597 {kVec2f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding},
michael@0 598 {kVec2f_GrVertexAttribType, 2*sizeof(GrPoint), kEffect_GrVertexAttribBinding},
michael@0 599 };
michael@0 600
michael@0 601 bool GrOvalRenderer::drawEllipse(GrDrawTarget* target,
michael@0 602 bool useCoverageAA,
michael@0 603 const SkRect& ellipse,
michael@0 604 const SkStrokeRec& stroke)
michael@0 605 {
michael@0 606 GrDrawState* drawState = target->drawState();
michael@0 607 #ifdef SK_DEBUG
michael@0 608 {
michael@0 609 // we should have checked for this previously
michael@0 610 bool isAxisAlignedEllipse = drawState->getViewMatrix().rectStaysRect();
michael@0 611 SkASSERT(useCoverageAA && isAxisAlignedEllipse);
michael@0 612 }
michael@0 613 #endif
michael@0 614
michael@0 615 // do any matrix crunching before we reset the draw state for device coords
michael@0 616 const SkMatrix& vm = drawState->getViewMatrix();
michael@0 617 GrPoint center = GrPoint::Make(ellipse.centerX(), ellipse.centerY());
michael@0 618 vm.mapPoints(&center, 1);
michael@0 619 SkScalar ellipseXRadius = SkScalarHalf(ellipse.width());
michael@0 620 SkScalar ellipseYRadius = SkScalarHalf(ellipse.height());
michael@0 621 SkScalar xRadius = SkScalarAbs(vm[SkMatrix::kMScaleX]*ellipseXRadius +
michael@0 622 vm[SkMatrix::kMSkewY]*ellipseYRadius);
michael@0 623 SkScalar yRadius = SkScalarAbs(vm[SkMatrix::kMSkewX]*ellipseXRadius +
michael@0 624 vm[SkMatrix::kMScaleY]*ellipseYRadius);
michael@0 625
michael@0 626 // do (potentially) anisotropic mapping of stroke
michael@0 627 SkVector scaledStroke;
michael@0 628 SkScalar strokeWidth = stroke.getWidth();
michael@0 629 scaledStroke.fX = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMScaleX] + vm[SkMatrix::kMSkewY]));
michael@0 630 scaledStroke.fY = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMSkewX] + vm[SkMatrix::kMScaleY]));
michael@0 631
michael@0 632 SkStrokeRec::Style style = stroke.getStyle();
michael@0 633 bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style);
michael@0 634
michael@0 635 SkScalar innerXRadius = 0;
michael@0 636 SkScalar innerYRadius = 0;
michael@0 637 if (SkStrokeRec::kFill_Style != style) {
michael@0 638 if (SkScalarNearlyZero(scaledStroke.length())) {
michael@0 639 scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf);
michael@0 640 } else {
michael@0 641 scaledStroke.scale(SK_ScalarHalf);
michael@0 642 }
michael@0 643
michael@0 644 // we only handle thick strokes for near-circular ellipses
michael@0 645 if (scaledStroke.length() > SK_ScalarHalf &&
michael@0 646 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
michael@0 647 return false;
michael@0 648 }
michael@0 649
michael@0 650 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
michael@0 651 if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius ||
michael@0 652 scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) {
michael@0 653 return false;
michael@0 654 }
michael@0 655
michael@0 656 // this is legit only if scale & translation (which should be the case at the moment)
michael@0 657 if (isStroked) {
michael@0 658 innerXRadius = xRadius - scaledStroke.fX;
michael@0 659 innerYRadius = yRadius - scaledStroke.fY;
michael@0 660 }
michael@0 661
michael@0 662 xRadius += scaledStroke.fX;
michael@0 663 yRadius += scaledStroke.fY;
michael@0 664 }
michael@0 665
michael@0 666 GrDrawState::AutoViewMatrixRestore avmr;
michael@0 667 if (!avmr.setIdentity(drawState)) {
michael@0 668 return false;
michael@0 669 }
michael@0 670
michael@0 671 drawState->setVertexAttribs<gEllipseVertexAttribs>(SK_ARRAY_COUNT(gEllipseVertexAttribs));
michael@0 672 SkASSERT(sizeof(EllipseVertex) == drawState->getVertexSize());
michael@0 673
michael@0 674 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
michael@0 675 if (!geo.succeeded()) {
michael@0 676 GrPrintf("Failed to get space for vertices!\n");
michael@0 677 return false;
michael@0 678 }
michael@0 679
michael@0 680 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices());
michael@0 681
michael@0 682 GrEffectRef* effect = EllipseEdgeEffect::Create(isStroked &&
michael@0 683 innerXRadius > 0 && innerYRadius > 0);
michael@0 684
michael@0 685 static const int kEllipseCenterAttrIndex = 1;
michael@0 686 static const int kEllipseEdgeAttrIndex = 2;
michael@0 687 drawState->addCoverageEffect(effect, kEllipseCenterAttrIndex, kEllipseEdgeAttrIndex)->unref();
michael@0 688
michael@0 689 // Compute the reciprocals of the radii here to save time in the shader
michael@0 690 SkScalar xRadRecip = SkScalarInvert(xRadius);
michael@0 691 SkScalar yRadRecip = SkScalarInvert(yRadius);
michael@0 692 SkScalar xInnerRadRecip = SkScalarInvert(innerXRadius);
michael@0 693 SkScalar yInnerRadRecip = SkScalarInvert(innerYRadius);
michael@0 694
michael@0 695 // We've extended the outer x radius out half a pixel to antialias.
michael@0 696 // This will also expand the rect so all the pixels will be captured.
michael@0 697 // TODO: Consider if we should use sqrt(2)/2 instead
michael@0 698 xRadius += SK_ScalarHalf;
michael@0 699 yRadius += SK_ScalarHalf;
michael@0 700
michael@0 701 SkRect bounds = SkRect::MakeLTRB(
michael@0 702 center.fX - xRadius,
michael@0 703 center.fY - yRadius,
michael@0 704 center.fX + xRadius,
michael@0 705 center.fY + yRadius
michael@0 706 );
michael@0 707
michael@0 708 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
michael@0 709 verts[0].fOffset = SkPoint::Make(-xRadius, -yRadius);
michael@0 710 verts[0].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
michael@0 711 verts[0].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
michael@0 712
michael@0 713 verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
michael@0 714 verts[1].fOffset = SkPoint::Make(xRadius, -yRadius);
michael@0 715 verts[1].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
michael@0 716 verts[1].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
michael@0 717
michael@0 718 verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
michael@0 719 verts[2].fOffset = SkPoint::Make(-xRadius, yRadius);
michael@0 720 verts[2].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
michael@0 721 verts[2].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
michael@0 722
michael@0 723 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
michael@0 724 verts[3].fOffset = SkPoint::Make(xRadius, yRadius);
michael@0 725 verts[3].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
michael@0 726 verts[3].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
michael@0 727
michael@0 728 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds);
michael@0 729
michael@0 730 return true;
michael@0 731 }
michael@0 732
michael@0 733 bool GrOvalRenderer::drawDIEllipse(GrDrawTarget* target,
michael@0 734 bool useCoverageAA,
michael@0 735 const SkRect& ellipse,
michael@0 736 const SkStrokeRec& stroke)
michael@0 737 {
michael@0 738 GrDrawState* drawState = target->drawState();
michael@0 739 const SkMatrix& vm = drawState->getViewMatrix();
michael@0 740
michael@0 741 GrPoint center = GrPoint::Make(ellipse.centerX(), ellipse.centerY());
michael@0 742 SkScalar xRadius = SkScalarHalf(ellipse.width());
michael@0 743 SkScalar yRadius = SkScalarHalf(ellipse.height());
michael@0 744
michael@0 745 SkStrokeRec::Style style = stroke.getStyle();
michael@0 746 DIEllipseEdgeEffect::Mode mode = (SkStrokeRec::kStroke_Style == style) ?
michael@0 747 DIEllipseEdgeEffect::kStroke :
michael@0 748 (SkStrokeRec::kHairline_Style == style) ?
michael@0 749 DIEllipseEdgeEffect::kHairline : DIEllipseEdgeEffect::kFill;
michael@0 750
michael@0 751 SkScalar innerXRadius = 0;
michael@0 752 SkScalar innerYRadius = 0;
michael@0 753 if (SkStrokeRec::kFill_Style != style && SkStrokeRec::kHairline_Style != style) {
michael@0 754 SkScalar strokeWidth = stroke.getWidth();
michael@0 755
michael@0 756 if (SkScalarNearlyZero(strokeWidth)) {
michael@0 757 strokeWidth = SK_ScalarHalf;
michael@0 758 } else {
michael@0 759 strokeWidth *= SK_ScalarHalf;
michael@0 760 }
michael@0 761
michael@0 762 // we only handle thick strokes for near-circular ellipses
michael@0 763 if (strokeWidth > SK_ScalarHalf &&
michael@0 764 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
michael@0 765 return false;
michael@0 766 }
michael@0 767
michael@0 768 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
michael@0 769 if (strokeWidth*(yRadius*yRadius) < (strokeWidth*strokeWidth)*xRadius ||
michael@0 770 strokeWidth*(xRadius*xRadius) < (strokeWidth*strokeWidth)*yRadius) {
michael@0 771 return false;
michael@0 772 }
michael@0 773
michael@0 774 // set inner radius (if needed)
michael@0 775 if (SkStrokeRec::kStroke_Style == style) {
michael@0 776 innerXRadius = xRadius - strokeWidth;
michael@0 777 innerYRadius = yRadius - strokeWidth;
michael@0 778 }
michael@0 779
michael@0 780 xRadius += strokeWidth;
michael@0 781 yRadius += strokeWidth;
michael@0 782 }
michael@0 783 if (DIEllipseEdgeEffect::kStroke == mode) {
michael@0 784 mode = (innerXRadius > 0 && innerYRadius > 0) ? DIEllipseEdgeEffect::kStroke :
michael@0 785 DIEllipseEdgeEffect::kFill;
michael@0 786 }
michael@0 787 SkScalar innerRatioX = SkScalarDiv(xRadius, innerXRadius);
michael@0 788 SkScalar innerRatioY = SkScalarDiv(yRadius, innerYRadius);
michael@0 789
michael@0 790 drawState->setVertexAttribs<gDIEllipseVertexAttribs>(SK_ARRAY_COUNT(gDIEllipseVertexAttribs));
michael@0 791 SkASSERT(sizeof(DIEllipseVertex) == drawState->getVertexSize());
michael@0 792
michael@0 793 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
michael@0 794 if (!geo.succeeded()) {
michael@0 795 GrPrintf("Failed to get space for vertices!\n");
michael@0 796 return false;
michael@0 797 }
michael@0 798
michael@0 799 DIEllipseVertex* verts = reinterpret_cast<DIEllipseVertex*>(geo.vertices());
michael@0 800
michael@0 801 GrEffectRef* effect = DIEllipseEdgeEffect::Create(mode);
michael@0 802
michael@0 803 static const int kEllipseOuterOffsetAttrIndex = 1;
michael@0 804 static const int kEllipseInnerOffsetAttrIndex = 2;
michael@0 805 drawState->addCoverageEffect(effect, kEllipseOuterOffsetAttrIndex,
michael@0 806 kEllipseInnerOffsetAttrIndex)->unref();
michael@0 807
michael@0 808 // This expands the outer rect so that after CTM we end up with a half-pixel border
michael@0 809 SkScalar a = vm[SkMatrix::kMScaleX];
michael@0 810 SkScalar b = vm[SkMatrix::kMSkewX];
michael@0 811 SkScalar c = vm[SkMatrix::kMSkewY];
michael@0 812 SkScalar d = vm[SkMatrix::kMScaleY];
michael@0 813 SkScalar geoDx = SkScalarDiv(SK_ScalarHalf, SkScalarSqrt(a*a + c*c));
michael@0 814 SkScalar geoDy = SkScalarDiv(SK_ScalarHalf, SkScalarSqrt(b*b + d*d));
michael@0 815 // This adjusts the "radius" to include the half-pixel border
michael@0 816 SkScalar offsetDx = SkScalarDiv(geoDx, xRadius);
michael@0 817 SkScalar offsetDy = SkScalarDiv(geoDy, yRadius);
michael@0 818
michael@0 819 SkRect bounds = SkRect::MakeLTRB(
michael@0 820 center.fX - xRadius - geoDx,
michael@0 821 center.fY - yRadius - geoDy,
michael@0 822 center.fX + xRadius + geoDx,
michael@0 823 center.fY + yRadius + geoDy
michael@0 824 );
michael@0 825
michael@0 826 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
michael@0 827 verts[0].fOuterOffset = SkPoint::Make(-1.0f - offsetDx, -1.0f - offsetDy);
michael@0 828 verts[0].fInnerOffset = SkPoint::Make(-innerRatioX - offsetDx, -innerRatioY - offsetDy);
michael@0 829
michael@0 830 verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
michael@0 831 verts[1].fOuterOffset = SkPoint::Make(1.0f + offsetDx, -1.0f - offsetDy);
michael@0 832 verts[1].fInnerOffset = SkPoint::Make(innerRatioX + offsetDx, -innerRatioY - offsetDy);
michael@0 833
michael@0 834 verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
michael@0 835 verts[2].fOuterOffset = SkPoint::Make(-1.0f - offsetDx, 1.0f + offsetDy);
michael@0 836 verts[2].fInnerOffset = SkPoint::Make(-innerRatioX - offsetDx, innerRatioY + offsetDy);
michael@0 837
michael@0 838 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
michael@0 839 verts[3].fOuterOffset = SkPoint::Make(1.0f + offsetDx, 1.0f + offsetDy);
michael@0 840 verts[3].fInnerOffset = SkPoint::Make(innerRatioX + offsetDx, innerRatioY + offsetDy);
michael@0 841
michael@0 842 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds);
michael@0 843
michael@0 844 return true;
michael@0 845 }
michael@0 846
michael@0 847 ///////////////////////////////////////////////////////////////////////////////
michael@0 848
michael@0 849 static const uint16_t gRRectIndices[] = {
michael@0 850 // corners
michael@0 851 0, 1, 5, 0, 5, 4,
michael@0 852 2, 3, 7, 2, 7, 6,
michael@0 853 8, 9, 13, 8, 13, 12,
michael@0 854 10, 11, 15, 10, 15, 14,
michael@0 855
michael@0 856 // edges
michael@0 857 1, 2, 6, 1, 6, 5,
michael@0 858 4, 5, 9, 4, 9, 8,
michael@0 859 6, 7, 11, 6, 11, 10,
michael@0 860 9, 10, 14, 9, 14, 13,
michael@0 861
michael@0 862 // center
michael@0 863 // we place this at the end so that we can ignore these indices when rendering stroke-only
michael@0 864 5, 6, 10, 5, 10, 9
michael@0 865 };
michael@0 866
michael@0 867
michael@0 868 GrIndexBuffer* GrOvalRenderer::rRectIndexBuffer(GrGpu* gpu) {
michael@0 869 if (NULL == fRRectIndexBuffer) {
michael@0 870 fRRectIndexBuffer =
michael@0 871 gpu->createIndexBuffer(sizeof(gRRectIndices), false);
michael@0 872 if (NULL != fRRectIndexBuffer) {
michael@0 873 #ifdef SK_DEBUG
michael@0 874 bool updated =
michael@0 875 #endif
michael@0 876 fRRectIndexBuffer->updateData(gRRectIndices,
michael@0 877 sizeof(gRRectIndices));
michael@0 878 GR_DEBUGASSERT(updated);
michael@0 879 }
michael@0 880 }
michael@0 881 return fRRectIndexBuffer;
michael@0 882 }
michael@0 883
michael@0 884 bool GrOvalRenderer::drawSimpleRRect(GrDrawTarget* target, GrContext* context, bool useAA,
michael@0 885 const SkRRect& rrect, const SkStrokeRec& stroke)
michael@0 886 {
michael@0 887 bool useCoverageAA = useAA &&
michael@0 888 !target->getDrawState().getRenderTarget()->isMultisampled() &&
michael@0 889 !target->shouldDisableCoverageAAForBlend();
michael@0 890
michael@0 891 // only anti-aliased rrects for now
michael@0 892 if (!useCoverageAA) {
michael@0 893 return false;
michael@0 894 }
michael@0 895
michael@0 896 const SkMatrix& vm = context->getMatrix();
michael@0 897 #ifdef SK_DEBUG
michael@0 898 {
michael@0 899 // we should have checked for this previously
michael@0 900 SkASSERT(useCoverageAA && vm.rectStaysRect() && rrect.isSimple());
michael@0 901 }
michael@0 902 #endif
michael@0 903
michael@0 904 // do any matrix crunching before we reset the draw state for device coords
michael@0 905 const SkRect& rrectBounds = rrect.getBounds();
michael@0 906 SkRect bounds;
michael@0 907 vm.mapRect(&bounds, rrectBounds);
michael@0 908
michael@0 909 SkVector radii = rrect.getSimpleRadii();
michael@0 910 SkScalar xRadius = SkScalarAbs(vm[SkMatrix::kMScaleX]*radii.fX +
michael@0 911 vm[SkMatrix::kMSkewY]*radii.fY);
michael@0 912 SkScalar yRadius = SkScalarAbs(vm[SkMatrix::kMSkewX]*radii.fX +
michael@0 913 vm[SkMatrix::kMScaleY]*radii.fY);
michael@0 914
michael@0 915 // if hairline stroke is greater than radius, we don't handle that right now
michael@0 916 SkStrokeRec::Style style = stroke.getStyle();
michael@0 917 if (SkStrokeRec::kHairline_Style == style &&
michael@0 918 (SK_ScalarHalf > xRadius || SK_ScalarHalf > yRadius)) {
michael@0 919 return false;
michael@0 920 }
michael@0 921
michael@0 922 // do (potentially) anisotropic mapping of stroke
michael@0 923 SkVector scaledStroke;
michael@0 924 SkScalar strokeWidth = stroke.getWidth();
michael@0 925 scaledStroke.fX = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMScaleX] + vm[SkMatrix::kMSkewY]));
michael@0 926 scaledStroke.fY = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMSkewX] + vm[SkMatrix::kMScaleY]));
michael@0 927
michael@0 928 // if half of strokewidth is greater than radius, we don't handle that right now
michael@0 929 if (SK_ScalarHalf*scaledStroke.fX > xRadius || SK_ScalarHalf*scaledStroke.fY > yRadius) {
michael@0 930 return false;
michael@0 931 }
michael@0 932
michael@0 933 // reset to device coordinates
michael@0 934 GrDrawState* drawState = target->drawState();
michael@0 935 GrDrawState::AutoViewMatrixRestore avmr;
michael@0 936 if (!avmr.setIdentity(drawState)) {
michael@0 937 return false;
michael@0 938 }
michael@0 939
michael@0 940 bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style);
michael@0 941
michael@0 942 GrIndexBuffer* indexBuffer = this->rRectIndexBuffer(context->getGpu());
michael@0 943 if (NULL == indexBuffer) {
michael@0 944 GrPrintf("Failed to create index buffer!\n");
michael@0 945 return false;
michael@0 946 }
michael@0 947
michael@0 948 // if the corners are circles, use the circle renderer
michael@0 949 if ((!isStroked || scaledStroke.fX == scaledStroke.fY) && xRadius == yRadius) {
michael@0 950 drawState->setVertexAttribs<gCircleVertexAttribs>(SK_ARRAY_COUNT(gCircleVertexAttribs));
michael@0 951 SkASSERT(sizeof(CircleVertex) == drawState->getVertexSize());
michael@0 952
michael@0 953 GrDrawTarget::AutoReleaseGeometry geo(target, 16, 0);
michael@0 954 if (!geo.succeeded()) {
michael@0 955 GrPrintf("Failed to get space for vertices!\n");
michael@0 956 return false;
michael@0 957 }
michael@0 958 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
michael@0 959
michael@0 960 SkScalar innerRadius = 0.0f;
michael@0 961 SkScalar outerRadius = xRadius;
michael@0 962 SkScalar halfWidth = 0;
michael@0 963 if (style != SkStrokeRec::kFill_Style) {
michael@0 964 if (SkScalarNearlyZero(scaledStroke.fX)) {
michael@0 965 halfWidth = SK_ScalarHalf;
michael@0 966 } else {
michael@0 967 halfWidth = SkScalarHalf(scaledStroke.fX);
michael@0 968 }
michael@0 969
michael@0 970 if (isStroked) {
michael@0 971 innerRadius = xRadius - halfWidth;
michael@0 972 }
michael@0 973 outerRadius += halfWidth;
michael@0 974 bounds.outset(halfWidth, halfWidth);
michael@0 975 }
michael@0 976
michael@0 977 isStroked = (isStroked && innerRadius >= 0);
michael@0 978
michael@0 979 GrEffectRef* effect = CircleEdgeEffect::Create(isStroked);
michael@0 980 static const int kCircleEdgeAttrIndex = 1;
michael@0 981 drawState->addCoverageEffect(effect, kCircleEdgeAttrIndex)->unref();
michael@0 982
michael@0 983 // The radii are outset for two reasons. First, it allows the shader to simply perform
michael@0 984 // clamp(distance-to-center - radius, 0, 1). Second, the outer radius is used to compute the
michael@0 985 // verts of the bounding box that is rendered and the outset ensures the box will cover all
michael@0 986 // pixels partially covered by the circle.
michael@0 987 outerRadius += SK_ScalarHalf;
michael@0 988 innerRadius -= SK_ScalarHalf;
michael@0 989
michael@0 990 // Expand the rect so all the pixels will be captured.
michael@0 991 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
michael@0 992
michael@0 993 SkScalar yCoords[4] = {
michael@0 994 bounds.fTop,
michael@0 995 bounds.fTop + outerRadius,
michael@0 996 bounds.fBottom - outerRadius,
michael@0 997 bounds.fBottom
michael@0 998 };
michael@0 999 SkScalar yOuterRadii[4] = {
michael@0 1000 -outerRadius,
michael@0 1001 0,
michael@0 1002 0,
michael@0 1003 outerRadius
michael@0 1004 };
michael@0 1005 for (int i = 0; i < 4; ++i) {
michael@0 1006 verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]);
michael@0 1007 verts->fOffset = SkPoint::Make(-outerRadius, yOuterRadii[i]);
michael@0 1008 verts->fOuterRadius = outerRadius;
michael@0 1009 verts->fInnerRadius = innerRadius;
michael@0 1010 verts++;
michael@0 1011
michael@0 1012 verts->fPos = SkPoint::Make(bounds.fLeft + outerRadius, yCoords[i]);
michael@0 1013 verts->fOffset = SkPoint::Make(0, yOuterRadii[i]);
michael@0 1014 verts->fOuterRadius = outerRadius;
michael@0 1015 verts->fInnerRadius = innerRadius;
michael@0 1016 verts++;
michael@0 1017
michael@0 1018 verts->fPos = SkPoint::Make(bounds.fRight - outerRadius, yCoords[i]);
michael@0 1019 verts->fOffset = SkPoint::Make(0, yOuterRadii[i]);
michael@0 1020 verts->fOuterRadius = outerRadius;
michael@0 1021 verts->fInnerRadius = innerRadius;
michael@0 1022 verts++;
michael@0 1023
michael@0 1024 verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]);
michael@0 1025 verts->fOffset = SkPoint::Make(outerRadius, yOuterRadii[i]);
michael@0 1026 verts->fOuterRadius = outerRadius;
michael@0 1027 verts->fInnerRadius = innerRadius;
michael@0 1028 verts++;
michael@0 1029 }
michael@0 1030
michael@0 1031 // drop out the middle quad if we're stroked
michael@0 1032 int indexCnt = isStroked ? GR_ARRAY_COUNT(gRRectIndices)-6 : GR_ARRAY_COUNT(gRRectIndices);
michael@0 1033 target->setIndexSourceToBuffer(indexBuffer);
michael@0 1034 target->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, 16, indexCnt, &bounds);
michael@0 1035
michael@0 1036 // otherwise we use the ellipse renderer
michael@0 1037 } else {
michael@0 1038 drawState->setVertexAttribs<gEllipseVertexAttribs>(SK_ARRAY_COUNT(gEllipseVertexAttribs));
michael@0 1039 SkASSERT(sizeof(EllipseVertex) == drawState->getVertexSize());
michael@0 1040
michael@0 1041 SkScalar innerXRadius = 0.0f;
michael@0 1042 SkScalar innerYRadius = 0.0f;
michael@0 1043 if (SkStrokeRec::kFill_Style != style) {
michael@0 1044 if (SkScalarNearlyZero(scaledStroke.length())) {
michael@0 1045 scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf);
michael@0 1046 } else {
michael@0 1047 scaledStroke.scale(SK_ScalarHalf);
michael@0 1048 }
michael@0 1049
michael@0 1050 // we only handle thick strokes for near-circular ellipses
michael@0 1051 if (scaledStroke.length() > SK_ScalarHalf &&
michael@0 1052 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
michael@0 1053 return false;
michael@0 1054 }
michael@0 1055
michael@0 1056 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
michael@0 1057 if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius ||
michael@0 1058 scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) {
michael@0 1059 return false;
michael@0 1060 }
michael@0 1061
michael@0 1062 // this is legit only if scale & translation (which should be the case at the moment)
michael@0 1063 if (isStroked) {
michael@0 1064 innerXRadius = xRadius - scaledStroke.fX;
michael@0 1065 innerYRadius = yRadius - scaledStroke.fY;
michael@0 1066 }
michael@0 1067
michael@0 1068 xRadius += scaledStroke.fX;
michael@0 1069 yRadius += scaledStroke.fY;
michael@0 1070 bounds.outset(scaledStroke.fX, scaledStroke.fY);
michael@0 1071 }
michael@0 1072
michael@0 1073 isStroked = (isStroked && innerXRadius >= 0 && innerYRadius >= 0);
michael@0 1074
michael@0 1075 GrDrawTarget::AutoReleaseGeometry geo(target, 16, 0);
michael@0 1076 if (!geo.succeeded()) {
michael@0 1077 GrPrintf("Failed to get space for vertices!\n");
michael@0 1078 return false;
michael@0 1079 }
michael@0 1080 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices());
michael@0 1081
michael@0 1082 GrEffectRef* effect = EllipseEdgeEffect::Create(isStroked);
michael@0 1083 static const int kEllipseOffsetAttrIndex = 1;
michael@0 1084 static const int kEllipseRadiiAttrIndex = 2;
michael@0 1085 drawState->addCoverageEffect(effect,
michael@0 1086 kEllipseOffsetAttrIndex, kEllipseRadiiAttrIndex)->unref();
michael@0 1087
michael@0 1088 // Compute the reciprocals of the radii here to save time in the shader
michael@0 1089 SkScalar xRadRecip = SkScalarInvert(xRadius);
michael@0 1090 SkScalar yRadRecip = SkScalarInvert(yRadius);
michael@0 1091 SkScalar xInnerRadRecip = SkScalarInvert(innerXRadius);
michael@0 1092 SkScalar yInnerRadRecip = SkScalarInvert(innerYRadius);
michael@0 1093
michael@0 1094 // Extend the radii out half a pixel to antialias.
michael@0 1095 SkScalar xOuterRadius = xRadius + SK_ScalarHalf;
michael@0 1096 SkScalar yOuterRadius = yRadius + SK_ScalarHalf;
michael@0 1097
michael@0 1098 // Expand the rect so all the pixels will be captured.
michael@0 1099 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
michael@0 1100
michael@0 1101 SkScalar yCoords[4] = {
michael@0 1102 bounds.fTop,
michael@0 1103 bounds.fTop + yOuterRadius,
michael@0 1104 bounds.fBottom - yOuterRadius,
michael@0 1105 bounds.fBottom
michael@0 1106 };
michael@0 1107 SkScalar yOuterOffsets[4] = {
michael@0 1108 yOuterRadius,
michael@0 1109 SK_ScalarNearlyZero, // we're using inversesqrt() in the shader, so can't be exactly 0
michael@0 1110 SK_ScalarNearlyZero,
michael@0 1111 yOuterRadius
michael@0 1112 };
michael@0 1113
michael@0 1114 for (int i = 0; i < 4; ++i) {
michael@0 1115 verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]);
michael@0 1116 verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]);
michael@0 1117 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
michael@0 1118 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
michael@0 1119 verts++;
michael@0 1120
michael@0 1121 verts->fPos = SkPoint::Make(bounds.fLeft + xOuterRadius, yCoords[i]);
michael@0 1122 verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]);
michael@0 1123 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
michael@0 1124 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
michael@0 1125 verts++;
michael@0 1126
michael@0 1127 verts->fPos = SkPoint::Make(bounds.fRight - xOuterRadius, yCoords[i]);
michael@0 1128 verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]);
michael@0 1129 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
michael@0 1130 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
michael@0 1131 verts++;
michael@0 1132
michael@0 1133 verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]);
michael@0 1134 verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]);
michael@0 1135 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
michael@0 1136 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
michael@0 1137 verts++;
michael@0 1138 }
michael@0 1139
michael@0 1140 // drop out the middle quad if we're stroked
michael@0 1141 int indexCnt = isStroked ? GR_ARRAY_COUNT(gRRectIndices)-6 : GR_ARRAY_COUNT(gRRectIndices);
michael@0 1142 target->setIndexSourceToBuffer(indexBuffer);
michael@0 1143 target->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, 16, indexCnt, &bounds);
michael@0 1144 }
michael@0 1145
michael@0 1146 return true;
michael@0 1147 }

mercurial