blob: 2c923f8f81e92a835f0d38a447b1cda40947ec47 [file] [log] [blame]
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "GrOvalRenderer.h"
9
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000010#include "GrEffect.h"
11#include "gl/GrGLEffect.h"
12#include "gl/GrGLSL.h"
13#include "GrTBackendEffectFactory.h"
commit-bot@chromium.org81312832013-03-22 18:34:09 +000014
15#include "GrDrawState.h"
16#include "GrDrawTarget.h"
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +000017#include "GrGpu.h"
18
19#include "SkRRect.h"
commit-bot@chromium.org81312832013-03-22 18:34:09 +000020#include "SkStrokeRec.h"
21
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +000022#include "effects/GrVertexEffect.h"
23
commit-bot@chromium.org81312832013-03-22 18:34:09 +000024SK_DEFINE_INST_COUNT(GrOvalRenderer)
25
26namespace {
27
28struct CircleVertex {
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +000029 GrPoint fPos;
30 GrPoint fOffset;
commit-bot@chromium.org81312832013-03-22 18:34:09 +000031 SkScalar fOuterRadius;
32 SkScalar fInnerRadius;
33};
34
35struct EllipseVertex {
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +000036 GrPoint fPos;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +000037 GrPoint fOffset;
38 GrPoint fOuterRadii;
39 GrPoint fInnerRadii;
40};
41
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +000042struct DIEllipseVertex {
43 GrPoint fPos;
44 GrPoint fOuterOffset;
45 GrPoint fInnerOffset;
46};
47
commit-bot@chromium.org81312832013-03-22 18:34:09 +000048inline bool circle_stays_circle(const SkMatrix& m) {
49 return m.isSimilarity();
50}
51
52}
53
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000054///////////////////////////////////////////////////////////////////////////////
55
56/**
57 * 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 +000058 * specified as offset_x, offset_y (both from center point), outer radius and inner radius.
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000059 */
60
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +000061class CircleEdgeEffect : public GrVertexEffect {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000062public:
63 static GrEffectRef* Create(bool stroke) {
bsalomon@google.comd42aca32013-04-23 15:37:27 +000064 GR_CREATE_STATIC_EFFECT(gCircleStrokeEdge, CircleEdgeEffect, (true));
65 GR_CREATE_STATIC_EFFECT(gCircleFillEdge, CircleEdgeEffect, (false));
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000066
67 if (stroke) {
bsalomon@google.comd42aca32013-04-23 15:37:27 +000068 gCircleStrokeEdge->ref();
69 return gCircleStrokeEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000070 } else {
bsalomon@google.comd42aca32013-04-23 15:37:27 +000071 gCircleFillEdge->ref();
72 return gCircleFillEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000073 }
74 }
75
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +000076 virtual void getConstantColorComponents(GrColor* color,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000077 uint32_t* validFlags) const SK_OVERRIDE {
78 *validFlags = 0;
79 }
80
81 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
82 return GrTBackendEffectFactory<CircleEdgeEffect>::getInstance();
83 }
84
85 virtual ~CircleEdgeEffect() {}
86
87 static const char* Name() { return "CircleEdge"; }
88
89 inline bool isStroked() const { return fStroke; }
90
91 class GLEffect : public GrGLEffect {
92 public:
93 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
94 : INHERITED (factory) {}
95
96 virtual void emitCode(GrGLShaderBuilder* builder,
97 const GrDrawEffect& drawEffect,
98 EffectKey key,
99 const char* outputColor,
100 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000101 const TransformedCoordsArray&,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000102 const TextureSamplerArray& samplers) SK_OVERRIDE {
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +0000103 GrGLShaderBuilder::VertexBuilder* vertexBuilder = builder->getVertexBuilder();
104 SkASSERT(NULL != vertexBuilder);
105
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000106 const CircleEdgeEffect& circleEffect = drawEffect.castEffect<CircleEdgeEffect>();
107 const char *vsName, *fsName;
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +0000108 vertexBuilder->addVarying(kVec4f_GrSLType, "CircleEdge", &vsName, &fsName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000109
110 const SkString* attrName =
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +0000111 vertexBuilder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]);
112 vertexBuilder->vsCodeAppendf("\t%s = %s;\n", vsName, attrName->c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000113
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000114 builder->fsCodeAppendf("\tfloat d = length(%s.xy);\n", fsName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000115 builder->fsCodeAppendf("\tfloat edgeAlpha = clamp(%s.z - d, 0.0, 1.0);\n", fsName);
116 if (circleEffect.isStroked()) {
117 builder->fsCodeAppendf("\tfloat innerAlpha = clamp(d - %s.w, 0.0, 1.0);\n", fsName);
118 builder->fsCodeAppend("\tedgeAlpha *= innerAlpha;\n");
119 }
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000120
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000121 SkString modulate;
bsalomon@google.com018f1792013-04-18 19:36:09 +0000122 GrGLSLModulatef<4>(&modulate, inputColor, "edgeAlpha");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000123 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str());
124 }
125
126 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
127 const CircleEdgeEffect& circleEffect = drawEffect.castEffect<CircleEdgeEffect>();
128
129 return circleEffect.isStroked() ? 0x1 : 0x0;
130 }
131
132 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {}
133
134 private:
135 typedef GrGLEffect INHERITED;
136 };
137
138
139private:
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000140 CircleEdgeEffect(bool stroke) : GrVertexEffect() {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000141 this->addVertexAttrib(kVec4f_GrSLType);
142 fStroke = stroke;
143 }
144
145 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
146 const CircleEdgeEffect& cee = CastEffect<CircleEdgeEffect>(other);
147 return cee.fStroke == fStroke;
148 }
149
150 bool fStroke;
151
152 GR_DECLARE_EFFECT_TEST;
153
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000154 typedef GrVertexEffect INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000155};
156
157GR_DEFINE_EFFECT_TEST(CircleEdgeEffect);
158
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000159GrEffectRef* CircleEdgeEffect::TestCreate(SkRandom* random,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000160 GrContext* context,
161 const GrDrawTargetCaps&,
162 GrTexture* textures[]) {
163 return CircleEdgeEffect::Create(random->nextBool());
164}
165
166///////////////////////////////////////////////////////////////////////////////
167
168/**
169 * 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 +0000170 * ellipse, specified as a 2D offset from center, and the reciprocals of the outer and inner radii,
171 * in both x and y directions.
172 *
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000173 * 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 +0000174 */
175
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000176class EllipseEdgeEffect : public GrVertexEffect {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000177public:
178 static GrEffectRef* Create(bool stroke) {
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000179 GR_CREATE_STATIC_EFFECT(gEllipseStrokeEdge, EllipseEdgeEffect, (true));
180 GR_CREATE_STATIC_EFFECT(gEllipseFillEdge, EllipseEdgeEffect, (false));
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000181
182 if (stroke) {
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000183 gEllipseStrokeEdge->ref();
184 return gEllipseStrokeEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000185 } else {
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000186 gEllipseFillEdge->ref();
187 return gEllipseFillEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000188 }
189 }
190
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000191 virtual void getConstantColorComponents(GrColor* color,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000192 uint32_t* validFlags) const SK_OVERRIDE {
193 *validFlags = 0;
194 }
195
196 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
197 return GrTBackendEffectFactory<EllipseEdgeEffect>::getInstance();
198 }
199
200 virtual ~EllipseEdgeEffect() {}
201
202 static const char* Name() { return "EllipseEdge"; }
203
204 inline bool isStroked() const { return fStroke; }
205
206 class GLEffect : public GrGLEffect {
207 public:
208 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
209 : INHERITED (factory) {}
210
211 virtual void emitCode(GrGLShaderBuilder* builder,
212 const GrDrawEffect& drawEffect,
213 EffectKey key,
214 const char* outputColor,
215 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000216 const TransformedCoordsArray&,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000217 const TextureSamplerArray& samplers) SK_OVERRIDE {
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +0000218 GrGLShaderBuilder::VertexBuilder* vertexBuilder = builder->getVertexBuilder();
219 SkASSERT(NULL != vertexBuilder);
220
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000221 const EllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<EllipseEdgeEffect>();
222
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000223 const char *vsOffsetName, *fsOffsetName;
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000224 const char *vsRadiiName, *fsRadiiName;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000225
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +0000226 vertexBuilder->addVarying(kVec2f_GrSLType, "EllipseOffsets", &vsOffsetName, &fsOffsetName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000227 const SkString* attr0Name =
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +0000228 vertexBuilder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]);
229 vertexBuilder->vsCodeAppendf("\t%s = %s;\n", vsOffsetName, attr0Name->c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000230
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +0000231 vertexBuilder->addVarying(kVec4f_GrSLType, "EllipseRadii", &vsRadiiName, &fsRadiiName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000232 const SkString* attr1Name =
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +0000233 vertexBuilder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[1]);
234 vertexBuilder->vsCodeAppendf("\t%s = %s;\n", vsRadiiName, attr1Name->c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000235
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +0000236 // for outer curve
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000237 builder->fsCodeAppendf("\tvec2 scaledOffset = %s*%s.xy;\n", fsOffsetName, fsRadiiName);
238 builder->fsCodeAppend("\tfloat test = dot(scaledOffset, scaledOffset) - 1.0;\n");
239 builder->fsCodeAppendf("\tvec2 grad = 2.0*scaledOffset*%s.xy;\n", fsRadiiName);
jvanverth@google.comd1b5b142013-07-02 14:46:03 +0000240 builder->fsCodeAppend("\tfloat grad_dot = dot(grad, grad);\n");
241 // we need to clamp the length^2 of the gradiant vector to a non-zero value, because
242 // on the Nexus 4 the undefined result of inversesqrt(0) drops out an entire tile
243 // TODO: restrict this to Adreno-only
244 builder->fsCodeAppend("\tgrad_dot = max(grad_dot, 1.0e-4);\n");
245 builder->fsCodeAppend("\tfloat invlen = inversesqrt(grad_dot);\n");
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000246 builder->fsCodeAppend("\tfloat edgeAlpha = clamp(0.5-test*invlen, 0.0, 1.0);\n");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000247
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000248 // for inner curve
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000249 if (ellipseEffect.isStroked()) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000250 builder->fsCodeAppendf("\tscaledOffset = %s*%s.zw;\n", fsOffsetName, fsRadiiName);
251 builder->fsCodeAppend("\ttest = dot(scaledOffset, scaledOffset) - 1.0;\n");
252 builder->fsCodeAppendf("\tgrad = 2.0*scaledOffset*%s.zw;\n", fsRadiiName);
253 builder->fsCodeAppend("\tinvlen = inversesqrt(dot(grad, grad));\n");
254 builder->fsCodeAppend("\tedgeAlpha *= clamp(0.5+test*invlen, 0.0, 1.0);\n");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000255 }
256
257 SkString modulate;
bsalomon@google.com018f1792013-04-18 19:36:09 +0000258 GrGLSLModulatef<4>(&modulate, inputColor, "edgeAlpha");
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000259 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000260 }
261
262 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
263 const EllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<EllipseEdgeEffect>();
264
265 return ellipseEffect.isStroked() ? 0x1 : 0x0;
266 }
267
268 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {
269 }
270
271 private:
272 typedef GrGLEffect INHERITED;
273 };
274
275private:
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000276 EllipseEdgeEffect(bool stroke) : GrVertexEffect() {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000277 this->addVertexAttrib(kVec2f_GrSLType);
278 this->addVertexAttrib(kVec4f_GrSLType);
279 fStroke = stroke;
280 }
281
282 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
283 const EllipseEdgeEffect& eee = CastEffect<EllipseEdgeEffect>(other);
284 return eee.fStroke == fStroke;
285 }
286
287 bool fStroke;
288
289 GR_DECLARE_EFFECT_TEST;
290
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000291 typedef GrVertexEffect INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000292};
293
294GR_DEFINE_EFFECT_TEST(EllipseEdgeEffect);
295
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000296GrEffectRef* EllipseEdgeEffect::TestCreate(SkRandom* random,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000297 GrContext* context,
298 const GrDrawTargetCaps&,
299 GrTexture* textures[]) {
300 return EllipseEdgeEffect::Create(random->nextBool());
301}
302
303///////////////////////////////////////////////////////////////////////////////
304
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000305/**
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000306 * The output of this effect is a modulation of the input color and coverage for an ellipse,
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000307 * specified as a 2D offset from center for both the outer and inner paths (if stroked). The
308 * implict equation used is for a unit circle (x^2 + y^2 - 1 = 0) and the edge corrected by
309 * using differentials.
310 *
311 * The result is device-independent and can be used with any affine matrix.
312 */
313
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000314class DIEllipseEdgeEffect : public GrVertexEffect {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000315public:
316 enum Mode { kStroke = 0, kHairline, kFill };
317
318 static GrEffectRef* Create(Mode mode) {
319 GR_CREATE_STATIC_EFFECT(gEllipseStrokeEdge, DIEllipseEdgeEffect, (kStroke));
320 GR_CREATE_STATIC_EFFECT(gEllipseHairlineEdge, DIEllipseEdgeEffect, (kHairline));
321 GR_CREATE_STATIC_EFFECT(gEllipseFillEdge, DIEllipseEdgeEffect, (kFill));
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000322
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000323 if (kStroke == mode) {
324 gEllipseStrokeEdge->ref();
325 return gEllipseStrokeEdge;
326 } else if (kHairline == mode) {
327 gEllipseHairlineEdge->ref();
328 return gEllipseHairlineEdge;
329 } else {
330 gEllipseFillEdge->ref();
331 return gEllipseFillEdge;
332 }
333 }
334
335 virtual void getConstantColorComponents(GrColor* color,
336 uint32_t* validFlags) const SK_OVERRIDE {
337 *validFlags = 0;
338 }
339
340 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
341 return GrTBackendEffectFactory<DIEllipseEdgeEffect>::getInstance();
342 }
343
344 virtual ~DIEllipseEdgeEffect() {}
345
346 static const char* Name() { return "DIEllipseEdge"; }
347
348 inline Mode getMode() const { return fMode; }
349
350 class GLEffect : public GrGLEffect {
351 public:
352 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
353 : INHERITED (factory) {}
354
355 virtual void emitCode(GrGLShaderBuilder* builder,
356 const GrDrawEffect& drawEffect,
357 EffectKey key,
358 const char* outputColor,
359 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000360 const TransformedCoordsArray&,
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000361 const TextureSamplerArray& samplers) SK_OVERRIDE {
362 GrGLShaderBuilder::VertexBuilder* vertexBuilder = builder->getVertexBuilder();
363 SkASSERT(NULL != vertexBuilder);
364
365 const DIEllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<DIEllipseEdgeEffect>();
366
367 SkAssertResult(builder->enableFeature(
368 GrGLShaderBuilder::kStandardDerivatives_GLSLFeature));
369
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000370 const char *vsOffsetName0, *fsOffsetName0;
371 vertexBuilder->addVarying(kVec2f_GrSLType, "EllipseOffsets0",
372 &vsOffsetName0, &fsOffsetName0);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000373 const SkString* attr0Name =
374 vertexBuilder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]);
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000375 vertexBuilder->vsCodeAppendf("\t%s = %s;\n", vsOffsetName0, attr0Name->c_str());
376 const char *vsOffsetName1, *fsOffsetName1;
377 vertexBuilder->addVarying(kVec2f_GrSLType, "EllipseOffsets1",
378 &vsOffsetName1, &fsOffsetName1);
379 const SkString* attr1Name =
380 vertexBuilder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[1]);
381 vertexBuilder->vsCodeAppendf("\t%s = %s;\n", vsOffsetName1, attr1Name->c_str());
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000382
383 // for outer curve
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000384 builder->fsCodeAppendf("\tvec2 scaledOffset = %s.xy;\n", fsOffsetName0);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000385 builder->fsCodeAppend("\tfloat test = dot(scaledOffset, scaledOffset) - 1.0;\n");
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000386 builder->fsCodeAppendf("\tvec2 duvdx = dFdx(%s);\n", fsOffsetName0);
387 builder->fsCodeAppendf("\tvec2 duvdy = dFdy(%s);\n", fsOffsetName0);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000388 builder->fsCodeAppendf("\tvec2 grad = vec2(2.0*%s.x*duvdx.x + 2.0*%s.y*duvdx.y,\n"
389 "\t 2.0*%s.x*duvdy.x + 2.0*%s.y*duvdy.y);\n",
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000390 fsOffsetName0, fsOffsetName0, fsOffsetName0, fsOffsetName0);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000391
392 builder->fsCodeAppend("\tfloat grad_dot = dot(grad, grad);\n");
393 // we need to clamp the length^2 of the gradiant vector to a non-zero value, because
394 // on the Nexus 4 the undefined result of inversesqrt(0) drops out an entire tile
395 // TODO: restrict this to Adreno-only
396 builder->fsCodeAppend("\tgrad_dot = max(grad_dot, 1.0e-4);\n");
397 builder->fsCodeAppend("\tfloat invlen = inversesqrt(grad_dot);\n");
398 if (kHairline == ellipseEffect.getMode()) {
399 // can probably do this with one step
400 builder->fsCodeAppend("\tfloat edgeAlpha = clamp(1.0-test*invlen, 0.0, 1.0);\n");
401 builder->fsCodeAppend("\tedgeAlpha *= clamp(1.0+test*invlen, 0.0, 1.0);\n");
402 } else {
403 builder->fsCodeAppend("\tfloat edgeAlpha = clamp(0.5-test*invlen, 0.0, 1.0);\n");
404 }
405
406 // for inner curve
407 if (kStroke == ellipseEffect.getMode()) {
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000408 builder->fsCodeAppendf("\tscaledOffset = %s.xy;\n", fsOffsetName1);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000409 builder->fsCodeAppend("\ttest = dot(scaledOffset, scaledOffset) - 1.0;\n");
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000410 builder->fsCodeAppendf("\tduvdx = dFdx(%s);\n", fsOffsetName1);
411 builder->fsCodeAppendf("\tduvdy = dFdy(%s);\n", fsOffsetName1);
412 builder->fsCodeAppendf("\tgrad = vec2(2.0*%s.x*duvdx.x + 2.0*%s.y*duvdx.y,\n"
413 "\t 2.0*%s.x*duvdy.x + 2.0*%s.y*duvdy.y);\n",
414 fsOffsetName1, fsOffsetName1, fsOffsetName1, fsOffsetName1);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000415 builder->fsCodeAppend("\tinvlen = inversesqrt(dot(grad, grad));\n");
416 builder->fsCodeAppend("\tedgeAlpha *= clamp(0.5+test*invlen, 0.0, 1.0);\n");
417 }
418
419 SkString modulate;
420 GrGLSLModulatef<4>(&modulate, inputColor, "edgeAlpha");
421 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str());
422 }
423
424 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
425 const DIEllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<DIEllipseEdgeEffect>();
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000426
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000427 return ellipseEffect.getMode();
428 }
429
430 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {
431 }
432
433 private:
434 typedef GrGLEffect INHERITED;
435 };
436
437private:
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000438 DIEllipseEdgeEffect(Mode mode) : GrVertexEffect() {
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000439 this->addVertexAttrib(kVec2f_GrSLType);
440 this->addVertexAttrib(kVec2f_GrSLType);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000441 fMode = mode;
442 }
443
444 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
445 const DIEllipseEdgeEffect& eee = CastEffect<DIEllipseEdgeEffect>(other);
446 return eee.fMode == fMode;
447 }
448
449 Mode fMode;
450
451 GR_DECLARE_EFFECT_TEST;
452
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000453 typedef GrVertexEffect INHERITED;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000454};
455
456GR_DEFINE_EFFECT_TEST(DIEllipseEdgeEffect);
457
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000458GrEffectRef* DIEllipseEdgeEffect::TestCreate(SkRandom* random,
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000459 GrContext* context,
460 const GrDrawTargetCaps&,
461 GrTexture* textures[]) {
462 return DIEllipseEdgeEffect::Create((Mode)(random->nextRangeU(0,2)));
463}
464
465///////////////////////////////////////////////////////////////////////////////
466
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000467void GrOvalRenderer::reset() {
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000468 SkSafeSetNull(fRRectIndexBuffer);
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000469}
470
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000471bool GrOvalRenderer::drawOval(GrDrawTarget* target, const GrContext* context, bool useAA,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000472 const SkRect& oval, const SkStrokeRec& stroke)
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000473{
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000474 if (!useAA) {
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000475 return false;
476 }
477
478 const SkMatrix& vm = context->getMatrix();
479
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000480 // we can draw circles
481 if (SkScalarNearlyEqual(oval.width(), oval.height())
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000482 && circle_stays_circle(vm)) {
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000483 this->drawCircle(target, useAA, oval, stroke);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000484 // if we have shader derivative support, render as device-independent
485 } else if (target->caps()->shaderDerivativeSupport()) {
486 return this->drawDIEllipse(target, useAA, oval, stroke);
487 // otherwise axis-aligned ellipses only
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000488 } else if (vm.rectStaysRect()) {
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000489 return this->drawEllipse(target, useAA, oval, stroke);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000490 } else {
491 return false;
492 }
493
494 return true;
495}
496
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000497///////////////////////////////////////////////////////////////////////////////
498
robertphillips@google.com42903302013-04-20 12:26:07 +0000499// position + edge
500extern const GrVertexAttrib gCircleVertexAttribs[] = {
501 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
502 {kVec4f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding}
503};
504
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000505void GrOvalRenderer::drawCircle(GrDrawTarget* target,
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000506 bool useAA,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000507 const SkRect& circle,
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000508 const SkStrokeRec& stroke)
509{
510 GrDrawState* drawState = target->drawState();
511
512 const SkMatrix& vm = drawState->getViewMatrix();
513 GrPoint center = GrPoint::Make(circle.centerX(), circle.centerY());
514 vm.mapPoints(&center, 1);
515 SkScalar radius = vm.mapRadius(SkScalarHalf(circle.width()));
516 SkScalar strokeWidth = vm.mapRadius(stroke.getWidth());
517
bsalomon@google.com137f1342013-05-29 21:27:53 +0000518 GrDrawState::AutoViewMatrixRestore avmr;
519 if (!avmr.setIdentity(drawState)) {
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000520 return;
521 }
522
robertphillips@google.com42903302013-04-20 12:26:07 +0000523 drawState->setVertexAttribs<gCircleVertexAttribs>(SK_ARRAY_COUNT(gCircleVertexAttribs));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000524 SkASSERT(sizeof(CircleVertex) == drawState->getVertexSize());
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000525
526 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
527 if (!geo.succeeded()) {
528 GrPrintf("Failed to get space for vertices!\n");
529 return;
530 }
531
532 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
533
534 SkStrokeRec::Style style = stroke.getStyle();
535 bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style);
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000536
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000537 SkScalar innerRadius = 0.0f;
538 SkScalar outerRadius = radius;
539 SkScalar halfWidth = 0;
540 if (style != SkStrokeRec::kFill_Style) {
541 if (SkScalarNearlyZero(strokeWidth)) {
542 halfWidth = SK_ScalarHalf;
543 } else {
544 halfWidth = SkScalarHalf(strokeWidth);
545 }
546
547 outerRadius += halfWidth;
548 if (isStroked) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000549 innerRadius = radius - halfWidth;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000550 }
551 }
552
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +0000553 GrEffectRef* effect = CircleEdgeEffect::Create(isStroked && innerRadius > 0);
554 static const int kCircleEdgeAttrIndex = 1;
555 drawState->addCoverageEffect(effect, kCircleEdgeAttrIndex)->unref();
556
bsalomon@google.com58e30fe2013-04-01 19:01:20 +0000557 // The radii are outset for two reasons. First, it allows the shader to simply perform
558 // clamp(distance-to-center - radius, 0, 1). Second, the outer radius is used to compute the
559 // verts of the bounding box that is rendered and the outset ensures the box will cover all
560 // pixels partially covered by the circle.
561 outerRadius += SK_ScalarHalf;
562 innerRadius -= SK_ScalarHalf;
563
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000564 SkRect bounds = SkRect::MakeLTRB(
bsalomon@google.com58e30fe2013-04-01 19:01:20 +0000565 center.fX - outerRadius,
566 center.fY - outerRadius,
567 center.fX + outerRadius,
568 center.fY + outerRadius
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000569 );
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000570
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000571 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000572 verts[0].fOffset = SkPoint::Make(-outerRadius, -outerRadius);
573 verts[0].fOuterRadius = outerRadius;
574 verts[0].fInnerRadius = innerRadius;
575
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000576 verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
skia.committer@gmail.com46746762013-04-12 07:01:51 +0000577 verts[1].fOffset = SkPoint::Make(outerRadius, -outerRadius);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000578 verts[1].fOuterRadius = outerRadius;
579 verts[1].fInnerRadius = innerRadius;
580
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000581 verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000582 verts[2].fOffset = SkPoint::Make(-outerRadius, outerRadius);
583 verts[2].fOuterRadius = outerRadius;
584 verts[2].fInnerRadius = innerRadius;
585
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000586 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000587 verts[3].fOffset = SkPoint::Make(outerRadius, outerRadius);
588 verts[3].fOuterRadius = outerRadius;
589 verts[3].fInnerRadius = innerRadius;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000590
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000591 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000592}
593
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000594///////////////////////////////////////////////////////////////////////////////
595
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000596// position + offset + 1/radii
robertphillips@google.com42903302013-04-20 12:26:07 +0000597extern const GrVertexAttrib gEllipseVertexAttribs[] = {
598 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
599 {kVec2f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding},
600 {kVec4f_GrVertexAttribType, 2*sizeof(GrPoint), kEffect_GrVertexAttribBinding}
601};
602
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000603// position + offsets
604extern const GrVertexAttrib gDIEllipseVertexAttribs[] = {
605 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000606 {kVec2f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding},
607 {kVec2f_GrVertexAttribType, 2*sizeof(GrPoint), kEffect_GrVertexAttribBinding},
robertphillips@google.com42903302013-04-20 12:26:07 +0000608};
609
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000610bool GrOvalRenderer::drawEllipse(GrDrawTarget* target,
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000611 bool useAA,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000612 const SkRect& ellipse,
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000613 const SkStrokeRec& stroke)
614{
615 GrDrawState* drawState = target->drawState();
616#ifdef SK_DEBUG
617 {
618 // we should have checked for this previously
619 bool isAxisAlignedEllipse = drawState->getViewMatrix().rectStaysRect();
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000620 SkASSERT(useAA && isAxisAlignedEllipse);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000621 }
622#endif
623
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000624 // do any matrix crunching before we reset the draw state for device coords
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000625 const SkMatrix& vm = drawState->getViewMatrix();
626 GrPoint center = GrPoint::Make(ellipse.centerX(), ellipse.centerY());
627 vm.mapPoints(&center, 1);
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000628 SkScalar ellipseXRadius = SkScalarHalf(ellipse.width());
629 SkScalar ellipseYRadius = SkScalarHalf(ellipse.height());
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +0000630 SkScalar xRadius = SkScalarAbs(vm[SkMatrix::kMScaleX]*ellipseXRadius +
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000631 vm[SkMatrix::kMSkewY]*ellipseYRadius);
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +0000632 SkScalar yRadius = SkScalarAbs(vm[SkMatrix::kMSkewX]*ellipseXRadius +
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000633 vm[SkMatrix::kMScaleY]*ellipseYRadius);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000634
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000635 // do (potentially) anisotropic mapping of stroke
636 SkVector scaledStroke;
637 SkScalar strokeWidth = stroke.getWidth();
638 scaledStroke.fX = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMScaleX] + vm[SkMatrix::kMSkewY]));
639 scaledStroke.fY = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMSkewX] + vm[SkMatrix::kMScaleY]));
640
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000641 SkStrokeRec::Style style = stroke.getStyle();
642 bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style);
643
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000644 SkScalar innerXRadius = 0;
645 SkScalar innerYRadius = 0;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000646 if (SkStrokeRec::kFill_Style != style) {
647 if (SkScalarNearlyZero(scaledStroke.length())) {
648 scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf);
649 } else {
650 scaledStroke.scale(SK_ScalarHalf);
651 }
652
653 // we only handle thick strokes for near-circular ellipses
654 if (scaledStroke.length() > SK_ScalarHalf &&
655 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
656 return false;
657 }
658
659 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
660 if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius ||
661 scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) {
662 return false;
663 }
664
665 // this is legit only if scale & translation (which should be the case at the moment)
666 if (isStroked) {
667 innerXRadius = xRadius - scaledStroke.fX;
668 innerYRadius = yRadius - scaledStroke.fY;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000669 }
670
671 xRadius += scaledStroke.fX;
672 yRadius += scaledStroke.fY;
673 }
674
bsalomon@google.com137f1342013-05-29 21:27:53 +0000675 GrDrawState::AutoViewMatrixRestore avmr;
676 if (!avmr.setIdentity(drawState)) {
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000677 return false;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000678 }
679
robertphillips@google.com42903302013-04-20 12:26:07 +0000680 drawState->setVertexAttribs<gEllipseVertexAttribs>(SK_ARRAY_COUNT(gEllipseVertexAttribs));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000681 SkASSERT(sizeof(EllipseVertex) == drawState->getVertexSize());
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000682
683 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
684 if (!geo.succeeded()) {
685 GrPrintf("Failed to get space for vertices!\n");
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000686 return false;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000687 }
688
689 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices());
690
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +0000691 GrEffectRef* effect = EllipseEdgeEffect::Create(isStroked &&
692 innerXRadius > 0 && innerYRadius > 0);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000693
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000694 static const int kEllipseCenterAttrIndex = 1;
jvanverth@google.com6cc8d442013-09-10 18:24:37 +0000695 static const int kEllipseEdgeAttrIndex = 2;
696 drawState->addCoverageEffect(effect, kEllipseCenterAttrIndex, kEllipseEdgeAttrIndex)->unref();
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000697
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000698 // Compute the reciprocals of the radii here to save time in the shader
699 SkScalar xRadRecip = SkScalarInvert(xRadius);
700 SkScalar yRadRecip = SkScalarInvert(yRadius);
701 SkScalar xInnerRadRecip = SkScalarInvert(innerXRadius);
702 SkScalar yInnerRadRecip = SkScalarInvert(innerYRadius);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000703
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000704 // We've extended the outer x radius out half a pixel to antialias.
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000705 // This will also expand the rect so all the pixels will be captured.
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000706 // TODO: Consider if we should use sqrt(2)/2 instead
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000707 xRadius += SK_ScalarHalf;
708 yRadius += SK_ScalarHalf;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000709
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000710 SkRect bounds = SkRect::MakeLTRB(
711 center.fX - xRadius,
712 center.fY - yRadius,
713 center.fX + xRadius,
714 center.fY + yRadius
715 );
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000716
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000717 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000718 verts[0].fOffset = SkPoint::Make(-xRadius, -yRadius);
719 verts[0].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
720 verts[0].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000721
722 verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000723 verts[1].fOffset = SkPoint::Make(xRadius, -yRadius);
724 verts[1].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
725 verts[1].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000726
727 verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000728 verts[2].fOffset = SkPoint::Make(-xRadius, yRadius);
729 verts[2].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
730 verts[2].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000731
732 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000733 verts[3].fOffset = SkPoint::Make(xRadius, yRadius);
734 verts[3].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
735 verts[3].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
skia.committer@gmail.com46746762013-04-12 07:01:51 +0000736
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000737 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds);
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000738
739 return true;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000740}
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000741
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000742bool GrOvalRenderer::drawDIEllipse(GrDrawTarget* target,
743 bool useAA,
744 const SkRect& ellipse,
745 const SkStrokeRec& stroke)
746{
747 GrDrawState* drawState = target->drawState();
748 const SkMatrix& vm = drawState->getViewMatrix();
749
750 GrPoint center = GrPoint::Make(ellipse.centerX(), ellipse.centerY());
751 SkScalar xRadius = SkScalarHalf(ellipse.width());
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000752 SkScalar yRadius = SkScalarHalf(ellipse.height());
753
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000754 SkStrokeRec::Style style = stroke.getStyle();
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000755 DIEllipseEdgeEffect::Mode mode = (SkStrokeRec::kStroke_Style == style) ?
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000756 DIEllipseEdgeEffect::kStroke :
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000757 (SkStrokeRec::kHairline_Style == style) ?
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000758 DIEllipseEdgeEffect::kHairline : DIEllipseEdgeEffect::kFill;
759
760 SkScalar innerXRadius = 0;
761 SkScalar innerYRadius = 0;
762 if (SkStrokeRec::kFill_Style != style && SkStrokeRec::kHairline_Style != style) {
763 SkScalar strokeWidth = stroke.getWidth();
764
765 if (SkScalarNearlyZero(strokeWidth)) {
766 strokeWidth = SK_ScalarHalf;
767 } else {
768 strokeWidth *= SK_ScalarHalf;
769 }
770
771 // we only handle thick strokes for near-circular ellipses
772 if (strokeWidth > SK_ScalarHalf &&
773 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
774 return false;
775 }
776
777 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
778 if (strokeWidth*(yRadius*yRadius) < (strokeWidth*strokeWidth)*xRadius ||
779 strokeWidth*(xRadius*xRadius) < (strokeWidth*strokeWidth)*yRadius) {
780 return false;
781 }
782
783 // set inner radius (if needed)
784 if (SkStrokeRec::kStroke_Style == style) {
785 innerXRadius = xRadius - strokeWidth;
786 innerYRadius = yRadius - strokeWidth;
787 }
788
789 xRadius += strokeWidth;
790 yRadius += strokeWidth;
791 }
792 if (DIEllipseEdgeEffect::kStroke == mode) {
793 mode = (innerXRadius > 0 && innerYRadius > 0) ? DIEllipseEdgeEffect::kStroke :
794 DIEllipseEdgeEffect::kFill;
795 }
796 SkScalar innerRatioX = SkScalarDiv(xRadius, innerXRadius);
797 SkScalar innerRatioY = SkScalarDiv(yRadius, innerYRadius);
798
799 drawState->setVertexAttribs<gDIEllipseVertexAttribs>(SK_ARRAY_COUNT(gDIEllipseVertexAttribs));
800 SkASSERT(sizeof(DIEllipseVertex) == drawState->getVertexSize());
801
802 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
803 if (!geo.succeeded()) {
804 GrPrintf("Failed to get space for vertices!\n");
805 return false;
806 }
807
808 DIEllipseVertex* verts = reinterpret_cast<DIEllipseVertex*>(geo.vertices());
809
810 GrEffectRef* effect = DIEllipseEdgeEffect::Create(mode);
811
812 static const int kEllipseOuterOffsetAttrIndex = 1;
813 static const int kEllipseInnerOffsetAttrIndex = 2;
814 drawState->addCoverageEffect(effect, kEllipseOuterOffsetAttrIndex,
815 kEllipseInnerOffsetAttrIndex)->unref();
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000816
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000817 // This expands the outer rect so that after CTM we end up with a half-pixel border
818 SkScalar a = vm[SkMatrix::kMScaleX];
819 SkScalar b = vm[SkMatrix::kMSkewX];
820 SkScalar c = vm[SkMatrix::kMSkewY];
821 SkScalar d = vm[SkMatrix::kMScaleY];
822 SkScalar geoDx = SkScalarDiv(SK_ScalarHalf, SkScalarSqrt(a*a + c*c));
823 SkScalar geoDy = SkScalarDiv(SK_ScalarHalf, SkScalarSqrt(b*b + d*d));
824 // This adjusts the "radius" to include the half-pixel border
825 SkScalar offsetDx = SkScalarDiv(geoDx, xRadius);
826 SkScalar offsetDy = SkScalarDiv(geoDy, yRadius);
827
828 SkRect bounds = SkRect::MakeLTRB(
829 center.fX - xRadius - geoDx,
830 center.fY - yRadius - geoDy,
831 center.fX + xRadius + geoDx,
832 center.fY + yRadius + geoDy
833 );
834
835 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
836 verts[0].fOuterOffset = SkPoint::Make(-1.0f - offsetDx, -1.0f - offsetDy);
837 verts[0].fInnerOffset = SkPoint::Make(-innerRatioX - offsetDx, -innerRatioY - offsetDy);
838
839 verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
840 verts[1].fOuterOffset = SkPoint::Make(1.0f + offsetDx, -1.0f - offsetDy);
841 verts[1].fInnerOffset = SkPoint::Make(innerRatioX + offsetDx, -innerRatioY - offsetDy);
842
843 verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
844 verts[2].fOuterOffset = SkPoint::Make(-1.0f - offsetDx, 1.0f + offsetDy);
845 verts[2].fInnerOffset = SkPoint::Make(-innerRatioX - offsetDx, innerRatioY + offsetDy);
846
847 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
848 verts[3].fOuterOffset = SkPoint::Make(1.0f + offsetDx, 1.0f + offsetDy);
849 verts[3].fInnerOffset = SkPoint::Make(innerRatioX + offsetDx, innerRatioY + offsetDy);
850
851 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds);
852
853 return true;
854}
855
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000856///////////////////////////////////////////////////////////////////////////////
857
858static const uint16_t gRRectIndices[] = {
859 // corners
860 0, 1, 5, 0, 5, 4,
861 2, 3, 7, 2, 7, 6,
862 8, 9, 13, 8, 13, 12,
863 10, 11, 15, 10, 15, 14,
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000864
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000865 // edges
866 1, 2, 6, 1, 6, 5,
867 4, 5, 9, 4, 9, 8,
868 6, 7, 11, 6, 11, 10,
869 9, 10, 14, 9, 14, 13,
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000870
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000871 // center
872 // we place this at the end so that we can ignore these indices when rendering stroke-only
873 5, 6, 10, 5, 10, 9
874};
875
876
877GrIndexBuffer* GrOvalRenderer::rRectIndexBuffer(GrGpu* gpu) {
878 if (NULL == fRRectIndexBuffer) {
879 fRRectIndexBuffer =
880 gpu->createIndexBuffer(sizeof(gRRectIndices), false);
881 if (NULL != fRRectIndexBuffer) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000882#ifdef SK_DEBUG
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000883 bool updated =
884#endif
885 fRRectIndexBuffer->updateData(gRRectIndices,
886 sizeof(gRRectIndices));
887 GR_DEBUGASSERT(updated);
888 }
889 }
890 return fRRectIndexBuffer;
891}
892
skia.committer@gmail.com2fd42c42013-05-03 07:01:00 +0000893bool GrOvalRenderer::drawSimpleRRect(GrDrawTarget* target, GrContext* context, bool useAA,
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000894 const SkRRect& rrect, const SkStrokeRec& stroke)
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000895{
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000896 // only anti-aliased rrects for now
897 if (!useAA) {
898 return false;
899 }
900
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000901 const SkMatrix& vm = context->getMatrix();
902#ifdef SK_DEBUG
903 {
904 // we should have checked for this previously
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000905 SkASSERT(useAA && vm.rectStaysRect() && rrect.isSimple());
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000906 }
907#endif
908
909 // do any matrix crunching before we reset the draw state for device coords
910 const SkRect& rrectBounds = rrect.getBounds();
911 SkRect bounds;
912 vm.mapRect(&bounds, rrectBounds);
913
914 SkVector radii = rrect.getSimpleRadii();
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000915 SkScalar xRadius = SkScalarAbs(vm[SkMatrix::kMScaleX]*radii.fX +
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000916 vm[SkMatrix::kMSkewY]*radii.fY);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000917 SkScalar yRadius = SkScalarAbs(vm[SkMatrix::kMSkewX]*radii.fX +
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000918 vm[SkMatrix::kMScaleY]*radii.fY);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000919
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000920 // if hairline stroke is greater than radius, we don't handle that right now
921 SkStrokeRec::Style style = stroke.getStyle();
922 if (SkStrokeRec::kHairline_Style == style &&
923 (SK_ScalarHalf >= xRadius || SK_ScalarHalf >= yRadius)) {
924 return false;
925 }
926
927 // do (potentially) anisotropic mapping of stroke
928 SkVector scaledStroke;
929 SkScalar strokeWidth = stroke.getWidth();
930 scaledStroke.fX = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMScaleX] + vm[SkMatrix::kMSkewY]));
931 scaledStroke.fY = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMSkewX] + vm[SkMatrix::kMScaleY]));
932
933 // if half of strokewidth is greater than radius, we don't handle that right now
934 if (SK_ScalarHalf*scaledStroke.fX >= xRadius || SK_ScalarHalf*scaledStroke.fY >= yRadius) {
935 return false;
936 }
937
938 // reset to device coordinates
939 GrDrawState* drawState = target->drawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000940 GrDrawState::AutoViewMatrixRestore avmr;
941 if (!avmr.setIdentity(drawState)) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000942 return false;
943 }
944
945 bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style);
946
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000947 GrIndexBuffer* indexBuffer = this->rRectIndexBuffer(context->getGpu());
948 if (NULL == indexBuffer) {
949 GrPrintf("Failed to create index buffer!\n");
950 return false;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000951 }
952
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000953 // if the corners are circles, use the circle renderer
954 if ((!isStroked || scaledStroke.fX == scaledStroke.fY) && xRadius == yRadius) {
955 drawState->setVertexAttribs<gCircleVertexAttribs>(SK_ARRAY_COUNT(gCircleVertexAttribs));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000956 SkASSERT(sizeof(CircleVertex) == drawState->getVertexSize());
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000957
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000958 GrDrawTarget::AutoReleaseGeometry geo(target, 16, 0);
959 if (!geo.succeeded()) {
960 GrPrintf("Failed to get space for vertices!\n");
961 return false;
962 }
963 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000964
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000965 SkScalar innerRadius = 0.0f;
966 SkScalar outerRadius = xRadius;
967 SkScalar halfWidth = 0;
968 if (style != SkStrokeRec::kFill_Style) {
969 if (SkScalarNearlyZero(scaledStroke.fX)) {
970 halfWidth = SK_ScalarHalf;
971 } else {
972 halfWidth = SkScalarHalf(scaledStroke.fX);
973 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000974
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000975 if (isStroked) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000976 innerRadius = xRadius - halfWidth;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000977 }
978 outerRadius += halfWidth;
979 bounds.outset(halfWidth, halfWidth);
980 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000981
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000982 isStroked = (isStroked && innerRadius > 0);
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +0000983
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000984 GrEffectRef* effect = CircleEdgeEffect::Create(isStroked);
985 static const int kCircleEdgeAttrIndex = 1;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000986 drawState->addCoverageEffect(effect, kCircleEdgeAttrIndex)->unref();
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +0000987
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000988 // The radii are outset for two reasons. First, it allows the shader to simply perform
989 // clamp(distance-to-center - radius, 0, 1). Second, the outer radius is used to compute the
990 // verts of the bounding box that is rendered and the outset ensures the box will cover all
991 // pixels partially covered by the circle.
992 outerRadius += SK_ScalarHalf;
993 innerRadius -= SK_ScalarHalf;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000994
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000995 // Expand the rect so all the pixels will be captured.
996 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000997
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000998 SkScalar yCoords[4] = {
999 bounds.fTop,
1000 bounds.fTop + outerRadius,
1001 bounds.fBottom - outerRadius,
1002 bounds.fBottom
1003 };
1004 SkScalar yOuterRadii[4] = {
1005 -outerRadius,
1006 0,
1007 0,
1008 outerRadius
1009 };
1010 for (int i = 0; i < 4; ++i) {
1011 verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]);
1012 verts->fOffset = SkPoint::Make(-outerRadius, yOuterRadii[i]);
1013 verts->fOuterRadius = outerRadius;
1014 verts->fInnerRadius = innerRadius;
1015 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001016
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001017 verts->fPos = SkPoint::Make(bounds.fLeft + outerRadius, yCoords[i]);
1018 verts->fOffset = SkPoint::Make(0, yOuterRadii[i]);
1019 verts->fOuterRadius = outerRadius;
1020 verts->fInnerRadius = innerRadius;
1021 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001022
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001023 verts->fPos = SkPoint::Make(bounds.fRight - outerRadius, yCoords[i]);
1024 verts->fOffset = SkPoint::Make(0, yOuterRadii[i]);
1025 verts->fOuterRadius = outerRadius;
1026 verts->fInnerRadius = innerRadius;
1027 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001028
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001029 verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]);
1030 verts->fOffset = SkPoint::Make(outerRadius, yOuterRadii[i]);
1031 verts->fOuterRadius = outerRadius;
1032 verts->fInnerRadius = innerRadius;
1033 verts++;
1034 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001035
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001036 // drop out the middle quad if we're stroked
1037 int indexCnt = isStroked ? GR_ARRAY_COUNT(gRRectIndices)-6 : GR_ARRAY_COUNT(gRRectIndices);
1038 target->setIndexSourceToBuffer(indexBuffer);
1039 target->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, 16, indexCnt, &bounds);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001040
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001041 // otherwise we use the ellipse renderer
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001042 } else {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001043 drawState->setVertexAttribs<gEllipseVertexAttribs>(SK_ARRAY_COUNT(gEllipseVertexAttribs));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001044 SkASSERT(sizeof(EllipseVertex) == drawState->getVertexSize());
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001045
1046 SkScalar innerXRadius = 0.0f;
1047 SkScalar innerYRadius = 0.0f;
1048 if (SkStrokeRec::kFill_Style != style) {
1049 if (SkScalarNearlyZero(scaledStroke.length())) {
1050 scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf);
1051 } else {
1052 scaledStroke.scale(SK_ScalarHalf);
1053 }
1054
1055 // we only handle thick strokes for near-circular ellipses
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +00001056 if (scaledStroke.length() > SK_ScalarHalf &&
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001057 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
1058 return false;
1059 }
1060
1061 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
1062 if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius ||
1063 scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) {
1064 return false;
1065 }
1066
1067 // this is legit only if scale & translation (which should be the case at the moment)
1068 if (isStroked) {
1069 innerXRadius = xRadius - scaledStroke.fX;
1070 innerYRadius = yRadius - scaledStroke.fY;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001071 }
1072
1073 xRadius += scaledStroke.fX;
1074 yRadius += scaledStroke.fY;
1075 bounds.outset(scaledStroke.fX, scaledStroke.fY);
1076 }
jvanverth@google.come3647412013-05-08 15:31:43 +00001077
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +00001078 isStroked = (isStroked && innerXRadius > 0 && innerYRadius > 0);
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +00001079
jvanverth@google.come3647412013-05-08 15:31:43 +00001080 GrDrawTarget::AutoReleaseGeometry geo(target, 16, 0);
1081 if (!geo.succeeded()) {
1082 GrPrintf("Failed to get space for vertices!\n");
1083 return false;
1084 }
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001085 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices());
jvanverth@google.come3647412013-05-08 15:31:43 +00001086
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001087 GrEffectRef* effect = EllipseEdgeEffect::Create(isStroked);
jvanverth@google.come3647412013-05-08 15:31:43 +00001088 static const int kEllipseOffsetAttrIndex = 1;
1089 static const int kEllipseRadiiAttrIndex = 2;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001090 drawState->addCoverageEffect(effect,
1091 kEllipseOffsetAttrIndex, kEllipseRadiiAttrIndex)->unref();
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001092
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001093 // Compute the reciprocals of the radii here to save time in the shader
1094 SkScalar xRadRecip = SkScalarInvert(xRadius);
1095 SkScalar yRadRecip = SkScalarInvert(yRadius);
1096 SkScalar xInnerRadRecip = SkScalarInvert(innerXRadius);
1097 SkScalar yInnerRadRecip = SkScalarInvert(innerYRadius);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001098
1099 // Extend the radii out half a pixel to antialias.
1100 SkScalar xOuterRadius = xRadius + SK_ScalarHalf;
1101 SkScalar yOuterRadius = yRadius + SK_ScalarHalf;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001102
1103 // Expand the rect so all the pixels will be captured.
1104 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1105
1106 SkScalar yCoords[4] = {
1107 bounds.fTop,
1108 bounds.fTop + yOuterRadius,
1109 bounds.fBottom - yOuterRadius,
1110 bounds.fBottom
1111 };
1112 SkScalar yOuterOffsets[4] = {
jvanverth@google.comd1b5b142013-07-02 14:46:03 +00001113 yOuterRadius,
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001114 SK_ScalarNearlyZero, // we're using inversesqrt() in the shader, so can't be exactly 0
1115 SK_ScalarNearlyZero,
1116 yOuterRadius
1117 };
1118
1119 for (int i = 0; i < 4; ++i) {
1120 verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]);
jvanverth@google.comd1b5b142013-07-02 14:46:03 +00001121 verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001122 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1123 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001124 verts++;
1125
1126 verts->fPos = SkPoint::Make(bounds.fLeft + xOuterRadius, yCoords[i]);
1127 verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001128 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1129 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001130 verts++;
1131
1132 verts->fPos = SkPoint::Make(bounds.fRight - xOuterRadius, yCoords[i]);
1133 verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001134 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1135 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001136 verts++;
1137
1138 verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]);
1139 verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001140 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1141 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001142 verts++;
1143 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001144
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001145 // drop out the middle quad if we're stroked
1146 int indexCnt = isStroked ? GR_ARRAY_COUNT(gRRectIndices)-6 : GR_ARRAY_COUNT(gRRectIndices);
1147 target->setIndexSourceToBuffer(indexBuffer);
1148 target->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, 16, indexCnt, &bounds);
1149 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001150
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001151 return true;
1152}