blob: 5ec750457798af3f38cabb2f498a457a3499b830 [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.orgef284a82013-07-11 22:29:29 +0000289void GrOvalRenderer::reset() {
290 GrSafeSetNull(fRRectIndexBuffer);
291}
292
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000293bool GrOvalRenderer::drawOval(GrDrawTarget* target, const GrContext* context, bool useAA,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000294 const SkRect& oval, const SkStrokeRec& stroke)
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000295{
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000296 if (!useAA) {
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000297 return false;
298 }
299
300 const SkMatrix& vm = context->getMatrix();
301
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000302 // we can draw circles
303 if (SkScalarNearlyEqual(oval.width(), oval.height())
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000304 && circle_stays_circle(vm)) {
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000305 this->drawCircle(target, useAA, oval, stroke);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000306
307 // and axis-aligned ellipses only
308 } else if (vm.rectStaysRect()) {
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000309 return this->drawEllipse(target, useAA, oval, stroke);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000310
311 } else {
312 return false;
313 }
314
315 return true;
316}
317
robertphillips@google.com42903302013-04-20 12:26:07 +0000318namespace {
319
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000320///////////////////////////////////////////////////////////////////////////////
321
robertphillips@google.com42903302013-04-20 12:26:07 +0000322// position + edge
323extern const GrVertexAttrib gCircleVertexAttribs[] = {
324 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
325 {kVec4f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding}
326};
327
328};
329
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000330void GrOvalRenderer::drawCircle(GrDrawTarget* target,
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000331 bool useAA,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000332 const SkRect& circle,
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000333 const SkStrokeRec& stroke)
334{
335 GrDrawState* drawState = target->drawState();
336
337 const SkMatrix& vm = drawState->getViewMatrix();
338 GrPoint center = GrPoint::Make(circle.centerX(), circle.centerY());
339 vm.mapPoints(&center, 1);
340 SkScalar radius = vm.mapRadius(SkScalarHalf(circle.width()));
341 SkScalar strokeWidth = vm.mapRadius(stroke.getWidth());
342
bsalomon@google.com137f1342013-05-29 21:27:53 +0000343 GrDrawState::AutoViewMatrixRestore avmr;
344 if (!avmr.setIdentity(drawState)) {
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000345 return;
346 }
347
robertphillips@google.com42903302013-04-20 12:26:07 +0000348 drawState->setVertexAttribs<gCircleVertexAttribs>(SK_ARRAY_COUNT(gCircleVertexAttribs));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000349 SkASSERT(sizeof(CircleVertex) == drawState->getVertexSize());
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000350
351 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
352 if (!geo.succeeded()) {
353 GrPrintf("Failed to get space for vertices!\n");
354 return;
355 }
356
357 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
358
359 SkStrokeRec::Style style = stroke.getStyle();
360 bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style);
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000361
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000362 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;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000375 }
376 }
377
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +0000378 GrEffectRef* effect = CircleEdgeEffect::Create(isStroked && innerRadius > 0);
379 static const int kCircleEdgeAttrIndex = 1;
380 drawState->addCoverageEffect(effect, kCircleEdgeAttrIndex)->unref();
381
bsalomon@google.com58e30fe2013-04-01 19:01:20 +0000382 // The radii are outset for two reasons. First, it allows the shader to simply perform
383 // clamp(distance-to-center - radius, 0, 1). Second, the outer radius is used to compute the
384 // verts of the bounding box that is rendered and the outset ensures the box will cover all
385 // pixels partially covered by the circle.
386 outerRadius += SK_ScalarHalf;
387 innerRadius -= SK_ScalarHalf;
388
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000389 SkRect bounds = SkRect::MakeLTRB(
bsalomon@google.com58e30fe2013-04-01 19:01:20 +0000390 center.fX - outerRadius,
391 center.fY - outerRadius,
392 center.fX + outerRadius,
393 center.fY + outerRadius
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000394 );
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000395
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000396 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000397 verts[0].fOffset = SkPoint::Make(-outerRadius, -outerRadius);
398 verts[0].fOuterRadius = outerRadius;
399 verts[0].fInnerRadius = innerRadius;
400
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000401 verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
skia.committer@gmail.com46746762013-04-12 07:01:51 +0000402 verts[1].fOffset = SkPoint::Make(outerRadius, -outerRadius);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000403 verts[1].fOuterRadius = outerRadius;
404 verts[1].fInnerRadius = innerRadius;
405
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000406 verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000407 verts[2].fOffset = SkPoint::Make(-outerRadius, outerRadius);
408 verts[2].fOuterRadius = outerRadius;
409 verts[2].fInnerRadius = innerRadius;
410
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000411 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000412 verts[3].fOffset = SkPoint::Make(outerRadius, outerRadius);
413 verts[3].fOuterRadius = outerRadius;
414 verts[3].fInnerRadius = innerRadius;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000415
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000416 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000417}
418
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000419///////////////////////////////////////////////////////////////////////////////
420
robertphillips@google.com42903302013-04-20 12:26:07 +0000421namespace {
422
423// position + edge
424extern const GrVertexAttrib gEllipseVertexAttribs[] = {
425 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
426 {kVec2f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding},
427 {kVec4f_GrVertexAttribType, 2*sizeof(GrPoint), kEffect_GrVertexAttribBinding}
428};
429
430};
431
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000432bool GrOvalRenderer::drawEllipse(GrDrawTarget* target,
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000433 bool useAA,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000434 const SkRect& ellipse,
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000435 const SkStrokeRec& stroke)
436{
437 GrDrawState* drawState = target->drawState();
438#ifdef SK_DEBUG
439 {
440 // we should have checked for this previously
441 bool isAxisAlignedEllipse = drawState->getViewMatrix().rectStaysRect();
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000442 SkASSERT(useAA && isAxisAlignedEllipse);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000443 }
444#endif
445
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000446 // do any matrix crunching before we reset the draw state for device coords
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000447 const SkMatrix& vm = drawState->getViewMatrix();
448 GrPoint center = GrPoint::Make(ellipse.centerX(), ellipse.centerY());
449 vm.mapPoints(&center, 1);
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000450 SkScalar ellipseXRadius = SkScalarHalf(ellipse.width());
451 SkScalar ellipseYRadius = SkScalarHalf(ellipse.height());
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +0000452 SkScalar xRadius = SkScalarAbs(vm[SkMatrix::kMScaleX]*ellipseXRadius +
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000453 vm[SkMatrix::kMSkewY]*ellipseYRadius);
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +0000454 SkScalar yRadius = SkScalarAbs(vm[SkMatrix::kMSkewX]*ellipseXRadius +
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000455 vm[SkMatrix::kMScaleY]*ellipseYRadius);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000456
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000457 // do (potentially) anisotropic mapping of stroke
458 SkVector scaledStroke;
459 SkScalar strokeWidth = stroke.getWidth();
460 scaledStroke.fX = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMScaleX] + vm[SkMatrix::kMSkewY]));
461 scaledStroke.fY = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMSkewX] + vm[SkMatrix::kMScaleY]));
462
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000463 SkStrokeRec::Style style = stroke.getStyle();
464 bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style);
465
466 SkScalar innerXRadius = 0.0f;
467 SkScalar innerYRadius = 0.0f;
468 if (SkStrokeRec::kFill_Style != style) {
469 if (SkScalarNearlyZero(scaledStroke.length())) {
470 scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf);
471 } else {
472 scaledStroke.scale(SK_ScalarHalf);
473 }
474
475 // we only handle thick strokes for near-circular ellipses
476 if (scaledStroke.length() > SK_ScalarHalf &&
477 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
478 return false;
479 }
480
481 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
482 if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius ||
483 scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) {
484 return false;
485 }
486
487 // this is legit only if scale & translation (which should be the case at the moment)
488 if (isStroked) {
489 innerXRadius = xRadius - scaledStroke.fX;
490 innerYRadius = yRadius - scaledStroke.fY;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000491 }
492
493 xRadius += scaledStroke.fX;
494 yRadius += scaledStroke.fY;
495 }
496
bsalomon@google.com137f1342013-05-29 21:27:53 +0000497 GrDrawState::AutoViewMatrixRestore avmr;
498 if (!avmr.setIdentity(drawState)) {
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000499 return false;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000500 }
501
robertphillips@google.com42903302013-04-20 12:26:07 +0000502 drawState->setVertexAttribs<gEllipseVertexAttribs>(SK_ARRAY_COUNT(gEllipseVertexAttribs));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000503 SkASSERT(sizeof(EllipseVertex) == drawState->getVertexSize());
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000504
505 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
506 if (!geo.succeeded()) {
507 GrPrintf("Failed to get space for vertices!\n");
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000508 return false;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000509 }
510
511 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices());
512
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +0000513 GrEffectRef* effect = EllipseEdgeEffect::Create(isStroked &&
514 innerXRadius > 0 && innerYRadius > 0);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000515
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000516 static const int kEllipseCenterAttrIndex = 1;
517 static const int kEllipseEdgeAttrIndex = 2;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000518 drawState->addCoverageEffect(effect, kEllipseCenterAttrIndex, kEllipseEdgeAttrIndex)->unref();
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000519
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000520 // Compute the reciprocals of the radii here to save time in the shader
521 SkScalar xRadRecip = SkScalarInvert(xRadius);
522 SkScalar yRadRecip = SkScalarInvert(yRadius);
523 SkScalar xInnerRadRecip = SkScalarInvert(innerXRadius);
524 SkScalar yInnerRadRecip = SkScalarInvert(innerYRadius);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000525
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000526 // We've extended the outer x radius out half a pixel to antialias.
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000527 // This will also expand the rect so all the pixels will be captured.
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000528 // TODO: Consider if we should use sqrt(2)/2 instead
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000529 xRadius += SK_ScalarHalf;
530 yRadius += SK_ScalarHalf;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000531
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000532 SkRect bounds = SkRect::MakeLTRB(
533 center.fX - xRadius,
534 center.fY - yRadius,
535 center.fX + xRadius,
536 center.fY + yRadius
537 );
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000538
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000539 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000540 verts[0].fOffset = SkPoint::Make(-xRadius, -yRadius);
541 verts[0].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
542 verts[0].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000543
544 verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000545 verts[1].fOffset = SkPoint::Make(xRadius, -yRadius);
546 verts[1].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
547 verts[1].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000548
549 verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000550 verts[2].fOffset = SkPoint::Make(-xRadius, yRadius);
551 verts[2].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
552 verts[2].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000553
554 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000555 verts[3].fOffset = SkPoint::Make(xRadius, yRadius);
556 verts[3].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
557 verts[3].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
skia.committer@gmail.com46746762013-04-12 07:01:51 +0000558
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000559 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds);
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000560
561 return true;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000562}
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000563
564///////////////////////////////////////////////////////////////////////////////
565
566static const uint16_t gRRectIndices[] = {
567 // corners
568 0, 1, 5, 0, 5, 4,
569 2, 3, 7, 2, 7, 6,
570 8, 9, 13, 8, 13, 12,
571 10, 11, 15, 10, 15, 14,
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000572
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000573 // edges
574 1, 2, 6, 1, 6, 5,
575 4, 5, 9, 4, 9, 8,
576 6, 7, 11, 6, 11, 10,
577 9, 10, 14, 9, 14, 13,
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000578
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000579 // center
580 // we place this at the end so that we can ignore these indices when rendering stroke-only
581 5, 6, 10, 5, 10, 9
582};
583
584
585GrIndexBuffer* GrOvalRenderer::rRectIndexBuffer(GrGpu* gpu) {
586 if (NULL == fRRectIndexBuffer) {
587 fRRectIndexBuffer =
588 gpu->createIndexBuffer(sizeof(gRRectIndices), false);
589 if (NULL != fRRectIndexBuffer) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000590#ifdef SK_DEBUG
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000591 bool updated =
592#endif
593 fRRectIndexBuffer->updateData(gRRectIndices,
594 sizeof(gRRectIndices));
595 GR_DEBUGASSERT(updated);
596 }
597 }
598 return fRRectIndexBuffer;
599}
600
skia.committer@gmail.com2fd42c42013-05-03 07:01:00 +0000601bool GrOvalRenderer::drawSimpleRRect(GrDrawTarget* target, GrContext* context, bool useAA,
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000602 const SkRRect& rrect, const SkStrokeRec& stroke)
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000603{
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000604 // only anti-aliased rrects for now
605 if (!useAA) {
606 return false;
607 }
608
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000609 const SkMatrix& vm = context->getMatrix();
610#ifdef SK_DEBUG
611 {
612 // we should have checked for this previously
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000613 SkASSERT(useAA && vm.rectStaysRect() && rrect.isSimple());
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000614 }
615#endif
616
617 // do any matrix crunching before we reset the draw state for device coords
618 const SkRect& rrectBounds = rrect.getBounds();
619 SkRect bounds;
620 vm.mapRect(&bounds, rrectBounds);
621
622 SkVector radii = rrect.getSimpleRadii();
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000623 SkScalar xRadius = SkScalarAbs(vm[SkMatrix::kMScaleX]*radii.fX +
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000624 vm[SkMatrix::kMSkewY]*radii.fY);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000625 SkScalar yRadius = SkScalarAbs(vm[SkMatrix::kMSkewX]*radii.fX +
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000626 vm[SkMatrix::kMScaleY]*radii.fY);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000627
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000628 // if hairline stroke is greater than radius, we don't handle that right now
629 SkStrokeRec::Style style = stroke.getStyle();
630 if (SkStrokeRec::kHairline_Style == style &&
631 (SK_ScalarHalf >= xRadius || SK_ScalarHalf >= yRadius)) {
632 return false;
633 }
634
635 // do (potentially) anisotropic mapping of stroke
636 SkVector scaledStroke;
637 SkScalar strokeWidth = stroke.getWidth();
638 scaledStroke.fX = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMScaleX] + vm[SkMatrix::kMSkewY]));
639 scaledStroke.fY = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMSkewX] + vm[SkMatrix::kMScaleY]));
640
641 // if half of strokewidth is greater than radius, we don't handle that right now
642 if (SK_ScalarHalf*scaledStroke.fX >= xRadius || SK_ScalarHalf*scaledStroke.fY >= yRadius) {
643 return false;
644 }
645
646 // reset to device coordinates
647 GrDrawState* drawState = target->drawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000648 GrDrawState::AutoViewMatrixRestore avmr;
649 if (!avmr.setIdentity(drawState)) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000650 return false;
651 }
652
653 bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style);
654
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000655 GrIndexBuffer* indexBuffer = this->rRectIndexBuffer(context->getGpu());
656 if (NULL == indexBuffer) {
657 GrPrintf("Failed to create index buffer!\n");
658 return false;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000659 }
660
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000661 // if the corners are circles, use the circle renderer
662 if ((!isStroked || scaledStroke.fX == scaledStroke.fY) && xRadius == yRadius) {
663 drawState->setVertexAttribs<gCircleVertexAttribs>(SK_ARRAY_COUNT(gCircleVertexAttribs));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000664 SkASSERT(sizeof(CircleVertex) == drawState->getVertexSize());
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000665
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000666 GrDrawTarget::AutoReleaseGeometry geo(target, 16, 0);
667 if (!geo.succeeded()) {
668 GrPrintf("Failed to get space for vertices!\n");
669 return false;
670 }
671 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000672
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000673 SkScalar innerRadius = 0.0f;
674 SkScalar outerRadius = xRadius;
675 SkScalar halfWidth = 0;
676 if (style != SkStrokeRec::kFill_Style) {
677 if (SkScalarNearlyZero(scaledStroke.fX)) {
678 halfWidth = SK_ScalarHalf;
679 } else {
680 halfWidth = SkScalarHalf(scaledStroke.fX);
681 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000682
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000683 if (isStroked) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000684 innerRadius = xRadius - halfWidth;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000685 }
686 outerRadius += halfWidth;
687 bounds.outset(halfWidth, halfWidth);
688 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000689
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +0000690 isStroked = (isStroked && innerRadius > 0);
691
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000692 GrEffectRef* effect = CircleEdgeEffect::Create(isStroked);
693 static const int kCircleEdgeAttrIndex = 1;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000694 drawState->addCoverageEffect(effect, kCircleEdgeAttrIndex)->unref();
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +0000695
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000696 // The radii are outset for two reasons. First, it allows the shader to simply perform
697 // clamp(distance-to-center - radius, 0, 1). Second, the outer radius is used to compute the
698 // verts of the bounding box that is rendered and the outset ensures the box will cover all
699 // pixels partially covered by the circle.
700 outerRadius += SK_ScalarHalf;
701 innerRadius -= SK_ScalarHalf;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000702
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000703 // Expand the rect so all the pixels will be captured.
704 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000705
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000706 SkScalar yCoords[4] = {
707 bounds.fTop,
708 bounds.fTop + outerRadius,
709 bounds.fBottom - outerRadius,
710 bounds.fBottom
711 };
712 SkScalar yOuterRadii[4] = {
713 -outerRadius,
714 0,
715 0,
716 outerRadius
717 };
718 for (int i = 0; i < 4; ++i) {
719 verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]);
720 verts->fOffset = SkPoint::Make(-outerRadius, yOuterRadii[i]);
721 verts->fOuterRadius = outerRadius;
722 verts->fInnerRadius = innerRadius;
723 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000724
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000725 verts->fPos = SkPoint::Make(bounds.fLeft + outerRadius, yCoords[i]);
726 verts->fOffset = SkPoint::Make(0, yOuterRadii[i]);
727 verts->fOuterRadius = outerRadius;
728 verts->fInnerRadius = innerRadius;
729 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000730
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000731 verts->fPos = SkPoint::Make(bounds.fRight - outerRadius, yCoords[i]);
732 verts->fOffset = SkPoint::Make(0, yOuterRadii[i]);
733 verts->fOuterRadius = outerRadius;
734 verts->fInnerRadius = innerRadius;
735 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000736
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000737 verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]);
738 verts->fOffset = SkPoint::Make(outerRadius, yOuterRadii[i]);
739 verts->fOuterRadius = outerRadius;
740 verts->fInnerRadius = innerRadius;
741 verts++;
742 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000743
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000744 // drop out the middle quad if we're stroked
745 int indexCnt = isStroked ? GR_ARRAY_COUNT(gRRectIndices)-6 : GR_ARRAY_COUNT(gRRectIndices);
746 target->setIndexSourceToBuffer(indexBuffer);
747 target->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, 16, indexCnt, &bounds);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000748
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000749 // otherwise we use the ellipse renderer
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000750 } else {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000751 drawState->setVertexAttribs<gEllipseVertexAttribs>(SK_ARRAY_COUNT(gEllipseVertexAttribs));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000752 SkASSERT(sizeof(EllipseVertex) == drawState->getVertexSize());
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000753
754 SkScalar innerXRadius = 0.0f;
755 SkScalar innerYRadius = 0.0f;
756 if (SkStrokeRec::kFill_Style != style) {
757 if (SkScalarNearlyZero(scaledStroke.length())) {
758 scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf);
759 } else {
760 scaledStroke.scale(SK_ScalarHalf);
761 }
762
763 // we only handle thick strokes for near-circular ellipses
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +0000764 if (scaledStroke.length() > SK_ScalarHalf &&
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000765 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
766 return false;
767 }
768
769 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
770 if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius ||
771 scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) {
772 return false;
773 }
774
775 // this is legit only if scale & translation (which should be the case at the moment)
776 if (isStroked) {
777 innerXRadius = xRadius - scaledStroke.fX;
778 innerYRadius = yRadius - scaledStroke.fY;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000779 }
780
781 xRadius += scaledStroke.fX;
782 yRadius += scaledStroke.fY;
783 bounds.outset(scaledStroke.fX, scaledStroke.fY);
784 }
jvanverth@google.come3647412013-05-08 15:31:43 +0000785
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +0000786 isStroked = (isStroked && innerXRadius > 0 && innerYRadius > 0);
787
jvanverth@google.come3647412013-05-08 15:31:43 +0000788 GrDrawTarget::AutoReleaseGeometry geo(target, 16, 0);
789 if (!geo.succeeded()) {
790 GrPrintf("Failed to get space for vertices!\n");
791 return false;
792 }
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000793 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices());
jvanverth@google.come3647412013-05-08 15:31:43 +0000794
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000795 GrEffectRef* effect = EllipseEdgeEffect::Create(isStroked);
jvanverth@google.come3647412013-05-08 15:31:43 +0000796 static const int kEllipseOffsetAttrIndex = 1;
797 static const int kEllipseRadiiAttrIndex = 2;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000798 drawState->addCoverageEffect(effect,
799 kEllipseOffsetAttrIndex, kEllipseRadiiAttrIndex)->unref();
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000800
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000801 // Compute the reciprocals of the radii here to save time in the shader
802 SkScalar xRadRecip = SkScalarInvert(xRadius);
803 SkScalar yRadRecip = SkScalarInvert(yRadius);
804 SkScalar xInnerRadRecip = SkScalarInvert(innerXRadius);
805 SkScalar yInnerRadRecip = SkScalarInvert(innerYRadius);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000806
807 // Extend the radii out half a pixel to antialias.
808 SkScalar xOuterRadius = xRadius + SK_ScalarHalf;
809 SkScalar yOuterRadius = yRadius + SK_ScalarHalf;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000810
811 // Expand the rect so all the pixels will be captured.
812 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
813
814 SkScalar yCoords[4] = {
815 bounds.fTop,
816 bounds.fTop + yOuterRadius,
817 bounds.fBottom - yOuterRadius,
818 bounds.fBottom
819 };
820 SkScalar yOuterOffsets[4] = {
jvanverth@google.comd1b5b142013-07-02 14:46:03 +0000821 yOuterRadius,
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000822 SK_ScalarNearlyZero, // we're using inversesqrt() in the shader, so can't be exactly 0
823 SK_ScalarNearlyZero,
824 yOuterRadius
825 };
826
827 for (int i = 0; i < 4; ++i) {
828 verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]);
jvanverth@google.comd1b5b142013-07-02 14:46:03 +0000829 verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000830 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
831 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000832 verts++;
833
834 verts->fPos = SkPoint::Make(bounds.fLeft + xOuterRadius, yCoords[i]);
835 verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000836 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
837 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000838 verts++;
839
840 verts->fPos = SkPoint::Make(bounds.fRight - xOuterRadius, yCoords[i]);
841 verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000842 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
843 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000844 verts++;
845
846 verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]);
847 verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000848 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
849 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000850 verts++;
851 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000852
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000853 // drop out the middle quad if we're stroked
854 int indexCnt = isStroked ? GR_ARRAY_COUNT(gRRectIndices)-6 : GR_ARRAY_COUNT(gRRectIndices);
855 target->setIndexSourceToBuffer(indexBuffer);
856 target->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, 16, indexCnt, &bounds);
857 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000858
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000859 return true;
860}