blob: 648bc14f08d7c65da59c7be2f099ccae230f01b8 [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"
bsalomon848faf02014-07-11 10:01:02 -070013#include "gl/GrGLShaderBuilder.h"
commit-bot@chromium.org261dc562013-10-04 15:42:56 +000014#include "gl/GrGLVertexEffect.h"
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000015#include "GrTBackendEffectFactory.h"
commit-bot@chromium.org81312832013-03-22 18:34:09 +000016
17#include "GrDrawState.h"
18#include "GrDrawTarget.h"
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +000019#include "GrGpu.h"
20
21#include "SkRRect.h"
commit-bot@chromium.org81312832013-03-22 18:34:09 +000022#include "SkStrokeRec.h"
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +000023#include "SkTLazy.h"
commit-bot@chromium.org81312832013-03-22 18:34:09 +000024
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +000025#include "effects/GrVertexEffect.h"
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +000026#include "effects/GrRRectEffect.h"
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +000027
commit-bot@chromium.org81312832013-03-22 18:34:09 +000028namespace {
29
30struct CircleVertex {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000031 SkPoint fPos;
32 SkPoint fOffset;
commit-bot@chromium.org81312832013-03-22 18:34:09 +000033 SkScalar fOuterRadius;
34 SkScalar fInnerRadius;
35};
36
37struct EllipseVertex {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000038 SkPoint fPos;
39 SkPoint fOffset;
40 SkPoint fOuterRadii;
41 SkPoint fInnerRadii;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +000042};
43
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +000044struct DIEllipseVertex {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000045 SkPoint fPos;
46 SkPoint fOuterOffset;
47 SkPoint fInnerOffset;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +000048};
49
commit-bot@chromium.org81312832013-03-22 18:34:09 +000050inline bool circle_stays_circle(const SkMatrix& m) {
51 return m.isSimilarity();
52}
53
54}
55
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000056///////////////////////////////////////////////////////////////////////////////
57
58/**
59 * 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 +000060 * specified as offset_x, offset_y (both from center point), outer radius and inner radius.
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000061 */
62
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +000063class CircleEdgeEffect : public GrVertexEffect {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000064public:
bsalomon83d081a2014-07-08 09:56:10 -070065 static GrEffect* Create(bool stroke) {
bsalomon@google.comd42aca32013-04-23 15:37:27 +000066 GR_CREATE_STATIC_EFFECT(gCircleStrokeEdge, CircleEdgeEffect, (true));
67 GR_CREATE_STATIC_EFFECT(gCircleFillEdge, CircleEdgeEffect, (false));
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000068
69 if (stroke) {
bsalomon@google.comd42aca32013-04-23 15:37:27 +000070 gCircleStrokeEdge->ref();
71 return gCircleStrokeEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000072 } else {
bsalomon@google.comd42aca32013-04-23 15:37:27 +000073 gCircleFillEdge->ref();
74 return gCircleFillEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000075 }
76 }
77
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +000078 virtual void getConstantColorComponents(GrColor* color,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000079 uint32_t* validFlags) const SK_OVERRIDE {
80 *validFlags = 0;
81 }
82
83 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
84 return GrTBackendEffectFactory<CircleEdgeEffect>::getInstance();
85 }
86
87 virtual ~CircleEdgeEffect() {}
88
89 static const char* Name() { return "CircleEdge"; }
90
91 inline bool isStroked() const { return fStroke; }
92
commit-bot@chromium.org261dc562013-10-04 15:42:56 +000093 class GLEffect : public GrGLVertexEffect {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000094 public:
95 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
96 : INHERITED (factory) {}
97
commit-bot@chromium.org261dc562013-10-04 15:42:56 +000098 virtual void emitCode(GrGLFullShaderBuilder* builder,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000099 const GrDrawEffect& drawEffect,
100 EffectKey key,
101 const char* outputColor,
102 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000103 const TransformedCoordsArray&,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000104 const TextureSamplerArray& samplers) SK_OVERRIDE {
105 const CircleEdgeEffect& circleEffect = drawEffect.castEffect<CircleEdgeEffect>();
106 const char *vsName, *fsName;
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000107 builder->addVarying(kVec4f_GrSLType, "CircleEdge", &vsName, &fsName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000108
109 const SkString* attrName =
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000110 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]);
111 builder->vsCodeAppendf("\t%s = %s;\n", vsName, attrName->c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000112
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000113 builder->fsCodeAppendf("\tfloat d = length(%s.xy);\n", fsName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000114 builder->fsCodeAppendf("\tfloat edgeAlpha = clamp(%s.z - d, 0.0, 1.0);\n", fsName);
115 if (circleEffect.isStroked()) {
116 builder->fsCodeAppendf("\tfloat innerAlpha = clamp(d - %s.w, 0.0, 1.0);\n", fsName);
117 builder->fsCodeAppend("\tedgeAlpha *= innerAlpha;\n");
118 }
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000119
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000120 builder->fsCodeAppendf("\t%s = %s;\n", outputColor,
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000121 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("edgeAlpha")).c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000122 }
123
124 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
125 const CircleEdgeEffect& circleEffect = drawEffect.castEffect<CircleEdgeEffect>();
126
127 return circleEffect.isStroked() ? 0x1 : 0x0;
128 }
129
130 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {}
131
132 private:
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000133 typedef GrGLVertexEffect INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000134 };
135
136
137private:
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000138 CircleEdgeEffect(bool stroke) : GrVertexEffect() {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000139 this->addVertexAttrib(kVec4f_GrSLType);
140 fStroke = stroke;
141 }
142
143 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
144 const CircleEdgeEffect& cee = CastEffect<CircleEdgeEffect>(other);
145 return cee.fStroke == fStroke;
146 }
147
148 bool fStroke;
149
150 GR_DECLARE_EFFECT_TEST;
151
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000152 typedef GrVertexEffect INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000153};
154
155GR_DEFINE_EFFECT_TEST(CircleEdgeEffect);
156
bsalomon83d081a2014-07-08 09:56:10 -0700157GrEffect* CircleEdgeEffect::TestCreate(SkRandom* random,
158 GrContext* context,
159 const GrDrawTargetCaps&,
160 GrTexture* textures[]) {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000161 return CircleEdgeEffect::Create(random->nextBool());
162}
163
164///////////////////////////////////////////////////////////////////////////////
165
166/**
167 * The output of this effect is a modulation of the input color and coverage for an axis-aligned
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +0000168 * ellipse, specified as a 2D offset from center, and the reciprocals of the outer and inner radii,
169 * in both x and y directions.
170 *
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000171 * We are using an implicit function of x^2/a^2 + y^2/b^2 - 1 = 0.
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000172 */
173
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000174class EllipseEdgeEffect : public GrVertexEffect {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000175public:
bsalomon83d081a2014-07-08 09:56:10 -0700176 static GrEffect* Create(bool stroke) {
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000177 GR_CREATE_STATIC_EFFECT(gEllipseStrokeEdge, EllipseEdgeEffect, (true));
178 GR_CREATE_STATIC_EFFECT(gEllipseFillEdge, EllipseEdgeEffect, (false));
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000179
180 if (stroke) {
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000181 gEllipseStrokeEdge->ref();
182 return gEllipseStrokeEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000183 } else {
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000184 gEllipseFillEdge->ref();
185 return gEllipseFillEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000186 }
187 }
188
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000189 virtual void getConstantColorComponents(GrColor* color,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000190 uint32_t* validFlags) const SK_OVERRIDE {
191 *validFlags = 0;
192 }
193
194 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
195 return GrTBackendEffectFactory<EllipseEdgeEffect>::getInstance();
196 }
197
198 virtual ~EllipseEdgeEffect() {}
199
200 static const char* Name() { return "EllipseEdge"; }
201
202 inline bool isStroked() const { return fStroke; }
203
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000204 class GLEffect : public GrGLVertexEffect {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000205 public:
206 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
207 : INHERITED (factory) {}
208
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000209 virtual void emitCode(GrGLFullShaderBuilder* builder,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000210 const GrDrawEffect& drawEffect,
211 EffectKey key,
212 const char* outputColor,
213 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000214 const TransformedCoordsArray&,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000215 const TextureSamplerArray& samplers) SK_OVERRIDE {
216 const EllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<EllipseEdgeEffect>();
217
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000218 const char *vsOffsetName, *fsOffsetName;
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000219 const char *vsRadiiName, *fsRadiiName;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000220
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000221 builder->addVarying(kVec2f_GrSLType, "EllipseOffsets", &vsOffsetName, &fsOffsetName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000222 const SkString* attr0Name =
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000223 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]);
224 builder->vsCodeAppendf("\t%s = %s;\n", vsOffsetName, attr0Name->c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000225
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000226 builder->addVarying(kVec4f_GrSLType, "EllipseRadii", &vsRadiiName, &fsRadiiName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000227 const SkString* attr1Name =
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000228 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[1]);
229 builder->vsCodeAppendf("\t%s = %s;\n", vsRadiiName, attr1Name->c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000230
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +0000231 // for outer curve
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000232 builder->fsCodeAppendf("\tvec2 scaledOffset = %s*%s.xy;\n", fsOffsetName, fsRadiiName);
233 builder->fsCodeAppend("\tfloat test = dot(scaledOffset, scaledOffset) - 1.0;\n");
234 builder->fsCodeAppendf("\tvec2 grad = 2.0*scaledOffset*%s.xy;\n", fsRadiiName);
jvanverth@google.comd1b5b142013-07-02 14:46:03 +0000235 builder->fsCodeAppend("\tfloat grad_dot = dot(grad, grad);\n");
commit-bot@chromium.org1b035d82014-04-09 17:11:09 +0000236 // avoid calling inversesqrt on zero.
237 builder->fsCodeAppend("\tgrad_dot = max(grad_dot, 1.0e-4);\n");
jvanverth@google.comd1b5b142013-07-02 14:46:03 +0000238 builder->fsCodeAppend("\tfloat invlen = inversesqrt(grad_dot);\n");
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000239 builder->fsCodeAppend("\tfloat edgeAlpha = clamp(0.5-test*invlen, 0.0, 1.0);\n");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000240
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000241 // for inner curve
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000242 if (ellipseEffect.isStroked()) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000243 builder->fsCodeAppendf("\tscaledOffset = %s*%s.zw;\n", fsOffsetName, fsRadiiName);
244 builder->fsCodeAppend("\ttest = dot(scaledOffset, scaledOffset) - 1.0;\n");
245 builder->fsCodeAppendf("\tgrad = 2.0*scaledOffset*%s.zw;\n", fsRadiiName);
246 builder->fsCodeAppend("\tinvlen = inversesqrt(dot(grad, grad));\n");
247 builder->fsCodeAppend("\tedgeAlpha *= clamp(0.5+test*invlen, 0.0, 1.0);\n");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000248 }
249
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000250 builder->fsCodeAppendf("\t%s = %s;\n", outputColor,
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000251 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("edgeAlpha")).c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000252 }
253
254 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
255 const EllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<EllipseEdgeEffect>();
256
257 return ellipseEffect.isStroked() ? 0x1 : 0x0;
258 }
259
260 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {
261 }
262
263 private:
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000264 typedef GrGLVertexEffect INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000265 };
266
267private:
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000268 EllipseEdgeEffect(bool stroke) : GrVertexEffect() {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000269 this->addVertexAttrib(kVec2f_GrSLType);
270 this->addVertexAttrib(kVec4f_GrSLType);
271 fStroke = stroke;
272 }
273
274 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
275 const EllipseEdgeEffect& eee = CastEffect<EllipseEdgeEffect>(other);
276 return eee.fStroke == fStroke;
277 }
278
279 bool fStroke;
280
281 GR_DECLARE_EFFECT_TEST;
282
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000283 typedef GrVertexEffect INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000284};
285
286GR_DEFINE_EFFECT_TEST(EllipseEdgeEffect);
287
bsalomon83d081a2014-07-08 09:56:10 -0700288GrEffect* EllipseEdgeEffect::TestCreate(SkRandom* random,
289 GrContext* context,
290 const GrDrawTargetCaps&,
291 GrTexture* textures[]) {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000292 return EllipseEdgeEffect::Create(random->nextBool());
293}
294
295///////////////////////////////////////////////////////////////////////////////
296
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000297/**
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000298 * 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 +0000299 * specified as a 2D offset from center for both the outer and inner paths (if stroked). The
300 * implict equation used is for a unit circle (x^2 + y^2 - 1 = 0) and the edge corrected by
301 * using differentials.
302 *
303 * The result is device-independent and can be used with any affine matrix.
304 */
305
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000306class DIEllipseEdgeEffect : public GrVertexEffect {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000307public:
308 enum Mode { kStroke = 0, kHairline, kFill };
309
bsalomon83d081a2014-07-08 09:56:10 -0700310 static GrEffect* Create(Mode mode) {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000311 GR_CREATE_STATIC_EFFECT(gEllipseStrokeEdge, DIEllipseEdgeEffect, (kStroke));
312 GR_CREATE_STATIC_EFFECT(gEllipseHairlineEdge, DIEllipseEdgeEffect, (kHairline));
313 GR_CREATE_STATIC_EFFECT(gEllipseFillEdge, DIEllipseEdgeEffect, (kFill));
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000314
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000315 if (kStroke == mode) {
316 gEllipseStrokeEdge->ref();
317 return gEllipseStrokeEdge;
318 } else if (kHairline == mode) {
319 gEllipseHairlineEdge->ref();
320 return gEllipseHairlineEdge;
321 } else {
322 gEllipseFillEdge->ref();
323 return gEllipseFillEdge;
324 }
325 }
326
327 virtual void getConstantColorComponents(GrColor* color,
328 uint32_t* validFlags) const SK_OVERRIDE {
329 *validFlags = 0;
330 }
331
332 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
333 return GrTBackendEffectFactory<DIEllipseEdgeEffect>::getInstance();
334 }
335
336 virtual ~DIEllipseEdgeEffect() {}
337
338 static const char* Name() { return "DIEllipseEdge"; }
339
340 inline Mode getMode() const { return fMode; }
341
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000342 class GLEffect : public GrGLVertexEffect {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000343 public:
344 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
345 : INHERITED (factory) {}
346
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000347 virtual void emitCode(GrGLFullShaderBuilder* builder,
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000348 const GrDrawEffect& drawEffect,
349 EffectKey key,
350 const char* outputColor,
351 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000352 const TransformedCoordsArray&,
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000353 const TextureSamplerArray& samplers) SK_OVERRIDE {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000354 const DIEllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<DIEllipseEdgeEffect>();
355
356 SkAssertResult(builder->enableFeature(
357 GrGLShaderBuilder::kStandardDerivatives_GLSLFeature));
358
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000359 const char *vsOffsetName0, *fsOffsetName0;
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000360 builder->addVarying(kVec2f_GrSLType, "EllipseOffsets0",
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000361 &vsOffsetName0, &fsOffsetName0);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000362 const SkString* attr0Name =
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000363 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]);
364 builder->vsCodeAppendf("\t%s = %s;\n", vsOffsetName0, attr0Name->c_str());
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000365 const char *vsOffsetName1, *fsOffsetName1;
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000366 builder->addVarying(kVec2f_GrSLType, "EllipseOffsets1",
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000367 &vsOffsetName1, &fsOffsetName1);
368 const SkString* attr1Name =
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000369 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[1]);
370 builder->vsCodeAppendf("\t%s = %s;\n", vsOffsetName1, attr1Name->c_str());
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000371
372 // for outer curve
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000373 builder->fsCodeAppendf("\tvec2 scaledOffset = %s.xy;\n", fsOffsetName0);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000374 builder->fsCodeAppend("\tfloat test = dot(scaledOffset, scaledOffset) - 1.0;\n");
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000375 builder->fsCodeAppendf("\tvec2 duvdx = dFdx(%s);\n", fsOffsetName0);
376 builder->fsCodeAppendf("\tvec2 duvdy = dFdy(%s);\n", fsOffsetName0);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000377 builder->fsCodeAppendf("\tvec2 grad = vec2(2.0*%s.x*duvdx.x + 2.0*%s.y*duvdx.y,\n"
378 "\t 2.0*%s.x*duvdy.x + 2.0*%s.y*duvdy.y);\n",
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000379 fsOffsetName0, fsOffsetName0, fsOffsetName0, fsOffsetName0);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000380
381 builder->fsCodeAppend("\tfloat grad_dot = dot(grad, grad);\n");
commit-bot@chromium.org1b035d82014-04-09 17:11:09 +0000382 // avoid calling inversesqrt on zero.
383 builder->fsCodeAppend("\tgrad_dot = max(grad_dot, 1.0e-4);\n");
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000384 builder->fsCodeAppend("\tfloat invlen = inversesqrt(grad_dot);\n");
385 if (kHairline == ellipseEffect.getMode()) {
386 // can probably do this with one step
387 builder->fsCodeAppend("\tfloat edgeAlpha = clamp(1.0-test*invlen, 0.0, 1.0);\n");
388 builder->fsCodeAppend("\tedgeAlpha *= clamp(1.0+test*invlen, 0.0, 1.0);\n");
389 } else {
390 builder->fsCodeAppend("\tfloat edgeAlpha = clamp(0.5-test*invlen, 0.0, 1.0);\n");
391 }
392
393 // for inner curve
394 if (kStroke == ellipseEffect.getMode()) {
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000395 builder->fsCodeAppendf("\tscaledOffset = %s.xy;\n", fsOffsetName1);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000396 builder->fsCodeAppend("\ttest = dot(scaledOffset, scaledOffset) - 1.0;\n");
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000397 builder->fsCodeAppendf("\tduvdx = dFdx(%s);\n", fsOffsetName1);
398 builder->fsCodeAppendf("\tduvdy = dFdy(%s);\n", fsOffsetName1);
399 builder->fsCodeAppendf("\tgrad = vec2(2.0*%s.x*duvdx.x + 2.0*%s.y*duvdx.y,\n"
400 "\t 2.0*%s.x*duvdy.x + 2.0*%s.y*duvdy.y);\n",
401 fsOffsetName1, fsOffsetName1, fsOffsetName1, fsOffsetName1);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000402 builder->fsCodeAppend("\tinvlen = inversesqrt(dot(grad, grad));\n");
403 builder->fsCodeAppend("\tedgeAlpha *= clamp(0.5+test*invlen, 0.0, 1.0);\n");
404 }
405
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000406 builder->fsCodeAppendf("\t%s = %s;\n", outputColor,
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000407 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("edgeAlpha")).c_str());
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000408 }
409
410 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
411 const DIEllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<DIEllipseEdgeEffect>();
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000412
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000413 return ellipseEffect.getMode();
414 }
415
416 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {
417 }
418
419 private:
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000420 typedef GrGLVertexEffect INHERITED;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000421 };
422
423private:
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000424 DIEllipseEdgeEffect(Mode mode) : GrVertexEffect() {
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000425 this->addVertexAttrib(kVec2f_GrSLType);
426 this->addVertexAttrib(kVec2f_GrSLType);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000427 fMode = mode;
428 }
429
430 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
431 const DIEllipseEdgeEffect& eee = CastEffect<DIEllipseEdgeEffect>(other);
432 return eee.fMode == fMode;
433 }
434
435 Mode fMode;
436
437 GR_DECLARE_EFFECT_TEST;
438
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000439 typedef GrVertexEffect INHERITED;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000440};
441
442GR_DEFINE_EFFECT_TEST(DIEllipseEdgeEffect);
443
bsalomon83d081a2014-07-08 09:56:10 -0700444GrEffect* DIEllipseEdgeEffect::TestCreate(SkRandom* random,
445 GrContext* context,
446 const GrDrawTargetCaps&,
447 GrTexture* textures[]) {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000448 return DIEllipseEdgeEffect::Create((Mode)(random->nextRangeU(0,2)));
449}
450
451///////////////////////////////////////////////////////////////////////////////
452
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000453void GrOvalRenderer::reset() {
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000454 SkSafeSetNull(fRRectIndexBuffer);
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000455}
456
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000457bool GrOvalRenderer::drawOval(GrDrawTarget* target, const GrContext* context, bool useAA,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000458 const SkRect& oval, const SkStrokeRec& stroke)
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000459{
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000460 bool useCoverageAA = useAA &&
461 !target->getDrawState().getRenderTarget()->isMultisampled() &&
462 !target->shouldDisableCoverageAAForBlend();
463
464 if (!useCoverageAA) {
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000465 return false;
466 }
467
468 const SkMatrix& vm = context->getMatrix();
469
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000470 // we can draw circles
471 if (SkScalarNearlyEqual(oval.width(), oval.height())
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000472 && circle_stays_circle(vm)) {
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000473 this->drawCircle(target, useCoverageAA, oval, stroke);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000474 // if we have shader derivative support, render as device-independent
475 } else if (target->caps()->shaderDerivativeSupport()) {
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000476 return this->drawDIEllipse(target, useCoverageAA, oval, stroke);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000477 // otherwise axis-aligned ellipses only
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000478 } else if (vm.rectStaysRect()) {
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000479 return this->drawEllipse(target, useCoverageAA, oval, stroke);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000480 } else {
481 return false;
482 }
483
484 return true;
485}
486
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000487///////////////////////////////////////////////////////////////////////////////
488
robertphillips@google.com42903302013-04-20 12:26:07 +0000489// position + edge
490extern const GrVertexAttrib gCircleVertexAttribs[] = {
491 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000492 {kVec4f_GrVertexAttribType, sizeof(SkPoint), kEffect_GrVertexAttribBinding}
robertphillips@google.com42903302013-04-20 12:26:07 +0000493};
494
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000495void GrOvalRenderer::drawCircle(GrDrawTarget* target,
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000496 bool useCoverageAA,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000497 const SkRect& circle,
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000498 const SkStrokeRec& stroke)
499{
500 GrDrawState* drawState = target->drawState();
501
502 const SkMatrix& vm = drawState->getViewMatrix();
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000503 SkPoint center = SkPoint::Make(circle.centerX(), circle.centerY());
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000504 vm.mapPoints(&center, 1);
505 SkScalar radius = vm.mapRadius(SkScalarHalf(circle.width()));
506 SkScalar strokeWidth = vm.mapRadius(stroke.getWidth());
507
bsalomon@google.com137f1342013-05-29 21:27:53 +0000508 GrDrawState::AutoViewMatrixRestore avmr;
509 if (!avmr.setIdentity(drawState)) {
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000510 return;
511 }
512
robertphillips@google.com42903302013-04-20 12:26:07 +0000513 drawState->setVertexAttribs<gCircleVertexAttribs>(SK_ARRAY_COUNT(gCircleVertexAttribs));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000514 SkASSERT(sizeof(CircleVertex) == drawState->getVertexSize());
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000515
516 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
517 if (!geo.succeeded()) {
518 GrPrintf("Failed to get space for vertices!\n");
519 return;
520 }
521
522 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
523
524 SkStrokeRec::Style style = stroke.getStyle();
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000525 bool isStrokeOnly = SkStrokeRec::kStroke_Style == style ||
526 SkStrokeRec::kHairline_Style == style;
527 bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == style;
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000528
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000529 SkScalar innerRadius = 0.0f;
530 SkScalar outerRadius = radius;
531 SkScalar halfWidth = 0;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000532 if (hasStroke) {
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000533 if (SkScalarNearlyZero(strokeWidth)) {
534 halfWidth = SK_ScalarHalf;
535 } else {
536 halfWidth = SkScalarHalf(strokeWidth);
537 }
538
539 outerRadius += halfWidth;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000540 if (isStrokeOnly) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000541 innerRadius = radius - halfWidth;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000542 }
543 }
544
bsalomon83d081a2014-07-08 09:56:10 -0700545 GrEffect* effect = CircleEdgeEffect::Create(isStrokeOnly && innerRadius > 0);
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +0000546 static const int kCircleEdgeAttrIndex = 1;
547 drawState->addCoverageEffect(effect, kCircleEdgeAttrIndex)->unref();
548
bsalomon@google.com58e30fe2013-04-01 19:01:20 +0000549 // The radii are outset for two reasons. First, it allows the shader to simply perform
550 // clamp(distance-to-center - radius, 0, 1). Second, the outer radius is used to compute the
551 // verts of the bounding box that is rendered and the outset ensures the box will cover all
552 // pixels partially covered by the circle.
553 outerRadius += SK_ScalarHalf;
554 innerRadius -= SK_ScalarHalf;
555
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000556 SkRect bounds = SkRect::MakeLTRB(
bsalomon@google.com58e30fe2013-04-01 19:01:20 +0000557 center.fX - outerRadius,
558 center.fY - outerRadius,
559 center.fX + outerRadius,
560 center.fY + outerRadius
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000561 );
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000562
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000563 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000564 verts[0].fOffset = SkPoint::Make(-outerRadius, -outerRadius);
565 verts[0].fOuterRadius = outerRadius;
566 verts[0].fInnerRadius = innerRadius;
567
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000568 verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
skia.committer@gmail.com46746762013-04-12 07:01:51 +0000569 verts[1].fOffset = SkPoint::Make(outerRadius, -outerRadius);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000570 verts[1].fOuterRadius = outerRadius;
571 verts[1].fInnerRadius = innerRadius;
572
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000573 verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000574 verts[2].fOffset = SkPoint::Make(-outerRadius, outerRadius);
575 verts[2].fOuterRadius = outerRadius;
576 verts[2].fInnerRadius = innerRadius;
577
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000578 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000579 verts[3].fOffset = SkPoint::Make(outerRadius, outerRadius);
580 verts[3].fOuterRadius = outerRadius;
581 verts[3].fInnerRadius = innerRadius;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000582
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000583 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000584}
585
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000586///////////////////////////////////////////////////////////////////////////////
587
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000588// position + offset + 1/radii
robertphillips@google.com42903302013-04-20 12:26:07 +0000589extern const GrVertexAttrib gEllipseVertexAttribs[] = {
590 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000591 {kVec2f_GrVertexAttribType, sizeof(SkPoint), kEffect_GrVertexAttribBinding},
592 {kVec4f_GrVertexAttribType, 2*sizeof(SkPoint), kEffect_GrVertexAttribBinding}
robertphillips@google.com42903302013-04-20 12:26:07 +0000593};
594
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000595// position + offsets
596extern const GrVertexAttrib gDIEllipseVertexAttribs[] = {
597 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000598 {kVec2f_GrVertexAttribType, sizeof(SkPoint), kEffect_GrVertexAttribBinding},
599 {kVec2f_GrVertexAttribType, 2*sizeof(SkPoint), kEffect_GrVertexAttribBinding},
robertphillips@google.com42903302013-04-20 12:26:07 +0000600};
601
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000602bool GrOvalRenderer::drawEllipse(GrDrawTarget* target,
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000603 bool useCoverageAA,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000604 const SkRect& ellipse,
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000605 const SkStrokeRec& stroke)
606{
607 GrDrawState* drawState = target->drawState();
608#ifdef SK_DEBUG
609 {
610 // we should have checked for this previously
611 bool isAxisAlignedEllipse = drawState->getViewMatrix().rectStaysRect();
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000612 SkASSERT(useCoverageAA && isAxisAlignedEllipse);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000613 }
614#endif
615
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000616 // do any matrix crunching before we reset the draw state for device coords
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000617 const SkMatrix& vm = drawState->getViewMatrix();
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000618 SkPoint center = SkPoint::Make(ellipse.centerX(), ellipse.centerY());
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000619 vm.mapPoints(&center, 1);
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000620 SkScalar ellipseXRadius = SkScalarHalf(ellipse.width());
621 SkScalar ellipseYRadius = SkScalarHalf(ellipse.height());
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +0000622 SkScalar xRadius = SkScalarAbs(vm[SkMatrix::kMScaleX]*ellipseXRadius +
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000623 vm[SkMatrix::kMSkewY]*ellipseYRadius);
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +0000624 SkScalar yRadius = SkScalarAbs(vm[SkMatrix::kMSkewX]*ellipseXRadius +
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000625 vm[SkMatrix::kMScaleY]*ellipseYRadius);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000626
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000627 // do (potentially) anisotropic mapping of stroke
628 SkVector scaledStroke;
629 SkScalar strokeWidth = stroke.getWidth();
630 scaledStroke.fX = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMScaleX] + vm[SkMatrix::kMSkewY]));
631 scaledStroke.fY = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMSkewX] + vm[SkMatrix::kMScaleY]));
632
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000633 SkStrokeRec::Style style = stroke.getStyle();
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000634 bool isStrokeOnly = SkStrokeRec::kStroke_Style == style ||
635 SkStrokeRec::kHairline_Style == style;
636 bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == style;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000637
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000638 SkScalar innerXRadius = 0;
639 SkScalar innerYRadius = 0;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000640 if (hasStroke) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000641 if (SkScalarNearlyZero(scaledStroke.length())) {
642 scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf);
643 } else {
644 scaledStroke.scale(SK_ScalarHalf);
645 }
646
647 // we only handle thick strokes for near-circular ellipses
648 if (scaledStroke.length() > SK_ScalarHalf &&
649 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
650 return false;
651 }
652
653 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
654 if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius ||
655 scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) {
656 return false;
657 }
658
659 // this is legit only if scale & translation (which should be the case at the moment)
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000660 if (isStrokeOnly) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000661 innerXRadius = xRadius - scaledStroke.fX;
662 innerYRadius = yRadius - scaledStroke.fY;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000663 }
664
665 xRadius += scaledStroke.fX;
666 yRadius += scaledStroke.fY;
667 }
668
bsalomon@google.com137f1342013-05-29 21:27:53 +0000669 GrDrawState::AutoViewMatrixRestore avmr;
670 if (!avmr.setIdentity(drawState)) {
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000671 return false;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000672 }
673
robertphillips@google.com42903302013-04-20 12:26:07 +0000674 drawState->setVertexAttribs<gEllipseVertexAttribs>(SK_ARRAY_COUNT(gEllipseVertexAttribs));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000675 SkASSERT(sizeof(EllipseVertex) == drawState->getVertexSize());
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000676
677 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
678 if (!geo.succeeded()) {
679 GrPrintf("Failed to get space for vertices!\n");
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000680 return false;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000681 }
682
683 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices());
684
bsalomon83d081a2014-07-08 09:56:10 -0700685 GrEffect* effect = EllipseEdgeEffect::Create(isStrokeOnly &&
686 innerXRadius > 0 && innerYRadius > 0);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000687
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000688 static const int kEllipseCenterAttrIndex = 1;
jvanverth@google.com6cc8d442013-09-10 18:24:37 +0000689 static const int kEllipseEdgeAttrIndex = 2;
690 drawState->addCoverageEffect(effect, kEllipseCenterAttrIndex, kEllipseEdgeAttrIndex)->unref();
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000691
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000692 // Compute the reciprocals of the radii here to save time in the shader
693 SkScalar xRadRecip = SkScalarInvert(xRadius);
694 SkScalar yRadRecip = SkScalarInvert(yRadius);
695 SkScalar xInnerRadRecip = SkScalarInvert(innerXRadius);
696 SkScalar yInnerRadRecip = SkScalarInvert(innerYRadius);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000697
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000698 // We've extended the outer x radius out half a pixel to antialias.
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000699 // This will also expand the rect so all the pixels will be captured.
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000700 // TODO: Consider if we should use sqrt(2)/2 instead
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000701 xRadius += SK_ScalarHalf;
702 yRadius += SK_ScalarHalf;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000703
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000704 SkRect bounds = SkRect::MakeLTRB(
705 center.fX - xRadius,
706 center.fY - yRadius,
707 center.fX + xRadius,
708 center.fY + yRadius
709 );
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000710
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000711 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000712 verts[0].fOffset = SkPoint::Make(-xRadius, -yRadius);
713 verts[0].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
714 verts[0].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000715
716 verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000717 verts[1].fOffset = SkPoint::Make(xRadius, -yRadius);
718 verts[1].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
719 verts[1].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000720
721 verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000722 verts[2].fOffset = SkPoint::Make(-xRadius, yRadius);
723 verts[2].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
724 verts[2].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000725
726 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000727 verts[3].fOffset = SkPoint::Make(xRadius, yRadius);
728 verts[3].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
729 verts[3].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
skia.committer@gmail.com46746762013-04-12 07:01:51 +0000730
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000731 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds);
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000732
733 return true;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000734}
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000735
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000736bool GrOvalRenderer::drawDIEllipse(GrDrawTarget* target,
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000737 bool useCoverageAA,
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000738 const SkRect& ellipse,
739 const SkStrokeRec& stroke)
740{
741 GrDrawState* drawState = target->drawState();
742 const SkMatrix& vm = drawState->getViewMatrix();
743
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000744 SkPoint center = SkPoint::Make(ellipse.centerX(), ellipse.centerY());
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000745 SkScalar xRadius = SkScalarHalf(ellipse.width());
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000746 SkScalar yRadius = SkScalarHalf(ellipse.height());
747
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000748 SkStrokeRec::Style style = stroke.getStyle();
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000749 DIEllipseEdgeEffect::Mode mode = (SkStrokeRec::kStroke_Style == style) ?
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000750 DIEllipseEdgeEffect::kStroke :
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000751 (SkStrokeRec::kHairline_Style == style) ?
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000752 DIEllipseEdgeEffect::kHairline : DIEllipseEdgeEffect::kFill;
753
754 SkScalar innerXRadius = 0;
755 SkScalar innerYRadius = 0;
756 if (SkStrokeRec::kFill_Style != style && SkStrokeRec::kHairline_Style != style) {
757 SkScalar strokeWidth = stroke.getWidth();
758
759 if (SkScalarNearlyZero(strokeWidth)) {
760 strokeWidth = SK_ScalarHalf;
761 } else {
762 strokeWidth *= SK_ScalarHalf;
763 }
764
765 // we only handle thick strokes for near-circular ellipses
766 if (strokeWidth > SK_ScalarHalf &&
767 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
768 return false;
769 }
770
771 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
772 if (strokeWidth*(yRadius*yRadius) < (strokeWidth*strokeWidth)*xRadius ||
773 strokeWidth*(xRadius*xRadius) < (strokeWidth*strokeWidth)*yRadius) {
774 return false;
775 }
776
777 // set inner radius (if needed)
778 if (SkStrokeRec::kStroke_Style == style) {
779 innerXRadius = xRadius - strokeWidth;
780 innerYRadius = yRadius - strokeWidth;
781 }
782
783 xRadius += strokeWidth;
784 yRadius += strokeWidth;
785 }
786 if (DIEllipseEdgeEffect::kStroke == mode) {
787 mode = (innerXRadius > 0 && innerYRadius > 0) ? DIEllipseEdgeEffect::kStroke :
788 DIEllipseEdgeEffect::kFill;
789 }
790 SkScalar innerRatioX = SkScalarDiv(xRadius, innerXRadius);
791 SkScalar innerRatioY = SkScalarDiv(yRadius, innerYRadius);
792
793 drawState->setVertexAttribs<gDIEllipseVertexAttribs>(SK_ARRAY_COUNT(gDIEllipseVertexAttribs));
794 SkASSERT(sizeof(DIEllipseVertex) == drawState->getVertexSize());
795
796 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
797 if (!geo.succeeded()) {
798 GrPrintf("Failed to get space for vertices!\n");
799 return false;
800 }
801
802 DIEllipseVertex* verts = reinterpret_cast<DIEllipseVertex*>(geo.vertices());
803
bsalomon83d081a2014-07-08 09:56:10 -0700804 GrEffect* effect = DIEllipseEdgeEffect::Create(mode);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000805
806 static const int kEllipseOuterOffsetAttrIndex = 1;
807 static const int kEllipseInnerOffsetAttrIndex = 2;
808 drawState->addCoverageEffect(effect, kEllipseOuterOffsetAttrIndex,
809 kEllipseInnerOffsetAttrIndex)->unref();
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000810
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000811 // This expands the outer rect so that after CTM we end up with a half-pixel border
812 SkScalar a = vm[SkMatrix::kMScaleX];
813 SkScalar b = vm[SkMatrix::kMSkewX];
814 SkScalar c = vm[SkMatrix::kMSkewY];
815 SkScalar d = vm[SkMatrix::kMScaleY];
816 SkScalar geoDx = SkScalarDiv(SK_ScalarHalf, SkScalarSqrt(a*a + c*c));
817 SkScalar geoDy = SkScalarDiv(SK_ScalarHalf, SkScalarSqrt(b*b + d*d));
818 // This adjusts the "radius" to include the half-pixel border
819 SkScalar offsetDx = SkScalarDiv(geoDx, xRadius);
820 SkScalar offsetDy = SkScalarDiv(geoDy, yRadius);
821
822 SkRect bounds = SkRect::MakeLTRB(
823 center.fX - xRadius - geoDx,
824 center.fY - yRadius - geoDy,
825 center.fX + xRadius + geoDx,
826 center.fY + yRadius + geoDy
827 );
828
829 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
830 verts[0].fOuterOffset = SkPoint::Make(-1.0f - offsetDx, -1.0f - offsetDy);
831 verts[0].fInnerOffset = SkPoint::Make(-innerRatioX - offsetDx, -innerRatioY - offsetDy);
832
833 verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
834 verts[1].fOuterOffset = SkPoint::Make(1.0f + offsetDx, -1.0f - offsetDy);
835 verts[1].fInnerOffset = SkPoint::Make(innerRatioX + offsetDx, -innerRatioY - offsetDy);
836
837 verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
838 verts[2].fOuterOffset = SkPoint::Make(-1.0f - offsetDx, 1.0f + offsetDy);
839 verts[2].fInnerOffset = SkPoint::Make(-innerRatioX - offsetDx, innerRatioY + offsetDy);
840
841 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
842 verts[3].fOuterOffset = SkPoint::Make(1.0f + offsetDx, 1.0f + offsetDy);
843 verts[3].fInnerOffset = SkPoint::Make(innerRatioX + offsetDx, innerRatioY + offsetDy);
844
845 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds);
846
847 return true;
848}
849
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000850///////////////////////////////////////////////////////////////////////////////
851
852static const uint16_t gRRectIndices[] = {
853 // corners
854 0, 1, 5, 0, 5, 4,
855 2, 3, 7, 2, 7, 6,
856 8, 9, 13, 8, 13, 12,
857 10, 11, 15, 10, 15, 14,
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000858
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000859 // edges
860 1, 2, 6, 1, 6, 5,
861 4, 5, 9, 4, 9, 8,
862 6, 7, 11, 6, 11, 10,
863 9, 10, 14, 9, 14, 13,
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000864
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000865 // center
866 // we place this at the end so that we can ignore these indices when rendering stroke-only
867 5, 6, 10, 5, 10, 9
868};
869
870
871GrIndexBuffer* GrOvalRenderer::rRectIndexBuffer(GrGpu* gpu) {
872 if (NULL == fRRectIndexBuffer) {
873 fRRectIndexBuffer =
874 gpu->createIndexBuffer(sizeof(gRRectIndices), false);
875 if (NULL != fRRectIndexBuffer) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000876#ifdef SK_DEBUG
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000877 bool updated =
878#endif
879 fRRectIndexBuffer->updateData(gRRectIndices,
880 sizeof(gRRectIndices));
881 GR_DEBUGASSERT(updated);
882 }
883 }
884 return fRRectIndexBuffer;
885}
886
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000887bool GrOvalRenderer::drawDRRect(GrDrawTarget* target, GrContext* context, bool useAA,
bsalomon8af05232014-06-03 06:34:58 -0700888 const SkRRect& origOuter, const SkRRect& origInner) {
889 bool applyAA = useAA &&
890 !target->getDrawState().getRenderTarget()->isMultisampled() &&
891 !target->shouldDisableCoverageAAForBlend();
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000892 GrDrawState::AutoRestoreEffects are;
893 if (!origInner.isEmpty()) {
894 SkTCopyOnFirstWrite<SkRRect> inner(origInner);
895 if (!context->getMatrix().isIdentity()) {
896 if (!origInner.transform(context->getMatrix(), inner.writable())) {
897 return false;
898 }
899 }
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000900 GrEffectEdgeType edgeType = applyAA ? kInverseFillAA_GrEffectEdgeType :
901 kInverseFillBW_GrEffectEdgeType;
bsalomon83d081a2014-07-08 09:56:10 -0700902 GrEffect* effect = GrRRectEffect::Create(edgeType, *inner);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000903 if (NULL == effect) {
904 return false;
905 }
906 are.set(target->drawState());
907 target->drawState()->addCoverageEffect(effect)->unref();
908 }
909
910 SkStrokeRec fillRec(SkStrokeRec::kFill_InitStyle);
bsalomon8af05232014-06-03 06:34:58 -0700911 if (this->drawRRect(target, context, useAA, origOuter, fillRec)) {
912 return true;
913 }
914
915 SkASSERT(!origOuter.isEmpty());
916 SkTCopyOnFirstWrite<SkRRect> outer(origOuter);
917 if (!context->getMatrix().isIdentity()) {
918 if (!origOuter.transform(context->getMatrix(), outer.writable())) {
919 return false;
920 }
921 }
922 GrEffectEdgeType edgeType = applyAA ? kFillAA_GrEffectEdgeType :
923 kFillBW_GrEffectEdgeType;
bsalomon83d081a2014-07-08 09:56:10 -0700924 GrEffect* effect = GrRRectEffect::Create(edgeType, *outer);
bsalomon8af05232014-06-03 06:34:58 -0700925 if (NULL == effect) {
926 return false;
927 }
928 if (!are.isSet()) {
929 are.set(target->drawState());
930 }
931 GrDrawState::AutoViewMatrixRestore avmr;
932 if (!avmr.setIdentity(target->drawState())) {
933 return false;
934 }
935 target->drawState()->addCoverageEffect(effect)->unref();
936 SkRect bounds = outer->getBounds();
937 if (applyAA) {
938 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
939 }
940 target->drawRect(bounds, NULL, NULL, NULL);
941 return true;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000942}
943
944bool GrOvalRenderer::drawRRect(GrDrawTarget* target, GrContext* context, bool useAA,
945 const SkRRect& rrect, const SkStrokeRec& stroke) {
946 if (rrect.isOval()) {
947 return this->drawOval(target, context, useAA, rrect.getBounds(), stroke);
948 }
949
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000950 bool useCoverageAA = useAA &&
951 !target->getDrawState().getRenderTarget()->isMultisampled() &&
952 !target->shouldDisableCoverageAAForBlend();
953
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000954 // only anti-aliased rrects for now
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000955 if (!useCoverageAA) {
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000956 return false;
957 }
958
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000959 const SkMatrix& vm = context->getMatrix();
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000960
961 if (!vm.rectStaysRect() || !rrect.isSimple()) {
962 return false;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000963 }
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000964
965 // do any matrix crunching before we reset the draw state for device coords
966 const SkRect& rrectBounds = rrect.getBounds();
967 SkRect bounds;
968 vm.mapRect(&bounds, rrectBounds);
969
970 SkVector radii = rrect.getSimpleRadii();
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000971 SkScalar xRadius = SkScalarAbs(vm[SkMatrix::kMScaleX]*radii.fX +
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000972 vm[SkMatrix::kMSkewY]*radii.fY);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000973 SkScalar yRadius = SkScalarAbs(vm[SkMatrix::kMSkewX]*radii.fX +
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000974 vm[SkMatrix::kMScaleY]*radii.fY);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000975
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000976 SkStrokeRec::Style style = stroke.getStyle();
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000977
978 // do (potentially) anisotropic mapping of stroke
979 SkVector scaledStroke;
980 SkScalar strokeWidth = stroke.getWidth();
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000981
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000982 bool isStrokeOnly = SkStrokeRec::kStroke_Style == style ||
983 SkStrokeRec::kHairline_Style == style;
984 bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == style;
985
986 if (hasStroke) {
987 if (SkStrokeRec::kHairline_Style == style) {
988 scaledStroke.set(1, 1);
989 } else {
990 scaledStroke.fX = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMScaleX] +
991 vm[SkMatrix::kMSkewY]));
992 scaledStroke.fY = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMSkewX] +
993 vm[SkMatrix::kMScaleY]));
994 }
995
996 // if half of strokewidth is greater than radius, we don't handle that right now
997 if (SK_ScalarHalf*scaledStroke.fX > xRadius || SK_ScalarHalf*scaledStroke.fY > yRadius) {
998 return false;
999 }
1000 }
1001
1002 // The way the effect interpolates the offset-to-ellipse/circle-center attribute only works on
1003 // the interior of the rrect if the radii are >= 0.5. Otherwise, the inner rect of the nine-
1004 // patch will have fractional coverage. This only matters when the interior is actually filled.
1005 // We could consider falling back to rect rendering here, since a tiny radius is
1006 // indistinguishable from a square corner.
1007 if (!isStrokeOnly && (SK_ScalarHalf > xRadius || SK_ScalarHalf > yRadius)) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001008 return false;
1009 }
1010
1011 // reset to device coordinates
1012 GrDrawState* drawState = target->drawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +00001013 GrDrawState::AutoViewMatrixRestore avmr;
1014 if (!avmr.setIdentity(drawState)) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001015 return false;
1016 }
1017
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001018 GrIndexBuffer* indexBuffer = this->rRectIndexBuffer(context->getGpu());
1019 if (NULL == indexBuffer) {
1020 GrPrintf("Failed to create index buffer!\n");
1021 return false;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001022 }
1023
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001024 // if the corners are circles, use the circle renderer
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001025 if ((!hasStroke || scaledStroke.fX == scaledStroke.fY) && xRadius == yRadius) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001026 drawState->setVertexAttribs<gCircleVertexAttribs>(SK_ARRAY_COUNT(gCircleVertexAttribs));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001027 SkASSERT(sizeof(CircleVertex) == drawState->getVertexSize());
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001028
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001029 GrDrawTarget::AutoReleaseGeometry geo(target, 16, 0);
1030 if (!geo.succeeded()) {
1031 GrPrintf("Failed to get space for vertices!\n");
1032 return false;
1033 }
1034 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001035
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001036 SkScalar innerRadius = 0.0f;
1037 SkScalar outerRadius = xRadius;
1038 SkScalar halfWidth = 0;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001039 if (hasStroke) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001040 if (SkScalarNearlyZero(scaledStroke.fX)) {
1041 halfWidth = SK_ScalarHalf;
1042 } else {
1043 halfWidth = SkScalarHalf(scaledStroke.fX);
1044 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001045
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001046 if (isStrokeOnly) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001047 innerRadius = xRadius - halfWidth;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001048 }
1049 outerRadius += halfWidth;
1050 bounds.outset(halfWidth, halfWidth);
1051 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001052
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001053 isStrokeOnly = (isStrokeOnly && innerRadius >= 0);
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +00001054
bsalomon83d081a2014-07-08 09:56:10 -07001055 GrEffect* effect = CircleEdgeEffect::Create(isStrokeOnly);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001056 static const int kCircleEdgeAttrIndex = 1;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001057 drawState->addCoverageEffect(effect, kCircleEdgeAttrIndex)->unref();
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +00001058
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001059 // The radii are outset for two reasons. First, it allows the shader to simply perform
1060 // clamp(distance-to-center - radius, 0, 1). Second, the outer radius is used to compute the
1061 // verts of the bounding box that is rendered and the outset ensures the box will cover all
1062 // pixels partially covered by the circle.
1063 outerRadius += SK_ScalarHalf;
1064 innerRadius -= SK_ScalarHalf;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001065
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001066 // Expand the rect so all the pixels will be captured.
1067 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001068
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001069 SkScalar yCoords[4] = {
1070 bounds.fTop,
1071 bounds.fTop + outerRadius,
1072 bounds.fBottom - outerRadius,
1073 bounds.fBottom
1074 };
1075 SkScalar yOuterRadii[4] = {
1076 -outerRadius,
1077 0,
1078 0,
1079 outerRadius
1080 };
1081 for (int i = 0; i < 4; ++i) {
1082 verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]);
1083 verts->fOffset = SkPoint::Make(-outerRadius, yOuterRadii[i]);
1084 verts->fOuterRadius = outerRadius;
1085 verts->fInnerRadius = innerRadius;
1086 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001087
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001088 verts->fPos = SkPoint::Make(bounds.fLeft + outerRadius, yCoords[i]);
1089 verts->fOffset = SkPoint::Make(0, yOuterRadii[i]);
1090 verts->fOuterRadius = outerRadius;
1091 verts->fInnerRadius = innerRadius;
1092 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001093
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001094 verts->fPos = SkPoint::Make(bounds.fRight - outerRadius, yCoords[i]);
1095 verts->fOffset = SkPoint::Make(0, yOuterRadii[i]);
1096 verts->fOuterRadius = outerRadius;
1097 verts->fInnerRadius = innerRadius;
1098 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001099
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001100 verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]);
1101 verts->fOffset = SkPoint::Make(outerRadius, yOuterRadii[i]);
1102 verts->fOuterRadius = outerRadius;
1103 verts->fInnerRadius = innerRadius;
1104 verts++;
1105 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001106
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001107 // drop out the middle quad if we're stroked
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001108 int indexCnt = isStrokeOnly ? SK_ARRAY_COUNT(gRRectIndices) - 6 :
1109 SK_ARRAY_COUNT(gRRectIndices);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001110 target->setIndexSourceToBuffer(indexBuffer);
1111 target->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, 16, indexCnt, &bounds);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001112
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001113 // otherwise we use the ellipse renderer
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001114 } else {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001115 drawState->setVertexAttribs<gEllipseVertexAttribs>(SK_ARRAY_COUNT(gEllipseVertexAttribs));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001116 SkASSERT(sizeof(EllipseVertex) == drawState->getVertexSize());
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001117
1118 SkScalar innerXRadius = 0.0f;
1119 SkScalar innerYRadius = 0.0f;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001120 if (hasStroke) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001121 if (SkScalarNearlyZero(scaledStroke.length())) {
1122 scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf);
1123 } else {
1124 scaledStroke.scale(SK_ScalarHalf);
1125 }
1126
1127 // we only handle thick strokes for near-circular ellipses
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +00001128 if (scaledStroke.length() > SK_ScalarHalf &&
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001129 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
1130 return false;
1131 }
1132
1133 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
1134 if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius ||
1135 scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) {
1136 return false;
1137 }
1138
1139 // this is legit only if scale & translation (which should be the case at the moment)
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001140 if (isStrokeOnly) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001141 innerXRadius = xRadius - scaledStroke.fX;
1142 innerYRadius = yRadius - scaledStroke.fY;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001143 }
1144
1145 xRadius += scaledStroke.fX;
1146 yRadius += scaledStroke.fY;
1147 bounds.outset(scaledStroke.fX, scaledStroke.fY);
1148 }
jvanverth@google.come3647412013-05-08 15:31:43 +00001149
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001150 isStrokeOnly = (isStrokeOnly && innerXRadius >= 0 && innerYRadius >= 0);
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +00001151
jvanverth@google.come3647412013-05-08 15:31:43 +00001152 GrDrawTarget::AutoReleaseGeometry geo(target, 16, 0);
1153 if (!geo.succeeded()) {
1154 GrPrintf("Failed to get space for vertices!\n");
1155 return false;
1156 }
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001157 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices());
jvanverth@google.come3647412013-05-08 15:31:43 +00001158
bsalomon83d081a2014-07-08 09:56:10 -07001159 GrEffect* effect = EllipseEdgeEffect::Create(isStrokeOnly);
jvanverth@google.come3647412013-05-08 15:31:43 +00001160 static const int kEllipseOffsetAttrIndex = 1;
1161 static const int kEllipseRadiiAttrIndex = 2;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001162 drawState->addCoverageEffect(effect,
1163 kEllipseOffsetAttrIndex, kEllipseRadiiAttrIndex)->unref();
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001164
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001165 // Compute the reciprocals of the radii here to save time in the shader
1166 SkScalar xRadRecip = SkScalarInvert(xRadius);
1167 SkScalar yRadRecip = SkScalarInvert(yRadius);
1168 SkScalar xInnerRadRecip = SkScalarInvert(innerXRadius);
1169 SkScalar yInnerRadRecip = SkScalarInvert(innerYRadius);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001170
1171 // Extend the radii out half a pixel to antialias.
1172 SkScalar xOuterRadius = xRadius + SK_ScalarHalf;
1173 SkScalar yOuterRadius = yRadius + SK_ScalarHalf;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001174
1175 // Expand the rect so all the pixels will be captured.
1176 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1177
1178 SkScalar yCoords[4] = {
1179 bounds.fTop,
1180 bounds.fTop + yOuterRadius,
1181 bounds.fBottom - yOuterRadius,
1182 bounds.fBottom
1183 };
1184 SkScalar yOuterOffsets[4] = {
jvanverth@google.comd1b5b142013-07-02 14:46:03 +00001185 yOuterRadius,
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001186 SK_ScalarNearlyZero, // we're using inversesqrt() in the shader, so can't be exactly 0
1187 SK_ScalarNearlyZero,
1188 yOuterRadius
1189 };
1190
1191 for (int i = 0; i < 4; ++i) {
1192 verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]);
jvanverth@google.comd1b5b142013-07-02 14:46:03 +00001193 verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001194 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1195 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001196 verts++;
1197
1198 verts->fPos = SkPoint::Make(bounds.fLeft + xOuterRadius, yCoords[i]);
1199 verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001200 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1201 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001202 verts++;
1203
1204 verts->fPos = SkPoint::Make(bounds.fRight - xOuterRadius, yCoords[i]);
1205 verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001206 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1207 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001208 verts++;
1209
1210 verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]);
1211 verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001212 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1213 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001214 verts++;
1215 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001216
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001217 // drop out the middle quad if we're stroked
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001218 int indexCnt = isStrokeOnly ? SK_ARRAY_COUNT(gRRectIndices) - 6 :
1219 SK_ARRAY_COUNT(gRRectIndices);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001220 target->setIndexSourceToBuffer(indexBuffer);
1221 target->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, 16, indexCnt, &bounds);
1222 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001223
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001224 return true;
1225}