blob: 3c438d455e4bbf865eea120d1212d064ac118246 [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
bsalomon75398562015-08-17 12:55:38 -070010#include "GrBatchFlushState.h"
joshualitt3e708c52015-04-30 13:49:27 -070011#include "GrBatchTest.h"
commit-bot@chromium.org81312832013-03-22 18:34:09 +000012#include "GrDrawTarget.h"
joshualitteb2a6762014-12-04 11:35:33 -080013#include "GrGeometryProcessor.h"
egdaniel605dd0f2014-11-12 08:35:25 -080014#include "GrInvariantOutput.h"
egdaniel8dd688b2015-01-22 10:16:09 -080015#include "GrPipelineBuilder.h"
joshualitt76e7fb62015-02-11 08:52:27 -080016#include "GrProcessor.h"
bsalomoned0bcad2015-05-04 10:36:42 -070017#include "GrResourceProvider.h"
bsalomon72e3ae42015-04-28 08:08:46 -070018#include "GrVertexBuffer.h"
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +000019#include "SkRRect.h"
commit-bot@chromium.org81312832013-03-22 18:34:09 +000020#include "SkStrokeRec.h"
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +000021#include "SkTLazy.h"
bsalomon16b99132015-08-13 14:55:50 -070022#include "batches/GrVertexBatch.h"
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +000023#include "effects/GrRRectEffect.h"
egdaniel2d721d32015-11-11 13:06:05 -080024#include "glsl/GrGLSLFragmentShaderBuilder.h"
egdaniele659a582015-11-13 09:55:43 -080025#include "glsl/GrGLSLGeometryProcessor.h"
egdaniel2d721d32015-11-11 13:06:05 -080026#include "glsl/GrGLSLProgramBuilder.h"
egdaniel018fb622015-10-28 07:26:40 -070027#include "glsl/GrGLSLProgramDataManager.h"
egdaniel2d721d32015-11-11 13:06:05 -080028#include "glsl/GrGLSLVertexShaderBuilder.h"
egdaniel64c47282015-11-13 06:54:19 -080029#include "glsl/GrGLSLUtil.h"
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +000030
joshualitt76e7fb62015-02-11 08:52:27 -080031// TODO(joshualitt) - Break this file up during GrBatch post implementation cleanup
32
commit-bot@chromium.org81312832013-03-22 18:34:09 +000033namespace {
joshualitt5ead6da2014-10-22 16:00:29 -070034// TODO(joshualitt) add per vertex colors
commit-bot@chromium.org81312832013-03-22 18:34:09 +000035struct CircleVertex {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000036 SkPoint fPos;
37 SkPoint fOffset;
commit-bot@chromium.org81312832013-03-22 18:34:09 +000038 SkScalar fOuterRadius;
39 SkScalar fInnerRadius;
40};
41
42struct EllipseVertex {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000043 SkPoint fPos;
44 SkPoint fOffset;
45 SkPoint fOuterRadii;
46 SkPoint fInnerRadii;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +000047};
48
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +000049struct DIEllipseVertex {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000050 SkPoint fPos;
51 SkPoint fOuterOffset;
52 SkPoint fInnerOffset;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +000053};
54
commit-bot@chromium.org81312832013-03-22 18:34:09 +000055inline bool circle_stays_circle(const SkMatrix& m) {
56 return m.isSimilarity();
57}
58
59}
60
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000061///////////////////////////////////////////////////////////////////////////////
62
63/**
bsalomonce1c8862014-12-15 07:11:22 -080064 * The output of this effect is a modulation of the input color and coverage for a circle. It
65 * operates in a space normalized by the circle radius (outer radius in the case of a stroke)
66 * with origin at the circle center. Two vertex attributes are used:
67 * vec2f : position in device space of the bounding geometry vertices
68 * vec4f : (p.xy, outerRad, innerRad)
69 * p is the position in the normalized space.
70 * outerRad is the outerRadius in device space.
71 * innerRad is the innerRadius in normalized space (ignored if not stroking).
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000072 */
73
joshualitt249af152014-09-15 11:41:13 -070074class CircleEdgeEffect : public GrGeometryProcessor {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000075public:
joshualittb8c241a2015-05-19 08:23:30 -070076 static GrGeometryProcessor* Create(GrColor color, bool stroke, const SkMatrix& localMatrix,
77 bool usesLocalCoords) {
halcanary385fe4d2015-08-26 13:07:48 -070078 return new CircleEdgeEffect(color, stroke, localMatrix, usesLocalCoords);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000079 }
80
joshualitt71c92602015-01-14 08:12:47 -080081 const Attribute* inPosition() const { return fInPosition; }
82 const Attribute* inCircleEdge() const { return fInCircleEdge; }
joshualitt88c23fc2015-05-13 14:18:07 -070083 GrColor color() const { return fColor; }
joshualittb8c241a2015-05-19 08:23:30 -070084 bool colorIgnored() const { return GrColor_ILLEGAL == fColor; }
joshualitte3ababe2015-05-15 07:56:07 -070085 const SkMatrix& localMatrix() const { return fLocalMatrix; }
joshualittb8c241a2015-05-19 08:23:30 -070086 bool usesLocalCoords() const { return fUsesLocalCoords; }
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000087 virtual ~CircleEdgeEffect() {}
88
mtklein36352bf2015-03-25 18:17:31 -070089 const char* name() const override { return "CircleEdge"; }
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000090
91 inline bool isStroked() const { return fStroke; }
92
egdaniele659a582015-11-13 09:55:43 -080093 class GLProcessor : public GrGLSLGeometryProcessor {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000094 public:
joshualitt465283c2015-09-11 08:19:35 -070095 GLProcessor()
joshualitt9b989322014-12-15 14:16:27 -080096 : fColor(GrColor_ILLEGAL) {}
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000097
mtklein36352bf2015-03-25 18:17:31 -070098 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{
joshualitt2dd1ae02014-12-03 06:24:10 -080099 const CircleEdgeEffect& ce = args.fGP.cast<CircleEdgeEffect>();
egdaniel8dcdedc2015-11-11 06:27:20 -0800100 GrGLSLGPBuilder* pb = args.fPB;
egdaniel2d721d32015-11-11 13:06:05 -0800101 GrGLSLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder();
joshualitt2dd1ae02014-12-03 06:24:10 -0800102
joshualittabb52a12015-01-13 15:02:10 -0800103 // emit attributes
104 vsBuilder->emitAttributes(ce);
105
egdaniel8dcdedc2015-11-11 06:27:20 -0800106 GrGLSLVertToFrag v(kVec4f_GrSLType);
joshualitt74077b92014-10-24 11:26:03 -0700107 args.fPB->addVarying("CircleEdge", &v);
joshualitt2dd1ae02014-12-03 06:24:10 -0800108 vsBuilder->codeAppendf("%s = %s;", v.vsOut(), ce.inCircleEdge()->fName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000109
joshualittb8c241a2015-05-19 08:23:30 -0700110 // setup pass through color
111 if (!ce.colorIgnored()) {
112 this->setupUniformColor(pb, args.fOutputColor, &fColorUniform);
113 }
joshualitt9b989322014-12-15 14:16:27 -0800114
joshualittabb52a12015-01-13 15:02:10 -0800115 // Setup position
joshualitte578a952015-05-14 10:09:13 -0700116 this->setupPosition(pb, gpArgs, ce.inPosition()->fName);
joshualittabb52a12015-01-13 15:02:10 -0800117
118 // emit transforms
robertphillips46d36f02015-01-18 08:14:14 -0800119 this->emitTransforms(args.fPB, gpArgs->fPositionVar, ce.inPosition()->fName,
tfarina567ff2f2015-04-27 07:01:44 -0700120 ce.localMatrix(), args.fTransformsIn, args.fTransformsOut);
joshualitt4973d9d2014-11-08 09:24:25 -0800121
egdaniel2d721d32015-11-11 13:06:05 -0800122 GrGLSLFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder();
joshualitt74077b92014-10-24 11:26:03 -0700123 fsBuilder->codeAppendf("float d = length(%s.xy);", v.fsIn());
bsalomonce1c8862014-12-15 07:11:22 -0800124 fsBuilder->codeAppendf("float edgeAlpha = clamp(%s.z * (1.0 - d), 0.0, 1.0);", v.fsIn());
joshualitt2dd1ae02014-12-03 06:24:10 -0800125 if (ce.isStroked()) {
bsalomonce1c8862014-12-15 07:11:22 -0800126 fsBuilder->codeAppendf("float innerAlpha = clamp(%s.z * (d - %s.w), 0.0, 1.0);",
127 v.fsIn(), v.fsIn());
joshualitt74077b92014-10-24 11:26:03 -0700128 fsBuilder->codeAppend("edgeAlpha *= innerAlpha;");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000129 }
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000130
joshualitt2dd1ae02014-12-03 06:24:10 -0800131 fsBuilder->codeAppendf("%s = vec4(edgeAlpha);", args.fOutputCoverage);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000132 }
133
robertphillips46d36f02015-01-18 08:14:14 -0800134 static void GenKey(const GrGeometryProcessor& gp,
jvanverthcfc18862015-04-28 08:48:20 -0700135 const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700136 GrProcessorKeyBuilder* b) {
joshualitte3ababe2015-05-15 07:56:07 -0700137 const CircleEdgeEffect& ce = gp.cast<CircleEdgeEffect>();
138 uint16_t key = ce.isStroked() ? 0x1 : 0x0;
joshualittb8c241a2015-05-19 08:23:30 -0700139 key |= ce.usesLocalCoords() && ce.localMatrix().hasPerspective() ? 0x2 : 0x0;
140 key |= ce.colorIgnored() ? 0x4 : 0x0;
141 b->add32(key);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000142 }
143
egdaniel018fb622015-10-28 07:26:40 -0700144 void setData(const GrGLSLProgramDataManager& pdman,
145 const GrPrimitiveProcessor& gp) override {
joshualittb8c241a2015-05-19 08:23:30 -0700146 const CircleEdgeEffect& ce = gp.cast<CircleEdgeEffect>();
147 if (ce.color() != fColor) {
egdaniel018fb622015-10-28 07:26:40 -0700148 float c[4];
joshualittb8c241a2015-05-19 08:23:30 -0700149 GrColorToRGBAFloat(ce.color(), c);
joshualitt9b989322014-12-15 14:16:27 -0800150 pdman.set4fv(fColorUniform, 1, c);
joshualittb8c241a2015-05-19 08:23:30 -0700151 fColor = ce.color();
joshualitt9b989322014-12-15 14:16:27 -0800152 }
153 }
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000154
joshualitte3ababe2015-05-15 07:56:07 -0700155 void setTransformData(const GrPrimitiveProcessor& primProc,
egdaniel018fb622015-10-28 07:26:40 -0700156 const GrGLSLProgramDataManager& pdman,
joshualitte3ababe2015-05-15 07:56:07 -0700157 int index,
158 const SkTArray<const GrCoordTransform*, true>& transforms) override {
159 this->setTransformDataHelper<CircleEdgeEffect>(primProc, pdman, index, transforms);
160 }
161
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000162 private:
joshualitt9b989322014-12-15 14:16:27 -0800163 GrColor fColor;
164 UniformHandle fColorUniform;
egdaniele659a582015-11-13 09:55:43 -0800165 typedef GrGLSLGeometryProcessor INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000166 };
167
joshualitt465283c2015-09-11 08:19:35 -0700168 void getGLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override {
169 GLProcessor::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800170 }
171
egdaniele659a582015-11-13 09:55:43 -0800172 GrGLSLPrimitiveProcessor* createGLInstance(const GrGLSLCaps&) const override {
joshualitt465283c2015-09-11 08:19:35 -0700173 return new GLProcessor();
joshualitteb2a6762014-12-04 11:35:33 -0800174 }
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000175
176private:
joshualittb8c241a2015-05-19 08:23:30 -0700177 CircleEdgeEffect(GrColor color, bool stroke, const SkMatrix& localMatrix, bool usesLocalCoords)
joshualitte3ababe2015-05-15 07:56:07 -0700178 : fColor(color)
joshualittb8c241a2015-05-19 08:23:30 -0700179 , fLocalMatrix(localMatrix)
180 , fUsesLocalCoords(usesLocalCoords) {
joshualitteb2a6762014-12-04 11:35:33 -0800181 this->initClassID<CircleEdgeEffect>();
senorblancof2539d52015-05-20 14:03:42 -0700182 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType,
183 kHigh_GrSLPrecision));
joshualitt71c92602015-01-14 08:12:47 -0800184 fInCircleEdge = &this->addVertexAttrib(Attribute("inCircleEdge",
joshualitt2dd1ae02014-12-03 06:24:10 -0800185 kVec4f_GrVertexAttribType));
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000186 fStroke = stroke;
187 }
188
joshualitt88c23fc2015-05-13 14:18:07 -0700189 GrColor fColor;
joshualitte3ababe2015-05-15 07:56:07 -0700190 SkMatrix fLocalMatrix;
joshualitt71c92602015-01-14 08:12:47 -0800191 const Attribute* fInPosition;
192 const Attribute* fInCircleEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000193 bool fStroke;
joshualittb8c241a2015-05-19 08:23:30 -0700194 bool fUsesLocalCoords;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000195
joshualittb0a8a372014-09-23 09:50:21 -0700196 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000197
joshualitt249af152014-09-15 11:41:13 -0700198 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000199};
200
joshualittb0a8a372014-09-23 09:50:21 -0700201GR_DEFINE_GEOMETRY_PROCESSOR_TEST(CircleEdgeEffect);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000202
bsalomonc21b09e2015-08-28 18:46:56 -0700203const GrGeometryProcessor* CircleEdgeEffect::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700204 return CircleEdgeEffect::Create(GrRandomColor(d->fRandom),
205 d->fRandom->nextBool(),
206 GrTest::TestMatrix(d->fRandom),
207 d->fRandom->nextBool());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000208}
209
210///////////////////////////////////////////////////////////////////////////////
211
212/**
213 * 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 +0000214 * ellipse, specified as a 2D offset from center, and the reciprocals of the outer and inner radii,
215 * in both x and y directions.
216 *
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000217 * 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 +0000218 */
219
joshualitt249af152014-09-15 11:41:13 -0700220class EllipseEdgeEffect : public GrGeometryProcessor {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000221public:
joshualittb8c241a2015-05-19 08:23:30 -0700222 static GrGeometryProcessor* Create(GrColor color, bool stroke, const SkMatrix& localMatrix,
223 bool usesLocalCoords) {
halcanary385fe4d2015-08-26 13:07:48 -0700224 return new EllipseEdgeEffect(color, stroke, localMatrix, usesLocalCoords);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000225 }
226
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000227 virtual ~EllipseEdgeEffect() {}
228
mtklein36352bf2015-03-25 18:17:31 -0700229 const char* name() const override { return "EllipseEdge"; }
joshualitt2dd1ae02014-12-03 06:24:10 -0800230
joshualitt71c92602015-01-14 08:12:47 -0800231 const Attribute* inPosition() const { return fInPosition; }
232 const Attribute* inEllipseOffset() const { return fInEllipseOffset; }
233 const Attribute* inEllipseRadii() const { return fInEllipseRadii; }
joshualitt88c23fc2015-05-13 14:18:07 -0700234 GrColor color() const { return fColor; }
joshualittb8c241a2015-05-19 08:23:30 -0700235 bool colorIgnored() const { return GrColor_ILLEGAL == fColor; }
joshualitte3ababe2015-05-15 07:56:07 -0700236 const SkMatrix& localMatrix() const { return fLocalMatrix; }
joshualittb8c241a2015-05-19 08:23:30 -0700237 bool usesLocalCoords() const { return fUsesLocalCoords; }
joshualitt249af152014-09-15 11:41:13 -0700238
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000239 inline bool isStroked() const { return fStroke; }
240
egdaniele659a582015-11-13 09:55:43 -0800241 class GLProcessor : public GrGLSLGeometryProcessor {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000242 public:
joshualitt465283c2015-09-11 08:19:35 -0700243 GLProcessor()
joshualitt9b989322014-12-15 14:16:27 -0800244 : fColor(GrColor_ILLEGAL) {}
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000245
mtklein36352bf2015-03-25 18:17:31 -0700246 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{
joshualitt2dd1ae02014-12-03 06:24:10 -0800247 const EllipseEdgeEffect& ee = args.fGP.cast<EllipseEdgeEffect>();
egdaniel8dcdedc2015-11-11 06:27:20 -0800248 GrGLSLGPBuilder* pb = args.fPB;
egdaniel2d721d32015-11-11 13:06:05 -0800249 GrGLSLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder();
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000250
joshualittabb52a12015-01-13 15:02:10 -0800251 // emit attributes
252 vsBuilder->emitAttributes(ee);
253
egdaniel8dcdedc2015-11-11 06:27:20 -0800254 GrGLSLVertToFrag ellipseOffsets(kVec2f_GrSLType);
joshualitt74077b92014-10-24 11:26:03 -0700255 args.fPB->addVarying("EllipseOffsets", &ellipseOffsets);
joshualitt74077b92014-10-24 11:26:03 -0700256 vsBuilder->codeAppendf("%s = %s;", ellipseOffsets.vsOut(),
joshualitt2dd1ae02014-12-03 06:24:10 -0800257 ee.inEllipseOffset()->fName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000258
egdaniel8dcdedc2015-11-11 06:27:20 -0800259 GrGLSLVertToFrag ellipseRadii(kVec4f_GrSLType);
joshualitt74077b92014-10-24 11:26:03 -0700260 args.fPB->addVarying("EllipseRadii", &ellipseRadii);
261 vsBuilder->codeAppendf("%s = %s;", ellipseRadii.vsOut(),
joshualitt2dd1ae02014-12-03 06:24:10 -0800262 ee.inEllipseRadii()->fName);
263
joshualittb8c241a2015-05-19 08:23:30 -0700264 // setup pass through color
265 if (!ee.colorIgnored()) {
266 this->setupUniformColor(pb, args.fOutputColor, &fColorUniform);
267 }
joshualitt9b989322014-12-15 14:16:27 -0800268
joshualittabb52a12015-01-13 15:02:10 -0800269 // Setup position
joshualitte578a952015-05-14 10:09:13 -0700270 this->setupPosition(pb, gpArgs, ee.inPosition()->fName);
joshualittabb52a12015-01-13 15:02:10 -0800271
272 // emit transforms
robertphillips46d36f02015-01-18 08:14:14 -0800273 this->emitTransforms(args.fPB, gpArgs->fPositionVar, ee.inPosition()->fName,
joshualittabb52a12015-01-13 15:02:10 -0800274 ee.localMatrix(), args.fTransformsIn, args.fTransformsOut);
joshualitt4973d9d2014-11-08 09:24:25 -0800275
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +0000276 // for outer curve
egdaniel2d721d32015-11-11 13:06:05 -0800277 GrGLSLFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder();
joshualitt74077b92014-10-24 11:26:03 -0700278 fsBuilder->codeAppendf("vec2 scaledOffset = %s*%s.xy;", ellipseOffsets.fsIn(),
279 ellipseRadii.fsIn());
280 fsBuilder->codeAppend("float test = dot(scaledOffset, scaledOffset) - 1.0;");
281 fsBuilder->codeAppendf("vec2 grad = 2.0*scaledOffset*%s.xy;", ellipseRadii.fsIn());
282 fsBuilder->codeAppend("float grad_dot = dot(grad, grad);");
283
commit-bot@chromium.org1b035d82014-04-09 17:11:09 +0000284 // avoid calling inversesqrt on zero.
joshualitt74077b92014-10-24 11:26:03 -0700285 fsBuilder->codeAppend("grad_dot = max(grad_dot, 1.0e-4);");
286 fsBuilder->codeAppend("float invlen = inversesqrt(grad_dot);");
287 fsBuilder->codeAppend("float edgeAlpha = clamp(0.5-test*invlen, 0.0, 1.0);");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000288
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000289 // for inner curve
joshualitt2dd1ae02014-12-03 06:24:10 -0800290 if (ee.isStroked()) {
joshualitt74077b92014-10-24 11:26:03 -0700291 fsBuilder->codeAppendf("scaledOffset = %s*%s.zw;",
292 ellipseOffsets.fsIn(), ellipseRadii.fsIn());
293 fsBuilder->codeAppend("test = dot(scaledOffset, scaledOffset) - 1.0;");
294 fsBuilder->codeAppendf("grad = 2.0*scaledOffset*%s.zw;",
295 ellipseRadii.fsIn());
296 fsBuilder->codeAppend("invlen = inversesqrt(dot(grad, grad));");
297 fsBuilder->codeAppend("edgeAlpha *= clamp(0.5+test*invlen, 0.0, 1.0);");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000298 }
299
joshualitt2dd1ae02014-12-03 06:24:10 -0800300 fsBuilder->codeAppendf("%s = vec4(edgeAlpha);", args.fOutputCoverage);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000301 }
302
robertphillips46d36f02015-01-18 08:14:14 -0800303 static void GenKey(const GrGeometryProcessor& gp,
jvanverthcfc18862015-04-28 08:48:20 -0700304 const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700305 GrProcessorKeyBuilder* b) {
joshualitte3ababe2015-05-15 07:56:07 -0700306 const EllipseEdgeEffect& ee = gp.cast<EllipseEdgeEffect>();
307 uint16_t key = ee.isStroked() ? 0x1 : 0x0;
joshualittb8c241a2015-05-19 08:23:30 -0700308 key |= ee.usesLocalCoords() && ee.localMatrix().hasPerspective() ? 0x2 : 0x0;
309 key |= ee.colorIgnored() ? 0x4 : 0x0;
310 b->add32(key);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000311 }
312
egdaniel018fb622015-10-28 07:26:40 -0700313 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& gp) override {
joshualittb8c241a2015-05-19 08:23:30 -0700314 const EllipseEdgeEffect& ee = gp.cast<EllipseEdgeEffect>();
315 if (ee.color() != fColor) {
egdaniel018fb622015-10-28 07:26:40 -0700316 float c[4];
joshualittb8c241a2015-05-19 08:23:30 -0700317 GrColorToRGBAFloat(ee.color(), c);
joshualitt9b989322014-12-15 14:16:27 -0800318 pdman.set4fv(fColorUniform, 1, c);
joshualittb8c241a2015-05-19 08:23:30 -0700319 fColor = ee.color();
joshualitt9b989322014-12-15 14:16:27 -0800320 }
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000321 }
322
joshualitte3ababe2015-05-15 07:56:07 -0700323 void setTransformData(const GrPrimitiveProcessor& primProc,
egdaniel018fb622015-10-28 07:26:40 -0700324 const GrGLSLProgramDataManager& pdman,
joshualitte3ababe2015-05-15 07:56:07 -0700325 int index,
326 const SkTArray<const GrCoordTransform*, true>& transforms) override {
327 this->setTransformDataHelper<EllipseEdgeEffect>(primProc, pdman, index, transforms);
328 }
329
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000330 private:
joshualitt9b989322014-12-15 14:16:27 -0800331 GrColor fColor;
332 UniformHandle fColorUniform;
333
egdaniele659a582015-11-13 09:55:43 -0800334 typedef GrGLSLGeometryProcessor INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000335 };
336
joshualitt465283c2015-09-11 08:19:35 -0700337 void getGLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override {
338 GLProcessor::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800339 }
340
egdaniele659a582015-11-13 09:55:43 -0800341 GrGLSLPrimitiveProcessor* createGLInstance(const GrGLSLCaps&) const override {
joshualitt465283c2015-09-11 08:19:35 -0700342 return new GLProcessor();
joshualitteb2a6762014-12-04 11:35:33 -0800343 }
344
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000345private:
joshualittb8c241a2015-05-19 08:23:30 -0700346 EllipseEdgeEffect(GrColor color, bool stroke, const SkMatrix& localMatrix,
347 bool usesLocalCoords)
joshualitte3ababe2015-05-15 07:56:07 -0700348 : fColor(color)
joshualittb8c241a2015-05-19 08:23:30 -0700349 , fLocalMatrix(localMatrix)
350 , fUsesLocalCoords(usesLocalCoords) {
joshualitteb2a6762014-12-04 11:35:33 -0800351 this->initClassID<EllipseEdgeEffect>();
joshualitt71c92602015-01-14 08:12:47 -0800352 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType));
353 fInEllipseOffset = &this->addVertexAttrib(Attribute("inEllipseOffset",
joshualitt465283c2015-09-11 08:19:35 -0700354 kVec2f_GrVertexAttribType));
joshualitt71c92602015-01-14 08:12:47 -0800355 fInEllipseRadii = &this->addVertexAttrib(Attribute("inEllipseRadii",
joshualitt465283c2015-09-11 08:19:35 -0700356 kVec4f_GrVertexAttribType));
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000357 fStroke = stroke;
358 }
359
joshualitt71c92602015-01-14 08:12:47 -0800360 const Attribute* fInPosition;
361 const Attribute* fInEllipseOffset;
362 const Attribute* fInEllipseRadii;
joshualitt88c23fc2015-05-13 14:18:07 -0700363 GrColor fColor;
joshualitte3ababe2015-05-15 07:56:07 -0700364 SkMatrix fLocalMatrix;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000365 bool fStroke;
joshualittb8c241a2015-05-19 08:23:30 -0700366 bool fUsesLocalCoords;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000367
joshualittb0a8a372014-09-23 09:50:21 -0700368 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000369
joshualitt249af152014-09-15 11:41:13 -0700370 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000371};
372
joshualittb0a8a372014-09-23 09:50:21 -0700373GR_DEFINE_GEOMETRY_PROCESSOR_TEST(EllipseEdgeEffect);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000374
bsalomonc21b09e2015-08-28 18:46:56 -0700375const GrGeometryProcessor* EllipseEdgeEffect::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700376 return EllipseEdgeEffect::Create(GrRandomColor(d->fRandom),
377 d->fRandom->nextBool(),
378 GrTest::TestMatrix(d->fRandom),
379 d->fRandom->nextBool());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000380}
381
382///////////////////////////////////////////////////////////////////////////////
383
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000384/**
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000385 * 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 +0000386 * specified as a 2D offset from center for both the outer and inner paths (if stroked). The
387 * implict equation used is for a unit circle (x^2 + y^2 - 1 = 0) and the edge corrected by
388 * using differentials.
389 *
390 * The result is device-independent and can be used with any affine matrix.
391 */
392
joshualitt249af152014-09-15 11:41:13 -0700393class DIEllipseEdgeEffect : public GrGeometryProcessor {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000394public:
395 enum Mode { kStroke = 0, kHairline, kFill };
396
joshualittb8c241a2015-05-19 08:23:30 -0700397 static GrGeometryProcessor* Create(GrColor color, const SkMatrix& viewMatrix, Mode mode,
398 bool usesLocalCoords) {
halcanary385fe4d2015-08-26 13:07:48 -0700399 return new DIEllipseEdgeEffect(color, viewMatrix, mode, usesLocalCoords);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000400 }
401
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000402 virtual ~DIEllipseEdgeEffect() {}
403
mtklein36352bf2015-03-25 18:17:31 -0700404 const char* name() const override { return "DIEllipseEdge"; }
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000405
joshualitt71c92602015-01-14 08:12:47 -0800406 const Attribute* inPosition() const { return fInPosition; }
407 const Attribute* inEllipseOffsets0() const { return fInEllipseOffsets0; }
408 const Attribute* inEllipseOffsets1() const { return fInEllipseOffsets1; }
joshualitt88c23fc2015-05-13 14:18:07 -0700409 GrColor color() const { return fColor; }
joshualittb8c241a2015-05-19 08:23:30 -0700410 bool colorIgnored() const { return GrColor_ILLEGAL == fColor; }
joshualitte578a952015-05-14 10:09:13 -0700411 const SkMatrix& viewMatrix() const { return fViewMatrix; }
joshualittb8c241a2015-05-19 08:23:30 -0700412 bool usesLocalCoords() const { return fUsesLocalCoords; }
joshualitt249af152014-09-15 11:41:13 -0700413
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000414 inline Mode getMode() const { return fMode; }
415
egdaniele659a582015-11-13 09:55:43 -0800416 class GLProcessor : public GrGLSLGeometryProcessor {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000417 public:
joshualitt465283c2015-09-11 08:19:35 -0700418 GLProcessor()
joshualitt5559ca22015-05-21 15:50:36 -0700419 : fViewMatrix(SkMatrix::InvalidMatrix()), fColor(GrColor_ILLEGAL) {}
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000420
joshualitt465283c2015-09-11 08:19:35 -0700421 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
joshualitt2dd1ae02014-12-03 06:24:10 -0800422 const DIEllipseEdgeEffect& ee = args.fGP.cast<DIEllipseEdgeEffect>();
egdaniel8dcdedc2015-11-11 06:27:20 -0800423 GrGLSLGPBuilder* pb = args.fPB;
egdaniel2d721d32015-11-11 13:06:05 -0800424 GrGLSLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder();
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000425
joshualittabb52a12015-01-13 15:02:10 -0800426 // emit attributes
427 vsBuilder->emitAttributes(ee);
428
egdaniel8dcdedc2015-11-11 06:27:20 -0800429 GrGLSLVertToFrag offsets0(kVec2f_GrSLType);
joshualitt74077b92014-10-24 11:26:03 -0700430 args.fPB->addVarying("EllipseOffsets0", &offsets0);
joshualitt74077b92014-10-24 11:26:03 -0700431 vsBuilder->codeAppendf("%s = %s;", offsets0.vsOut(),
joshualitt2dd1ae02014-12-03 06:24:10 -0800432 ee.inEllipseOffsets0()->fName);
joshualitt74077b92014-10-24 11:26:03 -0700433
egdaniel8dcdedc2015-11-11 06:27:20 -0800434 GrGLSLVertToFrag offsets1(kVec2f_GrSLType);
joshualitt74077b92014-10-24 11:26:03 -0700435 args.fPB->addVarying("EllipseOffsets1", &offsets1);
436 vsBuilder->codeAppendf("%s = %s;", offsets1.vsOut(),
joshualitt2dd1ae02014-12-03 06:24:10 -0800437 ee.inEllipseOffsets1()->fName);
438
joshualittb8c241a2015-05-19 08:23:30 -0700439 // setup pass through color
440 if (!ee.colorIgnored()) {
441 this->setupUniformColor(pb, args.fOutputColor, &fColorUniform);
442 }
joshualitt9b989322014-12-15 14:16:27 -0800443
joshualittabb52a12015-01-13 15:02:10 -0800444 // Setup position
joshualitt5559ca22015-05-21 15:50:36 -0700445 this->setupPosition(pb, gpArgs, ee.inPosition()->fName, ee.viewMatrix(),
446 &fViewMatrixUniform);
joshualittabb52a12015-01-13 15:02:10 -0800447
448 // emit transforms
robertphillips46d36f02015-01-18 08:14:14 -0800449 this->emitTransforms(args.fPB, gpArgs->fPositionVar, ee.inPosition()->fName,
joshualitte3ababe2015-05-15 07:56:07 -0700450 args.fTransformsIn, args.fTransformsOut);
joshualitt4973d9d2014-11-08 09:24:25 -0800451
egdaniel2d721d32015-11-11 13:06:05 -0800452 GrGLSLFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -0700453 SkAssertResult(fsBuilder->enableFeature(
egdaniel2d721d32015-11-11 13:06:05 -0800454 GrGLSLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature));
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000455 // for outer curve
joshualitt74077b92014-10-24 11:26:03 -0700456 fsBuilder->codeAppendf("vec2 scaledOffset = %s.xy;", offsets0.fsIn());
457 fsBuilder->codeAppend("float test = dot(scaledOffset, scaledOffset) - 1.0;");
458 fsBuilder->codeAppendf("vec2 duvdx = dFdx(%s);", offsets0.fsIn());
459 fsBuilder->codeAppendf("vec2 duvdy = dFdy(%s);", offsets0.fsIn());
460 fsBuilder->codeAppendf("vec2 grad = vec2(2.0*%s.x*duvdx.x + 2.0*%s.y*duvdx.y,"
461 " 2.0*%s.x*duvdy.x + 2.0*%s.y*duvdy.y);",
462 offsets0.fsIn(), offsets0.fsIn(), offsets0.fsIn(), offsets0.fsIn());
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000463
joshualitt74077b92014-10-24 11:26:03 -0700464 fsBuilder->codeAppend("float grad_dot = dot(grad, grad);");
commit-bot@chromium.org1b035d82014-04-09 17:11:09 +0000465 // avoid calling inversesqrt on zero.
joshualitt74077b92014-10-24 11:26:03 -0700466 fsBuilder->codeAppend("grad_dot = max(grad_dot, 1.0e-4);");
467 fsBuilder->codeAppend("float invlen = inversesqrt(grad_dot);");
joshualitt2dd1ae02014-12-03 06:24:10 -0800468 if (kHairline == ee.getMode()) {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000469 // can probably do this with one step
joshualitt74077b92014-10-24 11:26:03 -0700470 fsBuilder->codeAppend("float edgeAlpha = clamp(1.0-test*invlen, 0.0, 1.0);");
471 fsBuilder->codeAppend("edgeAlpha *= clamp(1.0+test*invlen, 0.0, 1.0);");
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000472 } else {
joshualitt74077b92014-10-24 11:26:03 -0700473 fsBuilder->codeAppend("float edgeAlpha = clamp(0.5-test*invlen, 0.0, 1.0);");
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000474 }
475
476 // for inner curve
joshualitt2dd1ae02014-12-03 06:24:10 -0800477 if (kStroke == ee.getMode()) {
joshualitt74077b92014-10-24 11:26:03 -0700478 fsBuilder->codeAppendf("scaledOffset = %s.xy;", offsets1.fsIn());
479 fsBuilder->codeAppend("test = dot(scaledOffset, scaledOffset) - 1.0;");
480 fsBuilder->codeAppendf("duvdx = dFdx(%s);", offsets1.fsIn());
481 fsBuilder->codeAppendf("duvdy = dFdy(%s);", offsets1.fsIn());
482 fsBuilder->codeAppendf("grad = vec2(2.0*%s.x*duvdx.x + 2.0*%s.y*duvdx.y,"
483 " 2.0*%s.x*duvdy.x + 2.0*%s.y*duvdy.y);",
484 offsets1.fsIn(), offsets1.fsIn(), offsets1.fsIn(),
485 offsets1.fsIn());
486 fsBuilder->codeAppend("invlen = inversesqrt(dot(grad, grad));");
487 fsBuilder->codeAppend("edgeAlpha *= clamp(0.5+test*invlen, 0.0, 1.0);");
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000488 }
489
joshualitt2dd1ae02014-12-03 06:24:10 -0800490 fsBuilder->codeAppendf("%s = vec4(edgeAlpha);", args.fOutputCoverage);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000491 }
492
robertphillips46d36f02015-01-18 08:14:14 -0800493 static void GenKey(const GrGeometryProcessor& gp,
jvanverthcfc18862015-04-28 08:48:20 -0700494 const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700495 GrProcessorKeyBuilder* b) {
robertphillips46d36f02015-01-18 08:14:14 -0800496 const DIEllipseEdgeEffect& ellipseEffect = gp.cast<DIEllipseEdgeEffect>();
joshualitt8fc6c2d2014-12-22 15:27:05 -0800497 uint16_t key = ellipseEffect.getMode();
joshualittb8c241a2015-05-19 08:23:30 -0700498 key |= ellipseEffect.colorIgnored() << 9;
499 key |= ComputePosKey(ellipseEffect.viewMatrix()) << 10;
500 b->add32(key);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000501 }
502
egdaniel018fb622015-10-28 07:26:40 -0700503 void setData(const GrGLSLProgramDataManager& pdman,
504 const GrPrimitiveProcessor& gp) override {
joshualitte578a952015-05-14 10:09:13 -0700505 const DIEllipseEdgeEffect& dee = gp.cast<DIEllipseEdgeEffect>();
joshualitt5559ca22015-05-21 15:50:36 -0700506
507 if (!dee.viewMatrix().isIdentity() && !fViewMatrix.cheapEqualTo(dee.viewMatrix())) {
508 fViewMatrix = dee.viewMatrix();
egdaniel018fb622015-10-28 07:26:40 -0700509 float viewMatrix[3 * 3];
egdaniel64c47282015-11-13 06:54:19 -0800510 GrGLSLGetMatrix<3>(viewMatrix, fViewMatrix);
joshualitt5559ca22015-05-21 15:50:36 -0700511 pdman.setMatrix3f(fViewMatrixUniform, viewMatrix);
512 }
joshualittee2af952014-12-30 09:04:15 -0800513
joshualittb8c241a2015-05-19 08:23:30 -0700514 if (dee.color() != fColor) {
egdaniel018fb622015-10-28 07:26:40 -0700515 float c[4];
joshualittb8c241a2015-05-19 08:23:30 -0700516 GrColorToRGBAFloat(dee.color(), c);
joshualitt9b989322014-12-15 14:16:27 -0800517 pdman.set4fv(fColorUniform, 1, c);
joshualittb8c241a2015-05-19 08:23:30 -0700518 fColor = dee.color();
joshualitt9b989322014-12-15 14:16:27 -0800519 }
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000520 }
521
522 private:
joshualitt5559ca22015-05-21 15:50:36 -0700523 SkMatrix fViewMatrix;
joshualitt9b989322014-12-15 14:16:27 -0800524 GrColor fColor;
525 UniformHandle fColorUniform;
joshualitt5559ca22015-05-21 15:50:36 -0700526 UniformHandle fViewMatrixUniform;
joshualitt9b989322014-12-15 14:16:27 -0800527
egdaniele659a582015-11-13 09:55:43 -0800528 typedef GrGLSLGeometryProcessor INHERITED;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000529 };
530
joshualitt465283c2015-09-11 08:19:35 -0700531 void getGLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override {
532 GLProcessor::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800533 }
534
egdaniele659a582015-11-13 09:55:43 -0800535 GrGLSLPrimitiveProcessor* createGLInstance(const GrGLSLCaps&) const override {
joshualitt465283c2015-09-11 08:19:35 -0700536 return new GLProcessor();
joshualitteb2a6762014-12-04 11:35:33 -0800537 }
538
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000539private:
joshualittb8c241a2015-05-19 08:23:30 -0700540 DIEllipseEdgeEffect(GrColor color, const SkMatrix& viewMatrix, Mode mode,
541 bool usesLocalCoords)
joshualitte578a952015-05-14 10:09:13 -0700542 : fColor(color)
joshualittb8c241a2015-05-19 08:23:30 -0700543 , fViewMatrix(viewMatrix)
544 , fUsesLocalCoords(usesLocalCoords) {
joshualitteb2a6762014-12-04 11:35:33 -0800545 this->initClassID<DIEllipseEdgeEffect>();
senorblancof2539d52015-05-20 14:03:42 -0700546 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType,
547 kHigh_GrSLPrecision));
joshualitt71c92602015-01-14 08:12:47 -0800548 fInEllipseOffsets0 = &this->addVertexAttrib(Attribute("inEllipseOffsets0",
joshualittb8c241a2015-05-19 08:23:30 -0700549 kVec2f_GrVertexAttribType));
joshualitt71c92602015-01-14 08:12:47 -0800550 fInEllipseOffsets1 = &this->addVertexAttrib(Attribute("inEllipseOffsets1",
joshualittb8c241a2015-05-19 08:23:30 -0700551 kVec2f_GrVertexAttribType));
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000552 fMode = mode;
553 }
554
joshualitt71c92602015-01-14 08:12:47 -0800555 const Attribute* fInPosition;
556 const Attribute* fInEllipseOffsets0;
557 const Attribute* fInEllipseOffsets1;
joshualitt88c23fc2015-05-13 14:18:07 -0700558 GrColor fColor;
joshualitte578a952015-05-14 10:09:13 -0700559 SkMatrix fViewMatrix;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000560 Mode fMode;
joshualittb8c241a2015-05-19 08:23:30 -0700561 bool fUsesLocalCoords;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000562
joshualittb0a8a372014-09-23 09:50:21 -0700563 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000564
joshualitt249af152014-09-15 11:41:13 -0700565 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000566};
567
joshualittb0a8a372014-09-23 09:50:21 -0700568GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DIEllipseEdgeEffect);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000569
bsalomonc21b09e2015-08-28 18:46:56 -0700570const GrGeometryProcessor* DIEllipseEdgeEffect::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700571 return DIEllipseEdgeEffect::Create(GrRandomColor(d->fRandom),
572 GrTest::TestMatrix(d->fRandom),
573 (Mode)(d->fRandom->nextRangeU(0,2)),
574 d->fRandom->nextBool());
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000575}
576
577///////////////////////////////////////////////////////////////////////////////
578
robertphillipsea461502015-05-26 11:38:03 -0700579bool GrOvalRenderer::DrawOval(GrDrawTarget* target,
joshualittae3d63a2015-07-13 08:44:06 -0700580 const GrPipelineBuilder& pipelineBuilder,
joshualitt2e3b3e32014-12-09 13:31:14 -0800581 GrColor color,
joshualitt8059eb92014-12-29 15:10:07 -0800582 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800583 bool useAA,
584 const SkRect& oval,
joshualittae3d63a2015-07-13 08:44:06 -0700585 const SkStrokeRec& stroke) {
586 bool useCoverageAA = useAA && !pipelineBuilder.getRenderTarget()->isUnifiedMultisampled();
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000587
588 if (!useCoverageAA) {
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000589 return false;
590 }
591
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000592 // we can draw circles
joshualitt8059eb92014-12-29 15:10:07 -0800593 if (SkScalarNearlyEqual(oval.width(), oval.height()) && circle_stays_circle(viewMatrix)) {
robertphillipsea461502015-05-26 11:38:03 -0700594 DrawCircle(target, pipelineBuilder, color, viewMatrix, useCoverageAA, oval, stroke);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000595 // if we have shader derivative support, render as device-independent
jvanverthe9c0fc62015-04-29 11:18:05 -0700596 } else if (target->caps()->shaderCaps()->shaderDerivativeSupport()) {
robertphillipsea461502015-05-26 11:38:03 -0700597 return DrawDIEllipse(target, pipelineBuilder, color, viewMatrix, useCoverageAA, oval,
598 stroke);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000599 // otherwise axis-aligned ellipses only
joshualitt8059eb92014-12-29 15:10:07 -0800600 } else if (viewMatrix.rectStaysRect()) {
robertphillipsea461502015-05-26 11:38:03 -0700601 return DrawEllipse(target, pipelineBuilder, color, viewMatrix, useCoverageAA, oval,
602 stroke);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000603 } else {
604 return false;
605 }
606
607 return true;
608}
609
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000610///////////////////////////////////////////////////////////////////////////////
611
bsalomonabd30f52015-08-13 13:34:48 -0700612class CircleBatch : public GrVertexBatch {
joshualitt76e7fb62015-02-11 08:52:27 -0800613public:
reed1b55a962015-09-17 20:16:13 -0700614 DEFINE_BATCH_CLASS_ID
615
joshualitt76e7fb62015-02-11 08:52:27 -0800616 struct Geometry {
joshualitt76e7fb62015-02-11 08:52:27 -0800617 SkMatrix fViewMatrix;
reed1b55a962015-09-17 20:16:13 -0700618 SkRect fDevBounds;
joshualitt76e7fb62015-02-11 08:52:27 -0800619 SkScalar fInnerRadius;
620 SkScalar fOuterRadius;
reed1b55a962015-09-17 20:16:13 -0700621 GrColor fColor;
joshualitt76e7fb62015-02-11 08:52:27 -0800622 bool fStroke;
joshualitt76e7fb62015-02-11 08:52:27 -0800623 };
624
halcanary385fe4d2015-08-26 13:07:48 -0700625 static GrDrawBatch* Create(const Geometry& geometry) { return new CircleBatch(geometry); }
joshualitt76e7fb62015-02-11 08:52:27 -0800626
mtklein36352bf2015-03-25 18:17:31 -0700627 const char* name() const override { return "CircleBatch"; }
joshualitt76e7fb62015-02-11 08:52:27 -0800628
mtklein36352bf2015-03-25 18:17:31 -0700629 void getInvariantOutputColor(GrInitInvariantOutput* out) const override {
joshualitt76e7fb62015-02-11 08:52:27 -0800630 // When this is called on a batch, there is only one geometry bundle
631 out->setKnownFourComponents(fGeoData[0].fColor);
632 }
633
mtklein36352bf2015-03-25 18:17:31 -0700634 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override {
joshualitt76e7fb62015-02-11 08:52:27 -0800635 out->setUnknownSingleComponent();
636 }
637
bsalomone46f9fe2015-08-18 06:05:14 -0700638private:
bsalomon91d844d2015-08-10 10:47:29 -0700639 void initBatchTracker(const GrPipelineOptimizations& opt) override {
joshualitt76e7fb62015-02-11 08:52:27 -0800640 // Handle any color overrides
bsalomon91d844d2015-08-10 10:47:29 -0700641 if (!opt.readsColor()) {
joshualitt76e7fb62015-02-11 08:52:27 -0800642 fGeoData[0].fColor = GrColor_ILLEGAL;
joshualitt76e7fb62015-02-11 08:52:27 -0800643 }
bsalomon91d844d2015-08-10 10:47:29 -0700644 opt.getOverrideColorIfSet(&fGeoData[0].fColor);
joshualitt76e7fb62015-02-11 08:52:27 -0800645
646 // setup batch properties
bsalomon91d844d2015-08-10 10:47:29 -0700647 fBatch.fColorIgnored = !opt.readsColor();
joshualitt76e7fb62015-02-11 08:52:27 -0800648 fBatch.fColor = fGeoData[0].fColor;
649 fBatch.fStroke = fGeoData[0].fStroke;
bsalomon91d844d2015-08-10 10:47:29 -0700650 fBatch.fUsesLocalCoords = opt.readsLocalCoords();
651 fBatch.fCoverageIgnored = !opt.readsCoverage();
joshualitt76e7fb62015-02-11 08:52:27 -0800652 }
653
bsalomon75398562015-08-17 12:55:38 -0700654 void onPrepareDraws(Target* target) override {
joshualitt76e7fb62015-02-11 08:52:27 -0800655 SkMatrix invert;
656 if (!this->viewMatrix().invert(&invert)) {
657 return;
658 }
659
660 // Setup geometry processor
661 SkAutoTUnref<GrGeometryProcessor> gp(CircleEdgeEffect::Create(this->color(),
662 this->stroke(),
joshualittb8c241a2015-05-19 08:23:30 -0700663 invert,
664 this->usesLocalCoords()));
joshualitt76e7fb62015-02-11 08:52:27 -0800665
bsalomon75398562015-08-17 12:55:38 -0700666 target->initDraw(gp, this->pipeline());
joshualitt76e7fb62015-02-11 08:52:27 -0800667
joshualitt76e7fb62015-02-11 08:52:27 -0800668 int instanceCount = fGeoData.count();
joshualitt76e7fb62015-02-11 08:52:27 -0800669 size_t vertexStride = gp->getVertexStride();
670 SkASSERT(vertexStride == sizeof(CircleVertex));
bsalomonb5238a72015-05-05 07:49:49 -0700671 QuadHelper helper;
bsalomon75398562015-08-17 12:55:38 -0700672 CircleVertex* verts = reinterpret_cast<CircleVertex*>(helper.init(target, vertexStride,
bsalomonb5238a72015-05-05 07:49:49 -0700673 instanceCount));
674 if (!verts) {
joshualitt4b31de82015-03-05 14:33:41 -0800675 return;
676 }
677
joshualitt76e7fb62015-02-11 08:52:27 -0800678 for (int i = 0; i < instanceCount; i++) {
bsalomonb5238a72015-05-05 07:49:49 -0700679 Geometry& geom = fGeoData[i];
joshualitt76e7fb62015-02-11 08:52:27 -0800680
bsalomonb5238a72015-05-05 07:49:49 -0700681 SkScalar innerRadius = geom.fInnerRadius;
682 SkScalar outerRadius = geom.fOuterRadius;
joshualitt76e7fb62015-02-11 08:52:27 -0800683
bsalomonb5238a72015-05-05 07:49:49 -0700684 const SkRect& bounds = geom.fDevBounds;
joshualitt76e7fb62015-02-11 08:52:27 -0800685
686 // The inner radius in the vertex data must be specified in normalized space.
687 innerRadius = innerRadius / outerRadius;
688 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
689 verts[0].fOffset = SkPoint::Make(-1, -1);
690 verts[0].fOuterRadius = outerRadius;
691 verts[0].fInnerRadius = innerRadius;
692
693 verts[1].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
694 verts[1].fOffset = SkPoint::Make(-1, 1);
695 verts[1].fOuterRadius = outerRadius;
696 verts[1].fInnerRadius = innerRadius;
697
698 verts[2].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
699 verts[2].fOffset = SkPoint::Make(1, 1);
700 verts[2].fOuterRadius = outerRadius;
701 verts[2].fInnerRadius = innerRadius;
702
703 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
704 verts[3].fOffset = SkPoint::Make(1, -1);
705 verts[3].fOuterRadius = outerRadius;
706 verts[3].fInnerRadius = innerRadius;
707
bsalomonb5238a72015-05-05 07:49:49 -0700708 verts += kVerticesPerQuad;
joshualitt76e7fb62015-02-11 08:52:27 -0800709 }
bsalomon75398562015-08-17 12:55:38 -0700710 helper.recordDraw(target);
joshualitt76e7fb62015-02-11 08:52:27 -0800711 }
712
713 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
714
reed1b55a962015-09-17 20:16:13 -0700715 CircleBatch(const Geometry& geometry) : INHERITED(ClassID()) {
joshualitt76e7fb62015-02-11 08:52:27 -0800716 fGeoData.push_back(geometry);
joshualitt99c7c072015-05-01 13:43:30 -0700717
718 this->setBounds(geometry.fDevBounds);
joshualitt76e7fb62015-02-11 08:52:27 -0800719 }
720
bsalomoncb02b382015-08-12 11:14:50 -0700721 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override {
bsalomonabd30f52015-08-13 13:34:48 -0700722 CircleBatch* that = t->cast<CircleBatch>();
723 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
724 that->bounds(), caps)) {
joshualitt8cab9a72015-07-16 09:13:50 -0700725 return false;
726 }
727
joshualitt76e7fb62015-02-11 08:52:27 -0800728 // TODO use vertex color to avoid breaking batches
729 if (this->color() != that->color()) {
730 return false;
731 }
732
733 if (this->stroke() != that->stroke()) {
734 return false;
735 }
736
737 SkASSERT(this->usesLocalCoords() == that->usesLocalCoords());
738 if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
739 return false;
740 }
741
742 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin());
joshualitt99c7c072015-05-01 13:43:30 -0700743 this->joinBounds(that->bounds());
joshualitt76e7fb62015-02-11 08:52:27 -0800744 return true;
745 }
746
747 GrColor color() const { return fBatch.fColor; }
748 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
749 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; }
750 bool stroke() const { return fBatch.fStroke; }
751
752 struct BatchTracker {
753 GrColor fColor;
754 bool fStroke;
755 bool fUsesLocalCoords;
756 bool fColorIgnored;
757 bool fCoverageIgnored;
758 };
759
joshualitt76e7fb62015-02-11 08:52:27 -0800760 BatchTracker fBatch;
761 SkSTArray<1, Geometry, true> fGeoData;
reed1b55a962015-09-17 20:16:13 -0700762
763 typedef GrVertexBatch INHERITED;
joshualitt76e7fb62015-02-11 08:52:27 -0800764};
765
bsalomonabd30f52015-08-13 13:34:48 -0700766static GrDrawBatch* create_circle_batch(GrColor color,
767 const SkMatrix& viewMatrix,
768 bool useCoverageAA,
769 const SkRect& circle,
770 const SkStrokeRec& stroke) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000771 SkPoint center = SkPoint::Make(circle.centerX(), circle.centerY());
joshualitt8059eb92014-12-29 15:10:07 -0800772 viewMatrix.mapPoints(&center, 1);
773 SkScalar radius = viewMatrix.mapRadius(SkScalarHalf(circle.width()));
774 SkScalar strokeWidth = viewMatrix.mapRadius(stroke.getWidth());
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000775
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000776 SkStrokeRec::Style style = stroke.getStyle();
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000777 bool isStrokeOnly = SkStrokeRec::kStroke_Style == style ||
778 SkStrokeRec::kHairline_Style == style;
779 bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == style;
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000780
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000781 SkScalar innerRadius = 0.0f;
782 SkScalar outerRadius = radius;
783 SkScalar halfWidth = 0;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000784 if (hasStroke) {
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000785 if (SkScalarNearlyZero(strokeWidth)) {
786 halfWidth = SK_ScalarHalf;
787 } else {
788 halfWidth = SkScalarHalf(strokeWidth);
789 }
790
791 outerRadius += halfWidth;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000792 if (isStrokeOnly) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000793 innerRadius = radius - halfWidth;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000794 }
795 }
796
bsalomonce1c8862014-12-15 07:11:22 -0800797 // The radii are outset for two reasons. First, it allows the shader to simply perform simpler
798 // computation because the computed alpha is zero, rather than 50%, at the radius.
799 // Second, the outer radius is used to compute the verts of the bounding box that is rendered
800 // and the outset ensures the box will cover all partially covered by the circle.
bsalomon@google.com58e30fe2013-04-01 19:01:20 +0000801 outerRadius += SK_ScalarHalf;
802 innerRadius -= SK_ScalarHalf;
803
joshualitt76e7fb62015-02-11 08:52:27 -0800804 CircleBatch::Geometry geometry;
805 geometry.fViewMatrix = viewMatrix;
806 geometry.fColor = color;
807 geometry.fInnerRadius = innerRadius;
808 geometry.fOuterRadius = outerRadius;
809 geometry.fStroke = isStrokeOnly && innerRadius > 0;
joshualittd96a67b2015-05-05 14:09:05 -0700810 geometry.fDevBounds = SkRect::MakeLTRB(center.fX - outerRadius, center.fY - outerRadius,
811 center.fX + outerRadius, center.fY + outerRadius);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000812
joshualitt3e708c52015-04-30 13:49:27 -0700813 return CircleBatch::Create(geometry);
814}
815
robertphillipsea461502015-05-26 11:38:03 -0700816void GrOvalRenderer::DrawCircle(GrDrawTarget* target,
joshualittae3d63a2015-07-13 08:44:06 -0700817 const GrPipelineBuilder& pipelineBuilder,
joshualitt3e708c52015-04-30 13:49:27 -0700818 GrColor color,
819 const SkMatrix& viewMatrix,
820 bool useCoverageAA,
821 const SkRect& circle,
822 const SkStrokeRec& stroke) {
bsalomonabd30f52015-08-13 13:34:48 -0700823 SkAutoTUnref<GrDrawBatch> batch(create_circle_batch(color, viewMatrix, useCoverageAA, circle,
824 stroke));
joshualittae3d63a2015-07-13 08:44:06 -0700825 target->drawBatch(pipelineBuilder, batch);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000826}
827
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000828///////////////////////////////////////////////////////////////////////////////
829
bsalomonabd30f52015-08-13 13:34:48 -0700830class EllipseBatch : public GrVertexBatch {
joshualitt76e7fb62015-02-11 08:52:27 -0800831public:
reed1b55a962015-09-17 20:16:13 -0700832 DEFINE_BATCH_CLASS_ID
833
joshualitt76e7fb62015-02-11 08:52:27 -0800834 struct Geometry {
joshualitt76e7fb62015-02-11 08:52:27 -0800835 SkMatrix fViewMatrix;
reed1b55a962015-09-17 20:16:13 -0700836 SkRect fDevBounds;
joshualitt76e7fb62015-02-11 08:52:27 -0800837 SkScalar fXRadius;
838 SkScalar fYRadius;
839 SkScalar fInnerXRadius;
840 SkScalar fInnerYRadius;
reed1b55a962015-09-17 20:16:13 -0700841 GrColor fColor;
joshualitt76e7fb62015-02-11 08:52:27 -0800842 bool fStroke;
joshualitt76e7fb62015-02-11 08:52:27 -0800843 };
844
halcanary385fe4d2015-08-26 13:07:48 -0700845 static GrDrawBatch* Create(const Geometry& geometry) { return new EllipseBatch(geometry); }
joshualitt76e7fb62015-02-11 08:52:27 -0800846
mtklein36352bf2015-03-25 18:17:31 -0700847 const char* name() const override { return "EllipseBatch"; }
joshualitt76e7fb62015-02-11 08:52:27 -0800848
mtklein36352bf2015-03-25 18:17:31 -0700849 void getInvariantOutputColor(GrInitInvariantOutput* out) const override {
joshualitt76e7fb62015-02-11 08:52:27 -0800850 // When this is called on a batch, there is only one geometry bundle
851 out->setKnownFourComponents(fGeoData[0].fColor);
852 }
mtklein36352bf2015-03-25 18:17:31 -0700853 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override {
joshualitt76e7fb62015-02-11 08:52:27 -0800854 out->setUnknownSingleComponent();
855 }
856
bsalomone46f9fe2015-08-18 06:05:14 -0700857private:
bsalomon91d844d2015-08-10 10:47:29 -0700858 void initBatchTracker(const GrPipelineOptimizations& opt) override {
joshualitt76e7fb62015-02-11 08:52:27 -0800859 // Handle any color overrides
bsalomon91d844d2015-08-10 10:47:29 -0700860 if (!opt.readsCoverage()) {
joshualitt76e7fb62015-02-11 08:52:27 -0800861 fGeoData[0].fColor = GrColor_ILLEGAL;
joshualitt76e7fb62015-02-11 08:52:27 -0800862 }
bsalomon91d844d2015-08-10 10:47:29 -0700863 opt.getOverrideColorIfSet(&fGeoData[0].fColor);
joshualitt76e7fb62015-02-11 08:52:27 -0800864
865 // setup batch properties
bsalomon91d844d2015-08-10 10:47:29 -0700866 fBatch.fColorIgnored = !opt.readsColor();
joshualitt76e7fb62015-02-11 08:52:27 -0800867 fBatch.fColor = fGeoData[0].fColor;
868 fBatch.fStroke = fGeoData[0].fStroke;
bsalomon91d844d2015-08-10 10:47:29 -0700869 fBatch.fUsesLocalCoords = opt.readsLocalCoords();
870 fBatch.fCoverageIgnored = !opt.readsCoverage();
joshualitt76e7fb62015-02-11 08:52:27 -0800871 }
872
bsalomon75398562015-08-17 12:55:38 -0700873 void onPrepareDraws(Target* target) override {
joshualitt76e7fb62015-02-11 08:52:27 -0800874 SkMatrix invert;
875 if (!this->viewMatrix().invert(&invert)) {
876 return;
877 }
878
879 // Setup geometry processor
880 SkAutoTUnref<GrGeometryProcessor> gp(EllipseEdgeEffect::Create(this->color(),
881 this->stroke(),
joshualittb8c241a2015-05-19 08:23:30 -0700882 invert,
883 this->usesLocalCoords()));
joshualitt76e7fb62015-02-11 08:52:27 -0800884
bsalomon75398562015-08-17 12:55:38 -0700885 target->initDraw(gp, this->pipeline());
joshualitt76e7fb62015-02-11 08:52:27 -0800886
joshualitt76e7fb62015-02-11 08:52:27 -0800887 int instanceCount = fGeoData.count();
bsalomonb5238a72015-05-05 07:49:49 -0700888 QuadHelper helper;
joshualitt76e7fb62015-02-11 08:52:27 -0800889 size_t vertexStride = gp->getVertexStride();
joshualitt19e00582015-02-11 17:36:30 -0800890 SkASSERT(vertexStride == sizeof(EllipseVertex));
bsalomonb5238a72015-05-05 07:49:49 -0700891 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(
bsalomon75398562015-08-17 12:55:38 -0700892 helper.init(target, vertexStride, instanceCount));
bsalomonb5238a72015-05-05 07:49:49 -0700893 if (!verts) {
joshualitt4b31de82015-03-05 14:33:41 -0800894 return;
895 }
896
bsalomon8415abe2015-05-04 11:41:41 -0700897 for (int i = 0; i < instanceCount; i++) {
bsalomonb5238a72015-05-05 07:49:49 -0700898 Geometry& geom = fGeoData[i];
bsalomon8415abe2015-05-04 11:41:41 -0700899
bsalomonb5238a72015-05-05 07:49:49 -0700900 SkScalar xRadius = geom.fXRadius;
901 SkScalar yRadius = geom.fYRadius;
joshualitt76e7fb62015-02-11 08:52:27 -0800902
903 // Compute the reciprocals of the radii here to save time in the shader
904 SkScalar xRadRecip = SkScalarInvert(xRadius);
905 SkScalar yRadRecip = SkScalarInvert(yRadius);
bsalomonb5238a72015-05-05 07:49:49 -0700906 SkScalar xInnerRadRecip = SkScalarInvert(geom.fInnerXRadius);
907 SkScalar yInnerRadRecip = SkScalarInvert(geom.fInnerYRadius);
joshualitt76e7fb62015-02-11 08:52:27 -0800908
bsalomonb5238a72015-05-05 07:49:49 -0700909 const SkRect& bounds = geom.fDevBounds;
joshualitt76e7fb62015-02-11 08:52:27 -0800910
911 // The inner radius in the vertex data must be specified in normalized space.
912 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
913 verts[0].fOffset = SkPoint::Make(-xRadius, -yRadius);
914 verts[0].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
915 verts[0].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
916
917 verts[1].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
918 verts[1].fOffset = SkPoint::Make(-xRadius, yRadius);
919 verts[1].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
920 verts[1].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
921
922 verts[2].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
923 verts[2].fOffset = SkPoint::Make(xRadius, yRadius);
924 verts[2].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
925 verts[2].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
926
927 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
928 verts[3].fOffset = SkPoint::Make(xRadius, -yRadius);
929 verts[3].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
930 verts[3].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
931
bsalomonb5238a72015-05-05 07:49:49 -0700932 verts += kVerticesPerQuad;
joshualitt76e7fb62015-02-11 08:52:27 -0800933 }
bsalomon75398562015-08-17 12:55:38 -0700934 helper.recordDraw(target);
joshualitt76e7fb62015-02-11 08:52:27 -0800935 }
936
937 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
938
reed1b55a962015-09-17 20:16:13 -0700939 EllipseBatch(const Geometry& geometry) : INHERITED(ClassID()) {
joshualitt76e7fb62015-02-11 08:52:27 -0800940 fGeoData.push_back(geometry);
joshualitt99c7c072015-05-01 13:43:30 -0700941
942 this->setBounds(geometry.fDevBounds);
joshualitt76e7fb62015-02-11 08:52:27 -0800943 }
944
bsalomoncb02b382015-08-12 11:14:50 -0700945 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override {
bsalomonabd30f52015-08-13 13:34:48 -0700946 EllipseBatch* that = t->cast<EllipseBatch>();
947
948 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
949 that->bounds(), caps)) {
joshualitt8cab9a72015-07-16 09:13:50 -0700950 return false;
951 }
952
joshualitt76e7fb62015-02-11 08:52:27 -0800953 // TODO use vertex color to avoid breaking batches
954 if (this->color() != that->color()) {
955 return false;
956 }
957
958 if (this->stroke() != that->stroke()) {
959 return false;
960 }
961
962 SkASSERT(this->usesLocalCoords() == that->usesLocalCoords());
963 if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
964 return false;
965 }
966
967 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin());
joshualitt99c7c072015-05-01 13:43:30 -0700968 this->joinBounds(that->bounds());
joshualitt76e7fb62015-02-11 08:52:27 -0800969 return true;
970 }
971
972 GrColor color() const { return fBatch.fColor; }
973 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
974 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; }
975 bool stroke() const { return fBatch.fStroke; }
976
977 struct BatchTracker {
978 GrColor fColor;
979 bool fStroke;
980 bool fUsesLocalCoords;
981 bool fColorIgnored;
982 bool fCoverageIgnored;
983 };
984
joshualitt76e7fb62015-02-11 08:52:27 -0800985 BatchTracker fBatch;
986 SkSTArray<1, Geometry, true> fGeoData;
reed1b55a962015-09-17 20:16:13 -0700987
988 typedef GrVertexBatch INHERITED;
joshualitt76e7fb62015-02-11 08:52:27 -0800989};
990
bsalomonabd30f52015-08-13 13:34:48 -0700991static GrDrawBatch* create_ellipse_batch(GrColor color,
992 const SkMatrix& viewMatrix,
993 bool useCoverageAA,
994 const SkRect& ellipse,
995 const SkStrokeRec& stroke) {
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000996#ifdef SK_DEBUG
997 {
998 // we should have checked for this previously
joshualitt8059eb92014-12-29 15:10:07 -0800999 bool isAxisAlignedEllipse = viewMatrix.rectStaysRect();
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001000 SkASSERT(useCoverageAA && isAxisAlignedEllipse);
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001001 }
1002#endif
1003
commit-bot@chromium.org0c888282013-04-19 19:01:45 +00001004 // do any matrix crunching before we reset the draw state for device coords
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001005 SkPoint center = SkPoint::Make(ellipse.centerX(), ellipse.centerY());
joshualitt8059eb92014-12-29 15:10:07 -08001006 viewMatrix.mapPoints(&center, 1);
commit-bot@chromium.org0c888282013-04-19 19:01:45 +00001007 SkScalar ellipseXRadius = SkScalarHalf(ellipse.width());
1008 SkScalar ellipseYRadius = SkScalarHalf(ellipse.height());
joshualitt8059eb92014-12-29 15:10:07 -08001009 SkScalar xRadius = SkScalarAbs(viewMatrix[SkMatrix::kMScaleX]*ellipseXRadius +
1010 viewMatrix[SkMatrix::kMSkewY]*ellipseYRadius);
1011 SkScalar yRadius = SkScalarAbs(viewMatrix[SkMatrix::kMSkewX]*ellipseXRadius +
1012 viewMatrix[SkMatrix::kMScaleY]*ellipseYRadius);
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001013
commit-bot@chromium.org0c888282013-04-19 19:01:45 +00001014 // do (potentially) anisotropic mapping of stroke
1015 SkVector scaledStroke;
1016 SkScalar strokeWidth = stroke.getWidth();
joshualitt8059eb92014-12-29 15:10:07 -08001017 scaledStroke.fX = SkScalarAbs(strokeWidth*(viewMatrix[SkMatrix::kMScaleX] +
1018 viewMatrix[SkMatrix::kMSkewY]));
1019 scaledStroke.fY = SkScalarAbs(strokeWidth*(viewMatrix[SkMatrix::kMSkewX] +
1020 viewMatrix[SkMatrix::kMScaleY]));
commit-bot@chromium.org0c888282013-04-19 19:01:45 +00001021
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001022 SkStrokeRec::Style style = stroke.getStyle();
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001023 bool isStrokeOnly = SkStrokeRec::kStroke_Style == style ||
1024 SkStrokeRec::kHairline_Style == style;
1025 bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == style;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001026
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +00001027 SkScalar innerXRadius = 0;
1028 SkScalar innerYRadius = 0;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001029 if (hasStroke) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001030 if (SkScalarNearlyZero(scaledStroke.length())) {
1031 scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf);
1032 } else {
1033 scaledStroke.scale(SK_ScalarHalf);
1034 }
1035
1036 // we only handle thick strokes for near-circular ellipses
1037 if (scaledStroke.length() > SK_ScalarHalf &&
1038 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
halcanary96fcdcc2015-08-27 07:41:13 -07001039 return nullptr;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001040 }
1041
1042 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
1043 if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius ||
1044 scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) {
halcanary96fcdcc2015-08-27 07:41:13 -07001045 return nullptr;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001046 }
1047
1048 // this is legit only if scale & translation (which should be the case at the moment)
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001049 if (isStrokeOnly) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001050 innerXRadius = xRadius - scaledStroke.fX;
1051 innerYRadius = yRadius - scaledStroke.fY;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001052 }
1053
1054 xRadius += scaledStroke.fX;
1055 yRadius += scaledStroke.fY;
1056 }
1057
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001058 // We've extended the outer x radius out half a pixel to antialias.
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +00001059 // This will also expand the rect so all the pixels will be captured.
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001060 // TODO: Consider if we should use sqrt(2)/2 instead
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +00001061 xRadius += SK_ScalarHalf;
1062 yRadius += SK_ScalarHalf;
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001063
joshualitt76e7fb62015-02-11 08:52:27 -08001064 EllipseBatch::Geometry geometry;
1065 geometry.fViewMatrix = viewMatrix;
1066 geometry.fColor = color;
1067 geometry.fXRadius = xRadius;
1068 geometry.fYRadius = yRadius;
1069 geometry.fInnerXRadius = innerXRadius;
1070 geometry.fInnerYRadius = innerYRadius;
1071 geometry.fStroke = isStrokeOnly && innerXRadius > 0 && innerYRadius > 0;
joshualittd96a67b2015-05-05 14:09:05 -07001072 geometry.fDevBounds = SkRect::MakeLTRB(center.fX - xRadius, center.fY - yRadius,
1073 center.fX + xRadius, center.fY + yRadius);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +00001074
joshualitt3e708c52015-04-30 13:49:27 -07001075 return EllipseBatch::Create(geometry);
1076}
1077
robertphillipsea461502015-05-26 11:38:03 -07001078bool GrOvalRenderer::DrawEllipse(GrDrawTarget* target,
joshualittae3d63a2015-07-13 08:44:06 -07001079 const GrPipelineBuilder& pipelineBuilder,
joshualitt3e708c52015-04-30 13:49:27 -07001080 GrColor color,
1081 const SkMatrix& viewMatrix,
1082 bool useCoverageAA,
1083 const SkRect& ellipse,
1084 const SkStrokeRec& stroke) {
bsalomonabd30f52015-08-13 13:34:48 -07001085 SkAutoTUnref<GrDrawBatch> batch(create_ellipse_batch(color, viewMatrix, useCoverageAA, ellipse,
1086 stroke));
joshualitt3e708c52015-04-30 13:49:27 -07001087 if (!batch) {
1088 return false;
1089 }
1090
joshualittae3d63a2015-07-13 08:44:06 -07001091 target->drawBatch(pipelineBuilder, batch);
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +00001092 return true;
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001093}
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001094
joshualitt76e7fb62015-02-11 08:52:27 -08001095/////////////////////////////////////////////////////////////////////////////////////////////////
1096
bsalomonabd30f52015-08-13 13:34:48 -07001097class DIEllipseBatch : public GrVertexBatch {
joshualitt76e7fb62015-02-11 08:52:27 -08001098public:
reed1b55a962015-09-17 20:16:13 -07001099 DEFINE_BATCH_CLASS_ID
1100
joshualitt76e7fb62015-02-11 08:52:27 -08001101 struct Geometry {
joshualitt76e7fb62015-02-11 08:52:27 -08001102 SkMatrix fViewMatrix;
reed1b55a962015-09-17 20:16:13 -07001103 SkRect fBounds;
joshualitt76e7fb62015-02-11 08:52:27 -08001104 SkScalar fXRadius;
1105 SkScalar fYRadius;
1106 SkScalar fInnerXRadius;
1107 SkScalar fInnerYRadius;
1108 SkScalar fGeoDx;
1109 SkScalar fGeoDy;
reed1b55a962015-09-17 20:16:13 -07001110 GrColor fColor;
joshualitt76e7fb62015-02-11 08:52:27 -08001111 DIEllipseEdgeEffect::Mode fMode;
joshualitt76e7fb62015-02-11 08:52:27 -08001112 };
1113
bsalomonabd30f52015-08-13 13:34:48 -07001114 static GrDrawBatch* Create(const Geometry& geometry, const SkRect& bounds) {
halcanary385fe4d2015-08-26 13:07:48 -07001115 return new DIEllipseBatch(geometry, bounds);
joshualitt76e7fb62015-02-11 08:52:27 -08001116 }
1117
mtklein36352bf2015-03-25 18:17:31 -07001118 const char* name() const override { return "DIEllipseBatch"; }
joshualitt76e7fb62015-02-11 08:52:27 -08001119
mtklein36352bf2015-03-25 18:17:31 -07001120 void getInvariantOutputColor(GrInitInvariantOutput* out) const override {
joshualitt76e7fb62015-02-11 08:52:27 -08001121 // When this is called on a batch, there is only one geometry bundle
1122 out->setKnownFourComponents(fGeoData[0].fColor);
1123 }
mtklein36352bf2015-03-25 18:17:31 -07001124 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override {
joshualitt76e7fb62015-02-11 08:52:27 -08001125 out->setUnknownSingleComponent();
1126 }
1127
bsalomone46f9fe2015-08-18 06:05:14 -07001128private:
1129
bsalomon91d844d2015-08-10 10:47:29 -07001130 void initBatchTracker(const GrPipelineOptimizations& opt) override {
joshualitt76e7fb62015-02-11 08:52:27 -08001131 // Handle any color overrides
bsalomon91d844d2015-08-10 10:47:29 -07001132 if (!opt.readsColor()) {
joshualitt76e7fb62015-02-11 08:52:27 -08001133 fGeoData[0].fColor = GrColor_ILLEGAL;
joshualitt76e7fb62015-02-11 08:52:27 -08001134 }
bsalomon91d844d2015-08-10 10:47:29 -07001135 opt.getOverrideColorIfSet(&fGeoData[0].fColor);
joshualitt76e7fb62015-02-11 08:52:27 -08001136
1137 // setup batch properties
bsalomon91d844d2015-08-10 10:47:29 -07001138 fBatch.fColorIgnored = !opt.readsColor();
joshualitt76e7fb62015-02-11 08:52:27 -08001139 fBatch.fColor = fGeoData[0].fColor;
1140 fBatch.fMode = fGeoData[0].fMode;
bsalomon91d844d2015-08-10 10:47:29 -07001141 fBatch.fUsesLocalCoords = opt.readsLocalCoords();
1142 fBatch.fCoverageIgnored = !opt.readsCoverage();
joshualitt76e7fb62015-02-11 08:52:27 -08001143 }
1144
bsalomon75398562015-08-17 12:55:38 -07001145 void onPrepareDraws(Target* target) override {
joshualitt76e7fb62015-02-11 08:52:27 -08001146 // Setup geometry processor
1147 SkAutoTUnref<GrGeometryProcessor> gp(DIEllipseEdgeEffect::Create(this->color(),
1148 this->viewMatrix(),
joshualittb8c241a2015-05-19 08:23:30 -07001149 this->mode(),
1150 this->usesLocalCoords()));
joshualitt76e7fb62015-02-11 08:52:27 -08001151
bsalomon75398562015-08-17 12:55:38 -07001152 target->initDraw(gp, this->pipeline());
joshualitt76e7fb62015-02-11 08:52:27 -08001153
joshualitt76e7fb62015-02-11 08:52:27 -08001154 int instanceCount = fGeoData.count();
joshualitt76e7fb62015-02-11 08:52:27 -08001155 size_t vertexStride = gp->getVertexStride();
joshualitt19e00582015-02-11 17:36:30 -08001156 SkASSERT(vertexStride == sizeof(DIEllipseVertex));
bsalomonb5238a72015-05-05 07:49:49 -07001157 QuadHelper helper;
1158 DIEllipseVertex* verts = reinterpret_cast<DIEllipseVertex*>(
bsalomon75398562015-08-17 12:55:38 -07001159 helper.init(target, vertexStride, instanceCount));
bsalomonb5238a72015-05-05 07:49:49 -07001160 if (!verts) {
joshualitt4b31de82015-03-05 14:33:41 -08001161 return;
1162 }
1163
joshualitt76e7fb62015-02-11 08:52:27 -08001164 for (int i = 0; i < instanceCount; i++) {
bsalomonb5238a72015-05-05 07:49:49 -07001165 Geometry& geom = fGeoData[i];
joshualitt76e7fb62015-02-11 08:52:27 -08001166
bsalomonb5238a72015-05-05 07:49:49 -07001167 SkScalar xRadius = geom.fXRadius;
1168 SkScalar yRadius = geom.fYRadius;
joshualitt76e7fb62015-02-11 08:52:27 -08001169
bsalomonb5238a72015-05-05 07:49:49 -07001170 const SkRect& bounds = geom.fBounds;
joshualitt76e7fb62015-02-11 08:52:27 -08001171
1172 // This adjusts the "radius" to include the half-pixel border
reed80ea19c2015-05-12 10:37:34 -07001173 SkScalar offsetDx = geom.fGeoDx / xRadius;
1174 SkScalar offsetDy = geom.fGeoDy / yRadius;
joshualitt76e7fb62015-02-11 08:52:27 -08001175
reed80ea19c2015-05-12 10:37:34 -07001176 SkScalar innerRatioX = xRadius / geom.fInnerXRadius;
1177 SkScalar innerRatioY = yRadius / geom.fInnerYRadius;
joshualitt76e7fb62015-02-11 08:52:27 -08001178
1179 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
1180 verts[0].fOuterOffset = SkPoint::Make(-1.0f - offsetDx, -1.0f - offsetDy);
1181 verts[0].fInnerOffset = SkPoint::Make(-innerRatioX - offsetDx, -innerRatioY - offsetDy);
1182
1183 verts[1].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
1184 verts[1].fOuterOffset = SkPoint::Make(-1.0f - offsetDx, 1.0f + offsetDy);
1185 verts[1].fInnerOffset = SkPoint::Make(-innerRatioX - offsetDx, innerRatioY + offsetDy);
1186
1187 verts[2].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
1188 verts[2].fOuterOffset = SkPoint::Make(1.0f + offsetDx, 1.0f + offsetDy);
1189 verts[2].fInnerOffset = SkPoint::Make(innerRatioX + offsetDx, innerRatioY + offsetDy);
1190
1191 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
1192 verts[3].fOuterOffset = SkPoint::Make(1.0f + offsetDx, -1.0f - offsetDy);
1193 verts[3].fInnerOffset = SkPoint::Make(innerRatioX + offsetDx, -innerRatioY - offsetDy);
1194
bsalomonb5238a72015-05-05 07:49:49 -07001195 verts += kVerticesPerQuad;
joshualitt76e7fb62015-02-11 08:52:27 -08001196 }
bsalomon75398562015-08-17 12:55:38 -07001197 helper.recordDraw(target);
joshualitt76e7fb62015-02-11 08:52:27 -08001198 }
1199
1200 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
1201
reed1b55a962015-09-17 20:16:13 -07001202 DIEllipseBatch(const Geometry& geometry, const SkRect& bounds) : INHERITED(ClassID()) {
joshualitt76e7fb62015-02-11 08:52:27 -08001203 fGeoData.push_back(geometry);
joshualitt99c7c072015-05-01 13:43:30 -07001204
1205 this->setBounds(bounds);
joshualitt76e7fb62015-02-11 08:52:27 -08001206 }
1207
bsalomoncb02b382015-08-12 11:14:50 -07001208 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override {
bsalomonabd30f52015-08-13 13:34:48 -07001209 DIEllipseBatch* that = t->cast<DIEllipseBatch>();
1210 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
1211 that->bounds(), caps)) {
joshualitt8cab9a72015-07-16 09:13:50 -07001212 return false;
1213 }
1214
joshualitt76e7fb62015-02-11 08:52:27 -08001215 // TODO use vertex color to avoid breaking batches
1216 if (this->color() != that->color()) {
1217 return false;
1218 }
1219
1220 if (this->mode() != that->mode()) {
1221 return false;
1222 }
1223
joshualittd96a67b2015-05-05 14:09:05 -07001224 // TODO rewrite to allow positioning on CPU
1225 if (!this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
joshualitt76e7fb62015-02-11 08:52:27 -08001226 return false;
1227 }
1228
1229 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin());
joshualitt99c7c072015-05-01 13:43:30 -07001230 this->joinBounds(that->bounds());
joshualitt76e7fb62015-02-11 08:52:27 -08001231 return true;
1232 }
1233
1234 GrColor color() const { return fBatch.fColor; }
1235 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
1236 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; }
1237 DIEllipseEdgeEffect::Mode mode() const { return fBatch.fMode; }
1238
1239 struct BatchTracker {
1240 GrColor fColor;
1241 DIEllipseEdgeEffect::Mode fMode;
1242 bool fUsesLocalCoords;
1243 bool fColorIgnored;
1244 bool fCoverageIgnored;
1245 };
1246
joshualitt76e7fb62015-02-11 08:52:27 -08001247 BatchTracker fBatch;
1248 SkSTArray<1, Geometry, true> fGeoData;
reed1b55a962015-09-17 20:16:13 -07001249
1250 typedef GrVertexBatch INHERITED;
joshualitt76e7fb62015-02-11 08:52:27 -08001251};
1252
bsalomonabd30f52015-08-13 13:34:48 -07001253static GrDrawBatch* create_diellipse_batch(GrColor color,
1254 const SkMatrix& viewMatrix,
1255 bool useCoverageAA,
1256 const SkRect& ellipse,
1257 const SkStrokeRec& stroke) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001258 SkPoint center = SkPoint::Make(ellipse.centerX(), ellipse.centerY());
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +00001259 SkScalar xRadius = SkScalarHalf(ellipse.width());
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +00001260 SkScalar yRadius = SkScalarHalf(ellipse.height());
1261
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +00001262 SkStrokeRec::Style style = stroke.getStyle();
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +00001263 DIEllipseEdgeEffect::Mode mode = (SkStrokeRec::kStroke_Style == style) ?
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +00001264 DIEllipseEdgeEffect::kStroke :
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +00001265 (SkStrokeRec::kHairline_Style == style) ?
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +00001266 DIEllipseEdgeEffect::kHairline : DIEllipseEdgeEffect::kFill;
1267
1268 SkScalar innerXRadius = 0;
1269 SkScalar innerYRadius = 0;
1270 if (SkStrokeRec::kFill_Style != style && SkStrokeRec::kHairline_Style != style) {
1271 SkScalar strokeWidth = stroke.getWidth();
1272
1273 if (SkScalarNearlyZero(strokeWidth)) {
1274 strokeWidth = SK_ScalarHalf;
1275 } else {
1276 strokeWidth *= SK_ScalarHalf;
1277 }
1278
1279 // we only handle thick strokes for near-circular ellipses
1280 if (strokeWidth > SK_ScalarHalf &&
1281 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
halcanary96fcdcc2015-08-27 07:41:13 -07001282 return nullptr;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +00001283 }
1284
1285 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
1286 if (strokeWidth*(yRadius*yRadius) < (strokeWidth*strokeWidth)*xRadius ||
1287 strokeWidth*(xRadius*xRadius) < (strokeWidth*strokeWidth)*yRadius) {
halcanary96fcdcc2015-08-27 07:41:13 -07001288 return nullptr;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +00001289 }
1290
1291 // set inner radius (if needed)
1292 if (SkStrokeRec::kStroke_Style == style) {
1293 innerXRadius = xRadius - strokeWidth;
1294 innerYRadius = yRadius - strokeWidth;
1295 }
1296
1297 xRadius += strokeWidth;
1298 yRadius += strokeWidth;
1299 }
1300 if (DIEllipseEdgeEffect::kStroke == mode) {
1301 mode = (innerXRadius > 0 && innerYRadius > 0) ? DIEllipseEdgeEffect::kStroke :
1302 DIEllipseEdgeEffect::kFill;
1303 }
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +00001304
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +00001305 // This expands the outer rect so that after CTM we end up with a half-pixel border
joshualitt8059eb92014-12-29 15:10:07 -08001306 SkScalar a = viewMatrix[SkMatrix::kMScaleX];
1307 SkScalar b = viewMatrix[SkMatrix::kMSkewX];
1308 SkScalar c = viewMatrix[SkMatrix::kMSkewY];
1309 SkScalar d = viewMatrix[SkMatrix::kMScaleY];
reed80ea19c2015-05-12 10:37:34 -07001310 SkScalar geoDx = SK_ScalarHalf / SkScalarSqrt(a*a + c*c);
1311 SkScalar geoDy = SK_ScalarHalf / SkScalarSqrt(b*b + d*d);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +00001312
joshualitt76e7fb62015-02-11 08:52:27 -08001313 DIEllipseBatch::Geometry geometry;
1314 geometry.fViewMatrix = viewMatrix;
1315 geometry.fColor = color;
1316 geometry.fXRadius = xRadius;
1317 geometry.fYRadius = yRadius;
1318 geometry.fInnerXRadius = innerXRadius;
1319 geometry.fInnerYRadius = innerYRadius;
1320 geometry.fGeoDx = geoDx;
1321 geometry.fGeoDy = geoDy;
1322 geometry.fMode = mode;
joshualittd96a67b2015-05-05 14:09:05 -07001323 geometry.fBounds = SkRect::MakeLTRB(center.fX - xRadius - geoDx, center.fY - yRadius - geoDy,
1324 center.fX + xRadius + geoDx, center.fY + yRadius + geoDy);
egdaniel9ef1bb12015-04-20 12:28:57 -07001325
joshualittd96a67b2015-05-05 14:09:05 -07001326 SkRect devBounds = geometry.fBounds;
1327 viewMatrix.mapRect(&devBounds);
1328 return DIEllipseBatch::Create(geometry, devBounds);
joshualitt3e708c52015-04-30 13:49:27 -07001329}
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +00001330
robertphillipsea461502015-05-26 11:38:03 -07001331bool GrOvalRenderer::DrawDIEllipse(GrDrawTarget* target,
joshualittae3d63a2015-07-13 08:44:06 -07001332 const GrPipelineBuilder& pipelineBuilder,
joshualitt3e708c52015-04-30 13:49:27 -07001333 GrColor color,
1334 const SkMatrix& viewMatrix,
1335 bool useCoverageAA,
1336 const SkRect& ellipse,
1337 const SkStrokeRec& stroke) {
bsalomonabd30f52015-08-13 13:34:48 -07001338 SkAutoTUnref<GrDrawBatch> batch(create_diellipse_batch(color, viewMatrix, useCoverageAA,
1339 ellipse, stroke));
joshualitt3e708c52015-04-30 13:49:27 -07001340 if (!batch) {
1341 return false;
1342 }
joshualittae3d63a2015-07-13 08:44:06 -07001343 target->drawBatch(pipelineBuilder, batch);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +00001344 return true;
1345}
1346
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001347///////////////////////////////////////////////////////////////////////////////
1348
1349static const uint16_t gRRectIndices[] = {
1350 // corners
1351 0, 1, 5, 0, 5, 4,
1352 2, 3, 7, 2, 7, 6,
1353 8, 9, 13, 8, 13, 12,
1354 10, 11, 15, 10, 15, 14,
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001355
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001356 // edges
1357 1, 2, 6, 1, 6, 5,
1358 4, 5, 9, 4, 9, 8,
1359 6, 7, 11, 6, 11, 10,
1360 9, 10, 14, 9, 14, 13,
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001361
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001362 // center
1363 // we place this at the end so that we can ignore these indices when rendering stroke-only
1364 5, 6, 10, 5, 10, 9
1365};
1366
joshualitt5ead6da2014-10-22 16:00:29 -07001367static const int kIndicesPerStrokeRRect = SK_ARRAY_COUNT(gRRectIndices) - 6;
1368static const int kIndicesPerRRect = SK_ARRAY_COUNT(gRRectIndices);
1369static const int kVertsPerRRect = 16;
1370static const int kNumRRectsInIndexBuffer = 256;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001371
bsalomoned0bcad2015-05-04 10:36:42 -07001372GR_DECLARE_STATIC_UNIQUE_KEY(gStrokeRRectOnlyIndexBufferKey);
1373GR_DECLARE_STATIC_UNIQUE_KEY(gRRectOnlyIndexBufferKey);
1374static const GrIndexBuffer* ref_rrect_index_buffer(bool strokeOnly,
1375 GrResourceProvider* resourceProvider) {
1376 GR_DEFINE_STATIC_UNIQUE_KEY(gStrokeRRectOnlyIndexBufferKey);
1377 GR_DEFINE_STATIC_UNIQUE_KEY(gRRectOnlyIndexBufferKey);
1378 if (strokeOnly) {
bsalomoneae62002015-07-31 13:59:30 -07001379 return resourceProvider->findOrCreateInstancedIndexBuffer(
bsalomoned0bcad2015-05-04 10:36:42 -07001380 gRRectIndices, kIndicesPerStrokeRRect, kNumRRectsInIndexBuffer, kVertsPerRRect,
1381 gStrokeRRectOnlyIndexBufferKey);
1382 } else {
bsalomoneae62002015-07-31 13:59:30 -07001383 return resourceProvider->findOrCreateInstancedIndexBuffer(
bsalomoned0bcad2015-05-04 10:36:42 -07001384 gRRectIndices, kIndicesPerRRect, kNumRRectsInIndexBuffer, kVertsPerRRect,
1385 gRRectOnlyIndexBufferKey);
1386
1387 }
1388}
1389
robertphillipsea461502015-05-26 11:38:03 -07001390bool GrOvalRenderer::DrawDRRect(GrDrawTarget* target,
joshualittae3d63a2015-07-13 08:44:06 -07001391 const GrPipelineBuilder& pipelineBuilder,
joshualitt2e3b3e32014-12-09 13:31:14 -08001392 GrColor color,
joshualitt8059eb92014-12-29 15:10:07 -08001393 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -08001394 bool useAA,
1395 const SkRRect& origOuter,
1396 const SkRRect& origInner) {
joshualittae3d63a2015-07-13 08:44:06 -07001397 bool applyAA = useAA && !pipelineBuilder.getRenderTarget()->isUnifiedMultisampled();
joshualitt4421a4c2015-07-13 09:36:41 -07001398 GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001399 if (!origInner.isEmpty()) {
1400 SkTCopyOnFirstWrite<SkRRect> inner(origInner);
joshualitt8059eb92014-12-29 15:10:07 -08001401 if (!viewMatrix.isIdentity()) {
1402 if (!origInner.transform(viewMatrix, inner.writable())) {
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001403 return false;
1404 }
1405 }
joshualittb0a8a372014-09-23 09:50:21 -07001406 GrPrimitiveEdgeType edgeType = applyAA ?
1407 kInverseFillAA_GrProcessorEdgeType :
1408 kInverseFillBW_GrProcessorEdgeType;
joshualitt2e3b3e32014-12-09 13:31:14 -08001409 // TODO this needs to be a geometry processor
joshualittb0a8a372014-09-23 09:50:21 -07001410 GrFragmentProcessor* fp = GrRRectEffect::Create(edgeType, *inner);
halcanary96fcdcc2015-08-27 07:41:13 -07001411 if (nullptr == fp) {
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001412 return false;
1413 }
joshualitt4421a4c2015-07-13 09:36:41 -07001414 arfps.set(&pipelineBuilder);
bsalomonac856c92015-08-27 06:30:17 -07001415 arfps.addCoverageFragmentProcessor(fp)->unref();
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001416 }
1417
1418 SkStrokeRec fillRec(SkStrokeRec::kFill_InitStyle);
robertphillipsea461502015-05-26 11:38:03 -07001419 if (DrawRRect(target, pipelineBuilder, color, viewMatrix, useAA, origOuter, fillRec)) {
bsalomon8af05232014-06-03 06:34:58 -07001420 return true;
1421 }
1422
1423 SkASSERT(!origOuter.isEmpty());
1424 SkTCopyOnFirstWrite<SkRRect> outer(origOuter);
joshualitt8059eb92014-12-29 15:10:07 -08001425 if (!viewMatrix.isIdentity()) {
1426 if (!origOuter.transform(viewMatrix, outer.writable())) {
bsalomon8af05232014-06-03 06:34:58 -07001427 return false;
1428 }
1429 }
joshualittb0a8a372014-09-23 09:50:21 -07001430 GrPrimitiveEdgeType edgeType = applyAA ? kFillAA_GrProcessorEdgeType :
joshualitt76e7fb62015-02-11 08:52:27 -08001431 kFillBW_GrProcessorEdgeType;
joshualittb0a8a372014-09-23 09:50:21 -07001432 GrFragmentProcessor* effect = GrRRectEffect::Create(edgeType, *outer);
halcanary96fcdcc2015-08-27 07:41:13 -07001433 if (nullptr == effect) {
bsalomon8af05232014-06-03 06:34:58 -07001434 return false;
1435 }
joshualitt4421a4c2015-07-13 09:36:41 -07001436 if (!arfps.isSet()) {
1437 arfps.set(&pipelineBuilder);
bsalomon8af05232014-06-03 06:34:58 -07001438 }
joshualittd27f73e2014-12-29 07:43:36 -08001439
1440 SkMatrix invert;
joshualitt8059eb92014-12-29 15:10:07 -08001441 if (!viewMatrix.invert(&invert)) {
bsalomon8af05232014-06-03 06:34:58 -07001442 return false;
1443 }
joshualittd27f73e2014-12-29 07:43:36 -08001444
bsalomonac856c92015-08-27 06:30:17 -07001445 arfps.addCoverageFragmentProcessor(effect)->unref();
bsalomon8af05232014-06-03 06:34:58 -07001446 SkRect bounds = outer->getBounds();
1447 if (applyAA) {
1448 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1449 }
joshualittd2b23e02015-08-21 10:53:34 -07001450 target->drawNonAARect(pipelineBuilder, color, SkMatrix::I(), bounds, invert);
bsalomon8af05232014-06-03 06:34:58 -07001451 return true;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001452}
1453
joshualitt76e7fb62015-02-11 08:52:27 -08001454///////////////////////////////////////////////////////////////////////////////////////////////////
1455
bsalomonabd30f52015-08-13 13:34:48 -07001456class RRectCircleRendererBatch : public GrVertexBatch {
joshualitt76e7fb62015-02-11 08:52:27 -08001457public:
reed1b55a962015-09-17 20:16:13 -07001458 DEFINE_BATCH_CLASS_ID
1459
joshualitt76e7fb62015-02-11 08:52:27 -08001460 struct Geometry {
joshualitt76e7fb62015-02-11 08:52:27 -08001461 SkMatrix fViewMatrix;
reed1b55a962015-09-17 20:16:13 -07001462 SkRect fDevBounds;
joshualitt76e7fb62015-02-11 08:52:27 -08001463 SkScalar fInnerRadius;
1464 SkScalar fOuterRadius;
reed1b55a962015-09-17 20:16:13 -07001465 GrColor fColor;
joshualitt76e7fb62015-02-11 08:52:27 -08001466 bool fStroke;
joshualitt76e7fb62015-02-11 08:52:27 -08001467 };
1468
bsalomonabd30f52015-08-13 13:34:48 -07001469 static GrDrawBatch* Create(const Geometry& geometry) {
halcanary385fe4d2015-08-26 13:07:48 -07001470 return new RRectCircleRendererBatch(geometry);
joshualitt76e7fb62015-02-11 08:52:27 -08001471 }
1472
mtklein36352bf2015-03-25 18:17:31 -07001473 const char* name() const override { return "RRectCircleBatch"; }
joshualitt76e7fb62015-02-11 08:52:27 -08001474
mtklein36352bf2015-03-25 18:17:31 -07001475 void getInvariantOutputColor(GrInitInvariantOutput* out) const override {
joshualitt76e7fb62015-02-11 08:52:27 -08001476 // When this is called on a batch, there is only one geometry bundle
1477 out->setKnownFourComponents(fGeoData[0].fColor);
1478 }
mtklein36352bf2015-03-25 18:17:31 -07001479 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override {
joshualitt76e7fb62015-02-11 08:52:27 -08001480 out->setUnknownSingleComponent();
1481 }
1482
bsalomone46f9fe2015-08-18 06:05:14 -07001483private:
bsalomon91d844d2015-08-10 10:47:29 -07001484 void initBatchTracker(const GrPipelineOptimizations& opt) override {
joshualitt76e7fb62015-02-11 08:52:27 -08001485 // Handle any color overrides
bsalomon91d844d2015-08-10 10:47:29 -07001486 if (!opt.readsColor()) {
joshualitt76e7fb62015-02-11 08:52:27 -08001487 fGeoData[0].fColor = GrColor_ILLEGAL;
joshualitt76e7fb62015-02-11 08:52:27 -08001488 }
bsalomon91d844d2015-08-10 10:47:29 -07001489 opt.getOverrideColorIfSet(&fGeoData[0].fColor);
joshualitt76e7fb62015-02-11 08:52:27 -08001490
1491 // setup batch properties
bsalomon91d844d2015-08-10 10:47:29 -07001492 fBatch.fColorIgnored = !opt.readsColor();
joshualitt76e7fb62015-02-11 08:52:27 -08001493 fBatch.fColor = fGeoData[0].fColor;
1494 fBatch.fStroke = fGeoData[0].fStroke;
bsalomon91d844d2015-08-10 10:47:29 -07001495 fBatch.fUsesLocalCoords = opt.readsLocalCoords();
1496 fBatch.fCoverageIgnored = !opt.readsCoverage();
joshualitt76e7fb62015-02-11 08:52:27 -08001497 }
1498
bsalomon75398562015-08-17 12:55:38 -07001499 void onPrepareDraws(Target* target) override {
joshualitt76e7fb62015-02-11 08:52:27 -08001500 // reset to device coordinates
1501 SkMatrix invert;
1502 if (!this->viewMatrix().invert(&invert)) {
1503 SkDebugf("Failed to invert\n");
1504 return;
1505 }
1506
1507 // Setup geometry processor
1508 SkAutoTUnref<GrGeometryProcessor> gp(CircleEdgeEffect::Create(this->color(),
1509 this->stroke(),
joshualittb8c241a2015-05-19 08:23:30 -07001510 invert,
1511 this->usesLocalCoords()));
joshualitt76e7fb62015-02-11 08:52:27 -08001512
bsalomon75398562015-08-17 12:55:38 -07001513 target->initDraw(gp, this->pipeline());
joshualitt76e7fb62015-02-11 08:52:27 -08001514
joshualitt76e7fb62015-02-11 08:52:27 -08001515 int instanceCount = fGeoData.count();
joshualitt76e7fb62015-02-11 08:52:27 -08001516 size_t vertexStride = gp->getVertexStride();
1517 SkASSERT(vertexStride == sizeof(CircleVertex));
1518
bsalomonb5238a72015-05-05 07:49:49 -07001519 // drop out the middle quad if we're stroked
1520 int indicesPerInstance = this->stroke() ? kIndicesPerStrokeRRect : kIndicesPerRRect;
bsalomoned0bcad2015-05-04 10:36:42 -07001521 SkAutoTUnref<const GrIndexBuffer> indexBuffer(
bsalomon75398562015-08-17 12:55:38 -07001522 ref_rrect_index_buffer(this->stroke(), target->resourceProvider()));
joshualitt76e7fb62015-02-11 08:52:27 -08001523
bsalomonb5238a72015-05-05 07:49:49 -07001524 InstancedHelper helper;
bsalomon75398562015-08-17 12:55:38 -07001525 CircleVertex* verts = reinterpret_cast<CircleVertex*>(helper.init(target,
bsalomonb5238a72015-05-05 07:49:49 -07001526 kTriangles_GrPrimitiveType, vertexStride, indexBuffer, kVertsPerRRect,
1527 indicesPerInstance, instanceCount));
1528 if (!verts || !indexBuffer) {
joshualitt4b31de82015-03-05 14:33:41 -08001529 SkDebugf("Could not allocate vertices\n");
1530 return;
1531 }
1532
joshualitt76e7fb62015-02-11 08:52:27 -08001533 for (int i = 0; i < instanceCount; i++) {
1534 Geometry& args = fGeoData[i];
1535
1536 SkScalar outerRadius = args.fOuterRadius;
1537
egdanielbc227142015-04-21 06:28:08 -07001538 const SkRect& bounds = args.fDevBounds;
joshualitt76e7fb62015-02-11 08:52:27 -08001539
1540 SkScalar yCoords[4] = {
1541 bounds.fTop,
1542 bounds.fTop + outerRadius,
1543 bounds.fBottom - outerRadius,
1544 bounds.fBottom
1545 };
1546
1547 SkScalar yOuterRadii[4] = {-1, 0, 0, 1 };
1548 // The inner radius in the vertex data must be specified in normalized space.
1549 SkScalar innerRadius = args.fInnerRadius / args.fOuterRadius;
1550 for (int i = 0; i < 4; ++i) {
1551 verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]);
1552 verts->fOffset = SkPoint::Make(-1, yOuterRadii[i]);
1553 verts->fOuterRadius = outerRadius;
1554 verts->fInnerRadius = innerRadius;
1555 verts++;
1556
1557 verts->fPos = SkPoint::Make(bounds.fLeft + outerRadius, yCoords[i]);
1558 verts->fOffset = SkPoint::Make(0, yOuterRadii[i]);
1559 verts->fOuterRadius = outerRadius;
1560 verts->fInnerRadius = innerRadius;
1561 verts++;
1562
1563 verts->fPos = SkPoint::Make(bounds.fRight - outerRadius, yCoords[i]);
1564 verts->fOffset = SkPoint::Make(0, yOuterRadii[i]);
1565 verts->fOuterRadius = outerRadius;
1566 verts->fInnerRadius = innerRadius;
1567 verts++;
1568
1569 verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]);
1570 verts->fOffset = SkPoint::Make(1, yOuterRadii[i]);
1571 verts->fOuterRadius = outerRadius;
1572 verts->fInnerRadius = innerRadius;
1573 verts++;
1574 }
1575 }
1576
bsalomon75398562015-08-17 12:55:38 -07001577 helper.recordDraw(target);
joshualitt76e7fb62015-02-11 08:52:27 -08001578 }
1579
1580 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
1581
reed1b55a962015-09-17 20:16:13 -07001582 RRectCircleRendererBatch(const Geometry& geometry) : INHERITED(ClassID()) {
joshualitt76e7fb62015-02-11 08:52:27 -08001583 fGeoData.push_back(geometry);
joshualitt99c7c072015-05-01 13:43:30 -07001584
1585 this->setBounds(geometry.fDevBounds);
joshualitt76e7fb62015-02-11 08:52:27 -08001586 }
1587
bsalomoncb02b382015-08-12 11:14:50 -07001588 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override {
bsalomonabd30f52015-08-13 13:34:48 -07001589 RRectCircleRendererBatch* that = t->cast<RRectCircleRendererBatch>();
1590 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
1591 that->bounds(), caps)) {
joshualitt8cab9a72015-07-16 09:13:50 -07001592 return false;
1593 }
1594
joshualitt76e7fb62015-02-11 08:52:27 -08001595 // TODO use vertex color to avoid breaking batches
1596 if (this->color() != that->color()) {
1597 return false;
1598 }
1599
1600 if (this->stroke() != that->stroke()) {
1601 return false;
1602 }
1603
1604 SkASSERT(this->usesLocalCoords() == that->usesLocalCoords());
1605 if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
1606 return false;
1607 }
1608
1609 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin());
joshualitt99c7c072015-05-01 13:43:30 -07001610 this->joinBounds(that->bounds());
joshualitt76e7fb62015-02-11 08:52:27 -08001611 return true;
1612 }
1613
1614 GrColor color() const { return fBatch.fColor; }
1615 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
1616 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; }
1617 bool stroke() const { return fBatch.fStroke; }
1618
1619 struct BatchTracker {
1620 GrColor fColor;
1621 bool fStroke;
1622 bool fUsesLocalCoords;
1623 bool fColorIgnored;
1624 bool fCoverageIgnored;
1625 };
1626
joshualitt76e7fb62015-02-11 08:52:27 -08001627 BatchTracker fBatch;
1628 SkSTArray<1, Geometry, true> fGeoData;
reed1b55a962015-09-17 20:16:13 -07001629
1630 typedef GrVertexBatch INHERITED;
joshualitt76e7fb62015-02-11 08:52:27 -08001631};
1632
bsalomonabd30f52015-08-13 13:34:48 -07001633class RRectEllipseRendererBatch : public GrVertexBatch {
joshualitt76e7fb62015-02-11 08:52:27 -08001634public:
reed1b55a962015-09-17 20:16:13 -07001635 DEFINE_BATCH_CLASS_ID
1636
joshualitt76e7fb62015-02-11 08:52:27 -08001637 struct Geometry {
joshualitt76e7fb62015-02-11 08:52:27 -08001638 SkMatrix fViewMatrix;
reed1b55a962015-09-17 20:16:13 -07001639 SkRect fDevBounds;
joshualitt76e7fb62015-02-11 08:52:27 -08001640 SkScalar fXRadius;
1641 SkScalar fYRadius;
1642 SkScalar fInnerXRadius;
1643 SkScalar fInnerYRadius;
reed1b55a962015-09-17 20:16:13 -07001644 GrColor fColor;
joshualitt76e7fb62015-02-11 08:52:27 -08001645 bool fStroke;
joshualitt76e7fb62015-02-11 08:52:27 -08001646 };
1647
bsalomonabd30f52015-08-13 13:34:48 -07001648 static GrDrawBatch* Create(const Geometry& geometry) {
halcanary385fe4d2015-08-26 13:07:48 -07001649 return new RRectEllipseRendererBatch(geometry);
joshualitt76e7fb62015-02-11 08:52:27 -08001650 }
1651
mtklein36352bf2015-03-25 18:17:31 -07001652 const char* name() const override { return "RRectEllipseRendererBatch"; }
joshualitt76e7fb62015-02-11 08:52:27 -08001653
mtklein36352bf2015-03-25 18:17:31 -07001654 void getInvariantOutputColor(GrInitInvariantOutput* out) const override {
joshualitt76e7fb62015-02-11 08:52:27 -08001655 // When this is called on a batch, there is only one geometry bundle
1656 out->setKnownFourComponents(fGeoData[0].fColor);
1657 }
mtklein36352bf2015-03-25 18:17:31 -07001658 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override {
joshualitt76e7fb62015-02-11 08:52:27 -08001659 out->setUnknownSingleComponent();
1660 }
1661
bsalomone46f9fe2015-08-18 06:05:14 -07001662private:
bsalomon91d844d2015-08-10 10:47:29 -07001663 void initBatchTracker(const GrPipelineOptimizations& opt) override {
joshualitt76e7fb62015-02-11 08:52:27 -08001664 // Handle any color overrides
bsalomon91d844d2015-08-10 10:47:29 -07001665 if (!opt.readsColor()) {
joshualitt76e7fb62015-02-11 08:52:27 -08001666 fGeoData[0].fColor = GrColor_ILLEGAL;
joshualitt76e7fb62015-02-11 08:52:27 -08001667 }
bsalomon91d844d2015-08-10 10:47:29 -07001668 opt.getOverrideColorIfSet(&fGeoData[0].fColor);
joshualitt76e7fb62015-02-11 08:52:27 -08001669
1670 // setup batch properties
bsalomon91d844d2015-08-10 10:47:29 -07001671 fBatch.fColorIgnored = !opt.readsColor();
joshualitt76e7fb62015-02-11 08:52:27 -08001672 fBatch.fColor = fGeoData[0].fColor;
1673 fBatch.fStroke = fGeoData[0].fStroke;
bsalomon91d844d2015-08-10 10:47:29 -07001674 fBatch.fUsesLocalCoords = opt.readsLocalCoords();
1675 fBatch.fCoverageIgnored = !opt.readsCoverage();
joshualitt76e7fb62015-02-11 08:52:27 -08001676 }
1677
bsalomon75398562015-08-17 12:55:38 -07001678 void onPrepareDraws(Target* target) override {
joshualitt76e7fb62015-02-11 08:52:27 -08001679 // reset to device coordinates
1680 SkMatrix invert;
1681 if (!this->viewMatrix().invert(&invert)) {
1682 SkDebugf("Failed to invert\n");
1683 return;
1684 }
1685
1686 // Setup geometry processor
1687 SkAutoTUnref<GrGeometryProcessor> gp(EllipseEdgeEffect::Create(this->color(),
1688 this->stroke(),
joshualittb8c241a2015-05-19 08:23:30 -07001689 invert,
1690 this->usesLocalCoords()));
joshualitt76e7fb62015-02-11 08:52:27 -08001691
bsalomon75398562015-08-17 12:55:38 -07001692 target->initDraw(gp, this->pipeline());
joshualitt76e7fb62015-02-11 08:52:27 -08001693
joshualitt76e7fb62015-02-11 08:52:27 -08001694 int instanceCount = fGeoData.count();
joshualitt76e7fb62015-02-11 08:52:27 -08001695 size_t vertexStride = gp->getVertexStride();
1696 SkASSERT(vertexStride == sizeof(EllipseVertex));
1697
bsalomonb5238a72015-05-05 07:49:49 -07001698 // drop out the middle quad if we're stroked
1699 int indicesPerInstance = this->stroke() ? kIndicesPerStrokeRRect : kIndicesPerRRect;
bsalomoned0bcad2015-05-04 10:36:42 -07001700 SkAutoTUnref<const GrIndexBuffer> indexBuffer(
bsalomon75398562015-08-17 12:55:38 -07001701 ref_rrect_index_buffer(this->stroke(), target->resourceProvider()));
joshualitt76e7fb62015-02-11 08:52:27 -08001702
bsalomonb5238a72015-05-05 07:49:49 -07001703 InstancedHelper helper;
1704 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(
bsalomon75398562015-08-17 12:55:38 -07001705 helper.init(target, kTriangles_GrPrimitiveType, vertexStride, indexBuffer,
bsalomonb5238a72015-05-05 07:49:49 -07001706 kVertsPerRRect, indicesPerInstance, instanceCount));
1707 if (!verts || !indexBuffer) {
joshualitt4b31de82015-03-05 14:33:41 -08001708 SkDebugf("Could not allocate vertices\n");
1709 return;
1710 }
1711
joshualitt76e7fb62015-02-11 08:52:27 -08001712 for (int i = 0; i < instanceCount; i++) {
1713 Geometry& args = fGeoData[i];
1714
1715 // Compute the reciprocals of the radii here to save time in the shader
1716 SkScalar xRadRecip = SkScalarInvert(args.fXRadius);
1717 SkScalar yRadRecip = SkScalarInvert(args.fYRadius);
1718 SkScalar xInnerRadRecip = SkScalarInvert(args.fInnerXRadius);
1719 SkScalar yInnerRadRecip = SkScalarInvert(args.fInnerYRadius);
1720
1721 // Extend the radii out half a pixel to antialias.
1722 SkScalar xOuterRadius = args.fXRadius + SK_ScalarHalf;
1723 SkScalar yOuterRadius = args.fYRadius + SK_ScalarHalf;
1724
egdanielbc227142015-04-21 06:28:08 -07001725 const SkRect& bounds = args.fDevBounds;
joshualitt76e7fb62015-02-11 08:52:27 -08001726
1727 SkScalar yCoords[4] = {
1728 bounds.fTop,
1729 bounds.fTop + yOuterRadius,
1730 bounds.fBottom - yOuterRadius,
1731 bounds.fBottom
1732 };
1733 SkScalar yOuterOffsets[4] = {
1734 yOuterRadius,
1735 SK_ScalarNearlyZero, // we're using inversesqrt() in shader, so can't be exactly 0
1736 SK_ScalarNearlyZero,
1737 yOuterRadius
1738 };
1739
1740 for (int i = 0; i < 4; ++i) {
1741 verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]);
1742 verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]);
1743 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1744 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
1745 verts++;
1746
1747 verts->fPos = SkPoint::Make(bounds.fLeft + xOuterRadius, yCoords[i]);
1748 verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]);
1749 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1750 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
1751 verts++;
1752
1753 verts->fPos = SkPoint::Make(bounds.fRight - xOuterRadius, yCoords[i]);
1754 verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]);
1755 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1756 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
1757 verts++;
1758
1759 verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]);
1760 verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]);
1761 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1762 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
1763 verts++;
1764 }
1765 }
bsalomon75398562015-08-17 12:55:38 -07001766 helper.recordDraw(target);
joshualitt76e7fb62015-02-11 08:52:27 -08001767 }
1768
1769 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
1770
reed1b55a962015-09-17 20:16:13 -07001771 RRectEllipseRendererBatch(const Geometry& geometry) : INHERITED(ClassID()) {
joshualitt76e7fb62015-02-11 08:52:27 -08001772 fGeoData.push_back(geometry);
joshualitt99c7c072015-05-01 13:43:30 -07001773
1774 this->setBounds(geometry.fDevBounds);
joshualitt76e7fb62015-02-11 08:52:27 -08001775 }
1776
bsalomoncb02b382015-08-12 11:14:50 -07001777 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override {
bsalomonabd30f52015-08-13 13:34:48 -07001778 RRectEllipseRendererBatch* that = t->cast<RRectEllipseRendererBatch>();
1779
1780 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
1781 that->bounds(), caps)) {
joshualitt8cab9a72015-07-16 09:13:50 -07001782 return false;
1783 }
1784
joshualitt76e7fb62015-02-11 08:52:27 -08001785 // TODO use vertex color to avoid breaking batches
1786 if (this->color() != that->color()) {
1787 return false;
1788 }
1789
1790 if (this->stroke() != that->stroke()) {
1791 return false;
1792 }
1793
1794 SkASSERT(this->usesLocalCoords() == that->usesLocalCoords());
1795 if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
1796 return false;
1797 }
1798
1799 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin());
joshualitt99c7c072015-05-01 13:43:30 -07001800 this->joinBounds(that->bounds());
joshualitt76e7fb62015-02-11 08:52:27 -08001801 return true;
1802 }
1803
1804 GrColor color() const { return fBatch.fColor; }
1805 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
1806 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; }
1807 bool stroke() const { return fBatch.fStroke; }
1808
1809 struct BatchTracker {
1810 GrColor fColor;
1811 bool fStroke;
1812 bool fUsesLocalCoords;
1813 bool fColorIgnored;
1814 bool fCoverageIgnored;
1815 };
1816
joshualitt76e7fb62015-02-11 08:52:27 -08001817 BatchTracker fBatch;
1818 SkSTArray<1, Geometry, true> fGeoData;
reed1b55a962015-09-17 20:16:13 -07001819
1820 typedef GrVertexBatch INHERITED;
joshualitt76e7fb62015-02-11 08:52:27 -08001821};
1822
bsalomonabd30f52015-08-13 13:34:48 -07001823static GrDrawBatch* create_rrect_batch(GrColor color,
1824 const SkMatrix& viewMatrix,
1825 const SkRRect& rrect,
1826 const SkStrokeRec& stroke) {
joshualitt3e708c52015-04-30 13:49:27 -07001827 SkASSERT(viewMatrix.rectStaysRect());
1828 SkASSERT(rrect.isSimple());
1829 SkASSERT(!rrect.isOval());
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001830
joshualitt3e708c52015-04-30 13:49:27 -07001831 // RRect batchs only handle simple, but not too simple, rrects
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001832 // do any matrix crunching before we reset the draw state for device coords
1833 const SkRect& rrectBounds = rrect.getBounds();
joshualittd96a67b2015-05-05 14:09:05 -07001834 SkRect bounds;
1835 viewMatrix.mapRect(&bounds, rrectBounds);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001836
1837 SkVector radii = rrect.getSimpleRadii();
joshualitt8059eb92014-12-29 15:10:07 -08001838 SkScalar xRadius = SkScalarAbs(viewMatrix[SkMatrix::kMScaleX]*radii.fX +
1839 viewMatrix[SkMatrix::kMSkewY]*radii.fY);
1840 SkScalar yRadius = SkScalarAbs(viewMatrix[SkMatrix::kMSkewX]*radii.fX +
1841 viewMatrix[SkMatrix::kMScaleY]*radii.fY);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001842
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001843 SkStrokeRec::Style style = stroke.getStyle();
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001844
1845 // do (potentially) anisotropic mapping of stroke
1846 SkVector scaledStroke;
1847 SkScalar strokeWidth = stroke.getWidth();
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001848
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001849 bool isStrokeOnly = SkStrokeRec::kStroke_Style == style ||
1850 SkStrokeRec::kHairline_Style == style;
1851 bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == style;
1852
1853 if (hasStroke) {
1854 if (SkStrokeRec::kHairline_Style == style) {
1855 scaledStroke.set(1, 1);
1856 } else {
joshualitt8059eb92014-12-29 15:10:07 -08001857 scaledStroke.fX = SkScalarAbs(strokeWidth*(viewMatrix[SkMatrix::kMScaleX] +
1858 viewMatrix[SkMatrix::kMSkewY]));
1859 scaledStroke.fY = SkScalarAbs(strokeWidth*(viewMatrix[SkMatrix::kMSkewX] +
1860 viewMatrix[SkMatrix::kMScaleY]));
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001861 }
1862
1863 // if half of strokewidth is greater than radius, we don't handle that right now
1864 if (SK_ScalarHalf*scaledStroke.fX > xRadius || SK_ScalarHalf*scaledStroke.fY > yRadius) {
halcanary96fcdcc2015-08-27 07:41:13 -07001865 return nullptr;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001866 }
1867 }
1868
1869 // The way the effect interpolates the offset-to-ellipse/circle-center attribute only works on
1870 // the interior of the rrect if the radii are >= 0.5. Otherwise, the inner rect of the nine-
1871 // patch will have fractional coverage. This only matters when the interior is actually filled.
1872 // We could consider falling back to rect rendering here, since a tiny radius is
1873 // indistinguishable from a square corner.
1874 if (!isStrokeOnly && (SK_ScalarHalf > xRadius || SK_ScalarHalf > yRadius)) {
halcanary96fcdcc2015-08-27 07:41:13 -07001875 return nullptr;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001876 }
1877
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001878 // if the corners are circles, use the circle renderer
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001879 if ((!hasStroke || scaledStroke.fX == scaledStroke.fY) && xRadius == yRadius) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001880 SkScalar innerRadius = 0.0f;
1881 SkScalar outerRadius = xRadius;
1882 SkScalar halfWidth = 0;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001883 if (hasStroke) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001884 if (SkScalarNearlyZero(scaledStroke.fX)) {
1885 halfWidth = SK_ScalarHalf;
1886 } else {
1887 halfWidth = SkScalarHalf(scaledStroke.fX);
1888 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001889
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001890 if (isStrokeOnly) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001891 innerRadius = xRadius - halfWidth;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001892 }
1893 outerRadius += halfWidth;
joshualittd96a67b2015-05-05 14:09:05 -07001894 bounds.outset(halfWidth, halfWidth);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001895 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001896
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001897 isStrokeOnly = (isStrokeOnly && innerRadius >= 0);
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +00001898
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001899 // The radii are outset for two reasons. First, it allows the shader to simply perform
bsalomonce1c8862014-12-15 07:11:22 -08001900 // simpler computation because the computed alpha is zero, rather than 50%, at the radius.
1901 // Second, the outer radius is used to compute the verts of the bounding box that is
1902 // rendered and the outset ensures the box will cover all partially covered by the rrect
1903 // corners.
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001904 outerRadius += SK_ScalarHalf;
1905 innerRadius -= SK_ScalarHalf;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001906
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001907 // Expand the rect so all the pixels will be captured.
joshualittd96a67b2015-05-05 14:09:05 -07001908 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001909
joshualitt76e7fb62015-02-11 08:52:27 -08001910 RRectCircleRendererBatch::Geometry geometry;
1911 geometry.fViewMatrix = viewMatrix;
1912 geometry.fColor = color;
1913 geometry.fInnerRadius = innerRadius;
1914 geometry.fOuterRadius = outerRadius;
1915 geometry.fStroke = isStrokeOnly;
joshualittd96a67b2015-05-05 14:09:05 -07001916 geometry.fDevBounds = bounds;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001917
bsalomoned0bcad2015-05-04 10:36:42 -07001918 return RRectCircleRendererBatch::Create(geometry);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001919 // otherwise we use the ellipse renderer
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001920 } else {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001921 SkScalar innerXRadius = 0.0f;
1922 SkScalar innerYRadius = 0.0f;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001923 if (hasStroke) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001924 if (SkScalarNearlyZero(scaledStroke.length())) {
1925 scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf);
1926 } else {
1927 scaledStroke.scale(SK_ScalarHalf);
1928 }
1929
1930 // we only handle thick strokes for near-circular ellipses
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +00001931 if (scaledStroke.length() > SK_ScalarHalf &&
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001932 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
halcanary96fcdcc2015-08-27 07:41:13 -07001933 return nullptr;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001934 }
1935
1936 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
1937 if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius ||
1938 scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) {
halcanary96fcdcc2015-08-27 07:41:13 -07001939 return nullptr;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001940 }
1941
1942 // this is legit only if scale & translation (which should be the case at the moment)
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001943 if (isStrokeOnly) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001944 innerXRadius = xRadius - scaledStroke.fX;
1945 innerYRadius = yRadius - scaledStroke.fY;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001946 }
1947
1948 xRadius += scaledStroke.fX;
1949 yRadius += scaledStroke.fY;
joshualittd96a67b2015-05-05 14:09:05 -07001950 bounds.outset(scaledStroke.fX, scaledStroke.fY);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001951 }
jvanverth@google.come3647412013-05-08 15:31:43 +00001952
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001953 isStrokeOnly = (isStrokeOnly && innerXRadius >= 0 && innerYRadius >= 0);
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +00001954
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001955 // Expand the rect so all the pixels will be captured.
joshualittd96a67b2015-05-05 14:09:05 -07001956 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001957
joshualitt76e7fb62015-02-11 08:52:27 -08001958 RRectEllipseRendererBatch::Geometry geometry;
1959 geometry.fViewMatrix = viewMatrix;
1960 geometry.fColor = color;
1961 geometry.fXRadius = xRadius;
1962 geometry.fYRadius = yRadius;
1963 geometry.fInnerXRadius = innerXRadius;
1964 geometry.fInnerYRadius = innerYRadius;
1965 geometry.fStroke = isStrokeOnly;
joshualittd96a67b2015-05-05 14:09:05 -07001966 geometry.fDevBounds = bounds;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001967
bsalomoned0bcad2015-05-04 10:36:42 -07001968 return RRectEllipseRendererBatch::Create(geometry);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001969 }
joshualitt3e708c52015-04-30 13:49:27 -07001970}
1971
robertphillipsea461502015-05-26 11:38:03 -07001972bool GrOvalRenderer::DrawRRect(GrDrawTarget* target,
joshualittae3d63a2015-07-13 08:44:06 -07001973 const GrPipelineBuilder& pipelineBuilder,
joshualitt3e708c52015-04-30 13:49:27 -07001974 GrColor color,
1975 const SkMatrix& viewMatrix,
1976 bool useAA,
1977 const SkRRect& rrect,
1978 const SkStrokeRec& stroke) {
1979 if (rrect.isOval()) {
robertphillipsea461502015-05-26 11:38:03 -07001980 return DrawOval(target, pipelineBuilder, color, viewMatrix, useAA, rrect.getBounds(),
1981 stroke);
joshualitt3e708c52015-04-30 13:49:27 -07001982 }
1983
joshualittae3d63a2015-07-13 08:44:06 -07001984 bool useCoverageAA = useAA && !pipelineBuilder.getRenderTarget()->isUnifiedMultisampled();
joshualitt3e708c52015-04-30 13:49:27 -07001985
1986 // only anti-aliased rrects for now
1987 if (!useCoverageAA) {
1988 return false;
1989 }
1990
1991 if (!viewMatrix.rectStaysRect() || !rrect.isSimple()) {
1992 return false;
1993 }
1994
bsalomonabd30f52015-08-13 13:34:48 -07001995 SkAutoTUnref<GrDrawBatch> batch(create_rrect_batch(color, viewMatrix, rrect, stroke));
joshualitt3e708c52015-04-30 13:49:27 -07001996 if (!batch) {
1997 return false;
1998 }
1999
joshualittae3d63a2015-07-13 08:44:06 -07002000 target->drawBatch(pipelineBuilder, batch);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00002001 return true;
2002}
joshualitt3e708c52015-04-30 13:49:27 -07002003
2004///////////////////////////////////////////////////////////////////////////////////////////////////
2005
2006#ifdef GR_TEST_UTILS
2007
bsalomonabd30f52015-08-13 13:34:48 -07002008DRAW_BATCH_TEST_DEFINE(CircleBatch) {
joshualitt3e708c52015-04-30 13:49:27 -07002009 SkMatrix viewMatrix = GrTest::TestMatrix(random);
2010 GrColor color = GrRandomColor(random);
2011 bool useCoverageAA = random->nextBool();
joshualitt6c891102015-05-13 08:51:49 -07002012 SkRect circle = GrTest::TestSquare(random);
joshualitt21279c72015-05-11 07:21:37 -07002013 return create_circle_batch(color, viewMatrix, useCoverageAA, circle,
2014 GrTest::TestStrokeRec(random));
joshualitt3e708c52015-04-30 13:49:27 -07002015}
2016
bsalomonabd30f52015-08-13 13:34:48 -07002017DRAW_BATCH_TEST_DEFINE(EllipseBatch) {
joshualitt3e708c52015-04-30 13:49:27 -07002018 SkMatrix viewMatrix = GrTest::TestMatrixRectStaysRect(random);
2019 GrColor color = GrRandomColor(random);
joshualitt6c891102015-05-13 08:51:49 -07002020 SkRect ellipse = GrTest::TestSquare(random);
2021 return create_ellipse_batch(color, viewMatrix, true, ellipse,
joshualitt21279c72015-05-11 07:21:37 -07002022 GrTest::TestStrokeRec(random));
joshualitt3e708c52015-04-30 13:49:27 -07002023}
2024
bsalomonabd30f52015-08-13 13:34:48 -07002025DRAW_BATCH_TEST_DEFINE(DIEllipseBatch) {
joshualitt3e708c52015-04-30 13:49:27 -07002026 SkMatrix viewMatrix = GrTest::TestMatrix(random);
2027 GrColor color = GrRandomColor(random);
2028 bool useCoverageAA = random->nextBool();
joshualitt6c891102015-05-13 08:51:49 -07002029 SkRect ellipse = GrTest::TestSquare(random);
joshualitt3e708c52015-04-30 13:49:27 -07002030 return create_diellipse_batch(color, viewMatrix, useCoverageAA, ellipse,
joshualitt21279c72015-05-11 07:21:37 -07002031 GrTest::TestStrokeRec(random));
joshualitt3e708c52015-04-30 13:49:27 -07002032}
2033
bsalomonabd30f52015-08-13 13:34:48 -07002034DRAW_BATCH_TEST_DEFINE(RRectBatch) {
joshualitt3e708c52015-04-30 13:49:27 -07002035 SkMatrix viewMatrix = GrTest::TestMatrixRectStaysRect(random);
2036 GrColor color = GrRandomColor(random);
2037 const SkRRect& rrect = GrTest::TestRRectSimple(random);
joshualitt21279c72015-05-11 07:21:37 -07002038 return create_rrect_batch(color, viewMatrix, rrect, GrTest::TestStrokeRec(random));
joshualitt3e708c52015-04-30 13:49:27 -07002039}
2040
2041#endif