blob: 465dd2b45e243e1d120d895314c0e074a695b6cd [file] [log] [blame]
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "GrOvalRenderer.h"
9
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000010#include "GrEffect.h"
11#include "gl/GrGLEffect.h"
12#include "gl/GrGLSL.h"
13#include "GrTBackendEffectFactory.h"
commit-bot@chromium.org81312832013-03-22 18:34:09 +000014
15#include "GrDrawState.h"
16#include "GrDrawTarget.h"
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +000017#include "GrGpu.h"
18
19#include "SkRRect.h"
commit-bot@chromium.org81312832013-03-22 18:34:09 +000020#include "SkStrokeRec.h"
21
22SK_DEFINE_INST_COUNT(GrOvalRenderer)
23
24namespace {
25
26struct CircleVertex {
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +000027 GrPoint fPos;
28 GrPoint fOffset;
commit-bot@chromium.org81312832013-03-22 18:34:09 +000029 SkScalar fOuterRadius;
30 SkScalar fInnerRadius;
31};
32
33struct EllipseVertex {
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +000034 GrPoint fPos;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +000035 GrPoint fOffset;
36 GrPoint fOuterRadii;
37 GrPoint fInnerRadii;
38};
39
commit-bot@chromium.org81312832013-03-22 18:34:09 +000040inline bool circle_stays_circle(const SkMatrix& m) {
41 return m.isSimilarity();
42}
43
44}
45
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000046///////////////////////////////////////////////////////////////////////////////
47
48/**
49 * The output of this effect is a modulation of the input color and coverage for a circle,
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +000050 * specified as offset_x, offset_y (both from center point), outer radius and inner radius.
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000051 */
52
53class CircleEdgeEffect : public GrEffect {
54public:
55 static GrEffectRef* Create(bool stroke) {
bsalomon@google.comd42aca32013-04-23 15:37:27 +000056 GR_CREATE_STATIC_EFFECT(gCircleStrokeEdge, CircleEdgeEffect, (true));
57 GR_CREATE_STATIC_EFFECT(gCircleFillEdge, CircleEdgeEffect, (false));
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000058
59 if (stroke) {
bsalomon@google.comd42aca32013-04-23 15:37:27 +000060 gCircleStrokeEdge->ref();
61 return gCircleStrokeEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000062 } else {
bsalomon@google.comd42aca32013-04-23 15:37:27 +000063 gCircleFillEdge->ref();
64 return gCircleFillEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000065 }
66 }
67
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +000068 virtual void getConstantColorComponents(GrColor* color,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000069 uint32_t* validFlags) const SK_OVERRIDE {
70 *validFlags = 0;
71 }
72
73 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
74 return GrTBackendEffectFactory<CircleEdgeEffect>::getInstance();
75 }
76
77 virtual ~CircleEdgeEffect() {}
78
79 static const char* Name() { return "CircleEdge"; }
80
81 inline bool isStroked() const { return fStroke; }
82
83 class GLEffect : public GrGLEffect {
84 public:
85 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
86 : INHERITED (factory) {}
87
88 virtual void emitCode(GrGLShaderBuilder* builder,
89 const GrDrawEffect& drawEffect,
90 EffectKey key,
91 const char* outputColor,
92 const char* inputColor,
93 const TextureSamplerArray& samplers) SK_OVERRIDE {
94 const CircleEdgeEffect& circleEffect = drawEffect.castEffect<CircleEdgeEffect>();
95 const char *vsName, *fsName;
96 builder->addVarying(kVec4f_GrSLType, "CircleEdge", &vsName, &fsName);
97
98 const SkString* attrName =
99 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]);
100 builder->vsCodeAppendf("\t%s = %s;\n", vsName, attrName->c_str());
101
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000102 builder->fsCodeAppendf("\tfloat d = length(%s.xy);\n", fsName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000103 builder->fsCodeAppendf("\tfloat edgeAlpha = clamp(%s.z - d, 0.0, 1.0);\n", fsName);
104 if (circleEffect.isStroked()) {
105 builder->fsCodeAppendf("\tfloat innerAlpha = clamp(d - %s.w, 0.0, 1.0);\n", fsName);
106 builder->fsCodeAppend("\tedgeAlpha *= innerAlpha;\n");
107 }
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000108
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000109 SkString modulate;
bsalomon@google.com018f1792013-04-18 19:36:09 +0000110 GrGLSLModulatef<4>(&modulate, inputColor, "edgeAlpha");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000111 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str());
112 }
113
114 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
115 const CircleEdgeEffect& circleEffect = drawEffect.castEffect<CircleEdgeEffect>();
116
117 return circleEffect.isStroked() ? 0x1 : 0x0;
118 }
119
120 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {}
121
122 private:
123 typedef GrGLEffect INHERITED;
124 };
125
126
127private:
128 CircleEdgeEffect(bool stroke) : GrEffect() {
129 this->addVertexAttrib(kVec4f_GrSLType);
130 fStroke = stroke;
131 }
132
133 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
134 const CircleEdgeEffect& cee = CastEffect<CircleEdgeEffect>(other);
135 return cee.fStroke == fStroke;
136 }
137
138 bool fStroke;
139
140 GR_DECLARE_EFFECT_TEST;
141
142 typedef GrEffect INHERITED;
143};
144
145GR_DEFINE_EFFECT_TEST(CircleEdgeEffect);
146
147GrEffectRef* CircleEdgeEffect::TestCreate(SkMWCRandom* random,
148 GrContext* context,
149 const GrDrawTargetCaps&,
150 GrTexture* textures[]) {
151 return CircleEdgeEffect::Create(random->nextBool());
152}
153
154///////////////////////////////////////////////////////////////////////////////
155
156/**
157 * The output of this effect is a modulation of the input color and coverage for an axis-aligned
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +0000158 * ellipse, specified as a 2D offset from center, and the reciprocals of the outer and inner radii,
159 * in both x and y directions.
160 *
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000161 * We are using an implicit function of x^2/a^2 + y^2/b^2 - 1 = 0.
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000162 */
163
164class EllipseEdgeEffect : public GrEffect {
165public:
166 static GrEffectRef* Create(bool stroke) {
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000167 GR_CREATE_STATIC_EFFECT(gEllipseStrokeEdge, EllipseEdgeEffect, (true));
168 GR_CREATE_STATIC_EFFECT(gEllipseFillEdge, EllipseEdgeEffect, (false));
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000169
170 if (stroke) {
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000171 gEllipseStrokeEdge->ref();
172 return gEllipseStrokeEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000173 } else {
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000174 gEllipseFillEdge->ref();
175 return gEllipseFillEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000176 }
177 }
178
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000179 virtual void getConstantColorComponents(GrColor* color,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000180 uint32_t* validFlags) const SK_OVERRIDE {
181 *validFlags = 0;
182 }
183
184 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
185 return GrTBackendEffectFactory<EllipseEdgeEffect>::getInstance();
186 }
187
188 virtual ~EllipseEdgeEffect() {}
189
190 static const char* Name() { return "EllipseEdge"; }
191
192 inline bool isStroked() const { return fStroke; }
193
194 class GLEffect : public GrGLEffect {
195 public:
196 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
197 : INHERITED (factory) {}
198
199 virtual void emitCode(GrGLShaderBuilder* builder,
200 const GrDrawEffect& drawEffect,
201 EffectKey key,
202 const char* outputColor,
203 const char* inputColor,
204 const TextureSamplerArray& samplers) SK_OVERRIDE {
205 const EllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<EllipseEdgeEffect>();
206
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000207 const char *vsOffsetName, *fsOffsetName;
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000208 const char *vsRadiiName, *fsRadiiName;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000209
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000210 builder->addVarying(kVec2f_GrSLType, "EllipseOffsets", &vsOffsetName, &fsOffsetName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000211 const SkString* attr0Name =
212 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000213 builder->vsCodeAppendf("\t%s = %s;\n", vsOffsetName, attr0Name->c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000214
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000215 builder->addVarying(kVec4f_GrSLType, "EllipseRadii", &vsRadiiName, &fsRadiiName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000216 const SkString* attr1Name =
217 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[1]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000218 builder->vsCodeAppendf("\t%s = %s;\n", vsRadiiName, attr1Name->c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000219
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +0000220 // for outer curve
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000221 builder->fsCodeAppendf("\tvec2 scaledOffset = %s*%s.xy;\n", fsOffsetName, fsRadiiName);
222 builder->fsCodeAppend("\tfloat test = dot(scaledOffset, scaledOffset) - 1.0;\n");
223 builder->fsCodeAppendf("\tvec2 grad = 2.0*scaledOffset*%s.xy;\n", fsRadiiName);
224 builder->fsCodeAppend("\tfloat invlen = inversesqrt(dot(grad, grad));\n");
225 builder->fsCodeAppend("\tfloat edgeAlpha = clamp(0.5-test*invlen, 0.0, 1.0);\n");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000226
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000227 // for inner curve
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000228 if (ellipseEffect.isStroked()) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000229 builder->fsCodeAppendf("\tscaledOffset = %s*%s.zw;\n", fsOffsetName, fsRadiiName);
230 builder->fsCodeAppend("\ttest = dot(scaledOffset, scaledOffset) - 1.0;\n");
231 builder->fsCodeAppendf("\tgrad = 2.0*scaledOffset*%s.zw;\n", fsRadiiName);
232 builder->fsCodeAppend("\tinvlen = inversesqrt(dot(grad, grad));\n");
233 builder->fsCodeAppend("\tedgeAlpha *= clamp(0.5+test*invlen, 0.0, 1.0);\n");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000234 }
235
236 SkString modulate;
bsalomon@google.com018f1792013-04-18 19:36:09 +0000237 GrGLSLModulatef<4>(&modulate, inputColor, "edgeAlpha");
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000238 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000239 }
240
241 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
242 const EllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<EllipseEdgeEffect>();
243
244 return ellipseEffect.isStroked() ? 0x1 : 0x0;
245 }
246
247 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {
248 }
249
250 private:
251 typedef GrGLEffect INHERITED;
252 };
253
254private:
255 EllipseEdgeEffect(bool stroke) : GrEffect() {
256 this->addVertexAttrib(kVec2f_GrSLType);
257 this->addVertexAttrib(kVec4f_GrSLType);
258 fStroke = stroke;
259 }
260
261 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
262 const EllipseEdgeEffect& eee = CastEffect<EllipseEdgeEffect>(other);
263 return eee.fStroke == fStroke;
264 }
265
266 bool fStroke;
267
268 GR_DECLARE_EFFECT_TEST;
269
270 typedef GrEffect INHERITED;
271};
272
273GR_DEFINE_EFFECT_TEST(EllipseEdgeEffect);
274
275GrEffectRef* EllipseEdgeEffect::TestCreate(SkMWCRandom* random,
276 GrContext* context,
277 const GrDrawTargetCaps&,
278 GrTexture* textures[]) {
279 return EllipseEdgeEffect::Create(random->nextBool());
280}
281
282///////////////////////////////////////////////////////////////////////////////
283
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000284bool GrOvalRenderer::drawOval(GrDrawTarget* target, const GrContext* context, bool useAA,
285 const GrRect& oval, const SkStrokeRec& stroke)
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000286{
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000287 if (!useAA) {
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000288 return false;
289 }
290
291 const SkMatrix& vm = context->getMatrix();
292
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000293 // we can draw circles
294 if (SkScalarNearlyEqual(oval.width(), oval.height())
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000295 && circle_stays_circle(vm)) {
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000296 this->drawCircle(target, useAA, oval, stroke);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000297
298 // and axis-aligned ellipses only
299 } else if (vm.rectStaysRect()) {
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000300 return this->drawEllipse(target, useAA, oval, stroke);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000301
302 } else {
303 return false;
304 }
305
306 return true;
307}
308
robertphillips@google.com42903302013-04-20 12:26:07 +0000309namespace {
310
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000311///////////////////////////////////////////////////////////////////////////////
312
robertphillips@google.com42903302013-04-20 12:26:07 +0000313// position + edge
314extern const GrVertexAttrib gCircleVertexAttribs[] = {
315 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
316 {kVec4f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding}
317};
318
319};
320
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000321void GrOvalRenderer::drawCircle(GrDrawTarget* target,
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000322 bool useAA,
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000323 const GrRect& circle,
324 const SkStrokeRec& stroke)
325{
326 GrDrawState* drawState = target->drawState();
327
328 const SkMatrix& vm = drawState->getViewMatrix();
329 GrPoint center = GrPoint::Make(circle.centerX(), circle.centerY());
330 vm.mapPoints(&center, 1);
331 SkScalar radius = vm.mapRadius(SkScalarHalf(circle.width()));
332 SkScalar strokeWidth = vm.mapRadius(stroke.getWidth());
333
bsalomon@google.com137f1342013-05-29 21:27:53 +0000334 GrDrawState::AutoViewMatrixRestore avmr;
335 if (!avmr.setIdentity(drawState)) {
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000336 return;
337 }
338
robertphillips@google.com42903302013-04-20 12:26:07 +0000339 drawState->setVertexAttribs<gCircleVertexAttribs>(SK_ARRAY_COUNT(gCircleVertexAttribs));
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000340 GrAssert(sizeof(CircleVertex) == drawState->getVertexSize());
341
342 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
343 if (!geo.succeeded()) {
344 GrPrintf("Failed to get space for vertices!\n");
345 return;
346 }
347
348 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
349
350 SkStrokeRec::Style style = stroke.getStyle();
351 bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style);
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000352
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000353 GrEffectRef* effect = CircleEdgeEffect::Create(isStroked);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000354 static const int kCircleEdgeAttrIndex = 1;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000355 drawState->addCoverageEffect(effect, kCircleEdgeAttrIndex)->unref();
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000356
357 SkScalar innerRadius = 0.0f;
358 SkScalar outerRadius = radius;
359 SkScalar halfWidth = 0;
360 if (style != SkStrokeRec::kFill_Style) {
361 if (SkScalarNearlyZero(strokeWidth)) {
362 halfWidth = SK_ScalarHalf;
363 } else {
364 halfWidth = SkScalarHalf(strokeWidth);
365 }
366
367 outerRadius += halfWidth;
368 if (isStroked) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000369 innerRadius = radius - halfWidth;
370 isStroked = (innerRadius > 0);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000371 }
372 }
373
bsalomon@google.com58e30fe2013-04-01 19:01:20 +0000374 // The radii are outset for two reasons. First, it allows the shader to simply perform
375 // clamp(distance-to-center - radius, 0, 1). Second, the outer radius is used to compute the
376 // verts of the bounding box that is rendered and the outset ensures the box will cover all
377 // pixels partially covered by the circle.
378 outerRadius += SK_ScalarHalf;
379 innerRadius -= SK_ScalarHalf;
380
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000381 SkRect bounds = SkRect::MakeLTRB(
bsalomon@google.com58e30fe2013-04-01 19:01:20 +0000382 center.fX - outerRadius,
383 center.fY - outerRadius,
384 center.fX + outerRadius,
385 center.fY + outerRadius
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000386 );
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000387
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000388 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000389 verts[0].fOffset = SkPoint::Make(-outerRadius, -outerRadius);
390 verts[0].fOuterRadius = outerRadius;
391 verts[0].fInnerRadius = innerRadius;
392
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000393 verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
skia.committer@gmail.com46746762013-04-12 07:01:51 +0000394 verts[1].fOffset = SkPoint::Make(outerRadius, -outerRadius);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000395 verts[1].fOuterRadius = outerRadius;
396 verts[1].fInnerRadius = innerRadius;
397
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000398 verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000399 verts[2].fOffset = SkPoint::Make(-outerRadius, outerRadius);
400 verts[2].fOuterRadius = outerRadius;
401 verts[2].fInnerRadius = innerRadius;
402
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000403 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000404 verts[3].fOffset = SkPoint::Make(outerRadius, outerRadius);
405 verts[3].fOuterRadius = outerRadius;
406 verts[3].fInnerRadius = innerRadius;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000407
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000408 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000409}
410
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000411///////////////////////////////////////////////////////////////////////////////
412
robertphillips@google.com42903302013-04-20 12:26:07 +0000413namespace {
414
415// position + edge
416extern const GrVertexAttrib gEllipseVertexAttribs[] = {
417 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
418 {kVec2f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding},
419 {kVec4f_GrVertexAttribType, 2*sizeof(GrPoint), kEffect_GrVertexAttribBinding}
420};
421
422};
423
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000424bool GrOvalRenderer::drawEllipse(GrDrawTarget* target,
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000425 bool useAA,
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000426 const GrRect& ellipse,
427 const SkStrokeRec& stroke)
428{
429 GrDrawState* drawState = target->drawState();
430#ifdef SK_DEBUG
431 {
432 // we should have checked for this previously
433 bool isAxisAlignedEllipse = drawState->getViewMatrix().rectStaysRect();
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000434 SkASSERT(useAA && isAxisAlignedEllipse);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000435 }
436#endif
437
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000438 // do any matrix crunching before we reset the draw state for device coords
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000439 const SkMatrix& vm = drawState->getViewMatrix();
440 GrPoint center = GrPoint::Make(ellipse.centerX(), ellipse.centerY());
441 vm.mapPoints(&center, 1);
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000442 SkScalar ellipseXRadius = SkScalarHalf(ellipse.width());
443 SkScalar ellipseYRadius = SkScalarHalf(ellipse.height());
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +0000444 SkScalar xRadius = SkScalarAbs(vm[SkMatrix::kMScaleX]*ellipseXRadius +
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000445 vm[SkMatrix::kMSkewY]*ellipseYRadius);
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +0000446 SkScalar yRadius = SkScalarAbs(vm[SkMatrix::kMSkewX]*ellipseXRadius +
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000447 vm[SkMatrix::kMScaleY]*ellipseYRadius);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000448
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000449 // do (potentially) anisotropic mapping of stroke
450 SkVector scaledStroke;
451 SkScalar strokeWidth = stroke.getWidth();
452 scaledStroke.fX = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMScaleX] + vm[SkMatrix::kMSkewY]));
453 scaledStroke.fY = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMSkewX] + vm[SkMatrix::kMScaleY]));
454
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000455 SkStrokeRec::Style style = stroke.getStyle();
456 bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style);
457
458 SkScalar innerXRadius = 0.0f;
459 SkScalar innerYRadius = 0.0f;
460 if (SkStrokeRec::kFill_Style != style) {
461 if (SkScalarNearlyZero(scaledStroke.length())) {
462 scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf);
463 } else {
464 scaledStroke.scale(SK_ScalarHalf);
465 }
466
467 // we only handle thick strokes for near-circular ellipses
468 if (scaledStroke.length() > SK_ScalarHalf &&
469 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
470 return false;
471 }
472
473 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
474 if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius ||
475 scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) {
476 return false;
477 }
478
479 // this is legit only if scale & translation (which should be the case at the moment)
480 if (isStroked) {
481 innerXRadius = xRadius - scaledStroke.fX;
482 innerYRadius = yRadius - scaledStroke.fY;
483 isStroked = (innerXRadius > 0 && innerYRadius > 0);
484 }
485
486 xRadius += scaledStroke.fX;
487 yRadius += scaledStroke.fY;
488 }
489
bsalomon@google.com137f1342013-05-29 21:27:53 +0000490 GrDrawState::AutoViewMatrixRestore avmr;
491 if (!avmr.setIdentity(drawState)) {
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000492 return false;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000493 }
494
robertphillips@google.com42903302013-04-20 12:26:07 +0000495 drawState->setVertexAttribs<gEllipseVertexAttribs>(SK_ARRAY_COUNT(gEllipseVertexAttribs));
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000496 GrAssert(sizeof(EllipseVertex) == drawState->getVertexSize());
497
498 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
499 if (!geo.succeeded()) {
500 GrPrintf("Failed to get space for vertices!\n");
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000501 return false;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000502 }
503
504 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices());
505
jvanverth@google.come3647412013-05-08 15:31:43 +0000506 GrEffectRef* effect = EllipseEdgeEffect::Create(isStroked);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000507
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000508 static const int kEllipseCenterAttrIndex = 1;
509 static const int kEllipseEdgeAttrIndex = 2;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000510 drawState->addCoverageEffect(effect, kEllipseCenterAttrIndex, kEllipseEdgeAttrIndex)->unref();
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000511
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000512 // Compute the reciprocals of the radii here to save time in the shader
513 SkScalar xRadRecip = SkScalarInvert(xRadius);
514 SkScalar yRadRecip = SkScalarInvert(yRadius);
515 SkScalar xInnerRadRecip = SkScalarInvert(innerXRadius);
516 SkScalar yInnerRadRecip = SkScalarInvert(innerYRadius);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000517
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000518 // We've extended the outer x radius out half a pixel to antialias.
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000519 // This will also expand the rect so all the pixels will be captured.
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000520 // TODO: Consider if we should use sqrt(2)/2 instead
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000521 xRadius += SK_ScalarHalf;
522 yRadius += SK_ScalarHalf;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000523
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000524 SkRect bounds = SkRect::MakeLTRB(
525 center.fX - xRadius,
526 center.fY - yRadius,
527 center.fX + xRadius,
528 center.fY + yRadius
529 );
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000530
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000531 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000532 verts[0].fOffset = SkPoint::Make(-xRadius, -yRadius);
533 verts[0].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
534 verts[0].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000535
536 verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000537 verts[1].fOffset = SkPoint::Make(xRadius, -yRadius);
538 verts[1].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
539 verts[1].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000540
541 verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000542 verts[2].fOffset = SkPoint::Make(-xRadius, yRadius);
543 verts[2].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
544 verts[2].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000545
546 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000547 verts[3].fOffset = SkPoint::Make(xRadius, yRadius);
548 verts[3].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
549 verts[3].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
skia.committer@gmail.com46746762013-04-12 07:01:51 +0000550
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000551 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds);
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000552
553 return true;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000554}
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000555
556///////////////////////////////////////////////////////////////////////////////
557
558static const uint16_t gRRectIndices[] = {
559 // corners
560 0, 1, 5, 0, 5, 4,
561 2, 3, 7, 2, 7, 6,
562 8, 9, 13, 8, 13, 12,
563 10, 11, 15, 10, 15, 14,
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000564
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000565 // edges
566 1, 2, 6, 1, 6, 5,
567 4, 5, 9, 4, 9, 8,
568 6, 7, 11, 6, 11, 10,
569 9, 10, 14, 9, 14, 13,
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000570
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000571 // center
572 // we place this at the end so that we can ignore these indices when rendering stroke-only
573 5, 6, 10, 5, 10, 9
574};
575
576
577GrIndexBuffer* GrOvalRenderer::rRectIndexBuffer(GrGpu* gpu) {
578 if (NULL == fRRectIndexBuffer) {
579 fRRectIndexBuffer =
580 gpu->createIndexBuffer(sizeof(gRRectIndices), false);
581 if (NULL != fRRectIndexBuffer) {
582#if GR_DEBUG
583 bool updated =
584#endif
585 fRRectIndexBuffer->updateData(gRRectIndices,
586 sizeof(gRRectIndices));
587 GR_DEBUGASSERT(updated);
588 }
589 }
590 return fRRectIndexBuffer;
591}
592
skia.committer@gmail.com2fd42c42013-05-03 07:01:00 +0000593bool GrOvalRenderer::drawSimpleRRect(GrDrawTarget* target, GrContext* context, bool useAA,
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000594 const SkRRect& rrect, const SkStrokeRec& stroke)
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000595{
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000596 // only anti-aliased rrects for now
597 if (!useAA) {
598 return false;
599 }
600
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000601 const SkMatrix& vm = context->getMatrix();
602#ifdef SK_DEBUG
603 {
604 // we should have checked for this previously
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000605 SkASSERT(useAA && vm.rectStaysRect() && rrect.isSimple());
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000606 }
607#endif
608
609 // do any matrix crunching before we reset the draw state for device coords
610 const SkRect& rrectBounds = rrect.getBounds();
611 SkRect bounds;
612 vm.mapRect(&bounds, rrectBounds);
613
614 SkVector radii = rrect.getSimpleRadii();
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000615 SkScalar xRadius = SkScalarAbs(vm[SkMatrix::kMScaleX]*radii.fX +
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000616 vm[SkMatrix::kMSkewY]*radii.fY);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000617 SkScalar yRadius = SkScalarAbs(vm[SkMatrix::kMSkewX]*radii.fX +
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000618 vm[SkMatrix::kMScaleY]*radii.fY);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000619
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000620 // if hairline stroke is greater than radius, we don't handle that right now
621 SkStrokeRec::Style style = stroke.getStyle();
622 if (SkStrokeRec::kHairline_Style == style &&
623 (SK_ScalarHalf >= xRadius || SK_ScalarHalf >= yRadius)) {
624 return false;
625 }
626
627 // do (potentially) anisotropic mapping of stroke
628 SkVector scaledStroke;
629 SkScalar strokeWidth = stroke.getWidth();
630 scaledStroke.fX = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMScaleX] + vm[SkMatrix::kMSkewY]));
631 scaledStroke.fY = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMSkewX] + vm[SkMatrix::kMScaleY]));
632
633 // if half of strokewidth is greater than radius, we don't handle that right now
634 if (SK_ScalarHalf*scaledStroke.fX >= xRadius || SK_ScalarHalf*scaledStroke.fY >= yRadius) {
635 return false;
636 }
637
638 // reset to device coordinates
639 GrDrawState* drawState = target->drawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000640 GrDrawState::AutoViewMatrixRestore avmr;
641 if (!avmr.setIdentity(drawState)) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000642 return false;
643 }
644
645 bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style);
646
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000647 GrIndexBuffer* indexBuffer = this->rRectIndexBuffer(context->getGpu());
648 if (NULL == indexBuffer) {
649 GrPrintf("Failed to create index buffer!\n");
650 return false;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000651 }
652
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000653 // if the corners are circles, use the circle renderer
654 if ((!isStroked || scaledStroke.fX == scaledStroke.fY) && xRadius == yRadius) {
655 drawState->setVertexAttribs<gCircleVertexAttribs>(SK_ARRAY_COUNT(gCircleVertexAttribs));
656 GrAssert(sizeof(CircleVertex) == drawState->getVertexSize());
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000657
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000658 GrDrawTarget::AutoReleaseGeometry geo(target, 16, 0);
659 if (!geo.succeeded()) {
660 GrPrintf("Failed to get space for vertices!\n");
661 return false;
662 }
663 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000664
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000665 SkScalar innerRadius = 0.0f;
666 SkScalar outerRadius = xRadius;
667 SkScalar halfWidth = 0;
668 if (style != SkStrokeRec::kFill_Style) {
669 if (SkScalarNearlyZero(scaledStroke.fX)) {
670 halfWidth = SK_ScalarHalf;
671 } else {
672 halfWidth = SkScalarHalf(scaledStroke.fX);
673 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000674
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000675 if (isStroked) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000676 innerRadius = xRadius - halfWidth;
677 isStroked = (innerRadius > 0);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000678 }
679 outerRadius += halfWidth;
680 bounds.outset(halfWidth, halfWidth);
681 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000682
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000683 GrEffectRef* effect = CircleEdgeEffect::Create(isStroked);
684 static const int kCircleEdgeAttrIndex = 1;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000685 drawState->addCoverageEffect(effect, kCircleEdgeAttrIndex)->unref();
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +0000686
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000687 // The radii are outset for two reasons. First, it allows the shader to simply perform
688 // clamp(distance-to-center - radius, 0, 1). Second, the outer radius is used to compute the
689 // verts of the bounding box that is rendered and the outset ensures the box will cover all
690 // pixels partially covered by the circle.
691 outerRadius += SK_ScalarHalf;
692 innerRadius -= SK_ScalarHalf;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000693
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000694 // Expand the rect so all the pixels will be captured.
695 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000696
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000697 SkScalar yCoords[4] = {
698 bounds.fTop,
699 bounds.fTop + outerRadius,
700 bounds.fBottom - outerRadius,
701 bounds.fBottom
702 };
703 SkScalar yOuterRadii[4] = {
704 -outerRadius,
705 0,
706 0,
707 outerRadius
708 };
709 for (int i = 0; i < 4; ++i) {
710 verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]);
711 verts->fOffset = SkPoint::Make(-outerRadius, yOuterRadii[i]);
712 verts->fOuterRadius = outerRadius;
713 verts->fInnerRadius = innerRadius;
714 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000715
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000716 verts->fPos = SkPoint::Make(bounds.fLeft + outerRadius, yCoords[i]);
717 verts->fOffset = SkPoint::Make(0, yOuterRadii[i]);
718 verts->fOuterRadius = outerRadius;
719 verts->fInnerRadius = innerRadius;
720 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000721
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000722 verts->fPos = SkPoint::Make(bounds.fRight - outerRadius, yCoords[i]);
723 verts->fOffset = SkPoint::Make(0, yOuterRadii[i]);
724 verts->fOuterRadius = outerRadius;
725 verts->fInnerRadius = innerRadius;
726 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000727
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000728 verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]);
729 verts->fOffset = SkPoint::Make(outerRadius, yOuterRadii[i]);
730 verts->fOuterRadius = outerRadius;
731 verts->fInnerRadius = innerRadius;
732 verts++;
733 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000734
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000735 // drop out the middle quad if we're stroked
736 int indexCnt = isStroked ? GR_ARRAY_COUNT(gRRectIndices)-6 : GR_ARRAY_COUNT(gRRectIndices);
737 target->setIndexSourceToBuffer(indexBuffer);
738 target->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, 16, indexCnt, &bounds);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000739
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000740 // otherwise we use the ellipse renderer
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000741 } else {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000742 drawState->setVertexAttribs<gEllipseVertexAttribs>(SK_ARRAY_COUNT(gEllipseVertexAttribs));
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000743 GrAssert(sizeof(EllipseVertex) == drawState->getVertexSize());
744
745 SkScalar innerXRadius = 0.0f;
746 SkScalar innerYRadius = 0.0f;
747 if (SkStrokeRec::kFill_Style != style) {
748 if (SkScalarNearlyZero(scaledStroke.length())) {
749 scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf);
750 } else {
751 scaledStroke.scale(SK_ScalarHalf);
752 }
753
754 // we only handle thick strokes for near-circular ellipses
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +0000755 if (scaledStroke.length() > SK_ScalarHalf &&
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000756 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
757 return false;
758 }
759
760 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
761 if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius ||
762 scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) {
763 return false;
764 }
765
766 // this is legit only if scale & translation (which should be the case at the moment)
767 if (isStroked) {
768 innerXRadius = xRadius - scaledStroke.fX;
769 innerYRadius = yRadius - scaledStroke.fY;
770 isStroked = (innerXRadius > 0 && innerYRadius > 0);
771 }
772
773 xRadius += scaledStroke.fX;
774 yRadius += scaledStroke.fY;
775 bounds.outset(scaledStroke.fX, scaledStroke.fY);
776 }
jvanverth@google.come3647412013-05-08 15:31:43 +0000777
778 GrDrawTarget::AutoReleaseGeometry geo(target, 16, 0);
779 if (!geo.succeeded()) {
780 GrPrintf("Failed to get space for vertices!\n");
781 return false;
782 }
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000783 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices());
jvanverth@google.come3647412013-05-08 15:31:43 +0000784
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000785 GrEffectRef* effect = EllipseEdgeEffect::Create(isStroked);
jvanverth@google.come3647412013-05-08 15:31:43 +0000786 static const int kEllipseOffsetAttrIndex = 1;
787 static const int kEllipseRadiiAttrIndex = 2;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000788 drawState->addCoverageEffect(effect,
789 kEllipseOffsetAttrIndex, kEllipseRadiiAttrIndex)->unref();
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000790
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000791 // Compute the reciprocals of the radii here to save time in the shader
792 SkScalar xRadRecip = SkScalarInvert(xRadius);
793 SkScalar yRadRecip = SkScalarInvert(yRadius);
794 SkScalar xInnerRadRecip = SkScalarInvert(innerXRadius);
795 SkScalar yInnerRadRecip = SkScalarInvert(innerYRadius);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000796
797 // Extend the radii out half a pixel to antialias.
798 SkScalar xOuterRadius = xRadius + SK_ScalarHalf;
799 SkScalar yOuterRadius = yRadius + SK_ScalarHalf;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000800
801 // Expand the rect so all the pixels will be captured.
802 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
803
804 SkScalar yCoords[4] = {
805 bounds.fTop,
806 bounds.fTop + yOuterRadius,
807 bounds.fBottom - yOuterRadius,
808 bounds.fBottom
809 };
810 SkScalar yOuterOffsets[4] = {
811 -yOuterRadius,
812 SK_ScalarNearlyZero, // we're using inversesqrt() in the shader, so can't be exactly 0
813 SK_ScalarNearlyZero,
814 yOuterRadius
815 };
816
817 for (int i = 0; i < 4; ++i) {
818 verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]);
819 verts->fOffset = SkPoint::Make(-xOuterRadius, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000820 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
821 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000822 verts++;
823
824 verts->fPos = SkPoint::Make(bounds.fLeft + xOuterRadius, yCoords[i]);
825 verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000826 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
827 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000828 verts++;
829
830 verts->fPos = SkPoint::Make(bounds.fRight - xOuterRadius, yCoords[i]);
831 verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000832 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
833 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000834 verts++;
835
836 verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]);
837 verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000838 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
839 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000840 verts++;
841 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000842
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000843 // drop out the middle quad if we're stroked
844 int indexCnt = isStroked ? GR_ARRAY_COUNT(gRRectIndices)-6 : GR_ARRAY_COUNT(gRRectIndices);
845 target->setIndexSourceToBuffer(indexBuffer);
846 target->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, 16, indexCnt, &bounds);
847 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000848
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000849 return true;
850}