blob: 2accbf67ae26bb8f5b20c6180a2f486713ce0142 [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
bsalomoncdaa97b2016-03-08 08:30:14 -0800187const GrGeometryProcessor* CircleGeometryProcessor::TestCreate(GrProcessorTestData* d) {
188 return new CircleGeometryProcessor(d->fRandom->nextBool(), GrTest::TestMatrix(d->fRandom));
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000189}
190
191///////////////////////////////////////////////////////////////////////////////
192
193/**
194 * 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 +0000195 * ellipse, specified as a 2D offset from center, and the reciprocals of the outer and inner radii,
196 * in both x and y directions.
197 *
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000198 * 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 +0000199 */
200
bsalomoncdaa97b2016-03-08 08:30:14 -0800201class EllipseGeometryProcessor : public GrGeometryProcessor {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000202public:
bsalomoncdaa97b2016-03-08 08:30:14 -0800203 EllipseGeometryProcessor(bool stroke, const SkMatrix& localMatrix)
204 : fLocalMatrix(localMatrix) {
205 this->initClassID<EllipseGeometryProcessor>();
206 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType));
207 fInColor = &this->addVertexAttrib(Attribute("inColor", kVec4ub_GrVertexAttribType));
208 fInEllipseOffset = &this->addVertexAttrib(Attribute("inEllipseOffset",
209 kVec2f_GrVertexAttribType));
210 fInEllipseRadii = &this->addVertexAttrib(Attribute("inEllipseRadii",
211 kVec4f_GrVertexAttribType));
212 fStroke = stroke;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000213 }
214
bsalomoncdaa97b2016-03-08 08:30:14 -0800215 virtual ~EllipseGeometryProcessor() {}
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000216
mtklein36352bf2015-03-25 18:17:31 -0700217 const char* name() const override { return "EllipseEdge"; }
joshualitt2dd1ae02014-12-03 06:24:10 -0800218
joshualitt71c92602015-01-14 08:12:47 -0800219 const Attribute* inPosition() const { return fInPosition; }
brianosmanbb2ff942016-02-11 14:15:18 -0800220 const Attribute* inColor() const { return fInColor; }
joshualitt71c92602015-01-14 08:12:47 -0800221 const Attribute* inEllipseOffset() const { return fInEllipseOffset; }
222 const Attribute* inEllipseRadii() const { return fInEllipseRadii; }
joshualitte3ababe2015-05-15 07:56:07 -0700223 const SkMatrix& localMatrix() const { return fLocalMatrix; }
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000224
egdaniel57d3b032015-11-13 11:57:27 -0800225 class GLSLProcessor : public GrGLSLGeometryProcessor {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000226 public:
brianosmanbb2ff942016-02-11 14:15:18 -0800227 GLSLProcessor() {}
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000228
mtklein36352bf2015-03-25 18:17:31 -0700229 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{
bsalomoncdaa97b2016-03-08 08:30:14 -0800230 const EllipseGeometryProcessor& egp = args.fGP.cast<EllipseGeometryProcessor>();
egdaniel4ca2e602015-11-18 08:01:26 -0800231 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
egdaniel0eafe792015-11-20 14:01:22 -0800232 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
egdaniel7ea439b2015-12-03 09:20:44 -0800233 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000234
joshualittabb52a12015-01-13 15:02:10 -0800235 // emit attributes
bsalomoncdaa97b2016-03-08 08:30:14 -0800236 varyingHandler->emitAttributes(egp);
joshualittabb52a12015-01-13 15:02:10 -0800237
egdaniel8dcdedc2015-11-11 06:27:20 -0800238 GrGLSLVertToFrag ellipseOffsets(kVec2f_GrSLType);
egdaniel0eafe792015-11-20 14:01:22 -0800239 varyingHandler->addVarying("EllipseOffsets", &ellipseOffsets);
egdaniel4ca2e602015-11-18 08:01:26 -0800240 vertBuilder->codeAppendf("%s = %s;", ellipseOffsets.vsOut(),
bsalomoncdaa97b2016-03-08 08:30:14 -0800241 egp.inEllipseOffset()->fName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000242
egdaniel8dcdedc2015-11-11 06:27:20 -0800243 GrGLSLVertToFrag ellipseRadii(kVec4f_GrSLType);
egdaniel0eafe792015-11-20 14:01:22 -0800244 varyingHandler->addVarying("EllipseRadii", &ellipseRadii);
egdaniel4ca2e602015-11-18 08:01:26 -0800245 vertBuilder->codeAppendf("%s = %s;", ellipseRadii.vsOut(),
bsalomoncdaa97b2016-03-08 08:30:14 -0800246 egp.inEllipseRadii()->fName);
joshualitt2dd1ae02014-12-03 06:24:10 -0800247
cdalton85285412016-02-18 12:37:07 -0800248 GrGLSLPPFragmentBuilder* fragBuilder = args.fFragBuilder;
joshualittb8c241a2015-05-19 08:23:30 -0700249 // setup pass through color
bsalomoncdaa97b2016-03-08 08:30:14 -0800250 varyingHandler->addPassThroughAttribute(egp.inColor(), args.fOutputColor);
joshualitt9b989322014-12-15 14:16:27 -0800251
joshualittabb52a12015-01-13 15:02:10 -0800252 // Setup position
bsalomoncdaa97b2016-03-08 08:30:14 -0800253 this->setupPosition(vertBuilder, gpArgs, egp.inPosition()->fName);
joshualittabb52a12015-01-13 15:02:10 -0800254
255 // emit transforms
egdaniel7ea439b2015-12-03 09:20:44 -0800256 this->emitTransforms(vertBuilder,
egdaniel0eafe792015-11-20 14:01:22 -0800257 varyingHandler,
egdaniel7ea439b2015-12-03 09:20:44 -0800258 uniformHandler,
egdaniel4ca2e602015-11-18 08:01:26 -0800259 gpArgs->fPositionVar,
bsalomoncdaa97b2016-03-08 08:30:14 -0800260 egp.inPosition()->fName,
261 egp.localMatrix(),
egdaniel4ca2e602015-11-18 08:01:26 -0800262 args.fTransformsIn,
263 args.fTransformsOut);
joshualitt4973d9d2014-11-08 09:24:25 -0800264
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +0000265 // for outer curve
egdaniel4ca2e602015-11-18 08:01:26 -0800266 fragBuilder->codeAppendf("vec2 scaledOffset = %s*%s.xy;", ellipseOffsets.fsIn(),
267 ellipseRadii.fsIn());
268 fragBuilder->codeAppend("float test = dot(scaledOffset, scaledOffset) - 1.0;");
269 fragBuilder->codeAppendf("vec2 grad = 2.0*scaledOffset*%s.xy;", ellipseRadii.fsIn());
270 fragBuilder->codeAppend("float grad_dot = dot(grad, grad);");
joshualitt74077b92014-10-24 11:26:03 -0700271
commit-bot@chromium.org1b035d82014-04-09 17:11:09 +0000272 // avoid calling inversesqrt on zero.
egdaniel4ca2e602015-11-18 08:01:26 -0800273 fragBuilder->codeAppend("grad_dot = max(grad_dot, 1.0e-4);");
274 fragBuilder->codeAppend("float invlen = inversesqrt(grad_dot);");
brianosmanc6052ac2016-02-12 10:20:00 -0800275 fragBuilder->codeAppend("float edgeAlpha = clamp(0.5-test*invlen, 0.0, 1.0);");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000276
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000277 // for inner curve
bsalomoncdaa97b2016-03-08 08:30:14 -0800278 if (egp.fStroke) {
egdaniel4ca2e602015-11-18 08:01:26 -0800279 fragBuilder->codeAppendf("scaledOffset = %s*%s.zw;",
280 ellipseOffsets.fsIn(), ellipseRadii.fsIn());
281 fragBuilder->codeAppend("test = dot(scaledOffset, scaledOffset) - 1.0;");
282 fragBuilder->codeAppendf("grad = 2.0*scaledOffset*%s.zw;",
283 ellipseRadii.fsIn());
284 fragBuilder->codeAppend("invlen = inversesqrt(dot(grad, grad));");
285 fragBuilder->codeAppend("edgeAlpha *= clamp(0.5+test*invlen, 0.0, 1.0);");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000286 }
287
egdaniel4ca2e602015-11-18 08:01:26 -0800288 fragBuilder->codeAppendf("%s = vec4(edgeAlpha);", args.fOutputCoverage);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000289 }
290
robertphillips46d36f02015-01-18 08:14:14 -0800291 static void GenKey(const GrGeometryProcessor& gp,
jvanverthcfc18862015-04-28 08:48:20 -0700292 const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700293 GrProcessorKeyBuilder* b) {
bsalomoncdaa97b2016-03-08 08:30:14 -0800294 const EllipseGeometryProcessor& egp = gp.cast<EllipseGeometryProcessor>();
295 uint16_t key = egp.fStroke ? 0x1 : 0x0;
296 key |= egp.localMatrix().hasPerspective() ? 0x2 : 0x0;
joshualittb8c241a2015-05-19 08:23:30 -0700297 b->add32(key);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000298 }
299
egdaniel018fb622015-10-28 07:26:40 -0700300 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& gp) override {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000301 }
302
joshualitte3ababe2015-05-15 07:56:07 -0700303 void setTransformData(const GrPrimitiveProcessor& primProc,
egdaniel018fb622015-10-28 07:26:40 -0700304 const GrGLSLProgramDataManager& pdman,
joshualitte3ababe2015-05-15 07:56:07 -0700305 int index,
306 const SkTArray<const GrCoordTransform*, true>& transforms) override {
bsalomoncdaa97b2016-03-08 08:30:14 -0800307 this->setTransformDataHelper<EllipseGeometryProcessor>(primProc, pdman, index,
308 transforms);
joshualitte3ababe2015-05-15 07:56:07 -0700309 }
310
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000311 private:
egdaniele659a582015-11-13 09:55:43 -0800312 typedef GrGLSLGeometryProcessor INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000313 };
314
egdaniel57d3b032015-11-13 11:57:27 -0800315 void getGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override {
316 GLSLProcessor::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800317 }
318
egdaniel57d3b032015-11-13 11:57:27 -0800319 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrGLSLCaps&) const override {
320 return new GLSLProcessor();
joshualitteb2a6762014-12-04 11:35:33 -0800321 }
322
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000323private:
joshualitt71c92602015-01-14 08:12:47 -0800324 const Attribute* fInPosition;
brianosmanbb2ff942016-02-11 14:15:18 -0800325 const Attribute* fInColor;
joshualitt71c92602015-01-14 08:12:47 -0800326 const Attribute* fInEllipseOffset;
327 const Attribute* fInEllipseRadii;
joshualitte3ababe2015-05-15 07:56:07 -0700328 SkMatrix fLocalMatrix;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000329 bool fStroke;
330
joshualittb0a8a372014-09-23 09:50:21 -0700331 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000332
joshualitt249af152014-09-15 11:41:13 -0700333 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000334};
335
bsalomoncdaa97b2016-03-08 08:30:14 -0800336GR_DEFINE_GEOMETRY_PROCESSOR_TEST(EllipseGeometryProcessor);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000337
bsalomoncdaa97b2016-03-08 08:30:14 -0800338const GrGeometryProcessor* EllipseGeometryProcessor::TestCreate(GrProcessorTestData* d) {
339 return new EllipseGeometryProcessor(d->fRandom->nextBool(), GrTest::TestMatrix(d->fRandom));
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000340}
341
342///////////////////////////////////////////////////////////////////////////////
343
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000344/**
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000345 * 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 +0000346 * specified as a 2D offset from center for both the outer and inner paths (if stroked). The
347 * implict equation used is for a unit circle (x^2 + y^2 - 1 = 0) and the edge corrected by
348 * using differentials.
349 *
350 * The result is device-independent and can be used with any affine matrix.
351 */
352
bsalomoncdaa97b2016-03-08 08:30:14 -0800353enum class DIEllipseStyle { kStroke = 0, kHairline, kFill };
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000354
bsalomoncdaa97b2016-03-08 08:30:14 -0800355class DIEllipseGeometryProcessor : public GrGeometryProcessor {
356public:
357 DIEllipseGeometryProcessor(const SkMatrix& viewMatrix, DIEllipseStyle style)
358 : fViewMatrix(viewMatrix) {
359 this->initClassID<DIEllipseGeometryProcessor>();
360 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType,
361 kHigh_GrSLPrecision));
362 fInColor = &this->addVertexAttrib(Attribute("inColor", kVec4ub_GrVertexAttribType));
363 fInEllipseOffsets0 = &this->addVertexAttrib(Attribute("inEllipseOffsets0",
364 kVec2f_GrVertexAttribType));
365 fInEllipseOffsets1 = &this->addVertexAttrib(Attribute("inEllipseOffsets1",
366 kVec2f_GrVertexAttribType));
367 fStyle = style;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000368 }
369
bsalomoncdaa97b2016-03-08 08:30:14 -0800370
371 virtual ~DIEllipseGeometryProcessor() {}
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000372
mtklein36352bf2015-03-25 18:17:31 -0700373 const char* name() const override { return "DIEllipseEdge"; }
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000374
joshualitt71c92602015-01-14 08:12:47 -0800375 const Attribute* inPosition() const { return fInPosition; }
brianosmanbb2ff942016-02-11 14:15:18 -0800376 const Attribute* inColor() const { return fInColor; }
joshualitt71c92602015-01-14 08:12:47 -0800377 const Attribute* inEllipseOffsets0() const { return fInEllipseOffsets0; }
378 const Attribute* inEllipseOffsets1() const { return fInEllipseOffsets1; }
joshualitte578a952015-05-14 10:09:13 -0700379 const SkMatrix& viewMatrix() const { return fViewMatrix; }
halcanary9d524f22016-03-29 09:03:52 -0700380
egdaniel57d3b032015-11-13 11:57:27 -0800381 class GLSLProcessor : public GrGLSLGeometryProcessor {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000382 public:
egdaniel57d3b032015-11-13 11:57:27 -0800383 GLSLProcessor()
brianosmanbb2ff942016-02-11 14:15:18 -0800384 : fViewMatrix(SkMatrix::InvalidMatrix()) {}
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000385
joshualitt465283c2015-09-11 08:19:35 -0700386 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
bsalomoncdaa97b2016-03-08 08:30:14 -0800387 const DIEllipseGeometryProcessor& diegp = args.fGP.cast<DIEllipseGeometryProcessor>();
egdaniel4ca2e602015-11-18 08:01:26 -0800388 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
egdaniel0eafe792015-11-20 14:01:22 -0800389 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
egdaniel7ea439b2015-12-03 09:20:44 -0800390 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000391
joshualittabb52a12015-01-13 15:02:10 -0800392 // emit attributes
bsalomoncdaa97b2016-03-08 08:30:14 -0800393 varyingHandler->emitAttributes(diegp);
joshualittabb52a12015-01-13 15:02:10 -0800394
egdaniel8dcdedc2015-11-11 06:27:20 -0800395 GrGLSLVertToFrag offsets0(kVec2f_GrSLType);
egdaniel0eafe792015-11-20 14:01:22 -0800396 varyingHandler->addVarying("EllipseOffsets0", &offsets0);
egdaniel4ca2e602015-11-18 08:01:26 -0800397 vertBuilder->codeAppendf("%s = %s;", offsets0.vsOut(),
bsalomoncdaa97b2016-03-08 08:30:14 -0800398 diegp.inEllipseOffsets0()->fName);
joshualitt74077b92014-10-24 11:26:03 -0700399
egdaniel8dcdedc2015-11-11 06:27:20 -0800400 GrGLSLVertToFrag offsets1(kVec2f_GrSLType);
egdaniel0eafe792015-11-20 14:01:22 -0800401 varyingHandler->addVarying("EllipseOffsets1", &offsets1);
egdaniel4ca2e602015-11-18 08:01:26 -0800402 vertBuilder->codeAppendf("%s = %s;", offsets1.vsOut(),
bsalomoncdaa97b2016-03-08 08:30:14 -0800403 diegp.inEllipseOffsets1()->fName);
joshualitt2dd1ae02014-12-03 06:24:10 -0800404
cdalton85285412016-02-18 12:37:07 -0800405 GrGLSLPPFragmentBuilder* fragBuilder = args.fFragBuilder;
bsalomoncdaa97b2016-03-08 08:30:14 -0800406 varyingHandler->addPassThroughAttribute(diegp.inColor(), args.fOutputColor);
joshualitt9b989322014-12-15 14:16:27 -0800407
joshualittabb52a12015-01-13 15:02:10 -0800408 // Setup position
egdaniel7ea439b2015-12-03 09:20:44 -0800409 this->setupPosition(vertBuilder,
410 uniformHandler,
egdaniel4ca2e602015-11-18 08:01:26 -0800411 gpArgs,
bsalomoncdaa97b2016-03-08 08:30:14 -0800412 diegp.inPosition()->fName,
413 diegp.viewMatrix(),
joshualitt5559ca22015-05-21 15:50:36 -0700414 &fViewMatrixUniform);
joshualittabb52a12015-01-13 15:02:10 -0800415
416 // emit transforms
egdaniel7ea439b2015-12-03 09:20:44 -0800417 this->emitTransforms(vertBuilder,
egdaniel0eafe792015-11-20 14:01:22 -0800418 varyingHandler,
egdaniel7ea439b2015-12-03 09:20:44 -0800419 uniformHandler,
egdaniel4ca2e602015-11-18 08:01:26 -0800420 gpArgs->fPositionVar,
bsalomoncdaa97b2016-03-08 08:30:14 -0800421 diegp.inPosition()->fName,
egdaniel4ca2e602015-11-18 08:01:26 -0800422 args.fTransformsIn,
423 args.fTransformsOut);
joshualitt4973d9d2014-11-08 09:24:25 -0800424
egdaniel4ca2e602015-11-18 08:01:26 -0800425 SkAssertResult(fragBuilder->enableFeature(
egdaniel2d721d32015-11-11 13:06:05 -0800426 GrGLSLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature));
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000427 // for outer curve
egdaniel4ca2e602015-11-18 08:01:26 -0800428 fragBuilder->codeAppendf("vec2 scaledOffset = %s.xy;", offsets0.fsIn());
429 fragBuilder->codeAppend("float test = dot(scaledOffset, scaledOffset) - 1.0;");
430 fragBuilder->codeAppendf("vec2 duvdx = dFdx(%s);", offsets0.fsIn());
431 fragBuilder->codeAppendf("vec2 duvdy = dFdy(%s);", offsets0.fsIn());
432 fragBuilder->codeAppendf("vec2 grad = vec2(2.0*%s.x*duvdx.x + 2.0*%s.y*duvdx.y,"
433 " 2.0*%s.x*duvdy.x + 2.0*%s.y*duvdy.y);",
bsalomoncdaa97b2016-03-08 08:30:14 -0800434 offsets0.fsIn(), offsets0.fsIn(), offsets0.fsIn(),
435 offsets0.fsIn());
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000436
egdaniel4ca2e602015-11-18 08:01:26 -0800437 fragBuilder->codeAppend("float grad_dot = dot(grad, grad);");
commit-bot@chromium.org1b035d82014-04-09 17:11:09 +0000438 // avoid calling inversesqrt on zero.
egdaniel4ca2e602015-11-18 08:01:26 -0800439 fragBuilder->codeAppend("grad_dot = max(grad_dot, 1.0e-4);");
440 fragBuilder->codeAppend("float invlen = inversesqrt(grad_dot);");
bsalomoncdaa97b2016-03-08 08:30:14 -0800441 if (DIEllipseStyle::kHairline == diegp.fStyle) {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000442 // can probably do this with one step
egdaniel4ca2e602015-11-18 08:01:26 -0800443 fragBuilder->codeAppend("float edgeAlpha = clamp(1.0-test*invlen, 0.0, 1.0);");
444 fragBuilder->codeAppend("edgeAlpha *= clamp(1.0+test*invlen, 0.0, 1.0);");
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000445 } else {
egdaniel4ca2e602015-11-18 08:01:26 -0800446 fragBuilder->codeAppend("float edgeAlpha = clamp(0.5-test*invlen, 0.0, 1.0);");
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000447 }
448
449 // for inner curve
bsalomoncdaa97b2016-03-08 08:30:14 -0800450 if (DIEllipseStyle::kStroke == diegp.fStyle) {
egdaniel4ca2e602015-11-18 08:01:26 -0800451 fragBuilder->codeAppendf("scaledOffset = %s.xy;", offsets1.fsIn());
452 fragBuilder->codeAppend("test = dot(scaledOffset, scaledOffset) - 1.0;");
453 fragBuilder->codeAppendf("duvdx = dFdx(%s);", offsets1.fsIn());
454 fragBuilder->codeAppendf("duvdy = dFdy(%s);", offsets1.fsIn());
455 fragBuilder->codeAppendf("grad = vec2(2.0*%s.x*duvdx.x + 2.0*%s.y*duvdx.y,"
456 " 2.0*%s.x*duvdy.x + 2.0*%s.y*duvdy.y);",
457 offsets1.fsIn(), offsets1.fsIn(), offsets1.fsIn(),
458 offsets1.fsIn());
459 fragBuilder->codeAppend("invlen = inversesqrt(dot(grad, grad));");
460 fragBuilder->codeAppend("edgeAlpha *= clamp(0.5+test*invlen, 0.0, 1.0);");
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000461 }
462
egdaniel4ca2e602015-11-18 08:01:26 -0800463 fragBuilder->codeAppendf("%s = vec4(edgeAlpha);", args.fOutputCoverage);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000464 }
465
robertphillips46d36f02015-01-18 08:14:14 -0800466 static void GenKey(const GrGeometryProcessor& gp,
jvanverthcfc18862015-04-28 08:48:20 -0700467 const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700468 GrProcessorKeyBuilder* b) {
bsalomoncdaa97b2016-03-08 08:30:14 -0800469 const DIEllipseGeometryProcessor& diegp = gp.cast<DIEllipseGeometryProcessor>();
470 uint16_t key = static_cast<uint16_t>(diegp.fStyle);
471 key |= ComputePosKey(diegp.viewMatrix()) << 10;
joshualittb8c241a2015-05-19 08:23:30 -0700472 b->add32(key);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000473 }
474
egdaniel018fb622015-10-28 07:26:40 -0700475 void setData(const GrGLSLProgramDataManager& pdman,
476 const GrPrimitiveProcessor& gp) override {
bsalomoncdaa97b2016-03-08 08:30:14 -0800477 const DIEllipseGeometryProcessor& diegp = gp.cast<DIEllipseGeometryProcessor>();
joshualitt5559ca22015-05-21 15:50:36 -0700478
bsalomoncdaa97b2016-03-08 08:30:14 -0800479 if (!diegp.viewMatrix().isIdentity() && !fViewMatrix.cheapEqualTo(diegp.viewMatrix())) {
480 fViewMatrix = diegp.viewMatrix();
egdaniel018fb622015-10-28 07:26:40 -0700481 float viewMatrix[3 * 3];
egdaniel64c47282015-11-13 06:54:19 -0800482 GrGLSLGetMatrix<3>(viewMatrix, fViewMatrix);
joshualitt5559ca22015-05-21 15:50:36 -0700483 pdman.setMatrix3f(fViewMatrixUniform, viewMatrix);
484 }
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000485 }
486
487 private:
joshualitt5559ca22015-05-21 15:50:36 -0700488 SkMatrix fViewMatrix;
joshualitt5559ca22015-05-21 15:50:36 -0700489 UniformHandle fViewMatrixUniform;
joshualitt9b989322014-12-15 14:16:27 -0800490
egdaniele659a582015-11-13 09:55:43 -0800491 typedef GrGLSLGeometryProcessor INHERITED;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000492 };
493
egdaniel57d3b032015-11-13 11:57:27 -0800494 void getGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override {
495 GLSLProcessor::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800496 }
497
egdaniel57d3b032015-11-13 11:57:27 -0800498 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrGLSLCaps&) const override {
499 return new GLSLProcessor();
joshualitteb2a6762014-12-04 11:35:33 -0800500 }
501
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000502private:
joshualitt71c92602015-01-14 08:12:47 -0800503 const Attribute* fInPosition;
brianosmanbb2ff942016-02-11 14:15:18 -0800504 const Attribute* fInColor;
joshualitt71c92602015-01-14 08:12:47 -0800505 const Attribute* fInEllipseOffsets0;
506 const Attribute* fInEllipseOffsets1;
bsalomoncdaa97b2016-03-08 08:30:14 -0800507 SkMatrix fViewMatrix;
508 DIEllipseStyle fStyle;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000509
joshualittb0a8a372014-09-23 09:50:21 -0700510 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000511
joshualitt249af152014-09-15 11:41:13 -0700512 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000513};
514
bsalomoncdaa97b2016-03-08 08:30:14 -0800515GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DIEllipseGeometryProcessor);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000516
bsalomoncdaa97b2016-03-08 08:30:14 -0800517const GrGeometryProcessor* DIEllipseGeometryProcessor::TestCreate(GrProcessorTestData* d) {
518 return new DIEllipseGeometryProcessor(GrTest::TestMatrix(d->fRandom),
519 (DIEllipseStyle)(d->fRandom->nextRangeU(0,2)));
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000520}
521
522///////////////////////////////////////////////////////////////////////////////
523
robertphillipsb56f9272016-02-25 11:03:52 -0800524GrDrawBatch* GrOvalRenderer::CreateOvalBatch(GrColor color,
robertphillips0cc2f852016-02-24 13:36:56 -0800525 const SkMatrix& viewMatrix,
robertphillips0cc2f852016-02-24 13:36:56 -0800526 const SkRect& oval,
527 const SkStrokeRec& stroke,
528 GrShaderCaps* shaderCaps) {
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000529 // we can draw circles
joshualitt8059eb92014-12-29 15:10:07 -0800530 if (SkScalarNearlyEqual(oval.width(), oval.height()) && circle_stays_circle(viewMatrix)) {
robertphillips0cc2f852016-02-24 13:36:56 -0800531 return CreateCircleBatch(color, viewMatrix, oval, stroke);
532 }
halcanary9d524f22016-03-29 09:03:52 -0700533
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000534 // if we have shader derivative support, render as device-independent
robertphillips0cc2f852016-02-24 13:36:56 -0800535 if (shaderCaps->shaderDerivativeSupport()) {
536 return CreateDIEllipseBatch(color, viewMatrix, oval, stroke);
537 }
halcanary9d524f22016-03-29 09:03:52 -0700538
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000539 // otherwise axis-aligned ellipses only
robertphillips0cc2f852016-02-24 13:36:56 -0800540 if (viewMatrix.rectStaysRect()) {
541 return CreateEllipseBatch(color, viewMatrix, oval, stroke);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000542 }
543
robertphillips0cc2f852016-02-24 13:36:56 -0800544 return nullptr;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000545}
546
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000547///////////////////////////////////////////////////////////////////////////////
548
bsalomonabd30f52015-08-13 13:34:48 -0700549class CircleBatch : public GrVertexBatch {
joshualitt76e7fb62015-02-11 08:52:27 -0800550public:
reed1b55a962015-09-17 20:16:13 -0700551 DEFINE_BATCH_CLASS_ID
552
joshualitt76e7fb62015-02-11 08:52:27 -0800553 struct Geometry {
reed1b55a962015-09-17 20:16:13 -0700554 SkRect fDevBounds;
joshualitt76e7fb62015-02-11 08:52:27 -0800555 SkScalar fInnerRadius;
556 SkScalar fOuterRadius;
reed1b55a962015-09-17 20:16:13 -0700557 GrColor fColor;
joshualitt76e7fb62015-02-11 08:52:27 -0800558 };
559
bsalomoncdaa97b2016-03-08 08:30:14 -0800560 CircleBatch(const Geometry& geometry, const SkMatrix& viewMatrix, bool stroked)
561 : INHERITED(ClassID())
562 , fStroked(stroked)
563 , fViewMatrixIfUsingLocalCoords(viewMatrix) {
564 fGeoData.push_back(geometry);
565 this->setBounds(geometry.fDevBounds);
566 }
mtklein36352bf2015-03-25 18:17:31 -0700567 const char* name() const override { return "CircleBatch"; }
joshualitt76e7fb62015-02-11 08:52:27 -0800568
robertphillipse004bfc2015-11-16 09:06:59 -0800569 SkString dumpInfo() const override {
570 SkString string;
571 for (int i = 0; i < fGeoData.count(); ++i) {
572 string.appendf("Color: 0x%08x Rect [L: %.2f, T: %.2f, R: %.2f, B: %.2f],"
573 "InnerRad: %.2f, OuterRad: %.2f\n",
574 fGeoData[i].fColor,
575 fGeoData[i].fDevBounds.fLeft, fGeoData[i].fDevBounds.fTop,
576 fGeoData[i].fDevBounds.fRight, fGeoData[i].fDevBounds.fBottom,
577 fGeoData[i].fInnerRadius,
578 fGeoData[i].fOuterRadius);
579 }
580 string.append(INHERITED::dumpInfo());
581 return string;
582 }
583
halcanary9d524f22016-03-29 09:03:52 -0700584 void computePipelineOptimizations(GrInitInvariantOutput* color,
ethannicholasff210322015-11-24 12:10:10 -0800585 GrInitInvariantOutput* coverage,
586 GrBatchToXPOverrides* overrides) const override {
joshualitt76e7fb62015-02-11 08:52:27 -0800587 // When this is called on a batch, there is only one geometry bundle
ethannicholasff210322015-11-24 12:10:10 -0800588 color->setKnownFourComponents(fGeoData[0].fColor);
589 coverage->setUnknownSingleComponent();
joshualitt76e7fb62015-02-11 08:52:27 -0800590 }
591
bsalomone46f9fe2015-08-18 06:05:14 -0700592private:
ethannicholasff210322015-11-24 12:10:10 -0800593 void initBatchTracker(const GrXPOverridesForBatch& overrides) override {
bsalomoncdaa97b2016-03-08 08:30:14 -0800594 // Handle any overrides that affect our GP.
ethannicholasff210322015-11-24 12:10:10 -0800595 overrides.getOverrideColorIfSet(&fGeoData[0].fColor);
bsalomoncdaa97b2016-03-08 08:30:14 -0800596 if (!overrides.readsLocalCoords()) {
597 fViewMatrixIfUsingLocalCoords.reset();
598 }
joshualitt76e7fb62015-02-11 08:52:27 -0800599 }
600
joshualitt144c3c82015-11-30 12:30:13 -0800601 void onPrepareDraws(Target* target) const override {
bsalomoncdaa97b2016-03-08 08:30:14 -0800602 SkMatrix localMatrix;
603 if (!fViewMatrixIfUsingLocalCoords.invert(&localMatrix)) {
joshualitt76e7fb62015-02-11 08:52:27 -0800604 return;
605 }
606
607 // Setup geometry processor
bsalomoncdaa97b2016-03-08 08:30:14 -0800608 SkAutoTUnref<GrGeometryProcessor> gp(new CircleGeometryProcessor(fStroked, localMatrix));
joshualitt76e7fb62015-02-11 08:52:27 -0800609
joshualitt76e7fb62015-02-11 08:52:27 -0800610 int instanceCount = fGeoData.count();
joshualitt76e7fb62015-02-11 08:52:27 -0800611 size_t vertexStride = gp->getVertexStride();
612 SkASSERT(vertexStride == sizeof(CircleVertex));
bsalomonb5238a72015-05-05 07:49:49 -0700613 QuadHelper helper;
bsalomon75398562015-08-17 12:55:38 -0700614 CircleVertex* verts = reinterpret_cast<CircleVertex*>(helper.init(target, vertexStride,
bsalomonb5238a72015-05-05 07:49:49 -0700615 instanceCount));
616 if (!verts) {
joshualitt4b31de82015-03-05 14:33:41 -0800617 return;
618 }
619
joshualitt76e7fb62015-02-11 08:52:27 -0800620 for (int i = 0; i < instanceCount; i++) {
joshualitt144c3c82015-11-30 12:30:13 -0800621 const Geometry& geom = fGeoData[i];
joshualitt76e7fb62015-02-11 08:52:27 -0800622
brianosmanbb2ff942016-02-11 14:15:18 -0800623 GrColor color = geom.fColor;
bsalomonb5238a72015-05-05 07:49:49 -0700624 SkScalar innerRadius = geom.fInnerRadius;
625 SkScalar outerRadius = geom.fOuterRadius;
joshualitt76e7fb62015-02-11 08:52:27 -0800626
bsalomonb5238a72015-05-05 07:49:49 -0700627 const SkRect& bounds = geom.fDevBounds;
joshualitt76e7fb62015-02-11 08:52:27 -0800628
629 // The inner radius in the vertex data must be specified in normalized space.
630 innerRadius = innerRadius / outerRadius;
631 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
brianosmanbb2ff942016-02-11 14:15:18 -0800632 verts[0].fColor = color;
joshualitt76e7fb62015-02-11 08:52:27 -0800633 verts[0].fOffset = SkPoint::Make(-1, -1);
634 verts[0].fOuterRadius = outerRadius;
635 verts[0].fInnerRadius = innerRadius;
636
637 verts[1].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
brianosmanbb2ff942016-02-11 14:15:18 -0800638 verts[1].fColor = color;
joshualitt76e7fb62015-02-11 08:52:27 -0800639 verts[1].fOffset = SkPoint::Make(-1, 1);
640 verts[1].fOuterRadius = outerRadius;
641 verts[1].fInnerRadius = innerRadius;
642
643 verts[2].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
brianosmanbb2ff942016-02-11 14:15:18 -0800644 verts[2].fColor = color;
joshualitt76e7fb62015-02-11 08:52:27 -0800645 verts[2].fOffset = SkPoint::Make(1, 1);
646 verts[2].fOuterRadius = outerRadius;
647 verts[2].fInnerRadius = innerRadius;
648
649 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
brianosmanbb2ff942016-02-11 14:15:18 -0800650 verts[3].fColor = color;
joshualitt76e7fb62015-02-11 08:52:27 -0800651 verts[3].fOffset = SkPoint::Make(1, -1);
652 verts[3].fOuterRadius = outerRadius;
653 verts[3].fInnerRadius = innerRadius;
654
bsalomonb5238a72015-05-05 07:49:49 -0700655 verts += kVerticesPerQuad;
joshualitt76e7fb62015-02-11 08:52:27 -0800656 }
bsalomon342bfc22016-04-01 06:06:20 -0700657 helper.recordDraw(target, gp);
joshualitt76e7fb62015-02-11 08:52:27 -0800658 }
659
bsalomoncb02b382015-08-12 11:14:50 -0700660 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override {
bsalomonabd30f52015-08-13 13:34:48 -0700661 CircleBatch* that = t->cast<CircleBatch>();
662 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
663 that->bounds(), caps)) {
joshualitt8cab9a72015-07-16 09:13:50 -0700664 return false;
665 }
666
bsalomoncdaa97b2016-03-08 08:30:14 -0800667 if (this->fStroked != that->fStroked) {
joshualitt76e7fb62015-02-11 08:52:27 -0800668 return false;
669 }
670
bsalomoncdaa97b2016-03-08 08:30:14 -0800671 if (!fViewMatrixIfUsingLocalCoords.cheapEqualTo(that->fViewMatrixIfUsingLocalCoords)) {
joshualitt76e7fb62015-02-11 08:52:27 -0800672 return false;
673 }
674
bsalomoncdaa97b2016-03-08 08:30:14 -0800675 fGeoData.push_back_n(that->fGeoData.count(), that->fGeoData.begin());
joshualitt99c7c072015-05-01 13:43:30 -0700676 this->joinBounds(that->bounds());
joshualitt76e7fb62015-02-11 08:52:27 -0800677 return true;
678 }
679
bsalomoncdaa97b2016-03-08 08:30:14 -0800680 bool fStroked;
681 SkMatrix fViewMatrixIfUsingLocalCoords;
joshualitt76e7fb62015-02-11 08:52:27 -0800682 SkSTArray<1, Geometry, true> fGeoData;
reed1b55a962015-09-17 20:16:13 -0700683
684 typedef GrVertexBatch INHERITED;
joshualitt76e7fb62015-02-11 08:52:27 -0800685};
686
bsalomonabd30f52015-08-13 13:34:48 -0700687static GrDrawBatch* create_circle_batch(GrColor color,
688 const SkMatrix& viewMatrix,
bsalomonabd30f52015-08-13 13:34:48 -0700689 const SkRect& circle,
690 const SkStrokeRec& stroke) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000691 SkPoint center = SkPoint::Make(circle.centerX(), circle.centerY());
joshualitt8059eb92014-12-29 15:10:07 -0800692 viewMatrix.mapPoints(&center, 1);
693 SkScalar radius = viewMatrix.mapRadius(SkScalarHalf(circle.width()));
694 SkScalar strokeWidth = viewMatrix.mapRadius(stroke.getWidth());
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000695
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000696 SkStrokeRec::Style style = stroke.getStyle();
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000697 bool isStrokeOnly = SkStrokeRec::kStroke_Style == style ||
698 SkStrokeRec::kHairline_Style == style;
699 bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == style;
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000700
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000701 SkScalar innerRadius = 0.0f;
702 SkScalar outerRadius = radius;
703 SkScalar halfWidth = 0;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000704 if (hasStroke) {
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000705 if (SkScalarNearlyZero(strokeWidth)) {
706 halfWidth = SK_ScalarHalf;
707 } else {
708 halfWidth = SkScalarHalf(strokeWidth);
709 }
710
711 outerRadius += halfWidth;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000712 if (isStrokeOnly) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000713 innerRadius = radius - halfWidth;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000714 }
715 }
716
bsalomonce1c8862014-12-15 07:11:22 -0800717 // The radii are outset for two reasons. First, it allows the shader to simply perform simpler
718 // computation because the computed alpha is zero, rather than 50%, at the radius.
719 // Second, the outer radius is used to compute the verts of the bounding box that is rendered
720 // and the outset ensures the box will cover all partially covered by the circle.
bsalomon@google.com58e30fe2013-04-01 19:01:20 +0000721 outerRadius += SK_ScalarHalf;
722 innerRadius -= SK_ScalarHalf;
723
joshualitt76e7fb62015-02-11 08:52:27 -0800724 CircleBatch::Geometry geometry;
joshualitt76e7fb62015-02-11 08:52:27 -0800725 geometry.fColor = color;
726 geometry.fInnerRadius = innerRadius;
727 geometry.fOuterRadius = outerRadius;
joshualittd96a67b2015-05-05 14:09:05 -0700728 geometry.fDevBounds = SkRect::MakeLTRB(center.fX - outerRadius, center.fY - outerRadius,
729 center.fX + outerRadius, center.fY + outerRadius);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000730
bsalomoncdaa97b2016-03-08 08:30:14 -0800731 return new CircleBatch(geometry, viewMatrix, isStrokeOnly && innerRadius > 0);
joshualitt3e708c52015-04-30 13:49:27 -0700732}
733
robertphillips0cc2f852016-02-24 13:36:56 -0800734GrDrawBatch* GrOvalRenderer::CreateCircleBatch(GrColor color,
735 const SkMatrix& viewMatrix,
736 const SkRect& circle,
737 const SkStrokeRec& stroke) {
738 return create_circle_batch(color, viewMatrix, circle, stroke);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000739}
740
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000741///////////////////////////////////////////////////////////////////////////////
742
bsalomonabd30f52015-08-13 13:34:48 -0700743class EllipseBatch : public GrVertexBatch {
joshualitt76e7fb62015-02-11 08:52:27 -0800744public:
reed1b55a962015-09-17 20:16:13 -0700745 DEFINE_BATCH_CLASS_ID
746
joshualitt76e7fb62015-02-11 08:52:27 -0800747 struct Geometry {
reed1b55a962015-09-17 20:16:13 -0700748 SkRect fDevBounds;
joshualitt76e7fb62015-02-11 08:52:27 -0800749 SkScalar fXRadius;
750 SkScalar fYRadius;
751 SkScalar fInnerXRadius;
752 SkScalar fInnerYRadius;
reed1b55a962015-09-17 20:16:13 -0700753 GrColor fColor;
joshualitt76e7fb62015-02-11 08:52:27 -0800754 };
755
bsalomoncdaa97b2016-03-08 08:30:14 -0800756 EllipseBatch(const Geometry& geometry, const SkMatrix& viewMatrix, bool stroked)
757 : INHERITED(ClassID())
758 , fStroked(stroked)
759 , fViewMatrixIfUsingLocalCoords(viewMatrix) {
760 fGeoData.push_back(geometry);
761 this->setBounds(geometry.fDevBounds);
762 }
joshualitt76e7fb62015-02-11 08:52:27 -0800763
mtklein36352bf2015-03-25 18:17:31 -0700764 const char* name() const override { return "EllipseBatch"; }
joshualitt76e7fb62015-02-11 08:52:27 -0800765
halcanary9d524f22016-03-29 09:03:52 -0700766 void computePipelineOptimizations(GrInitInvariantOutput* color,
ethannicholasff210322015-11-24 12:10:10 -0800767 GrInitInvariantOutput* coverage,
768 GrBatchToXPOverrides* overrides) const override {
joshualitt76e7fb62015-02-11 08:52:27 -0800769 // When this is called on a batch, there is only one geometry bundle
ethannicholasff210322015-11-24 12:10:10 -0800770 color->setKnownFourComponents(fGeoData[0].fColor);
771 coverage->setUnknownSingleComponent();
joshualitt76e7fb62015-02-11 08:52:27 -0800772 }
773
bsalomone46f9fe2015-08-18 06:05:14 -0700774private:
ethannicholasff210322015-11-24 12:10:10 -0800775 void initBatchTracker(const GrXPOverridesForBatch& overrides) override {
bsalomoncdaa97b2016-03-08 08:30:14 -0800776 // Handle any overrides that affect our GP.
ethannicholasff210322015-11-24 12:10:10 -0800777 if (!overrides.readsCoverage()) {
joshualitt76e7fb62015-02-11 08:52:27 -0800778 fGeoData[0].fColor = GrColor_ILLEGAL;
joshualitt76e7fb62015-02-11 08:52:27 -0800779 }
bsalomoncdaa97b2016-03-08 08:30:14 -0800780 if (!overrides.readsLocalCoords()) {
781 fViewMatrixIfUsingLocalCoords.reset();
782 }
joshualitt76e7fb62015-02-11 08:52:27 -0800783 }
784
joshualitt144c3c82015-11-30 12:30:13 -0800785 void onPrepareDraws(Target* target) const override {
bsalomoncdaa97b2016-03-08 08:30:14 -0800786 SkMatrix localMatrix;
787 if (!fViewMatrixIfUsingLocalCoords.invert(&localMatrix)) {
joshualitt76e7fb62015-02-11 08:52:27 -0800788 return;
789 }
790
791 // Setup geometry processor
bsalomoncdaa97b2016-03-08 08:30:14 -0800792 SkAutoTUnref<GrGeometryProcessor> gp(new EllipseGeometryProcessor(fStroked, localMatrix));
joshualitt76e7fb62015-02-11 08:52:27 -0800793
joshualitt76e7fb62015-02-11 08:52:27 -0800794 int instanceCount = fGeoData.count();
bsalomonb5238a72015-05-05 07:49:49 -0700795 QuadHelper helper;
joshualitt76e7fb62015-02-11 08:52:27 -0800796 size_t vertexStride = gp->getVertexStride();
joshualitt19e00582015-02-11 17:36:30 -0800797 SkASSERT(vertexStride == sizeof(EllipseVertex));
bsalomonb5238a72015-05-05 07:49:49 -0700798 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(
bsalomon75398562015-08-17 12:55:38 -0700799 helper.init(target, vertexStride, instanceCount));
bsalomonb5238a72015-05-05 07:49:49 -0700800 if (!verts) {
joshualitt4b31de82015-03-05 14:33:41 -0800801 return;
802 }
803
bsalomon8415abe2015-05-04 11:41:41 -0700804 for (int i = 0; i < instanceCount; i++) {
joshualitt144c3c82015-11-30 12:30:13 -0800805 const Geometry& geom = fGeoData[i];
bsalomon8415abe2015-05-04 11:41:41 -0700806
brianosmanbb2ff942016-02-11 14:15:18 -0800807 GrColor color = geom.fColor;
bsalomonb5238a72015-05-05 07:49:49 -0700808 SkScalar xRadius = geom.fXRadius;
809 SkScalar yRadius = geom.fYRadius;
joshualitt76e7fb62015-02-11 08:52:27 -0800810
811 // Compute the reciprocals of the radii here to save time in the shader
812 SkScalar xRadRecip = SkScalarInvert(xRadius);
813 SkScalar yRadRecip = SkScalarInvert(yRadius);
bsalomonb5238a72015-05-05 07:49:49 -0700814 SkScalar xInnerRadRecip = SkScalarInvert(geom.fInnerXRadius);
815 SkScalar yInnerRadRecip = SkScalarInvert(geom.fInnerYRadius);
joshualitt76e7fb62015-02-11 08:52:27 -0800816
bsalomonb5238a72015-05-05 07:49:49 -0700817 const SkRect& bounds = geom.fDevBounds;
joshualitt76e7fb62015-02-11 08:52:27 -0800818
819 // The inner radius in the vertex data must be specified in normalized space.
820 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
brianosmanbb2ff942016-02-11 14:15:18 -0800821 verts[0].fColor = color;
joshualitt76e7fb62015-02-11 08:52:27 -0800822 verts[0].fOffset = SkPoint::Make(-xRadius, -yRadius);
823 verts[0].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
824 verts[0].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
825
826 verts[1].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
brianosmanbb2ff942016-02-11 14:15:18 -0800827 verts[1].fColor = color;
joshualitt76e7fb62015-02-11 08:52:27 -0800828 verts[1].fOffset = SkPoint::Make(-xRadius, yRadius);
829 verts[1].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
830 verts[1].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
831
832 verts[2].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
brianosmanbb2ff942016-02-11 14:15:18 -0800833 verts[2].fColor = color;
joshualitt76e7fb62015-02-11 08:52:27 -0800834 verts[2].fOffset = SkPoint::Make(xRadius, yRadius);
835 verts[2].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
836 verts[2].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
837
838 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
brianosmanbb2ff942016-02-11 14:15:18 -0800839 verts[3].fColor = color;
joshualitt76e7fb62015-02-11 08:52:27 -0800840 verts[3].fOffset = SkPoint::Make(xRadius, -yRadius);
841 verts[3].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
842 verts[3].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
843
bsalomonb5238a72015-05-05 07:49:49 -0700844 verts += kVerticesPerQuad;
joshualitt76e7fb62015-02-11 08:52:27 -0800845 }
bsalomon342bfc22016-04-01 06:06:20 -0700846 helper.recordDraw(target, gp);
joshualitt76e7fb62015-02-11 08:52:27 -0800847 }
848
bsalomoncb02b382015-08-12 11:14:50 -0700849 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override {
bsalomonabd30f52015-08-13 13:34:48 -0700850 EllipseBatch* that = t->cast<EllipseBatch>();
851
852 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
853 that->bounds(), caps)) {
joshualitt8cab9a72015-07-16 09:13:50 -0700854 return false;
855 }
856
bsalomoncdaa97b2016-03-08 08:30:14 -0800857 if (fStroked != that->fStroked) {
joshualitt76e7fb62015-02-11 08:52:27 -0800858 return false;
859 }
860
bsalomoncdaa97b2016-03-08 08:30:14 -0800861 if (!fViewMatrixIfUsingLocalCoords.cheapEqualTo(that->fViewMatrixIfUsingLocalCoords)) {
joshualitt76e7fb62015-02-11 08:52:27 -0800862 return false;
863 }
864
bsalomoncdaa97b2016-03-08 08:30:14 -0800865 fGeoData.push_back_n(that->fGeoData.count(), that->fGeoData.begin());
joshualitt99c7c072015-05-01 13:43:30 -0700866 this->joinBounds(that->bounds());
joshualitt76e7fb62015-02-11 08:52:27 -0800867 return true;
868 }
869
joshualitt76e7fb62015-02-11 08:52:27 -0800870
bsalomoncdaa97b2016-03-08 08:30:14 -0800871 bool fStroked;
872 SkMatrix fViewMatrixIfUsingLocalCoords;
joshualitt76e7fb62015-02-11 08:52:27 -0800873 SkSTArray<1, Geometry, true> fGeoData;
reed1b55a962015-09-17 20:16:13 -0700874
875 typedef GrVertexBatch INHERITED;
joshualitt76e7fb62015-02-11 08:52:27 -0800876};
877
bsalomonabd30f52015-08-13 13:34:48 -0700878static GrDrawBatch* create_ellipse_batch(GrColor color,
879 const SkMatrix& viewMatrix,
bsalomonabd30f52015-08-13 13:34:48 -0700880 const SkRect& ellipse,
881 const SkStrokeRec& stroke) {
robertphillips0cc2f852016-02-24 13:36:56 -0800882 SkASSERT(viewMatrix.rectStaysRect());
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000883
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000884 // do any matrix crunching before we reset the draw state for device coords
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000885 SkPoint center = SkPoint::Make(ellipse.centerX(), ellipse.centerY());
joshualitt8059eb92014-12-29 15:10:07 -0800886 viewMatrix.mapPoints(&center, 1);
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000887 SkScalar ellipseXRadius = SkScalarHalf(ellipse.width());
888 SkScalar ellipseYRadius = SkScalarHalf(ellipse.height());
joshualitt8059eb92014-12-29 15:10:07 -0800889 SkScalar xRadius = SkScalarAbs(viewMatrix[SkMatrix::kMScaleX]*ellipseXRadius +
890 viewMatrix[SkMatrix::kMSkewY]*ellipseYRadius);
891 SkScalar yRadius = SkScalarAbs(viewMatrix[SkMatrix::kMSkewX]*ellipseXRadius +
892 viewMatrix[SkMatrix::kMScaleY]*ellipseYRadius);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000893
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000894 // do (potentially) anisotropic mapping of stroke
895 SkVector scaledStroke;
896 SkScalar strokeWidth = stroke.getWidth();
joshualitt8059eb92014-12-29 15:10:07 -0800897 scaledStroke.fX = SkScalarAbs(strokeWidth*(viewMatrix[SkMatrix::kMScaleX] +
898 viewMatrix[SkMatrix::kMSkewY]));
899 scaledStroke.fY = SkScalarAbs(strokeWidth*(viewMatrix[SkMatrix::kMSkewX] +
900 viewMatrix[SkMatrix::kMScaleY]));
commit-bot@chromium.org0c888282013-04-19 19:01:45 +0000901
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000902 SkStrokeRec::Style style = stroke.getStyle();
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000903 bool isStrokeOnly = SkStrokeRec::kStroke_Style == style ||
904 SkStrokeRec::kHairline_Style == style;
905 bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == style;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000906
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000907 SkScalar innerXRadius = 0;
908 SkScalar innerYRadius = 0;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000909 if (hasStroke) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000910 if (SkScalarNearlyZero(scaledStroke.length())) {
911 scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf);
912 } else {
913 scaledStroke.scale(SK_ScalarHalf);
914 }
915
916 // we only handle thick strokes for near-circular ellipses
917 if (scaledStroke.length() > SK_ScalarHalf &&
918 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700919 return nullptr;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000920 }
921
922 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
923 if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius ||
924 scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) {
halcanary96fcdcc2015-08-27 07:41:13 -0700925 return nullptr;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000926 }
927
928 // this is legit only if scale & translation (which should be the case at the moment)
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000929 if (isStrokeOnly) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000930 innerXRadius = xRadius - scaledStroke.fX;
931 innerYRadius = yRadius - scaledStroke.fY;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000932 }
933
934 xRadius += scaledStroke.fX;
935 yRadius += scaledStroke.fY;
936 }
937
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000938 // We've extended the outer x radius out half a pixel to antialias.
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000939 // This will also expand the rect so all the pixels will be captured.
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000940 // TODO: Consider if we should use sqrt(2)/2 instead
brianosmanbd6366a2016-02-14 10:33:03 -0800941 xRadius += SK_ScalarHalf;
942 yRadius += SK_ScalarHalf;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000943
joshualitt76e7fb62015-02-11 08:52:27 -0800944 EllipseBatch::Geometry geometry;
joshualitt76e7fb62015-02-11 08:52:27 -0800945 geometry.fColor = color;
946 geometry.fXRadius = xRadius;
947 geometry.fYRadius = yRadius;
948 geometry.fInnerXRadius = innerXRadius;
949 geometry.fInnerYRadius = innerYRadius;
brianosmanbd6366a2016-02-14 10:33:03 -0800950 geometry.fDevBounds = SkRect::MakeLTRB(center.fX - xRadius, center.fY - yRadius,
951 center.fX + xRadius, center.fY + yRadius);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000952
bsalomoncdaa97b2016-03-08 08:30:14 -0800953 return new EllipseBatch(geometry, viewMatrix,
954 isStrokeOnly && innerXRadius > 0 && innerYRadius > 0);
joshualitt3e708c52015-04-30 13:49:27 -0700955}
956
robertphillips0cc2f852016-02-24 13:36:56 -0800957GrDrawBatch* GrOvalRenderer::CreateEllipseBatch(GrColor color,
958 const SkMatrix& viewMatrix,
959 const SkRect& ellipse,
960 const SkStrokeRec& stroke) {
961 return create_ellipse_batch(color, viewMatrix, ellipse, stroke);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000962}
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000963
joshualitt76e7fb62015-02-11 08:52:27 -0800964/////////////////////////////////////////////////////////////////////////////////////////////////
965
bsalomonabd30f52015-08-13 13:34:48 -0700966class DIEllipseBatch : public GrVertexBatch {
joshualitt76e7fb62015-02-11 08:52:27 -0800967public:
reed1b55a962015-09-17 20:16:13 -0700968 DEFINE_BATCH_CLASS_ID
969
joshualitt76e7fb62015-02-11 08:52:27 -0800970 struct Geometry {
joshualitt76e7fb62015-02-11 08:52:27 -0800971 SkMatrix fViewMatrix;
reed1b55a962015-09-17 20:16:13 -0700972 SkRect fBounds;
joshualitt76e7fb62015-02-11 08:52:27 -0800973 SkScalar fXRadius;
974 SkScalar fYRadius;
975 SkScalar fInnerXRadius;
976 SkScalar fInnerYRadius;
977 SkScalar fGeoDx;
978 SkScalar fGeoDy;
reed1b55a962015-09-17 20:16:13 -0700979 GrColor fColor;
bsalomoncdaa97b2016-03-08 08:30:14 -0800980 DIEllipseStyle fStyle;
joshualitt76e7fb62015-02-11 08:52:27 -0800981 };
982
bsalomonabd30f52015-08-13 13:34:48 -0700983 static GrDrawBatch* Create(const Geometry& geometry, const SkRect& bounds) {
halcanary385fe4d2015-08-26 13:07:48 -0700984 return new DIEllipseBatch(geometry, bounds);
joshualitt76e7fb62015-02-11 08:52:27 -0800985 }
986
mtklein36352bf2015-03-25 18:17:31 -0700987 const char* name() const override { return "DIEllipseBatch"; }
joshualitt76e7fb62015-02-11 08:52:27 -0800988
halcanary9d524f22016-03-29 09:03:52 -0700989 void computePipelineOptimizations(GrInitInvariantOutput* color,
ethannicholasff210322015-11-24 12:10:10 -0800990 GrInitInvariantOutput* coverage,
991 GrBatchToXPOverrides* overrides) const override {
joshualitt76e7fb62015-02-11 08:52:27 -0800992 // When this is called on a batch, there is only one geometry bundle
ethannicholasff210322015-11-24 12:10:10 -0800993 color->setKnownFourComponents(fGeoData[0].fColor);
994 coverage->setUnknownSingleComponent();
joshualitt76e7fb62015-02-11 08:52:27 -0800995 }
996
bsalomone46f9fe2015-08-18 06:05:14 -0700997private:
998
ethannicholasff210322015-11-24 12:10:10 -0800999 void initBatchTracker(const GrXPOverridesForBatch& overrides) override {
bsalomoncdaa97b2016-03-08 08:30:14 -08001000 // Handle any overrides that affect our GP.
ethannicholasff210322015-11-24 12:10:10 -08001001 overrides.getOverrideColorIfSet(&fGeoData[0].fColor);
bsalomoncdaa97b2016-03-08 08:30:14 -08001002 fUsesLocalCoords = overrides.readsLocalCoords();
joshualitt76e7fb62015-02-11 08:52:27 -08001003 }
1004
joshualitt144c3c82015-11-30 12:30:13 -08001005 void onPrepareDraws(Target* target) const override {
joshualitt76e7fb62015-02-11 08:52:27 -08001006 // Setup geometry processor
bsalomoncdaa97b2016-03-08 08:30:14 -08001007 SkAutoTUnref<GrGeometryProcessor> gp(new DIEllipseGeometryProcessor(this->viewMatrix(),
1008 this->style()));
joshualitt76e7fb62015-02-11 08:52:27 -08001009
joshualitt76e7fb62015-02-11 08:52:27 -08001010 int instanceCount = fGeoData.count();
joshualitt76e7fb62015-02-11 08:52:27 -08001011 size_t vertexStride = gp->getVertexStride();
joshualitt19e00582015-02-11 17:36:30 -08001012 SkASSERT(vertexStride == sizeof(DIEllipseVertex));
bsalomonb5238a72015-05-05 07:49:49 -07001013 QuadHelper helper;
1014 DIEllipseVertex* verts = reinterpret_cast<DIEllipseVertex*>(
bsalomon75398562015-08-17 12:55:38 -07001015 helper.init(target, vertexStride, instanceCount));
bsalomonb5238a72015-05-05 07:49:49 -07001016 if (!verts) {
joshualitt4b31de82015-03-05 14:33:41 -08001017 return;
1018 }
1019
joshualitt76e7fb62015-02-11 08:52:27 -08001020 for (int i = 0; i < instanceCount; i++) {
joshualitt144c3c82015-11-30 12:30:13 -08001021 const Geometry& geom = fGeoData[i];
joshualitt76e7fb62015-02-11 08:52:27 -08001022
brianosmanbb2ff942016-02-11 14:15:18 -08001023 GrColor color = geom.fColor;
bsalomonb5238a72015-05-05 07:49:49 -07001024 SkScalar xRadius = geom.fXRadius;
1025 SkScalar yRadius = geom.fYRadius;
joshualitt76e7fb62015-02-11 08:52:27 -08001026
bsalomonb5238a72015-05-05 07:49:49 -07001027 const SkRect& bounds = geom.fBounds;
joshualitt76e7fb62015-02-11 08:52:27 -08001028
1029 // This adjusts the "radius" to include the half-pixel border
reed80ea19c2015-05-12 10:37:34 -07001030 SkScalar offsetDx = geom.fGeoDx / xRadius;
1031 SkScalar offsetDy = geom.fGeoDy / yRadius;
joshualitt76e7fb62015-02-11 08:52:27 -08001032
reed80ea19c2015-05-12 10:37:34 -07001033 SkScalar innerRatioX = xRadius / geom.fInnerXRadius;
1034 SkScalar innerRatioY = yRadius / geom.fInnerYRadius;
joshualitt76e7fb62015-02-11 08:52:27 -08001035
1036 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
brianosmanbb2ff942016-02-11 14:15:18 -08001037 verts[0].fColor = color;
joshualitt76e7fb62015-02-11 08:52:27 -08001038 verts[0].fOuterOffset = SkPoint::Make(-1.0f - offsetDx, -1.0f - offsetDy);
1039 verts[0].fInnerOffset = SkPoint::Make(-innerRatioX - offsetDx, -innerRatioY - offsetDy);
1040
1041 verts[1].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
brianosmanbb2ff942016-02-11 14:15:18 -08001042 verts[1].fColor = color;
joshualitt76e7fb62015-02-11 08:52:27 -08001043 verts[1].fOuterOffset = SkPoint::Make(-1.0f - offsetDx, 1.0f + offsetDy);
1044 verts[1].fInnerOffset = SkPoint::Make(-innerRatioX - offsetDx, innerRatioY + offsetDy);
1045
1046 verts[2].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
brianosmanbb2ff942016-02-11 14:15:18 -08001047 verts[2].fColor = color;
joshualitt76e7fb62015-02-11 08:52:27 -08001048 verts[2].fOuterOffset = SkPoint::Make(1.0f + offsetDx, 1.0f + offsetDy);
1049 verts[2].fInnerOffset = SkPoint::Make(innerRatioX + offsetDx, innerRatioY + offsetDy);
1050
1051 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
brianosmanbb2ff942016-02-11 14:15:18 -08001052 verts[3].fColor = color;
joshualitt76e7fb62015-02-11 08:52:27 -08001053 verts[3].fOuterOffset = SkPoint::Make(1.0f + offsetDx, -1.0f - offsetDy);
1054 verts[3].fInnerOffset = SkPoint::Make(innerRatioX + offsetDx, -innerRatioY - offsetDy);
1055
bsalomonb5238a72015-05-05 07:49:49 -07001056 verts += kVerticesPerQuad;
joshualitt76e7fb62015-02-11 08:52:27 -08001057 }
bsalomon342bfc22016-04-01 06:06:20 -07001058 helper.recordDraw(target, gp);
joshualitt76e7fb62015-02-11 08:52:27 -08001059 }
halcanary9d524f22016-03-29 09:03:52 -07001060
reed1b55a962015-09-17 20:16:13 -07001061 DIEllipseBatch(const Geometry& geometry, const SkRect& bounds) : INHERITED(ClassID()) {
joshualitt76e7fb62015-02-11 08:52:27 -08001062 fGeoData.push_back(geometry);
joshualitt99c7c072015-05-01 13:43:30 -07001063
1064 this->setBounds(bounds);
joshualitt76e7fb62015-02-11 08:52:27 -08001065 }
1066
bsalomoncb02b382015-08-12 11:14:50 -07001067 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override {
bsalomonabd30f52015-08-13 13:34:48 -07001068 DIEllipseBatch* that = t->cast<DIEllipseBatch>();
1069 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
1070 that->bounds(), caps)) {
joshualitt8cab9a72015-07-16 09:13:50 -07001071 return false;
1072 }
1073
bsalomoncdaa97b2016-03-08 08:30:14 -08001074 if (this->style() != that->style()) {
joshualitt76e7fb62015-02-11 08:52:27 -08001075 return false;
1076 }
1077
joshualittd96a67b2015-05-05 14:09:05 -07001078 // TODO rewrite to allow positioning on CPU
1079 if (!this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
joshualitt76e7fb62015-02-11 08:52:27 -08001080 return false;
1081 }
1082
bsalomoncdaa97b2016-03-08 08:30:14 -08001083 fGeoData.push_back_n(that->fGeoData.count(), that->fGeoData.begin());
joshualitt99c7c072015-05-01 13:43:30 -07001084 this->joinBounds(that->bounds());
joshualitt76e7fb62015-02-11 08:52:27 -08001085 return true;
1086 }
1087
joshualitt76e7fb62015-02-11 08:52:27 -08001088 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; }
bsalomoncdaa97b2016-03-08 08:30:14 -08001089 DIEllipseStyle style() const { return fGeoData[0].fStyle; }
joshualitt76e7fb62015-02-11 08:52:27 -08001090
bsalomoncdaa97b2016-03-08 08:30:14 -08001091 bool fUsesLocalCoords;
joshualitt76e7fb62015-02-11 08:52:27 -08001092 SkSTArray<1, Geometry, true> fGeoData;
reed1b55a962015-09-17 20:16:13 -07001093
1094 typedef GrVertexBatch INHERITED;
joshualitt76e7fb62015-02-11 08:52:27 -08001095};
1096
bsalomonabd30f52015-08-13 13:34:48 -07001097static GrDrawBatch* create_diellipse_batch(GrColor color,
1098 const SkMatrix& viewMatrix,
bsalomonabd30f52015-08-13 13:34:48 -07001099 const SkRect& ellipse,
1100 const SkStrokeRec& stroke) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001101 SkPoint center = SkPoint::Make(ellipse.centerX(), ellipse.centerY());
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +00001102 SkScalar xRadius = SkScalarHalf(ellipse.width());
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +00001103 SkScalar yRadius = SkScalarHalf(ellipse.height());
1104
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +00001105 SkStrokeRec::Style style = stroke.getStyle();
bsalomoncdaa97b2016-03-08 08:30:14 -08001106 DIEllipseStyle dieStyle = (SkStrokeRec::kStroke_Style == style) ?
1107 DIEllipseStyle::kStroke :
1108 (SkStrokeRec::kHairline_Style == style) ?
1109 DIEllipseStyle::kHairline : DIEllipseStyle::kFill;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +00001110
1111 SkScalar innerXRadius = 0;
1112 SkScalar innerYRadius = 0;
1113 if (SkStrokeRec::kFill_Style != style && SkStrokeRec::kHairline_Style != style) {
1114 SkScalar strokeWidth = stroke.getWidth();
1115
1116 if (SkScalarNearlyZero(strokeWidth)) {
1117 strokeWidth = SK_ScalarHalf;
1118 } else {
1119 strokeWidth *= SK_ScalarHalf;
1120 }
1121
1122 // we only handle thick strokes for near-circular ellipses
1123 if (strokeWidth > SK_ScalarHalf &&
1124 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
halcanary96fcdcc2015-08-27 07:41:13 -07001125 return nullptr;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +00001126 }
1127
1128 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
1129 if (strokeWidth*(yRadius*yRadius) < (strokeWidth*strokeWidth)*xRadius ||
1130 strokeWidth*(xRadius*xRadius) < (strokeWidth*strokeWidth)*yRadius) {
halcanary96fcdcc2015-08-27 07:41:13 -07001131 return nullptr;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +00001132 }
1133
1134 // set inner radius (if needed)
1135 if (SkStrokeRec::kStroke_Style == style) {
1136 innerXRadius = xRadius - strokeWidth;
1137 innerYRadius = yRadius - strokeWidth;
1138 }
1139
1140 xRadius += strokeWidth;
1141 yRadius += strokeWidth;
1142 }
bsalomoncdaa97b2016-03-08 08:30:14 -08001143 if (DIEllipseStyle::kStroke == dieStyle) {
1144 dieStyle = (innerXRadius > 0 && innerYRadius > 0) ? DIEllipseStyle ::kStroke :
1145 DIEllipseStyle ::kFill;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +00001146 }
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +00001147
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +00001148 // This expands the outer rect so that after CTM we end up with a half-pixel border
joshualitt8059eb92014-12-29 15:10:07 -08001149 SkScalar a = viewMatrix[SkMatrix::kMScaleX];
1150 SkScalar b = viewMatrix[SkMatrix::kMSkewX];
1151 SkScalar c = viewMatrix[SkMatrix::kMSkewY];
1152 SkScalar d = viewMatrix[SkMatrix::kMScaleY];
reed80ea19c2015-05-12 10:37:34 -07001153 SkScalar geoDx = SK_ScalarHalf / SkScalarSqrt(a*a + c*c);
1154 SkScalar geoDy = SK_ScalarHalf / SkScalarSqrt(b*b + d*d);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +00001155
joshualitt76e7fb62015-02-11 08:52:27 -08001156 DIEllipseBatch::Geometry geometry;
1157 geometry.fViewMatrix = viewMatrix;
1158 geometry.fColor = color;
1159 geometry.fXRadius = xRadius;
1160 geometry.fYRadius = yRadius;
1161 geometry.fInnerXRadius = innerXRadius;
1162 geometry.fInnerYRadius = innerYRadius;
1163 geometry.fGeoDx = geoDx;
1164 geometry.fGeoDy = geoDy;
bsalomoncdaa97b2016-03-08 08:30:14 -08001165 geometry.fStyle = dieStyle;
joshualittd96a67b2015-05-05 14:09:05 -07001166 geometry.fBounds = SkRect::MakeLTRB(center.fX - xRadius - geoDx, center.fY - yRadius - geoDy,
1167 center.fX + xRadius + geoDx, center.fY + yRadius + geoDy);
egdaniel9ef1bb12015-04-20 12:28:57 -07001168
joshualittd96a67b2015-05-05 14:09:05 -07001169 SkRect devBounds = geometry.fBounds;
1170 viewMatrix.mapRect(&devBounds);
1171 return DIEllipseBatch::Create(geometry, devBounds);
joshualitt3e708c52015-04-30 13:49:27 -07001172}
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +00001173
robertphillips0cc2f852016-02-24 13:36:56 -08001174GrDrawBatch* GrOvalRenderer::CreateDIEllipseBatch(GrColor color,
1175 const SkMatrix& viewMatrix,
1176 const SkRect& ellipse,
1177 const SkStrokeRec& stroke) {
1178 return create_diellipse_batch(color, viewMatrix, ellipse, stroke);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +00001179}
1180
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001181///////////////////////////////////////////////////////////////////////////////
1182
1183static const uint16_t gRRectIndices[] = {
1184 // corners
1185 0, 1, 5, 0, 5, 4,
1186 2, 3, 7, 2, 7, 6,
1187 8, 9, 13, 8, 13, 12,
1188 10, 11, 15, 10, 15, 14,
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001189
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001190 // edges
1191 1, 2, 6, 1, 6, 5,
1192 4, 5, 9, 4, 9, 8,
1193 6, 7, 11, 6, 11, 10,
1194 9, 10, 14, 9, 14, 13,
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001195
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001196 // center
1197 // we place this at the end so that we can ignore these indices when rendering stroke-only
1198 5, 6, 10, 5, 10, 9
1199};
1200
joshualitt5ead6da2014-10-22 16:00:29 -07001201static const int kIndicesPerStrokeRRect = SK_ARRAY_COUNT(gRRectIndices) - 6;
1202static const int kIndicesPerRRect = SK_ARRAY_COUNT(gRRectIndices);
1203static const int kVertsPerRRect = 16;
1204static const int kNumRRectsInIndexBuffer = 256;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001205
bsalomoned0bcad2015-05-04 10:36:42 -07001206GR_DECLARE_STATIC_UNIQUE_KEY(gStrokeRRectOnlyIndexBufferKey);
1207GR_DECLARE_STATIC_UNIQUE_KEY(gRRectOnlyIndexBufferKey);
cdalton397536c2016-03-25 12:15:03 -07001208static const GrBuffer* ref_rrect_index_buffer(bool strokeOnly,
1209 GrResourceProvider* resourceProvider) {
bsalomoned0bcad2015-05-04 10:36:42 -07001210 GR_DEFINE_STATIC_UNIQUE_KEY(gStrokeRRectOnlyIndexBufferKey);
1211 GR_DEFINE_STATIC_UNIQUE_KEY(gRRectOnlyIndexBufferKey);
1212 if (strokeOnly) {
bsalomoneae62002015-07-31 13:59:30 -07001213 return resourceProvider->findOrCreateInstancedIndexBuffer(
bsalomoned0bcad2015-05-04 10:36:42 -07001214 gRRectIndices, kIndicesPerStrokeRRect, kNumRRectsInIndexBuffer, kVertsPerRRect,
1215 gStrokeRRectOnlyIndexBufferKey);
1216 } else {
bsalomoneae62002015-07-31 13:59:30 -07001217 return resourceProvider->findOrCreateInstancedIndexBuffer(
bsalomoned0bcad2015-05-04 10:36:42 -07001218 gRRectIndices, kIndicesPerRRect, kNumRRectsInIndexBuffer, kVertsPerRRect,
1219 gRRectOnlyIndexBufferKey);
1220
1221 }
1222}
1223
joshualitt76e7fb62015-02-11 08:52:27 -08001224///////////////////////////////////////////////////////////////////////////////////////////////////
1225
bsalomonabd30f52015-08-13 13:34:48 -07001226class RRectCircleRendererBatch : public GrVertexBatch {
joshualitt76e7fb62015-02-11 08:52:27 -08001227public:
reed1b55a962015-09-17 20:16:13 -07001228 DEFINE_BATCH_CLASS_ID
1229
joshualitt76e7fb62015-02-11 08:52:27 -08001230 struct Geometry {
reed1b55a962015-09-17 20:16:13 -07001231 SkRect fDevBounds;
joshualitt76e7fb62015-02-11 08:52:27 -08001232 SkScalar fInnerRadius;
1233 SkScalar fOuterRadius;
bsalomoncdaa97b2016-03-08 08:30:14 -08001234 GrColor fColor;
joshualitt76e7fb62015-02-11 08:52:27 -08001235 };
1236
bsalomoncdaa97b2016-03-08 08:30:14 -08001237 RRectCircleRendererBatch(const Geometry& geometry, const SkMatrix& viewMatrix, bool stroked)
1238 : INHERITED(ClassID())
1239 , fStroked(stroked)
1240 , fViewMatrixIfUsingLocalCoords(viewMatrix) {
1241 fGeoData.push_back(geometry);
1242
1243 this->setBounds(geometry.fDevBounds);
joshualitt76e7fb62015-02-11 08:52:27 -08001244 }
1245
mtklein36352bf2015-03-25 18:17:31 -07001246 const char* name() const override { return "RRectCircleBatch"; }
joshualitt76e7fb62015-02-11 08:52:27 -08001247
halcanary9d524f22016-03-29 09:03:52 -07001248 void computePipelineOptimizations(GrInitInvariantOutput* color,
ethannicholasff210322015-11-24 12:10:10 -08001249 GrInitInvariantOutput* coverage,
1250 GrBatchToXPOverrides* overrides) const override {
joshualitt76e7fb62015-02-11 08:52:27 -08001251 // When this is called on a batch, there is only one geometry bundle
ethannicholasff210322015-11-24 12:10:10 -08001252 color->setKnownFourComponents(fGeoData[0].fColor);
1253 coverage->setUnknownSingleComponent();
joshualitt76e7fb62015-02-11 08:52:27 -08001254 }
1255
bsalomone46f9fe2015-08-18 06:05:14 -07001256private:
ethannicholasff210322015-11-24 12:10:10 -08001257 void initBatchTracker(const GrXPOverridesForBatch& overrides) override {
bsalomoncdaa97b2016-03-08 08:30:14 -08001258 // Handle any overrides that affect our GP.
ethannicholasff210322015-11-24 12:10:10 -08001259 overrides.getOverrideColorIfSet(&fGeoData[0].fColor);
bsalomoncdaa97b2016-03-08 08:30:14 -08001260 if (!overrides.readsLocalCoords()) {
1261 fViewMatrixIfUsingLocalCoords.reset();
1262 }
joshualitt76e7fb62015-02-11 08:52:27 -08001263 }
1264
joshualitt144c3c82015-11-30 12:30:13 -08001265 void onPrepareDraws(Target* target) const override {
bsalomoncdaa97b2016-03-08 08:30:14 -08001266 // Invert the view matrix as a local matrix (if any other processors require coords).
1267 SkMatrix localMatrix;
1268 if (!fViewMatrixIfUsingLocalCoords.invert(&localMatrix)) {
joshualitt76e7fb62015-02-11 08:52:27 -08001269 return;
1270 }
1271
1272 // Setup geometry processor
bsalomoncdaa97b2016-03-08 08:30:14 -08001273 SkAutoTUnref<GrGeometryProcessor> gp(new CircleGeometryProcessor(fStroked, localMatrix));
joshualitt76e7fb62015-02-11 08:52:27 -08001274
joshualitt76e7fb62015-02-11 08:52:27 -08001275 int instanceCount = fGeoData.count();
joshualitt76e7fb62015-02-11 08:52:27 -08001276 size_t vertexStride = gp->getVertexStride();
1277 SkASSERT(vertexStride == sizeof(CircleVertex));
1278
bsalomonb5238a72015-05-05 07:49:49 -07001279 // drop out the middle quad if we're stroked
bsalomoncdaa97b2016-03-08 08:30:14 -08001280 int indicesPerInstance = fStroked ? kIndicesPerStrokeRRect : kIndicesPerRRect;
cdalton397536c2016-03-25 12:15:03 -07001281 SkAutoTUnref<const GrBuffer> indexBuffer(
bsalomoncdaa97b2016-03-08 08:30:14 -08001282 ref_rrect_index_buffer(fStroked, target->resourceProvider()));
joshualitt76e7fb62015-02-11 08:52:27 -08001283
bsalomonb5238a72015-05-05 07:49:49 -07001284 InstancedHelper helper;
bsalomon75398562015-08-17 12:55:38 -07001285 CircleVertex* verts = reinterpret_cast<CircleVertex*>(helper.init(target,
bsalomonb5238a72015-05-05 07:49:49 -07001286 kTriangles_GrPrimitiveType, vertexStride, indexBuffer, kVertsPerRRect,
1287 indicesPerInstance, instanceCount));
1288 if (!verts || !indexBuffer) {
joshualitt4b31de82015-03-05 14:33:41 -08001289 SkDebugf("Could not allocate vertices\n");
1290 return;
1291 }
1292
joshualitt76e7fb62015-02-11 08:52:27 -08001293 for (int i = 0; i < instanceCount; i++) {
joshualitt144c3c82015-11-30 12:30:13 -08001294 const Geometry& args = fGeoData[i];
joshualitt76e7fb62015-02-11 08:52:27 -08001295
brianosmanbb2ff942016-02-11 14:15:18 -08001296 GrColor color = args.fColor;
joshualitt76e7fb62015-02-11 08:52:27 -08001297 SkScalar outerRadius = args.fOuterRadius;
1298
egdanielbc227142015-04-21 06:28:08 -07001299 const SkRect& bounds = args.fDevBounds;
joshualitt76e7fb62015-02-11 08:52:27 -08001300
1301 SkScalar yCoords[4] = {
1302 bounds.fTop,
1303 bounds.fTop + outerRadius,
1304 bounds.fBottom - outerRadius,
1305 bounds.fBottom
1306 };
1307
1308 SkScalar yOuterRadii[4] = {-1, 0, 0, 1 };
1309 // The inner radius in the vertex data must be specified in normalized space.
1310 SkScalar innerRadius = args.fInnerRadius / args.fOuterRadius;
1311 for (int i = 0; i < 4; ++i) {
1312 verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]);
brianosmanbb2ff942016-02-11 14:15:18 -08001313 verts->fColor = color;
joshualitt76e7fb62015-02-11 08:52:27 -08001314 verts->fOffset = SkPoint::Make(-1, yOuterRadii[i]);
1315 verts->fOuterRadius = outerRadius;
1316 verts->fInnerRadius = innerRadius;
1317 verts++;
1318
1319 verts->fPos = SkPoint::Make(bounds.fLeft + outerRadius, yCoords[i]);
brianosmanbb2ff942016-02-11 14:15:18 -08001320 verts->fColor = color;
joshualitt76e7fb62015-02-11 08:52:27 -08001321 verts->fOffset = SkPoint::Make(0, yOuterRadii[i]);
1322 verts->fOuterRadius = outerRadius;
1323 verts->fInnerRadius = innerRadius;
1324 verts++;
1325
1326 verts->fPos = SkPoint::Make(bounds.fRight - outerRadius, yCoords[i]);
brianosmanbb2ff942016-02-11 14:15:18 -08001327 verts->fColor = color;
joshualitt76e7fb62015-02-11 08:52:27 -08001328 verts->fOffset = SkPoint::Make(0, yOuterRadii[i]);
1329 verts->fOuterRadius = outerRadius;
1330 verts->fInnerRadius = innerRadius;
1331 verts++;
1332
1333 verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]);
brianosmanbb2ff942016-02-11 14:15:18 -08001334 verts->fColor = color;
joshualitt76e7fb62015-02-11 08:52:27 -08001335 verts->fOffset = SkPoint::Make(1, yOuterRadii[i]);
1336 verts->fOuterRadius = outerRadius;
1337 verts->fInnerRadius = innerRadius;
1338 verts++;
1339 }
1340 }
1341
bsalomon342bfc22016-04-01 06:06:20 -07001342 helper.recordDraw(target, gp);
joshualitt76e7fb62015-02-11 08:52:27 -08001343 }
1344
bsalomoncb02b382015-08-12 11:14:50 -07001345 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override {
bsalomonabd30f52015-08-13 13:34:48 -07001346 RRectCircleRendererBatch* that = t->cast<RRectCircleRendererBatch>();
1347 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
1348 that->bounds(), caps)) {
joshualitt8cab9a72015-07-16 09:13:50 -07001349 return false;
1350 }
1351
bsalomoncdaa97b2016-03-08 08:30:14 -08001352 if (fStroked != that->fStroked) {
joshualitt76e7fb62015-02-11 08:52:27 -08001353 return false;
1354 }
1355
bsalomoncdaa97b2016-03-08 08:30:14 -08001356 if (!fViewMatrixIfUsingLocalCoords.cheapEqualTo(that->fViewMatrixIfUsingLocalCoords)) {
joshualitt76e7fb62015-02-11 08:52:27 -08001357 return false;
1358 }
1359
bsalomoncdaa97b2016-03-08 08:30:14 -08001360 fGeoData.push_back_n(that->fGeoData.count(), that->fGeoData.begin());
joshualitt99c7c072015-05-01 13:43:30 -07001361 this->joinBounds(that->bounds());
joshualitt76e7fb62015-02-11 08:52:27 -08001362 return true;
1363 }
1364
bsalomoncdaa97b2016-03-08 08:30:14 -08001365 bool fStroked;
1366 SkMatrix fViewMatrixIfUsingLocalCoords;
joshualitt76e7fb62015-02-11 08:52:27 -08001367 SkSTArray<1, Geometry, true> fGeoData;
reed1b55a962015-09-17 20:16:13 -07001368
1369 typedef GrVertexBatch INHERITED;
joshualitt76e7fb62015-02-11 08:52:27 -08001370};
1371
bsalomonabd30f52015-08-13 13:34:48 -07001372class RRectEllipseRendererBatch : public GrVertexBatch {
joshualitt76e7fb62015-02-11 08:52:27 -08001373public:
reed1b55a962015-09-17 20:16:13 -07001374 DEFINE_BATCH_CLASS_ID
1375
joshualitt76e7fb62015-02-11 08:52:27 -08001376 struct Geometry {
reed1b55a962015-09-17 20:16:13 -07001377 SkRect fDevBounds;
joshualitt76e7fb62015-02-11 08:52:27 -08001378 SkScalar fXRadius;
1379 SkScalar fYRadius;
1380 SkScalar fInnerXRadius;
1381 SkScalar fInnerYRadius;
reed1b55a962015-09-17 20:16:13 -07001382 GrColor fColor;
joshualitt76e7fb62015-02-11 08:52:27 -08001383 };
1384
bsalomoncdaa97b2016-03-08 08:30:14 -08001385 RRectEllipseRendererBatch(const Geometry& geometry, const SkMatrix& viewMatrix, bool stroked)
1386 : INHERITED(ClassID())
1387 , fStroked(stroked)
1388 , fViewMatrixIfUsingLocalCoords(viewMatrix) {
1389 fGeoData.push_back(geometry);
1390 this->setBounds(geometry.fDevBounds);
joshualitt76e7fb62015-02-11 08:52:27 -08001391 }
1392
mtklein36352bf2015-03-25 18:17:31 -07001393 const char* name() const override { return "RRectEllipseRendererBatch"; }
joshualitt76e7fb62015-02-11 08:52:27 -08001394
halcanary9d524f22016-03-29 09:03:52 -07001395 void computePipelineOptimizations(GrInitInvariantOutput* color,
ethannicholasff210322015-11-24 12:10:10 -08001396 GrInitInvariantOutput* coverage,
1397 GrBatchToXPOverrides* overrides) const override {
joshualitt76e7fb62015-02-11 08:52:27 -08001398 // When this is called on a batch, there is only one geometry bundle
ethannicholasff210322015-11-24 12:10:10 -08001399 color->setKnownFourComponents(fGeoData[0].fColor);
1400 coverage->setUnknownSingleComponent();
joshualitt76e7fb62015-02-11 08:52:27 -08001401 }
1402
bsalomone46f9fe2015-08-18 06:05:14 -07001403private:
ethannicholasff210322015-11-24 12:10:10 -08001404 void initBatchTracker(const GrXPOverridesForBatch& overrides) override {
bsalomoncdaa97b2016-03-08 08:30:14 -08001405 // Handle overrides that affect our GP.
ethannicholasff210322015-11-24 12:10:10 -08001406 overrides.getOverrideColorIfSet(&fGeoData[0].fColor);
bsalomoncdaa97b2016-03-08 08:30:14 -08001407 if (!overrides.readsLocalCoords()) {
1408 fViewMatrixIfUsingLocalCoords.reset();
1409 }
joshualitt76e7fb62015-02-11 08:52:27 -08001410 }
1411
joshualitt144c3c82015-11-30 12:30:13 -08001412 void onPrepareDraws(Target* target) const override {
bsalomoncdaa97b2016-03-08 08:30:14 -08001413 SkMatrix localMatrix;
1414 if (!fViewMatrixIfUsingLocalCoords.invert(&localMatrix)) {
joshualitt76e7fb62015-02-11 08:52:27 -08001415 return;
1416 }
1417
1418 // Setup geometry processor
bsalomoncdaa97b2016-03-08 08:30:14 -08001419 SkAutoTUnref<GrGeometryProcessor> gp(new EllipseGeometryProcessor(fStroked, localMatrix));
joshualitt76e7fb62015-02-11 08:52:27 -08001420
joshualitt76e7fb62015-02-11 08:52:27 -08001421 int instanceCount = fGeoData.count();
joshualitt76e7fb62015-02-11 08:52:27 -08001422 size_t vertexStride = gp->getVertexStride();
1423 SkASSERT(vertexStride == sizeof(EllipseVertex));
1424
bsalomonb5238a72015-05-05 07:49:49 -07001425 // drop out the middle quad if we're stroked
bsalomoncdaa97b2016-03-08 08:30:14 -08001426 int indicesPerInstance = fStroked ? kIndicesPerStrokeRRect : kIndicesPerRRect;
cdalton397536c2016-03-25 12:15:03 -07001427 SkAutoTUnref<const GrBuffer> indexBuffer(
bsalomoncdaa97b2016-03-08 08:30:14 -08001428 ref_rrect_index_buffer(fStroked, target->resourceProvider()));
joshualitt76e7fb62015-02-11 08:52:27 -08001429
bsalomonb5238a72015-05-05 07:49:49 -07001430 InstancedHelper helper;
1431 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(
bsalomon75398562015-08-17 12:55:38 -07001432 helper.init(target, kTriangles_GrPrimitiveType, vertexStride, indexBuffer,
bsalomonb5238a72015-05-05 07:49:49 -07001433 kVertsPerRRect, indicesPerInstance, instanceCount));
1434 if (!verts || !indexBuffer) {
joshualitt4b31de82015-03-05 14:33:41 -08001435 SkDebugf("Could not allocate vertices\n");
1436 return;
1437 }
1438
joshualitt76e7fb62015-02-11 08:52:27 -08001439 for (int i = 0; i < instanceCount; i++) {
joshualitt144c3c82015-11-30 12:30:13 -08001440 const Geometry& args = fGeoData[i];
joshualitt76e7fb62015-02-11 08:52:27 -08001441
brianosmanbb2ff942016-02-11 14:15:18 -08001442 GrColor color = args.fColor;
1443
joshualitt76e7fb62015-02-11 08:52:27 -08001444 // Compute the reciprocals of the radii here to save time in the shader
1445 SkScalar xRadRecip = SkScalarInvert(args.fXRadius);
1446 SkScalar yRadRecip = SkScalarInvert(args.fYRadius);
1447 SkScalar xInnerRadRecip = SkScalarInvert(args.fInnerXRadius);
1448 SkScalar yInnerRadRecip = SkScalarInvert(args.fInnerYRadius);
1449
1450 // Extend the radii out half a pixel to antialias.
1451 SkScalar xOuterRadius = args.fXRadius + SK_ScalarHalf;
1452 SkScalar yOuterRadius = args.fYRadius + SK_ScalarHalf;
1453
egdanielbc227142015-04-21 06:28:08 -07001454 const SkRect& bounds = args.fDevBounds;
joshualitt76e7fb62015-02-11 08:52:27 -08001455
1456 SkScalar yCoords[4] = {
1457 bounds.fTop,
1458 bounds.fTop + yOuterRadius,
1459 bounds.fBottom - yOuterRadius,
1460 bounds.fBottom
1461 };
1462 SkScalar yOuterOffsets[4] = {
1463 yOuterRadius,
1464 SK_ScalarNearlyZero, // we're using inversesqrt() in shader, so can't be exactly 0
1465 SK_ScalarNearlyZero,
1466 yOuterRadius
1467 };
1468
1469 for (int i = 0; i < 4; ++i) {
1470 verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]);
brianosmanbb2ff942016-02-11 14:15:18 -08001471 verts->fColor = color;
joshualitt76e7fb62015-02-11 08:52:27 -08001472 verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]);
1473 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1474 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
1475 verts++;
1476
1477 verts->fPos = SkPoint::Make(bounds.fLeft + xOuterRadius, yCoords[i]);
brianosmanbb2ff942016-02-11 14:15:18 -08001478 verts->fColor = color;
joshualitt76e7fb62015-02-11 08:52:27 -08001479 verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]);
1480 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1481 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
1482 verts++;
1483
1484 verts->fPos = SkPoint::Make(bounds.fRight - xOuterRadius, yCoords[i]);
brianosmanbb2ff942016-02-11 14:15:18 -08001485 verts->fColor = color;
joshualitt76e7fb62015-02-11 08:52:27 -08001486 verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]);
1487 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1488 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
1489 verts++;
1490
1491 verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]);
brianosmanbb2ff942016-02-11 14:15:18 -08001492 verts->fColor = color;
joshualitt76e7fb62015-02-11 08:52:27 -08001493 verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]);
1494 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1495 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
1496 verts++;
1497 }
1498 }
bsalomon342bfc22016-04-01 06:06:20 -07001499 helper.recordDraw(target, gp);
joshualitt76e7fb62015-02-11 08:52:27 -08001500 }
1501
bsalomoncb02b382015-08-12 11:14:50 -07001502 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override {
bsalomonabd30f52015-08-13 13:34:48 -07001503 RRectEllipseRendererBatch* that = t->cast<RRectEllipseRendererBatch>();
1504
1505 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
1506 that->bounds(), caps)) {
joshualitt8cab9a72015-07-16 09:13:50 -07001507 return false;
1508 }
1509
bsalomoncdaa97b2016-03-08 08:30:14 -08001510 if (fStroked != that->fStroked) {
joshualitt76e7fb62015-02-11 08:52:27 -08001511 return false;
1512 }
1513
bsalomoncdaa97b2016-03-08 08:30:14 -08001514 if (!fViewMatrixIfUsingLocalCoords.cheapEqualTo(that->fViewMatrixIfUsingLocalCoords)) {
joshualitt76e7fb62015-02-11 08:52:27 -08001515 return false;
1516 }
1517
bsalomoncdaa97b2016-03-08 08:30:14 -08001518 fGeoData.push_back_n(that->fGeoData.count(), that->fGeoData.begin());
joshualitt99c7c072015-05-01 13:43:30 -07001519 this->joinBounds(that->bounds());
joshualitt76e7fb62015-02-11 08:52:27 -08001520 return true;
1521 }
1522
bsalomoncdaa97b2016-03-08 08:30:14 -08001523 bool fStroked;
1524 SkMatrix fViewMatrixIfUsingLocalCoords;
1525 SkSTArray<1, Geometry, true> fGeoData;
reed1b55a962015-09-17 20:16:13 -07001526
1527 typedef GrVertexBatch INHERITED;
joshualitt76e7fb62015-02-11 08:52:27 -08001528};
1529
bsalomonabd30f52015-08-13 13:34:48 -07001530static GrDrawBatch* create_rrect_batch(GrColor color,
1531 const SkMatrix& viewMatrix,
1532 const SkRRect& rrect,
1533 const SkStrokeRec& stroke) {
joshualitt3e708c52015-04-30 13:49:27 -07001534 SkASSERT(viewMatrix.rectStaysRect());
1535 SkASSERT(rrect.isSimple());
1536 SkASSERT(!rrect.isOval());
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001537
joshualitt3e708c52015-04-30 13:49:27 -07001538 // RRect batchs only handle simple, but not too simple, rrects
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001539 // do any matrix crunching before we reset the draw state for device coords
1540 const SkRect& rrectBounds = rrect.getBounds();
joshualittd96a67b2015-05-05 14:09:05 -07001541 SkRect bounds;
1542 viewMatrix.mapRect(&bounds, rrectBounds);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001543
1544 SkVector radii = rrect.getSimpleRadii();
joshualitt8059eb92014-12-29 15:10:07 -08001545 SkScalar xRadius = SkScalarAbs(viewMatrix[SkMatrix::kMScaleX]*radii.fX +
1546 viewMatrix[SkMatrix::kMSkewY]*radii.fY);
1547 SkScalar yRadius = SkScalarAbs(viewMatrix[SkMatrix::kMSkewX]*radii.fX +
1548 viewMatrix[SkMatrix::kMScaleY]*radii.fY);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001549
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001550 SkStrokeRec::Style style = stroke.getStyle();
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001551
1552 // do (potentially) anisotropic mapping of stroke
1553 SkVector scaledStroke;
1554 SkScalar strokeWidth = stroke.getWidth();
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001555
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001556 bool isStrokeOnly = SkStrokeRec::kStroke_Style == style ||
1557 SkStrokeRec::kHairline_Style == style;
1558 bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == style;
1559
1560 if (hasStroke) {
1561 if (SkStrokeRec::kHairline_Style == style) {
1562 scaledStroke.set(1, 1);
1563 } else {
joshualitt8059eb92014-12-29 15:10:07 -08001564 scaledStroke.fX = SkScalarAbs(strokeWidth*(viewMatrix[SkMatrix::kMScaleX] +
1565 viewMatrix[SkMatrix::kMSkewY]));
1566 scaledStroke.fY = SkScalarAbs(strokeWidth*(viewMatrix[SkMatrix::kMSkewX] +
1567 viewMatrix[SkMatrix::kMScaleY]));
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001568 }
1569
1570 // if half of strokewidth is greater than radius, we don't handle that right now
1571 if (SK_ScalarHalf*scaledStroke.fX > xRadius || SK_ScalarHalf*scaledStroke.fY > yRadius) {
halcanary96fcdcc2015-08-27 07:41:13 -07001572 return nullptr;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001573 }
1574 }
1575
1576 // The way the effect interpolates the offset-to-ellipse/circle-center attribute only works on
1577 // the interior of the rrect if the radii are >= 0.5. Otherwise, the inner rect of the nine-
1578 // patch will have fractional coverage. This only matters when the interior is actually filled.
1579 // We could consider falling back to rect rendering here, since a tiny radius is
1580 // indistinguishable from a square corner.
1581 if (!isStrokeOnly && (SK_ScalarHalf > xRadius || SK_ScalarHalf > yRadius)) {
halcanary96fcdcc2015-08-27 07:41:13 -07001582 return nullptr;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001583 }
1584
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001585 // if the corners are circles, use the circle renderer
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001586 if ((!hasStroke || scaledStroke.fX == scaledStroke.fY) && xRadius == yRadius) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001587 SkScalar innerRadius = 0.0f;
1588 SkScalar outerRadius = xRadius;
1589 SkScalar halfWidth = 0;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001590 if (hasStroke) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001591 if (SkScalarNearlyZero(scaledStroke.fX)) {
1592 halfWidth = SK_ScalarHalf;
1593 } else {
1594 halfWidth = SkScalarHalf(scaledStroke.fX);
1595 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001596
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001597 if (isStrokeOnly) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001598 innerRadius = xRadius - halfWidth;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001599 }
1600 outerRadius += halfWidth;
joshualittd96a67b2015-05-05 14:09:05 -07001601 bounds.outset(halfWidth, halfWidth);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001602 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001603
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001604 isStrokeOnly = (isStrokeOnly && innerRadius >= 0);
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +00001605
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001606 // The radii are outset for two reasons. First, it allows the shader to simply perform
bsalomonce1c8862014-12-15 07:11:22 -08001607 // simpler computation because the computed alpha is zero, rather than 50%, at the radius.
1608 // Second, the outer radius is used to compute the verts of the bounding box that is
1609 // rendered and the outset ensures the box will cover all partially covered by the rrect
1610 // corners.
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001611 outerRadius += SK_ScalarHalf;
1612 innerRadius -= SK_ScalarHalf;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001613
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001614 // Expand the rect so all the pixels will be captured.
joshualittd96a67b2015-05-05 14:09:05 -07001615 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001616
joshualitt76e7fb62015-02-11 08:52:27 -08001617 RRectCircleRendererBatch::Geometry geometry;
joshualitt76e7fb62015-02-11 08:52:27 -08001618 geometry.fColor = color;
1619 geometry.fInnerRadius = innerRadius;
1620 geometry.fOuterRadius = outerRadius;
joshualittd96a67b2015-05-05 14:09:05 -07001621 geometry.fDevBounds = bounds;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001622
bsalomoncdaa97b2016-03-08 08:30:14 -08001623 return new RRectCircleRendererBatch(geometry, viewMatrix, isStrokeOnly);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001624 // otherwise we use the ellipse renderer
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001625 } else {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001626 SkScalar innerXRadius = 0.0f;
1627 SkScalar innerYRadius = 0.0f;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001628 if (hasStroke) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001629 if (SkScalarNearlyZero(scaledStroke.length())) {
1630 scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf);
1631 } else {
1632 scaledStroke.scale(SK_ScalarHalf);
1633 }
1634
1635 // we only handle thick strokes for near-circular ellipses
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +00001636 if (scaledStroke.length() > SK_ScalarHalf &&
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001637 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
halcanary96fcdcc2015-08-27 07:41:13 -07001638 return nullptr;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001639 }
1640
1641 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
1642 if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius ||
1643 scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) {
halcanary96fcdcc2015-08-27 07:41:13 -07001644 return nullptr;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001645 }
1646
1647 // this is legit only if scale & translation (which should be the case at the moment)
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001648 if (isStrokeOnly) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001649 innerXRadius = xRadius - scaledStroke.fX;
1650 innerYRadius = yRadius - scaledStroke.fY;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001651 }
1652
1653 xRadius += scaledStroke.fX;
1654 yRadius += scaledStroke.fY;
joshualittd96a67b2015-05-05 14:09:05 -07001655 bounds.outset(scaledStroke.fX, scaledStroke.fY);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001656 }
jvanverth@google.come3647412013-05-08 15:31:43 +00001657
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001658 isStrokeOnly = (isStrokeOnly && innerXRadius >= 0 && innerYRadius >= 0);
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +00001659
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001660 // Expand the rect so all the pixels will be captured.
joshualittd96a67b2015-05-05 14:09:05 -07001661 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001662
joshualitt76e7fb62015-02-11 08:52:27 -08001663 RRectEllipseRendererBatch::Geometry geometry;
joshualitt76e7fb62015-02-11 08:52:27 -08001664 geometry.fColor = color;
1665 geometry.fXRadius = xRadius;
1666 geometry.fYRadius = yRadius;
1667 geometry.fInnerXRadius = innerXRadius;
1668 geometry.fInnerYRadius = innerYRadius;
joshualittd96a67b2015-05-05 14:09:05 -07001669 geometry.fDevBounds = bounds;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001670
bsalomoncdaa97b2016-03-08 08:30:14 -08001671 return new RRectEllipseRendererBatch(geometry, viewMatrix, isStrokeOnly);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001672 }
joshualitt3e708c52015-04-30 13:49:27 -07001673}
1674
robertphillipsb56f9272016-02-25 11:03:52 -08001675GrDrawBatch* GrOvalRenderer::CreateRRectBatch(GrColor color,
robertphillips0cc2f852016-02-24 13:36:56 -08001676 const SkMatrix& viewMatrix,
robertphillips0cc2f852016-02-24 13:36:56 -08001677 const SkRRect& rrect,
1678 const SkStrokeRec& stroke,
1679 GrShaderCaps* shaderCaps) {
robertphillips0cc2f852016-02-24 13:36:56 -08001680 if (rrect.isOval()) {
robertphillipsb56f9272016-02-25 11:03:52 -08001681 return CreateOvalBatch(color, viewMatrix, rrect.getBounds(), stroke, shaderCaps);
joshualitt3e708c52015-04-30 13:49:27 -07001682 }
1683
1684 if (!viewMatrix.rectStaysRect() || !rrect.isSimple()) {
robertphillips0cc2f852016-02-24 13:36:56 -08001685 return nullptr;
joshualitt3e708c52015-04-30 13:49:27 -07001686 }
1687
robertphillips0cc2f852016-02-24 13:36:56 -08001688 return create_rrect_batch(color, viewMatrix, rrect, stroke);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001689}
joshualitt3e708c52015-04-30 13:49:27 -07001690
1691///////////////////////////////////////////////////////////////////////////////////////////////////
1692
1693#ifdef GR_TEST_UTILS
1694
bsalomonabd30f52015-08-13 13:34:48 -07001695DRAW_BATCH_TEST_DEFINE(CircleBatch) {
joshualitt3e708c52015-04-30 13:49:27 -07001696 SkMatrix viewMatrix = GrTest::TestMatrix(random);
1697 GrColor color = GrRandomColor(random);
joshualitt6c891102015-05-13 08:51:49 -07001698 SkRect circle = GrTest::TestSquare(random);
robertphillips0cc2f852016-02-24 13:36:56 -08001699 return create_circle_batch(color, viewMatrix, circle, GrTest::TestStrokeRec(random));
joshualitt3e708c52015-04-30 13:49:27 -07001700}
1701
bsalomonabd30f52015-08-13 13:34:48 -07001702DRAW_BATCH_TEST_DEFINE(EllipseBatch) {
joshualitt3e708c52015-04-30 13:49:27 -07001703 SkMatrix viewMatrix = GrTest::TestMatrixRectStaysRect(random);
1704 GrColor color = GrRandomColor(random);
joshualitt6c891102015-05-13 08:51:49 -07001705 SkRect ellipse = GrTest::TestSquare(random);
robertphillips0cc2f852016-02-24 13:36:56 -08001706 return create_ellipse_batch(color, viewMatrix, ellipse, GrTest::TestStrokeRec(random));
joshualitt3e708c52015-04-30 13:49:27 -07001707}
1708
bsalomonabd30f52015-08-13 13:34:48 -07001709DRAW_BATCH_TEST_DEFINE(DIEllipseBatch) {
joshualitt3e708c52015-04-30 13:49:27 -07001710 SkMatrix viewMatrix = GrTest::TestMatrix(random);
1711 GrColor color = GrRandomColor(random);
joshualitt6c891102015-05-13 08:51:49 -07001712 SkRect ellipse = GrTest::TestSquare(random);
robertphillips0cc2f852016-02-24 13:36:56 -08001713 return create_diellipse_batch(color, viewMatrix, ellipse, GrTest::TestStrokeRec(random));
joshualitt3e708c52015-04-30 13:49:27 -07001714}
1715
bsalomonabd30f52015-08-13 13:34:48 -07001716DRAW_BATCH_TEST_DEFINE(RRectBatch) {
joshualitt3e708c52015-04-30 13:49:27 -07001717 SkMatrix viewMatrix = GrTest::TestMatrixRectStaysRect(random);
1718 GrColor color = GrRandomColor(random);
1719 const SkRRect& rrect = GrTest::TestRRectSimple(random);
joshualitt21279c72015-05-11 07:21:37 -07001720 return create_rrect_batch(color, viewMatrix, rrect, GrTest::TestStrokeRec(random));
joshualitt3e708c52015-04-30 13:49:27 -07001721}
1722
1723#endif