blob: 013c7ab7e85ba5f4ba985254c2fc2f65fc43ae94 [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.org5242ed72013-09-05 19:26:51 +000040struct DIEllipseVertex {
41 GrPoint fPos;
42 GrPoint fOuterOffset;
43 GrPoint fInnerOffset;
44};
45
commit-bot@chromium.org81312832013-03-22 18:34:09 +000046inline bool circle_stays_circle(const SkMatrix& m) {
47 return m.isSimilarity();
48}
49
50}
51
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000052///////////////////////////////////////////////////////////////////////////////
53
54/**
55 * 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 +000056 * specified as offset_x, offset_y (both from center point), outer radius and inner radius.
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000057 */
58
59class CircleEdgeEffect : public GrEffect {
60public:
61 static GrEffectRef* Create(bool stroke) {
bsalomon@google.comd42aca32013-04-23 15:37:27 +000062 GR_CREATE_STATIC_EFFECT(gCircleStrokeEdge, CircleEdgeEffect, (true));
63 GR_CREATE_STATIC_EFFECT(gCircleFillEdge, CircleEdgeEffect, (false));
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000064
65 if (stroke) {
bsalomon@google.comd42aca32013-04-23 15:37:27 +000066 gCircleStrokeEdge->ref();
67 return gCircleStrokeEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000068 } else {
bsalomon@google.comd42aca32013-04-23 15:37:27 +000069 gCircleFillEdge->ref();
70 return gCircleFillEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000071 }
72 }
73
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +000074 virtual void getConstantColorComponents(GrColor* color,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000075 uint32_t* validFlags) const SK_OVERRIDE {
76 *validFlags = 0;
77 }
78
79 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
80 return GrTBackendEffectFactory<CircleEdgeEffect>::getInstance();
81 }
82
83 virtual ~CircleEdgeEffect() {}
84
85 static const char* Name() { return "CircleEdge"; }
86
87 inline bool isStroked() const { return fStroke; }
88
89 class GLEffect : public GrGLEffect {
90 public:
91 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
92 : INHERITED (factory) {}
93
94 virtual void emitCode(GrGLShaderBuilder* builder,
95 const GrDrawEffect& drawEffect,
96 EffectKey key,
97 const char* outputColor,
98 const char* inputColor,
99 const TextureSamplerArray& samplers) SK_OVERRIDE {
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +0000100 GrGLShaderBuilder::VertexBuilder* vertexBuilder = builder->getVertexBuilder();
101 SkASSERT(NULL != vertexBuilder);
102
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000103 const CircleEdgeEffect& circleEffect = drawEffect.castEffect<CircleEdgeEffect>();
104 const char *vsName, *fsName;
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +0000105 vertexBuilder->addVarying(kVec4f_GrSLType, "CircleEdge", &vsName, &fsName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000106
107 const SkString* attrName =
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +0000108 vertexBuilder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]);
109 vertexBuilder->vsCodeAppendf("\t%s = %s;\n", vsName, attrName->c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000110
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000111 builder->fsCodeAppendf("\tfloat d = length(%s.xy);\n", fsName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000112 builder->fsCodeAppendf("\tfloat edgeAlpha = clamp(%s.z - d, 0.0, 1.0);\n", fsName);
113 if (circleEffect.isStroked()) {
114 builder->fsCodeAppendf("\tfloat innerAlpha = clamp(d - %s.w, 0.0, 1.0);\n", fsName);
115 builder->fsCodeAppend("\tedgeAlpha *= innerAlpha;\n");
116 }
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000117
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000118 SkString modulate;
bsalomon@google.com018f1792013-04-18 19:36:09 +0000119 GrGLSLModulatef<4>(&modulate, inputColor, "edgeAlpha");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000120 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str());
121 }
122
123 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
124 const CircleEdgeEffect& circleEffect = drawEffect.castEffect<CircleEdgeEffect>();
125
126 return circleEffect.isStroked() ? 0x1 : 0x0;
127 }
128
129 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {}
130
131 private:
132 typedef GrGLEffect INHERITED;
133 };
134
135
136private:
137 CircleEdgeEffect(bool stroke) : GrEffect() {
138 this->addVertexAttrib(kVec4f_GrSLType);
139 fStroke = stroke;
140 }
141
142 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
143 const CircleEdgeEffect& cee = CastEffect<CircleEdgeEffect>(other);
144 return cee.fStroke == fStroke;
145 }
146
147 bool fStroke;
148
149 GR_DECLARE_EFFECT_TEST;
150
151 typedef GrEffect INHERITED;
152};
153
154GR_DEFINE_EFFECT_TEST(CircleEdgeEffect);
155
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000156GrEffectRef* CircleEdgeEffect::TestCreate(SkRandom* random,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000157 GrContext* context,
158 const GrDrawTargetCaps&,
159 GrTexture* textures[]) {
160 return CircleEdgeEffect::Create(random->nextBool());
161}
162
163///////////////////////////////////////////////////////////////////////////////
164
165/**
166 * 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 +0000167 * ellipse, specified as a 2D offset from center, and the reciprocals of the outer and inner radii,
168 * in both x and y directions.
169 *
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000170 * 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 +0000171 */
172
173class EllipseEdgeEffect : public GrEffect {
174public:
175 static GrEffectRef* Create(bool stroke) {
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000176 GR_CREATE_STATIC_EFFECT(gEllipseStrokeEdge, EllipseEdgeEffect, (true));
177 GR_CREATE_STATIC_EFFECT(gEllipseFillEdge, EllipseEdgeEffect, (false));
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000178
179 if (stroke) {
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000180 gEllipseStrokeEdge->ref();
181 return gEllipseStrokeEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000182 } else {
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000183 gEllipseFillEdge->ref();
184 return gEllipseFillEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000185 }
186 }
187
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000188 virtual void getConstantColorComponents(GrColor* color,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000189 uint32_t* validFlags) const SK_OVERRIDE {
190 *validFlags = 0;
191 }
192
193 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
194 return GrTBackendEffectFactory<EllipseEdgeEffect>::getInstance();
195 }
196
197 virtual ~EllipseEdgeEffect() {}
198
199 static const char* Name() { return "EllipseEdge"; }
200
201 inline bool isStroked() const { return fStroke; }
202
203 class GLEffect : public GrGLEffect {
204 public:
205 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
206 : INHERITED (factory) {}
207
208 virtual void emitCode(GrGLShaderBuilder* builder,
209 const GrDrawEffect& drawEffect,
210 EffectKey key,
211 const char* outputColor,
212 const char* inputColor,
213 const TextureSamplerArray& samplers) SK_OVERRIDE {
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +0000214 GrGLShaderBuilder::VertexBuilder* vertexBuilder = builder->getVertexBuilder();
215 SkASSERT(NULL != vertexBuilder);
216
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000217 const EllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<EllipseEdgeEffect>();
218
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000219 const char *vsOffsetName, *fsOffsetName;
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000220 const char *vsRadiiName, *fsRadiiName;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000221
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +0000222 vertexBuilder->addVarying(kVec2f_GrSLType, "EllipseOffsets", &vsOffsetName, &fsOffsetName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000223 const SkString* attr0Name =
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +0000224 vertexBuilder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]);
225 vertexBuilder->vsCodeAppendf("\t%s = %s;\n", vsOffsetName, attr0Name->c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000226
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +0000227 vertexBuilder->addVarying(kVec4f_GrSLType, "EllipseRadii", &vsRadiiName, &fsRadiiName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000228 const SkString* attr1Name =
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +0000229 vertexBuilder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[1]);
230 vertexBuilder->vsCodeAppendf("\t%s = %s;\n", vsRadiiName, attr1Name->c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000231
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +0000232 // for outer curve
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000233 builder->fsCodeAppendf("\tvec2 scaledOffset = %s*%s.xy;\n", fsOffsetName, fsRadiiName);
234 builder->fsCodeAppend("\tfloat test = dot(scaledOffset, scaledOffset) - 1.0;\n");
235 builder->fsCodeAppendf("\tvec2 grad = 2.0*scaledOffset*%s.xy;\n", fsRadiiName);
jvanverth@google.comd1b5b142013-07-02 14:46:03 +0000236 builder->fsCodeAppend("\tfloat grad_dot = dot(grad, grad);\n");
237 // we need to clamp the length^2 of the gradiant vector to a non-zero value, because
238 // on the Nexus 4 the undefined result of inversesqrt(0) drops out an entire tile
239 // TODO: restrict this to Adreno-only
240 builder->fsCodeAppend("\tgrad_dot = max(grad_dot, 1.0e-4);\n");
241 builder->fsCodeAppend("\tfloat invlen = inversesqrt(grad_dot);\n");
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000242 builder->fsCodeAppend("\tfloat edgeAlpha = clamp(0.5-test*invlen, 0.0, 1.0);\n");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000243
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000244 // for inner curve
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000245 if (ellipseEffect.isStroked()) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000246 builder->fsCodeAppendf("\tscaledOffset = %s*%s.zw;\n", fsOffsetName, fsRadiiName);
247 builder->fsCodeAppend("\ttest = dot(scaledOffset, scaledOffset) - 1.0;\n");
248 builder->fsCodeAppendf("\tgrad = 2.0*scaledOffset*%s.zw;\n", fsRadiiName);
249 builder->fsCodeAppend("\tinvlen = inversesqrt(dot(grad, grad));\n");
250 builder->fsCodeAppend("\tedgeAlpha *= clamp(0.5+test*invlen, 0.0, 1.0);\n");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000251 }
252
253 SkString modulate;
bsalomon@google.com018f1792013-04-18 19:36:09 +0000254 GrGLSLModulatef<4>(&modulate, inputColor, "edgeAlpha");
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000255 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000256 }
257
258 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
259 const EllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<EllipseEdgeEffect>();
260
261 return ellipseEffect.isStroked() ? 0x1 : 0x0;
262 }
263
264 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {
265 }
266
267 private:
268 typedef GrGLEffect INHERITED;
269 };
270
271private:
272 EllipseEdgeEffect(bool stroke) : GrEffect() {
273 this->addVertexAttrib(kVec2f_GrSLType);
274 this->addVertexAttrib(kVec4f_GrSLType);
275 fStroke = stroke;
276 }
277
278 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
279 const EllipseEdgeEffect& eee = CastEffect<EllipseEdgeEffect>(other);
280 return eee.fStroke == fStroke;
281 }
282
283 bool fStroke;
284
285 GR_DECLARE_EFFECT_TEST;
286
287 typedef GrEffect INHERITED;
288};
289
290GR_DEFINE_EFFECT_TEST(EllipseEdgeEffect);
291
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000292GrEffectRef* EllipseEdgeEffect::TestCreate(SkRandom* random,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000293 GrContext* context,
294 const GrDrawTargetCaps&,
295 GrTexture* textures[]) {
296 return EllipseEdgeEffect::Create(random->nextBool());
297}
298
299///////////////////////////////////////////////////////////////////////////////
300
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000301/**
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000302 * The output of this effect is a modulation of the input color and coverage for an ellipse,
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000303 * specified as a 2D offset from center for both the outer and inner paths (if stroked). The
304 * implict equation used is for a unit circle (x^2 + y^2 - 1 = 0) and the edge corrected by
305 * using differentials.
306 *
307 * The result is device-independent and can be used with any affine matrix.
308 */
309
310class DIEllipseEdgeEffect : public GrEffect {
311public:
312 enum Mode { kStroke = 0, kHairline, kFill };
313
314 static GrEffectRef* Create(Mode mode) {
315 GR_CREATE_STATIC_EFFECT(gEllipseStrokeEdge, DIEllipseEdgeEffect, (kStroke));
316 GR_CREATE_STATIC_EFFECT(gEllipseHairlineEdge, DIEllipseEdgeEffect, (kHairline));
317 GR_CREATE_STATIC_EFFECT(gEllipseFillEdge, DIEllipseEdgeEffect, (kFill));
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000318
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000319 if (kStroke == mode) {
320 gEllipseStrokeEdge->ref();
321 return gEllipseStrokeEdge;
322 } else if (kHairline == mode) {
323 gEllipseHairlineEdge->ref();
324 return gEllipseHairlineEdge;
325 } else {
326 gEllipseFillEdge->ref();
327 return gEllipseFillEdge;
328 }
329 }
330
331 virtual void getConstantColorComponents(GrColor* color,
332 uint32_t* validFlags) const SK_OVERRIDE {
333 *validFlags = 0;
334 }
335
336 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
337 return GrTBackendEffectFactory<DIEllipseEdgeEffect>::getInstance();
338 }
339
340 virtual ~DIEllipseEdgeEffect() {}
341
342 static const char* Name() { return "DIEllipseEdge"; }
343
344 inline Mode getMode() const { return fMode; }
345
346 class GLEffect : public GrGLEffect {
347 public:
348 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
349 : INHERITED (factory) {}
350
351 virtual void emitCode(GrGLShaderBuilder* builder,
352 const GrDrawEffect& drawEffect,
353 EffectKey key,
354 const char* outputColor,
355 const char* inputColor,
356 const TextureSamplerArray& samplers) SK_OVERRIDE {
357 GrGLShaderBuilder::VertexBuilder* vertexBuilder = builder->getVertexBuilder();
358 SkASSERT(NULL != vertexBuilder);
359
360 const DIEllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<DIEllipseEdgeEffect>();
361
362 SkAssertResult(builder->enableFeature(
363 GrGLShaderBuilder::kStandardDerivatives_GLSLFeature));
364
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000365 const char *vsOffsetName0, *fsOffsetName0;
366 vertexBuilder->addVarying(kVec2f_GrSLType, "EllipseOffsets0",
367 &vsOffsetName0, &fsOffsetName0);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000368 const SkString* attr0Name =
369 vertexBuilder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]);
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000370 vertexBuilder->vsCodeAppendf("\t%s = %s;\n", vsOffsetName0, attr0Name->c_str());
371 const char *vsOffsetName1, *fsOffsetName1;
372 vertexBuilder->addVarying(kVec2f_GrSLType, "EllipseOffsets1",
373 &vsOffsetName1, &fsOffsetName1);
374 const SkString* attr1Name =
375 vertexBuilder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[1]);
376 vertexBuilder->vsCodeAppendf("\t%s = %s;\n", vsOffsetName1, attr1Name->c_str());
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000377
378 // for outer curve
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000379 builder->fsCodeAppendf("\tvec2 scaledOffset = %s.xy;\n", fsOffsetName0);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000380 builder->fsCodeAppend("\tfloat test = dot(scaledOffset, scaledOffset) - 1.0;\n");
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000381 builder->fsCodeAppendf("\tvec2 duvdx = dFdx(%s);\n", fsOffsetName0);
382 builder->fsCodeAppendf("\tvec2 duvdy = dFdy(%s);\n", fsOffsetName0);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000383 builder->fsCodeAppendf("\tvec2 grad = vec2(2.0*%s.x*duvdx.x + 2.0*%s.y*duvdx.y,\n"
384 "\t 2.0*%s.x*duvdy.x + 2.0*%s.y*duvdy.y);\n",
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000385 fsOffsetName0, fsOffsetName0, fsOffsetName0, fsOffsetName0);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000386
387 builder->fsCodeAppend("\tfloat grad_dot = dot(grad, grad);\n");
388 // we need to clamp the length^2 of the gradiant vector to a non-zero value, because
389 // on the Nexus 4 the undefined result of inversesqrt(0) drops out an entire tile
390 // TODO: restrict this to Adreno-only
391 builder->fsCodeAppend("\tgrad_dot = max(grad_dot, 1.0e-4);\n");
392 builder->fsCodeAppend("\tfloat invlen = inversesqrt(grad_dot);\n");
393 if (kHairline == ellipseEffect.getMode()) {
394 // can probably do this with one step
395 builder->fsCodeAppend("\tfloat edgeAlpha = clamp(1.0-test*invlen, 0.0, 1.0);\n");
396 builder->fsCodeAppend("\tedgeAlpha *= clamp(1.0+test*invlen, 0.0, 1.0);\n");
397 } else {
398 builder->fsCodeAppend("\tfloat edgeAlpha = clamp(0.5-test*invlen, 0.0, 1.0);\n");
399 }
400
401 // for inner curve
402 if (kStroke == ellipseEffect.getMode()) {
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000403 builder->fsCodeAppendf("\tscaledOffset = %s.xy;\n", fsOffsetName1);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000404 builder->fsCodeAppend("\ttest = dot(scaledOffset, scaledOffset) - 1.0;\n");
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000405 builder->fsCodeAppendf("\tduvdx = dFdx(%s);\n", fsOffsetName1);
406 builder->fsCodeAppendf("\tduvdy = dFdy(%s);\n", fsOffsetName1);
407 builder->fsCodeAppendf("\tgrad = vec2(2.0*%s.x*duvdx.x + 2.0*%s.y*duvdx.y,\n"
408 "\t 2.0*%s.x*duvdy.x + 2.0*%s.y*duvdy.y);\n",
409 fsOffsetName1, fsOffsetName1, fsOffsetName1, fsOffsetName1);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000410 builder->fsCodeAppend("\tinvlen = inversesqrt(dot(grad, grad));\n");
411 builder->fsCodeAppend("\tedgeAlpha *= clamp(0.5+test*invlen, 0.0, 1.0);\n");
412 }
413
414 SkString modulate;
415 GrGLSLModulatef<4>(&modulate, inputColor, "edgeAlpha");
416 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str());
417 }
418
419 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
420 const DIEllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<DIEllipseEdgeEffect>();
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000421
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000422 return ellipseEffect.getMode();
423 }
424
425 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {
426 }
427
428 private:
429 typedef GrGLEffect INHERITED;
430 };
431
432private:
433 DIEllipseEdgeEffect(Mode mode) : GrEffect() {
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000434 this->addVertexAttrib(kVec2f_GrSLType);
435 this->addVertexAttrib(kVec2f_GrSLType);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000436 fMode = mode;
437 }
438
439 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
440 const DIEllipseEdgeEffect& eee = CastEffect<DIEllipseEdgeEffect>(other);
441 return eee.fMode == fMode;
442 }
443
444 Mode fMode;
445
446 GR_DECLARE_EFFECT_TEST;
447
448 typedef GrEffect INHERITED;
449};
450
451GR_DEFINE_EFFECT_TEST(DIEllipseEdgeEffect);
452
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000453GrEffectRef* DIEllipseEdgeEffect::TestCreate(SkRandom* random,
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000454 GrContext* context,
455 const GrDrawTargetCaps&,
456 GrTexture* textures[]) {
457 return DIEllipseEdgeEffect::Create((Mode)(random->nextRangeU(0,2)));
458}
459
460///////////////////////////////////////////////////////////////////////////////
461
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000462void GrOvalRenderer::reset() {
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000463 SkSafeSetNull(fRRectIndexBuffer);
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000464}
465
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000466bool GrOvalRenderer::drawOval(GrDrawTarget* target, const GrContext* context, bool useAA,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000467 const SkRect& oval, const SkStrokeRec& stroke)
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000468{
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000469 if (!useAA) {
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000470 return false;
471 }
472
473 const SkMatrix& vm = context->getMatrix();
474
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000475 // we can draw circles
476 if (SkScalarNearlyEqual(oval.width(), oval.height())
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000477 && circle_stays_circle(vm)) {
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000478 this->drawCircle(target, useAA, oval, stroke);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000479 // if we have shader derivative support, render as device-independent
480 } else if (target->caps()->shaderDerivativeSupport()) {
481 return this->drawDIEllipse(target, useAA, oval, stroke);
482 // otherwise axis-aligned ellipses only
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000483 } else if (vm.rectStaysRect()) {
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000484 return this->drawEllipse(target, useAA, oval, stroke);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000485 } else {
486 return false;
487 }
488
489 return true;
490}
491
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000492///////////////////////////////////////////////////////////////////////////////
493
robertphillips@google.com42903302013-04-20 12:26:07 +0000494// position + edge
495extern const GrVertexAttrib gCircleVertexAttribs[] = {
496 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
497 {kVec4f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding}
498};
499
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000500void GrOvalRenderer::drawCircle(GrDrawTarget* target,
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000501 bool useAA,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000502 const SkRect& circle,
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000503 const SkStrokeRec& stroke)
504{
505 GrDrawState* drawState = target->drawState();
506
507 const SkMatrix& vm = drawState->getViewMatrix();
508 GrPoint center = GrPoint::Make(circle.centerX(), circle.centerY());
509 vm.mapPoints(&center, 1);
510 SkScalar radius = vm.mapRadius(SkScalarHalf(circle.width()));
511 SkScalar strokeWidth = vm.mapRadius(stroke.getWidth());
512
bsalomon@google.com137f1342013-05-29 21:27:53 +0000513 GrDrawState::AutoViewMatrixRestore avmr;
514 if (!avmr.setIdentity(drawState)) {
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000515 return;
516 }
517
robertphillips@google.com42903302013-04-20 12:26:07 +0000518 drawState->setVertexAttribs<gCircleVertexAttribs>(SK_ARRAY_COUNT(gCircleVertexAttribs));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000519 SkASSERT(sizeof(CircleVertex) == drawState->getVertexSize());
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000520
521 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
522 if (!geo.succeeded()) {
523 GrPrintf("Failed to get space for vertices!\n");
524 return;
525 }
526
527 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
528
529 SkStrokeRec::Style style = stroke.getStyle();
530 bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style);
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000531
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000532 SkScalar innerRadius = 0.0f;
533 SkScalar outerRadius = radius;
534 SkScalar halfWidth = 0;
535 if (style != SkStrokeRec::kFill_Style) {
536 if (SkScalarNearlyZero(strokeWidth)) {
537 halfWidth = SK_ScalarHalf;
538 } else {
539 halfWidth = SkScalarHalf(strokeWidth);
540 }
541
542 outerRadius += halfWidth;
543 if (isStroked) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000544 innerRadius = radius - halfWidth;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000545 }
546 }
547
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +0000548 GrEffectRef* effect = CircleEdgeEffect::Create(isStroked && innerRadius > 0);
549 static const int kCircleEdgeAttrIndex = 1;
550 drawState->addCoverageEffect(effect, kCircleEdgeAttrIndex)->unref();
551
bsalomon@google.com58e30fe2013-04-01 19:01:20 +0000552 // The radii are outset for two reasons. First, it allows the shader to simply perform
553 // clamp(distance-to-center - radius, 0, 1). Second, the outer radius is used to compute the
554 // verts of the bounding box that is rendered and the outset ensures the box will cover all
555 // pixels partially covered by the circle.
556 outerRadius += SK_ScalarHalf;
557 innerRadius -= SK_ScalarHalf;
558
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000559 SkRect bounds = SkRect::MakeLTRB(
bsalomon@google.com58e30fe2013-04-01 19:01:20 +0000560 center.fX - outerRadius,
561 center.fY - outerRadius,
562 center.fX + outerRadius,
563 center.fY + outerRadius
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000564 );
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000565
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000566 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000567 verts[0].fOffset = SkPoint::Make(-outerRadius, -outerRadius);
568 verts[0].fOuterRadius = outerRadius;
569 verts[0].fInnerRadius = innerRadius;
570
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000571 verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
skia.committer@gmail.com46746762013-04-12 07:01:51 +0000572 verts[1].fOffset = SkPoint::Make(outerRadius, -outerRadius);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000573 verts[1].fOuterRadius = outerRadius;
574 verts[1].fInnerRadius = innerRadius;
575
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000576 verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000577 verts[2].fOffset = SkPoint::Make(-outerRadius, outerRadius);
578 verts[2].fOuterRadius = outerRadius;
579 verts[2].fInnerRadius = innerRadius;
580
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000581 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000582 verts[3].fOffset = SkPoint::Make(outerRadius, outerRadius);
583 verts[3].fOuterRadius = outerRadius;
584 verts[3].fInnerRadius = innerRadius;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000585
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000586 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000587}
588
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000589///////////////////////////////////////////////////////////////////////////////
590
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000591// position + offset + 1/radii
robertphillips@google.com42903302013-04-20 12:26:07 +0000592extern const GrVertexAttrib gEllipseVertexAttribs[] = {
593 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
594 {kVec2f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding},
595 {kVec4f_GrVertexAttribType, 2*sizeof(GrPoint), kEffect_GrVertexAttribBinding}
596};
597
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000598// position + offsets
599extern const GrVertexAttrib gDIEllipseVertexAttribs[] = {
600 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000601 {kVec2f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding},
602 {kVec2f_GrVertexAttribType, 2*sizeof(GrPoint), kEffect_GrVertexAttribBinding},
robertphillips@google.com42903302013-04-20 12:26:07 +0000603};
604
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000605bool GrOvalRenderer::drawEllipse(GrDrawTarget* target,
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000606 bool useAA,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000607 const SkRect& ellipse,
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000608 const SkStrokeRec& stroke)
609{
610 GrDrawState* drawState = target->drawState();
611#ifdef SK_DEBUG
612 {
613 // we should have checked for this previously
614 bool isAxisAlignedEllipse = drawState->getViewMatrix().rectStaysRect();
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000615 SkASSERT(useAA && isAxisAlignedEllipse);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000616 }
617#endif
618
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000619 // do any matrix crunching before we reset the draw state for device coords
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000620 const SkMatrix& vm = drawState->getViewMatrix();
621 GrPoint center = GrPoint::Make(ellipse.centerX(), ellipse.centerY());
622 vm.mapPoints(&center, 1);
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000623 SkScalar ellipseXRadius = SkScalarHalf(ellipse.width());
624 SkScalar ellipseYRadius = SkScalarHalf(ellipse.height());
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +0000625 SkScalar xRadius = SkScalarAbs(vm[SkMatrix::kMScaleX]*ellipseXRadius +
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000626 vm[SkMatrix::kMSkewY]*ellipseYRadius);
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +0000627 SkScalar yRadius = SkScalarAbs(vm[SkMatrix::kMSkewX]*ellipseXRadius +
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000628 vm[SkMatrix::kMScaleY]*ellipseYRadius);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000629
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000630 // do (potentially) anisotropic mapping of stroke
631 SkVector scaledStroke;
632 SkScalar strokeWidth = stroke.getWidth();
633 scaledStroke.fX = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMScaleX] + vm[SkMatrix::kMSkewY]));
634 scaledStroke.fY = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMSkewX] + vm[SkMatrix::kMScaleY]));
635
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000636 SkStrokeRec::Style style = stroke.getStyle();
637 bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style);
638
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000639 SkScalar innerXRadius = 0;
640 SkScalar innerYRadius = 0;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000641 if (SkStrokeRec::kFill_Style != style) {
642 if (SkScalarNearlyZero(scaledStroke.length())) {
643 scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf);
644 } else {
645 scaledStroke.scale(SK_ScalarHalf);
646 }
647
648 // we only handle thick strokes for near-circular ellipses
649 if (scaledStroke.length() > SK_ScalarHalf &&
650 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
651 return false;
652 }
653
654 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
655 if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius ||
656 scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) {
657 return false;
658 }
659
660 // this is legit only if scale & translation (which should be the case at the moment)
661 if (isStroked) {
662 innerXRadius = xRadius - scaledStroke.fX;
663 innerYRadius = yRadius - scaledStroke.fY;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000664 }
665
666 xRadius += scaledStroke.fX;
667 yRadius += scaledStroke.fY;
668 }
669
bsalomon@google.com137f1342013-05-29 21:27:53 +0000670 GrDrawState::AutoViewMatrixRestore avmr;
671 if (!avmr.setIdentity(drawState)) {
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000672 return false;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000673 }
674
robertphillips@google.com42903302013-04-20 12:26:07 +0000675 drawState->setVertexAttribs<gEllipseVertexAttribs>(SK_ARRAY_COUNT(gEllipseVertexAttribs));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000676 SkASSERT(sizeof(EllipseVertex) == drawState->getVertexSize());
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000677
678 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
679 if (!geo.succeeded()) {
680 GrPrintf("Failed to get space for vertices!\n");
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000681 return false;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000682 }
683
684 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices());
685
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +0000686 GrEffectRef* effect = EllipseEdgeEffect::Create(isStroked &&
687 innerXRadius > 0 && innerYRadius > 0);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000688
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000689 static const int kEllipseCenterAttrIndex = 1;
jvanverth@google.com6cc8d442013-09-10 18:24:37 +0000690 static const int kEllipseEdgeAttrIndex = 2;
691 drawState->addCoverageEffect(effect, kEllipseCenterAttrIndex, kEllipseEdgeAttrIndex)->unref();
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000692
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000693 // Compute the reciprocals of the radii here to save time in the shader
694 SkScalar xRadRecip = SkScalarInvert(xRadius);
695 SkScalar yRadRecip = SkScalarInvert(yRadius);
696 SkScalar xInnerRadRecip = SkScalarInvert(innerXRadius);
697 SkScalar yInnerRadRecip = SkScalarInvert(innerYRadius);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000698
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000699 // We've extended the outer x radius out half a pixel to antialias.
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000700 // This will also expand the rect so all the pixels will be captured.
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000701 // TODO: Consider if we should use sqrt(2)/2 instead
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000702 xRadius += SK_ScalarHalf;
703 yRadius += SK_ScalarHalf;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000704
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000705 SkRect bounds = SkRect::MakeLTRB(
706 center.fX - xRadius,
707 center.fY - yRadius,
708 center.fX + xRadius,
709 center.fY + yRadius
710 );
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000711
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000712 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000713 verts[0].fOffset = SkPoint::Make(-xRadius, -yRadius);
714 verts[0].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
715 verts[0].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000716
717 verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000718 verts[1].fOffset = SkPoint::Make(xRadius, -yRadius);
719 verts[1].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
720 verts[1].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000721
722 verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000723 verts[2].fOffset = SkPoint::Make(-xRadius, yRadius);
724 verts[2].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
725 verts[2].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000726
727 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000728 verts[3].fOffset = SkPoint::Make(xRadius, yRadius);
729 verts[3].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
730 verts[3].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
skia.committer@gmail.com46746762013-04-12 07:01:51 +0000731
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000732 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds);
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000733
734 return true;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000735}
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000736
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000737bool GrOvalRenderer::drawDIEllipse(GrDrawTarget* target,
738 bool useAA,
739 const SkRect& ellipse,
740 const SkStrokeRec& stroke)
741{
742 GrDrawState* drawState = target->drawState();
743 const SkMatrix& vm = drawState->getViewMatrix();
744
745 GrPoint center = GrPoint::Make(ellipse.centerX(), ellipse.centerY());
746 SkScalar xRadius = SkScalarHalf(ellipse.width());
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000747 SkScalar yRadius = SkScalarHalf(ellipse.height());
748
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000749 SkStrokeRec::Style style = stroke.getStyle();
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000750 DIEllipseEdgeEffect::Mode mode = (SkStrokeRec::kStroke_Style == style) ?
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000751 DIEllipseEdgeEffect::kStroke :
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000752 (SkStrokeRec::kHairline_Style == style) ?
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000753 DIEllipseEdgeEffect::kHairline : DIEllipseEdgeEffect::kFill;
754
755 SkScalar innerXRadius = 0;
756 SkScalar innerYRadius = 0;
757 if (SkStrokeRec::kFill_Style != style && SkStrokeRec::kHairline_Style != style) {
758 SkScalar strokeWidth = stroke.getWidth();
759
760 if (SkScalarNearlyZero(strokeWidth)) {
761 strokeWidth = SK_ScalarHalf;
762 } else {
763 strokeWidth *= SK_ScalarHalf;
764 }
765
766 // we only handle thick strokes for near-circular ellipses
767 if (strokeWidth > SK_ScalarHalf &&
768 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
769 return false;
770 }
771
772 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
773 if (strokeWidth*(yRadius*yRadius) < (strokeWidth*strokeWidth)*xRadius ||
774 strokeWidth*(xRadius*xRadius) < (strokeWidth*strokeWidth)*yRadius) {
775 return false;
776 }
777
778 // set inner radius (if needed)
779 if (SkStrokeRec::kStroke_Style == style) {
780 innerXRadius = xRadius - strokeWidth;
781 innerYRadius = yRadius - strokeWidth;
782 }
783
784 xRadius += strokeWidth;
785 yRadius += strokeWidth;
786 }
787 if (DIEllipseEdgeEffect::kStroke == mode) {
788 mode = (innerXRadius > 0 && innerYRadius > 0) ? DIEllipseEdgeEffect::kStroke :
789 DIEllipseEdgeEffect::kFill;
790 }
791 SkScalar innerRatioX = SkScalarDiv(xRadius, innerXRadius);
792 SkScalar innerRatioY = SkScalarDiv(yRadius, innerYRadius);
793
794 drawState->setVertexAttribs<gDIEllipseVertexAttribs>(SK_ARRAY_COUNT(gDIEllipseVertexAttribs));
795 SkASSERT(sizeof(DIEllipseVertex) == drawState->getVertexSize());
796
797 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
798 if (!geo.succeeded()) {
799 GrPrintf("Failed to get space for vertices!\n");
800 return false;
801 }
802
803 DIEllipseVertex* verts = reinterpret_cast<DIEllipseVertex*>(geo.vertices());
804
805 GrEffectRef* effect = DIEllipseEdgeEffect::Create(mode);
806
807 static const int kEllipseOuterOffsetAttrIndex = 1;
808 static const int kEllipseInnerOffsetAttrIndex = 2;
809 drawState->addCoverageEffect(effect, kEllipseOuterOffsetAttrIndex,
810 kEllipseInnerOffsetAttrIndex)->unref();
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000811
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000812 // This expands the outer rect so that after CTM we end up with a half-pixel border
813 SkScalar a = vm[SkMatrix::kMScaleX];
814 SkScalar b = vm[SkMatrix::kMSkewX];
815 SkScalar c = vm[SkMatrix::kMSkewY];
816 SkScalar d = vm[SkMatrix::kMScaleY];
817 SkScalar geoDx = SkScalarDiv(SK_ScalarHalf, SkScalarSqrt(a*a + c*c));
818 SkScalar geoDy = SkScalarDiv(SK_ScalarHalf, SkScalarSqrt(b*b + d*d));
819 // This adjusts the "radius" to include the half-pixel border
820 SkScalar offsetDx = SkScalarDiv(geoDx, xRadius);
821 SkScalar offsetDy = SkScalarDiv(geoDy, yRadius);
822
823 SkRect bounds = SkRect::MakeLTRB(
824 center.fX - xRadius - geoDx,
825 center.fY - yRadius - geoDy,
826 center.fX + xRadius + geoDx,
827 center.fY + yRadius + geoDy
828 );
829
830 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
831 verts[0].fOuterOffset = SkPoint::Make(-1.0f - offsetDx, -1.0f - offsetDy);
832 verts[0].fInnerOffset = SkPoint::Make(-innerRatioX - offsetDx, -innerRatioY - offsetDy);
833
834 verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
835 verts[1].fOuterOffset = SkPoint::Make(1.0f + offsetDx, -1.0f - offsetDy);
836 verts[1].fInnerOffset = SkPoint::Make(innerRatioX + offsetDx, -innerRatioY - offsetDy);
837
838 verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
839 verts[2].fOuterOffset = SkPoint::Make(-1.0f - offsetDx, 1.0f + offsetDy);
840 verts[2].fInnerOffset = SkPoint::Make(-innerRatioX - offsetDx, innerRatioY + offsetDy);
841
842 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
843 verts[3].fOuterOffset = SkPoint::Make(1.0f + offsetDx, 1.0f + offsetDy);
844 verts[3].fInnerOffset = SkPoint::Make(innerRatioX + offsetDx, innerRatioY + offsetDy);
845
846 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds);
847
848 return true;
849}
850
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000851///////////////////////////////////////////////////////////////////////////////
852
853static const uint16_t gRRectIndices[] = {
854 // corners
855 0, 1, 5, 0, 5, 4,
856 2, 3, 7, 2, 7, 6,
857 8, 9, 13, 8, 13, 12,
858 10, 11, 15, 10, 15, 14,
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000859
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000860 // edges
861 1, 2, 6, 1, 6, 5,
862 4, 5, 9, 4, 9, 8,
863 6, 7, 11, 6, 11, 10,
864 9, 10, 14, 9, 14, 13,
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000865
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000866 // center
867 // we place this at the end so that we can ignore these indices when rendering stroke-only
868 5, 6, 10, 5, 10, 9
869};
870
871
872GrIndexBuffer* GrOvalRenderer::rRectIndexBuffer(GrGpu* gpu) {
873 if (NULL == fRRectIndexBuffer) {
874 fRRectIndexBuffer =
875 gpu->createIndexBuffer(sizeof(gRRectIndices), false);
876 if (NULL != fRRectIndexBuffer) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000877#ifdef SK_DEBUG
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000878 bool updated =
879#endif
880 fRRectIndexBuffer->updateData(gRRectIndices,
881 sizeof(gRRectIndices));
882 GR_DEBUGASSERT(updated);
883 }
884 }
885 return fRRectIndexBuffer;
886}
887
skia.committer@gmail.com2fd42c42013-05-03 07:01:00 +0000888bool GrOvalRenderer::drawSimpleRRect(GrDrawTarget* target, GrContext* context, bool useAA,
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000889 const SkRRect& rrect, const SkStrokeRec& stroke)
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000890{
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000891 // only anti-aliased rrects for now
892 if (!useAA) {
893 return false;
894 }
895
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000896 const SkMatrix& vm = context->getMatrix();
897#ifdef SK_DEBUG
898 {
899 // we should have checked for this previously
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000900 SkASSERT(useAA && vm.rectStaysRect() && rrect.isSimple());
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000901 }
902#endif
903
904 // do any matrix crunching before we reset the draw state for device coords
905 const SkRect& rrectBounds = rrect.getBounds();
906 SkRect bounds;
907 vm.mapRect(&bounds, rrectBounds);
908
909 SkVector radii = rrect.getSimpleRadii();
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000910 SkScalar xRadius = SkScalarAbs(vm[SkMatrix::kMScaleX]*radii.fX +
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000911 vm[SkMatrix::kMSkewY]*radii.fY);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000912 SkScalar yRadius = SkScalarAbs(vm[SkMatrix::kMSkewX]*radii.fX +
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000913 vm[SkMatrix::kMScaleY]*radii.fY);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000914
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000915 // if hairline stroke is greater than radius, we don't handle that right now
916 SkStrokeRec::Style style = stroke.getStyle();
917 if (SkStrokeRec::kHairline_Style == style &&
918 (SK_ScalarHalf >= xRadius || SK_ScalarHalf >= yRadius)) {
919 return false;
920 }
921
922 // do (potentially) anisotropic mapping of stroke
923 SkVector scaledStroke;
924 SkScalar strokeWidth = stroke.getWidth();
925 scaledStroke.fX = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMScaleX] + vm[SkMatrix::kMSkewY]));
926 scaledStroke.fY = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMSkewX] + vm[SkMatrix::kMScaleY]));
927
928 // if half of strokewidth is greater than radius, we don't handle that right now
929 if (SK_ScalarHalf*scaledStroke.fX >= xRadius || SK_ScalarHalf*scaledStroke.fY >= yRadius) {
930 return false;
931 }
932
933 // reset to device coordinates
934 GrDrawState* drawState = target->drawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000935 GrDrawState::AutoViewMatrixRestore avmr;
936 if (!avmr.setIdentity(drawState)) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000937 return false;
938 }
939
940 bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style);
941
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000942 GrIndexBuffer* indexBuffer = this->rRectIndexBuffer(context->getGpu());
943 if (NULL == indexBuffer) {
944 GrPrintf("Failed to create index buffer!\n");
945 return false;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000946 }
947
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000948 // if the corners are circles, use the circle renderer
949 if ((!isStroked || scaledStroke.fX == scaledStroke.fY) && xRadius == yRadius) {
950 drawState->setVertexAttribs<gCircleVertexAttribs>(SK_ARRAY_COUNT(gCircleVertexAttribs));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000951 SkASSERT(sizeof(CircleVertex) == drawState->getVertexSize());
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000952
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000953 GrDrawTarget::AutoReleaseGeometry geo(target, 16, 0);
954 if (!geo.succeeded()) {
955 GrPrintf("Failed to get space for vertices!\n");
956 return false;
957 }
958 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000959
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000960 SkScalar innerRadius = 0.0f;
961 SkScalar outerRadius = xRadius;
962 SkScalar halfWidth = 0;
963 if (style != SkStrokeRec::kFill_Style) {
964 if (SkScalarNearlyZero(scaledStroke.fX)) {
965 halfWidth = SK_ScalarHalf;
966 } else {
967 halfWidth = SkScalarHalf(scaledStroke.fX);
968 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000969
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000970 if (isStroked) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000971 innerRadius = xRadius - halfWidth;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000972 }
973 outerRadius += halfWidth;
974 bounds.outset(halfWidth, halfWidth);
975 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000976
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000977 isStroked = (isStroked && innerRadius > 0);
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +0000978
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000979 GrEffectRef* effect = CircleEdgeEffect::Create(isStroked);
980 static const int kCircleEdgeAttrIndex = 1;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000981 drawState->addCoverageEffect(effect, kCircleEdgeAttrIndex)->unref();
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +0000982
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000983 // The radii are outset for two reasons. First, it allows the shader to simply perform
984 // clamp(distance-to-center - radius, 0, 1). Second, the outer radius is used to compute the
985 // verts of the bounding box that is rendered and the outset ensures the box will cover all
986 // pixels partially covered by the circle.
987 outerRadius += SK_ScalarHalf;
988 innerRadius -= SK_ScalarHalf;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000989
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000990 // Expand the rect so all the pixels will be captured.
991 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000992
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000993 SkScalar yCoords[4] = {
994 bounds.fTop,
995 bounds.fTop + outerRadius,
996 bounds.fBottom - outerRadius,
997 bounds.fBottom
998 };
999 SkScalar yOuterRadii[4] = {
1000 -outerRadius,
1001 0,
1002 0,
1003 outerRadius
1004 };
1005 for (int i = 0; i < 4; ++i) {
1006 verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]);
1007 verts->fOffset = SkPoint::Make(-outerRadius, yOuterRadii[i]);
1008 verts->fOuterRadius = outerRadius;
1009 verts->fInnerRadius = innerRadius;
1010 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001011
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001012 verts->fPos = SkPoint::Make(bounds.fLeft + outerRadius, yCoords[i]);
1013 verts->fOffset = SkPoint::Make(0, yOuterRadii[i]);
1014 verts->fOuterRadius = outerRadius;
1015 verts->fInnerRadius = innerRadius;
1016 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001017
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001018 verts->fPos = SkPoint::Make(bounds.fRight - outerRadius, yCoords[i]);
1019 verts->fOffset = SkPoint::Make(0, yOuterRadii[i]);
1020 verts->fOuterRadius = outerRadius;
1021 verts->fInnerRadius = innerRadius;
1022 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001023
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001024 verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]);
1025 verts->fOffset = SkPoint::Make(outerRadius, yOuterRadii[i]);
1026 verts->fOuterRadius = outerRadius;
1027 verts->fInnerRadius = innerRadius;
1028 verts++;
1029 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001030
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001031 // drop out the middle quad if we're stroked
1032 int indexCnt = isStroked ? GR_ARRAY_COUNT(gRRectIndices)-6 : GR_ARRAY_COUNT(gRRectIndices);
1033 target->setIndexSourceToBuffer(indexBuffer);
1034 target->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, 16, indexCnt, &bounds);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001035
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001036 // otherwise we use the ellipse renderer
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001037 } else {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001038 drawState->setVertexAttribs<gEllipseVertexAttribs>(SK_ARRAY_COUNT(gEllipseVertexAttribs));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001039 SkASSERT(sizeof(EllipseVertex) == drawState->getVertexSize());
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001040
1041 SkScalar innerXRadius = 0.0f;
1042 SkScalar innerYRadius = 0.0f;
1043 if (SkStrokeRec::kFill_Style != style) {
1044 if (SkScalarNearlyZero(scaledStroke.length())) {
1045 scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf);
1046 } else {
1047 scaledStroke.scale(SK_ScalarHalf);
1048 }
1049
1050 // we only handle thick strokes for near-circular ellipses
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +00001051 if (scaledStroke.length() > SK_ScalarHalf &&
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001052 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
1053 return false;
1054 }
1055
1056 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
1057 if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius ||
1058 scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) {
1059 return false;
1060 }
1061
1062 // this is legit only if scale & translation (which should be the case at the moment)
1063 if (isStroked) {
1064 innerXRadius = xRadius - scaledStroke.fX;
1065 innerYRadius = yRadius - scaledStroke.fY;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001066 }
1067
1068 xRadius += scaledStroke.fX;
1069 yRadius += scaledStroke.fY;
1070 bounds.outset(scaledStroke.fX, scaledStroke.fY);
1071 }
jvanverth@google.come3647412013-05-08 15:31:43 +00001072
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +00001073 isStroked = (isStroked && innerXRadius > 0 && innerYRadius > 0);
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +00001074
jvanverth@google.come3647412013-05-08 15:31:43 +00001075 GrDrawTarget::AutoReleaseGeometry geo(target, 16, 0);
1076 if (!geo.succeeded()) {
1077 GrPrintf("Failed to get space for vertices!\n");
1078 return false;
1079 }
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001080 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices());
jvanverth@google.come3647412013-05-08 15:31:43 +00001081
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001082 GrEffectRef* effect = EllipseEdgeEffect::Create(isStroked);
jvanverth@google.come3647412013-05-08 15:31:43 +00001083 static const int kEllipseOffsetAttrIndex = 1;
1084 static const int kEllipseRadiiAttrIndex = 2;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001085 drawState->addCoverageEffect(effect,
1086 kEllipseOffsetAttrIndex, kEllipseRadiiAttrIndex)->unref();
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001087
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001088 // Compute the reciprocals of the radii here to save time in the shader
1089 SkScalar xRadRecip = SkScalarInvert(xRadius);
1090 SkScalar yRadRecip = SkScalarInvert(yRadius);
1091 SkScalar xInnerRadRecip = SkScalarInvert(innerXRadius);
1092 SkScalar yInnerRadRecip = SkScalarInvert(innerYRadius);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001093
1094 // Extend the radii out half a pixel to antialias.
1095 SkScalar xOuterRadius = xRadius + SK_ScalarHalf;
1096 SkScalar yOuterRadius = yRadius + SK_ScalarHalf;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001097
1098 // Expand the rect so all the pixels will be captured.
1099 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1100
1101 SkScalar yCoords[4] = {
1102 bounds.fTop,
1103 bounds.fTop + yOuterRadius,
1104 bounds.fBottom - yOuterRadius,
1105 bounds.fBottom
1106 };
1107 SkScalar yOuterOffsets[4] = {
jvanverth@google.comd1b5b142013-07-02 14:46:03 +00001108 yOuterRadius,
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001109 SK_ScalarNearlyZero, // we're using inversesqrt() in the shader, so can't be exactly 0
1110 SK_ScalarNearlyZero,
1111 yOuterRadius
1112 };
1113
1114 for (int i = 0; i < 4; ++i) {
1115 verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]);
jvanverth@google.comd1b5b142013-07-02 14:46:03 +00001116 verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001117 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1118 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001119 verts++;
1120
1121 verts->fPos = SkPoint::Make(bounds.fLeft + xOuterRadius, yCoords[i]);
1122 verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001123 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1124 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001125 verts++;
1126
1127 verts->fPos = SkPoint::Make(bounds.fRight - xOuterRadius, yCoords[i]);
1128 verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001129 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1130 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001131 verts++;
1132
1133 verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]);
1134 verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001135 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1136 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001137 verts++;
1138 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001139
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001140 // drop out the middle quad if we're stroked
1141 int indexCnt = isStroked ? GR_ARRAY_COUNT(gRRectIndices)-6 : GR_ARRAY_COUNT(gRRectIndices);
1142 target->setIndexSourceToBuffer(indexBuffer);
1143 target->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, 16, indexCnt, &bounds);
1144 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001145
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001146 return true;
1147}