blob: f5dcfcb0f5ad51c10b81ddfa5e52254b8e5627d7 [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"
joshualitteb2a6762014-12-04 11:35:33 -080012#include "GrGeometryProcessor.h"
egdaniel605dd0f2014-11-12 08:35:25 -080013#include "GrInvariantOutput.h"
joshualitt76e7fb62015-02-11 08:52:27 -080014#include "GrProcessor.h"
bsalomoned0bcad2015-05-04 10:36:42 -070015#include "GrResourceProvider.h"
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +000016#include "SkRRect.h"
commit-bot@chromium.org81312832013-03-22 18:34:09 +000017#include "SkStrokeRec.h"
bsalomon16b99132015-08-13 14:55:50 -070018#include "batches/GrVertexBatch.h"
egdaniel2d721d32015-11-11 13:06:05 -080019#include "glsl/GrGLSLFragmentShaderBuilder.h"
egdaniele659a582015-11-13 09:55:43 -080020#include "glsl/GrGLSLGeometryProcessor.h"
egdaniel018fb622015-10-28 07:26:40 -070021#include "glsl/GrGLSLProgramDataManager.h"
egdaniel0eafe792015-11-20 14:01:22 -080022#include "glsl/GrGLSLVarying.h"
egdaniel2d721d32015-11-11 13:06:05 -080023#include "glsl/GrGLSLVertexShaderBuilder.h"
egdaniel7ea439b2015-12-03 09:20:44 -080024#include "glsl/GrGLSLUniformHandler.h"
egdaniel64c47282015-11-13 06:54:19 -080025#include "glsl/GrGLSLUtil.h"
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +000026
joshualitt76e7fb62015-02-11 08:52:27 -080027// TODO(joshualitt) - Break this file up during GrBatch post implementation cleanup
28
commit-bot@chromium.org81312832013-03-22 18:34:09 +000029namespace {
brianosmanbb2ff942016-02-11 14:15:18 -080030
commit-bot@chromium.org81312832013-03-22 18:34:09 +000031struct CircleVertex {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000032 SkPoint fPos;
brianosmanbb2ff942016-02-11 14:15:18 -080033 GrColor fColor;
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000034 SkPoint fOffset;
commit-bot@chromium.org81312832013-03-22 18:34:09 +000035 SkScalar fOuterRadius;
36 SkScalar fInnerRadius;
37};
38
39struct EllipseVertex {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000040 SkPoint fPos;
brianosmanbb2ff942016-02-11 14:15:18 -080041 GrColor fColor;
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000042 SkPoint fOffset;
43 SkPoint fOuterRadii;
44 SkPoint fInnerRadii;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +000045};
46
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +000047struct DIEllipseVertex {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000048 SkPoint fPos;
brianosmanbb2ff942016-02-11 14:15:18 -080049 GrColor fColor;
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000050 SkPoint fOuterOffset;
51 SkPoint fInnerOffset;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +000052};
53
commit-bot@chromium.org81312832013-03-22 18:34:09 +000054inline bool circle_stays_circle(const SkMatrix& m) {
55 return m.isSimilarity();
56}
57
58}
59
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000060///////////////////////////////////////////////////////////////////////////////
61
62/**
bsalomonce1c8862014-12-15 07:11:22 -080063 * The output of this effect is a modulation of the input color and coverage for a circle. It
64 * operates in a space normalized by the circle radius (outer radius in the case of a stroke)
bsalomoncdaa97b2016-03-08 08:30:14 -080065 * with origin at the circle center. Three vertex attributes are used:
bsalomonce1c8862014-12-15 07:11:22 -080066 * vec2f : position in device space of the bounding geometry vertices
bsalomoncdaa97b2016-03-08 08:30:14 -080067 * vec4ub: color
bsalomonce1c8862014-12-15 07:11:22 -080068 * 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
bsalomoncdaa97b2016-03-08 08:30:14 -080074class CircleGeometryProcessor : public GrGeometryProcessor {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000075public:
bsalomoncdaa97b2016-03-08 08:30:14 -080076 CircleGeometryProcessor(bool stroke, const SkMatrix& localMatrix) : fLocalMatrix(localMatrix){
77 this->initClassID<CircleGeometryProcessor>();
78 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType,
79 kHigh_GrSLPrecision));
80 fInColor = &this->addVertexAttrib(Attribute("inColor", kVec4ub_GrVertexAttribType));
81 fInCircleEdge = &this->addVertexAttrib(Attribute("inCircleEdge",
82 kVec4f_GrVertexAttribType));
83 fStroke = stroke;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000084 }
85
joshualitt71c92602015-01-14 08:12:47 -080086 const Attribute* inPosition() const { return fInPosition; }
brianosmanbb2ff942016-02-11 14:15:18 -080087 const Attribute* inColor() const { return fInColor; }
joshualitt71c92602015-01-14 08:12:47 -080088 const Attribute* inCircleEdge() const { return fInCircleEdge; }
joshualitte3ababe2015-05-15 07:56:07 -070089 const SkMatrix& localMatrix() const { return fLocalMatrix; }
bsalomoncdaa97b2016-03-08 08:30:14 -080090 virtual ~CircleGeometryProcessor() {}
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000091
mtklein36352bf2015-03-25 18:17:31 -070092 const char* name() const override { return "CircleEdge"; }
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000093
egdaniel57d3b032015-11-13 11:57:27 -080094 class GLSLProcessor : public GrGLSLGeometryProcessor {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000095 public:
brianosmanbb2ff942016-02-11 14:15:18 -080096 GLSLProcessor() {}
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000097
mtklein36352bf2015-03-25 18:17:31 -070098 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{
bsalomoncdaa97b2016-03-08 08:30:14 -080099 const CircleGeometryProcessor& cgp = args.fGP.cast<CircleGeometryProcessor>();
egdaniel4ca2e602015-11-18 08:01:26 -0800100 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
egdaniel0eafe792015-11-20 14:01:22 -0800101 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
egdaniel7ea439b2015-12-03 09:20:44 -0800102 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
joshualitt2dd1ae02014-12-03 06:24:10 -0800103
joshualittabb52a12015-01-13 15:02:10 -0800104 // emit attributes
bsalomoncdaa97b2016-03-08 08:30:14 -0800105 varyingHandler->emitAttributes(cgp);
joshualittabb52a12015-01-13 15:02:10 -0800106
egdaniel8dcdedc2015-11-11 06:27:20 -0800107 GrGLSLVertToFrag v(kVec4f_GrSLType);
egdaniel0eafe792015-11-20 14:01:22 -0800108 varyingHandler->addVarying("CircleEdge", &v);
bsalomoncdaa97b2016-03-08 08:30:14 -0800109 vertBuilder->codeAppendf("%s = %s;", v.vsOut(), cgp.inCircleEdge()->fName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000110
cdalton85285412016-02-18 12:37:07 -0800111 GrGLSLPPFragmentBuilder* fragBuilder = args.fFragBuilder;
joshualittb8c241a2015-05-19 08:23:30 -0700112 // setup pass through color
bsalomoncdaa97b2016-03-08 08:30:14 -0800113 varyingHandler->addPassThroughAttribute(cgp.inColor(), args.fOutputColor);
joshualitt9b989322014-12-15 14:16:27 -0800114
joshualittabb52a12015-01-13 15:02:10 -0800115 // Setup position
bsalomoncdaa97b2016-03-08 08:30:14 -0800116 this->setupPosition(vertBuilder, gpArgs, cgp.inPosition()->fName);
joshualittabb52a12015-01-13 15:02:10 -0800117
118 // emit transforms
egdaniel7ea439b2015-12-03 09:20:44 -0800119 this->emitTransforms(vertBuilder,
egdaniel0eafe792015-11-20 14:01:22 -0800120 varyingHandler,
egdaniel7ea439b2015-12-03 09:20:44 -0800121 uniformHandler,
egdaniel4ca2e602015-11-18 08:01:26 -0800122 gpArgs->fPositionVar,
bsalomoncdaa97b2016-03-08 08:30:14 -0800123 cgp.inPosition()->fName,
124 cgp.localMatrix(),
egdaniel4ca2e602015-11-18 08:01:26 -0800125 args.fTransformsIn,
126 args.fTransformsOut);
joshualitt4973d9d2014-11-08 09:24:25 -0800127
egdaniel4ca2e602015-11-18 08:01:26 -0800128 fragBuilder->codeAppendf("float d = length(%s.xy);", v.fsIn());
129 fragBuilder->codeAppendf("float edgeAlpha = clamp(%s.z * (1.0 - d), 0.0, 1.0);",
130 v.fsIn());
bsalomoncdaa97b2016-03-08 08:30:14 -0800131 if (cgp.fStroke) {
egdaniel4ca2e602015-11-18 08:01:26 -0800132 fragBuilder->codeAppendf("float innerAlpha = clamp(%s.z * (d - %s.w), 0.0, 1.0);",
133 v.fsIn(), v.fsIn());
134 fragBuilder->codeAppend("edgeAlpha *= innerAlpha;");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000135 }
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000136
egdaniel4ca2e602015-11-18 08:01:26 -0800137 fragBuilder->codeAppendf("%s = vec4(edgeAlpha);", args.fOutputCoverage);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000138 }
139
robertphillips46d36f02015-01-18 08:14:14 -0800140 static void GenKey(const GrGeometryProcessor& gp,
jvanverthcfc18862015-04-28 08:48:20 -0700141 const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700142 GrProcessorKeyBuilder* b) {
bsalomoncdaa97b2016-03-08 08:30:14 -0800143 const CircleGeometryProcessor& cgp = gp.cast<CircleGeometryProcessor>();
144 uint16_t key = cgp.fStroke ? 0x1 : 0x0;
145 key |= cgp.localMatrix().hasPerspective() ? 0x2 : 0x0;
joshualittb8c241a2015-05-19 08:23:30 -0700146 b->add32(key);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000147 }
148
egdaniel018fb622015-10-28 07:26:40 -0700149 void setData(const GrGLSLProgramDataManager& pdman,
150 const GrPrimitiveProcessor& gp) override {
joshualitt9b989322014-12-15 14:16:27 -0800151 }
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000152
joshualitte3ababe2015-05-15 07:56:07 -0700153 void setTransformData(const GrPrimitiveProcessor& primProc,
egdaniel018fb622015-10-28 07:26:40 -0700154 const GrGLSLProgramDataManager& pdman,
joshualitte3ababe2015-05-15 07:56:07 -0700155 int index,
156 const SkTArray<const GrCoordTransform*, true>& transforms) override {
bsalomoncdaa97b2016-03-08 08:30:14 -0800157 this->setTransformDataHelper<CircleGeometryProcessor>(primProc, pdman, index,
158 transforms);
joshualitte3ababe2015-05-15 07:56:07 -0700159 }
160
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000161 private:
egdaniele659a582015-11-13 09:55:43 -0800162 typedef GrGLSLGeometryProcessor INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000163 };
164
egdaniel57d3b032015-11-13 11:57:27 -0800165 void getGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override {
166 GLSLProcessor::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800167 }
168
egdaniel57d3b032015-11-13 11:57:27 -0800169 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrGLSLCaps&) const override {
170 return new GLSLProcessor();
joshualitteb2a6762014-12-04 11:35:33 -0800171 }
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000172
173private:
bsalomoncdaa97b2016-03-08 08:30:14 -0800174 SkMatrix fLocalMatrix;
joshualitt71c92602015-01-14 08:12:47 -0800175 const Attribute* fInPosition;
brianosmanbb2ff942016-02-11 14:15:18 -0800176 const Attribute* fInColor;
joshualitt71c92602015-01-14 08:12:47 -0800177 const Attribute* fInCircleEdge;
bsalomoncdaa97b2016-03-08 08:30:14 -0800178 bool fStroke;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000179
joshualittb0a8a372014-09-23 09:50:21 -0700180 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000181
joshualitt249af152014-09-15 11:41:13 -0700182 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000183};
184
bsalomoncdaa97b2016-03-08 08:30:14 -0800185GR_DEFINE_GEOMETRY_PROCESSOR_TEST(CircleGeometryProcessor);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000186
bungeman06ca8ec2016-06-09 08:01:03 -0700187sk_sp<GrGeometryProcessor> CircleGeometryProcessor::TestCreate(GrProcessorTestData* d) {
188 return sk_sp<GrGeometryProcessor>(
189 new CircleGeometryProcessor(d->fRandom->nextBool(), GrTest::TestMatrix(d->fRandom)));
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000190}
191
192///////////////////////////////////////////////////////////////////////////////
193
194/**
195 * 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 +0000196 * ellipse, specified as a 2D offset from center, and the reciprocals of the outer and inner radii,
197 * in both x and y directions.
198 *
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000199 * 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 +0000200 */
201
bsalomoncdaa97b2016-03-08 08:30:14 -0800202class EllipseGeometryProcessor : public GrGeometryProcessor {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000203public:
bsalomoncdaa97b2016-03-08 08:30:14 -0800204 EllipseGeometryProcessor(bool stroke, const SkMatrix& localMatrix)
205 : fLocalMatrix(localMatrix) {
206 this->initClassID<EllipseGeometryProcessor>();
207 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType));
208 fInColor = &this->addVertexAttrib(Attribute("inColor", kVec4ub_GrVertexAttribType));
209 fInEllipseOffset = &this->addVertexAttrib(Attribute("inEllipseOffset",
210 kVec2f_GrVertexAttribType));
211 fInEllipseRadii = &this->addVertexAttrib(Attribute("inEllipseRadii",
212 kVec4f_GrVertexAttribType));
213 fStroke = stroke;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000214 }
215
bsalomoncdaa97b2016-03-08 08:30:14 -0800216 virtual ~EllipseGeometryProcessor() {}
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000217
mtklein36352bf2015-03-25 18:17:31 -0700218 const char* name() const override { return "EllipseEdge"; }
joshualitt2dd1ae02014-12-03 06:24:10 -0800219
joshualitt71c92602015-01-14 08:12:47 -0800220 const Attribute* inPosition() const { return fInPosition; }
brianosmanbb2ff942016-02-11 14:15:18 -0800221 const Attribute* inColor() const { return fInColor; }
joshualitt71c92602015-01-14 08:12:47 -0800222 const Attribute* inEllipseOffset() const { return fInEllipseOffset; }
223 const Attribute* inEllipseRadii() const { return fInEllipseRadii; }
joshualitte3ababe2015-05-15 07:56:07 -0700224 const SkMatrix& localMatrix() const { return fLocalMatrix; }
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000225
egdaniel57d3b032015-11-13 11:57:27 -0800226 class GLSLProcessor : public GrGLSLGeometryProcessor {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000227 public:
brianosmanbb2ff942016-02-11 14:15:18 -0800228 GLSLProcessor() {}
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000229
mtklein36352bf2015-03-25 18:17:31 -0700230 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{
bsalomoncdaa97b2016-03-08 08:30:14 -0800231 const EllipseGeometryProcessor& egp = args.fGP.cast<EllipseGeometryProcessor>();
egdaniel4ca2e602015-11-18 08:01:26 -0800232 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
egdaniel0eafe792015-11-20 14:01:22 -0800233 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
egdaniel7ea439b2015-12-03 09:20:44 -0800234 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000235
joshualittabb52a12015-01-13 15:02:10 -0800236 // emit attributes
bsalomoncdaa97b2016-03-08 08:30:14 -0800237 varyingHandler->emitAttributes(egp);
joshualittabb52a12015-01-13 15:02:10 -0800238
egdaniel8dcdedc2015-11-11 06:27:20 -0800239 GrGLSLVertToFrag ellipseOffsets(kVec2f_GrSLType);
egdaniel0eafe792015-11-20 14:01:22 -0800240 varyingHandler->addVarying("EllipseOffsets", &ellipseOffsets);
egdaniel4ca2e602015-11-18 08:01:26 -0800241 vertBuilder->codeAppendf("%s = %s;", ellipseOffsets.vsOut(),
bsalomoncdaa97b2016-03-08 08:30:14 -0800242 egp.inEllipseOffset()->fName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000243
egdaniel8dcdedc2015-11-11 06:27:20 -0800244 GrGLSLVertToFrag ellipseRadii(kVec4f_GrSLType);
egdaniel0eafe792015-11-20 14:01:22 -0800245 varyingHandler->addVarying("EllipseRadii", &ellipseRadii);
egdaniel4ca2e602015-11-18 08:01:26 -0800246 vertBuilder->codeAppendf("%s = %s;", ellipseRadii.vsOut(),
bsalomoncdaa97b2016-03-08 08:30:14 -0800247 egp.inEllipseRadii()->fName);
joshualitt2dd1ae02014-12-03 06:24:10 -0800248
cdalton85285412016-02-18 12:37:07 -0800249 GrGLSLPPFragmentBuilder* fragBuilder = args.fFragBuilder;
joshualittb8c241a2015-05-19 08:23:30 -0700250 // setup pass through color
bsalomoncdaa97b2016-03-08 08:30:14 -0800251 varyingHandler->addPassThroughAttribute(egp.inColor(), args.fOutputColor);
joshualitt9b989322014-12-15 14:16:27 -0800252
joshualittabb52a12015-01-13 15:02:10 -0800253 // Setup position
bsalomoncdaa97b2016-03-08 08:30:14 -0800254 this->setupPosition(vertBuilder, gpArgs, egp.inPosition()->fName);
joshualittabb52a12015-01-13 15:02:10 -0800255
256 // emit transforms
egdaniel7ea439b2015-12-03 09:20:44 -0800257 this->emitTransforms(vertBuilder,
egdaniel0eafe792015-11-20 14:01:22 -0800258 varyingHandler,
egdaniel7ea439b2015-12-03 09:20:44 -0800259 uniformHandler,
egdaniel4ca2e602015-11-18 08:01:26 -0800260 gpArgs->fPositionVar,
bsalomoncdaa97b2016-03-08 08:30:14 -0800261 egp.inPosition()->fName,
262 egp.localMatrix(),
egdaniel4ca2e602015-11-18 08:01:26 -0800263 args.fTransformsIn,
264 args.fTransformsOut);
joshualitt4973d9d2014-11-08 09:24:25 -0800265
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +0000266 // for outer curve
egdaniel4ca2e602015-11-18 08:01:26 -0800267 fragBuilder->codeAppendf("vec2 scaledOffset = %s*%s.xy;", ellipseOffsets.fsIn(),
268 ellipseRadii.fsIn());
269 fragBuilder->codeAppend("float test = dot(scaledOffset, scaledOffset) - 1.0;");
270 fragBuilder->codeAppendf("vec2 grad = 2.0*scaledOffset*%s.xy;", ellipseRadii.fsIn());
271 fragBuilder->codeAppend("float grad_dot = dot(grad, grad);");
joshualitt74077b92014-10-24 11:26:03 -0700272
commit-bot@chromium.org1b035d82014-04-09 17:11:09 +0000273 // avoid calling inversesqrt on zero.
egdaniel4ca2e602015-11-18 08:01:26 -0800274 fragBuilder->codeAppend("grad_dot = max(grad_dot, 1.0e-4);");
275 fragBuilder->codeAppend("float invlen = inversesqrt(grad_dot);");
brianosmanc6052ac2016-02-12 10:20:00 -0800276 fragBuilder->codeAppend("float edgeAlpha = clamp(0.5-test*invlen, 0.0, 1.0);");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000277
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000278 // for inner curve
bsalomoncdaa97b2016-03-08 08:30:14 -0800279 if (egp.fStroke) {
egdaniel4ca2e602015-11-18 08:01:26 -0800280 fragBuilder->codeAppendf("scaledOffset = %s*%s.zw;",
281 ellipseOffsets.fsIn(), ellipseRadii.fsIn());
282 fragBuilder->codeAppend("test = dot(scaledOffset, scaledOffset) - 1.0;");
283 fragBuilder->codeAppendf("grad = 2.0*scaledOffset*%s.zw;",
284 ellipseRadii.fsIn());
285 fragBuilder->codeAppend("invlen = inversesqrt(dot(grad, grad));");
286 fragBuilder->codeAppend("edgeAlpha *= clamp(0.5+test*invlen, 0.0, 1.0);");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000287 }
288
egdaniel4ca2e602015-11-18 08:01:26 -0800289 fragBuilder->codeAppendf("%s = vec4(edgeAlpha);", args.fOutputCoverage);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000290 }
291
robertphillips46d36f02015-01-18 08:14:14 -0800292 static void GenKey(const GrGeometryProcessor& gp,
jvanverthcfc18862015-04-28 08:48:20 -0700293 const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700294 GrProcessorKeyBuilder* b) {
bsalomoncdaa97b2016-03-08 08:30:14 -0800295 const EllipseGeometryProcessor& egp = gp.cast<EllipseGeometryProcessor>();
296 uint16_t key = egp.fStroke ? 0x1 : 0x0;
297 key |= egp.localMatrix().hasPerspective() ? 0x2 : 0x0;
joshualittb8c241a2015-05-19 08:23:30 -0700298 b->add32(key);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000299 }
300
egdaniel018fb622015-10-28 07:26:40 -0700301 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& gp) override {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000302 }
303
joshualitte3ababe2015-05-15 07:56:07 -0700304 void setTransformData(const GrPrimitiveProcessor& primProc,
egdaniel018fb622015-10-28 07:26:40 -0700305 const GrGLSLProgramDataManager& pdman,
joshualitte3ababe2015-05-15 07:56:07 -0700306 int index,
307 const SkTArray<const GrCoordTransform*, true>& transforms) override {
bsalomoncdaa97b2016-03-08 08:30:14 -0800308 this->setTransformDataHelper<EllipseGeometryProcessor>(primProc, pdman, index,
309 transforms);
joshualitte3ababe2015-05-15 07:56:07 -0700310 }
311
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000312 private:
egdaniele659a582015-11-13 09:55:43 -0800313 typedef GrGLSLGeometryProcessor INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000314 };
315
egdaniel57d3b032015-11-13 11:57:27 -0800316 void getGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override {
317 GLSLProcessor::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800318 }
319
egdaniel57d3b032015-11-13 11:57:27 -0800320 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrGLSLCaps&) const override {
321 return new GLSLProcessor();
joshualitteb2a6762014-12-04 11:35:33 -0800322 }
323
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000324private:
joshualitt71c92602015-01-14 08:12:47 -0800325 const Attribute* fInPosition;
brianosmanbb2ff942016-02-11 14:15:18 -0800326 const Attribute* fInColor;
joshualitt71c92602015-01-14 08:12:47 -0800327 const Attribute* fInEllipseOffset;
328 const Attribute* fInEllipseRadii;
joshualitte3ababe2015-05-15 07:56:07 -0700329 SkMatrix fLocalMatrix;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000330 bool fStroke;
331
joshualittb0a8a372014-09-23 09:50:21 -0700332 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000333
joshualitt249af152014-09-15 11:41:13 -0700334 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000335};
336
bsalomoncdaa97b2016-03-08 08:30:14 -0800337GR_DEFINE_GEOMETRY_PROCESSOR_TEST(EllipseGeometryProcessor);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000338
bungeman06ca8ec2016-06-09 08:01:03 -0700339sk_sp<GrGeometryProcessor> EllipseGeometryProcessor::TestCreate(GrProcessorTestData* d) {
340 return sk_sp<GrGeometryProcessor>(
341 new EllipseGeometryProcessor(d->fRandom->nextBool(), GrTest::TestMatrix(d->fRandom)));
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000342}
343
344///////////////////////////////////////////////////////////////////////////////
345
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000346/**
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000347 * 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 +0000348 * specified as a 2D offset from center for both the outer and inner paths (if stroked). The
349 * implict equation used is for a unit circle (x^2 + y^2 - 1 = 0) and the edge corrected by
350 * using differentials.
351 *
352 * The result is device-independent and can be used with any affine matrix.
353 */
354
bsalomoncdaa97b2016-03-08 08:30:14 -0800355enum class DIEllipseStyle { kStroke = 0, kHairline, kFill };
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000356
bsalomoncdaa97b2016-03-08 08:30:14 -0800357class DIEllipseGeometryProcessor : public GrGeometryProcessor {
358public:
359 DIEllipseGeometryProcessor(const SkMatrix& viewMatrix, DIEllipseStyle style)
360 : fViewMatrix(viewMatrix) {
361 this->initClassID<DIEllipseGeometryProcessor>();
362 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType,
363 kHigh_GrSLPrecision));
364 fInColor = &this->addVertexAttrib(Attribute("inColor", kVec4ub_GrVertexAttribType));
365 fInEllipseOffsets0 = &this->addVertexAttrib(Attribute("inEllipseOffsets0",
366 kVec2f_GrVertexAttribType));
367 fInEllipseOffsets1 = &this->addVertexAttrib(Attribute("inEllipseOffsets1",
368 kVec2f_GrVertexAttribType));
369 fStyle = style;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000370 }
371
bsalomoncdaa97b2016-03-08 08:30:14 -0800372
373 virtual ~DIEllipseGeometryProcessor() {}
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000374
mtklein36352bf2015-03-25 18:17:31 -0700375 const char* name() const override { return "DIEllipseEdge"; }
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000376
joshualitt71c92602015-01-14 08:12:47 -0800377 const Attribute* inPosition() const { return fInPosition; }
brianosmanbb2ff942016-02-11 14:15:18 -0800378 const Attribute* inColor() const { return fInColor; }
joshualitt71c92602015-01-14 08:12:47 -0800379 const Attribute* inEllipseOffsets0() const { return fInEllipseOffsets0; }
380 const Attribute* inEllipseOffsets1() const { return fInEllipseOffsets1; }
joshualitte578a952015-05-14 10:09:13 -0700381 const SkMatrix& viewMatrix() const { return fViewMatrix; }
halcanary9d524f22016-03-29 09:03:52 -0700382
egdaniel57d3b032015-11-13 11:57:27 -0800383 class GLSLProcessor : public GrGLSLGeometryProcessor {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000384 public:
egdaniel57d3b032015-11-13 11:57:27 -0800385 GLSLProcessor()
brianosmanbb2ff942016-02-11 14:15:18 -0800386 : fViewMatrix(SkMatrix::InvalidMatrix()) {}
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000387
joshualitt465283c2015-09-11 08:19:35 -0700388 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
bsalomoncdaa97b2016-03-08 08:30:14 -0800389 const DIEllipseGeometryProcessor& diegp = args.fGP.cast<DIEllipseGeometryProcessor>();
egdaniel4ca2e602015-11-18 08:01:26 -0800390 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
egdaniel0eafe792015-11-20 14:01:22 -0800391 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
egdaniel7ea439b2015-12-03 09:20:44 -0800392 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000393
joshualittabb52a12015-01-13 15:02:10 -0800394 // emit attributes
bsalomoncdaa97b2016-03-08 08:30:14 -0800395 varyingHandler->emitAttributes(diegp);
joshualittabb52a12015-01-13 15:02:10 -0800396
egdaniel8dcdedc2015-11-11 06:27:20 -0800397 GrGLSLVertToFrag offsets0(kVec2f_GrSLType);
egdaniel0eafe792015-11-20 14:01:22 -0800398 varyingHandler->addVarying("EllipseOffsets0", &offsets0);
egdaniel4ca2e602015-11-18 08:01:26 -0800399 vertBuilder->codeAppendf("%s = %s;", offsets0.vsOut(),
bsalomoncdaa97b2016-03-08 08:30:14 -0800400 diegp.inEllipseOffsets0()->fName);
joshualitt74077b92014-10-24 11:26:03 -0700401
egdaniel8dcdedc2015-11-11 06:27:20 -0800402 GrGLSLVertToFrag offsets1(kVec2f_GrSLType);
egdaniel0eafe792015-11-20 14:01:22 -0800403 varyingHandler->addVarying("EllipseOffsets1", &offsets1);
egdaniel4ca2e602015-11-18 08:01:26 -0800404 vertBuilder->codeAppendf("%s = %s;", offsets1.vsOut(),
bsalomoncdaa97b2016-03-08 08:30:14 -0800405 diegp.inEllipseOffsets1()->fName);
joshualitt2dd1ae02014-12-03 06:24:10 -0800406
cdalton85285412016-02-18 12:37:07 -0800407 GrGLSLPPFragmentBuilder* fragBuilder = args.fFragBuilder;
bsalomoncdaa97b2016-03-08 08:30:14 -0800408 varyingHandler->addPassThroughAttribute(diegp.inColor(), args.fOutputColor);
joshualitt9b989322014-12-15 14:16:27 -0800409
joshualittabb52a12015-01-13 15:02:10 -0800410 // Setup position
egdaniel7ea439b2015-12-03 09:20:44 -0800411 this->setupPosition(vertBuilder,
412 uniformHandler,
egdaniel4ca2e602015-11-18 08:01:26 -0800413 gpArgs,
bsalomoncdaa97b2016-03-08 08:30:14 -0800414 diegp.inPosition()->fName,
415 diegp.viewMatrix(),
joshualitt5559ca22015-05-21 15:50:36 -0700416 &fViewMatrixUniform);
joshualittabb52a12015-01-13 15:02:10 -0800417
418 // emit transforms
egdaniel7ea439b2015-12-03 09:20:44 -0800419 this->emitTransforms(vertBuilder,
egdaniel0eafe792015-11-20 14:01:22 -0800420 varyingHandler,
egdaniel7ea439b2015-12-03 09:20:44 -0800421 uniformHandler,
egdaniel4ca2e602015-11-18 08:01:26 -0800422 gpArgs->fPositionVar,
bsalomoncdaa97b2016-03-08 08:30:14 -0800423 diegp.inPosition()->fName,
egdaniel4ca2e602015-11-18 08:01:26 -0800424 args.fTransformsIn,
425 args.fTransformsOut);
joshualitt4973d9d2014-11-08 09:24:25 -0800426
egdaniel4ca2e602015-11-18 08:01:26 -0800427 SkAssertResult(fragBuilder->enableFeature(
egdaniel2d721d32015-11-11 13:06:05 -0800428 GrGLSLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature));
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000429 // for outer curve
egdaniel4ca2e602015-11-18 08:01:26 -0800430 fragBuilder->codeAppendf("vec2 scaledOffset = %s.xy;", offsets0.fsIn());
431 fragBuilder->codeAppend("float test = dot(scaledOffset, scaledOffset) - 1.0;");
432 fragBuilder->codeAppendf("vec2 duvdx = dFdx(%s);", offsets0.fsIn());
433 fragBuilder->codeAppendf("vec2 duvdy = dFdy(%s);", offsets0.fsIn());
434 fragBuilder->codeAppendf("vec2 grad = vec2(2.0*%s.x*duvdx.x + 2.0*%s.y*duvdx.y,"
435 " 2.0*%s.x*duvdy.x + 2.0*%s.y*duvdy.y);",
bsalomoncdaa97b2016-03-08 08:30:14 -0800436 offsets0.fsIn(), offsets0.fsIn(), offsets0.fsIn(),
437 offsets0.fsIn());
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000438
egdaniel4ca2e602015-11-18 08:01:26 -0800439 fragBuilder->codeAppend("float grad_dot = dot(grad, grad);");
commit-bot@chromium.org1b035d82014-04-09 17:11:09 +0000440 // avoid calling inversesqrt on zero.
egdaniel4ca2e602015-11-18 08:01:26 -0800441 fragBuilder->codeAppend("grad_dot = max(grad_dot, 1.0e-4);");
442 fragBuilder->codeAppend("float invlen = inversesqrt(grad_dot);");
bsalomoncdaa97b2016-03-08 08:30:14 -0800443 if (DIEllipseStyle::kHairline == diegp.fStyle) {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000444 // can probably do this with one step
egdaniel4ca2e602015-11-18 08:01:26 -0800445 fragBuilder->codeAppend("float edgeAlpha = clamp(1.0-test*invlen, 0.0, 1.0);");
446 fragBuilder->codeAppend("edgeAlpha *= clamp(1.0+test*invlen, 0.0, 1.0);");
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000447 } else {
egdaniel4ca2e602015-11-18 08:01:26 -0800448 fragBuilder->codeAppend("float edgeAlpha = clamp(0.5-test*invlen, 0.0, 1.0);");
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000449 }
450
451 // for inner curve
bsalomoncdaa97b2016-03-08 08:30:14 -0800452 if (DIEllipseStyle::kStroke == diegp.fStyle) {
egdaniel4ca2e602015-11-18 08:01:26 -0800453 fragBuilder->codeAppendf("scaledOffset = %s.xy;", offsets1.fsIn());
454 fragBuilder->codeAppend("test = dot(scaledOffset, scaledOffset) - 1.0;");
455 fragBuilder->codeAppendf("duvdx = dFdx(%s);", offsets1.fsIn());
456 fragBuilder->codeAppendf("duvdy = dFdy(%s);", offsets1.fsIn());
457 fragBuilder->codeAppendf("grad = vec2(2.0*%s.x*duvdx.x + 2.0*%s.y*duvdx.y,"
458 " 2.0*%s.x*duvdy.x + 2.0*%s.y*duvdy.y);",
459 offsets1.fsIn(), offsets1.fsIn(), offsets1.fsIn(),
460 offsets1.fsIn());
461 fragBuilder->codeAppend("invlen = inversesqrt(dot(grad, grad));");
462 fragBuilder->codeAppend("edgeAlpha *= clamp(0.5+test*invlen, 0.0, 1.0);");
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000463 }
464
egdaniel4ca2e602015-11-18 08:01:26 -0800465 fragBuilder->codeAppendf("%s = vec4(edgeAlpha);", args.fOutputCoverage);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000466 }
467
robertphillips46d36f02015-01-18 08:14:14 -0800468 static void GenKey(const GrGeometryProcessor& gp,
jvanverthcfc18862015-04-28 08:48:20 -0700469 const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700470 GrProcessorKeyBuilder* b) {
bsalomoncdaa97b2016-03-08 08:30:14 -0800471 const DIEllipseGeometryProcessor& diegp = gp.cast<DIEllipseGeometryProcessor>();
472 uint16_t key = static_cast<uint16_t>(diegp.fStyle);
473 key |= ComputePosKey(diegp.viewMatrix()) << 10;
joshualittb8c241a2015-05-19 08:23:30 -0700474 b->add32(key);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000475 }
476
egdaniel018fb622015-10-28 07:26:40 -0700477 void setData(const GrGLSLProgramDataManager& pdman,
478 const GrPrimitiveProcessor& gp) override {
bsalomoncdaa97b2016-03-08 08:30:14 -0800479 const DIEllipseGeometryProcessor& diegp = gp.cast<DIEllipseGeometryProcessor>();
joshualitt5559ca22015-05-21 15:50:36 -0700480
bsalomoncdaa97b2016-03-08 08:30:14 -0800481 if (!diegp.viewMatrix().isIdentity() && !fViewMatrix.cheapEqualTo(diegp.viewMatrix())) {
482 fViewMatrix = diegp.viewMatrix();
egdaniel018fb622015-10-28 07:26:40 -0700483 float viewMatrix[3 * 3];
egdaniel64c47282015-11-13 06:54:19 -0800484 GrGLSLGetMatrix<3>(viewMatrix, fViewMatrix);
joshualitt5559ca22015-05-21 15:50:36 -0700485 pdman.setMatrix3f(fViewMatrixUniform, viewMatrix);
486 }
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000487 }
488
489 private:
joshualitt5559ca22015-05-21 15:50:36 -0700490 SkMatrix fViewMatrix;
joshualitt5559ca22015-05-21 15:50:36 -0700491 UniformHandle fViewMatrixUniform;
joshualitt9b989322014-12-15 14:16:27 -0800492
egdaniele659a582015-11-13 09:55:43 -0800493 typedef GrGLSLGeometryProcessor INHERITED;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000494 };
495
egdaniel57d3b032015-11-13 11:57:27 -0800496 void getGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override {
497 GLSLProcessor::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800498 }
499
egdaniel57d3b032015-11-13 11:57:27 -0800500 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrGLSLCaps&) const override {
501 return new GLSLProcessor();
joshualitteb2a6762014-12-04 11:35:33 -0800502 }
503
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000504private:
joshualitt71c92602015-01-14 08:12:47 -0800505 const Attribute* fInPosition;
brianosmanbb2ff942016-02-11 14:15:18 -0800506 const Attribute* fInColor;
joshualitt71c92602015-01-14 08:12:47 -0800507 const Attribute* fInEllipseOffsets0;
508 const Attribute* fInEllipseOffsets1;
bsalomoncdaa97b2016-03-08 08:30:14 -0800509 SkMatrix fViewMatrix;
510 DIEllipseStyle fStyle;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000511
joshualittb0a8a372014-09-23 09:50:21 -0700512 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000513
joshualitt249af152014-09-15 11:41:13 -0700514 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000515};
516
bsalomoncdaa97b2016-03-08 08:30:14 -0800517GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DIEllipseGeometryProcessor);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000518
bungeman06ca8ec2016-06-09 08:01:03 -0700519sk_sp<GrGeometryProcessor> DIEllipseGeometryProcessor::TestCreate(GrProcessorTestData* d) {
520 return sk_sp<GrGeometryProcessor>(
521 new DIEllipseGeometryProcessor(GrTest::TestMatrix(d->fRandom),
522 (DIEllipseStyle)(d->fRandom->nextRangeU(0,2))));
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000523}
524
525///////////////////////////////////////////////////////////////////////////////
526
bsalomonabd30f52015-08-13 13:34:48 -0700527class CircleBatch : public GrVertexBatch {
joshualitt76e7fb62015-02-11 08:52:27 -0800528public:
reed1b55a962015-09-17 20:16:13 -0700529 DEFINE_BATCH_CLASS_ID
530
bsalomon4b4a7cc2016-07-08 04:42:54 -0700531 CircleBatch(GrColor color, const SkMatrix& viewMatrix, const SkRect& circle,
532 const SkStrokeRec& stroke)
533 : INHERITED(ClassID())
534 , fViewMatrixIfUsingLocalCoords(viewMatrix) {
535 SkPoint center = SkPoint::Make(circle.centerX(), circle.centerY());
536 viewMatrix.mapPoints(&center, 1);
537 SkScalar radius = viewMatrix.mapRadius(SkScalarHalf(circle.width()));
538 SkScalar strokeWidth = viewMatrix.mapRadius(stroke.getWidth());
joshualitt76e7fb62015-02-11 08:52:27 -0800539
bsalomon4b4a7cc2016-07-08 04:42:54 -0700540 SkStrokeRec::Style style = stroke.getStyle();
541 bool isStrokeOnly = SkStrokeRec::kStroke_Style == style ||
542 SkStrokeRec::kHairline_Style == style;
543 bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == style;
544
545 SkScalar innerRadius = 0.0f;
546 SkScalar outerRadius = radius;
547 SkScalar halfWidth = 0;
548 if (hasStroke) {
549 if (SkScalarNearlyZero(strokeWidth)) {
550 halfWidth = SK_ScalarHalf;
551 } else {
552 halfWidth = SkScalarHalf(strokeWidth);
553 }
554
555 outerRadius += halfWidth;
556 if (isStrokeOnly) {
557 innerRadius = radius - halfWidth;
558 }
559 }
560
561 // The radii are outset for two reasons. First, it allows the shader to simply perform
562 // simpler computation because the computed alpha is zero, rather than 50%, at the radius.
563 // Second, the outer radius is used to compute the verts of the bounding box that is
564 // rendered and the outset ensures the box will cover all partially covered by the circle.
565 outerRadius += SK_ScalarHalf;
566 innerRadius -= SK_ScalarHalf;
567
568 fGeoData.emplace_back(Geometry {
569 color,
570 innerRadius,
571 outerRadius,
572 SkRect::MakeLTRB(center.fX - outerRadius, center.fY - outerRadius,
573 center.fX + outerRadius, center.fY + outerRadius)
574 });
575 this->setBounds(fGeoData.back().fDevBounds);
576 fStroked = isStrokeOnly && innerRadius > 0;
bsalomoncdaa97b2016-03-08 08:30:14 -0800577 }
bsalomon4b4a7cc2016-07-08 04:42:54 -0700578
mtklein36352bf2015-03-25 18:17:31 -0700579 const char* name() const override { return "CircleBatch"; }
joshualitt76e7fb62015-02-11 08:52:27 -0800580
robertphillipse004bfc2015-11-16 09:06:59 -0800581 SkString dumpInfo() const override {
582 SkString string;
583 for (int i = 0; i < fGeoData.count(); ++i) {
584 string.appendf("Color: 0x%08x Rect [L: %.2f, T: %.2f, R: %.2f, B: %.2f],"
585 "InnerRad: %.2f, OuterRad: %.2f\n",
586 fGeoData[i].fColor,
587 fGeoData[i].fDevBounds.fLeft, fGeoData[i].fDevBounds.fTop,
588 fGeoData[i].fDevBounds.fRight, fGeoData[i].fDevBounds.fBottom,
589 fGeoData[i].fInnerRadius,
590 fGeoData[i].fOuterRadius);
591 }
592 string.append(INHERITED::dumpInfo());
593 return string;
594 }
595
halcanary9d524f22016-03-29 09:03:52 -0700596 void computePipelineOptimizations(GrInitInvariantOutput* color,
ethannicholasff210322015-11-24 12:10:10 -0800597 GrInitInvariantOutput* coverage,
598 GrBatchToXPOverrides* overrides) const override {
joshualitt76e7fb62015-02-11 08:52:27 -0800599 // When this is called on a batch, there is only one geometry bundle
ethannicholasff210322015-11-24 12:10:10 -0800600 color->setKnownFourComponents(fGeoData[0].fColor);
601 coverage->setUnknownSingleComponent();
joshualitt76e7fb62015-02-11 08:52:27 -0800602 }
603
bsalomone46f9fe2015-08-18 06:05:14 -0700604private:
ethannicholasff210322015-11-24 12:10:10 -0800605 void initBatchTracker(const GrXPOverridesForBatch& overrides) override {
bsalomoncdaa97b2016-03-08 08:30:14 -0800606 // Handle any overrides that affect our GP.
ethannicholasff210322015-11-24 12:10:10 -0800607 overrides.getOverrideColorIfSet(&fGeoData[0].fColor);
bsalomoncdaa97b2016-03-08 08:30:14 -0800608 if (!overrides.readsLocalCoords()) {
609 fViewMatrixIfUsingLocalCoords.reset();
610 }
joshualitt76e7fb62015-02-11 08:52:27 -0800611 }
612
joshualitt144c3c82015-11-30 12:30:13 -0800613 void onPrepareDraws(Target* target) const override {
bsalomoncdaa97b2016-03-08 08:30:14 -0800614 SkMatrix localMatrix;
615 if (!fViewMatrixIfUsingLocalCoords.invert(&localMatrix)) {
joshualitt76e7fb62015-02-11 08:52:27 -0800616 return;
617 }
618
619 // Setup geometry processor
bsalomoncdaa97b2016-03-08 08:30:14 -0800620 SkAutoTUnref<GrGeometryProcessor> gp(new CircleGeometryProcessor(fStroked, localMatrix));
joshualitt76e7fb62015-02-11 08:52:27 -0800621
joshualitt76e7fb62015-02-11 08:52:27 -0800622 int instanceCount = fGeoData.count();
joshualitt76e7fb62015-02-11 08:52:27 -0800623 size_t vertexStride = gp->getVertexStride();
624 SkASSERT(vertexStride == sizeof(CircleVertex));
bsalomonb5238a72015-05-05 07:49:49 -0700625 QuadHelper helper;
bsalomon75398562015-08-17 12:55:38 -0700626 CircleVertex* verts = reinterpret_cast<CircleVertex*>(helper.init(target, vertexStride,
bsalomonb5238a72015-05-05 07:49:49 -0700627 instanceCount));
628 if (!verts) {
joshualitt4b31de82015-03-05 14:33:41 -0800629 return;
630 }
631
joshualitt76e7fb62015-02-11 08:52:27 -0800632 for (int i = 0; i < instanceCount; i++) {
joshualitt144c3c82015-11-30 12:30:13 -0800633 const Geometry& geom = fGeoData[i];
joshualitt76e7fb62015-02-11 08:52:27 -0800634
brianosmanbb2ff942016-02-11 14:15:18 -0800635 GrColor color = geom.fColor;
bsalomonb5238a72015-05-05 07:49:49 -0700636 SkScalar innerRadius = geom.fInnerRadius;
637 SkScalar outerRadius = geom.fOuterRadius;
joshualitt76e7fb62015-02-11 08:52:27 -0800638
bsalomonb5238a72015-05-05 07:49:49 -0700639 const SkRect& bounds = geom.fDevBounds;
joshualitt76e7fb62015-02-11 08:52:27 -0800640
641 // The inner radius in the vertex data must be specified in normalized space.
642 innerRadius = innerRadius / outerRadius;
643 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
brianosmanbb2ff942016-02-11 14:15:18 -0800644 verts[0].fColor = color;
joshualitt76e7fb62015-02-11 08:52:27 -0800645 verts[0].fOffset = SkPoint::Make(-1, -1);
646 verts[0].fOuterRadius = outerRadius;
647 verts[0].fInnerRadius = innerRadius;
648
649 verts[1].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
brianosmanbb2ff942016-02-11 14:15:18 -0800650 verts[1].fColor = color;
joshualitt76e7fb62015-02-11 08:52:27 -0800651 verts[1].fOffset = SkPoint::Make(-1, 1);
652 verts[1].fOuterRadius = outerRadius;
653 verts[1].fInnerRadius = innerRadius;
654
655 verts[2].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
brianosmanbb2ff942016-02-11 14:15:18 -0800656 verts[2].fColor = color;
joshualitt76e7fb62015-02-11 08:52:27 -0800657 verts[2].fOffset = SkPoint::Make(1, 1);
658 verts[2].fOuterRadius = outerRadius;
659 verts[2].fInnerRadius = innerRadius;
660
661 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
brianosmanbb2ff942016-02-11 14:15:18 -0800662 verts[3].fColor = color;
joshualitt76e7fb62015-02-11 08:52:27 -0800663 verts[3].fOffset = SkPoint::Make(1, -1);
664 verts[3].fOuterRadius = outerRadius;
665 verts[3].fInnerRadius = innerRadius;
666
bsalomonb5238a72015-05-05 07:49:49 -0700667 verts += kVerticesPerQuad;
joshualitt76e7fb62015-02-11 08:52:27 -0800668 }
bsalomon342bfc22016-04-01 06:06:20 -0700669 helper.recordDraw(target, gp);
joshualitt76e7fb62015-02-11 08:52:27 -0800670 }
671
bsalomoncb02b382015-08-12 11:14:50 -0700672 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override {
bsalomonabd30f52015-08-13 13:34:48 -0700673 CircleBatch* that = t->cast<CircleBatch>();
674 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
675 that->bounds(), caps)) {
joshualitt8cab9a72015-07-16 09:13:50 -0700676 return false;
677 }
678
bsalomoncdaa97b2016-03-08 08:30:14 -0800679 if (this->fStroked != that->fStroked) {
joshualitt76e7fb62015-02-11 08:52:27 -0800680 return false;
681 }
682
bsalomoncdaa97b2016-03-08 08:30:14 -0800683 if (!fViewMatrixIfUsingLocalCoords.cheapEqualTo(that->fViewMatrixIfUsingLocalCoords)) {
joshualitt76e7fb62015-02-11 08:52:27 -0800684 return false;
685 }
686
bsalomoncdaa97b2016-03-08 08:30:14 -0800687 fGeoData.push_back_n(that->fGeoData.count(), that->fGeoData.begin());
joshualitt99c7c072015-05-01 13:43:30 -0700688 this->joinBounds(that->bounds());
joshualitt76e7fb62015-02-11 08:52:27 -0800689 return true;
690 }
691
bsalomon4b4a7cc2016-07-08 04:42:54 -0700692 struct Geometry {
693 GrColor fColor;
694 SkScalar fInnerRadius;
695 SkScalar fOuterRadius;
696 SkRect fDevBounds;
697 };
698
bsalomoncdaa97b2016-03-08 08:30:14 -0800699 bool fStroked;
700 SkMatrix fViewMatrixIfUsingLocalCoords;
joshualitt76e7fb62015-02-11 08:52:27 -0800701 SkSTArray<1, Geometry, true> fGeoData;
reed1b55a962015-09-17 20:16:13 -0700702
703 typedef GrVertexBatch INHERITED;
joshualitt76e7fb62015-02-11 08:52:27 -0800704};
705
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000706///////////////////////////////////////////////////////////////////////////////
707
bsalomonabd30f52015-08-13 13:34:48 -0700708class EllipseBatch : public GrVertexBatch {
joshualitt76e7fb62015-02-11 08:52:27 -0800709public:
reed1b55a962015-09-17 20:16:13 -0700710 DEFINE_BATCH_CLASS_ID
bsalomon4b4a7cc2016-07-08 04:42:54 -0700711 static GrDrawBatch* Create(GrColor color, const SkMatrix& viewMatrix, const SkRect& ellipse,
712 const SkStrokeRec& stroke) {
713 SkASSERT(viewMatrix.rectStaysRect());
reed1b55a962015-09-17 20:16:13 -0700714
bsalomon4b4a7cc2016-07-08 04:42:54 -0700715 // do any matrix crunching before we reset the draw state for device coords
716 SkPoint center = SkPoint::Make(ellipse.centerX(), ellipse.centerY());
717 viewMatrix.mapPoints(&center, 1);
718 SkScalar ellipseXRadius = SkScalarHalf(ellipse.width());
719 SkScalar ellipseYRadius = SkScalarHalf(ellipse.height());
720 SkScalar xRadius = SkScalarAbs(viewMatrix[SkMatrix::kMScaleX]*ellipseXRadius +
721 viewMatrix[SkMatrix::kMSkewY]*ellipseYRadius);
722 SkScalar yRadius = SkScalarAbs(viewMatrix[SkMatrix::kMSkewX]*ellipseXRadius +
723 viewMatrix[SkMatrix::kMScaleY]*ellipseYRadius);
joshualitt76e7fb62015-02-11 08:52:27 -0800724
bsalomon4b4a7cc2016-07-08 04:42:54 -0700725 // do (potentially) anisotropic mapping of stroke
726 SkVector scaledStroke;
727 SkScalar strokeWidth = stroke.getWidth();
728 scaledStroke.fX = SkScalarAbs(strokeWidth*(viewMatrix[SkMatrix::kMScaleX] +
729 viewMatrix[SkMatrix::kMSkewY]));
730 scaledStroke.fY = SkScalarAbs(strokeWidth*(viewMatrix[SkMatrix::kMSkewX] +
731 viewMatrix[SkMatrix::kMScaleY]));
732
733 SkStrokeRec::Style style = stroke.getStyle();
734 bool isStrokeOnly = SkStrokeRec::kStroke_Style == style ||
735 SkStrokeRec::kHairline_Style == style;
736 bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == style;
737
738 SkScalar innerXRadius = 0;
739 SkScalar innerYRadius = 0;
740 if (hasStroke) {
741 if (SkScalarNearlyZero(scaledStroke.length())) {
742 scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf);
743 } else {
744 scaledStroke.scale(SK_ScalarHalf);
745 }
746
747 // we only handle thick strokes for near-circular ellipses
748 if (scaledStroke.length() > SK_ScalarHalf &&
749 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
750 return nullptr;
751 }
752
753 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
754 if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius ||
755 scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) {
756 return nullptr;
757 }
758
759 // this is legit only if scale & translation (which should be the case at the moment)
760 if (isStrokeOnly) {
761 innerXRadius = xRadius - scaledStroke.fX;
762 innerYRadius = yRadius - scaledStroke.fY;
763 }
764
765 xRadius += scaledStroke.fX;
766 yRadius += scaledStroke.fY;
767 }
768
769 EllipseBatch* batch = new EllipseBatch();
770 batch->fGeoData.emplace_back(Geometry {
771 color,
772 xRadius,
773 yRadius,
774 innerXRadius,
775 innerYRadius,
776 SkRect::MakeLTRB(center.fX - xRadius, center.fY - yRadius,
777 center.fX + xRadius, center.fY + yRadius)
778 });
779
780 // Outset bounds to include half-pixel width antialiasing.
781 batch->fGeoData[0].fDevBounds.outset(SK_ScalarHalf, SK_ScalarHalf);
782
783 batch->fStroked = isStrokeOnly && innerXRadius > 0 && innerYRadius > 0;
784 batch->fViewMatrixIfUsingLocalCoords = viewMatrix;
785 batch->setBounds(batch->fGeoData.back().fDevBounds);
786 return batch;
bsalomoncdaa97b2016-03-08 08:30:14 -0800787 }
joshualitt76e7fb62015-02-11 08:52:27 -0800788
mtklein36352bf2015-03-25 18:17:31 -0700789 const char* name() const override { return "EllipseBatch"; }
joshualitt76e7fb62015-02-11 08:52:27 -0800790
halcanary9d524f22016-03-29 09:03:52 -0700791 void computePipelineOptimizations(GrInitInvariantOutput* color,
ethannicholasff210322015-11-24 12:10:10 -0800792 GrInitInvariantOutput* coverage,
793 GrBatchToXPOverrides* overrides) const override {
joshualitt76e7fb62015-02-11 08:52:27 -0800794 // When this is called on a batch, there is only one geometry bundle
ethannicholasff210322015-11-24 12:10:10 -0800795 color->setKnownFourComponents(fGeoData[0].fColor);
796 coverage->setUnknownSingleComponent();
joshualitt76e7fb62015-02-11 08:52:27 -0800797 }
798
bsalomone46f9fe2015-08-18 06:05:14 -0700799private:
bsalomon4b4a7cc2016-07-08 04:42:54 -0700800 EllipseBatch() : INHERITED(ClassID()) {}
801
ethannicholasff210322015-11-24 12:10:10 -0800802 void initBatchTracker(const GrXPOverridesForBatch& overrides) override {
bsalomoncdaa97b2016-03-08 08:30:14 -0800803 // Handle any overrides that affect our GP.
ethannicholasff210322015-11-24 12:10:10 -0800804 if (!overrides.readsCoverage()) {
joshualitt76e7fb62015-02-11 08:52:27 -0800805 fGeoData[0].fColor = GrColor_ILLEGAL;
joshualitt76e7fb62015-02-11 08:52:27 -0800806 }
bsalomoncdaa97b2016-03-08 08:30:14 -0800807 if (!overrides.readsLocalCoords()) {
808 fViewMatrixIfUsingLocalCoords.reset();
809 }
joshualitt76e7fb62015-02-11 08:52:27 -0800810 }
811
joshualitt144c3c82015-11-30 12:30:13 -0800812 void onPrepareDraws(Target* target) const override {
bsalomoncdaa97b2016-03-08 08:30:14 -0800813 SkMatrix localMatrix;
814 if (!fViewMatrixIfUsingLocalCoords.invert(&localMatrix)) {
joshualitt76e7fb62015-02-11 08:52:27 -0800815 return;
816 }
817
818 // Setup geometry processor
bsalomoncdaa97b2016-03-08 08:30:14 -0800819 SkAutoTUnref<GrGeometryProcessor> gp(new EllipseGeometryProcessor(fStroked, localMatrix));
joshualitt76e7fb62015-02-11 08:52:27 -0800820
joshualitt76e7fb62015-02-11 08:52:27 -0800821 int instanceCount = fGeoData.count();
bsalomonb5238a72015-05-05 07:49:49 -0700822 QuadHelper helper;
joshualitt76e7fb62015-02-11 08:52:27 -0800823 size_t vertexStride = gp->getVertexStride();
joshualitt19e00582015-02-11 17:36:30 -0800824 SkASSERT(vertexStride == sizeof(EllipseVertex));
bsalomonb5238a72015-05-05 07:49:49 -0700825 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(
bsalomon75398562015-08-17 12:55:38 -0700826 helper.init(target, vertexStride, instanceCount));
bsalomonb5238a72015-05-05 07:49:49 -0700827 if (!verts) {
joshualitt4b31de82015-03-05 14:33:41 -0800828 return;
829 }
830
bsalomon8415abe2015-05-04 11:41:41 -0700831 for (int i = 0; i < instanceCount; i++) {
joshualitt144c3c82015-11-30 12:30:13 -0800832 const Geometry& geom = fGeoData[i];
bsalomon8415abe2015-05-04 11:41:41 -0700833
brianosmanbb2ff942016-02-11 14:15:18 -0800834 GrColor color = geom.fColor;
bsalomonb5238a72015-05-05 07:49:49 -0700835 SkScalar xRadius = geom.fXRadius;
836 SkScalar yRadius = geom.fYRadius;
joshualitt76e7fb62015-02-11 08:52:27 -0800837
838 // Compute the reciprocals of the radii here to save time in the shader
839 SkScalar xRadRecip = SkScalarInvert(xRadius);
840 SkScalar yRadRecip = SkScalarInvert(yRadius);
bsalomonb5238a72015-05-05 07:49:49 -0700841 SkScalar xInnerRadRecip = SkScalarInvert(geom.fInnerXRadius);
842 SkScalar yInnerRadRecip = SkScalarInvert(geom.fInnerYRadius);
joshualitt76e7fb62015-02-11 08:52:27 -0800843
bsalomonb5238a72015-05-05 07:49:49 -0700844 const SkRect& bounds = geom.fDevBounds;
joshualitt76e7fb62015-02-11 08:52:27 -0800845
vjiaoblack977996d2016-06-30 12:20:54 -0700846 // fOffsets are expanded from xyRadii to include the half-pixel antialiasing width.
847 SkScalar xMaxOffset = xRadius + SK_ScalarHalf;
848 SkScalar yMaxOffset = yRadius + SK_ScalarHalf;
849
joshualitt76e7fb62015-02-11 08:52:27 -0800850 // The inner radius in the vertex data must be specified in normalized space.
851 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
brianosmanbb2ff942016-02-11 14:15:18 -0800852 verts[0].fColor = color;
vjiaoblack977996d2016-06-30 12:20:54 -0700853 verts[0].fOffset = SkPoint::Make(-xMaxOffset, -yMaxOffset);
joshualitt76e7fb62015-02-11 08:52:27 -0800854 verts[0].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
855 verts[0].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
856
857 verts[1].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
brianosmanbb2ff942016-02-11 14:15:18 -0800858 verts[1].fColor = color;
vjiaoblack977996d2016-06-30 12:20:54 -0700859 verts[1].fOffset = SkPoint::Make(-xMaxOffset, yMaxOffset);
joshualitt76e7fb62015-02-11 08:52:27 -0800860 verts[1].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
861 verts[1].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
862
863 verts[2].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
brianosmanbb2ff942016-02-11 14:15:18 -0800864 verts[2].fColor = color;
vjiaoblack977996d2016-06-30 12:20:54 -0700865 verts[2].fOffset = SkPoint::Make(xMaxOffset, yMaxOffset);
joshualitt76e7fb62015-02-11 08:52:27 -0800866 verts[2].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
867 verts[2].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
868
869 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
brianosmanbb2ff942016-02-11 14:15:18 -0800870 verts[3].fColor = color;
vjiaoblack977996d2016-06-30 12:20:54 -0700871 verts[3].fOffset = SkPoint::Make(xMaxOffset, -yMaxOffset);
joshualitt76e7fb62015-02-11 08:52:27 -0800872 verts[3].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
873 verts[3].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
874
bsalomonb5238a72015-05-05 07:49:49 -0700875 verts += kVerticesPerQuad;
joshualitt76e7fb62015-02-11 08:52:27 -0800876 }
bsalomon342bfc22016-04-01 06:06:20 -0700877 helper.recordDraw(target, gp);
joshualitt76e7fb62015-02-11 08:52:27 -0800878 }
879
bsalomoncb02b382015-08-12 11:14:50 -0700880 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override {
bsalomonabd30f52015-08-13 13:34:48 -0700881 EllipseBatch* that = t->cast<EllipseBatch>();
882
883 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
884 that->bounds(), caps)) {
joshualitt8cab9a72015-07-16 09:13:50 -0700885 return false;
886 }
887
bsalomoncdaa97b2016-03-08 08:30:14 -0800888 if (fStroked != that->fStroked) {
joshualitt76e7fb62015-02-11 08:52:27 -0800889 return false;
890 }
891
bsalomoncdaa97b2016-03-08 08:30:14 -0800892 if (!fViewMatrixIfUsingLocalCoords.cheapEqualTo(that->fViewMatrixIfUsingLocalCoords)) {
joshualitt76e7fb62015-02-11 08:52:27 -0800893 return false;
894 }
895
bsalomoncdaa97b2016-03-08 08:30:14 -0800896 fGeoData.push_back_n(that->fGeoData.count(), that->fGeoData.begin());
joshualitt99c7c072015-05-01 13:43:30 -0700897 this->joinBounds(that->bounds());
joshualitt76e7fb62015-02-11 08:52:27 -0800898 return true;
899 }
900
bsalomon4b4a7cc2016-07-08 04:42:54 -0700901 struct Geometry {
902 GrColor fColor;
903 SkScalar fXRadius;
904 SkScalar fYRadius;
905 SkScalar fInnerXRadius;
906 SkScalar fInnerYRadius;
907 SkRect fDevBounds;
908 };
joshualitt76e7fb62015-02-11 08:52:27 -0800909
bsalomoncdaa97b2016-03-08 08:30:14 -0800910 bool fStroked;
911 SkMatrix fViewMatrixIfUsingLocalCoords;
joshualitt76e7fb62015-02-11 08:52:27 -0800912 SkSTArray<1, Geometry, true> fGeoData;
reed1b55a962015-09-17 20:16:13 -0700913
914 typedef GrVertexBatch INHERITED;
joshualitt76e7fb62015-02-11 08:52:27 -0800915};
916
joshualitt76e7fb62015-02-11 08:52:27 -0800917/////////////////////////////////////////////////////////////////////////////////////////////////
918
bsalomonabd30f52015-08-13 13:34:48 -0700919class DIEllipseBatch : public GrVertexBatch {
joshualitt76e7fb62015-02-11 08:52:27 -0800920public:
reed1b55a962015-09-17 20:16:13 -0700921 DEFINE_BATCH_CLASS_ID
922
bsalomon4b4a7cc2016-07-08 04:42:54 -0700923 static GrDrawBatch* Create(GrColor color,
924 const SkMatrix& viewMatrix,
925 const SkRect& ellipse,
926 const SkStrokeRec& stroke) {
927 SkPoint center = SkPoint::Make(ellipse.centerX(), ellipse.centerY());
928 SkScalar xRadius = SkScalarHalf(ellipse.width());
929 SkScalar yRadius = SkScalarHalf(ellipse.height());
joshualitt76e7fb62015-02-11 08:52:27 -0800930
bsalomon4b4a7cc2016-07-08 04:42:54 -0700931 SkStrokeRec::Style style = stroke.getStyle();
932 DIEllipseStyle dieStyle = (SkStrokeRec::kStroke_Style == style) ?
933 DIEllipseStyle::kStroke :
934 (SkStrokeRec::kHairline_Style == style) ?
935 DIEllipseStyle::kHairline : DIEllipseStyle::kFill;
936
937 SkScalar innerXRadius = 0;
938 SkScalar innerYRadius = 0;
939 if (SkStrokeRec::kFill_Style != style && SkStrokeRec::kHairline_Style != style) {
940 SkScalar strokeWidth = stroke.getWidth();
941
942 if (SkScalarNearlyZero(strokeWidth)) {
943 strokeWidth = SK_ScalarHalf;
944 } else {
945 strokeWidth *= SK_ScalarHalf;
946 }
947
948 // we only handle thick strokes for near-circular ellipses
949 if (strokeWidth > SK_ScalarHalf &&
950 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
951 return nullptr;
952 }
953
954 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
955 if (strokeWidth*(yRadius*yRadius) < (strokeWidth*strokeWidth)*xRadius ||
956 strokeWidth*(xRadius*xRadius) < (strokeWidth*strokeWidth)*yRadius) {
957 return nullptr;
958 }
959
960 // set inner radius (if needed)
961 if (SkStrokeRec::kStroke_Style == style) {
962 innerXRadius = xRadius - strokeWidth;
963 innerYRadius = yRadius - strokeWidth;
964 }
965
966 xRadius += strokeWidth;
967 yRadius += strokeWidth;
968 }
969 if (DIEllipseStyle::kStroke == dieStyle) {
970 dieStyle = (innerXRadius > 0 && innerYRadius > 0) ? DIEllipseStyle ::kStroke :
971 DIEllipseStyle ::kFill;
972 }
973
974 // This expands the outer rect so that after CTM we end up with a half-pixel border
975 SkScalar a = viewMatrix[SkMatrix::kMScaleX];
976 SkScalar b = viewMatrix[SkMatrix::kMSkewX];
977 SkScalar c = viewMatrix[SkMatrix::kMSkewY];
978 SkScalar d = viewMatrix[SkMatrix::kMScaleY];
979 SkScalar geoDx = SK_ScalarHalf / SkScalarSqrt(a*a + c*c);
980 SkScalar geoDy = SK_ScalarHalf / SkScalarSqrt(b*b + d*d);
981
982 DIEllipseBatch* batch = new DIEllipseBatch();
983 batch->fGeoData.emplace_back(Geometry {
984 viewMatrix,
985 color,
986 xRadius,
987 yRadius,
988 innerXRadius,
989 innerYRadius,
990 geoDx,
991 geoDy,
992 dieStyle,
993 SkRect::MakeLTRB(center.fX - xRadius - geoDx, center.fY - yRadius - geoDy,
994 center.fX + xRadius + geoDx, center.fY + yRadius + geoDy)
995 });
996 SkRect devBounds = batch->fGeoData.back().fBounds;
997 viewMatrix.mapRect(&devBounds);
998 batch->setBounds(devBounds);
999 return batch;
joshualitt76e7fb62015-02-11 08:52:27 -08001000 }
1001
mtklein36352bf2015-03-25 18:17:31 -07001002 const char* name() const override { return "DIEllipseBatch"; }
joshualitt76e7fb62015-02-11 08:52:27 -08001003
halcanary9d524f22016-03-29 09:03:52 -07001004 void computePipelineOptimizations(GrInitInvariantOutput* color,
ethannicholasff210322015-11-24 12:10:10 -08001005 GrInitInvariantOutput* coverage,
1006 GrBatchToXPOverrides* overrides) const override {
joshualitt76e7fb62015-02-11 08:52:27 -08001007 // When this is called on a batch, there is only one geometry bundle
ethannicholasff210322015-11-24 12:10:10 -08001008 color->setKnownFourComponents(fGeoData[0].fColor);
1009 coverage->setUnknownSingleComponent();
joshualitt76e7fb62015-02-11 08:52:27 -08001010 }
1011
bsalomone46f9fe2015-08-18 06:05:14 -07001012private:
1013
bsalomon4b4a7cc2016-07-08 04:42:54 -07001014 DIEllipseBatch() : INHERITED(ClassID()) {}
1015
ethannicholasff210322015-11-24 12:10:10 -08001016 void initBatchTracker(const GrXPOverridesForBatch& overrides) override {
bsalomoncdaa97b2016-03-08 08:30:14 -08001017 // Handle any overrides that affect our GP.
ethannicholasff210322015-11-24 12:10:10 -08001018 overrides.getOverrideColorIfSet(&fGeoData[0].fColor);
bsalomoncdaa97b2016-03-08 08:30:14 -08001019 fUsesLocalCoords = overrides.readsLocalCoords();
joshualitt76e7fb62015-02-11 08:52:27 -08001020 }
1021
joshualitt144c3c82015-11-30 12:30:13 -08001022 void onPrepareDraws(Target* target) const override {
joshualitt76e7fb62015-02-11 08:52:27 -08001023 // Setup geometry processor
bsalomoncdaa97b2016-03-08 08:30:14 -08001024 SkAutoTUnref<GrGeometryProcessor> gp(new DIEllipseGeometryProcessor(this->viewMatrix(),
1025 this->style()));
joshualitt76e7fb62015-02-11 08:52:27 -08001026
joshualitt76e7fb62015-02-11 08:52:27 -08001027 int instanceCount = fGeoData.count();
joshualitt76e7fb62015-02-11 08:52:27 -08001028 size_t vertexStride = gp->getVertexStride();
joshualitt19e00582015-02-11 17:36:30 -08001029 SkASSERT(vertexStride == sizeof(DIEllipseVertex));
bsalomonb5238a72015-05-05 07:49:49 -07001030 QuadHelper helper;
1031 DIEllipseVertex* verts = reinterpret_cast<DIEllipseVertex*>(
bsalomon75398562015-08-17 12:55:38 -07001032 helper.init(target, vertexStride, instanceCount));
bsalomonb5238a72015-05-05 07:49:49 -07001033 if (!verts) {
joshualitt4b31de82015-03-05 14:33:41 -08001034 return;
1035 }
1036
joshualitt76e7fb62015-02-11 08:52:27 -08001037 for (int i = 0; i < instanceCount; i++) {
joshualitt144c3c82015-11-30 12:30:13 -08001038 const Geometry& geom = fGeoData[i];
joshualitt76e7fb62015-02-11 08:52:27 -08001039
brianosmanbb2ff942016-02-11 14:15:18 -08001040 GrColor color = geom.fColor;
bsalomonb5238a72015-05-05 07:49:49 -07001041 SkScalar xRadius = geom.fXRadius;
1042 SkScalar yRadius = geom.fYRadius;
joshualitt76e7fb62015-02-11 08:52:27 -08001043
bsalomonb5238a72015-05-05 07:49:49 -07001044 const SkRect& bounds = geom.fBounds;
joshualitt76e7fb62015-02-11 08:52:27 -08001045
1046 // This adjusts the "radius" to include the half-pixel border
reed80ea19c2015-05-12 10:37:34 -07001047 SkScalar offsetDx = geom.fGeoDx / xRadius;
1048 SkScalar offsetDy = geom.fGeoDy / yRadius;
joshualitt76e7fb62015-02-11 08:52:27 -08001049
reed80ea19c2015-05-12 10:37:34 -07001050 SkScalar innerRatioX = xRadius / geom.fInnerXRadius;
1051 SkScalar innerRatioY = yRadius / geom.fInnerYRadius;
joshualitt76e7fb62015-02-11 08:52:27 -08001052
1053 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
brianosmanbb2ff942016-02-11 14:15:18 -08001054 verts[0].fColor = color;
joshualitt76e7fb62015-02-11 08:52:27 -08001055 verts[0].fOuterOffset = SkPoint::Make(-1.0f - offsetDx, -1.0f - offsetDy);
1056 verts[0].fInnerOffset = SkPoint::Make(-innerRatioX - offsetDx, -innerRatioY - offsetDy);
1057
1058 verts[1].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
brianosmanbb2ff942016-02-11 14:15:18 -08001059 verts[1].fColor = color;
joshualitt76e7fb62015-02-11 08:52:27 -08001060 verts[1].fOuterOffset = SkPoint::Make(-1.0f - offsetDx, 1.0f + offsetDy);
1061 verts[1].fInnerOffset = SkPoint::Make(-innerRatioX - offsetDx, innerRatioY + offsetDy);
1062
1063 verts[2].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
brianosmanbb2ff942016-02-11 14:15:18 -08001064 verts[2].fColor = color;
joshualitt76e7fb62015-02-11 08:52:27 -08001065 verts[2].fOuterOffset = SkPoint::Make(1.0f + offsetDx, 1.0f + offsetDy);
1066 verts[2].fInnerOffset = SkPoint::Make(innerRatioX + offsetDx, innerRatioY + offsetDy);
1067
1068 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
brianosmanbb2ff942016-02-11 14:15:18 -08001069 verts[3].fColor = color;
joshualitt76e7fb62015-02-11 08:52:27 -08001070 verts[3].fOuterOffset = SkPoint::Make(1.0f + offsetDx, -1.0f - offsetDy);
1071 verts[3].fInnerOffset = SkPoint::Make(innerRatioX + offsetDx, -innerRatioY - offsetDy);
1072
bsalomonb5238a72015-05-05 07:49:49 -07001073 verts += kVerticesPerQuad;
joshualitt76e7fb62015-02-11 08:52:27 -08001074 }
bsalomon342bfc22016-04-01 06:06:20 -07001075 helper.recordDraw(target, gp);
joshualitt76e7fb62015-02-11 08:52:27 -08001076 }
halcanary9d524f22016-03-29 09:03:52 -07001077
bsalomoncb02b382015-08-12 11:14:50 -07001078 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override {
bsalomonabd30f52015-08-13 13:34:48 -07001079 DIEllipseBatch* that = t->cast<DIEllipseBatch>();
1080 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
1081 that->bounds(), caps)) {
joshualitt8cab9a72015-07-16 09:13:50 -07001082 return false;
1083 }
1084
bsalomoncdaa97b2016-03-08 08:30:14 -08001085 if (this->style() != that->style()) {
joshualitt76e7fb62015-02-11 08:52:27 -08001086 return false;
1087 }
1088
joshualittd96a67b2015-05-05 14:09:05 -07001089 // TODO rewrite to allow positioning on CPU
1090 if (!this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
joshualitt76e7fb62015-02-11 08:52:27 -08001091 return false;
1092 }
1093
bsalomoncdaa97b2016-03-08 08:30:14 -08001094 fGeoData.push_back_n(that->fGeoData.count(), that->fGeoData.begin());
joshualitt99c7c072015-05-01 13:43:30 -07001095 this->joinBounds(that->bounds());
joshualitt76e7fb62015-02-11 08:52:27 -08001096 return true;
1097 }
1098
joshualitt76e7fb62015-02-11 08:52:27 -08001099 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; }
bsalomoncdaa97b2016-03-08 08:30:14 -08001100 DIEllipseStyle style() const { return fGeoData[0].fStyle; }
joshualitt76e7fb62015-02-11 08:52:27 -08001101
bsalomon4b4a7cc2016-07-08 04:42:54 -07001102 struct Geometry {
1103 SkMatrix fViewMatrix;
1104 GrColor fColor;
1105 SkScalar fXRadius;
1106 SkScalar fYRadius;
1107 SkScalar fInnerXRadius;
1108 SkScalar fInnerYRadius;
1109 SkScalar fGeoDx;
1110 SkScalar fGeoDy;
1111 DIEllipseStyle fStyle;
1112 SkRect fBounds;
1113 };
1114
bsalomoncdaa97b2016-03-08 08:30:14 -08001115 bool fUsesLocalCoords;
joshualitt76e7fb62015-02-11 08:52:27 -08001116 SkSTArray<1, Geometry, true> fGeoData;
reed1b55a962015-09-17 20:16:13 -07001117
1118 typedef GrVertexBatch INHERITED;
joshualitt76e7fb62015-02-11 08:52:27 -08001119};
1120
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001121///////////////////////////////////////////////////////////////////////////////
1122
1123static const uint16_t gRRectIndices[] = {
1124 // corners
1125 0, 1, 5, 0, 5, 4,
1126 2, 3, 7, 2, 7, 6,
1127 8, 9, 13, 8, 13, 12,
1128 10, 11, 15, 10, 15, 14,
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001129
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001130 // edges
1131 1, 2, 6, 1, 6, 5,
1132 4, 5, 9, 4, 9, 8,
1133 6, 7, 11, 6, 11, 10,
1134 9, 10, 14, 9, 14, 13,
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001135
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001136 // center
1137 // we place this at the end so that we can ignore these indices when rendering stroke-only
1138 5, 6, 10, 5, 10, 9
1139};
1140
joshualitt5ead6da2014-10-22 16:00:29 -07001141static const int kIndicesPerStrokeRRect = SK_ARRAY_COUNT(gRRectIndices) - 6;
1142static const int kIndicesPerRRect = SK_ARRAY_COUNT(gRRectIndices);
1143static const int kVertsPerRRect = 16;
1144static const int kNumRRectsInIndexBuffer = 256;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001145
bsalomoned0bcad2015-05-04 10:36:42 -07001146GR_DECLARE_STATIC_UNIQUE_KEY(gStrokeRRectOnlyIndexBufferKey);
1147GR_DECLARE_STATIC_UNIQUE_KEY(gRRectOnlyIndexBufferKey);
cdalton397536c2016-03-25 12:15:03 -07001148static const GrBuffer* ref_rrect_index_buffer(bool strokeOnly,
1149 GrResourceProvider* resourceProvider) {
bsalomoned0bcad2015-05-04 10:36:42 -07001150 GR_DEFINE_STATIC_UNIQUE_KEY(gStrokeRRectOnlyIndexBufferKey);
1151 GR_DEFINE_STATIC_UNIQUE_KEY(gRRectOnlyIndexBufferKey);
1152 if (strokeOnly) {
bsalomoneae62002015-07-31 13:59:30 -07001153 return resourceProvider->findOrCreateInstancedIndexBuffer(
bsalomoned0bcad2015-05-04 10:36:42 -07001154 gRRectIndices, kIndicesPerStrokeRRect, kNumRRectsInIndexBuffer, kVertsPerRRect,
1155 gStrokeRRectOnlyIndexBufferKey);
1156 } else {
bsalomoneae62002015-07-31 13:59:30 -07001157 return resourceProvider->findOrCreateInstancedIndexBuffer(
bsalomoned0bcad2015-05-04 10:36:42 -07001158 gRRectIndices, kIndicesPerRRect, kNumRRectsInIndexBuffer, kVertsPerRRect,
1159 gRRectOnlyIndexBufferKey);
1160
1161 }
1162}
1163
joshualitt76e7fb62015-02-11 08:52:27 -08001164///////////////////////////////////////////////////////////////////////////////////////////////////
1165
bsalomonabd30f52015-08-13 13:34:48 -07001166class RRectCircleRendererBatch : public GrVertexBatch {
joshualitt76e7fb62015-02-11 08:52:27 -08001167public:
reed1b55a962015-09-17 20:16:13 -07001168 DEFINE_BATCH_CLASS_ID
1169
bsalomon4b4a7cc2016-07-08 04:42:54 -07001170 // A devStrokeWidth <= 0 indicates a fill only. If devStrokeWidth > 0 then strokeOnly indicates
1171 // whether the rrect is only stroked or stroked and filled.
1172 RRectCircleRendererBatch(GrColor color, const SkMatrix& viewMatrix, const SkRect& devRect,
1173 float devRadius, float devStrokeWidth, bool strokeOnly)
1174 : INHERITED(ClassID())
1175 , fViewMatrixIfUsingLocalCoords(viewMatrix) {
1176 SkRect bounds = devRect;
1177 SkASSERT(!(devStrokeWidth <= 0 && strokeOnly));
1178 SkScalar innerRadius = 0.0f;
1179 SkScalar outerRadius = devRadius;
1180 SkScalar halfWidth = 0;
1181 fStroked = false;
1182 if (devStrokeWidth > 0) {
1183 if (SkScalarNearlyZero(devStrokeWidth)) {
1184 halfWidth = SK_ScalarHalf;
1185 } else {
1186 halfWidth = SkScalarHalf(devStrokeWidth);
1187 }
joshualitt76e7fb62015-02-11 08:52:27 -08001188
bsalomon4b4a7cc2016-07-08 04:42:54 -07001189 if (strokeOnly) {
1190 innerRadius = devRadius - halfWidth;
1191 fStroked = innerRadius >= 0;
1192 }
1193 outerRadius += halfWidth;
1194 bounds.outset(halfWidth, halfWidth);
1195 }
bsalomoncdaa97b2016-03-08 08:30:14 -08001196
bsalomon4b4a7cc2016-07-08 04:42:54 -07001197 // The radii are outset for two reasons. First, it allows the shader to simply perform
1198 // simpler computation because the computed alpha is zero, rather than 50%, at the radius.
1199 // Second, the outer radius is used to compute the verts of the bounding box that is
1200 // rendered and the outset ensures the box will cover all partially covered by the rrect
1201 // corners.
1202 outerRadius += SK_ScalarHalf;
1203 innerRadius -= SK_ScalarHalf;
1204
1205 // Expand the rect so all the pixels will be captured.
1206 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1207
1208 fGeoData.emplace_back(Geometry { color, innerRadius, outerRadius, bounds });
1209 this->setBounds(bounds);
joshualitt76e7fb62015-02-11 08:52:27 -08001210 }
1211
mtklein36352bf2015-03-25 18:17:31 -07001212 const char* name() const override { return "RRectCircleBatch"; }
joshualitt76e7fb62015-02-11 08:52:27 -08001213
halcanary9d524f22016-03-29 09:03:52 -07001214 void computePipelineOptimizations(GrInitInvariantOutput* color,
ethannicholasff210322015-11-24 12:10:10 -08001215 GrInitInvariantOutput* coverage,
1216 GrBatchToXPOverrides* overrides) const override {
joshualitt76e7fb62015-02-11 08:52:27 -08001217 // When this is called on a batch, there is only one geometry bundle
ethannicholasff210322015-11-24 12:10:10 -08001218 color->setKnownFourComponents(fGeoData[0].fColor);
1219 coverage->setUnknownSingleComponent();
joshualitt76e7fb62015-02-11 08:52:27 -08001220 }
1221
bsalomone46f9fe2015-08-18 06:05:14 -07001222private:
ethannicholasff210322015-11-24 12:10:10 -08001223 void initBatchTracker(const GrXPOverridesForBatch& overrides) override {
bsalomoncdaa97b2016-03-08 08:30:14 -08001224 // Handle any overrides that affect our GP.
ethannicholasff210322015-11-24 12:10:10 -08001225 overrides.getOverrideColorIfSet(&fGeoData[0].fColor);
bsalomoncdaa97b2016-03-08 08:30:14 -08001226 if (!overrides.readsLocalCoords()) {
1227 fViewMatrixIfUsingLocalCoords.reset();
1228 }
joshualitt76e7fb62015-02-11 08:52:27 -08001229 }
1230
joshualitt144c3c82015-11-30 12:30:13 -08001231 void onPrepareDraws(Target* target) const override {
bsalomoncdaa97b2016-03-08 08:30:14 -08001232 // Invert the view matrix as a local matrix (if any other processors require coords).
1233 SkMatrix localMatrix;
1234 if (!fViewMatrixIfUsingLocalCoords.invert(&localMatrix)) {
joshualitt76e7fb62015-02-11 08:52:27 -08001235 return;
1236 }
1237
1238 // Setup geometry processor
bsalomoncdaa97b2016-03-08 08:30:14 -08001239 SkAutoTUnref<GrGeometryProcessor> gp(new CircleGeometryProcessor(fStroked, localMatrix));
joshualitt76e7fb62015-02-11 08:52:27 -08001240
joshualitt76e7fb62015-02-11 08:52:27 -08001241 int instanceCount = fGeoData.count();
joshualitt76e7fb62015-02-11 08:52:27 -08001242 size_t vertexStride = gp->getVertexStride();
1243 SkASSERT(vertexStride == sizeof(CircleVertex));
1244
bsalomonb5238a72015-05-05 07:49:49 -07001245 // drop out the middle quad if we're stroked
bsalomoncdaa97b2016-03-08 08:30:14 -08001246 int indicesPerInstance = fStroked ? kIndicesPerStrokeRRect : kIndicesPerRRect;
cdalton397536c2016-03-25 12:15:03 -07001247 SkAutoTUnref<const GrBuffer> indexBuffer(
bsalomoncdaa97b2016-03-08 08:30:14 -08001248 ref_rrect_index_buffer(fStroked, target->resourceProvider()));
joshualitt76e7fb62015-02-11 08:52:27 -08001249
bsalomonb5238a72015-05-05 07:49:49 -07001250 InstancedHelper helper;
bsalomon75398562015-08-17 12:55:38 -07001251 CircleVertex* verts = reinterpret_cast<CircleVertex*>(helper.init(target,
bsalomonb5238a72015-05-05 07:49:49 -07001252 kTriangles_GrPrimitiveType, vertexStride, indexBuffer, kVertsPerRRect,
1253 indicesPerInstance, instanceCount));
1254 if (!verts || !indexBuffer) {
joshualitt4b31de82015-03-05 14:33:41 -08001255 SkDebugf("Could not allocate vertices\n");
1256 return;
1257 }
1258
joshualitt76e7fb62015-02-11 08:52:27 -08001259 for (int i = 0; i < instanceCount; i++) {
joshualitt144c3c82015-11-30 12:30:13 -08001260 const Geometry& args = fGeoData[i];
joshualitt76e7fb62015-02-11 08:52:27 -08001261
brianosmanbb2ff942016-02-11 14:15:18 -08001262 GrColor color = args.fColor;
joshualitt76e7fb62015-02-11 08:52:27 -08001263 SkScalar outerRadius = args.fOuterRadius;
1264
egdanielbc227142015-04-21 06:28:08 -07001265 const SkRect& bounds = args.fDevBounds;
joshualitt76e7fb62015-02-11 08:52:27 -08001266
1267 SkScalar yCoords[4] = {
1268 bounds.fTop,
1269 bounds.fTop + outerRadius,
1270 bounds.fBottom - outerRadius,
1271 bounds.fBottom
1272 };
1273
1274 SkScalar yOuterRadii[4] = {-1, 0, 0, 1 };
1275 // The inner radius in the vertex data must be specified in normalized space.
1276 SkScalar innerRadius = args.fInnerRadius / args.fOuterRadius;
1277 for (int i = 0; i < 4; ++i) {
1278 verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]);
brianosmanbb2ff942016-02-11 14:15:18 -08001279 verts->fColor = color;
joshualitt76e7fb62015-02-11 08:52:27 -08001280 verts->fOffset = SkPoint::Make(-1, yOuterRadii[i]);
1281 verts->fOuterRadius = outerRadius;
1282 verts->fInnerRadius = innerRadius;
1283 verts++;
1284
1285 verts->fPos = SkPoint::Make(bounds.fLeft + outerRadius, yCoords[i]);
brianosmanbb2ff942016-02-11 14:15:18 -08001286 verts->fColor = color;
joshualitt76e7fb62015-02-11 08:52:27 -08001287 verts->fOffset = SkPoint::Make(0, yOuterRadii[i]);
1288 verts->fOuterRadius = outerRadius;
1289 verts->fInnerRadius = innerRadius;
1290 verts++;
1291
1292 verts->fPos = SkPoint::Make(bounds.fRight - outerRadius, yCoords[i]);
brianosmanbb2ff942016-02-11 14:15:18 -08001293 verts->fColor = color;
joshualitt76e7fb62015-02-11 08:52:27 -08001294 verts->fOffset = SkPoint::Make(0, yOuterRadii[i]);
1295 verts->fOuterRadius = outerRadius;
1296 verts->fInnerRadius = innerRadius;
1297 verts++;
1298
1299 verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]);
brianosmanbb2ff942016-02-11 14:15:18 -08001300 verts->fColor = color;
joshualitt76e7fb62015-02-11 08:52:27 -08001301 verts->fOffset = SkPoint::Make(1, yOuterRadii[i]);
1302 verts->fOuterRadius = outerRadius;
1303 verts->fInnerRadius = innerRadius;
1304 verts++;
1305 }
1306 }
1307
bsalomon342bfc22016-04-01 06:06:20 -07001308 helper.recordDraw(target, gp);
joshualitt76e7fb62015-02-11 08:52:27 -08001309 }
1310
bsalomoncb02b382015-08-12 11:14:50 -07001311 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override {
bsalomonabd30f52015-08-13 13:34:48 -07001312 RRectCircleRendererBatch* that = t->cast<RRectCircleRendererBatch>();
1313 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
1314 that->bounds(), caps)) {
joshualitt8cab9a72015-07-16 09:13:50 -07001315 return false;
1316 }
1317
bsalomoncdaa97b2016-03-08 08:30:14 -08001318 if (fStroked != that->fStroked) {
joshualitt76e7fb62015-02-11 08:52:27 -08001319 return false;
1320 }
1321
bsalomoncdaa97b2016-03-08 08:30:14 -08001322 if (!fViewMatrixIfUsingLocalCoords.cheapEqualTo(that->fViewMatrixIfUsingLocalCoords)) {
joshualitt76e7fb62015-02-11 08:52:27 -08001323 return false;
1324 }
1325
bsalomoncdaa97b2016-03-08 08:30:14 -08001326 fGeoData.push_back_n(that->fGeoData.count(), that->fGeoData.begin());
joshualitt99c7c072015-05-01 13:43:30 -07001327 this->joinBounds(that->bounds());
joshualitt76e7fb62015-02-11 08:52:27 -08001328 return true;
1329 }
1330
bsalomon4b4a7cc2016-07-08 04:42:54 -07001331 struct Geometry {
1332 GrColor fColor;
1333 SkScalar fInnerRadius;
1334 SkScalar fOuterRadius;
1335 SkRect fDevBounds;
1336 };
1337
bsalomoncdaa97b2016-03-08 08:30:14 -08001338 bool fStroked;
1339 SkMatrix fViewMatrixIfUsingLocalCoords;
joshualitt76e7fb62015-02-11 08:52:27 -08001340 SkSTArray<1, Geometry, true> fGeoData;
reed1b55a962015-09-17 20:16:13 -07001341
1342 typedef GrVertexBatch INHERITED;
joshualitt76e7fb62015-02-11 08:52:27 -08001343};
1344
bsalomonabd30f52015-08-13 13:34:48 -07001345class RRectEllipseRendererBatch : public GrVertexBatch {
joshualitt76e7fb62015-02-11 08:52:27 -08001346public:
reed1b55a962015-09-17 20:16:13 -07001347 DEFINE_BATCH_CLASS_ID
1348
bsalomon4b4a7cc2016-07-08 04:42:54 -07001349 // If devStrokeWidths values are <= 0 indicates then fill only. Otherwise, strokeOnly indicates
1350 // whether the rrect is only stroked or stroked and filled.
1351 static GrDrawBatch* Create(GrColor color, const SkMatrix& viewMatrix, const SkRect& devRect,
1352 float devXRadius, float devYRadius, SkVector devStrokeWidths,
1353 bool strokeOnly) {
1354 SkASSERT(devXRadius > 0.5);
1355 SkASSERT(devYRadius > 0.5);
1356 SkASSERT((devStrokeWidths.fX > 0) == (devStrokeWidths.fY > 0));
1357 SkASSERT(!(strokeOnly && devStrokeWidths.fX <= 0));
1358 SkScalar innerXRadius = 0.0f;
1359 SkScalar innerYRadius = 0.0f;
1360 SkRect bounds = devRect;
1361 bool stroked = false;
1362 if (devStrokeWidths.fX > 0) {
1363 if (SkScalarNearlyZero(devStrokeWidths.length())) {
1364 devStrokeWidths.set(SK_ScalarHalf, SK_ScalarHalf);
1365 } else {
1366 devStrokeWidths.scale(SK_ScalarHalf);
1367 }
joshualitt76e7fb62015-02-11 08:52:27 -08001368
bsalomon4b4a7cc2016-07-08 04:42:54 -07001369 // we only handle thick strokes for near-circular ellipses
1370 if (devStrokeWidths.length() > SK_ScalarHalf &&
1371 (SK_ScalarHalf*devXRadius > devYRadius || SK_ScalarHalf*devYRadius > devXRadius)) {
1372 return nullptr;
1373 }
1374
1375 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
1376 if (devStrokeWidths.fX*(devYRadius*devYRadius) <
1377 (devStrokeWidths.fY*devStrokeWidths.fY)*devXRadius) {
1378 return nullptr;
1379 }
1380 if (devStrokeWidths.fY*(devXRadius*devXRadius) <
1381 (devStrokeWidths.fX*devStrokeWidths.fX)*devYRadius) {
1382 return nullptr;
1383 }
1384
1385 // this is legit only if scale & translation (which should be the case at the moment)
1386 if (strokeOnly) {
1387 innerXRadius = devXRadius - devStrokeWidths.fX;
1388 innerYRadius = devYRadius - devStrokeWidths.fY;
1389 stroked = (innerXRadius >= 0 && innerYRadius >= 0);
1390 }
1391
1392 devXRadius += devStrokeWidths.fX;
1393 devYRadius += devStrokeWidths.fY;
1394 bounds.outset(devStrokeWidths.fX, devStrokeWidths.fY);
1395 }
1396
1397 // Expand the rect so all the pixels will be captured.
1398 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1399
1400 RRectEllipseRendererBatch* batch = new RRectEllipseRendererBatch();
1401 batch->fStroked = stroked;
1402 batch->fViewMatrixIfUsingLocalCoords = viewMatrix;
1403 batch->fGeoData.emplace_back(
1404 Geometry {color, devXRadius, devYRadius, innerXRadius, innerYRadius, bounds});
1405 batch->setBounds(bounds);
1406 return batch;
joshualitt76e7fb62015-02-11 08:52:27 -08001407 }
1408
mtklein36352bf2015-03-25 18:17:31 -07001409 const char* name() const override { return "RRectEllipseRendererBatch"; }
joshualitt76e7fb62015-02-11 08:52:27 -08001410
halcanary9d524f22016-03-29 09:03:52 -07001411 void computePipelineOptimizations(GrInitInvariantOutput* color,
ethannicholasff210322015-11-24 12:10:10 -08001412 GrInitInvariantOutput* coverage,
1413 GrBatchToXPOverrides* overrides) const override {
joshualitt76e7fb62015-02-11 08:52:27 -08001414 // When this is called on a batch, there is only one geometry bundle
ethannicholasff210322015-11-24 12:10:10 -08001415 color->setKnownFourComponents(fGeoData[0].fColor);
1416 coverage->setUnknownSingleComponent();
joshualitt76e7fb62015-02-11 08:52:27 -08001417 }
1418
bsalomone46f9fe2015-08-18 06:05:14 -07001419private:
bsalomon4b4a7cc2016-07-08 04:42:54 -07001420 RRectEllipseRendererBatch() : INHERITED(ClassID()) {}
1421
ethannicholasff210322015-11-24 12:10:10 -08001422 void initBatchTracker(const GrXPOverridesForBatch& overrides) override {
bsalomoncdaa97b2016-03-08 08:30:14 -08001423 // Handle overrides that affect our GP.
ethannicholasff210322015-11-24 12:10:10 -08001424 overrides.getOverrideColorIfSet(&fGeoData[0].fColor);
bsalomoncdaa97b2016-03-08 08:30:14 -08001425 if (!overrides.readsLocalCoords()) {
1426 fViewMatrixIfUsingLocalCoords.reset();
1427 }
joshualitt76e7fb62015-02-11 08:52:27 -08001428 }
1429
joshualitt144c3c82015-11-30 12:30:13 -08001430 void onPrepareDraws(Target* target) const override {
bsalomoncdaa97b2016-03-08 08:30:14 -08001431 SkMatrix localMatrix;
1432 if (!fViewMatrixIfUsingLocalCoords.invert(&localMatrix)) {
joshualitt76e7fb62015-02-11 08:52:27 -08001433 return;
1434 }
1435
1436 // Setup geometry processor
bsalomoncdaa97b2016-03-08 08:30:14 -08001437 SkAutoTUnref<GrGeometryProcessor> gp(new EllipseGeometryProcessor(fStroked, localMatrix));
joshualitt76e7fb62015-02-11 08:52:27 -08001438
joshualitt76e7fb62015-02-11 08:52:27 -08001439 int instanceCount = fGeoData.count();
joshualitt76e7fb62015-02-11 08:52:27 -08001440 size_t vertexStride = gp->getVertexStride();
1441 SkASSERT(vertexStride == sizeof(EllipseVertex));
1442
bsalomonb5238a72015-05-05 07:49:49 -07001443 // drop out the middle quad if we're stroked
bsalomoncdaa97b2016-03-08 08:30:14 -08001444 int indicesPerInstance = fStroked ? kIndicesPerStrokeRRect : kIndicesPerRRect;
cdalton397536c2016-03-25 12:15:03 -07001445 SkAutoTUnref<const GrBuffer> indexBuffer(
bsalomoncdaa97b2016-03-08 08:30:14 -08001446 ref_rrect_index_buffer(fStroked, target->resourceProvider()));
joshualitt76e7fb62015-02-11 08:52:27 -08001447
bsalomonb5238a72015-05-05 07:49:49 -07001448 InstancedHelper helper;
1449 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(
bsalomon75398562015-08-17 12:55:38 -07001450 helper.init(target, kTriangles_GrPrimitiveType, vertexStride, indexBuffer,
bsalomonb5238a72015-05-05 07:49:49 -07001451 kVertsPerRRect, indicesPerInstance, instanceCount));
1452 if (!verts || !indexBuffer) {
joshualitt4b31de82015-03-05 14:33:41 -08001453 SkDebugf("Could not allocate vertices\n");
1454 return;
1455 }
1456
joshualitt76e7fb62015-02-11 08:52:27 -08001457 for (int i = 0; i < instanceCount; i++) {
joshualitt144c3c82015-11-30 12:30:13 -08001458 const Geometry& args = fGeoData[i];
joshualitt76e7fb62015-02-11 08:52:27 -08001459
brianosmanbb2ff942016-02-11 14:15:18 -08001460 GrColor color = args.fColor;
1461
joshualitt76e7fb62015-02-11 08:52:27 -08001462 // Compute the reciprocals of the radii here to save time in the shader
1463 SkScalar xRadRecip = SkScalarInvert(args.fXRadius);
1464 SkScalar yRadRecip = SkScalarInvert(args.fYRadius);
1465 SkScalar xInnerRadRecip = SkScalarInvert(args.fInnerXRadius);
1466 SkScalar yInnerRadRecip = SkScalarInvert(args.fInnerYRadius);
1467
1468 // Extend the radii out half a pixel to antialias.
1469 SkScalar xOuterRadius = args.fXRadius + SK_ScalarHalf;
1470 SkScalar yOuterRadius = args.fYRadius + SK_ScalarHalf;
1471
egdanielbc227142015-04-21 06:28:08 -07001472 const SkRect& bounds = args.fDevBounds;
joshualitt76e7fb62015-02-11 08:52:27 -08001473
1474 SkScalar yCoords[4] = {
1475 bounds.fTop,
1476 bounds.fTop + yOuterRadius,
1477 bounds.fBottom - yOuterRadius,
1478 bounds.fBottom
1479 };
1480 SkScalar yOuterOffsets[4] = {
1481 yOuterRadius,
1482 SK_ScalarNearlyZero, // we're using inversesqrt() in shader, so can't be exactly 0
1483 SK_ScalarNearlyZero,
1484 yOuterRadius
1485 };
1486
1487 for (int i = 0; i < 4; ++i) {
1488 verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]);
brianosmanbb2ff942016-02-11 14:15:18 -08001489 verts->fColor = color;
joshualitt76e7fb62015-02-11 08:52:27 -08001490 verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]);
1491 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1492 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
1493 verts++;
1494
1495 verts->fPos = SkPoint::Make(bounds.fLeft + xOuterRadius, yCoords[i]);
brianosmanbb2ff942016-02-11 14:15:18 -08001496 verts->fColor = color;
joshualitt76e7fb62015-02-11 08:52:27 -08001497 verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]);
1498 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1499 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
1500 verts++;
1501
1502 verts->fPos = SkPoint::Make(bounds.fRight - xOuterRadius, yCoords[i]);
brianosmanbb2ff942016-02-11 14:15:18 -08001503 verts->fColor = color;
joshualitt76e7fb62015-02-11 08:52:27 -08001504 verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]);
1505 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1506 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
1507 verts++;
1508
1509 verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]);
brianosmanbb2ff942016-02-11 14:15:18 -08001510 verts->fColor = color;
joshualitt76e7fb62015-02-11 08:52:27 -08001511 verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]);
1512 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1513 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
1514 verts++;
1515 }
1516 }
bsalomon342bfc22016-04-01 06:06:20 -07001517 helper.recordDraw(target, gp);
joshualitt76e7fb62015-02-11 08:52:27 -08001518 }
1519
bsalomoncb02b382015-08-12 11:14:50 -07001520 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override {
bsalomonabd30f52015-08-13 13:34:48 -07001521 RRectEllipseRendererBatch* that = t->cast<RRectEllipseRendererBatch>();
1522
1523 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
1524 that->bounds(), caps)) {
joshualitt8cab9a72015-07-16 09:13:50 -07001525 return false;
1526 }
1527
bsalomoncdaa97b2016-03-08 08:30:14 -08001528 if (fStroked != that->fStroked) {
joshualitt76e7fb62015-02-11 08:52:27 -08001529 return false;
1530 }
1531
bsalomoncdaa97b2016-03-08 08:30:14 -08001532 if (!fViewMatrixIfUsingLocalCoords.cheapEqualTo(that->fViewMatrixIfUsingLocalCoords)) {
joshualitt76e7fb62015-02-11 08:52:27 -08001533 return false;
1534 }
1535
bsalomoncdaa97b2016-03-08 08:30:14 -08001536 fGeoData.push_back_n(that->fGeoData.count(), that->fGeoData.begin());
joshualitt99c7c072015-05-01 13:43:30 -07001537 this->joinBounds(that->bounds());
joshualitt76e7fb62015-02-11 08:52:27 -08001538 return true;
1539 }
1540
bsalomon4b4a7cc2016-07-08 04:42:54 -07001541 struct Geometry {
1542 GrColor fColor;
1543 SkScalar fXRadius;
1544 SkScalar fYRadius;
1545 SkScalar fInnerXRadius;
1546 SkScalar fInnerYRadius;
1547 SkRect fDevBounds;
1548 };
1549
bsalomoncdaa97b2016-03-08 08:30:14 -08001550 bool fStroked;
1551 SkMatrix fViewMatrixIfUsingLocalCoords;
1552 SkSTArray<1, Geometry, true> fGeoData;
reed1b55a962015-09-17 20:16:13 -07001553
1554 typedef GrVertexBatch INHERITED;
joshualitt76e7fb62015-02-11 08:52:27 -08001555};
1556
bsalomonabd30f52015-08-13 13:34:48 -07001557static GrDrawBatch* create_rrect_batch(GrColor color,
1558 const SkMatrix& viewMatrix,
1559 const SkRRect& rrect,
1560 const SkStrokeRec& stroke) {
joshualitt3e708c52015-04-30 13:49:27 -07001561 SkASSERT(viewMatrix.rectStaysRect());
1562 SkASSERT(rrect.isSimple());
1563 SkASSERT(!rrect.isOval());
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001564
joshualitt3e708c52015-04-30 13:49:27 -07001565 // RRect batchs only handle simple, but not too simple, rrects
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001566 // do any matrix crunching before we reset the draw state for device coords
1567 const SkRect& rrectBounds = rrect.getBounds();
joshualittd96a67b2015-05-05 14:09:05 -07001568 SkRect bounds;
1569 viewMatrix.mapRect(&bounds, rrectBounds);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001570
1571 SkVector radii = rrect.getSimpleRadii();
joshualitt8059eb92014-12-29 15:10:07 -08001572 SkScalar xRadius = SkScalarAbs(viewMatrix[SkMatrix::kMScaleX]*radii.fX +
1573 viewMatrix[SkMatrix::kMSkewY]*radii.fY);
1574 SkScalar yRadius = SkScalarAbs(viewMatrix[SkMatrix::kMSkewX]*radii.fX +
1575 viewMatrix[SkMatrix::kMScaleY]*radii.fY);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001576
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001577 SkStrokeRec::Style style = stroke.getStyle();
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001578
bsalomon4b4a7cc2016-07-08 04:42:54 -07001579 // Do (potentially) anisotropic mapping of stroke. Use -1s to indicate fill-only draws.
1580 SkVector scaledStroke = {-1, -1};
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001581 SkScalar strokeWidth = stroke.getWidth();
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001582
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001583 bool isStrokeOnly = SkStrokeRec::kStroke_Style == style ||
1584 SkStrokeRec::kHairline_Style == style;
1585 bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == style;
1586
1587 if (hasStroke) {
1588 if (SkStrokeRec::kHairline_Style == style) {
1589 scaledStroke.set(1, 1);
1590 } else {
joshualitt8059eb92014-12-29 15:10:07 -08001591 scaledStroke.fX = SkScalarAbs(strokeWidth*(viewMatrix[SkMatrix::kMScaleX] +
1592 viewMatrix[SkMatrix::kMSkewY]));
1593 scaledStroke.fY = SkScalarAbs(strokeWidth*(viewMatrix[SkMatrix::kMSkewX] +
1594 viewMatrix[SkMatrix::kMScaleY]));
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001595 }
1596
1597 // if half of strokewidth is greater than radius, we don't handle that right now
1598 if (SK_ScalarHalf*scaledStroke.fX > xRadius || SK_ScalarHalf*scaledStroke.fY > yRadius) {
halcanary96fcdcc2015-08-27 07:41:13 -07001599 return nullptr;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001600 }
1601 }
1602
1603 // The way the effect interpolates the offset-to-ellipse/circle-center attribute only works on
1604 // the interior of the rrect if the radii are >= 0.5. Otherwise, the inner rect of the nine-
1605 // patch will have fractional coverage. This only matters when the interior is actually filled.
1606 // We could consider falling back to rect rendering here, since a tiny radius is
1607 // indistinguishable from a square corner.
1608 if (!isStrokeOnly && (SK_ScalarHalf > xRadius || SK_ScalarHalf > yRadius)) {
halcanary96fcdcc2015-08-27 07:41:13 -07001609 return nullptr;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001610 }
1611
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001612 // if the corners are circles, use the circle renderer
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001613 if ((!hasStroke || scaledStroke.fX == scaledStroke.fY) && xRadius == yRadius) {
bsalomon4b4a7cc2016-07-08 04:42:54 -07001614 return new RRectCircleRendererBatch(color, viewMatrix, bounds, xRadius, scaledStroke.fX,
1615 isStrokeOnly);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001616 // otherwise we use the ellipse renderer
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001617 } else {
bsalomon4b4a7cc2016-07-08 04:42:54 -07001618 return RRectEllipseRendererBatch::Create(color, viewMatrix, bounds, xRadius, yRadius,
1619 scaledStroke, isStrokeOnly);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001620
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001621 }
joshualitt3e708c52015-04-30 13:49:27 -07001622}
1623
robertphillipsb56f9272016-02-25 11:03:52 -08001624GrDrawBatch* GrOvalRenderer::CreateRRectBatch(GrColor color,
robertphillips0cc2f852016-02-24 13:36:56 -08001625 const SkMatrix& viewMatrix,
robertphillips0cc2f852016-02-24 13:36:56 -08001626 const SkRRect& rrect,
1627 const SkStrokeRec& stroke,
1628 GrShaderCaps* shaderCaps) {
robertphillips0cc2f852016-02-24 13:36:56 -08001629 if (rrect.isOval()) {
robertphillipsb56f9272016-02-25 11:03:52 -08001630 return CreateOvalBatch(color, viewMatrix, rrect.getBounds(), stroke, shaderCaps);
joshualitt3e708c52015-04-30 13:49:27 -07001631 }
1632
1633 if (!viewMatrix.rectStaysRect() || !rrect.isSimple()) {
robertphillips0cc2f852016-02-24 13:36:56 -08001634 return nullptr;
joshualitt3e708c52015-04-30 13:49:27 -07001635 }
1636
robertphillips0cc2f852016-02-24 13:36:56 -08001637 return create_rrect_batch(color, viewMatrix, rrect, stroke);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001638}
joshualitt3e708c52015-04-30 13:49:27 -07001639
bsalomon4b4a7cc2016-07-08 04:42:54 -07001640///////////////////////////////////////////////////////////////////////////////
1641
1642GrDrawBatch* GrOvalRenderer::CreateOvalBatch(GrColor color,
1643 const SkMatrix& viewMatrix,
1644 const SkRect& oval,
1645 const SkStrokeRec& stroke,
1646 GrShaderCaps* shaderCaps) {
1647 // we can draw circles
1648 if (SkScalarNearlyEqual(oval.width(), oval.height()) && circle_stays_circle(viewMatrix)) {
1649 return new CircleBatch(color, viewMatrix, oval, stroke);
1650 }
1651
1652 // if we have shader derivative support, render as device-independent
1653 if (shaderCaps->shaderDerivativeSupport()) {
1654 return DIEllipseBatch::Create(color, viewMatrix, oval, stroke);
1655 }
1656
1657 // otherwise axis-aligned ellipses only
1658 if (viewMatrix.rectStaysRect()) {
1659 return EllipseBatch::Create(color, viewMatrix, oval, stroke);
1660 }
1661
1662 return nullptr;
1663}
1664
1665///////////////////////////////////////////////////////////////////////////////
joshualitt3e708c52015-04-30 13:49:27 -07001666
1667#ifdef GR_TEST_UTILS
1668
bsalomonabd30f52015-08-13 13:34:48 -07001669DRAW_BATCH_TEST_DEFINE(CircleBatch) {
joshualitt3e708c52015-04-30 13:49:27 -07001670 SkMatrix viewMatrix = GrTest::TestMatrix(random);
1671 GrColor color = GrRandomColor(random);
joshualitt6c891102015-05-13 08:51:49 -07001672 SkRect circle = GrTest::TestSquare(random);
bsalomon4b4a7cc2016-07-08 04:42:54 -07001673 return new CircleBatch(color, viewMatrix, circle, GrTest::TestStrokeRec(random));
joshualitt3e708c52015-04-30 13:49:27 -07001674}
1675
bsalomonabd30f52015-08-13 13:34:48 -07001676DRAW_BATCH_TEST_DEFINE(EllipseBatch) {
joshualitt3e708c52015-04-30 13:49:27 -07001677 SkMatrix viewMatrix = GrTest::TestMatrixRectStaysRect(random);
1678 GrColor color = GrRandomColor(random);
joshualitt6c891102015-05-13 08:51:49 -07001679 SkRect ellipse = GrTest::TestSquare(random);
bsalomon4b4a7cc2016-07-08 04:42:54 -07001680 return EllipseBatch::Create(color, viewMatrix, ellipse, GrTest::TestStrokeRec(random));
joshualitt3e708c52015-04-30 13:49:27 -07001681}
1682
bsalomonabd30f52015-08-13 13:34:48 -07001683DRAW_BATCH_TEST_DEFINE(DIEllipseBatch) {
joshualitt3e708c52015-04-30 13:49:27 -07001684 SkMatrix viewMatrix = GrTest::TestMatrix(random);
1685 GrColor color = GrRandomColor(random);
joshualitt6c891102015-05-13 08:51:49 -07001686 SkRect ellipse = GrTest::TestSquare(random);
bsalomon4b4a7cc2016-07-08 04:42:54 -07001687 return DIEllipseBatch::Create(color, viewMatrix, ellipse, GrTest::TestStrokeRec(random));
joshualitt3e708c52015-04-30 13:49:27 -07001688}
1689
bsalomonabd30f52015-08-13 13:34:48 -07001690DRAW_BATCH_TEST_DEFINE(RRectBatch) {
joshualitt3e708c52015-04-30 13:49:27 -07001691 SkMatrix viewMatrix = GrTest::TestMatrixRectStaysRect(random);
1692 GrColor color = GrRandomColor(random);
1693 const SkRRect& rrect = GrTest::TestRRectSimple(random);
joshualitt21279c72015-05-11 07:21:37 -07001694 return create_rrect_batch(color, viewMatrix, rrect, GrTest::TestStrokeRec(random));
joshualitt3e708c52015-04-30 13:49:27 -07001695}
1696
1697#endif