blob: 72a01813b4028e0edc010d51b93e4d9885787c36 [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
joshualitt47bb3822014-10-07 16:43:25 -070010#include "gl/builders/GrGLProgramBuilder.h"
joshualittb0a8a372014-09-23 09:50:21 -070011#include "gl/GrGLProcessor.h"
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000012#include "gl/GrGLSL.h"
joshualitt249af152014-09-15 11:41:13 -070013#include "gl/GrGLGeometryProcessor.h"
joshualittb0a8a372014-09-23 09:50:21 -070014#include "GrProcessor.h"
15#include "GrTBackendProcessorFactory.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
joshualittb0a8a372014-09-23 09:50:21 -070025#include "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 {
joshualitt5ead6da2014-10-22 16:00:29 -070029// TODO(joshualitt) add per vertex colors
commit-bot@chromium.org81312832013-03-22 18:34:09 +000030struct 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:
joshualittb0a8a372014-09-23 09:50:21 -070065 static GrGeometryProcessor* Create(bool stroke) {
bsalomon98b33eb2014-10-15 11:05:26 -070066 GR_CREATE_STATIC_PROCESSOR(gCircleStrokeEdge, CircleEdgeEffect, (true));
67 GR_CREATE_STATIC_PROCESSOR(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
joshualitt249af152014-09-15 11:41:13 -070078 const GrShaderVar& inCircleEdge() const { return fInCircleEdge; }
79
joshualittb0a8a372014-09-23 09:50:21 -070080 virtual const GrBackendGeometryProcessorFactory& getFactory() const SK_OVERRIDE {
81 return GrTBackendGeometryProcessorFactory<CircleEdgeEffect>::getInstance();
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000082 }
83
84 virtual ~CircleEdgeEffect() {}
85
86 static const char* Name() { return "CircleEdge"; }
87
88 inline bool isStroked() const { return fStroke; }
89
joshualittb0a8a372014-09-23 09:50:21 -070090 class GLProcessor : public GrGLGeometryProcessor {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000091 public:
joshualittb0a8a372014-09-23 09:50:21 -070092 GLProcessor(const GrBackendProcessorFactory& factory, const GrProcessor&)
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000093 : INHERITED (factory) {}
94
joshualittc369e7c2014-10-22 10:56:26 -070095 virtual void emitCode(const EmitArgs& args) SK_OVERRIDE {
96 const CircleEdgeEffect& circleEffect = args.fGP.cast<CircleEdgeEffect>();
joshualitt74077b92014-10-24 11:26:03 -070097 GrGLVertToFrag v(kVec4f_GrSLType);
98 args.fPB->addVarying("CircleEdge", &v);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000099
joshualittc369e7c2014-10-22 10:56:26 -0700100 GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder();;
joshualitt74077b92014-10-24 11:26:03 -0700101 vsBuilder->codeAppendf("%s = %s;", v.vsOut(), circleEffect.inCircleEdge().c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000102
joshualittc369e7c2014-10-22 10:56:26 -0700103 GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder();
joshualitt74077b92014-10-24 11:26:03 -0700104 fsBuilder->codeAppendf("float d = length(%s.xy);", v.fsIn());
105 fsBuilder->codeAppendf("float edgeAlpha = clamp(%s.z - d, 0.0, 1.0);", v.fsIn());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000106 if (circleEffect.isStroked()) {
joshualitt74077b92014-10-24 11:26:03 -0700107 fsBuilder->codeAppendf("float innerAlpha = clamp(d - %s.w, 0.0, 1.0);",
108 v.fsIn());
109 fsBuilder->codeAppend("edgeAlpha *= innerAlpha;");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000110 }
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000111
joshualitt74077b92014-10-24 11:26:03 -0700112 fsBuilder->codeAppendf("%s = %s;\n", args.fOutput,
joshualittc369e7c2014-10-22 10:56:26 -0700113 (GrGLSLExpr4(args.fInput) * GrGLSLExpr1("edgeAlpha")).c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000114 }
115
joshualittb0a8a372014-09-23 09:50:21 -0700116 static void GenKey(const GrProcessor& processor, const GrGLCaps&,
117 GrProcessorKeyBuilder* b) {
118 const CircleEdgeEffect& circleEffect = processor.cast<CircleEdgeEffect>();
bsalomon63e99f72014-07-21 08:03:14 -0700119 b->add32(circleEffect.isStroked());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000120 }
121
joshualittb0a8a372014-09-23 09:50:21 -0700122 virtual void setData(const GrGLProgramDataManager&, const GrProcessor&) SK_OVERRIDE {}
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000123
124 private:
joshualitt249af152014-09-15 11:41:13 -0700125 typedef GrGLGeometryProcessor INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000126 };
127
128
129private:
joshualitt249af152014-09-15 11:41:13 -0700130 CircleEdgeEffect(bool stroke)
131 : fInCircleEdge(this->addVertexAttrib(
132 GrShaderVar("inCircleEdge",
133 kVec4f_GrSLType,
134 GrShaderVar::kAttribute_TypeModifier))) {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000135 fStroke = stroke;
136 }
137
bsalomon0e08fc12014-10-15 08:19:04 -0700138 virtual bool onIsEqual(const GrGeometryProcessor& other) const SK_OVERRIDE {
joshualitt49586be2014-09-16 08:21:41 -0700139 const CircleEdgeEffect& cee = other.cast<CircleEdgeEffect>();
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000140 return cee.fStroke == fStroke;
141 }
142
egdaniel1a8ecdf2014-10-03 06:24:12 -0700143 virtual void onComputeInvariantOutput(InvariantOutput* inout) const SK_OVERRIDE {
egdanielccb2e382014-10-13 12:53:46 -0700144 inout->mulByUnknownAlpha();
egdaniel1a8ecdf2014-10-03 06:24:12 -0700145 }
146
joshualitt249af152014-09-15 11:41:13 -0700147 const GrShaderVar& fInCircleEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000148 bool fStroke;
149
joshualittb0a8a372014-09-23 09:50:21 -0700150 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000151
joshualitt249af152014-09-15 11:41:13 -0700152 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000153};
154
joshualittb0a8a372014-09-23 09:50:21 -0700155GR_DEFINE_GEOMETRY_PROCESSOR_TEST(CircleEdgeEffect);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000156
joshualittb0a8a372014-09-23 09:50:21 -0700157GrGeometryProcessor* 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
joshualitt249af152014-09-15 11:41:13 -0700174class EllipseEdgeEffect : public GrGeometryProcessor {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000175public:
joshualittb0a8a372014-09-23 09:50:21 -0700176 static GrGeometryProcessor* Create(bool stroke) {
bsalomon98b33eb2014-10-15 11:05:26 -0700177 GR_CREATE_STATIC_PROCESSOR(gEllipseStrokeEdge, EllipseEdgeEffect, (true));
178 GR_CREATE_STATIC_PROCESSOR(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
joshualittb0a8a372014-09-23 09:50:21 -0700189 virtual const GrBackendGeometryProcessorFactory& getFactory() const SK_OVERRIDE {
190 return GrTBackendGeometryProcessorFactory<EllipseEdgeEffect>::getInstance();
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000191 }
192
193 virtual ~EllipseEdgeEffect() {}
194
195 static const char* Name() { return "EllipseEdge"; }
196
joshualitt249af152014-09-15 11:41:13 -0700197 const GrShaderVar& inEllipseOffset() const { return fInEllipseOffset; }
198 const GrShaderVar& inEllipseRadii() const { return fInEllipseRadii; }
199
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000200 inline bool isStroked() const { return fStroke; }
201
joshualittb0a8a372014-09-23 09:50:21 -0700202 class GLProcessor : public GrGLGeometryProcessor {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000203 public:
joshualittb0a8a372014-09-23 09:50:21 -0700204 GLProcessor(const GrBackendProcessorFactory& factory, const GrProcessor&)
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000205 : INHERITED (factory) {}
206
joshualittc369e7c2014-10-22 10:56:26 -0700207 virtual void emitCode(const EmitArgs& args) SK_OVERRIDE {
208 const EllipseEdgeEffect& ellipseEffect = args.fGP.cast<EllipseEdgeEffect>();
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000209
joshualitt74077b92014-10-24 11:26:03 -0700210 GrGLVertToFrag ellipseOffsets(kVec2f_GrSLType);
211 args.fPB->addVarying("EllipseOffsets", &ellipseOffsets);
joshualitt30ba4362014-08-21 20:18:45 -0700212
joshualittc369e7c2014-10-22 10:56:26 -0700213 GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder();
joshualitt74077b92014-10-24 11:26:03 -0700214 vsBuilder->codeAppendf("%s = %s;", ellipseOffsets.vsOut(),
joshualitt249af152014-09-15 11:41:13 -0700215 ellipseEffect.inEllipseOffset().c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000216
joshualitt74077b92014-10-24 11:26:03 -0700217 GrGLVertToFrag ellipseRadii(kVec4f_GrSLType);
218 args.fPB->addVarying("EllipseRadii", &ellipseRadii);
219 vsBuilder->codeAppendf("%s = %s;", ellipseRadii.vsOut(),
220 ellipseEffect.inEllipseRadii().c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000221
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +0000222 // for outer curve
joshualittc369e7c2014-10-22 10:56:26 -0700223 GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder();
joshualitt74077b92014-10-24 11:26:03 -0700224 fsBuilder->codeAppendf("vec2 scaledOffset = %s*%s.xy;", ellipseOffsets.fsIn(),
225 ellipseRadii.fsIn());
226 fsBuilder->codeAppend("float test = dot(scaledOffset, scaledOffset) - 1.0;");
227 fsBuilder->codeAppendf("vec2 grad = 2.0*scaledOffset*%s.xy;", ellipseRadii.fsIn());
228 fsBuilder->codeAppend("float grad_dot = dot(grad, grad);");
229
commit-bot@chromium.org1b035d82014-04-09 17:11:09 +0000230 // avoid calling inversesqrt on zero.
joshualitt74077b92014-10-24 11:26:03 -0700231 fsBuilder->codeAppend("grad_dot = max(grad_dot, 1.0e-4);");
232 fsBuilder->codeAppend("float invlen = inversesqrt(grad_dot);");
233 fsBuilder->codeAppend("float edgeAlpha = clamp(0.5-test*invlen, 0.0, 1.0);");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000234
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000235 // for inner curve
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000236 if (ellipseEffect.isStroked()) {
joshualitt74077b92014-10-24 11:26:03 -0700237 fsBuilder->codeAppendf("scaledOffset = %s*%s.zw;",
238 ellipseOffsets.fsIn(), ellipseRadii.fsIn());
239 fsBuilder->codeAppend("test = dot(scaledOffset, scaledOffset) - 1.0;");
240 fsBuilder->codeAppendf("grad = 2.0*scaledOffset*%s.zw;",
241 ellipseRadii.fsIn());
242 fsBuilder->codeAppend("invlen = inversesqrt(dot(grad, grad));");
243 fsBuilder->codeAppend("edgeAlpha *= clamp(0.5+test*invlen, 0.0, 1.0);");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000244 }
245
joshualitt74077b92014-10-24 11:26:03 -0700246 fsBuilder->codeAppendf("%s = %s;", args.fOutput,
joshualittc369e7c2014-10-22 10:56:26 -0700247 (GrGLSLExpr4(args.fInput) * GrGLSLExpr1("edgeAlpha")).c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000248 }
249
joshualittb0a8a372014-09-23 09:50:21 -0700250 static void GenKey(const GrProcessor& processor, const GrGLCaps&,
251 GrProcessorKeyBuilder* b) {
252 const EllipseEdgeEffect& ellipseEffect = processor.cast<EllipseEdgeEffect>();
bsalomon63e99f72014-07-21 08:03:14 -0700253 b->add32(ellipseEffect.isStroked());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000254 }
255
joshualittb0a8a372014-09-23 09:50:21 -0700256 virtual void setData(const GrGLProgramDataManager&, const GrProcessor&) SK_OVERRIDE {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000257 }
258
259 private:
joshualitt249af152014-09-15 11:41:13 -0700260 typedef GrGLGeometryProcessor INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000261 };
262
263private:
joshualitt249af152014-09-15 11:41:13 -0700264 EllipseEdgeEffect(bool stroke)
265 : fInEllipseOffset(this->addVertexAttrib(
266 GrShaderVar("inEllipseOffset",
267 kVec2f_GrSLType,
268 GrShaderVar::kAttribute_TypeModifier)))
269 , fInEllipseRadii(this->addVertexAttrib(
270 GrShaderVar("inEllipseRadii",
271 kVec4f_GrSLType,
272 GrShaderVar::kAttribute_TypeModifier))) {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000273 fStroke = stroke;
274 }
275
bsalomon0e08fc12014-10-15 08:19:04 -0700276 virtual bool onIsEqual(const GrGeometryProcessor& other) const SK_OVERRIDE {
joshualitt49586be2014-09-16 08:21:41 -0700277 const EllipseEdgeEffect& eee = other.cast<EllipseEdgeEffect>();
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000278 return eee.fStroke == fStroke;
279 }
280
egdaniel1a8ecdf2014-10-03 06:24:12 -0700281 virtual void onComputeInvariantOutput(InvariantOutput* inout) const SK_OVERRIDE {
egdanielccb2e382014-10-13 12:53:46 -0700282 inout->mulByUnknownAlpha();
egdaniel1a8ecdf2014-10-03 06:24:12 -0700283 }
284
joshualitt249af152014-09-15 11:41:13 -0700285 const GrShaderVar& fInEllipseOffset;
286 const GrShaderVar& fInEllipseRadii;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000287 bool fStroke;
288
joshualittb0a8a372014-09-23 09:50:21 -0700289 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000290
joshualitt249af152014-09-15 11:41:13 -0700291 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000292};
293
joshualittb0a8a372014-09-23 09:50:21 -0700294GR_DEFINE_GEOMETRY_PROCESSOR_TEST(EllipseEdgeEffect);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000295
joshualittb0a8a372014-09-23 09:50:21 -0700296GrGeometryProcessor* EllipseEdgeEffect::TestCreate(SkRandom* random,
297 GrContext* context,
298 const GrDrawTargetCaps&,
299 GrTexture* textures[]) {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000300 return EllipseEdgeEffect::Create(random->nextBool());
301}
302
303///////////////////////////////////////////////////////////////////////////////
304
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000305/**
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000306 * The output of this effect is a modulation of the input color and coverage for an ellipse,
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000307 * specified as a 2D offset from center for both the outer and inner paths (if stroked). The
308 * implict equation used is for a unit circle (x^2 + y^2 - 1 = 0) and the edge corrected by
309 * using differentials.
310 *
311 * The result is device-independent and can be used with any affine matrix.
312 */
313
joshualitt249af152014-09-15 11:41:13 -0700314class DIEllipseEdgeEffect : public GrGeometryProcessor {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000315public:
316 enum Mode { kStroke = 0, kHairline, kFill };
317
joshualittb0a8a372014-09-23 09:50:21 -0700318 static GrGeometryProcessor* Create(Mode mode) {
bsalomon98b33eb2014-10-15 11:05:26 -0700319 GR_CREATE_STATIC_PROCESSOR(gEllipseStrokeEdge, DIEllipseEdgeEffect, (kStroke));
320 GR_CREATE_STATIC_PROCESSOR(gEllipseHairlineEdge, DIEllipseEdgeEffect, (kHairline));
321 GR_CREATE_STATIC_PROCESSOR(gEllipseFillEdge, DIEllipseEdgeEffect, (kFill));
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000322
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000323 if (kStroke == mode) {
324 gEllipseStrokeEdge->ref();
325 return gEllipseStrokeEdge;
326 } else if (kHairline == mode) {
327 gEllipseHairlineEdge->ref();
328 return gEllipseHairlineEdge;
329 } else {
330 gEllipseFillEdge->ref();
331 return gEllipseFillEdge;
332 }
333 }
334
joshualittb0a8a372014-09-23 09:50:21 -0700335 virtual const GrBackendGeometryProcessorFactory& getFactory() const SK_OVERRIDE {
336 return GrTBackendGeometryProcessorFactory<DIEllipseEdgeEffect>::getInstance();
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000337 }
338
339 virtual ~DIEllipseEdgeEffect() {}
340
341 static const char* Name() { return "DIEllipseEdge"; }
342
joshualitt249af152014-09-15 11:41:13 -0700343 const GrShaderVar& inEllipseOffsets0() const { return fInEllipseOffsets0; }
344 const GrShaderVar& inEllipseOffsets1() const { return fInEllipseOffsets1; }
345
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000346 inline Mode getMode() const { return fMode; }
347
joshualittb0a8a372014-09-23 09:50:21 -0700348 class GLProcessor : public GrGLGeometryProcessor {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000349 public:
joshualittb0a8a372014-09-23 09:50:21 -0700350 GLProcessor(const GrBackendProcessorFactory& factory, const GrProcessor&)
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000351 : INHERITED (factory) {}
352
joshualittc369e7c2014-10-22 10:56:26 -0700353 virtual void emitCode(const EmitArgs& args) SK_OVERRIDE {
354 const DIEllipseEdgeEffect& ellipseEffect = args.fGP.cast<DIEllipseEdgeEffect>();
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000355
joshualitt74077b92014-10-24 11:26:03 -0700356 GrGLVertToFrag offsets0(kVec2f_GrSLType);
357 args.fPB->addVarying("EllipseOffsets0", &offsets0);
joshualitt30ba4362014-08-21 20:18:45 -0700358
joshualittc369e7c2014-10-22 10:56:26 -0700359 GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder();
joshualitt74077b92014-10-24 11:26:03 -0700360 vsBuilder->codeAppendf("%s = %s;", offsets0.vsOut(),
joshualitt249af152014-09-15 11:41:13 -0700361 ellipseEffect.inEllipseOffsets0().c_str());
joshualitt74077b92014-10-24 11:26:03 -0700362
363 GrGLVertToFrag offsets1(kVec2f_GrSLType);
364 args.fPB->addVarying("EllipseOffsets1", &offsets1);
365 vsBuilder->codeAppendf("%s = %s;", offsets1.vsOut(),
joshualitt249af152014-09-15 11:41:13 -0700366 ellipseEffect.inEllipseOffsets1().c_str());
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000367
joshualittc369e7c2014-10-22 10:56:26 -0700368 GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -0700369 SkAssertResult(fsBuilder->enableFeature(
370 GrGLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature));
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000371 // for outer curve
joshualitt74077b92014-10-24 11:26:03 -0700372 fsBuilder->codeAppendf("vec2 scaledOffset = %s.xy;", offsets0.fsIn());
373 fsBuilder->codeAppend("float test = dot(scaledOffset, scaledOffset) - 1.0;");
374 fsBuilder->codeAppendf("vec2 duvdx = dFdx(%s);", offsets0.fsIn());
375 fsBuilder->codeAppendf("vec2 duvdy = dFdy(%s);", offsets0.fsIn());
376 fsBuilder->codeAppendf("vec2 grad = vec2(2.0*%s.x*duvdx.x + 2.0*%s.y*duvdx.y,"
377 " 2.0*%s.x*duvdy.x + 2.0*%s.y*duvdy.y);",
378 offsets0.fsIn(), offsets0.fsIn(), offsets0.fsIn(), offsets0.fsIn());
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000379
joshualitt74077b92014-10-24 11:26:03 -0700380 fsBuilder->codeAppend("float grad_dot = dot(grad, grad);");
commit-bot@chromium.org1b035d82014-04-09 17:11:09 +0000381 // avoid calling inversesqrt on zero.
joshualitt74077b92014-10-24 11:26:03 -0700382 fsBuilder->codeAppend("grad_dot = max(grad_dot, 1.0e-4);");
383 fsBuilder->codeAppend("float invlen = inversesqrt(grad_dot);");
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000384 if (kHairline == ellipseEffect.getMode()) {
385 // can probably do this with one step
joshualitt74077b92014-10-24 11:26:03 -0700386 fsBuilder->codeAppend("float edgeAlpha = clamp(1.0-test*invlen, 0.0, 1.0);");
387 fsBuilder->codeAppend("edgeAlpha *= clamp(1.0+test*invlen, 0.0, 1.0);");
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000388 } else {
joshualitt74077b92014-10-24 11:26:03 -0700389 fsBuilder->codeAppend("float edgeAlpha = clamp(0.5-test*invlen, 0.0, 1.0);");
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000390 }
391
392 // for inner curve
393 if (kStroke == ellipseEffect.getMode()) {
joshualitt74077b92014-10-24 11:26:03 -0700394 fsBuilder->codeAppendf("scaledOffset = %s.xy;", offsets1.fsIn());
395 fsBuilder->codeAppend("test = dot(scaledOffset, scaledOffset) - 1.0;");
396 fsBuilder->codeAppendf("duvdx = dFdx(%s);", offsets1.fsIn());
397 fsBuilder->codeAppendf("duvdy = dFdy(%s);", offsets1.fsIn());
398 fsBuilder->codeAppendf("grad = vec2(2.0*%s.x*duvdx.x + 2.0*%s.y*duvdx.y,"
399 " 2.0*%s.x*duvdy.x + 2.0*%s.y*duvdy.y);",
400 offsets1.fsIn(), offsets1.fsIn(), offsets1.fsIn(),
401 offsets1.fsIn());
402 fsBuilder->codeAppend("invlen = inversesqrt(dot(grad, grad));");
403 fsBuilder->codeAppend("edgeAlpha *= clamp(0.5+test*invlen, 0.0, 1.0);");
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000404 }
405
joshualitt74077b92014-10-24 11:26:03 -0700406 fsBuilder->codeAppendf("%s = %s;", args.fOutput,
joshualittc369e7c2014-10-22 10:56:26 -0700407 (GrGLSLExpr4(args.fInput) * GrGLSLExpr1("edgeAlpha")).c_str());
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000408 }
409
joshualittb0a8a372014-09-23 09:50:21 -0700410 static void GenKey(const GrProcessor& processor, const GrGLCaps&,
411 GrProcessorKeyBuilder* b) {
412 const DIEllipseEdgeEffect& ellipseEffect = processor.cast<DIEllipseEdgeEffect>();
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000413
bsalomon63e99f72014-07-21 08:03:14 -0700414 b->add32(ellipseEffect.getMode());
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000415 }
416
joshualittb0a8a372014-09-23 09:50:21 -0700417 virtual void setData(const GrGLProgramDataManager&, const GrProcessor&) SK_OVERRIDE {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000418 }
419
420 private:
joshualitt249af152014-09-15 11:41:13 -0700421 typedef GrGLGeometryProcessor INHERITED;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000422 };
423
424private:
joshualitt249af152014-09-15 11:41:13 -0700425 DIEllipseEdgeEffect(Mode mode)
426 : fInEllipseOffsets0(this->addVertexAttrib(
427 GrShaderVar("inEllipseOffsets0",
428 kVec2f_GrSLType,
429 GrShaderVar::kAttribute_TypeModifier)))
430 , fInEllipseOffsets1(this->addVertexAttrib(
431 GrShaderVar("inEllipseOffsets1",
432 kVec2f_GrSLType,
433 GrShaderVar::kAttribute_TypeModifier))) {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000434 fMode = mode;
435 }
436
bsalomon0e08fc12014-10-15 08:19:04 -0700437 virtual bool onIsEqual(const GrGeometryProcessor& other) const SK_OVERRIDE {
joshualitt49586be2014-09-16 08:21:41 -0700438 const DIEllipseEdgeEffect& eee = other.cast<DIEllipseEdgeEffect>();
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000439 return eee.fMode == fMode;
440 }
441
egdaniel1a8ecdf2014-10-03 06:24:12 -0700442 virtual void onComputeInvariantOutput(InvariantOutput* inout) const SK_OVERRIDE {
egdanielccb2e382014-10-13 12:53:46 -0700443 inout->mulByUnknownAlpha();
egdaniel1a8ecdf2014-10-03 06:24:12 -0700444 }
445
joshualitt249af152014-09-15 11:41:13 -0700446 const GrShaderVar& fInEllipseOffsets0;
447 const GrShaderVar& fInEllipseOffsets1;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000448 Mode fMode;
449
joshualittb0a8a372014-09-23 09:50:21 -0700450 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000451
joshualitt249af152014-09-15 11:41:13 -0700452 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000453};
454
joshualittb0a8a372014-09-23 09:50:21 -0700455GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DIEllipseEdgeEffect);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000456
joshualittb0a8a372014-09-23 09:50:21 -0700457GrGeometryProcessor* DIEllipseEdgeEffect::TestCreate(SkRandom* random,
458 GrContext* context,
459 const GrDrawTargetCaps&,
460 GrTexture* textures[]) {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000461 return DIEllipseEdgeEffect::Create((Mode)(random->nextRangeU(0,2)));
462}
463
464///////////////////////////////////////////////////////////////////////////////
465
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000466void GrOvalRenderer::reset() {
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000467 SkSafeSetNull(fRRectIndexBuffer);
joshualitt58a65442014-10-22 20:53:24 -0700468 SkSafeSetNull(fStrokeRRectIndexBuffer);
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000469}
470
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000471bool GrOvalRenderer::drawOval(GrDrawTarget* target, const GrContext* context, bool useAA,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000472 const SkRect& oval, const SkStrokeRec& stroke)
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000473{
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000474 bool useCoverageAA = useAA &&
475 !target->getDrawState().getRenderTarget()->isMultisampled() &&
476 !target->shouldDisableCoverageAAForBlend();
477
478 if (!useCoverageAA) {
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000479 return false;
480 }
481
482 const SkMatrix& vm = context->getMatrix();
483
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000484 // we can draw circles
485 if (SkScalarNearlyEqual(oval.width(), oval.height())
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000486 && circle_stays_circle(vm)) {
joshualitt5ead6da2014-10-22 16:00:29 -0700487 this->drawCircle(target, context, useCoverageAA, oval, stroke);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000488 // if we have shader derivative support, render as device-independent
489 } else if (target->caps()->shaderDerivativeSupport()) {
joshualitt5ead6da2014-10-22 16:00:29 -0700490 return this->drawDIEllipse(target, context, useCoverageAA, oval, stroke);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000491 // otherwise axis-aligned ellipses only
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000492 } else if (vm.rectStaysRect()) {
joshualitt5ead6da2014-10-22 16:00:29 -0700493 return this->drawEllipse(target, context, useCoverageAA, oval, stroke);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000494 } else {
495 return false;
496 }
497
498 return true;
499}
500
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000501///////////////////////////////////////////////////////////////////////////////
502
robertphillips@google.com42903302013-04-20 12:26:07 +0000503// position + edge
504extern const GrVertexAttrib gCircleVertexAttribs[] = {
505 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
joshualittb0a8a372014-09-23 09:50:21 -0700506 {kVec4f_GrVertexAttribType, sizeof(SkPoint), kGeometryProcessor_GrVertexAttribBinding}
robertphillips@google.com42903302013-04-20 12:26:07 +0000507};
508
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000509void GrOvalRenderer::drawCircle(GrDrawTarget* target,
joshualitt5ead6da2014-10-22 16:00:29 -0700510 const GrContext* context,
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000511 bool useCoverageAA,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000512 const SkRect& circle,
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000513 const SkStrokeRec& stroke)
514{
515 GrDrawState* drawState = target->drawState();
516
517 const SkMatrix& vm = drawState->getViewMatrix();
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000518 SkPoint center = SkPoint::Make(circle.centerX(), circle.centerY());
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000519 vm.mapPoints(&center, 1);
520 SkScalar radius = vm.mapRadius(SkScalarHalf(circle.width()));
521 SkScalar strokeWidth = vm.mapRadius(stroke.getWidth());
522
bsalomon@google.com137f1342013-05-29 21:27:53 +0000523 GrDrawState::AutoViewMatrixRestore avmr;
524 if (!avmr.setIdentity(drawState)) {
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000525 return;
526 }
527
egdaniel7b3d5ee2014-08-28 05:41:14 -0700528 drawState->setVertexAttribs<gCircleVertexAttribs>(SK_ARRAY_COUNT(gCircleVertexAttribs),
529 sizeof(CircleVertex));
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000530
531 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
532 if (!geo.succeeded()) {
533 GrPrintf("Failed to get space for vertices!\n");
534 return;
535 }
536
537 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
538
539 SkStrokeRec::Style style = stroke.getStyle();
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000540 bool isStrokeOnly = SkStrokeRec::kStroke_Style == style ||
541 SkStrokeRec::kHairline_Style == style;
542 bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == style;
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000543
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000544 SkScalar innerRadius = 0.0f;
545 SkScalar outerRadius = radius;
546 SkScalar halfWidth = 0;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000547 if (hasStroke) {
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000548 if (SkScalarNearlyZero(strokeWidth)) {
549 halfWidth = SK_ScalarHalf;
550 } else {
551 halfWidth = SkScalarHalf(strokeWidth);
552 }
553
554 outerRadius += halfWidth;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000555 if (isStrokeOnly) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000556 innerRadius = radius - halfWidth;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000557 }
558 }
559
joshualittb0a8a372014-09-23 09:50:21 -0700560 GrGeometryProcessor* gp = CircleEdgeEffect::Create(isStrokeOnly && innerRadius > 0);
561 drawState->setGeometryProcessor(gp)->unref();
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +0000562
bsalomon@google.com58e30fe2013-04-01 19:01:20 +0000563 // The radii are outset for two reasons. First, it allows the shader to simply perform
564 // clamp(distance-to-center - radius, 0, 1). Second, the outer radius is used to compute the
565 // verts of the bounding box that is rendered and the outset ensures the box will cover all
566 // pixels partially covered by the circle.
567 outerRadius += SK_ScalarHalf;
568 innerRadius -= SK_ScalarHalf;
569
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000570 SkRect bounds = SkRect::MakeLTRB(
bsalomon@google.com58e30fe2013-04-01 19:01:20 +0000571 center.fX - outerRadius,
572 center.fY - outerRadius,
573 center.fX + outerRadius,
574 center.fY + outerRadius
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000575 );
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000576
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000577 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000578 verts[0].fOffset = SkPoint::Make(-outerRadius, -outerRadius);
579 verts[0].fOuterRadius = outerRadius;
580 verts[0].fInnerRadius = innerRadius;
581
joshualitt5ead6da2014-10-22 16:00:29 -0700582 verts[1].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
583 verts[1].fOffset = SkPoint::Make(-outerRadius, outerRadius);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000584 verts[1].fOuterRadius = outerRadius;
585 verts[1].fInnerRadius = innerRadius;
586
joshualitt5ead6da2014-10-22 16:00:29 -0700587 verts[2].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
588 verts[2].fOffset = SkPoint::Make(outerRadius, outerRadius);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000589 verts[2].fOuterRadius = outerRadius;
590 verts[2].fInnerRadius = innerRadius;
591
joshualitt5ead6da2014-10-22 16:00:29 -0700592 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
593 verts[3].fOffset = SkPoint::Make(outerRadius, -outerRadius);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000594 verts[3].fOuterRadius = outerRadius;
595 verts[3].fInnerRadius = innerRadius;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000596
joshualitt5ead6da2014-10-22 16:00:29 -0700597 target->setIndexSourceToBuffer(context->getGpu()->getQuadIndexBuffer());
598 target->drawIndexedInstances(kTriangles_GrPrimitiveType, 1, 4, 6, &bounds);
599 target->resetIndexSource();
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000600}
601
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000602///////////////////////////////////////////////////////////////////////////////
603
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000604// position + offset + 1/radii
robertphillips@google.com42903302013-04-20 12:26:07 +0000605extern const GrVertexAttrib gEllipseVertexAttribs[] = {
606 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
joshualittb0a8a372014-09-23 09:50:21 -0700607 {kVec2f_GrVertexAttribType, sizeof(SkPoint), kGeometryProcessor_GrVertexAttribBinding},
608 {kVec4f_GrVertexAttribType, 2*sizeof(SkPoint), kGeometryProcessor_GrVertexAttribBinding}
robertphillips@google.com42903302013-04-20 12:26:07 +0000609};
610
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000611// position + offsets
612extern const GrVertexAttrib gDIEllipseVertexAttribs[] = {
613 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
joshualittb0a8a372014-09-23 09:50:21 -0700614 {kVec2f_GrVertexAttribType, sizeof(SkPoint), kGeometryProcessor_GrVertexAttribBinding},
615 {kVec2f_GrVertexAttribType, 2*sizeof(SkPoint), kGeometryProcessor_GrVertexAttribBinding},
robertphillips@google.com42903302013-04-20 12:26:07 +0000616};
617
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000618bool GrOvalRenderer::drawEllipse(GrDrawTarget* target,
joshualitt5ead6da2014-10-22 16:00:29 -0700619 const GrContext* context,
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000620 bool useCoverageAA,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000621 const SkRect& ellipse,
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000622 const SkStrokeRec& stroke)
623{
624 GrDrawState* drawState = target->drawState();
625#ifdef SK_DEBUG
626 {
627 // we should have checked for this previously
628 bool isAxisAlignedEllipse = drawState->getViewMatrix().rectStaysRect();
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000629 SkASSERT(useCoverageAA && isAxisAlignedEllipse);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000630 }
631#endif
632
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000633 // do any matrix crunching before we reset the draw state for device coords
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000634 const SkMatrix& vm = drawState->getViewMatrix();
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000635 SkPoint center = SkPoint::Make(ellipse.centerX(), ellipse.centerY());
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000636 vm.mapPoints(&center, 1);
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000637 SkScalar ellipseXRadius = SkScalarHalf(ellipse.width());
638 SkScalar ellipseYRadius = SkScalarHalf(ellipse.height());
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +0000639 SkScalar xRadius = SkScalarAbs(vm[SkMatrix::kMScaleX]*ellipseXRadius +
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000640 vm[SkMatrix::kMSkewY]*ellipseYRadius);
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +0000641 SkScalar yRadius = SkScalarAbs(vm[SkMatrix::kMSkewX]*ellipseXRadius +
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000642 vm[SkMatrix::kMScaleY]*ellipseYRadius);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000643
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000644 // do (potentially) anisotropic mapping of stroke
645 SkVector scaledStroke;
646 SkScalar strokeWidth = stroke.getWidth();
647 scaledStroke.fX = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMScaleX] + vm[SkMatrix::kMSkewY]));
648 scaledStroke.fY = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMSkewX] + vm[SkMatrix::kMScaleY]));
649
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000650 SkStrokeRec::Style style = stroke.getStyle();
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000651 bool isStrokeOnly = SkStrokeRec::kStroke_Style == style ||
652 SkStrokeRec::kHairline_Style == style;
653 bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == style;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000654
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000655 SkScalar innerXRadius = 0;
656 SkScalar innerYRadius = 0;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000657 if (hasStroke) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000658 if (SkScalarNearlyZero(scaledStroke.length())) {
659 scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf);
660 } else {
661 scaledStroke.scale(SK_ScalarHalf);
662 }
663
664 // we only handle thick strokes for near-circular ellipses
665 if (scaledStroke.length() > SK_ScalarHalf &&
666 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
667 return false;
668 }
669
670 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
671 if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius ||
672 scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) {
673 return false;
674 }
675
676 // this is legit only if scale & translation (which should be the case at the moment)
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000677 if (isStrokeOnly) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000678 innerXRadius = xRadius - scaledStroke.fX;
679 innerYRadius = yRadius - scaledStroke.fY;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000680 }
681
682 xRadius += scaledStroke.fX;
683 yRadius += scaledStroke.fY;
684 }
685
bsalomon@google.com137f1342013-05-29 21:27:53 +0000686 GrDrawState::AutoViewMatrixRestore avmr;
687 if (!avmr.setIdentity(drawState)) {
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000688 return false;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000689 }
690
egdaniel7b3d5ee2014-08-28 05:41:14 -0700691 drawState->setVertexAttribs<gEllipseVertexAttribs>(SK_ARRAY_COUNT(gEllipseVertexAttribs),
692 sizeof(EllipseVertex));
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000693
694 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
695 if (!geo.succeeded()) {
696 GrPrintf("Failed to get space for vertices!\n");
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000697 return false;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000698 }
699
700 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices());
701
joshualittb0a8a372014-09-23 09:50:21 -0700702 GrGeometryProcessor* gp = EllipseEdgeEffect::Create(isStrokeOnly &&
703 innerXRadius > 0 && innerYRadius > 0);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000704
joshualittb0a8a372014-09-23 09:50:21 -0700705 drawState->setGeometryProcessor(gp)->unref();
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000706
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000707 // Compute the reciprocals of the radii here to save time in the shader
708 SkScalar xRadRecip = SkScalarInvert(xRadius);
709 SkScalar yRadRecip = SkScalarInvert(yRadius);
710 SkScalar xInnerRadRecip = SkScalarInvert(innerXRadius);
711 SkScalar yInnerRadRecip = SkScalarInvert(innerYRadius);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000712
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000713 // We've extended the outer x radius out half a pixel to antialias.
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000714 // This will also expand the rect so all the pixels will be captured.
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000715 // TODO: Consider if we should use sqrt(2)/2 instead
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000716 xRadius += SK_ScalarHalf;
717 yRadius += SK_ScalarHalf;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000718
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000719 SkRect bounds = SkRect::MakeLTRB(
720 center.fX - xRadius,
721 center.fY - yRadius,
722 center.fX + xRadius,
723 center.fY + yRadius
724 );
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000725
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000726 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000727 verts[0].fOffset = SkPoint::Make(-xRadius, -yRadius);
728 verts[0].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
729 verts[0].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000730
joshualitt5ead6da2014-10-22 16:00:29 -0700731 verts[1].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
732 verts[1].fOffset = SkPoint::Make(-xRadius, yRadius);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000733 verts[1].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
734 verts[1].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000735
joshualitt5ead6da2014-10-22 16:00:29 -0700736 verts[2].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
737 verts[2].fOffset = SkPoint::Make(xRadius, yRadius);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000738 verts[2].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
739 verts[2].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000740
joshualitt5ead6da2014-10-22 16:00:29 -0700741 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
742 verts[3].fOffset = SkPoint::Make(xRadius, -yRadius);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000743 verts[3].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
744 verts[3].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
skia.committer@gmail.com46746762013-04-12 07:01:51 +0000745
joshualitt5ead6da2014-10-22 16:00:29 -0700746 target->setIndexSourceToBuffer(context->getGpu()->getQuadIndexBuffer());
747 target->drawIndexedInstances(kTriangles_GrPrimitiveType, 1, 4, 6, &bounds);
748 target->resetIndexSource();
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000749
750 return true;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000751}
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000752
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000753bool GrOvalRenderer::drawDIEllipse(GrDrawTarget* target,
joshualitt5ead6da2014-10-22 16:00:29 -0700754 const GrContext* context,
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000755 bool useCoverageAA,
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000756 const SkRect& ellipse,
757 const SkStrokeRec& stroke)
758{
759 GrDrawState* drawState = target->drawState();
760 const SkMatrix& vm = drawState->getViewMatrix();
761
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000762 SkPoint center = SkPoint::Make(ellipse.centerX(), ellipse.centerY());
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000763 SkScalar xRadius = SkScalarHalf(ellipse.width());
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000764 SkScalar yRadius = SkScalarHalf(ellipse.height());
765
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000766 SkStrokeRec::Style style = stroke.getStyle();
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000767 DIEllipseEdgeEffect::Mode mode = (SkStrokeRec::kStroke_Style == style) ?
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000768 DIEllipseEdgeEffect::kStroke :
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000769 (SkStrokeRec::kHairline_Style == style) ?
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000770 DIEllipseEdgeEffect::kHairline : DIEllipseEdgeEffect::kFill;
771
772 SkScalar innerXRadius = 0;
773 SkScalar innerYRadius = 0;
774 if (SkStrokeRec::kFill_Style != style && SkStrokeRec::kHairline_Style != style) {
775 SkScalar strokeWidth = stroke.getWidth();
776
777 if (SkScalarNearlyZero(strokeWidth)) {
778 strokeWidth = SK_ScalarHalf;
779 } else {
780 strokeWidth *= SK_ScalarHalf;
781 }
782
783 // we only handle thick strokes for near-circular ellipses
784 if (strokeWidth > SK_ScalarHalf &&
785 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
786 return false;
787 }
788
789 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
790 if (strokeWidth*(yRadius*yRadius) < (strokeWidth*strokeWidth)*xRadius ||
791 strokeWidth*(xRadius*xRadius) < (strokeWidth*strokeWidth)*yRadius) {
792 return false;
793 }
794
795 // set inner radius (if needed)
796 if (SkStrokeRec::kStroke_Style == style) {
797 innerXRadius = xRadius - strokeWidth;
798 innerYRadius = yRadius - strokeWidth;
799 }
800
801 xRadius += strokeWidth;
802 yRadius += strokeWidth;
803 }
804 if (DIEllipseEdgeEffect::kStroke == mode) {
805 mode = (innerXRadius > 0 && innerYRadius > 0) ? DIEllipseEdgeEffect::kStroke :
806 DIEllipseEdgeEffect::kFill;
807 }
808 SkScalar innerRatioX = SkScalarDiv(xRadius, innerXRadius);
809 SkScalar innerRatioY = SkScalarDiv(yRadius, innerYRadius);
810
egdaniel7b3d5ee2014-08-28 05:41:14 -0700811 drawState->setVertexAttribs<gDIEllipseVertexAttribs>(SK_ARRAY_COUNT(gDIEllipseVertexAttribs),
812 sizeof(DIEllipseVertex));
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000813
814 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
815 if (!geo.succeeded()) {
816 GrPrintf("Failed to get space for vertices!\n");
817 return false;
818 }
819
820 DIEllipseVertex* verts = reinterpret_cast<DIEllipseVertex*>(geo.vertices());
821
joshualittb0a8a372014-09-23 09:50:21 -0700822 GrGeometryProcessor* gp = DIEllipseEdgeEffect::Create(mode);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000823
joshualittb0a8a372014-09-23 09:50:21 -0700824 drawState->setGeometryProcessor(gp)->unref();
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000825
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000826 // This expands the outer rect so that after CTM we end up with a half-pixel border
827 SkScalar a = vm[SkMatrix::kMScaleX];
828 SkScalar b = vm[SkMatrix::kMSkewX];
829 SkScalar c = vm[SkMatrix::kMSkewY];
830 SkScalar d = vm[SkMatrix::kMScaleY];
831 SkScalar geoDx = SkScalarDiv(SK_ScalarHalf, SkScalarSqrt(a*a + c*c));
832 SkScalar geoDy = SkScalarDiv(SK_ScalarHalf, SkScalarSqrt(b*b + d*d));
833 // This adjusts the "radius" to include the half-pixel border
834 SkScalar offsetDx = SkScalarDiv(geoDx, xRadius);
835 SkScalar offsetDy = SkScalarDiv(geoDy, yRadius);
836
837 SkRect bounds = SkRect::MakeLTRB(
838 center.fX - xRadius - geoDx,
839 center.fY - yRadius - geoDy,
840 center.fX + xRadius + geoDx,
841 center.fY + yRadius + geoDy
842 );
843
844 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
845 verts[0].fOuterOffset = SkPoint::Make(-1.0f - offsetDx, -1.0f - offsetDy);
846 verts[0].fInnerOffset = SkPoint::Make(-innerRatioX - offsetDx, -innerRatioY - offsetDy);
847
joshualitt5ead6da2014-10-22 16:00:29 -0700848 verts[1].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
849 verts[1].fOuterOffset = SkPoint::Make(-1.0f - offsetDx, 1.0f + offsetDy);
850 verts[1].fInnerOffset = SkPoint::Make(-innerRatioX - offsetDx, innerRatioY + offsetDy);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000851
joshualitt5ead6da2014-10-22 16:00:29 -0700852 verts[2].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
853 verts[2].fOuterOffset = SkPoint::Make(1.0f + offsetDx, 1.0f + offsetDy);
854 verts[2].fInnerOffset = SkPoint::Make(innerRatioX + offsetDx, innerRatioY + offsetDy);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000855
joshualitt5ead6da2014-10-22 16:00:29 -0700856 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
857 verts[3].fOuterOffset = SkPoint::Make(1.0f + offsetDx, -1.0f - offsetDy);
858 verts[3].fInnerOffset = SkPoint::Make(innerRatioX + offsetDx, -innerRatioY - offsetDy);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000859
joshualitt5ead6da2014-10-22 16:00:29 -0700860 target->setIndexSourceToBuffer(context->getGpu()->getQuadIndexBuffer());
861 target->drawIndexedInstances(kTriangles_GrPrimitiveType, 1, 4, 6, &bounds);
862 target->resetIndexSource();
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000863
864 return true;
865}
866
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000867///////////////////////////////////////////////////////////////////////////////
868
869static const uint16_t gRRectIndices[] = {
870 // corners
871 0, 1, 5, 0, 5, 4,
872 2, 3, 7, 2, 7, 6,
873 8, 9, 13, 8, 13, 12,
874 10, 11, 15, 10, 15, 14,
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000875
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000876 // edges
877 1, 2, 6, 1, 6, 5,
878 4, 5, 9, 4, 9, 8,
879 6, 7, 11, 6, 11, 10,
880 9, 10, 14, 9, 14, 13,
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000881
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000882 // center
883 // we place this at the end so that we can ignore these indices when rendering stroke-only
884 5, 6, 10, 5, 10, 9
885};
886
joshualitt5ead6da2014-10-22 16:00:29 -0700887static const int kIndicesPerStrokeRRect = SK_ARRAY_COUNT(gRRectIndices) - 6;
888static const int kIndicesPerRRect = SK_ARRAY_COUNT(gRRectIndices);
889static const int kVertsPerRRect = 16;
890static const int kNumRRectsInIndexBuffer = 256;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000891
joshualitt5ead6da2014-10-22 16:00:29 -0700892GrIndexBuffer* GrOvalRenderer::rRectIndexBuffer(bool isStrokeOnly, GrGpu* gpu) {
893 if (isStrokeOnly) {
894 if (NULL == fStrokeRRectIndexBuffer) {
895 fStrokeRRectIndexBuffer = gpu->createInstancedIndexBuffer(gRRectIndices,
896 kIndicesPerStrokeRRect,
897 kNumRRectsInIndexBuffer,
898 kVertsPerRRect);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000899 }
joshualitt5ead6da2014-10-22 16:00:29 -0700900 return fStrokeRRectIndexBuffer;
901 } else {
902 if (NULL == fRRectIndexBuffer) {
903 fRRectIndexBuffer = gpu->createInstancedIndexBuffer(gRRectIndices,
904 kIndicesPerRRect,
905 kNumRRectsInIndexBuffer,
906 kVertsPerRRect);
907 }
908 return fRRectIndexBuffer;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000909 }
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000910}
911
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000912bool GrOvalRenderer::drawDRRect(GrDrawTarget* target, GrContext* context, bool useAA,
bsalomon8af05232014-06-03 06:34:58 -0700913 const SkRRect& origOuter, const SkRRect& origInner) {
914 bool applyAA = useAA &&
915 !target->getDrawState().getRenderTarget()->isMultisampled() &&
916 !target->shouldDisableCoverageAAForBlend();
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000917 GrDrawState::AutoRestoreEffects are;
918 if (!origInner.isEmpty()) {
919 SkTCopyOnFirstWrite<SkRRect> inner(origInner);
920 if (!context->getMatrix().isIdentity()) {
921 if (!origInner.transform(context->getMatrix(), inner.writable())) {
922 return false;
923 }
924 }
joshualittb0a8a372014-09-23 09:50:21 -0700925 GrPrimitiveEdgeType edgeType = applyAA ?
926 kInverseFillAA_GrProcessorEdgeType :
927 kInverseFillBW_GrProcessorEdgeType;
928 GrFragmentProcessor* fp = GrRRectEffect::Create(edgeType, *inner);
929 if (NULL == fp) {
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000930 return false;
931 }
932 are.set(target->drawState());
joshualittb0a8a372014-09-23 09:50:21 -0700933 target->drawState()->addCoverageProcessor(fp)->unref();
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000934 }
935
936 SkStrokeRec fillRec(SkStrokeRec::kFill_InitStyle);
bsalomon8af05232014-06-03 06:34:58 -0700937 if (this->drawRRect(target, context, useAA, origOuter, fillRec)) {
938 return true;
939 }
940
941 SkASSERT(!origOuter.isEmpty());
942 SkTCopyOnFirstWrite<SkRRect> outer(origOuter);
943 if (!context->getMatrix().isIdentity()) {
944 if (!origOuter.transform(context->getMatrix(), outer.writable())) {
945 return false;
946 }
947 }
joshualittb0a8a372014-09-23 09:50:21 -0700948 GrPrimitiveEdgeType edgeType = applyAA ? kFillAA_GrProcessorEdgeType :
949 kFillBW_GrProcessorEdgeType;
950 GrFragmentProcessor* effect = GrRRectEffect::Create(edgeType, *outer);
bsalomon8af05232014-06-03 06:34:58 -0700951 if (NULL == effect) {
952 return false;
953 }
954 if (!are.isSet()) {
955 are.set(target->drawState());
956 }
957 GrDrawState::AutoViewMatrixRestore avmr;
958 if (!avmr.setIdentity(target->drawState())) {
959 return false;
960 }
joshualittb0a8a372014-09-23 09:50:21 -0700961 target->drawState()->addCoverageProcessor(effect)->unref();
bsalomon8af05232014-06-03 06:34:58 -0700962 SkRect bounds = outer->getBounds();
963 if (applyAA) {
964 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
965 }
bsalomon01c8da12014-08-04 09:21:30 -0700966 target->drawRect(bounds, NULL, NULL);
bsalomon8af05232014-06-03 06:34:58 -0700967 return true;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000968}
969
970bool GrOvalRenderer::drawRRect(GrDrawTarget* target, GrContext* context, bool useAA,
971 const SkRRect& rrect, const SkStrokeRec& stroke) {
972 if (rrect.isOval()) {
973 return this->drawOval(target, context, useAA, rrect.getBounds(), stroke);
974 }
975
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000976 bool useCoverageAA = useAA &&
977 !target->getDrawState().getRenderTarget()->isMultisampled() &&
978 !target->shouldDisableCoverageAAForBlend();
979
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000980 // only anti-aliased rrects for now
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000981 if (!useCoverageAA) {
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000982 return false;
983 }
984
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000985 const SkMatrix& vm = context->getMatrix();
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000986
987 if (!vm.rectStaysRect() || !rrect.isSimple()) {
988 return false;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000989 }
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000990
991 // do any matrix crunching before we reset the draw state for device coords
992 const SkRect& rrectBounds = rrect.getBounds();
993 SkRect bounds;
994 vm.mapRect(&bounds, rrectBounds);
995
996 SkVector radii = rrect.getSimpleRadii();
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000997 SkScalar xRadius = SkScalarAbs(vm[SkMatrix::kMScaleX]*radii.fX +
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000998 vm[SkMatrix::kMSkewY]*radii.fY);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000999 SkScalar yRadius = SkScalarAbs(vm[SkMatrix::kMSkewX]*radii.fX +
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001000 vm[SkMatrix::kMScaleY]*radii.fY);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001001
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001002 SkStrokeRec::Style style = stroke.getStyle();
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001003
1004 // do (potentially) anisotropic mapping of stroke
1005 SkVector scaledStroke;
1006 SkScalar strokeWidth = stroke.getWidth();
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001007
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001008 bool isStrokeOnly = SkStrokeRec::kStroke_Style == style ||
1009 SkStrokeRec::kHairline_Style == style;
1010 bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == style;
1011
1012 if (hasStroke) {
1013 if (SkStrokeRec::kHairline_Style == style) {
1014 scaledStroke.set(1, 1);
1015 } else {
1016 scaledStroke.fX = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMScaleX] +
1017 vm[SkMatrix::kMSkewY]));
1018 scaledStroke.fY = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMSkewX] +
1019 vm[SkMatrix::kMScaleY]));
1020 }
1021
1022 // if half of strokewidth is greater than radius, we don't handle that right now
1023 if (SK_ScalarHalf*scaledStroke.fX > xRadius || SK_ScalarHalf*scaledStroke.fY > yRadius) {
1024 return false;
1025 }
1026 }
1027
1028 // The way the effect interpolates the offset-to-ellipse/circle-center attribute only works on
1029 // the interior of the rrect if the radii are >= 0.5. Otherwise, the inner rect of the nine-
1030 // patch will have fractional coverage. This only matters when the interior is actually filled.
1031 // We could consider falling back to rect rendering here, since a tiny radius is
1032 // indistinguishable from a square corner.
1033 if (!isStrokeOnly && (SK_ScalarHalf > xRadius || SK_ScalarHalf > yRadius)) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001034 return false;
1035 }
1036
1037 // reset to device coordinates
1038 GrDrawState* drawState = target->drawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +00001039 GrDrawState::AutoViewMatrixRestore avmr;
1040 if (!avmr.setIdentity(drawState)) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001041 return false;
1042 }
1043
joshualitt5ead6da2014-10-22 16:00:29 -07001044 GrIndexBuffer* indexBuffer = this->rRectIndexBuffer(isStrokeOnly, context->getGpu());
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001045 if (NULL == indexBuffer) {
1046 GrPrintf("Failed to create index buffer!\n");
1047 return false;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001048 }
1049
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001050 // if the corners are circles, use the circle renderer
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001051 if ((!hasStroke || scaledStroke.fX == scaledStroke.fY) && xRadius == yRadius) {
egdaniel7b3d5ee2014-08-28 05:41:14 -07001052 drawState->setVertexAttribs<gCircleVertexAttribs>(SK_ARRAY_COUNT(gCircleVertexAttribs),
1053 sizeof(CircleVertex));
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001054
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001055 GrDrawTarget::AutoReleaseGeometry geo(target, 16, 0);
1056 if (!geo.succeeded()) {
1057 GrPrintf("Failed to get space for vertices!\n");
1058 return false;
1059 }
1060 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001061
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001062 SkScalar innerRadius = 0.0f;
1063 SkScalar outerRadius = xRadius;
1064 SkScalar halfWidth = 0;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001065 if (hasStroke) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001066 if (SkScalarNearlyZero(scaledStroke.fX)) {
1067 halfWidth = SK_ScalarHalf;
1068 } else {
1069 halfWidth = SkScalarHalf(scaledStroke.fX);
1070 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001071
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001072 if (isStrokeOnly) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001073 innerRadius = xRadius - halfWidth;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001074 }
1075 outerRadius += halfWidth;
1076 bounds.outset(halfWidth, halfWidth);
1077 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001078
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001079 isStrokeOnly = (isStrokeOnly && innerRadius >= 0);
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +00001080
joshualittb0a8a372014-09-23 09:50:21 -07001081 GrGeometryProcessor* effect = CircleEdgeEffect::Create(isStrokeOnly);
joshualitt249af152014-09-15 11:41:13 -07001082 drawState->setGeometryProcessor(effect)->unref();
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +00001083
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001084 // The radii are outset for two reasons. First, it allows the shader to simply perform
1085 // clamp(distance-to-center - radius, 0, 1). Second, the outer radius is used to compute the
1086 // verts of the bounding box that is rendered and the outset ensures the box will cover all
1087 // pixels partially covered by the circle.
1088 outerRadius += SK_ScalarHalf;
1089 innerRadius -= SK_ScalarHalf;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001090
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001091 // Expand the rect so all the pixels will be captured.
1092 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001093
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001094 SkScalar yCoords[4] = {
1095 bounds.fTop,
1096 bounds.fTop + outerRadius,
1097 bounds.fBottom - outerRadius,
1098 bounds.fBottom
1099 };
1100 SkScalar yOuterRadii[4] = {
1101 -outerRadius,
1102 0,
1103 0,
1104 outerRadius
1105 };
1106 for (int i = 0; i < 4; ++i) {
1107 verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]);
1108 verts->fOffset = SkPoint::Make(-outerRadius, yOuterRadii[i]);
1109 verts->fOuterRadius = outerRadius;
1110 verts->fInnerRadius = innerRadius;
1111 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001112
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001113 verts->fPos = SkPoint::Make(bounds.fLeft + outerRadius, yCoords[i]);
1114 verts->fOffset = SkPoint::Make(0, yOuterRadii[i]);
1115 verts->fOuterRadius = outerRadius;
1116 verts->fInnerRadius = innerRadius;
1117 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001118
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001119 verts->fPos = SkPoint::Make(bounds.fRight - outerRadius, yCoords[i]);
1120 verts->fOffset = SkPoint::Make(0, yOuterRadii[i]);
1121 verts->fOuterRadius = outerRadius;
1122 verts->fInnerRadius = innerRadius;
1123 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001124
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001125 verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]);
1126 verts->fOffset = SkPoint::Make(outerRadius, yOuterRadii[i]);
1127 verts->fOuterRadius = outerRadius;
1128 verts->fInnerRadius = innerRadius;
1129 verts++;
1130 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001131
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001132 // drop out the middle quad if we're stroked
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001133 int indexCnt = isStrokeOnly ? SK_ARRAY_COUNT(gRRectIndices) - 6 :
1134 SK_ARRAY_COUNT(gRRectIndices);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001135 target->setIndexSourceToBuffer(indexBuffer);
joshualitt5ead6da2014-10-22 16:00:29 -07001136 target->drawIndexedInstances(kTriangles_GrPrimitiveType, 1, 16, indexCnt, &bounds);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001137
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001138 // otherwise we use the ellipse renderer
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001139 } else {
egdaniel7b3d5ee2014-08-28 05:41:14 -07001140 drawState->setVertexAttribs<gEllipseVertexAttribs>(SK_ARRAY_COUNT(gEllipseVertexAttribs),
1141 sizeof(EllipseVertex));
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001142
1143 SkScalar innerXRadius = 0.0f;
1144 SkScalar innerYRadius = 0.0f;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001145 if (hasStroke) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001146 if (SkScalarNearlyZero(scaledStroke.length())) {
1147 scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf);
1148 } else {
1149 scaledStroke.scale(SK_ScalarHalf);
1150 }
1151
1152 // we only handle thick strokes for near-circular ellipses
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +00001153 if (scaledStroke.length() > SK_ScalarHalf &&
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001154 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
1155 return false;
1156 }
1157
1158 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
1159 if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius ||
1160 scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) {
1161 return false;
1162 }
1163
1164 // this is legit only if scale & translation (which should be the case at the moment)
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001165 if (isStrokeOnly) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001166 innerXRadius = xRadius - scaledStroke.fX;
1167 innerYRadius = yRadius - scaledStroke.fY;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001168 }
1169
1170 xRadius += scaledStroke.fX;
1171 yRadius += scaledStroke.fY;
1172 bounds.outset(scaledStroke.fX, scaledStroke.fY);
1173 }
jvanverth@google.come3647412013-05-08 15:31:43 +00001174
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001175 isStrokeOnly = (isStrokeOnly && innerXRadius >= 0 && innerYRadius >= 0);
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +00001176
jvanverth@google.come3647412013-05-08 15:31:43 +00001177 GrDrawTarget::AutoReleaseGeometry geo(target, 16, 0);
1178 if (!geo.succeeded()) {
1179 GrPrintf("Failed to get space for vertices!\n");
1180 return false;
1181 }
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001182 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices());
jvanverth@google.come3647412013-05-08 15:31:43 +00001183
joshualittb0a8a372014-09-23 09:50:21 -07001184 GrGeometryProcessor* effect = EllipseEdgeEffect::Create(isStrokeOnly);
joshualitt249af152014-09-15 11:41:13 -07001185 drawState->setGeometryProcessor(effect)->unref();
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001186
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001187 // Compute the reciprocals of the radii here to save time in the shader
1188 SkScalar xRadRecip = SkScalarInvert(xRadius);
1189 SkScalar yRadRecip = SkScalarInvert(yRadius);
1190 SkScalar xInnerRadRecip = SkScalarInvert(innerXRadius);
1191 SkScalar yInnerRadRecip = SkScalarInvert(innerYRadius);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001192
1193 // Extend the radii out half a pixel to antialias.
1194 SkScalar xOuterRadius = xRadius + SK_ScalarHalf;
1195 SkScalar yOuterRadius = yRadius + SK_ScalarHalf;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001196
1197 // Expand the rect so all the pixels will be captured.
1198 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1199
1200 SkScalar yCoords[4] = {
1201 bounds.fTop,
1202 bounds.fTop + yOuterRadius,
1203 bounds.fBottom - yOuterRadius,
1204 bounds.fBottom
1205 };
1206 SkScalar yOuterOffsets[4] = {
jvanverth@google.comd1b5b142013-07-02 14:46:03 +00001207 yOuterRadius,
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001208 SK_ScalarNearlyZero, // we're using inversesqrt() in the shader, so can't be exactly 0
1209 SK_ScalarNearlyZero,
1210 yOuterRadius
1211 };
1212
1213 for (int i = 0; i < 4; ++i) {
1214 verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]);
jvanverth@google.comd1b5b142013-07-02 14:46:03 +00001215 verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001216 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1217 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001218 verts++;
1219
1220 verts->fPos = SkPoint::Make(bounds.fLeft + xOuterRadius, yCoords[i]);
1221 verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001222 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1223 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001224 verts++;
1225
1226 verts->fPos = SkPoint::Make(bounds.fRight - xOuterRadius, yCoords[i]);
1227 verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001228 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1229 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001230 verts++;
1231
1232 verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]);
1233 verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001234 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1235 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001236 verts++;
1237 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001238
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001239 // drop out the middle quad if we're stroked
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001240 int indexCnt = isStrokeOnly ? SK_ARRAY_COUNT(gRRectIndices) - 6 :
1241 SK_ARRAY_COUNT(gRRectIndices);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001242 target->setIndexSourceToBuffer(indexBuffer);
joshualitt5ead6da2014-10-22 16:00:29 -07001243 target->drawIndexedInstances(kTriangles_GrPrimitiveType, 1, 16, indexCnt, &bounds);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001244 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001245
joshualitt5ead6da2014-10-22 16:00:29 -07001246 target->resetIndexSource();
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001247 return true;
1248}