blob: de7f4a36ef26917414d30d81c90edb8515613a05 [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 {
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +000094 GrGLShaderBuilder::VertexBuilder* vertexBuilder = builder->getVertexBuilder();
95 SkASSERT(NULL != vertexBuilder);
96
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000097 const CircleEdgeEffect& circleEffect = drawEffect.castEffect<CircleEdgeEffect>();
98 const char *vsName, *fsName;
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +000099 vertexBuilder->addVarying(kVec4f_GrSLType, "CircleEdge", &vsName, &fsName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000100
101 const SkString* attrName =
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +0000102 vertexBuilder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]);
103 vertexBuilder->vsCodeAppendf("\t%s = %s;\n", vsName, attrName->c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000104
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000105 builder->fsCodeAppendf("\tfloat d = length(%s.xy);\n", fsName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000106 builder->fsCodeAppendf("\tfloat edgeAlpha = clamp(%s.z - d, 0.0, 1.0);\n", fsName);
107 if (circleEffect.isStroked()) {
108 builder->fsCodeAppendf("\tfloat innerAlpha = clamp(d - %s.w, 0.0, 1.0);\n", fsName);
109 builder->fsCodeAppend("\tedgeAlpha *= innerAlpha;\n");
110 }
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000111
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000112 SkString modulate;
bsalomon@google.com018f1792013-04-18 19:36:09 +0000113 GrGLSLModulatef<4>(&modulate, inputColor, "edgeAlpha");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000114 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str());
115 }
116
117 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
118 const CircleEdgeEffect& circleEffect = drawEffect.castEffect<CircleEdgeEffect>();
119
120 return circleEffect.isStroked() ? 0x1 : 0x0;
121 }
122
123 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {}
124
125 private:
126 typedef GrGLEffect INHERITED;
127 };
128
129
130private:
131 CircleEdgeEffect(bool stroke) : GrEffect() {
132 this->addVertexAttrib(kVec4f_GrSLType);
133 fStroke = stroke;
134 }
135
136 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
137 const CircleEdgeEffect& cee = CastEffect<CircleEdgeEffect>(other);
138 return cee.fStroke == fStroke;
139 }
140
141 bool fStroke;
142
143 GR_DECLARE_EFFECT_TEST;
144
145 typedef GrEffect INHERITED;
146};
147
148GR_DEFINE_EFFECT_TEST(CircleEdgeEffect);
149
150GrEffectRef* CircleEdgeEffect::TestCreate(SkMWCRandom* random,
151 GrContext* context,
152 const GrDrawTargetCaps&,
153 GrTexture* textures[]) {
154 return CircleEdgeEffect::Create(random->nextBool());
155}
156
157///////////////////////////////////////////////////////////////////////////////
158
159/**
160 * 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 +0000161 * ellipse, specified as a 2D offset from center, and the reciprocals of the outer and inner radii,
162 * in both x and y directions.
163 *
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000164 * 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 +0000165 */
166
167class EllipseEdgeEffect : public GrEffect {
168public:
169 static GrEffectRef* Create(bool stroke) {
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000170 GR_CREATE_STATIC_EFFECT(gEllipseStrokeEdge, EllipseEdgeEffect, (true));
171 GR_CREATE_STATIC_EFFECT(gEllipseFillEdge, EllipseEdgeEffect, (false));
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000172
173 if (stroke) {
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000174 gEllipseStrokeEdge->ref();
175 return gEllipseStrokeEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000176 } else {
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000177 gEllipseFillEdge->ref();
178 return gEllipseFillEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000179 }
180 }
181
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000182 virtual void getConstantColorComponents(GrColor* color,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000183 uint32_t* validFlags) const SK_OVERRIDE {
184 *validFlags = 0;
185 }
186
187 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
188 return GrTBackendEffectFactory<EllipseEdgeEffect>::getInstance();
189 }
190
191 virtual ~EllipseEdgeEffect() {}
192
193 static const char* Name() { return "EllipseEdge"; }
194
195 inline bool isStroked() const { return fStroke; }
196
197 class GLEffect : public GrGLEffect {
198 public:
199 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
200 : INHERITED (factory) {}
201
202 virtual void emitCode(GrGLShaderBuilder* builder,
203 const GrDrawEffect& drawEffect,
204 EffectKey key,
205 const char* outputColor,
206 const char* inputColor,
207 const TextureSamplerArray& samplers) SK_OVERRIDE {
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +0000208 GrGLShaderBuilder::VertexBuilder* vertexBuilder = builder->getVertexBuilder();
209 SkASSERT(NULL != vertexBuilder);
210
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000211 const EllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<EllipseEdgeEffect>();
212
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000213 const char *vsOffsetName, *fsOffsetName;
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000214 const char *vsRadiiName, *fsRadiiName;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000215
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +0000216 vertexBuilder->addVarying(kVec2f_GrSLType, "EllipseOffsets", &vsOffsetName, &fsOffsetName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000217 const SkString* attr0Name =
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +0000218 vertexBuilder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]);
219 vertexBuilder->vsCodeAppendf("\t%s = %s;\n", vsOffsetName, attr0Name->c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000220
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +0000221 vertexBuilder->addVarying(kVec4f_GrSLType, "EllipseRadii", &vsRadiiName, &fsRadiiName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000222 const SkString* attr1Name =
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +0000223 vertexBuilder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[1]);
224 vertexBuilder->vsCodeAppendf("\t%s = %s;\n", vsRadiiName, attr1Name->c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000225
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +0000226 // for outer curve
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000227 builder->fsCodeAppendf("\tvec2 scaledOffset = %s*%s.xy;\n", fsOffsetName, fsRadiiName);
228 builder->fsCodeAppend("\tfloat test = dot(scaledOffset, scaledOffset) - 1.0;\n");
229 builder->fsCodeAppendf("\tvec2 grad = 2.0*scaledOffset*%s.xy;\n", fsRadiiName);
jvanverth@google.comd1b5b142013-07-02 14:46:03 +0000230 builder->fsCodeAppend("\tfloat grad_dot = dot(grad, grad);\n");
231 // we need to clamp the length^2 of the gradiant vector to a non-zero value, because
232 // on the Nexus 4 the undefined result of inversesqrt(0) drops out an entire tile
233 // TODO: restrict this to Adreno-only
234 builder->fsCodeAppend("\tgrad_dot = max(grad_dot, 1.0e-4);\n");
235 builder->fsCodeAppend("\tfloat invlen = inversesqrt(grad_dot);\n");
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000236 builder->fsCodeAppend("\tfloat edgeAlpha = clamp(0.5-test*invlen, 0.0, 1.0);\n");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000237
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000238 // for inner curve
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000239 if (ellipseEffect.isStroked()) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000240 builder->fsCodeAppendf("\tscaledOffset = %s*%s.zw;\n", fsOffsetName, fsRadiiName);
241 builder->fsCodeAppend("\ttest = dot(scaledOffset, scaledOffset) - 1.0;\n");
242 builder->fsCodeAppendf("\tgrad = 2.0*scaledOffset*%s.zw;\n", fsRadiiName);
243 builder->fsCodeAppend("\tinvlen = inversesqrt(dot(grad, grad));\n");
244 builder->fsCodeAppend("\tedgeAlpha *= clamp(0.5+test*invlen, 0.0, 1.0);\n");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000245 }
246
247 SkString modulate;
bsalomon@google.com018f1792013-04-18 19:36:09 +0000248 GrGLSLModulatef<4>(&modulate, inputColor, "edgeAlpha");
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000249 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000250 }
251
252 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
253 const EllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<EllipseEdgeEffect>();
254
255 return ellipseEffect.isStroked() ? 0x1 : 0x0;
256 }
257
258 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {
259 }
260
261 private:
262 typedef GrGLEffect INHERITED;
263 };
264
265private:
266 EllipseEdgeEffect(bool stroke) : GrEffect() {
267 this->addVertexAttrib(kVec2f_GrSLType);
268 this->addVertexAttrib(kVec4f_GrSLType);
269 fStroke = stroke;
270 }
271
272 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
273 const EllipseEdgeEffect& eee = CastEffect<EllipseEdgeEffect>(other);
274 return eee.fStroke == fStroke;
275 }
276
277 bool fStroke;
278
279 GR_DECLARE_EFFECT_TEST;
280
281 typedef GrEffect INHERITED;
282};
283
284GR_DEFINE_EFFECT_TEST(EllipseEdgeEffect);
285
286GrEffectRef* EllipseEdgeEffect::TestCreate(SkMWCRandom* random,
287 GrContext* context,
288 const GrDrawTargetCaps&,
289 GrTexture* textures[]) {
290 return EllipseEdgeEffect::Create(random->nextBool());
291}
292
293///////////////////////////////////////////////////////////////////////////////
294
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000295void GrOvalRenderer::reset() {
296 GrSafeSetNull(fRRectIndexBuffer);
297}
298
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000299bool GrOvalRenderer::drawOval(GrDrawTarget* target, const GrContext* context, bool useAA,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000300 const SkRect& oval, const SkStrokeRec& stroke)
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000301{
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000302 if (!useAA) {
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000303 return false;
304 }
305
306 const SkMatrix& vm = context->getMatrix();
307
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000308 // we can draw circles
309 if (SkScalarNearlyEqual(oval.width(), oval.height())
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000310 && circle_stays_circle(vm)) {
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000311 this->drawCircle(target, useAA, oval, stroke);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000312
313 // and axis-aligned ellipses only
314 } else if (vm.rectStaysRect()) {
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000315 return this->drawEllipse(target, useAA, oval, stroke);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000316
317 } else {
318 return false;
319 }
320
321 return true;
322}
323
robertphillips@google.com42903302013-04-20 12:26:07 +0000324namespace {
325
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000326///////////////////////////////////////////////////////////////////////////////
327
robertphillips@google.com42903302013-04-20 12:26:07 +0000328// position + edge
329extern const GrVertexAttrib gCircleVertexAttribs[] = {
330 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
331 {kVec4f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding}
332};
333
334};
335
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000336void GrOvalRenderer::drawCircle(GrDrawTarget* target,
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000337 bool useAA,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000338 const SkRect& circle,
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000339 const SkStrokeRec& stroke)
340{
341 GrDrawState* drawState = target->drawState();
342
343 const SkMatrix& vm = drawState->getViewMatrix();
344 GrPoint center = GrPoint::Make(circle.centerX(), circle.centerY());
345 vm.mapPoints(&center, 1);
346 SkScalar radius = vm.mapRadius(SkScalarHalf(circle.width()));
347 SkScalar strokeWidth = vm.mapRadius(stroke.getWidth());
348
bsalomon@google.com137f1342013-05-29 21:27:53 +0000349 GrDrawState::AutoViewMatrixRestore avmr;
350 if (!avmr.setIdentity(drawState)) {
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000351 return;
352 }
353
robertphillips@google.com42903302013-04-20 12:26:07 +0000354 drawState->setVertexAttribs<gCircleVertexAttribs>(SK_ARRAY_COUNT(gCircleVertexAttribs));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000355 SkASSERT(sizeof(CircleVertex) == drawState->getVertexSize());
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000356
357 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
358 if (!geo.succeeded()) {
359 GrPrintf("Failed to get space for vertices!\n");
360 return;
361 }
362
363 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
364
365 SkStrokeRec::Style style = stroke.getStyle();
366 bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style);
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000367
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000368 SkScalar innerRadius = 0.0f;
369 SkScalar outerRadius = radius;
370 SkScalar halfWidth = 0;
371 if (style != SkStrokeRec::kFill_Style) {
372 if (SkScalarNearlyZero(strokeWidth)) {
373 halfWidth = SK_ScalarHalf;
374 } else {
375 halfWidth = SkScalarHalf(strokeWidth);
376 }
377
378 outerRadius += halfWidth;
379 if (isStroked) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000380 innerRadius = radius - halfWidth;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000381 }
382 }
383
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +0000384 GrEffectRef* effect = CircleEdgeEffect::Create(isStroked && innerRadius > 0);
385 static const int kCircleEdgeAttrIndex = 1;
386 drawState->addCoverageEffect(effect, kCircleEdgeAttrIndex)->unref();
387
bsalomon@google.com58e30fe2013-04-01 19:01:20 +0000388 // The radii are outset for two reasons. First, it allows the shader to simply perform
389 // clamp(distance-to-center - radius, 0, 1). Second, the outer radius is used to compute the
390 // verts of the bounding box that is rendered and the outset ensures the box will cover all
391 // pixels partially covered by the circle.
392 outerRadius += SK_ScalarHalf;
393 innerRadius -= SK_ScalarHalf;
394
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000395 SkRect bounds = SkRect::MakeLTRB(
bsalomon@google.com58e30fe2013-04-01 19:01:20 +0000396 center.fX - outerRadius,
397 center.fY - outerRadius,
398 center.fX + outerRadius,
399 center.fY + outerRadius
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000400 );
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000401
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000402 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000403 verts[0].fOffset = SkPoint::Make(-outerRadius, -outerRadius);
404 verts[0].fOuterRadius = outerRadius;
405 verts[0].fInnerRadius = innerRadius;
406
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000407 verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
skia.committer@gmail.com46746762013-04-12 07:01:51 +0000408 verts[1].fOffset = SkPoint::Make(outerRadius, -outerRadius);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000409 verts[1].fOuterRadius = outerRadius;
410 verts[1].fInnerRadius = innerRadius;
411
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000412 verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000413 verts[2].fOffset = SkPoint::Make(-outerRadius, outerRadius);
414 verts[2].fOuterRadius = outerRadius;
415 verts[2].fInnerRadius = innerRadius;
416
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000417 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000418 verts[3].fOffset = SkPoint::Make(outerRadius, outerRadius);
419 verts[3].fOuterRadius = outerRadius;
420 verts[3].fInnerRadius = innerRadius;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000421
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000422 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000423}
424
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000425///////////////////////////////////////////////////////////////////////////////
426
robertphillips@google.com42903302013-04-20 12:26:07 +0000427namespace {
428
429// position + edge
430extern const GrVertexAttrib gEllipseVertexAttribs[] = {
431 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
432 {kVec2f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding},
433 {kVec4f_GrVertexAttribType, 2*sizeof(GrPoint), kEffect_GrVertexAttribBinding}
434};
435
436};
437
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000438bool GrOvalRenderer::drawEllipse(GrDrawTarget* target,
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000439 bool useAA,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000440 const SkRect& ellipse,
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000441 const SkStrokeRec& stroke)
442{
443 GrDrawState* drawState = target->drawState();
444#ifdef SK_DEBUG
445 {
446 // we should have checked for this previously
447 bool isAxisAlignedEllipse = drawState->getViewMatrix().rectStaysRect();
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000448 SkASSERT(useAA && isAxisAlignedEllipse);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000449 }
450#endif
451
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000452 // do any matrix crunching before we reset the draw state for device coords
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000453 const SkMatrix& vm = drawState->getViewMatrix();
454 GrPoint center = GrPoint::Make(ellipse.centerX(), ellipse.centerY());
455 vm.mapPoints(&center, 1);
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000456 SkScalar ellipseXRadius = SkScalarHalf(ellipse.width());
457 SkScalar ellipseYRadius = SkScalarHalf(ellipse.height());
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +0000458 SkScalar xRadius = SkScalarAbs(vm[SkMatrix::kMScaleX]*ellipseXRadius +
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000459 vm[SkMatrix::kMSkewY]*ellipseYRadius);
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +0000460 SkScalar yRadius = SkScalarAbs(vm[SkMatrix::kMSkewX]*ellipseXRadius +
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000461 vm[SkMatrix::kMScaleY]*ellipseYRadius);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000462
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000463 // do (potentially) anisotropic mapping of stroke
464 SkVector scaledStroke;
465 SkScalar strokeWidth = stroke.getWidth();
466 scaledStroke.fX = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMScaleX] + vm[SkMatrix::kMSkewY]));
467 scaledStroke.fY = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMSkewX] + vm[SkMatrix::kMScaleY]));
468
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000469 SkStrokeRec::Style style = stroke.getStyle();
470 bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style);
471
472 SkScalar innerXRadius = 0.0f;
473 SkScalar innerYRadius = 0.0f;
474 if (SkStrokeRec::kFill_Style != style) {
475 if (SkScalarNearlyZero(scaledStroke.length())) {
476 scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf);
477 } else {
478 scaledStroke.scale(SK_ScalarHalf);
479 }
480
481 // we only handle thick strokes for near-circular ellipses
482 if (scaledStroke.length() > SK_ScalarHalf &&
483 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
484 return false;
485 }
486
487 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
488 if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius ||
489 scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) {
490 return false;
491 }
492
493 // this is legit only if scale & translation (which should be the case at the moment)
494 if (isStroked) {
495 innerXRadius = xRadius - scaledStroke.fX;
496 innerYRadius = yRadius - scaledStroke.fY;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000497 }
498
499 xRadius += scaledStroke.fX;
500 yRadius += scaledStroke.fY;
501 }
502
bsalomon@google.com137f1342013-05-29 21:27:53 +0000503 GrDrawState::AutoViewMatrixRestore avmr;
504 if (!avmr.setIdentity(drawState)) {
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000505 return false;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000506 }
507
robertphillips@google.com42903302013-04-20 12:26:07 +0000508 drawState->setVertexAttribs<gEllipseVertexAttribs>(SK_ARRAY_COUNT(gEllipseVertexAttribs));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000509 SkASSERT(sizeof(EllipseVertex) == drawState->getVertexSize());
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000510
511 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
512 if (!geo.succeeded()) {
513 GrPrintf("Failed to get space for vertices!\n");
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000514 return false;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000515 }
516
517 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices());
518
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +0000519 GrEffectRef* effect = EllipseEdgeEffect::Create(isStroked &&
520 innerXRadius > 0 && innerYRadius > 0);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000521
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000522 static const int kEllipseCenterAttrIndex = 1;
523 static const int kEllipseEdgeAttrIndex = 2;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000524 drawState->addCoverageEffect(effect, kEllipseCenterAttrIndex, kEllipseEdgeAttrIndex)->unref();
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000525
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000526 // Compute the reciprocals of the radii here to save time in the shader
527 SkScalar xRadRecip = SkScalarInvert(xRadius);
528 SkScalar yRadRecip = SkScalarInvert(yRadius);
529 SkScalar xInnerRadRecip = SkScalarInvert(innerXRadius);
530 SkScalar yInnerRadRecip = SkScalarInvert(innerYRadius);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000531
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000532 // We've extended the outer x radius out half a pixel to antialias.
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000533 // This will also expand the rect so all the pixels will be captured.
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000534 // TODO: Consider if we should use sqrt(2)/2 instead
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000535 xRadius += SK_ScalarHalf;
536 yRadius += SK_ScalarHalf;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000537
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000538 SkRect bounds = SkRect::MakeLTRB(
539 center.fX - xRadius,
540 center.fY - yRadius,
541 center.fX + xRadius,
542 center.fY + yRadius
543 );
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000544
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000545 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000546 verts[0].fOffset = SkPoint::Make(-xRadius, -yRadius);
547 verts[0].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
548 verts[0].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000549
550 verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000551 verts[1].fOffset = SkPoint::Make(xRadius, -yRadius);
552 verts[1].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
553 verts[1].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000554
555 verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000556 verts[2].fOffset = SkPoint::Make(-xRadius, yRadius);
557 verts[2].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
558 verts[2].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000559
560 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000561 verts[3].fOffset = SkPoint::Make(xRadius, yRadius);
562 verts[3].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
563 verts[3].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
skia.committer@gmail.com46746762013-04-12 07:01:51 +0000564
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000565 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds);
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000566
567 return true;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000568}
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000569
570///////////////////////////////////////////////////////////////////////////////
571
572static const uint16_t gRRectIndices[] = {
573 // corners
574 0, 1, 5, 0, 5, 4,
575 2, 3, 7, 2, 7, 6,
576 8, 9, 13, 8, 13, 12,
577 10, 11, 15, 10, 15, 14,
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000578
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000579 // edges
580 1, 2, 6, 1, 6, 5,
581 4, 5, 9, 4, 9, 8,
582 6, 7, 11, 6, 11, 10,
583 9, 10, 14, 9, 14, 13,
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000584
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000585 // center
586 // we place this at the end so that we can ignore these indices when rendering stroke-only
587 5, 6, 10, 5, 10, 9
588};
589
590
591GrIndexBuffer* GrOvalRenderer::rRectIndexBuffer(GrGpu* gpu) {
592 if (NULL == fRRectIndexBuffer) {
593 fRRectIndexBuffer =
594 gpu->createIndexBuffer(sizeof(gRRectIndices), false);
595 if (NULL != fRRectIndexBuffer) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000596#ifdef SK_DEBUG
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000597 bool updated =
598#endif
599 fRRectIndexBuffer->updateData(gRRectIndices,
600 sizeof(gRRectIndices));
601 GR_DEBUGASSERT(updated);
602 }
603 }
604 return fRRectIndexBuffer;
605}
606
skia.committer@gmail.com2fd42c42013-05-03 07:01:00 +0000607bool GrOvalRenderer::drawSimpleRRect(GrDrawTarget* target, GrContext* context, bool useAA,
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000608 const SkRRect& rrect, const SkStrokeRec& stroke)
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000609{
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000610 // only anti-aliased rrects for now
611 if (!useAA) {
612 return false;
613 }
614
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000615 const SkMatrix& vm = context->getMatrix();
616#ifdef SK_DEBUG
617 {
618 // we should have checked for this previously
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000619 SkASSERT(useAA && vm.rectStaysRect() && rrect.isSimple());
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000620 }
621#endif
622
623 // do any matrix crunching before we reset the draw state for device coords
624 const SkRect& rrectBounds = rrect.getBounds();
625 SkRect bounds;
626 vm.mapRect(&bounds, rrectBounds);
627
628 SkVector radii = rrect.getSimpleRadii();
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000629 SkScalar xRadius = SkScalarAbs(vm[SkMatrix::kMScaleX]*radii.fX +
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000630 vm[SkMatrix::kMSkewY]*radii.fY);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000631 SkScalar yRadius = SkScalarAbs(vm[SkMatrix::kMSkewX]*radii.fX +
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000632 vm[SkMatrix::kMScaleY]*radii.fY);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000633
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000634 // if hairline stroke is greater than radius, we don't handle that right now
635 SkStrokeRec::Style style = stroke.getStyle();
636 if (SkStrokeRec::kHairline_Style == style &&
637 (SK_ScalarHalf >= xRadius || SK_ScalarHalf >= yRadius)) {
638 return false;
639 }
640
641 // do (potentially) anisotropic mapping of stroke
642 SkVector scaledStroke;
643 SkScalar strokeWidth = stroke.getWidth();
644 scaledStroke.fX = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMScaleX] + vm[SkMatrix::kMSkewY]));
645 scaledStroke.fY = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMSkewX] + vm[SkMatrix::kMScaleY]));
646
647 // if half of strokewidth is greater than radius, we don't handle that right now
648 if (SK_ScalarHalf*scaledStroke.fX >= xRadius || SK_ScalarHalf*scaledStroke.fY >= yRadius) {
649 return false;
650 }
651
652 // reset to device coordinates
653 GrDrawState* drawState = target->drawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000654 GrDrawState::AutoViewMatrixRestore avmr;
655 if (!avmr.setIdentity(drawState)) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000656 return false;
657 }
658
659 bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style);
660
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000661 GrIndexBuffer* indexBuffer = this->rRectIndexBuffer(context->getGpu());
662 if (NULL == indexBuffer) {
663 GrPrintf("Failed to create index buffer!\n");
664 return false;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000665 }
666
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000667 // if the corners are circles, use the circle renderer
668 if ((!isStroked || scaledStroke.fX == scaledStroke.fY) && xRadius == yRadius) {
669 drawState->setVertexAttribs<gCircleVertexAttribs>(SK_ARRAY_COUNT(gCircleVertexAttribs));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000670 SkASSERT(sizeof(CircleVertex) == drawState->getVertexSize());
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000671
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000672 GrDrawTarget::AutoReleaseGeometry geo(target, 16, 0);
673 if (!geo.succeeded()) {
674 GrPrintf("Failed to get space for vertices!\n");
675 return false;
676 }
677 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000678
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000679 SkScalar innerRadius = 0.0f;
680 SkScalar outerRadius = xRadius;
681 SkScalar halfWidth = 0;
682 if (style != SkStrokeRec::kFill_Style) {
683 if (SkScalarNearlyZero(scaledStroke.fX)) {
684 halfWidth = SK_ScalarHalf;
685 } else {
686 halfWidth = SkScalarHalf(scaledStroke.fX);
687 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000688
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000689 if (isStroked) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000690 innerRadius = xRadius - halfWidth;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000691 }
692 outerRadius += halfWidth;
693 bounds.outset(halfWidth, halfWidth);
694 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000695
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +0000696 isStroked = (isStroked && innerRadius > 0);
697
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000698 GrEffectRef* effect = CircleEdgeEffect::Create(isStroked);
699 static const int kCircleEdgeAttrIndex = 1;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000700 drawState->addCoverageEffect(effect, kCircleEdgeAttrIndex)->unref();
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +0000701
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000702 // The radii are outset for two reasons. First, it allows the shader to simply perform
703 // clamp(distance-to-center - radius, 0, 1). Second, the outer radius is used to compute the
704 // verts of the bounding box that is rendered and the outset ensures the box will cover all
705 // pixels partially covered by the circle.
706 outerRadius += SK_ScalarHalf;
707 innerRadius -= SK_ScalarHalf;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000708
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000709 // Expand the rect so all the pixels will be captured.
710 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000711
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000712 SkScalar yCoords[4] = {
713 bounds.fTop,
714 bounds.fTop + outerRadius,
715 bounds.fBottom - outerRadius,
716 bounds.fBottom
717 };
718 SkScalar yOuterRadii[4] = {
719 -outerRadius,
720 0,
721 0,
722 outerRadius
723 };
724 for (int i = 0; i < 4; ++i) {
725 verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]);
726 verts->fOffset = SkPoint::Make(-outerRadius, 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.fLeft + 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 - outerRadius, yCoords[i]);
738 verts->fOffset = SkPoint::Make(0, yOuterRadii[i]);
739 verts->fOuterRadius = outerRadius;
740 verts->fInnerRadius = innerRadius;
741 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000742
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000743 verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]);
744 verts->fOffset = SkPoint::Make(outerRadius, yOuterRadii[i]);
745 verts->fOuterRadius = outerRadius;
746 verts->fInnerRadius = innerRadius;
747 verts++;
748 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000749
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000750 // drop out the middle quad if we're stroked
751 int indexCnt = isStroked ? GR_ARRAY_COUNT(gRRectIndices)-6 : GR_ARRAY_COUNT(gRRectIndices);
752 target->setIndexSourceToBuffer(indexBuffer);
753 target->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, 16, indexCnt, &bounds);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000754
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000755 // otherwise we use the ellipse renderer
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000756 } else {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000757 drawState->setVertexAttribs<gEllipseVertexAttribs>(SK_ARRAY_COUNT(gEllipseVertexAttribs));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000758 SkASSERT(sizeof(EllipseVertex) == drawState->getVertexSize());
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000759
760 SkScalar innerXRadius = 0.0f;
761 SkScalar innerYRadius = 0.0f;
762 if (SkStrokeRec::kFill_Style != style) {
763 if (SkScalarNearlyZero(scaledStroke.length())) {
764 scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf);
765 } else {
766 scaledStroke.scale(SK_ScalarHalf);
767 }
768
769 // we only handle thick strokes for near-circular ellipses
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +0000770 if (scaledStroke.length() > SK_ScalarHalf &&
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000771 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
772 return false;
773 }
774
775 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
776 if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius ||
777 scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) {
778 return false;
779 }
780
781 // this is legit only if scale & translation (which should be the case at the moment)
782 if (isStroked) {
783 innerXRadius = xRadius - scaledStroke.fX;
784 innerYRadius = yRadius - scaledStroke.fY;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000785 }
786
787 xRadius += scaledStroke.fX;
788 yRadius += scaledStroke.fY;
789 bounds.outset(scaledStroke.fX, scaledStroke.fY);
790 }
jvanverth@google.come3647412013-05-08 15:31:43 +0000791
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +0000792 isStroked = (isStroked && innerXRadius > 0 && innerYRadius > 0);
793
jvanverth@google.come3647412013-05-08 15:31:43 +0000794 GrDrawTarget::AutoReleaseGeometry geo(target, 16, 0);
795 if (!geo.succeeded()) {
796 GrPrintf("Failed to get space for vertices!\n");
797 return false;
798 }
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000799 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices());
jvanverth@google.come3647412013-05-08 15:31:43 +0000800
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000801 GrEffectRef* effect = EllipseEdgeEffect::Create(isStroked);
jvanverth@google.come3647412013-05-08 15:31:43 +0000802 static const int kEllipseOffsetAttrIndex = 1;
803 static const int kEllipseRadiiAttrIndex = 2;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000804 drawState->addCoverageEffect(effect,
805 kEllipseOffsetAttrIndex, kEllipseRadiiAttrIndex)->unref();
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000806
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000807 // Compute the reciprocals of the radii here to save time in the shader
808 SkScalar xRadRecip = SkScalarInvert(xRadius);
809 SkScalar yRadRecip = SkScalarInvert(yRadius);
810 SkScalar xInnerRadRecip = SkScalarInvert(innerXRadius);
811 SkScalar yInnerRadRecip = SkScalarInvert(innerYRadius);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000812
813 // Extend the radii out half a pixel to antialias.
814 SkScalar xOuterRadius = xRadius + SK_ScalarHalf;
815 SkScalar yOuterRadius = yRadius + SK_ScalarHalf;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000816
817 // Expand the rect so all the pixels will be captured.
818 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
819
820 SkScalar yCoords[4] = {
821 bounds.fTop,
822 bounds.fTop + yOuterRadius,
823 bounds.fBottom - yOuterRadius,
824 bounds.fBottom
825 };
826 SkScalar yOuterOffsets[4] = {
jvanverth@google.comd1b5b142013-07-02 14:46:03 +0000827 yOuterRadius,
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000828 SK_ScalarNearlyZero, // we're using inversesqrt() in the shader, so can't be exactly 0
829 SK_ScalarNearlyZero,
830 yOuterRadius
831 };
832
833 for (int i = 0; i < 4; ++i) {
834 verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]);
jvanverth@google.comd1b5b142013-07-02 14:46:03 +0000835 verts->fOffset = SkPoint::Make(xOuterRadius, 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.fLeft + 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 - xOuterRadius, yCoords[i]);
847 verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, 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
852 verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]);
853 verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000854 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
855 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000856 verts++;
857 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000858
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000859 // drop out the middle quad if we're stroked
860 int indexCnt = isStroked ? GR_ARRAY_COUNT(gRRectIndices)-6 : GR_ARRAY_COUNT(gRRectIndices);
861 target->setIndexSourceToBuffer(indexBuffer);
862 target->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, 16, indexCnt, &bounds);
863 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000864
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000865 return true;
866}