blob: a9dda840aa59d2b036413597d76c4f6e13c9a295 [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"
egdaniel605dd0f2014-11-12 08:35:25 -080020#include "GrInvariantOutput.h"
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +000021
22#include "SkRRect.h"
commit-bot@chromium.org81312832013-03-22 18:34:09 +000023#include "SkStrokeRec.h"
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +000024#include "SkTLazy.h"
commit-bot@chromium.org81312832013-03-22 18:34:09 +000025
joshualittb0a8a372014-09-23 09:50:21 -070026#include "GrGeometryProcessor.h"
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +000027#include "effects/GrRRectEffect.h"
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +000028
commit-bot@chromium.org81312832013-03-22 18:34:09 +000029namespace {
joshualitt5ead6da2014-10-22 16:00:29 -070030// TODO(joshualitt) add per vertex colors
commit-bot@chromium.org81312832013-03-22 18:34:09 +000031struct CircleVertex {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000032 SkPoint fPos;
33 SkPoint fOffset;
commit-bot@chromium.org81312832013-03-22 18:34:09 +000034 SkScalar fOuterRadius;
35 SkScalar fInnerRadius;
36};
37
38struct EllipseVertex {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000039 SkPoint fPos;
40 SkPoint fOffset;
41 SkPoint fOuterRadii;
42 SkPoint fInnerRadii;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +000043};
44
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +000045struct DIEllipseVertex {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000046 SkPoint fPos;
47 SkPoint fOuterOffset;
48 SkPoint fInnerOffset;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +000049};
50
commit-bot@chromium.org81312832013-03-22 18:34:09 +000051inline bool circle_stays_circle(const SkMatrix& m) {
52 return m.isSimilarity();
53}
54
55}
56
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000057///////////////////////////////////////////////////////////////////////////////
58
59/**
60 * 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 +000061 * specified as offset_x, offset_y (both from center point), outer radius and inner radius.
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000062 */
63
joshualitt249af152014-09-15 11:41:13 -070064class CircleEdgeEffect : public GrGeometryProcessor {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000065public:
joshualittb0a8a372014-09-23 09:50:21 -070066 static GrGeometryProcessor* Create(bool stroke) {
bsalomon98b33eb2014-10-15 11:05:26 -070067 GR_CREATE_STATIC_PROCESSOR(gCircleStrokeEdge, CircleEdgeEffect, (true));
68 GR_CREATE_STATIC_PROCESSOR(gCircleFillEdge, CircleEdgeEffect, (false));
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000069
70 if (stroke) {
bsalomon@google.comd42aca32013-04-23 15:37:27 +000071 gCircleStrokeEdge->ref();
72 return gCircleStrokeEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000073 } else {
bsalomon@google.comd42aca32013-04-23 15:37:27 +000074 gCircleFillEdge->ref();
75 return gCircleFillEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000076 }
77 }
78
joshualitt2dd1ae02014-12-03 06:24:10 -080079 const GrAttribute* inPosition() const { return fInPosition; }
80 const GrAttribute* inCircleEdge() const { return fInCircleEdge; }
joshualitt249af152014-09-15 11:41:13 -070081
joshualittb0a8a372014-09-23 09:50:21 -070082 virtual const GrBackendGeometryProcessorFactory& getFactory() const SK_OVERRIDE {
83 return GrTBackendGeometryProcessorFactory<CircleEdgeEffect>::getInstance();
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000084 }
85
86 virtual ~CircleEdgeEffect() {}
87
88 static const char* Name() { return "CircleEdge"; }
89
90 inline bool isStroked() const { return fStroke; }
91
joshualittb0a8a372014-09-23 09:50:21 -070092 class GLProcessor : public GrGLGeometryProcessor {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000093 public:
joshualittb0a8a372014-09-23 09:50:21 -070094 GLProcessor(const GrBackendProcessorFactory& factory, const GrProcessor&)
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000095 : INHERITED (factory) {}
96
joshualittc369e7c2014-10-22 10:56:26 -070097 virtual void emitCode(const EmitArgs& args) SK_OVERRIDE {
joshualitt2dd1ae02014-12-03 06:24:10 -080098 const CircleEdgeEffect& ce = args.fGP.cast<CircleEdgeEffect>();
99 GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder();
100
joshualitt74077b92014-10-24 11:26:03 -0700101 GrGLVertToFrag v(kVec4f_GrSLType);
102 args.fPB->addVarying("CircleEdge", &v);
joshualitt2dd1ae02014-12-03 06:24:10 -0800103 vsBuilder->codeAppendf("%s = %s;", v.vsOut(), ce.inCircleEdge()->fName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000104
joshualitt2dd1ae02014-12-03 06:24:10 -0800105 // setup coord outputs
106 vsBuilder->codeAppendf("%s = %s;", vsBuilder->positionCoords(), ce.inPosition()->fName);
107 vsBuilder->codeAppendf("%s = %s;", vsBuilder->localCoords(), ce.inPosition()->fName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000108
joshualitt4973d9d2014-11-08 09:24:25 -0800109 // setup position varying
110 vsBuilder->codeAppendf("%s = %s * vec3(%s, 1);", vsBuilder->glPosition(),
joshualitt2dd1ae02014-12-03 06:24:10 -0800111 vsBuilder->uViewM(), ce.inPosition()->fName);
joshualitt4973d9d2014-11-08 09:24:25 -0800112
joshualittc369e7c2014-10-22 10:56:26 -0700113 GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder();
joshualitt74077b92014-10-24 11:26:03 -0700114 fsBuilder->codeAppendf("float d = length(%s.xy);", v.fsIn());
115 fsBuilder->codeAppendf("float edgeAlpha = clamp(%s.z - d, 0.0, 1.0);", v.fsIn());
joshualitt2dd1ae02014-12-03 06:24:10 -0800116 if (ce.isStroked()) {
joshualitt74077b92014-10-24 11:26:03 -0700117 fsBuilder->codeAppendf("float innerAlpha = clamp(d - %s.w, 0.0, 1.0);",
118 v.fsIn());
119 fsBuilder->codeAppend("edgeAlpha *= innerAlpha;");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000120 }
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000121
joshualitt2dd1ae02014-12-03 06:24:10 -0800122 fsBuilder->codeAppendf("%s = vec4(edgeAlpha);", args.fOutputCoverage);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000123 }
124
joshualittb0a8a372014-09-23 09:50:21 -0700125 static void GenKey(const GrProcessor& processor, const GrGLCaps&,
126 GrProcessorKeyBuilder* b) {
127 const CircleEdgeEffect& circleEffect = processor.cast<CircleEdgeEffect>();
bsalomon63e99f72014-07-21 08:03:14 -0700128 b->add32(circleEffect.isStroked());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000129 }
130
joshualittb0a8a372014-09-23 09:50:21 -0700131 virtual void setData(const GrGLProgramDataManager&, const GrProcessor&) SK_OVERRIDE {}
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000132
133 private:
joshualitt249af152014-09-15 11:41:13 -0700134 typedef GrGLGeometryProcessor INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000135 };
136
137
138private:
joshualitt2dd1ae02014-12-03 06:24:10 -0800139 CircleEdgeEffect(bool stroke) {
140 fInPosition = &this->addVertexAttrib(GrAttribute("inPosition", kVec2f_GrVertexAttribType));
141 fInCircleEdge = &this->addVertexAttrib(GrAttribute("inCircleEdge",
142 kVec4f_GrVertexAttribType));
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000143 fStroke = stroke;
144 }
145
bsalomon0e08fc12014-10-15 08:19:04 -0700146 virtual bool onIsEqual(const GrGeometryProcessor& other) const SK_OVERRIDE {
joshualitt49586be2014-09-16 08:21:41 -0700147 const CircleEdgeEffect& cee = other.cast<CircleEdgeEffect>();
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000148 return cee.fStroke == fStroke;
149 }
150
egdaniel605dd0f2014-11-12 08:35:25 -0800151 virtual void onComputeInvariantOutput(GrInvariantOutput* inout) const SK_OVERRIDE {
egdanielccb2e382014-10-13 12:53:46 -0700152 inout->mulByUnknownAlpha();
egdaniel1a8ecdf2014-10-03 06:24:12 -0700153 }
154
joshualitt2dd1ae02014-12-03 06:24:10 -0800155 const GrAttribute* fInPosition;
156 const GrAttribute* fInCircleEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000157 bool fStroke;
158
joshualittb0a8a372014-09-23 09:50:21 -0700159 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000160
joshualitt249af152014-09-15 11:41:13 -0700161 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000162};
163
joshualittb0a8a372014-09-23 09:50:21 -0700164GR_DEFINE_GEOMETRY_PROCESSOR_TEST(CircleEdgeEffect);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000165
joshualittb0a8a372014-09-23 09:50:21 -0700166GrGeometryProcessor* CircleEdgeEffect::TestCreate(SkRandom* random,
167 GrContext* context,
168 const GrDrawTargetCaps&,
169 GrTexture* textures[]) {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000170 return CircleEdgeEffect::Create(random->nextBool());
171}
172
173///////////////////////////////////////////////////////////////////////////////
174
175/**
176 * 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 +0000177 * ellipse, specified as a 2D offset from center, and the reciprocals of the outer and inner radii,
178 * in both x and y directions.
179 *
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000180 * 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 +0000181 */
182
joshualitt249af152014-09-15 11:41:13 -0700183class EllipseEdgeEffect : public GrGeometryProcessor {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000184public:
joshualittb0a8a372014-09-23 09:50:21 -0700185 static GrGeometryProcessor* Create(bool stroke) {
bsalomon98b33eb2014-10-15 11:05:26 -0700186 GR_CREATE_STATIC_PROCESSOR(gEllipseStrokeEdge, EllipseEdgeEffect, (true));
187 GR_CREATE_STATIC_PROCESSOR(gEllipseFillEdge, EllipseEdgeEffect, (false));
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000188
189 if (stroke) {
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000190 gEllipseStrokeEdge->ref();
191 return gEllipseStrokeEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000192 } else {
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000193 gEllipseFillEdge->ref();
194 return gEllipseFillEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000195 }
196 }
197
joshualittb0a8a372014-09-23 09:50:21 -0700198 virtual const GrBackendGeometryProcessorFactory& getFactory() const SK_OVERRIDE {
199 return GrTBackendGeometryProcessorFactory<EllipseEdgeEffect>::getInstance();
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000200 }
201
202 virtual ~EllipseEdgeEffect() {}
203
204 static const char* Name() { return "EllipseEdge"; }
205
joshualitt2dd1ae02014-12-03 06:24:10 -0800206
207 const GrAttribute* inPosition() const { return fInPosition; }
208 const GrAttribute* inEllipseOffset() const { return fInEllipseOffset; }
209 const GrAttribute* inEllipseRadii() const { return fInEllipseRadii; }
joshualitt249af152014-09-15 11:41:13 -0700210
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000211 inline bool isStroked() const { return fStroke; }
212
joshualittb0a8a372014-09-23 09:50:21 -0700213 class GLProcessor : public GrGLGeometryProcessor {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000214 public:
joshualittb0a8a372014-09-23 09:50:21 -0700215 GLProcessor(const GrBackendProcessorFactory& factory, const GrProcessor&)
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000216 : INHERITED (factory) {}
217
joshualittc369e7c2014-10-22 10:56:26 -0700218 virtual void emitCode(const EmitArgs& args) SK_OVERRIDE {
joshualitt2dd1ae02014-12-03 06:24:10 -0800219 const EllipseEdgeEffect& ee = args.fGP.cast<EllipseEdgeEffect>();
220
221 GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder();
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000222
joshualitt74077b92014-10-24 11:26:03 -0700223 GrGLVertToFrag ellipseOffsets(kVec2f_GrSLType);
224 args.fPB->addVarying("EllipseOffsets", &ellipseOffsets);
joshualitt74077b92014-10-24 11:26:03 -0700225 vsBuilder->codeAppendf("%s = %s;", ellipseOffsets.vsOut(),
joshualitt2dd1ae02014-12-03 06:24:10 -0800226 ee.inEllipseOffset()->fName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000227
joshualitt74077b92014-10-24 11:26:03 -0700228 GrGLVertToFrag ellipseRadii(kVec4f_GrSLType);
229 args.fPB->addVarying("EllipseRadii", &ellipseRadii);
230 vsBuilder->codeAppendf("%s = %s;", ellipseRadii.vsOut(),
joshualitt2dd1ae02014-12-03 06:24:10 -0800231 ee.inEllipseRadii()->fName);
232
233 // setup coord outputs
234 vsBuilder->codeAppendf("%s = %s;", vsBuilder->positionCoords(), ee.inPosition()->fName);
235 vsBuilder->codeAppendf("%s = %s;", vsBuilder->localCoords(), ee.inPosition()->fName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000236
joshualitt4973d9d2014-11-08 09:24:25 -0800237 // setup position varying
238 vsBuilder->codeAppendf("%s = %s * vec3(%s, 1);", vsBuilder->glPosition(),
joshualitt2dd1ae02014-12-03 06:24:10 -0800239 vsBuilder->uViewM(), ee.inPosition()->fName);
joshualitt4973d9d2014-11-08 09:24:25 -0800240
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +0000241 // for outer curve
joshualittc369e7c2014-10-22 10:56:26 -0700242 GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder();
joshualitt74077b92014-10-24 11:26:03 -0700243 fsBuilder->codeAppendf("vec2 scaledOffset = %s*%s.xy;", ellipseOffsets.fsIn(),
244 ellipseRadii.fsIn());
245 fsBuilder->codeAppend("float test = dot(scaledOffset, scaledOffset) - 1.0;");
246 fsBuilder->codeAppendf("vec2 grad = 2.0*scaledOffset*%s.xy;", ellipseRadii.fsIn());
247 fsBuilder->codeAppend("float grad_dot = dot(grad, grad);");
248
commit-bot@chromium.org1b035d82014-04-09 17:11:09 +0000249 // avoid calling inversesqrt on zero.
joshualitt74077b92014-10-24 11:26:03 -0700250 fsBuilder->codeAppend("grad_dot = max(grad_dot, 1.0e-4);");
251 fsBuilder->codeAppend("float invlen = inversesqrt(grad_dot);");
252 fsBuilder->codeAppend("float edgeAlpha = clamp(0.5-test*invlen, 0.0, 1.0);");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000253
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000254 // for inner curve
joshualitt2dd1ae02014-12-03 06:24:10 -0800255 if (ee.isStroked()) {
joshualitt74077b92014-10-24 11:26:03 -0700256 fsBuilder->codeAppendf("scaledOffset = %s*%s.zw;",
257 ellipseOffsets.fsIn(), ellipseRadii.fsIn());
258 fsBuilder->codeAppend("test = dot(scaledOffset, scaledOffset) - 1.0;");
259 fsBuilder->codeAppendf("grad = 2.0*scaledOffset*%s.zw;",
260 ellipseRadii.fsIn());
261 fsBuilder->codeAppend("invlen = inversesqrt(dot(grad, grad));");
262 fsBuilder->codeAppend("edgeAlpha *= clamp(0.5+test*invlen, 0.0, 1.0);");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000263 }
264
joshualitt2dd1ae02014-12-03 06:24:10 -0800265 fsBuilder->codeAppendf("%s = vec4(edgeAlpha);", args.fOutputCoverage);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000266 }
267
joshualittb0a8a372014-09-23 09:50:21 -0700268 static void GenKey(const GrProcessor& processor, const GrGLCaps&,
269 GrProcessorKeyBuilder* b) {
270 const EllipseEdgeEffect& ellipseEffect = processor.cast<EllipseEdgeEffect>();
bsalomon63e99f72014-07-21 08:03:14 -0700271 b->add32(ellipseEffect.isStroked());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000272 }
273
joshualittb0a8a372014-09-23 09:50:21 -0700274 virtual void setData(const GrGLProgramDataManager&, const GrProcessor&) SK_OVERRIDE {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000275 }
276
277 private:
joshualitt249af152014-09-15 11:41:13 -0700278 typedef GrGLGeometryProcessor INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000279 };
280
281private:
joshualitt2dd1ae02014-12-03 06:24:10 -0800282 EllipseEdgeEffect(bool stroke) {
283 fInPosition = &this->addVertexAttrib(GrAttribute("inPosition", kVec2f_GrVertexAttribType));
284 fInEllipseOffset = &this->addVertexAttrib(GrAttribute("inEllipseOffset",
285 kVec2f_GrVertexAttribType));
286 fInEllipseRadii = &this->addVertexAttrib(GrAttribute("inEllipseRadii",
287 kVec4f_GrVertexAttribType));
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000288 fStroke = stroke;
289 }
290
bsalomon0e08fc12014-10-15 08:19:04 -0700291 virtual bool onIsEqual(const GrGeometryProcessor& other) const SK_OVERRIDE {
joshualitt49586be2014-09-16 08:21:41 -0700292 const EllipseEdgeEffect& eee = other.cast<EllipseEdgeEffect>();
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000293 return eee.fStroke == fStroke;
294 }
295
egdaniel605dd0f2014-11-12 08:35:25 -0800296 virtual void onComputeInvariantOutput(GrInvariantOutput* inout) const SK_OVERRIDE {
egdanielccb2e382014-10-13 12:53:46 -0700297 inout->mulByUnknownAlpha();
egdaniel1a8ecdf2014-10-03 06:24:12 -0700298 }
299
joshualitt2dd1ae02014-12-03 06:24:10 -0800300 const GrAttribute* fInPosition;
301 const GrAttribute* fInEllipseOffset;
302 const GrAttribute* fInEllipseRadii;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000303 bool fStroke;
304
joshualittb0a8a372014-09-23 09:50:21 -0700305 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000306
joshualitt249af152014-09-15 11:41:13 -0700307 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000308};
309
joshualittb0a8a372014-09-23 09:50:21 -0700310GR_DEFINE_GEOMETRY_PROCESSOR_TEST(EllipseEdgeEffect);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000311
joshualittb0a8a372014-09-23 09:50:21 -0700312GrGeometryProcessor* EllipseEdgeEffect::TestCreate(SkRandom* random,
313 GrContext* context,
314 const GrDrawTargetCaps&,
315 GrTexture* textures[]) {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000316 return EllipseEdgeEffect::Create(random->nextBool());
317}
318
319///////////////////////////////////////////////////////////////////////////////
320
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000321/**
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000322 * 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 +0000323 * specified as a 2D offset from center for both the outer and inner paths (if stroked). The
324 * implict equation used is for a unit circle (x^2 + y^2 - 1 = 0) and the edge corrected by
325 * using differentials.
326 *
327 * The result is device-independent and can be used with any affine matrix.
328 */
329
joshualitt249af152014-09-15 11:41:13 -0700330class DIEllipseEdgeEffect : public GrGeometryProcessor {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000331public:
332 enum Mode { kStroke = 0, kHairline, kFill };
333
joshualittb0a8a372014-09-23 09:50:21 -0700334 static GrGeometryProcessor* Create(Mode mode) {
bsalomon98b33eb2014-10-15 11:05:26 -0700335 GR_CREATE_STATIC_PROCESSOR(gEllipseStrokeEdge, DIEllipseEdgeEffect, (kStroke));
336 GR_CREATE_STATIC_PROCESSOR(gEllipseHairlineEdge, DIEllipseEdgeEffect, (kHairline));
337 GR_CREATE_STATIC_PROCESSOR(gEllipseFillEdge, DIEllipseEdgeEffect, (kFill));
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000338
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000339 if (kStroke == mode) {
340 gEllipseStrokeEdge->ref();
341 return gEllipseStrokeEdge;
342 } else if (kHairline == mode) {
343 gEllipseHairlineEdge->ref();
344 return gEllipseHairlineEdge;
345 } else {
346 gEllipseFillEdge->ref();
347 return gEllipseFillEdge;
348 }
349 }
350
joshualittb0a8a372014-09-23 09:50:21 -0700351 virtual const GrBackendGeometryProcessorFactory& getFactory() const SK_OVERRIDE {
352 return GrTBackendGeometryProcessorFactory<DIEllipseEdgeEffect>::getInstance();
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000353 }
354
355 virtual ~DIEllipseEdgeEffect() {}
356
357 static const char* Name() { return "DIEllipseEdge"; }
358
joshualitt2dd1ae02014-12-03 06:24:10 -0800359 const GrAttribute* inPosition() const { return fInPosition; }
360 const GrAttribute* inEllipseOffsets0() const { return fInEllipseOffsets0; }
361 const GrAttribute* inEllipseOffsets1() const { return fInEllipseOffsets1; }
joshualitt249af152014-09-15 11:41:13 -0700362
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000363 inline Mode getMode() const { return fMode; }
364
joshualittb0a8a372014-09-23 09:50:21 -0700365 class GLProcessor : public GrGLGeometryProcessor {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000366 public:
joshualittb0a8a372014-09-23 09:50:21 -0700367 GLProcessor(const GrBackendProcessorFactory& factory, const GrProcessor&)
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000368 : INHERITED (factory) {}
369
joshualittc369e7c2014-10-22 10:56:26 -0700370 virtual void emitCode(const EmitArgs& args) SK_OVERRIDE {
joshualitt2dd1ae02014-12-03 06:24:10 -0800371 const DIEllipseEdgeEffect& ee = args.fGP.cast<DIEllipseEdgeEffect>();
372
373 GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder();
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000374
joshualitt74077b92014-10-24 11:26:03 -0700375 GrGLVertToFrag offsets0(kVec2f_GrSLType);
376 args.fPB->addVarying("EllipseOffsets0", &offsets0);
joshualitt74077b92014-10-24 11:26:03 -0700377 vsBuilder->codeAppendf("%s = %s;", offsets0.vsOut(),
joshualitt2dd1ae02014-12-03 06:24:10 -0800378 ee.inEllipseOffsets0()->fName);
joshualitt74077b92014-10-24 11:26:03 -0700379
380 GrGLVertToFrag offsets1(kVec2f_GrSLType);
381 args.fPB->addVarying("EllipseOffsets1", &offsets1);
382 vsBuilder->codeAppendf("%s = %s;", offsets1.vsOut(),
joshualitt2dd1ae02014-12-03 06:24:10 -0800383 ee.inEllipseOffsets1()->fName);
384
385 // setup coord outputs
386 vsBuilder->codeAppendf("%s = %s;", vsBuilder->positionCoords(), ee.inPosition()->fName);
387 vsBuilder->codeAppendf("%s = %s;", vsBuilder->localCoords(), ee.inPosition()->fName);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000388
joshualitt4973d9d2014-11-08 09:24:25 -0800389 // setup position varying
390 vsBuilder->codeAppendf("%s = %s * vec3(%s, 1);", vsBuilder->glPosition(),
joshualitt2dd1ae02014-12-03 06:24:10 -0800391 vsBuilder->uViewM(), ee.inPosition()->fName);
joshualitt4973d9d2014-11-08 09:24:25 -0800392
joshualittc369e7c2014-10-22 10:56:26 -0700393 GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -0700394 SkAssertResult(fsBuilder->enableFeature(
395 GrGLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature));
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000396 // for outer curve
joshualitt74077b92014-10-24 11:26:03 -0700397 fsBuilder->codeAppendf("vec2 scaledOffset = %s.xy;", offsets0.fsIn());
398 fsBuilder->codeAppend("float test = dot(scaledOffset, scaledOffset) - 1.0;");
399 fsBuilder->codeAppendf("vec2 duvdx = dFdx(%s);", offsets0.fsIn());
400 fsBuilder->codeAppendf("vec2 duvdy = dFdy(%s);", offsets0.fsIn());
401 fsBuilder->codeAppendf("vec2 grad = vec2(2.0*%s.x*duvdx.x + 2.0*%s.y*duvdx.y,"
402 " 2.0*%s.x*duvdy.x + 2.0*%s.y*duvdy.y);",
403 offsets0.fsIn(), offsets0.fsIn(), offsets0.fsIn(), offsets0.fsIn());
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000404
joshualitt74077b92014-10-24 11:26:03 -0700405 fsBuilder->codeAppend("float grad_dot = dot(grad, grad);");
commit-bot@chromium.org1b035d82014-04-09 17:11:09 +0000406 // avoid calling inversesqrt on zero.
joshualitt74077b92014-10-24 11:26:03 -0700407 fsBuilder->codeAppend("grad_dot = max(grad_dot, 1.0e-4);");
408 fsBuilder->codeAppend("float invlen = inversesqrt(grad_dot);");
joshualitt2dd1ae02014-12-03 06:24:10 -0800409 if (kHairline == ee.getMode()) {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000410 // can probably do this with one step
joshualitt74077b92014-10-24 11:26:03 -0700411 fsBuilder->codeAppend("float edgeAlpha = clamp(1.0-test*invlen, 0.0, 1.0);");
412 fsBuilder->codeAppend("edgeAlpha *= clamp(1.0+test*invlen, 0.0, 1.0);");
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000413 } else {
joshualitt74077b92014-10-24 11:26:03 -0700414 fsBuilder->codeAppend("float edgeAlpha = clamp(0.5-test*invlen, 0.0, 1.0);");
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000415 }
416
417 // for inner curve
joshualitt2dd1ae02014-12-03 06:24:10 -0800418 if (kStroke == ee.getMode()) {
joshualitt74077b92014-10-24 11:26:03 -0700419 fsBuilder->codeAppendf("scaledOffset = %s.xy;", offsets1.fsIn());
420 fsBuilder->codeAppend("test = dot(scaledOffset, scaledOffset) - 1.0;");
421 fsBuilder->codeAppendf("duvdx = dFdx(%s);", offsets1.fsIn());
422 fsBuilder->codeAppendf("duvdy = dFdy(%s);", offsets1.fsIn());
423 fsBuilder->codeAppendf("grad = vec2(2.0*%s.x*duvdx.x + 2.0*%s.y*duvdx.y,"
424 " 2.0*%s.x*duvdy.x + 2.0*%s.y*duvdy.y);",
425 offsets1.fsIn(), offsets1.fsIn(), offsets1.fsIn(),
426 offsets1.fsIn());
427 fsBuilder->codeAppend("invlen = inversesqrt(dot(grad, grad));");
428 fsBuilder->codeAppend("edgeAlpha *= clamp(0.5+test*invlen, 0.0, 1.0);");
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000429 }
430
joshualitt2dd1ae02014-12-03 06:24:10 -0800431 fsBuilder->codeAppendf("%s = vec4(edgeAlpha);", args.fOutputCoverage);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000432 }
433
joshualittb0a8a372014-09-23 09:50:21 -0700434 static void GenKey(const GrProcessor& processor, const GrGLCaps&,
435 GrProcessorKeyBuilder* b) {
436 const DIEllipseEdgeEffect& ellipseEffect = processor.cast<DIEllipseEdgeEffect>();
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000437
bsalomon63e99f72014-07-21 08:03:14 -0700438 b->add32(ellipseEffect.getMode());
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000439 }
440
joshualittb0a8a372014-09-23 09:50:21 -0700441 virtual void setData(const GrGLProgramDataManager&, const GrProcessor&) SK_OVERRIDE {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000442 }
443
444 private:
joshualitt249af152014-09-15 11:41:13 -0700445 typedef GrGLGeometryProcessor INHERITED;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000446 };
447
448private:
joshualitt2dd1ae02014-12-03 06:24:10 -0800449 DIEllipseEdgeEffect(Mode mode) {
450 fInPosition = &this->addVertexAttrib(GrAttribute("inPosition", kVec2f_GrVertexAttribType));
451 fInEllipseOffsets0 = &this->addVertexAttrib(GrAttribute("inEllipseOffsets0",
452 kVec2f_GrVertexAttribType));
453 fInEllipseOffsets1 = &this->addVertexAttrib(GrAttribute("inEllipseOffsets1",
454 kVec2f_GrVertexAttribType));
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000455 fMode = mode;
456 }
457
bsalomon0e08fc12014-10-15 08:19:04 -0700458 virtual bool onIsEqual(const GrGeometryProcessor& other) const SK_OVERRIDE {
joshualitt49586be2014-09-16 08:21:41 -0700459 const DIEllipseEdgeEffect& eee = other.cast<DIEllipseEdgeEffect>();
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000460 return eee.fMode == fMode;
461 }
462
egdaniel605dd0f2014-11-12 08:35:25 -0800463 virtual void onComputeInvariantOutput(GrInvariantOutput* inout) const SK_OVERRIDE {
egdanielccb2e382014-10-13 12:53:46 -0700464 inout->mulByUnknownAlpha();
egdaniel1a8ecdf2014-10-03 06:24:12 -0700465 }
466
joshualitt2dd1ae02014-12-03 06:24:10 -0800467 const GrAttribute* fInPosition;
468 const GrAttribute* fInEllipseOffsets0;
469 const GrAttribute* fInEllipseOffsets1;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000470 Mode fMode;
471
joshualittb0a8a372014-09-23 09:50:21 -0700472 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000473
joshualitt249af152014-09-15 11:41:13 -0700474 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000475};
476
joshualittb0a8a372014-09-23 09:50:21 -0700477GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DIEllipseEdgeEffect);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000478
joshualittb0a8a372014-09-23 09:50:21 -0700479GrGeometryProcessor* DIEllipseEdgeEffect::TestCreate(SkRandom* random,
480 GrContext* context,
481 const GrDrawTargetCaps&,
482 GrTexture* textures[]) {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000483 return DIEllipseEdgeEffect::Create((Mode)(random->nextRangeU(0,2)));
484}
485
486///////////////////////////////////////////////////////////////////////////////
487
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000488void GrOvalRenderer::reset() {
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000489 SkSafeSetNull(fRRectIndexBuffer);
joshualitt58a65442014-10-22 20:53:24 -0700490 SkSafeSetNull(fStrokeRRectIndexBuffer);
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000491}
492
joshualitt9853cce2014-11-17 14:22:48 -0800493bool GrOvalRenderer::drawOval(GrDrawTarget* target,
494 GrDrawState* drawState,
495 const GrContext* context,
496 bool useAA,
497 const SkRect& oval,
498 const SkStrokeRec& stroke)
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000499{
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000500 bool useCoverageAA = useAA &&
joshualitt9853cce2014-11-17 14:22:48 -0800501 !drawState->getRenderTarget()->isMultisampled() &&
502 drawState->couldApplyCoverage(*target->caps());
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000503
504 if (!useCoverageAA) {
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000505 return false;
506 }
507
508 const SkMatrix& vm = context->getMatrix();
509
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000510 // we can draw circles
511 if (SkScalarNearlyEqual(oval.width(), oval.height())
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000512 && circle_stays_circle(vm)) {
joshualitt9853cce2014-11-17 14:22:48 -0800513 this->drawCircle(target, drawState, context, useCoverageAA, oval, stroke);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000514 // if we have shader derivative support, render as device-independent
515 } else if (target->caps()->shaderDerivativeSupport()) {
joshualitt9853cce2014-11-17 14:22:48 -0800516 return this->drawDIEllipse(target, drawState, context, useCoverageAA, oval, stroke);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000517 // otherwise axis-aligned ellipses only
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000518 } else if (vm.rectStaysRect()) {
joshualitt9853cce2014-11-17 14:22:48 -0800519 return this->drawEllipse(target, drawState, context, useCoverageAA, oval, stroke);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000520 } else {
521 return false;
522 }
523
524 return true;
525}
526
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000527///////////////////////////////////////////////////////////////////////////////
528
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000529void GrOvalRenderer::drawCircle(GrDrawTarget* target,
joshualitt9853cce2014-11-17 14:22:48 -0800530 GrDrawState* drawState,
joshualitt5ead6da2014-10-22 16:00:29 -0700531 const GrContext* context,
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000532 bool useCoverageAA,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000533 const SkRect& circle,
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000534 const SkStrokeRec& stroke)
535{
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000536 const SkMatrix& vm = drawState->getViewMatrix();
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000537 SkPoint center = SkPoint::Make(circle.centerX(), circle.centerY());
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000538 vm.mapPoints(&center, 1);
539 SkScalar radius = vm.mapRadius(SkScalarHalf(circle.width()));
540 SkScalar strokeWidth = vm.mapRadius(stroke.getWidth());
541
bsalomon@google.com137f1342013-05-29 21:27:53 +0000542 GrDrawState::AutoViewMatrixRestore avmr;
543 if (!avmr.setIdentity(drawState)) {
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000544 return;
545 }
546
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000547 SkStrokeRec::Style style = stroke.getStyle();
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000548 bool isStrokeOnly = SkStrokeRec::kStroke_Style == style ||
549 SkStrokeRec::kHairline_Style == style;
550 bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == style;
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000551
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000552 SkScalar innerRadius = 0.0f;
553 SkScalar outerRadius = radius;
554 SkScalar halfWidth = 0;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000555 if (hasStroke) {
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000556 if (SkScalarNearlyZero(strokeWidth)) {
557 halfWidth = SK_ScalarHalf;
558 } else {
559 halfWidth = SkScalarHalf(strokeWidth);
560 }
561
562 outerRadius += halfWidth;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000563 if (isStrokeOnly) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000564 innerRadius = radius - halfWidth;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000565 }
566 }
567
joshualittb0a8a372014-09-23 09:50:21 -0700568 GrGeometryProcessor* gp = CircleEdgeEffect::Create(isStrokeOnly && innerRadius > 0);
569 drawState->setGeometryProcessor(gp)->unref();
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +0000570
joshualitt2dd1ae02014-12-03 06:24:10 -0800571 GrDrawTarget::AutoReleaseGeometry geo(target, 4, gp->getVertexStride(), 0);
572 SkASSERT(gp->getVertexStride() == sizeof(CircleVertex));
573 if (!geo.succeeded()) {
574 SkDebugf("Failed to get space for vertices!\n");
575 return;
576 }
577
578 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
579
bsalomon@google.com58e30fe2013-04-01 19:01:20 +0000580 // The radii are outset for two reasons. First, it allows the shader to simply perform
581 // clamp(distance-to-center - radius, 0, 1). Second, the outer radius is used to compute the
582 // verts of the bounding box that is rendered and the outset ensures the box will cover all
583 // pixels partially covered by the circle.
584 outerRadius += SK_ScalarHalf;
585 innerRadius -= SK_ScalarHalf;
586
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000587 SkRect bounds = SkRect::MakeLTRB(
bsalomon@google.com58e30fe2013-04-01 19:01:20 +0000588 center.fX - outerRadius,
589 center.fY - outerRadius,
590 center.fX + outerRadius,
591 center.fY + outerRadius
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000592 );
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000593
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000594 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000595 verts[0].fOffset = SkPoint::Make(-outerRadius, -outerRadius);
596 verts[0].fOuterRadius = outerRadius;
597 verts[0].fInnerRadius = innerRadius;
598
joshualitt5ead6da2014-10-22 16:00:29 -0700599 verts[1].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
600 verts[1].fOffset = SkPoint::Make(-outerRadius, outerRadius);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000601 verts[1].fOuterRadius = outerRadius;
602 verts[1].fInnerRadius = innerRadius;
603
joshualitt5ead6da2014-10-22 16:00:29 -0700604 verts[2].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
605 verts[2].fOffset = SkPoint::Make(outerRadius, outerRadius);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000606 verts[2].fOuterRadius = outerRadius;
607 verts[2].fInnerRadius = innerRadius;
608
joshualitt5ead6da2014-10-22 16:00:29 -0700609 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
610 verts[3].fOffset = SkPoint::Make(outerRadius, -outerRadius);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000611 verts[3].fOuterRadius = outerRadius;
612 verts[3].fInnerRadius = innerRadius;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000613
joshualitt5ead6da2014-10-22 16:00:29 -0700614 target->setIndexSourceToBuffer(context->getGpu()->getQuadIndexBuffer());
joshualitt9853cce2014-11-17 14:22:48 -0800615 target->drawIndexedInstances(drawState, kTriangles_GrPrimitiveType, 1, 4, 6, &bounds);
joshualitt5ead6da2014-10-22 16:00:29 -0700616 target->resetIndexSource();
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000617}
618
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000619///////////////////////////////////////////////////////////////////////////////
620
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000621bool GrOvalRenderer::drawEllipse(GrDrawTarget* target,
joshualitt9853cce2014-11-17 14:22:48 -0800622 GrDrawState* drawState,
joshualitt5ead6da2014-10-22 16:00:29 -0700623 const GrContext* context,
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000624 bool useCoverageAA,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000625 const SkRect& ellipse,
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000626 const SkStrokeRec& stroke)
627{
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000628#ifdef SK_DEBUG
629 {
630 // we should have checked for this previously
631 bool isAxisAlignedEllipse = drawState->getViewMatrix().rectStaysRect();
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000632 SkASSERT(useCoverageAA && isAxisAlignedEllipse);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000633 }
634#endif
635
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000636 // do any matrix crunching before we reset the draw state for device coords
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000637 const SkMatrix& vm = drawState->getViewMatrix();
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000638 SkPoint center = SkPoint::Make(ellipse.centerX(), ellipse.centerY());
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000639 vm.mapPoints(&center, 1);
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000640 SkScalar ellipseXRadius = SkScalarHalf(ellipse.width());
641 SkScalar ellipseYRadius = SkScalarHalf(ellipse.height());
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +0000642 SkScalar xRadius = SkScalarAbs(vm[SkMatrix::kMScaleX]*ellipseXRadius +
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000643 vm[SkMatrix::kMSkewY]*ellipseYRadius);
skia.committer@gmail.com64b682c2013-04-20 07:01:07 +0000644 SkScalar yRadius = SkScalarAbs(vm[SkMatrix::kMSkewX]*ellipseXRadius +
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000645 vm[SkMatrix::kMScaleY]*ellipseYRadius);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000646
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000647 // do (potentially) anisotropic mapping of stroke
648 SkVector scaledStroke;
649 SkScalar strokeWidth = stroke.getWidth();
650 scaledStroke.fX = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMScaleX] + vm[SkMatrix::kMSkewY]));
651 scaledStroke.fY = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMSkewX] + vm[SkMatrix::kMScaleY]));
652
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000653 SkStrokeRec::Style style = stroke.getStyle();
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000654 bool isStrokeOnly = SkStrokeRec::kStroke_Style == style ||
655 SkStrokeRec::kHairline_Style == style;
656 bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == style;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000657
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000658 SkScalar innerXRadius = 0;
659 SkScalar innerYRadius = 0;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000660 if (hasStroke) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000661 if (SkScalarNearlyZero(scaledStroke.length())) {
662 scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf);
663 } else {
664 scaledStroke.scale(SK_ScalarHalf);
665 }
666
667 // we only handle thick strokes for near-circular ellipses
668 if (scaledStroke.length() > SK_ScalarHalf &&
669 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
670 return false;
671 }
672
673 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
674 if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius ||
675 scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) {
676 return false;
677 }
678
679 // this is legit only if scale & translation (which should be the case at the moment)
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000680 if (isStrokeOnly) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000681 innerXRadius = xRadius - scaledStroke.fX;
682 innerYRadius = yRadius - scaledStroke.fY;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000683 }
684
685 xRadius += scaledStroke.fX;
686 yRadius += scaledStroke.fY;
687 }
688
bsalomon@google.com137f1342013-05-29 21:27:53 +0000689 GrDrawState::AutoViewMatrixRestore avmr;
690 if (!avmr.setIdentity(drawState)) {
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000691 return false;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000692 }
693
joshualitt2dd1ae02014-12-03 06:24:10 -0800694 GrGeometryProcessor* gp = EllipseEdgeEffect::Create(isStrokeOnly &&
695 innerXRadius > 0 && innerYRadius > 0);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000696
joshualitt2dd1ae02014-12-03 06:24:10 -0800697 drawState->setGeometryProcessor(gp)->unref();
698
699 GrDrawTarget::AutoReleaseGeometry geo(target, 4, gp->getVertexStride(), 0);
700 SkASSERT(gp->getVertexStride() == sizeof(EllipseVertex));
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000701 if (!geo.succeeded()) {
tfarina38406c82014-10-31 07:11:12 -0700702 SkDebugf("Failed to get space for vertices!\n");
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000703 return false;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000704 }
705
706 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices());
707
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000708 // Compute the reciprocals of the radii here to save time in the shader
709 SkScalar xRadRecip = SkScalarInvert(xRadius);
710 SkScalar yRadRecip = SkScalarInvert(yRadius);
711 SkScalar xInnerRadRecip = SkScalarInvert(innerXRadius);
712 SkScalar yInnerRadRecip = SkScalarInvert(innerYRadius);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000713
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000714 // We've extended the outer x radius out half a pixel to antialias.
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000715 // This will also expand the rect so all the pixels will be captured.
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000716 // TODO: Consider if we should use sqrt(2)/2 instead
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000717 xRadius += SK_ScalarHalf;
718 yRadius += SK_ScalarHalf;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000719
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000720 SkRect bounds = SkRect::MakeLTRB(
721 center.fX - xRadius,
722 center.fY - yRadius,
723 center.fX + xRadius,
724 center.fY + yRadius
725 );
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000726
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000727 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000728 verts[0].fOffset = SkPoint::Make(-xRadius, -yRadius);
729 verts[0].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
730 verts[0].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000731
joshualitt5ead6da2014-10-22 16:00:29 -0700732 verts[1].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
733 verts[1].fOffset = SkPoint::Make(-xRadius, yRadius);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000734 verts[1].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
735 verts[1].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000736
joshualitt5ead6da2014-10-22 16:00:29 -0700737 verts[2].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
738 verts[2].fOffset = SkPoint::Make(xRadius, yRadius);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000739 verts[2].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
740 verts[2].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000741
joshualitt5ead6da2014-10-22 16:00:29 -0700742 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
743 verts[3].fOffset = SkPoint::Make(xRadius, -yRadius);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000744 verts[3].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
745 verts[3].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
skia.committer@gmail.com46746762013-04-12 07:01:51 +0000746
joshualitt5ead6da2014-10-22 16:00:29 -0700747 target->setIndexSourceToBuffer(context->getGpu()->getQuadIndexBuffer());
joshualitt9853cce2014-11-17 14:22:48 -0800748 target->drawIndexedInstances(drawState, kTriangles_GrPrimitiveType, 1, 4, 6, &bounds);
joshualitt5ead6da2014-10-22 16:00:29 -0700749 target->resetIndexSource();
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +0000750
751 return true;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000752}
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000753
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000754bool GrOvalRenderer::drawDIEllipse(GrDrawTarget* target,
joshualitt9853cce2014-11-17 14:22:48 -0800755 GrDrawState* drawState,
joshualitt5ead6da2014-10-22 16:00:29 -0700756 const GrContext* context,
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000757 bool useCoverageAA,
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000758 const SkRect& ellipse,
759 const SkStrokeRec& stroke)
760{
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000761 const SkMatrix& vm = drawState->getViewMatrix();
762
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000763 SkPoint center = SkPoint::Make(ellipse.centerX(), ellipse.centerY());
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000764 SkScalar xRadius = SkScalarHalf(ellipse.width());
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000765 SkScalar yRadius = SkScalarHalf(ellipse.height());
766
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000767 SkStrokeRec::Style style = stroke.getStyle();
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000768 DIEllipseEdgeEffect::Mode mode = (SkStrokeRec::kStroke_Style == style) ?
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000769 DIEllipseEdgeEffect::kStroke :
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000770 (SkStrokeRec::kHairline_Style == style) ?
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000771 DIEllipseEdgeEffect::kHairline : DIEllipseEdgeEffect::kFill;
772
773 SkScalar innerXRadius = 0;
774 SkScalar innerYRadius = 0;
775 if (SkStrokeRec::kFill_Style != style && SkStrokeRec::kHairline_Style != style) {
776 SkScalar strokeWidth = stroke.getWidth();
777
778 if (SkScalarNearlyZero(strokeWidth)) {
779 strokeWidth = SK_ScalarHalf;
780 } else {
781 strokeWidth *= SK_ScalarHalf;
782 }
783
784 // we only handle thick strokes for near-circular ellipses
785 if (strokeWidth > SK_ScalarHalf &&
786 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
787 return false;
788 }
789
790 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
791 if (strokeWidth*(yRadius*yRadius) < (strokeWidth*strokeWidth)*xRadius ||
792 strokeWidth*(xRadius*xRadius) < (strokeWidth*strokeWidth)*yRadius) {
793 return false;
794 }
795
796 // set inner radius (if needed)
797 if (SkStrokeRec::kStroke_Style == style) {
798 innerXRadius = xRadius - strokeWidth;
799 innerYRadius = yRadius - strokeWidth;
800 }
801
802 xRadius += strokeWidth;
803 yRadius += strokeWidth;
804 }
805 if (DIEllipseEdgeEffect::kStroke == mode) {
806 mode = (innerXRadius > 0 && innerYRadius > 0) ? DIEllipseEdgeEffect::kStroke :
807 DIEllipseEdgeEffect::kFill;
808 }
809 SkScalar innerRatioX = SkScalarDiv(xRadius, innerXRadius);
810 SkScalar innerRatioY = SkScalarDiv(yRadius, innerYRadius);
811
joshualitt2dd1ae02014-12-03 06:24:10 -0800812 GrGeometryProcessor* gp = DIEllipseEdgeEffect::Create(mode);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000813
joshualitt2dd1ae02014-12-03 06:24:10 -0800814 drawState->setGeometryProcessor(gp)->unref();
815
816 GrDrawTarget::AutoReleaseGeometry geo(target, 4, gp->getVertexStride(), 0);
817 SkASSERT(gp->getVertexStride() == sizeof(DIEllipseVertex));
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000818 if (!geo.succeeded()) {
tfarina38406c82014-10-31 07:11:12 -0700819 SkDebugf("Failed to get space for vertices!\n");
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000820 return false;
821 }
822
823 DIEllipseVertex* verts = reinterpret_cast<DIEllipseVertex*>(geo.vertices());
824
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000825 // This expands the outer rect so that after CTM we end up with a half-pixel border
826 SkScalar a = vm[SkMatrix::kMScaleX];
827 SkScalar b = vm[SkMatrix::kMSkewX];
828 SkScalar c = vm[SkMatrix::kMSkewY];
829 SkScalar d = vm[SkMatrix::kMScaleY];
830 SkScalar geoDx = SkScalarDiv(SK_ScalarHalf, SkScalarSqrt(a*a + c*c));
831 SkScalar geoDy = SkScalarDiv(SK_ScalarHalf, SkScalarSqrt(b*b + d*d));
832 // This adjusts the "radius" to include the half-pixel border
833 SkScalar offsetDx = SkScalarDiv(geoDx, xRadius);
834 SkScalar offsetDy = SkScalarDiv(geoDy, yRadius);
835
836 SkRect bounds = SkRect::MakeLTRB(
837 center.fX - xRadius - geoDx,
838 center.fY - yRadius - geoDy,
839 center.fX + xRadius + geoDx,
840 center.fY + yRadius + geoDy
841 );
842
843 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
844 verts[0].fOuterOffset = SkPoint::Make(-1.0f - offsetDx, -1.0f - offsetDy);
845 verts[0].fInnerOffset = SkPoint::Make(-innerRatioX - offsetDx, -innerRatioY - offsetDy);
846
joshualitt5ead6da2014-10-22 16:00:29 -0700847 verts[1].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
848 verts[1].fOuterOffset = SkPoint::Make(-1.0f - offsetDx, 1.0f + offsetDy);
849 verts[1].fInnerOffset = SkPoint::Make(-innerRatioX - offsetDx, innerRatioY + offsetDy);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000850
joshualitt5ead6da2014-10-22 16:00:29 -0700851 verts[2].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
852 verts[2].fOuterOffset = SkPoint::Make(1.0f + offsetDx, 1.0f + offsetDy);
853 verts[2].fInnerOffset = SkPoint::Make(innerRatioX + offsetDx, innerRatioY + offsetDy);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000854
joshualitt5ead6da2014-10-22 16:00:29 -0700855 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
856 verts[3].fOuterOffset = SkPoint::Make(1.0f + offsetDx, -1.0f - offsetDy);
857 verts[3].fInnerOffset = SkPoint::Make(innerRatioX + offsetDx, -innerRatioY - offsetDy);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000858
joshualitt5ead6da2014-10-22 16:00:29 -0700859 target->setIndexSourceToBuffer(context->getGpu()->getQuadIndexBuffer());
joshualitt9853cce2014-11-17 14:22:48 -0800860 target->drawIndexedInstances(drawState, kTriangles_GrPrimitiveType, 1, 4, 6, &bounds);
joshualitt5ead6da2014-10-22 16:00:29 -0700861 target->resetIndexSource();
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000862
863 return true;
864}
865
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000866///////////////////////////////////////////////////////////////////////////////
867
868static const uint16_t gRRectIndices[] = {
869 // corners
870 0, 1, 5, 0, 5, 4,
871 2, 3, 7, 2, 7, 6,
872 8, 9, 13, 8, 13, 12,
873 10, 11, 15, 10, 15, 14,
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000874
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000875 // edges
876 1, 2, 6, 1, 6, 5,
877 4, 5, 9, 4, 9, 8,
878 6, 7, 11, 6, 11, 10,
879 9, 10, 14, 9, 14, 13,
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000880
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000881 // center
882 // we place this at the end so that we can ignore these indices when rendering stroke-only
883 5, 6, 10, 5, 10, 9
884};
885
joshualitt5ead6da2014-10-22 16:00:29 -0700886static const int kIndicesPerStrokeRRect = SK_ARRAY_COUNT(gRRectIndices) - 6;
887static const int kIndicesPerRRect = SK_ARRAY_COUNT(gRRectIndices);
888static const int kVertsPerRRect = 16;
889static const int kNumRRectsInIndexBuffer = 256;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000890
joshualitt5ead6da2014-10-22 16:00:29 -0700891GrIndexBuffer* GrOvalRenderer::rRectIndexBuffer(bool isStrokeOnly, GrGpu* gpu) {
892 if (isStrokeOnly) {
893 if (NULL == fStrokeRRectIndexBuffer) {
894 fStrokeRRectIndexBuffer = gpu->createInstancedIndexBuffer(gRRectIndices,
895 kIndicesPerStrokeRRect,
896 kNumRRectsInIndexBuffer,
897 kVertsPerRRect);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000898 }
joshualitt5ead6da2014-10-22 16:00:29 -0700899 return fStrokeRRectIndexBuffer;
900 } else {
901 if (NULL == fRRectIndexBuffer) {
902 fRRectIndexBuffer = gpu->createInstancedIndexBuffer(gRRectIndices,
903 kIndicesPerRRect,
904 kNumRRectsInIndexBuffer,
905 kVertsPerRRect);
906 }
907 return fRRectIndexBuffer;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000908 }
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000909}
910
joshualitt9853cce2014-11-17 14:22:48 -0800911bool GrOvalRenderer::drawDRRect(GrDrawTarget* target,
912 GrDrawState* drawState,
913 GrContext* context,
914 bool useAA,
915 const SkRRect& origOuter,
916 const SkRRect& origInner) {
bsalomon8af05232014-06-03 06:34:58 -0700917 bool applyAA = useAA &&
joshualitt9853cce2014-11-17 14:22:48 -0800918 !drawState->getRenderTarget()->isMultisampled() &&
919 drawState->couldApplyCoverage(*target->caps());
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000920 GrDrawState::AutoRestoreEffects are;
921 if (!origInner.isEmpty()) {
922 SkTCopyOnFirstWrite<SkRRect> inner(origInner);
923 if (!context->getMatrix().isIdentity()) {
924 if (!origInner.transform(context->getMatrix(), inner.writable())) {
925 return false;
926 }
927 }
joshualittb0a8a372014-09-23 09:50:21 -0700928 GrPrimitiveEdgeType edgeType = applyAA ?
929 kInverseFillAA_GrProcessorEdgeType :
930 kInverseFillBW_GrProcessorEdgeType;
931 GrFragmentProcessor* fp = GrRRectEffect::Create(edgeType, *inner);
932 if (NULL == fp) {
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000933 return false;
934 }
joshualitt9853cce2014-11-17 14:22:48 -0800935 are.set(drawState);
936 drawState->addCoverageProcessor(fp)->unref();
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000937 }
938
939 SkStrokeRec fillRec(SkStrokeRec::kFill_InitStyle);
joshualitt9853cce2014-11-17 14:22:48 -0800940 if (this->drawRRect(target, drawState, context, useAA, origOuter, fillRec)) {
bsalomon8af05232014-06-03 06:34:58 -0700941 return true;
942 }
943
944 SkASSERT(!origOuter.isEmpty());
945 SkTCopyOnFirstWrite<SkRRect> outer(origOuter);
946 if (!context->getMatrix().isIdentity()) {
947 if (!origOuter.transform(context->getMatrix(), outer.writable())) {
948 return false;
949 }
950 }
joshualittb0a8a372014-09-23 09:50:21 -0700951 GrPrimitiveEdgeType edgeType = applyAA ? kFillAA_GrProcessorEdgeType :
952 kFillBW_GrProcessorEdgeType;
953 GrFragmentProcessor* effect = GrRRectEffect::Create(edgeType, *outer);
bsalomon8af05232014-06-03 06:34:58 -0700954 if (NULL == effect) {
955 return false;
956 }
957 if (!are.isSet()) {
joshualitt9853cce2014-11-17 14:22:48 -0800958 are.set(drawState);
bsalomon8af05232014-06-03 06:34:58 -0700959 }
960 GrDrawState::AutoViewMatrixRestore avmr;
joshualitt9853cce2014-11-17 14:22:48 -0800961 if (!avmr.setIdentity(drawState)) {
bsalomon8af05232014-06-03 06:34:58 -0700962 return false;
963 }
joshualitt9853cce2014-11-17 14:22:48 -0800964 drawState->addCoverageProcessor(effect)->unref();
bsalomon8af05232014-06-03 06:34:58 -0700965 SkRect bounds = outer->getBounds();
966 if (applyAA) {
967 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
968 }
joshualitt9853cce2014-11-17 14:22:48 -0800969 target->drawRect(drawState, bounds, NULL, NULL);
bsalomon8af05232014-06-03 06:34:58 -0700970 return true;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000971}
972
joshualitt9853cce2014-11-17 14:22:48 -0800973bool GrOvalRenderer::drawRRect(GrDrawTarget* target,
974 GrDrawState* drawState,
975 GrContext* context,
976 bool useAA,
977 const SkRRect& rrect,
978 const SkStrokeRec& stroke) {
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000979 if (rrect.isOval()) {
joshualitt9853cce2014-11-17 14:22:48 -0800980 return this->drawOval(target, drawState, context, useAA, rrect.getBounds(), stroke);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000981 }
982
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000983 bool useCoverageAA = useAA &&
joshualitt9853cce2014-11-17 14:22:48 -0800984 !drawState->getRenderTarget()->isMultisampled() &&
985 drawState->couldApplyCoverage(*target->caps());
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000986
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000987 // only anti-aliased rrects for now
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000988 if (!useCoverageAA) {
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000989 return false;
990 }
991
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000992 const SkMatrix& vm = context->getMatrix();
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000993
994 if (!vm.rectStaysRect() || !rrect.isSimple()) {
995 return false;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000996 }
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000997
998 // do any matrix crunching before we reset the draw state for device coords
999 const SkRect& rrectBounds = rrect.getBounds();
1000 SkRect bounds;
1001 vm.mapRect(&bounds, rrectBounds);
1002
1003 SkVector radii = rrect.getSimpleRadii();
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001004 SkScalar xRadius = SkScalarAbs(vm[SkMatrix::kMScaleX]*radii.fX +
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001005 vm[SkMatrix::kMSkewY]*radii.fY);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001006 SkScalar yRadius = SkScalarAbs(vm[SkMatrix::kMSkewX]*radii.fX +
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001007 vm[SkMatrix::kMScaleY]*radii.fY);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001008
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001009 SkStrokeRec::Style style = stroke.getStyle();
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001010
1011 // do (potentially) anisotropic mapping of stroke
1012 SkVector scaledStroke;
1013 SkScalar strokeWidth = stroke.getWidth();
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001014
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001015 bool isStrokeOnly = SkStrokeRec::kStroke_Style == style ||
1016 SkStrokeRec::kHairline_Style == style;
1017 bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == style;
1018
1019 if (hasStroke) {
1020 if (SkStrokeRec::kHairline_Style == style) {
1021 scaledStroke.set(1, 1);
1022 } else {
1023 scaledStroke.fX = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMScaleX] +
1024 vm[SkMatrix::kMSkewY]));
1025 scaledStroke.fY = SkScalarAbs(strokeWidth*(vm[SkMatrix::kMSkewX] +
1026 vm[SkMatrix::kMScaleY]));
1027 }
1028
1029 // if half of strokewidth is greater than radius, we don't handle that right now
1030 if (SK_ScalarHalf*scaledStroke.fX > xRadius || SK_ScalarHalf*scaledStroke.fY > yRadius) {
1031 return false;
1032 }
1033 }
1034
1035 // The way the effect interpolates the offset-to-ellipse/circle-center attribute only works on
1036 // the interior of the rrect if the radii are >= 0.5. Otherwise, the inner rect of the nine-
1037 // patch will have fractional coverage. This only matters when the interior is actually filled.
1038 // We could consider falling back to rect rendering here, since a tiny radius is
1039 // indistinguishable from a square corner.
1040 if (!isStrokeOnly && (SK_ScalarHalf > xRadius || SK_ScalarHalf > yRadius)) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001041 return false;
1042 }
1043
1044 // reset to device coordinates
bsalomon@google.com137f1342013-05-29 21:27:53 +00001045 GrDrawState::AutoViewMatrixRestore avmr;
1046 if (!avmr.setIdentity(drawState)) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001047 return false;
1048 }
1049
joshualitt5ead6da2014-10-22 16:00:29 -07001050 GrIndexBuffer* indexBuffer = this->rRectIndexBuffer(isStrokeOnly, context->getGpu());
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001051 if (NULL == indexBuffer) {
tfarina38406c82014-10-31 07:11:12 -07001052 SkDebugf("Failed to create index buffer!\n");
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001053 return false;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001054 }
1055
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001056 // if the corners are circles, use the circle renderer
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001057 if ((!hasStroke || scaledStroke.fX == scaledStroke.fY) && xRadius == yRadius) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001058 SkScalar innerRadius = 0.0f;
1059 SkScalar outerRadius = xRadius;
1060 SkScalar halfWidth = 0;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001061 if (hasStroke) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001062 if (SkScalarNearlyZero(scaledStroke.fX)) {
1063 halfWidth = SK_ScalarHalf;
1064 } else {
1065 halfWidth = SkScalarHalf(scaledStroke.fX);
1066 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001067
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001068 if (isStrokeOnly) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001069 innerRadius = xRadius - halfWidth;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001070 }
1071 outerRadius += halfWidth;
1072 bounds.outset(halfWidth, halfWidth);
1073 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001074
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001075 isStrokeOnly = (isStrokeOnly && innerRadius >= 0);
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +00001076
joshualittb0a8a372014-09-23 09:50:21 -07001077 GrGeometryProcessor* effect = CircleEdgeEffect::Create(isStrokeOnly);
joshualitt249af152014-09-15 11:41:13 -07001078 drawState->setGeometryProcessor(effect)->unref();
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +00001079
joshualitt2dd1ae02014-12-03 06:24:10 -08001080 GrDrawTarget::AutoReleaseGeometry geo(target, 16, effect->getVertexStride(), 0);
1081 SkASSERT(effect->getVertexStride() == sizeof(CircleVertex));
1082 if (!geo.succeeded()) {
1083 SkDebugf("Failed to get space for vertices!\n");
1084 return false;
1085 }
1086 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
1087
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001088 // The radii are outset for two reasons. First, it allows the shader to simply perform
1089 // clamp(distance-to-center - radius, 0, 1). Second, the outer radius is used to compute the
1090 // verts of the bounding box that is rendered and the outset ensures the box will cover all
1091 // pixels partially covered by the circle.
1092 outerRadius += SK_ScalarHalf;
1093 innerRadius -= SK_ScalarHalf;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001094
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001095 // Expand the rect so all the pixels will be captured.
1096 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001097
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001098 SkScalar yCoords[4] = {
1099 bounds.fTop,
1100 bounds.fTop + outerRadius,
1101 bounds.fBottom - outerRadius,
1102 bounds.fBottom
1103 };
1104 SkScalar yOuterRadii[4] = {
1105 -outerRadius,
1106 0,
1107 0,
1108 outerRadius
1109 };
1110 for (int i = 0; i < 4; ++i) {
1111 verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]);
1112 verts->fOffset = SkPoint::Make(-outerRadius, yOuterRadii[i]);
1113 verts->fOuterRadius = outerRadius;
1114 verts->fInnerRadius = innerRadius;
1115 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001116
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001117 verts->fPos = SkPoint::Make(bounds.fLeft + outerRadius, yCoords[i]);
1118 verts->fOffset = SkPoint::Make(0, yOuterRadii[i]);
1119 verts->fOuterRadius = outerRadius;
1120 verts->fInnerRadius = innerRadius;
1121 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001122
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001123 verts->fPos = SkPoint::Make(bounds.fRight - outerRadius, yCoords[i]);
1124 verts->fOffset = SkPoint::Make(0, yOuterRadii[i]);
1125 verts->fOuterRadius = outerRadius;
1126 verts->fInnerRadius = innerRadius;
1127 verts++;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001128
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001129 verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]);
1130 verts->fOffset = SkPoint::Make(outerRadius, yOuterRadii[i]);
1131 verts->fOuterRadius = outerRadius;
1132 verts->fInnerRadius = innerRadius;
1133 verts++;
1134 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001135
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001136 // drop out the middle quad if we're stroked
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001137 int indexCnt = isStrokeOnly ? SK_ARRAY_COUNT(gRRectIndices) - 6 :
1138 SK_ARRAY_COUNT(gRRectIndices);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001139 target->setIndexSourceToBuffer(indexBuffer);
joshualitt9853cce2014-11-17 14:22:48 -08001140 target->drawIndexedInstances(drawState, kTriangles_GrPrimitiveType, 1, 16, indexCnt,
1141 &bounds);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001142
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001143 // otherwise we use the ellipse renderer
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001144 } else {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001145 SkScalar innerXRadius = 0.0f;
1146 SkScalar innerYRadius = 0.0f;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001147 if (hasStroke) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001148 if (SkScalarNearlyZero(scaledStroke.length())) {
1149 scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf);
1150 } else {
1151 scaledStroke.scale(SK_ScalarHalf);
1152 }
1153
1154 // we only handle thick strokes for near-circular ellipses
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +00001155 if (scaledStroke.length() > SK_ScalarHalf &&
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001156 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
1157 return false;
1158 }
1159
1160 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
1161 if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius ||
1162 scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) {
1163 return false;
1164 }
1165
1166 // this is legit only if scale & translation (which should be the case at the moment)
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001167 if (isStrokeOnly) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001168 innerXRadius = xRadius - scaledStroke.fX;
1169 innerYRadius = yRadius - scaledStroke.fY;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001170 }
1171
1172 xRadius += scaledStroke.fX;
1173 yRadius += scaledStroke.fY;
1174 bounds.outset(scaledStroke.fX, scaledStroke.fY);
1175 }
jvanverth@google.come3647412013-05-08 15:31:43 +00001176
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001177 isStrokeOnly = (isStrokeOnly && innerXRadius >= 0 && innerYRadius >= 0);
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +00001178
joshualitt2dd1ae02014-12-03 06:24:10 -08001179 GrGeometryProcessor* effect = EllipseEdgeEffect::Create(isStrokeOnly);
1180 drawState->setGeometryProcessor(effect)->unref();
1181
1182 GrDrawTarget::AutoReleaseGeometry geo(target, 16, effect->getVertexStride(), 0);
1183 SkASSERT(effect->getVertexStride() == sizeof(EllipseVertex));
jvanverth@google.come3647412013-05-08 15:31:43 +00001184 if (!geo.succeeded()) {
tfarina38406c82014-10-31 07:11:12 -07001185 SkDebugf("Failed to get space for vertices!\n");
jvanverth@google.come3647412013-05-08 15:31:43 +00001186 return false;
1187 }
jvanverth@google.come3647412013-05-08 15:31:43 +00001188
joshualitt2dd1ae02014-12-03 06:24:10 -08001189 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices());
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001190
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001191 // Compute the reciprocals of the radii here to save time in the shader
1192 SkScalar xRadRecip = SkScalarInvert(xRadius);
1193 SkScalar yRadRecip = SkScalarInvert(yRadius);
1194 SkScalar xInnerRadRecip = SkScalarInvert(innerXRadius);
1195 SkScalar yInnerRadRecip = SkScalarInvert(innerYRadius);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001196
1197 // Extend the radii out half a pixel to antialias.
1198 SkScalar xOuterRadius = xRadius + SK_ScalarHalf;
1199 SkScalar yOuterRadius = yRadius + SK_ScalarHalf;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001200
1201 // Expand the rect so all the pixels will be captured.
1202 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1203
1204 SkScalar yCoords[4] = {
1205 bounds.fTop,
1206 bounds.fTop + yOuterRadius,
1207 bounds.fBottom - yOuterRadius,
1208 bounds.fBottom
1209 };
1210 SkScalar yOuterOffsets[4] = {
jvanverth@google.comd1b5b142013-07-02 14:46:03 +00001211 yOuterRadius,
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001212 SK_ScalarNearlyZero, // we're using inversesqrt() in the shader, so can't be exactly 0
1213 SK_ScalarNearlyZero,
1214 yOuterRadius
1215 };
1216
1217 for (int i = 0; i < 4; ++i) {
1218 verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]);
jvanverth@google.comd1b5b142013-07-02 14:46:03 +00001219 verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001220 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1221 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001222 verts++;
1223
1224 verts->fPos = SkPoint::Make(bounds.fLeft + xOuterRadius, yCoords[i]);
1225 verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001226 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1227 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001228 verts++;
1229
1230 verts->fPos = SkPoint::Make(bounds.fRight - xOuterRadius, yCoords[i]);
1231 verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001232 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1233 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001234 verts++;
1235
1236 verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]);
1237 verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001238 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1239 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001240 verts++;
1241 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001242
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001243 // drop out the middle quad if we're stroked
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001244 int indexCnt = isStrokeOnly ? SK_ARRAY_COUNT(gRRectIndices) - 6 :
1245 SK_ARRAY_COUNT(gRRectIndices);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001246 target->setIndexSourceToBuffer(indexBuffer);
joshualitt9853cce2014-11-17 14:22:48 -08001247 target->drawIndexedInstances(drawState, kTriangles_GrPrimitiveType, 1, 16, indexCnt,
1248 &bounds);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001249 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001250
joshualitt5ead6da2014-10-22 16:00:29 -07001251 target->resetIndexSource();
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001252 return true;
1253}