blob: bcd64d740dcf081b9261d26979e3ab56f604d829 [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);
jvanverth@google.comd1b5b142013-07-02 14:46:03 +0000224 builder->fsCodeAppend("\tfloat grad_dot = dot(grad, grad);\n");
225 // we need to clamp the length^2 of the gradiant vector to a non-zero value, because
226 // on the Nexus 4 the undefined result of inversesqrt(0) drops out an entire tile
227 // TODO: restrict this to Adreno-only
228 builder->fsCodeAppend("\tgrad_dot = max(grad_dot, 1.0e-4);\n");
229 builder->fsCodeAppend("\tfloat invlen = inversesqrt(grad_dot);\n");
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000230 builder->fsCodeAppend("\tfloat edgeAlpha = clamp(0.5-test*invlen, 0.0, 1.0);\n");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000231
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000232 // for inner curve
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000233 if (ellipseEffect.isStroked()) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000234 builder->fsCodeAppendf("\tscaledOffset = %s*%s.zw;\n", fsOffsetName, fsRadiiName);
235 builder->fsCodeAppend("\ttest = dot(scaledOffset, scaledOffset) - 1.0;\n");
236 builder->fsCodeAppendf("\tgrad = 2.0*scaledOffset*%s.zw;\n", fsRadiiName);
237 builder->fsCodeAppend("\tinvlen = inversesqrt(dot(grad, grad));\n");
238 builder->fsCodeAppend("\tedgeAlpha *= clamp(0.5+test*invlen, 0.0, 1.0);\n");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000239 }
240
241 SkString modulate;
bsalomon@google.com018f1792013-04-18 19:36:09 +0000242 GrGLSLModulatef<4>(&modulate, inputColor, "edgeAlpha");
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000243 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000244 }
245
246 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
247 const EllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<EllipseEdgeEffect>();
248
249 return ellipseEffect.isStroked() ? 0x1 : 0x0;
250 }
251
252 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {
253 }
254
255 private:
256 typedef GrGLEffect INHERITED;
257 };
258
259private:
260 EllipseEdgeEffect(bool stroke) : GrEffect() {
261 this->addVertexAttrib(kVec2f_GrSLType);
262 this->addVertexAttrib(kVec4f_GrSLType);
263 fStroke = stroke;
264 }
265
266 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
267 const EllipseEdgeEffect& eee = CastEffect<EllipseEdgeEffect>(other);
268 return eee.fStroke == fStroke;
269 }
270
271 bool fStroke;
272
273 GR_DECLARE_EFFECT_TEST;
274
275 typedef GrEffect INHERITED;
276};
277
278GR_DEFINE_EFFECT_TEST(EllipseEdgeEffect);
279
280GrEffectRef* EllipseEdgeEffect::TestCreate(SkMWCRandom* random,
281 GrContext* context,
282 const GrDrawTargetCaps&,
283 GrTexture* textures[]) {
284 return EllipseEdgeEffect::Create(random->nextBool());
285}
286
287///////////////////////////////////////////////////////////////////////////////
288
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000289bool GrOvalRenderer::drawOval(GrDrawTarget* target, const GrContext* context, bool useAA,
290 const GrRect& oval, const SkStrokeRec& stroke)
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000291{
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000292 if (!useAA) {
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000293 return false;
294 }
295
296 const SkMatrix& vm = context->getMatrix();
297
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000298 // we can draw circles
299 if (SkScalarNearlyEqual(oval.width(), oval.height())
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000300 && circle_stays_circle(vm)) {
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000301 this->drawCircle(target, useAA, oval, stroke);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000302
303 // and axis-aligned ellipses only
304 } else if (vm.rectStaysRect()) {
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000305 return this->drawEllipse(target, useAA, oval, stroke);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000306
307 } else {
308 return false;
309 }
310
311 return true;
312}
313
robertphillips@google.com42903302013-04-20 12:26:07 +0000314namespace {
315
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000316///////////////////////////////////////////////////////////////////////////////
317
robertphillips@google.com42903302013-04-20 12:26:07 +0000318// position + edge
319extern const GrVertexAttrib gCircleVertexAttribs[] = {
320 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
321 {kVec4f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding}
322};
323
324};
325
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000326void GrOvalRenderer::drawCircle(GrDrawTarget* target,
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000327 bool useAA,
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000328 const GrRect& circle,
329 const SkStrokeRec& stroke)
330{
331 GrDrawState* drawState = target->drawState();
332
333 const SkMatrix& vm = drawState->getViewMatrix();
334 GrPoint center = GrPoint::Make(circle.centerX(), circle.centerY());
335 vm.mapPoints(&center, 1);
336 SkScalar radius = vm.mapRadius(SkScalarHalf(circle.width()));
337 SkScalar strokeWidth = vm.mapRadius(stroke.getWidth());
338
bsalomon@google.com137f1342013-05-29 21:27:53 +0000339 GrDrawState::AutoViewMatrixRestore avmr;
340 if (!avmr.setIdentity(drawState)) {
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000341 return;
342 }
343
robertphillips@google.com42903302013-04-20 12:26:07 +0000344 drawState->setVertexAttribs<gCircleVertexAttribs>(SK_ARRAY_COUNT(gCircleVertexAttribs));
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000345 GrAssert(sizeof(CircleVertex) == drawState->getVertexSize());
346
347 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
348 if (!geo.succeeded()) {
349 GrPrintf("Failed to get space for vertices!\n");
350 return;
351 }
352
353 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
354
355 SkStrokeRec::Style style = stroke.getStyle();
356 bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style);
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000357
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000358 GrEffectRef* effect = CircleEdgeEffect::Create(isStroked);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000359 static const int kCircleEdgeAttrIndex = 1;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000360 drawState->addCoverageEffect(effect, kCircleEdgeAttrIndex)->unref();
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000361
362 SkScalar innerRadius = 0.0f;
363 SkScalar outerRadius = radius;
364 SkScalar halfWidth = 0;
365 if (style != SkStrokeRec::kFill_Style) {
366 if (SkScalarNearlyZero(strokeWidth)) {
367 halfWidth = SK_ScalarHalf;
368 } else {
369 halfWidth = SkScalarHalf(strokeWidth);
370 }
371
372 outerRadius += halfWidth;
373 if (isStroked) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000374 innerRadius = radius - halfWidth;
375 isStroked = (innerRadius > 0);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000376 }
377 }
378
bsalomon@google.com58e30fe2013-04-01 19:01:20 +0000379 // The radii are outset for two reasons. First, it allows the shader to simply perform
380 // clamp(distance-to-center - radius, 0, 1). Second, the outer radius is used to compute the
381 // verts of the bounding box that is rendered and the outset ensures the box will cover all
382 // pixels partially covered by the circle.
383 outerRadius += SK_ScalarHalf;
384 innerRadius -= SK_ScalarHalf;
385
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000386 SkRect bounds = SkRect::MakeLTRB(
bsalomon@google.com58e30fe2013-04-01 19:01:20 +0000387 center.fX - outerRadius,
388 center.fY - outerRadius,
389 center.fX + outerRadius,
390 center.fY + outerRadius
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000391 );
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000392
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000393 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000394 verts[0].fOffset = SkPoint::Make(-outerRadius, -outerRadius);
395 verts[0].fOuterRadius = outerRadius;
396 verts[0].fInnerRadius = innerRadius;
397
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000398 verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
skia.committer@gmail.com46746762013-04-12 07:01:51 +0000399 verts[1].fOffset = SkPoint::Make(outerRadius, -outerRadius);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000400 verts[1].fOuterRadius = outerRadius;
401 verts[1].fInnerRadius = innerRadius;
402
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000403 verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000404 verts[2].fOffset = SkPoint::Make(-outerRadius, outerRadius);
405 verts[2].fOuterRadius = outerRadius;
406 verts[2].fInnerRadius = innerRadius;
407
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000408 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000409 verts[3].fOffset = SkPoint::Make(outerRadius, outerRadius);
410 verts[3].fOuterRadius = outerRadius;
411 verts[3].fInnerRadius = innerRadius;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000412
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000413 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000414}
415
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000416///////////////////////////////////////////////////////////////////////////////
417
robertphillips@google.com42903302013-04-20 12:26:07 +0000418namespace {
419
420// position + edge
421extern const GrVertexAttrib gEllipseVertexAttribs[] = {
422 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
423 {kVec2f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding},
424 {kVec4f_GrVertexAttribType, 2*sizeof(GrPoint), kEffect_GrVertexAttribBinding}
425};
426
427};
428
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000429bool GrOvalRenderer::drawEllipse(GrDrawTarget* target,
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000430 bool useAA,
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000431 const GrRect& ellipse,
432 const SkStrokeRec& stroke)
433{
434 GrDrawState* drawState = target->drawState();
435#ifdef SK_DEBUG
436 {
437 // we should have checked for this previously
438 bool isAxisAlignedEllipse = drawState->getViewMatrix().rectStaysRect();
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000439 SkASSERT(useAA && isAxisAlignedEllipse);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000440 }
441#endif
442
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000443 // do any matrix crunching before we reset the draw state for device coords
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000444 const SkMatrix& vm = drawState->getViewMatrix();
445 GrPoint center = GrPoint::Make(ellipse.centerX(), ellipse.centerY());
446 vm.mapPoints(&center, 1);
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000447 SkScalar ellipseXRadius = SkScalarHalf(ellipse.width());
448 SkScalar ellipseYRadius = SkScalarHalf(ellipse.height());
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +0000449 SkScalar xRadius = SkScalarAbs(vm[SkMatrix::kMScaleX]*ellipseXRadius +
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000450 vm[SkMatrix::kMSkewY]*ellipseYRadius);
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +0000451 SkScalar yRadius = SkScalarAbs(vm[SkMatrix::kMSkewX]*ellipseXRadius +
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000452 vm[SkMatrix::kMScaleY]*ellipseYRadius);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000453
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000454 // do (potentially) anisotropic mapping of stroke
455 SkVector scaledStroke;
456 SkScalar strokeWidth = stroke.getWidth();
457 scaledStroke.fX = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMScaleX] + vm[SkMatrix::kMSkewY]));
458 scaledStroke.fY = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMSkewX] + vm[SkMatrix::kMScaleY]));
459
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000460 SkStrokeRec::Style style = stroke.getStyle();
461 bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style);
462
463 SkScalar innerXRadius = 0.0f;
464 SkScalar innerYRadius = 0.0f;
465 if (SkStrokeRec::kFill_Style != style) {
466 if (SkScalarNearlyZero(scaledStroke.length())) {
467 scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf);
468 } else {
469 scaledStroke.scale(SK_ScalarHalf);
470 }
471
472 // we only handle thick strokes for near-circular ellipses
473 if (scaledStroke.length() > SK_ScalarHalf &&
474 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
475 return false;
476 }
477
478 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
479 if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius ||
480 scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) {
481 return false;
482 }
483
484 // this is legit only if scale & translation (which should be the case at the moment)
485 if (isStroked) {
486 innerXRadius = xRadius - scaledStroke.fX;
487 innerYRadius = yRadius - scaledStroke.fY;
488 isStroked = (innerXRadius > 0 && innerYRadius > 0);
489 }
490
491 xRadius += scaledStroke.fX;
492 yRadius += scaledStroke.fY;
493 }
494
bsalomon@google.com137f1342013-05-29 21:27:53 +0000495 GrDrawState::AutoViewMatrixRestore avmr;
496 if (!avmr.setIdentity(drawState)) {
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000497 return false;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000498 }
499
robertphillips@google.com42903302013-04-20 12:26:07 +0000500 drawState->setVertexAttribs<gEllipseVertexAttribs>(SK_ARRAY_COUNT(gEllipseVertexAttribs));
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000501 GrAssert(sizeof(EllipseVertex) == drawState->getVertexSize());
502
503 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
504 if (!geo.succeeded()) {
505 GrPrintf("Failed to get space for vertices!\n");
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000506 return false;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000507 }
508
509 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices());
510
jvanverth@google.come3647412013-05-08 15:31:43 +0000511 GrEffectRef* effect = EllipseEdgeEffect::Create(isStroked);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000512
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000513 static const int kEllipseCenterAttrIndex = 1;
514 static const int kEllipseEdgeAttrIndex = 2;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000515 drawState->addCoverageEffect(effect, kEllipseCenterAttrIndex, kEllipseEdgeAttrIndex)->unref();
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000516
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000517 // Compute the reciprocals of the radii here to save time in the shader
518 SkScalar xRadRecip = SkScalarInvert(xRadius);
519 SkScalar yRadRecip = SkScalarInvert(yRadius);
520 SkScalar xInnerRadRecip = SkScalarInvert(innerXRadius);
521 SkScalar yInnerRadRecip = SkScalarInvert(innerYRadius);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000522
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000523 // We've extended the outer x radius out half a pixel to antialias.
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000524 // This will also expand the rect so all the pixels will be captured.
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000525 // TODO: Consider if we should use sqrt(2)/2 instead
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000526 xRadius += SK_ScalarHalf;
527 yRadius += SK_ScalarHalf;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000528
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000529 SkRect bounds = SkRect::MakeLTRB(
530 center.fX - xRadius,
531 center.fY - yRadius,
532 center.fX + xRadius,
533 center.fY + yRadius
534 );
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000535
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000536 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000537 verts[0].fOffset = SkPoint::Make(-xRadius, -yRadius);
538 verts[0].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
539 verts[0].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000540
541 verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000542 verts[1].fOffset = SkPoint::Make(xRadius, -yRadius);
543 verts[1].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
544 verts[1].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000545
546 verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000547 verts[2].fOffset = SkPoint::Make(-xRadius, yRadius);
548 verts[2].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
549 verts[2].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000550
551 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000552 verts[3].fOffset = SkPoint::Make(xRadius, yRadius);
553 verts[3].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
554 verts[3].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
skia.committer@gmail.com46746762013-04-12 07:01:51 +0000555
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000556 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds);
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000557
558 return true;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000559}
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000560
561///////////////////////////////////////////////////////////////////////////////
562
563static const uint16_t gRRectIndices[] = {
564 // corners
565 0, 1, 5, 0, 5, 4,
566 2, 3, 7, 2, 7, 6,
567 8, 9, 13, 8, 13, 12,
568 10, 11, 15, 10, 15, 14,
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000569
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000570 // edges
571 1, 2, 6, 1, 6, 5,
572 4, 5, 9, 4, 9, 8,
573 6, 7, 11, 6, 11, 10,
574 9, 10, 14, 9, 14, 13,
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000575
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000576 // center
577 // we place this at the end so that we can ignore these indices when rendering stroke-only
578 5, 6, 10, 5, 10, 9
579};
580
581
582GrIndexBuffer* GrOvalRenderer::rRectIndexBuffer(GrGpu* gpu) {
583 if (NULL == fRRectIndexBuffer) {
584 fRRectIndexBuffer =
585 gpu->createIndexBuffer(sizeof(gRRectIndices), false);
586 if (NULL != fRRectIndexBuffer) {
587#if GR_DEBUG
588 bool updated =
589#endif
590 fRRectIndexBuffer->updateData(gRRectIndices,
591 sizeof(gRRectIndices));
592 GR_DEBUGASSERT(updated);
593 }
594 }
595 return fRRectIndexBuffer;
596}
597
skia.committer@gmail.com2fd42c42013-05-03 07:01:00 +0000598bool GrOvalRenderer::drawSimpleRRect(GrDrawTarget* target, GrContext* context, bool useAA,
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000599 const SkRRect& rrect, const SkStrokeRec& stroke)
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000600{
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000601 // only anti-aliased rrects for now
602 if (!useAA) {
603 return false;
604 }
605
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000606 const SkMatrix& vm = context->getMatrix();
607#ifdef SK_DEBUG
608 {
609 // we should have checked for this previously
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000610 SkASSERT(useAA && vm.rectStaysRect() && rrect.isSimple());
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000611 }
612#endif
613
614 // do any matrix crunching before we reset the draw state for device coords
615 const SkRect& rrectBounds = rrect.getBounds();
616 SkRect bounds;
617 vm.mapRect(&bounds, rrectBounds);
618
619 SkVector radii = rrect.getSimpleRadii();
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000620 SkScalar xRadius = SkScalarAbs(vm[SkMatrix::kMScaleX]*radii.fX +
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000621 vm[SkMatrix::kMSkewY]*radii.fY);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000622 SkScalar yRadius = SkScalarAbs(vm[SkMatrix::kMSkewX]*radii.fX +
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000623 vm[SkMatrix::kMScaleY]*radii.fY);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000624
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000625 // if hairline stroke is greater than radius, we don't handle that right now
626 SkStrokeRec::Style style = stroke.getStyle();
627 if (SkStrokeRec::kHairline_Style == style &&
628 (SK_ScalarHalf >= xRadius || SK_ScalarHalf >= yRadius)) {
629 return false;
630 }
631
632 // do (potentially) anisotropic mapping of stroke
633 SkVector scaledStroke;
634 SkScalar strokeWidth = stroke.getWidth();
635 scaledStroke.fX = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMScaleX] + vm[SkMatrix::kMSkewY]));
636 scaledStroke.fY = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMSkewX] + vm[SkMatrix::kMScaleY]));
637
638 // if half of strokewidth is greater than radius, we don't handle that right now
639 if (SK_ScalarHalf*scaledStroke.fX >= xRadius || SK_ScalarHalf*scaledStroke.fY >= yRadius) {
640 return false;
641 }
642
643 // reset to device coordinates
644 GrDrawState* drawState = target->drawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000645 GrDrawState::AutoViewMatrixRestore avmr;
646 if (!avmr.setIdentity(drawState)) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000647 return false;
648 }
649
650 bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style);
651
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000652 GrIndexBuffer* indexBuffer = this->rRectIndexBuffer(context->getGpu());
653 if (NULL == indexBuffer) {
654 GrPrintf("Failed to create index buffer!\n");
655 return false;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000656 }
657
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000658 // if the corners are circles, use the circle renderer
659 if ((!isStroked || scaledStroke.fX == scaledStroke.fY) && xRadius == yRadius) {
660 drawState->setVertexAttribs<gCircleVertexAttribs>(SK_ARRAY_COUNT(gCircleVertexAttribs));
661 GrAssert(sizeof(CircleVertex) == drawState->getVertexSize());
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000662
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000663 GrDrawTarget::AutoReleaseGeometry geo(target, 16, 0);
664 if (!geo.succeeded()) {
665 GrPrintf("Failed to get space for vertices!\n");
666 return false;
667 }
668 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000669
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000670 SkScalar innerRadius = 0.0f;
671 SkScalar outerRadius = xRadius;
672 SkScalar halfWidth = 0;
673 if (style != SkStrokeRec::kFill_Style) {
674 if (SkScalarNearlyZero(scaledStroke.fX)) {
675 halfWidth = SK_ScalarHalf;
676 } else {
677 halfWidth = SkScalarHalf(scaledStroke.fX);
678 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000679
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000680 if (isStroked) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000681 innerRadius = xRadius - halfWidth;
682 isStroked = (innerRadius > 0);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000683 }
684 outerRadius += halfWidth;
685 bounds.outset(halfWidth, halfWidth);
686 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000687
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000688 GrEffectRef* effect = CircleEdgeEffect::Create(isStroked);
689 static const int kCircleEdgeAttrIndex = 1;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000690 drawState->addCoverageEffect(effect, kCircleEdgeAttrIndex)->unref();
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +0000691
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000692 // The radii are outset for two reasons. First, it allows the shader to simply perform
693 // clamp(distance-to-center - radius, 0, 1). Second, the outer radius is used to compute the
694 // verts of the bounding box that is rendered and the outset ensures the box will cover all
695 // pixels partially covered by the circle.
696 outerRadius += SK_ScalarHalf;
697 innerRadius -= SK_ScalarHalf;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000698
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000699 // Expand the rect so all the pixels will be captured.
700 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000701
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000702 SkScalar yCoords[4] = {
703 bounds.fTop,
704 bounds.fTop + outerRadius,
705 bounds.fBottom - outerRadius,
706 bounds.fBottom
707 };
708 SkScalar yOuterRadii[4] = {
709 -outerRadius,
710 0,
711 0,
712 outerRadius
713 };
714 for (int i = 0; i < 4; ++i) {
715 verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]);
716 verts->fOffset = SkPoint::Make(-outerRadius, yOuterRadii[i]);
717 verts->fOuterRadius = outerRadius;
718 verts->fInnerRadius = innerRadius;
719 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000720
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000721 verts->fPos = SkPoint::Make(bounds.fLeft + outerRadius, yCoords[i]);
722 verts->fOffset = SkPoint::Make(0, yOuterRadii[i]);
723 verts->fOuterRadius = outerRadius;
724 verts->fInnerRadius = innerRadius;
725 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000726
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000727 verts->fPos = SkPoint::Make(bounds.fRight - outerRadius, yCoords[i]);
728 verts->fOffset = SkPoint::Make(0, yOuterRadii[i]);
729 verts->fOuterRadius = outerRadius;
730 verts->fInnerRadius = innerRadius;
731 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000732
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000733 verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]);
734 verts->fOffset = SkPoint::Make(outerRadius, yOuterRadii[i]);
735 verts->fOuterRadius = outerRadius;
736 verts->fInnerRadius = innerRadius;
737 verts++;
738 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000739
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000740 // drop out the middle quad if we're stroked
741 int indexCnt = isStroked ? GR_ARRAY_COUNT(gRRectIndices)-6 : GR_ARRAY_COUNT(gRRectIndices);
742 target->setIndexSourceToBuffer(indexBuffer);
743 target->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, 16, indexCnt, &bounds);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000744
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000745 // otherwise we use the ellipse renderer
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000746 } else {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000747 drawState->setVertexAttribs<gEllipseVertexAttribs>(SK_ARRAY_COUNT(gEllipseVertexAttribs));
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000748 GrAssert(sizeof(EllipseVertex) == drawState->getVertexSize());
749
750 SkScalar innerXRadius = 0.0f;
751 SkScalar innerYRadius = 0.0f;
752 if (SkStrokeRec::kFill_Style != style) {
753 if (SkScalarNearlyZero(scaledStroke.length())) {
754 scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf);
755 } else {
756 scaledStroke.scale(SK_ScalarHalf);
757 }
758
759 // we only handle thick strokes for near-circular ellipses
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +0000760 if (scaledStroke.length() > SK_ScalarHalf &&
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000761 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
762 return false;
763 }
764
765 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
766 if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius ||
767 scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) {
768 return false;
769 }
770
771 // this is legit only if scale & translation (which should be the case at the moment)
772 if (isStroked) {
773 innerXRadius = xRadius - scaledStroke.fX;
774 innerYRadius = yRadius - scaledStroke.fY;
775 isStroked = (innerXRadius > 0 && innerYRadius > 0);
776 }
777
778 xRadius += scaledStroke.fX;
779 yRadius += scaledStroke.fY;
780 bounds.outset(scaledStroke.fX, scaledStroke.fY);
781 }
jvanverth@google.come3647412013-05-08 15:31:43 +0000782
783 GrDrawTarget::AutoReleaseGeometry geo(target, 16, 0);
784 if (!geo.succeeded()) {
785 GrPrintf("Failed to get space for vertices!\n");
786 return false;
787 }
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000788 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices());
jvanverth@google.come3647412013-05-08 15:31:43 +0000789
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000790 GrEffectRef* effect = EllipseEdgeEffect::Create(isStroked);
jvanverth@google.come3647412013-05-08 15:31:43 +0000791 static const int kEllipseOffsetAttrIndex = 1;
792 static const int kEllipseRadiiAttrIndex = 2;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000793 drawState->addCoverageEffect(effect,
794 kEllipseOffsetAttrIndex, kEllipseRadiiAttrIndex)->unref();
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000795
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000796 // Compute the reciprocals of the radii here to save time in the shader
797 SkScalar xRadRecip = SkScalarInvert(xRadius);
798 SkScalar yRadRecip = SkScalarInvert(yRadius);
799 SkScalar xInnerRadRecip = SkScalarInvert(innerXRadius);
800 SkScalar yInnerRadRecip = SkScalarInvert(innerYRadius);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000801
802 // Extend the radii out half a pixel to antialias.
803 SkScalar xOuterRadius = xRadius + SK_ScalarHalf;
804 SkScalar yOuterRadius = yRadius + SK_ScalarHalf;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000805
806 // Expand the rect so all the pixels will be captured.
807 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
808
809 SkScalar yCoords[4] = {
810 bounds.fTop,
811 bounds.fTop + yOuterRadius,
812 bounds.fBottom - yOuterRadius,
813 bounds.fBottom
814 };
815 SkScalar yOuterOffsets[4] = {
jvanverth@google.comd1b5b142013-07-02 14:46:03 +0000816 yOuterRadius,
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000817 SK_ScalarNearlyZero, // we're using inversesqrt() in the shader, so can't be exactly 0
818 SK_ScalarNearlyZero,
819 yOuterRadius
820 };
821
822 for (int i = 0; i < 4; ++i) {
823 verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]);
jvanverth@google.comd1b5b142013-07-02 14:46:03 +0000824 verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000825 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
826 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000827 verts++;
828
829 verts->fPos = SkPoint::Make(bounds.fLeft + xOuterRadius, yCoords[i]);
830 verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000831 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
832 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000833 verts++;
834
835 verts->fPos = SkPoint::Make(bounds.fRight - xOuterRadius, yCoords[i]);
836 verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000837 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
838 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000839 verts++;
840
841 verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]);
842 verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000843 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
844 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000845 verts++;
846 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000847
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000848 // drop out the middle quad if we're stroked
849 int indexCnt = isStroked ? GR_ARRAY_COUNT(gRRectIndices)-6 : GR_ARRAY_COUNT(gRRectIndices);
850 target->setIndexSourceToBuffer(indexBuffer);
851 target->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, 16, indexCnt, &bounds);
852 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000853
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000854 return true;
855}