blob: dd9b19e17a4c218b99bcd8c6b2b0ef9e2d6bd5b0 [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
334 GrDrawState::AutoDeviceCoordDraw adcd(drawState);
335 if (!adcd.succeeded()) {
336 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);
352 enum {
353 // the edge effects share this stage with glyph rendering
354 // (kGlyphMaskStage in GrTextContext) && SW path rendering
355 // (kPathMaskStage in GrSWMaskHelper)
356 kEdgeEffectStage = GrPaint::kTotalStages,
357 };
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000358
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000359 GrEffectRef* effect = CircleEdgeEffect::Create(isStroked);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000360 static const int kCircleEdgeAttrIndex = 1;
361 drawState->setEffect(kEdgeEffectStage, effect, kCircleEdgeAttrIndex)->unref();
362
363 SkScalar innerRadius = 0.0f;
364 SkScalar outerRadius = radius;
365 SkScalar halfWidth = 0;
366 if (style != SkStrokeRec::kFill_Style) {
367 if (SkScalarNearlyZero(strokeWidth)) {
368 halfWidth = SK_ScalarHalf;
369 } else {
370 halfWidth = SkScalarHalf(strokeWidth);
371 }
372
373 outerRadius += halfWidth;
374 if (isStroked) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000375 innerRadius = radius - halfWidth;
376 isStroked = (innerRadius > 0);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000377 }
378 }
379
bsalomon@google.com58e30fe2013-04-01 19:01:20 +0000380 // The radii are outset for two reasons. First, it allows the shader to simply perform
381 // clamp(distance-to-center - radius, 0, 1). Second, the outer radius is used to compute the
382 // verts of the bounding box that is rendered and the outset ensures the box will cover all
383 // pixels partially covered by the circle.
384 outerRadius += SK_ScalarHalf;
385 innerRadius -= SK_ScalarHalf;
386
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000387 SkRect bounds = SkRect::MakeLTRB(
bsalomon@google.com58e30fe2013-04-01 19:01:20 +0000388 center.fX - outerRadius,
389 center.fY - outerRadius,
390 center.fX + outerRadius,
391 center.fY + outerRadius
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000392 );
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000393
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000394 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000395 verts[0].fOffset = SkPoint::Make(-outerRadius, -outerRadius);
396 verts[0].fOuterRadius = outerRadius;
397 verts[0].fInnerRadius = innerRadius;
398
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000399 verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
skia.committer@gmail.com46746762013-04-12 07:01:51 +0000400 verts[1].fOffset = SkPoint::Make(outerRadius, -outerRadius);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000401 verts[1].fOuterRadius = outerRadius;
402 verts[1].fInnerRadius = innerRadius;
403
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000404 verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000405 verts[2].fOffset = SkPoint::Make(-outerRadius, outerRadius);
406 verts[2].fOuterRadius = outerRadius;
407 verts[2].fInnerRadius = innerRadius;
408
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000409 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000410 verts[3].fOffset = SkPoint::Make(outerRadius, outerRadius);
411 verts[3].fOuterRadius = outerRadius;
412 verts[3].fInnerRadius = innerRadius;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000413
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000414 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000415}
416
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000417///////////////////////////////////////////////////////////////////////////////
418
robertphillips@google.com42903302013-04-20 12:26:07 +0000419namespace {
420
421// position + edge
422extern const GrVertexAttrib gEllipseVertexAttribs[] = {
423 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
424 {kVec2f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding},
425 {kVec4f_GrVertexAttribType, 2*sizeof(GrPoint), kEffect_GrVertexAttribBinding}
426};
427
428};
429
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000430bool GrOvalRenderer::drawEllipse(GrDrawTarget* target,
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000431 bool useAA,
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000432 const GrRect& ellipse,
433 const SkStrokeRec& stroke)
434{
435 GrDrawState* drawState = target->drawState();
436#ifdef SK_DEBUG
437 {
438 // we should have checked for this previously
439 bool isAxisAlignedEllipse = drawState->getViewMatrix().rectStaysRect();
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000440 SkASSERT(useAA && isAxisAlignedEllipse);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000441 }
442#endif
443
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000444 // do any matrix crunching before we reset the draw state for device coords
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000445 const SkMatrix& vm = drawState->getViewMatrix();
446 GrPoint center = GrPoint::Make(ellipse.centerX(), ellipse.centerY());
447 vm.mapPoints(&center, 1);
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000448 SkScalar ellipseXRadius = SkScalarHalf(ellipse.width());
449 SkScalar ellipseYRadius = SkScalarHalf(ellipse.height());
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +0000450 SkScalar xRadius = SkScalarAbs(vm[SkMatrix::kMScaleX]*ellipseXRadius +
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000451 vm[SkMatrix::kMSkewY]*ellipseYRadius);
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +0000452 SkScalar yRadius = SkScalarAbs(vm[SkMatrix::kMSkewX]*ellipseXRadius +
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000453 vm[SkMatrix::kMScaleY]*ellipseYRadius);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000454
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000455 // do (potentially) anisotropic mapping of stroke
456 SkVector scaledStroke;
457 SkScalar strokeWidth = stroke.getWidth();
458 scaledStroke.fX = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMScaleX] + vm[SkMatrix::kMSkewY]));
459 scaledStroke.fY = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMSkewX] + vm[SkMatrix::kMScaleY]));
460
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000461 SkStrokeRec::Style style = stroke.getStyle();
462 bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style);
463
464 SkScalar innerXRadius = 0.0f;
465 SkScalar innerYRadius = 0.0f;
466 if (SkStrokeRec::kFill_Style != style) {
467 if (SkScalarNearlyZero(scaledStroke.length())) {
468 scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf);
469 } else {
470 scaledStroke.scale(SK_ScalarHalf);
471 }
472
473 // we only handle thick strokes for near-circular ellipses
474 if (scaledStroke.length() > SK_ScalarHalf &&
475 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
476 return false;
477 }
478
479 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
480 if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius ||
481 scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) {
482 return false;
483 }
484
485 // this is legit only if scale & translation (which should be the case at the moment)
486 if (isStroked) {
487 innerXRadius = xRadius - scaledStroke.fX;
488 innerYRadius = yRadius - scaledStroke.fY;
489 isStroked = (innerXRadius > 0 && innerYRadius > 0);
490 }
491
492 xRadius += scaledStroke.fX;
493 yRadius += scaledStroke.fY;
494 }
495
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000496 GrDrawState::AutoDeviceCoordDraw adcd(drawState);
497 if (!adcd.succeeded()) {
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000498 return false;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000499 }
500
robertphillips@google.com42903302013-04-20 12:26:07 +0000501 drawState->setVertexAttribs<gEllipseVertexAttribs>(SK_ARRAY_COUNT(gEllipseVertexAttribs));
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000502 GrAssert(sizeof(EllipseVertex) == drawState->getVertexSize());
503
504 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
505 if (!geo.succeeded()) {
506 GrPrintf("Failed to get space for vertices!\n");
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000507 return false;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000508 }
509
510 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices());
511
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000512 enum {
513 // the edge effects share this stage with glyph rendering
514 // (kGlyphMaskStage in GrTextContext) && SW path rendering
515 // (kPathMaskStage in GrSWMaskHelper)
516 kEdgeEffectStage = GrPaint::kTotalStages,
517 };
jvanverth@google.come3647412013-05-08 15:31:43 +0000518 GrEffectRef* effect = EllipseEdgeEffect::Create(isStroked);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000519
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000520 static const int kEllipseCenterAttrIndex = 1;
521 static const int kEllipseEdgeAttrIndex = 2;
522 drawState->setEffect(kEdgeEffectStage, effect,
523 kEllipseCenterAttrIndex, kEllipseEdgeAttrIndex)->unref();
524
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000525 // Compute the reciprocals of the radii here to save time in the shader
526 SkScalar xRadRecip = SkScalarInvert(xRadius);
527 SkScalar yRadRecip = SkScalarInvert(yRadius);
528 SkScalar xInnerRadRecip = SkScalarInvert(innerXRadius);
529 SkScalar yInnerRadRecip = SkScalarInvert(innerYRadius);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000530
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000531 // We've extended the outer x radius out half a pixel to antialias.
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000532 // This will also expand the rect so all the pixels will be captured.
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000533 // TODO: Consider if we should use sqrt(2)/2 instead
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000534 xRadius += SK_ScalarHalf;
535 yRadius += SK_ScalarHalf;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000536
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000537 SkRect bounds = SkRect::MakeLTRB(
538 center.fX - xRadius,
539 center.fY - yRadius,
540 center.fX + xRadius,
541 center.fY + yRadius
542 );
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000543
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000544 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000545 verts[0].fOffset = SkPoint::Make(-xRadius, -yRadius);
546 verts[0].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
547 verts[0].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000548
549 verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000550 verts[1].fOffset = SkPoint::Make(xRadius, -yRadius);
551 verts[1].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
552 verts[1].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000553
554 verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000555 verts[2].fOffset = SkPoint::Make(-xRadius, yRadius);
556 verts[2].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
557 verts[2].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000558
559 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000560 verts[3].fOffset = SkPoint::Make(xRadius, yRadius);
561 verts[3].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
562 verts[3].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
skia.committer@gmail.com46746762013-04-12 07:01:51 +0000563
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000564 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds);
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000565
566 return true;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000567}
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000568
569///////////////////////////////////////////////////////////////////////////////
570
571static const uint16_t gRRectIndices[] = {
572 // corners
573 0, 1, 5, 0, 5, 4,
574 2, 3, 7, 2, 7, 6,
575 8, 9, 13, 8, 13, 12,
576 10, 11, 15, 10, 15, 14,
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000577
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000578 // edges
579 1, 2, 6, 1, 6, 5,
580 4, 5, 9, 4, 9, 8,
581 6, 7, 11, 6, 11, 10,
582 9, 10, 14, 9, 14, 13,
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000583
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000584 // center
585 // we place this at the end so that we can ignore these indices when rendering stroke-only
586 5, 6, 10, 5, 10, 9
587};
588
589
590GrIndexBuffer* GrOvalRenderer::rRectIndexBuffer(GrGpu* gpu) {
591 if (NULL == fRRectIndexBuffer) {
592 fRRectIndexBuffer =
593 gpu->createIndexBuffer(sizeof(gRRectIndices), false);
594 if (NULL != fRRectIndexBuffer) {
595#if GR_DEBUG
596 bool updated =
597#endif
598 fRRectIndexBuffer->updateData(gRRectIndices,
599 sizeof(gRRectIndices));
600 GR_DEBUGASSERT(updated);
601 }
602 }
603 return fRRectIndexBuffer;
604}
605
skia.committer@gmail.com2fd42c42013-05-03 07:01:00 +0000606bool GrOvalRenderer::drawSimpleRRect(GrDrawTarget* target, GrContext* context, bool useAA,
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000607 const SkRRect& rrect, const SkStrokeRec& stroke)
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000608{
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000609 // only anti-aliased rrects for now
610 if (!useAA) {
611 return false;
612 }
613
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000614 const SkMatrix& vm = context->getMatrix();
615#ifdef SK_DEBUG
616 {
617 // we should have checked for this previously
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000618 SkASSERT(useAA && vm.rectStaysRect() && rrect.isSimple());
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000619 }
620#endif
621
622 // do any matrix crunching before we reset the draw state for device coords
623 const SkRect& rrectBounds = rrect.getBounds();
624 SkRect bounds;
625 vm.mapRect(&bounds, rrectBounds);
626
627 SkVector radii = rrect.getSimpleRadii();
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000628 SkScalar xRadius = SkScalarAbs(vm[SkMatrix::kMScaleX]*radii.fX +
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000629 vm[SkMatrix::kMSkewY]*radii.fY);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000630 SkScalar yRadius = SkScalarAbs(vm[SkMatrix::kMSkewX]*radii.fX +
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000631 vm[SkMatrix::kMScaleY]*radii.fY);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000632
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000633 // if hairline stroke is greater than radius, we don't handle that right now
634 SkStrokeRec::Style style = stroke.getStyle();
635 if (SkStrokeRec::kHairline_Style == style &&
636 (SK_ScalarHalf >= xRadius || SK_ScalarHalf >= yRadius)) {
637 return false;
638 }
639
640 // do (potentially) anisotropic mapping of stroke
641 SkVector scaledStroke;
642 SkScalar strokeWidth = stroke.getWidth();
643 scaledStroke.fX = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMScaleX] + vm[SkMatrix::kMSkewY]));
644 scaledStroke.fY = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMSkewX] + vm[SkMatrix::kMScaleY]));
645
646 // if half of strokewidth is greater than radius, we don't handle that right now
647 if (SK_ScalarHalf*scaledStroke.fX >= xRadius || SK_ScalarHalf*scaledStroke.fY >= yRadius) {
648 return false;
649 }
650
651 // reset to device coordinates
652 GrDrawState* drawState = target->drawState();
653 GrDrawState::AutoDeviceCoordDraw adcd(drawState);
654 if (!adcd.succeeded()) {
655 return false;
656 }
657
658 bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style);
659
660 enum {
661 // the edge effects share this stage with glyph rendering
662 // (kGlyphMaskStage in GrTextContext) && SW path rendering
663 // (kPathMaskStage in GrSWMaskHelper)
664 kEdgeEffectStage = GrPaint::kTotalStages,
665 };
666
667 GrIndexBuffer* indexBuffer = this->rRectIndexBuffer(context->getGpu());
668 if (NULL == indexBuffer) {
669 GrPrintf("Failed to create index buffer!\n");
670 return false;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000671 }
672
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000673 // if the corners are circles, use the circle renderer
674 if ((!isStroked || scaledStroke.fX == scaledStroke.fY) && xRadius == yRadius) {
675 drawState->setVertexAttribs<gCircleVertexAttribs>(SK_ARRAY_COUNT(gCircleVertexAttribs));
676 GrAssert(sizeof(CircleVertex) == drawState->getVertexSize());
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000677
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000678 GrDrawTarget::AutoReleaseGeometry geo(target, 16, 0);
679 if (!geo.succeeded()) {
680 GrPrintf("Failed to get space for vertices!\n");
681 return false;
682 }
683 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000684
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000685 SkScalar innerRadius = 0.0f;
686 SkScalar outerRadius = xRadius;
687 SkScalar halfWidth = 0;
688 if (style != SkStrokeRec::kFill_Style) {
689 if (SkScalarNearlyZero(scaledStroke.fX)) {
690 halfWidth = SK_ScalarHalf;
691 } else {
692 halfWidth = SkScalarHalf(scaledStroke.fX);
693 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000694
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000695 if (isStroked) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000696 innerRadius = xRadius - halfWidth;
697 isStroked = (innerRadius > 0);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000698 }
699 outerRadius += halfWidth;
700 bounds.outset(halfWidth, halfWidth);
701 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000702
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000703 GrEffectRef* effect = CircleEdgeEffect::Create(isStroked);
704 static const int kCircleEdgeAttrIndex = 1;
705 drawState->setEffect(kEdgeEffectStage, effect, kCircleEdgeAttrIndex)->unref();
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +0000706
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000707 // The radii are outset for two reasons. First, it allows the shader to simply perform
708 // clamp(distance-to-center - radius, 0, 1). Second, the outer radius is used to compute the
709 // verts of the bounding box that is rendered and the outset ensures the box will cover all
710 // pixels partially covered by the circle.
711 outerRadius += SK_ScalarHalf;
712 innerRadius -= SK_ScalarHalf;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000713
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000714 // Expand the rect so all the pixels will be captured.
715 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000716
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000717 SkScalar yCoords[4] = {
718 bounds.fTop,
719 bounds.fTop + outerRadius,
720 bounds.fBottom - outerRadius,
721 bounds.fBottom
722 };
723 SkScalar yOuterRadii[4] = {
724 -outerRadius,
725 0,
726 0,
727 outerRadius
728 };
729 for (int i = 0; i < 4; ++i) {
730 verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]);
731 verts->fOffset = SkPoint::Make(-outerRadius, yOuterRadii[i]);
732 verts->fOuterRadius = outerRadius;
733 verts->fInnerRadius = innerRadius;
734 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000735
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000736 verts->fPos = SkPoint::Make(bounds.fLeft + outerRadius, yCoords[i]);
737 verts->fOffset = SkPoint::Make(0, yOuterRadii[i]);
738 verts->fOuterRadius = outerRadius;
739 verts->fInnerRadius = innerRadius;
740 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000741
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000742 verts->fPos = SkPoint::Make(bounds.fRight - outerRadius, yCoords[i]);
743 verts->fOffset = SkPoint::Make(0, yOuterRadii[i]);
744 verts->fOuterRadius = outerRadius;
745 verts->fInnerRadius = innerRadius;
746 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000747
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000748 verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]);
749 verts->fOffset = SkPoint::Make(outerRadius, yOuterRadii[i]);
750 verts->fOuterRadius = outerRadius;
751 verts->fInnerRadius = innerRadius;
752 verts++;
753 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000754
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000755 // drop out the middle quad if we're stroked
756 int indexCnt = isStroked ? GR_ARRAY_COUNT(gRRectIndices)-6 : GR_ARRAY_COUNT(gRRectIndices);
757 target->setIndexSourceToBuffer(indexBuffer);
758 target->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, 16, indexCnt, &bounds);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000759
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000760 // otherwise we use the ellipse renderer
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000761 } else {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000762 drawState->setVertexAttribs<gEllipseVertexAttribs>(SK_ARRAY_COUNT(gEllipseVertexAttribs));
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000763 GrAssert(sizeof(EllipseVertex) == drawState->getVertexSize());
764
765 SkScalar innerXRadius = 0.0f;
766 SkScalar innerYRadius = 0.0f;
767 if (SkStrokeRec::kFill_Style != style) {
768 if (SkScalarNearlyZero(scaledStroke.length())) {
769 scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf);
770 } else {
771 scaledStroke.scale(SK_ScalarHalf);
772 }
773
774 // we only handle thick strokes for near-circular ellipses
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +0000775 if (scaledStroke.length() > SK_ScalarHalf &&
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000776 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
777 return false;
778 }
779
780 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
781 if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius ||
782 scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) {
783 return false;
784 }
785
786 // this is legit only if scale & translation (which should be the case at the moment)
787 if (isStroked) {
788 innerXRadius = xRadius - scaledStroke.fX;
789 innerYRadius = yRadius - scaledStroke.fY;
790 isStroked = (innerXRadius > 0 && innerYRadius > 0);
791 }
792
793 xRadius += scaledStroke.fX;
794 yRadius += scaledStroke.fY;
795 bounds.outset(scaledStroke.fX, scaledStroke.fY);
796 }
jvanverth@google.come3647412013-05-08 15:31:43 +0000797
798 GrDrawTarget::AutoReleaseGeometry geo(target, 16, 0);
799 if (!geo.succeeded()) {
800 GrPrintf("Failed to get space for vertices!\n");
801 return false;
802 }
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000803 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices());
jvanverth@google.come3647412013-05-08 15:31:43 +0000804
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000805 GrEffectRef* effect = EllipseEdgeEffect::Create(isStroked);
jvanverth@google.come3647412013-05-08 15:31:43 +0000806 static const int kEllipseOffsetAttrIndex = 1;
807 static const int kEllipseRadiiAttrIndex = 2;
808 drawState->setEffect(kEdgeEffectStage, effect,
809 kEllipseOffsetAttrIndex, kEllipseRadiiAttrIndex)->unref();
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000810
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000811 // Compute the reciprocals of the radii here to save time in the shader
812 SkScalar xRadRecip = SkScalarInvert(xRadius);
813 SkScalar yRadRecip = SkScalarInvert(yRadius);
814 SkScalar xInnerRadRecip = SkScalarInvert(innerXRadius);
815 SkScalar yInnerRadRecip = SkScalarInvert(innerYRadius);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000816
817 // Extend the radii out half a pixel to antialias.
818 SkScalar xOuterRadius = xRadius + SK_ScalarHalf;
819 SkScalar yOuterRadius = yRadius + SK_ScalarHalf;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000820
821 // Expand the rect so all the pixels will be captured.
822 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
823
824 SkScalar yCoords[4] = {
825 bounds.fTop,
826 bounds.fTop + yOuterRadius,
827 bounds.fBottom - yOuterRadius,
828 bounds.fBottom
829 };
830 SkScalar yOuterOffsets[4] = {
831 -yOuterRadius,
832 SK_ScalarNearlyZero, // we're using inversesqrt() in the shader, so can't be exactly 0
833 SK_ScalarNearlyZero,
834 yOuterRadius
835 };
836
837 for (int i = 0; i < 4; ++i) {
838 verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]);
839 verts->fOffset = SkPoint::Make(-xOuterRadius, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000840 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
841 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000842 verts++;
843
844 verts->fPos = SkPoint::Make(bounds.fLeft + xOuterRadius, yCoords[i]);
845 verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000846 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
847 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000848 verts++;
849
850 verts->fPos = SkPoint::Make(bounds.fRight - xOuterRadius, yCoords[i]);
851 verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000852 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
853 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000854 verts++;
855
856 verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]);
857 verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000858 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
859 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000860 verts++;
861 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000862
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000863 // drop out the middle quad if we're stroked
864 int indexCnt = isStroked ? GR_ARRAY_COUNT(gRRectIndices)-6 : GR_ARRAY_COUNT(gRRectIndices);
865 target->setIndexSourceToBuffer(indexBuffer);
866 target->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, 16, indexCnt, &bounds);
867 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000868
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000869 return true;
870}