blob: d324639e7634faa4d24bec0e41b8b83b1545ffad [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;
jvanverth@google.come3647412013-05-08 15:31:43 +000035 SkScalar fOuterXRadius;
36 SkScalar fInnerXRadius;
37 GrPoint fOuterOffset;
38 GrPoint fInnerOffset;
39};
40
41struct RRectVertex {
42 GrPoint fPos;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +000043 GrPoint fOffset;
44 GrPoint fOuterRadii;
45 GrPoint fInnerRadii;
46};
47
commit-bot@chromium.org81312832013-03-22 18:34:09 +000048inline bool circle_stays_circle(const SkMatrix& m) {
49 return m.isSimilarity();
50}
51
52}
53
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000054///////////////////////////////////////////////////////////////////////////////
55
56/**
57 * 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 +000058 * specified as offset_x, offset_y (both from center point), outer radius and inner radius.
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000059 */
60
61class CircleEdgeEffect : public GrEffect {
62public:
63 static GrEffectRef* Create(bool stroke) {
bsalomon@google.comd42aca32013-04-23 15:37:27 +000064 GR_CREATE_STATIC_EFFECT(gCircleStrokeEdge, CircleEdgeEffect, (true));
65 GR_CREATE_STATIC_EFFECT(gCircleFillEdge, CircleEdgeEffect, (false));
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000066
67 if (stroke) {
bsalomon@google.comd42aca32013-04-23 15:37:27 +000068 gCircleStrokeEdge->ref();
69 return gCircleStrokeEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000070 } else {
bsalomon@google.comd42aca32013-04-23 15:37:27 +000071 gCircleFillEdge->ref();
72 return gCircleFillEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000073 }
74 }
75
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +000076 virtual void getConstantColorComponents(GrColor* color,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000077 uint32_t* validFlags) const SK_OVERRIDE {
78 *validFlags = 0;
79 }
80
81 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
82 return GrTBackendEffectFactory<CircleEdgeEffect>::getInstance();
83 }
84
85 virtual ~CircleEdgeEffect() {}
86
87 static const char* Name() { return "CircleEdge"; }
88
89 inline bool isStroked() const { return fStroke; }
90
91 class GLEffect : public GrGLEffect {
92 public:
93 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
94 : INHERITED (factory) {}
95
96 virtual void emitCode(GrGLShaderBuilder* builder,
97 const GrDrawEffect& drawEffect,
98 EffectKey key,
99 const char* outputColor,
100 const char* inputColor,
101 const TextureSamplerArray& samplers) SK_OVERRIDE {
102 const CircleEdgeEffect& circleEffect = drawEffect.castEffect<CircleEdgeEffect>();
103 const char *vsName, *fsName;
104 builder->addVarying(kVec4f_GrSLType, "CircleEdge", &vsName, &fsName);
105
106 const SkString* attrName =
107 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]);
108 builder->vsCodeAppendf("\t%s = %s;\n", vsName, attrName->c_str());
109
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000110 builder->fsCodeAppendf("\tfloat d = length(%s.xy);\n", fsName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000111 builder->fsCodeAppendf("\tfloat edgeAlpha = clamp(%s.z - d, 0.0, 1.0);\n", fsName);
112 if (circleEffect.isStroked()) {
113 builder->fsCodeAppendf("\tfloat innerAlpha = clamp(d - %s.w, 0.0, 1.0);\n", fsName);
114 builder->fsCodeAppend("\tedgeAlpha *= innerAlpha;\n");
115 }
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000116
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000117 SkString modulate;
bsalomon@google.com018f1792013-04-18 19:36:09 +0000118 GrGLSLModulatef<4>(&modulate, inputColor, "edgeAlpha");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000119 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str());
120 }
121
122 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
123 const CircleEdgeEffect& circleEffect = drawEffect.castEffect<CircleEdgeEffect>();
124
125 return circleEffect.isStroked() ? 0x1 : 0x0;
126 }
127
128 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {}
129
130 private:
131 typedef GrGLEffect INHERITED;
132 };
133
134
135private:
136 CircleEdgeEffect(bool stroke) : GrEffect() {
137 this->addVertexAttrib(kVec4f_GrSLType);
138 fStroke = stroke;
139 }
140
141 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
142 const CircleEdgeEffect& cee = CastEffect<CircleEdgeEffect>(other);
143 return cee.fStroke == fStroke;
144 }
145
146 bool fStroke;
147
148 GR_DECLARE_EFFECT_TEST;
149
150 typedef GrEffect INHERITED;
151};
152
153GR_DEFINE_EFFECT_TEST(CircleEdgeEffect);
154
155GrEffectRef* CircleEdgeEffect::TestCreate(SkMWCRandom* random,
156 GrContext* context,
157 const GrDrawTargetCaps&,
158 GrTexture* textures[]) {
159 return CircleEdgeEffect::Create(random->nextBool());
160}
161
162///////////////////////////////////////////////////////////////////////////////
163
164/**
165 * The output of this effect is a modulation of the input color and coverage for an axis-aligned
jvanverth@google.come3647412013-05-08 15:31:43 +0000166 * ellipse, specified as outer and inner radii, and outer and inner offsets from center.
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000167 */
168
169class EllipseEdgeEffect : public GrEffect {
170public:
171 static GrEffectRef* Create(bool stroke) {
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000172 GR_CREATE_STATIC_EFFECT(gEllipseStrokeEdge, EllipseEdgeEffect, (true));
173 GR_CREATE_STATIC_EFFECT(gEllipseFillEdge, EllipseEdgeEffect, (false));
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000174
175 if (stroke) {
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000176 gEllipseStrokeEdge->ref();
177 return gEllipseStrokeEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000178 } else {
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000179 gEllipseFillEdge->ref();
180 return gEllipseFillEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000181 }
182 }
183
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000184 virtual void getConstantColorComponents(GrColor* color,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000185 uint32_t* validFlags) const SK_OVERRIDE {
186 *validFlags = 0;
187 }
188
189 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
190 return GrTBackendEffectFactory<EllipseEdgeEffect>::getInstance();
191 }
192
193 virtual ~EllipseEdgeEffect() {}
194
195 static const char* Name() { return "EllipseEdge"; }
196
197 inline bool isStroked() const { return fStroke; }
198
199 class GLEffect : public GrGLEffect {
200 public:
201 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
202 : INHERITED (factory) {}
203
204 virtual void emitCode(GrGLShaderBuilder* builder,
205 const GrDrawEffect& drawEffect,
206 EffectKey key,
207 const char* outputColor,
208 const char* inputColor,
209 const TextureSamplerArray& samplers) SK_OVERRIDE {
210 const EllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<EllipseEdgeEffect>();
211
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000212 const char *vsRadiiName, *fsRadiiName;
jvanverth@google.come3647412013-05-08 15:31:43 +0000213 const char *vsOffsetsName, *fsOffsetsName;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000214
jvanverth@google.come3647412013-05-08 15:31:43 +0000215 builder->addVarying(kVec2f_GrSLType, "EllipseRadii", &vsRadiiName, &fsRadiiName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000216 const SkString* attr0Name =
217 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]);
jvanverth@google.come3647412013-05-08 15:31:43 +0000218 builder->vsCodeAppendf("\t%s = %s;\n", vsRadiiName, attr0Name->c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000219
jvanverth@google.come3647412013-05-08 15:31:43 +0000220 builder->addVarying(kVec4f_GrSLType, "EllipseOffsets", &vsOffsetsName, &fsOffsetsName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000221 const SkString* attr1Name =
222 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[1]);
jvanverth@google.come3647412013-05-08 15:31:43 +0000223 builder->vsCodeAppendf("\t%s = %s;\n", vsOffsetsName, attr1Name->c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000224
jvanverth@google.come3647412013-05-08 15:31:43 +0000225 // get length of offset
226 builder->fsCodeAppendf("\tfloat dOuter = length(%s.xy);\n", fsOffsetsName);
227 // compare outer lengths against xOuterRadius
228 builder->fsCodeAppendf("\tfloat edgeAlpha = clamp(%s.x-dOuter, 0.0, 1.0);\n",
229 fsRadiiName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000230
231 if (ellipseEffect.isStroked()) {
jvanverth@google.come3647412013-05-08 15:31:43 +0000232 builder->fsCodeAppendf("\tfloat dInner = length(%s.zw);\n", fsOffsetsName);
233
234 // compare inner lengths against xInnerRadius
235 builder->fsCodeAppendf("\tfloat innerAlpha = clamp(dInner-%s.y, 0.0, 1.0);\n",
236 fsRadiiName);
237 builder->fsCodeAppend("\tedgeAlpha *= innerAlpha;\n");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000238 }
239
240 SkString modulate;
bsalomon@google.com018f1792013-04-18 19:36:09 +0000241 GrGLSLModulatef<4>(&modulate, inputColor, "edgeAlpha");
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000242 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000243 }
244
245 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
246 const EllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<EllipseEdgeEffect>();
247
248 return ellipseEffect.isStroked() ? 0x1 : 0x0;
249 }
250
251 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {
252 }
253
254 private:
255 typedef GrGLEffect INHERITED;
256 };
257
258private:
259 EllipseEdgeEffect(bool stroke) : GrEffect() {
260 this->addVertexAttrib(kVec2f_GrSLType);
261 this->addVertexAttrib(kVec4f_GrSLType);
262 fStroke = stroke;
263 }
264
265 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
266 const EllipseEdgeEffect& eee = CastEffect<EllipseEdgeEffect>(other);
267 return eee.fStroke == fStroke;
268 }
269
270 bool fStroke;
271
272 GR_DECLARE_EFFECT_TEST;
273
274 typedef GrEffect INHERITED;
275};
276
277GR_DEFINE_EFFECT_TEST(EllipseEdgeEffect);
278
279GrEffectRef* EllipseEdgeEffect::TestCreate(SkMWCRandom* random,
280 GrContext* context,
281 const GrDrawTargetCaps&,
282 GrTexture* textures[]) {
283 return EllipseEdgeEffect::Create(random->nextBool());
284}
285
286///////////////////////////////////////////////////////////////////////////////
287
jvanverth@google.come3647412013-05-08 15:31:43 +0000288/**
289 * The output of this effect is a modulation of the input color and coverage for an axis-aligned
skia.committer@gmail.com0431b872013-05-14 07:01:11 +0000290 * ellipse, specified as an offset vector from center and reciprocals of outer and inner radii in
commit-bot@chromium.org166726b2013-05-13 14:41:03 +0000291 * both x and y directions.
jvanverth@google.come3647412013-05-08 15:31:43 +0000292 *
293 * This uses a slightly different algorithm than the EllipseEdgeEffect, above. Rather than
294 * scaling an ellipse to be a circle, it attempts to find the distance from the offset point to the
295 * ellipse by determining where the line through the origin and offset point would cross the
296 * ellipse, and computing the distance to that. This is slower but works better for roundrects
297 * because the straight edges will be more accurate.
298 */
299
300class AltEllipseEdgeEffect : public GrEffect {
301public:
302 static GrEffectRef* Create(bool stroke) {
303 // we go through this so we only have one copy of each effect (stroked/filled)
304 GR_CREATE_STATIC_EFFECT(gAltEllipseStrokeEdge, AltEllipseEdgeEffect, (true));
305 GR_CREATE_STATIC_EFFECT(gAltEllipseFillEdge, AltEllipseEdgeEffect, (false));
306
307 if (stroke) {
308 gAltEllipseStrokeEdge->ref();
309 return gAltEllipseStrokeEdge;
310 } else {
311 gAltEllipseFillEdge->ref();
312 return gAltEllipseFillEdge;
313 }
314 }
315
316 virtual void getConstantColorComponents(GrColor* color,
317 uint32_t* validFlags) const SK_OVERRIDE {
318 *validFlags = 0;
319 }
320
321 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
322 return GrTBackendEffectFactory<AltEllipseEdgeEffect>::getInstance();
323 }
324
325 virtual ~AltEllipseEdgeEffect() {}
326
327 static const char* Name() { return "RRectEdge"; }
328
329 inline bool isStroked() const { return fStroke; }
330
331 class GLEffect : public GrGLEffect {
332 public:
333 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
334 : INHERITED (factory) {}
335
336 virtual void emitCode(GrGLShaderBuilder* builder,
337 const GrDrawEffect& drawEffect,
338 EffectKey key,
339 const char* outputColor,
340 const char* inputColor,
341 const TextureSamplerArray& samplers) SK_OVERRIDE {
342 const AltEllipseEdgeEffect& rrectEffect = drawEffect.castEffect<AltEllipseEdgeEffect>();
343
344 const char *vsOffsetName, *fsOffsetName;
345 const char *vsRadiiName, *fsRadiiName;
346
347 builder->addVarying(kVec2f_GrSLType, "EllipseOffsets", &vsOffsetName, &fsOffsetName);
348 const SkString* attr0Name =
349 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]);
350 builder->vsCodeAppendf("\t%s = %s;\n", vsOffsetName, attr0Name->c_str());
351
352 builder->addVarying(kVec4f_GrSLType, "EllipseRadii", &vsRadiiName, &fsRadiiName);
353 const SkString* attr1Name =
354 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[1]);
355 builder->vsCodeAppendf("\t%s = %s;\n", vsRadiiName, attr1Name->c_str());
356
357 builder->fsCodeAppend("\tfloat edgeAlpha;\n");
358 // get length of offset
359 builder->fsCodeAppendf("\tfloat len = length(%s.xy);\n", fsOffsetName);
jvanverth@google.come3647412013-05-08 15:31:43 +0000360
361 // for outer curve
commit-bot@chromium.org166726b2013-05-13 14:41:03 +0000362 builder->fsCodeAppendf("\tvec2 offset = %s.xy*%s.xy;\n",
jvanverth@google.come3647412013-05-08 15:31:43 +0000363 fsOffsetName, fsRadiiName);
commit-bot@chromium.org166726b2013-05-13 14:41:03 +0000364 builder->fsCodeAppendf("\tfloat t = inversesqrt(dot(offset.xy, offset.xy));\n");
365 builder->fsCodeAppend("\tedgeAlpha = clamp(len*t - len, 0.0, 1.0);\n");
jvanverth@google.come3647412013-05-08 15:31:43 +0000366
367 // for inner curve
368 if (rrectEffect.isStroked()) {
commit-bot@chromium.org166726b2013-05-13 14:41:03 +0000369 builder->fsCodeAppendf("\toffset = %s.xy*%s.zw;\n",
jvanverth@google.come3647412013-05-08 15:31:43 +0000370 fsOffsetName, fsRadiiName);
commit-bot@chromium.org166726b2013-05-13 14:41:03 +0000371 builder->fsCodeAppendf("\tt = inversesqrt(dot(offset.xy, offset.xy));\n");
372 builder->fsCodeAppend("\tedgeAlpha *= clamp(len - len*t, 0.0, 1.0);\n");
jvanverth@google.come3647412013-05-08 15:31:43 +0000373 }
374
375 SkString modulate;
376 GrGLSLModulatef<4>(&modulate, inputColor, "edgeAlpha");
377 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str());
378 }
379
380 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
381 const AltEllipseEdgeEffect& rrectEffect = drawEffect.castEffect<AltEllipseEdgeEffect>();
382
383 return rrectEffect.isStroked() ? 0x1 : 0x0;
384 }
385
386 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {
387 }
388
389 private:
390 typedef GrGLEffect INHERITED;
391 };
392
393private:
394 AltEllipseEdgeEffect(bool stroke) : GrEffect() {
395 this->addVertexAttrib(kVec2f_GrSLType);
396 this->addVertexAttrib(kVec4f_GrSLType);
397 fStroke = stroke;
398 }
399
400 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
401 const AltEllipseEdgeEffect& aeee = CastEffect<AltEllipseEdgeEffect>(other);
402 return aeee.fStroke == fStroke;
403 }
404
405 bool fStroke;
406
407 GR_DECLARE_EFFECT_TEST;
408
409 typedef GrEffect INHERITED;
410};
411
412GR_DEFINE_EFFECT_TEST(AltEllipseEdgeEffect);
413
414GrEffectRef* AltEllipseEdgeEffect::TestCreate(SkMWCRandom* random,
415 GrContext* context,
416 const GrDrawTargetCaps&,
417 GrTexture* textures[]) {
418 return AltEllipseEdgeEffect::Create(random->nextBool());
419}
420
421///////////////////////////////////////////////////////////////////////////////
422
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000423bool GrOvalRenderer::drawOval(GrDrawTarget* target, const GrContext* context, bool useAA,
424 const GrRect& oval, const SkStrokeRec& stroke)
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000425{
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000426 if (!useAA) {
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000427 return false;
428 }
429
430 const SkMatrix& vm = context->getMatrix();
431
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000432 // we can draw circles
433 if (SkScalarNearlyEqual(oval.width(), oval.height())
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000434 && circle_stays_circle(vm)) {
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000435 this->drawCircle(target, useAA, oval, stroke);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000436
437 // and axis-aligned ellipses only
438 } else if (vm.rectStaysRect()) {
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000439 return this->drawEllipse(target, useAA, oval, stroke);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000440
441 } else {
442 return false;
443 }
444
445 return true;
446}
447
robertphillips@google.com42903302013-04-20 12:26:07 +0000448namespace {
449
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000450///////////////////////////////////////////////////////////////////////////////
451
robertphillips@google.com42903302013-04-20 12:26:07 +0000452// position + edge
453extern const GrVertexAttrib gCircleVertexAttribs[] = {
454 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
455 {kVec4f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding}
456};
457
458};
459
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000460void GrOvalRenderer::drawCircle(GrDrawTarget* target,
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000461 bool useAA,
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000462 const GrRect& circle,
463 const SkStrokeRec& stroke)
464{
465 GrDrawState* drawState = target->drawState();
466
467 const SkMatrix& vm = drawState->getViewMatrix();
468 GrPoint center = GrPoint::Make(circle.centerX(), circle.centerY());
469 vm.mapPoints(&center, 1);
470 SkScalar radius = vm.mapRadius(SkScalarHalf(circle.width()));
471 SkScalar strokeWidth = vm.mapRadius(stroke.getWidth());
472
473 GrDrawState::AutoDeviceCoordDraw adcd(drawState);
474 if (!adcd.succeeded()) {
475 return;
476 }
477
robertphillips@google.com42903302013-04-20 12:26:07 +0000478 drawState->setVertexAttribs<gCircleVertexAttribs>(SK_ARRAY_COUNT(gCircleVertexAttribs));
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000479 GrAssert(sizeof(CircleVertex) == drawState->getVertexSize());
480
481 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
482 if (!geo.succeeded()) {
483 GrPrintf("Failed to get space for vertices!\n");
484 return;
485 }
486
487 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
488
489 SkStrokeRec::Style style = stroke.getStyle();
490 bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style);
491 enum {
492 // the edge effects share this stage with glyph rendering
493 // (kGlyphMaskStage in GrTextContext) && SW path rendering
494 // (kPathMaskStage in GrSWMaskHelper)
495 kEdgeEffectStage = GrPaint::kTotalStages,
496 };
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000497
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000498 GrEffectRef* effect = CircleEdgeEffect::Create(isStroked);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000499 static const int kCircleEdgeAttrIndex = 1;
500 drawState->setEffect(kEdgeEffectStage, effect, kCircleEdgeAttrIndex)->unref();
501
502 SkScalar innerRadius = 0.0f;
503 SkScalar outerRadius = radius;
504 SkScalar halfWidth = 0;
505 if (style != SkStrokeRec::kFill_Style) {
506 if (SkScalarNearlyZero(strokeWidth)) {
507 halfWidth = SK_ScalarHalf;
508 } else {
509 halfWidth = SkScalarHalf(strokeWidth);
510 }
511
512 outerRadius += halfWidth;
513 if (isStroked) {
514 innerRadius = SkMaxScalar(0, radius - halfWidth);
515 }
516 }
517
bsalomon@google.com58e30fe2013-04-01 19:01:20 +0000518 // The radii are outset for two reasons. First, it allows the shader to simply perform
519 // clamp(distance-to-center - radius, 0, 1). Second, the outer radius is used to compute the
520 // verts of the bounding box that is rendered and the outset ensures the box will cover all
521 // pixels partially covered by the circle.
522 outerRadius += SK_ScalarHalf;
523 innerRadius -= SK_ScalarHalf;
524
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000525 SkRect bounds = SkRect::MakeLTRB(
bsalomon@google.com58e30fe2013-04-01 19:01:20 +0000526 center.fX - outerRadius,
527 center.fY - outerRadius,
528 center.fX + outerRadius,
529 center.fY + outerRadius
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000530 );
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000531
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000532 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000533 verts[0].fOffset = SkPoint::Make(-outerRadius, -outerRadius);
534 verts[0].fOuterRadius = outerRadius;
535 verts[0].fInnerRadius = innerRadius;
536
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000537 verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
skia.committer@gmail.com46746762013-04-12 07:01:51 +0000538 verts[1].fOffset = SkPoint::Make(outerRadius, -outerRadius);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000539 verts[1].fOuterRadius = outerRadius;
540 verts[1].fInnerRadius = innerRadius;
541
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000542 verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000543 verts[2].fOffset = SkPoint::Make(-outerRadius, outerRadius);
544 verts[2].fOuterRadius = outerRadius;
545 verts[2].fInnerRadius = innerRadius;
546
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000547 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000548 verts[3].fOffset = SkPoint::Make(outerRadius, outerRadius);
549 verts[3].fOuterRadius = outerRadius;
550 verts[3].fInnerRadius = innerRadius;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000551
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000552 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000553}
554
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000555///////////////////////////////////////////////////////////////////////////////
556
robertphillips@google.com42903302013-04-20 12:26:07 +0000557namespace {
558
559// position + edge
560extern const GrVertexAttrib gEllipseVertexAttribs[] = {
561 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
562 {kVec2f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding},
563 {kVec4f_GrVertexAttribType, 2*sizeof(GrPoint), kEffect_GrVertexAttribBinding}
564};
565
566};
567
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000568bool GrOvalRenderer::drawEllipse(GrDrawTarget* target,
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000569 bool useAA,
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000570 const GrRect& ellipse,
571 const SkStrokeRec& stroke)
572{
573 GrDrawState* drawState = target->drawState();
574#ifdef SK_DEBUG
575 {
576 // we should have checked for this previously
577 bool isAxisAlignedEllipse = drawState->getViewMatrix().rectStaysRect();
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000578 SkASSERT(useAA && isAxisAlignedEllipse);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000579 }
580#endif
581
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000582 // do any matrix crunching before we reset the draw state for device coords
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000583 const SkMatrix& vm = drawState->getViewMatrix();
584 GrPoint center = GrPoint::Make(ellipse.centerX(), ellipse.centerY());
585 vm.mapPoints(&center, 1);
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000586 SkScalar ellipseXRadius = SkScalarHalf(ellipse.width());
587 SkScalar ellipseYRadius = SkScalarHalf(ellipse.height());
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +0000588 SkScalar xRadius = SkScalarAbs(vm[SkMatrix::kMScaleX]*ellipseXRadius +
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000589 vm[SkMatrix::kMSkewY]*ellipseYRadius);
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +0000590 SkScalar yRadius = SkScalarAbs(vm[SkMatrix::kMSkewX]*ellipseXRadius +
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000591 vm[SkMatrix::kMScaleY]*ellipseYRadius);
jvanverth@google.come3647412013-05-08 15:31:43 +0000592 if (SkScalarDiv(xRadius, yRadius) > 2 || SkScalarDiv(yRadius, xRadius) > 2) {
593 return false;
594 }
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000595
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000596 // do (potentially) anisotropic mapping of stroke
597 SkVector scaledStroke;
598 SkScalar strokeWidth = stroke.getWidth();
599 scaledStroke.fX = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMScaleX] + vm[SkMatrix::kMSkewY]));
600 scaledStroke.fY = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMSkewX] + vm[SkMatrix::kMScaleY]));
601
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000602 GrDrawState::AutoDeviceCoordDraw adcd(drawState);
603 if (!adcd.succeeded()) {
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000604 return false;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000605 }
606
robertphillips@google.com42903302013-04-20 12:26:07 +0000607 drawState->setVertexAttribs<gEllipseVertexAttribs>(SK_ARRAY_COUNT(gEllipseVertexAttribs));
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000608 GrAssert(sizeof(EllipseVertex) == drawState->getVertexSize());
609
610 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
611 if (!geo.succeeded()) {
612 GrPrintf("Failed to get space for vertices!\n");
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000613 return false;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000614 }
615
616 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices());
617
jvanverth@google.come3647412013-05-08 15:31:43 +0000618 SkStrokeRec::Style style = stroke.getStyle();
619 bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000620 enum {
621 // the edge effects share this stage with glyph rendering
622 // (kGlyphMaskStage in GrTextContext) && SW path rendering
623 // (kPathMaskStage in GrSWMaskHelper)
624 kEdgeEffectStage = GrPaint::kTotalStages,
625 };
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000626
jvanverth@google.come3647412013-05-08 15:31:43 +0000627 GrEffectRef* effect = EllipseEdgeEffect::Create(isStroked);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000628 static const int kEllipseCenterAttrIndex = 1;
629 static const int kEllipseEdgeAttrIndex = 2;
630 drawState->setEffect(kEdgeEffectStage, effect,
631 kEllipseCenterAttrIndex, kEllipseEdgeAttrIndex)->unref();
632
jvanverth@google.come3647412013-05-08 15:31:43 +0000633 SkScalar innerXRadius = 0.0f;
634 SkScalar innerRatio = 1.0f;
635
636 if (SkStrokeRec::kFill_Style != style) {
637 if (SkScalarNearlyZero(scaledStroke.length())) {
638 scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf);
639 } else {
640 scaledStroke.scale(0.5f);
641 }
642
643 // this is legit only if scale & translation (which should be the case at the moment)
644 if (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style) {
645 SkScalar innerYRadius = SkMaxScalar(0, yRadius - scaledStroke.fY);
646 if (innerYRadius > SK_ScalarNearlyZero) {
647 innerXRadius = SkMaxScalar(0, xRadius - scaledStroke.fX);
648 innerRatio = innerXRadius/innerYRadius;
649 }
650 }
651 xRadius += scaledStroke.fX;
652 yRadius += scaledStroke.fY;
653 }
654
655 SkScalar outerRatio = SkScalarDiv(xRadius, yRadius);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000656
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000657 // We've extended the outer x radius out half a pixel to antialias.
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000658 // This will also expand the rect so all the pixels will be captured.
659 xRadius += SK_ScalarHalf;
660 yRadius += SK_ScalarHalf;
jvanverth@google.come3647412013-05-08 15:31:43 +0000661 innerXRadius -= SK_ScalarHalf;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000662
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000663 SkRect bounds = SkRect::MakeLTRB(
664 center.fX - xRadius,
665 center.fY - yRadius,
666 center.fX + xRadius,
667 center.fY + yRadius
668 );
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000669
jvanverth@google.come3647412013-05-08 15:31:43 +0000670 // The offsets are created by scaling the y radius by the appropriate ratio. This way we end up
671 // with a circle equation which can be checked quickly in the shader. We need one offset for
672 // outer and one for inner because they have different scale factors -- otherwise we end up with
673 // non-uniform strokes.
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000674 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
jvanverth@google.come3647412013-05-08 15:31:43 +0000675 verts[0].fOuterXRadius = xRadius;
676 verts[0].fInnerXRadius = innerXRadius;
677 verts[0].fOuterOffset = SkPoint::Make(-xRadius, -outerRatio*yRadius);
678 verts[0].fInnerOffset = SkPoint::Make(-xRadius, -innerRatio*yRadius);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000679
680 verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
jvanverth@google.come3647412013-05-08 15:31:43 +0000681 verts[1].fOuterXRadius = xRadius;
682 verts[1].fInnerXRadius = innerXRadius;
683 verts[1].fOuterOffset = SkPoint::Make(xRadius, -outerRatio*yRadius);
684 verts[1].fInnerOffset = SkPoint::Make(xRadius, -innerRatio*yRadius);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000685
686 verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
jvanverth@google.come3647412013-05-08 15:31:43 +0000687 verts[2].fOuterXRadius = xRadius;
688 verts[2].fInnerXRadius = innerXRadius;
689 verts[2].fOuterOffset = SkPoint::Make(-xRadius, outerRatio*yRadius);
690 verts[2].fInnerOffset = SkPoint::Make(-xRadius, innerRatio*yRadius);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000691
692 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
jvanverth@google.come3647412013-05-08 15:31:43 +0000693 verts[3].fOuterXRadius = xRadius;
694 verts[3].fInnerXRadius = innerXRadius;
695 verts[3].fOuterOffset = SkPoint::Make(xRadius, outerRatio*yRadius);
696 verts[3].fInnerOffset = SkPoint::Make(xRadius, innerRatio*yRadius);
skia.committer@gmail.com46746762013-04-12 07:01:51 +0000697
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000698 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds);
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000699
700 return true;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000701}
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000702
703///////////////////////////////////////////////////////////////////////////////
704
705static const uint16_t gRRectIndices[] = {
706 // corners
707 0, 1, 5, 0, 5, 4,
708 2, 3, 7, 2, 7, 6,
709 8, 9, 13, 8, 13, 12,
710 10, 11, 15, 10, 15, 14,
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000711
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000712 // edges
713 1, 2, 6, 1, 6, 5,
714 4, 5, 9, 4, 9, 8,
715 6, 7, 11, 6, 11, 10,
716 9, 10, 14, 9, 14, 13,
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000717
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000718 // center
719 // we place this at the end so that we can ignore these indices when rendering stroke-only
720 5, 6, 10, 5, 10, 9
721};
722
723
724GrIndexBuffer* GrOvalRenderer::rRectIndexBuffer(GrGpu* gpu) {
725 if (NULL == fRRectIndexBuffer) {
726 fRRectIndexBuffer =
727 gpu->createIndexBuffer(sizeof(gRRectIndices), false);
728 if (NULL != fRRectIndexBuffer) {
729#if GR_DEBUG
730 bool updated =
731#endif
732 fRRectIndexBuffer->updateData(gRRectIndices,
733 sizeof(gRRectIndices));
734 GR_DEBUGASSERT(updated);
735 }
736 }
737 return fRRectIndexBuffer;
738}
739
skia.committer@gmail.com2fd42c42013-05-03 07:01:00 +0000740bool GrOvalRenderer::drawSimpleRRect(GrDrawTarget* target, GrContext* context, bool useAA,
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000741 const SkRRect& rrect, const SkStrokeRec& stroke)
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000742{
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000743 // only anti-aliased rrects for now
744 if (!useAA) {
745 return false;
746 }
747
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000748 const SkMatrix& vm = context->getMatrix();
749#ifdef SK_DEBUG
750 {
751 // we should have checked for this previously
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000752 SkASSERT(useAA && vm.rectStaysRect() && rrect.isSimple());
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000753 }
754#endif
755
756 // do any matrix crunching before we reset the draw state for device coords
757 const SkRect& rrectBounds = rrect.getBounds();
758 SkRect bounds;
759 vm.mapRect(&bounds, rrectBounds);
760
761 SkVector radii = rrect.getSimpleRadii();
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000762 SkScalar xRadius = SkScalarAbs(vm[SkMatrix::kMScaleX]*radii.fX +
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000763 vm[SkMatrix::kMSkewY]*radii.fY);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000764 SkScalar yRadius = SkScalarAbs(vm[SkMatrix::kMSkewX]*radii.fX +
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000765 vm[SkMatrix::kMScaleY]*radii.fY);
jvanverth@google.come3647412013-05-08 15:31:43 +0000766 // tall or wide quarter-ellipse corners aren't handled
767 if (SkScalarDiv(xRadius, yRadius) > 2 || SkScalarDiv(yRadius, xRadius) > 2) {
768 return false;
769 }
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000770 // if hairline stroke is greater than radius, we don't handle that right now
771 SkStrokeRec::Style style = stroke.getStyle();
772 if (SkStrokeRec::kHairline_Style == style &&
773 (SK_ScalarHalf >= xRadius || SK_ScalarHalf >= yRadius)) {
774 return false;
775 }
776
777 // do (potentially) anisotropic mapping of stroke
778 SkVector scaledStroke;
779 SkScalar strokeWidth = stroke.getWidth();
780 scaledStroke.fX = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMScaleX] + vm[SkMatrix::kMSkewY]));
781 scaledStroke.fY = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMSkewX] + vm[SkMatrix::kMScaleY]));
782
783 // if half of strokewidth is greater than radius, we don't handle that right now
784 if (SK_ScalarHalf*scaledStroke.fX >= xRadius || SK_ScalarHalf*scaledStroke.fY >= yRadius) {
785 return false;
786 }
787
788 // reset to device coordinates
789 GrDrawState* drawState = target->drawState();
790 GrDrawState::AutoDeviceCoordDraw adcd(drawState);
791 if (!adcd.succeeded()) {
792 return false;
793 }
794
795 bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style);
796
797 enum {
798 // the edge effects share this stage with glyph rendering
799 // (kGlyphMaskStage in GrTextContext) && SW path rendering
800 // (kPathMaskStage in GrSWMaskHelper)
801 kEdgeEffectStage = GrPaint::kTotalStages,
802 };
803
804 GrIndexBuffer* indexBuffer = this->rRectIndexBuffer(context->getGpu());
805 if (NULL == indexBuffer) {
806 GrPrintf("Failed to create index buffer!\n");
807 return false;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000808 }
809
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000810 // if the corners are circles, use the circle renderer
811 if ((!isStroked || scaledStroke.fX == scaledStroke.fY) && xRadius == yRadius) {
812 drawState->setVertexAttribs<gCircleVertexAttribs>(SK_ARRAY_COUNT(gCircleVertexAttribs));
813 GrAssert(sizeof(CircleVertex) == drawState->getVertexSize());
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000814
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000815 GrDrawTarget::AutoReleaseGeometry geo(target, 16, 0);
816 if (!geo.succeeded()) {
817 GrPrintf("Failed to get space for vertices!\n");
818 return false;
819 }
820 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000821
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000822 GrEffectRef* effect = CircleEdgeEffect::Create(isStroked);
823 static const int kCircleEdgeAttrIndex = 1;
824 drawState->setEffect(kEdgeEffectStage, effect, kCircleEdgeAttrIndex)->unref();
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000825
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000826 SkScalar innerRadius = 0.0f;
827 SkScalar outerRadius = xRadius;
828 SkScalar halfWidth = 0;
829 if (style != SkStrokeRec::kFill_Style) {
830 if (SkScalarNearlyZero(scaledStroke.fX)) {
831 halfWidth = SK_ScalarHalf;
832 } else {
833 halfWidth = SkScalarHalf(scaledStroke.fX);
834 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000835
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000836 if (isStroked) {
837 innerRadius = SkMaxScalar(0, xRadius - halfWidth);
838 }
839 outerRadius += halfWidth;
840 bounds.outset(halfWidth, halfWidth);
841 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000842
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000843 // The radii are outset for two reasons. First, it allows the shader to simply perform
844 // clamp(distance-to-center - radius, 0, 1). Second, the outer radius is used to compute the
845 // verts of the bounding box that is rendered and the outset ensures the box will cover all
846 // pixels partially covered by the circle.
847 outerRadius += SK_ScalarHalf;
848 innerRadius -= SK_ScalarHalf;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000849
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000850 // Expand the rect so all the pixels will be captured.
851 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000852
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000853 SkScalar yCoords[4] = {
854 bounds.fTop,
855 bounds.fTop + outerRadius,
856 bounds.fBottom - outerRadius,
857 bounds.fBottom
858 };
859 SkScalar yOuterRadii[4] = {
860 -outerRadius,
861 0,
862 0,
863 outerRadius
864 };
865 for (int i = 0; i < 4; ++i) {
866 verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]);
867 verts->fOffset = SkPoint::Make(-outerRadius, yOuterRadii[i]);
868 verts->fOuterRadius = outerRadius;
869 verts->fInnerRadius = innerRadius;
870 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000871
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000872 verts->fPos = SkPoint::Make(bounds.fLeft + outerRadius, yCoords[i]);
873 verts->fOffset = SkPoint::Make(0, yOuterRadii[i]);
874 verts->fOuterRadius = outerRadius;
875 verts->fInnerRadius = innerRadius;
876 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000877
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000878 verts->fPos = SkPoint::Make(bounds.fRight - outerRadius, yCoords[i]);
879 verts->fOffset = SkPoint::Make(0, yOuterRadii[i]);
880 verts->fOuterRadius = outerRadius;
881 verts->fInnerRadius = innerRadius;
882 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000883
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000884 verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]);
885 verts->fOffset = SkPoint::Make(outerRadius, yOuterRadii[i]);
886 verts->fOuterRadius = outerRadius;
887 verts->fInnerRadius = innerRadius;
888 verts++;
889 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000890
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000891 // drop out the middle quad if we're stroked
892 int indexCnt = isStroked ? GR_ARRAY_COUNT(gRRectIndices)-6 : GR_ARRAY_COUNT(gRRectIndices);
893 target->setIndexSourceToBuffer(indexBuffer);
894 target->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, 16, indexCnt, &bounds);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000895
jvanverth@google.come3647412013-05-08 15:31:43 +0000896 // otherwise we use the special ellipse renderer
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000897 } else {
jvanverth@google.come3647412013-05-08 15:31:43 +0000898
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000899 drawState->setVertexAttribs<gEllipseVertexAttribs>(SK_ARRAY_COUNT(gEllipseVertexAttribs));
jvanverth@google.come3647412013-05-08 15:31:43 +0000900 GrAssert(sizeof(RRectVertex) == drawState->getVertexSize());
901
902 GrDrawTarget::AutoReleaseGeometry geo(target, 16, 0);
903 if (!geo.succeeded()) {
904 GrPrintf("Failed to get space for vertices!\n");
905 return false;
906 }
907 RRectVertex* verts = reinterpret_cast<RRectVertex*>(geo.vertices());
908
909 GrEffectRef* effect = AltEllipseEdgeEffect::Create(isStroked);
910 static const int kEllipseOffsetAttrIndex = 1;
911 static const int kEllipseRadiiAttrIndex = 2;
912 drawState->setEffect(kEdgeEffectStage, effect,
913 kEllipseOffsetAttrIndex, kEllipseRadiiAttrIndex)->unref();
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000914
915 SkScalar innerXRadius = 0.0f;
916 SkScalar innerYRadius = 0.0f;
jvanverth@google.come3647412013-05-08 15:31:43 +0000917
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000918 if (SkStrokeRec::kFill_Style != style) {
919 if (SkScalarNearlyZero(scaledStroke.length())) {
920 scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf);
921 } else {
922 scaledStroke.scale(0.5f);
923 }
924
925 // this is legit only if scale & translation (which should be the case at the moment)
926 if (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style) {
927 innerXRadius = SkMaxScalar(0, xRadius - scaledStroke.fX);
928 innerYRadius = SkMaxScalar(0, yRadius - scaledStroke.fY);
929 }
930 xRadius += scaledStroke.fX;
931 yRadius += scaledStroke.fY;
932 bounds.outset(scaledStroke.fX, scaledStroke.fY);
933 }
934
935 // Extend the radii out half a pixel to antialias.
936 SkScalar xOuterRadius = xRadius + SK_ScalarHalf;
937 SkScalar yOuterRadius = yRadius + SK_ScalarHalf;
jvanverth@google.come3647412013-05-08 15:31:43 +0000938 SkScalar xInnerRadius = SkMaxScalar(innerXRadius - SK_ScalarHalf, 0);
939 SkScalar yInnerRadius = SkMaxScalar(innerYRadius - SK_ScalarHalf, 0);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000940
941 // Expand the rect so all the pixels will be captured.
942 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
943
944 SkScalar yCoords[4] = {
945 bounds.fTop,
946 bounds.fTop + yOuterRadius,
947 bounds.fBottom - yOuterRadius,
948 bounds.fBottom
949 };
950 SkScalar yOuterOffsets[4] = {
951 -yOuterRadius,
952 SK_ScalarNearlyZero, // we're using inversesqrt() in the shader, so can't be exactly 0
953 SK_ScalarNearlyZero,
954 yOuterRadius
955 };
956
commit-bot@chromium.org166726b2013-05-13 14:41:03 +0000957 SkScalar recipOuterX = SK_Scalar1/xOuterRadius;
958 SkScalar recipOuterY = SK_Scalar1/yOuterRadius;
959 SkScalar recipInnerX = SK_Scalar1/xInnerRadius;
960 SkScalar recipInnerY = SK_Scalar1/yInnerRadius;
skia.committer@gmail.com0431b872013-05-14 07:01:11 +0000961
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000962 for (int i = 0; i < 4; ++i) {
963 verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]);
964 verts->fOffset = SkPoint::Make(-xOuterRadius, yOuterOffsets[i]);
commit-bot@chromium.org166726b2013-05-13 14:41:03 +0000965 verts->fOuterRadii = SkPoint::Make(recipOuterX, recipOuterY);
966 verts->fInnerRadii = SkPoint::Make(recipInnerX, recipInnerY);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000967 verts++;
968
969 verts->fPos = SkPoint::Make(bounds.fLeft + xOuterRadius, yCoords[i]);
970 verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]);
commit-bot@chromium.org166726b2013-05-13 14:41:03 +0000971 verts->fOuterRadii = SkPoint::Make(recipOuterX, recipOuterY);
972 verts->fInnerRadii = SkPoint::Make(recipInnerX, recipInnerY);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000973 verts++;
974
975 verts->fPos = SkPoint::Make(bounds.fRight - xOuterRadius, yCoords[i]);
976 verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]);
commit-bot@chromium.org166726b2013-05-13 14:41:03 +0000977 verts->fOuterRadii = SkPoint::Make(recipOuterX, recipOuterY);
978 verts->fInnerRadii = SkPoint::Make(recipInnerX, recipInnerY);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000979 verts++;
980
981 verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]);
982 verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]);
commit-bot@chromium.org166726b2013-05-13 14:41:03 +0000983 verts->fOuterRadii = SkPoint::Make(recipOuterX, recipOuterY);
984 verts->fInnerRadii = SkPoint::Make(recipInnerX, recipInnerY);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000985 verts++;
986 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000987
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000988 // drop out the middle quad if we're stroked
989 int indexCnt = isStroked ? GR_ARRAY_COUNT(gRRectIndices)-6 : GR_ARRAY_COUNT(gRRectIndices);
990 target->setIndexSourceToBuffer(indexBuffer);
991 target->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, 16, indexCnt, &bounds);
992 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000993
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000994 return true;
995}