blob: 2ade8cde07cad184f44c48bd34d6e27d243249ac [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 +000025namespace {
26
27struct CircleVertex {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000028 SkPoint fPos;
29 SkPoint fOffset;
commit-bot@chromium.org81312832013-03-22 18:34:09 +000030 SkScalar fOuterRadius;
31 SkScalar fInnerRadius;
32};
33
34struct EllipseVertex {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000035 SkPoint fPos;
36 SkPoint fOffset;
37 SkPoint fOuterRadii;
38 SkPoint fInnerRadii;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +000039};
40
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +000041struct DIEllipseVertex {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000042 SkPoint fPos;
43 SkPoint fOuterOffset;
44 SkPoint fInnerOffset;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +000045};
46
commit-bot@chromium.org81312832013-03-22 18:34:09 +000047inline bool circle_stays_circle(const SkMatrix& m) {
48 return m.isSimilarity();
49}
50
51}
52
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000053///////////////////////////////////////////////////////////////////////////////
54
55/**
56 * 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 +000057 * specified as offset_x, offset_y (both from center point), outer radius and inner radius.
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000058 */
59
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +000060class CircleEdgeEffect : public GrVertexEffect {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000061public:
62 static GrEffectRef* Create(bool stroke) {
bsalomon@google.comd42aca32013-04-23 15:37:27 +000063 GR_CREATE_STATIC_EFFECT(gCircleStrokeEdge, CircleEdgeEffect, (true));
64 GR_CREATE_STATIC_EFFECT(gCircleFillEdge, CircleEdgeEffect, (false));
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000065
66 if (stroke) {
bsalomon@google.comd42aca32013-04-23 15:37:27 +000067 gCircleStrokeEdge->ref();
68 return gCircleStrokeEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000069 } else {
bsalomon@google.comd42aca32013-04-23 15:37:27 +000070 gCircleFillEdge->ref();
71 return gCircleFillEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000072 }
73 }
74
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +000075 virtual void getConstantColorComponents(GrColor* color,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000076 uint32_t* validFlags) const SK_OVERRIDE {
77 *validFlags = 0;
78 }
79
80 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
81 return GrTBackendEffectFactory<CircleEdgeEffect>::getInstance();
82 }
83
84 virtual ~CircleEdgeEffect() {}
85
86 static const char* Name() { return "CircleEdge"; }
87
88 inline bool isStroked() const { return fStroke; }
89
commit-bot@chromium.org261dc562013-10-04 15:42:56 +000090 class GLEffect : public GrGLVertexEffect {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000091 public:
92 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
93 : INHERITED (factory) {}
94
commit-bot@chromium.org261dc562013-10-04 15:42:56 +000095 virtual void emitCode(GrGLFullShaderBuilder* builder,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000096 const GrDrawEffect& drawEffect,
97 EffectKey key,
98 const char* outputColor,
99 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000100 const TransformedCoordsArray&,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000101 const TextureSamplerArray& samplers) SK_OVERRIDE {
102 const CircleEdgeEffect& circleEffect = drawEffect.castEffect<CircleEdgeEffect>();
103 const char *vsName, *fsName;
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000104 builder->addVarying(kVec4f_GrSLType, "CircleEdge", &vsName, &fsName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000105
106 const SkString* attrName =
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000107 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]);
108 builder->vsCodeAppendf("\t%s = %s;\n", vsName, attrName->c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000109
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000110 builder->fsCodeAppendf("\tfloat d = length(%s.xy);\n", fsName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000111 builder->fsCodeAppendf("\tfloat edgeAlpha = clamp(%s.z - d, 0.0, 1.0);\n", fsName);
112 if (circleEffect.isStroked()) {
113 builder->fsCodeAppendf("\tfloat innerAlpha = clamp(d - %s.w, 0.0, 1.0);\n", fsName);
114 builder->fsCodeAppend("\tedgeAlpha *= innerAlpha;\n");
115 }
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000116
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000117 builder->fsCodeAppendf("\t%s = %s;\n", outputColor,
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000118 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("edgeAlpha")).c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000119 }
120
121 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
122 const CircleEdgeEffect& circleEffect = drawEffect.castEffect<CircleEdgeEffect>();
123
124 return circleEffect.isStroked() ? 0x1 : 0x0;
125 }
126
127 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {}
128
129 private:
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000130 typedef GrGLVertexEffect INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000131 };
132
133
134private:
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000135 CircleEdgeEffect(bool stroke) : GrVertexEffect() {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000136 this->addVertexAttrib(kVec4f_GrSLType);
137 fStroke = stroke;
138 }
139
140 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
141 const CircleEdgeEffect& cee = CastEffect<CircleEdgeEffect>(other);
142 return cee.fStroke == fStroke;
143 }
144
145 bool fStroke;
146
147 GR_DECLARE_EFFECT_TEST;
148
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000149 typedef GrVertexEffect INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000150};
151
152GR_DEFINE_EFFECT_TEST(CircleEdgeEffect);
153
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000154GrEffectRef* CircleEdgeEffect::TestCreate(SkRandom* random,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000155 GrContext* context,
156 const GrDrawTargetCaps&,
157 GrTexture* textures[]) {
158 return CircleEdgeEffect::Create(random->nextBool());
159}
160
161///////////////////////////////////////////////////////////////////////////////
162
163/**
164 * 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 +0000165 * ellipse, specified as a 2D offset from center, and the reciprocals of the outer and inner radii,
166 * in both x and y directions.
167 *
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000168 * 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 +0000169 */
170
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000171class EllipseEdgeEffect : public GrVertexEffect {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000172public:
173 static GrEffectRef* Create(bool stroke) {
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000174 GR_CREATE_STATIC_EFFECT(gEllipseStrokeEdge, EllipseEdgeEffect, (true));
175 GR_CREATE_STATIC_EFFECT(gEllipseFillEdge, EllipseEdgeEffect, (false));
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000176
177 if (stroke) {
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000178 gEllipseStrokeEdge->ref();
179 return gEllipseStrokeEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000180 } else {
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000181 gEllipseFillEdge->ref();
182 return gEllipseFillEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000183 }
184 }
185
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000186 virtual void getConstantColorComponents(GrColor* color,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000187 uint32_t* validFlags) const SK_OVERRIDE {
188 *validFlags = 0;
189 }
190
191 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
192 return GrTBackendEffectFactory<EllipseEdgeEffect>::getInstance();
193 }
194
195 virtual ~EllipseEdgeEffect() {}
196
197 static const char* Name() { return "EllipseEdge"; }
198
199 inline bool isStroked() const { return fStroke; }
200
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000201 class GLEffect : public GrGLVertexEffect {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000202 public:
203 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
204 : INHERITED (factory) {}
205
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000206 virtual void emitCode(GrGLFullShaderBuilder* builder,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000207 const GrDrawEffect& drawEffect,
208 EffectKey key,
209 const char* outputColor,
210 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000211 const TransformedCoordsArray&,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000212 const TextureSamplerArray& samplers) SK_OVERRIDE {
213 const EllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<EllipseEdgeEffect>();
214
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000215 const char *vsOffsetName, *fsOffsetName;
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000216 const char *vsRadiiName, *fsRadiiName;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000217
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000218 builder->addVarying(kVec2f_GrSLType, "EllipseOffsets", &vsOffsetName, &fsOffsetName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000219 const SkString* attr0Name =
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000220 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]);
221 builder->vsCodeAppendf("\t%s = %s;\n", vsOffsetName, attr0Name->c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000222
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000223 builder->addVarying(kVec4f_GrSLType, "EllipseRadii", &vsRadiiName, &fsRadiiName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000224 const SkString* attr1Name =
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000225 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[1]);
226 builder->vsCodeAppendf("\t%s = %s;\n", vsRadiiName, attr1Name->c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000227
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +0000228 // for outer curve
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000229 builder->fsCodeAppendf("\tvec2 scaledOffset = %s*%s.xy;\n", fsOffsetName, fsRadiiName);
230 builder->fsCodeAppend("\tfloat test = dot(scaledOffset, scaledOffset) - 1.0;\n");
231 builder->fsCodeAppendf("\tvec2 grad = 2.0*scaledOffset*%s.xy;\n", fsRadiiName);
jvanverth@google.comd1b5b142013-07-02 14:46:03 +0000232 builder->fsCodeAppend("\tfloat grad_dot = dot(grad, grad);\n");
commit-bot@chromium.org1b035d82014-04-09 17:11:09 +0000233 // avoid calling inversesqrt on zero.
234 builder->fsCodeAppend("\tgrad_dot = max(grad_dot, 1.0e-4);\n");
jvanverth@google.comd1b5b142013-07-02 14:46:03 +0000235 builder->fsCodeAppend("\tfloat invlen = inversesqrt(grad_dot);\n");
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000236 builder->fsCodeAppend("\tfloat edgeAlpha = clamp(0.5-test*invlen, 0.0, 1.0);\n");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000237
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000238 // for inner curve
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000239 if (ellipseEffect.isStroked()) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000240 builder->fsCodeAppendf("\tscaledOffset = %s*%s.zw;\n", fsOffsetName, fsRadiiName);
241 builder->fsCodeAppend("\ttest = dot(scaledOffset, scaledOffset) - 1.0;\n");
242 builder->fsCodeAppendf("\tgrad = 2.0*scaledOffset*%s.zw;\n", fsRadiiName);
243 builder->fsCodeAppend("\tinvlen = inversesqrt(dot(grad, grad));\n");
244 builder->fsCodeAppend("\tedgeAlpha *= clamp(0.5+test*invlen, 0.0, 1.0);\n");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000245 }
246
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000247 builder->fsCodeAppendf("\t%s = %s;\n", outputColor,
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000248 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("edgeAlpha")).c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000249 }
250
251 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
252 const EllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<EllipseEdgeEffect>();
253
254 return ellipseEffect.isStroked() ? 0x1 : 0x0;
255 }
256
257 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {
258 }
259
260 private:
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000261 typedef GrGLVertexEffect INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000262 };
263
264private:
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000265 EllipseEdgeEffect(bool stroke) : GrVertexEffect() {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000266 this->addVertexAttrib(kVec2f_GrSLType);
267 this->addVertexAttrib(kVec4f_GrSLType);
268 fStroke = stroke;
269 }
270
271 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
272 const EllipseEdgeEffect& eee = CastEffect<EllipseEdgeEffect>(other);
273 return eee.fStroke == fStroke;
274 }
275
276 bool fStroke;
277
278 GR_DECLARE_EFFECT_TEST;
279
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000280 typedef GrVertexEffect INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000281};
282
283GR_DEFINE_EFFECT_TEST(EllipseEdgeEffect);
284
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000285GrEffectRef* EllipseEdgeEffect::TestCreate(SkRandom* random,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000286 GrContext* context,
287 const GrDrawTargetCaps&,
288 GrTexture* textures[]) {
289 return EllipseEdgeEffect::Create(random->nextBool());
290}
291
292///////////////////////////////////////////////////////////////////////////////
293
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000294/**
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000295 * 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 +0000296 * specified as a 2D offset from center for both the outer and inner paths (if stroked). The
297 * implict equation used is for a unit circle (x^2 + y^2 - 1 = 0) and the edge corrected by
298 * using differentials.
299 *
300 * The result is device-independent and can be used with any affine matrix.
301 */
302
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000303class DIEllipseEdgeEffect : public GrVertexEffect {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000304public:
305 enum Mode { kStroke = 0, kHairline, kFill };
306
307 static GrEffectRef* Create(Mode mode) {
308 GR_CREATE_STATIC_EFFECT(gEllipseStrokeEdge, DIEllipseEdgeEffect, (kStroke));
309 GR_CREATE_STATIC_EFFECT(gEllipseHairlineEdge, DIEllipseEdgeEffect, (kHairline));
310 GR_CREATE_STATIC_EFFECT(gEllipseFillEdge, DIEllipseEdgeEffect, (kFill));
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000311
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000312 if (kStroke == mode) {
313 gEllipseStrokeEdge->ref();
314 return gEllipseStrokeEdge;
315 } else if (kHairline == mode) {
316 gEllipseHairlineEdge->ref();
317 return gEllipseHairlineEdge;
318 } else {
319 gEllipseFillEdge->ref();
320 return gEllipseFillEdge;
321 }
322 }
323
324 virtual void getConstantColorComponents(GrColor* color,
325 uint32_t* validFlags) const SK_OVERRIDE {
326 *validFlags = 0;
327 }
328
329 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
330 return GrTBackendEffectFactory<DIEllipseEdgeEffect>::getInstance();
331 }
332
333 virtual ~DIEllipseEdgeEffect() {}
334
335 static const char* Name() { return "DIEllipseEdge"; }
336
337 inline Mode getMode() const { return fMode; }
338
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000339 class GLEffect : public GrGLVertexEffect {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000340 public:
341 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
342 : INHERITED (factory) {}
343
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000344 virtual void emitCode(GrGLFullShaderBuilder* builder,
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000345 const GrDrawEffect& drawEffect,
346 EffectKey key,
347 const char* outputColor,
348 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000349 const TransformedCoordsArray&,
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000350 const TextureSamplerArray& samplers) SK_OVERRIDE {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000351 const DIEllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<DIEllipseEdgeEffect>();
352
353 SkAssertResult(builder->enableFeature(
354 GrGLShaderBuilder::kStandardDerivatives_GLSLFeature));
355
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000356 const char *vsOffsetName0, *fsOffsetName0;
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000357 builder->addVarying(kVec2f_GrSLType, "EllipseOffsets0",
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000358 &vsOffsetName0, &fsOffsetName0);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000359 const SkString* attr0Name =
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000360 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]);
361 builder->vsCodeAppendf("\t%s = %s;\n", vsOffsetName0, attr0Name->c_str());
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000362 const char *vsOffsetName1, *fsOffsetName1;
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000363 builder->addVarying(kVec2f_GrSLType, "EllipseOffsets1",
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000364 &vsOffsetName1, &fsOffsetName1);
365 const SkString* attr1Name =
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000366 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[1]);
367 builder->vsCodeAppendf("\t%s = %s;\n", vsOffsetName1, attr1Name->c_str());
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000368
369 // for outer curve
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000370 builder->fsCodeAppendf("\tvec2 scaledOffset = %s.xy;\n", fsOffsetName0);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000371 builder->fsCodeAppend("\tfloat test = dot(scaledOffset, scaledOffset) - 1.0;\n");
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000372 builder->fsCodeAppendf("\tvec2 duvdx = dFdx(%s);\n", fsOffsetName0);
373 builder->fsCodeAppendf("\tvec2 duvdy = dFdy(%s);\n", fsOffsetName0);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000374 builder->fsCodeAppendf("\tvec2 grad = vec2(2.0*%s.x*duvdx.x + 2.0*%s.y*duvdx.y,\n"
375 "\t 2.0*%s.x*duvdy.x + 2.0*%s.y*duvdy.y);\n",
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000376 fsOffsetName0, fsOffsetName0, fsOffsetName0, fsOffsetName0);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000377
378 builder->fsCodeAppend("\tfloat grad_dot = dot(grad, grad);\n");
commit-bot@chromium.org1b035d82014-04-09 17:11:09 +0000379 // avoid calling inversesqrt on zero.
380 builder->fsCodeAppend("\tgrad_dot = max(grad_dot, 1.0e-4);\n");
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000381 builder->fsCodeAppend("\tfloat invlen = inversesqrt(grad_dot);\n");
382 if (kHairline == ellipseEffect.getMode()) {
383 // can probably do this with one step
384 builder->fsCodeAppend("\tfloat edgeAlpha = clamp(1.0-test*invlen, 0.0, 1.0);\n");
385 builder->fsCodeAppend("\tedgeAlpha *= clamp(1.0+test*invlen, 0.0, 1.0);\n");
386 } else {
387 builder->fsCodeAppend("\tfloat edgeAlpha = clamp(0.5-test*invlen, 0.0, 1.0);\n");
388 }
389
390 // for inner curve
391 if (kStroke == ellipseEffect.getMode()) {
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000392 builder->fsCodeAppendf("\tscaledOffset = %s.xy;\n", fsOffsetName1);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000393 builder->fsCodeAppend("\ttest = dot(scaledOffset, scaledOffset) - 1.0;\n");
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000394 builder->fsCodeAppendf("\tduvdx = dFdx(%s);\n", fsOffsetName1);
395 builder->fsCodeAppendf("\tduvdy = dFdy(%s);\n", fsOffsetName1);
396 builder->fsCodeAppendf("\tgrad = vec2(2.0*%s.x*duvdx.x + 2.0*%s.y*duvdx.y,\n"
397 "\t 2.0*%s.x*duvdy.x + 2.0*%s.y*duvdy.y);\n",
398 fsOffsetName1, fsOffsetName1, fsOffsetName1, fsOffsetName1);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000399 builder->fsCodeAppend("\tinvlen = inversesqrt(dot(grad, grad));\n");
400 builder->fsCodeAppend("\tedgeAlpha *= clamp(0.5+test*invlen, 0.0, 1.0);\n");
401 }
402
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000403 builder->fsCodeAppendf("\t%s = %s;\n", outputColor,
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000404 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("edgeAlpha")).c_str());
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000405 }
406
407 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
408 const DIEllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<DIEllipseEdgeEffect>();
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000409
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000410 return ellipseEffect.getMode();
411 }
412
413 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {
414 }
415
416 private:
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000417 typedef GrGLVertexEffect INHERITED;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000418 };
419
420private:
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000421 DIEllipseEdgeEffect(Mode mode) : GrVertexEffect() {
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000422 this->addVertexAttrib(kVec2f_GrSLType);
423 this->addVertexAttrib(kVec2f_GrSLType);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000424 fMode = mode;
425 }
426
427 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
428 const DIEllipseEdgeEffect& eee = CastEffect<DIEllipseEdgeEffect>(other);
429 return eee.fMode == fMode;
430 }
431
432 Mode fMode;
433
434 GR_DECLARE_EFFECT_TEST;
435
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000436 typedef GrVertexEffect INHERITED;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000437};
438
439GR_DEFINE_EFFECT_TEST(DIEllipseEdgeEffect);
440
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000441GrEffectRef* DIEllipseEdgeEffect::TestCreate(SkRandom* random,
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000442 GrContext* context,
443 const GrDrawTargetCaps&,
444 GrTexture* textures[]) {
445 return DIEllipseEdgeEffect::Create((Mode)(random->nextRangeU(0,2)));
446}
447
448///////////////////////////////////////////////////////////////////////////////
449
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000450void GrOvalRenderer::reset() {
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000451 SkSafeSetNull(fRRectIndexBuffer);
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000452}
453
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000454bool GrOvalRenderer::drawOval(GrDrawTarget* target, const GrContext* context, bool useAA,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000455 const SkRect& oval, const SkStrokeRec& stroke)
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000456{
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000457 bool useCoverageAA = useAA &&
458 !target->getDrawState().getRenderTarget()->isMultisampled() &&
459 !target->shouldDisableCoverageAAForBlend();
460
461 if (!useCoverageAA) {
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000462 return false;
463 }
464
465 const SkMatrix& vm = context->getMatrix();
466
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000467 // we can draw circles
468 if (SkScalarNearlyEqual(oval.width(), oval.height())
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000469 && circle_stays_circle(vm)) {
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000470 this->drawCircle(target, useCoverageAA, oval, stroke);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000471 // if we have shader derivative support, render as device-independent
472 } else if (target->caps()->shaderDerivativeSupport()) {
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000473 return this->drawDIEllipse(target, useCoverageAA, oval, stroke);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000474 // otherwise axis-aligned ellipses only
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000475 } else if (vm.rectStaysRect()) {
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000476 return this->drawEllipse(target, useCoverageAA, oval, stroke);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000477 } else {
478 return false;
479 }
480
481 return true;
482}
483
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000484///////////////////////////////////////////////////////////////////////////////
485
robertphillips@google.com42903302013-04-20 12:26:07 +0000486// position + edge
487extern const GrVertexAttrib gCircleVertexAttribs[] = {
488 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000489 {kVec4f_GrVertexAttribType, sizeof(SkPoint), kEffect_GrVertexAttribBinding}
robertphillips@google.com42903302013-04-20 12:26:07 +0000490};
491
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000492void GrOvalRenderer::drawCircle(GrDrawTarget* target,
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000493 bool useCoverageAA,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000494 const SkRect& circle,
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000495 const SkStrokeRec& stroke)
496{
497 GrDrawState* drawState = target->drawState();
498
499 const SkMatrix& vm = drawState->getViewMatrix();
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000500 SkPoint center = SkPoint::Make(circle.centerX(), circle.centerY());
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000501 vm.mapPoints(&center, 1);
502 SkScalar radius = vm.mapRadius(SkScalarHalf(circle.width()));
503 SkScalar strokeWidth = vm.mapRadius(stroke.getWidth());
504
bsalomon@google.com137f1342013-05-29 21:27:53 +0000505 GrDrawState::AutoViewMatrixRestore avmr;
506 if (!avmr.setIdentity(drawState)) {
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000507 return;
508 }
509
robertphillips@google.com42903302013-04-20 12:26:07 +0000510 drawState->setVertexAttribs<gCircleVertexAttribs>(SK_ARRAY_COUNT(gCircleVertexAttribs));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000511 SkASSERT(sizeof(CircleVertex) == drawState->getVertexSize());
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000512
513 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
514 if (!geo.succeeded()) {
515 GrPrintf("Failed to get space for vertices!\n");
516 return;
517 }
518
519 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
520
521 SkStrokeRec::Style style = stroke.getStyle();
522 bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style);
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000523
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000524 SkScalar innerRadius = 0.0f;
525 SkScalar outerRadius = radius;
526 SkScalar halfWidth = 0;
527 if (style != SkStrokeRec::kFill_Style) {
528 if (SkScalarNearlyZero(strokeWidth)) {
529 halfWidth = SK_ScalarHalf;
530 } else {
531 halfWidth = SkScalarHalf(strokeWidth);
532 }
533
534 outerRadius += halfWidth;
535 if (isStroked) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000536 innerRadius = radius - halfWidth;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000537 }
538 }
539
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +0000540 GrEffectRef* effect = CircleEdgeEffect::Create(isStroked && innerRadius > 0);
541 static const int kCircleEdgeAttrIndex = 1;
542 drawState->addCoverageEffect(effect, kCircleEdgeAttrIndex)->unref();
543
bsalomon@google.com58e30fe2013-04-01 19:01:20 +0000544 // The radii are outset for two reasons. First, it allows the shader to simply perform
545 // clamp(distance-to-center - radius, 0, 1). Second, the outer radius is used to compute the
546 // verts of the bounding box that is rendered and the outset ensures the box will cover all
547 // pixels partially covered by the circle.
548 outerRadius += SK_ScalarHalf;
549 innerRadius -= SK_ScalarHalf;
550
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000551 SkRect bounds = SkRect::MakeLTRB(
bsalomon@google.com58e30fe2013-04-01 19:01:20 +0000552 center.fX - outerRadius,
553 center.fY - outerRadius,
554 center.fX + outerRadius,
555 center.fY + outerRadius
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000556 );
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000557
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000558 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000559 verts[0].fOffset = SkPoint::Make(-outerRadius, -outerRadius);
560 verts[0].fOuterRadius = outerRadius;
561 verts[0].fInnerRadius = innerRadius;
562
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000563 verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
skia.committer@gmail.com46746762013-04-12 07:01:51 +0000564 verts[1].fOffset = SkPoint::Make(outerRadius, -outerRadius);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000565 verts[1].fOuterRadius = outerRadius;
566 verts[1].fInnerRadius = innerRadius;
567
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000568 verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000569 verts[2].fOffset = SkPoint::Make(-outerRadius, outerRadius);
570 verts[2].fOuterRadius = outerRadius;
571 verts[2].fInnerRadius = innerRadius;
572
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000573 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000574 verts[3].fOffset = SkPoint::Make(outerRadius, outerRadius);
575 verts[3].fOuterRadius = outerRadius;
576 verts[3].fInnerRadius = innerRadius;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000577
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000578 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000579}
580
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000581///////////////////////////////////////////////////////////////////////////////
582
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000583// position + offset + 1/radii
robertphillips@google.com42903302013-04-20 12:26:07 +0000584extern const GrVertexAttrib gEllipseVertexAttribs[] = {
585 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000586 {kVec2f_GrVertexAttribType, sizeof(SkPoint), kEffect_GrVertexAttribBinding},
587 {kVec4f_GrVertexAttribType, 2*sizeof(SkPoint), kEffect_GrVertexAttribBinding}
robertphillips@google.com42903302013-04-20 12:26:07 +0000588};
589
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000590// position + offsets
591extern const GrVertexAttrib gDIEllipseVertexAttribs[] = {
592 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000593 {kVec2f_GrVertexAttribType, sizeof(SkPoint), kEffect_GrVertexAttribBinding},
594 {kVec2f_GrVertexAttribType, 2*sizeof(SkPoint), kEffect_GrVertexAttribBinding},
robertphillips@google.com42903302013-04-20 12:26:07 +0000595};
596
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000597bool GrOvalRenderer::drawEllipse(GrDrawTarget* target,
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000598 bool useCoverageAA,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000599 const SkRect& ellipse,
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000600 const SkStrokeRec& stroke)
601{
602 GrDrawState* drawState = target->drawState();
603#ifdef SK_DEBUG
604 {
605 // we should have checked for this previously
606 bool isAxisAlignedEllipse = drawState->getViewMatrix().rectStaysRect();
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000607 SkASSERT(useCoverageAA && isAxisAlignedEllipse);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000608 }
609#endif
610
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000611 // do any matrix crunching before we reset the draw state for device coords
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000612 const SkMatrix& vm = drawState->getViewMatrix();
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000613 SkPoint center = SkPoint::Make(ellipse.centerX(), ellipse.centerY());
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000614 vm.mapPoints(&center, 1);
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000615 SkScalar ellipseXRadius = SkScalarHalf(ellipse.width());
616 SkScalar ellipseYRadius = SkScalarHalf(ellipse.height());
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +0000617 SkScalar xRadius = SkScalarAbs(vm[SkMatrix::kMScaleX]*ellipseXRadius +
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000618 vm[SkMatrix::kMSkewY]*ellipseYRadius);
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +0000619 SkScalar yRadius = SkScalarAbs(vm[SkMatrix::kMSkewX]*ellipseXRadius +
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000620 vm[SkMatrix::kMScaleY]*ellipseYRadius);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000621
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000622 // do (potentially) anisotropic mapping of stroke
623 SkVector scaledStroke;
624 SkScalar strokeWidth = stroke.getWidth();
625 scaledStroke.fX = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMScaleX] + vm[SkMatrix::kMSkewY]));
626 scaledStroke.fY = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMSkewX] + vm[SkMatrix::kMScaleY]));
627
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000628 SkStrokeRec::Style style = stroke.getStyle();
629 bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style);
630
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000631 SkScalar innerXRadius = 0;
632 SkScalar innerYRadius = 0;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000633 if (SkStrokeRec::kFill_Style != style) {
634 if (SkScalarNearlyZero(scaledStroke.length())) {
635 scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf);
636 } else {
637 scaledStroke.scale(SK_ScalarHalf);
638 }
639
640 // we only handle thick strokes for near-circular ellipses
641 if (scaledStroke.length() > SK_ScalarHalf &&
642 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
643 return false;
644 }
645
646 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
647 if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius ||
648 scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) {
649 return false;
650 }
651
652 // this is legit only if scale & translation (which should be the case at the moment)
653 if (isStroked) {
654 innerXRadius = xRadius - scaledStroke.fX;
655 innerYRadius = yRadius - scaledStroke.fY;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000656 }
657
658 xRadius += scaledStroke.fX;
659 yRadius += scaledStroke.fY;
660 }
661
bsalomon@google.com137f1342013-05-29 21:27:53 +0000662 GrDrawState::AutoViewMatrixRestore avmr;
663 if (!avmr.setIdentity(drawState)) {
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000664 return false;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000665 }
666
robertphillips@google.com42903302013-04-20 12:26:07 +0000667 drawState->setVertexAttribs<gEllipseVertexAttribs>(SK_ARRAY_COUNT(gEllipseVertexAttribs));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000668 SkASSERT(sizeof(EllipseVertex) == drawState->getVertexSize());
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000669
670 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
671 if (!geo.succeeded()) {
672 GrPrintf("Failed to get space for vertices!\n");
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000673 return false;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000674 }
675
676 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices());
677
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +0000678 GrEffectRef* effect = EllipseEdgeEffect::Create(isStroked &&
679 innerXRadius > 0 && innerYRadius > 0);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000680
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000681 static const int kEllipseCenterAttrIndex = 1;
jvanverth@google.com6cc8d442013-09-10 18:24:37 +0000682 static const int kEllipseEdgeAttrIndex = 2;
683 drawState->addCoverageEffect(effect, kEllipseCenterAttrIndex, kEllipseEdgeAttrIndex)->unref();
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000684
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000685 // Compute the reciprocals of the radii here to save time in the shader
686 SkScalar xRadRecip = SkScalarInvert(xRadius);
687 SkScalar yRadRecip = SkScalarInvert(yRadius);
688 SkScalar xInnerRadRecip = SkScalarInvert(innerXRadius);
689 SkScalar yInnerRadRecip = SkScalarInvert(innerYRadius);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000690
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000691 // We've extended the outer x radius out half a pixel to antialias.
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000692 // This will also expand the rect so all the pixels will be captured.
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000693 // TODO: Consider if we should use sqrt(2)/2 instead
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000694 xRadius += SK_ScalarHalf;
695 yRadius += SK_ScalarHalf;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000696
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000697 SkRect bounds = SkRect::MakeLTRB(
698 center.fX - xRadius,
699 center.fY - yRadius,
700 center.fX + xRadius,
701 center.fY + yRadius
702 );
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000703
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000704 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000705 verts[0].fOffset = SkPoint::Make(-xRadius, -yRadius);
706 verts[0].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
707 verts[0].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000708
709 verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000710 verts[1].fOffset = SkPoint::Make(xRadius, -yRadius);
711 verts[1].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
712 verts[1].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000713
714 verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000715 verts[2].fOffset = SkPoint::Make(-xRadius, yRadius);
716 verts[2].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
717 verts[2].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000718
719 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000720 verts[3].fOffset = SkPoint::Make(xRadius, yRadius);
721 verts[3].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
722 verts[3].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
skia.committer@gmail.com46746762013-04-12 07:01:51 +0000723
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000724 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds);
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000725
726 return true;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000727}
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000728
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000729bool GrOvalRenderer::drawDIEllipse(GrDrawTarget* target,
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000730 bool useCoverageAA,
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000731 const SkRect& ellipse,
732 const SkStrokeRec& stroke)
733{
734 GrDrawState* drawState = target->drawState();
735 const SkMatrix& vm = drawState->getViewMatrix();
736
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000737 SkPoint center = SkPoint::Make(ellipse.centerX(), ellipse.centerY());
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000738 SkScalar xRadius = SkScalarHalf(ellipse.width());
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000739 SkScalar yRadius = SkScalarHalf(ellipse.height());
740
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000741 SkStrokeRec::Style style = stroke.getStyle();
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000742 DIEllipseEdgeEffect::Mode mode = (SkStrokeRec::kStroke_Style == style) ?
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000743 DIEllipseEdgeEffect::kStroke :
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000744 (SkStrokeRec::kHairline_Style == style) ?
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000745 DIEllipseEdgeEffect::kHairline : DIEllipseEdgeEffect::kFill;
746
747 SkScalar innerXRadius = 0;
748 SkScalar innerYRadius = 0;
749 if (SkStrokeRec::kFill_Style != style && SkStrokeRec::kHairline_Style != style) {
750 SkScalar strokeWidth = stroke.getWidth();
751
752 if (SkScalarNearlyZero(strokeWidth)) {
753 strokeWidth = SK_ScalarHalf;
754 } else {
755 strokeWidth *= SK_ScalarHalf;
756 }
757
758 // we only handle thick strokes for near-circular ellipses
759 if (strokeWidth > SK_ScalarHalf &&
760 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
761 return false;
762 }
763
764 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
765 if (strokeWidth*(yRadius*yRadius) < (strokeWidth*strokeWidth)*xRadius ||
766 strokeWidth*(xRadius*xRadius) < (strokeWidth*strokeWidth)*yRadius) {
767 return false;
768 }
769
770 // set inner radius (if needed)
771 if (SkStrokeRec::kStroke_Style == style) {
772 innerXRadius = xRadius - strokeWidth;
773 innerYRadius = yRadius - strokeWidth;
774 }
775
776 xRadius += strokeWidth;
777 yRadius += strokeWidth;
778 }
779 if (DIEllipseEdgeEffect::kStroke == mode) {
780 mode = (innerXRadius > 0 && innerYRadius > 0) ? DIEllipseEdgeEffect::kStroke :
781 DIEllipseEdgeEffect::kFill;
782 }
783 SkScalar innerRatioX = SkScalarDiv(xRadius, innerXRadius);
784 SkScalar innerRatioY = SkScalarDiv(yRadius, innerYRadius);
785
786 drawState->setVertexAttribs<gDIEllipseVertexAttribs>(SK_ARRAY_COUNT(gDIEllipseVertexAttribs));
787 SkASSERT(sizeof(DIEllipseVertex) == drawState->getVertexSize());
788
789 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
790 if (!geo.succeeded()) {
791 GrPrintf("Failed to get space for vertices!\n");
792 return false;
793 }
794
795 DIEllipseVertex* verts = reinterpret_cast<DIEllipseVertex*>(geo.vertices());
796
797 GrEffectRef* effect = DIEllipseEdgeEffect::Create(mode);
798
799 static const int kEllipseOuterOffsetAttrIndex = 1;
800 static const int kEllipseInnerOffsetAttrIndex = 2;
801 drawState->addCoverageEffect(effect, kEllipseOuterOffsetAttrIndex,
802 kEllipseInnerOffsetAttrIndex)->unref();
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000803
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000804 // This expands the outer rect so that after CTM we end up with a half-pixel border
805 SkScalar a = vm[SkMatrix::kMScaleX];
806 SkScalar b = vm[SkMatrix::kMSkewX];
807 SkScalar c = vm[SkMatrix::kMSkewY];
808 SkScalar d = vm[SkMatrix::kMScaleY];
809 SkScalar geoDx = SkScalarDiv(SK_ScalarHalf, SkScalarSqrt(a*a + c*c));
810 SkScalar geoDy = SkScalarDiv(SK_ScalarHalf, SkScalarSqrt(b*b + d*d));
811 // This adjusts the "radius" to include the half-pixel border
812 SkScalar offsetDx = SkScalarDiv(geoDx, xRadius);
813 SkScalar offsetDy = SkScalarDiv(geoDy, yRadius);
814
815 SkRect bounds = SkRect::MakeLTRB(
816 center.fX - xRadius - geoDx,
817 center.fY - yRadius - geoDy,
818 center.fX + xRadius + geoDx,
819 center.fY + yRadius + geoDy
820 );
821
822 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
823 verts[0].fOuterOffset = SkPoint::Make(-1.0f - offsetDx, -1.0f - offsetDy);
824 verts[0].fInnerOffset = SkPoint::Make(-innerRatioX - offsetDx, -innerRatioY - offsetDy);
825
826 verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
827 verts[1].fOuterOffset = SkPoint::Make(1.0f + offsetDx, -1.0f - offsetDy);
828 verts[1].fInnerOffset = SkPoint::Make(innerRatioX + offsetDx, -innerRatioY - offsetDy);
829
830 verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
831 verts[2].fOuterOffset = SkPoint::Make(-1.0f - offsetDx, 1.0f + offsetDy);
832 verts[2].fInnerOffset = SkPoint::Make(-innerRatioX - offsetDx, innerRatioY + offsetDy);
833
834 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
835 verts[3].fOuterOffset = SkPoint::Make(1.0f + offsetDx, 1.0f + offsetDy);
836 verts[3].fInnerOffset = SkPoint::Make(innerRatioX + offsetDx, innerRatioY + offsetDy);
837
838 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds);
839
840 return true;
841}
842
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000843///////////////////////////////////////////////////////////////////////////////
844
845static const uint16_t gRRectIndices[] = {
846 // corners
847 0, 1, 5, 0, 5, 4,
848 2, 3, 7, 2, 7, 6,
849 8, 9, 13, 8, 13, 12,
850 10, 11, 15, 10, 15, 14,
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000851
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000852 // edges
853 1, 2, 6, 1, 6, 5,
854 4, 5, 9, 4, 9, 8,
855 6, 7, 11, 6, 11, 10,
856 9, 10, 14, 9, 14, 13,
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000857
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000858 // center
859 // we place this at the end so that we can ignore these indices when rendering stroke-only
860 5, 6, 10, 5, 10, 9
861};
862
863
864GrIndexBuffer* GrOvalRenderer::rRectIndexBuffer(GrGpu* gpu) {
865 if (NULL == fRRectIndexBuffer) {
866 fRRectIndexBuffer =
867 gpu->createIndexBuffer(sizeof(gRRectIndices), false);
868 if (NULL != fRRectIndexBuffer) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000869#ifdef SK_DEBUG
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000870 bool updated =
871#endif
872 fRRectIndexBuffer->updateData(gRRectIndices,
873 sizeof(gRRectIndices));
874 GR_DEBUGASSERT(updated);
875 }
876 }
877 return fRRectIndexBuffer;
878}
879
skia.committer@gmail.com2fd42c42013-05-03 07:01:00 +0000880bool GrOvalRenderer::drawSimpleRRect(GrDrawTarget* target, GrContext* context, bool useAA,
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000881 const SkRRect& rrect, const SkStrokeRec& stroke)
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000882{
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000883 bool useCoverageAA = useAA &&
884 !target->getDrawState().getRenderTarget()->isMultisampled() &&
885 !target->shouldDisableCoverageAAForBlend();
886
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000887 // only anti-aliased rrects for now
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000888 if (!useCoverageAA) {
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000889 return false;
890 }
891
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000892 const SkMatrix& vm = context->getMatrix();
893#ifdef SK_DEBUG
894 {
895 // we should have checked for this previously
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000896 SkASSERT(useCoverageAA && vm.rectStaysRect() && rrect.isSimple());
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000897 }
898#endif
899
900 // do any matrix crunching before we reset the draw state for device coords
901 const SkRect& rrectBounds = rrect.getBounds();
902 SkRect bounds;
903 vm.mapRect(&bounds, rrectBounds);
904
905 SkVector radii = rrect.getSimpleRadii();
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000906 SkScalar xRadius = SkScalarAbs(vm[SkMatrix::kMScaleX]*radii.fX +
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000907 vm[SkMatrix::kMSkewY]*radii.fY);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000908 SkScalar yRadius = SkScalarAbs(vm[SkMatrix::kMSkewX]*radii.fX +
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000909 vm[SkMatrix::kMScaleY]*radii.fY);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000910
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000911 // if hairline stroke is greater than radius, we don't handle that right now
912 SkStrokeRec::Style style = stroke.getStyle();
913 if (SkStrokeRec::kHairline_Style == style &&
commit-bot@chromium.orgeacc4892013-11-07 16:34:53 +0000914 (SK_ScalarHalf > xRadius || SK_ScalarHalf > yRadius)) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000915 return false;
916 }
917
918 // do (potentially) anisotropic mapping of stroke
919 SkVector scaledStroke;
920 SkScalar strokeWidth = stroke.getWidth();
921 scaledStroke.fX = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMScaleX] + vm[SkMatrix::kMSkewY]));
922 scaledStroke.fY = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMSkewX] + vm[SkMatrix::kMScaleY]));
923
924 // if half of strokewidth is greater than radius, we don't handle that right now
commit-bot@chromium.orgeacc4892013-11-07 16:34:53 +0000925 if (SK_ScalarHalf*scaledStroke.fX > xRadius || SK_ScalarHalf*scaledStroke.fY > yRadius) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000926 return false;
927 }
928
929 // reset to device coordinates
930 GrDrawState* drawState = target->drawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000931 GrDrawState::AutoViewMatrixRestore avmr;
932 if (!avmr.setIdentity(drawState)) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000933 return false;
934 }
935
936 bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style);
937
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000938 GrIndexBuffer* indexBuffer = this->rRectIndexBuffer(context->getGpu());
939 if (NULL == indexBuffer) {
940 GrPrintf("Failed to create index buffer!\n");
941 return false;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000942 }
943
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000944 // if the corners are circles, use the circle renderer
945 if ((!isStroked || scaledStroke.fX == scaledStroke.fY) && xRadius == yRadius) {
946 drawState->setVertexAttribs<gCircleVertexAttribs>(SK_ARRAY_COUNT(gCircleVertexAttribs));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000947 SkASSERT(sizeof(CircleVertex) == drawState->getVertexSize());
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000948
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000949 GrDrawTarget::AutoReleaseGeometry geo(target, 16, 0);
950 if (!geo.succeeded()) {
951 GrPrintf("Failed to get space for vertices!\n");
952 return false;
953 }
954 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000955
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000956 SkScalar innerRadius = 0.0f;
957 SkScalar outerRadius = xRadius;
958 SkScalar halfWidth = 0;
959 if (style != SkStrokeRec::kFill_Style) {
960 if (SkScalarNearlyZero(scaledStroke.fX)) {
961 halfWidth = SK_ScalarHalf;
962 } else {
963 halfWidth = SkScalarHalf(scaledStroke.fX);
964 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000965
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000966 if (isStroked) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000967 innerRadius = xRadius - halfWidth;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000968 }
969 outerRadius += halfWidth;
970 bounds.outset(halfWidth, halfWidth);
971 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000972
commit-bot@chromium.orgeacc4892013-11-07 16:34:53 +0000973 isStroked = (isStroked && innerRadius >= 0);
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +0000974
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000975 GrEffectRef* effect = CircleEdgeEffect::Create(isStroked);
976 static const int kCircleEdgeAttrIndex = 1;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000977 drawState->addCoverageEffect(effect, kCircleEdgeAttrIndex)->unref();
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +0000978
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000979 // The radii are outset for two reasons. First, it allows the shader to simply perform
980 // clamp(distance-to-center - radius, 0, 1). Second, the outer radius is used to compute the
981 // verts of the bounding box that is rendered and the outset ensures the box will cover all
982 // pixels partially covered by the circle.
983 outerRadius += SK_ScalarHalf;
984 innerRadius -= SK_ScalarHalf;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000985
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000986 // Expand the rect so all the pixels will be captured.
987 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000988
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000989 SkScalar yCoords[4] = {
990 bounds.fTop,
991 bounds.fTop + outerRadius,
992 bounds.fBottom - outerRadius,
993 bounds.fBottom
994 };
995 SkScalar yOuterRadii[4] = {
996 -outerRadius,
997 0,
998 0,
999 outerRadius
1000 };
1001 for (int i = 0; i < 4; ++i) {
1002 verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]);
1003 verts->fOffset = SkPoint::Make(-outerRadius, yOuterRadii[i]);
1004 verts->fOuterRadius = outerRadius;
1005 verts->fInnerRadius = innerRadius;
1006 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001007
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001008 verts->fPos = SkPoint::Make(bounds.fLeft + outerRadius, yCoords[i]);
1009 verts->fOffset = SkPoint::Make(0, yOuterRadii[i]);
1010 verts->fOuterRadius = outerRadius;
1011 verts->fInnerRadius = innerRadius;
1012 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001013
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001014 verts->fPos = SkPoint::Make(bounds.fRight - outerRadius, yCoords[i]);
1015 verts->fOffset = SkPoint::Make(0, yOuterRadii[i]);
1016 verts->fOuterRadius = outerRadius;
1017 verts->fInnerRadius = innerRadius;
1018 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001019
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001020 verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]);
1021 verts->fOffset = SkPoint::Make(outerRadius, yOuterRadii[i]);
1022 verts->fOuterRadius = outerRadius;
1023 verts->fInnerRadius = innerRadius;
1024 verts++;
1025 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001026
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001027 // drop out the middle quad if we're stroked
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001028 int indexCnt = isStroked ? SK_ARRAY_COUNT(gRRectIndices)-6 : SK_ARRAY_COUNT(gRRectIndices);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001029 target->setIndexSourceToBuffer(indexBuffer);
1030 target->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, 16, indexCnt, &bounds);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001031
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001032 // otherwise we use the ellipse renderer
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001033 } else {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001034 drawState->setVertexAttribs<gEllipseVertexAttribs>(SK_ARRAY_COUNT(gEllipseVertexAttribs));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001035 SkASSERT(sizeof(EllipseVertex) == drawState->getVertexSize());
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001036
1037 SkScalar innerXRadius = 0.0f;
1038 SkScalar innerYRadius = 0.0f;
1039 if (SkStrokeRec::kFill_Style != style) {
1040 if (SkScalarNearlyZero(scaledStroke.length())) {
1041 scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf);
1042 } else {
1043 scaledStroke.scale(SK_ScalarHalf);
1044 }
1045
1046 // we only handle thick strokes for near-circular ellipses
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +00001047 if (scaledStroke.length() > SK_ScalarHalf &&
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001048 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
1049 return false;
1050 }
1051
1052 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
1053 if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius ||
1054 scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) {
1055 return false;
1056 }
1057
1058 // this is legit only if scale & translation (which should be the case at the moment)
1059 if (isStroked) {
1060 innerXRadius = xRadius - scaledStroke.fX;
1061 innerYRadius = yRadius - scaledStroke.fY;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001062 }
1063
1064 xRadius += scaledStroke.fX;
1065 yRadius += scaledStroke.fY;
1066 bounds.outset(scaledStroke.fX, scaledStroke.fY);
1067 }
jvanverth@google.come3647412013-05-08 15:31:43 +00001068
commit-bot@chromium.orgeacc4892013-11-07 16:34:53 +00001069 isStroked = (isStroked && innerXRadius >= 0 && innerYRadius >= 0);
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +00001070
jvanverth@google.come3647412013-05-08 15:31:43 +00001071 GrDrawTarget::AutoReleaseGeometry geo(target, 16, 0);
1072 if (!geo.succeeded()) {
1073 GrPrintf("Failed to get space for vertices!\n");
1074 return false;
1075 }
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001076 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices());
jvanverth@google.come3647412013-05-08 15:31:43 +00001077
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001078 GrEffectRef* effect = EllipseEdgeEffect::Create(isStroked);
jvanverth@google.come3647412013-05-08 15:31:43 +00001079 static const int kEllipseOffsetAttrIndex = 1;
1080 static const int kEllipseRadiiAttrIndex = 2;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001081 drawState->addCoverageEffect(effect,
1082 kEllipseOffsetAttrIndex, kEllipseRadiiAttrIndex)->unref();
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001083
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001084 // Compute the reciprocals of the radii here to save time in the shader
1085 SkScalar xRadRecip = SkScalarInvert(xRadius);
1086 SkScalar yRadRecip = SkScalarInvert(yRadius);
1087 SkScalar xInnerRadRecip = SkScalarInvert(innerXRadius);
1088 SkScalar yInnerRadRecip = SkScalarInvert(innerYRadius);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001089
1090 // Extend the radii out half a pixel to antialias.
1091 SkScalar xOuterRadius = xRadius + SK_ScalarHalf;
1092 SkScalar yOuterRadius = yRadius + SK_ScalarHalf;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001093
1094 // Expand the rect so all the pixels will be captured.
1095 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1096
1097 SkScalar yCoords[4] = {
1098 bounds.fTop,
1099 bounds.fTop + yOuterRadius,
1100 bounds.fBottom - yOuterRadius,
1101 bounds.fBottom
1102 };
1103 SkScalar yOuterOffsets[4] = {
jvanverth@google.comd1b5b142013-07-02 14:46:03 +00001104 yOuterRadius,
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001105 SK_ScalarNearlyZero, // we're using inversesqrt() in the shader, so can't be exactly 0
1106 SK_ScalarNearlyZero,
1107 yOuterRadius
1108 };
1109
1110 for (int i = 0; i < 4; ++i) {
1111 verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]);
jvanverth@google.comd1b5b142013-07-02 14:46:03 +00001112 verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001113 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1114 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001115 verts++;
1116
1117 verts->fPos = SkPoint::Make(bounds.fLeft + xOuterRadius, yCoords[i]);
1118 verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001119 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1120 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001121 verts++;
1122
1123 verts->fPos = SkPoint::Make(bounds.fRight - xOuterRadius, yCoords[i]);
1124 verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001125 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1126 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001127 verts++;
1128
1129 verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]);
1130 verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001131 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1132 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001133 verts++;
1134 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001135
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001136 // drop out the middle quad if we're stroked
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001137 int indexCnt = isStroked ? SK_ARRAY_COUNT(gRRectIndices)-6 : SK_ARRAY_COUNT(gRRectIndices);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001138 target->setIndexSourceToBuffer(indexBuffer);
1139 target->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, 16, indexCnt, &bounds);
1140 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001141
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001142 return true;
1143}