blob: 6ea922c28b3322591e23cfb512bae5e1fbb088c7 [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));
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000349 GrAssert(sizeof(CircleVertex) == drawState->getVertexSize());
350
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.org90c240a2013-04-02 17:57:21 +0000362 GrEffectRef* effect = CircleEdgeEffect::Create(isStroked);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000363 static const int kCircleEdgeAttrIndex = 1;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000364 drawState->addCoverageEffect(effect, kCircleEdgeAttrIndex)->unref();
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000365
366 SkScalar innerRadius = 0.0f;
367 SkScalar outerRadius = radius;
368 SkScalar halfWidth = 0;
369 if (style != SkStrokeRec::kFill_Style) {
370 if (SkScalarNearlyZero(strokeWidth)) {
371 halfWidth = SK_ScalarHalf;
372 } else {
373 halfWidth = SkScalarHalf(strokeWidth);
374 }
375
376 outerRadius += halfWidth;
377 if (isStroked) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000378 innerRadius = radius - halfWidth;
379 isStroked = (innerRadius > 0);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000380 }
381 }
382
bsalomon@google.com58e30fe2013-04-01 19:01:20 +0000383 // The radii are outset for two reasons. First, it allows the shader to simply perform
384 // clamp(distance-to-center - radius, 0, 1). Second, the outer radius is used to compute the
385 // verts of the bounding box that is rendered and the outset ensures the box will cover all
386 // pixels partially covered by the circle.
387 outerRadius += SK_ScalarHalf;
388 innerRadius -= SK_ScalarHalf;
389
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000390 SkRect bounds = SkRect::MakeLTRB(
bsalomon@google.com58e30fe2013-04-01 19:01:20 +0000391 center.fX - outerRadius,
392 center.fY - outerRadius,
393 center.fX + outerRadius,
394 center.fY + outerRadius
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000395 );
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000396
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000397 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000398 verts[0].fOffset = SkPoint::Make(-outerRadius, -outerRadius);
399 verts[0].fOuterRadius = outerRadius;
400 verts[0].fInnerRadius = innerRadius;
401
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000402 verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
skia.committer@gmail.com46746762013-04-12 07:01:51 +0000403 verts[1].fOffset = SkPoint::Make(outerRadius, -outerRadius);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000404 verts[1].fOuterRadius = outerRadius;
405 verts[1].fInnerRadius = innerRadius;
406
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000407 verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000408 verts[2].fOffset = SkPoint::Make(-outerRadius, outerRadius);
409 verts[2].fOuterRadius = outerRadius;
410 verts[2].fInnerRadius = innerRadius;
411
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000412 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000413 verts[3].fOffset = SkPoint::Make(outerRadius, outerRadius);
414 verts[3].fOuterRadius = outerRadius;
415 verts[3].fInnerRadius = innerRadius;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000416
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000417 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000418}
419
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000420///////////////////////////////////////////////////////////////////////////////
421
robertphillips@google.com42903302013-04-20 12:26:07 +0000422namespace {
423
424// position + edge
425extern const GrVertexAttrib gEllipseVertexAttribs[] = {
426 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
427 {kVec2f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding},
428 {kVec4f_GrVertexAttribType, 2*sizeof(GrPoint), kEffect_GrVertexAttribBinding}
429};
430
431};
432
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000433bool GrOvalRenderer::drawEllipse(GrDrawTarget* target,
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000434 bool useAA,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000435 const SkRect& ellipse,
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000436 const SkStrokeRec& stroke)
437{
438 GrDrawState* drawState = target->drawState();
439#ifdef SK_DEBUG
440 {
441 // we should have checked for this previously
442 bool isAxisAlignedEllipse = drawState->getViewMatrix().rectStaysRect();
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000443 SkASSERT(useAA && isAxisAlignedEllipse);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000444 }
445#endif
446
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000447 // do any matrix crunching before we reset the draw state for device coords
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000448 const SkMatrix& vm = drawState->getViewMatrix();
449 GrPoint center = GrPoint::Make(ellipse.centerX(), ellipse.centerY());
450 vm.mapPoints(&center, 1);
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000451 SkScalar ellipseXRadius = SkScalarHalf(ellipse.width());
452 SkScalar ellipseYRadius = SkScalarHalf(ellipse.height());
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +0000453 SkScalar xRadius = SkScalarAbs(vm[SkMatrix::kMScaleX]*ellipseXRadius +
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000454 vm[SkMatrix::kMSkewY]*ellipseYRadius);
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +0000455 SkScalar yRadius = SkScalarAbs(vm[SkMatrix::kMSkewX]*ellipseXRadius +
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000456 vm[SkMatrix::kMScaleY]*ellipseYRadius);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000457
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000458 // do (potentially) anisotropic mapping of stroke
459 SkVector scaledStroke;
460 SkScalar strokeWidth = stroke.getWidth();
461 scaledStroke.fX = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMScaleX] + vm[SkMatrix::kMSkewY]));
462 scaledStroke.fY = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMSkewX] + vm[SkMatrix::kMScaleY]));
463
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000464 SkStrokeRec::Style style = stroke.getStyle();
465 bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style);
466
467 SkScalar innerXRadius = 0.0f;
468 SkScalar innerYRadius = 0.0f;
469 if (SkStrokeRec::kFill_Style != style) {
470 if (SkScalarNearlyZero(scaledStroke.length())) {
471 scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf);
472 } else {
473 scaledStroke.scale(SK_ScalarHalf);
474 }
475
476 // we only handle thick strokes for near-circular ellipses
477 if (scaledStroke.length() > SK_ScalarHalf &&
478 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
479 return false;
480 }
481
482 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
483 if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius ||
484 scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) {
485 return false;
486 }
487
488 // this is legit only if scale & translation (which should be the case at the moment)
489 if (isStroked) {
490 innerXRadius = xRadius - scaledStroke.fX;
491 innerYRadius = yRadius - scaledStroke.fY;
492 isStroked = (innerXRadius > 0 && innerYRadius > 0);
493 }
494
495 xRadius += scaledStroke.fX;
496 yRadius += scaledStroke.fY;
497 }
498
bsalomon@google.com137f1342013-05-29 21:27:53 +0000499 GrDrawState::AutoViewMatrixRestore avmr;
500 if (!avmr.setIdentity(drawState)) {
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000501 return false;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000502 }
503
robertphillips@google.com42903302013-04-20 12:26:07 +0000504 drawState->setVertexAttribs<gEllipseVertexAttribs>(SK_ARRAY_COUNT(gEllipseVertexAttribs));
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000505 GrAssert(sizeof(EllipseVertex) == drawState->getVertexSize());
506
507 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
508 if (!geo.succeeded()) {
509 GrPrintf("Failed to get space for vertices!\n");
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000510 return false;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000511 }
512
513 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices());
514
jvanverth@google.come3647412013-05-08 15:31:43 +0000515 GrEffectRef* effect = EllipseEdgeEffect::Create(isStroked);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000516
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000517 static const int kEllipseCenterAttrIndex = 1;
518 static const int kEllipseEdgeAttrIndex = 2;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000519 drawState->addCoverageEffect(effect, kEllipseCenterAttrIndex, kEllipseEdgeAttrIndex)->unref();
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000520
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000521 // Compute the reciprocals of the radii here to save time in the shader
522 SkScalar xRadRecip = SkScalarInvert(xRadius);
523 SkScalar yRadRecip = SkScalarInvert(yRadius);
524 SkScalar xInnerRadRecip = SkScalarInvert(innerXRadius);
525 SkScalar yInnerRadRecip = SkScalarInvert(innerYRadius);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000526
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000527 // We've extended the outer x radius out half a pixel to antialias.
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000528 // This will also expand the rect so all the pixels will be captured.
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000529 // TODO: Consider if we should use sqrt(2)/2 instead
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000530 xRadius += SK_ScalarHalf;
531 yRadius += SK_ScalarHalf;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000532
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000533 SkRect bounds = SkRect::MakeLTRB(
534 center.fX - xRadius,
535 center.fY - yRadius,
536 center.fX + xRadius,
537 center.fY + yRadius
538 );
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000539
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000540 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000541 verts[0].fOffset = SkPoint::Make(-xRadius, -yRadius);
542 verts[0].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
543 verts[0].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000544
545 verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000546 verts[1].fOffset = SkPoint::Make(xRadius, -yRadius);
547 verts[1].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
548 verts[1].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000549
550 verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000551 verts[2].fOffset = SkPoint::Make(-xRadius, yRadius);
552 verts[2].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
553 verts[2].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000554
555 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000556 verts[3].fOffset = SkPoint::Make(xRadius, yRadius);
557 verts[3].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
558 verts[3].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
skia.committer@gmail.com46746762013-04-12 07:01:51 +0000559
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000560 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds);
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000561
562 return true;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000563}
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000564
565///////////////////////////////////////////////////////////////////////////////
566
567static const uint16_t gRRectIndices[] = {
568 // corners
569 0, 1, 5, 0, 5, 4,
570 2, 3, 7, 2, 7, 6,
571 8, 9, 13, 8, 13, 12,
572 10, 11, 15, 10, 15, 14,
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000573
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000574 // edges
575 1, 2, 6, 1, 6, 5,
576 4, 5, 9, 4, 9, 8,
577 6, 7, 11, 6, 11, 10,
578 9, 10, 14, 9, 14, 13,
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000579
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000580 // center
581 // we place this at the end so that we can ignore these indices when rendering stroke-only
582 5, 6, 10, 5, 10, 9
583};
584
585
586GrIndexBuffer* GrOvalRenderer::rRectIndexBuffer(GrGpu* gpu) {
587 if (NULL == fRRectIndexBuffer) {
588 fRRectIndexBuffer =
589 gpu->createIndexBuffer(sizeof(gRRectIndices), false);
590 if (NULL != fRRectIndexBuffer) {
591#if GR_DEBUG
592 bool updated =
593#endif
594 fRRectIndexBuffer->updateData(gRRectIndices,
595 sizeof(gRRectIndices));
596 GR_DEBUGASSERT(updated);
597 }
598 }
599 return fRRectIndexBuffer;
600}
601
skia.committer@gmail.com2fd42c42013-05-03 07:01:00 +0000602bool GrOvalRenderer::drawSimpleRRect(GrDrawTarget* target, GrContext* context, bool useAA,
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000603 const SkRRect& rrect, const SkStrokeRec& stroke)
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000604{
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000605 // only anti-aliased rrects for now
606 if (!useAA) {
607 return false;
608 }
609
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000610 const SkMatrix& vm = context->getMatrix();
611#ifdef SK_DEBUG
612 {
613 // we should have checked for this previously
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000614 SkASSERT(useAA && vm.rectStaysRect() && rrect.isSimple());
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000615 }
616#endif
617
618 // do any matrix crunching before we reset the draw state for device coords
619 const SkRect& rrectBounds = rrect.getBounds();
620 SkRect bounds;
621 vm.mapRect(&bounds, rrectBounds);
622
623 SkVector radii = rrect.getSimpleRadii();
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000624 SkScalar xRadius = SkScalarAbs(vm[SkMatrix::kMScaleX]*radii.fX +
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000625 vm[SkMatrix::kMSkewY]*radii.fY);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000626 SkScalar yRadius = SkScalarAbs(vm[SkMatrix::kMSkewX]*radii.fX +
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000627 vm[SkMatrix::kMScaleY]*radii.fY);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000628
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000629 // if hairline stroke is greater than radius, we don't handle that right now
630 SkStrokeRec::Style style = stroke.getStyle();
631 if (SkStrokeRec::kHairline_Style == style &&
632 (SK_ScalarHalf >= xRadius || SK_ScalarHalf >= yRadius)) {
633 return false;
634 }
635
636 // do (potentially) anisotropic mapping of stroke
637 SkVector scaledStroke;
638 SkScalar strokeWidth = stroke.getWidth();
639 scaledStroke.fX = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMScaleX] + vm[SkMatrix::kMSkewY]));
640 scaledStroke.fY = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMSkewX] + vm[SkMatrix::kMScaleY]));
641
642 // if half of strokewidth is greater than radius, we don't handle that right now
643 if (SK_ScalarHalf*scaledStroke.fX >= xRadius || SK_ScalarHalf*scaledStroke.fY >= yRadius) {
644 return false;
645 }
646
647 // reset to device coordinates
648 GrDrawState* drawState = target->drawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000649 GrDrawState::AutoViewMatrixRestore avmr;
650 if (!avmr.setIdentity(drawState)) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000651 return false;
652 }
653
654 bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style);
655
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000656 GrIndexBuffer* indexBuffer = this->rRectIndexBuffer(context->getGpu());
657 if (NULL == indexBuffer) {
658 GrPrintf("Failed to create index buffer!\n");
659 return false;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000660 }
661
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000662 // if the corners are circles, use the circle renderer
663 if ((!isStroked || scaledStroke.fX == scaledStroke.fY) && xRadius == yRadius) {
664 drawState->setVertexAttribs<gCircleVertexAttribs>(SK_ARRAY_COUNT(gCircleVertexAttribs));
665 GrAssert(sizeof(CircleVertex) == drawState->getVertexSize());
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000666
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000667 GrDrawTarget::AutoReleaseGeometry geo(target, 16, 0);
668 if (!geo.succeeded()) {
669 GrPrintf("Failed to get space for vertices!\n");
670 return false;
671 }
672 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000673
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000674 SkScalar innerRadius = 0.0f;
675 SkScalar outerRadius = xRadius;
676 SkScalar halfWidth = 0;
677 if (style != SkStrokeRec::kFill_Style) {
678 if (SkScalarNearlyZero(scaledStroke.fX)) {
679 halfWidth = SK_ScalarHalf;
680 } else {
681 halfWidth = SkScalarHalf(scaledStroke.fX);
682 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000683
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000684 if (isStroked) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000685 innerRadius = xRadius - halfWidth;
686 isStroked = (innerRadius > 0);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000687 }
688 outerRadius += halfWidth;
689 bounds.outset(halfWidth, halfWidth);
690 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000691
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));
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000752 GrAssert(sizeof(EllipseVertex) == drawState->getVertexSize());
753
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;
779 isStroked = (innerXRadius > 0 && innerYRadius > 0);
780 }
781
782 xRadius += scaledStroke.fX;
783 yRadius += scaledStroke.fY;
784 bounds.outset(scaledStroke.fX, scaledStroke.fY);
785 }
jvanverth@google.come3647412013-05-08 15:31:43 +0000786
787 GrDrawTarget::AutoReleaseGeometry geo(target, 16, 0);
788 if (!geo.succeeded()) {
789 GrPrintf("Failed to get space for vertices!\n");
790 return false;
791 }
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000792 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices());
jvanverth@google.come3647412013-05-08 15:31:43 +0000793
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000794 GrEffectRef* effect = EllipseEdgeEffect::Create(isStroked);
jvanverth@google.come3647412013-05-08 15:31:43 +0000795 static const int kEllipseOffsetAttrIndex = 1;
796 static const int kEllipseRadiiAttrIndex = 2;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000797 drawState->addCoverageEffect(effect,
798 kEllipseOffsetAttrIndex, kEllipseRadiiAttrIndex)->unref();
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000799
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000800 // Compute the reciprocals of the radii here to save time in the shader
801 SkScalar xRadRecip = SkScalarInvert(xRadius);
802 SkScalar yRadRecip = SkScalarInvert(yRadius);
803 SkScalar xInnerRadRecip = SkScalarInvert(innerXRadius);
804 SkScalar yInnerRadRecip = SkScalarInvert(innerYRadius);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000805
806 // Extend the radii out half a pixel to antialias.
807 SkScalar xOuterRadius = xRadius + SK_ScalarHalf;
808 SkScalar yOuterRadius = yRadius + SK_ScalarHalf;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000809
810 // Expand the rect so all the pixels will be captured.
811 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
812
813 SkScalar yCoords[4] = {
814 bounds.fTop,
815 bounds.fTop + yOuterRadius,
816 bounds.fBottom - yOuterRadius,
817 bounds.fBottom
818 };
819 SkScalar yOuterOffsets[4] = {
jvanverth@google.comd1b5b142013-07-02 14:46:03 +0000820 yOuterRadius,
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000821 SK_ScalarNearlyZero, // we're using inversesqrt() in the shader, so can't be exactly 0
822 SK_ScalarNearlyZero,
823 yOuterRadius
824 };
825
826 for (int i = 0; i < 4; ++i) {
827 verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]);
jvanverth@google.comd1b5b142013-07-02 14:46:03 +0000828 verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000829 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
830 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000831 verts++;
832
833 verts->fPos = SkPoint::Make(bounds.fLeft + xOuterRadius, yCoords[i]);
834 verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000835 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
836 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000837 verts++;
838
839 verts->fPos = SkPoint::Make(bounds.fRight - xOuterRadius, yCoords[i]);
840 verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000841 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
842 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000843 verts++;
844
845 verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]);
846 verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000847 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
848 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000849 verts++;
850 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000851
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000852 // drop out the middle quad if we're stroked
853 int indexCnt = isStroked ? GR_ARRAY_COUNT(gRRectIndices)-6 : GR_ARRAY_COUNT(gRRectIndices);
854 target->setIndexSourceToBuffer(indexBuffer);
855 target->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, 16, indexCnt, &bounds);
856 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000857
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000858 return true;
859}