blob: d337bbb3657546eedd72e5aecc0342d3bb732621 [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
joshualitt76e7fb62015-02-11 08:52:27 -080010#include "GrBatch.h"
11#include "GrBatchTarget.h"
joshualitt3e708c52015-04-30 13:49:27 -070012#include "GrBatchTest.h"
commit-bot@chromium.org81312832013-03-22 18:34:09 +000013#include "GrDrawTarget.h"
joshualitteb2a6762014-12-04 11:35:33 -080014#include "GrGeometryProcessor.h"
egdaniel605dd0f2014-11-12 08:35:25 -080015#include "GrInvariantOutput.h"
egdaniel8dd688b2015-01-22 10:16:09 -080016#include "GrPipelineBuilder.h"
joshualitt76e7fb62015-02-11 08:52:27 -080017#include "GrProcessor.h"
bsalomoned0bcad2015-05-04 10:36:42 -070018#include "GrResourceProvider.h"
bsalomon72e3ae42015-04-28 08:08:46 -070019#include "GrVertexBuffer.h"
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +000020#include "SkRRect.h"
commit-bot@chromium.org81312832013-03-22 18:34:09 +000021#include "SkStrokeRec.h"
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +000022#include "SkTLazy.h"
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +000023#include "effects/GrRRectEffect.h"
joshualitteb2a6762014-12-04 11:35:33 -080024#include "gl/GrGLProcessor.h"
joshualitteb2a6762014-12-04 11:35:33 -080025#include "gl/GrGLGeometryProcessor.h"
26#include "gl/builders/GrGLProgramBuilder.h"
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +000027
joshualitt76e7fb62015-02-11 08:52:27 -080028// TODO(joshualitt) - Break this file up during GrBatch post implementation cleanup
29
commit-bot@chromium.org81312832013-03-22 18:34:09 +000030namespace {
joshualitt5ead6da2014-10-22 16:00:29 -070031// TODO(joshualitt) add per vertex colors
commit-bot@chromium.org81312832013-03-22 18:34:09 +000032struct CircleVertex {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000033 SkPoint fPos;
34 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;
41 SkPoint fOffset;
42 SkPoint fOuterRadii;
43 SkPoint fInnerRadii;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +000044};
45
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +000046struct DIEllipseVertex {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000047 SkPoint fPos;
48 SkPoint fOuterOffset;
49 SkPoint fInnerOffset;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +000050};
51
commit-bot@chromium.org81312832013-03-22 18:34:09 +000052inline bool circle_stays_circle(const SkMatrix& m) {
53 return m.isSimilarity();
54}
55
56}
57
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000058///////////////////////////////////////////////////////////////////////////////
59
60/**
bsalomonce1c8862014-12-15 07:11:22 -080061 * The output of this effect is a modulation of the input color and coverage for a circle. It
62 * operates in a space normalized by the circle radius (outer radius in the case of a stroke)
63 * with origin at the circle center. Two vertex attributes are used:
64 * vec2f : position in device space of the bounding geometry vertices
65 * vec4f : (p.xy, outerRad, innerRad)
66 * p is the position in the normalized space.
67 * outerRad is the outerRadius in device space.
68 * innerRad is the innerRadius in normalized space (ignored if not stroking).
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000069 */
70
joshualitt249af152014-09-15 11:41:13 -070071class CircleEdgeEffect : public GrGeometryProcessor {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000072public:
joshualittb8c241a2015-05-19 08:23:30 -070073 static GrGeometryProcessor* Create(GrColor color, bool stroke, const SkMatrix& localMatrix,
74 bool usesLocalCoords) {
75 return SkNEW_ARGS(CircleEdgeEffect, (color, stroke, localMatrix, usesLocalCoords));
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000076 }
77
joshualitt71c92602015-01-14 08:12:47 -080078 const Attribute* inPosition() const { return fInPosition; }
79 const Attribute* inCircleEdge() const { return fInCircleEdge; }
joshualitt88c23fc2015-05-13 14:18:07 -070080 GrColor color() const { return fColor; }
joshualittb8c241a2015-05-19 08:23:30 -070081 bool colorIgnored() const { return GrColor_ILLEGAL == fColor; }
joshualitte3ababe2015-05-15 07:56:07 -070082 const SkMatrix& localMatrix() const { return fLocalMatrix; }
joshualittb8c241a2015-05-19 08:23:30 -070083 bool usesLocalCoords() const { return fUsesLocalCoords; }
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000084 virtual ~CircleEdgeEffect() {}
85
mtklein36352bf2015-03-25 18:17:31 -070086 const char* name() const override { return "CircleEdge"; }
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000087
88 inline bool isStroked() const { return fStroke; }
89
joshualittb0a8a372014-09-23 09:50:21 -070090 class GLProcessor : public GrGLGeometryProcessor {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000091 public:
joshualitteb2a6762014-12-04 11:35:33 -080092 GLProcessor(const GrGeometryProcessor&,
joshualitt9b989322014-12-15 14:16:27 -080093 const GrBatchTracker&)
94 : fColor(GrColor_ILLEGAL) {}
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000095
mtklein36352bf2015-03-25 18:17:31 -070096 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{
joshualitt2dd1ae02014-12-03 06:24:10 -080097 const CircleEdgeEffect& ce = args.fGP.cast<CircleEdgeEffect>();
joshualitt9b989322014-12-15 14:16:27 -080098 GrGLGPBuilder* pb = args.fPB;
joshualitt2dd1ae02014-12-03 06:24:10 -080099 GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder();
100
joshualittabb52a12015-01-13 15:02:10 -0800101 // emit attributes
102 vsBuilder->emitAttributes(ce);
103
joshualitt74077b92014-10-24 11:26:03 -0700104 GrGLVertToFrag v(kVec4f_GrSLType);
105 args.fPB->addVarying("CircleEdge", &v);
joshualitt2dd1ae02014-12-03 06:24:10 -0800106 vsBuilder->codeAppendf("%s = %s;", v.vsOut(), ce.inCircleEdge()->fName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000107
joshualittb8c241a2015-05-19 08:23:30 -0700108 // setup pass through color
109 if (!ce.colorIgnored()) {
110 this->setupUniformColor(pb, args.fOutputColor, &fColorUniform);
111 }
joshualitt9b989322014-12-15 14:16:27 -0800112
joshualittabb52a12015-01-13 15:02:10 -0800113 // Setup position
joshualitte578a952015-05-14 10:09:13 -0700114 this->setupPosition(pb, gpArgs, ce.inPosition()->fName);
joshualittabb52a12015-01-13 15:02:10 -0800115
116 // emit transforms
robertphillips46d36f02015-01-18 08:14:14 -0800117 this->emitTransforms(args.fPB, gpArgs->fPositionVar, ce.inPosition()->fName,
tfarina567ff2f2015-04-27 07:01:44 -0700118 ce.localMatrix(), args.fTransformsIn, args.fTransformsOut);
joshualitt4973d9d2014-11-08 09:24:25 -0800119
egdaniel29bee0f2015-04-29 11:54:42 -0700120 GrGLFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder();
joshualitt74077b92014-10-24 11:26:03 -0700121 fsBuilder->codeAppendf("float d = length(%s.xy);", v.fsIn());
bsalomonce1c8862014-12-15 07:11:22 -0800122 fsBuilder->codeAppendf("float edgeAlpha = clamp(%s.z * (1.0 - d), 0.0, 1.0);", v.fsIn());
joshualitt2dd1ae02014-12-03 06:24:10 -0800123 if (ce.isStroked()) {
bsalomonce1c8862014-12-15 07:11:22 -0800124 fsBuilder->codeAppendf("float innerAlpha = clamp(%s.z * (d - %s.w), 0.0, 1.0);",
125 v.fsIn(), v.fsIn());
joshualitt74077b92014-10-24 11:26:03 -0700126 fsBuilder->codeAppend("edgeAlpha *= innerAlpha;");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000127 }
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000128
joshualitt2dd1ae02014-12-03 06:24:10 -0800129 fsBuilder->codeAppendf("%s = vec4(edgeAlpha);", args.fOutputCoverage);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000130 }
131
robertphillips46d36f02015-01-18 08:14:14 -0800132 static void GenKey(const GrGeometryProcessor& gp,
joshualitt9b989322014-12-15 14:16:27 -0800133 const GrBatchTracker& bt,
jvanverthcfc18862015-04-28 08:48:20 -0700134 const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700135 GrProcessorKeyBuilder* b) {
joshualitte3ababe2015-05-15 07:56:07 -0700136 const CircleEdgeEffect& ce = gp.cast<CircleEdgeEffect>();
137 uint16_t key = ce.isStroked() ? 0x1 : 0x0;
joshualittb8c241a2015-05-19 08:23:30 -0700138 key |= ce.usesLocalCoords() && ce.localMatrix().hasPerspective() ? 0x2 : 0x0;
139 key |= ce.colorIgnored() ? 0x4 : 0x0;
140 b->add32(key);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000141 }
142
joshualitt9b989322014-12-15 14:16:27 -0800143 virtual void setData(const GrGLProgramDataManager& pdman,
144 const GrPrimitiveProcessor& gp,
mtklein36352bf2015-03-25 18:17:31 -0700145 const GrBatchTracker& bt) override {
joshualittb8c241a2015-05-19 08:23:30 -0700146 const CircleEdgeEffect& ce = gp.cast<CircleEdgeEffect>();
147 if (ce.color() != fColor) {
joshualitt9b989322014-12-15 14:16:27 -0800148 GrGLfloat c[4];
joshualittb8c241a2015-05-19 08:23:30 -0700149 GrColorToRGBAFloat(ce.color(), c);
joshualitt9b989322014-12-15 14:16:27 -0800150 pdman.set4fv(fColorUniform, 1, c);
joshualittb8c241a2015-05-19 08:23:30 -0700151 fColor = ce.color();
joshualitt9b989322014-12-15 14:16:27 -0800152 }
153 }
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000154
joshualitte3ababe2015-05-15 07:56:07 -0700155 void setTransformData(const GrPrimitiveProcessor& primProc,
156 const GrGLProgramDataManager& pdman,
157 int index,
158 const SkTArray<const GrCoordTransform*, true>& transforms) override {
159 this->setTransformDataHelper<CircleEdgeEffect>(primProc, pdman, index, transforms);
160 }
161
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000162 private:
joshualitt9b989322014-12-15 14:16:27 -0800163 GrColor fColor;
164 UniformHandle fColorUniform;
joshualitt249af152014-09-15 11:41:13 -0700165 typedef GrGLGeometryProcessor INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000166 };
167
joshualitteb2a6762014-12-04 11:35:33 -0800168 virtual void getGLProcessorKey(const GrBatchTracker& bt,
jvanverthcfc18862015-04-28 08:48:20 -0700169 const GrGLSLCaps& caps,
mtklein36352bf2015-03-25 18:17:31 -0700170 GrProcessorKeyBuilder* b) const override {
joshualitteb2a6762014-12-04 11:35:33 -0800171 GLProcessor::GenKey(*this, bt, caps, b);
172 }
173
joshualittabb52a12015-01-13 15:02:10 -0800174 virtual GrGLPrimitiveProcessor* createGLInstance(const GrBatchTracker& bt,
jvanverthcfc18862015-04-28 08:48:20 -0700175 const GrGLSLCaps&) const override {
joshualitteb2a6762014-12-04 11:35:33 -0800176 return SkNEW_ARGS(GLProcessor, (*this, bt));
177 }
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000178
179private:
joshualittb8c241a2015-05-19 08:23:30 -0700180 CircleEdgeEffect(GrColor color, bool stroke, const SkMatrix& localMatrix, bool usesLocalCoords)
joshualitte3ababe2015-05-15 07:56:07 -0700181 : fColor(color)
joshualittb8c241a2015-05-19 08:23:30 -0700182 , fLocalMatrix(localMatrix)
183 , fUsesLocalCoords(usesLocalCoords) {
joshualitteb2a6762014-12-04 11:35:33 -0800184 this->initClassID<CircleEdgeEffect>();
senorblancof2539d52015-05-20 14:03:42 -0700185 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType,
186 kHigh_GrSLPrecision));
joshualitt71c92602015-01-14 08:12:47 -0800187 fInCircleEdge = &this->addVertexAttrib(Attribute("inCircleEdge",
joshualitt2dd1ae02014-12-03 06:24:10 -0800188 kVec4f_GrVertexAttribType));
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000189 fStroke = stroke;
190 }
191
joshualitt88c23fc2015-05-13 14:18:07 -0700192 GrColor fColor;
joshualitte3ababe2015-05-15 07:56:07 -0700193 SkMatrix fLocalMatrix;
joshualitt71c92602015-01-14 08:12:47 -0800194 const Attribute* fInPosition;
195 const Attribute* fInCircleEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000196 bool fStroke;
joshualittb8c241a2015-05-19 08:23:30 -0700197 bool fUsesLocalCoords;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000198
joshualittb0a8a372014-09-23 09:50:21 -0700199 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000200
joshualitt249af152014-09-15 11:41:13 -0700201 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000202};
203
joshualittb0a8a372014-09-23 09:50:21 -0700204GR_DEFINE_GEOMETRY_PROCESSOR_TEST(CircleEdgeEffect);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000205
joshualitt0067ff52015-07-08 14:26:19 -0700206GrGeometryProcessor* CircleEdgeEffect::TestCreate(GrProcessorTestData* d) {
207 return CircleEdgeEffect::Create(GrRandomColor(d->fRandom),
208 d->fRandom->nextBool(),
209 GrTest::TestMatrix(d->fRandom),
210 d->fRandom->nextBool());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000211}
212
213///////////////////////////////////////////////////////////////////////////////
214
215/**
216 * 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 +0000217 * ellipse, specified as a 2D offset from center, and the reciprocals of the outer and inner radii,
218 * in both x and y directions.
219 *
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000220 * 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 +0000221 */
222
joshualitt249af152014-09-15 11:41:13 -0700223class EllipseEdgeEffect : public GrGeometryProcessor {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000224public:
joshualittb8c241a2015-05-19 08:23:30 -0700225 static GrGeometryProcessor* Create(GrColor color, bool stroke, const SkMatrix& localMatrix,
226 bool usesLocalCoords) {
227 return SkNEW_ARGS(EllipseEdgeEffect, (color, stroke, localMatrix, usesLocalCoords));
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000228 }
229
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000230 virtual ~EllipseEdgeEffect() {}
231
mtklein36352bf2015-03-25 18:17:31 -0700232 const char* name() const override { return "EllipseEdge"; }
joshualitt2dd1ae02014-12-03 06:24:10 -0800233
joshualitt71c92602015-01-14 08:12:47 -0800234 const Attribute* inPosition() const { return fInPosition; }
235 const Attribute* inEllipseOffset() const { return fInEllipseOffset; }
236 const Attribute* inEllipseRadii() const { return fInEllipseRadii; }
joshualitt88c23fc2015-05-13 14:18:07 -0700237 GrColor color() const { return fColor; }
joshualittb8c241a2015-05-19 08:23:30 -0700238 bool colorIgnored() const { return GrColor_ILLEGAL == fColor; }
joshualitte3ababe2015-05-15 07:56:07 -0700239 const SkMatrix& localMatrix() const { return fLocalMatrix; }
joshualittb8c241a2015-05-19 08:23:30 -0700240 bool usesLocalCoords() const { return fUsesLocalCoords; }
joshualitt249af152014-09-15 11:41:13 -0700241
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000242 inline bool isStroked() const { return fStroke; }
243
joshualittb0a8a372014-09-23 09:50:21 -0700244 class GLProcessor : public GrGLGeometryProcessor {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000245 public:
joshualitteb2a6762014-12-04 11:35:33 -0800246 GLProcessor(const GrGeometryProcessor&,
joshualitt9b989322014-12-15 14:16:27 -0800247 const GrBatchTracker&)
248 : fColor(GrColor_ILLEGAL) {}
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000249
mtklein36352bf2015-03-25 18:17:31 -0700250 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{
joshualitt2dd1ae02014-12-03 06:24:10 -0800251 const EllipseEdgeEffect& ee = args.fGP.cast<EllipseEdgeEffect>();
joshualitt9b989322014-12-15 14:16:27 -0800252 GrGLGPBuilder* pb = args.fPB;
joshualitt2dd1ae02014-12-03 06:24:10 -0800253 GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder();
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000254
joshualittabb52a12015-01-13 15:02:10 -0800255 // emit attributes
256 vsBuilder->emitAttributes(ee);
257
joshualitt74077b92014-10-24 11:26:03 -0700258 GrGLVertToFrag ellipseOffsets(kVec2f_GrSLType);
259 args.fPB->addVarying("EllipseOffsets", &ellipseOffsets);
joshualitt74077b92014-10-24 11:26:03 -0700260 vsBuilder->codeAppendf("%s = %s;", ellipseOffsets.vsOut(),
joshualitt2dd1ae02014-12-03 06:24:10 -0800261 ee.inEllipseOffset()->fName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000262
joshualitt74077b92014-10-24 11:26:03 -0700263 GrGLVertToFrag ellipseRadii(kVec4f_GrSLType);
264 args.fPB->addVarying("EllipseRadii", &ellipseRadii);
265 vsBuilder->codeAppendf("%s = %s;", ellipseRadii.vsOut(),
joshualitt2dd1ae02014-12-03 06:24:10 -0800266 ee.inEllipseRadii()->fName);
267
joshualittb8c241a2015-05-19 08:23:30 -0700268 // setup pass through color
269 if (!ee.colorIgnored()) {
270 this->setupUniformColor(pb, args.fOutputColor, &fColorUniform);
271 }
joshualitt9b989322014-12-15 14:16:27 -0800272
joshualittabb52a12015-01-13 15:02:10 -0800273 // Setup position
joshualitte578a952015-05-14 10:09:13 -0700274 this->setupPosition(pb, gpArgs, ee.inPosition()->fName);
joshualittabb52a12015-01-13 15:02:10 -0800275
276 // emit transforms
robertphillips46d36f02015-01-18 08:14:14 -0800277 this->emitTransforms(args.fPB, gpArgs->fPositionVar, ee.inPosition()->fName,
joshualittabb52a12015-01-13 15:02:10 -0800278 ee.localMatrix(), args.fTransformsIn, args.fTransformsOut);
joshualitt4973d9d2014-11-08 09:24:25 -0800279
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +0000280 // for outer curve
egdaniel29bee0f2015-04-29 11:54:42 -0700281 GrGLFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder();
joshualitt74077b92014-10-24 11:26:03 -0700282 fsBuilder->codeAppendf("vec2 scaledOffset = %s*%s.xy;", ellipseOffsets.fsIn(),
283 ellipseRadii.fsIn());
284 fsBuilder->codeAppend("float test = dot(scaledOffset, scaledOffset) - 1.0;");
285 fsBuilder->codeAppendf("vec2 grad = 2.0*scaledOffset*%s.xy;", ellipseRadii.fsIn());
286 fsBuilder->codeAppend("float grad_dot = dot(grad, grad);");
287
commit-bot@chromium.org1b035d82014-04-09 17:11:09 +0000288 // avoid calling inversesqrt on zero.
joshualitt74077b92014-10-24 11:26:03 -0700289 fsBuilder->codeAppend("grad_dot = max(grad_dot, 1.0e-4);");
290 fsBuilder->codeAppend("float invlen = inversesqrt(grad_dot);");
291 fsBuilder->codeAppend("float edgeAlpha = clamp(0.5-test*invlen, 0.0, 1.0);");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000292
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000293 // for inner curve
joshualitt2dd1ae02014-12-03 06:24:10 -0800294 if (ee.isStroked()) {
joshualitt74077b92014-10-24 11:26:03 -0700295 fsBuilder->codeAppendf("scaledOffset = %s*%s.zw;",
296 ellipseOffsets.fsIn(), ellipseRadii.fsIn());
297 fsBuilder->codeAppend("test = dot(scaledOffset, scaledOffset) - 1.0;");
298 fsBuilder->codeAppendf("grad = 2.0*scaledOffset*%s.zw;",
299 ellipseRadii.fsIn());
300 fsBuilder->codeAppend("invlen = inversesqrt(dot(grad, grad));");
301 fsBuilder->codeAppend("edgeAlpha *= clamp(0.5+test*invlen, 0.0, 1.0);");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000302 }
303
joshualitt2dd1ae02014-12-03 06:24:10 -0800304 fsBuilder->codeAppendf("%s = vec4(edgeAlpha);", args.fOutputCoverage);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000305 }
306
robertphillips46d36f02015-01-18 08:14:14 -0800307 static void GenKey(const GrGeometryProcessor& gp,
joshualitt9b989322014-12-15 14:16:27 -0800308 const GrBatchTracker& bt,
jvanverthcfc18862015-04-28 08:48:20 -0700309 const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700310 GrProcessorKeyBuilder* b) {
joshualitte3ababe2015-05-15 07:56:07 -0700311 const EllipseEdgeEffect& ee = gp.cast<EllipseEdgeEffect>();
312 uint16_t key = ee.isStroked() ? 0x1 : 0x0;
joshualittb8c241a2015-05-19 08:23:30 -0700313 key |= ee.usesLocalCoords() && ee.localMatrix().hasPerspective() ? 0x2 : 0x0;
314 key |= ee.colorIgnored() ? 0x4 : 0x0;
315 b->add32(key);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000316 }
317
joshualitt9b989322014-12-15 14:16:27 -0800318 virtual void setData(const GrGLProgramDataManager& pdman,
319 const GrPrimitiveProcessor& gp,
mtklein36352bf2015-03-25 18:17:31 -0700320 const GrBatchTracker& bt) override {
joshualittb8c241a2015-05-19 08:23:30 -0700321 const EllipseEdgeEffect& ee = gp.cast<EllipseEdgeEffect>();
322 if (ee.color() != fColor) {
joshualitt9b989322014-12-15 14:16:27 -0800323 GrGLfloat c[4];
joshualittb8c241a2015-05-19 08:23:30 -0700324 GrColorToRGBAFloat(ee.color(), c);
joshualitt9b989322014-12-15 14:16:27 -0800325 pdman.set4fv(fColorUniform, 1, c);
joshualittb8c241a2015-05-19 08:23:30 -0700326 fColor = ee.color();
joshualitt9b989322014-12-15 14:16:27 -0800327 }
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000328 }
329
joshualitte3ababe2015-05-15 07:56:07 -0700330 void setTransformData(const GrPrimitiveProcessor& primProc,
331 const GrGLProgramDataManager& pdman,
332 int index,
333 const SkTArray<const GrCoordTransform*, true>& transforms) override {
334 this->setTransformDataHelper<EllipseEdgeEffect>(primProc, pdman, index, transforms);
335 }
336
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000337 private:
joshualitt9b989322014-12-15 14:16:27 -0800338 GrColor fColor;
339 UniformHandle fColorUniform;
340
joshualitt249af152014-09-15 11:41:13 -0700341 typedef GrGLGeometryProcessor INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000342 };
343
joshualitteb2a6762014-12-04 11:35:33 -0800344 virtual void getGLProcessorKey(const GrBatchTracker& bt,
jvanverthcfc18862015-04-28 08:48:20 -0700345 const GrGLSLCaps& caps,
mtklein36352bf2015-03-25 18:17:31 -0700346 GrProcessorKeyBuilder* b) const override {
joshualitteb2a6762014-12-04 11:35:33 -0800347 GLProcessor::GenKey(*this, bt, caps, b);
348 }
349
joshualittabb52a12015-01-13 15:02:10 -0800350 virtual GrGLPrimitiveProcessor* createGLInstance(const GrBatchTracker& bt,
jvanverthcfc18862015-04-28 08:48:20 -0700351 const GrGLSLCaps&) const override {
joshualitteb2a6762014-12-04 11:35:33 -0800352 return SkNEW_ARGS(GLProcessor, (*this, bt));
353 }
354
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000355private:
joshualittb8c241a2015-05-19 08:23:30 -0700356 EllipseEdgeEffect(GrColor color, bool stroke, const SkMatrix& localMatrix,
357 bool usesLocalCoords)
joshualitte3ababe2015-05-15 07:56:07 -0700358 : fColor(color)
joshualittb8c241a2015-05-19 08:23:30 -0700359 , fLocalMatrix(localMatrix)
360 , fUsesLocalCoords(usesLocalCoords) {
joshualitteb2a6762014-12-04 11:35:33 -0800361 this->initClassID<EllipseEdgeEffect>();
joshualitt71c92602015-01-14 08:12:47 -0800362 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType));
363 fInEllipseOffset = &this->addVertexAttrib(Attribute("inEllipseOffset",
joshualitt2dd1ae02014-12-03 06:24:10 -0800364 kVec2f_GrVertexAttribType));
joshualitt71c92602015-01-14 08:12:47 -0800365 fInEllipseRadii = &this->addVertexAttrib(Attribute("inEllipseRadii",
joshualitt2dd1ae02014-12-03 06:24:10 -0800366 kVec4f_GrVertexAttribType));
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000367 fStroke = stroke;
368 }
369
joshualitt71c92602015-01-14 08:12:47 -0800370 const Attribute* fInPosition;
371 const Attribute* fInEllipseOffset;
372 const Attribute* fInEllipseRadii;
joshualitt88c23fc2015-05-13 14:18:07 -0700373 GrColor fColor;
joshualitte3ababe2015-05-15 07:56:07 -0700374 SkMatrix fLocalMatrix;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000375 bool fStroke;
joshualittb8c241a2015-05-19 08:23:30 -0700376 bool fUsesLocalCoords;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000377
joshualittb0a8a372014-09-23 09:50:21 -0700378 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000379
joshualitt249af152014-09-15 11:41:13 -0700380 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000381};
382
joshualittb0a8a372014-09-23 09:50:21 -0700383GR_DEFINE_GEOMETRY_PROCESSOR_TEST(EllipseEdgeEffect);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000384
joshualitt0067ff52015-07-08 14:26:19 -0700385GrGeometryProcessor* EllipseEdgeEffect::TestCreate(GrProcessorTestData* d) {
386 return EllipseEdgeEffect::Create(GrRandomColor(d->fRandom),
387 d->fRandom->nextBool(),
388 GrTest::TestMatrix(d->fRandom),
389 d->fRandom->nextBool());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000390}
391
392///////////////////////////////////////////////////////////////////////////////
393
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000394/**
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000395 * 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 +0000396 * specified as a 2D offset from center for both the outer and inner paths (if stroked). The
397 * implict equation used is for a unit circle (x^2 + y^2 - 1 = 0) and the edge corrected by
398 * using differentials.
399 *
400 * The result is device-independent and can be used with any affine matrix.
401 */
402
joshualitt249af152014-09-15 11:41:13 -0700403class DIEllipseEdgeEffect : public GrGeometryProcessor {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000404public:
405 enum Mode { kStroke = 0, kHairline, kFill };
406
joshualittb8c241a2015-05-19 08:23:30 -0700407 static GrGeometryProcessor* Create(GrColor color, const SkMatrix& viewMatrix, Mode mode,
408 bool usesLocalCoords) {
409 return SkNEW_ARGS(DIEllipseEdgeEffect, (color, viewMatrix, mode, usesLocalCoords));
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000410 }
411
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000412 virtual ~DIEllipseEdgeEffect() {}
413
mtklein36352bf2015-03-25 18:17:31 -0700414 const char* name() const override { return "DIEllipseEdge"; }
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000415
joshualitt71c92602015-01-14 08:12:47 -0800416 const Attribute* inPosition() const { return fInPosition; }
417 const Attribute* inEllipseOffsets0() const { return fInEllipseOffsets0; }
418 const Attribute* inEllipseOffsets1() const { return fInEllipseOffsets1; }
joshualitt88c23fc2015-05-13 14:18:07 -0700419 GrColor color() const { return fColor; }
joshualittb8c241a2015-05-19 08:23:30 -0700420 bool colorIgnored() const { return GrColor_ILLEGAL == fColor; }
joshualitte578a952015-05-14 10:09:13 -0700421 const SkMatrix& viewMatrix() const { return fViewMatrix; }
joshualittb8c241a2015-05-19 08:23:30 -0700422 bool usesLocalCoords() const { return fUsesLocalCoords; }
joshualitt249af152014-09-15 11:41:13 -0700423
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000424 inline Mode getMode() const { return fMode; }
425
joshualittb0a8a372014-09-23 09:50:21 -0700426 class GLProcessor : public GrGLGeometryProcessor {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000427 public:
joshualitteb2a6762014-12-04 11:35:33 -0800428 GLProcessor(const GrGeometryProcessor&,
joshualitt9b989322014-12-15 14:16:27 -0800429 const GrBatchTracker&)
joshualitt5559ca22015-05-21 15:50:36 -0700430 : fViewMatrix(SkMatrix::InvalidMatrix()), fColor(GrColor_ILLEGAL) {}
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000431
mtklein36352bf2015-03-25 18:17:31 -0700432 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{
joshualitt2dd1ae02014-12-03 06:24:10 -0800433 const DIEllipseEdgeEffect& ee = args.fGP.cast<DIEllipseEdgeEffect>();
joshualitt9b989322014-12-15 14:16:27 -0800434 GrGLGPBuilder* pb = args.fPB;
joshualitt2dd1ae02014-12-03 06:24:10 -0800435 GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder();
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000436
joshualittabb52a12015-01-13 15:02:10 -0800437 // emit attributes
438 vsBuilder->emitAttributes(ee);
439
joshualitt74077b92014-10-24 11:26:03 -0700440 GrGLVertToFrag offsets0(kVec2f_GrSLType);
441 args.fPB->addVarying("EllipseOffsets0", &offsets0);
joshualitt74077b92014-10-24 11:26:03 -0700442 vsBuilder->codeAppendf("%s = %s;", offsets0.vsOut(),
joshualitt2dd1ae02014-12-03 06:24:10 -0800443 ee.inEllipseOffsets0()->fName);
joshualitt74077b92014-10-24 11:26:03 -0700444
445 GrGLVertToFrag offsets1(kVec2f_GrSLType);
446 args.fPB->addVarying("EllipseOffsets1", &offsets1);
447 vsBuilder->codeAppendf("%s = %s;", offsets1.vsOut(),
joshualitt2dd1ae02014-12-03 06:24:10 -0800448 ee.inEllipseOffsets1()->fName);
449
joshualittb8c241a2015-05-19 08:23:30 -0700450 // setup pass through color
451 if (!ee.colorIgnored()) {
452 this->setupUniformColor(pb, args.fOutputColor, &fColorUniform);
453 }
joshualitt9b989322014-12-15 14:16:27 -0800454
joshualittabb52a12015-01-13 15:02:10 -0800455 // Setup position
joshualitt5559ca22015-05-21 15:50:36 -0700456 this->setupPosition(pb, gpArgs, ee.inPosition()->fName, ee.viewMatrix(),
457 &fViewMatrixUniform);
joshualittabb52a12015-01-13 15:02:10 -0800458
459 // emit transforms
robertphillips46d36f02015-01-18 08:14:14 -0800460 this->emitTransforms(args.fPB, gpArgs->fPositionVar, ee.inPosition()->fName,
joshualitte3ababe2015-05-15 07:56:07 -0700461 args.fTransformsIn, args.fTransformsOut);
joshualitt4973d9d2014-11-08 09:24:25 -0800462
egdaniel29bee0f2015-04-29 11:54:42 -0700463 GrGLFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -0700464 SkAssertResult(fsBuilder->enableFeature(
465 GrGLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature));
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000466 // for outer curve
joshualitt74077b92014-10-24 11:26:03 -0700467 fsBuilder->codeAppendf("vec2 scaledOffset = %s.xy;", offsets0.fsIn());
468 fsBuilder->codeAppend("float test = dot(scaledOffset, scaledOffset) - 1.0;");
469 fsBuilder->codeAppendf("vec2 duvdx = dFdx(%s);", offsets0.fsIn());
470 fsBuilder->codeAppendf("vec2 duvdy = dFdy(%s);", offsets0.fsIn());
471 fsBuilder->codeAppendf("vec2 grad = vec2(2.0*%s.x*duvdx.x + 2.0*%s.y*duvdx.y,"
472 " 2.0*%s.x*duvdy.x + 2.0*%s.y*duvdy.y);",
473 offsets0.fsIn(), offsets0.fsIn(), offsets0.fsIn(), offsets0.fsIn());
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000474
joshualitt74077b92014-10-24 11:26:03 -0700475 fsBuilder->codeAppend("float grad_dot = dot(grad, grad);");
commit-bot@chromium.org1b035d82014-04-09 17:11:09 +0000476 // avoid calling inversesqrt on zero.
joshualitt74077b92014-10-24 11:26:03 -0700477 fsBuilder->codeAppend("grad_dot = max(grad_dot, 1.0e-4);");
478 fsBuilder->codeAppend("float invlen = inversesqrt(grad_dot);");
joshualitt2dd1ae02014-12-03 06:24:10 -0800479 if (kHairline == ee.getMode()) {
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000480 // can probably do this with one step
joshualitt74077b92014-10-24 11:26:03 -0700481 fsBuilder->codeAppend("float edgeAlpha = clamp(1.0-test*invlen, 0.0, 1.0);");
482 fsBuilder->codeAppend("edgeAlpha *= clamp(1.0+test*invlen, 0.0, 1.0);");
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000483 } else {
joshualitt74077b92014-10-24 11:26:03 -0700484 fsBuilder->codeAppend("float edgeAlpha = clamp(0.5-test*invlen, 0.0, 1.0);");
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000485 }
486
487 // for inner curve
joshualitt2dd1ae02014-12-03 06:24:10 -0800488 if (kStroke == ee.getMode()) {
joshualitt74077b92014-10-24 11:26:03 -0700489 fsBuilder->codeAppendf("scaledOffset = %s.xy;", offsets1.fsIn());
490 fsBuilder->codeAppend("test = dot(scaledOffset, scaledOffset) - 1.0;");
491 fsBuilder->codeAppendf("duvdx = dFdx(%s);", offsets1.fsIn());
492 fsBuilder->codeAppendf("duvdy = dFdy(%s);", offsets1.fsIn());
493 fsBuilder->codeAppendf("grad = vec2(2.0*%s.x*duvdx.x + 2.0*%s.y*duvdx.y,"
494 " 2.0*%s.x*duvdy.x + 2.0*%s.y*duvdy.y);",
495 offsets1.fsIn(), offsets1.fsIn(), offsets1.fsIn(),
496 offsets1.fsIn());
497 fsBuilder->codeAppend("invlen = inversesqrt(dot(grad, grad));");
498 fsBuilder->codeAppend("edgeAlpha *= clamp(0.5+test*invlen, 0.0, 1.0);");
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000499 }
500
joshualitt2dd1ae02014-12-03 06:24:10 -0800501 fsBuilder->codeAppendf("%s = vec4(edgeAlpha);", args.fOutputCoverage);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000502 }
503
robertphillips46d36f02015-01-18 08:14:14 -0800504 static void GenKey(const GrGeometryProcessor& gp,
joshualitt9b989322014-12-15 14:16:27 -0800505 const GrBatchTracker& bt,
jvanverthcfc18862015-04-28 08:48:20 -0700506 const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700507 GrProcessorKeyBuilder* b) {
robertphillips46d36f02015-01-18 08:14:14 -0800508 const DIEllipseEdgeEffect& ellipseEffect = gp.cast<DIEllipseEdgeEffect>();
joshualitt8fc6c2d2014-12-22 15:27:05 -0800509 uint16_t key = ellipseEffect.getMode();
joshualittb8c241a2015-05-19 08:23:30 -0700510 key |= ellipseEffect.colorIgnored() << 9;
511 key |= ComputePosKey(ellipseEffect.viewMatrix()) << 10;
512 b->add32(key);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000513 }
514
joshualitt9b989322014-12-15 14:16:27 -0800515 virtual void setData(const GrGLProgramDataManager& pdman,
516 const GrPrimitiveProcessor& gp,
mtklein36352bf2015-03-25 18:17:31 -0700517 const GrBatchTracker& bt) override {
joshualitte578a952015-05-14 10:09:13 -0700518 const DIEllipseEdgeEffect& dee = gp.cast<DIEllipseEdgeEffect>();
joshualitt5559ca22015-05-21 15:50:36 -0700519
520 if (!dee.viewMatrix().isIdentity() && !fViewMatrix.cheapEqualTo(dee.viewMatrix())) {
521 fViewMatrix = dee.viewMatrix();
522 GrGLfloat viewMatrix[3 * 3];
523 GrGLGetMatrix<3>(viewMatrix, fViewMatrix);
524 pdman.setMatrix3f(fViewMatrixUniform, viewMatrix);
525 }
joshualittee2af952014-12-30 09:04:15 -0800526
joshualittb8c241a2015-05-19 08:23:30 -0700527 if (dee.color() != fColor) {
joshualitt9b989322014-12-15 14:16:27 -0800528 GrGLfloat c[4];
joshualittb8c241a2015-05-19 08:23:30 -0700529 GrColorToRGBAFloat(dee.color(), c);
joshualitt9b989322014-12-15 14:16:27 -0800530 pdman.set4fv(fColorUniform, 1, c);
joshualittb8c241a2015-05-19 08:23:30 -0700531 fColor = dee.color();
joshualitt9b989322014-12-15 14:16:27 -0800532 }
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000533 }
534
535 private:
joshualitt5559ca22015-05-21 15:50:36 -0700536 SkMatrix fViewMatrix;
joshualitt9b989322014-12-15 14:16:27 -0800537 GrColor fColor;
538 UniformHandle fColorUniform;
joshualitt5559ca22015-05-21 15:50:36 -0700539 UniformHandle fViewMatrixUniform;
joshualitt9b989322014-12-15 14:16:27 -0800540
joshualitt249af152014-09-15 11:41:13 -0700541 typedef GrGLGeometryProcessor INHERITED;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000542 };
543
joshualitteb2a6762014-12-04 11:35:33 -0800544 virtual void getGLProcessorKey(const GrBatchTracker& bt,
jvanverthcfc18862015-04-28 08:48:20 -0700545 const GrGLSLCaps& caps,
mtklein36352bf2015-03-25 18:17:31 -0700546 GrProcessorKeyBuilder* b) const override {
joshualitteb2a6762014-12-04 11:35:33 -0800547 GLProcessor::GenKey(*this, bt, caps, b);
548 }
549
joshualittabb52a12015-01-13 15:02:10 -0800550 virtual GrGLPrimitiveProcessor* createGLInstance(const GrBatchTracker& bt,
jvanverthcfc18862015-04-28 08:48:20 -0700551 const GrGLSLCaps&) const override {
joshualitteb2a6762014-12-04 11:35:33 -0800552 return SkNEW_ARGS(GLProcessor, (*this, bt));
553 }
554
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000555private:
joshualittb8c241a2015-05-19 08:23:30 -0700556 DIEllipseEdgeEffect(GrColor color, const SkMatrix& viewMatrix, Mode mode,
557 bool usesLocalCoords)
joshualitte578a952015-05-14 10:09:13 -0700558 : fColor(color)
joshualittb8c241a2015-05-19 08:23:30 -0700559 , fViewMatrix(viewMatrix)
560 , fUsesLocalCoords(usesLocalCoords) {
joshualitteb2a6762014-12-04 11:35:33 -0800561 this->initClassID<DIEllipseEdgeEffect>();
senorblancof2539d52015-05-20 14:03:42 -0700562 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType,
563 kHigh_GrSLPrecision));
joshualitt71c92602015-01-14 08:12:47 -0800564 fInEllipseOffsets0 = &this->addVertexAttrib(Attribute("inEllipseOffsets0",
joshualittb8c241a2015-05-19 08:23:30 -0700565 kVec2f_GrVertexAttribType));
joshualitt71c92602015-01-14 08:12:47 -0800566 fInEllipseOffsets1 = &this->addVertexAttrib(Attribute("inEllipseOffsets1",
joshualittb8c241a2015-05-19 08:23:30 -0700567 kVec2f_GrVertexAttribType));
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000568 fMode = mode;
569 }
570
joshualitt71c92602015-01-14 08:12:47 -0800571 const Attribute* fInPosition;
572 const Attribute* fInEllipseOffsets0;
573 const Attribute* fInEllipseOffsets1;
joshualitt88c23fc2015-05-13 14:18:07 -0700574 GrColor fColor;
joshualitte578a952015-05-14 10:09:13 -0700575 SkMatrix fViewMatrix;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000576 Mode fMode;
joshualittb8c241a2015-05-19 08:23:30 -0700577 bool fUsesLocalCoords;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000578
joshualittb0a8a372014-09-23 09:50:21 -0700579 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000580
joshualitt249af152014-09-15 11:41:13 -0700581 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000582};
583
joshualittb0a8a372014-09-23 09:50:21 -0700584GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DIEllipseEdgeEffect);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000585
joshualitt0067ff52015-07-08 14:26:19 -0700586GrGeometryProcessor* DIEllipseEdgeEffect::TestCreate(GrProcessorTestData* d) {
587 return DIEllipseEdgeEffect::Create(GrRandomColor(d->fRandom),
588 GrTest::TestMatrix(d->fRandom),
589 (Mode)(d->fRandom->nextRangeU(0,2)),
590 d->fRandom->nextBool());
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000591}
592
593///////////////////////////////////////////////////////////////////////////////
594
robertphillipsea461502015-05-26 11:38:03 -0700595bool GrOvalRenderer::DrawOval(GrDrawTarget* target,
joshualittae3d63a2015-07-13 08:44:06 -0700596 const GrPipelineBuilder& pipelineBuilder,
joshualitt2e3b3e32014-12-09 13:31:14 -0800597 GrColor color,
joshualitt8059eb92014-12-29 15:10:07 -0800598 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800599 bool useAA,
600 const SkRect& oval,
joshualittae3d63a2015-07-13 08:44:06 -0700601 const SkStrokeRec& stroke) {
602 bool useCoverageAA = useAA && !pipelineBuilder.getRenderTarget()->isUnifiedMultisampled();
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000603
604 if (!useCoverageAA) {
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000605 return false;
606 }
607
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000608 // we can draw circles
joshualitt8059eb92014-12-29 15:10:07 -0800609 if (SkScalarNearlyEqual(oval.width(), oval.height()) && circle_stays_circle(viewMatrix)) {
robertphillipsea461502015-05-26 11:38:03 -0700610 DrawCircle(target, pipelineBuilder, color, viewMatrix, useCoverageAA, oval, stroke);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000611 // if we have shader derivative support, render as device-independent
jvanverthe9c0fc62015-04-29 11:18:05 -0700612 } else if (target->caps()->shaderCaps()->shaderDerivativeSupport()) {
robertphillipsea461502015-05-26 11:38:03 -0700613 return DrawDIEllipse(target, pipelineBuilder, color, viewMatrix, useCoverageAA, oval,
614 stroke);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +0000615 // otherwise axis-aligned ellipses only
joshualitt8059eb92014-12-29 15:10:07 -0800616 } else if (viewMatrix.rectStaysRect()) {
robertphillipsea461502015-05-26 11:38:03 -0700617 return DrawEllipse(target, pipelineBuilder, color, viewMatrix, useCoverageAA, oval,
618 stroke);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000619 } else {
620 return false;
621 }
622
623 return true;
624}
625
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000626///////////////////////////////////////////////////////////////////////////////
627
joshualitt76e7fb62015-02-11 08:52:27 -0800628class CircleBatch : public GrBatch {
629public:
630 struct Geometry {
631 GrColor fColor;
632 SkMatrix fViewMatrix;
633 SkScalar fInnerRadius;
634 SkScalar fOuterRadius;
635 bool fStroke;
egdanielbc227142015-04-21 06:28:08 -0700636 SkRect fDevBounds;
joshualitt76e7fb62015-02-11 08:52:27 -0800637 };
638
bsalomoned0bcad2015-05-04 10:36:42 -0700639 static GrBatch* Create(const Geometry& geometry) { return SkNEW_ARGS(CircleBatch, (geometry)); }
joshualitt76e7fb62015-02-11 08:52:27 -0800640
mtklein36352bf2015-03-25 18:17:31 -0700641 const char* name() const override { return "CircleBatch"; }
joshualitt76e7fb62015-02-11 08:52:27 -0800642
mtklein36352bf2015-03-25 18:17:31 -0700643 void getInvariantOutputColor(GrInitInvariantOutput* out) const override {
joshualitt76e7fb62015-02-11 08:52:27 -0800644 // When this is called on a batch, there is only one geometry bundle
645 out->setKnownFourComponents(fGeoData[0].fColor);
646 }
647
mtklein36352bf2015-03-25 18:17:31 -0700648 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override {
joshualitt76e7fb62015-02-11 08:52:27 -0800649 out->setUnknownSingleComponent();
650 }
651
mtklein36352bf2015-03-25 18:17:31 -0700652 void initBatchTracker(const GrPipelineInfo& init) override {
joshualitt76e7fb62015-02-11 08:52:27 -0800653 // Handle any color overrides
bsalomon7765a472015-07-08 11:26:37 -0700654 if (!init.readsColor()) {
joshualitt76e7fb62015-02-11 08:52:27 -0800655 fGeoData[0].fColor = GrColor_ILLEGAL;
joshualitt76e7fb62015-02-11 08:52:27 -0800656 }
bsalomon7765a472015-07-08 11:26:37 -0700657 init.getOverrideColorIfSet(&fGeoData[0].fColor);
joshualitt76e7fb62015-02-11 08:52:27 -0800658
659 // setup batch properties
bsalomon7765a472015-07-08 11:26:37 -0700660 fBatch.fColorIgnored = !init.readsColor();
joshualitt76e7fb62015-02-11 08:52:27 -0800661 fBatch.fColor = fGeoData[0].fColor;
662 fBatch.fStroke = fGeoData[0].fStroke;
bsalomon7765a472015-07-08 11:26:37 -0700663 fBatch.fUsesLocalCoords = init.readsLocalCoords();
664 fBatch.fCoverageIgnored = !init.readsCoverage();
joshualitt76e7fb62015-02-11 08:52:27 -0800665 }
666
mtklein36352bf2015-03-25 18:17:31 -0700667 void generateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline) override {
joshualitt76e7fb62015-02-11 08:52:27 -0800668 SkMatrix invert;
669 if (!this->viewMatrix().invert(&invert)) {
670 return;
671 }
672
673 // Setup geometry processor
674 SkAutoTUnref<GrGeometryProcessor> gp(CircleEdgeEffect::Create(this->color(),
675 this->stroke(),
joshualittb8c241a2015-05-19 08:23:30 -0700676 invert,
677 this->usesLocalCoords()));
joshualitt76e7fb62015-02-11 08:52:27 -0800678
679 batchTarget->initDraw(gp, pipeline);
680
joshualitt76e7fb62015-02-11 08:52:27 -0800681 int instanceCount = fGeoData.count();
joshualitt76e7fb62015-02-11 08:52:27 -0800682 size_t vertexStride = gp->getVertexStride();
683 SkASSERT(vertexStride == sizeof(CircleVertex));
bsalomonb5238a72015-05-05 07:49:49 -0700684 QuadHelper helper;
685 CircleVertex* verts = reinterpret_cast<CircleVertex*>(helper.init(batchTarget, vertexStride,
686 instanceCount));
687 if (!verts) {
joshualitt4b31de82015-03-05 14:33:41 -0800688 return;
689 }
690
joshualitt76e7fb62015-02-11 08:52:27 -0800691 for (int i = 0; i < instanceCount; i++) {
bsalomonb5238a72015-05-05 07:49:49 -0700692 Geometry& geom = fGeoData[i];
joshualitt76e7fb62015-02-11 08:52:27 -0800693
bsalomonb5238a72015-05-05 07:49:49 -0700694 SkScalar innerRadius = geom.fInnerRadius;
695 SkScalar outerRadius = geom.fOuterRadius;
joshualitt76e7fb62015-02-11 08:52:27 -0800696
bsalomonb5238a72015-05-05 07:49:49 -0700697 const SkRect& bounds = geom.fDevBounds;
joshualitt76e7fb62015-02-11 08:52:27 -0800698
699 // The inner radius in the vertex data must be specified in normalized space.
700 innerRadius = innerRadius / outerRadius;
701 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
702 verts[0].fOffset = SkPoint::Make(-1, -1);
703 verts[0].fOuterRadius = outerRadius;
704 verts[0].fInnerRadius = innerRadius;
705
706 verts[1].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
707 verts[1].fOffset = SkPoint::Make(-1, 1);
708 verts[1].fOuterRadius = outerRadius;
709 verts[1].fInnerRadius = innerRadius;
710
711 verts[2].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
712 verts[2].fOffset = SkPoint::Make(1, 1);
713 verts[2].fOuterRadius = outerRadius;
714 verts[2].fInnerRadius = innerRadius;
715
716 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
717 verts[3].fOffset = SkPoint::Make(1, -1);
718 verts[3].fOuterRadius = outerRadius;
719 verts[3].fInnerRadius = innerRadius;
720
bsalomonb5238a72015-05-05 07:49:49 -0700721 verts += kVerticesPerQuad;
joshualitt76e7fb62015-02-11 08:52:27 -0800722 }
bsalomone64eb572015-05-07 11:35:55 -0700723 helper.issueDraw(batchTarget);
joshualitt76e7fb62015-02-11 08:52:27 -0800724 }
725
726 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
727
728private:
729 CircleBatch(const Geometry& geometry) {
730 this->initClassID<CircleBatch>();
731 fGeoData.push_back(geometry);
joshualitt99c7c072015-05-01 13:43:30 -0700732
733 this->setBounds(geometry.fDevBounds);
joshualitt76e7fb62015-02-11 08:52:27 -0800734 }
735
mtklein36352bf2015-03-25 18:17:31 -0700736 bool onCombineIfPossible(GrBatch* t) override {
joshualitt8cab9a72015-07-16 09:13:50 -0700737 if (!this->pipeline()->isEqual(*t->pipeline())) {
738 return false;
739 }
740
joshualitt76e7fb62015-02-11 08:52:27 -0800741 CircleBatch* that = t->cast<CircleBatch>();
742
743 // TODO use vertex color to avoid breaking batches
744 if (this->color() != that->color()) {
745 return false;
746 }
747
748 if (this->stroke() != that->stroke()) {
749 return false;
750 }
751
752 SkASSERT(this->usesLocalCoords() == that->usesLocalCoords());
753 if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
754 return false;
755 }
756
757 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin());
joshualitt99c7c072015-05-01 13:43:30 -0700758 this->joinBounds(that->bounds());
joshualitt76e7fb62015-02-11 08:52:27 -0800759 return true;
760 }
761
762 GrColor color() const { return fBatch.fColor; }
763 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
764 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; }
765 bool stroke() const { return fBatch.fStroke; }
766
767 struct BatchTracker {
768 GrColor fColor;
769 bool fStroke;
770 bool fUsesLocalCoords;
771 bool fColorIgnored;
772 bool fCoverageIgnored;
773 };
774
joshualitt76e7fb62015-02-11 08:52:27 -0800775 BatchTracker fBatch;
776 SkSTArray<1, Geometry, true> fGeoData;
777};
778
joshualitt3e708c52015-04-30 13:49:27 -0700779static GrBatch* create_circle_batch(GrColor color,
780 const SkMatrix& viewMatrix,
781 bool useCoverageAA,
782 const SkRect& circle,
joshualittd96a67b2015-05-05 14:09:05 -0700783 const SkStrokeRec& stroke) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000784 SkPoint center = SkPoint::Make(circle.centerX(), circle.centerY());
joshualitt8059eb92014-12-29 15:10:07 -0800785 viewMatrix.mapPoints(&center, 1);
786 SkScalar radius = viewMatrix.mapRadius(SkScalarHalf(circle.width()));
787 SkScalar strokeWidth = viewMatrix.mapRadius(stroke.getWidth());
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000788
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000789 SkStrokeRec::Style style = stroke.getStyle();
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000790 bool isStrokeOnly = SkStrokeRec::kStroke_Style == style ||
791 SkStrokeRec::kHairline_Style == style;
792 bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == style;
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000793
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000794 SkScalar innerRadius = 0.0f;
795 SkScalar outerRadius = radius;
796 SkScalar halfWidth = 0;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000797 if (hasStroke) {
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000798 if (SkScalarNearlyZero(strokeWidth)) {
799 halfWidth = SK_ScalarHalf;
800 } else {
801 halfWidth = SkScalarHalf(strokeWidth);
802 }
803
804 outerRadius += halfWidth;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000805 if (isStrokeOnly) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000806 innerRadius = radius - halfWidth;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000807 }
808 }
809
bsalomonce1c8862014-12-15 07:11:22 -0800810 // The radii are outset for two reasons. First, it allows the shader to simply perform simpler
811 // computation because the computed alpha is zero, rather than 50%, at the radius.
812 // Second, the outer radius is used to compute the verts of the bounding box that is rendered
813 // and the outset ensures the box will cover all partially covered by the circle.
bsalomon@google.com58e30fe2013-04-01 19:01:20 +0000814 outerRadius += SK_ScalarHalf;
815 innerRadius -= SK_ScalarHalf;
816
joshualitt76e7fb62015-02-11 08:52:27 -0800817 CircleBatch::Geometry geometry;
818 geometry.fViewMatrix = viewMatrix;
819 geometry.fColor = color;
820 geometry.fInnerRadius = innerRadius;
821 geometry.fOuterRadius = outerRadius;
822 geometry.fStroke = isStrokeOnly && innerRadius > 0;
joshualittd96a67b2015-05-05 14:09:05 -0700823 geometry.fDevBounds = SkRect::MakeLTRB(center.fX - outerRadius, center.fY - outerRadius,
824 center.fX + outerRadius, center.fY + outerRadius);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000825
joshualitt3e708c52015-04-30 13:49:27 -0700826 return CircleBatch::Create(geometry);
827}
828
robertphillipsea461502015-05-26 11:38:03 -0700829void GrOvalRenderer::DrawCircle(GrDrawTarget* target,
joshualittae3d63a2015-07-13 08:44:06 -0700830 const GrPipelineBuilder& pipelineBuilder,
joshualitt3e708c52015-04-30 13:49:27 -0700831 GrColor color,
832 const SkMatrix& viewMatrix,
833 bool useCoverageAA,
834 const SkRect& circle,
835 const SkStrokeRec& stroke) {
joshualitt3e708c52015-04-30 13:49:27 -0700836 SkAutoTUnref<GrBatch> batch(create_circle_batch(color, viewMatrix, useCoverageAA, circle,
joshualittd96a67b2015-05-05 14:09:05 -0700837 stroke));
joshualittae3d63a2015-07-13 08:44:06 -0700838 target->drawBatch(pipelineBuilder, batch);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000839}
840
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000841///////////////////////////////////////////////////////////////////////////////
842
joshualitt76e7fb62015-02-11 08:52:27 -0800843class EllipseBatch : public GrBatch {
844public:
845 struct Geometry {
846 GrColor fColor;
847 SkMatrix fViewMatrix;
848 SkScalar fXRadius;
849 SkScalar fYRadius;
850 SkScalar fInnerXRadius;
851 SkScalar fInnerYRadius;
852 bool fStroke;
egdanielbc227142015-04-21 06:28:08 -0700853 SkRect fDevBounds;
joshualitt76e7fb62015-02-11 08:52:27 -0800854 };
855
856 static GrBatch* Create(const Geometry& geometry) {
857 return SkNEW_ARGS(EllipseBatch, (geometry));
858 }
859
mtklein36352bf2015-03-25 18:17:31 -0700860 const char* name() const override { return "EllipseBatch"; }
joshualitt76e7fb62015-02-11 08:52:27 -0800861
mtklein36352bf2015-03-25 18:17:31 -0700862 void getInvariantOutputColor(GrInitInvariantOutput* out) const override {
joshualitt76e7fb62015-02-11 08:52:27 -0800863 // When this is called on a batch, there is only one geometry bundle
864 out->setKnownFourComponents(fGeoData[0].fColor);
865 }
mtklein36352bf2015-03-25 18:17:31 -0700866 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override {
joshualitt76e7fb62015-02-11 08:52:27 -0800867 out->setUnknownSingleComponent();
868 }
869
mtklein36352bf2015-03-25 18:17:31 -0700870 void initBatchTracker(const GrPipelineInfo& init) override {
joshualitt76e7fb62015-02-11 08:52:27 -0800871 // Handle any color overrides
bsalomon7765a472015-07-08 11:26:37 -0700872 if (!init.readsCoverage()) {
joshualitt76e7fb62015-02-11 08:52:27 -0800873 fGeoData[0].fColor = GrColor_ILLEGAL;
joshualitt76e7fb62015-02-11 08:52:27 -0800874 }
bsalomon7765a472015-07-08 11:26:37 -0700875 init.getOverrideColorIfSet(&fGeoData[0].fColor);
joshualitt76e7fb62015-02-11 08:52:27 -0800876
877 // setup batch properties
bsalomon7765a472015-07-08 11:26:37 -0700878 fBatch.fColorIgnored = !init.readsColor();
joshualitt76e7fb62015-02-11 08:52:27 -0800879 fBatch.fColor = fGeoData[0].fColor;
880 fBatch.fStroke = fGeoData[0].fStroke;
bsalomon7765a472015-07-08 11:26:37 -0700881 fBatch.fUsesLocalCoords = init.readsLocalCoords();
882 fBatch.fCoverageIgnored = !init.readsCoverage();
joshualitt76e7fb62015-02-11 08:52:27 -0800883 }
884
mtklein36352bf2015-03-25 18:17:31 -0700885 void generateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline) override {
joshualitt76e7fb62015-02-11 08:52:27 -0800886 SkMatrix invert;
887 if (!this->viewMatrix().invert(&invert)) {
888 return;
889 }
890
891 // Setup geometry processor
892 SkAutoTUnref<GrGeometryProcessor> gp(EllipseEdgeEffect::Create(this->color(),
893 this->stroke(),
joshualittb8c241a2015-05-19 08:23:30 -0700894 invert,
895 this->usesLocalCoords()));
joshualitt76e7fb62015-02-11 08:52:27 -0800896
897 batchTarget->initDraw(gp, pipeline);
898
joshualitt76e7fb62015-02-11 08:52:27 -0800899 int instanceCount = fGeoData.count();
bsalomonb5238a72015-05-05 07:49:49 -0700900 QuadHelper helper;
joshualitt76e7fb62015-02-11 08:52:27 -0800901 size_t vertexStride = gp->getVertexStride();
joshualitt19e00582015-02-11 17:36:30 -0800902 SkASSERT(vertexStride == sizeof(EllipseVertex));
bsalomonb5238a72015-05-05 07:49:49 -0700903 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(
904 helper.init(batchTarget, vertexStride, instanceCount));
905 if (!verts) {
joshualitt4b31de82015-03-05 14:33:41 -0800906 return;
907 }
908
bsalomon8415abe2015-05-04 11:41:41 -0700909 for (int i = 0; i < instanceCount; i++) {
bsalomonb5238a72015-05-05 07:49:49 -0700910 Geometry& geom = fGeoData[i];
bsalomon8415abe2015-05-04 11:41:41 -0700911
bsalomonb5238a72015-05-05 07:49:49 -0700912 SkScalar xRadius = geom.fXRadius;
913 SkScalar yRadius = geom.fYRadius;
joshualitt76e7fb62015-02-11 08:52:27 -0800914
915 // Compute the reciprocals of the radii here to save time in the shader
916 SkScalar xRadRecip = SkScalarInvert(xRadius);
917 SkScalar yRadRecip = SkScalarInvert(yRadius);
bsalomonb5238a72015-05-05 07:49:49 -0700918 SkScalar xInnerRadRecip = SkScalarInvert(geom.fInnerXRadius);
919 SkScalar yInnerRadRecip = SkScalarInvert(geom.fInnerYRadius);
joshualitt76e7fb62015-02-11 08:52:27 -0800920
bsalomonb5238a72015-05-05 07:49:49 -0700921 const SkRect& bounds = geom.fDevBounds;
joshualitt76e7fb62015-02-11 08:52:27 -0800922
923 // The inner radius in the vertex data must be specified in normalized space.
924 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
925 verts[0].fOffset = SkPoint::Make(-xRadius, -yRadius);
926 verts[0].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
927 verts[0].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
928
929 verts[1].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
930 verts[1].fOffset = SkPoint::Make(-xRadius, yRadius);
931 verts[1].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
932 verts[1].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
933
934 verts[2].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
935 verts[2].fOffset = SkPoint::Make(xRadius, yRadius);
936 verts[2].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
937 verts[2].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
938
939 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
940 verts[3].fOffset = SkPoint::Make(xRadius, -yRadius);
941 verts[3].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
942 verts[3].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
943
bsalomonb5238a72015-05-05 07:49:49 -0700944 verts += kVerticesPerQuad;
joshualitt76e7fb62015-02-11 08:52:27 -0800945 }
bsalomone64eb572015-05-07 11:35:55 -0700946 helper.issueDraw(batchTarget);
joshualitt76e7fb62015-02-11 08:52:27 -0800947 }
948
949 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
950
951private:
952 EllipseBatch(const Geometry& geometry) {
953 this->initClassID<EllipseBatch>();
954 fGeoData.push_back(geometry);
joshualitt99c7c072015-05-01 13:43:30 -0700955
956 this->setBounds(geometry.fDevBounds);
joshualitt76e7fb62015-02-11 08:52:27 -0800957 }
958
mtklein36352bf2015-03-25 18:17:31 -0700959 bool onCombineIfPossible(GrBatch* t) override {
joshualitt8cab9a72015-07-16 09:13:50 -0700960 if (!this->pipeline()->isEqual(*t->pipeline())) {
961 return false;
962 }
963
joshualitt76e7fb62015-02-11 08:52:27 -0800964 EllipseBatch* that = t->cast<EllipseBatch>();
965
966 // TODO use vertex color to avoid breaking batches
967 if (this->color() != that->color()) {
968 return false;
969 }
970
971 if (this->stroke() != that->stroke()) {
972 return false;
973 }
974
975 SkASSERT(this->usesLocalCoords() == that->usesLocalCoords());
976 if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
977 return false;
978 }
979
980 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin());
joshualitt99c7c072015-05-01 13:43:30 -0700981 this->joinBounds(that->bounds());
joshualitt76e7fb62015-02-11 08:52:27 -0800982 return true;
983 }
984
985 GrColor color() const { return fBatch.fColor; }
986 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
987 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; }
988 bool stroke() const { return fBatch.fStroke; }
989
990 struct BatchTracker {
991 GrColor fColor;
992 bool fStroke;
993 bool fUsesLocalCoords;
994 bool fColorIgnored;
995 bool fCoverageIgnored;
996 };
997
joshualitt76e7fb62015-02-11 08:52:27 -0800998 BatchTracker fBatch;
999 SkSTArray<1, Geometry, true> fGeoData;
1000};
1001
joshualitt3e708c52015-04-30 13:49:27 -07001002static GrBatch* create_ellipse_batch(GrColor color,
1003 const SkMatrix& viewMatrix,
1004 bool useCoverageAA,
1005 const SkRect& ellipse,
joshualittd96a67b2015-05-05 14:09:05 -07001006 const SkStrokeRec& stroke) {
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001007#ifdef SK_DEBUG
1008 {
1009 // we should have checked for this previously
joshualitt8059eb92014-12-29 15:10:07 -08001010 bool isAxisAlignedEllipse = viewMatrix.rectStaysRect();
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001011 SkASSERT(useCoverageAA && isAxisAlignedEllipse);
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001012 }
1013#endif
1014
commit-bot@chromium.org0c888282013-04-19 19:01:45 +00001015 // do any matrix crunching before we reset the draw state for device coords
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001016 SkPoint center = SkPoint::Make(ellipse.centerX(), ellipse.centerY());
joshualitt8059eb92014-12-29 15:10:07 -08001017 viewMatrix.mapPoints(&center, 1);
commit-bot@chromium.org0c888282013-04-19 19:01:45 +00001018 SkScalar ellipseXRadius = SkScalarHalf(ellipse.width());
1019 SkScalar ellipseYRadius = SkScalarHalf(ellipse.height());
joshualitt8059eb92014-12-29 15:10:07 -08001020 SkScalar xRadius = SkScalarAbs(viewMatrix[SkMatrix::kMScaleX]*ellipseXRadius +
1021 viewMatrix[SkMatrix::kMSkewY]*ellipseYRadius);
1022 SkScalar yRadius = SkScalarAbs(viewMatrix[SkMatrix::kMSkewX]*ellipseXRadius +
1023 viewMatrix[SkMatrix::kMScaleY]*ellipseYRadius);
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001024
commit-bot@chromium.org0c888282013-04-19 19:01:45 +00001025 // do (potentially) anisotropic mapping of stroke
1026 SkVector scaledStroke;
1027 SkScalar strokeWidth = stroke.getWidth();
joshualitt8059eb92014-12-29 15:10:07 -08001028 scaledStroke.fX = SkScalarAbs(strokeWidth*(viewMatrix[SkMatrix::kMScaleX] +
1029 viewMatrix[SkMatrix::kMSkewY]));
1030 scaledStroke.fY = SkScalarAbs(strokeWidth*(viewMatrix[SkMatrix::kMSkewX] +
1031 viewMatrix[SkMatrix::kMScaleY]));
commit-bot@chromium.org0c888282013-04-19 19:01:45 +00001032
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001033 SkStrokeRec::Style style = stroke.getStyle();
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001034 bool isStrokeOnly = SkStrokeRec::kStroke_Style == style ||
1035 SkStrokeRec::kHairline_Style == style;
1036 bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == style;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001037
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +00001038 SkScalar innerXRadius = 0;
1039 SkScalar innerYRadius = 0;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001040 if (hasStroke) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001041 if (SkScalarNearlyZero(scaledStroke.length())) {
1042 scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf);
1043 } else {
1044 scaledStroke.scale(SK_ScalarHalf);
1045 }
1046
1047 // we only handle thick strokes for near-circular ellipses
1048 if (scaledStroke.length() > SK_ScalarHalf &&
1049 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
joshualitt3e708c52015-04-30 13:49:27 -07001050 return NULL;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001051 }
1052
1053 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
1054 if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius ||
1055 scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) {
joshualitt3e708c52015-04-30 13:49:27 -07001056 return NULL;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001057 }
1058
1059 // this is legit only if scale & translation (which should be the case at the moment)
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001060 if (isStrokeOnly) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001061 innerXRadius = xRadius - scaledStroke.fX;
1062 innerYRadius = yRadius - scaledStroke.fY;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001063 }
1064
1065 xRadius += scaledStroke.fX;
1066 yRadius += scaledStroke.fY;
1067 }
1068
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001069 // We've extended the outer x radius out half a pixel to antialias.
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +00001070 // This will also expand the rect so all the pixels will be captured.
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001071 // TODO: Consider if we should use sqrt(2)/2 instead
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +00001072 xRadius += SK_ScalarHalf;
1073 yRadius += SK_ScalarHalf;
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001074
joshualitt76e7fb62015-02-11 08:52:27 -08001075 EllipseBatch::Geometry geometry;
1076 geometry.fViewMatrix = viewMatrix;
1077 geometry.fColor = color;
1078 geometry.fXRadius = xRadius;
1079 geometry.fYRadius = yRadius;
1080 geometry.fInnerXRadius = innerXRadius;
1081 geometry.fInnerYRadius = innerYRadius;
1082 geometry.fStroke = isStrokeOnly && innerXRadius > 0 && innerYRadius > 0;
joshualittd96a67b2015-05-05 14:09:05 -07001083 geometry.fDevBounds = SkRect::MakeLTRB(center.fX - xRadius, center.fY - yRadius,
1084 center.fX + xRadius, center.fY + yRadius);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +00001085
joshualitt3e708c52015-04-30 13:49:27 -07001086 return EllipseBatch::Create(geometry);
1087}
1088
robertphillipsea461502015-05-26 11:38:03 -07001089bool GrOvalRenderer::DrawEllipse(GrDrawTarget* target,
joshualittae3d63a2015-07-13 08:44:06 -07001090 const GrPipelineBuilder& pipelineBuilder,
joshualitt3e708c52015-04-30 13:49:27 -07001091 GrColor color,
1092 const SkMatrix& viewMatrix,
1093 bool useCoverageAA,
1094 const SkRect& ellipse,
1095 const SkStrokeRec& stroke) {
joshualitt3e708c52015-04-30 13:49:27 -07001096 SkAutoTUnref<GrBatch> batch(create_ellipse_batch(color, viewMatrix, useCoverageAA, ellipse,
joshualittd96a67b2015-05-05 14:09:05 -07001097 stroke));
joshualitt3e708c52015-04-30 13:49:27 -07001098 if (!batch) {
1099 return false;
1100 }
1101
joshualittae3d63a2015-07-13 08:44:06 -07001102 target->drawBatch(pipelineBuilder, batch);
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +00001103 return true;
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001104}
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001105
joshualitt76e7fb62015-02-11 08:52:27 -08001106/////////////////////////////////////////////////////////////////////////////////////////////////
1107
1108class DIEllipseBatch : public GrBatch {
1109public:
1110 struct Geometry {
1111 GrColor fColor;
1112 SkMatrix fViewMatrix;
1113 SkScalar fXRadius;
1114 SkScalar fYRadius;
1115 SkScalar fInnerXRadius;
1116 SkScalar fInnerYRadius;
1117 SkScalar fGeoDx;
1118 SkScalar fGeoDy;
1119 DIEllipseEdgeEffect::Mode fMode;
egdaniel9ef1bb12015-04-20 12:28:57 -07001120 SkRect fBounds;
joshualitt76e7fb62015-02-11 08:52:27 -08001121 };
1122
joshualitt99c7c072015-05-01 13:43:30 -07001123 static GrBatch* Create(const Geometry& geometry, const SkRect& bounds) {
1124 return SkNEW_ARGS(DIEllipseBatch, (geometry, bounds));
joshualitt76e7fb62015-02-11 08:52:27 -08001125 }
1126
mtklein36352bf2015-03-25 18:17:31 -07001127 const char* name() const override { return "DIEllipseBatch"; }
joshualitt76e7fb62015-02-11 08:52:27 -08001128
mtklein36352bf2015-03-25 18:17:31 -07001129 void getInvariantOutputColor(GrInitInvariantOutput* out) const override {
joshualitt76e7fb62015-02-11 08:52:27 -08001130 // When this is called on a batch, there is only one geometry bundle
1131 out->setKnownFourComponents(fGeoData[0].fColor);
1132 }
mtklein36352bf2015-03-25 18:17:31 -07001133 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override {
joshualitt76e7fb62015-02-11 08:52:27 -08001134 out->setUnknownSingleComponent();
1135 }
1136
mtklein36352bf2015-03-25 18:17:31 -07001137 void initBatchTracker(const GrPipelineInfo& init) override {
joshualitt76e7fb62015-02-11 08:52:27 -08001138 // Handle any color overrides
bsalomon7765a472015-07-08 11:26:37 -07001139 if (!init.readsColor()) {
joshualitt76e7fb62015-02-11 08:52:27 -08001140 fGeoData[0].fColor = GrColor_ILLEGAL;
joshualitt76e7fb62015-02-11 08:52:27 -08001141 }
bsalomon7765a472015-07-08 11:26:37 -07001142 init.getOverrideColorIfSet(&fGeoData[0].fColor);
joshualitt76e7fb62015-02-11 08:52:27 -08001143
1144 // setup batch properties
bsalomon7765a472015-07-08 11:26:37 -07001145 fBatch.fColorIgnored = !init.readsColor();
joshualitt76e7fb62015-02-11 08:52:27 -08001146 fBatch.fColor = fGeoData[0].fColor;
1147 fBatch.fMode = fGeoData[0].fMode;
bsalomon7765a472015-07-08 11:26:37 -07001148 fBatch.fUsesLocalCoords = init.readsLocalCoords();
1149 fBatch.fCoverageIgnored = !init.readsCoverage();
joshualitt76e7fb62015-02-11 08:52:27 -08001150 }
1151
mtklein36352bf2015-03-25 18:17:31 -07001152 void generateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline) override {
joshualitt76e7fb62015-02-11 08:52:27 -08001153 // Setup geometry processor
1154 SkAutoTUnref<GrGeometryProcessor> gp(DIEllipseEdgeEffect::Create(this->color(),
1155 this->viewMatrix(),
joshualittb8c241a2015-05-19 08:23:30 -07001156 this->mode(),
1157 this->usesLocalCoords()));
joshualitt76e7fb62015-02-11 08:52:27 -08001158
joshualitt76e7fb62015-02-11 08:52:27 -08001159 batchTarget->initDraw(gp, pipeline);
1160
joshualitt76e7fb62015-02-11 08:52:27 -08001161 int instanceCount = fGeoData.count();
joshualitt76e7fb62015-02-11 08:52:27 -08001162 size_t vertexStride = gp->getVertexStride();
joshualitt19e00582015-02-11 17:36:30 -08001163 SkASSERT(vertexStride == sizeof(DIEllipseVertex));
bsalomonb5238a72015-05-05 07:49:49 -07001164 QuadHelper helper;
1165 DIEllipseVertex* verts = reinterpret_cast<DIEllipseVertex*>(
1166 helper.init(batchTarget, vertexStride, instanceCount));
1167 if (!verts) {
joshualitt4b31de82015-03-05 14:33:41 -08001168 return;
1169 }
1170
joshualitt76e7fb62015-02-11 08:52:27 -08001171 for (int i = 0; i < instanceCount; i++) {
bsalomonb5238a72015-05-05 07:49:49 -07001172 Geometry& geom = fGeoData[i];
joshualitt76e7fb62015-02-11 08:52:27 -08001173
bsalomonb5238a72015-05-05 07:49:49 -07001174 SkScalar xRadius = geom.fXRadius;
1175 SkScalar yRadius = geom.fYRadius;
joshualitt76e7fb62015-02-11 08:52:27 -08001176
bsalomonb5238a72015-05-05 07:49:49 -07001177 const SkRect& bounds = geom.fBounds;
joshualitt76e7fb62015-02-11 08:52:27 -08001178
1179 // This adjusts the "radius" to include the half-pixel border
reed80ea19c2015-05-12 10:37:34 -07001180 SkScalar offsetDx = geom.fGeoDx / xRadius;
1181 SkScalar offsetDy = geom.fGeoDy / yRadius;
joshualitt76e7fb62015-02-11 08:52:27 -08001182
reed80ea19c2015-05-12 10:37:34 -07001183 SkScalar innerRatioX = xRadius / geom.fInnerXRadius;
1184 SkScalar innerRatioY = yRadius / geom.fInnerYRadius;
joshualitt76e7fb62015-02-11 08:52:27 -08001185
1186 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
1187 verts[0].fOuterOffset = SkPoint::Make(-1.0f - offsetDx, -1.0f - offsetDy);
1188 verts[0].fInnerOffset = SkPoint::Make(-innerRatioX - offsetDx, -innerRatioY - offsetDy);
1189
1190 verts[1].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
1191 verts[1].fOuterOffset = SkPoint::Make(-1.0f - offsetDx, 1.0f + offsetDy);
1192 verts[1].fInnerOffset = SkPoint::Make(-innerRatioX - offsetDx, innerRatioY + offsetDy);
1193
1194 verts[2].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
1195 verts[2].fOuterOffset = SkPoint::Make(1.0f + offsetDx, 1.0f + offsetDy);
1196 verts[2].fInnerOffset = SkPoint::Make(innerRatioX + offsetDx, innerRatioY + offsetDy);
1197
1198 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
1199 verts[3].fOuterOffset = SkPoint::Make(1.0f + offsetDx, -1.0f - offsetDy);
1200 verts[3].fInnerOffset = SkPoint::Make(innerRatioX + offsetDx, -innerRatioY - offsetDy);
1201
bsalomonb5238a72015-05-05 07:49:49 -07001202 verts += kVerticesPerQuad;
joshualitt76e7fb62015-02-11 08:52:27 -08001203 }
bsalomone64eb572015-05-07 11:35:55 -07001204 helper.issueDraw(batchTarget);
joshualitt76e7fb62015-02-11 08:52:27 -08001205 }
1206
1207 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
1208
1209private:
joshualitt99c7c072015-05-01 13:43:30 -07001210 DIEllipseBatch(const Geometry& geometry, const SkRect& bounds) {
joshualitt76e7fb62015-02-11 08:52:27 -08001211 this->initClassID<DIEllipseBatch>();
1212 fGeoData.push_back(geometry);
joshualitt99c7c072015-05-01 13:43:30 -07001213
1214 this->setBounds(bounds);
joshualitt76e7fb62015-02-11 08:52:27 -08001215 }
1216
mtklein36352bf2015-03-25 18:17:31 -07001217 bool onCombineIfPossible(GrBatch* t) override {
joshualitt8cab9a72015-07-16 09:13:50 -07001218 if (!this->pipeline()->isEqual(*t->pipeline())) {
1219 return false;
1220 }
1221
joshualitt76e7fb62015-02-11 08:52:27 -08001222 DIEllipseBatch* that = t->cast<DIEllipseBatch>();
1223
1224 // TODO use vertex color to avoid breaking batches
1225 if (this->color() != that->color()) {
1226 return false;
1227 }
1228
1229 if (this->mode() != that->mode()) {
1230 return false;
1231 }
1232
joshualittd96a67b2015-05-05 14:09:05 -07001233 // TODO rewrite to allow positioning on CPU
1234 if (!this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
joshualitt76e7fb62015-02-11 08:52:27 -08001235 return false;
1236 }
1237
1238 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin());
joshualitt99c7c072015-05-01 13:43:30 -07001239 this->joinBounds(that->bounds());
joshualitt76e7fb62015-02-11 08:52:27 -08001240 return true;
1241 }
1242
1243 GrColor color() const { return fBatch.fColor; }
1244 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
1245 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; }
1246 DIEllipseEdgeEffect::Mode mode() const { return fBatch.fMode; }
1247
1248 struct BatchTracker {
1249 GrColor fColor;
1250 DIEllipseEdgeEffect::Mode fMode;
1251 bool fUsesLocalCoords;
1252 bool fColorIgnored;
1253 bool fCoverageIgnored;
1254 };
1255
joshualitt76e7fb62015-02-11 08:52:27 -08001256 BatchTracker fBatch;
1257 SkSTArray<1, Geometry, true> fGeoData;
1258};
1259
joshualitt3e708c52015-04-30 13:49:27 -07001260static GrBatch* create_diellipse_batch(GrColor color,
1261 const SkMatrix& viewMatrix,
1262 bool useCoverageAA,
1263 const SkRect& ellipse,
joshualittd96a67b2015-05-05 14:09:05 -07001264 const SkStrokeRec& stroke) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001265 SkPoint center = SkPoint::Make(ellipse.centerX(), ellipse.centerY());
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +00001266 SkScalar xRadius = SkScalarHalf(ellipse.width());
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +00001267 SkScalar yRadius = SkScalarHalf(ellipse.height());
1268
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +00001269 SkStrokeRec::Style style = stroke.getStyle();
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +00001270 DIEllipseEdgeEffect::Mode mode = (SkStrokeRec::kStroke_Style == style) ?
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +00001271 DIEllipseEdgeEffect::kStroke :
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +00001272 (SkStrokeRec::kHairline_Style == style) ?
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +00001273 DIEllipseEdgeEffect::kHairline : DIEllipseEdgeEffect::kFill;
1274
1275 SkScalar innerXRadius = 0;
1276 SkScalar innerYRadius = 0;
1277 if (SkStrokeRec::kFill_Style != style && SkStrokeRec::kHairline_Style != style) {
1278 SkScalar strokeWidth = stroke.getWidth();
1279
1280 if (SkScalarNearlyZero(strokeWidth)) {
1281 strokeWidth = SK_ScalarHalf;
1282 } else {
1283 strokeWidth *= SK_ScalarHalf;
1284 }
1285
1286 // we only handle thick strokes for near-circular ellipses
1287 if (strokeWidth > SK_ScalarHalf &&
1288 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
joshualitt3e708c52015-04-30 13:49:27 -07001289 return NULL;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +00001290 }
1291
1292 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
1293 if (strokeWidth*(yRadius*yRadius) < (strokeWidth*strokeWidth)*xRadius ||
1294 strokeWidth*(xRadius*xRadius) < (strokeWidth*strokeWidth)*yRadius) {
joshualitt3e708c52015-04-30 13:49:27 -07001295 return NULL;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +00001296 }
1297
1298 // set inner radius (if needed)
1299 if (SkStrokeRec::kStroke_Style == style) {
1300 innerXRadius = xRadius - strokeWidth;
1301 innerYRadius = yRadius - strokeWidth;
1302 }
1303
1304 xRadius += strokeWidth;
1305 yRadius += strokeWidth;
1306 }
1307 if (DIEllipseEdgeEffect::kStroke == mode) {
1308 mode = (innerXRadius > 0 && innerYRadius > 0) ? DIEllipseEdgeEffect::kStroke :
1309 DIEllipseEdgeEffect::kFill;
1310 }
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +00001311
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +00001312 // This expands the outer rect so that after CTM we end up with a half-pixel border
joshualitt8059eb92014-12-29 15:10:07 -08001313 SkScalar a = viewMatrix[SkMatrix::kMScaleX];
1314 SkScalar b = viewMatrix[SkMatrix::kMSkewX];
1315 SkScalar c = viewMatrix[SkMatrix::kMSkewY];
1316 SkScalar d = viewMatrix[SkMatrix::kMScaleY];
reed80ea19c2015-05-12 10:37:34 -07001317 SkScalar geoDx = SK_ScalarHalf / SkScalarSqrt(a*a + c*c);
1318 SkScalar geoDy = SK_ScalarHalf / SkScalarSqrt(b*b + d*d);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +00001319
joshualitt76e7fb62015-02-11 08:52:27 -08001320 DIEllipseBatch::Geometry geometry;
1321 geometry.fViewMatrix = viewMatrix;
1322 geometry.fColor = color;
1323 geometry.fXRadius = xRadius;
1324 geometry.fYRadius = yRadius;
1325 geometry.fInnerXRadius = innerXRadius;
1326 geometry.fInnerYRadius = innerYRadius;
1327 geometry.fGeoDx = geoDx;
1328 geometry.fGeoDy = geoDy;
1329 geometry.fMode = mode;
joshualittd96a67b2015-05-05 14:09:05 -07001330 geometry.fBounds = SkRect::MakeLTRB(center.fX - xRadius - geoDx, center.fY - yRadius - geoDy,
1331 center.fX + xRadius + geoDx, center.fY + yRadius + geoDy);
egdaniel9ef1bb12015-04-20 12:28:57 -07001332
joshualittd96a67b2015-05-05 14:09:05 -07001333 SkRect devBounds = geometry.fBounds;
1334 viewMatrix.mapRect(&devBounds);
1335 return DIEllipseBatch::Create(geometry, devBounds);
joshualitt3e708c52015-04-30 13:49:27 -07001336}
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +00001337
robertphillipsea461502015-05-26 11:38:03 -07001338bool GrOvalRenderer::DrawDIEllipse(GrDrawTarget* target,
joshualittae3d63a2015-07-13 08:44:06 -07001339 const GrPipelineBuilder& pipelineBuilder,
joshualitt3e708c52015-04-30 13:49:27 -07001340 GrColor color,
1341 const SkMatrix& viewMatrix,
1342 bool useCoverageAA,
1343 const SkRect& ellipse,
1344 const SkStrokeRec& stroke) {
joshualitt3e708c52015-04-30 13:49:27 -07001345 SkAutoTUnref<GrBatch> batch(create_diellipse_batch(color, viewMatrix, useCoverageAA, ellipse,
joshualittd96a67b2015-05-05 14:09:05 -07001346 stroke));
joshualitt3e708c52015-04-30 13:49:27 -07001347 if (!batch) {
1348 return false;
1349 }
joshualittae3d63a2015-07-13 08:44:06 -07001350 target->drawBatch(pipelineBuilder, batch);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +00001351 return true;
1352}
1353
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001354///////////////////////////////////////////////////////////////////////////////
1355
1356static const uint16_t gRRectIndices[] = {
1357 // corners
1358 0, 1, 5, 0, 5, 4,
1359 2, 3, 7, 2, 7, 6,
1360 8, 9, 13, 8, 13, 12,
1361 10, 11, 15, 10, 15, 14,
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001362
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001363 // edges
1364 1, 2, 6, 1, 6, 5,
1365 4, 5, 9, 4, 9, 8,
1366 6, 7, 11, 6, 11, 10,
1367 9, 10, 14, 9, 14, 13,
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001368
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001369 // center
1370 // we place this at the end so that we can ignore these indices when rendering stroke-only
1371 5, 6, 10, 5, 10, 9
1372};
1373
joshualitt5ead6da2014-10-22 16:00:29 -07001374static const int kIndicesPerStrokeRRect = SK_ARRAY_COUNT(gRRectIndices) - 6;
1375static const int kIndicesPerRRect = SK_ARRAY_COUNT(gRRectIndices);
1376static const int kVertsPerRRect = 16;
1377static const int kNumRRectsInIndexBuffer = 256;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001378
bsalomoned0bcad2015-05-04 10:36:42 -07001379GR_DECLARE_STATIC_UNIQUE_KEY(gStrokeRRectOnlyIndexBufferKey);
1380GR_DECLARE_STATIC_UNIQUE_KEY(gRRectOnlyIndexBufferKey);
1381static const GrIndexBuffer* ref_rrect_index_buffer(bool strokeOnly,
1382 GrResourceProvider* resourceProvider) {
1383 GR_DEFINE_STATIC_UNIQUE_KEY(gStrokeRRectOnlyIndexBufferKey);
1384 GR_DEFINE_STATIC_UNIQUE_KEY(gRRectOnlyIndexBufferKey);
1385 if (strokeOnly) {
1386 return resourceProvider->refOrCreateInstancedIndexBuffer(
1387 gRRectIndices, kIndicesPerStrokeRRect, kNumRRectsInIndexBuffer, kVertsPerRRect,
1388 gStrokeRRectOnlyIndexBufferKey);
1389 } else {
1390 return resourceProvider->refOrCreateInstancedIndexBuffer(
1391 gRRectIndices, kIndicesPerRRect, kNumRRectsInIndexBuffer, kVertsPerRRect,
1392 gRRectOnlyIndexBufferKey);
1393
1394 }
1395}
1396
robertphillipsea461502015-05-26 11:38:03 -07001397bool GrOvalRenderer::DrawDRRect(GrDrawTarget* target,
joshualittae3d63a2015-07-13 08:44:06 -07001398 const GrPipelineBuilder& pipelineBuilder,
joshualitt2e3b3e32014-12-09 13:31:14 -08001399 GrColor color,
joshualitt8059eb92014-12-29 15:10:07 -08001400 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -08001401 bool useAA,
1402 const SkRRect& origOuter,
1403 const SkRRect& origInner) {
joshualittae3d63a2015-07-13 08:44:06 -07001404 bool applyAA = useAA && !pipelineBuilder.getRenderTarget()->isUnifiedMultisampled();
joshualitt4421a4c2015-07-13 09:36:41 -07001405 GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001406 if (!origInner.isEmpty()) {
1407 SkTCopyOnFirstWrite<SkRRect> inner(origInner);
joshualitt8059eb92014-12-29 15:10:07 -08001408 if (!viewMatrix.isIdentity()) {
1409 if (!origInner.transform(viewMatrix, inner.writable())) {
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001410 return false;
1411 }
1412 }
joshualittb0a8a372014-09-23 09:50:21 -07001413 GrPrimitiveEdgeType edgeType = applyAA ?
1414 kInverseFillAA_GrProcessorEdgeType :
1415 kInverseFillBW_GrProcessorEdgeType;
joshualitt2e3b3e32014-12-09 13:31:14 -08001416 // TODO this needs to be a geometry processor
joshualittb0a8a372014-09-23 09:50:21 -07001417 GrFragmentProcessor* fp = GrRRectEffect::Create(edgeType, *inner);
1418 if (NULL == fp) {
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001419 return false;
1420 }
joshualitt4421a4c2015-07-13 09:36:41 -07001421 arfps.set(&pipelineBuilder);
1422 arfps.addCoverageProcessor(fp)->unref();
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001423 }
1424
1425 SkStrokeRec fillRec(SkStrokeRec::kFill_InitStyle);
robertphillipsea461502015-05-26 11:38:03 -07001426 if (DrawRRect(target, pipelineBuilder, color, viewMatrix, useAA, origOuter, fillRec)) {
bsalomon8af05232014-06-03 06:34:58 -07001427 return true;
1428 }
1429
1430 SkASSERT(!origOuter.isEmpty());
1431 SkTCopyOnFirstWrite<SkRRect> outer(origOuter);
joshualitt8059eb92014-12-29 15:10:07 -08001432 if (!viewMatrix.isIdentity()) {
1433 if (!origOuter.transform(viewMatrix, outer.writable())) {
bsalomon8af05232014-06-03 06:34:58 -07001434 return false;
1435 }
1436 }
joshualittb0a8a372014-09-23 09:50:21 -07001437 GrPrimitiveEdgeType edgeType = applyAA ? kFillAA_GrProcessorEdgeType :
joshualitt76e7fb62015-02-11 08:52:27 -08001438 kFillBW_GrProcessorEdgeType;
joshualittb0a8a372014-09-23 09:50:21 -07001439 GrFragmentProcessor* effect = GrRRectEffect::Create(edgeType, *outer);
bsalomon8af05232014-06-03 06:34:58 -07001440 if (NULL == effect) {
1441 return false;
1442 }
joshualitt4421a4c2015-07-13 09:36:41 -07001443 if (!arfps.isSet()) {
1444 arfps.set(&pipelineBuilder);
bsalomon8af05232014-06-03 06:34:58 -07001445 }
joshualittd27f73e2014-12-29 07:43:36 -08001446
1447 SkMatrix invert;
joshualitt8059eb92014-12-29 15:10:07 -08001448 if (!viewMatrix.invert(&invert)) {
bsalomon8af05232014-06-03 06:34:58 -07001449 return false;
1450 }
joshualittd27f73e2014-12-29 07:43:36 -08001451
joshualitt4421a4c2015-07-13 09:36:41 -07001452 arfps.addCoverageProcessor(effect)->unref();
bsalomon8af05232014-06-03 06:34:58 -07001453 SkRect bounds = outer->getBounds();
1454 if (applyAA) {
1455 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1456 }
joshualittae3d63a2015-07-13 08:44:06 -07001457 target->drawBWRect(pipelineBuilder, color, SkMatrix::I(), bounds, NULL, &invert);
bsalomon8af05232014-06-03 06:34:58 -07001458 return true;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001459}
1460
joshualitt76e7fb62015-02-11 08:52:27 -08001461///////////////////////////////////////////////////////////////////////////////////////////////////
1462
1463class RRectCircleRendererBatch : public GrBatch {
1464public:
1465 struct Geometry {
1466 GrColor fColor;
1467 SkMatrix fViewMatrix;
1468 SkScalar fInnerRadius;
1469 SkScalar fOuterRadius;
1470 bool fStroke;
egdanielbc227142015-04-21 06:28:08 -07001471 SkRect fDevBounds;
joshualitt76e7fb62015-02-11 08:52:27 -08001472 };
1473
bsalomoned0bcad2015-05-04 10:36:42 -07001474 static GrBatch* Create(const Geometry& geometry) {
1475 return SkNEW_ARGS(RRectCircleRendererBatch, (geometry));
joshualitt76e7fb62015-02-11 08:52:27 -08001476 }
1477
mtklein36352bf2015-03-25 18:17:31 -07001478 const char* name() const override { return "RRectCircleBatch"; }
joshualitt76e7fb62015-02-11 08:52:27 -08001479
mtklein36352bf2015-03-25 18:17:31 -07001480 void getInvariantOutputColor(GrInitInvariantOutput* out) const override {
joshualitt76e7fb62015-02-11 08:52:27 -08001481 // When this is called on a batch, there is only one geometry bundle
1482 out->setKnownFourComponents(fGeoData[0].fColor);
1483 }
mtklein36352bf2015-03-25 18:17:31 -07001484 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override {
joshualitt76e7fb62015-02-11 08:52:27 -08001485 out->setUnknownSingleComponent();
1486 }
1487
mtklein36352bf2015-03-25 18:17:31 -07001488 void initBatchTracker(const GrPipelineInfo& init) override {
joshualitt76e7fb62015-02-11 08:52:27 -08001489 // Handle any color overrides
bsalomon7765a472015-07-08 11:26:37 -07001490 if (!init.readsColor()) {
joshualitt76e7fb62015-02-11 08:52:27 -08001491 fGeoData[0].fColor = GrColor_ILLEGAL;
joshualitt76e7fb62015-02-11 08:52:27 -08001492 }
bsalomon7765a472015-07-08 11:26:37 -07001493 init.getOverrideColorIfSet(&fGeoData[0].fColor);
joshualitt76e7fb62015-02-11 08:52:27 -08001494
1495 // setup batch properties
bsalomon7765a472015-07-08 11:26:37 -07001496 fBatch.fColorIgnored = !init.readsColor();
joshualitt76e7fb62015-02-11 08:52:27 -08001497 fBatch.fColor = fGeoData[0].fColor;
1498 fBatch.fStroke = fGeoData[0].fStroke;
bsalomon7765a472015-07-08 11:26:37 -07001499 fBatch.fUsesLocalCoords = init.readsLocalCoords();
1500 fBatch.fCoverageIgnored = !init.readsCoverage();
joshualitt76e7fb62015-02-11 08:52:27 -08001501 }
1502
mtklein36352bf2015-03-25 18:17:31 -07001503 void generateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline) override {
joshualitt76e7fb62015-02-11 08:52:27 -08001504 // reset to device coordinates
1505 SkMatrix invert;
1506 if (!this->viewMatrix().invert(&invert)) {
1507 SkDebugf("Failed to invert\n");
1508 return;
1509 }
1510
1511 // Setup geometry processor
1512 SkAutoTUnref<GrGeometryProcessor> gp(CircleEdgeEffect::Create(this->color(),
1513 this->stroke(),
joshualittb8c241a2015-05-19 08:23:30 -07001514 invert,
1515 this->usesLocalCoords()));
joshualitt76e7fb62015-02-11 08:52:27 -08001516
1517 batchTarget->initDraw(gp, pipeline);
1518
joshualitt76e7fb62015-02-11 08:52:27 -08001519 int instanceCount = fGeoData.count();
joshualitt76e7fb62015-02-11 08:52:27 -08001520 size_t vertexStride = gp->getVertexStride();
1521 SkASSERT(vertexStride == sizeof(CircleVertex));
1522
bsalomonb5238a72015-05-05 07:49:49 -07001523 // drop out the middle quad if we're stroked
1524 int indicesPerInstance = this->stroke() ? kIndicesPerStrokeRRect : kIndicesPerRRect;
bsalomoned0bcad2015-05-04 10:36:42 -07001525 SkAutoTUnref<const GrIndexBuffer> indexBuffer(
1526 ref_rrect_index_buffer(this->stroke(), batchTarget->resourceProvider()));
joshualitt76e7fb62015-02-11 08:52:27 -08001527
bsalomonb5238a72015-05-05 07:49:49 -07001528 InstancedHelper helper;
1529 CircleVertex* verts = reinterpret_cast<CircleVertex*>(helper.init(batchTarget,
1530 kTriangles_GrPrimitiveType, vertexStride, indexBuffer, kVertsPerRRect,
1531 indicesPerInstance, instanceCount));
1532 if (!verts || !indexBuffer) {
joshualitt4b31de82015-03-05 14:33:41 -08001533 SkDebugf("Could not allocate vertices\n");
1534 return;
1535 }
1536
joshualitt76e7fb62015-02-11 08:52:27 -08001537 for (int i = 0; i < instanceCount; i++) {
1538 Geometry& args = fGeoData[i];
1539
1540 SkScalar outerRadius = args.fOuterRadius;
1541
egdanielbc227142015-04-21 06:28:08 -07001542 const SkRect& bounds = args.fDevBounds;
joshualitt76e7fb62015-02-11 08:52:27 -08001543
1544 SkScalar yCoords[4] = {
1545 bounds.fTop,
1546 bounds.fTop + outerRadius,
1547 bounds.fBottom - outerRadius,
1548 bounds.fBottom
1549 };
1550
1551 SkScalar yOuterRadii[4] = {-1, 0, 0, 1 };
1552 // The inner radius in the vertex data must be specified in normalized space.
1553 SkScalar innerRadius = args.fInnerRadius / args.fOuterRadius;
1554 for (int i = 0; i < 4; ++i) {
1555 verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]);
1556 verts->fOffset = SkPoint::Make(-1, yOuterRadii[i]);
1557 verts->fOuterRadius = outerRadius;
1558 verts->fInnerRadius = innerRadius;
1559 verts++;
1560
1561 verts->fPos = SkPoint::Make(bounds.fLeft + outerRadius, yCoords[i]);
1562 verts->fOffset = SkPoint::Make(0, yOuterRadii[i]);
1563 verts->fOuterRadius = outerRadius;
1564 verts->fInnerRadius = innerRadius;
1565 verts++;
1566
1567 verts->fPos = SkPoint::Make(bounds.fRight - outerRadius, yCoords[i]);
1568 verts->fOffset = SkPoint::Make(0, yOuterRadii[i]);
1569 verts->fOuterRadius = outerRadius;
1570 verts->fInnerRadius = innerRadius;
1571 verts++;
1572
1573 verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]);
1574 verts->fOffset = SkPoint::Make(1, yOuterRadii[i]);
1575 verts->fOuterRadius = outerRadius;
1576 verts->fInnerRadius = innerRadius;
1577 verts++;
1578 }
1579 }
1580
bsalomone64eb572015-05-07 11:35:55 -07001581 helper.issueDraw(batchTarget);
joshualitt76e7fb62015-02-11 08:52:27 -08001582 }
1583
1584 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
1585
1586private:
bsalomoned0bcad2015-05-04 10:36:42 -07001587 RRectCircleRendererBatch(const Geometry& geometry) {
joshualitt76e7fb62015-02-11 08:52:27 -08001588 this->initClassID<RRectCircleRendererBatch>();
1589 fGeoData.push_back(geometry);
joshualitt99c7c072015-05-01 13:43:30 -07001590
1591 this->setBounds(geometry.fDevBounds);
joshualitt76e7fb62015-02-11 08:52:27 -08001592 }
1593
mtklein36352bf2015-03-25 18:17:31 -07001594 bool onCombineIfPossible(GrBatch* t) override {
joshualitt8cab9a72015-07-16 09:13:50 -07001595 if (!this->pipeline()->isEqual(*t->pipeline())) {
1596 return false;
1597 }
1598
joshualitt76e7fb62015-02-11 08:52:27 -08001599 RRectCircleRendererBatch* that = t->cast<RRectCircleRendererBatch>();
1600
1601 // TODO use vertex color to avoid breaking batches
1602 if (this->color() != that->color()) {
1603 return false;
1604 }
1605
1606 if (this->stroke() != that->stroke()) {
1607 return false;
1608 }
1609
1610 SkASSERT(this->usesLocalCoords() == that->usesLocalCoords());
1611 if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
1612 return false;
1613 }
1614
1615 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin());
joshualitt99c7c072015-05-01 13:43:30 -07001616 this->joinBounds(that->bounds());
joshualitt76e7fb62015-02-11 08:52:27 -08001617 return true;
1618 }
1619
1620 GrColor color() const { return fBatch.fColor; }
1621 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
1622 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; }
1623 bool stroke() const { return fBatch.fStroke; }
1624
1625 struct BatchTracker {
1626 GrColor fColor;
1627 bool fStroke;
1628 bool fUsesLocalCoords;
1629 bool fColorIgnored;
1630 bool fCoverageIgnored;
1631 };
1632
joshualitt76e7fb62015-02-11 08:52:27 -08001633 BatchTracker fBatch;
1634 SkSTArray<1, Geometry, true> fGeoData;
joshualitt76e7fb62015-02-11 08:52:27 -08001635};
1636
1637class RRectEllipseRendererBatch : public GrBatch {
1638public:
1639 struct Geometry {
1640 GrColor fColor;
1641 SkMatrix fViewMatrix;
1642 SkScalar fXRadius;
1643 SkScalar fYRadius;
1644 SkScalar fInnerXRadius;
1645 SkScalar fInnerYRadius;
1646 bool fStroke;
egdanielbc227142015-04-21 06:28:08 -07001647 SkRect fDevBounds;
joshualitt76e7fb62015-02-11 08:52:27 -08001648 };
1649
bsalomoned0bcad2015-05-04 10:36:42 -07001650 static GrBatch* Create(const Geometry& geometry) {
1651 return SkNEW_ARGS(RRectEllipseRendererBatch, (geometry));
joshualitt76e7fb62015-02-11 08:52:27 -08001652 }
1653
mtklein36352bf2015-03-25 18:17:31 -07001654 const char* name() const override { return "RRectEllipseRendererBatch"; }
joshualitt76e7fb62015-02-11 08:52:27 -08001655
mtklein36352bf2015-03-25 18:17:31 -07001656 void getInvariantOutputColor(GrInitInvariantOutput* out) const override {
joshualitt76e7fb62015-02-11 08:52:27 -08001657 // When this is called on a batch, there is only one geometry bundle
1658 out->setKnownFourComponents(fGeoData[0].fColor);
1659 }
mtklein36352bf2015-03-25 18:17:31 -07001660 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override {
joshualitt76e7fb62015-02-11 08:52:27 -08001661 out->setUnknownSingleComponent();
1662 }
1663
mtklein36352bf2015-03-25 18:17:31 -07001664 void initBatchTracker(const GrPipelineInfo& init) override {
joshualitt76e7fb62015-02-11 08:52:27 -08001665 // Handle any color overrides
bsalomon7765a472015-07-08 11:26:37 -07001666 if (!init.readsColor()) {
joshualitt76e7fb62015-02-11 08:52:27 -08001667 fGeoData[0].fColor = GrColor_ILLEGAL;
joshualitt76e7fb62015-02-11 08:52:27 -08001668 }
bsalomon7765a472015-07-08 11:26:37 -07001669 init.getOverrideColorIfSet(&fGeoData[0].fColor);
joshualitt76e7fb62015-02-11 08:52:27 -08001670
1671 // setup batch properties
bsalomon7765a472015-07-08 11:26:37 -07001672 fBatch.fColorIgnored = !init.readsColor();
joshualitt76e7fb62015-02-11 08:52:27 -08001673 fBatch.fColor = fGeoData[0].fColor;
1674 fBatch.fStroke = fGeoData[0].fStroke;
bsalomon7765a472015-07-08 11:26:37 -07001675 fBatch.fUsesLocalCoords = init.readsLocalCoords();
1676 fBatch.fCoverageIgnored = !init.readsCoverage();
joshualitt76e7fb62015-02-11 08:52:27 -08001677 }
1678
mtklein36352bf2015-03-25 18:17:31 -07001679 void generateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline) override {
joshualitt76e7fb62015-02-11 08:52:27 -08001680 // reset to device coordinates
1681 SkMatrix invert;
1682 if (!this->viewMatrix().invert(&invert)) {
1683 SkDebugf("Failed to invert\n");
1684 return;
1685 }
1686
1687 // Setup geometry processor
1688 SkAutoTUnref<GrGeometryProcessor> gp(EllipseEdgeEffect::Create(this->color(),
1689 this->stroke(),
joshualittb8c241a2015-05-19 08:23:30 -07001690 invert,
1691 this->usesLocalCoords()));
joshualitt76e7fb62015-02-11 08:52:27 -08001692
1693 batchTarget->initDraw(gp, pipeline);
1694
joshualitt76e7fb62015-02-11 08:52:27 -08001695 int instanceCount = fGeoData.count();
joshualitt76e7fb62015-02-11 08:52:27 -08001696 size_t vertexStride = gp->getVertexStride();
1697 SkASSERT(vertexStride == sizeof(EllipseVertex));
1698
bsalomonb5238a72015-05-05 07:49:49 -07001699 // drop out the middle quad if we're stroked
1700 int indicesPerInstance = this->stroke() ? kIndicesPerStrokeRRect : kIndicesPerRRect;
bsalomoned0bcad2015-05-04 10:36:42 -07001701 SkAutoTUnref<const GrIndexBuffer> indexBuffer(
1702 ref_rrect_index_buffer(this->stroke(), batchTarget->resourceProvider()));
joshualitt76e7fb62015-02-11 08:52:27 -08001703
bsalomonb5238a72015-05-05 07:49:49 -07001704 InstancedHelper helper;
1705 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(
1706 helper.init(batchTarget, kTriangles_GrPrimitiveType, vertexStride, indexBuffer,
1707 kVertsPerRRect, indicesPerInstance, instanceCount));
1708 if (!verts || !indexBuffer) {
joshualitt4b31de82015-03-05 14:33:41 -08001709 SkDebugf("Could not allocate vertices\n");
1710 return;
1711 }
1712
joshualitt76e7fb62015-02-11 08:52:27 -08001713 for (int i = 0; i < instanceCount; i++) {
1714 Geometry& args = fGeoData[i];
1715
1716 // Compute the reciprocals of the radii here to save time in the shader
1717 SkScalar xRadRecip = SkScalarInvert(args.fXRadius);
1718 SkScalar yRadRecip = SkScalarInvert(args.fYRadius);
1719 SkScalar xInnerRadRecip = SkScalarInvert(args.fInnerXRadius);
1720 SkScalar yInnerRadRecip = SkScalarInvert(args.fInnerYRadius);
1721
1722 // Extend the radii out half a pixel to antialias.
1723 SkScalar xOuterRadius = args.fXRadius + SK_ScalarHalf;
1724 SkScalar yOuterRadius = args.fYRadius + SK_ScalarHalf;
1725
egdanielbc227142015-04-21 06:28:08 -07001726 const SkRect& bounds = args.fDevBounds;
joshualitt76e7fb62015-02-11 08:52:27 -08001727
1728 SkScalar yCoords[4] = {
1729 bounds.fTop,
1730 bounds.fTop + yOuterRadius,
1731 bounds.fBottom - yOuterRadius,
1732 bounds.fBottom
1733 };
1734 SkScalar yOuterOffsets[4] = {
1735 yOuterRadius,
1736 SK_ScalarNearlyZero, // we're using inversesqrt() in shader, so can't be exactly 0
1737 SK_ScalarNearlyZero,
1738 yOuterRadius
1739 };
1740
1741 for (int i = 0; i < 4; ++i) {
1742 verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]);
1743 verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]);
1744 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1745 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
1746 verts++;
1747
1748 verts->fPos = SkPoint::Make(bounds.fLeft + xOuterRadius, yCoords[i]);
1749 verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]);
1750 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1751 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
1752 verts++;
1753
1754 verts->fPos = SkPoint::Make(bounds.fRight - xOuterRadius, yCoords[i]);
1755 verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]);
1756 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1757 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
1758 verts++;
1759
1760 verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]);
1761 verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]);
1762 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1763 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
1764 verts++;
1765 }
1766 }
bsalomone64eb572015-05-07 11:35:55 -07001767 helper.issueDraw(batchTarget);
joshualitt76e7fb62015-02-11 08:52:27 -08001768 }
1769
1770 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
1771
1772private:
bsalomoned0bcad2015-05-04 10:36:42 -07001773 RRectEllipseRendererBatch(const Geometry& geometry) {
joshualitt76e7fb62015-02-11 08:52:27 -08001774 this->initClassID<RRectEllipseRendererBatch>();
1775 fGeoData.push_back(geometry);
joshualitt99c7c072015-05-01 13:43:30 -07001776
1777 this->setBounds(geometry.fDevBounds);
joshualitt76e7fb62015-02-11 08:52:27 -08001778 }
1779
mtklein36352bf2015-03-25 18:17:31 -07001780 bool onCombineIfPossible(GrBatch* t) override {
joshualitt8cab9a72015-07-16 09:13:50 -07001781 if (!this->pipeline()->isEqual(*t->pipeline())) {
1782 return false;
1783 }
1784
joshualitt76e7fb62015-02-11 08:52:27 -08001785 RRectEllipseRendererBatch* that = t->cast<RRectEllipseRendererBatch>();
1786
1787 // TODO use vertex color to avoid breaking batches
1788 if (this->color() != that->color()) {
1789 return false;
1790 }
1791
1792 if (this->stroke() != that->stroke()) {
1793 return false;
1794 }
1795
1796 SkASSERT(this->usesLocalCoords() == that->usesLocalCoords());
1797 if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
1798 return false;
1799 }
1800
1801 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin());
joshualitt99c7c072015-05-01 13:43:30 -07001802 this->joinBounds(that->bounds());
joshualitt76e7fb62015-02-11 08:52:27 -08001803 return true;
1804 }
1805
1806 GrColor color() const { return fBatch.fColor; }
1807 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
1808 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; }
1809 bool stroke() const { return fBatch.fStroke; }
1810
1811 struct BatchTracker {
1812 GrColor fColor;
1813 bool fStroke;
1814 bool fUsesLocalCoords;
1815 bool fColorIgnored;
1816 bool fCoverageIgnored;
1817 };
1818
joshualitt76e7fb62015-02-11 08:52:27 -08001819 BatchTracker fBatch;
1820 SkSTArray<1, Geometry, true> fGeoData;
joshualitt76e7fb62015-02-11 08:52:27 -08001821};
1822
joshualitt3e708c52015-04-30 13:49:27 -07001823static GrBatch* create_rrect_batch(GrColor color,
1824 const SkMatrix& viewMatrix,
1825 const SkRRect& rrect,
joshualittd96a67b2015-05-05 14:09:05 -07001826 const SkStrokeRec& stroke) {
joshualitt3e708c52015-04-30 13:49:27 -07001827 SkASSERT(viewMatrix.rectStaysRect());
1828 SkASSERT(rrect.isSimple());
1829 SkASSERT(!rrect.isOval());
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001830
joshualitt3e708c52015-04-30 13:49:27 -07001831 // RRect batchs only handle simple, but not too simple, rrects
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001832 // do any matrix crunching before we reset the draw state for device coords
1833 const SkRect& rrectBounds = rrect.getBounds();
joshualittd96a67b2015-05-05 14:09:05 -07001834 SkRect bounds;
1835 viewMatrix.mapRect(&bounds, rrectBounds);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001836
1837 SkVector radii = rrect.getSimpleRadii();
joshualitt8059eb92014-12-29 15:10:07 -08001838 SkScalar xRadius = SkScalarAbs(viewMatrix[SkMatrix::kMScaleX]*radii.fX +
1839 viewMatrix[SkMatrix::kMSkewY]*radii.fY);
1840 SkScalar yRadius = SkScalarAbs(viewMatrix[SkMatrix::kMSkewX]*radii.fX +
1841 viewMatrix[SkMatrix::kMScaleY]*radii.fY);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001842
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001843 SkStrokeRec::Style style = stroke.getStyle();
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001844
1845 // do (potentially) anisotropic mapping of stroke
1846 SkVector scaledStroke;
1847 SkScalar strokeWidth = stroke.getWidth();
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001848
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001849 bool isStrokeOnly = SkStrokeRec::kStroke_Style == style ||
1850 SkStrokeRec::kHairline_Style == style;
1851 bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == style;
1852
1853 if (hasStroke) {
1854 if (SkStrokeRec::kHairline_Style == style) {
1855 scaledStroke.set(1, 1);
1856 } else {
joshualitt8059eb92014-12-29 15:10:07 -08001857 scaledStroke.fX = SkScalarAbs(strokeWidth*(viewMatrix[SkMatrix::kMScaleX] +
1858 viewMatrix[SkMatrix::kMSkewY]));
1859 scaledStroke.fY = SkScalarAbs(strokeWidth*(viewMatrix[SkMatrix::kMSkewX] +
1860 viewMatrix[SkMatrix::kMScaleY]));
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001861 }
1862
1863 // if half of strokewidth is greater than radius, we don't handle that right now
1864 if (SK_ScalarHalf*scaledStroke.fX > xRadius || SK_ScalarHalf*scaledStroke.fY > yRadius) {
joshualitt3e708c52015-04-30 13:49:27 -07001865 return NULL;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001866 }
1867 }
1868
1869 // The way the effect interpolates the offset-to-ellipse/circle-center attribute only works on
1870 // the interior of the rrect if the radii are >= 0.5. Otherwise, the inner rect of the nine-
1871 // patch will have fractional coverage. This only matters when the interior is actually filled.
1872 // We could consider falling back to rect rendering here, since a tiny radius is
1873 // indistinguishable from a square corner.
1874 if (!isStrokeOnly && (SK_ScalarHalf > xRadius || SK_ScalarHalf > yRadius)) {
joshualitt3e708c52015-04-30 13:49:27 -07001875 return NULL;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001876 }
1877
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001878 // if the corners are circles, use the circle renderer
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001879 if ((!hasStroke || scaledStroke.fX == scaledStroke.fY) && xRadius == yRadius) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001880 SkScalar innerRadius = 0.0f;
1881 SkScalar outerRadius = xRadius;
1882 SkScalar halfWidth = 0;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001883 if (hasStroke) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001884 if (SkScalarNearlyZero(scaledStroke.fX)) {
1885 halfWidth = SK_ScalarHalf;
1886 } else {
1887 halfWidth = SkScalarHalf(scaledStroke.fX);
1888 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001889
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001890 if (isStrokeOnly) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001891 innerRadius = xRadius - halfWidth;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001892 }
1893 outerRadius += halfWidth;
joshualittd96a67b2015-05-05 14:09:05 -07001894 bounds.outset(halfWidth, halfWidth);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001895 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001896
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001897 isStrokeOnly = (isStrokeOnly && innerRadius >= 0);
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +00001898
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001899 // The radii are outset for two reasons. First, it allows the shader to simply perform
bsalomonce1c8862014-12-15 07:11:22 -08001900 // simpler computation because the computed alpha is zero, rather than 50%, at the radius.
1901 // Second, the outer radius is used to compute the verts of the bounding box that is
1902 // rendered and the outset ensures the box will cover all partially covered by the rrect
1903 // corners.
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001904 outerRadius += SK_ScalarHalf;
1905 innerRadius -= SK_ScalarHalf;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001906
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001907 // Expand the rect so all the pixels will be captured.
joshualittd96a67b2015-05-05 14:09:05 -07001908 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001909
joshualitt76e7fb62015-02-11 08:52:27 -08001910 RRectCircleRendererBatch::Geometry geometry;
1911 geometry.fViewMatrix = viewMatrix;
1912 geometry.fColor = color;
1913 geometry.fInnerRadius = innerRadius;
1914 geometry.fOuterRadius = outerRadius;
1915 geometry.fStroke = isStrokeOnly;
joshualittd96a67b2015-05-05 14:09:05 -07001916 geometry.fDevBounds = bounds;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001917
bsalomoned0bcad2015-05-04 10:36:42 -07001918 return RRectCircleRendererBatch::Create(geometry);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001919 // otherwise we use the ellipse renderer
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001920 } else {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001921 SkScalar innerXRadius = 0.0f;
1922 SkScalar innerYRadius = 0.0f;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001923 if (hasStroke) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001924 if (SkScalarNearlyZero(scaledStroke.length())) {
1925 scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf);
1926 } else {
1927 scaledStroke.scale(SK_ScalarHalf);
1928 }
1929
1930 // we only handle thick strokes for near-circular ellipses
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +00001931 if (scaledStroke.length() > SK_ScalarHalf &&
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001932 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
joshualitt3e708c52015-04-30 13:49:27 -07001933 return NULL;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001934 }
1935
1936 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
1937 if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius ||
1938 scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) {
joshualitt3e708c52015-04-30 13:49:27 -07001939 return NULL;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001940 }
1941
1942 // this is legit only if scale & translation (which should be the case at the moment)
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001943 if (isStrokeOnly) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001944 innerXRadius = xRadius - scaledStroke.fX;
1945 innerYRadius = yRadius - scaledStroke.fY;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001946 }
1947
1948 xRadius += scaledStroke.fX;
1949 yRadius += scaledStroke.fY;
joshualittd96a67b2015-05-05 14:09:05 -07001950 bounds.outset(scaledStroke.fX, scaledStroke.fY);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001951 }
jvanverth@google.come3647412013-05-08 15:31:43 +00001952
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001953 isStrokeOnly = (isStrokeOnly && innerXRadius >= 0 && innerYRadius >= 0);
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +00001954
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001955 // Expand the rect so all the pixels will be captured.
joshualittd96a67b2015-05-05 14:09:05 -07001956 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001957
joshualitt76e7fb62015-02-11 08:52:27 -08001958 RRectEllipseRendererBatch::Geometry geometry;
1959 geometry.fViewMatrix = viewMatrix;
1960 geometry.fColor = color;
1961 geometry.fXRadius = xRadius;
1962 geometry.fYRadius = yRadius;
1963 geometry.fInnerXRadius = innerXRadius;
1964 geometry.fInnerYRadius = innerYRadius;
1965 geometry.fStroke = isStrokeOnly;
joshualittd96a67b2015-05-05 14:09:05 -07001966 geometry.fDevBounds = bounds;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001967
bsalomoned0bcad2015-05-04 10:36:42 -07001968 return RRectEllipseRendererBatch::Create(geometry);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001969 }
joshualitt3e708c52015-04-30 13:49:27 -07001970}
1971
robertphillipsea461502015-05-26 11:38:03 -07001972bool GrOvalRenderer::DrawRRect(GrDrawTarget* target,
joshualittae3d63a2015-07-13 08:44:06 -07001973 const GrPipelineBuilder& pipelineBuilder,
joshualitt3e708c52015-04-30 13:49:27 -07001974 GrColor color,
1975 const SkMatrix& viewMatrix,
1976 bool useAA,
1977 const SkRRect& rrect,
1978 const SkStrokeRec& stroke) {
1979 if (rrect.isOval()) {
robertphillipsea461502015-05-26 11:38:03 -07001980 return DrawOval(target, pipelineBuilder, color, viewMatrix, useAA, rrect.getBounds(),
1981 stroke);
joshualitt3e708c52015-04-30 13:49:27 -07001982 }
1983
joshualittae3d63a2015-07-13 08:44:06 -07001984 bool useCoverageAA = useAA && !pipelineBuilder.getRenderTarget()->isUnifiedMultisampled();
joshualitt3e708c52015-04-30 13:49:27 -07001985
1986 // only anti-aliased rrects for now
1987 if (!useCoverageAA) {
1988 return false;
1989 }
1990
1991 if (!viewMatrix.rectStaysRect() || !rrect.isSimple()) {
1992 return false;
1993 }
1994
joshualittd96a67b2015-05-05 14:09:05 -07001995 SkAutoTUnref<GrBatch> batch(create_rrect_batch(color, viewMatrix, rrect, stroke));
joshualitt3e708c52015-04-30 13:49:27 -07001996 if (!batch) {
1997 return false;
1998 }
1999
joshualittae3d63a2015-07-13 08:44:06 -07002000 target->drawBatch(pipelineBuilder, batch);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00002001 return true;
2002}
joshualitt3e708c52015-04-30 13:49:27 -07002003
2004///////////////////////////////////////////////////////////////////////////////////////////////////
2005
2006#ifdef GR_TEST_UTILS
2007
joshualitt3e708c52015-04-30 13:49:27 -07002008BATCH_TEST_DEFINE(CircleBatch) {
2009 SkMatrix viewMatrix = GrTest::TestMatrix(random);
2010 GrColor color = GrRandomColor(random);
2011 bool useCoverageAA = random->nextBool();
joshualitt6c891102015-05-13 08:51:49 -07002012 SkRect circle = GrTest::TestSquare(random);
joshualitt21279c72015-05-11 07:21:37 -07002013 return create_circle_batch(color, viewMatrix, useCoverageAA, circle,
2014 GrTest::TestStrokeRec(random));
joshualitt3e708c52015-04-30 13:49:27 -07002015}
2016
2017BATCH_TEST_DEFINE(EllipseBatch) {
2018 SkMatrix viewMatrix = GrTest::TestMatrixRectStaysRect(random);
2019 GrColor color = GrRandomColor(random);
joshualitt6c891102015-05-13 08:51:49 -07002020 SkRect ellipse = GrTest::TestSquare(random);
2021 return create_ellipse_batch(color, viewMatrix, true, ellipse,
joshualitt21279c72015-05-11 07:21:37 -07002022 GrTest::TestStrokeRec(random));
joshualitt3e708c52015-04-30 13:49:27 -07002023}
2024
2025BATCH_TEST_DEFINE(DIEllipseBatch) {
2026 SkMatrix viewMatrix = GrTest::TestMatrix(random);
2027 GrColor color = GrRandomColor(random);
2028 bool useCoverageAA = random->nextBool();
joshualitt6c891102015-05-13 08:51:49 -07002029 SkRect ellipse = GrTest::TestSquare(random);
joshualitt3e708c52015-04-30 13:49:27 -07002030 return create_diellipse_batch(color, viewMatrix, useCoverageAA, ellipse,
joshualitt21279c72015-05-11 07:21:37 -07002031 GrTest::TestStrokeRec(random));
joshualitt3e708c52015-04-30 13:49:27 -07002032}
2033
2034BATCH_TEST_DEFINE(RRectBatch) {
2035 SkMatrix viewMatrix = GrTest::TestMatrixRectStaysRect(random);
2036 GrColor color = GrRandomColor(random);
2037 const SkRRect& rrect = GrTest::TestRRectSimple(random);
joshualitt21279c72015-05-11 07:21:37 -07002038 return create_rrect_batch(color, viewMatrix, rrect, GrTest::TestStrokeRec(random));
joshualitt3e708c52015-04-30 13:49:27 -07002039}
2040
2041#endif