blob: 46ba72d239e8d7a26c679436b0d42a34cb85e1e1 [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
joshualitt30ba4362014-08-21 20:18:45 -07008#include "gl/builders/GrGLProgramBuilder.h"
commit-bot@chromium.org81312832013-03-22 18:34:09 +00009#include "GrOvalRenderer.h"
10
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000011#include "GrEffect.h"
12#include "gl/GrGLEffect.h"
13#include "gl/GrGLSL.h"
joshualitt249af152014-09-15 11:41:13 -070014#include "gl/GrGLGeometryProcessor.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
joshualitt249af152014-09-15 11:41:13 -070025#include "effects/GrGeometryProcessor.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
joshualitt249af152014-09-15 11:41:13 -070063class CircleEdgeEffect : public GrGeometryProcessor {
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
joshualitt249af152014-09-15 11:41:13 -070083 const GrShaderVar& inCircleEdge() const { return fInCircleEdge; }
84
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000085 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
86 return GrTBackendEffectFactory<CircleEdgeEffect>::getInstance();
87 }
88
89 virtual ~CircleEdgeEffect() {}
90
91 static const char* Name() { return "CircleEdge"; }
92
93 inline bool isStroked() const { return fStroke; }
94
joshualitt249af152014-09-15 11:41:13 -070095 class GLEffect : public GrGLGeometryProcessor {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000096 public:
97 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
98 : INHERITED (factory) {}
99
joshualitt30ba4362014-08-21 20:18:45 -0700100 virtual void emitCode(GrGLFullProgramBuilder* builder,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000101 const GrDrawEffect& drawEffect,
bsalomon63e99f72014-07-21 08:03:14 -0700102 const GrEffectKey& key,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000103 const char* outputColor,
104 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000105 const TransformedCoordsArray&,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000106 const TextureSamplerArray& samplers) SK_OVERRIDE {
107 const CircleEdgeEffect& circleEffect = drawEffect.castEffect<CircleEdgeEffect>();
108 const char *vsName, *fsName;
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000109 builder->addVarying(kVec4f_GrSLType, "CircleEdge", &vsName, &fsName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000110
joshualitt249af152014-09-15 11:41:13 -0700111 GrGLVertexShaderBuilder* vsBuilder = builder->getVertexShaderBuilder();;
112 vsBuilder->codeAppendf("\t%s = %s;\n", vsName, circleEffect.inCircleEdge().c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000113
joshualitt30ba4362014-08-21 20:18:45 -0700114 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder();
115 fsBuilder->codeAppendf("\tfloat d = length(%s.xy);\n", fsName);
116 fsBuilder->codeAppendf("\tfloat edgeAlpha = clamp(%s.z - d, 0.0, 1.0);\n", fsName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000117 if (circleEffect.isStroked()) {
joshualitt30ba4362014-08-21 20:18:45 -0700118 fsBuilder->codeAppendf("\tfloat innerAlpha = clamp(d - %s.w, 0.0, 1.0);\n", fsName);
119 fsBuilder->codeAppend("\tedgeAlpha *= innerAlpha;\n");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000120 }
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000121
joshualitt30ba4362014-08-21 20:18:45 -0700122 fsBuilder->codeAppendf("\t%s = %s;\n", outputColor,
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000123 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("edgeAlpha")).c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000124 }
125
bsalomon63e99f72014-07-21 08:03:14 -0700126 static void GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&,
127 GrEffectKeyBuilder* b) {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000128 const CircleEdgeEffect& circleEffect = drawEffect.castEffect<CircleEdgeEffect>();
bsalomon63e99f72014-07-21 08:03:14 -0700129 b->add32(circleEffect.isStroked());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000130 }
131
kkinnunen7510b222014-07-30 00:04:16 -0700132 virtual void setData(const GrGLProgramDataManager&, const GrDrawEffect&) SK_OVERRIDE {}
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000133
134 private:
joshualitt249af152014-09-15 11:41:13 -0700135 typedef GrGLGeometryProcessor INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000136 };
137
138
139private:
joshualitt249af152014-09-15 11:41:13 -0700140 CircleEdgeEffect(bool stroke)
141 : fInCircleEdge(this->addVertexAttrib(
142 GrShaderVar("inCircleEdge",
143 kVec4f_GrSLType,
144 GrShaderVar::kAttribute_TypeModifier))) {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000145 fStroke = stroke;
146 }
147
148 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
149 const CircleEdgeEffect& cee = CastEffect<CircleEdgeEffect>(other);
150 return cee.fStroke == fStroke;
151 }
152
joshualitt249af152014-09-15 11:41:13 -0700153 const GrShaderVar& fInCircleEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000154 bool fStroke;
155
156 GR_DECLARE_EFFECT_TEST;
157
joshualitt249af152014-09-15 11:41:13 -0700158 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000159};
160
161GR_DEFINE_EFFECT_TEST(CircleEdgeEffect);
162
bsalomon83d081a2014-07-08 09:56:10 -0700163GrEffect* CircleEdgeEffect::TestCreate(SkRandom* random,
164 GrContext* context,
165 const GrDrawTargetCaps&,
166 GrTexture* textures[]) {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000167 return CircleEdgeEffect::Create(random->nextBool());
168}
169
170///////////////////////////////////////////////////////////////////////////////
171
172/**
173 * 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 +0000174 * ellipse, specified as a 2D offset from center, and the reciprocals of the outer and inner radii,
175 * in both x and y directions.
176 *
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000177 * 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 +0000178 */
179
joshualitt249af152014-09-15 11:41:13 -0700180class EllipseEdgeEffect : public GrGeometryProcessor {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000181public:
bsalomon83d081a2014-07-08 09:56:10 -0700182 static GrEffect* Create(bool stroke) {
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000183 GR_CREATE_STATIC_EFFECT(gEllipseStrokeEdge, EllipseEdgeEffect, (true));
184 GR_CREATE_STATIC_EFFECT(gEllipseFillEdge, EllipseEdgeEffect, (false));
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000185
186 if (stroke) {
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000187 gEllipseStrokeEdge->ref();
188 return gEllipseStrokeEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000189 } else {
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000190 gEllipseFillEdge->ref();
191 return gEllipseFillEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000192 }
193 }
194
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000195 virtual void getConstantColorComponents(GrColor* color,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000196 uint32_t* validFlags) const SK_OVERRIDE {
197 *validFlags = 0;
198 }
199
200 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
201 return GrTBackendEffectFactory<EllipseEdgeEffect>::getInstance();
202 }
203
204 virtual ~EllipseEdgeEffect() {}
205
206 static const char* Name() { return "EllipseEdge"; }
207
joshualitt249af152014-09-15 11:41:13 -0700208 const GrShaderVar& inEllipseOffset() const { return fInEllipseOffset; }
209 const GrShaderVar& inEllipseRadii() const { return fInEllipseRadii; }
210
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000211 inline bool isStroked() const { return fStroke; }
212
joshualitt249af152014-09-15 11:41:13 -0700213 class GLEffect : public GrGLGeometryProcessor {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000214 public:
215 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
216 : INHERITED (factory) {}
217
joshualitt30ba4362014-08-21 20:18:45 -0700218 virtual void emitCode(GrGLFullProgramBuilder* builder,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000219 const GrDrawEffect& drawEffect,
bsalomon63e99f72014-07-21 08:03:14 -0700220 const GrEffectKey& key,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000221 const char* outputColor,
222 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000223 const TransformedCoordsArray&,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000224 const TextureSamplerArray& samplers) SK_OVERRIDE {
225 const EllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<EllipseEdgeEffect>();
226
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000227 const char *vsOffsetName, *fsOffsetName;
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000228 const char *vsRadiiName, *fsRadiiName;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000229
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000230 builder->addVarying(kVec2f_GrSLType, "EllipseOffsets", &vsOffsetName, &fsOffsetName);
joshualitt30ba4362014-08-21 20:18:45 -0700231
232 GrGLVertexShaderBuilder* vsBuilder = builder->getVertexShaderBuilder();
joshualitt249af152014-09-15 11:41:13 -0700233 vsBuilder->codeAppendf("%s = %s;", vsOffsetName,
234 ellipseEffect.inEllipseOffset().c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000235
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000236 builder->addVarying(kVec4f_GrSLType, "EllipseRadii", &vsRadiiName, &fsRadiiName);
joshualitt249af152014-09-15 11:41:13 -0700237 vsBuilder->codeAppendf("%s = %s;", vsRadiiName, ellipseEffect.inEllipseRadii().c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000238
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +0000239 // for outer curve
joshualitt30ba4362014-08-21 20:18:45 -0700240 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder();
241 fsBuilder->codeAppendf("\tvec2 scaledOffset = %s*%s.xy;\n", fsOffsetName, fsRadiiName);
242 fsBuilder->codeAppend("\tfloat test = dot(scaledOffset, scaledOffset) - 1.0;\n");
243 fsBuilder->codeAppendf("\tvec2 grad = 2.0*scaledOffset*%s.xy;\n", fsRadiiName);
244 fsBuilder->codeAppend("\tfloat grad_dot = dot(grad, grad);\n");
commit-bot@chromium.org1b035d82014-04-09 17:11:09 +0000245 // avoid calling inversesqrt on zero.
joshualitt30ba4362014-08-21 20:18:45 -0700246 fsBuilder->codeAppend("\tgrad_dot = max(grad_dot, 1.0e-4);\n");
247 fsBuilder->codeAppend("\tfloat invlen = inversesqrt(grad_dot);\n");
248 fsBuilder->codeAppend("\tfloat edgeAlpha = clamp(0.5-test*invlen, 0.0, 1.0);\n");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000249
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000250 // for inner curve
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000251 if (ellipseEffect.isStroked()) {
joshualitt30ba4362014-08-21 20:18:45 -0700252 fsBuilder->codeAppendf("\tscaledOffset = %s*%s.zw;\n", fsOffsetName, fsRadiiName);
253 fsBuilder->codeAppend("\ttest = dot(scaledOffset, scaledOffset) - 1.0;\n");
254 fsBuilder->codeAppendf("\tgrad = 2.0*scaledOffset*%s.zw;\n", fsRadiiName);
255 fsBuilder->codeAppend("\tinvlen = inversesqrt(dot(grad, grad));\n");
256 fsBuilder->codeAppend("\tedgeAlpha *= clamp(0.5+test*invlen, 0.0, 1.0);\n");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000257 }
258
joshualitt30ba4362014-08-21 20:18:45 -0700259 fsBuilder->codeAppendf("\t%s = %s;\n", outputColor,
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000260 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("edgeAlpha")).c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000261 }
262
bsalomon63e99f72014-07-21 08:03:14 -0700263 static void GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&,
264 GrEffectKeyBuilder* b) {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000265 const EllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<EllipseEdgeEffect>();
bsalomon63e99f72014-07-21 08:03:14 -0700266 b->add32(ellipseEffect.isStroked());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000267 }
268
kkinnunen7510b222014-07-30 00:04:16 -0700269 virtual void setData(const GrGLProgramDataManager&, const GrDrawEffect&) SK_OVERRIDE {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000270 }
271
272 private:
joshualitt249af152014-09-15 11:41:13 -0700273 typedef GrGLGeometryProcessor INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000274 };
275
276private:
joshualitt249af152014-09-15 11:41:13 -0700277 EllipseEdgeEffect(bool stroke)
278 : fInEllipseOffset(this->addVertexAttrib(
279 GrShaderVar("inEllipseOffset",
280 kVec2f_GrSLType,
281 GrShaderVar::kAttribute_TypeModifier)))
282 , fInEllipseRadii(this->addVertexAttrib(
283 GrShaderVar("inEllipseRadii",
284 kVec4f_GrSLType,
285 GrShaderVar::kAttribute_TypeModifier))) {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000286 fStroke = stroke;
287 }
288
289 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
290 const EllipseEdgeEffect& eee = CastEffect<EllipseEdgeEffect>(other);
291 return eee.fStroke == fStroke;
292 }
293
joshualitt249af152014-09-15 11:41:13 -0700294 const GrShaderVar& fInEllipseOffset;
295 const GrShaderVar& fInEllipseRadii;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000296 bool fStroke;
297
298 GR_DECLARE_EFFECT_TEST;
299
joshualitt249af152014-09-15 11:41:13 -0700300 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000301};
302
303GR_DEFINE_EFFECT_TEST(EllipseEdgeEffect);
304
bsalomon83d081a2014-07-08 09:56:10 -0700305GrEffect* EllipseEdgeEffect::TestCreate(SkRandom* random,
306 GrContext* context,
307 const GrDrawTargetCaps&,
308 GrTexture* textures[]) {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000309 return EllipseEdgeEffect::Create(random->nextBool());
310}
311
312///////////////////////////////////////////////////////////////////////////////
313
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000314/**
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000315 * 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 +0000316 * specified as a 2D offset from center for both the outer and inner paths (if stroked). The
317 * implict equation used is for a unit circle (x^2 + y^2 - 1 = 0) and the edge corrected by
318 * using differentials.
319 *
320 * The result is device-independent and can be used with any affine matrix.
321 */
322
joshualitt249af152014-09-15 11:41:13 -0700323class DIEllipseEdgeEffect : public GrGeometryProcessor {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000324public:
325 enum Mode { kStroke = 0, kHairline, kFill };
326
bsalomon83d081a2014-07-08 09:56:10 -0700327 static GrEffect* Create(Mode mode) {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000328 GR_CREATE_STATIC_EFFECT(gEllipseStrokeEdge, DIEllipseEdgeEffect, (kStroke));
329 GR_CREATE_STATIC_EFFECT(gEllipseHairlineEdge, DIEllipseEdgeEffect, (kHairline));
330 GR_CREATE_STATIC_EFFECT(gEllipseFillEdge, DIEllipseEdgeEffect, (kFill));
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000331
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000332 if (kStroke == mode) {
333 gEllipseStrokeEdge->ref();
334 return gEllipseStrokeEdge;
335 } else if (kHairline == mode) {
336 gEllipseHairlineEdge->ref();
337 return gEllipseHairlineEdge;
338 } else {
339 gEllipseFillEdge->ref();
340 return gEllipseFillEdge;
341 }
342 }
343
344 virtual void getConstantColorComponents(GrColor* color,
345 uint32_t* validFlags) const SK_OVERRIDE {
346 *validFlags = 0;
347 }
348
349 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
350 return GrTBackendEffectFactory<DIEllipseEdgeEffect>::getInstance();
351 }
352
353 virtual ~DIEllipseEdgeEffect() {}
354
355 static const char* Name() { return "DIEllipseEdge"; }
356
joshualitt249af152014-09-15 11:41:13 -0700357 const GrShaderVar& inEllipseOffsets0() const { return fInEllipseOffsets0; }
358 const GrShaderVar& inEllipseOffsets1() const { return fInEllipseOffsets1; }
359
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000360 inline Mode getMode() const { return fMode; }
361
joshualitt249af152014-09-15 11:41:13 -0700362 class GLEffect : public GrGLGeometryProcessor {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000363 public:
364 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
365 : INHERITED (factory) {}
366
joshualitt30ba4362014-08-21 20:18:45 -0700367 virtual void emitCode(GrGLFullProgramBuilder* builder,
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000368 const GrDrawEffect& drawEffect,
bsalomon63e99f72014-07-21 08:03:14 -0700369 const GrEffectKey& key,
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000370 const char* outputColor,
371 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000372 const TransformedCoordsArray&,
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000373 const TextureSamplerArray& samplers) SK_OVERRIDE {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000374 const DIEllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<DIEllipseEdgeEffect>();
375
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000376 const char *vsOffsetName0, *fsOffsetName0;
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000377 builder->addVarying(kVec2f_GrSLType, "EllipseOffsets0",
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000378 &vsOffsetName0, &fsOffsetName0);
joshualitt30ba4362014-08-21 20:18:45 -0700379
380 GrGLVertexShaderBuilder* vsBuilder = builder->getVertexShaderBuilder();
joshualitt249af152014-09-15 11:41:13 -0700381 vsBuilder->codeAppendf("%s = %s;", vsOffsetName0,
382 ellipseEffect.inEllipseOffsets0().c_str());
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000383 const char *vsOffsetName1, *fsOffsetName1;
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000384 builder->addVarying(kVec2f_GrSLType, "EllipseOffsets1",
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000385 &vsOffsetName1, &fsOffsetName1);
joshualitt249af152014-09-15 11:41:13 -0700386 vsBuilder->codeAppendf("\t%s = %s;\n", vsOffsetName1,
387 ellipseEffect.inEllipseOffsets1().c_str());
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000388
joshualitt30ba4362014-08-21 20:18:45 -0700389 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder();
390 SkAssertResult(fsBuilder->enableFeature(
391 GrGLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature));
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000392 // for outer curve
joshualitt30ba4362014-08-21 20:18:45 -0700393 fsBuilder->codeAppendf("\tvec2 scaledOffset = %s.xy;\n", fsOffsetName0);
394 fsBuilder->codeAppend("\tfloat test = dot(scaledOffset, scaledOffset) - 1.0;\n");
395 fsBuilder->codeAppendf("\tvec2 duvdx = dFdx(%s);\n", fsOffsetName0);
396 fsBuilder->codeAppendf("\tvec2 duvdy = dFdy(%s);\n", fsOffsetName0);
397 fsBuilder->codeAppendf("\tvec2 grad = vec2(2.0*%s.x*duvdx.x + 2.0*%s.y*duvdx.y,\n"
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000398 "\t 2.0*%s.x*duvdy.x + 2.0*%s.y*duvdy.y);\n",
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000399 fsOffsetName0, fsOffsetName0, fsOffsetName0, fsOffsetName0);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000400
joshualitt30ba4362014-08-21 20:18:45 -0700401 fsBuilder->codeAppend("\tfloat grad_dot = dot(grad, grad);\n");
commit-bot@chromium.org1b035d82014-04-09 17:11:09 +0000402 // avoid calling inversesqrt on zero.
joshualitt30ba4362014-08-21 20:18:45 -0700403 fsBuilder->codeAppend("\tgrad_dot = max(grad_dot, 1.0e-4);\n");
404 fsBuilder->codeAppend("\tfloat invlen = inversesqrt(grad_dot);\n");
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000405 if (kHairline == ellipseEffect.getMode()) {
406 // can probably do this with one step
joshualitt30ba4362014-08-21 20:18:45 -0700407 fsBuilder->codeAppend("\tfloat edgeAlpha = clamp(1.0-test*invlen, 0.0, 1.0);\n");
408 fsBuilder->codeAppend("\tedgeAlpha *= clamp(1.0+test*invlen, 0.0, 1.0);\n");
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000409 } else {
joshualitt30ba4362014-08-21 20:18:45 -0700410 fsBuilder->codeAppend("\tfloat edgeAlpha = clamp(0.5-test*invlen, 0.0, 1.0);\n");
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000411 }
412
413 // for inner curve
414 if (kStroke == ellipseEffect.getMode()) {
joshualitt30ba4362014-08-21 20:18:45 -0700415 fsBuilder->codeAppendf("\tscaledOffset = %s.xy;\n", fsOffsetName1);
416 fsBuilder->codeAppend("\ttest = dot(scaledOffset, scaledOffset) - 1.0;\n");
417 fsBuilder->codeAppendf("\tduvdx = dFdx(%s);\n", fsOffsetName1);
418 fsBuilder->codeAppendf("\tduvdy = dFdy(%s);\n", fsOffsetName1);
419 fsBuilder->codeAppendf("\tgrad = vec2(2.0*%s.x*duvdx.x + 2.0*%s.y*duvdx.y,\n"
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000420 "\t 2.0*%s.x*duvdy.x + 2.0*%s.y*duvdy.y);\n",
421 fsOffsetName1, fsOffsetName1, fsOffsetName1, fsOffsetName1);
joshualitt30ba4362014-08-21 20:18:45 -0700422 fsBuilder->codeAppend("\tinvlen = inversesqrt(dot(grad, grad));\n");
423 fsBuilder->codeAppend("\tedgeAlpha *= clamp(0.5+test*invlen, 0.0, 1.0);\n");
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000424 }
425
joshualitt30ba4362014-08-21 20:18:45 -0700426 fsBuilder->codeAppendf("\t%s = %s;\n", outputColor,
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000427 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("edgeAlpha")).c_str());
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000428 }
429
bsalomon63e99f72014-07-21 08:03:14 -0700430 static void GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&,
431 GrEffectKeyBuilder* b) {
432 const DIEllipseEdgeEffect& ellipseEffect =
433 drawEffect.castEffect<DIEllipseEdgeEffect>();
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000434
bsalomon63e99f72014-07-21 08:03:14 -0700435 b->add32(ellipseEffect.getMode());
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000436 }
437
kkinnunen7510b222014-07-30 00:04:16 -0700438 virtual void setData(const GrGLProgramDataManager&, const GrDrawEffect&) SK_OVERRIDE {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000439 }
440
441 private:
joshualitt249af152014-09-15 11:41:13 -0700442 typedef GrGLGeometryProcessor INHERITED;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000443 };
444
445private:
joshualitt249af152014-09-15 11:41:13 -0700446 DIEllipseEdgeEffect(Mode mode)
447 : fInEllipseOffsets0(this->addVertexAttrib(
448 GrShaderVar("inEllipseOffsets0",
449 kVec2f_GrSLType,
450 GrShaderVar::kAttribute_TypeModifier)))
451 , fInEllipseOffsets1(this->addVertexAttrib(
452 GrShaderVar("inEllipseOffsets1",
453 kVec2f_GrSLType,
454 GrShaderVar::kAttribute_TypeModifier))) {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000455 fMode = mode;
456 }
457
458 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
459 const DIEllipseEdgeEffect& eee = CastEffect<DIEllipseEdgeEffect>(other);
460 return eee.fMode == fMode;
461 }
462
joshualitt249af152014-09-15 11:41:13 -0700463 const GrShaderVar& fInEllipseOffsets0;
464 const GrShaderVar& fInEllipseOffsets1;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000465 Mode fMode;
466
467 GR_DECLARE_EFFECT_TEST;
468
joshualitt249af152014-09-15 11:41:13 -0700469 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000470};
471
472GR_DEFINE_EFFECT_TEST(DIEllipseEdgeEffect);
473
bsalomon83d081a2014-07-08 09:56:10 -0700474GrEffect* DIEllipseEdgeEffect::TestCreate(SkRandom* random,
475 GrContext* context,
476 const GrDrawTargetCaps&,
477 GrTexture* textures[]) {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000478 return DIEllipseEdgeEffect::Create((Mode)(random->nextRangeU(0,2)));
479}
480
481///////////////////////////////////////////////////////////////////////////////
482
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000483void GrOvalRenderer::reset() {
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000484 SkSafeSetNull(fRRectIndexBuffer);
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000485}
486
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000487bool GrOvalRenderer::drawOval(GrDrawTarget* target, const GrContext* context, bool useAA,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000488 const SkRect& oval, const SkStrokeRec& stroke)
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000489{
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000490 bool useCoverageAA = useAA &&
491 !target->getDrawState().getRenderTarget()->isMultisampled() &&
492 !target->shouldDisableCoverageAAForBlend();
493
494 if (!useCoverageAA) {
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000495 return false;
496 }
497
498 const SkMatrix& vm = context->getMatrix();
499
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000500 // we can draw circles
501 if (SkScalarNearlyEqual(oval.width(), oval.height())
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000502 && circle_stays_circle(vm)) {
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000503 this->drawCircle(target, useCoverageAA, oval, stroke);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000504 // if we have shader derivative support, render as device-independent
505 } else if (target->caps()->shaderDerivativeSupport()) {
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000506 return this->drawDIEllipse(target, useCoverageAA, oval, stroke);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000507 // otherwise axis-aligned ellipses only
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000508 } else if (vm.rectStaysRect()) {
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000509 return this->drawEllipse(target, useCoverageAA, oval, stroke);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000510 } else {
511 return false;
512 }
513
514 return true;
515}
516
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000517///////////////////////////////////////////////////////////////////////////////
518
robertphillips@google.com42903302013-04-20 12:26:07 +0000519// position + edge
520extern const GrVertexAttrib gCircleVertexAttribs[] = {
521 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000522 {kVec4f_GrVertexAttribType, sizeof(SkPoint), kEffect_GrVertexAttribBinding}
robertphillips@google.com42903302013-04-20 12:26:07 +0000523};
524
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000525void GrOvalRenderer::drawCircle(GrDrawTarget* target,
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000526 bool useCoverageAA,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000527 const SkRect& circle,
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000528 const SkStrokeRec& stroke)
529{
530 GrDrawState* drawState = target->drawState();
531
532 const SkMatrix& vm = drawState->getViewMatrix();
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000533 SkPoint center = SkPoint::Make(circle.centerX(), circle.centerY());
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000534 vm.mapPoints(&center, 1);
535 SkScalar radius = vm.mapRadius(SkScalarHalf(circle.width()));
536 SkScalar strokeWidth = vm.mapRadius(stroke.getWidth());
537
bsalomon@google.com137f1342013-05-29 21:27:53 +0000538 GrDrawState::AutoViewMatrixRestore avmr;
539 if (!avmr.setIdentity(drawState)) {
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000540 return;
541 }
542
egdaniel7b3d5ee2014-08-28 05:41:14 -0700543 drawState->setVertexAttribs<gCircleVertexAttribs>(SK_ARRAY_COUNT(gCircleVertexAttribs),
544 sizeof(CircleVertex));
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000545
546 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
547 if (!geo.succeeded()) {
548 GrPrintf("Failed to get space for vertices!\n");
549 return;
550 }
551
552 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
553
554 SkStrokeRec::Style style = stroke.getStyle();
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000555 bool isStrokeOnly = SkStrokeRec::kStroke_Style == style ||
556 SkStrokeRec::kHairline_Style == style;
557 bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == style;
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000558
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000559 SkScalar innerRadius = 0.0f;
560 SkScalar outerRadius = radius;
561 SkScalar halfWidth = 0;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000562 if (hasStroke) {
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000563 if (SkScalarNearlyZero(strokeWidth)) {
564 halfWidth = SK_ScalarHalf;
565 } else {
566 halfWidth = SkScalarHalf(strokeWidth);
567 }
568
569 outerRadius += halfWidth;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000570 if (isStrokeOnly) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000571 innerRadius = radius - halfWidth;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000572 }
573 }
574
bsalomon83d081a2014-07-08 09:56:10 -0700575 GrEffect* effect = CircleEdgeEffect::Create(isStrokeOnly && innerRadius > 0);
joshualitt249af152014-09-15 11:41:13 -0700576 drawState->setGeometryProcessor(effect)->unref();
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +0000577
bsalomon@google.com58e30fe2013-04-01 19:01:20 +0000578 // The radii are outset for two reasons. First, it allows the shader to simply perform
579 // clamp(distance-to-center - radius, 0, 1). Second, the outer radius is used to compute the
580 // verts of the bounding box that is rendered and the outset ensures the box will cover all
581 // pixels partially covered by the circle.
582 outerRadius += SK_ScalarHalf;
583 innerRadius -= SK_ScalarHalf;
584
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000585 SkRect bounds = SkRect::MakeLTRB(
bsalomon@google.com58e30fe2013-04-01 19:01:20 +0000586 center.fX - outerRadius,
587 center.fY - outerRadius,
588 center.fX + outerRadius,
589 center.fY + outerRadius
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000590 );
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000591
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000592 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000593 verts[0].fOffset = SkPoint::Make(-outerRadius, -outerRadius);
594 verts[0].fOuterRadius = outerRadius;
595 verts[0].fInnerRadius = innerRadius;
596
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000597 verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
skia.committer@gmail.com46746762013-04-12 07:01:51 +0000598 verts[1].fOffset = SkPoint::Make(outerRadius, -outerRadius);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000599 verts[1].fOuterRadius = outerRadius;
600 verts[1].fInnerRadius = innerRadius;
601
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000602 verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000603 verts[2].fOffset = SkPoint::Make(-outerRadius, outerRadius);
604 verts[2].fOuterRadius = outerRadius;
605 verts[2].fInnerRadius = innerRadius;
606
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000607 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000608 verts[3].fOffset = SkPoint::Make(outerRadius, outerRadius);
609 verts[3].fOuterRadius = outerRadius;
610 verts[3].fInnerRadius = innerRadius;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000611
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000612 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000613}
614
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000615///////////////////////////////////////////////////////////////////////////////
616
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000617// position + offset + 1/radii
robertphillips@google.com42903302013-04-20 12:26:07 +0000618extern const GrVertexAttrib gEllipseVertexAttribs[] = {
619 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000620 {kVec2f_GrVertexAttribType, sizeof(SkPoint), kEffect_GrVertexAttribBinding},
621 {kVec4f_GrVertexAttribType, 2*sizeof(SkPoint), kEffect_GrVertexAttribBinding}
robertphillips@google.com42903302013-04-20 12:26:07 +0000622};
623
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000624// position + offsets
625extern const GrVertexAttrib gDIEllipseVertexAttribs[] = {
626 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000627 {kVec2f_GrVertexAttribType, sizeof(SkPoint), kEffect_GrVertexAttribBinding},
628 {kVec2f_GrVertexAttribType, 2*sizeof(SkPoint), kEffect_GrVertexAttribBinding},
robertphillips@google.com42903302013-04-20 12:26:07 +0000629};
630
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000631bool GrOvalRenderer::drawEllipse(GrDrawTarget* target,
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000632 bool useCoverageAA,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000633 const SkRect& ellipse,
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000634 const SkStrokeRec& stroke)
635{
636 GrDrawState* drawState = target->drawState();
637#ifdef SK_DEBUG
638 {
639 // we should have checked for this previously
640 bool isAxisAlignedEllipse = drawState->getViewMatrix().rectStaysRect();
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000641 SkASSERT(useCoverageAA && isAxisAlignedEllipse);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000642 }
643#endif
644
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000645 // do any matrix crunching before we reset the draw state for device coords
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000646 const SkMatrix& vm = drawState->getViewMatrix();
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000647 SkPoint center = SkPoint::Make(ellipse.centerX(), ellipse.centerY());
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000648 vm.mapPoints(&center, 1);
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000649 SkScalar ellipseXRadius = SkScalarHalf(ellipse.width());
650 SkScalar ellipseYRadius = SkScalarHalf(ellipse.height());
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +0000651 SkScalar xRadius = SkScalarAbs(vm[SkMatrix::kMScaleX]*ellipseXRadius +
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000652 vm[SkMatrix::kMSkewY]*ellipseYRadius);
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +0000653 SkScalar yRadius = SkScalarAbs(vm[SkMatrix::kMSkewX]*ellipseXRadius +
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000654 vm[SkMatrix::kMScaleY]*ellipseYRadius);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000655
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000656 // do (potentially) anisotropic mapping of stroke
657 SkVector scaledStroke;
658 SkScalar strokeWidth = stroke.getWidth();
659 scaledStroke.fX = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMScaleX] + vm[SkMatrix::kMSkewY]));
660 scaledStroke.fY = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMSkewX] + vm[SkMatrix::kMScaleY]));
661
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000662 SkStrokeRec::Style style = stroke.getStyle();
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000663 bool isStrokeOnly = SkStrokeRec::kStroke_Style == style ||
664 SkStrokeRec::kHairline_Style == style;
665 bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == style;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000666
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000667 SkScalar innerXRadius = 0;
668 SkScalar innerYRadius = 0;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000669 if (hasStroke) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000670 if (SkScalarNearlyZero(scaledStroke.length())) {
671 scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf);
672 } else {
673 scaledStroke.scale(SK_ScalarHalf);
674 }
675
676 // we only handle thick strokes for near-circular ellipses
677 if (scaledStroke.length() > SK_ScalarHalf &&
678 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
679 return false;
680 }
681
682 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
683 if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius ||
684 scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) {
685 return false;
686 }
687
688 // this is legit only if scale & translation (which should be the case at the moment)
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000689 if (isStrokeOnly) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000690 innerXRadius = xRadius - scaledStroke.fX;
691 innerYRadius = yRadius - scaledStroke.fY;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000692 }
693
694 xRadius += scaledStroke.fX;
695 yRadius += scaledStroke.fY;
696 }
697
bsalomon@google.com137f1342013-05-29 21:27:53 +0000698 GrDrawState::AutoViewMatrixRestore avmr;
699 if (!avmr.setIdentity(drawState)) {
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000700 return false;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000701 }
702
egdaniel7b3d5ee2014-08-28 05:41:14 -0700703 drawState->setVertexAttribs<gEllipseVertexAttribs>(SK_ARRAY_COUNT(gEllipseVertexAttribs),
704 sizeof(EllipseVertex));
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000705
706 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
707 if (!geo.succeeded()) {
708 GrPrintf("Failed to get space for vertices!\n");
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000709 return false;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000710 }
711
712 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices());
713
bsalomon83d081a2014-07-08 09:56:10 -0700714 GrEffect* effect = EllipseEdgeEffect::Create(isStrokeOnly &&
715 innerXRadius > 0 && innerYRadius > 0);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000716
joshualitt249af152014-09-15 11:41:13 -0700717 drawState->setGeometryProcessor(effect)->unref();
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000718
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000719 // Compute the reciprocals of the radii here to save time in the shader
720 SkScalar xRadRecip = SkScalarInvert(xRadius);
721 SkScalar yRadRecip = SkScalarInvert(yRadius);
722 SkScalar xInnerRadRecip = SkScalarInvert(innerXRadius);
723 SkScalar yInnerRadRecip = SkScalarInvert(innerYRadius);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000724
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000725 // We've extended the outer x radius out half a pixel to antialias.
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000726 // This will also expand the rect so all the pixels will be captured.
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000727 // TODO: Consider if we should use sqrt(2)/2 instead
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000728 xRadius += SK_ScalarHalf;
729 yRadius += SK_ScalarHalf;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000730
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000731 SkRect bounds = SkRect::MakeLTRB(
732 center.fX - xRadius,
733 center.fY - yRadius,
734 center.fX + xRadius,
735 center.fY + yRadius
736 );
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000737
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000738 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000739 verts[0].fOffset = SkPoint::Make(-xRadius, -yRadius);
740 verts[0].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
741 verts[0].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000742
743 verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000744 verts[1].fOffset = SkPoint::Make(xRadius, -yRadius);
745 verts[1].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
746 verts[1].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000747
748 verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000749 verts[2].fOffset = SkPoint::Make(-xRadius, yRadius);
750 verts[2].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
751 verts[2].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000752
753 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000754 verts[3].fOffset = SkPoint::Make(xRadius, yRadius);
755 verts[3].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
756 verts[3].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
skia.committer@gmail.com46746762013-04-12 07:01:51 +0000757
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000758 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds);
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000759
760 return true;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000761}
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000762
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000763bool GrOvalRenderer::drawDIEllipse(GrDrawTarget* target,
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000764 bool useCoverageAA,
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000765 const SkRect& ellipse,
766 const SkStrokeRec& stroke)
767{
768 GrDrawState* drawState = target->drawState();
769 const SkMatrix& vm = drawState->getViewMatrix();
770
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000771 SkPoint center = SkPoint::Make(ellipse.centerX(), ellipse.centerY());
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000772 SkScalar xRadius = SkScalarHalf(ellipse.width());
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000773 SkScalar yRadius = SkScalarHalf(ellipse.height());
774
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000775 SkStrokeRec::Style style = stroke.getStyle();
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000776 DIEllipseEdgeEffect::Mode mode = (SkStrokeRec::kStroke_Style == style) ?
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000777 DIEllipseEdgeEffect::kStroke :
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000778 (SkStrokeRec::kHairline_Style == style) ?
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000779 DIEllipseEdgeEffect::kHairline : DIEllipseEdgeEffect::kFill;
780
781 SkScalar innerXRadius = 0;
782 SkScalar innerYRadius = 0;
783 if (SkStrokeRec::kFill_Style != style && SkStrokeRec::kHairline_Style != style) {
784 SkScalar strokeWidth = stroke.getWidth();
785
786 if (SkScalarNearlyZero(strokeWidth)) {
787 strokeWidth = SK_ScalarHalf;
788 } else {
789 strokeWidth *= SK_ScalarHalf;
790 }
791
792 // we only handle thick strokes for near-circular ellipses
793 if (strokeWidth > SK_ScalarHalf &&
794 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
795 return false;
796 }
797
798 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
799 if (strokeWidth*(yRadius*yRadius) < (strokeWidth*strokeWidth)*xRadius ||
800 strokeWidth*(xRadius*xRadius) < (strokeWidth*strokeWidth)*yRadius) {
801 return false;
802 }
803
804 // set inner radius (if needed)
805 if (SkStrokeRec::kStroke_Style == style) {
806 innerXRadius = xRadius - strokeWidth;
807 innerYRadius = yRadius - strokeWidth;
808 }
809
810 xRadius += strokeWidth;
811 yRadius += strokeWidth;
812 }
813 if (DIEllipseEdgeEffect::kStroke == mode) {
814 mode = (innerXRadius > 0 && innerYRadius > 0) ? DIEllipseEdgeEffect::kStroke :
815 DIEllipseEdgeEffect::kFill;
816 }
817 SkScalar innerRatioX = SkScalarDiv(xRadius, innerXRadius);
818 SkScalar innerRatioY = SkScalarDiv(yRadius, innerYRadius);
819
egdaniel7b3d5ee2014-08-28 05:41:14 -0700820 drawState->setVertexAttribs<gDIEllipseVertexAttribs>(SK_ARRAY_COUNT(gDIEllipseVertexAttribs),
821 sizeof(DIEllipseVertex));
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000822
823 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
824 if (!geo.succeeded()) {
825 GrPrintf("Failed to get space for vertices!\n");
826 return false;
827 }
828
829 DIEllipseVertex* verts = reinterpret_cast<DIEllipseVertex*>(geo.vertices());
830
bsalomon83d081a2014-07-08 09:56:10 -0700831 GrEffect* effect = DIEllipseEdgeEffect::Create(mode);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000832
joshualitt249af152014-09-15 11:41:13 -0700833 drawState->setGeometryProcessor(effect)->unref();
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000834
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000835 // This expands the outer rect so that after CTM we end up with a half-pixel border
836 SkScalar a = vm[SkMatrix::kMScaleX];
837 SkScalar b = vm[SkMatrix::kMSkewX];
838 SkScalar c = vm[SkMatrix::kMSkewY];
839 SkScalar d = vm[SkMatrix::kMScaleY];
840 SkScalar geoDx = SkScalarDiv(SK_ScalarHalf, SkScalarSqrt(a*a + c*c));
841 SkScalar geoDy = SkScalarDiv(SK_ScalarHalf, SkScalarSqrt(b*b + d*d));
842 // This adjusts the "radius" to include the half-pixel border
843 SkScalar offsetDx = SkScalarDiv(geoDx, xRadius);
844 SkScalar offsetDy = SkScalarDiv(geoDy, yRadius);
845
846 SkRect bounds = SkRect::MakeLTRB(
847 center.fX - xRadius - geoDx,
848 center.fY - yRadius - geoDy,
849 center.fX + xRadius + geoDx,
850 center.fY + yRadius + geoDy
851 );
852
853 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
854 verts[0].fOuterOffset = SkPoint::Make(-1.0f - offsetDx, -1.0f - offsetDy);
855 verts[0].fInnerOffset = SkPoint::Make(-innerRatioX - offsetDx, -innerRatioY - offsetDy);
856
857 verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
858 verts[1].fOuterOffset = SkPoint::Make(1.0f + offsetDx, -1.0f - offsetDy);
859 verts[1].fInnerOffset = SkPoint::Make(innerRatioX + offsetDx, -innerRatioY - offsetDy);
860
861 verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
862 verts[2].fOuterOffset = SkPoint::Make(-1.0f - offsetDx, 1.0f + offsetDy);
863 verts[2].fInnerOffset = SkPoint::Make(-innerRatioX - offsetDx, innerRatioY + offsetDy);
864
865 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
866 verts[3].fOuterOffset = SkPoint::Make(1.0f + offsetDx, 1.0f + offsetDy);
867 verts[3].fInnerOffset = SkPoint::Make(innerRatioX + offsetDx, innerRatioY + offsetDy);
868
869 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds);
870
871 return true;
872}
873
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000874///////////////////////////////////////////////////////////////////////////////
875
876static const uint16_t gRRectIndices[] = {
877 // corners
878 0, 1, 5, 0, 5, 4,
879 2, 3, 7, 2, 7, 6,
880 8, 9, 13, 8, 13, 12,
881 10, 11, 15, 10, 15, 14,
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000882
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000883 // edges
884 1, 2, 6, 1, 6, 5,
885 4, 5, 9, 4, 9, 8,
886 6, 7, 11, 6, 11, 10,
887 9, 10, 14, 9, 14, 13,
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000888
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000889 // center
890 // we place this at the end so that we can ignore these indices when rendering stroke-only
891 5, 6, 10, 5, 10, 9
892};
893
894
895GrIndexBuffer* GrOvalRenderer::rRectIndexBuffer(GrGpu* gpu) {
896 if (NULL == fRRectIndexBuffer) {
897 fRRectIndexBuffer =
898 gpu->createIndexBuffer(sizeof(gRRectIndices), false);
bsalomon49f085d2014-09-05 13:34:00 -0700899 if (fRRectIndexBuffer) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000900#ifdef SK_DEBUG
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000901 bool updated =
902#endif
903 fRRectIndexBuffer->updateData(gRRectIndices,
904 sizeof(gRRectIndices));
905 GR_DEBUGASSERT(updated);
906 }
907 }
908 return fRRectIndexBuffer;
909}
910
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000911bool GrOvalRenderer::drawDRRect(GrDrawTarget* target, GrContext* context, bool useAA,
bsalomon8af05232014-06-03 06:34:58 -0700912 const SkRRect& origOuter, const SkRRect& origInner) {
913 bool applyAA = useAA &&
914 !target->getDrawState().getRenderTarget()->isMultisampled() &&
915 !target->shouldDisableCoverageAAForBlend();
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000916 GrDrawState::AutoRestoreEffects are;
917 if (!origInner.isEmpty()) {
918 SkTCopyOnFirstWrite<SkRRect> inner(origInner);
919 if (!context->getMatrix().isIdentity()) {
920 if (!origInner.transform(context->getMatrix(), inner.writable())) {
921 return false;
922 }
923 }
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000924 GrEffectEdgeType edgeType = applyAA ? kInverseFillAA_GrEffectEdgeType :
925 kInverseFillBW_GrEffectEdgeType;
bsalomon83d081a2014-07-08 09:56:10 -0700926 GrEffect* effect = GrRRectEffect::Create(edgeType, *inner);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000927 if (NULL == effect) {
928 return false;
929 }
930 are.set(target->drawState());
931 target->drawState()->addCoverageEffect(effect)->unref();
932 }
933
934 SkStrokeRec fillRec(SkStrokeRec::kFill_InitStyle);
bsalomon8af05232014-06-03 06:34:58 -0700935 if (this->drawRRect(target, context, useAA, origOuter, fillRec)) {
936 return true;
937 }
938
939 SkASSERT(!origOuter.isEmpty());
940 SkTCopyOnFirstWrite<SkRRect> outer(origOuter);
941 if (!context->getMatrix().isIdentity()) {
942 if (!origOuter.transform(context->getMatrix(), outer.writable())) {
943 return false;
944 }
945 }
946 GrEffectEdgeType edgeType = applyAA ? kFillAA_GrEffectEdgeType :
947 kFillBW_GrEffectEdgeType;
bsalomon83d081a2014-07-08 09:56:10 -0700948 GrEffect* effect = GrRRectEffect::Create(edgeType, *outer);
bsalomon8af05232014-06-03 06:34:58 -0700949 if (NULL == effect) {
950 return false;
951 }
952 if (!are.isSet()) {
953 are.set(target->drawState());
954 }
955 GrDrawState::AutoViewMatrixRestore avmr;
956 if (!avmr.setIdentity(target->drawState())) {
957 return false;
958 }
959 target->drawState()->addCoverageEffect(effect)->unref();
960 SkRect bounds = outer->getBounds();
961 if (applyAA) {
962 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
963 }
bsalomon01c8da12014-08-04 09:21:30 -0700964 target->drawRect(bounds, NULL, NULL);
bsalomon8af05232014-06-03 06:34:58 -0700965 return true;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000966}
967
968bool GrOvalRenderer::drawRRect(GrDrawTarget* target, GrContext* context, bool useAA,
969 const SkRRect& rrect, const SkStrokeRec& stroke) {
970 if (rrect.isOval()) {
971 return this->drawOval(target, context, useAA, rrect.getBounds(), stroke);
972 }
973
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000974 bool useCoverageAA = useAA &&
975 !target->getDrawState().getRenderTarget()->isMultisampled() &&
976 !target->shouldDisableCoverageAAForBlend();
977
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000978 // only anti-aliased rrects for now
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000979 if (!useCoverageAA) {
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000980 return false;
981 }
982
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000983 const SkMatrix& vm = context->getMatrix();
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000984
985 if (!vm.rectStaysRect() || !rrect.isSimple()) {
986 return false;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000987 }
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000988
989 // do any matrix crunching before we reset the draw state for device coords
990 const SkRect& rrectBounds = rrect.getBounds();
991 SkRect bounds;
992 vm.mapRect(&bounds, rrectBounds);
993
994 SkVector radii = rrect.getSimpleRadii();
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000995 SkScalar xRadius = SkScalarAbs(vm[SkMatrix::kMScaleX]*radii.fX +
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000996 vm[SkMatrix::kMSkewY]*radii.fY);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000997 SkScalar yRadius = SkScalarAbs(vm[SkMatrix::kMSkewX]*radii.fX +
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000998 vm[SkMatrix::kMScaleY]*radii.fY);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000999
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001000 SkStrokeRec::Style style = stroke.getStyle();
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001001
1002 // do (potentially) anisotropic mapping of stroke
1003 SkVector scaledStroke;
1004 SkScalar strokeWidth = stroke.getWidth();
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001005
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001006 bool isStrokeOnly = SkStrokeRec::kStroke_Style == style ||
1007 SkStrokeRec::kHairline_Style == style;
1008 bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == style;
1009
1010 if (hasStroke) {
1011 if (SkStrokeRec::kHairline_Style == style) {
1012 scaledStroke.set(1, 1);
1013 } else {
1014 scaledStroke.fX = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMScaleX] +
1015 vm[SkMatrix::kMSkewY]));
1016 scaledStroke.fY = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMSkewX] +
1017 vm[SkMatrix::kMScaleY]));
1018 }
1019
1020 // if half of strokewidth is greater than radius, we don't handle that right now
1021 if (SK_ScalarHalf*scaledStroke.fX > xRadius || SK_ScalarHalf*scaledStroke.fY > yRadius) {
1022 return false;
1023 }
1024 }
1025
1026 // The way the effect interpolates the offset-to-ellipse/circle-center attribute only works on
1027 // the interior of the rrect if the radii are >= 0.5. Otherwise, the inner rect of the nine-
1028 // patch will have fractional coverage. This only matters when the interior is actually filled.
1029 // We could consider falling back to rect rendering here, since a tiny radius is
1030 // indistinguishable from a square corner.
1031 if (!isStrokeOnly && (SK_ScalarHalf > xRadius || SK_ScalarHalf > yRadius)) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001032 return false;
1033 }
1034
1035 // reset to device coordinates
1036 GrDrawState* drawState = target->drawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +00001037 GrDrawState::AutoViewMatrixRestore avmr;
1038 if (!avmr.setIdentity(drawState)) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001039 return false;
1040 }
1041
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001042 GrIndexBuffer* indexBuffer = this->rRectIndexBuffer(context->getGpu());
1043 if (NULL == indexBuffer) {
1044 GrPrintf("Failed to create index buffer!\n");
1045 return false;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001046 }
1047
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001048 // if the corners are circles, use the circle renderer
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001049 if ((!hasStroke || scaledStroke.fX == scaledStroke.fY) && xRadius == yRadius) {
egdaniel7b3d5ee2014-08-28 05:41:14 -07001050 drawState->setVertexAttribs<gCircleVertexAttribs>(SK_ARRAY_COUNT(gCircleVertexAttribs),
1051 sizeof(CircleVertex));
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001052
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001053 GrDrawTarget::AutoReleaseGeometry geo(target, 16, 0);
1054 if (!geo.succeeded()) {
1055 GrPrintf("Failed to get space for vertices!\n");
1056 return false;
1057 }
1058 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001059
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001060 SkScalar innerRadius = 0.0f;
1061 SkScalar outerRadius = xRadius;
1062 SkScalar halfWidth = 0;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001063 if (hasStroke) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001064 if (SkScalarNearlyZero(scaledStroke.fX)) {
1065 halfWidth = SK_ScalarHalf;
1066 } else {
1067 halfWidth = SkScalarHalf(scaledStroke.fX);
1068 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001069
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001070 if (isStrokeOnly) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001071 innerRadius = xRadius - halfWidth;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001072 }
1073 outerRadius += halfWidth;
1074 bounds.outset(halfWidth, halfWidth);
1075 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001076
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001077 isStrokeOnly = (isStrokeOnly && innerRadius >= 0);
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +00001078
bsalomon83d081a2014-07-08 09:56:10 -07001079 GrEffect* effect = CircleEdgeEffect::Create(isStrokeOnly);
joshualitt249af152014-09-15 11:41:13 -07001080 drawState->setGeometryProcessor(effect)->unref();
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +00001081
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001082 // The radii are outset for two reasons. First, it allows the shader to simply perform
1083 // clamp(distance-to-center - radius, 0, 1). Second, the outer radius is used to compute the
1084 // verts of the bounding box that is rendered and the outset ensures the box will cover all
1085 // pixels partially covered by the circle.
1086 outerRadius += SK_ScalarHalf;
1087 innerRadius -= SK_ScalarHalf;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001088
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001089 // Expand the rect so all the pixels will be captured.
1090 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001091
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001092 SkScalar yCoords[4] = {
1093 bounds.fTop,
1094 bounds.fTop + outerRadius,
1095 bounds.fBottom - outerRadius,
1096 bounds.fBottom
1097 };
1098 SkScalar yOuterRadii[4] = {
1099 -outerRadius,
1100 0,
1101 0,
1102 outerRadius
1103 };
1104 for (int i = 0; i < 4; ++i) {
1105 verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]);
1106 verts->fOffset = SkPoint::Make(-outerRadius, yOuterRadii[i]);
1107 verts->fOuterRadius = outerRadius;
1108 verts->fInnerRadius = innerRadius;
1109 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001110
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001111 verts->fPos = SkPoint::Make(bounds.fLeft + outerRadius, yCoords[i]);
1112 verts->fOffset = SkPoint::Make(0, yOuterRadii[i]);
1113 verts->fOuterRadius = outerRadius;
1114 verts->fInnerRadius = innerRadius;
1115 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001116
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001117 verts->fPos = SkPoint::Make(bounds.fRight - outerRadius, yCoords[i]);
1118 verts->fOffset = SkPoint::Make(0, yOuterRadii[i]);
1119 verts->fOuterRadius = outerRadius;
1120 verts->fInnerRadius = innerRadius;
1121 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001122
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001123 verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]);
1124 verts->fOffset = SkPoint::Make(outerRadius, yOuterRadii[i]);
1125 verts->fOuterRadius = outerRadius;
1126 verts->fInnerRadius = innerRadius;
1127 verts++;
1128 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001129
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001130 // drop out the middle quad if we're stroked
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001131 int indexCnt = isStrokeOnly ? SK_ARRAY_COUNT(gRRectIndices) - 6 :
1132 SK_ARRAY_COUNT(gRRectIndices);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001133 target->setIndexSourceToBuffer(indexBuffer);
1134 target->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, 16, indexCnt, &bounds);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001135
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001136 // otherwise we use the ellipse renderer
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001137 } else {
egdaniel7b3d5ee2014-08-28 05:41:14 -07001138 drawState->setVertexAttribs<gEllipseVertexAttribs>(SK_ARRAY_COUNT(gEllipseVertexAttribs),
1139 sizeof(EllipseVertex));
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001140
1141 SkScalar innerXRadius = 0.0f;
1142 SkScalar innerYRadius = 0.0f;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001143 if (hasStroke) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001144 if (SkScalarNearlyZero(scaledStroke.length())) {
1145 scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf);
1146 } else {
1147 scaledStroke.scale(SK_ScalarHalf);
1148 }
1149
1150 // we only handle thick strokes for near-circular ellipses
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +00001151 if (scaledStroke.length() > SK_ScalarHalf &&
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001152 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
1153 return false;
1154 }
1155
1156 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
1157 if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius ||
1158 scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) {
1159 return false;
1160 }
1161
1162 // this is legit only if scale & translation (which should be the case at the moment)
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001163 if (isStrokeOnly) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001164 innerXRadius = xRadius - scaledStroke.fX;
1165 innerYRadius = yRadius - scaledStroke.fY;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001166 }
1167
1168 xRadius += scaledStroke.fX;
1169 yRadius += scaledStroke.fY;
1170 bounds.outset(scaledStroke.fX, scaledStroke.fY);
1171 }
jvanverth@google.come3647412013-05-08 15:31:43 +00001172
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001173 isStrokeOnly = (isStrokeOnly && innerXRadius >= 0 && innerYRadius >= 0);
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +00001174
jvanverth@google.come3647412013-05-08 15:31:43 +00001175 GrDrawTarget::AutoReleaseGeometry geo(target, 16, 0);
1176 if (!geo.succeeded()) {
1177 GrPrintf("Failed to get space for vertices!\n");
1178 return false;
1179 }
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001180 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices());
jvanverth@google.come3647412013-05-08 15:31:43 +00001181
bsalomon83d081a2014-07-08 09:56:10 -07001182 GrEffect* effect = EllipseEdgeEffect::Create(isStrokeOnly);
joshualitt249af152014-09-15 11:41:13 -07001183 drawState->setGeometryProcessor(effect)->unref();
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001184
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001185 // Compute the reciprocals of the radii here to save time in the shader
1186 SkScalar xRadRecip = SkScalarInvert(xRadius);
1187 SkScalar yRadRecip = SkScalarInvert(yRadius);
1188 SkScalar xInnerRadRecip = SkScalarInvert(innerXRadius);
1189 SkScalar yInnerRadRecip = SkScalarInvert(innerYRadius);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001190
1191 // Extend the radii out half a pixel to antialias.
1192 SkScalar xOuterRadius = xRadius + SK_ScalarHalf;
1193 SkScalar yOuterRadius = yRadius + SK_ScalarHalf;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001194
1195 // Expand the rect so all the pixels will be captured.
1196 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1197
1198 SkScalar yCoords[4] = {
1199 bounds.fTop,
1200 bounds.fTop + yOuterRadius,
1201 bounds.fBottom - yOuterRadius,
1202 bounds.fBottom
1203 };
1204 SkScalar yOuterOffsets[4] = {
jvanverth@google.comd1b5b142013-07-02 14:46:03 +00001205 yOuterRadius,
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001206 SK_ScalarNearlyZero, // we're using inversesqrt() in the shader, so can't be exactly 0
1207 SK_ScalarNearlyZero,
1208 yOuterRadius
1209 };
1210
1211 for (int i = 0; i < 4; ++i) {
1212 verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]);
jvanverth@google.comd1b5b142013-07-02 14:46:03 +00001213 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
1218 verts->fPos = SkPoint::Make(bounds.fLeft + xOuterRadius, yCoords[i]);
1219 verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001220 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1221 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001222 verts++;
1223
1224 verts->fPos = SkPoint::Make(bounds.fRight - xOuterRadius, yCoords[i]);
1225 verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001226 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1227 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001228 verts++;
1229
1230 verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]);
1231 verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001232 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1233 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001234 verts++;
1235 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001236
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001237 // drop out the middle quad if we're stroked
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001238 int indexCnt = isStrokeOnly ? SK_ARRAY_COUNT(gRRectIndices) - 6 :
1239 SK_ARRAY_COUNT(gRRectIndices);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001240 target->setIndexSourceToBuffer(indexBuffer);
1241 target->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, 16, indexCnt, &bounds);
1242 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001243
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001244 return true;
1245}