blob: 785126560acafc56d055d83811c31f1fbfeeb748 [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"
commit-bot@chromium.org261dc562013-10-04 15:42:56 +000013#include "gl/GrGLVertexEffect.h"
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000014#include "GrTBackendEffectFactory.h"
commit-bot@chromium.org81312832013-03-22 18:34:09 +000015
16#include "GrDrawState.h"
17#include "GrDrawTarget.h"
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +000018#include "GrGpu.h"
19
20#include "SkRRect.h"
commit-bot@chromium.org81312832013-03-22 18:34:09 +000021#include "SkStrokeRec.h"
22
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +000023#include "effects/GrVertexEffect.h"
24
commit-bot@chromium.org81312832013-03-22 18:34:09 +000025SK_DEFINE_INST_COUNT(GrOvalRenderer)
26
27namespace {
28
29struct CircleVertex {
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +000030 GrPoint fPos;
31 GrPoint fOffset;
commit-bot@chromium.org81312832013-03-22 18:34:09 +000032 SkScalar fOuterRadius;
33 SkScalar fInnerRadius;
34};
35
36struct EllipseVertex {
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +000037 GrPoint fPos;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +000038 GrPoint fOffset;
39 GrPoint fOuterRadii;
40 GrPoint fInnerRadii;
41};
42
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +000043struct DIEllipseVertex {
44 GrPoint fPos;
45 GrPoint fOuterOffset;
46 GrPoint fInnerOffset;
47};
48
commit-bot@chromium.org81312832013-03-22 18:34:09 +000049inline bool circle_stays_circle(const SkMatrix& m) {
50 return m.isSimilarity();
51}
52
53}
54
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000055///////////////////////////////////////////////////////////////////////////////
56
57/**
58 * 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 +000059 * specified as offset_x, offset_y (both from center point), outer radius and inner radius.
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000060 */
61
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +000062class CircleEdgeEffect : public GrVertexEffect {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000063public:
64 static GrEffectRef* Create(bool stroke) {
bsalomon@google.comd42aca32013-04-23 15:37:27 +000065 GR_CREATE_STATIC_EFFECT(gCircleStrokeEdge, CircleEdgeEffect, (true));
66 GR_CREATE_STATIC_EFFECT(gCircleFillEdge, CircleEdgeEffect, (false));
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000067
68 if (stroke) {
bsalomon@google.comd42aca32013-04-23 15:37:27 +000069 gCircleStrokeEdge->ref();
70 return gCircleStrokeEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000071 } else {
bsalomon@google.comd42aca32013-04-23 15:37:27 +000072 gCircleFillEdge->ref();
73 return gCircleFillEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000074 }
75 }
76
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +000077 virtual void getConstantColorComponents(GrColor* color,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000078 uint32_t* validFlags) const SK_OVERRIDE {
79 *validFlags = 0;
80 }
81
82 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
83 return GrTBackendEffectFactory<CircleEdgeEffect>::getInstance();
84 }
85
86 virtual ~CircleEdgeEffect() {}
87
88 static const char* Name() { return "CircleEdge"; }
89
90 inline bool isStroked() const { return fStroke; }
91
commit-bot@chromium.org261dc562013-10-04 15:42:56 +000092 class GLEffect : public GrGLVertexEffect {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000093 public:
94 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
95 : INHERITED (factory) {}
96
commit-bot@chromium.org261dc562013-10-04 15:42:56 +000097 virtual void emitCode(GrGLFullShaderBuilder* builder,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000098 const GrDrawEffect& drawEffect,
99 EffectKey key,
100 const char* outputColor,
101 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000102 const TransformedCoordsArray&,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000103 const TextureSamplerArray& samplers) SK_OVERRIDE {
104 const CircleEdgeEffect& circleEffect = drawEffect.castEffect<CircleEdgeEffect>();
105 const char *vsName, *fsName;
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000106 builder->addVarying(kVec4f_GrSLType, "CircleEdge", &vsName, &fsName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000107
108 const SkString* attrName =
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000109 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]);
110 builder->vsCodeAppendf("\t%s = %s;\n", vsName, attrName->c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000111
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000112 builder->fsCodeAppendf("\tfloat d = length(%s.xy);\n", fsName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000113 builder->fsCodeAppendf("\tfloat edgeAlpha = clamp(%s.z - d, 0.0, 1.0);\n", fsName);
114 if (circleEffect.isStroked()) {
115 builder->fsCodeAppendf("\tfloat innerAlpha = clamp(d - %s.w, 0.0, 1.0);\n", fsName);
116 builder->fsCodeAppend("\tedgeAlpha *= innerAlpha;\n");
117 }
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000118
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000119 SkString modulate;
bsalomon@google.com018f1792013-04-18 19:36:09 +0000120 GrGLSLModulatef<4>(&modulate, inputColor, "edgeAlpha");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000121 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str());
122 }
123
124 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
125 const CircleEdgeEffect& circleEffect = drawEffect.castEffect<CircleEdgeEffect>();
126
127 return circleEffect.isStroked() ? 0x1 : 0x0;
128 }
129
130 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {}
131
132 private:
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000133 typedef GrGLVertexEffect INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000134 };
135
136
137private:
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000138 CircleEdgeEffect(bool stroke) : GrVertexEffect() {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000139 this->addVertexAttrib(kVec4f_GrSLType);
140 fStroke = stroke;
141 }
142
143 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
144 const CircleEdgeEffect& cee = CastEffect<CircleEdgeEffect>(other);
145 return cee.fStroke == fStroke;
146 }
147
148 bool fStroke;
149
150 GR_DECLARE_EFFECT_TEST;
151
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000152 typedef GrVertexEffect INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000153};
154
155GR_DEFINE_EFFECT_TEST(CircleEdgeEffect);
156
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000157GrEffectRef* CircleEdgeEffect::TestCreate(SkRandom* random,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000158 GrContext* context,
159 const GrDrawTargetCaps&,
160 GrTexture* textures[]) {
161 return CircleEdgeEffect::Create(random->nextBool());
162}
163
164///////////////////////////////////////////////////////////////////////////////
165
166/**
167 * 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 +0000168 * ellipse, specified as a 2D offset from center, and the reciprocals of the outer and inner radii,
169 * in both x and y directions.
170 *
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000171 * 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 +0000172 */
173
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000174class EllipseEdgeEffect : public GrVertexEffect {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000175public:
176 static GrEffectRef* Create(bool stroke) {
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000177 GR_CREATE_STATIC_EFFECT(gEllipseStrokeEdge, EllipseEdgeEffect, (true));
178 GR_CREATE_STATIC_EFFECT(gEllipseFillEdge, EllipseEdgeEffect, (false));
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000179
180 if (stroke) {
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000181 gEllipseStrokeEdge->ref();
182 return gEllipseStrokeEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000183 } else {
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000184 gEllipseFillEdge->ref();
185 return gEllipseFillEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000186 }
187 }
188
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000189 virtual void getConstantColorComponents(GrColor* color,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000190 uint32_t* validFlags) const SK_OVERRIDE {
191 *validFlags = 0;
192 }
193
194 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
195 return GrTBackendEffectFactory<EllipseEdgeEffect>::getInstance();
196 }
197
198 virtual ~EllipseEdgeEffect() {}
199
200 static const char* Name() { return "EllipseEdge"; }
201
202 inline bool isStroked() const { return fStroke; }
203
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000204 class GLEffect : public GrGLVertexEffect {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000205 public:
206 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
207 : INHERITED (factory) {}
208
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000209 virtual void emitCode(GrGLFullShaderBuilder* builder,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000210 const GrDrawEffect& drawEffect,
211 EffectKey key,
212 const char* outputColor,
213 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000214 const TransformedCoordsArray&,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000215 const TextureSamplerArray& samplers) SK_OVERRIDE {
216 const EllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<EllipseEdgeEffect>();
217
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000218 const char *vsOffsetName, *fsOffsetName;
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000219 const char *vsRadiiName, *fsRadiiName;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000220
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000221 builder->addVarying(kVec2f_GrSLType, "EllipseOffsets", &vsOffsetName, &fsOffsetName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000222 const SkString* attr0Name =
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000223 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]);
224 builder->vsCodeAppendf("\t%s = %s;\n", vsOffsetName, attr0Name->c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000225
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000226 builder->addVarying(kVec4f_GrSLType, "EllipseRadii", &vsRadiiName, &fsRadiiName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000227 const SkString* attr1Name =
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000228 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[1]);
229 builder->vsCodeAppendf("\t%s = %s;\n", vsRadiiName, attr1Name->c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000230
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +0000231 // for outer curve
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000232 builder->fsCodeAppendf("\tvec2 scaledOffset = %s*%s.xy;\n", fsOffsetName, fsRadiiName);
233 builder->fsCodeAppend("\tfloat test = dot(scaledOffset, scaledOffset) - 1.0;\n");
234 builder->fsCodeAppendf("\tvec2 grad = 2.0*scaledOffset*%s.xy;\n", fsRadiiName);
jvanverth@google.comd1b5b142013-07-02 14:46:03 +0000235 builder->fsCodeAppend("\tfloat grad_dot = dot(grad, grad);\n");
236 // we need to clamp the length^2 of the gradiant vector to a non-zero value, because
237 // on the Nexus 4 the undefined result of inversesqrt(0) drops out an entire tile
238 // TODO: restrict this to Adreno-only
239 builder->fsCodeAppend("\tgrad_dot = max(grad_dot, 1.0e-4);\n");
240 builder->fsCodeAppend("\tfloat invlen = inversesqrt(grad_dot);\n");
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000241 builder->fsCodeAppend("\tfloat edgeAlpha = clamp(0.5-test*invlen, 0.0, 1.0);\n");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000242
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000243 // for inner curve
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000244 if (ellipseEffect.isStroked()) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000245 builder->fsCodeAppendf("\tscaledOffset = %s*%s.zw;\n", fsOffsetName, fsRadiiName);
246 builder->fsCodeAppend("\ttest = dot(scaledOffset, scaledOffset) - 1.0;\n");
247 builder->fsCodeAppendf("\tgrad = 2.0*scaledOffset*%s.zw;\n", fsRadiiName);
248 builder->fsCodeAppend("\tinvlen = inversesqrt(dot(grad, grad));\n");
249 builder->fsCodeAppend("\tedgeAlpha *= clamp(0.5+test*invlen, 0.0, 1.0);\n");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000250 }
251
252 SkString modulate;
bsalomon@google.com018f1792013-04-18 19:36:09 +0000253 GrGLSLModulatef<4>(&modulate, inputColor, "edgeAlpha");
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000254 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000255 }
256
257 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
258 const EllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<EllipseEdgeEffect>();
259
260 return ellipseEffect.isStroked() ? 0x1 : 0x0;
261 }
262
263 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {
264 }
265
266 private:
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000267 typedef GrGLVertexEffect INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000268 };
269
270private:
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000271 EllipseEdgeEffect(bool stroke) : GrVertexEffect() {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000272 this->addVertexAttrib(kVec2f_GrSLType);
273 this->addVertexAttrib(kVec4f_GrSLType);
274 fStroke = stroke;
275 }
276
277 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
278 const EllipseEdgeEffect& eee = CastEffect<EllipseEdgeEffect>(other);
279 return eee.fStroke == fStroke;
280 }
281
282 bool fStroke;
283
284 GR_DECLARE_EFFECT_TEST;
285
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000286 typedef GrVertexEffect INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000287};
288
289GR_DEFINE_EFFECT_TEST(EllipseEdgeEffect);
290
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000291GrEffectRef* EllipseEdgeEffect::TestCreate(SkRandom* random,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000292 GrContext* context,
293 const GrDrawTargetCaps&,
294 GrTexture* textures[]) {
295 return EllipseEdgeEffect::Create(random->nextBool());
296}
297
298///////////////////////////////////////////////////////////////////////////////
299
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000300/**
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000301 * 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 +0000302 * specified as a 2D offset from center for both the outer and inner paths (if stroked). The
303 * implict equation used is for a unit circle (x^2 + y^2 - 1 = 0) and the edge corrected by
304 * using differentials.
305 *
306 * The result is device-independent and can be used with any affine matrix.
307 */
308
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000309class DIEllipseEdgeEffect : public GrVertexEffect {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000310public:
311 enum Mode { kStroke = 0, kHairline, kFill };
312
313 static GrEffectRef* Create(Mode mode) {
314 GR_CREATE_STATIC_EFFECT(gEllipseStrokeEdge, DIEllipseEdgeEffect, (kStroke));
315 GR_CREATE_STATIC_EFFECT(gEllipseHairlineEdge, DIEllipseEdgeEffect, (kHairline));
316 GR_CREATE_STATIC_EFFECT(gEllipseFillEdge, DIEllipseEdgeEffect, (kFill));
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000317
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000318 if (kStroke == mode) {
319 gEllipseStrokeEdge->ref();
320 return gEllipseStrokeEdge;
321 } else if (kHairline == mode) {
322 gEllipseHairlineEdge->ref();
323 return gEllipseHairlineEdge;
324 } else {
325 gEllipseFillEdge->ref();
326 return gEllipseFillEdge;
327 }
328 }
329
330 virtual void getConstantColorComponents(GrColor* color,
331 uint32_t* validFlags) const SK_OVERRIDE {
332 *validFlags = 0;
333 }
334
335 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
336 return GrTBackendEffectFactory<DIEllipseEdgeEffect>::getInstance();
337 }
338
339 virtual ~DIEllipseEdgeEffect() {}
340
341 static const char* Name() { return "DIEllipseEdge"; }
342
343 inline Mode getMode() const { return fMode; }
344
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000345 class GLEffect : public GrGLVertexEffect {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000346 public:
347 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
348 : INHERITED (factory) {}
349
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000350 virtual void emitCode(GrGLFullShaderBuilder* builder,
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000351 const GrDrawEffect& drawEffect,
352 EffectKey key,
353 const char* outputColor,
354 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000355 const TransformedCoordsArray&,
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000356 const TextureSamplerArray& samplers) SK_OVERRIDE {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000357 const DIEllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<DIEllipseEdgeEffect>();
358
359 SkAssertResult(builder->enableFeature(
360 GrGLShaderBuilder::kStandardDerivatives_GLSLFeature));
361
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000362 const char *vsOffsetName0, *fsOffsetName0;
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000363 builder->addVarying(kVec2f_GrSLType, "EllipseOffsets0",
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000364 &vsOffsetName0, &fsOffsetName0);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000365 const SkString* attr0Name =
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000366 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]);
367 builder->vsCodeAppendf("\t%s = %s;\n", vsOffsetName0, attr0Name->c_str());
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000368 const char *vsOffsetName1, *fsOffsetName1;
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000369 builder->addVarying(kVec2f_GrSLType, "EllipseOffsets1",
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000370 &vsOffsetName1, &fsOffsetName1);
371 const SkString* attr1Name =
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000372 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[1]);
373 builder->vsCodeAppendf("\t%s = %s;\n", vsOffsetName1, attr1Name->c_str());
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000374
375 // for outer curve
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000376 builder->fsCodeAppendf("\tvec2 scaledOffset = %s.xy;\n", fsOffsetName0);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000377 builder->fsCodeAppend("\tfloat test = dot(scaledOffset, scaledOffset) - 1.0;\n");
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000378 builder->fsCodeAppendf("\tvec2 duvdx = dFdx(%s);\n", fsOffsetName0);
379 builder->fsCodeAppendf("\tvec2 duvdy = dFdy(%s);\n", fsOffsetName0);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000380 builder->fsCodeAppendf("\tvec2 grad = vec2(2.0*%s.x*duvdx.x + 2.0*%s.y*duvdx.y,\n"
381 "\t 2.0*%s.x*duvdy.x + 2.0*%s.y*duvdy.y);\n",
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000382 fsOffsetName0, fsOffsetName0, fsOffsetName0, fsOffsetName0);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000383
384 builder->fsCodeAppend("\tfloat grad_dot = dot(grad, grad);\n");
385 // we need to clamp the length^2 of the gradiant vector to a non-zero value, because
386 // on the Nexus 4 the undefined result of inversesqrt(0) drops out an entire tile
387 // TODO: restrict this to Adreno-only
388 builder->fsCodeAppend("\tgrad_dot = max(grad_dot, 1.0e-4);\n");
389 builder->fsCodeAppend("\tfloat invlen = inversesqrt(grad_dot);\n");
390 if (kHairline == ellipseEffect.getMode()) {
391 // can probably do this with one step
392 builder->fsCodeAppend("\tfloat edgeAlpha = clamp(1.0-test*invlen, 0.0, 1.0);\n");
393 builder->fsCodeAppend("\tedgeAlpha *= clamp(1.0+test*invlen, 0.0, 1.0);\n");
394 } else {
395 builder->fsCodeAppend("\tfloat edgeAlpha = clamp(0.5-test*invlen, 0.0, 1.0);\n");
396 }
397
398 // for inner curve
399 if (kStroke == ellipseEffect.getMode()) {
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000400 builder->fsCodeAppendf("\tscaledOffset = %s.xy;\n", fsOffsetName1);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000401 builder->fsCodeAppend("\ttest = dot(scaledOffset, scaledOffset) - 1.0;\n");
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000402 builder->fsCodeAppendf("\tduvdx = dFdx(%s);\n", fsOffsetName1);
403 builder->fsCodeAppendf("\tduvdy = dFdy(%s);\n", fsOffsetName1);
404 builder->fsCodeAppendf("\tgrad = vec2(2.0*%s.x*duvdx.x + 2.0*%s.y*duvdx.y,\n"
405 "\t 2.0*%s.x*duvdy.x + 2.0*%s.y*duvdy.y);\n",
406 fsOffsetName1, fsOffsetName1, fsOffsetName1, fsOffsetName1);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000407 builder->fsCodeAppend("\tinvlen = inversesqrt(dot(grad, grad));\n");
408 builder->fsCodeAppend("\tedgeAlpha *= clamp(0.5+test*invlen, 0.0, 1.0);\n");
409 }
410
411 SkString modulate;
412 GrGLSLModulatef<4>(&modulate, inputColor, "edgeAlpha");
413 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str());
414 }
415
416 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
417 const DIEllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<DIEllipseEdgeEffect>();
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000418
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000419 return ellipseEffect.getMode();
420 }
421
422 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {
423 }
424
425 private:
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000426 typedef GrGLVertexEffect INHERITED;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000427 };
428
429private:
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000430 DIEllipseEdgeEffect(Mode mode) : GrVertexEffect() {
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000431 this->addVertexAttrib(kVec2f_GrSLType);
432 this->addVertexAttrib(kVec2f_GrSLType);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000433 fMode = mode;
434 }
435
436 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
437 const DIEllipseEdgeEffect& eee = CastEffect<DIEllipseEdgeEffect>(other);
438 return eee.fMode == fMode;
439 }
440
441 Mode fMode;
442
443 GR_DECLARE_EFFECT_TEST;
444
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000445 typedef GrVertexEffect INHERITED;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000446};
447
448GR_DEFINE_EFFECT_TEST(DIEllipseEdgeEffect);
449
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000450GrEffectRef* DIEllipseEdgeEffect::TestCreate(SkRandom* random,
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000451 GrContext* context,
452 const GrDrawTargetCaps&,
453 GrTexture* textures[]) {
454 return DIEllipseEdgeEffect::Create((Mode)(random->nextRangeU(0,2)));
455}
456
457///////////////////////////////////////////////////////////////////////////////
458
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000459void GrOvalRenderer::reset() {
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000460 SkSafeSetNull(fRRectIndexBuffer);
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000461}
462
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000463bool GrOvalRenderer::drawOval(GrDrawTarget* target, const GrContext* context, bool useAA,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000464 const SkRect& oval, const SkStrokeRec& stroke)
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000465{
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000466 if (!useAA) {
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000467 return false;
468 }
469
470 const SkMatrix& vm = context->getMatrix();
471
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000472 // we can draw circles
473 if (SkScalarNearlyEqual(oval.width(), oval.height())
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000474 && circle_stays_circle(vm)) {
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000475 this->drawCircle(target, useAA, oval, stroke);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000476 // if we have shader derivative support, render as device-independent
477 } else if (target->caps()->shaderDerivativeSupport()) {
478 return this->drawDIEllipse(target, useAA, oval, stroke);
479 // otherwise axis-aligned ellipses only
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000480 } else if (vm.rectStaysRect()) {
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000481 return this->drawEllipse(target, useAA, oval, stroke);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000482 } else {
483 return false;
484 }
485
486 return true;
487}
488
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000489///////////////////////////////////////////////////////////////////////////////
490
robertphillips@google.com42903302013-04-20 12:26:07 +0000491// position + edge
492extern const GrVertexAttrib gCircleVertexAttribs[] = {
493 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
494 {kVec4f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding}
495};
496
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000497void GrOvalRenderer::drawCircle(GrDrawTarget* target,
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000498 bool useAA,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000499 const SkRect& circle,
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000500 const SkStrokeRec& stroke)
501{
502 GrDrawState* drawState = target->drawState();
503
504 const SkMatrix& vm = drawState->getViewMatrix();
505 GrPoint center = GrPoint::Make(circle.centerX(), circle.centerY());
506 vm.mapPoints(&center, 1);
507 SkScalar radius = vm.mapRadius(SkScalarHalf(circle.width()));
508 SkScalar strokeWidth = vm.mapRadius(stroke.getWidth());
509
bsalomon@google.com137f1342013-05-29 21:27:53 +0000510 GrDrawState::AutoViewMatrixRestore avmr;
511 if (!avmr.setIdentity(drawState)) {
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000512 return;
513 }
514
robertphillips@google.com42903302013-04-20 12:26:07 +0000515 drawState->setVertexAttribs<gCircleVertexAttribs>(SK_ARRAY_COUNT(gCircleVertexAttribs));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000516 SkASSERT(sizeof(CircleVertex) == drawState->getVertexSize());
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000517
518 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
519 if (!geo.succeeded()) {
520 GrPrintf("Failed to get space for vertices!\n");
521 return;
522 }
523
524 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
525
526 SkStrokeRec::Style style = stroke.getStyle();
527 bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style);
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000528
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000529 SkScalar innerRadius = 0.0f;
530 SkScalar outerRadius = radius;
531 SkScalar halfWidth = 0;
532 if (style != SkStrokeRec::kFill_Style) {
533 if (SkScalarNearlyZero(strokeWidth)) {
534 halfWidth = SK_ScalarHalf;
535 } else {
536 halfWidth = SkScalarHalf(strokeWidth);
537 }
538
539 outerRadius += halfWidth;
540 if (isStroked) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000541 innerRadius = radius - halfWidth;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000542 }
543 }
544
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +0000545 GrEffectRef* effect = CircleEdgeEffect::Create(isStroked && innerRadius > 0);
546 static const int kCircleEdgeAttrIndex = 1;
547 drawState->addCoverageEffect(effect, kCircleEdgeAttrIndex)->unref();
548
bsalomon@google.com58e30fe2013-04-01 19:01:20 +0000549 // The radii are outset for two reasons. First, it allows the shader to simply perform
550 // clamp(distance-to-center - radius, 0, 1). Second, the outer radius is used to compute the
551 // verts of the bounding box that is rendered and the outset ensures the box will cover all
552 // pixels partially covered by the circle.
553 outerRadius += SK_ScalarHalf;
554 innerRadius -= SK_ScalarHalf;
555
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000556 SkRect bounds = SkRect::MakeLTRB(
bsalomon@google.com58e30fe2013-04-01 19:01:20 +0000557 center.fX - outerRadius,
558 center.fY - outerRadius,
559 center.fX + outerRadius,
560 center.fY + outerRadius
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000561 );
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000562
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000563 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000564 verts[0].fOffset = SkPoint::Make(-outerRadius, -outerRadius);
565 verts[0].fOuterRadius = outerRadius;
566 verts[0].fInnerRadius = innerRadius;
567
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000568 verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
skia.committer@gmail.com46746762013-04-12 07:01:51 +0000569 verts[1].fOffset = SkPoint::Make(outerRadius, -outerRadius);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000570 verts[1].fOuterRadius = outerRadius;
571 verts[1].fInnerRadius = innerRadius;
572
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000573 verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000574 verts[2].fOffset = SkPoint::Make(-outerRadius, outerRadius);
575 verts[2].fOuterRadius = outerRadius;
576 verts[2].fInnerRadius = innerRadius;
577
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000578 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000579 verts[3].fOffset = SkPoint::Make(outerRadius, outerRadius);
580 verts[3].fOuterRadius = outerRadius;
581 verts[3].fInnerRadius = innerRadius;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000582
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000583 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000584}
585
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000586///////////////////////////////////////////////////////////////////////////////
587
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000588// position + offset + 1/radii
robertphillips@google.com42903302013-04-20 12:26:07 +0000589extern const GrVertexAttrib gEllipseVertexAttribs[] = {
590 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
591 {kVec2f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding},
592 {kVec4f_GrVertexAttribType, 2*sizeof(GrPoint), kEffect_GrVertexAttribBinding}
593};
594
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000595// position + offsets
596extern const GrVertexAttrib gDIEllipseVertexAttribs[] = {
597 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000598 {kVec2f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding},
599 {kVec2f_GrVertexAttribType, 2*sizeof(GrPoint), kEffect_GrVertexAttribBinding},
robertphillips@google.com42903302013-04-20 12:26:07 +0000600};
601
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000602bool GrOvalRenderer::drawEllipse(GrDrawTarget* target,
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000603 bool useAA,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000604 const SkRect& ellipse,
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000605 const SkStrokeRec& stroke)
606{
607 GrDrawState* drawState = target->drawState();
608#ifdef SK_DEBUG
609 {
610 // we should have checked for this previously
611 bool isAxisAlignedEllipse = drawState->getViewMatrix().rectStaysRect();
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000612 SkASSERT(useAA && isAxisAlignedEllipse);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000613 }
614#endif
615
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000616 // do any matrix crunching before we reset the draw state for device coords
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000617 const SkMatrix& vm = drawState->getViewMatrix();
618 GrPoint center = GrPoint::Make(ellipse.centerX(), ellipse.centerY());
619 vm.mapPoints(&center, 1);
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000620 SkScalar ellipseXRadius = SkScalarHalf(ellipse.width());
621 SkScalar ellipseYRadius = SkScalarHalf(ellipse.height());
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +0000622 SkScalar xRadius = SkScalarAbs(vm[SkMatrix::kMScaleX]*ellipseXRadius +
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000623 vm[SkMatrix::kMSkewY]*ellipseYRadius);
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +0000624 SkScalar yRadius = SkScalarAbs(vm[SkMatrix::kMSkewX]*ellipseXRadius +
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000625 vm[SkMatrix::kMScaleY]*ellipseYRadius);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000626
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000627 // do (potentially) anisotropic mapping of stroke
628 SkVector scaledStroke;
629 SkScalar strokeWidth = stroke.getWidth();
630 scaledStroke.fX = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMScaleX] + vm[SkMatrix::kMSkewY]));
631 scaledStroke.fY = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMSkewX] + vm[SkMatrix::kMScaleY]));
632
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000633 SkStrokeRec::Style style = stroke.getStyle();
634 bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style);
635
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000636 SkScalar innerXRadius = 0;
637 SkScalar innerYRadius = 0;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000638 if (SkStrokeRec::kFill_Style != style) {
639 if (SkScalarNearlyZero(scaledStroke.length())) {
640 scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf);
641 } else {
642 scaledStroke.scale(SK_ScalarHalf);
643 }
644
645 // we only handle thick strokes for near-circular ellipses
646 if (scaledStroke.length() > SK_ScalarHalf &&
647 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
648 return false;
649 }
650
651 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
652 if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius ||
653 scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) {
654 return false;
655 }
656
657 // this is legit only if scale & translation (which should be the case at the moment)
658 if (isStroked) {
659 innerXRadius = xRadius - scaledStroke.fX;
660 innerYRadius = yRadius - scaledStroke.fY;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000661 }
662
663 xRadius += scaledStroke.fX;
664 yRadius += scaledStroke.fY;
665 }
666
bsalomon@google.com137f1342013-05-29 21:27:53 +0000667 GrDrawState::AutoViewMatrixRestore avmr;
668 if (!avmr.setIdentity(drawState)) {
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000669 return false;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000670 }
671
robertphillips@google.com42903302013-04-20 12:26:07 +0000672 drawState->setVertexAttribs<gEllipseVertexAttribs>(SK_ARRAY_COUNT(gEllipseVertexAttribs));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000673 SkASSERT(sizeof(EllipseVertex) == drawState->getVertexSize());
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000674
675 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
676 if (!geo.succeeded()) {
677 GrPrintf("Failed to get space for vertices!\n");
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000678 return false;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000679 }
680
681 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices());
682
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +0000683 GrEffectRef* effect = EllipseEdgeEffect::Create(isStroked &&
684 innerXRadius > 0 && innerYRadius > 0);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000685
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000686 static const int kEllipseCenterAttrIndex = 1;
jvanverth@google.com6cc8d442013-09-10 18:24:37 +0000687 static const int kEllipseEdgeAttrIndex = 2;
688 drawState->addCoverageEffect(effect, kEllipseCenterAttrIndex, kEllipseEdgeAttrIndex)->unref();
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000689
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000690 // Compute the reciprocals of the radii here to save time in the shader
691 SkScalar xRadRecip = SkScalarInvert(xRadius);
692 SkScalar yRadRecip = SkScalarInvert(yRadius);
693 SkScalar xInnerRadRecip = SkScalarInvert(innerXRadius);
694 SkScalar yInnerRadRecip = SkScalarInvert(innerYRadius);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000695
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000696 // We've extended the outer x radius out half a pixel to antialias.
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000697 // This will also expand the rect so all the pixels will be captured.
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000698 // TODO: Consider if we should use sqrt(2)/2 instead
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000699 xRadius += SK_ScalarHalf;
700 yRadius += SK_ScalarHalf;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000701
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000702 SkRect bounds = SkRect::MakeLTRB(
703 center.fX - xRadius,
704 center.fY - yRadius,
705 center.fX + xRadius,
706 center.fY + yRadius
707 );
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000708
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000709 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000710 verts[0].fOffset = SkPoint::Make(-xRadius, -yRadius);
711 verts[0].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
712 verts[0].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000713
714 verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000715 verts[1].fOffset = SkPoint::Make(xRadius, -yRadius);
716 verts[1].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
717 verts[1].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000718
719 verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000720 verts[2].fOffset = SkPoint::Make(-xRadius, yRadius);
721 verts[2].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
722 verts[2].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000723
724 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000725 verts[3].fOffset = SkPoint::Make(xRadius, yRadius);
726 verts[3].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
727 verts[3].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
skia.committer@gmail.com46746762013-04-12 07:01:51 +0000728
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000729 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds);
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000730
731 return true;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000732}
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000733
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000734bool GrOvalRenderer::drawDIEllipse(GrDrawTarget* target,
735 bool useAA,
736 const SkRect& ellipse,
737 const SkStrokeRec& stroke)
738{
739 GrDrawState* drawState = target->drawState();
740 const SkMatrix& vm = drawState->getViewMatrix();
741
742 GrPoint center = GrPoint::Make(ellipse.centerX(), ellipse.centerY());
743 SkScalar xRadius = SkScalarHalf(ellipse.width());
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000744 SkScalar yRadius = SkScalarHalf(ellipse.height());
745
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000746 SkStrokeRec::Style style = stroke.getStyle();
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000747 DIEllipseEdgeEffect::Mode mode = (SkStrokeRec::kStroke_Style == style) ?
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000748 DIEllipseEdgeEffect::kStroke :
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000749 (SkStrokeRec::kHairline_Style == style) ?
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000750 DIEllipseEdgeEffect::kHairline : DIEllipseEdgeEffect::kFill;
751
752 SkScalar innerXRadius = 0;
753 SkScalar innerYRadius = 0;
754 if (SkStrokeRec::kFill_Style != style && SkStrokeRec::kHairline_Style != style) {
755 SkScalar strokeWidth = stroke.getWidth();
756
757 if (SkScalarNearlyZero(strokeWidth)) {
758 strokeWidth = SK_ScalarHalf;
759 } else {
760 strokeWidth *= SK_ScalarHalf;
761 }
762
763 // we only handle thick strokes for near-circular ellipses
764 if (strokeWidth > SK_ScalarHalf &&
765 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
766 return false;
767 }
768
769 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
770 if (strokeWidth*(yRadius*yRadius) < (strokeWidth*strokeWidth)*xRadius ||
771 strokeWidth*(xRadius*xRadius) < (strokeWidth*strokeWidth)*yRadius) {
772 return false;
773 }
774
775 // set inner radius (if needed)
776 if (SkStrokeRec::kStroke_Style == style) {
777 innerXRadius = xRadius - strokeWidth;
778 innerYRadius = yRadius - strokeWidth;
779 }
780
781 xRadius += strokeWidth;
782 yRadius += strokeWidth;
783 }
784 if (DIEllipseEdgeEffect::kStroke == mode) {
785 mode = (innerXRadius > 0 && innerYRadius > 0) ? DIEllipseEdgeEffect::kStroke :
786 DIEllipseEdgeEffect::kFill;
787 }
788 SkScalar innerRatioX = SkScalarDiv(xRadius, innerXRadius);
789 SkScalar innerRatioY = SkScalarDiv(yRadius, innerYRadius);
790
791 drawState->setVertexAttribs<gDIEllipseVertexAttribs>(SK_ARRAY_COUNT(gDIEllipseVertexAttribs));
792 SkASSERT(sizeof(DIEllipseVertex) == drawState->getVertexSize());
793
794 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
795 if (!geo.succeeded()) {
796 GrPrintf("Failed to get space for vertices!\n");
797 return false;
798 }
799
800 DIEllipseVertex* verts = reinterpret_cast<DIEllipseVertex*>(geo.vertices());
801
802 GrEffectRef* effect = DIEllipseEdgeEffect::Create(mode);
803
804 static const int kEllipseOuterOffsetAttrIndex = 1;
805 static const int kEllipseInnerOffsetAttrIndex = 2;
806 drawState->addCoverageEffect(effect, kEllipseOuterOffsetAttrIndex,
807 kEllipseInnerOffsetAttrIndex)->unref();
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000808
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000809 // This expands the outer rect so that after CTM we end up with a half-pixel border
810 SkScalar a = vm[SkMatrix::kMScaleX];
811 SkScalar b = vm[SkMatrix::kMSkewX];
812 SkScalar c = vm[SkMatrix::kMSkewY];
813 SkScalar d = vm[SkMatrix::kMScaleY];
814 SkScalar geoDx = SkScalarDiv(SK_ScalarHalf, SkScalarSqrt(a*a + c*c));
815 SkScalar geoDy = SkScalarDiv(SK_ScalarHalf, SkScalarSqrt(b*b + d*d));
816 // This adjusts the "radius" to include the half-pixel border
817 SkScalar offsetDx = SkScalarDiv(geoDx, xRadius);
818 SkScalar offsetDy = SkScalarDiv(geoDy, yRadius);
819
820 SkRect bounds = SkRect::MakeLTRB(
821 center.fX - xRadius - geoDx,
822 center.fY - yRadius - geoDy,
823 center.fX + xRadius + geoDx,
824 center.fY + yRadius + geoDy
825 );
826
827 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
828 verts[0].fOuterOffset = SkPoint::Make(-1.0f - offsetDx, -1.0f - offsetDy);
829 verts[0].fInnerOffset = SkPoint::Make(-innerRatioX - offsetDx, -innerRatioY - offsetDy);
830
831 verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
832 verts[1].fOuterOffset = SkPoint::Make(1.0f + offsetDx, -1.0f - offsetDy);
833 verts[1].fInnerOffset = SkPoint::Make(innerRatioX + offsetDx, -innerRatioY - offsetDy);
834
835 verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
836 verts[2].fOuterOffset = SkPoint::Make(-1.0f - offsetDx, 1.0f + offsetDy);
837 verts[2].fInnerOffset = SkPoint::Make(-innerRatioX - offsetDx, innerRatioY + offsetDy);
838
839 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
840 verts[3].fOuterOffset = SkPoint::Make(1.0f + offsetDx, 1.0f + offsetDy);
841 verts[3].fInnerOffset = SkPoint::Make(innerRatioX + offsetDx, innerRatioY + offsetDy);
842
843 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds);
844
845 return true;
846}
847
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000848///////////////////////////////////////////////////////////////////////////////
849
850static const uint16_t gRRectIndices[] = {
851 // corners
852 0, 1, 5, 0, 5, 4,
853 2, 3, 7, 2, 7, 6,
854 8, 9, 13, 8, 13, 12,
855 10, 11, 15, 10, 15, 14,
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000856
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000857 // edges
858 1, 2, 6, 1, 6, 5,
859 4, 5, 9, 4, 9, 8,
860 6, 7, 11, 6, 11, 10,
861 9, 10, 14, 9, 14, 13,
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000862
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000863 // center
864 // we place this at the end so that we can ignore these indices when rendering stroke-only
865 5, 6, 10, 5, 10, 9
866};
867
868
869GrIndexBuffer* GrOvalRenderer::rRectIndexBuffer(GrGpu* gpu) {
870 if (NULL == fRRectIndexBuffer) {
871 fRRectIndexBuffer =
872 gpu->createIndexBuffer(sizeof(gRRectIndices), false);
873 if (NULL != fRRectIndexBuffer) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000874#ifdef SK_DEBUG
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000875 bool updated =
876#endif
877 fRRectIndexBuffer->updateData(gRRectIndices,
878 sizeof(gRRectIndices));
879 GR_DEBUGASSERT(updated);
880 }
881 }
882 return fRRectIndexBuffer;
883}
884
skia.committer@gmail.com2fd42c42013-05-03 07:01:00 +0000885bool GrOvalRenderer::drawSimpleRRect(GrDrawTarget* target, GrContext* context, bool useAA,
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000886 const SkRRect& rrect, const SkStrokeRec& stroke)
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000887{
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000888 // only anti-aliased rrects for now
889 if (!useAA) {
890 return false;
891 }
892
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000893 const SkMatrix& vm = context->getMatrix();
894#ifdef SK_DEBUG
895 {
896 // we should have checked for this previously
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000897 SkASSERT(useAA && vm.rectStaysRect() && rrect.isSimple());
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000898 }
899#endif
900
901 // do any matrix crunching before we reset the draw state for device coords
902 const SkRect& rrectBounds = rrect.getBounds();
903 SkRect bounds;
904 vm.mapRect(&bounds, rrectBounds);
905
906 SkVector radii = rrect.getSimpleRadii();
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000907 SkScalar xRadius = SkScalarAbs(vm[SkMatrix::kMScaleX]*radii.fX +
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000908 vm[SkMatrix::kMSkewY]*radii.fY);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000909 SkScalar yRadius = SkScalarAbs(vm[SkMatrix::kMSkewX]*radii.fX +
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000910 vm[SkMatrix::kMScaleY]*radii.fY);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000911
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000912 // if hairline stroke is greater than radius, we don't handle that right now
913 SkStrokeRec::Style style = stroke.getStyle();
914 if (SkStrokeRec::kHairline_Style == style &&
915 (SK_ScalarHalf >= xRadius || SK_ScalarHalf >= yRadius)) {
916 return false;
917 }
918
919 // do (potentially) anisotropic mapping of stroke
920 SkVector scaledStroke;
921 SkScalar strokeWidth = stroke.getWidth();
922 scaledStroke.fX = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMScaleX] + vm[SkMatrix::kMSkewY]));
923 scaledStroke.fY = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMSkewX] + vm[SkMatrix::kMScaleY]));
924
925 // if half of strokewidth is greater than radius, we don't handle that right now
926 if (SK_ScalarHalf*scaledStroke.fX >= xRadius || SK_ScalarHalf*scaledStroke.fY >= yRadius) {
927 return false;
928 }
929
930 // reset to device coordinates
931 GrDrawState* drawState = target->drawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000932 GrDrawState::AutoViewMatrixRestore avmr;
933 if (!avmr.setIdentity(drawState)) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000934 return false;
935 }
936
937 bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style);
938
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000939 GrIndexBuffer* indexBuffer = this->rRectIndexBuffer(context->getGpu());
940 if (NULL == indexBuffer) {
941 GrPrintf("Failed to create index buffer!\n");
942 return false;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000943 }
944
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000945 // if the corners are circles, use the circle renderer
946 if ((!isStroked || scaledStroke.fX == scaledStroke.fY) && xRadius == yRadius) {
947 drawState->setVertexAttribs<gCircleVertexAttribs>(SK_ARRAY_COUNT(gCircleVertexAttribs));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000948 SkASSERT(sizeof(CircleVertex) == drawState->getVertexSize());
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000949
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000950 GrDrawTarget::AutoReleaseGeometry geo(target, 16, 0);
951 if (!geo.succeeded()) {
952 GrPrintf("Failed to get space for vertices!\n");
953 return false;
954 }
955 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000956
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000957 SkScalar innerRadius = 0.0f;
958 SkScalar outerRadius = xRadius;
959 SkScalar halfWidth = 0;
960 if (style != SkStrokeRec::kFill_Style) {
961 if (SkScalarNearlyZero(scaledStroke.fX)) {
962 halfWidth = SK_ScalarHalf;
963 } else {
964 halfWidth = SkScalarHalf(scaledStroke.fX);
965 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000966
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000967 if (isStroked) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000968 innerRadius = xRadius - halfWidth;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000969 }
970 outerRadius += halfWidth;
971 bounds.outset(halfWidth, halfWidth);
972 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000973
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000974 isStroked = (isStroked && innerRadius > 0);
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +0000975
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000976 GrEffectRef* effect = CircleEdgeEffect::Create(isStroked);
977 static const int kCircleEdgeAttrIndex = 1;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000978 drawState->addCoverageEffect(effect, kCircleEdgeAttrIndex)->unref();
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +0000979
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000980 // The radii are outset for two reasons. First, it allows the shader to simply perform
981 // clamp(distance-to-center - radius, 0, 1). Second, the outer radius is used to compute the
982 // verts of the bounding box that is rendered and the outset ensures the box will cover all
983 // pixels partially covered by the circle.
984 outerRadius += SK_ScalarHalf;
985 innerRadius -= SK_ScalarHalf;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000986
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000987 // Expand the rect so all the pixels will be captured.
988 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000989
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000990 SkScalar yCoords[4] = {
991 bounds.fTop,
992 bounds.fTop + outerRadius,
993 bounds.fBottom - outerRadius,
994 bounds.fBottom
995 };
996 SkScalar yOuterRadii[4] = {
997 -outerRadius,
998 0,
999 0,
1000 outerRadius
1001 };
1002 for (int i = 0; i < 4; ++i) {
1003 verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]);
1004 verts->fOffset = SkPoint::Make(-outerRadius, yOuterRadii[i]);
1005 verts->fOuterRadius = outerRadius;
1006 verts->fInnerRadius = innerRadius;
1007 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001008
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001009 verts->fPos = SkPoint::Make(bounds.fLeft + outerRadius, yCoords[i]);
1010 verts->fOffset = SkPoint::Make(0, yOuterRadii[i]);
1011 verts->fOuterRadius = outerRadius;
1012 verts->fInnerRadius = innerRadius;
1013 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001014
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001015 verts->fPos = SkPoint::Make(bounds.fRight - outerRadius, yCoords[i]);
1016 verts->fOffset = SkPoint::Make(0, yOuterRadii[i]);
1017 verts->fOuterRadius = outerRadius;
1018 verts->fInnerRadius = innerRadius;
1019 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001020
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001021 verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]);
1022 verts->fOffset = SkPoint::Make(outerRadius, yOuterRadii[i]);
1023 verts->fOuterRadius = outerRadius;
1024 verts->fInnerRadius = innerRadius;
1025 verts++;
1026 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001027
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001028 // drop out the middle quad if we're stroked
1029 int indexCnt = isStroked ? GR_ARRAY_COUNT(gRRectIndices)-6 : GR_ARRAY_COUNT(gRRectIndices);
1030 target->setIndexSourceToBuffer(indexBuffer);
1031 target->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, 16, indexCnt, &bounds);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001032
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001033 // otherwise we use the ellipse renderer
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001034 } else {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001035 drawState->setVertexAttribs<gEllipseVertexAttribs>(SK_ARRAY_COUNT(gEllipseVertexAttribs));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001036 SkASSERT(sizeof(EllipseVertex) == drawState->getVertexSize());
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001037
1038 SkScalar innerXRadius = 0.0f;
1039 SkScalar innerYRadius = 0.0f;
1040 if (SkStrokeRec::kFill_Style != style) {
1041 if (SkScalarNearlyZero(scaledStroke.length())) {
1042 scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf);
1043 } else {
1044 scaledStroke.scale(SK_ScalarHalf);
1045 }
1046
1047 // we only handle thick strokes for near-circular ellipses
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +00001048 if (scaledStroke.length() > SK_ScalarHalf &&
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001049 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
1050 return false;
1051 }
1052
1053 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
1054 if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius ||
1055 scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) {
1056 return false;
1057 }
1058
1059 // this is legit only if scale & translation (which should be the case at the moment)
1060 if (isStroked) {
1061 innerXRadius = xRadius - scaledStroke.fX;
1062 innerYRadius = yRadius - scaledStroke.fY;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001063 }
1064
1065 xRadius += scaledStroke.fX;
1066 yRadius += scaledStroke.fY;
1067 bounds.outset(scaledStroke.fX, scaledStroke.fY);
1068 }
jvanverth@google.come3647412013-05-08 15:31:43 +00001069
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +00001070 isStroked = (isStroked && innerXRadius > 0 && innerYRadius > 0);
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +00001071
jvanverth@google.come3647412013-05-08 15:31:43 +00001072 GrDrawTarget::AutoReleaseGeometry geo(target, 16, 0);
1073 if (!geo.succeeded()) {
1074 GrPrintf("Failed to get space for vertices!\n");
1075 return false;
1076 }
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001077 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices());
jvanverth@google.come3647412013-05-08 15:31:43 +00001078
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001079 GrEffectRef* effect = EllipseEdgeEffect::Create(isStroked);
jvanverth@google.come3647412013-05-08 15:31:43 +00001080 static const int kEllipseOffsetAttrIndex = 1;
1081 static const int kEllipseRadiiAttrIndex = 2;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001082 drawState->addCoverageEffect(effect,
1083 kEllipseOffsetAttrIndex, kEllipseRadiiAttrIndex)->unref();
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001084
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001085 // Compute the reciprocals of the radii here to save time in the shader
1086 SkScalar xRadRecip = SkScalarInvert(xRadius);
1087 SkScalar yRadRecip = SkScalarInvert(yRadius);
1088 SkScalar xInnerRadRecip = SkScalarInvert(innerXRadius);
1089 SkScalar yInnerRadRecip = SkScalarInvert(innerYRadius);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001090
1091 // Extend the radii out half a pixel to antialias.
1092 SkScalar xOuterRadius = xRadius + SK_ScalarHalf;
1093 SkScalar yOuterRadius = yRadius + SK_ScalarHalf;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001094
1095 // Expand the rect so all the pixels will be captured.
1096 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1097
1098 SkScalar yCoords[4] = {
1099 bounds.fTop,
1100 bounds.fTop + yOuterRadius,
1101 bounds.fBottom - yOuterRadius,
1102 bounds.fBottom
1103 };
1104 SkScalar yOuterOffsets[4] = {
jvanverth@google.comd1b5b142013-07-02 14:46:03 +00001105 yOuterRadius,
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001106 SK_ScalarNearlyZero, // we're using inversesqrt() in the shader, so can't be exactly 0
1107 SK_ScalarNearlyZero,
1108 yOuterRadius
1109 };
1110
1111 for (int i = 0; i < 4; ++i) {
1112 verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]);
jvanverth@google.comd1b5b142013-07-02 14:46:03 +00001113 verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001114 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1115 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001116 verts++;
1117
1118 verts->fPos = SkPoint::Make(bounds.fLeft + xOuterRadius, yCoords[i]);
1119 verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001120 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1121 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001122 verts++;
1123
1124 verts->fPos = SkPoint::Make(bounds.fRight - xOuterRadius, yCoords[i]);
1125 verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001126 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1127 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001128 verts++;
1129
1130 verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]);
1131 verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001132 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1133 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001134 verts++;
1135 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001136
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001137 // drop out the middle quad if we're stroked
1138 int indexCnt = isStroked ? GR_ARRAY_COUNT(gRRectIndices)-6 : GR_ARRAY_COUNT(gRRectIndices);
1139 target->setIndexSourceToBuffer(indexBuffer);
1140 target->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, 16, indexCnt, &bounds);
1141 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001142
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001143 return true;
1144}