blob: 946cdc06ae0ba518b8259c083fe9d32ee839fc2a [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
commit-bot@chromium.orga91f0312013-09-06 20:19:56 +000094 virtual bool requiresVertexShader(const GrDrawEffect&) const SK_OVERRIDE { return true; }
95
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000096 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 {
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +0000102 GrGLShaderBuilder::VertexBuilder* vertexBuilder = builder->getVertexBuilder();
103 SkASSERT(NULL != vertexBuilder);
104
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000105 const CircleEdgeEffect& circleEffect = drawEffect.castEffect<CircleEdgeEffect>();
106 const char *vsName, *fsName;
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +0000107 vertexBuilder->addVarying(kVec4f_GrSLType, "CircleEdge", &vsName, &fsName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000108
109 const SkString* attrName =
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +0000110 vertexBuilder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]);
111 vertexBuilder->vsCodeAppendf("\t%s = %s;\n", vsName, attrName->c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000112
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000113 builder->fsCodeAppendf("\tfloat d = length(%s.xy);\n", fsName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000114 builder->fsCodeAppendf("\tfloat edgeAlpha = clamp(%s.z - d, 0.0, 1.0);\n", fsName);
115 if (circleEffect.isStroked()) {
116 builder->fsCodeAppendf("\tfloat innerAlpha = clamp(d - %s.w, 0.0, 1.0);\n", fsName);
117 builder->fsCodeAppend("\tedgeAlpha *= innerAlpha;\n");
118 }
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000119
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000120 SkString modulate;
bsalomon@google.com018f1792013-04-18 19:36:09 +0000121 GrGLSLModulatef<4>(&modulate, inputColor, "edgeAlpha");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000122 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str());
123 }
124
125 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
126 const CircleEdgeEffect& circleEffect = drawEffect.castEffect<CircleEdgeEffect>();
127
128 return circleEffect.isStroked() ? 0x1 : 0x0;
129 }
130
131 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {}
132
133 private:
134 typedef GrGLEffect INHERITED;
135 };
136
137
138private:
139 CircleEdgeEffect(bool stroke) : GrEffect() {
140 this->addVertexAttrib(kVec4f_GrSLType);
141 fStroke = stroke;
142 }
143
144 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
145 const CircleEdgeEffect& cee = CastEffect<CircleEdgeEffect>(other);
146 return cee.fStroke == fStroke;
147 }
148
149 bool fStroke;
150
151 GR_DECLARE_EFFECT_TEST;
152
153 typedef GrEffect INHERITED;
154};
155
156GR_DEFINE_EFFECT_TEST(CircleEdgeEffect);
157
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000158GrEffectRef* CircleEdgeEffect::TestCreate(SkRandom* random,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000159 GrContext* context,
160 const GrDrawTargetCaps&,
161 GrTexture* textures[]) {
162 return CircleEdgeEffect::Create(random->nextBool());
163}
164
165///////////////////////////////////////////////////////////////////////////////
166
167/**
168 * 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 +0000169 * ellipse, specified as a 2D offset from center, and the reciprocals of the outer and inner radii,
170 * in both x and y directions.
171 *
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000172 * 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 +0000173 */
174
175class EllipseEdgeEffect : public GrEffect {
176public:
177 static GrEffectRef* Create(bool stroke) {
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000178 GR_CREATE_STATIC_EFFECT(gEllipseStrokeEdge, EllipseEdgeEffect, (true));
179 GR_CREATE_STATIC_EFFECT(gEllipseFillEdge, EllipseEdgeEffect, (false));
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000180
181 if (stroke) {
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000182 gEllipseStrokeEdge->ref();
183 return gEllipseStrokeEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000184 } else {
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000185 gEllipseFillEdge->ref();
186 return gEllipseFillEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000187 }
188 }
189
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000190 virtual void getConstantColorComponents(GrColor* color,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000191 uint32_t* validFlags) const SK_OVERRIDE {
192 *validFlags = 0;
193 }
194
195 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
196 return GrTBackendEffectFactory<EllipseEdgeEffect>::getInstance();
197 }
198
199 virtual ~EllipseEdgeEffect() {}
200
201 static const char* Name() { return "EllipseEdge"; }
202
203 inline bool isStroked() const { return fStroke; }
204
205 class GLEffect : public GrGLEffect {
206 public:
207 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
208 : INHERITED (factory) {}
209
commit-bot@chromium.orga91f0312013-09-06 20:19:56 +0000210 virtual bool requiresVertexShader(const GrDrawEffect&) const SK_OVERRIDE { return true; }
211
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000212 virtual void emitCode(GrGLShaderBuilder* builder,
213 const GrDrawEffect& drawEffect,
214 EffectKey key,
215 const char* outputColor,
216 const char* inputColor,
217 const TextureSamplerArray& samplers) SK_OVERRIDE {
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +0000218 GrGLShaderBuilder::VertexBuilder* vertexBuilder = builder->getVertexBuilder();
219 SkASSERT(NULL != vertexBuilder);
220
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000221 const EllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<EllipseEdgeEffect>();
222
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000223 const char *vsOffsetName, *fsOffsetName;
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000224 const char *vsRadiiName, *fsRadiiName;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000225
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +0000226 vertexBuilder->addVarying(kVec2f_GrSLType, "EllipseOffsets", &vsOffsetName, &fsOffsetName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000227 const SkString* attr0Name =
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +0000228 vertexBuilder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]);
229 vertexBuilder->vsCodeAppendf("\t%s = %s;\n", vsOffsetName, attr0Name->c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000230
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +0000231 vertexBuilder->addVarying(kVec4f_GrSLType, "EllipseRadii", &vsRadiiName, &fsRadiiName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000232 const SkString* attr1Name =
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +0000233 vertexBuilder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[1]);
234 vertexBuilder->vsCodeAppendf("\t%s = %s;\n", vsRadiiName, attr1Name->c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000235
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +0000236 // for outer curve
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000237 builder->fsCodeAppendf("\tvec2 scaledOffset = %s*%s.xy;\n", fsOffsetName, fsRadiiName);
238 builder->fsCodeAppend("\tfloat test = dot(scaledOffset, scaledOffset) - 1.0;\n");
239 builder->fsCodeAppendf("\tvec2 grad = 2.0*scaledOffset*%s.xy;\n", fsRadiiName);
jvanverth@google.comd1b5b142013-07-02 14:46:03 +0000240 builder->fsCodeAppend("\tfloat grad_dot = dot(grad, grad);\n");
241 // we need to clamp the length^2 of the gradiant vector to a non-zero value, because
242 // on the Nexus 4 the undefined result of inversesqrt(0) drops out an entire tile
243 // TODO: restrict this to Adreno-only
244 builder->fsCodeAppend("\tgrad_dot = max(grad_dot, 1.0e-4);\n");
245 builder->fsCodeAppend("\tfloat invlen = inversesqrt(grad_dot);\n");
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000246 builder->fsCodeAppend("\tfloat edgeAlpha = clamp(0.5-test*invlen, 0.0, 1.0);\n");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000247
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000248 // for inner curve
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000249 if (ellipseEffect.isStroked()) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000250 builder->fsCodeAppendf("\tscaledOffset = %s*%s.zw;\n", fsOffsetName, fsRadiiName);
251 builder->fsCodeAppend("\ttest = dot(scaledOffset, scaledOffset) - 1.0;\n");
252 builder->fsCodeAppendf("\tgrad = 2.0*scaledOffset*%s.zw;\n", fsRadiiName);
253 builder->fsCodeAppend("\tinvlen = inversesqrt(dot(grad, grad));\n");
254 builder->fsCodeAppend("\tedgeAlpha *= clamp(0.5+test*invlen, 0.0, 1.0);\n");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000255 }
256
257 SkString modulate;
bsalomon@google.com018f1792013-04-18 19:36:09 +0000258 GrGLSLModulatef<4>(&modulate, inputColor, "edgeAlpha");
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000259 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000260 }
261
262 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
263 const EllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<EllipseEdgeEffect>();
264
265 return ellipseEffect.isStroked() ? 0x1 : 0x0;
266 }
267
268 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {
269 }
270
271 private:
272 typedef GrGLEffect INHERITED;
273 };
274
275private:
276 EllipseEdgeEffect(bool stroke) : GrEffect() {
277 this->addVertexAttrib(kVec2f_GrSLType);
278 this->addVertexAttrib(kVec4f_GrSLType);
279 fStroke = stroke;
280 }
281
282 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
283 const EllipseEdgeEffect& eee = CastEffect<EllipseEdgeEffect>(other);
284 return eee.fStroke == fStroke;
285 }
286
287 bool fStroke;
288
289 GR_DECLARE_EFFECT_TEST;
290
291 typedef GrEffect INHERITED;
292};
293
294GR_DEFINE_EFFECT_TEST(EllipseEdgeEffect);
295
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000296GrEffectRef* EllipseEdgeEffect::TestCreate(SkRandom* random,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000297 GrContext* context,
298 const GrDrawTargetCaps&,
299 GrTexture* textures[]) {
300 return EllipseEdgeEffect::Create(random->nextBool());
301}
302
303///////////////////////////////////////////////////////////////////////////////
304
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000305/**
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000306 * 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 +0000307 * specified as a 2D offset from center for both the outer and inner paths (if stroked). The
308 * implict equation used is for a unit circle (x^2 + y^2 - 1 = 0) and the edge corrected by
309 * using differentials.
310 *
311 * The result is device-independent and can be used with any affine matrix.
312 */
313
314class DIEllipseEdgeEffect : public GrEffect {
315public:
316 enum Mode { kStroke = 0, kHairline, kFill };
317
318 static GrEffectRef* Create(Mode mode) {
319 GR_CREATE_STATIC_EFFECT(gEllipseStrokeEdge, DIEllipseEdgeEffect, (kStroke));
320 GR_CREATE_STATIC_EFFECT(gEllipseHairlineEdge, DIEllipseEdgeEffect, (kHairline));
321 GR_CREATE_STATIC_EFFECT(gEllipseFillEdge, DIEllipseEdgeEffect, (kFill));
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000322
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000323 if (kStroke == mode) {
324 gEllipseStrokeEdge->ref();
325 return gEllipseStrokeEdge;
326 } else if (kHairline == mode) {
327 gEllipseHairlineEdge->ref();
328 return gEllipseHairlineEdge;
329 } else {
330 gEllipseFillEdge->ref();
331 return gEllipseFillEdge;
332 }
333 }
334
335 virtual void getConstantColorComponents(GrColor* color,
336 uint32_t* validFlags) const SK_OVERRIDE {
337 *validFlags = 0;
338 }
339
340 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
341 return GrTBackendEffectFactory<DIEllipseEdgeEffect>::getInstance();
342 }
343
344 virtual ~DIEllipseEdgeEffect() {}
345
346 static const char* Name() { return "DIEllipseEdge"; }
347
348 inline Mode getMode() const { return fMode; }
349
350 class GLEffect : public GrGLEffect {
351 public:
352 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
353 : INHERITED (factory) {}
354
355 virtual void emitCode(GrGLShaderBuilder* builder,
356 const GrDrawEffect& drawEffect,
357 EffectKey key,
358 const char* outputColor,
359 const char* inputColor,
360 const TextureSamplerArray& samplers) SK_OVERRIDE {
361 GrGLShaderBuilder::VertexBuilder* vertexBuilder = builder->getVertexBuilder();
362 SkASSERT(NULL != vertexBuilder);
363
364 const DIEllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<DIEllipseEdgeEffect>();
365
366 SkAssertResult(builder->enableFeature(
367 GrGLShaderBuilder::kStandardDerivatives_GLSLFeature));
368
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000369 const char *vsOffsetName0, *fsOffsetName0;
370 vertexBuilder->addVarying(kVec2f_GrSLType, "EllipseOffsets0",
371 &vsOffsetName0, &fsOffsetName0);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000372 const SkString* attr0Name =
373 vertexBuilder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]);
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000374 vertexBuilder->vsCodeAppendf("\t%s = %s;\n", vsOffsetName0, attr0Name->c_str());
375 const char *vsOffsetName1, *fsOffsetName1;
376 vertexBuilder->addVarying(kVec2f_GrSLType, "EllipseOffsets1",
377 &vsOffsetName1, &fsOffsetName1);
378 const SkString* attr1Name =
379 vertexBuilder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[1]);
380 vertexBuilder->vsCodeAppendf("\t%s = %s;\n", vsOffsetName1, attr1Name->c_str());
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000381
382 // for outer curve
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000383 builder->fsCodeAppendf("\tvec2 scaledOffset = %s.xy;\n", fsOffsetName0);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000384 builder->fsCodeAppend("\tfloat test = dot(scaledOffset, scaledOffset) - 1.0;\n");
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000385 builder->fsCodeAppendf("\tvec2 duvdx = dFdx(%s);\n", fsOffsetName0);
386 builder->fsCodeAppendf("\tvec2 duvdy = dFdy(%s);\n", fsOffsetName0);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000387 builder->fsCodeAppendf("\tvec2 grad = vec2(2.0*%s.x*duvdx.x + 2.0*%s.y*duvdx.y,\n"
388 "\t 2.0*%s.x*duvdy.x + 2.0*%s.y*duvdy.y);\n",
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000389 fsOffsetName0, fsOffsetName0, fsOffsetName0, fsOffsetName0);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000390
391 builder->fsCodeAppend("\tfloat grad_dot = dot(grad, grad);\n");
392 // we need to clamp the length^2 of the gradiant vector to a non-zero value, because
393 // on the Nexus 4 the undefined result of inversesqrt(0) drops out an entire tile
394 // TODO: restrict this to Adreno-only
395 builder->fsCodeAppend("\tgrad_dot = max(grad_dot, 1.0e-4);\n");
396 builder->fsCodeAppend("\tfloat invlen = inversesqrt(grad_dot);\n");
397 if (kHairline == ellipseEffect.getMode()) {
398 // can probably do this with one step
399 builder->fsCodeAppend("\tfloat edgeAlpha = clamp(1.0-test*invlen, 0.0, 1.0);\n");
400 builder->fsCodeAppend("\tedgeAlpha *= clamp(1.0+test*invlen, 0.0, 1.0);\n");
401 } else {
402 builder->fsCodeAppend("\tfloat edgeAlpha = clamp(0.5-test*invlen, 0.0, 1.0);\n");
403 }
404
405 // for inner curve
406 if (kStroke == ellipseEffect.getMode()) {
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000407 builder->fsCodeAppendf("\tscaledOffset = %s.xy;\n", fsOffsetName1);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000408 builder->fsCodeAppend("\ttest = dot(scaledOffset, scaledOffset) - 1.0;\n");
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000409 builder->fsCodeAppendf("\tduvdx = dFdx(%s);\n", fsOffsetName1);
410 builder->fsCodeAppendf("\tduvdy = dFdy(%s);\n", fsOffsetName1);
411 builder->fsCodeAppendf("\tgrad = vec2(2.0*%s.x*duvdx.x + 2.0*%s.y*duvdx.y,\n"
412 "\t 2.0*%s.x*duvdy.x + 2.0*%s.y*duvdy.y);\n",
413 fsOffsetName1, fsOffsetName1, fsOffsetName1, fsOffsetName1);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000414 builder->fsCodeAppend("\tinvlen = inversesqrt(dot(grad, grad));\n");
415 builder->fsCodeAppend("\tedgeAlpha *= clamp(0.5+test*invlen, 0.0, 1.0);\n");
416 }
417
418 SkString modulate;
419 GrGLSLModulatef<4>(&modulate, inputColor, "edgeAlpha");
420 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str());
421 }
422
423 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
424 const DIEllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<DIEllipseEdgeEffect>();
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000425
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000426 return ellipseEffect.getMode();
427 }
428
429 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {
430 }
431
432 private:
433 typedef GrGLEffect INHERITED;
434 };
435
436private:
437 DIEllipseEdgeEffect(Mode mode) : GrEffect() {
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000438 this->addVertexAttrib(kVec2f_GrSLType);
439 this->addVertexAttrib(kVec2f_GrSLType);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000440 fMode = mode;
441 }
442
443 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
444 const DIEllipseEdgeEffect& eee = CastEffect<DIEllipseEdgeEffect>(other);
445 return eee.fMode == fMode;
446 }
447
448 Mode fMode;
449
450 GR_DECLARE_EFFECT_TEST;
451
452 typedef GrEffect INHERITED;
453};
454
455GR_DEFINE_EFFECT_TEST(DIEllipseEdgeEffect);
456
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000457GrEffectRef* DIEllipseEdgeEffect::TestCreate(SkRandom* random,
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000458 GrContext* context,
459 const GrDrawTargetCaps&,
460 GrTexture* textures[]) {
461 return DIEllipseEdgeEffect::Create((Mode)(random->nextRangeU(0,2)));
462}
463
464///////////////////////////////////////////////////////////////////////////////
465
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000466void GrOvalRenderer::reset() {
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000467 SkSafeSetNull(fRRectIndexBuffer);
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000468}
469
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000470bool GrOvalRenderer::drawOval(GrDrawTarget* target, const GrContext* context, bool useAA,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000471 const SkRect& oval, const SkStrokeRec& stroke)
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000472{
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000473 if (!useAA) {
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000474 return false;
475 }
476
477 const SkMatrix& vm = context->getMatrix();
478
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000479 // we can draw circles
480 if (SkScalarNearlyEqual(oval.width(), oval.height())
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000481 && circle_stays_circle(vm)) {
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000482 this->drawCircle(target, useAA, oval, stroke);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000483 // if we have shader derivative support, render as device-independent
484 } else if (target->caps()->shaderDerivativeSupport()) {
485 return this->drawDIEllipse(target, useAA, oval, stroke);
486 // otherwise axis-aligned ellipses only
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000487 } else if (vm.rectStaysRect()) {
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000488 return this->drawEllipse(target, useAA, oval, stroke);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000489 } else {
490 return false;
491 }
492
493 return true;
494}
495
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000496///////////////////////////////////////////////////////////////////////////////
497
robertphillips@google.com42903302013-04-20 12:26:07 +0000498// position + edge
499extern const GrVertexAttrib gCircleVertexAttribs[] = {
500 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
501 {kVec4f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding}
502};
503
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000504void GrOvalRenderer::drawCircle(GrDrawTarget* target,
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000505 bool useAA,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000506 const SkRect& circle,
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000507 const SkStrokeRec& stroke)
508{
509 GrDrawState* drawState = target->drawState();
510
511 const SkMatrix& vm = drawState->getViewMatrix();
512 GrPoint center = GrPoint::Make(circle.centerX(), circle.centerY());
513 vm.mapPoints(&center, 1);
514 SkScalar radius = vm.mapRadius(SkScalarHalf(circle.width()));
515 SkScalar strokeWidth = vm.mapRadius(stroke.getWidth());
516
bsalomon@google.com137f1342013-05-29 21:27:53 +0000517 GrDrawState::AutoViewMatrixRestore avmr;
518 if (!avmr.setIdentity(drawState)) {
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000519 return;
520 }
521
robertphillips@google.com42903302013-04-20 12:26:07 +0000522 drawState->setVertexAttribs<gCircleVertexAttribs>(SK_ARRAY_COUNT(gCircleVertexAttribs));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000523 SkASSERT(sizeof(CircleVertex) == drawState->getVertexSize());
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000524
525 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
526 if (!geo.succeeded()) {
527 GrPrintf("Failed to get space for vertices!\n");
528 return;
529 }
530
531 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
532
533 SkStrokeRec::Style style = stroke.getStyle();
534 bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style);
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000535
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000536 SkScalar innerRadius = 0.0f;
537 SkScalar outerRadius = radius;
538 SkScalar halfWidth = 0;
539 if (style != SkStrokeRec::kFill_Style) {
540 if (SkScalarNearlyZero(strokeWidth)) {
541 halfWidth = SK_ScalarHalf;
542 } else {
543 halfWidth = SkScalarHalf(strokeWidth);
544 }
545
546 outerRadius += halfWidth;
547 if (isStroked) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000548 innerRadius = radius - halfWidth;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000549 }
550 }
551
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +0000552 GrEffectRef* effect = CircleEdgeEffect::Create(isStroked && innerRadius > 0);
553 static const int kCircleEdgeAttrIndex = 1;
554 drawState->addCoverageEffect(effect, kCircleEdgeAttrIndex)->unref();
555
bsalomon@google.com58e30fe2013-04-01 19:01:20 +0000556 // The radii are outset for two reasons. First, it allows the shader to simply perform
557 // clamp(distance-to-center - radius, 0, 1). Second, the outer radius is used to compute the
558 // verts of the bounding box that is rendered and the outset ensures the box will cover all
559 // pixels partially covered by the circle.
560 outerRadius += SK_ScalarHalf;
561 innerRadius -= SK_ScalarHalf;
562
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000563 SkRect bounds = SkRect::MakeLTRB(
bsalomon@google.com58e30fe2013-04-01 19:01:20 +0000564 center.fX - outerRadius,
565 center.fY - outerRadius,
566 center.fX + outerRadius,
567 center.fY + outerRadius
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000568 );
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000569
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000570 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000571 verts[0].fOffset = SkPoint::Make(-outerRadius, -outerRadius);
572 verts[0].fOuterRadius = outerRadius;
573 verts[0].fInnerRadius = innerRadius;
574
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000575 verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
skia.committer@gmail.com46746762013-04-12 07:01:51 +0000576 verts[1].fOffset = SkPoint::Make(outerRadius, -outerRadius);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000577 verts[1].fOuterRadius = outerRadius;
578 verts[1].fInnerRadius = innerRadius;
579
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000580 verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000581 verts[2].fOffset = SkPoint::Make(-outerRadius, outerRadius);
582 verts[2].fOuterRadius = outerRadius;
583 verts[2].fInnerRadius = innerRadius;
584
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000585 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000586 verts[3].fOffset = SkPoint::Make(outerRadius, outerRadius);
587 verts[3].fOuterRadius = outerRadius;
588 verts[3].fInnerRadius = innerRadius;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000589
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000590 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000591}
592
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000593///////////////////////////////////////////////////////////////////////////////
594
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000595// position + offset + 1/radii
robertphillips@google.com42903302013-04-20 12:26:07 +0000596extern const GrVertexAttrib gEllipseVertexAttribs[] = {
597 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
598 {kVec2f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding},
599 {kVec4f_GrVertexAttribType, 2*sizeof(GrPoint), kEffect_GrVertexAttribBinding}
600};
601
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000602// position + offsets
603extern const GrVertexAttrib gDIEllipseVertexAttribs[] = {
604 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000605 {kVec2f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding},
606 {kVec2f_GrVertexAttribType, 2*sizeof(GrPoint), kEffect_GrVertexAttribBinding},
robertphillips@google.com42903302013-04-20 12:26:07 +0000607};
608
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000609bool GrOvalRenderer::drawEllipse(GrDrawTarget* target,
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000610 bool useAA,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000611 const SkRect& ellipse,
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000612 const SkStrokeRec& stroke)
613{
614 GrDrawState* drawState = target->drawState();
615#ifdef SK_DEBUG
616 {
617 // we should have checked for this previously
618 bool isAxisAlignedEllipse = drawState->getViewMatrix().rectStaysRect();
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000619 SkASSERT(useAA && isAxisAlignedEllipse);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000620 }
621#endif
622
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000623 // do any matrix crunching before we reset the draw state for device coords
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000624 const SkMatrix& vm = drawState->getViewMatrix();
625 GrPoint center = GrPoint::Make(ellipse.centerX(), ellipse.centerY());
626 vm.mapPoints(&center, 1);
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000627 SkScalar ellipseXRadius = SkScalarHalf(ellipse.width());
628 SkScalar ellipseYRadius = SkScalarHalf(ellipse.height());
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +0000629 SkScalar xRadius = SkScalarAbs(vm[SkMatrix::kMScaleX]*ellipseXRadius +
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000630 vm[SkMatrix::kMSkewY]*ellipseYRadius);
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +0000631 SkScalar yRadius = SkScalarAbs(vm[SkMatrix::kMSkewX]*ellipseXRadius +
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000632 vm[SkMatrix::kMScaleY]*ellipseYRadius);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000633
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000634 // do (potentially) anisotropic mapping of stroke
635 SkVector scaledStroke;
636 SkScalar strokeWidth = stroke.getWidth();
637 scaledStroke.fX = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMScaleX] + vm[SkMatrix::kMSkewY]));
638 scaledStroke.fY = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMSkewX] + vm[SkMatrix::kMScaleY]));
639
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000640 SkStrokeRec::Style style = stroke.getStyle();
641 bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style);
642
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000643 SkScalar innerXRadius = 0;
644 SkScalar innerYRadius = 0;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000645 if (SkStrokeRec::kFill_Style != style) {
646 if (SkScalarNearlyZero(scaledStroke.length())) {
647 scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf);
648 } else {
649 scaledStroke.scale(SK_ScalarHalf);
650 }
651
652 // we only handle thick strokes for near-circular ellipses
653 if (scaledStroke.length() > SK_ScalarHalf &&
654 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
655 return false;
656 }
657
658 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
659 if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius ||
660 scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) {
661 return false;
662 }
663
664 // this is legit only if scale & translation (which should be the case at the moment)
665 if (isStroked) {
666 innerXRadius = xRadius - scaledStroke.fX;
667 innerYRadius = yRadius - scaledStroke.fY;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000668 }
669
670 xRadius += scaledStroke.fX;
671 yRadius += scaledStroke.fY;
672 }
673
bsalomon@google.com137f1342013-05-29 21:27:53 +0000674 GrDrawState::AutoViewMatrixRestore avmr;
675 if (!avmr.setIdentity(drawState)) {
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000676 return false;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000677 }
678
robertphillips@google.com42903302013-04-20 12:26:07 +0000679 drawState->setVertexAttribs<gEllipseVertexAttribs>(SK_ARRAY_COUNT(gEllipseVertexAttribs));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000680 SkASSERT(sizeof(EllipseVertex) == drawState->getVertexSize());
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000681
682 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
683 if (!geo.succeeded()) {
684 GrPrintf("Failed to get space for vertices!\n");
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000685 return false;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000686 }
687
688 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices());
689
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +0000690 GrEffectRef* effect = EllipseEdgeEffect::Create(isStroked &&
691 innerXRadius > 0 && innerYRadius > 0);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000692
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000693 static const int kEllipseCenterAttrIndex = 1;
jvanverth@google.com6cc8d442013-09-10 18:24:37 +0000694 static const int kEllipseEdgeAttrIndex = 2;
695 drawState->addCoverageEffect(effect, kEllipseCenterAttrIndex, kEllipseEdgeAttrIndex)->unref();
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000696
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000697 // Compute the reciprocals of the radii here to save time in the shader
698 SkScalar xRadRecip = SkScalarInvert(xRadius);
699 SkScalar yRadRecip = SkScalarInvert(yRadius);
700 SkScalar xInnerRadRecip = SkScalarInvert(innerXRadius);
701 SkScalar yInnerRadRecip = SkScalarInvert(innerYRadius);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000702
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000703 // We've extended the outer x radius out half a pixel to antialias.
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000704 // This will also expand the rect so all the pixels will be captured.
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000705 // TODO: Consider if we should use sqrt(2)/2 instead
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000706 xRadius += SK_ScalarHalf;
707 yRadius += SK_ScalarHalf;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000708
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000709 SkRect bounds = SkRect::MakeLTRB(
710 center.fX - xRadius,
711 center.fY - yRadius,
712 center.fX + xRadius,
713 center.fY + yRadius
714 );
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000715
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000716 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000717 verts[0].fOffset = SkPoint::Make(-xRadius, -yRadius);
718 verts[0].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
719 verts[0].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000720
721 verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000722 verts[1].fOffset = SkPoint::Make(xRadius, -yRadius);
723 verts[1].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
724 verts[1].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000725
726 verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000727 verts[2].fOffset = SkPoint::Make(-xRadius, yRadius);
728 verts[2].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
729 verts[2].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000730
731 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000732 verts[3].fOffset = SkPoint::Make(xRadius, yRadius);
733 verts[3].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
734 verts[3].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
skia.committer@gmail.com46746762013-04-12 07:01:51 +0000735
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000736 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds);
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000737
738 return true;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000739}
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000740
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000741bool GrOvalRenderer::drawDIEllipse(GrDrawTarget* target,
742 bool useAA,
743 const SkRect& ellipse,
744 const SkStrokeRec& stroke)
745{
746 GrDrawState* drawState = target->drawState();
747 const SkMatrix& vm = drawState->getViewMatrix();
748
749 GrPoint center = GrPoint::Make(ellipse.centerX(), ellipse.centerY());
750 SkScalar xRadius = SkScalarHalf(ellipse.width());
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000751 SkScalar yRadius = SkScalarHalf(ellipse.height());
752
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000753 SkStrokeRec::Style style = stroke.getStyle();
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000754 DIEllipseEdgeEffect::Mode mode = (SkStrokeRec::kStroke_Style == style) ?
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000755 DIEllipseEdgeEffect::kStroke :
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000756 (SkStrokeRec::kHairline_Style == style) ?
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000757 DIEllipseEdgeEffect::kHairline : DIEllipseEdgeEffect::kFill;
758
759 SkScalar innerXRadius = 0;
760 SkScalar innerYRadius = 0;
761 if (SkStrokeRec::kFill_Style != style && SkStrokeRec::kHairline_Style != style) {
762 SkScalar strokeWidth = stroke.getWidth();
763
764 if (SkScalarNearlyZero(strokeWidth)) {
765 strokeWidth = SK_ScalarHalf;
766 } else {
767 strokeWidth *= SK_ScalarHalf;
768 }
769
770 // we only handle thick strokes for near-circular ellipses
771 if (strokeWidth > SK_ScalarHalf &&
772 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
773 return false;
774 }
775
776 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
777 if (strokeWidth*(yRadius*yRadius) < (strokeWidth*strokeWidth)*xRadius ||
778 strokeWidth*(xRadius*xRadius) < (strokeWidth*strokeWidth)*yRadius) {
779 return false;
780 }
781
782 // set inner radius (if needed)
783 if (SkStrokeRec::kStroke_Style == style) {
784 innerXRadius = xRadius - strokeWidth;
785 innerYRadius = yRadius - strokeWidth;
786 }
787
788 xRadius += strokeWidth;
789 yRadius += strokeWidth;
790 }
791 if (DIEllipseEdgeEffect::kStroke == mode) {
792 mode = (innerXRadius > 0 && innerYRadius > 0) ? DIEllipseEdgeEffect::kStroke :
793 DIEllipseEdgeEffect::kFill;
794 }
795 SkScalar innerRatioX = SkScalarDiv(xRadius, innerXRadius);
796 SkScalar innerRatioY = SkScalarDiv(yRadius, innerYRadius);
797
798 drawState->setVertexAttribs<gDIEllipseVertexAttribs>(SK_ARRAY_COUNT(gDIEllipseVertexAttribs));
799 SkASSERT(sizeof(DIEllipseVertex) == drawState->getVertexSize());
800
801 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
802 if (!geo.succeeded()) {
803 GrPrintf("Failed to get space for vertices!\n");
804 return false;
805 }
806
807 DIEllipseVertex* verts = reinterpret_cast<DIEllipseVertex*>(geo.vertices());
808
809 GrEffectRef* effect = DIEllipseEdgeEffect::Create(mode);
810
811 static const int kEllipseOuterOffsetAttrIndex = 1;
812 static const int kEllipseInnerOffsetAttrIndex = 2;
813 drawState->addCoverageEffect(effect, kEllipseOuterOffsetAttrIndex,
814 kEllipseInnerOffsetAttrIndex)->unref();
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000815
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000816 // This expands the outer rect so that after CTM we end up with a half-pixel border
817 SkScalar a = vm[SkMatrix::kMScaleX];
818 SkScalar b = vm[SkMatrix::kMSkewX];
819 SkScalar c = vm[SkMatrix::kMSkewY];
820 SkScalar d = vm[SkMatrix::kMScaleY];
821 SkScalar geoDx = SkScalarDiv(SK_ScalarHalf, SkScalarSqrt(a*a + c*c));
822 SkScalar geoDy = SkScalarDiv(SK_ScalarHalf, SkScalarSqrt(b*b + d*d));
823 // This adjusts the "radius" to include the half-pixel border
824 SkScalar offsetDx = SkScalarDiv(geoDx, xRadius);
825 SkScalar offsetDy = SkScalarDiv(geoDy, yRadius);
826
827 SkRect bounds = SkRect::MakeLTRB(
828 center.fX - xRadius - geoDx,
829 center.fY - yRadius - geoDy,
830 center.fX + xRadius + geoDx,
831 center.fY + yRadius + geoDy
832 );
833
834 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
835 verts[0].fOuterOffset = SkPoint::Make(-1.0f - offsetDx, -1.0f - offsetDy);
836 verts[0].fInnerOffset = SkPoint::Make(-innerRatioX - offsetDx, -innerRatioY - offsetDy);
837
838 verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
839 verts[1].fOuterOffset = SkPoint::Make(1.0f + offsetDx, -1.0f - offsetDy);
840 verts[1].fInnerOffset = SkPoint::Make(innerRatioX + offsetDx, -innerRatioY - offsetDy);
841
842 verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
843 verts[2].fOuterOffset = SkPoint::Make(-1.0f - offsetDx, 1.0f + offsetDy);
844 verts[2].fInnerOffset = SkPoint::Make(-innerRatioX - offsetDx, innerRatioY + offsetDy);
845
846 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
847 verts[3].fOuterOffset = SkPoint::Make(1.0f + offsetDx, 1.0f + offsetDy);
848 verts[3].fInnerOffset = SkPoint::Make(innerRatioX + offsetDx, innerRatioY + offsetDy);
849
850 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds);
851
852 return true;
853}
854
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000855///////////////////////////////////////////////////////////////////////////////
856
857static const uint16_t gRRectIndices[] = {
858 // corners
859 0, 1, 5, 0, 5, 4,
860 2, 3, 7, 2, 7, 6,
861 8, 9, 13, 8, 13, 12,
862 10, 11, 15, 10, 15, 14,
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000863
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000864 // edges
865 1, 2, 6, 1, 6, 5,
866 4, 5, 9, 4, 9, 8,
867 6, 7, 11, 6, 11, 10,
868 9, 10, 14, 9, 14, 13,
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000869
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000870 // center
871 // we place this at the end so that we can ignore these indices when rendering stroke-only
872 5, 6, 10, 5, 10, 9
873};
874
875
876GrIndexBuffer* GrOvalRenderer::rRectIndexBuffer(GrGpu* gpu) {
877 if (NULL == fRRectIndexBuffer) {
878 fRRectIndexBuffer =
879 gpu->createIndexBuffer(sizeof(gRRectIndices), false);
880 if (NULL != fRRectIndexBuffer) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000881#ifdef SK_DEBUG
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000882 bool updated =
883#endif
884 fRRectIndexBuffer->updateData(gRRectIndices,
885 sizeof(gRRectIndices));
886 GR_DEBUGASSERT(updated);
887 }
888 }
889 return fRRectIndexBuffer;
890}
891
skia.committer@gmail.com2fd42c42013-05-03 07:01:00 +0000892bool GrOvalRenderer::drawSimpleRRect(GrDrawTarget* target, GrContext* context, bool useAA,
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000893 const SkRRect& rrect, const SkStrokeRec& stroke)
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000894{
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000895 // only anti-aliased rrects for now
896 if (!useAA) {
897 return false;
898 }
899
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000900 const SkMatrix& vm = context->getMatrix();
901#ifdef SK_DEBUG
902 {
903 // we should have checked for this previously
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000904 SkASSERT(useAA && vm.rectStaysRect() && rrect.isSimple());
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000905 }
906#endif
907
908 // do any matrix crunching before we reset the draw state for device coords
909 const SkRect& rrectBounds = rrect.getBounds();
910 SkRect bounds;
911 vm.mapRect(&bounds, rrectBounds);
912
913 SkVector radii = rrect.getSimpleRadii();
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000914 SkScalar xRadius = SkScalarAbs(vm[SkMatrix::kMScaleX]*radii.fX +
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000915 vm[SkMatrix::kMSkewY]*radii.fY);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000916 SkScalar yRadius = SkScalarAbs(vm[SkMatrix::kMSkewX]*radii.fX +
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000917 vm[SkMatrix::kMScaleY]*radii.fY);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000918
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000919 // if hairline stroke is greater than radius, we don't handle that right now
920 SkStrokeRec::Style style = stroke.getStyle();
921 if (SkStrokeRec::kHairline_Style == style &&
922 (SK_ScalarHalf >= xRadius || SK_ScalarHalf >= yRadius)) {
923 return false;
924 }
925
926 // do (potentially) anisotropic mapping of stroke
927 SkVector scaledStroke;
928 SkScalar strokeWidth = stroke.getWidth();
929 scaledStroke.fX = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMScaleX] + vm[SkMatrix::kMSkewY]));
930 scaledStroke.fY = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMSkewX] + vm[SkMatrix::kMScaleY]));
931
932 // if half of strokewidth is greater than radius, we don't handle that right now
933 if (SK_ScalarHalf*scaledStroke.fX >= xRadius || SK_ScalarHalf*scaledStroke.fY >= yRadius) {
934 return false;
935 }
936
937 // reset to device coordinates
938 GrDrawState* drawState = target->drawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000939 GrDrawState::AutoViewMatrixRestore avmr;
940 if (!avmr.setIdentity(drawState)) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000941 return false;
942 }
943
944 bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style);
945
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000946 GrIndexBuffer* indexBuffer = this->rRectIndexBuffer(context->getGpu());
947 if (NULL == indexBuffer) {
948 GrPrintf("Failed to create index buffer!\n");
949 return false;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000950 }
951
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000952 // if the corners are circles, use the circle renderer
953 if ((!isStroked || scaledStroke.fX == scaledStroke.fY) && xRadius == yRadius) {
954 drawState->setVertexAttribs<gCircleVertexAttribs>(SK_ARRAY_COUNT(gCircleVertexAttribs));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000955 SkASSERT(sizeof(CircleVertex) == drawState->getVertexSize());
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000956
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000957 GrDrawTarget::AutoReleaseGeometry geo(target, 16, 0);
958 if (!geo.succeeded()) {
959 GrPrintf("Failed to get space for vertices!\n");
960 return false;
961 }
962 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000963
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000964 SkScalar innerRadius = 0.0f;
965 SkScalar outerRadius = xRadius;
966 SkScalar halfWidth = 0;
967 if (style != SkStrokeRec::kFill_Style) {
968 if (SkScalarNearlyZero(scaledStroke.fX)) {
969 halfWidth = SK_ScalarHalf;
970 } else {
971 halfWidth = SkScalarHalf(scaledStroke.fX);
972 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000973
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000974 if (isStroked) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000975 innerRadius = xRadius - halfWidth;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000976 }
977 outerRadius += halfWidth;
978 bounds.outset(halfWidth, halfWidth);
979 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000980
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000981 isStroked = (isStroked && innerRadius > 0);
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +0000982
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000983 GrEffectRef* effect = CircleEdgeEffect::Create(isStroked);
984 static const int kCircleEdgeAttrIndex = 1;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000985 drawState->addCoverageEffect(effect, kCircleEdgeAttrIndex)->unref();
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +0000986
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000987 // The radii are outset for two reasons. First, it allows the shader to simply perform
988 // clamp(distance-to-center - radius, 0, 1). Second, the outer radius is used to compute the
989 // verts of the bounding box that is rendered and the outset ensures the box will cover all
990 // pixels partially covered by the circle.
991 outerRadius += SK_ScalarHalf;
992 innerRadius -= SK_ScalarHalf;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000993
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000994 // Expand the rect so all the pixels will be captured.
995 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000996
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000997 SkScalar yCoords[4] = {
998 bounds.fTop,
999 bounds.fTop + outerRadius,
1000 bounds.fBottom - outerRadius,
1001 bounds.fBottom
1002 };
1003 SkScalar yOuterRadii[4] = {
1004 -outerRadius,
1005 0,
1006 0,
1007 outerRadius
1008 };
1009 for (int i = 0; i < 4; ++i) {
1010 verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]);
1011 verts->fOffset = SkPoint::Make(-outerRadius, yOuterRadii[i]);
1012 verts->fOuterRadius = outerRadius;
1013 verts->fInnerRadius = innerRadius;
1014 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001015
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001016 verts->fPos = SkPoint::Make(bounds.fLeft + outerRadius, yCoords[i]);
1017 verts->fOffset = SkPoint::Make(0, yOuterRadii[i]);
1018 verts->fOuterRadius = outerRadius;
1019 verts->fInnerRadius = innerRadius;
1020 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001021
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001022 verts->fPos = SkPoint::Make(bounds.fRight - outerRadius, yCoords[i]);
1023 verts->fOffset = SkPoint::Make(0, yOuterRadii[i]);
1024 verts->fOuterRadius = outerRadius;
1025 verts->fInnerRadius = innerRadius;
1026 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001027
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001028 verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]);
1029 verts->fOffset = SkPoint::Make(outerRadius, yOuterRadii[i]);
1030 verts->fOuterRadius = outerRadius;
1031 verts->fInnerRadius = innerRadius;
1032 verts++;
1033 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001034
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001035 // drop out the middle quad if we're stroked
1036 int indexCnt = isStroked ? GR_ARRAY_COUNT(gRRectIndices)-6 : GR_ARRAY_COUNT(gRRectIndices);
1037 target->setIndexSourceToBuffer(indexBuffer);
1038 target->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, 16, indexCnt, &bounds);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001039
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001040 // otherwise we use the ellipse renderer
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001041 } else {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001042 drawState->setVertexAttribs<gEllipseVertexAttribs>(SK_ARRAY_COUNT(gEllipseVertexAttribs));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001043 SkASSERT(sizeof(EllipseVertex) == drawState->getVertexSize());
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001044
1045 SkScalar innerXRadius = 0.0f;
1046 SkScalar innerYRadius = 0.0f;
1047 if (SkStrokeRec::kFill_Style != style) {
1048 if (SkScalarNearlyZero(scaledStroke.length())) {
1049 scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf);
1050 } else {
1051 scaledStroke.scale(SK_ScalarHalf);
1052 }
1053
1054 // we only handle thick strokes for near-circular ellipses
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +00001055 if (scaledStroke.length() > SK_ScalarHalf &&
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001056 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
1057 return false;
1058 }
1059
1060 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
1061 if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius ||
1062 scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) {
1063 return false;
1064 }
1065
1066 // this is legit only if scale & translation (which should be the case at the moment)
1067 if (isStroked) {
1068 innerXRadius = xRadius - scaledStroke.fX;
1069 innerYRadius = yRadius - scaledStroke.fY;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001070 }
1071
1072 xRadius += scaledStroke.fX;
1073 yRadius += scaledStroke.fY;
1074 bounds.outset(scaledStroke.fX, scaledStroke.fY);
1075 }
jvanverth@google.come3647412013-05-08 15:31:43 +00001076
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +00001077 isStroked = (isStroked && innerXRadius > 0 && innerYRadius > 0);
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +00001078
jvanverth@google.come3647412013-05-08 15:31:43 +00001079 GrDrawTarget::AutoReleaseGeometry geo(target, 16, 0);
1080 if (!geo.succeeded()) {
1081 GrPrintf("Failed to get space for vertices!\n");
1082 return false;
1083 }
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001084 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices());
jvanverth@google.come3647412013-05-08 15:31:43 +00001085
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001086 GrEffectRef* effect = EllipseEdgeEffect::Create(isStroked);
jvanverth@google.come3647412013-05-08 15:31:43 +00001087 static const int kEllipseOffsetAttrIndex = 1;
1088 static const int kEllipseRadiiAttrIndex = 2;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001089 drawState->addCoverageEffect(effect,
1090 kEllipseOffsetAttrIndex, kEllipseRadiiAttrIndex)->unref();
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001091
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001092 // Compute the reciprocals of the radii here to save time in the shader
1093 SkScalar xRadRecip = SkScalarInvert(xRadius);
1094 SkScalar yRadRecip = SkScalarInvert(yRadius);
1095 SkScalar xInnerRadRecip = SkScalarInvert(innerXRadius);
1096 SkScalar yInnerRadRecip = SkScalarInvert(innerYRadius);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001097
1098 // Extend the radii out half a pixel to antialias.
1099 SkScalar xOuterRadius = xRadius + SK_ScalarHalf;
1100 SkScalar yOuterRadius = yRadius + SK_ScalarHalf;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001101
1102 // Expand the rect so all the pixels will be captured.
1103 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1104
1105 SkScalar yCoords[4] = {
1106 bounds.fTop,
1107 bounds.fTop + yOuterRadius,
1108 bounds.fBottom - yOuterRadius,
1109 bounds.fBottom
1110 };
1111 SkScalar yOuterOffsets[4] = {
jvanverth@google.comd1b5b142013-07-02 14:46:03 +00001112 yOuterRadius,
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001113 SK_ScalarNearlyZero, // we're using inversesqrt() in the shader, so can't be exactly 0
1114 SK_ScalarNearlyZero,
1115 yOuterRadius
1116 };
1117
1118 for (int i = 0; i < 4; ++i) {
1119 verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]);
jvanverth@google.comd1b5b142013-07-02 14:46:03 +00001120 verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001121 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1122 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001123 verts++;
1124
1125 verts->fPos = SkPoint::Make(bounds.fLeft + xOuterRadius, yCoords[i]);
1126 verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001127 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1128 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001129 verts++;
1130
1131 verts->fPos = SkPoint::Make(bounds.fRight - xOuterRadius, yCoords[i]);
1132 verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001133 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1134 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001135 verts++;
1136
1137 verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]);
1138 verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001139 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1140 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001141 verts++;
1142 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001143
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001144 // drop out the middle quad if we're stroked
1145 int indexCnt = isStroked ? GR_ARRAY_COUNT(gRRectIndices)-6 : GR_ARRAY_COUNT(gRRectIndices);
1146 target->setIndexSourceToBuffer(indexBuffer);
1147 target->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, 16, indexCnt, &bounds);
1148 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001149
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001150 return true;
1151}