blob: 85a538973b844891df17b53f1942694bad425179 [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 {
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:
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>();
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000097 const char *vsName, *fsName;
joshualittc369e7c2014-10-22 10:56:26 -070098 args.fPB->addVarying(kVec4f_GrSLType, "CircleEdge", &vsName, &fsName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000099
joshualittc369e7c2014-10-22 10:56:26 -0700100 GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder();;
joshualitt249af152014-09-15 11:41:13 -0700101 vsBuilder->codeAppendf("\t%s = %s;\n", vsName, 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();
joshualitt30ba4362014-08-21 20:18:45 -0700104 fsBuilder->codeAppendf("\tfloat d = length(%s.xy);\n", fsName);
105 fsBuilder->codeAppendf("\tfloat edgeAlpha = clamp(%s.z - d, 0.0, 1.0);\n", fsName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000106 if (circleEffect.isStroked()) {
joshualitt30ba4362014-08-21 20:18:45 -0700107 fsBuilder->codeAppendf("\tfloat innerAlpha = clamp(d - %s.w, 0.0, 1.0);\n", fsName);
108 fsBuilder->codeAppend("\tedgeAlpha *= innerAlpha;\n");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000109 }
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000110
joshualittc369e7c2014-10-22 10:56:26 -0700111 fsBuilder->codeAppendf("\t%s = %s;\n", args.fOutput,
112 (GrGLSLExpr4(args.fInput) * GrGLSLExpr1("edgeAlpha")).c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000113 }
114
joshualittb0a8a372014-09-23 09:50:21 -0700115 static void GenKey(const GrProcessor& processor, const GrGLCaps&,
116 GrProcessorKeyBuilder* b) {
117 const CircleEdgeEffect& circleEffect = processor.cast<CircleEdgeEffect>();
bsalomon63e99f72014-07-21 08:03:14 -0700118 b->add32(circleEffect.isStroked());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000119 }
120
joshualittb0a8a372014-09-23 09:50:21 -0700121 virtual void setData(const GrGLProgramDataManager&, const GrProcessor&) SK_OVERRIDE {}
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000122
123 private:
joshualitt249af152014-09-15 11:41:13 -0700124 typedef GrGLGeometryProcessor INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000125 };
126
127
128private:
joshualitt249af152014-09-15 11:41:13 -0700129 CircleEdgeEffect(bool stroke)
130 : fInCircleEdge(this->addVertexAttrib(
131 GrShaderVar("inCircleEdge",
132 kVec4f_GrSLType,
133 GrShaderVar::kAttribute_TypeModifier))) {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000134 fStroke = stroke;
135 }
136
bsalomon0e08fc12014-10-15 08:19:04 -0700137 virtual bool onIsEqual(const GrGeometryProcessor& other) const SK_OVERRIDE {
joshualitt49586be2014-09-16 08:21:41 -0700138 const CircleEdgeEffect& cee = other.cast<CircleEdgeEffect>();
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000139 return cee.fStroke == fStroke;
140 }
141
egdaniel1a8ecdf2014-10-03 06:24:12 -0700142 virtual void onComputeInvariantOutput(InvariantOutput* inout) const SK_OVERRIDE {
egdanielccb2e382014-10-13 12:53:46 -0700143 inout->mulByUnknownAlpha();
egdaniel1a8ecdf2014-10-03 06:24:12 -0700144 }
145
joshualitt249af152014-09-15 11:41:13 -0700146 const GrShaderVar& fInCircleEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000147 bool fStroke;
148
joshualittb0a8a372014-09-23 09:50:21 -0700149 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000150
joshualitt249af152014-09-15 11:41:13 -0700151 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000152};
153
joshualittb0a8a372014-09-23 09:50:21 -0700154GR_DEFINE_GEOMETRY_PROCESSOR_TEST(CircleEdgeEffect);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000155
joshualittb0a8a372014-09-23 09:50:21 -0700156GrGeometryProcessor* CircleEdgeEffect::TestCreate(SkRandom* random,
157 GrContext* context,
158 const GrDrawTargetCaps&,
159 GrTexture* textures[]) {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000160 return CircleEdgeEffect::Create(random->nextBool());
161}
162
163///////////////////////////////////////////////////////////////////////////////
164
165/**
166 * 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 +0000167 * ellipse, specified as a 2D offset from center, and the reciprocals of the outer and inner radii,
168 * in both x and y directions.
169 *
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000170 * 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 +0000171 */
172
joshualitt249af152014-09-15 11:41:13 -0700173class EllipseEdgeEffect : public GrGeometryProcessor {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000174public:
joshualittb0a8a372014-09-23 09:50:21 -0700175 static GrGeometryProcessor* Create(bool stroke) {
bsalomon98b33eb2014-10-15 11:05:26 -0700176 GR_CREATE_STATIC_PROCESSOR(gEllipseStrokeEdge, EllipseEdgeEffect, (true));
177 GR_CREATE_STATIC_PROCESSOR(gEllipseFillEdge, EllipseEdgeEffect, (false));
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000178
179 if (stroke) {
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000180 gEllipseStrokeEdge->ref();
181 return gEllipseStrokeEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000182 } else {
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000183 gEllipseFillEdge->ref();
184 return gEllipseFillEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000185 }
186 }
187
joshualittb0a8a372014-09-23 09:50:21 -0700188 virtual const GrBackendGeometryProcessorFactory& getFactory() const SK_OVERRIDE {
189 return GrTBackendGeometryProcessorFactory<EllipseEdgeEffect>::getInstance();
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000190 }
191
192 virtual ~EllipseEdgeEffect() {}
193
194 static const char* Name() { return "EllipseEdge"; }
195
joshualitt249af152014-09-15 11:41:13 -0700196 const GrShaderVar& inEllipseOffset() const { return fInEllipseOffset; }
197 const GrShaderVar& inEllipseRadii() const { return fInEllipseRadii; }
198
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000199 inline bool isStroked() const { return fStroke; }
200
joshualittb0a8a372014-09-23 09:50:21 -0700201 class GLProcessor : public GrGLGeometryProcessor {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000202 public:
joshualittb0a8a372014-09-23 09:50:21 -0700203 GLProcessor(const GrBackendProcessorFactory& factory, const GrProcessor&)
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000204 : INHERITED (factory) {}
205
joshualittc369e7c2014-10-22 10:56:26 -0700206 virtual void emitCode(const EmitArgs& args) SK_OVERRIDE {
207 const EllipseEdgeEffect& ellipseEffect = args.fGP.cast<EllipseEdgeEffect>();
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000208
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000209 const char *vsOffsetName, *fsOffsetName;
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000210 const char *vsRadiiName, *fsRadiiName;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000211
joshualittc369e7c2014-10-22 10:56:26 -0700212 args.fPB->addVarying(kVec2f_GrSLType, "EllipseOffsets", &vsOffsetName, &fsOffsetName);
joshualitt30ba4362014-08-21 20:18:45 -0700213
joshualittc369e7c2014-10-22 10:56:26 -0700214 GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder();
joshualitt249af152014-09-15 11:41:13 -0700215 vsBuilder->codeAppendf("%s = %s;", vsOffsetName,
216 ellipseEffect.inEllipseOffset().c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000217
joshualittc369e7c2014-10-22 10:56:26 -0700218 args.fPB->addVarying(kVec4f_GrSLType, "EllipseRadii", &vsRadiiName, &fsRadiiName);
joshualitt249af152014-09-15 11:41:13 -0700219 vsBuilder->codeAppendf("%s = %s;", vsRadiiName, ellipseEffect.inEllipseRadii().c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000220
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +0000221 // for outer curve
joshualittc369e7c2014-10-22 10:56:26 -0700222 GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -0700223 fsBuilder->codeAppendf("\tvec2 scaledOffset = %s*%s.xy;\n", fsOffsetName, fsRadiiName);
224 fsBuilder->codeAppend("\tfloat test = dot(scaledOffset, scaledOffset) - 1.0;\n");
225 fsBuilder->codeAppendf("\tvec2 grad = 2.0*scaledOffset*%s.xy;\n", fsRadiiName);
226 fsBuilder->codeAppend("\tfloat grad_dot = dot(grad, grad);\n");
commit-bot@chromium.org1b035d82014-04-09 17:11:09 +0000227 // avoid calling inversesqrt on zero.
joshualitt30ba4362014-08-21 20:18:45 -0700228 fsBuilder->codeAppend("\tgrad_dot = max(grad_dot, 1.0e-4);\n");
229 fsBuilder->codeAppend("\tfloat invlen = inversesqrt(grad_dot);\n");
230 fsBuilder->codeAppend("\tfloat edgeAlpha = clamp(0.5-test*invlen, 0.0, 1.0);\n");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000231
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000232 // for inner curve
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000233 if (ellipseEffect.isStroked()) {
joshualitt30ba4362014-08-21 20:18:45 -0700234 fsBuilder->codeAppendf("\tscaledOffset = %s*%s.zw;\n", fsOffsetName, fsRadiiName);
235 fsBuilder->codeAppend("\ttest = dot(scaledOffset, scaledOffset) - 1.0;\n");
236 fsBuilder->codeAppendf("\tgrad = 2.0*scaledOffset*%s.zw;\n", fsRadiiName);
237 fsBuilder->codeAppend("\tinvlen = inversesqrt(dot(grad, grad));\n");
238 fsBuilder->codeAppend("\tedgeAlpha *= clamp(0.5+test*invlen, 0.0, 1.0);\n");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000239 }
240
joshualittc369e7c2014-10-22 10:56:26 -0700241 fsBuilder->codeAppendf("\t%s = %s;\n", args.fOutput,
242 (GrGLSLExpr4(args.fInput) * GrGLSLExpr1("edgeAlpha")).c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000243 }
244
joshualittb0a8a372014-09-23 09:50:21 -0700245 static void GenKey(const GrProcessor& processor, const GrGLCaps&,
246 GrProcessorKeyBuilder* b) {
247 const EllipseEdgeEffect& ellipseEffect = processor.cast<EllipseEdgeEffect>();
bsalomon63e99f72014-07-21 08:03:14 -0700248 b->add32(ellipseEffect.isStroked());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000249 }
250
joshualittb0a8a372014-09-23 09:50:21 -0700251 virtual void setData(const GrGLProgramDataManager&, const GrProcessor&) SK_OVERRIDE {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000252 }
253
254 private:
joshualitt249af152014-09-15 11:41:13 -0700255 typedef GrGLGeometryProcessor INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000256 };
257
258private:
joshualitt249af152014-09-15 11:41:13 -0700259 EllipseEdgeEffect(bool stroke)
260 : fInEllipseOffset(this->addVertexAttrib(
261 GrShaderVar("inEllipseOffset",
262 kVec2f_GrSLType,
263 GrShaderVar::kAttribute_TypeModifier)))
264 , fInEllipseRadii(this->addVertexAttrib(
265 GrShaderVar("inEllipseRadii",
266 kVec4f_GrSLType,
267 GrShaderVar::kAttribute_TypeModifier))) {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000268 fStroke = stroke;
269 }
270
bsalomon0e08fc12014-10-15 08:19:04 -0700271 virtual bool onIsEqual(const GrGeometryProcessor& other) const SK_OVERRIDE {
joshualitt49586be2014-09-16 08:21:41 -0700272 const EllipseEdgeEffect& eee = other.cast<EllipseEdgeEffect>();
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000273 return eee.fStroke == fStroke;
274 }
275
egdaniel1a8ecdf2014-10-03 06:24:12 -0700276 virtual void onComputeInvariantOutput(InvariantOutput* inout) const SK_OVERRIDE {
egdanielccb2e382014-10-13 12:53:46 -0700277 inout->mulByUnknownAlpha();
egdaniel1a8ecdf2014-10-03 06:24:12 -0700278 }
279
joshualitt249af152014-09-15 11:41:13 -0700280 const GrShaderVar& fInEllipseOffset;
281 const GrShaderVar& fInEllipseRadii;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000282 bool fStroke;
283
joshualittb0a8a372014-09-23 09:50:21 -0700284 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000285
joshualitt249af152014-09-15 11:41:13 -0700286 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000287};
288
joshualittb0a8a372014-09-23 09:50:21 -0700289GR_DEFINE_GEOMETRY_PROCESSOR_TEST(EllipseEdgeEffect);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000290
joshualittb0a8a372014-09-23 09:50:21 -0700291GrGeometryProcessor* EllipseEdgeEffect::TestCreate(SkRandom* random,
292 GrContext* context,
293 const GrDrawTargetCaps&,
294 GrTexture* textures[]) {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000295 return EllipseEdgeEffect::Create(random->nextBool());
296}
297
298///////////////////////////////////////////////////////////////////////////////
299
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000300/**
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000301 * 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 +0000302 * specified as a 2D offset from center for both the outer and inner paths (if stroked). The
303 * implict equation used is for a unit circle (x^2 + y^2 - 1 = 0) and the edge corrected by
304 * using differentials.
305 *
306 * The result is device-independent and can be used with any affine matrix.
307 */
308
joshualitt249af152014-09-15 11:41:13 -0700309class DIEllipseEdgeEffect : public GrGeometryProcessor {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000310public:
311 enum Mode { kStroke = 0, kHairline, kFill };
312
joshualittb0a8a372014-09-23 09:50:21 -0700313 static GrGeometryProcessor* Create(Mode mode) {
bsalomon98b33eb2014-10-15 11:05:26 -0700314 GR_CREATE_STATIC_PROCESSOR(gEllipseStrokeEdge, DIEllipseEdgeEffect, (kStroke));
315 GR_CREATE_STATIC_PROCESSOR(gEllipseHairlineEdge, DIEllipseEdgeEffect, (kHairline));
316 GR_CREATE_STATIC_PROCESSOR(gEllipseFillEdge, DIEllipseEdgeEffect, (kFill));
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000317
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000318 if (kStroke == mode) {
319 gEllipseStrokeEdge->ref();
320 return gEllipseStrokeEdge;
321 } else if (kHairline == mode) {
322 gEllipseHairlineEdge->ref();
323 return gEllipseHairlineEdge;
324 } else {
325 gEllipseFillEdge->ref();
326 return gEllipseFillEdge;
327 }
328 }
329
joshualittb0a8a372014-09-23 09:50:21 -0700330 virtual const GrBackendGeometryProcessorFactory& getFactory() const SK_OVERRIDE {
331 return GrTBackendGeometryProcessorFactory<DIEllipseEdgeEffect>::getInstance();
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000332 }
333
334 virtual ~DIEllipseEdgeEffect() {}
335
336 static const char* Name() { return "DIEllipseEdge"; }
337
joshualitt249af152014-09-15 11:41:13 -0700338 const GrShaderVar& inEllipseOffsets0() const { return fInEllipseOffsets0; }
339 const GrShaderVar& inEllipseOffsets1() const { return fInEllipseOffsets1; }
340
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000341 inline Mode getMode() const { return fMode; }
342
joshualittb0a8a372014-09-23 09:50:21 -0700343 class GLProcessor : public GrGLGeometryProcessor {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000344 public:
joshualittb0a8a372014-09-23 09:50:21 -0700345 GLProcessor(const GrBackendProcessorFactory& factory, const GrProcessor&)
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000346 : INHERITED (factory) {}
347
joshualittc369e7c2014-10-22 10:56:26 -0700348 virtual void emitCode(const EmitArgs& args) SK_OVERRIDE {
349 const DIEllipseEdgeEffect& ellipseEffect = args.fGP.cast<DIEllipseEdgeEffect>();
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000350
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000351 const char *vsOffsetName0, *fsOffsetName0;
joshualittc369e7c2014-10-22 10:56:26 -0700352 args.fPB->addVarying(kVec2f_GrSLType, "EllipseOffsets0",
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000353 &vsOffsetName0, &fsOffsetName0);
joshualitt30ba4362014-08-21 20:18:45 -0700354
joshualittc369e7c2014-10-22 10:56:26 -0700355 GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder();
joshualitt249af152014-09-15 11:41:13 -0700356 vsBuilder->codeAppendf("%s = %s;", vsOffsetName0,
357 ellipseEffect.inEllipseOffsets0().c_str());
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000358 const char *vsOffsetName1, *fsOffsetName1;
joshualittc369e7c2014-10-22 10:56:26 -0700359 args.fPB->addVarying(kVec2f_GrSLType, "EllipseOffsets1",
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000360 &vsOffsetName1, &fsOffsetName1);
joshualitt249af152014-09-15 11:41:13 -0700361 vsBuilder->codeAppendf("\t%s = %s;\n", vsOffsetName1,
362 ellipseEffect.inEllipseOffsets1().c_str());
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000363
joshualittc369e7c2014-10-22 10:56:26 -0700364 GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -0700365 SkAssertResult(fsBuilder->enableFeature(
366 GrGLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature));
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000367 // for outer curve
joshualitt30ba4362014-08-21 20:18:45 -0700368 fsBuilder->codeAppendf("\tvec2 scaledOffset = %s.xy;\n", fsOffsetName0);
369 fsBuilder->codeAppend("\tfloat test = dot(scaledOffset, scaledOffset) - 1.0;\n");
370 fsBuilder->codeAppendf("\tvec2 duvdx = dFdx(%s);\n", fsOffsetName0);
371 fsBuilder->codeAppendf("\tvec2 duvdy = dFdy(%s);\n", fsOffsetName0);
372 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 +0000373 "\t 2.0*%s.x*duvdy.x + 2.0*%s.y*duvdy.y);\n",
commit-bot@chromium.org96a7a962013-09-06 19:46:48 +0000374 fsOffsetName0, fsOffsetName0, fsOffsetName0, fsOffsetName0);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000375
joshualitt30ba4362014-08-21 20:18:45 -0700376 fsBuilder->codeAppend("\tfloat grad_dot = dot(grad, grad);\n");
commit-bot@chromium.org1b035d82014-04-09 17:11:09 +0000377 // avoid calling inversesqrt on zero.
joshualitt30ba4362014-08-21 20:18:45 -0700378 fsBuilder->codeAppend("\tgrad_dot = max(grad_dot, 1.0e-4);\n");
379 fsBuilder->codeAppend("\tfloat invlen = inversesqrt(grad_dot);\n");
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000380 if (kHairline == ellipseEffect.getMode()) {
381 // can probably do this with one step
joshualitt30ba4362014-08-21 20:18:45 -0700382 fsBuilder->codeAppend("\tfloat edgeAlpha = clamp(1.0-test*invlen, 0.0, 1.0);\n");
383 fsBuilder->codeAppend("\tedgeAlpha *= clamp(1.0+test*invlen, 0.0, 1.0);\n");
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000384 } else {
joshualitt30ba4362014-08-21 20:18:45 -0700385 fsBuilder->codeAppend("\tfloat edgeAlpha = clamp(0.5-test*invlen, 0.0, 1.0);\n");
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000386 }
387
388 // for inner curve
389 if (kStroke == ellipseEffect.getMode()) {
joshualitt30ba4362014-08-21 20:18:45 -0700390 fsBuilder->codeAppendf("\tscaledOffset = %s.xy;\n", fsOffsetName1);
391 fsBuilder->codeAppend("\ttest = dot(scaledOffset, scaledOffset) - 1.0;\n");
392 fsBuilder->codeAppendf("\tduvdx = dFdx(%s);\n", fsOffsetName1);
393 fsBuilder->codeAppendf("\tduvdy = dFdy(%s);\n", fsOffsetName1);
394 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 +0000395 "\t 2.0*%s.x*duvdy.x + 2.0*%s.y*duvdy.y);\n",
396 fsOffsetName1, fsOffsetName1, fsOffsetName1, fsOffsetName1);
joshualitt30ba4362014-08-21 20:18:45 -0700397 fsBuilder->codeAppend("\tinvlen = inversesqrt(dot(grad, grad));\n");
398 fsBuilder->codeAppend("\tedgeAlpha *= clamp(0.5+test*invlen, 0.0, 1.0);\n");
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000399 }
400
joshualittc369e7c2014-10-22 10:56:26 -0700401 fsBuilder->codeAppendf("\t%s = %s;\n", args.fOutput,
402 (GrGLSLExpr4(args.fInput) * GrGLSLExpr1("edgeAlpha")).c_str());
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000403 }
404
joshualittb0a8a372014-09-23 09:50:21 -0700405 static void GenKey(const GrProcessor& processor, const GrGLCaps&,
406 GrProcessorKeyBuilder* b) {
407 const DIEllipseEdgeEffect& ellipseEffect = processor.cast<DIEllipseEdgeEffect>();
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000408
bsalomon63e99f72014-07-21 08:03:14 -0700409 b->add32(ellipseEffect.getMode());
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000410 }
411
joshualittb0a8a372014-09-23 09:50:21 -0700412 virtual void setData(const GrGLProgramDataManager&, const GrProcessor&) SK_OVERRIDE {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000413 }
414
415 private:
joshualitt249af152014-09-15 11:41:13 -0700416 typedef GrGLGeometryProcessor INHERITED;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000417 };
418
419private:
joshualitt249af152014-09-15 11:41:13 -0700420 DIEllipseEdgeEffect(Mode mode)
421 : fInEllipseOffsets0(this->addVertexAttrib(
422 GrShaderVar("inEllipseOffsets0",
423 kVec2f_GrSLType,
424 GrShaderVar::kAttribute_TypeModifier)))
425 , fInEllipseOffsets1(this->addVertexAttrib(
426 GrShaderVar("inEllipseOffsets1",
427 kVec2f_GrSLType,
428 GrShaderVar::kAttribute_TypeModifier))) {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000429 fMode = mode;
430 }
431
bsalomon0e08fc12014-10-15 08:19:04 -0700432 virtual bool onIsEqual(const GrGeometryProcessor& other) const SK_OVERRIDE {
joshualitt49586be2014-09-16 08:21:41 -0700433 const DIEllipseEdgeEffect& eee = other.cast<DIEllipseEdgeEffect>();
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000434 return eee.fMode == fMode;
435 }
436
egdaniel1a8ecdf2014-10-03 06:24:12 -0700437 virtual void onComputeInvariantOutput(InvariantOutput* inout) const SK_OVERRIDE {
egdanielccb2e382014-10-13 12:53:46 -0700438 inout->mulByUnknownAlpha();
egdaniel1a8ecdf2014-10-03 06:24:12 -0700439 }
440
joshualitt249af152014-09-15 11:41:13 -0700441 const GrShaderVar& fInEllipseOffsets0;
442 const GrShaderVar& fInEllipseOffsets1;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000443 Mode fMode;
444
joshualittb0a8a372014-09-23 09:50:21 -0700445 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000446
joshualitt249af152014-09-15 11:41:13 -0700447 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000448};
449
joshualittb0a8a372014-09-23 09:50:21 -0700450GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DIEllipseEdgeEffect);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000451
joshualittb0a8a372014-09-23 09:50:21 -0700452GrGeometryProcessor* DIEllipseEdgeEffect::TestCreate(SkRandom* random,
453 GrContext* context,
454 const GrDrawTargetCaps&,
455 GrTexture* textures[]) {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000456 return DIEllipseEdgeEffect::Create((Mode)(random->nextRangeU(0,2)));
457}
458
459///////////////////////////////////////////////////////////////////////////////
460
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000461void GrOvalRenderer::reset() {
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000462 SkSafeSetNull(fRRectIndexBuffer);
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000463}
464
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000465bool GrOvalRenderer::drawOval(GrDrawTarget* target, const GrContext* context, bool useAA,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000466 const SkRect& oval, const SkStrokeRec& stroke)
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000467{
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000468 bool useCoverageAA = useAA &&
469 !target->getDrawState().getRenderTarget()->isMultisampled() &&
470 !target->shouldDisableCoverageAAForBlend();
471
472 if (!useCoverageAA) {
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000473 return false;
474 }
475
476 const SkMatrix& vm = context->getMatrix();
477
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000478 // we can draw circles
479 if (SkScalarNearlyEqual(oval.width(), oval.height())
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000480 && circle_stays_circle(vm)) {
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000481 this->drawCircle(target, useCoverageAA, oval, stroke);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000482 // if we have shader derivative support, render as device-independent
483 } else if (target->caps()->shaderDerivativeSupport()) {
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000484 return this->drawDIEllipse(target, useCoverageAA, oval, stroke);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000485 // otherwise axis-aligned ellipses only
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000486 } else if (vm.rectStaysRect()) {
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000487 return this->drawEllipse(target, useCoverageAA, oval, stroke);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000488 } else {
489 return false;
490 }
491
492 return true;
493}
494
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000495///////////////////////////////////////////////////////////////////////////////
496
robertphillips@google.com42903302013-04-20 12:26:07 +0000497// position + edge
498extern const GrVertexAttrib gCircleVertexAttribs[] = {
499 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
joshualittb0a8a372014-09-23 09:50:21 -0700500 {kVec4f_GrVertexAttribType, sizeof(SkPoint), kGeometryProcessor_GrVertexAttribBinding}
robertphillips@google.com42903302013-04-20 12:26:07 +0000501};
502
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000503void GrOvalRenderer::drawCircle(GrDrawTarget* target,
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000504 bool useCoverageAA,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000505 const SkRect& circle,
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000506 const SkStrokeRec& stroke)
507{
508 GrDrawState* drawState = target->drawState();
509
510 const SkMatrix& vm = drawState->getViewMatrix();
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000511 SkPoint center = SkPoint::Make(circle.centerX(), circle.centerY());
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000512 vm.mapPoints(&center, 1);
513 SkScalar radius = vm.mapRadius(SkScalarHalf(circle.width()));
514 SkScalar strokeWidth = vm.mapRadius(stroke.getWidth());
515
bsalomon@google.com137f1342013-05-29 21:27:53 +0000516 GrDrawState::AutoViewMatrixRestore avmr;
517 if (!avmr.setIdentity(drawState)) {
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000518 return;
519 }
520
egdaniel7b3d5ee2014-08-28 05:41:14 -0700521 drawState->setVertexAttribs<gCircleVertexAttribs>(SK_ARRAY_COUNT(gCircleVertexAttribs),
522 sizeof(CircleVertex));
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000523
524 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
525 if (!geo.succeeded()) {
526 GrPrintf("Failed to get space for vertices!\n");
527 return;
528 }
529
530 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
531
532 SkStrokeRec::Style style = stroke.getStyle();
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000533 bool isStrokeOnly = SkStrokeRec::kStroke_Style == style ||
534 SkStrokeRec::kHairline_Style == style;
535 bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == style;
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000536
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000537 SkScalar innerRadius = 0.0f;
538 SkScalar outerRadius = radius;
539 SkScalar halfWidth = 0;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000540 if (hasStroke) {
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000541 if (SkScalarNearlyZero(strokeWidth)) {
542 halfWidth = SK_ScalarHalf;
543 } else {
544 halfWidth = SkScalarHalf(strokeWidth);
545 }
546
547 outerRadius += halfWidth;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000548 if (isStrokeOnly) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000549 innerRadius = radius - halfWidth;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000550 }
551 }
552
joshualittb0a8a372014-09-23 09:50:21 -0700553 GrGeometryProcessor* gp = CircleEdgeEffect::Create(isStrokeOnly && innerRadius > 0);
554 drawState->setGeometryProcessor(gp)->unref();
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +0000555
bsalomon@google.com58e30fe2013-04-01 19:01:20 +0000556 // The radii are outset for two reasons. First, it allows the shader to simply perform
557 // clamp(distance-to-center - radius, 0, 1). Second, the outer radius is used to compute the
558 // verts of the bounding box that is rendered and the outset ensures the box will cover all
559 // pixels partially covered by the circle.
560 outerRadius += SK_ScalarHalf;
561 innerRadius -= SK_ScalarHalf;
562
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000563 SkRect bounds = SkRect::MakeLTRB(
bsalomon@google.com58e30fe2013-04-01 19:01:20 +0000564 center.fX - outerRadius,
565 center.fY - outerRadius,
566 center.fX + outerRadius,
567 center.fY + outerRadius
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000568 );
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000569
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000570 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000571 verts[0].fOffset = SkPoint::Make(-outerRadius, -outerRadius);
572 verts[0].fOuterRadius = outerRadius;
573 verts[0].fInnerRadius = innerRadius;
574
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000575 verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
skia.committer@gmail.com46746762013-04-12 07:01:51 +0000576 verts[1].fOffset = SkPoint::Make(outerRadius, -outerRadius);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000577 verts[1].fOuterRadius = outerRadius;
578 verts[1].fInnerRadius = innerRadius;
579
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000580 verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000581 verts[2].fOffset = SkPoint::Make(-outerRadius, outerRadius);
582 verts[2].fOuterRadius = outerRadius;
583 verts[2].fInnerRadius = innerRadius;
584
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000585 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000586 verts[3].fOffset = SkPoint::Make(outerRadius, outerRadius);
587 verts[3].fOuterRadius = outerRadius;
588 verts[3].fInnerRadius = innerRadius;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000589
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000590 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000591}
592
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000593///////////////////////////////////////////////////////////////////////////////
594
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000595// position + offset + 1/radii
robertphillips@google.com42903302013-04-20 12:26:07 +0000596extern const GrVertexAttrib gEllipseVertexAttribs[] = {
597 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
joshualittb0a8a372014-09-23 09:50:21 -0700598 {kVec2f_GrVertexAttribType, sizeof(SkPoint), kGeometryProcessor_GrVertexAttribBinding},
599 {kVec4f_GrVertexAttribType, 2*sizeof(SkPoint), kGeometryProcessor_GrVertexAttribBinding}
robertphillips@google.com42903302013-04-20 12:26:07 +0000600};
601
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000602// position + offsets
603extern const GrVertexAttrib gDIEllipseVertexAttribs[] = {
604 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
joshualittb0a8a372014-09-23 09:50:21 -0700605 {kVec2f_GrVertexAttribType, sizeof(SkPoint), kGeometryProcessor_GrVertexAttribBinding},
606 {kVec2f_GrVertexAttribType, 2*sizeof(SkPoint), kGeometryProcessor_GrVertexAttribBinding},
robertphillips@google.com42903302013-04-20 12:26:07 +0000607};
608
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000609bool GrOvalRenderer::drawEllipse(GrDrawTarget* target,
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000610 bool useCoverageAA,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000611 const SkRect& ellipse,
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000612 const SkStrokeRec& stroke)
613{
614 GrDrawState* drawState = target->drawState();
615#ifdef SK_DEBUG
616 {
617 // we should have checked for this previously
618 bool isAxisAlignedEllipse = drawState->getViewMatrix().rectStaysRect();
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000619 SkASSERT(useCoverageAA && isAxisAlignedEllipse);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000620 }
621#endif
622
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000623 // do any matrix crunching before we reset the draw state for device coords
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000624 const SkMatrix& vm = drawState->getViewMatrix();
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000625 SkPoint center = SkPoint::Make(ellipse.centerX(), ellipse.centerY());
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000626 vm.mapPoints(&center, 1);
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000627 SkScalar ellipseXRadius = SkScalarHalf(ellipse.width());
628 SkScalar ellipseYRadius = SkScalarHalf(ellipse.height());
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +0000629 SkScalar xRadius = SkScalarAbs(vm[SkMatrix::kMScaleX]*ellipseXRadius +
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000630 vm[SkMatrix::kMSkewY]*ellipseYRadius);
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +0000631 SkScalar yRadius = SkScalarAbs(vm[SkMatrix::kMSkewX]*ellipseXRadius +
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000632 vm[SkMatrix::kMScaleY]*ellipseYRadius);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000633
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000634 // do (potentially) anisotropic mapping of stroke
635 SkVector scaledStroke;
636 SkScalar strokeWidth = stroke.getWidth();
637 scaledStroke.fX = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMScaleX] + vm[SkMatrix::kMSkewY]));
638 scaledStroke.fY = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMSkewX] + vm[SkMatrix::kMScaleY]));
639
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000640 SkStrokeRec::Style style = stroke.getStyle();
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000641 bool isStrokeOnly = SkStrokeRec::kStroke_Style == style ||
642 SkStrokeRec::kHairline_Style == style;
643 bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == style;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000644
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000645 SkScalar innerXRadius = 0;
646 SkScalar innerYRadius = 0;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000647 if (hasStroke) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000648 if (SkScalarNearlyZero(scaledStroke.length())) {
649 scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf);
650 } else {
651 scaledStroke.scale(SK_ScalarHalf);
652 }
653
654 // we only handle thick strokes for near-circular ellipses
655 if (scaledStroke.length() > SK_ScalarHalf &&
656 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
657 return false;
658 }
659
660 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
661 if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius ||
662 scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) {
663 return false;
664 }
665
666 // this is legit only if scale & translation (which should be the case at the moment)
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000667 if (isStrokeOnly) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000668 innerXRadius = xRadius - scaledStroke.fX;
669 innerYRadius = yRadius - scaledStroke.fY;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000670 }
671
672 xRadius += scaledStroke.fX;
673 yRadius += scaledStroke.fY;
674 }
675
bsalomon@google.com137f1342013-05-29 21:27:53 +0000676 GrDrawState::AutoViewMatrixRestore avmr;
677 if (!avmr.setIdentity(drawState)) {
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000678 return false;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000679 }
680
egdaniel7b3d5ee2014-08-28 05:41:14 -0700681 drawState->setVertexAttribs<gEllipseVertexAttribs>(SK_ARRAY_COUNT(gEllipseVertexAttribs),
682 sizeof(EllipseVertex));
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000683
684 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
685 if (!geo.succeeded()) {
686 GrPrintf("Failed to get space for vertices!\n");
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000687 return false;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000688 }
689
690 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices());
691
joshualittb0a8a372014-09-23 09:50:21 -0700692 GrGeometryProcessor* gp = EllipseEdgeEffect::Create(isStrokeOnly &&
693 innerXRadius > 0 && innerYRadius > 0);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000694
joshualittb0a8a372014-09-23 09:50:21 -0700695 drawState->setGeometryProcessor(gp)->unref();
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000696
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000697 // Compute the reciprocals of the radii here to save time in the shader
698 SkScalar xRadRecip = SkScalarInvert(xRadius);
699 SkScalar yRadRecip = SkScalarInvert(yRadius);
700 SkScalar xInnerRadRecip = SkScalarInvert(innerXRadius);
701 SkScalar yInnerRadRecip = SkScalarInvert(innerYRadius);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000702
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000703 // We've extended the outer x radius out half a pixel to antialias.
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000704 // This will also expand the rect so all the pixels will be captured.
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000705 // TODO: Consider if we should use sqrt(2)/2 instead
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000706 xRadius += SK_ScalarHalf;
707 yRadius += SK_ScalarHalf;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000708
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000709 SkRect bounds = SkRect::MakeLTRB(
710 center.fX - xRadius,
711 center.fY - yRadius,
712 center.fX + xRadius,
713 center.fY + yRadius
714 );
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000715
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000716 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000717 verts[0].fOffset = SkPoint::Make(-xRadius, -yRadius);
718 verts[0].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
719 verts[0].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000720
721 verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000722 verts[1].fOffset = SkPoint::Make(xRadius, -yRadius);
723 verts[1].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
724 verts[1].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000725
726 verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000727 verts[2].fOffset = SkPoint::Make(-xRadius, yRadius);
728 verts[2].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
729 verts[2].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000730
731 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000732 verts[3].fOffset = SkPoint::Make(xRadius, yRadius);
733 verts[3].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
734 verts[3].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
skia.committer@gmail.com46746762013-04-12 07:01:51 +0000735
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000736 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds);
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000737
738 return true;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000739}
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000740
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000741bool GrOvalRenderer::drawDIEllipse(GrDrawTarget* target,
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000742 bool useCoverageAA,
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000743 const SkRect& ellipse,
744 const SkStrokeRec& stroke)
745{
746 GrDrawState* drawState = target->drawState();
747 const SkMatrix& vm = drawState->getViewMatrix();
748
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000749 SkPoint center = SkPoint::Make(ellipse.centerX(), ellipse.centerY());
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000750 SkScalar xRadius = SkScalarHalf(ellipse.width());
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000751 SkScalar yRadius = SkScalarHalf(ellipse.height());
752
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000753 SkStrokeRec::Style style = stroke.getStyle();
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000754 DIEllipseEdgeEffect::Mode mode = (SkStrokeRec::kStroke_Style == style) ?
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000755 DIEllipseEdgeEffect::kStroke :
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000756 (SkStrokeRec::kHairline_Style == style) ?
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000757 DIEllipseEdgeEffect::kHairline : DIEllipseEdgeEffect::kFill;
758
759 SkScalar innerXRadius = 0;
760 SkScalar innerYRadius = 0;
761 if (SkStrokeRec::kFill_Style != style && SkStrokeRec::kHairline_Style != style) {
762 SkScalar strokeWidth = stroke.getWidth();
763
764 if (SkScalarNearlyZero(strokeWidth)) {
765 strokeWidth = SK_ScalarHalf;
766 } else {
767 strokeWidth *= SK_ScalarHalf;
768 }
769
770 // we only handle thick strokes for near-circular ellipses
771 if (strokeWidth > SK_ScalarHalf &&
772 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
773 return false;
774 }
775
776 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
777 if (strokeWidth*(yRadius*yRadius) < (strokeWidth*strokeWidth)*xRadius ||
778 strokeWidth*(xRadius*xRadius) < (strokeWidth*strokeWidth)*yRadius) {
779 return false;
780 }
781
782 // set inner radius (if needed)
783 if (SkStrokeRec::kStroke_Style == style) {
784 innerXRadius = xRadius - strokeWidth;
785 innerYRadius = yRadius - strokeWidth;
786 }
787
788 xRadius += strokeWidth;
789 yRadius += strokeWidth;
790 }
791 if (DIEllipseEdgeEffect::kStroke == mode) {
792 mode = (innerXRadius > 0 && innerYRadius > 0) ? DIEllipseEdgeEffect::kStroke :
793 DIEllipseEdgeEffect::kFill;
794 }
795 SkScalar innerRatioX = SkScalarDiv(xRadius, innerXRadius);
796 SkScalar innerRatioY = SkScalarDiv(yRadius, innerYRadius);
797
egdaniel7b3d5ee2014-08-28 05:41:14 -0700798 drawState->setVertexAttribs<gDIEllipseVertexAttribs>(SK_ARRAY_COUNT(gDIEllipseVertexAttribs),
799 sizeof(DIEllipseVertex));
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000800
801 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
802 if (!geo.succeeded()) {
803 GrPrintf("Failed to get space for vertices!\n");
804 return false;
805 }
806
807 DIEllipseVertex* verts = reinterpret_cast<DIEllipseVertex*>(geo.vertices());
808
joshualittb0a8a372014-09-23 09:50:21 -0700809 GrGeometryProcessor* gp = DIEllipseEdgeEffect::Create(mode);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000810
joshualittb0a8a372014-09-23 09:50:21 -0700811 drawState->setGeometryProcessor(gp)->unref();
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000812
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000813 // This expands the outer rect so that after CTM we end up with a half-pixel border
814 SkScalar a = vm[SkMatrix::kMScaleX];
815 SkScalar b = vm[SkMatrix::kMSkewX];
816 SkScalar c = vm[SkMatrix::kMSkewY];
817 SkScalar d = vm[SkMatrix::kMScaleY];
818 SkScalar geoDx = SkScalarDiv(SK_ScalarHalf, SkScalarSqrt(a*a + c*c));
819 SkScalar geoDy = SkScalarDiv(SK_ScalarHalf, SkScalarSqrt(b*b + d*d));
820 // This adjusts the "radius" to include the half-pixel border
821 SkScalar offsetDx = SkScalarDiv(geoDx, xRadius);
822 SkScalar offsetDy = SkScalarDiv(geoDy, yRadius);
823
824 SkRect bounds = SkRect::MakeLTRB(
825 center.fX - xRadius - geoDx,
826 center.fY - yRadius - geoDy,
827 center.fX + xRadius + geoDx,
828 center.fY + yRadius + geoDy
829 );
830
831 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
832 verts[0].fOuterOffset = SkPoint::Make(-1.0f - offsetDx, -1.0f - offsetDy);
833 verts[0].fInnerOffset = SkPoint::Make(-innerRatioX - offsetDx, -innerRatioY - offsetDy);
834
835 verts[1].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
836 verts[1].fOuterOffset = SkPoint::Make(1.0f + offsetDx, -1.0f - offsetDy);
837 verts[1].fInnerOffset = SkPoint::Make(innerRatioX + offsetDx, -innerRatioY - offsetDy);
838
839 verts[2].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
840 verts[2].fOuterOffset = SkPoint::Make(-1.0f - offsetDx, 1.0f + offsetDy);
841 verts[2].fInnerOffset = SkPoint::Make(-innerRatioX - offsetDx, innerRatioY + offsetDy);
842
843 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
844 verts[3].fOuterOffset = SkPoint::Make(1.0f + offsetDx, 1.0f + offsetDy);
845 verts[3].fInnerOffset = SkPoint::Make(innerRatioX + offsetDx, innerRatioY + offsetDy);
846
847 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4, &bounds);
848
849 return true;
850}
851
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000852///////////////////////////////////////////////////////////////////////////////
853
854static const uint16_t gRRectIndices[] = {
855 // corners
856 0, 1, 5, 0, 5, 4,
857 2, 3, 7, 2, 7, 6,
858 8, 9, 13, 8, 13, 12,
859 10, 11, 15, 10, 15, 14,
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000860
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000861 // edges
862 1, 2, 6, 1, 6, 5,
863 4, 5, 9, 4, 9, 8,
864 6, 7, 11, 6, 11, 10,
865 9, 10, 14, 9, 14, 13,
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000866
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000867 // center
868 // we place this at the end so that we can ignore these indices when rendering stroke-only
869 5, 6, 10, 5, 10, 9
870};
871
872
873GrIndexBuffer* GrOvalRenderer::rRectIndexBuffer(GrGpu* gpu) {
874 if (NULL == fRRectIndexBuffer) {
875 fRRectIndexBuffer =
876 gpu->createIndexBuffer(sizeof(gRRectIndices), false);
bsalomon49f085d2014-09-05 13:34:00 -0700877 if (fRRectIndexBuffer) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000878#ifdef SK_DEBUG
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000879 bool updated =
880#endif
881 fRRectIndexBuffer->updateData(gRRectIndices,
882 sizeof(gRRectIndices));
883 GR_DEBUGASSERT(updated);
884 }
885 }
886 return fRRectIndexBuffer;
887}
888
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000889bool GrOvalRenderer::drawDRRect(GrDrawTarget* target, GrContext* context, bool useAA,
bsalomon8af05232014-06-03 06:34:58 -0700890 const SkRRect& origOuter, const SkRRect& origInner) {
891 bool applyAA = useAA &&
892 !target->getDrawState().getRenderTarget()->isMultisampled() &&
893 !target->shouldDisableCoverageAAForBlend();
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000894 GrDrawState::AutoRestoreEffects are;
895 if (!origInner.isEmpty()) {
896 SkTCopyOnFirstWrite<SkRRect> inner(origInner);
897 if (!context->getMatrix().isIdentity()) {
898 if (!origInner.transform(context->getMatrix(), inner.writable())) {
899 return false;
900 }
901 }
joshualittb0a8a372014-09-23 09:50:21 -0700902 GrPrimitiveEdgeType edgeType = applyAA ?
903 kInverseFillAA_GrProcessorEdgeType :
904 kInverseFillBW_GrProcessorEdgeType;
905 GrFragmentProcessor* fp = GrRRectEffect::Create(edgeType, *inner);
906 if (NULL == fp) {
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000907 return false;
908 }
909 are.set(target->drawState());
joshualittb0a8a372014-09-23 09:50:21 -0700910 target->drawState()->addCoverageProcessor(fp)->unref();
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000911 }
912
913 SkStrokeRec fillRec(SkStrokeRec::kFill_InitStyle);
bsalomon8af05232014-06-03 06:34:58 -0700914 if (this->drawRRect(target, context, useAA, origOuter, fillRec)) {
915 return true;
916 }
917
918 SkASSERT(!origOuter.isEmpty());
919 SkTCopyOnFirstWrite<SkRRect> outer(origOuter);
920 if (!context->getMatrix().isIdentity()) {
921 if (!origOuter.transform(context->getMatrix(), outer.writable())) {
922 return false;
923 }
924 }
joshualittb0a8a372014-09-23 09:50:21 -0700925 GrPrimitiveEdgeType edgeType = applyAA ? kFillAA_GrProcessorEdgeType :
926 kFillBW_GrProcessorEdgeType;
927 GrFragmentProcessor* effect = GrRRectEffect::Create(edgeType, *outer);
bsalomon8af05232014-06-03 06:34:58 -0700928 if (NULL == effect) {
929 return false;
930 }
931 if (!are.isSet()) {
932 are.set(target->drawState());
933 }
934 GrDrawState::AutoViewMatrixRestore avmr;
935 if (!avmr.setIdentity(target->drawState())) {
936 return false;
937 }
joshualittb0a8a372014-09-23 09:50:21 -0700938 target->drawState()->addCoverageProcessor(effect)->unref();
bsalomon8af05232014-06-03 06:34:58 -0700939 SkRect bounds = outer->getBounds();
940 if (applyAA) {
941 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
942 }
bsalomon01c8da12014-08-04 09:21:30 -0700943 target->drawRect(bounds, NULL, NULL);
bsalomon8af05232014-06-03 06:34:58 -0700944 return true;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000945}
946
947bool GrOvalRenderer::drawRRect(GrDrawTarget* target, GrContext* context, bool useAA,
948 const SkRRect& rrect, const SkStrokeRec& stroke) {
949 if (rrect.isOval()) {
950 return this->drawOval(target, context, useAA, rrect.getBounds(), stroke);
951 }
952
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000953 bool useCoverageAA = useAA &&
954 !target->getDrawState().getRenderTarget()->isMultisampled() &&
955 !target->shouldDisableCoverageAAForBlend();
956
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000957 // only anti-aliased rrects for now
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000958 if (!useCoverageAA) {
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000959 return false;
960 }
961
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000962 const SkMatrix& vm = context->getMatrix();
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000963
964 if (!vm.rectStaysRect() || !rrect.isSimple()) {
965 return false;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000966 }
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000967
968 // do any matrix crunching before we reset the draw state for device coords
969 const SkRect& rrectBounds = rrect.getBounds();
970 SkRect bounds;
971 vm.mapRect(&bounds, rrectBounds);
972
973 SkVector radii = rrect.getSimpleRadii();
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000974 SkScalar xRadius = SkScalarAbs(vm[SkMatrix::kMScaleX]*radii.fX +
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000975 vm[SkMatrix::kMSkewY]*radii.fY);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000976 SkScalar yRadius = SkScalarAbs(vm[SkMatrix::kMSkewX]*radii.fX +
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000977 vm[SkMatrix::kMScaleY]*radii.fY);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000978
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000979 SkStrokeRec::Style style = stroke.getStyle();
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000980
981 // do (potentially) anisotropic mapping of stroke
982 SkVector scaledStroke;
983 SkScalar strokeWidth = stroke.getWidth();
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000984
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000985 bool isStrokeOnly = SkStrokeRec::kStroke_Style == style ||
986 SkStrokeRec::kHairline_Style == style;
987 bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == style;
988
989 if (hasStroke) {
990 if (SkStrokeRec::kHairline_Style == style) {
991 scaledStroke.set(1, 1);
992 } else {
993 scaledStroke.fX = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMScaleX] +
994 vm[SkMatrix::kMSkewY]));
995 scaledStroke.fY = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMSkewX] +
996 vm[SkMatrix::kMScaleY]));
997 }
998
999 // if half of strokewidth is greater than radius, we don't handle that right now
1000 if (SK_ScalarHalf*scaledStroke.fX > xRadius || SK_ScalarHalf*scaledStroke.fY > yRadius) {
1001 return false;
1002 }
1003 }
1004
1005 // The way the effect interpolates the offset-to-ellipse/circle-center attribute only works on
1006 // the interior of the rrect if the radii are >= 0.5. Otherwise, the inner rect of the nine-
1007 // patch will have fractional coverage. This only matters when the interior is actually filled.
1008 // We could consider falling back to rect rendering here, since a tiny radius is
1009 // indistinguishable from a square corner.
1010 if (!isStrokeOnly && (SK_ScalarHalf > xRadius || SK_ScalarHalf > yRadius)) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001011 return false;
1012 }
1013
1014 // reset to device coordinates
1015 GrDrawState* drawState = target->drawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +00001016 GrDrawState::AutoViewMatrixRestore avmr;
1017 if (!avmr.setIdentity(drawState)) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001018 return false;
1019 }
1020
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001021 GrIndexBuffer* indexBuffer = this->rRectIndexBuffer(context->getGpu());
1022 if (NULL == indexBuffer) {
1023 GrPrintf("Failed to create index buffer!\n");
1024 return false;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001025 }
1026
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001027 // if the corners are circles, use the circle renderer
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001028 if ((!hasStroke || scaledStroke.fX == scaledStroke.fY) && xRadius == yRadius) {
egdaniel7b3d5ee2014-08-28 05:41:14 -07001029 drawState->setVertexAttribs<gCircleVertexAttribs>(SK_ARRAY_COUNT(gCircleVertexAttribs),
1030 sizeof(CircleVertex));
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001031
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001032 GrDrawTarget::AutoReleaseGeometry geo(target, 16, 0);
1033 if (!geo.succeeded()) {
1034 GrPrintf("Failed to get space for vertices!\n");
1035 return false;
1036 }
1037 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001038
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001039 SkScalar innerRadius = 0.0f;
1040 SkScalar outerRadius = xRadius;
1041 SkScalar halfWidth = 0;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001042 if (hasStroke) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001043 if (SkScalarNearlyZero(scaledStroke.fX)) {
1044 halfWidth = SK_ScalarHalf;
1045 } else {
1046 halfWidth = SkScalarHalf(scaledStroke.fX);
1047 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001048
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001049 if (isStrokeOnly) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001050 innerRadius = xRadius - halfWidth;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001051 }
1052 outerRadius += halfWidth;
1053 bounds.outset(halfWidth, halfWidth);
1054 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001055
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001056 isStrokeOnly = (isStrokeOnly && innerRadius >= 0);
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +00001057
joshualittb0a8a372014-09-23 09:50:21 -07001058 GrGeometryProcessor* effect = CircleEdgeEffect::Create(isStrokeOnly);
joshualitt249af152014-09-15 11:41:13 -07001059 drawState->setGeometryProcessor(effect)->unref();
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +00001060
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001061 // The radii are outset for two reasons. First, it allows the shader to simply perform
1062 // clamp(distance-to-center - radius, 0, 1). Second, the outer radius is used to compute the
1063 // verts of the bounding box that is rendered and the outset ensures the box will cover all
1064 // pixels partially covered by the circle.
1065 outerRadius += SK_ScalarHalf;
1066 innerRadius -= SK_ScalarHalf;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001067
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001068 // Expand the rect so all the pixels will be captured.
1069 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001070
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001071 SkScalar yCoords[4] = {
1072 bounds.fTop,
1073 bounds.fTop + outerRadius,
1074 bounds.fBottom - outerRadius,
1075 bounds.fBottom
1076 };
1077 SkScalar yOuterRadii[4] = {
1078 -outerRadius,
1079 0,
1080 0,
1081 outerRadius
1082 };
1083 for (int i = 0; i < 4; ++i) {
1084 verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]);
1085 verts->fOffset = SkPoint::Make(-outerRadius, yOuterRadii[i]);
1086 verts->fOuterRadius = outerRadius;
1087 verts->fInnerRadius = innerRadius;
1088 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001089
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001090 verts->fPos = SkPoint::Make(bounds.fLeft + outerRadius, yCoords[i]);
1091 verts->fOffset = SkPoint::Make(0, yOuterRadii[i]);
1092 verts->fOuterRadius = outerRadius;
1093 verts->fInnerRadius = innerRadius;
1094 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001095
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001096 verts->fPos = SkPoint::Make(bounds.fRight - outerRadius, yCoords[i]);
1097 verts->fOffset = SkPoint::Make(0, yOuterRadii[i]);
1098 verts->fOuterRadius = outerRadius;
1099 verts->fInnerRadius = innerRadius;
1100 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001101
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001102 verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]);
1103 verts->fOffset = SkPoint::Make(outerRadius, yOuterRadii[i]);
1104 verts->fOuterRadius = outerRadius;
1105 verts->fInnerRadius = innerRadius;
1106 verts++;
1107 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001108
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001109 // drop out the middle quad if we're stroked
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001110 int indexCnt = isStrokeOnly ? SK_ARRAY_COUNT(gRRectIndices) - 6 :
1111 SK_ARRAY_COUNT(gRRectIndices);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001112 target->setIndexSourceToBuffer(indexBuffer);
1113 target->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, 16, indexCnt, &bounds);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001114
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001115 // otherwise we use the ellipse renderer
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001116 } else {
egdaniel7b3d5ee2014-08-28 05:41:14 -07001117 drawState->setVertexAttribs<gEllipseVertexAttribs>(SK_ARRAY_COUNT(gEllipseVertexAttribs),
1118 sizeof(EllipseVertex));
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001119
1120 SkScalar innerXRadius = 0.0f;
1121 SkScalar innerYRadius = 0.0f;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001122 if (hasStroke) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001123 if (SkScalarNearlyZero(scaledStroke.length())) {
1124 scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf);
1125 } else {
1126 scaledStroke.scale(SK_ScalarHalf);
1127 }
1128
1129 // we only handle thick strokes for near-circular ellipses
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +00001130 if (scaledStroke.length() > SK_ScalarHalf &&
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001131 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
1132 return false;
1133 }
1134
1135 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
1136 if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius ||
1137 scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) {
1138 return false;
1139 }
1140
1141 // this is legit only if scale & translation (which should be the case at the moment)
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001142 if (isStrokeOnly) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001143 innerXRadius = xRadius - scaledStroke.fX;
1144 innerYRadius = yRadius - scaledStroke.fY;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001145 }
1146
1147 xRadius += scaledStroke.fX;
1148 yRadius += scaledStroke.fY;
1149 bounds.outset(scaledStroke.fX, scaledStroke.fY);
1150 }
jvanverth@google.come3647412013-05-08 15:31:43 +00001151
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001152 isStrokeOnly = (isStrokeOnly && innerXRadius >= 0 && innerYRadius >= 0);
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +00001153
jvanverth@google.come3647412013-05-08 15:31:43 +00001154 GrDrawTarget::AutoReleaseGeometry geo(target, 16, 0);
1155 if (!geo.succeeded()) {
1156 GrPrintf("Failed to get space for vertices!\n");
1157 return false;
1158 }
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001159 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices());
jvanverth@google.come3647412013-05-08 15:31:43 +00001160
joshualittb0a8a372014-09-23 09:50:21 -07001161 GrGeometryProcessor* effect = EllipseEdgeEffect::Create(isStrokeOnly);
joshualitt249af152014-09-15 11:41:13 -07001162 drawState->setGeometryProcessor(effect)->unref();
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001163
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001164 // Compute the reciprocals of the radii here to save time in the shader
1165 SkScalar xRadRecip = SkScalarInvert(xRadius);
1166 SkScalar yRadRecip = SkScalarInvert(yRadius);
1167 SkScalar xInnerRadRecip = SkScalarInvert(innerXRadius);
1168 SkScalar yInnerRadRecip = SkScalarInvert(innerYRadius);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001169
1170 // Extend the radii out half a pixel to antialias.
1171 SkScalar xOuterRadius = xRadius + SK_ScalarHalf;
1172 SkScalar yOuterRadius = yRadius + SK_ScalarHalf;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001173
1174 // Expand the rect so all the pixels will be captured.
1175 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1176
1177 SkScalar yCoords[4] = {
1178 bounds.fTop,
1179 bounds.fTop + yOuterRadius,
1180 bounds.fBottom - yOuterRadius,
1181 bounds.fBottom
1182 };
1183 SkScalar yOuterOffsets[4] = {
jvanverth@google.comd1b5b142013-07-02 14:46:03 +00001184 yOuterRadius,
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001185 SK_ScalarNearlyZero, // we're using inversesqrt() in the shader, so can't be exactly 0
1186 SK_ScalarNearlyZero,
1187 yOuterRadius
1188 };
1189
1190 for (int i = 0; i < 4; ++i) {
1191 verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]);
jvanverth@google.comd1b5b142013-07-02 14:46:03 +00001192 verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001193 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1194 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001195 verts++;
1196
1197 verts->fPos = SkPoint::Make(bounds.fLeft + xOuterRadius, yCoords[i]);
1198 verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001199 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1200 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001201 verts++;
1202
1203 verts->fPos = SkPoint::Make(bounds.fRight - xOuterRadius, yCoords[i]);
1204 verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001205 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1206 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001207 verts++;
1208
1209 verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]);
1210 verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001211 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1212 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001213 verts++;
1214 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001215
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001216 // drop out the middle quad if we're stroked
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001217 int indexCnt = isStrokeOnly ? SK_ARRAY_COUNT(gRRectIndices) - 6 :
1218 SK_ARRAY_COUNT(gRRectIndices);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001219 target->setIndexSourceToBuffer(indexBuffer);
1220 target->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, 16, indexCnt, &bounds);
1221 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001222
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001223 return true;
1224}