blob: aec9b76ba85f986236a0382fcb451f84beb5e2c2 [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 });
bsalomon88cf17d2016-07-08 06:40:56 -0700575 // Use the original radius and stroke radius for the bounds so that it does not include the
576 // AA bloat.
577 radius += halfWidth;
578 this->setBounds({center.fX - radius, center.fY - radius,
579 center.fX + radius, center.fY + radius},
580 HasAABloat::kYes, IsZeroArea::kNo);
bsalomon4b4a7cc2016-07-08 04:42:54 -0700581 fStroked = isStrokeOnly && innerRadius > 0;
bsalomoncdaa97b2016-03-08 08:30:14 -0800582 }
bsalomon4b4a7cc2016-07-08 04:42:54 -0700583
mtklein36352bf2015-03-25 18:17:31 -0700584 const char* name() const override { return "CircleBatch"; }
joshualitt76e7fb62015-02-11 08:52:27 -0800585
robertphillipse004bfc2015-11-16 09:06:59 -0800586 SkString dumpInfo() const override {
587 SkString string;
588 for (int i = 0; i < fGeoData.count(); ++i) {
589 string.appendf("Color: 0x%08x Rect [L: %.2f, T: %.2f, R: %.2f, B: %.2f],"
590 "InnerRad: %.2f, OuterRad: %.2f\n",
591 fGeoData[i].fColor,
592 fGeoData[i].fDevBounds.fLeft, fGeoData[i].fDevBounds.fTop,
593 fGeoData[i].fDevBounds.fRight, fGeoData[i].fDevBounds.fBottom,
594 fGeoData[i].fInnerRadius,
595 fGeoData[i].fOuterRadius);
596 }
597 string.append(INHERITED::dumpInfo());
598 return string;
599 }
600
halcanary9d524f22016-03-29 09:03:52 -0700601 void computePipelineOptimizations(GrInitInvariantOutput* color,
ethannicholasff210322015-11-24 12:10:10 -0800602 GrInitInvariantOutput* coverage,
603 GrBatchToXPOverrides* overrides) const override {
joshualitt76e7fb62015-02-11 08:52:27 -0800604 // When this is called on a batch, there is only one geometry bundle
ethannicholasff210322015-11-24 12:10:10 -0800605 color->setKnownFourComponents(fGeoData[0].fColor);
606 coverage->setUnknownSingleComponent();
joshualitt76e7fb62015-02-11 08:52:27 -0800607 }
608
bsalomone46f9fe2015-08-18 06:05:14 -0700609private:
ethannicholasff210322015-11-24 12:10:10 -0800610 void initBatchTracker(const GrXPOverridesForBatch& overrides) override {
bsalomoncdaa97b2016-03-08 08:30:14 -0800611 // Handle any overrides that affect our GP.
ethannicholasff210322015-11-24 12:10:10 -0800612 overrides.getOverrideColorIfSet(&fGeoData[0].fColor);
bsalomoncdaa97b2016-03-08 08:30:14 -0800613 if (!overrides.readsLocalCoords()) {
614 fViewMatrixIfUsingLocalCoords.reset();
615 }
joshualitt76e7fb62015-02-11 08:52:27 -0800616 }
617
joshualitt144c3c82015-11-30 12:30:13 -0800618 void onPrepareDraws(Target* target) const override {
bsalomoncdaa97b2016-03-08 08:30:14 -0800619 SkMatrix localMatrix;
620 if (!fViewMatrixIfUsingLocalCoords.invert(&localMatrix)) {
joshualitt76e7fb62015-02-11 08:52:27 -0800621 return;
622 }
623
624 // Setup geometry processor
bsalomoncdaa97b2016-03-08 08:30:14 -0800625 SkAutoTUnref<GrGeometryProcessor> gp(new CircleGeometryProcessor(fStroked, localMatrix));
joshualitt76e7fb62015-02-11 08:52:27 -0800626
joshualitt76e7fb62015-02-11 08:52:27 -0800627 int instanceCount = fGeoData.count();
joshualitt76e7fb62015-02-11 08:52:27 -0800628 size_t vertexStride = gp->getVertexStride();
629 SkASSERT(vertexStride == sizeof(CircleVertex));
bsalomonb5238a72015-05-05 07:49:49 -0700630 QuadHelper helper;
bsalomon75398562015-08-17 12:55:38 -0700631 CircleVertex* verts = reinterpret_cast<CircleVertex*>(helper.init(target, vertexStride,
bsalomonb5238a72015-05-05 07:49:49 -0700632 instanceCount));
633 if (!verts) {
joshualitt4b31de82015-03-05 14:33:41 -0800634 return;
635 }
636
joshualitt76e7fb62015-02-11 08:52:27 -0800637 for (int i = 0; i < instanceCount; i++) {
joshualitt144c3c82015-11-30 12:30:13 -0800638 const Geometry& geom = fGeoData[i];
joshualitt76e7fb62015-02-11 08:52:27 -0800639
brianosmanbb2ff942016-02-11 14:15:18 -0800640 GrColor color = geom.fColor;
bsalomonb5238a72015-05-05 07:49:49 -0700641 SkScalar innerRadius = geom.fInnerRadius;
642 SkScalar outerRadius = geom.fOuterRadius;
joshualitt76e7fb62015-02-11 08:52:27 -0800643
bsalomonb5238a72015-05-05 07:49:49 -0700644 const SkRect& bounds = geom.fDevBounds;
joshualitt76e7fb62015-02-11 08:52:27 -0800645
646 // The inner radius in the vertex data must be specified in normalized space.
647 innerRadius = innerRadius / outerRadius;
648 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
brianosmanbb2ff942016-02-11 14:15:18 -0800649 verts[0].fColor = color;
joshualitt76e7fb62015-02-11 08:52:27 -0800650 verts[0].fOffset = SkPoint::Make(-1, -1);
651 verts[0].fOuterRadius = outerRadius;
652 verts[0].fInnerRadius = innerRadius;
653
654 verts[1].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
brianosmanbb2ff942016-02-11 14:15:18 -0800655 verts[1].fColor = color;
joshualitt76e7fb62015-02-11 08:52:27 -0800656 verts[1].fOffset = SkPoint::Make(-1, 1);
657 verts[1].fOuterRadius = outerRadius;
658 verts[1].fInnerRadius = innerRadius;
659
660 verts[2].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
brianosmanbb2ff942016-02-11 14:15:18 -0800661 verts[2].fColor = color;
joshualitt76e7fb62015-02-11 08:52:27 -0800662 verts[2].fOffset = SkPoint::Make(1, 1);
663 verts[2].fOuterRadius = outerRadius;
664 verts[2].fInnerRadius = innerRadius;
665
666 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
brianosmanbb2ff942016-02-11 14:15:18 -0800667 verts[3].fColor = color;
joshualitt76e7fb62015-02-11 08:52:27 -0800668 verts[3].fOffset = SkPoint::Make(1, -1);
669 verts[3].fOuterRadius = outerRadius;
670 verts[3].fInnerRadius = innerRadius;
671
bsalomonb5238a72015-05-05 07:49:49 -0700672 verts += kVerticesPerQuad;
joshualitt76e7fb62015-02-11 08:52:27 -0800673 }
bsalomon342bfc22016-04-01 06:06:20 -0700674 helper.recordDraw(target, gp);
joshualitt76e7fb62015-02-11 08:52:27 -0800675 }
676
bsalomoncb02b382015-08-12 11:14:50 -0700677 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override {
bsalomonabd30f52015-08-13 13:34:48 -0700678 CircleBatch* that = t->cast<CircleBatch>();
679 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
680 that->bounds(), caps)) {
joshualitt8cab9a72015-07-16 09:13:50 -0700681 return false;
682 }
683
bsalomoncdaa97b2016-03-08 08:30:14 -0800684 if (this->fStroked != that->fStroked) {
joshualitt76e7fb62015-02-11 08:52:27 -0800685 return false;
686 }
687
bsalomoncdaa97b2016-03-08 08:30:14 -0800688 if (!fViewMatrixIfUsingLocalCoords.cheapEqualTo(that->fViewMatrixIfUsingLocalCoords)) {
joshualitt76e7fb62015-02-11 08:52:27 -0800689 return false;
690 }
691
bsalomoncdaa97b2016-03-08 08:30:14 -0800692 fGeoData.push_back_n(that->fGeoData.count(), that->fGeoData.begin());
bsalomon88cf17d2016-07-08 06:40:56 -0700693 this->joinBounds(*that);
joshualitt76e7fb62015-02-11 08:52:27 -0800694 return true;
695 }
696
bsalomon4b4a7cc2016-07-08 04:42:54 -0700697 struct Geometry {
698 GrColor fColor;
699 SkScalar fInnerRadius;
700 SkScalar fOuterRadius;
701 SkRect fDevBounds;
702 };
703
bsalomoncdaa97b2016-03-08 08:30:14 -0800704 bool fStroked;
705 SkMatrix fViewMatrixIfUsingLocalCoords;
joshualitt76e7fb62015-02-11 08:52:27 -0800706 SkSTArray<1, Geometry, true> fGeoData;
reed1b55a962015-09-17 20:16:13 -0700707
708 typedef GrVertexBatch INHERITED;
joshualitt76e7fb62015-02-11 08:52:27 -0800709};
710
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000711///////////////////////////////////////////////////////////////////////////////
712
bsalomonabd30f52015-08-13 13:34:48 -0700713class EllipseBatch : public GrVertexBatch {
joshualitt76e7fb62015-02-11 08:52:27 -0800714public:
reed1b55a962015-09-17 20:16:13 -0700715 DEFINE_BATCH_CLASS_ID
bsalomon4b4a7cc2016-07-08 04:42:54 -0700716 static GrDrawBatch* Create(GrColor color, const SkMatrix& viewMatrix, const SkRect& ellipse,
717 const SkStrokeRec& stroke) {
718 SkASSERT(viewMatrix.rectStaysRect());
reed1b55a962015-09-17 20:16:13 -0700719
bsalomon4b4a7cc2016-07-08 04:42:54 -0700720 // do any matrix crunching before we reset the draw state for device coords
721 SkPoint center = SkPoint::Make(ellipse.centerX(), ellipse.centerY());
722 viewMatrix.mapPoints(&center, 1);
723 SkScalar ellipseXRadius = SkScalarHalf(ellipse.width());
724 SkScalar ellipseYRadius = SkScalarHalf(ellipse.height());
725 SkScalar xRadius = SkScalarAbs(viewMatrix[SkMatrix::kMScaleX]*ellipseXRadius +
726 viewMatrix[SkMatrix::kMSkewY]*ellipseYRadius);
727 SkScalar yRadius = SkScalarAbs(viewMatrix[SkMatrix::kMSkewX]*ellipseXRadius +
728 viewMatrix[SkMatrix::kMScaleY]*ellipseYRadius);
joshualitt76e7fb62015-02-11 08:52:27 -0800729
bsalomon4b4a7cc2016-07-08 04:42:54 -0700730 // do (potentially) anisotropic mapping of stroke
731 SkVector scaledStroke;
732 SkScalar strokeWidth = stroke.getWidth();
733 scaledStroke.fX = SkScalarAbs(strokeWidth*(viewMatrix[SkMatrix::kMScaleX] +
734 viewMatrix[SkMatrix::kMSkewY]));
735 scaledStroke.fY = SkScalarAbs(strokeWidth*(viewMatrix[SkMatrix::kMSkewX] +
736 viewMatrix[SkMatrix::kMScaleY]));
737
738 SkStrokeRec::Style style = stroke.getStyle();
739 bool isStrokeOnly = SkStrokeRec::kStroke_Style == style ||
740 SkStrokeRec::kHairline_Style == style;
741 bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == style;
742
743 SkScalar innerXRadius = 0;
744 SkScalar innerYRadius = 0;
745 if (hasStroke) {
746 if (SkScalarNearlyZero(scaledStroke.length())) {
747 scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf);
748 } else {
749 scaledStroke.scale(SK_ScalarHalf);
750 }
751
752 // we only handle thick strokes for near-circular ellipses
753 if (scaledStroke.length() > SK_ScalarHalf &&
754 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
755 return nullptr;
756 }
757
758 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
759 if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius ||
760 scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) {
761 return nullptr;
762 }
763
764 // this is legit only if scale & translation (which should be the case at the moment)
765 if (isStrokeOnly) {
766 innerXRadius = xRadius - scaledStroke.fX;
767 innerYRadius = yRadius - scaledStroke.fY;
768 }
769
770 xRadius += scaledStroke.fX;
771 yRadius += scaledStroke.fY;
772 }
773
774 EllipseBatch* batch = new EllipseBatch();
775 batch->fGeoData.emplace_back(Geometry {
776 color,
777 xRadius,
778 yRadius,
779 innerXRadius,
780 innerYRadius,
781 SkRect::MakeLTRB(center.fX - xRadius, center.fY - yRadius,
782 center.fX + xRadius, center.fY + yRadius)
783 });
784
bsalomon88cf17d2016-07-08 06:40:56 -0700785 batch->setBounds(batch->fGeoData.back().fDevBounds, HasAABloat::kYes, IsZeroArea::kNo);
786
bsalomon4b4a7cc2016-07-08 04:42:54 -0700787 // Outset bounds to include half-pixel width antialiasing.
788 batch->fGeoData[0].fDevBounds.outset(SK_ScalarHalf, SK_ScalarHalf);
789
790 batch->fStroked = isStrokeOnly && innerXRadius > 0 && innerYRadius > 0;
791 batch->fViewMatrixIfUsingLocalCoords = viewMatrix;
bsalomon4b4a7cc2016-07-08 04:42:54 -0700792 return batch;
bsalomoncdaa97b2016-03-08 08:30:14 -0800793 }
joshualitt76e7fb62015-02-11 08:52:27 -0800794
mtklein36352bf2015-03-25 18:17:31 -0700795 const char* name() const override { return "EllipseBatch"; }
joshualitt76e7fb62015-02-11 08:52:27 -0800796
halcanary9d524f22016-03-29 09:03:52 -0700797 void computePipelineOptimizations(GrInitInvariantOutput* color,
ethannicholasff210322015-11-24 12:10:10 -0800798 GrInitInvariantOutput* coverage,
799 GrBatchToXPOverrides* overrides) const override {
joshualitt76e7fb62015-02-11 08:52:27 -0800800 // When this is called on a batch, there is only one geometry bundle
ethannicholasff210322015-11-24 12:10:10 -0800801 color->setKnownFourComponents(fGeoData[0].fColor);
802 coverage->setUnknownSingleComponent();
joshualitt76e7fb62015-02-11 08:52:27 -0800803 }
804
bsalomone46f9fe2015-08-18 06:05:14 -0700805private:
bsalomon4b4a7cc2016-07-08 04:42:54 -0700806 EllipseBatch() : INHERITED(ClassID()) {}
807
ethannicholasff210322015-11-24 12:10:10 -0800808 void initBatchTracker(const GrXPOverridesForBatch& overrides) override {
bsalomoncdaa97b2016-03-08 08:30:14 -0800809 // Handle any overrides that affect our GP.
ethannicholasff210322015-11-24 12:10:10 -0800810 if (!overrides.readsCoverage()) {
joshualitt76e7fb62015-02-11 08:52:27 -0800811 fGeoData[0].fColor = GrColor_ILLEGAL;
joshualitt76e7fb62015-02-11 08:52:27 -0800812 }
bsalomoncdaa97b2016-03-08 08:30:14 -0800813 if (!overrides.readsLocalCoords()) {
814 fViewMatrixIfUsingLocalCoords.reset();
815 }
joshualitt76e7fb62015-02-11 08:52:27 -0800816 }
817
joshualitt144c3c82015-11-30 12:30:13 -0800818 void onPrepareDraws(Target* target) const override {
bsalomoncdaa97b2016-03-08 08:30:14 -0800819 SkMatrix localMatrix;
820 if (!fViewMatrixIfUsingLocalCoords.invert(&localMatrix)) {
joshualitt76e7fb62015-02-11 08:52:27 -0800821 return;
822 }
823
824 // Setup geometry processor
bsalomoncdaa97b2016-03-08 08:30:14 -0800825 SkAutoTUnref<GrGeometryProcessor> gp(new EllipseGeometryProcessor(fStroked, localMatrix));
joshualitt76e7fb62015-02-11 08:52:27 -0800826
joshualitt76e7fb62015-02-11 08:52:27 -0800827 int instanceCount = fGeoData.count();
bsalomonb5238a72015-05-05 07:49:49 -0700828 QuadHelper helper;
joshualitt76e7fb62015-02-11 08:52:27 -0800829 size_t vertexStride = gp->getVertexStride();
joshualitt19e00582015-02-11 17:36:30 -0800830 SkASSERT(vertexStride == sizeof(EllipseVertex));
bsalomonb5238a72015-05-05 07:49:49 -0700831 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(
bsalomon75398562015-08-17 12:55:38 -0700832 helper.init(target, vertexStride, instanceCount));
bsalomonb5238a72015-05-05 07:49:49 -0700833 if (!verts) {
joshualitt4b31de82015-03-05 14:33:41 -0800834 return;
835 }
836
bsalomon8415abe2015-05-04 11:41:41 -0700837 for (int i = 0; i < instanceCount; i++) {
joshualitt144c3c82015-11-30 12:30:13 -0800838 const Geometry& geom = fGeoData[i];
bsalomon8415abe2015-05-04 11:41:41 -0700839
brianosmanbb2ff942016-02-11 14:15:18 -0800840 GrColor color = geom.fColor;
bsalomonb5238a72015-05-05 07:49:49 -0700841 SkScalar xRadius = geom.fXRadius;
842 SkScalar yRadius = geom.fYRadius;
joshualitt76e7fb62015-02-11 08:52:27 -0800843
844 // Compute the reciprocals of the radii here to save time in the shader
845 SkScalar xRadRecip = SkScalarInvert(xRadius);
846 SkScalar yRadRecip = SkScalarInvert(yRadius);
bsalomonb5238a72015-05-05 07:49:49 -0700847 SkScalar xInnerRadRecip = SkScalarInvert(geom.fInnerXRadius);
848 SkScalar yInnerRadRecip = SkScalarInvert(geom.fInnerYRadius);
joshualitt76e7fb62015-02-11 08:52:27 -0800849
bsalomonb5238a72015-05-05 07:49:49 -0700850 const SkRect& bounds = geom.fDevBounds;
joshualitt76e7fb62015-02-11 08:52:27 -0800851
vjiaoblack977996d2016-06-30 12:20:54 -0700852 // fOffsets are expanded from xyRadii to include the half-pixel antialiasing width.
853 SkScalar xMaxOffset = xRadius + SK_ScalarHalf;
854 SkScalar yMaxOffset = yRadius + SK_ScalarHalf;
855
joshualitt76e7fb62015-02-11 08:52:27 -0800856 // The inner radius in the vertex data must be specified in normalized space.
857 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
brianosmanbb2ff942016-02-11 14:15:18 -0800858 verts[0].fColor = color;
vjiaoblack977996d2016-06-30 12:20:54 -0700859 verts[0].fOffset = SkPoint::Make(-xMaxOffset, -yMaxOffset);
joshualitt76e7fb62015-02-11 08:52:27 -0800860 verts[0].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
861 verts[0].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
862
863 verts[1].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
brianosmanbb2ff942016-02-11 14:15:18 -0800864 verts[1].fColor = color;
vjiaoblack977996d2016-06-30 12:20:54 -0700865 verts[1].fOffset = SkPoint::Make(-xMaxOffset, yMaxOffset);
joshualitt76e7fb62015-02-11 08:52:27 -0800866 verts[1].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
867 verts[1].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
868
869 verts[2].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
brianosmanbb2ff942016-02-11 14:15:18 -0800870 verts[2].fColor = color;
vjiaoblack977996d2016-06-30 12:20:54 -0700871 verts[2].fOffset = SkPoint::Make(xMaxOffset, yMaxOffset);
joshualitt76e7fb62015-02-11 08:52:27 -0800872 verts[2].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
873 verts[2].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
874
875 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
brianosmanbb2ff942016-02-11 14:15:18 -0800876 verts[3].fColor = color;
vjiaoblack977996d2016-06-30 12:20:54 -0700877 verts[3].fOffset = SkPoint::Make(xMaxOffset, -yMaxOffset);
joshualitt76e7fb62015-02-11 08:52:27 -0800878 verts[3].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
879 verts[3].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
880
bsalomonb5238a72015-05-05 07:49:49 -0700881 verts += kVerticesPerQuad;
joshualitt76e7fb62015-02-11 08:52:27 -0800882 }
bsalomon342bfc22016-04-01 06:06:20 -0700883 helper.recordDraw(target, gp);
joshualitt76e7fb62015-02-11 08:52:27 -0800884 }
885
bsalomoncb02b382015-08-12 11:14:50 -0700886 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override {
bsalomonabd30f52015-08-13 13:34:48 -0700887 EllipseBatch* that = t->cast<EllipseBatch>();
888
889 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
890 that->bounds(), caps)) {
joshualitt8cab9a72015-07-16 09:13:50 -0700891 return false;
892 }
893
bsalomoncdaa97b2016-03-08 08:30:14 -0800894 if (fStroked != that->fStroked) {
joshualitt76e7fb62015-02-11 08:52:27 -0800895 return false;
896 }
897
bsalomoncdaa97b2016-03-08 08:30:14 -0800898 if (!fViewMatrixIfUsingLocalCoords.cheapEqualTo(that->fViewMatrixIfUsingLocalCoords)) {
joshualitt76e7fb62015-02-11 08:52:27 -0800899 return false;
900 }
901
bsalomoncdaa97b2016-03-08 08:30:14 -0800902 fGeoData.push_back_n(that->fGeoData.count(), that->fGeoData.begin());
bsalomon88cf17d2016-07-08 06:40:56 -0700903 this->joinBounds(*that);
joshualitt76e7fb62015-02-11 08:52:27 -0800904 return true;
905 }
906
bsalomon4b4a7cc2016-07-08 04:42:54 -0700907 struct Geometry {
908 GrColor fColor;
909 SkScalar fXRadius;
910 SkScalar fYRadius;
911 SkScalar fInnerXRadius;
912 SkScalar fInnerYRadius;
913 SkRect fDevBounds;
914 };
joshualitt76e7fb62015-02-11 08:52:27 -0800915
bsalomoncdaa97b2016-03-08 08:30:14 -0800916 bool fStroked;
917 SkMatrix fViewMatrixIfUsingLocalCoords;
joshualitt76e7fb62015-02-11 08:52:27 -0800918 SkSTArray<1, Geometry, true> fGeoData;
reed1b55a962015-09-17 20:16:13 -0700919
920 typedef GrVertexBatch INHERITED;
joshualitt76e7fb62015-02-11 08:52:27 -0800921};
922
joshualitt76e7fb62015-02-11 08:52:27 -0800923/////////////////////////////////////////////////////////////////////////////////////////////////
924
bsalomonabd30f52015-08-13 13:34:48 -0700925class DIEllipseBatch : public GrVertexBatch {
joshualitt76e7fb62015-02-11 08:52:27 -0800926public:
reed1b55a962015-09-17 20:16:13 -0700927 DEFINE_BATCH_CLASS_ID
928
bsalomon4b4a7cc2016-07-08 04:42:54 -0700929 static GrDrawBatch* Create(GrColor color,
930 const SkMatrix& viewMatrix,
931 const SkRect& ellipse,
932 const SkStrokeRec& stroke) {
933 SkPoint center = SkPoint::Make(ellipse.centerX(), ellipse.centerY());
934 SkScalar xRadius = SkScalarHalf(ellipse.width());
935 SkScalar yRadius = SkScalarHalf(ellipse.height());
joshualitt76e7fb62015-02-11 08:52:27 -0800936
bsalomon4b4a7cc2016-07-08 04:42:54 -0700937 SkStrokeRec::Style style = stroke.getStyle();
938 DIEllipseStyle dieStyle = (SkStrokeRec::kStroke_Style == style) ?
939 DIEllipseStyle::kStroke :
940 (SkStrokeRec::kHairline_Style == style) ?
941 DIEllipseStyle::kHairline : DIEllipseStyle::kFill;
942
943 SkScalar innerXRadius = 0;
944 SkScalar innerYRadius = 0;
945 if (SkStrokeRec::kFill_Style != style && SkStrokeRec::kHairline_Style != style) {
946 SkScalar strokeWidth = stroke.getWidth();
947
948 if (SkScalarNearlyZero(strokeWidth)) {
949 strokeWidth = SK_ScalarHalf;
950 } else {
951 strokeWidth *= SK_ScalarHalf;
952 }
953
954 // we only handle thick strokes for near-circular ellipses
955 if (strokeWidth > SK_ScalarHalf &&
956 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
957 return nullptr;
958 }
959
960 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
961 if (strokeWidth*(yRadius*yRadius) < (strokeWidth*strokeWidth)*xRadius ||
962 strokeWidth*(xRadius*xRadius) < (strokeWidth*strokeWidth)*yRadius) {
963 return nullptr;
964 }
965
966 // set inner radius (if needed)
967 if (SkStrokeRec::kStroke_Style == style) {
968 innerXRadius = xRadius - strokeWidth;
969 innerYRadius = yRadius - strokeWidth;
970 }
971
972 xRadius += strokeWidth;
973 yRadius += strokeWidth;
974 }
975 if (DIEllipseStyle::kStroke == dieStyle) {
976 dieStyle = (innerXRadius > 0 && innerYRadius > 0) ? DIEllipseStyle ::kStroke :
977 DIEllipseStyle ::kFill;
978 }
979
980 // This expands the outer rect so that after CTM we end up with a half-pixel border
981 SkScalar a = viewMatrix[SkMatrix::kMScaleX];
982 SkScalar b = viewMatrix[SkMatrix::kMSkewX];
983 SkScalar c = viewMatrix[SkMatrix::kMSkewY];
984 SkScalar d = viewMatrix[SkMatrix::kMScaleY];
985 SkScalar geoDx = SK_ScalarHalf / SkScalarSqrt(a*a + c*c);
986 SkScalar geoDy = SK_ScalarHalf / SkScalarSqrt(b*b + d*d);
987
988 DIEllipseBatch* batch = new DIEllipseBatch();
989 batch->fGeoData.emplace_back(Geometry {
990 viewMatrix,
991 color,
992 xRadius,
993 yRadius,
994 innerXRadius,
995 innerYRadius,
996 geoDx,
997 geoDy,
998 dieStyle,
999 SkRect::MakeLTRB(center.fX - xRadius - geoDx, center.fY - yRadius - geoDy,
1000 center.fX + xRadius + geoDx, center.fY + yRadius + geoDy)
1001 });
bsalomon88cf17d2016-07-08 06:40:56 -07001002 batch->setTransformedBounds(batch->fGeoData[0].fBounds, viewMatrix, HasAABloat::kYes,
1003 IsZeroArea::kNo);
bsalomon4b4a7cc2016-07-08 04:42:54 -07001004 return batch;
joshualitt76e7fb62015-02-11 08:52:27 -08001005 }
1006
mtklein36352bf2015-03-25 18:17:31 -07001007 const char* name() const override { return "DIEllipseBatch"; }
joshualitt76e7fb62015-02-11 08:52:27 -08001008
halcanary9d524f22016-03-29 09:03:52 -07001009 void computePipelineOptimizations(GrInitInvariantOutput* color,
ethannicholasff210322015-11-24 12:10:10 -08001010 GrInitInvariantOutput* coverage,
1011 GrBatchToXPOverrides* overrides) const override {
joshualitt76e7fb62015-02-11 08:52:27 -08001012 // When this is called on a batch, there is only one geometry bundle
ethannicholasff210322015-11-24 12:10:10 -08001013 color->setKnownFourComponents(fGeoData[0].fColor);
1014 coverage->setUnknownSingleComponent();
joshualitt76e7fb62015-02-11 08:52:27 -08001015 }
1016
bsalomone46f9fe2015-08-18 06:05:14 -07001017private:
1018
bsalomon4b4a7cc2016-07-08 04:42:54 -07001019 DIEllipseBatch() : INHERITED(ClassID()) {}
1020
ethannicholasff210322015-11-24 12:10:10 -08001021 void initBatchTracker(const GrXPOverridesForBatch& overrides) override {
bsalomoncdaa97b2016-03-08 08:30:14 -08001022 // Handle any overrides that affect our GP.
ethannicholasff210322015-11-24 12:10:10 -08001023 overrides.getOverrideColorIfSet(&fGeoData[0].fColor);
bsalomoncdaa97b2016-03-08 08:30:14 -08001024 fUsesLocalCoords = overrides.readsLocalCoords();
joshualitt76e7fb62015-02-11 08:52:27 -08001025 }
1026
joshualitt144c3c82015-11-30 12:30:13 -08001027 void onPrepareDraws(Target* target) const override {
joshualitt76e7fb62015-02-11 08:52:27 -08001028 // Setup geometry processor
bsalomoncdaa97b2016-03-08 08:30:14 -08001029 SkAutoTUnref<GrGeometryProcessor> gp(new DIEllipseGeometryProcessor(this->viewMatrix(),
1030 this->style()));
joshualitt76e7fb62015-02-11 08:52:27 -08001031
joshualitt76e7fb62015-02-11 08:52:27 -08001032 int instanceCount = fGeoData.count();
joshualitt76e7fb62015-02-11 08:52:27 -08001033 size_t vertexStride = gp->getVertexStride();
joshualitt19e00582015-02-11 17:36:30 -08001034 SkASSERT(vertexStride == sizeof(DIEllipseVertex));
bsalomonb5238a72015-05-05 07:49:49 -07001035 QuadHelper helper;
1036 DIEllipseVertex* verts = reinterpret_cast<DIEllipseVertex*>(
bsalomon75398562015-08-17 12:55:38 -07001037 helper.init(target, vertexStride, instanceCount));
bsalomonb5238a72015-05-05 07:49:49 -07001038 if (!verts) {
joshualitt4b31de82015-03-05 14:33:41 -08001039 return;
1040 }
1041
joshualitt76e7fb62015-02-11 08:52:27 -08001042 for (int i = 0; i < instanceCount; i++) {
joshualitt144c3c82015-11-30 12:30:13 -08001043 const Geometry& geom = fGeoData[i];
joshualitt76e7fb62015-02-11 08:52:27 -08001044
brianosmanbb2ff942016-02-11 14:15:18 -08001045 GrColor color = geom.fColor;
bsalomonb5238a72015-05-05 07:49:49 -07001046 SkScalar xRadius = geom.fXRadius;
1047 SkScalar yRadius = geom.fYRadius;
joshualitt76e7fb62015-02-11 08:52:27 -08001048
bsalomonb5238a72015-05-05 07:49:49 -07001049 const SkRect& bounds = geom.fBounds;
joshualitt76e7fb62015-02-11 08:52:27 -08001050
1051 // This adjusts the "radius" to include the half-pixel border
reed80ea19c2015-05-12 10:37:34 -07001052 SkScalar offsetDx = geom.fGeoDx / xRadius;
1053 SkScalar offsetDy = geom.fGeoDy / yRadius;
joshualitt76e7fb62015-02-11 08:52:27 -08001054
reed80ea19c2015-05-12 10:37:34 -07001055 SkScalar innerRatioX = xRadius / geom.fInnerXRadius;
1056 SkScalar innerRatioY = yRadius / geom.fInnerYRadius;
joshualitt76e7fb62015-02-11 08:52:27 -08001057
1058 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
brianosmanbb2ff942016-02-11 14:15:18 -08001059 verts[0].fColor = color;
joshualitt76e7fb62015-02-11 08:52:27 -08001060 verts[0].fOuterOffset = SkPoint::Make(-1.0f - offsetDx, -1.0f - offsetDy);
1061 verts[0].fInnerOffset = SkPoint::Make(-innerRatioX - offsetDx, -innerRatioY - offsetDy);
1062
1063 verts[1].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
brianosmanbb2ff942016-02-11 14:15:18 -08001064 verts[1].fColor = color;
joshualitt76e7fb62015-02-11 08:52:27 -08001065 verts[1].fOuterOffset = SkPoint::Make(-1.0f - offsetDx, 1.0f + offsetDy);
1066 verts[1].fInnerOffset = SkPoint::Make(-innerRatioX - offsetDx, innerRatioY + offsetDy);
1067
1068 verts[2].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
brianosmanbb2ff942016-02-11 14:15:18 -08001069 verts[2].fColor = color;
joshualitt76e7fb62015-02-11 08:52:27 -08001070 verts[2].fOuterOffset = SkPoint::Make(1.0f + offsetDx, 1.0f + offsetDy);
1071 verts[2].fInnerOffset = SkPoint::Make(innerRatioX + offsetDx, innerRatioY + offsetDy);
1072
1073 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
brianosmanbb2ff942016-02-11 14:15:18 -08001074 verts[3].fColor = color;
joshualitt76e7fb62015-02-11 08:52:27 -08001075 verts[3].fOuterOffset = SkPoint::Make(1.0f + offsetDx, -1.0f - offsetDy);
1076 verts[3].fInnerOffset = SkPoint::Make(innerRatioX + offsetDx, -innerRatioY - offsetDy);
1077
bsalomonb5238a72015-05-05 07:49:49 -07001078 verts += kVerticesPerQuad;
joshualitt76e7fb62015-02-11 08:52:27 -08001079 }
bsalomon342bfc22016-04-01 06:06:20 -07001080 helper.recordDraw(target, gp);
joshualitt76e7fb62015-02-11 08:52:27 -08001081 }
halcanary9d524f22016-03-29 09:03:52 -07001082
bsalomoncb02b382015-08-12 11:14:50 -07001083 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override {
bsalomonabd30f52015-08-13 13:34:48 -07001084 DIEllipseBatch* that = t->cast<DIEllipseBatch>();
1085 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
1086 that->bounds(), caps)) {
joshualitt8cab9a72015-07-16 09:13:50 -07001087 return false;
1088 }
1089
bsalomoncdaa97b2016-03-08 08:30:14 -08001090 if (this->style() != that->style()) {
joshualitt76e7fb62015-02-11 08:52:27 -08001091 return false;
1092 }
1093
joshualittd96a67b2015-05-05 14:09:05 -07001094 // TODO rewrite to allow positioning on CPU
1095 if (!this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
joshualitt76e7fb62015-02-11 08:52:27 -08001096 return false;
1097 }
1098
bsalomoncdaa97b2016-03-08 08:30:14 -08001099 fGeoData.push_back_n(that->fGeoData.count(), that->fGeoData.begin());
bsalomon88cf17d2016-07-08 06:40:56 -07001100 this->joinBounds(*that);
joshualitt76e7fb62015-02-11 08:52:27 -08001101 return true;
1102 }
1103
joshualitt76e7fb62015-02-11 08:52:27 -08001104 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; }
bsalomoncdaa97b2016-03-08 08:30:14 -08001105 DIEllipseStyle style() const { return fGeoData[0].fStyle; }
joshualitt76e7fb62015-02-11 08:52:27 -08001106
bsalomon4b4a7cc2016-07-08 04:42:54 -07001107 struct Geometry {
1108 SkMatrix fViewMatrix;
1109 GrColor fColor;
1110 SkScalar fXRadius;
1111 SkScalar fYRadius;
1112 SkScalar fInnerXRadius;
1113 SkScalar fInnerYRadius;
1114 SkScalar fGeoDx;
1115 SkScalar fGeoDy;
1116 DIEllipseStyle fStyle;
1117 SkRect fBounds;
1118 };
1119
bsalomoncdaa97b2016-03-08 08:30:14 -08001120 bool fUsesLocalCoords;
joshualitt76e7fb62015-02-11 08:52:27 -08001121 SkSTArray<1, Geometry, true> fGeoData;
reed1b55a962015-09-17 20:16:13 -07001122
1123 typedef GrVertexBatch INHERITED;
joshualitt76e7fb62015-02-11 08:52:27 -08001124};
1125
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001126///////////////////////////////////////////////////////////////////////////////
1127
1128static const uint16_t gRRectIndices[] = {
1129 // corners
1130 0, 1, 5, 0, 5, 4,
1131 2, 3, 7, 2, 7, 6,
1132 8, 9, 13, 8, 13, 12,
1133 10, 11, 15, 10, 15, 14,
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001134
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001135 // edges
1136 1, 2, 6, 1, 6, 5,
1137 4, 5, 9, 4, 9, 8,
1138 6, 7, 11, 6, 11, 10,
1139 9, 10, 14, 9, 14, 13,
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001140
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001141 // center
1142 // we place this at the end so that we can ignore these indices when rendering stroke-only
1143 5, 6, 10, 5, 10, 9
1144};
1145
joshualitt5ead6da2014-10-22 16:00:29 -07001146static const int kIndicesPerStrokeRRect = SK_ARRAY_COUNT(gRRectIndices) - 6;
1147static const int kIndicesPerRRect = SK_ARRAY_COUNT(gRRectIndices);
1148static const int kVertsPerRRect = 16;
1149static const int kNumRRectsInIndexBuffer = 256;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001150
bsalomoned0bcad2015-05-04 10:36:42 -07001151GR_DECLARE_STATIC_UNIQUE_KEY(gStrokeRRectOnlyIndexBufferKey);
1152GR_DECLARE_STATIC_UNIQUE_KEY(gRRectOnlyIndexBufferKey);
cdalton397536c2016-03-25 12:15:03 -07001153static const GrBuffer* ref_rrect_index_buffer(bool strokeOnly,
1154 GrResourceProvider* resourceProvider) {
bsalomoned0bcad2015-05-04 10:36:42 -07001155 GR_DEFINE_STATIC_UNIQUE_KEY(gStrokeRRectOnlyIndexBufferKey);
1156 GR_DEFINE_STATIC_UNIQUE_KEY(gRRectOnlyIndexBufferKey);
1157 if (strokeOnly) {
bsalomoneae62002015-07-31 13:59:30 -07001158 return resourceProvider->findOrCreateInstancedIndexBuffer(
bsalomoned0bcad2015-05-04 10:36:42 -07001159 gRRectIndices, kIndicesPerStrokeRRect, kNumRRectsInIndexBuffer, kVertsPerRRect,
1160 gStrokeRRectOnlyIndexBufferKey);
1161 } else {
bsalomoneae62002015-07-31 13:59:30 -07001162 return resourceProvider->findOrCreateInstancedIndexBuffer(
bsalomoned0bcad2015-05-04 10:36:42 -07001163 gRRectIndices, kIndicesPerRRect, kNumRRectsInIndexBuffer, kVertsPerRRect,
1164 gRRectOnlyIndexBufferKey);
1165
1166 }
1167}
1168
joshualitt76e7fb62015-02-11 08:52:27 -08001169///////////////////////////////////////////////////////////////////////////////////////////////////
1170
bsalomonabd30f52015-08-13 13:34:48 -07001171class RRectCircleRendererBatch : public GrVertexBatch {
joshualitt76e7fb62015-02-11 08:52:27 -08001172public:
reed1b55a962015-09-17 20:16:13 -07001173 DEFINE_BATCH_CLASS_ID
1174
bsalomon4b4a7cc2016-07-08 04:42:54 -07001175 // A devStrokeWidth <= 0 indicates a fill only. If devStrokeWidth > 0 then strokeOnly indicates
1176 // whether the rrect is only stroked or stroked and filled.
1177 RRectCircleRendererBatch(GrColor color, const SkMatrix& viewMatrix, const SkRect& devRect,
1178 float devRadius, float devStrokeWidth, bool strokeOnly)
1179 : INHERITED(ClassID())
1180 , fViewMatrixIfUsingLocalCoords(viewMatrix) {
1181 SkRect bounds = devRect;
1182 SkASSERT(!(devStrokeWidth <= 0 && strokeOnly));
1183 SkScalar innerRadius = 0.0f;
1184 SkScalar outerRadius = devRadius;
1185 SkScalar halfWidth = 0;
1186 fStroked = false;
1187 if (devStrokeWidth > 0) {
1188 if (SkScalarNearlyZero(devStrokeWidth)) {
1189 halfWidth = SK_ScalarHalf;
1190 } else {
1191 halfWidth = SkScalarHalf(devStrokeWidth);
1192 }
joshualitt76e7fb62015-02-11 08:52:27 -08001193
bsalomon4b4a7cc2016-07-08 04:42:54 -07001194 if (strokeOnly) {
1195 innerRadius = devRadius - halfWidth;
1196 fStroked = innerRadius >= 0;
1197 }
1198 outerRadius += halfWidth;
1199 bounds.outset(halfWidth, halfWidth);
1200 }
bsalomoncdaa97b2016-03-08 08:30:14 -08001201
bsalomon4b4a7cc2016-07-08 04:42:54 -07001202 // The radii are outset for two reasons. First, it allows the shader to simply perform
1203 // simpler computation because the computed alpha is zero, rather than 50%, at the radius.
1204 // Second, the outer radius is used to compute the verts of the bounding box that is
1205 // rendered and the outset ensures the box will cover all partially covered by the rrect
1206 // corners.
1207 outerRadius += SK_ScalarHalf;
1208 innerRadius -= SK_ScalarHalf;
1209
bsalomon88cf17d2016-07-08 06:40:56 -07001210 this->setBounds(bounds, HasAABloat::kYes, IsZeroArea::kNo);
1211
1212 // Expand the rect for aa to generate correct vertices.
bsalomon4b4a7cc2016-07-08 04:42:54 -07001213 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1214
1215 fGeoData.emplace_back(Geometry { color, innerRadius, outerRadius, bounds });
joshualitt76e7fb62015-02-11 08:52:27 -08001216 }
1217
mtklein36352bf2015-03-25 18:17:31 -07001218 const char* name() const override { return "RRectCircleBatch"; }
joshualitt76e7fb62015-02-11 08:52:27 -08001219
halcanary9d524f22016-03-29 09:03:52 -07001220 void computePipelineOptimizations(GrInitInvariantOutput* color,
ethannicholasff210322015-11-24 12:10:10 -08001221 GrInitInvariantOutput* coverage,
1222 GrBatchToXPOverrides* overrides) const override {
joshualitt76e7fb62015-02-11 08:52:27 -08001223 // When this is called on a batch, there is only one geometry bundle
ethannicholasff210322015-11-24 12:10:10 -08001224 color->setKnownFourComponents(fGeoData[0].fColor);
1225 coverage->setUnknownSingleComponent();
joshualitt76e7fb62015-02-11 08:52:27 -08001226 }
1227
bsalomone46f9fe2015-08-18 06:05:14 -07001228private:
ethannicholasff210322015-11-24 12:10:10 -08001229 void initBatchTracker(const GrXPOverridesForBatch& overrides) override {
bsalomoncdaa97b2016-03-08 08:30:14 -08001230 // Handle any overrides that affect our GP.
ethannicholasff210322015-11-24 12:10:10 -08001231 overrides.getOverrideColorIfSet(&fGeoData[0].fColor);
bsalomoncdaa97b2016-03-08 08:30:14 -08001232 if (!overrides.readsLocalCoords()) {
1233 fViewMatrixIfUsingLocalCoords.reset();
1234 }
joshualitt76e7fb62015-02-11 08:52:27 -08001235 }
1236
joshualitt144c3c82015-11-30 12:30:13 -08001237 void onPrepareDraws(Target* target) const override {
bsalomoncdaa97b2016-03-08 08:30:14 -08001238 // Invert the view matrix as a local matrix (if any other processors require coords).
1239 SkMatrix localMatrix;
1240 if (!fViewMatrixIfUsingLocalCoords.invert(&localMatrix)) {
joshualitt76e7fb62015-02-11 08:52:27 -08001241 return;
1242 }
1243
1244 // Setup geometry processor
bsalomoncdaa97b2016-03-08 08:30:14 -08001245 SkAutoTUnref<GrGeometryProcessor> gp(new CircleGeometryProcessor(fStroked, localMatrix));
joshualitt76e7fb62015-02-11 08:52:27 -08001246
joshualitt76e7fb62015-02-11 08:52:27 -08001247 int instanceCount = fGeoData.count();
joshualitt76e7fb62015-02-11 08:52:27 -08001248 size_t vertexStride = gp->getVertexStride();
1249 SkASSERT(vertexStride == sizeof(CircleVertex));
1250
bsalomonb5238a72015-05-05 07:49:49 -07001251 // drop out the middle quad if we're stroked
bsalomoncdaa97b2016-03-08 08:30:14 -08001252 int indicesPerInstance = fStroked ? kIndicesPerStrokeRRect : kIndicesPerRRect;
cdalton397536c2016-03-25 12:15:03 -07001253 SkAutoTUnref<const GrBuffer> indexBuffer(
bsalomoncdaa97b2016-03-08 08:30:14 -08001254 ref_rrect_index_buffer(fStroked, target->resourceProvider()));
joshualitt76e7fb62015-02-11 08:52:27 -08001255
bsalomonb5238a72015-05-05 07:49:49 -07001256 InstancedHelper helper;
bsalomon75398562015-08-17 12:55:38 -07001257 CircleVertex* verts = reinterpret_cast<CircleVertex*>(helper.init(target,
bsalomonb5238a72015-05-05 07:49:49 -07001258 kTriangles_GrPrimitiveType, vertexStride, indexBuffer, kVertsPerRRect,
1259 indicesPerInstance, instanceCount));
1260 if (!verts || !indexBuffer) {
joshualitt4b31de82015-03-05 14:33:41 -08001261 SkDebugf("Could not allocate vertices\n");
1262 return;
1263 }
1264
joshualitt76e7fb62015-02-11 08:52:27 -08001265 for (int i = 0; i < instanceCount; i++) {
joshualitt144c3c82015-11-30 12:30:13 -08001266 const Geometry& args = fGeoData[i];
joshualitt76e7fb62015-02-11 08:52:27 -08001267
brianosmanbb2ff942016-02-11 14:15:18 -08001268 GrColor color = args.fColor;
joshualitt76e7fb62015-02-11 08:52:27 -08001269 SkScalar outerRadius = args.fOuterRadius;
1270
egdanielbc227142015-04-21 06:28:08 -07001271 const SkRect& bounds = args.fDevBounds;
joshualitt76e7fb62015-02-11 08:52:27 -08001272
1273 SkScalar yCoords[4] = {
1274 bounds.fTop,
1275 bounds.fTop + outerRadius,
1276 bounds.fBottom - outerRadius,
1277 bounds.fBottom
1278 };
1279
1280 SkScalar yOuterRadii[4] = {-1, 0, 0, 1 };
1281 // The inner radius in the vertex data must be specified in normalized space.
1282 SkScalar innerRadius = args.fInnerRadius / args.fOuterRadius;
1283 for (int i = 0; i < 4; ++i) {
1284 verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]);
brianosmanbb2ff942016-02-11 14:15:18 -08001285 verts->fColor = color;
joshualitt76e7fb62015-02-11 08:52:27 -08001286 verts->fOffset = SkPoint::Make(-1, yOuterRadii[i]);
1287 verts->fOuterRadius = outerRadius;
1288 verts->fInnerRadius = innerRadius;
1289 verts++;
1290
1291 verts->fPos = SkPoint::Make(bounds.fLeft + outerRadius, yCoords[i]);
brianosmanbb2ff942016-02-11 14:15:18 -08001292 verts->fColor = color;
joshualitt76e7fb62015-02-11 08:52:27 -08001293 verts->fOffset = SkPoint::Make(0, yOuterRadii[i]);
1294 verts->fOuterRadius = outerRadius;
1295 verts->fInnerRadius = innerRadius;
1296 verts++;
1297
1298 verts->fPos = SkPoint::Make(bounds.fRight - outerRadius, yCoords[i]);
brianosmanbb2ff942016-02-11 14:15:18 -08001299 verts->fColor = color;
joshualitt76e7fb62015-02-11 08:52:27 -08001300 verts->fOffset = SkPoint::Make(0, yOuterRadii[i]);
1301 verts->fOuterRadius = outerRadius;
1302 verts->fInnerRadius = innerRadius;
1303 verts++;
1304
1305 verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]);
brianosmanbb2ff942016-02-11 14:15:18 -08001306 verts->fColor = color;
joshualitt76e7fb62015-02-11 08:52:27 -08001307 verts->fOffset = SkPoint::Make(1, yOuterRadii[i]);
1308 verts->fOuterRadius = outerRadius;
1309 verts->fInnerRadius = innerRadius;
1310 verts++;
1311 }
1312 }
1313
bsalomon342bfc22016-04-01 06:06:20 -07001314 helper.recordDraw(target, gp);
joshualitt76e7fb62015-02-11 08:52:27 -08001315 }
1316
bsalomoncb02b382015-08-12 11:14:50 -07001317 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override {
bsalomonabd30f52015-08-13 13:34:48 -07001318 RRectCircleRendererBatch* that = t->cast<RRectCircleRendererBatch>();
1319 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
1320 that->bounds(), caps)) {
joshualitt8cab9a72015-07-16 09:13:50 -07001321 return false;
1322 }
1323
bsalomoncdaa97b2016-03-08 08:30:14 -08001324 if (fStroked != that->fStroked) {
joshualitt76e7fb62015-02-11 08:52:27 -08001325 return false;
1326 }
1327
bsalomoncdaa97b2016-03-08 08:30:14 -08001328 if (!fViewMatrixIfUsingLocalCoords.cheapEqualTo(that->fViewMatrixIfUsingLocalCoords)) {
joshualitt76e7fb62015-02-11 08:52:27 -08001329 return false;
1330 }
1331
bsalomoncdaa97b2016-03-08 08:30:14 -08001332 fGeoData.push_back_n(that->fGeoData.count(), that->fGeoData.begin());
bsalomon88cf17d2016-07-08 06:40:56 -07001333 this->joinBounds(*that);
joshualitt76e7fb62015-02-11 08:52:27 -08001334 return true;
1335 }
1336
bsalomon4b4a7cc2016-07-08 04:42:54 -07001337 struct Geometry {
1338 GrColor fColor;
1339 SkScalar fInnerRadius;
1340 SkScalar fOuterRadius;
1341 SkRect fDevBounds;
1342 };
1343
bsalomoncdaa97b2016-03-08 08:30:14 -08001344 bool fStroked;
1345 SkMatrix fViewMatrixIfUsingLocalCoords;
joshualitt76e7fb62015-02-11 08:52:27 -08001346 SkSTArray<1, Geometry, true> fGeoData;
reed1b55a962015-09-17 20:16:13 -07001347
1348 typedef GrVertexBatch INHERITED;
joshualitt76e7fb62015-02-11 08:52:27 -08001349};
1350
bsalomonabd30f52015-08-13 13:34:48 -07001351class RRectEllipseRendererBatch : public GrVertexBatch {
joshualitt76e7fb62015-02-11 08:52:27 -08001352public:
reed1b55a962015-09-17 20:16:13 -07001353 DEFINE_BATCH_CLASS_ID
1354
bsalomon4b4a7cc2016-07-08 04:42:54 -07001355 // If devStrokeWidths values are <= 0 indicates then fill only. Otherwise, strokeOnly indicates
1356 // whether the rrect is only stroked or stroked and filled.
1357 static GrDrawBatch* Create(GrColor color, const SkMatrix& viewMatrix, const SkRect& devRect,
1358 float devXRadius, float devYRadius, SkVector devStrokeWidths,
1359 bool strokeOnly) {
1360 SkASSERT(devXRadius > 0.5);
1361 SkASSERT(devYRadius > 0.5);
1362 SkASSERT((devStrokeWidths.fX > 0) == (devStrokeWidths.fY > 0));
1363 SkASSERT(!(strokeOnly && devStrokeWidths.fX <= 0));
1364 SkScalar innerXRadius = 0.0f;
1365 SkScalar innerYRadius = 0.0f;
1366 SkRect bounds = devRect;
1367 bool stroked = false;
1368 if (devStrokeWidths.fX > 0) {
1369 if (SkScalarNearlyZero(devStrokeWidths.length())) {
1370 devStrokeWidths.set(SK_ScalarHalf, SK_ScalarHalf);
1371 } else {
1372 devStrokeWidths.scale(SK_ScalarHalf);
1373 }
joshualitt76e7fb62015-02-11 08:52:27 -08001374
bsalomon4b4a7cc2016-07-08 04:42:54 -07001375 // we only handle thick strokes for near-circular ellipses
1376 if (devStrokeWidths.length() > SK_ScalarHalf &&
1377 (SK_ScalarHalf*devXRadius > devYRadius || SK_ScalarHalf*devYRadius > devXRadius)) {
1378 return nullptr;
1379 }
1380
1381 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
1382 if (devStrokeWidths.fX*(devYRadius*devYRadius) <
1383 (devStrokeWidths.fY*devStrokeWidths.fY)*devXRadius) {
1384 return nullptr;
1385 }
1386 if (devStrokeWidths.fY*(devXRadius*devXRadius) <
1387 (devStrokeWidths.fX*devStrokeWidths.fX)*devYRadius) {
1388 return nullptr;
1389 }
1390
1391 // this is legit only if scale & translation (which should be the case at the moment)
1392 if (strokeOnly) {
1393 innerXRadius = devXRadius - devStrokeWidths.fX;
1394 innerYRadius = devYRadius - devStrokeWidths.fY;
1395 stroked = (innerXRadius >= 0 && innerYRadius >= 0);
1396 }
1397
1398 devXRadius += devStrokeWidths.fX;
1399 devYRadius += devStrokeWidths.fY;
1400 bounds.outset(devStrokeWidths.fX, devStrokeWidths.fY);
1401 }
1402
bsalomon4b4a7cc2016-07-08 04:42:54 -07001403 RRectEllipseRendererBatch* batch = new RRectEllipseRendererBatch();
1404 batch->fStroked = stroked;
1405 batch->fViewMatrixIfUsingLocalCoords = viewMatrix;
bsalomon88cf17d2016-07-08 06:40:56 -07001406 batch->setBounds(bounds, HasAABloat::kYes, IsZeroArea::kNo);
1407 // Expand the rect for aa in order to generate the correct vertices.
1408 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
bsalomon4b4a7cc2016-07-08 04:42:54 -07001409 batch->fGeoData.emplace_back(
1410 Geometry {color, devXRadius, devYRadius, innerXRadius, innerYRadius, bounds});
bsalomon4b4a7cc2016-07-08 04:42:54 -07001411 return batch;
joshualitt76e7fb62015-02-11 08:52:27 -08001412 }
1413
mtklein36352bf2015-03-25 18:17:31 -07001414 const char* name() const override { return "RRectEllipseRendererBatch"; }
joshualitt76e7fb62015-02-11 08:52:27 -08001415
halcanary9d524f22016-03-29 09:03:52 -07001416 void computePipelineOptimizations(GrInitInvariantOutput* color,
ethannicholasff210322015-11-24 12:10:10 -08001417 GrInitInvariantOutput* coverage,
1418 GrBatchToXPOverrides* overrides) const override {
joshualitt76e7fb62015-02-11 08:52:27 -08001419 // When this is called on a batch, there is only one geometry bundle
ethannicholasff210322015-11-24 12:10:10 -08001420 color->setKnownFourComponents(fGeoData[0].fColor);
1421 coverage->setUnknownSingleComponent();
joshualitt76e7fb62015-02-11 08:52:27 -08001422 }
1423
bsalomone46f9fe2015-08-18 06:05:14 -07001424private:
bsalomon4b4a7cc2016-07-08 04:42:54 -07001425 RRectEllipseRendererBatch() : INHERITED(ClassID()) {}
1426
ethannicholasff210322015-11-24 12:10:10 -08001427 void initBatchTracker(const GrXPOverridesForBatch& overrides) override {
bsalomoncdaa97b2016-03-08 08:30:14 -08001428 // Handle overrides that affect our GP.
ethannicholasff210322015-11-24 12:10:10 -08001429 overrides.getOverrideColorIfSet(&fGeoData[0].fColor);
bsalomoncdaa97b2016-03-08 08:30:14 -08001430 if (!overrides.readsLocalCoords()) {
1431 fViewMatrixIfUsingLocalCoords.reset();
1432 }
joshualitt76e7fb62015-02-11 08:52:27 -08001433 }
1434
joshualitt144c3c82015-11-30 12:30:13 -08001435 void onPrepareDraws(Target* target) const override {
bsalomoncdaa97b2016-03-08 08:30:14 -08001436 SkMatrix localMatrix;
1437 if (!fViewMatrixIfUsingLocalCoords.invert(&localMatrix)) {
joshualitt76e7fb62015-02-11 08:52:27 -08001438 return;
1439 }
1440
1441 // Setup geometry processor
bsalomoncdaa97b2016-03-08 08:30:14 -08001442 SkAutoTUnref<GrGeometryProcessor> gp(new EllipseGeometryProcessor(fStroked, localMatrix));
joshualitt76e7fb62015-02-11 08:52:27 -08001443
joshualitt76e7fb62015-02-11 08:52:27 -08001444 int instanceCount = fGeoData.count();
joshualitt76e7fb62015-02-11 08:52:27 -08001445 size_t vertexStride = gp->getVertexStride();
1446 SkASSERT(vertexStride == sizeof(EllipseVertex));
1447
bsalomonb5238a72015-05-05 07:49:49 -07001448 // drop out the middle quad if we're stroked
bsalomoncdaa97b2016-03-08 08:30:14 -08001449 int indicesPerInstance = fStroked ? kIndicesPerStrokeRRect : kIndicesPerRRect;
cdalton397536c2016-03-25 12:15:03 -07001450 SkAutoTUnref<const GrBuffer> indexBuffer(
bsalomoncdaa97b2016-03-08 08:30:14 -08001451 ref_rrect_index_buffer(fStroked, target->resourceProvider()));
joshualitt76e7fb62015-02-11 08:52:27 -08001452
bsalomonb5238a72015-05-05 07:49:49 -07001453 InstancedHelper helper;
1454 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(
bsalomon75398562015-08-17 12:55:38 -07001455 helper.init(target, kTriangles_GrPrimitiveType, vertexStride, indexBuffer,
bsalomonb5238a72015-05-05 07:49:49 -07001456 kVertsPerRRect, indicesPerInstance, instanceCount));
1457 if (!verts || !indexBuffer) {
joshualitt4b31de82015-03-05 14:33:41 -08001458 SkDebugf("Could not allocate vertices\n");
1459 return;
1460 }
1461
joshualitt76e7fb62015-02-11 08:52:27 -08001462 for (int i = 0; i < instanceCount; i++) {
joshualitt144c3c82015-11-30 12:30:13 -08001463 const Geometry& args = fGeoData[i];
joshualitt76e7fb62015-02-11 08:52:27 -08001464
brianosmanbb2ff942016-02-11 14:15:18 -08001465 GrColor color = args.fColor;
1466
joshualitt76e7fb62015-02-11 08:52:27 -08001467 // Compute the reciprocals of the radii here to save time in the shader
1468 SkScalar xRadRecip = SkScalarInvert(args.fXRadius);
1469 SkScalar yRadRecip = SkScalarInvert(args.fYRadius);
1470 SkScalar xInnerRadRecip = SkScalarInvert(args.fInnerXRadius);
1471 SkScalar yInnerRadRecip = SkScalarInvert(args.fInnerYRadius);
1472
1473 // Extend the radii out half a pixel to antialias.
1474 SkScalar xOuterRadius = args.fXRadius + SK_ScalarHalf;
1475 SkScalar yOuterRadius = args.fYRadius + SK_ScalarHalf;
1476
egdanielbc227142015-04-21 06:28:08 -07001477 const SkRect& bounds = args.fDevBounds;
joshualitt76e7fb62015-02-11 08:52:27 -08001478
1479 SkScalar yCoords[4] = {
1480 bounds.fTop,
1481 bounds.fTop + yOuterRadius,
1482 bounds.fBottom - yOuterRadius,
1483 bounds.fBottom
1484 };
1485 SkScalar yOuterOffsets[4] = {
1486 yOuterRadius,
1487 SK_ScalarNearlyZero, // we're using inversesqrt() in shader, so can't be exactly 0
1488 SK_ScalarNearlyZero,
1489 yOuterRadius
1490 };
1491
1492 for (int i = 0; i < 4; ++i) {
1493 verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]);
brianosmanbb2ff942016-02-11 14:15:18 -08001494 verts->fColor = color;
joshualitt76e7fb62015-02-11 08:52:27 -08001495 verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]);
1496 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1497 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
1498 verts++;
1499
1500 verts->fPos = SkPoint::Make(bounds.fLeft + xOuterRadius, yCoords[i]);
brianosmanbb2ff942016-02-11 14:15:18 -08001501 verts->fColor = color;
joshualitt76e7fb62015-02-11 08:52:27 -08001502 verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]);
1503 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1504 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
1505 verts++;
1506
1507 verts->fPos = SkPoint::Make(bounds.fRight - xOuterRadius, yCoords[i]);
brianosmanbb2ff942016-02-11 14:15:18 -08001508 verts->fColor = color;
joshualitt76e7fb62015-02-11 08:52:27 -08001509 verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]);
1510 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1511 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
1512 verts++;
1513
1514 verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]);
brianosmanbb2ff942016-02-11 14:15:18 -08001515 verts->fColor = color;
joshualitt76e7fb62015-02-11 08:52:27 -08001516 verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]);
1517 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1518 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
1519 verts++;
1520 }
1521 }
bsalomon342bfc22016-04-01 06:06:20 -07001522 helper.recordDraw(target, gp);
joshualitt76e7fb62015-02-11 08:52:27 -08001523 }
1524
bsalomoncb02b382015-08-12 11:14:50 -07001525 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override {
bsalomonabd30f52015-08-13 13:34:48 -07001526 RRectEllipseRendererBatch* that = t->cast<RRectEllipseRendererBatch>();
1527
1528 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
1529 that->bounds(), caps)) {
joshualitt8cab9a72015-07-16 09:13:50 -07001530 return false;
1531 }
1532
bsalomoncdaa97b2016-03-08 08:30:14 -08001533 if (fStroked != that->fStroked) {
joshualitt76e7fb62015-02-11 08:52:27 -08001534 return false;
1535 }
1536
bsalomoncdaa97b2016-03-08 08:30:14 -08001537 if (!fViewMatrixIfUsingLocalCoords.cheapEqualTo(that->fViewMatrixIfUsingLocalCoords)) {
joshualitt76e7fb62015-02-11 08:52:27 -08001538 return false;
1539 }
1540
bsalomoncdaa97b2016-03-08 08:30:14 -08001541 fGeoData.push_back_n(that->fGeoData.count(), that->fGeoData.begin());
bsalomon88cf17d2016-07-08 06:40:56 -07001542 this->joinBounds(*that);
joshualitt76e7fb62015-02-11 08:52:27 -08001543 return true;
1544 }
1545
bsalomon4b4a7cc2016-07-08 04:42:54 -07001546 struct Geometry {
1547 GrColor fColor;
1548 SkScalar fXRadius;
1549 SkScalar fYRadius;
1550 SkScalar fInnerXRadius;
1551 SkScalar fInnerYRadius;
1552 SkRect fDevBounds;
1553 };
1554
bsalomoncdaa97b2016-03-08 08:30:14 -08001555 bool fStroked;
1556 SkMatrix fViewMatrixIfUsingLocalCoords;
1557 SkSTArray<1, Geometry, true> fGeoData;
reed1b55a962015-09-17 20:16:13 -07001558
1559 typedef GrVertexBatch INHERITED;
joshualitt76e7fb62015-02-11 08:52:27 -08001560};
1561
bsalomonabd30f52015-08-13 13:34:48 -07001562static GrDrawBatch* create_rrect_batch(GrColor color,
1563 const SkMatrix& viewMatrix,
1564 const SkRRect& rrect,
1565 const SkStrokeRec& stroke) {
joshualitt3e708c52015-04-30 13:49:27 -07001566 SkASSERT(viewMatrix.rectStaysRect());
1567 SkASSERT(rrect.isSimple());
1568 SkASSERT(!rrect.isOval());
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001569
joshualitt3e708c52015-04-30 13:49:27 -07001570 // RRect batchs only handle simple, but not too simple, rrects
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001571 // do any matrix crunching before we reset the draw state for device coords
1572 const SkRect& rrectBounds = rrect.getBounds();
joshualittd96a67b2015-05-05 14:09:05 -07001573 SkRect bounds;
1574 viewMatrix.mapRect(&bounds, rrectBounds);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001575
1576 SkVector radii = rrect.getSimpleRadii();
joshualitt8059eb92014-12-29 15:10:07 -08001577 SkScalar xRadius = SkScalarAbs(viewMatrix[SkMatrix::kMScaleX]*radii.fX +
1578 viewMatrix[SkMatrix::kMSkewY]*radii.fY);
1579 SkScalar yRadius = SkScalarAbs(viewMatrix[SkMatrix::kMSkewX]*radii.fX +
1580 viewMatrix[SkMatrix::kMScaleY]*radii.fY);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001581
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001582 SkStrokeRec::Style style = stroke.getStyle();
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001583
bsalomon4b4a7cc2016-07-08 04:42:54 -07001584 // Do (potentially) anisotropic mapping of stroke. Use -1s to indicate fill-only draws.
1585 SkVector scaledStroke = {-1, -1};
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001586 SkScalar strokeWidth = stroke.getWidth();
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001587
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001588 bool isStrokeOnly = SkStrokeRec::kStroke_Style == style ||
1589 SkStrokeRec::kHairline_Style == style;
1590 bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == style;
1591
1592 if (hasStroke) {
1593 if (SkStrokeRec::kHairline_Style == style) {
1594 scaledStroke.set(1, 1);
1595 } else {
joshualitt8059eb92014-12-29 15:10:07 -08001596 scaledStroke.fX = SkScalarAbs(strokeWidth*(viewMatrix[SkMatrix::kMScaleX] +
1597 viewMatrix[SkMatrix::kMSkewY]));
1598 scaledStroke.fY = SkScalarAbs(strokeWidth*(viewMatrix[SkMatrix::kMSkewX] +
1599 viewMatrix[SkMatrix::kMScaleY]));
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001600 }
1601
1602 // if half of strokewidth is greater than radius, we don't handle that right now
1603 if (SK_ScalarHalf*scaledStroke.fX > xRadius || SK_ScalarHalf*scaledStroke.fY > yRadius) {
halcanary96fcdcc2015-08-27 07:41:13 -07001604 return nullptr;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001605 }
1606 }
1607
1608 // The way the effect interpolates the offset-to-ellipse/circle-center attribute only works on
1609 // the interior of the rrect if the radii are >= 0.5. Otherwise, the inner rect of the nine-
1610 // patch will have fractional coverage. This only matters when the interior is actually filled.
1611 // We could consider falling back to rect rendering here, since a tiny radius is
1612 // indistinguishable from a square corner.
1613 if (!isStrokeOnly && (SK_ScalarHalf > xRadius || SK_ScalarHalf > yRadius)) {
halcanary96fcdcc2015-08-27 07:41:13 -07001614 return nullptr;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001615 }
1616
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001617 // if the corners are circles, use the circle renderer
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001618 if ((!hasStroke || scaledStroke.fX == scaledStroke.fY) && xRadius == yRadius) {
bsalomon4b4a7cc2016-07-08 04:42:54 -07001619 return new RRectCircleRendererBatch(color, viewMatrix, bounds, xRadius, scaledStroke.fX,
1620 isStrokeOnly);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001621 // otherwise we use the ellipse renderer
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001622 } else {
bsalomon4b4a7cc2016-07-08 04:42:54 -07001623 return RRectEllipseRendererBatch::Create(color, viewMatrix, bounds, xRadius, yRadius,
1624 scaledStroke, isStrokeOnly);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001625
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001626 }
joshualitt3e708c52015-04-30 13:49:27 -07001627}
1628
robertphillipsb56f9272016-02-25 11:03:52 -08001629GrDrawBatch* GrOvalRenderer::CreateRRectBatch(GrColor color,
robertphillips0cc2f852016-02-24 13:36:56 -08001630 const SkMatrix& viewMatrix,
robertphillips0cc2f852016-02-24 13:36:56 -08001631 const SkRRect& rrect,
1632 const SkStrokeRec& stroke,
1633 GrShaderCaps* shaderCaps) {
robertphillips0cc2f852016-02-24 13:36:56 -08001634 if (rrect.isOval()) {
robertphillipsb56f9272016-02-25 11:03:52 -08001635 return CreateOvalBatch(color, viewMatrix, rrect.getBounds(), stroke, shaderCaps);
joshualitt3e708c52015-04-30 13:49:27 -07001636 }
1637
1638 if (!viewMatrix.rectStaysRect() || !rrect.isSimple()) {
robertphillips0cc2f852016-02-24 13:36:56 -08001639 return nullptr;
joshualitt3e708c52015-04-30 13:49:27 -07001640 }
1641
robertphillips0cc2f852016-02-24 13:36:56 -08001642 return create_rrect_batch(color, viewMatrix, rrect, stroke);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001643}
joshualitt3e708c52015-04-30 13:49:27 -07001644
bsalomon4b4a7cc2016-07-08 04:42:54 -07001645///////////////////////////////////////////////////////////////////////////////
1646
1647GrDrawBatch* GrOvalRenderer::CreateOvalBatch(GrColor color,
1648 const SkMatrix& viewMatrix,
1649 const SkRect& oval,
1650 const SkStrokeRec& stroke,
1651 GrShaderCaps* shaderCaps) {
1652 // we can draw circles
1653 if (SkScalarNearlyEqual(oval.width(), oval.height()) && circle_stays_circle(viewMatrix)) {
1654 return new CircleBatch(color, viewMatrix, oval, stroke);
1655 }
1656
1657 // if we have shader derivative support, render as device-independent
1658 if (shaderCaps->shaderDerivativeSupport()) {
1659 return DIEllipseBatch::Create(color, viewMatrix, oval, stroke);
1660 }
1661
1662 // otherwise axis-aligned ellipses only
1663 if (viewMatrix.rectStaysRect()) {
1664 return EllipseBatch::Create(color, viewMatrix, oval, stroke);
1665 }
1666
1667 return nullptr;
1668}
1669
1670///////////////////////////////////////////////////////////////////////////////
joshualitt3e708c52015-04-30 13:49:27 -07001671
1672#ifdef GR_TEST_UTILS
1673
bsalomonabd30f52015-08-13 13:34:48 -07001674DRAW_BATCH_TEST_DEFINE(CircleBatch) {
joshualitt3e708c52015-04-30 13:49:27 -07001675 SkMatrix viewMatrix = GrTest::TestMatrix(random);
1676 GrColor color = GrRandomColor(random);
joshualitt6c891102015-05-13 08:51:49 -07001677 SkRect circle = GrTest::TestSquare(random);
bsalomon4b4a7cc2016-07-08 04:42:54 -07001678 return new CircleBatch(color, viewMatrix, circle, GrTest::TestStrokeRec(random));
joshualitt3e708c52015-04-30 13:49:27 -07001679}
1680
bsalomonabd30f52015-08-13 13:34:48 -07001681DRAW_BATCH_TEST_DEFINE(EllipseBatch) {
joshualitt3e708c52015-04-30 13:49:27 -07001682 SkMatrix viewMatrix = GrTest::TestMatrixRectStaysRect(random);
1683 GrColor color = GrRandomColor(random);
joshualitt6c891102015-05-13 08:51:49 -07001684 SkRect ellipse = GrTest::TestSquare(random);
bsalomon4b4a7cc2016-07-08 04:42:54 -07001685 return EllipseBatch::Create(color, viewMatrix, ellipse, GrTest::TestStrokeRec(random));
joshualitt3e708c52015-04-30 13:49:27 -07001686}
1687
bsalomonabd30f52015-08-13 13:34:48 -07001688DRAW_BATCH_TEST_DEFINE(DIEllipseBatch) {
joshualitt3e708c52015-04-30 13:49:27 -07001689 SkMatrix viewMatrix = GrTest::TestMatrix(random);
1690 GrColor color = GrRandomColor(random);
joshualitt6c891102015-05-13 08:51:49 -07001691 SkRect ellipse = GrTest::TestSquare(random);
bsalomon4b4a7cc2016-07-08 04:42:54 -07001692 return DIEllipseBatch::Create(color, viewMatrix, ellipse, GrTest::TestStrokeRec(random));
joshualitt3e708c52015-04-30 13:49:27 -07001693}
1694
bsalomonabd30f52015-08-13 13:34:48 -07001695DRAW_BATCH_TEST_DEFINE(RRectBatch) {
joshualitt3e708c52015-04-30 13:49:27 -07001696 SkMatrix viewMatrix = GrTest::TestMatrixRectStaysRect(random);
1697 GrColor color = GrRandomColor(random);
1698 const SkRRect& rrect = GrTest::TestRRectSimple(random);
joshualitt21279c72015-05-11 07:21:37 -07001699 return create_rrect_batch(color, viewMatrix, rrect, GrTest::TestStrokeRec(random));
joshualitt3e708c52015-04-30 13:49:27 -07001700}
1701
1702#endif