blob: b604f021578b0de99268ad219b9bbae581056911 [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,
bsalomon63e99f72014-07-21 08:03:14 -0700100 const GrEffectKey& key,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000101 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
bsalomon63e99f72014-07-21 08:03:14 -0700124 static void GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&,
125 GrEffectKeyBuilder* b) {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000126 const CircleEdgeEffect& circleEffect = drawEffect.castEffect<CircleEdgeEffect>();
bsalomon63e99f72014-07-21 08:03:14 -0700127 b->add32(circleEffect.isStroked());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000128 }
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,
bsalomon63e99f72014-07-21 08:03:14 -0700211 const GrEffectKey& key,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000212 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
bsalomon63e99f72014-07-21 08:03:14 -0700254 static void GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&,
255 GrEffectKeyBuilder* b) {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000256 const EllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<EllipseEdgeEffect>();
bsalomon63e99f72014-07-21 08:03:14 -0700257 b->add32(ellipseEffect.isStroked());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000258 }
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,
bsalomon63e99f72014-07-21 08:03:14 -0700349 const GrEffectKey& key,
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000350 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
bsalomon63e99f72014-07-21 08:03:14 -0700410 static void GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&,
411 GrEffectKeyBuilder* b) {
412 const DIEllipseEdgeEffect& ellipseEffect =
413 drawEffect.castEffect<DIEllipseEdgeEffect>();
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000414
bsalomon63e99f72014-07-21 08:03:14 -0700415 b->add32(ellipseEffect.getMode());
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000416 }
417
418 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {
419 }
420
421 private:
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000422 typedef GrGLVertexEffect INHERITED;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000423 };
424
425private:
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000426 DIEllipseEdgeEffect(Mode mode) : GrVertexEffect() {
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000427 this->addVertexAttrib(kVec2f_GrSLType);
428 this->addVertexAttrib(kVec2f_GrSLType);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000429 fMode = mode;
430 }
431
432 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
433 const DIEllipseEdgeEffect& eee = CastEffect<DIEllipseEdgeEffect>(other);
434 return eee.fMode == fMode;
435 }
436
437 Mode fMode;
438
439 GR_DECLARE_EFFECT_TEST;
440
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000441 typedef GrVertexEffect INHERITED;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000442};
443
444GR_DEFINE_EFFECT_TEST(DIEllipseEdgeEffect);
445
bsalomon83d081a2014-07-08 09:56:10 -0700446GrEffect* DIEllipseEdgeEffect::TestCreate(SkRandom* random,
447 GrContext* context,
448 const GrDrawTargetCaps&,
449 GrTexture* textures[]) {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000450 return DIEllipseEdgeEffect::Create((Mode)(random->nextRangeU(0,2)));
451}
452
453///////////////////////////////////////////////////////////////////////////////
454
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000455void GrOvalRenderer::reset() {
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000456 SkSafeSetNull(fRRectIndexBuffer);
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000457}
458
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000459bool GrOvalRenderer::drawOval(GrDrawTarget* target, const GrContext* context, bool useAA,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000460 const SkRect& oval, const SkStrokeRec& stroke)
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000461{
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000462 bool useCoverageAA = useAA &&
463 !target->getDrawState().getRenderTarget()->isMultisampled() &&
464 !target->shouldDisableCoverageAAForBlend();
465
466 if (!useCoverageAA) {
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000467 return false;
468 }
469
470 const SkMatrix& vm = context->getMatrix();
471
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000472 // we can draw circles
473 if (SkScalarNearlyEqual(oval.width(), oval.height())
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000474 && circle_stays_circle(vm)) {
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000475 this->drawCircle(target, useCoverageAA, oval, stroke);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000476 // if we have shader derivative support, render as device-independent
477 } else if (target->caps()->shaderDerivativeSupport()) {
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000478 return this->drawDIEllipse(target, useCoverageAA, oval, stroke);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000479 // otherwise axis-aligned ellipses only
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000480 } else if (vm.rectStaysRect()) {
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000481 return this->drawEllipse(target, useCoverageAA, oval, stroke);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000482 } else {
483 return false;
484 }
485
486 return true;
487}
488
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000489///////////////////////////////////////////////////////////////////////////////
490
robertphillips@google.com42903302013-04-20 12:26:07 +0000491// position + edge
492extern const GrVertexAttrib gCircleVertexAttribs[] = {
493 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000494 {kVec4f_GrVertexAttribType, sizeof(SkPoint), kEffect_GrVertexAttribBinding}
robertphillips@google.com42903302013-04-20 12:26:07 +0000495};
496
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000497void GrOvalRenderer::drawCircle(GrDrawTarget* target,
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000498 bool useCoverageAA,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000499 const SkRect& circle,
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000500 const SkStrokeRec& stroke)
501{
502 GrDrawState* drawState = target->drawState();
503
504 const SkMatrix& vm = drawState->getViewMatrix();
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000505 SkPoint center = SkPoint::Make(circle.centerX(), circle.centerY());
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000506 vm.mapPoints(&center, 1);
507 SkScalar radius = vm.mapRadius(SkScalarHalf(circle.width()));
508 SkScalar strokeWidth = vm.mapRadius(stroke.getWidth());
509
bsalomon@google.com137f1342013-05-29 21:27:53 +0000510 GrDrawState::AutoViewMatrixRestore avmr;
511 if (!avmr.setIdentity(drawState)) {
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000512 return;
513 }
514
robertphillips@google.com42903302013-04-20 12:26:07 +0000515 drawState->setVertexAttribs<gCircleVertexAttribs>(SK_ARRAY_COUNT(gCircleVertexAttribs));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000516 SkASSERT(sizeof(CircleVertex) == drawState->getVertexSize());
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000517
518 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
519 if (!geo.succeeded()) {
520 GrPrintf("Failed to get space for vertices!\n");
521 return;
522 }
523
524 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
525
526 SkStrokeRec::Style style = stroke.getStyle();
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000527 bool isStrokeOnly = SkStrokeRec::kStroke_Style == style ||
528 SkStrokeRec::kHairline_Style == style;
529 bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == style;
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000530
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000531 SkScalar innerRadius = 0.0f;
532 SkScalar outerRadius = radius;
533 SkScalar halfWidth = 0;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000534 if (hasStroke) {
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000535 if (SkScalarNearlyZero(strokeWidth)) {
536 halfWidth = SK_ScalarHalf;
537 } else {
538 halfWidth = SkScalarHalf(strokeWidth);
539 }
540
541 outerRadius += halfWidth;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000542 if (isStrokeOnly) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000543 innerRadius = radius - halfWidth;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000544 }
545 }
546
bsalomon83d081a2014-07-08 09:56:10 -0700547 GrEffect* effect = CircleEdgeEffect::Create(isStrokeOnly && innerRadius > 0);
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +0000548 static const int kCircleEdgeAttrIndex = 1;
549 drawState->addCoverageEffect(effect, kCircleEdgeAttrIndex)->unref();
550
bsalomon@google.com58e30fe2013-04-01 19:01:20 +0000551 // The radii are outset for two reasons. First, it allows the shader to simply perform
552 // clamp(distance-to-center - radius, 0, 1). Second, the outer radius is used to compute the
553 // verts of the bounding box that is rendered and the outset ensures the box will cover all
554 // pixels partially covered by the circle.
555 outerRadius += SK_ScalarHalf;
556 innerRadius -= SK_ScalarHalf;
557
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000558 SkRect bounds = SkRect::MakeLTRB(
bsalomon@google.com58e30fe2013-04-01 19:01:20 +0000559 center.fX - outerRadius,
560 center.fY - outerRadius,
561 center.fX + outerRadius,
562 center.fY + outerRadius
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000563 );
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000564
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000565 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000566 verts[0].fOffset = SkPoint::Make(-outerRadius, -outerRadius);
567 verts[0].fOuterRadius = outerRadius;
568 verts[0].fInnerRadius = innerRadius;
569
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000570 verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
skia.committer@gmail.com46746762013-04-12 07:01:51 +0000571 verts[1].fOffset = SkPoint::Make(outerRadius, -outerRadius);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000572 verts[1].fOuterRadius = outerRadius;
573 verts[1].fInnerRadius = innerRadius;
574
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000575 verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000576 verts[2].fOffset = SkPoint::Make(-outerRadius, outerRadius);
577 verts[2].fOuterRadius = outerRadius;
578 verts[2].fInnerRadius = innerRadius;
579
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000580 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000581 verts[3].fOffset = SkPoint::Make(outerRadius, outerRadius);
582 verts[3].fOuterRadius = outerRadius;
583 verts[3].fInnerRadius = innerRadius;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000584
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000585 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000586}
587
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000588///////////////////////////////////////////////////////////////////////////////
589
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000590// position + offset + 1/radii
robertphillips@google.com42903302013-04-20 12:26:07 +0000591extern const GrVertexAttrib gEllipseVertexAttribs[] = {
592 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000593 {kVec2f_GrVertexAttribType, sizeof(SkPoint), kEffect_GrVertexAttribBinding},
594 {kVec4f_GrVertexAttribType, 2*sizeof(SkPoint), kEffect_GrVertexAttribBinding}
robertphillips@google.com42903302013-04-20 12:26:07 +0000595};
596
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000597// position + offsets
598extern const GrVertexAttrib gDIEllipseVertexAttribs[] = {
599 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000600 {kVec2f_GrVertexAttribType, sizeof(SkPoint), kEffect_GrVertexAttribBinding},
601 {kVec2f_GrVertexAttribType, 2*sizeof(SkPoint), kEffect_GrVertexAttribBinding},
robertphillips@google.com42903302013-04-20 12:26:07 +0000602};
603
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000604bool GrOvalRenderer::drawEllipse(GrDrawTarget* target,
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000605 bool useCoverageAA,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000606 const SkRect& ellipse,
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000607 const SkStrokeRec& stroke)
608{
609 GrDrawState* drawState = target->drawState();
610#ifdef SK_DEBUG
611 {
612 // we should have checked for this previously
613 bool isAxisAlignedEllipse = drawState->getViewMatrix().rectStaysRect();
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000614 SkASSERT(useCoverageAA && isAxisAlignedEllipse);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000615 }
616#endif
617
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000618 // do any matrix crunching before we reset the draw state for device coords
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000619 const SkMatrix& vm = drawState->getViewMatrix();
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000620 SkPoint center = SkPoint::Make(ellipse.centerX(), ellipse.centerY());
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000621 vm.mapPoints(&center, 1);
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000622 SkScalar ellipseXRadius = SkScalarHalf(ellipse.width());
623 SkScalar ellipseYRadius = SkScalarHalf(ellipse.height());
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +0000624 SkScalar xRadius = SkScalarAbs(vm[SkMatrix::kMScaleX]*ellipseXRadius +
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000625 vm[SkMatrix::kMSkewY]*ellipseYRadius);
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +0000626 SkScalar yRadius = SkScalarAbs(vm[SkMatrix::kMSkewX]*ellipseXRadius +
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000627 vm[SkMatrix::kMScaleY]*ellipseYRadius);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000628
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000629 // do (potentially) anisotropic mapping of stroke
630 SkVector scaledStroke;
631 SkScalar strokeWidth = stroke.getWidth();
632 scaledStroke.fX = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMScaleX] + vm[SkMatrix::kMSkewY]));
633 scaledStroke.fY = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMSkewX] + vm[SkMatrix::kMScaleY]));
634
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000635 SkStrokeRec::Style style = stroke.getStyle();
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000636 bool isStrokeOnly = SkStrokeRec::kStroke_Style == style ||
637 SkStrokeRec::kHairline_Style == style;
638 bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == style;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000639
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000640 SkScalar innerXRadius = 0;
641 SkScalar innerYRadius = 0;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000642 if (hasStroke) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000643 if (SkScalarNearlyZero(scaledStroke.length())) {
644 scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf);
645 } else {
646 scaledStroke.scale(SK_ScalarHalf);
647 }
648
649 // we only handle thick strokes for near-circular ellipses
650 if (scaledStroke.length() > SK_ScalarHalf &&
651 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
652 return false;
653 }
654
655 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
656 if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius ||
657 scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) {
658 return false;
659 }
660
661 // this is legit only if scale & translation (which should be the case at the moment)
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000662 if (isStrokeOnly) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000663 innerXRadius = xRadius - scaledStroke.fX;
664 innerYRadius = yRadius - scaledStroke.fY;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000665 }
666
667 xRadius += scaledStroke.fX;
668 yRadius += scaledStroke.fY;
669 }
670
bsalomon@google.com137f1342013-05-29 21:27:53 +0000671 GrDrawState::AutoViewMatrixRestore avmr;
672 if (!avmr.setIdentity(drawState)) {
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000673 return false;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000674 }
675
robertphillips@google.com42903302013-04-20 12:26:07 +0000676 drawState->setVertexAttribs<gEllipseVertexAttribs>(SK_ARRAY_COUNT(gEllipseVertexAttribs));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000677 SkASSERT(sizeof(EllipseVertex) == drawState->getVertexSize());
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000678
679 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
680 if (!geo.succeeded()) {
681 GrPrintf("Failed to get space for vertices!\n");
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000682 return false;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000683 }
684
685 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices());
686
bsalomon83d081a2014-07-08 09:56:10 -0700687 GrEffect* effect = EllipseEdgeEffect::Create(isStrokeOnly &&
688 innerXRadius > 0 && innerYRadius > 0);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000689
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000690 static const int kEllipseCenterAttrIndex = 1;
jvanverth@google.com6cc8d442013-09-10 18:24:37 +0000691 static const int kEllipseEdgeAttrIndex = 2;
692 drawState->addCoverageEffect(effect, kEllipseCenterAttrIndex, kEllipseEdgeAttrIndex)->unref();
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000693
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000694 // Compute the reciprocals of the radii here to save time in the shader
695 SkScalar xRadRecip = SkScalarInvert(xRadius);
696 SkScalar yRadRecip = SkScalarInvert(yRadius);
697 SkScalar xInnerRadRecip = SkScalarInvert(innerXRadius);
698 SkScalar yInnerRadRecip = SkScalarInvert(innerYRadius);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000699
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000700 // We've extended the outer x radius out half a pixel to antialias.
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000701 // This will also expand the rect so all the pixels will be captured.
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000702 // TODO: Consider if we should use sqrt(2)/2 instead
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000703 xRadius += SK_ScalarHalf;
704 yRadius += SK_ScalarHalf;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000705
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000706 SkRect bounds = SkRect::MakeLTRB(
707 center.fX - xRadius,
708 center.fY - yRadius,
709 center.fX + xRadius,
710 center.fY + yRadius
711 );
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000712
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000713 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000714 verts[0].fOffset = SkPoint::Make(-xRadius, -yRadius);
715 verts[0].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
716 verts[0].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000717
718 verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000719 verts[1].fOffset = SkPoint::Make(xRadius, -yRadius);
720 verts[1].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
721 verts[1].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000722
723 verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000724 verts[2].fOffset = SkPoint::Make(-xRadius, yRadius);
725 verts[2].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
726 verts[2].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000727
728 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000729 verts[3].fOffset = SkPoint::Make(xRadius, yRadius);
730 verts[3].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
731 verts[3].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
skia.committer@gmail.com46746762013-04-12 07:01:51 +0000732
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000733 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds);
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000734
735 return true;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000736}
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000737
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000738bool GrOvalRenderer::drawDIEllipse(GrDrawTarget* target,
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000739 bool useCoverageAA,
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000740 const SkRect& ellipse,
741 const SkStrokeRec& stroke)
742{
743 GrDrawState* drawState = target->drawState();
744 const SkMatrix& vm = drawState->getViewMatrix();
745
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000746 SkPoint center = SkPoint::Make(ellipse.centerX(), ellipse.centerY());
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000747 SkScalar xRadius = SkScalarHalf(ellipse.width());
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000748 SkScalar yRadius = SkScalarHalf(ellipse.height());
749
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000750 SkStrokeRec::Style style = stroke.getStyle();
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000751 DIEllipseEdgeEffect::Mode mode = (SkStrokeRec::kStroke_Style == style) ?
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000752 DIEllipseEdgeEffect::kStroke :
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000753 (SkStrokeRec::kHairline_Style == style) ?
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000754 DIEllipseEdgeEffect::kHairline : DIEllipseEdgeEffect::kFill;
755
756 SkScalar innerXRadius = 0;
757 SkScalar innerYRadius = 0;
758 if (SkStrokeRec::kFill_Style != style && SkStrokeRec::kHairline_Style != style) {
759 SkScalar strokeWidth = stroke.getWidth();
760
761 if (SkScalarNearlyZero(strokeWidth)) {
762 strokeWidth = SK_ScalarHalf;
763 } else {
764 strokeWidth *= SK_ScalarHalf;
765 }
766
767 // we only handle thick strokes for near-circular ellipses
768 if (strokeWidth > SK_ScalarHalf &&
769 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
770 return false;
771 }
772
773 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
774 if (strokeWidth*(yRadius*yRadius) < (strokeWidth*strokeWidth)*xRadius ||
775 strokeWidth*(xRadius*xRadius) < (strokeWidth*strokeWidth)*yRadius) {
776 return false;
777 }
778
779 // set inner radius (if needed)
780 if (SkStrokeRec::kStroke_Style == style) {
781 innerXRadius = xRadius - strokeWidth;
782 innerYRadius = yRadius - strokeWidth;
783 }
784
785 xRadius += strokeWidth;
786 yRadius += strokeWidth;
787 }
788 if (DIEllipseEdgeEffect::kStroke == mode) {
789 mode = (innerXRadius > 0 && innerYRadius > 0) ? DIEllipseEdgeEffect::kStroke :
790 DIEllipseEdgeEffect::kFill;
791 }
792 SkScalar innerRatioX = SkScalarDiv(xRadius, innerXRadius);
793 SkScalar innerRatioY = SkScalarDiv(yRadius, innerYRadius);
794
795 drawState->setVertexAttribs<gDIEllipseVertexAttribs>(SK_ARRAY_COUNT(gDIEllipseVertexAttribs));
796 SkASSERT(sizeof(DIEllipseVertex) == drawState->getVertexSize());
797
798 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
799 if (!geo.succeeded()) {
800 GrPrintf("Failed to get space for vertices!\n");
801 return false;
802 }
803
804 DIEllipseVertex* verts = reinterpret_cast<DIEllipseVertex*>(geo.vertices());
805
bsalomon83d081a2014-07-08 09:56:10 -0700806 GrEffect* effect = DIEllipseEdgeEffect::Create(mode);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000807
808 static const int kEllipseOuterOffsetAttrIndex = 1;
809 static const int kEllipseInnerOffsetAttrIndex = 2;
810 drawState->addCoverageEffect(effect, kEllipseOuterOffsetAttrIndex,
811 kEllipseInnerOffsetAttrIndex)->unref();
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000812
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000813 // This expands the outer rect so that after CTM we end up with a half-pixel border
814 SkScalar a = vm[SkMatrix::kMScaleX];
815 SkScalar b = vm[SkMatrix::kMSkewX];
816 SkScalar c = vm[SkMatrix::kMSkewY];
817 SkScalar d = vm[SkMatrix::kMScaleY];
818 SkScalar geoDx = SkScalarDiv(SK_ScalarHalf, SkScalarSqrt(a*a + c*c));
819 SkScalar geoDy = SkScalarDiv(SK_ScalarHalf, SkScalarSqrt(b*b + d*d));
820 // This adjusts the "radius" to include the half-pixel border
821 SkScalar offsetDx = SkScalarDiv(geoDx, xRadius);
822 SkScalar offsetDy = SkScalarDiv(geoDy, yRadius);
823
824 SkRect bounds = SkRect::MakeLTRB(
825 center.fX - xRadius - geoDx,
826 center.fY - yRadius - geoDy,
827 center.fX + xRadius + geoDx,
828 center.fY + yRadius + geoDy
829 );
830
831 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
832 verts[0].fOuterOffset = SkPoint::Make(-1.0f - offsetDx, -1.0f - offsetDy);
833 verts[0].fInnerOffset = SkPoint::Make(-innerRatioX - offsetDx, -innerRatioY - offsetDy);
834
835 verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
836 verts[1].fOuterOffset = SkPoint::Make(1.0f + offsetDx, -1.0f - offsetDy);
837 verts[1].fInnerOffset = SkPoint::Make(innerRatioX + offsetDx, -innerRatioY - offsetDy);
838
839 verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
840 verts[2].fOuterOffset = SkPoint::Make(-1.0f - offsetDx, 1.0f + offsetDy);
841 verts[2].fInnerOffset = SkPoint::Make(-innerRatioX - offsetDx, innerRatioY + offsetDy);
842
843 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
844 verts[3].fOuterOffset = SkPoint::Make(1.0f + offsetDx, 1.0f + offsetDy);
845 verts[3].fInnerOffset = SkPoint::Make(innerRatioX + offsetDx, innerRatioY + offsetDy);
846
847 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds);
848
849 return true;
850}
851
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000852///////////////////////////////////////////////////////////////////////////////
853
854static const uint16_t gRRectIndices[] = {
855 // corners
856 0, 1, 5, 0, 5, 4,
857 2, 3, 7, 2, 7, 6,
858 8, 9, 13, 8, 13, 12,
859 10, 11, 15, 10, 15, 14,
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000860
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000861 // edges
862 1, 2, 6, 1, 6, 5,
863 4, 5, 9, 4, 9, 8,
864 6, 7, 11, 6, 11, 10,
865 9, 10, 14, 9, 14, 13,
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000866
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000867 // center
868 // we place this at the end so that we can ignore these indices when rendering stroke-only
869 5, 6, 10, 5, 10, 9
870};
871
872
873GrIndexBuffer* GrOvalRenderer::rRectIndexBuffer(GrGpu* gpu) {
874 if (NULL == fRRectIndexBuffer) {
875 fRRectIndexBuffer =
876 gpu->createIndexBuffer(sizeof(gRRectIndices), false);
877 if (NULL != fRRectIndexBuffer) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000878#ifdef SK_DEBUG
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000879 bool updated =
880#endif
881 fRRectIndexBuffer->updateData(gRRectIndices,
882 sizeof(gRRectIndices));
883 GR_DEBUGASSERT(updated);
884 }
885 }
886 return fRRectIndexBuffer;
887}
888
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000889bool GrOvalRenderer::drawDRRect(GrDrawTarget* target, GrContext* context, bool useAA,
bsalomon8af05232014-06-03 06:34:58 -0700890 const SkRRect& origOuter, const SkRRect& origInner) {
891 bool applyAA = useAA &&
892 !target->getDrawState().getRenderTarget()->isMultisampled() &&
893 !target->shouldDisableCoverageAAForBlend();
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000894 GrDrawState::AutoRestoreEffects are;
895 if (!origInner.isEmpty()) {
896 SkTCopyOnFirstWrite<SkRRect> inner(origInner);
897 if (!context->getMatrix().isIdentity()) {
898 if (!origInner.transform(context->getMatrix(), inner.writable())) {
899 return false;
900 }
901 }
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000902 GrEffectEdgeType edgeType = applyAA ? kInverseFillAA_GrEffectEdgeType :
903 kInverseFillBW_GrEffectEdgeType;
bsalomon83d081a2014-07-08 09:56:10 -0700904 GrEffect* effect = GrRRectEffect::Create(edgeType, *inner);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000905 if (NULL == effect) {
906 return false;
907 }
908 are.set(target->drawState());
909 target->drawState()->addCoverageEffect(effect)->unref();
910 }
911
912 SkStrokeRec fillRec(SkStrokeRec::kFill_InitStyle);
bsalomon8af05232014-06-03 06:34:58 -0700913 if (this->drawRRect(target, context, useAA, origOuter, fillRec)) {
914 return true;
915 }
916
917 SkASSERT(!origOuter.isEmpty());
918 SkTCopyOnFirstWrite<SkRRect> outer(origOuter);
919 if (!context->getMatrix().isIdentity()) {
920 if (!origOuter.transform(context->getMatrix(), outer.writable())) {
921 return false;
922 }
923 }
924 GrEffectEdgeType edgeType = applyAA ? kFillAA_GrEffectEdgeType :
925 kFillBW_GrEffectEdgeType;
bsalomon83d081a2014-07-08 09:56:10 -0700926 GrEffect* effect = GrRRectEffect::Create(edgeType, *outer);
bsalomon8af05232014-06-03 06:34:58 -0700927 if (NULL == effect) {
928 return false;
929 }
930 if (!are.isSet()) {
931 are.set(target->drawState());
932 }
933 GrDrawState::AutoViewMatrixRestore avmr;
934 if (!avmr.setIdentity(target->drawState())) {
935 return false;
936 }
937 target->drawState()->addCoverageEffect(effect)->unref();
938 SkRect bounds = outer->getBounds();
939 if (applyAA) {
940 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
941 }
942 target->drawRect(bounds, NULL, NULL, NULL);
943 return true;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000944}
945
946bool GrOvalRenderer::drawRRect(GrDrawTarget* target, GrContext* context, bool useAA,
947 const SkRRect& rrect, const SkStrokeRec& stroke) {
948 if (rrect.isOval()) {
949 return this->drawOval(target, context, useAA, rrect.getBounds(), stroke);
950 }
951
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000952 bool useCoverageAA = useAA &&
953 !target->getDrawState().getRenderTarget()->isMultisampled() &&
954 !target->shouldDisableCoverageAAForBlend();
955
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000956 // only anti-aliased rrects for now
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000957 if (!useCoverageAA) {
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000958 return false;
959 }
960
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000961 const SkMatrix& vm = context->getMatrix();
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000962
963 if (!vm.rectStaysRect() || !rrect.isSimple()) {
964 return false;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000965 }
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000966
967 // do any matrix crunching before we reset the draw state for device coords
968 const SkRect& rrectBounds = rrect.getBounds();
969 SkRect bounds;
970 vm.mapRect(&bounds, rrectBounds);
971
972 SkVector radii = rrect.getSimpleRadii();
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000973 SkScalar xRadius = SkScalarAbs(vm[SkMatrix::kMScaleX]*radii.fX +
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000974 vm[SkMatrix::kMSkewY]*radii.fY);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000975 SkScalar yRadius = SkScalarAbs(vm[SkMatrix::kMSkewX]*radii.fX +
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000976 vm[SkMatrix::kMScaleY]*radii.fY);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000977
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000978 SkStrokeRec::Style style = stroke.getStyle();
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000979
980 // do (potentially) anisotropic mapping of stroke
981 SkVector scaledStroke;
982 SkScalar strokeWidth = stroke.getWidth();
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000983
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000984 bool isStrokeOnly = SkStrokeRec::kStroke_Style == style ||
985 SkStrokeRec::kHairline_Style == style;
986 bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == style;
987
988 if (hasStroke) {
989 if (SkStrokeRec::kHairline_Style == style) {
990 scaledStroke.set(1, 1);
991 } else {
992 scaledStroke.fX = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMScaleX] +
993 vm[SkMatrix::kMSkewY]));
994 scaledStroke.fY = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMSkewX] +
995 vm[SkMatrix::kMScaleY]));
996 }
997
998 // if half of strokewidth is greater than radius, we don't handle that right now
999 if (SK_ScalarHalf*scaledStroke.fX > xRadius || SK_ScalarHalf*scaledStroke.fY > yRadius) {
1000 return false;
1001 }
1002 }
1003
1004 // The way the effect interpolates the offset-to-ellipse/circle-center attribute only works on
1005 // the interior of the rrect if the radii are >= 0.5. Otherwise, the inner rect of the nine-
1006 // patch will have fractional coverage. This only matters when the interior is actually filled.
1007 // We could consider falling back to rect rendering here, since a tiny radius is
1008 // indistinguishable from a square corner.
1009 if (!isStrokeOnly && (SK_ScalarHalf > xRadius || SK_ScalarHalf > yRadius)) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001010 return false;
1011 }
1012
1013 // reset to device coordinates
1014 GrDrawState* drawState = target->drawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +00001015 GrDrawState::AutoViewMatrixRestore avmr;
1016 if (!avmr.setIdentity(drawState)) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001017 return false;
1018 }
1019
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001020 GrIndexBuffer* indexBuffer = this->rRectIndexBuffer(context->getGpu());
1021 if (NULL == indexBuffer) {
1022 GrPrintf("Failed to create index buffer!\n");
1023 return false;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001024 }
1025
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001026 // if the corners are circles, use the circle renderer
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001027 if ((!hasStroke || scaledStroke.fX == scaledStroke.fY) && xRadius == yRadius) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001028 drawState->setVertexAttribs<gCircleVertexAttribs>(SK_ARRAY_COUNT(gCircleVertexAttribs));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001029 SkASSERT(sizeof(CircleVertex) == drawState->getVertexSize());
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001030
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001031 GrDrawTarget::AutoReleaseGeometry geo(target, 16, 0);
1032 if (!geo.succeeded()) {
1033 GrPrintf("Failed to get space for vertices!\n");
1034 return false;
1035 }
1036 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001037
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001038 SkScalar innerRadius = 0.0f;
1039 SkScalar outerRadius = xRadius;
1040 SkScalar halfWidth = 0;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001041 if (hasStroke) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001042 if (SkScalarNearlyZero(scaledStroke.fX)) {
1043 halfWidth = SK_ScalarHalf;
1044 } else {
1045 halfWidth = SkScalarHalf(scaledStroke.fX);
1046 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001047
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001048 if (isStrokeOnly) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001049 innerRadius = xRadius - halfWidth;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001050 }
1051 outerRadius += halfWidth;
1052 bounds.outset(halfWidth, halfWidth);
1053 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001054
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001055 isStrokeOnly = (isStrokeOnly && innerRadius >= 0);
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +00001056
bsalomon83d081a2014-07-08 09:56:10 -07001057 GrEffect* effect = CircleEdgeEffect::Create(isStrokeOnly);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001058 static const int kCircleEdgeAttrIndex = 1;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001059 drawState->addCoverageEffect(effect, kCircleEdgeAttrIndex)->unref();
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +00001060
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001061 // The radii are outset for two reasons. First, it allows the shader to simply perform
1062 // clamp(distance-to-center - radius, 0, 1). Second, the outer radius is used to compute the
1063 // verts of the bounding box that is rendered and the outset ensures the box will cover all
1064 // pixels partially covered by the circle.
1065 outerRadius += SK_ScalarHalf;
1066 innerRadius -= SK_ScalarHalf;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001067
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001068 // Expand the rect so all the pixels will be captured.
1069 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001070
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001071 SkScalar yCoords[4] = {
1072 bounds.fTop,
1073 bounds.fTop + outerRadius,
1074 bounds.fBottom - outerRadius,
1075 bounds.fBottom
1076 };
1077 SkScalar yOuterRadii[4] = {
1078 -outerRadius,
1079 0,
1080 0,
1081 outerRadius
1082 };
1083 for (int i = 0; i < 4; ++i) {
1084 verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]);
1085 verts->fOffset = SkPoint::Make(-outerRadius, yOuterRadii[i]);
1086 verts->fOuterRadius = outerRadius;
1087 verts->fInnerRadius = innerRadius;
1088 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001089
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001090 verts->fPos = SkPoint::Make(bounds.fLeft + outerRadius, yCoords[i]);
1091 verts->fOffset = SkPoint::Make(0, yOuterRadii[i]);
1092 verts->fOuterRadius = outerRadius;
1093 verts->fInnerRadius = innerRadius;
1094 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001095
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001096 verts->fPos = SkPoint::Make(bounds.fRight - outerRadius, yCoords[i]);
1097 verts->fOffset = SkPoint::Make(0, yOuterRadii[i]);
1098 verts->fOuterRadius = outerRadius;
1099 verts->fInnerRadius = innerRadius;
1100 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001101
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001102 verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]);
1103 verts->fOffset = SkPoint::Make(outerRadius, yOuterRadii[i]);
1104 verts->fOuterRadius = outerRadius;
1105 verts->fInnerRadius = innerRadius;
1106 verts++;
1107 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001108
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001109 // drop out the middle quad if we're stroked
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001110 int indexCnt = isStrokeOnly ? SK_ARRAY_COUNT(gRRectIndices) - 6 :
1111 SK_ARRAY_COUNT(gRRectIndices);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001112 target->setIndexSourceToBuffer(indexBuffer);
1113 target->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, 16, indexCnt, &bounds);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001114
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001115 // otherwise we use the ellipse renderer
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001116 } else {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001117 drawState->setVertexAttribs<gEllipseVertexAttribs>(SK_ARRAY_COUNT(gEllipseVertexAttribs));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001118 SkASSERT(sizeof(EllipseVertex) == drawState->getVertexSize());
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001119
1120 SkScalar innerXRadius = 0.0f;
1121 SkScalar innerYRadius = 0.0f;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001122 if (hasStroke) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001123 if (SkScalarNearlyZero(scaledStroke.length())) {
1124 scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf);
1125 } else {
1126 scaledStroke.scale(SK_ScalarHalf);
1127 }
1128
1129 // we only handle thick strokes for near-circular ellipses
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +00001130 if (scaledStroke.length() > SK_ScalarHalf &&
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001131 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
1132 return false;
1133 }
1134
1135 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
1136 if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius ||
1137 scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) {
1138 return false;
1139 }
1140
1141 // this is legit only if scale & translation (which should be the case at the moment)
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001142 if (isStrokeOnly) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001143 innerXRadius = xRadius - scaledStroke.fX;
1144 innerYRadius = yRadius - scaledStroke.fY;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001145 }
1146
1147 xRadius += scaledStroke.fX;
1148 yRadius += scaledStroke.fY;
1149 bounds.outset(scaledStroke.fX, scaledStroke.fY);
1150 }
jvanverth@google.come3647412013-05-08 15:31:43 +00001151
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001152 isStrokeOnly = (isStrokeOnly && innerXRadius >= 0 && innerYRadius >= 0);
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +00001153
jvanverth@google.come3647412013-05-08 15:31:43 +00001154 GrDrawTarget::AutoReleaseGeometry geo(target, 16, 0);
1155 if (!geo.succeeded()) {
1156 GrPrintf("Failed to get space for vertices!\n");
1157 return false;
1158 }
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001159 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices());
jvanverth@google.come3647412013-05-08 15:31:43 +00001160
bsalomon83d081a2014-07-08 09:56:10 -07001161 GrEffect* effect = EllipseEdgeEffect::Create(isStrokeOnly);
jvanverth@google.come3647412013-05-08 15:31:43 +00001162 static const int kEllipseOffsetAttrIndex = 1;
1163 static const int kEllipseRadiiAttrIndex = 2;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001164 drawState->addCoverageEffect(effect,
1165 kEllipseOffsetAttrIndex, kEllipseRadiiAttrIndex)->unref();
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001166
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001167 // Compute the reciprocals of the radii here to save time in the shader
1168 SkScalar xRadRecip = SkScalarInvert(xRadius);
1169 SkScalar yRadRecip = SkScalarInvert(yRadius);
1170 SkScalar xInnerRadRecip = SkScalarInvert(innerXRadius);
1171 SkScalar yInnerRadRecip = SkScalarInvert(innerYRadius);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001172
1173 // Extend the radii out half a pixel to antialias.
1174 SkScalar xOuterRadius = xRadius + SK_ScalarHalf;
1175 SkScalar yOuterRadius = yRadius + SK_ScalarHalf;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001176
1177 // Expand the rect so all the pixels will be captured.
1178 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1179
1180 SkScalar yCoords[4] = {
1181 bounds.fTop,
1182 bounds.fTop + yOuterRadius,
1183 bounds.fBottom - yOuterRadius,
1184 bounds.fBottom
1185 };
1186 SkScalar yOuterOffsets[4] = {
jvanverth@google.comd1b5b142013-07-02 14:46:03 +00001187 yOuterRadius,
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001188 SK_ScalarNearlyZero, // we're using inversesqrt() in the shader, so can't be exactly 0
1189 SK_ScalarNearlyZero,
1190 yOuterRadius
1191 };
1192
1193 for (int i = 0; i < 4; ++i) {
1194 verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]);
jvanverth@google.comd1b5b142013-07-02 14:46:03 +00001195 verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001196 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1197 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001198 verts++;
1199
1200 verts->fPos = SkPoint::Make(bounds.fLeft + xOuterRadius, yCoords[i]);
1201 verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001202 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1203 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001204 verts++;
1205
1206 verts->fPos = SkPoint::Make(bounds.fRight - xOuterRadius, yCoords[i]);
1207 verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001208 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1209 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001210 verts++;
1211
1212 verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]);
1213 verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001214 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1215 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001216 verts++;
1217 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001218
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001219 // drop out the middle quad if we're stroked
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001220 int indexCnt = isStrokeOnly ? SK_ARRAY_COUNT(gRRectIndices) - 6 :
1221 SK_ARRAY_COUNT(gRRectIndices);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001222 target->setIndexSourceToBuffer(indexBuffer);
1223 target->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, 16, indexCnt, &bounds);
1224 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001225
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001226 return true;
1227}