blob: aeeb85d2e154a13d4c9177457ec782d1d51204ed [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.org824c3462013-10-10 06:30:18 +0000119 builder->fsCodeAppendf("\t%s = %s;\n", outputColor,
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000120 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("edgeAlpha")).c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000121 }
122
123 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
124 const CircleEdgeEffect& circleEffect = drawEffect.castEffect<CircleEdgeEffect>();
125
126 return circleEffect.isStroked() ? 0x1 : 0x0;
127 }
128
129 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {}
130
131 private:
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000132 typedef GrGLVertexEffect INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000133 };
134
135
136private:
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000137 CircleEdgeEffect(bool stroke) : GrVertexEffect() {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000138 this->addVertexAttrib(kVec4f_GrSLType);
139 fStroke = stroke;
140 }
141
142 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
143 const CircleEdgeEffect& cee = CastEffect<CircleEdgeEffect>(other);
144 return cee.fStroke == fStroke;
145 }
146
147 bool fStroke;
148
149 GR_DECLARE_EFFECT_TEST;
150
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000151 typedef GrVertexEffect INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000152};
153
154GR_DEFINE_EFFECT_TEST(CircleEdgeEffect);
155
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000156GrEffectRef* CircleEdgeEffect::TestCreate(SkRandom* random,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000157 GrContext* context,
158 const GrDrawTargetCaps&,
159 GrTexture* textures[]) {
160 return CircleEdgeEffect::Create(random->nextBool());
161}
162
163///////////////////////////////////////////////////////////////////////////////
164
165/**
166 * The output of this effect is a modulation of the input color and coverage for an axis-aligned
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +0000167 * ellipse, specified as a 2D offset from center, and the reciprocals of the outer and inner radii,
168 * in both x and y directions.
169 *
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000170 * We are using an implicit function of x^2/a^2 + y^2/b^2 - 1 = 0.
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000171 */
172
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000173class EllipseEdgeEffect : public GrVertexEffect {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000174public:
175 static GrEffectRef* Create(bool stroke) {
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000176 GR_CREATE_STATIC_EFFECT(gEllipseStrokeEdge, EllipseEdgeEffect, (true));
177 GR_CREATE_STATIC_EFFECT(gEllipseFillEdge, EllipseEdgeEffect, (false));
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000178
179 if (stroke) {
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000180 gEllipseStrokeEdge->ref();
181 return gEllipseStrokeEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000182 } else {
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000183 gEllipseFillEdge->ref();
184 return gEllipseFillEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000185 }
186 }
187
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000188 virtual void getConstantColorComponents(GrColor* color,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000189 uint32_t* validFlags) const SK_OVERRIDE {
190 *validFlags = 0;
191 }
192
193 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
194 return GrTBackendEffectFactory<EllipseEdgeEffect>::getInstance();
195 }
196
197 virtual ~EllipseEdgeEffect() {}
198
199 static const char* Name() { return "EllipseEdge"; }
200
201 inline bool isStroked() const { return fStroke; }
202
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000203 class GLEffect : public GrGLVertexEffect {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000204 public:
205 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
206 : INHERITED (factory) {}
207
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000208 virtual void emitCode(GrGLFullShaderBuilder* builder,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000209 const GrDrawEffect& drawEffect,
210 EffectKey key,
211 const char* outputColor,
212 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000213 const TransformedCoordsArray&,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000214 const TextureSamplerArray& samplers) SK_OVERRIDE {
215 const EllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<EllipseEdgeEffect>();
216
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000217 const char *vsOffsetName, *fsOffsetName;
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000218 const char *vsRadiiName, *fsRadiiName;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000219
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000220 builder->addVarying(kVec2f_GrSLType, "EllipseOffsets", &vsOffsetName, &fsOffsetName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000221 const SkString* attr0Name =
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000222 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]);
223 builder->vsCodeAppendf("\t%s = %s;\n", vsOffsetName, attr0Name->c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000224
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000225 builder->addVarying(kVec4f_GrSLType, "EllipseRadii", &vsRadiiName, &fsRadiiName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000226 const SkString* attr1Name =
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000227 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[1]);
228 builder->vsCodeAppendf("\t%s = %s;\n", vsRadiiName, attr1Name->c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000229
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +0000230 // for outer curve
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000231 builder->fsCodeAppendf("\tvec2 scaledOffset = %s*%s.xy;\n", fsOffsetName, fsRadiiName);
232 builder->fsCodeAppend("\tfloat test = dot(scaledOffset, scaledOffset) - 1.0;\n");
233 builder->fsCodeAppendf("\tvec2 grad = 2.0*scaledOffset*%s.xy;\n", fsRadiiName);
jvanverth@google.comd1b5b142013-07-02 14:46:03 +0000234 builder->fsCodeAppend("\tfloat grad_dot = dot(grad, grad);\n");
235 // we need to clamp the length^2 of the gradiant vector to a non-zero value, because
236 // on the Nexus 4 the undefined result of inversesqrt(0) drops out an entire tile
237 // TODO: restrict this to Adreno-only
238 builder->fsCodeAppend("\tgrad_dot = max(grad_dot, 1.0e-4);\n");
239 builder->fsCodeAppend("\tfloat invlen = inversesqrt(grad_dot);\n");
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000240 builder->fsCodeAppend("\tfloat edgeAlpha = clamp(0.5-test*invlen, 0.0, 1.0);\n");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000241
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000242 // for inner curve
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000243 if (ellipseEffect.isStroked()) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000244 builder->fsCodeAppendf("\tscaledOffset = %s*%s.zw;\n", fsOffsetName, fsRadiiName);
245 builder->fsCodeAppend("\ttest = dot(scaledOffset, scaledOffset) - 1.0;\n");
246 builder->fsCodeAppendf("\tgrad = 2.0*scaledOffset*%s.zw;\n", fsRadiiName);
247 builder->fsCodeAppend("\tinvlen = inversesqrt(dot(grad, grad));\n");
248 builder->fsCodeAppend("\tedgeAlpha *= clamp(0.5+test*invlen, 0.0, 1.0);\n");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000249 }
250
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000251 builder->fsCodeAppendf("\t%s = %s;\n", outputColor,
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000252 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("edgeAlpha")).c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000253 }
254
255 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
256 const EllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<EllipseEdgeEffect>();
257
258 return ellipseEffect.isStroked() ? 0x1 : 0x0;
259 }
260
261 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {
262 }
263
264 private:
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000265 typedef GrGLVertexEffect INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000266 };
267
268private:
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000269 EllipseEdgeEffect(bool stroke) : GrVertexEffect() {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000270 this->addVertexAttrib(kVec2f_GrSLType);
271 this->addVertexAttrib(kVec4f_GrSLType);
272 fStroke = stroke;
273 }
274
275 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
276 const EllipseEdgeEffect& eee = CastEffect<EllipseEdgeEffect>(other);
277 return eee.fStroke == fStroke;
278 }
279
280 bool fStroke;
281
282 GR_DECLARE_EFFECT_TEST;
283
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000284 typedef GrVertexEffect INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000285};
286
287GR_DEFINE_EFFECT_TEST(EllipseEdgeEffect);
288
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000289GrEffectRef* EllipseEdgeEffect::TestCreate(SkRandom* random,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000290 GrContext* context,
291 const GrDrawTargetCaps&,
292 GrTexture* textures[]) {
293 return EllipseEdgeEffect::Create(random->nextBool());
294}
295
296///////////////////////////////////////////////////////////////////////////////
297
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000298/**
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000299 * 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 +0000300 * specified as a 2D offset from center for both the outer and inner paths (if stroked). The
301 * implict equation used is for a unit circle (x^2 + y^2 - 1 = 0) and the edge corrected by
302 * using differentials.
303 *
304 * The result is device-independent and can be used with any affine matrix.
305 */
306
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000307class DIEllipseEdgeEffect : public GrVertexEffect {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000308public:
309 enum Mode { kStroke = 0, kHairline, kFill };
310
311 static GrEffectRef* Create(Mode mode) {
312 GR_CREATE_STATIC_EFFECT(gEllipseStrokeEdge, DIEllipseEdgeEffect, (kStroke));
313 GR_CREATE_STATIC_EFFECT(gEllipseHairlineEdge, DIEllipseEdgeEffect, (kHairline));
314 GR_CREATE_STATIC_EFFECT(gEllipseFillEdge, DIEllipseEdgeEffect, (kFill));
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000315
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000316 if (kStroke == mode) {
317 gEllipseStrokeEdge->ref();
318 return gEllipseStrokeEdge;
319 } else if (kHairline == mode) {
320 gEllipseHairlineEdge->ref();
321 return gEllipseHairlineEdge;
322 } else {
323 gEllipseFillEdge->ref();
324 return gEllipseFillEdge;
325 }
326 }
327
328 virtual void getConstantColorComponents(GrColor* color,
329 uint32_t* validFlags) const SK_OVERRIDE {
330 *validFlags = 0;
331 }
332
333 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
334 return GrTBackendEffectFactory<DIEllipseEdgeEffect>::getInstance();
335 }
336
337 virtual ~DIEllipseEdgeEffect() {}
338
339 static const char* Name() { return "DIEllipseEdge"; }
340
341 inline Mode getMode() const { return fMode; }
342
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000343 class GLEffect : public GrGLVertexEffect {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000344 public:
345 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
346 : INHERITED (factory) {}
347
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000348 virtual void emitCode(GrGLFullShaderBuilder* builder,
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000349 const GrDrawEffect& drawEffect,
350 EffectKey key,
351 const char* outputColor,
352 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000353 const TransformedCoordsArray&,
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000354 const TextureSamplerArray& samplers) SK_OVERRIDE {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000355 const DIEllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<DIEllipseEdgeEffect>();
356
357 SkAssertResult(builder->enableFeature(
358 GrGLShaderBuilder::kStandardDerivatives_GLSLFeature));
359
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000360 const char *vsOffsetName0, *fsOffsetName0;
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000361 builder->addVarying(kVec2f_GrSLType, "EllipseOffsets0",
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000362 &vsOffsetName0, &fsOffsetName0);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000363 const SkString* attr0Name =
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000364 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]);
365 builder->vsCodeAppendf("\t%s = %s;\n", vsOffsetName0, attr0Name->c_str());
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000366 const char *vsOffsetName1, *fsOffsetName1;
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000367 builder->addVarying(kVec2f_GrSLType, "EllipseOffsets1",
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000368 &vsOffsetName1, &fsOffsetName1);
369 const SkString* attr1Name =
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000370 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[1]);
371 builder->vsCodeAppendf("\t%s = %s;\n", vsOffsetName1, attr1Name->c_str());
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000372
373 // for outer curve
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000374 builder->fsCodeAppendf("\tvec2 scaledOffset = %s.xy;\n", fsOffsetName0);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000375 builder->fsCodeAppend("\tfloat test = dot(scaledOffset, scaledOffset) - 1.0;\n");
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000376 builder->fsCodeAppendf("\tvec2 duvdx = dFdx(%s);\n", fsOffsetName0);
377 builder->fsCodeAppendf("\tvec2 duvdy = dFdy(%s);\n", fsOffsetName0);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000378 builder->fsCodeAppendf("\tvec2 grad = vec2(2.0*%s.x*duvdx.x + 2.0*%s.y*duvdx.y,\n"
379 "\t 2.0*%s.x*duvdy.x + 2.0*%s.y*duvdy.y);\n",
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000380 fsOffsetName0, fsOffsetName0, fsOffsetName0, fsOffsetName0);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000381
382 builder->fsCodeAppend("\tfloat grad_dot = dot(grad, grad);\n");
383 // we need to clamp the length^2 of the gradiant vector to a non-zero value, because
384 // on the Nexus 4 the undefined result of inversesqrt(0) drops out an entire tile
385 // TODO: restrict this to Adreno-only
386 builder->fsCodeAppend("\tgrad_dot = max(grad_dot, 1.0e-4);\n");
387 builder->fsCodeAppend("\tfloat invlen = inversesqrt(grad_dot);\n");
388 if (kHairline == ellipseEffect.getMode()) {
389 // can probably do this with one step
390 builder->fsCodeAppend("\tfloat edgeAlpha = clamp(1.0-test*invlen, 0.0, 1.0);\n");
391 builder->fsCodeAppend("\tedgeAlpha *= clamp(1.0+test*invlen, 0.0, 1.0);\n");
392 } else {
393 builder->fsCodeAppend("\tfloat edgeAlpha = clamp(0.5-test*invlen, 0.0, 1.0);\n");
394 }
395
396 // for inner curve
397 if (kStroke == ellipseEffect.getMode()) {
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000398 builder->fsCodeAppendf("\tscaledOffset = %s.xy;\n", fsOffsetName1);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000399 builder->fsCodeAppend("\ttest = dot(scaledOffset, scaledOffset) - 1.0;\n");
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000400 builder->fsCodeAppendf("\tduvdx = dFdx(%s);\n", fsOffsetName1);
401 builder->fsCodeAppendf("\tduvdy = dFdy(%s);\n", fsOffsetName1);
402 builder->fsCodeAppendf("\tgrad = vec2(2.0*%s.x*duvdx.x + 2.0*%s.y*duvdx.y,\n"
403 "\t 2.0*%s.x*duvdy.x + 2.0*%s.y*duvdy.y);\n",
404 fsOffsetName1, fsOffsetName1, fsOffsetName1, fsOffsetName1);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000405 builder->fsCodeAppend("\tinvlen = inversesqrt(dot(grad, grad));\n");
406 builder->fsCodeAppend("\tedgeAlpha *= clamp(0.5+test*invlen, 0.0, 1.0);\n");
407 }
408
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000409 builder->fsCodeAppendf("\t%s = %s;\n", outputColor,
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000410 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("edgeAlpha")).c_str());
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000411 }
412
413 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
414 const DIEllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<DIEllipseEdgeEffect>();
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000415
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000416 return ellipseEffect.getMode();
417 }
418
419 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {
420 }
421
422 private:
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000423 typedef GrGLVertexEffect INHERITED;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000424 };
425
426private:
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000427 DIEllipseEdgeEffect(Mode mode) : GrVertexEffect() {
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000428 this->addVertexAttrib(kVec2f_GrSLType);
429 this->addVertexAttrib(kVec2f_GrSLType);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000430 fMode = mode;
431 }
432
433 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
434 const DIEllipseEdgeEffect& eee = CastEffect<DIEllipseEdgeEffect>(other);
435 return eee.fMode == fMode;
436 }
437
438 Mode fMode;
439
440 GR_DECLARE_EFFECT_TEST;
441
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000442 typedef GrVertexEffect INHERITED;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000443};
444
445GR_DEFINE_EFFECT_TEST(DIEllipseEdgeEffect);
446
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000447GrEffectRef* DIEllipseEdgeEffect::TestCreate(SkRandom* random,
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000448 GrContext* context,
449 const GrDrawTargetCaps&,
450 GrTexture* textures[]) {
451 return DIEllipseEdgeEffect::Create((Mode)(random->nextRangeU(0,2)));
452}
453
454///////////////////////////////////////////////////////////////////////////////
455
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000456void GrOvalRenderer::reset() {
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000457 SkSafeSetNull(fRRectIndexBuffer);
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000458}
459
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000460bool GrOvalRenderer::drawOval(GrDrawTarget* target, const GrContext* context, bool useAA,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000461 const SkRect& oval, const SkStrokeRec& stroke)
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000462{
bsalomon@google.com1b20a102013-11-08 14:42:56 +0000463 if (!useAA) {
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000464 return false;
465 }
466
467 const SkMatrix& vm = context->getMatrix();
468
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000469 // we can draw circles
470 if (SkScalarNearlyEqual(oval.width(), oval.height())
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000471 && circle_stays_circle(vm)) {
bsalomon@google.com1b20a102013-11-08 14:42:56 +0000472 this->drawCircle(target, useAA, oval, stroke);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000473 // if we have shader derivative support, render as device-independent
474 } else if (target->caps()->shaderDerivativeSupport()) {
bsalomon@google.com1b20a102013-11-08 14:42:56 +0000475 return this->drawDIEllipse(target, useAA, oval, stroke);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000476 // otherwise axis-aligned ellipses only
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000477 } else if (vm.rectStaysRect()) {
bsalomon@google.com1b20a102013-11-08 14:42:56 +0000478 return this->drawEllipse(target, useAA, oval, stroke);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000479 } else {
480 return false;
481 }
482
483 return true;
484}
485
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000486///////////////////////////////////////////////////////////////////////////////
487
robertphillips@google.com42903302013-04-20 12:26:07 +0000488// position + edge
489extern const GrVertexAttrib gCircleVertexAttribs[] = {
490 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
491 {kVec4f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding}
492};
493
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000494void GrOvalRenderer::drawCircle(GrDrawTarget* target,
bsalomon@google.com1b20a102013-11-08 14:42:56 +0000495 bool useAA,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000496 const SkRect& circle,
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000497 const SkStrokeRec& stroke)
498{
499 GrDrawState* drawState = target->drawState();
500
501 const SkMatrix& vm = drawState->getViewMatrix();
502 GrPoint center = GrPoint::Make(circle.centerX(), circle.centerY());
503 vm.mapPoints(&center, 1);
504 SkScalar radius = vm.mapRadius(SkScalarHalf(circle.width()));
505 SkScalar strokeWidth = vm.mapRadius(stroke.getWidth());
506
bsalomon@google.com137f1342013-05-29 21:27:53 +0000507 GrDrawState::AutoViewMatrixRestore avmr;
508 if (!avmr.setIdentity(drawState)) {
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000509 return;
510 }
511
robertphillips@google.com42903302013-04-20 12:26:07 +0000512 drawState->setVertexAttribs<gCircleVertexAttribs>(SK_ARRAY_COUNT(gCircleVertexAttribs));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000513 SkASSERT(sizeof(CircleVertex) == drawState->getVertexSize());
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000514
515 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
516 if (!geo.succeeded()) {
517 GrPrintf("Failed to get space for vertices!\n");
518 return;
519 }
520
521 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
522
523 SkStrokeRec::Style style = stroke.getStyle();
524 bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style);
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000525
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000526 SkScalar innerRadius = 0.0f;
527 SkScalar outerRadius = radius;
528 SkScalar halfWidth = 0;
529 if (style != SkStrokeRec::kFill_Style) {
530 if (SkScalarNearlyZero(strokeWidth)) {
531 halfWidth = SK_ScalarHalf;
532 } else {
533 halfWidth = SkScalarHalf(strokeWidth);
534 }
535
536 outerRadius += halfWidth;
537 if (isStroked) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000538 innerRadius = radius - halfWidth;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000539 }
540 }
541
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +0000542 GrEffectRef* effect = CircleEdgeEffect::Create(isStroked && innerRadius > 0);
543 static const int kCircleEdgeAttrIndex = 1;
544 drawState->addCoverageEffect(effect, kCircleEdgeAttrIndex)->unref();
545
bsalomon@google.com58e30fe2013-04-01 19:01:20 +0000546 // The radii are outset for two reasons. First, it allows the shader to simply perform
547 // clamp(distance-to-center - radius, 0, 1). Second, the outer radius is used to compute the
548 // verts of the bounding box that is rendered and the outset ensures the box will cover all
549 // pixels partially covered by the circle.
550 outerRadius += SK_ScalarHalf;
551 innerRadius -= SK_ScalarHalf;
552
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000553 SkRect bounds = SkRect::MakeLTRB(
bsalomon@google.com58e30fe2013-04-01 19:01:20 +0000554 center.fX - outerRadius,
555 center.fY - outerRadius,
556 center.fX + outerRadius,
557 center.fY + outerRadius
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000558 );
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000559
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000560 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000561 verts[0].fOffset = SkPoint::Make(-outerRadius, -outerRadius);
562 verts[0].fOuterRadius = outerRadius;
563 verts[0].fInnerRadius = innerRadius;
564
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000565 verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
skia.committer@gmail.com46746762013-04-12 07:01:51 +0000566 verts[1].fOffset = SkPoint::Make(outerRadius, -outerRadius);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000567 verts[1].fOuterRadius = outerRadius;
568 verts[1].fInnerRadius = innerRadius;
569
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000570 verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000571 verts[2].fOffset = SkPoint::Make(-outerRadius, outerRadius);
572 verts[2].fOuterRadius = outerRadius;
573 verts[2].fInnerRadius = innerRadius;
574
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000575 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000576 verts[3].fOffset = SkPoint::Make(outerRadius, outerRadius);
577 verts[3].fOuterRadius = outerRadius;
578 verts[3].fInnerRadius = innerRadius;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000579
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000580 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000581}
582
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000583///////////////////////////////////////////////////////////////////////////////
584
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000585// position + offset + 1/radii
robertphillips@google.com42903302013-04-20 12:26:07 +0000586extern const GrVertexAttrib gEllipseVertexAttribs[] = {
587 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
588 {kVec2f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding},
589 {kVec4f_GrVertexAttribType, 2*sizeof(GrPoint), kEffect_GrVertexAttribBinding}
590};
591
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000592// position + offsets
593extern const GrVertexAttrib gDIEllipseVertexAttribs[] = {
594 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000595 {kVec2f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding},
596 {kVec2f_GrVertexAttribType, 2*sizeof(GrPoint), kEffect_GrVertexAttribBinding},
robertphillips@google.com42903302013-04-20 12:26:07 +0000597};
598
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000599bool GrOvalRenderer::drawEllipse(GrDrawTarget* target,
bsalomon@google.com1b20a102013-11-08 14:42:56 +0000600 bool useAA,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000601 const SkRect& ellipse,
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000602 const SkStrokeRec& stroke)
603{
604 GrDrawState* drawState = target->drawState();
605#ifdef SK_DEBUG
606 {
607 // we should have checked for this previously
608 bool isAxisAlignedEllipse = drawState->getViewMatrix().rectStaysRect();
bsalomon@google.com1b20a102013-11-08 14:42:56 +0000609 SkASSERT(useAA && isAxisAlignedEllipse);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000610 }
611#endif
612
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000613 // do any matrix crunching before we reset the draw state for device coords
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000614 const SkMatrix& vm = drawState->getViewMatrix();
615 GrPoint center = GrPoint::Make(ellipse.centerX(), ellipse.centerY());
616 vm.mapPoints(&center, 1);
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000617 SkScalar ellipseXRadius = SkScalarHalf(ellipse.width());
618 SkScalar ellipseYRadius = SkScalarHalf(ellipse.height());
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +0000619 SkScalar xRadius = SkScalarAbs(vm[SkMatrix::kMScaleX]*ellipseXRadius +
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000620 vm[SkMatrix::kMSkewY]*ellipseYRadius);
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +0000621 SkScalar yRadius = SkScalarAbs(vm[SkMatrix::kMSkewX]*ellipseXRadius +
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000622 vm[SkMatrix::kMScaleY]*ellipseYRadius);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000623
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000624 // do (potentially) anisotropic mapping of stroke
625 SkVector scaledStroke;
626 SkScalar strokeWidth = stroke.getWidth();
627 scaledStroke.fX = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMScaleX] + vm[SkMatrix::kMSkewY]));
628 scaledStroke.fY = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMSkewX] + vm[SkMatrix::kMScaleY]));
629
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000630 SkStrokeRec::Style style = stroke.getStyle();
631 bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style);
632
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000633 SkScalar innerXRadius = 0;
634 SkScalar innerYRadius = 0;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000635 if (SkStrokeRec::kFill_Style != style) {
636 if (SkScalarNearlyZero(scaledStroke.length())) {
637 scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf);
638 } else {
639 scaledStroke.scale(SK_ScalarHalf);
640 }
641
642 // we only handle thick strokes for near-circular ellipses
643 if (scaledStroke.length() > SK_ScalarHalf &&
644 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
645 return false;
646 }
647
648 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
649 if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius ||
650 scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) {
651 return false;
652 }
653
654 // this is legit only if scale & translation (which should be the case at the moment)
655 if (isStroked) {
656 innerXRadius = xRadius - scaledStroke.fX;
657 innerYRadius = yRadius - scaledStroke.fY;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000658 }
659
660 xRadius += scaledStroke.fX;
661 yRadius += scaledStroke.fY;
662 }
663
bsalomon@google.com137f1342013-05-29 21:27:53 +0000664 GrDrawState::AutoViewMatrixRestore avmr;
665 if (!avmr.setIdentity(drawState)) {
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000666 return false;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000667 }
668
robertphillips@google.com42903302013-04-20 12:26:07 +0000669 drawState->setVertexAttribs<gEllipseVertexAttribs>(SK_ARRAY_COUNT(gEllipseVertexAttribs));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000670 SkASSERT(sizeof(EllipseVertex) == drawState->getVertexSize());
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000671
672 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
673 if (!geo.succeeded()) {
674 GrPrintf("Failed to get space for vertices!\n");
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000675 return false;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000676 }
677
678 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices());
679
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +0000680 GrEffectRef* effect = EllipseEdgeEffect::Create(isStroked &&
681 innerXRadius > 0 && innerYRadius > 0);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000682
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000683 static const int kEllipseCenterAttrIndex = 1;
jvanverth@google.com6cc8d442013-09-10 18:24:37 +0000684 static const int kEllipseEdgeAttrIndex = 2;
685 drawState->addCoverageEffect(effect, kEllipseCenterAttrIndex, kEllipseEdgeAttrIndex)->unref();
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000686
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000687 // Compute the reciprocals of the radii here to save time in the shader
688 SkScalar xRadRecip = SkScalarInvert(xRadius);
689 SkScalar yRadRecip = SkScalarInvert(yRadius);
690 SkScalar xInnerRadRecip = SkScalarInvert(innerXRadius);
691 SkScalar yInnerRadRecip = SkScalarInvert(innerYRadius);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000692
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000693 // We've extended the outer x radius out half a pixel to antialias.
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000694 // This will also expand the rect so all the pixels will be captured.
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000695 // TODO: Consider if we should use sqrt(2)/2 instead
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000696 xRadius += SK_ScalarHalf;
697 yRadius += SK_ScalarHalf;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000698
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000699 SkRect bounds = SkRect::MakeLTRB(
700 center.fX - xRadius,
701 center.fY - yRadius,
702 center.fX + xRadius,
703 center.fY + yRadius
704 );
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000705
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000706 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000707 verts[0].fOffset = SkPoint::Make(-xRadius, -yRadius);
708 verts[0].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
709 verts[0].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000710
711 verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000712 verts[1].fOffset = SkPoint::Make(xRadius, -yRadius);
713 verts[1].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
714 verts[1].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000715
716 verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000717 verts[2].fOffset = SkPoint::Make(-xRadius, yRadius);
718 verts[2].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
719 verts[2].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000720
721 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000722 verts[3].fOffset = SkPoint::Make(xRadius, yRadius);
723 verts[3].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
724 verts[3].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
skia.committer@gmail.com46746762013-04-12 07:01:51 +0000725
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000726 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds);
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000727
728 return true;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000729}
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000730
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000731bool GrOvalRenderer::drawDIEllipse(GrDrawTarget* target,
bsalomon@google.com1b20a102013-11-08 14:42:56 +0000732 bool useAA,
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000733 const SkRect& ellipse,
734 const SkStrokeRec& stroke)
735{
736 GrDrawState* drawState = target->drawState();
737 const SkMatrix& vm = drawState->getViewMatrix();
738
739 GrPoint center = GrPoint::Make(ellipse.centerX(), ellipse.centerY());
740 SkScalar xRadius = SkScalarHalf(ellipse.width());
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000741 SkScalar yRadius = SkScalarHalf(ellipse.height());
742
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000743 SkStrokeRec::Style style = stroke.getStyle();
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000744 DIEllipseEdgeEffect::Mode mode = (SkStrokeRec::kStroke_Style == style) ?
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000745 DIEllipseEdgeEffect::kStroke :
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000746 (SkStrokeRec::kHairline_Style == style) ?
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000747 DIEllipseEdgeEffect::kHairline : DIEllipseEdgeEffect::kFill;
748
749 SkScalar innerXRadius = 0;
750 SkScalar innerYRadius = 0;
751 if (SkStrokeRec::kFill_Style != style && SkStrokeRec::kHairline_Style != style) {
752 SkScalar strokeWidth = stroke.getWidth();
753
754 if (SkScalarNearlyZero(strokeWidth)) {
755 strokeWidth = SK_ScalarHalf;
756 } else {
757 strokeWidth *= SK_ScalarHalf;
758 }
759
760 // we only handle thick strokes for near-circular ellipses
761 if (strokeWidth > SK_ScalarHalf &&
762 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
763 return false;
764 }
765
766 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
767 if (strokeWidth*(yRadius*yRadius) < (strokeWidth*strokeWidth)*xRadius ||
768 strokeWidth*(xRadius*xRadius) < (strokeWidth*strokeWidth)*yRadius) {
769 return false;
770 }
771
772 // set inner radius (if needed)
773 if (SkStrokeRec::kStroke_Style == style) {
774 innerXRadius = xRadius - strokeWidth;
775 innerYRadius = yRadius - strokeWidth;
776 }
777
778 xRadius += strokeWidth;
779 yRadius += strokeWidth;
780 }
781 if (DIEllipseEdgeEffect::kStroke == mode) {
782 mode = (innerXRadius > 0 && innerYRadius > 0) ? DIEllipseEdgeEffect::kStroke :
783 DIEllipseEdgeEffect::kFill;
784 }
785 SkScalar innerRatioX = SkScalarDiv(xRadius, innerXRadius);
786 SkScalar innerRatioY = SkScalarDiv(yRadius, innerYRadius);
787
788 drawState->setVertexAttribs<gDIEllipseVertexAttribs>(SK_ARRAY_COUNT(gDIEllipseVertexAttribs));
789 SkASSERT(sizeof(DIEllipseVertex) == drawState->getVertexSize());
790
791 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
792 if (!geo.succeeded()) {
793 GrPrintf("Failed to get space for vertices!\n");
794 return false;
795 }
796
797 DIEllipseVertex* verts = reinterpret_cast<DIEllipseVertex*>(geo.vertices());
798
799 GrEffectRef* effect = DIEllipseEdgeEffect::Create(mode);
800
801 static const int kEllipseOuterOffsetAttrIndex = 1;
802 static const int kEllipseInnerOffsetAttrIndex = 2;
803 drawState->addCoverageEffect(effect, kEllipseOuterOffsetAttrIndex,
804 kEllipseInnerOffsetAttrIndex)->unref();
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000805
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000806 // This expands the outer rect so that after CTM we end up with a half-pixel border
807 SkScalar a = vm[SkMatrix::kMScaleX];
808 SkScalar b = vm[SkMatrix::kMSkewX];
809 SkScalar c = vm[SkMatrix::kMSkewY];
810 SkScalar d = vm[SkMatrix::kMScaleY];
811 SkScalar geoDx = SkScalarDiv(SK_ScalarHalf, SkScalarSqrt(a*a + c*c));
812 SkScalar geoDy = SkScalarDiv(SK_ScalarHalf, SkScalarSqrt(b*b + d*d));
813 // This adjusts the "radius" to include the half-pixel border
814 SkScalar offsetDx = SkScalarDiv(geoDx, xRadius);
815 SkScalar offsetDy = SkScalarDiv(geoDy, yRadius);
816
817 SkRect bounds = SkRect::MakeLTRB(
818 center.fX - xRadius - geoDx,
819 center.fY - yRadius - geoDy,
820 center.fX + xRadius + geoDx,
821 center.fY + yRadius + geoDy
822 );
823
824 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
825 verts[0].fOuterOffset = SkPoint::Make(-1.0f - offsetDx, -1.0f - offsetDy);
826 verts[0].fInnerOffset = SkPoint::Make(-innerRatioX - offsetDx, -innerRatioY - offsetDy);
827
828 verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
829 verts[1].fOuterOffset = SkPoint::Make(1.0f + offsetDx, -1.0f - offsetDy);
830 verts[1].fInnerOffset = SkPoint::Make(innerRatioX + offsetDx, -innerRatioY - offsetDy);
831
832 verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
833 verts[2].fOuterOffset = SkPoint::Make(-1.0f - offsetDx, 1.0f + offsetDy);
834 verts[2].fInnerOffset = SkPoint::Make(-innerRatioX - offsetDx, innerRatioY + offsetDy);
835
836 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
837 verts[3].fOuterOffset = SkPoint::Make(1.0f + offsetDx, 1.0f + offsetDy);
838 verts[3].fInnerOffset = SkPoint::Make(innerRatioX + offsetDx, innerRatioY + offsetDy);
839
840 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds);
841
842 return true;
843}
844
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000845///////////////////////////////////////////////////////////////////////////////
846
847static const uint16_t gRRectIndices[] = {
848 // corners
849 0, 1, 5, 0, 5, 4,
850 2, 3, 7, 2, 7, 6,
851 8, 9, 13, 8, 13, 12,
852 10, 11, 15, 10, 15, 14,
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000853
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000854 // edges
855 1, 2, 6, 1, 6, 5,
856 4, 5, 9, 4, 9, 8,
857 6, 7, 11, 6, 11, 10,
858 9, 10, 14, 9, 14, 13,
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000859
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000860 // center
861 // we place this at the end so that we can ignore these indices when rendering stroke-only
862 5, 6, 10, 5, 10, 9
863};
864
865
866GrIndexBuffer* GrOvalRenderer::rRectIndexBuffer(GrGpu* gpu) {
867 if (NULL == fRRectIndexBuffer) {
868 fRRectIndexBuffer =
869 gpu->createIndexBuffer(sizeof(gRRectIndices), false);
870 if (NULL != fRRectIndexBuffer) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000871#ifdef SK_DEBUG
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000872 bool updated =
873#endif
874 fRRectIndexBuffer->updateData(gRRectIndices,
875 sizeof(gRRectIndices));
876 GR_DEBUGASSERT(updated);
877 }
878 }
879 return fRRectIndexBuffer;
880}
881
skia.committer@gmail.com2fd42c42013-05-03 07:01:00 +0000882bool GrOvalRenderer::drawSimpleRRect(GrDrawTarget* target, GrContext* context, bool useAA,
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000883 const SkRRect& rrect, const SkStrokeRec& stroke)
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000884{
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000885 // only anti-aliased rrects for now
bsalomon@google.com1b20a102013-11-08 14:42:56 +0000886 if (!useAA) {
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000887 return false;
888 }
889
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000890 const SkMatrix& vm = context->getMatrix();
891#ifdef SK_DEBUG
892 {
893 // we should have checked for this previously
bsalomon@google.com1b20a102013-11-08 14:42:56 +0000894 SkASSERT(useAA && vm.rectStaysRect() && rrect.isSimple());
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000895 }
896#endif
897
898 // do any matrix crunching before we reset the draw state for device coords
899 const SkRect& rrectBounds = rrect.getBounds();
900 SkRect bounds;
901 vm.mapRect(&bounds, rrectBounds);
902
903 SkVector radii = rrect.getSimpleRadii();
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000904 SkScalar xRadius = SkScalarAbs(vm[SkMatrix::kMScaleX]*radii.fX +
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000905 vm[SkMatrix::kMSkewY]*radii.fY);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000906 SkScalar yRadius = SkScalarAbs(vm[SkMatrix::kMSkewX]*radii.fX +
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000907 vm[SkMatrix::kMScaleY]*radii.fY);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000908
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000909 // if hairline stroke is greater than radius, we don't handle that right now
910 SkStrokeRec::Style style = stroke.getStyle();
911 if (SkStrokeRec::kHairline_Style == style &&
commit-bot@chromium.orgeacc4892013-11-07 16:34:53 +0000912 (SK_ScalarHalf > xRadius || SK_ScalarHalf > yRadius)) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000913 return false;
914 }
915
916 // do (potentially) anisotropic mapping of stroke
917 SkVector scaledStroke;
918 SkScalar strokeWidth = stroke.getWidth();
919 scaledStroke.fX = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMScaleX] + vm[SkMatrix::kMSkewY]));
920 scaledStroke.fY = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMSkewX] + vm[SkMatrix::kMScaleY]));
921
922 // if half of strokewidth is greater than radius, we don't handle that right now
commit-bot@chromium.orgeacc4892013-11-07 16:34:53 +0000923 if (SK_ScalarHalf*scaledStroke.fX > xRadius || SK_ScalarHalf*scaledStroke.fY > yRadius) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000924 return false;
925 }
926
927 // reset to device coordinates
928 GrDrawState* drawState = target->drawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000929 GrDrawState::AutoViewMatrixRestore avmr;
930 if (!avmr.setIdentity(drawState)) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000931 return false;
932 }
933
934 bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style);
935
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000936 GrIndexBuffer* indexBuffer = this->rRectIndexBuffer(context->getGpu());
937 if (NULL == indexBuffer) {
938 GrPrintf("Failed to create index buffer!\n");
939 return false;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000940 }
941
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000942 // if the corners are circles, use the circle renderer
943 if ((!isStroked || scaledStroke.fX == scaledStroke.fY) && xRadius == yRadius) {
944 drawState->setVertexAttribs<gCircleVertexAttribs>(SK_ARRAY_COUNT(gCircleVertexAttribs));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000945 SkASSERT(sizeof(CircleVertex) == drawState->getVertexSize());
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000946
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000947 GrDrawTarget::AutoReleaseGeometry geo(target, 16, 0);
948 if (!geo.succeeded()) {
949 GrPrintf("Failed to get space for vertices!\n");
950 return false;
951 }
952 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000953
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000954 SkScalar innerRadius = 0.0f;
955 SkScalar outerRadius = xRadius;
956 SkScalar halfWidth = 0;
957 if (style != SkStrokeRec::kFill_Style) {
958 if (SkScalarNearlyZero(scaledStroke.fX)) {
959 halfWidth = SK_ScalarHalf;
960 } else {
961 halfWidth = SkScalarHalf(scaledStroke.fX);
962 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000963
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000964 if (isStroked) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000965 innerRadius = xRadius - halfWidth;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000966 }
967 outerRadius += halfWidth;
968 bounds.outset(halfWidth, halfWidth);
969 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000970
commit-bot@chromium.orgeacc4892013-11-07 16:34:53 +0000971 isStroked = (isStroked && innerRadius >= 0);
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +0000972
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000973 GrEffectRef* effect = CircleEdgeEffect::Create(isStroked);
974 static const int kCircleEdgeAttrIndex = 1;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000975 drawState->addCoverageEffect(effect, kCircleEdgeAttrIndex)->unref();
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +0000976
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000977 // The radii are outset for two reasons. First, it allows the shader to simply perform
978 // clamp(distance-to-center - radius, 0, 1). Second, the outer radius is used to compute the
979 // verts of the bounding box that is rendered and the outset ensures the box will cover all
980 // pixels partially covered by the circle.
981 outerRadius += SK_ScalarHalf;
982 innerRadius -= SK_ScalarHalf;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000983
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000984 // Expand the rect so all the pixels will be captured.
985 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000986
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000987 SkScalar yCoords[4] = {
988 bounds.fTop,
989 bounds.fTop + outerRadius,
990 bounds.fBottom - outerRadius,
991 bounds.fBottom
992 };
993 SkScalar yOuterRadii[4] = {
994 -outerRadius,
995 0,
996 0,
997 outerRadius
998 };
999 for (int i = 0; i < 4; ++i) {
1000 verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]);
1001 verts->fOffset = SkPoint::Make(-outerRadius, yOuterRadii[i]);
1002 verts->fOuterRadius = outerRadius;
1003 verts->fInnerRadius = innerRadius;
1004 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001005
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001006 verts->fPos = SkPoint::Make(bounds.fLeft + outerRadius, yCoords[i]);
1007 verts->fOffset = SkPoint::Make(0, yOuterRadii[i]);
1008 verts->fOuterRadius = outerRadius;
1009 verts->fInnerRadius = innerRadius;
1010 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001011
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001012 verts->fPos = SkPoint::Make(bounds.fRight - outerRadius, yCoords[i]);
1013 verts->fOffset = SkPoint::Make(0, yOuterRadii[i]);
1014 verts->fOuterRadius = outerRadius;
1015 verts->fInnerRadius = innerRadius;
1016 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001017
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001018 verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]);
1019 verts->fOffset = SkPoint::Make(outerRadius, yOuterRadii[i]);
1020 verts->fOuterRadius = outerRadius;
1021 verts->fInnerRadius = innerRadius;
1022 verts++;
1023 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001024
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001025 // drop out the middle quad if we're stroked
1026 int indexCnt = isStroked ? GR_ARRAY_COUNT(gRRectIndices)-6 : GR_ARRAY_COUNT(gRRectIndices);
1027 target->setIndexSourceToBuffer(indexBuffer);
1028 target->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, 16, indexCnt, &bounds);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001029
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001030 // otherwise we use the ellipse renderer
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001031 } else {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001032 drawState->setVertexAttribs<gEllipseVertexAttribs>(SK_ARRAY_COUNT(gEllipseVertexAttribs));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001033 SkASSERT(sizeof(EllipseVertex) == drawState->getVertexSize());
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001034
1035 SkScalar innerXRadius = 0.0f;
1036 SkScalar innerYRadius = 0.0f;
1037 if (SkStrokeRec::kFill_Style != style) {
1038 if (SkScalarNearlyZero(scaledStroke.length())) {
1039 scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf);
1040 } else {
1041 scaledStroke.scale(SK_ScalarHalf);
1042 }
1043
1044 // we only handle thick strokes for near-circular ellipses
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +00001045 if (scaledStroke.length() > SK_ScalarHalf &&
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001046 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
1047 return false;
1048 }
1049
1050 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
1051 if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius ||
1052 scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) {
1053 return false;
1054 }
1055
1056 // this is legit only if scale & translation (which should be the case at the moment)
1057 if (isStroked) {
1058 innerXRadius = xRadius - scaledStroke.fX;
1059 innerYRadius = yRadius - scaledStroke.fY;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001060 }
1061
1062 xRadius += scaledStroke.fX;
1063 yRadius += scaledStroke.fY;
1064 bounds.outset(scaledStroke.fX, scaledStroke.fY);
1065 }
jvanverth@google.come3647412013-05-08 15:31:43 +00001066
commit-bot@chromium.orgeacc4892013-11-07 16:34:53 +00001067 isStroked = (isStroked && innerXRadius >= 0 && innerYRadius >= 0);
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +00001068
jvanverth@google.come3647412013-05-08 15:31:43 +00001069 GrDrawTarget::AutoReleaseGeometry geo(target, 16, 0);
1070 if (!geo.succeeded()) {
1071 GrPrintf("Failed to get space for vertices!\n");
1072 return false;
1073 }
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001074 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices());
jvanverth@google.come3647412013-05-08 15:31:43 +00001075
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001076 GrEffectRef* effect = EllipseEdgeEffect::Create(isStroked);
jvanverth@google.come3647412013-05-08 15:31:43 +00001077 static const int kEllipseOffsetAttrIndex = 1;
1078 static const int kEllipseRadiiAttrIndex = 2;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001079 drawState->addCoverageEffect(effect,
1080 kEllipseOffsetAttrIndex, kEllipseRadiiAttrIndex)->unref();
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001081
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001082 // Compute the reciprocals of the radii here to save time in the shader
1083 SkScalar xRadRecip = SkScalarInvert(xRadius);
1084 SkScalar yRadRecip = SkScalarInvert(yRadius);
1085 SkScalar xInnerRadRecip = SkScalarInvert(innerXRadius);
1086 SkScalar yInnerRadRecip = SkScalarInvert(innerYRadius);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001087
1088 // Extend the radii out half a pixel to antialias.
1089 SkScalar xOuterRadius = xRadius + SK_ScalarHalf;
1090 SkScalar yOuterRadius = yRadius + SK_ScalarHalf;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001091
1092 // Expand the rect so all the pixels will be captured.
1093 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1094
1095 SkScalar yCoords[4] = {
1096 bounds.fTop,
1097 bounds.fTop + yOuterRadius,
1098 bounds.fBottom - yOuterRadius,
1099 bounds.fBottom
1100 };
1101 SkScalar yOuterOffsets[4] = {
jvanverth@google.comd1b5b142013-07-02 14:46:03 +00001102 yOuterRadius,
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001103 SK_ScalarNearlyZero, // we're using inversesqrt() in the shader, so can't be exactly 0
1104 SK_ScalarNearlyZero,
1105 yOuterRadius
1106 };
1107
1108 for (int i = 0; i < 4; ++i) {
1109 verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]);
jvanverth@google.comd1b5b142013-07-02 14:46:03 +00001110 verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001111 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1112 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001113 verts++;
1114
1115 verts->fPos = SkPoint::Make(bounds.fLeft + xOuterRadius, yCoords[i]);
1116 verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001117 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1118 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001119 verts++;
1120
1121 verts->fPos = SkPoint::Make(bounds.fRight - xOuterRadius, yCoords[i]);
1122 verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001123 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1124 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001125 verts++;
1126
1127 verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]);
1128 verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001129 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1130 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001131 verts++;
1132 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001133
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001134 // drop out the middle quad if we're stroked
1135 int indexCnt = isStroked ? GR_ARRAY_COUNT(gRRectIndices)-6 : GR_ARRAY_COUNT(gRRectIndices);
1136 target->setIndexSourceToBuffer(indexBuffer);
1137 target->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, 16, indexCnt, &bounds);
1138 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001139
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001140 return true;
1141}