blob: 89f6f5b15dee0d263a78adb6ed5713ee121e3d57 [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 {
joshualitt76e7fb62015-02-11 08:52:27 -0800737 CircleBatch* that = t->cast<CircleBatch>();
738
739 // TODO use vertex color to avoid breaking batches
740 if (this->color() != that->color()) {
741 return false;
742 }
743
744 if (this->stroke() != that->stroke()) {
745 return false;
746 }
747
748 SkASSERT(this->usesLocalCoords() == that->usesLocalCoords());
749 if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
750 return false;
751 }
752
753 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin());
joshualitt99c7c072015-05-01 13:43:30 -0700754 this->joinBounds(that->bounds());
joshualitt76e7fb62015-02-11 08:52:27 -0800755 return true;
756 }
757
758 GrColor color() const { return fBatch.fColor; }
759 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
760 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; }
761 bool stroke() const { return fBatch.fStroke; }
762
763 struct BatchTracker {
764 GrColor fColor;
765 bool fStroke;
766 bool fUsesLocalCoords;
767 bool fColorIgnored;
768 bool fCoverageIgnored;
769 };
770
joshualitt76e7fb62015-02-11 08:52:27 -0800771 BatchTracker fBatch;
772 SkSTArray<1, Geometry, true> fGeoData;
773};
774
joshualitt3e708c52015-04-30 13:49:27 -0700775static GrBatch* create_circle_batch(GrColor color,
776 const SkMatrix& viewMatrix,
777 bool useCoverageAA,
778 const SkRect& circle,
joshualittd96a67b2015-05-05 14:09:05 -0700779 const SkStrokeRec& stroke) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000780 SkPoint center = SkPoint::Make(circle.centerX(), circle.centerY());
joshualitt8059eb92014-12-29 15:10:07 -0800781 viewMatrix.mapPoints(&center, 1);
782 SkScalar radius = viewMatrix.mapRadius(SkScalarHalf(circle.width()));
783 SkScalar strokeWidth = viewMatrix.mapRadius(stroke.getWidth());
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000784
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000785 SkStrokeRec::Style style = stroke.getStyle();
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000786 bool isStrokeOnly = SkStrokeRec::kStroke_Style == style ||
787 SkStrokeRec::kHairline_Style == style;
788 bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == style;
skia.committer@gmail.com7e328512013-03-23 07:01:28 +0000789
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000790 SkScalar innerRadius = 0.0f;
791 SkScalar outerRadius = radius;
792 SkScalar halfWidth = 0;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000793 if (hasStroke) {
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000794 if (SkScalarNearlyZero(strokeWidth)) {
795 halfWidth = SK_ScalarHalf;
796 } else {
797 halfWidth = SkScalarHalf(strokeWidth);
798 }
799
800 outerRadius += halfWidth;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000801 if (isStrokeOnly) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +0000802 innerRadius = radius - halfWidth;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000803 }
804 }
805
bsalomonce1c8862014-12-15 07:11:22 -0800806 // The radii are outset for two reasons. First, it allows the shader to simply perform simpler
807 // computation because the computed alpha is zero, rather than 50%, at the radius.
808 // Second, the outer radius is used to compute the verts of the bounding box that is rendered
809 // and the outset ensures the box will cover all partially covered by the circle.
bsalomon@google.com58e30fe2013-04-01 19:01:20 +0000810 outerRadius += SK_ScalarHalf;
811 innerRadius -= SK_ScalarHalf;
812
joshualitt76e7fb62015-02-11 08:52:27 -0800813 CircleBatch::Geometry geometry;
814 geometry.fViewMatrix = viewMatrix;
815 geometry.fColor = color;
816 geometry.fInnerRadius = innerRadius;
817 geometry.fOuterRadius = outerRadius;
818 geometry.fStroke = isStrokeOnly && innerRadius > 0;
joshualittd96a67b2015-05-05 14:09:05 -0700819 geometry.fDevBounds = SkRect::MakeLTRB(center.fX - outerRadius, center.fY - outerRadius,
820 center.fX + outerRadius, center.fY + outerRadius);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +0000821
joshualitt3e708c52015-04-30 13:49:27 -0700822 return CircleBatch::Create(geometry);
823}
824
robertphillipsea461502015-05-26 11:38:03 -0700825void GrOvalRenderer::DrawCircle(GrDrawTarget* target,
joshualittae3d63a2015-07-13 08:44:06 -0700826 const GrPipelineBuilder& pipelineBuilder,
joshualitt3e708c52015-04-30 13:49:27 -0700827 GrColor color,
828 const SkMatrix& viewMatrix,
829 bool useCoverageAA,
830 const SkRect& circle,
831 const SkStrokeRec& stroke) {
joshualitt3e708c52015-04-30 13:49:27 -0700832 SkAutoTUnref<GrBatch> batch(create_circle_batch(color, viewMatrix, useCoverageAA, circle,
joshualittd96a67b2015-05-05 14:09:05 -0700833 stroke));
joshualittae3d63a2015-07-13 08:44:06 -0700834 target->drawBatch(pipelineBuilder, batch);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000835}
836
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000837///////////////////////////////////////////////////////////////////////////////
838
joshualitt76e7fb62015-02-11 08:52:27 -0800839class EllipseBatch : public GrBatch {
840public:
841 struct Geometry {
842 GrColor fColor;
843 SkMatrix fViewMatrix;
844 SkScalar fXRadius;
845 SkScalar fYRadius;
846 SkScalar fInnerXRadius;
847 SkScalar fInnerYRadius;
848 bool fStroke;
egdanielbc227142015-04-21 06:28:08 -0700849 SkRect fDevBounds;
joshualitt76e7fb62015-02-11 08:52:27 -0800850 };
851
852 static GrBatch* Create(const Geometry& geometry) {
853 return SkNEW_ARGS(EllipseBatch, (geometry));
854 }
855
mtklein36352bf2015-03-25 18:17:31 -0700856 const char* name() const override { return "EllipseBatch"; }
joshualitt76e7fb62015-02-11 08:52:27 -0800857
mtklein36352bf2015-03-25 18:17:31 -0700858 void getInvariantOutputColor(GrInitInvariantOutput* out) const override {
joshualitt76e7fb62015-02-11 08:52:27 -0800859 // When this is called on a batch, there is only one geometry bundle
860 out->setKnownFourComponents(fGeoData[0].fColor);
861 }
mtklein36352bf2015-03-25 18:17:31 -0700862 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override {
joshualitt76e7fb62015-02-11 08:52:27 -0800863 out->setUnknownSingleComponent();
864 }
865
mtklein36352bf2015-03-25 18:17:31 -0700866 void initBatchTracker(const GrPipelineInfo& init) override {
joshualitt76e7fb62015-02-11 08:52:27 -0800867 // Handle any color overrides
bsalomon7765a472015-07-08 11:26:37 -0700868 if (!init.readsCoverage()) {
joshualitt76e7fb62015-02-11 08:52:27 -0800869 fGeoData[0].fColor = GrColor_ILLEGAL;
joshualitt76e7fb62015-02-11 08:52:27 -0800870 }
bsalomon7765a472015-07-08 11:26:37 -0700871 init.getOverrideColorIfSet(&fGeoData[0].fColor);
joshualitt76e7fb62015-02-11 08:52:27 -0800872
873 // setup batch properties
bsalomon7765a472015-07-08 11:26:37 -0700874 fBatch.fColorIgnored = !init.readsColor();
joshualitt76e7fb62015-02-11 08:52:27 -0800875 fBatch.fColor = fGeoData[0].fColor;
876 fBatch.fStroke = fGeoData[0].fStroke;
bsalomon7765a472015-07-08 11:26:37 -0700877 fBatch.fUsesLocalCoords = init.readsLocalCoords();
878 fBatch.fCoverageIgnored = !init.readsCoverage();
joshualitt76e7fb62015-02-11 08:52:27 -0800879 }
880
mtklein36352bf2015-03-25 18:17:31 -0700881 void generateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline) override {
joshualitt76e7fb62015-02-11 08:52:27 -0800882 SkMatrix invert;
883 if (!this->viewMatrix().invert(&invert)) {
884 return;
885 }
886
887 // Setup geometry processor
888 SkAutoTUnref<GrGeometryProcessor> gp(EllipseEdgeEffect::Create(this->color(),
889 this->stroke(),
joshualittb8c241a2015-05-19 08:23:30 -0700890 invert,
891 this->usesLocalCoords()));
joshualitt76e7fb62015-02-11 08:52:27 -0800892
893 batchTarget->initDraw(gp, pipeline);
894
joshualitt76e7fb62015-02-11 08:52:27 -0800895 int instanceCount = fGeoData.count();
bsalomonb5238a72015-05-05 07:49:49 -0700896 QuadHelper helper;
joshualitt76e7fb62015-02-11 08:52:27 -0800897 size_t vertexStride = gp->getVertexStride();
joshualitt19e00582015-02-11 17:36:30 -0800898 SkASSERT(vertexStride == sizeof(EllipseVertex));
bsalomonb5238a72015-05-05 07:49:49 -0700899 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(
900 helper.init(batchTarget, vertexStride, instanceCount));
901 if (!verts) {
joshualitt4b31de82015-03-05 14:33:41 -0800902 return;
903 }
904
bsalomon8415abe2015-05-04 11:41:41 -0700905 for (int i = 0; i < instanceCount; i++) {
bsalomonb5238a72015-05-05 07:49:49 -0700906 Geometry& geom = fGeoData[i];
bsalomon8415abe2015-05-04 11:41:41 -0700907
bsalomonb5238a72015-05-05 07:49:49 -0700908 SkScalar xRadius = geom.fXRadius;
909 SkScalar yRadius = geom.fYRadius;
joshualitt76e7fb62015-02-11 08:52:27 -0800910
911 // Compute the reciprocals of the radii here to save time in the shader
912 SkScalar xRadRecip = SkScalarInvert(xRadius);
913 SkScalar yRadRecip = SkScalarInvert(yRadius);
bsalomonb5238a72015-05-05 07:49:49 -0700914 SkScalar xInnerRadRecip = SkScalarInvert(geom.fInnerXRadius);
915 SkScalar yInnerRadRecip = SkScalarInvert(geom.fInnerYRadius);
joshualitt76e7fb62015-02-11 08:52:27 -0800916
bsalomonb5238a72015-05-05 07:49:49 -0700917 const SkRect& bounds = geom.fDevBounds;
joshualitt76e7fb62015-02-11 08:52:27 -0800918
919 // The inner radius in the vertex data must be specified in normalized space.
920 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
921 verts[0].fOffset = SkPoint::Make(-xRadius, -yRadius);
922 verts[0].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
923 verts[0].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
924
925 verts[1].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
926 verts[1].fOffset = SkPoint::Make(-xRadius, yRadius);
927 verts[1].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
928 verts[1].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
929
930 verts[2].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
931 verts[2].fOffset = SkPoint::Make(xRadius, yRadius);
932 verts[2].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
933 verts[2].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
934
935 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
936 verts[3].fOffset = SkPoint::Make(xRadius, -yRadius);
937 verts[3].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
938 verts[3].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
939
bsalomonb5238a72015-05-05 07:49:49 -0700940 verts += kVerticesPerQuad;
joshualitt76e7fb62015-02-11 08:52:27 -0800941 }
bsalomone64eb572015-05-07 11:35:55 -0700942 helper.issueDraw(batchTarget);
joshualitt76e7fb62015-02-11 08:52:27 -0800943 }
944
945 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
946
947private:
948 EllipseBatch(const Geometry& geometry) {
949 this->initClassID<EllipseBatch>();
950 fGeoData.push_back(geometry);
joshualitt99c7c072015-05-01 13:43:30 -0700951
952 this->setBounds(geometry.fDevBounds);
joshualitt76e7fb62015-02-11 08:52:27 -0800953 }
954
mtklein36352bf2015-03-25 18:17:31 -0700955 bool onCombineIfPossible(GrBatch* t) override {
joshualitt76e7fb62015-02-11 08:52:27 -0800956 EllipseBatch* that = t->cast<EllipseBatch>();
957
958 // TODO use vertex color to avoid breaking batches
959 if (this->color() != that->color()) {
960 return false;
961 }
962
963 if (this->stroke() != that->stroke()) {
964 return false;
965 }
966
967 SkASSERT(this->usesLocalCoords() == that->usesLocalCoords());
968 if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
969 return false;
970 }
971
972 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin());
joshualitt99c7c072015-05-01 13:43:30 -0700973 this->joinBounds(that->bounds());
joshualitt76e7fb62015-02-11 08:52:27 -0800974 return true;
975 }
976
977 GrColor color() const { return fBatch.fColor; }
978 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
979 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; }
980 bool stroke() const { return fBatch.fStroke; }
981
982 struct BatchTracker {
983 GrColor fColor;
984 bool fStroke;
985 bool fUsesLocalCoords;
986 bool fColorIgnored;
987 bool fCoverageIgnored;
988 };
989
joshualitt76e7fb62015-02-11 08:52:27 -0800990 BatchTracker fBatch;
991 SkSTArray<1, Geometry, true> fGeoData;
992};
993
joshualitt3e708c52015-04-30 13:49:27 -0700994static GrBatch* create_ellipse_batch(GrColor color,
995 const SkMatrix& viewMatrix,
996 bool useCoverageAA,
997 const SkRect& ellipse,
joshualittd96a67b2015-05-05 14:09:05 -0700998 const SkStrokeRec& stroke) {
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000999#ifdef SK_DEBUG
1000 {
1001 // we should have checked for this previously
joshualitt8059eb92014-12-29 15:10:07 -08001002 bool isAxisAlignedEllipse = viewMatrix.rectStaysRect();
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001003 SkASSERT(useCoverageAA && isAxisAlignedEllipse);
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001004 }
1005#endif
1006
commit-bot@chromium.org0c888282013-04-19 19:01:45 +00001007 // do any matrix crunching before we reset the draw state for device coords
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001008 SkPoint center = SkPoint::Make(ellipse.centerX(), ellipse.centerY());
joshualitt8059eb92014-12-29 15:10:07 -08001009 viewMatrix.mapPoints(&center, 1);
commit-bot@chromium.org0c888282013-04-19 19:01:45 +00001010 SkScalar ellipseXRadius = SkScalarHalf(ellipse.width());
1011 SkScalar ellipseYRadius = SkScalarHalf(ellipse.height());
joshualitt8059eb92014-12-29 15:10:07 -08001012 SkScalar xRadius = SkScalarAbs(viewMatrix[SkMatrix::kMScaleX]*ellipseXRadius +
1013 viewMatrix[SkMatrix::kMSkewY]*ellipseYRadius);
1014 SkScalar yRadius = SkScalarAbs(viewMatrix[SkMatrix::kMSkewX]*ellipseXRadius +
1015 viewMatrix[SkMatrix::kMScaleY]*ellipseYRadius);
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001016
commit-bot@chromium.org0c888282013-04-19 19:01:45 +00001017 // do (potentially) anisotropic mapping of stroke
1018 SkVector scaledStroke;
1019 SkScalar strokeWidth = stroke.getWidth();
joshualitt8059eb92014-12-29 15:10:07 -08001020 scaledStroke.fX = SkScalarAbs(strokeWidth*(viewMatrix[SkMatrix::kMScaleX] +
1021 viewMatrix[SkMatrix::kMSkewY]));
1022 scaledStroke.fY = SkScalarAbs(strokeWidth*(viewMatrix[SkMatrix::kMSkewX] +
1023 viewMatrix[SkMatrix::kMScaleY]));
commit-bot@chromium.org0c888282013-04-19 19:01:45 +00001024
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001025 SkStrokeRec::Style style = stroke.getStyle();
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001026 bool isStrokeOnly = SkStrokeRec::kStroke_Style == style ||
1027 SkStrokeRec::kHairline_Style == style;
1028 bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == style;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001029
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +00001030 SkScalar innerXRadius = 0;
1031 SkScalar innerYRadius = 0;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001032 if (hasStroke) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001033 if (SkScalarNearlyZero(scaledStroke.length())) {
1034 scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf);
1035 } else {
1036 scaledStroke.scale(SK_ScalarHalf);
1037 }
1038
1039 // we only handle thick strokes for near-circular ellipses
1040 if (scaledStroke.length() > SK_ScalarHalf &&
1041 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
joshualitt3e708c52015-04-30 13:49:27 -07001042 return NULL;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001043 }
1044
1045 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
1046 if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius ||
1047 scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) {
joshualitt3e708c52015-04-30 13:49:27 -07001048 return NULL;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001049 }
1050
1051 // this is legit only if scale & translation (which should be the case at the moment)
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001052 if (isStrokeOnly) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001053 innerXRadius = xRadius - scaledStroke.fX;
1054 innerYRadius = yRadius - scaledStroke.fY;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001055 }
1056
1057 xRadius += scaledStroke.fX;
1058 yRadius += scaledStroke.fY;
1059 }
1060
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001061 // We've extended the outer x radius out half a pixel to antialias.
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +00001062 // This will also expand the rect so all the pixels will be captured.
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001063 // TODO: Consider if we should use sqrt(2)/2 instead
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +00001064 xRadius += SK_ScalarHalf;
1065 yRadius += SK_ScalarHalf;
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001066
joshualitt76e7fb62015-02-11 08:52:27 -08001067 EllipseBatch::Geometry geometry;
1068 geometry.fViewMatrix = viewMatrix;
1069 geometry.fColor = color;
1070 geometry.fXRadius = xRadius;
1071 geometry.fYRadius = yRadius;
1072 geometry.fInnerXRadius = innerXRadius;
1073 geometry.fInnerYRadius = innerYRadius;
1074 geometry.fStroke = isStrokeOnly && innerXRadius > 0 && innerYRadius > 0;
joshualittd96a67b2015-05-05 14:09:05 -07001075 geometry.fDevBounds = SkRect::MakeLTRB(center.fX - xRadius, center.fY - yRadius,
1076 center.fX + xRadius, center.fY + yRadius);
commit-bot@chromium.org0a6cb602013-04-11 15:05:37 +00001077
joshualitt3e708c52015-04-30 13:49:27 -07001078 return EllipseBatch::Create(geometry);
1079}
1080
robertphillipsea461502015-05-26 11:38:03 -07001081bool GrOvalRenderer::DrawEllipse(GrDrawTarget* target,
joshualittae3d63a2015-07-13 08:44:06 -07001082 const GrPipelineBuilder& pipelineBuilder,
joshualitt3e708c52015-04-30 13:49:27 -07001083 GrColor color,
1084 const SkMatrix& viewMatrix,
1085 bool useCoverageAA,
1086 const SkRect& ellipse,
1087 const SkStrokeRec& stroke) {
joshualitt3e708c52015-04-30 13:49:27 -07001088 SkAutoTUnref<GrBatch> batch(create_ellipse_batch(color, viewMatrix, useCoverageAA, ellipse,
joshualittd96a67b2015-05-05 14:09:05 -07001089 stroke));
joshualitt3e708c52015-04-30 13:49:27 -07001090 if (!batch) {
1091 return false;
1092 }
1093
joshualittae3d63a2015-07-13 08:44:06 -07001094 target->drawBatch(pipelineBuilder, batch);
jvanverth@google.comc4f2eca2013-04-16 12:30:35 +00001095 return true;
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001096}
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001097
joshualitt76e7fb62015-02-11 08:52:27 -08001098/////////////////////////////////////////////////////////////////////////////////////////////////
1099
1100class DIEllipseBatch : public GrBatch {
1101public:
1102 struct Geometry {
1103 GrColor fColor;
1104 SkMatrix fViewMatrix;
1105 SkScalar fXRadius;
1106 SkScalar fYRadius;
1107 SkScalar fInnerXRadius;
1108 SkScalar fInnerYRadius;
1109 SkScalar fGeoDx;
1110 SkScalar fGeoDy;
1111 DIEllipseEdgeEffect::Mode fMode;
egdaniel9ef1bb12015-04-20 12:28:57 -07001112 SkRect fBounds;
joshualitt76e7fb62015-02-11 08:52:27 -08001113 };
1114
joshualitt99c7c072015-05-01 13:43:30 -07001115 static GrBatch* Create(const Geometry& geometry, const SkRect& bounds) {
1116 return SkNEW_ARGS(DIEllipseBatch, (geometry, bounds));
joshualitt76e7fb62015-02-11 08:52:27 -08001117 }
1118
mtklein36352bf2015-03-25 18:17:31 -07001119 const char* name() const override { return "DIEllipseBatch"; }
joshualitt76e7fb62015-02-11 08:52:27 -08001120
mtklein36352bf2015-03-25 18:17:31 -07001121 void getInvariantOutputColor(GrInitInvariantOutput* out) const override {
joshualitt76e7fb62015-02-11 08:52:27 -08001122 // When this is called on a batch, there is only one geometry bundle
1123 out->setKnownFourComponents(fGeoData[0].fColor);
1124 }
mtklein36352bf2015-03-25 18:17:31 -07001125 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override {
joshualitt76e7fb62015-02-11 08:52:27 -08001126 out->setUnknownSingleComponent();
1127 }
1128
mtklein36352bf2015-03-25 18:17:31 -07001129 void initBatchTracker(const GrPipelineInfo& init) override {
joshualitt76e7fb62015-02-11 08:52:27 -08001130 // Handle any color overrides
bsalomon7765a472015-07-08 11:26:37 -07001131 if (!init.readsColor()) {
joshualitt76e7fb62015-02-11 08:52:27 -08001132 fGeoData[0].fColor = GrColor_ILLEGAL;
joshualitt76e7fb62015-02-11 08:52:27 -08001133 }
bsalomon7765a472015-07-08 11:26:37 -07001134 init.getOverrideColorIfSet(&fGeoData[0].fColor);
joshualitt76e7fb62015-02-11 08:52:27 -08001135
1136 // setup batch properties
bsalomon7765a472015-07-08 11:26:37 -07001137 fBatch.fColorIgnored = !init.readsColor();
joshualitt76e7fb62015-02-11 08:52:27 -08001138 fBatch.fColor = fGeoData[0].fColor;
1139 fBatch.fMode = fGeoData[0].fMode;
bsalomon7765a472015-07-08 11:26:37 -07001140 fBatch.fUsesLocalCoords = init.readsLocalCoords();
1141 fBatch.fCoverageIgnored = !init.readsCoverage();
joshualitt76e7fb62015-02-11 08:52:27 -08001142 }
1143
mtklein36352bf2015-03-25 18:17:31 -07001144 void generateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline) override {
joshualitt76e7fb62015-02-11 08:52:27 -08001145 // Setup geometry processor
1146 SkAutoTUnref<GrGeometryProcessor> gp(DIEllipseEdgeEffect::Create(this->color(),
1147 this->viewMatrix(),
joshualittb8c241a2015-05-19 08:23:30 -07001148 this->mode(),
1149 this->usesLocalCoords()));
joshualitt76e7fb62015-02-11 08:52:27 -08001150
joshualitt76e7fb62015-02-11 08:52:27 -08001151 batchTarget->initDraw(gp, pipeline);
1152
joshualitt76e7fb62015-02-11 08:52:27 -08001153 int instanceCount = fGeoData.count();
joshualitt76e7fb62015-02-11 08:52:27 -08001154 size_t vertexStride = gp->getVertexStride();
joshualitt19e00582015-02-11 17:36:30 -08001155 SkASSERT(vertexStride == sizeof(DIEllipseVertex));
bsalomonb5238a72015-05-05 07:49:49 -07001156 QuadHelper helper;
1157 DIEllipseVertex* verts = reinterpret_cast<DIEllipseVertex*>(
1158 helper.init(batchTarget, vertexStride, instanceCount));
1159 if (!verts) {
joshualitt4b31de82015-03-05 14:33:41 -08001160 return;
1161 }
1162
joshualitt76e7fb62015-02-11 08:52:27 -08001163 for (int i = 0; i < instanceCount; i++) {
bsalomonb5238a72015-05-05 07:49:49 -07001164 Geometry& geom = fGeoData[i];
joshualitt76e7fb62015-02-11 08:52:27 -08001165
bsalomonb5238a72015-05-05 07:49:49 -07001166 SkScalar xRadius = geom.fXRadius;
1167 SkScalar yRadius = geom.fYRadius;
joshualitt76e7fb62015-02-11 08:52:27 -08001168
bsalomonb5238a72015-05-05 07:49:49 -07001169 const SkRect& bounds = geom.fBounds;
joshualitt76e7fb62015-02-11 08:52:27 -08001170
1171 // This adjusts the "radius" to include the half-pixel border
reed80ea19c2015-05-12 10:37:34 -07001172 SkScalar offsetDx = geom.fGeoDx / xRadius;
1173 SkScalar offsetDy = geom.fGeoDy / yRadius;
joshualitt76e7fb62015-02-11 08:52:27 -08001174
reed80ea19c2015-05-12 10:37:34 -07001175 SkScalar innerRatioX = xRadius / geom.fInnerXRadius;
1176 SkScalar innerRatioY = yRadius / geom.fInnerYRadius;
joshualitt76e7fb62015-02-11 08:52:27 -08001177
1178 verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop);
1179 verts[0].fOuterOffset = SkPoint::Make(-1.0f - offsetDx, -1.0f - offsetDy);
1180 verts[0].fInnerOffset = SkPoint::Make(-innerRatioX - offsetDx, -innerRatioY - offsetDy);
1181
1182 verts[1].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom);
1183 verts[1].fOuterOffset = SkPoint::Make(-1.0f - offsetDx, 1.0f + offsetDy);
1184 verts[1].fInnerOffset = SkPoint::Make(-innerRatioX - offsetDx, innerRatioY + offsetDy);
1185
1186 verts[2].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom);
1187 verts[2].fOuterOffset = SkPoint::Make(1.0f + offsetDx, 1.0f + offsetDy);
1188 verts[2].fInnerOffset = SkPoint::Make(innerRatioX + offsetDx, innerRatioY + offsetDy);
1189
1190 verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fTop);
1191 verts[3].fOuterOffset = SkPoint::Make(1.0f + offsetDx, -1.0f - offsetDy);
1192 verts[3].fInnerOffset = SkPoint::Make(innerRatioX + offsetDx, -innerRatioY - offsetDy);
1193
bsalomonb5238a72015-05-05 07:49:49 -07001194 verts += kVerticesPerQuad;
joshualitt76e7fb62015-02-11 08:52:27 -08001195 }
bsalomone64eb572015-05-07 11:35:55 -07001196 helper.issueDraw(batchTarget);
joshualitt76e7fb62015-02-11 08:52:27 -08001197 }
1198
1199 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
1200
1201private:
joshualitt99c7c072015-05-01 13:43:30 -07001202 DIEllipseBatch(const Geometry& geometry, const SkRect& bounds) {
joshualitt76e7fb62015-02-11 08:52:27 -08001203 this->initClassID<DIEllipseBatch>();
1204 fGeoData.push_back(geometry);
joshualitt99c7c072015-05-01 13:43:30 -07001205
1206 this->setBounds(bounds);
joshualitt76e7fb62015-02-11 08:52:27 -08001207 }
1208
mtklein36352bf2015-03-25 18:17:31 -07001209 bool onCombineIfPossible(GrBatch* t) override {
joshualitt76e7fb62015-02-11 08:52:27 -08001210 DIEllipseBatch* that = t->cast<DIEllipseBatch>();
1211
1212 // TODO use vertex color to avoid breaking batches
1213 if (this->color() != that->color()) {
1214 return false;
1215 }
1216
1217 if (this->mode() != that->mode()) {
1218 return false;
1219 }
1220
joshualittd96a67b2015-05-05 14:09:05 -07001221 // TODO rewrite to allow positioning on CPU
1222 if (!this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
joshualitt76e7fb62015-02-11 08:52:27 -08001223 return false;
1224 }
1225
1226 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin());
joshualitt99c7c072015-05-01 13:43:30 -07001227 this->joinBounds(that->bounds());
joshualitt76e7fb62015-02-11 08:52:27 -08001228 return true;
1229 }
1230
1231 GrColor color() const { return fBatch.fColor; }
1232 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
1233 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; }
1234 DIEllipseEdgeEffect::Mode mode() const { return fBatch.fMode; }
1235
1236 struct BatchTracker {
1237 GrColor fColor;
1238 DIEllipseEdgeEffect::Mode fMode;
1239 bool fUsesLocalCoords;
1240 bool fColorIgnored;
1241 bool fCoverageIgnored;
1242 };
1243
joshualitt76e7fb62015-02-11 08:52:27 -08001244 BatchTracker fBatch;
1245 SkSTArray<1, Geometry, true> fGeoData;
1246};
1247
joshualitt3e708c52015-04-30 13:49:27 -07001248static GrBatch* create_diellipse_batch(GrColor color,
1249 const SkMatrix& viewMatrix,
1250 bool useCoverageAA,
1251 const SkRect& ellipse,
joshualittd96a67b2015-05-05 14:09:05 -07001252 const SkStrokeRec& stroke) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001253 SkPoint center = SkPoint::Make(ellipse.centerX(), ellipse.centerY());
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +00001254 SkScalar xRadius = SkScalarHalf(ellipse.width());
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +00001255 SkScalar yRadius = SkScalarHalf(ellipse.height());
1256
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +00001257 SkStrokeRec::Style style = stroke.getStyle();
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +00001258 DIEllipseEdgeEffect::Mode mode = (SkStrokeRec::kStroke_Style == style) ?
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +00001259 DIEllipseEdgeEffect::kStroke :
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +00001260 (SkStrokeRec::kHairline_Style == style) ?
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +00001261 DIEllipseEdgeEffect::kHairline : DIEllipseEdgeEffect::kFill;
1262
1263 SkScalar innerXRadius = 0;
1264 SkScalar innerYRadius = 0;
1265 if (SkStrokeRec::kFill_Style != style && SkStrokeRec::kHairline_Style != style) {
1266 SkScalar strokeWidth = stroke.getWidth();
1267
1268 if (SkScalarNearlyZero(strokeWidth)) {
1269 strokeWidth = SK_ScalarHalf;
1270 } else {
1271 strokeWidth *= SK_ScalarHalf;
1272 }
1273
1274 // we only handle thick strokes for near-circular ellipses
1275 if (strokeWidth > SK_ScalarHalf &&
1276 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
joshualitt3e708c52015-04-30 13:49:27 -07001277 return NULL;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +00001278 }
1279
1280 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
1281 if (strokeWidth*(yRadius*yRadius) < (strokeWidth*strokeWidth)*xRadius ||
1282 strokeWidth*(xRadius*xRadius) < (strokeWidth*strokeWidth)*yRadius) {
joshualitt3e708c52015-04-30 13:49:27 -07001283 return NULL;
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +00001284 }
1285
1286 // set inner radius (if needed)
1287 if (SkStrokeRec::kStroke_Style == style) {
1288 innerXRadius = xRadius - strokeWidth;
1289 innerYRadius = yRadius - strokeWidth;
1290 }
1291
1292 xRadius += strokeWidth;
1293 yRadius += strokeWidth;
1294 }
1295 if (DIEllipseEdgeEffect::kStroke == mode) {
1296 mode = (innerXRadius > 0 && innerYRadius > 0) ? DIEllipseEdgeEffect::kStroke :
1297 DIEllipseEdgeEffect::kFill;
1298 }
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +00001299
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +00001300 // This expands the outer rect so that after CTM we end up with a half-pixel border
joshualitt8059eb92014-12-29 15:10:07 -08001301 SkScalar a = viewMatrix[SkMatrix::kMScaleX];
1302 SkScalar b = viewMatrix[SkMatrix::kMSkewX];
1303 SkScalar c = viewMatrix[SkMatrix::kMSkewY];
1304 SkScalar d = viewMatrix[SkMatrix::kMScaleY];
reed80ea19c2015-05-12 10:37:34 -07001305 SkScalar geoDx = SK_ScalarHalf / SkScalarSqrt(a*a + c*c);
1306 SkScalar geoDy = SK_ScalarHalf / SkScalarSqrt(b*b + d*d);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +00001307
joshualitt76e7fb62015-02-11 08:52:27 -08001308 DIEllipseBatch::Geometry geometry;
1309 geometry.fViewMatrix = viewMatrix;
1310 geometry.fColor = color;
1311 geometry.fXRadius = xRadius;
1312 geometry.fYRadius = yRadius;
1313 geometry.fInnerXRadius = innerXRadius;
1314 geometry.fInnerYRadius = innerYRadius;
1315 geometry.fGeoDx = geoDx;
1316 geometry.fGeoDy = geoDy;
1317 geometry.fMode = mode;
joshualittd96a67b2015-05-05 14:09:05 -07001318 geometry.fBounds = SkRect::MakeLTRB(center.fX - xRadius - geoDx, center.fY - yRadius - geoDy,
1319 center.fX + xRadius + geoDx, center.fY + yRadius + geoDy);
egdaniel9ef1bb12015-04-20 12:28:57 -07001320
joshualittd96a67b2015-05-05 14:09:05 -07001321 SkRect devBounds = geometry.fBounds;
1322 viewMatrix.mapRect(&devBounds);
1323 return DIEllipseBatch::Create(geometry, devBounds);
joshualitt3e708c52015-04-30 13:49:27 -07001324}
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +00001325
robertphillipsea461502015-05-26 11:38:03 -07001326bool GrOvalRenderer::DrawDIEllipse(GrDrawTarget* target,
joshualittae3d63a2015-07-13 08:44:06 -07001327 const GrPipelineBuilder& pipelineBuilder,
joshualitt3e708c52015-04-30 13:49:27 -07001328 GrColor color,
1329 const SkMatrix& viewMatrix,
1330 bool useCoverageAA,
1331 const SkRect& ellipse,
1332 const SkStrokeRec& stroke) {
joshualitt3e708c52015-04-30 13:49:27 -07001333 SkAutoTUnref<GrBatch> batch(create_diellipse_batch(color, viewMatrix, useCoverageAA, ellipse,
joshualittd96a67b2015-05-05 14:09:05 -07001334 stroke));
joshualitt3e708c52015-04-30 13:49:27 -07001335 if (!batch) {
1336 return false;
1337 }
joshualittae3d63a2015-07-13 08:44:06 -07001338 target->drawBatch(pipelineBuilder, batch);
commit-bot@chromium.org5242ed72013-09-05 19:26:51 +00001339 return true;
1340}
1341
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001342///////////////////////////////////////////////////////////////////////////////
1343
1344static const uint16_t gRRectIndices[] = {
1345 // corners
1346 0, 1, 5, 0, 5, 4,
1347 2, 3, 7, 2, 7, 6,
1348 8, 9, 13, 8, 13, 12,
1349 10, 11, 15, 10, 15, 14,
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001350
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001351 // edges
1352 1, 2, 6, 1, 6, 5,
1353 4, 5, 9, 4, 9, 8,
1354 6, 7, 11, 6, 11, 10,
1355 9, 10, 14, 9, 14, 13,
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001356
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001357 // center
1358 // we place this at the end so that we can ignore these indices when rendering stroke-only
1359 5, 6, 10, 5, 10, 9
1360};
1361
joshualitt5ead6da2014-10-22 16:00:29 -07001362static const int kIndicesPerStrokeRRect = SK_ARRAY_COUNT(gRRectIndices) - 6;
1363static const int kIndicesPerRRect = SK_ARRAY_COUNT(gRRectIndices);
1364static const int kVertsPerRRect = 16;
1365static const int kNumRRectsInIndexBuffer = 256;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001366
bsalomoned0bcad2015-05-04 10:36:42 -07001367GR_DECLARE_STATIC_UNIQUE_KEY(gStrokeRRectOnlyIndexBufferKey);
1368GR_DECLARE_STATIC_UNIQUE_KEY(gRRectOnlyIndexBufferKey);
1369static const GrIndexBuffer* ref_rrect_index_buffer(bool strokeOnly,
1370 GrResourceProvider* resourceProvider) {
1371 GR_DEFINE_STATIC_UNIQUE_KEY(gStrokeRRectOnlyIndexBufferKey);
1372 GR_DEFINE_STATIC_UNIQUE_KEY(gRRectOnlyIndexBufferKey);
1373 if (strokeOnly) {
1374 return resourceProvider->refOrCreateInstancedIndexBuffer(
1375 gRRectIndices, kIndicesPerStrokeRRect, kNumRRectsInIndexBuffer, kVertsPerRRect,
1376 gStrokeRRectOnlyIndexBufferKey);
1377 } else {
1378 return resourceProvider->refOrCreateInstancedIndexBuffer(
1379 gRRectIndices, kIndicesPerRRect, kNumRRectsInIndexBuffer, kVertsPerRRect,
1380 gRRectOnlyIndexBufferKey);
1381
1382 }
1383}
1384
robertphillipsea461502015-05-26 11:38:03 -07001385bool GrOvalRenderer::DrawDRRect(GrDrawTarget* target,
joshualittae3d63a2015-07-13 08:44:06 -07001386 const GrPipelineBuilder& pipelineBuilder,
joshualitt2e3b3e32014-12-09 13:31:14 -08001387 GrColor color,
joshualitt8059eb92014-12-29 15:10:07 -08001388 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -08001389 bool useAA,
1390 const SkRRect& origOuter,
1391 const SkRRect& origInner) {
joshualittae3d63a2015-07-13 08:44:06 -07001392 bool applyAA = useAA && !pipelineBuilder.getRenderTarget()->isUnifiedMultisampled();
bsalomon6be6f7c2015-02-26 13:05:21 -08001393 GrPipelineBuilder::AutoRestoreFragmentProcessors arfp;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001394 if (!origInner.isEmpty()) {
1395 SkTCopyOnFirstWrite<SkRRect> inner(origInner);
joshualitt8059eb92014-12-29 15:10:07 -08001396 if (!viewMatrix.isIdentity()) {
1397 if (!origInner.transform(viewMatrix, inner.writable())) {
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001398 return false;
1399 }
1400 }
joshualittb0a8a372014-09-23 09:50:21 -07001401 GrPrimitiveEdgeType edgeType = applyAA ?
1402 kInverseFillAA_GrProcessorEdgeType :
1403 kInverseFillBW_GrProcessorEdgeType;
joshualitt2e3b3e32014-12-09 13:31:14 -08001404 // TODO this needs to be a geometry processor
joshualittb0a8a372014-09-23 09:50:21 -07001405 GrFragmentProcessor* fp = GrRRectEffect::Create(edgeType, *inner);
1406 if (NULL == fp) {
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001407 return false;
1408 }
joshualittae3d63a2015-07-13 08:44:06 -07001409 arfp.set(&pipelineBuilder);
1410 arfp.addCoverageProcessor(fp)->unref();
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001411 }
1412
1413 SkStrokeRec fillRec(SkStrokeRec::kFill_InitStyle);
robertphillipsea461502015-05-26 11:38:03 -07001414 if (DrawRRect(target, pipelineBuilder, color, viewMatrix, useAA, origOuter, fillRec)) {
bsalomon8af05232014-06-03 06:34:58 -07001415 return true;
1416 }
1417
1418 SkASSERT(!origOuter.isEmpty());
1419 SkTCopyOnFirstWrite<SkRRect> outer(origOuter);
joshualitt8059eb92014-12-29 15:10:07 -08001420 if (!viewMatrix.isIdentity()) {
1421 if (!origOuter.transform(viewMatrix, outer.writable())) {
bsalomon8af05232014-06-03 06:34:58 -07001422 return false;
1423 }
1424 }
joshualittb0a8a372014-09-23 09:50:21 -07001425 GrPrimitiveEdgeType edgeType = applyAA ? kFillAA_GrProcessorEdgeType :
joshualitt76e7fb62015-02-11 08:52:27 -08001426 kFillBW_GrProcessorEdgeType;
joshualittb0a8a372014-09-23 09:50:21 -07001427 GrFragmentProcessor* effect = GrRRectEffect::Create(edgeType, *outer);
bsalomon8af05232014-06-03 06:34:58 -07001428 if (NULL == effect) {
1429 return false;
1430 }
bsalomon6be6f7c2015-02-26 13:05:21 -08001431 if (!arfp.isSet()) {
joshualittae3d63a2015-07-13 08:44:06 -07001432 arfp.set(&pipelineBuilder);
bsalomon8af05232014-06-03 06:34:58 -07001433 }
joshualittd27f73e2014-12-29 07:43:36 -08001434
1435 SkMatrix invert;
joshualitt8059eb92014-12-29 15:10:07 -08001436 if (!viewMatrix.invert(&invert)) {
bsalomon8af05232014-06-03 06:34:58 -07001437 return false;
1438 }
joshualittd27f73e2014-12-29 07:43:36 -08001439
joshualittae3d63a2015-07-13 08:44:06 -07001440 arfp.addCoverageProcessor(effect)->unref();
bsalomon8af05232014-06-03 06:34:58 -07001441 SkRect bounds = outer->getBounds();
1442 if (applyAA) {
1443 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1444 }
joshualittae3d63a2015-07-13 08:44:06 -07001445 target->drawBWRect(pipelineBuilder, color, SkMatrix::I(), bounds, NULL, &invert);
bsalomon8af05232014-06-03 06:34:58 -07001446 return true;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001447}
1448
joshualitt76e7fb62015-02-11 08:52:27 -08001449///////////////////////////////////////////////////////////////////////////////////////////////////
1450
1451class RRectCircleRendererBatch : public GrBatch {
1452public:
1453 struct Geometry {
1454 GrColor fColor;
1455 SkMatrix fViewMatrix;
1456 SkScalar fInnerRadius;
1457 SkScalar fOuterRadius;
1458 bool fStroke;
egdanielbc227142015-04-21 06:28:08 -07001459 SkRect fDevBounds;
joshualitt76e7fb62015-02-11 08:52:27 -08001460 };
1461
bsalomoned0bcad2015-05-04 10:36:42 -07001462 static GrBatch* Create(const Geometry& geometry) {
1463 return SkNEW_ARGS(RRectCircleRendererBatch, (geometry));
joshualitt76e7fb62015-02-11 08:52:27 -08001464 }
1465
mtklein36352bf2015-03-25 18:17:31 -07001466 const char* name() const override { return "RRectCircleBatch"; }
joshualitt76e7fb62015-02-11 08:52:27 -08001467
mtklein36352bf2015-03-25 18:17:31 -07001468 void getInvariantOutputColor(GrInitInvariantOutput* out) const override {
joshualitt76e7fb62015-02-11 08:52:27 -08001469 // When this is called on a batch, there is only one geometry bundle
1470 out->setKnownFourComponents(fGeoData[0].fColor);
1471 }
mtklein36352bf2015-03-25 18:17:31 -07001472 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override {
joshualitt76e7fb62015-02-11 08:52:27 -08001473 out->setUnknownSingleComponent();
1474 }
1475
mtklein36352bf2015-03-25 18:17:31 -07001476 void initBatchTracker(const GrPipelineInfo& init) override {
joshualitt76e7fb62015-02-11 08:52:27 -08001477 // Handle any color overrides
bsalomon7765a472015-07-08 11:26:37 -07001478 if (!init.readsColor()) {
joshualitt76e7fb62015-02-11 08:52:27 -08001479 fGeoData[0].fColor = GrColor_ILLEGAL;
joshualitt76e7fb62015-02-11 08:52:27 -08001480 }
bsalomon7765a472015-07-08 11:26:37 -07001481 init.getOverrideColorIfSet(&fGeoData[0].fColor);
joshualitt76e7fb62015-02-11 08:52:27 -08001482
1483 // setup batch properties
bsalomon7765a472015-07-08 11:26:37 -07001484 fBatch.fColorIgnored = !init.readsColor();
joshualitt76e7fb62015-02-11 08:52:27 -08001485 fBatch.fColor = fGeoData[0].fColor;
1486 fBatch.fStroke = fGeoData[0].fStroke;
bsalomon7765a472015-07-08 11:26:37 -07001487 fBatch.fUsesLocalCoords = init.readsLocalCoords();
1488 fBatch.fCoverageIgnored = !init.readsCoverage();
joshualitt76e7fb62015-02-11 08:52:27 -08001489 }
1490
mtklein36352bf2015-03-25 18:17:31 -07001491 void generateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline) override {
joshualitt76e7fb62015-02-11 08:52:27 -08001492 // reset to device coordinates
1493 SkMatrix invert;
1494 if (!this->viewMatrix().invert(&invert)) {
1495 SkDebugf("Failed to invert\n");
1496 return;
1497 }
1498
1499 // Setup geometry processor
1500 SkAutoTUnref<GrGeometryProcessor> gp(CircleEdgeEffect::Create(this->color(),
1501 this->stroke(),
joshualittb8c241a2015-05-19 08:23:30 -07001502 invert,
1503 this->usesLocalCoords()));
joshualitt76e7fb62015-02-11 08:52:27 -08001504
1505 batchTarget->initDraw(gp, pipeline);
1506
joshualitt76e7fb62015-02-11 08:52:27 -08001507 int instanceCount = fGeoData.count();
joshualitt76e7fb62015-02-11 08:52:27 -08001508 size_t vertexStride = gp->getVertexStride();
1509 SkASSERT(vertexStride == sizeof(CircleVertex));
1510
bsalomonb5238a72015-05-05 07:49:49 -07001511 // drop out the middle quad if we're stroked
1512 int indicesPerInstance = this->stroke() ? kIndicesPerStrokeRRect : kIndicesPerRRect;
bsalomoned0bcad2015-05-04 10:36:42 -07001513 SkAutoTUnref<const GrIndexBuffer> indexBuffer(
1514 ref_rrect_index_buffer(this->stroke(), batchTarget->resourceProvider()));
joshualitt76e7fb62015-02-11 08:52:27 -08001515
bsalomonb5238a72015-05-05 07:49:49 -07001516 InstancedHelper helper;
1517 CircleVertex* verts = reinterpret_cast<CircleVertex*>(helper.init(batchTarget,
1518 kTriangles_GrPrimitiveType, vertexStride, indexBuffer, kVertsPerRRect,
1519 indicesPerInstance, instanceCount));
1520 if (!verts || !indexBuffer) {
joshualitt4b31de82015-03-05 14:33:41 -08001521 SkDebugf("Could not allocate vertices\n");
1522 return;
1523 }
1524
joshualitt76e7fb62015-02-11 08:52:27 -08001525 for (int i = 0; i < instanceCount; i++) {
1526 Geometry& args = fGeoData[i];
1527
1528 SkScalar outerRadius = args.fOuterRadius;
1529
egdanielbc227142015-04-21 06:28:08 -07001530 const SkRect& bounds = args.fDevBounds;
joshualitt76e7fb62015-02-11 08:52:27 -08001531
1532 SkScalar yCoords[4] = {
1533 bounds.fTop,
1534 bounds.fTop + outerRadius,
1535 bounds.fBottom - outerRadius,
1536 bounds.fBottom
1537 };
1538
1539 SkScalar yOuterRadii[4] = {-1, 0, 0, 1 };
1540 // The inner radius in the vertex data must be specified in normalized space.
1541 SkScalar innerRadius = args.fInnerRadius / args.fOuterRadius;
1542 for (int i = 0; i < 4; ++i) {
1543 verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]);
1544 verts->fOffset = SkPoint::Make(-1, yOuterRadii[i]);
1545 verts->fOuterRadius = outerRadius;
1546 verts->fInnerRadius = innerRadius;
1547 verts++;
1548
1549 verts->fPos = SkPoint::Make(bounds.fLeft + outerRadius, yCoords[i]);
1550 verts->fOffset = SkPoint::Make(0, yOuterRadii[i]);
1551 verts->fOuterRadius = outerRadius;
1552 verts->fInnerRadius = innerRadius;
1553 verts++;
1554
1555 verts->fPos = SkPoint::Make(bounds.fRight - outerRadius, yCoords[i]);
1556 verts->fOffset = SkPoint::Make(0, yOuterRadii[i]);
1557 verts->fOuterRadius = outerRadius;
1558 verts->fInnerRadius = innerRadius;
1559 verts++;
1560
1561 verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]);
1562 verts->fOffset = SkPoint::Make(1, yOuterRadii[i]);
1563 verts->fOuterRadius = outerRadius;
1564 verts->fInnerRadius = innerRadius;
1565 verts++;
1566 }
1567 }
1568
bsalomone64eb572015-05-07 11:35:55 -07001569 helper.issueDraw(batchTarget);
joshualitt76e7fb62015-02-11 08:52:27 -08001570 }
1571
1572 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
1573
1574private:
bsalomoned0bcad2015-05-04 10:36:42 -07001575 RRectCircleRendererBatch(const Geometry& geometry) {
joshualitt76e7fb62015-02-11 08:52:27 -08001576 this->initClassID<RRectCircleRendererBatch>();
1577 fGeoData.push_back(geometry);
joshualitt99c7c072015-05-01 13:43:30 -07001578
1579 this->setBounds(geometry.fDevBounds);
joshualitt76e7fb62015-02-11 08:52:27 -08001580 }
1581
mtklein36352bf2015-03-25 18:17:31 -07001582 bool onCombineIfPossible(GrBatch* t) override {
joshualitt76e7fb62015-02-11 08:52:27 -08001583 RRectCircleRendererBatch* that = t->cast<RRectCircleRendererBatch>();
1584
1585 // TODO use vertex color to avoid breaking batches
1586 if (this->color() != that->color()) {
1587 return false;
1588 }
1589
1590 if (this->stroke() != that->stroke()) {
1591 return false;
1592 }
1593
1594 SkASSERT(this->usesLocalCoords() == that->usesLocalCoords());
1595 if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
1596 return false;
1597 }
1598
1599 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin());
joshualitt99c7c072015-05-01 13:43:30 -07001600 this->joinBounds(that->bounds());
joshualitt76e7fb62015-02-11 08:52:27 -08001601 return true;
1602 }
1603
1604 GrColor color() const { return fBatch.fColor; }
1605 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
1606 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; }
1607 bool stroke() const { return fBatch.fStroke; }
1608
1609 struct BatchTracker {
1610 GrColor fColor;
1611 bool fStroke;
1612 bool fUsesLocalCoords;
1613 bool fColorIgnored;
1614 bool fCoverageIgnored;
1615 };
1616
joshualitt76e7fb62015-02-11 08:52:27 -08001617 BatchTracker fBatch;
1618 SkSTArray<1, Geometry, true> fGeoData;
joshualitt76e7fb62015-02-11 08:52:27 -08001619};
1620
1621class RRectEllipseRendererBatch : public GrBatch {
1622public:
1623 struct Geometry {
1624 GrColor fColor;
1625 SkMatrix fViewMatrix;
1626 SkScalar fXRadius;
1627 SkScalar fYRadius;
1628 SkScalar fInnerXRadius;
1629 SkScalar fInnerYRadius;
1630 bool fStroke;
egdanielbc227142015-04-21 06:28:08 -07001631 SkRect fDevBounds;
joshualitt76e7fb62015-02-11 08:52:27 -08001632 };
1633
bsalomoned0bcad2015-05-04 10:36:42 -07001634 static GrBatch* Create(const Geometry& geometry) {
1635 return SkNEW_ARGS(RRectEllipseRendererBatch, (geometry));
joshualitt76e7fb62015-02-11 08:52:27 -08001636 }
1637
mtklein36352bf2015-03-25 18:17:31 -07001638 const char* name() const override { return "RRectEllipseRendererBatch"; }
joshualitt76e7fb62015-02-11 08:52:27 -08001639
mtklein36352bf2015-03-25 18:17:31 -07001640 void getInvariantOutputColor(GrInitInvariantOutput* out) const override {
joshualitt76e7fb62015-02-11 08:52:27 -08001641 // When this is called on a batch, there is only one geometry bundle
1642 out->setKnownFourComponents(fGeoData[0].fColor);
1643 }
mtklein36352bf2015-03-25 18:17:31 -07001644 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override {
joshualitt76e7fb62015-02-11 08:52:27 -08001645 out->setUnknownSingleComponent();
1646 }
1647
mtklein36352bf2015-03-25 18:17:31 -07001648 void initBatchTracker(const GrPipelineInfo& init) override {
joshualitt76e7fb62015-02-11 08:52:27 -08001649 // Handle any color overrides
bsalomon7765a472015-07-08 11:26:37 -07001650 if (!init.readsColor()) {
joshualitt76e7fb62015-02-11 08:52:27 -08001651 fGeoData[0].fColor = GrColor_ILLEGAL;
joshualitt76e7fb62015-02-11 08:52:27 -08001652 }
bsalomon7765a472015-07-08 11:26:37 -07001653 init.getOverrideColorIfSet(&fGeoData[0].fColor);
joshualitt76e7fb62015-02-11 08:52:27 -08001654
1655 // setup batch properties
bsalomon7765a472015-07-08 11:26:37 -07001656 fBatch.fColorIgnored = !init.readsColor();
joshualitt76e7fb62015-02-11 08:52:27 -08001657 fBatch.fColor = fGeoData[0].fColor;
1658 fBatch.fStroke = fGeoData[0].fStroke;
bsalomon7765a472015-07-08 11:26:37 -07001659 fBatch.fUsesLocalCoords = init.readsLocalCoords();
1660 fBatch.fCoverageIgnored = !init.readsCoverage();
joshualitt76e7fb62015-02-11 08:52:27 -08001661 }
1662
mtklein36352bf2015-03-25 18:17:31 -07001663 void generateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline) override {
joshualitt76e7fb62015-02-11 08:52:27 -08001664 // reset to device coordinates
1665 SkMatrix invert;
1666 if (!this->viewMatrix().invert(&invert)) {
1667 SkDebugf("Failed to invert\n");
1668 return;
1669 }
1670
1671 // Setup geometry processor
1672 SkAutoTUnref<GrGeometryProcessor> gp(EllipseEdgeEffect::Create(this->color(),
1673 this->stroke(),
joshualittb8c241a2015-05-19 08:23:30 -07001674 invert,
1675 this->usesLocalCoords()));
joshualitt76e7fb62015-02-11 08:52:27 -08001676
1677 batchTarget->initDraw(gp, pipeline);
1678
joshualitt76e7fb62015-02-11 08:52:27 -08001679 int instanceCount = fGeoData.count();
joshualitt76e7fb62015-02-11 08:52:27 -08001680 size_t vertexStride = gp->getVertexStride();
1681 SkASSERT(vertexStride == sizeof(EllipseVertex));
1682
bsalomonb5238a72015-05-05 07:49:49 -07001683 // drop out the middle quad if we're stroked
1684 int indicesPerInstance = this->stroke() ? kIndicesPerStrokeRRect : kIndicesPerRRect;
bsalomoned0bcad2015-05-04 10:36:42 -07001685 SkAutoTUnref<const GrIndexBuffer> indexBuffer(
1686 ref_rrect_index_buffer(this->stroke(), batchTarget->resourceProvider()));
joshualitt76e7fb62015-02-11 08:52:27 -08001687
bsalomonb5238a72015-05-05 07:49:49 -07001688 InstancedHelper helper;
1689 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(
1690 helper.init(batchTarget, kTriangles_GrPrimitiveType, vertexStride, indexBuffer,
1691 kVertsPerRRect, indicesPerInstance, instanceCount));
1692 if (!verts || !indexBuffer) {
joshualitt4b31de82015-03-05 14:33:41 -08001693 SkDebugf("Could not allocate vertices\n");
1694 return;
1695 }
1696
joshualitt76e7fb62015-02-11 08:52:27 -08001697 for (int i = 0; i < instanceCount; i++) {
1698 Geometry& args = fGeoData[i];
1699
1700 // Compute the reciprocals of the radii here to save time in the shader
1701 SkScalar xRadRecip = SkScalarInvert(args.fXRadius);
1702 SkScalar yRadRecip = SkScalarInvert(args.fYRadius);
1703 SkScalar xInnerRadRecip = SkScalarInvert(args.fInnerXRadius);
1704 SkScalar yInnerRadRecip = SkScalarInvert(args.fInnerYRadius);
1705
1706 // Extend the radii out half a pixel to antialias.
1707 SkScalar xOuterRadius = args.fXRadius + SK_ScalarHalf;
1708 SkScalar yOuterRadius = args.fYRadius + SK_ScalarHalf;
1709
egdanielbc227142015-04-21 06:28:08 -07001710 const SkRect& bounds = args.fDevBounds;
joshualitt76e7fb62015-02-11 08:52:27 -08001711
1712 SkScalar yCoords[4] = {
1713 bounds.fTop,
1714 bounds.fTop + yOuterRadius,
1715 bounds.fBottom - yOuterRadius,
1716 bounds.fBottom
1717 };
1718 SkScalar yOuterOffsets[4] = {
1719 yOuterRadius,
1720 SK_ScalarNearlyZero, // we're using inversesqrt() in shader, so can't be exactly 0
1721 SK_ScalarNearlyZero,
1722 yOuterRadius
1723 };
1724
1725 for (int i = 0; i < 4; ++i) {
1726 verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]);
1727 verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]);
1728 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1729 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
1730 verts++;
1731
1732 verts->fPos = SkPoint::Make(bounds.fLeft + xOuterRadius, yCoords[i]);
1733 verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]);
1734 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1735 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
1736 verts++;
1737
1738 verts->fPos = SkPoint::Make(bounds.fRight - xOuterRadius, yCoords[i]);
1739 verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]);
1740 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1741 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
1742 verts++;
1743
1744 verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]);
1745 verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]);
1746 verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip);
1747 verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip);
1748 verts++;
1749 }
1750 }
bsalomone64eb572015-05-07 11:35:55 -07001751 helper.issueDraw(batchTarget);
joshualitt76e7fb62015-02-11 08:52:27 -08001752 }
1753
1754 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
1755
1756private:
bsalomoned0bcad2015-05-04 10:36:42 -07001757 RRectEllipseRendererBatch(const Geometry& geometry) {
joshualitt76e7fb62015-02-11 08:52:27 -08001758 this->initClassID<RRectEllipseRendererBatch>();
1759 fGeoData.push_back(geometry);
joshualitt99c7c072015-05-01 13:43:30 -07001760
1761 this->setBounds(geometry.fDevBounds);
joshualitt76e7fb62015-02-11 08:52:27 -08001762 }
1763
mtklein36352bf2015-03-25 18:17:31 -07001764 bool onCombineIfPossible(GrBatch* t) override {
joshualitt76e7fb62015-02-11 08:52:27 -08001765 RRectEllipseRendererBatch* that = t->cast<RRectEllipseRendererBatch>();
1766
1767 // TODO use vertex color to avoid breaking batches
1768 if (this->color() != that->color()) {
1769 return false;
1770 }
1771
1772 if (this->stroke() != that->stroke()) {
1773 return false;
1774 }
1775
1776 SkASSERT(this->usesLocalCoords() == that->usesLocalCoords());
1777 if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
1778 return false;
1779 }
1780
1781 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin());
joshualitt99c7c072015-05-01 13:43:30 -07001782 this->joinBounds(that->bounds());
joshualitt76e7fb62015-02-11 08:52:27 -08001783 return true;
1784 }
1785
1786 GrColor color() const { return fBatch.fColor; }
1787 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
1788 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; }
1789 bool stroke() const { return fBatch.fStroke; }
1790
1791 struct BatchTracker {
1792 GrColor fColor;
1793 bool fStroke;
1794 bool fUsesLocalCoords;
1795 bool fColorIgnored;
1796 bool fCoverageIgnored;
1797 };
1798
joshualitt76e7fb62015-02-11 08:52:27 -08001799 BatchTracker fBatch;
1800 SkSTArray<1, Geometry, true> fGeoData;
joshualitt76e7fb62015-02-11 08:52:27 -08001801};
1802
joshualitt3e708c52015-04-30 13:49:27 -07001803static GrBatch* create_rrect_batch(GrColor color,
1804 const SkMatrix& viewMatrix,
1805 const SkRRect& rrect,
joshualittd96a67b2015-05-05 14:09:05 -07001806 const SkStrokeRec& stroke) {
joshualitt3e708c52015-04-30 13:49:27 -07001807 SkASSERT(viewMatrix.rectStaysRect());
1808 SkASSERT(rrect.isSimple());
1809 SkASSERT(!rrect.isOval());
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001810
joshualitt3e708c52015-04-30 13:49:27 -07001811 // RRect batchs only handle simple, but not too simple, rrects
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001812 // do any matrix crunching before we reset the draw state for device coords
1813 const SkRect& rrectBounds = rrect.getBounds();
joshualittd96a67b2015-05-05 14:09:05 -07001814 SkRect bounds;
1815 viewMatrix.mapRect(&bounds, rrectBounds);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001816
1817 SkVector radii = rrect.getSimpleRadii();
joshualitt8059eb92014-12-29 15:10:07 -08001818 SkScalar xRadius = SkScalarAbs(viewMatrix[SkMatrix::kMScaleX]*radii.fX +
1819 viewMatrix[SkMatrix::kMSkewY]*radii.fY);
1820 SkScalar yRadius = SkScalarAbs(viewMatrix[SkMatrix::kMSkewX]*radii.fX +
1821 viewMatrix[SkMatrix::kMScaleY]*radii.fY);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001822
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001823 SkStrokeRec::Style style = stroke.getStyle();
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001824
1825 // do (potentially) anisotropic mapping of stroke
1826 SkVector scaledStroke;
1827 SkScalar strokeWidth = stroke.getWidth();
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001828
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001829 bool isStrokeOnly = SkStrokeRec::kStroke_Style == style ||
1830 SkStrokeRec::kHairline_Style == style;
1831 bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == style;
1832
1833 if (hasStroke) {
1834 if (SkStrokeRec::kHairline_Style == style) {
1835 scaledStroke.set(1, 1);
1836 } else {
joshualitt8059eb92014-12-29 15:10:07 -08001837 scaledStroke.fX = SkScalarAbs(strokeWidth*(viewMatrix[SkMatrix::kMScaleX] +
1838 viewMatrix[SkMatrix::kMSkewY]));
1839 scaledStroke.fY = SkScalarAbs(strokeWidth*(viewMatrix[SkMatrix::kMSkewX] +
1840 viewMatrix[SkMatrix::kMScaleY]));
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001841 }
1842
1843 // if half of strokewidth is greater than radius, we don't handle that right now
1844 if (SK_ScalarHalf*scaledStroke.fX > xRadius || SK_ScalarHalf*scaledStroke.fY > yRadius) {
joshualitt3e708c52015-04-30 13:49:27 -07001845 return NULL;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001846 }
1847 }
1848
1849 // The way the effect interpolates the offset-to-ellipse/circle-center attribute only works on
1850 // the interior of the rrect if the radii are >= 0.5. Otherwise, the inner rect of the nine-
1851 // patch will have fractional coverage. This only matters when the interior is actually filled.
1852 // We could consider falling back to rect rendering here, since a tiny radius is
1853 // indistinguishable from a square corner.
1854 if (!isStrokeOnly && (SK_ScalarHalf > xRadius || SK_ScalarHalf > yRadius)) {
joshualitt3e708c52015-04-30 13:49:27 -07001855 return NULL;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001856 }
1857
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001858 // if the corners are circles, use the circle renderer
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001859 if ((!hasStroke || scaledStroke.fX == scaledStroke.fY) && xRadius == yRadius) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001860 SkScalar innerRadius = 0.0f;
1861 SkScalar outerRadius = xRadius;
1862 SkScalar halfWidth = 0;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001863 if (hasStroke) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001864 if (SkScalarNearlyZero(scaledStroke.fX)) {
1865 halfWidth = SK_ScalarHalf;
1866 } else {
1867 halfWidth = SkScalarHalf(scaledStroke.fX);
1868 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001869
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001870 if (isStrokeOnly) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001871 innerRadius = xRadius - halfWidth;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001872 }
1873 outerRadius += halfWidth;
joshualittd96a67b2015-05-05 14:09:05 -07001874 bounds.outset(halfWidth, halfWidth);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001875 }
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001876
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001877 isStrokeOnly = (isStrokeOnly && innerRadius >= 0);
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +00001878
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001879 // The radii are outset for two reasons. First, it allows the shader to simply perform
bsalomonce1c8862014-12-15 07:11:22 -08001880 // simpler computation because the computed alpha is zero, rather than 50%, at the radius.
1881 // Second, the outer radius is used to compute the verts of the bounding box that is
1882 // rendered and the outset ensures the box will cover all partially covered by the rrect
1883 // corners.
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001884 outerRadius += SK_ScalarHalf;
1885 innerRadius -= SK_ScalarHalf;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001886
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001887 // Expand the rect so all the pixels will be captured.
joshualittd96a67b2015-05-05 14:09:05 -07001888 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001889
joshualitt76e7fb62015-02-11 08:52:27 -08001890 RRectCircleRendererBatch::Geometry geometry;
1891 geometry.fViewMatrix = viewMatrix;
1892 geometry.fColor = color;
1893 geometry.fInnerRadius = innerRadius;
1894 geometry.fOuterRadius = outerRadius;
1895 geometry.fStroke = isStrokeOnly;
joshualittd96a67b2015-05-05 14:09:05 -07001896 geometry.fDevBounds = bounds;
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +00001897
bsalomoned0bcad2015-05-04 10:36:42 -07001898 return RRectCircleRendererBatch::Create(geometry);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001899 // otherwise we use the ellipse renderer
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001900 } else {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001901 SkScalar innerXRadius = 0.0f;
1902 SkScalar innerYRadius = 0.0f;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001903 if (hasStroke) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001904 if (SkScalarNearlyZero(scaledStroke.length())) {
1905 scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf);
1906 } else {
1907 scaledStroke.scale(SK_ScalarHalf);
1908 }
1909
1910 // we only handle thick strokes for near-circular ellipses
skia.committer@gmail.com8be02fc2013-05-17 07:01:11 +00001911 if (scaledStroke.length() > SK_ScalarHalf &&
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001912 (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) {
joshualitt3e708c52015-04-30 13:49:27 -07001913 return NULL;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001914 }
1915
1916 // we don't handle it if curvature of the stroke is less than curvature of the ellipse
1917 if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius ||
1918 scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) {
joshualitt3e708c52015-04-30 13:49:27 -07001919 return NULL;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001920 }
1921
1922 // this is legit only if scale & translation (which should be the case at the moment)
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001923 if (isStrokeOnly) {
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001924 innerXRadius = xRadius - scaledStroke.fX;
1925 innerYRadius = yRadius - scaledStroke.fY;
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001926 }
1927
1928 xRadius += scaledStroke.fX;
1929 yRadius += scaledStroke.fY;
joshualittd96a67b2015-05-05 14:09:05 -07001930 bounds.outset(scaledStroke.fX, scaledStroke.fY);
commit-bot@chromium.org6bb3efc2013-05-16 13:14:46 +00001931 }
jvanverth@google.come3647412013-05-08 15:31:43 +00001932
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001933 isStrokeOnly = (isStrokeOnly && innerXRadius >= 0 && innerYRadius >= 0);
commit-bot@chromium.orgcefde6e2013-08-30 16:34:52 +00001934
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001935 // Expand the rect so all the pixels will be captured.
joshualittd96a67b2015-05-05 14:09:05 -07001936 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001937
joshualitt76e7fb62015-02-11 08:52:27 -08001938 RRectEllipseRendererBatch::Geometry geometry;
1939 geometry.fViewMatrix = viewMatrix;
1940 geometry.fColor = color;
1941 geometry.fXRadius = xRadius;
1942 geometry.fYRadius = yRadius;
1943 geometry.fInnerXRadius = innerXRadius;
1944 geometry.fInnerYRadius = innerYRadius;
1945 geometry.fStroke = isStrokeOnly;
joshualittd96a67b2015-05-05 14:09:05 -07001946 geometry.fDevBounds = bounds;
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001947
bsalomoned0bcad2015-05-04 10:36:42 -07001948 return RRectEllipseRendererBatch::Create(geometry);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001949 }
joshualitt3e708c52015-04-30 13:49:27 -07001950}
1951
robertphillipsea461502015-05-26 11:38:03 -07001952bool GrOvalRenderer::DrawRRect(GrDrawTarget* target,
joshualittae3d63a2015-07-13 08:44:06 -07001953 const GrPipelineBuilder& pipelineBuilder,
joshualitt3e708c52015-04-30 13:49:27 -07001954 GrColor color,
1955 const SkMatrix& viewMatrix,
1956 bool useAA,
1957 const SkRRect& rrect,
1958 const SkStrokeRec& stroke) {
1959 if (rrect.isOval()) {
robertphillipsea461502015-05-26 11:38:03 -07001960 return DrawOval(target, pipelineBuilder, color, viewMatrix, useAA, rrect.getBounds(),
1961 stroke);
joshualitt3e708c52015-04-30 13:49:27 -07001962 }
1963
joshualittae3d63a2015-07-13 08:44:06 -07001964 bool useCoverageAA = useAA && !pipelineBuilder.getRenderTarget()->isUnifiedMultisampled();
joshualitt3e708c52015-04-30 13:49:27 -07001965
1966 // only anti-aliased rrects for now
1967 if (!useCoverageAA) {
1968 return false;
1969 }
1970
1971 if (!viewMatrix.rectStaysRect() || !rrect.isSimple()) {
1972 return false;
1973 }
1974
joshualittd96a67b2015-05-05 14:09:05 -07001975 SkAutoTUnref<GrBatch> batch(create_rrect_batch(color, viewMatrix, rrect, stroke));
joshualitt3e708c52015-04-30 13:49:27 -07001976 if (!batch) {
1977 return false;
1978 }
1979
joshualittae3d63a2015-07-13 08:44:06 -07001980 target->drawBatch(pipelineBuilder, batch);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001981 return true;
1982}
joshualitt3e708c52015-04-30 13:49:27 -07001983
1984///////////////////////////////////////////////////////////////////////////////////////////////////
1985
1986#ifdef GR_TEST_UTILS
1987
joshualitt3e708c52015-04-30 13:49:27 -07001988BATCH_TEST_DEFINE(CircleBatch) {
1989 SkMatrix viewMatrix = GrTest::TestMatrix(random);
1990 GrColor color = GrRandomColor(random);
1991 bool useCoverageAA = random->nextBool();
joshualitt6c891102015-05-13 08:51:49 -07001992 SkRect circle = GrTest::TestSquare(random);
joshualitt21279c72015-05-11 07:21:37 -07001993 return create_circle_batch(color, viewMatrix, useCoverageAA, circle,
1994 GrTest::TestStrokeRec(random));
joshualitt3e708c52015-04-30 13:49:27 -07001995}
1996
1997BATCH_TEST_DEFINE(EllipseBatch) {
1998 SkMatrix viewMatrix = GrTest::TestMatrixRectStaysRect(random);
1999 GrColor color = GrRandomColor(random);
joshualitt6c891102015-05-13 08:51:49 -07002000 SkRect ellipse = GrTest::TestSquare(random);
2001 return create_ellipse_batch(color, viewMatrix, true, ellipse,
joshualitt21279c72015-05-11 07:21:37 -07002002 GrTest::TestStrokeRec(random));
joshualitt3e708c52015-04-30 13:49:27 -07002003}
2004
2005BATCH_TEST_DEFINE(DIEllipseBatch) {
2006 SkMatrix viewMatrix = GrTest::TestMatrix(random);
2007 GrColor color = GrRandomColor(random);
2008 bool useCoverageAA = random->nextBool();
joshualitt6c891102015-05-13 08:51:49 -07002009 SkRect ellipse = GrTest::TestSquare(random);
joshualitt3e708c52015-04-30 13:49:27 -07002010 return create_diellipse_batch(color, viewMatrix, useCoverageAA, ellipse,
joshualitt21279c72015-05-11 07:21:37 -07002011 GrTest::TestStrokeRec(random));
joshualitt3e708c52015-04-30 13:49:27 -07002012}
2013
2014BATCH_TEST_DEFINE(RRectBatch) {
2015 SkMatrix viewMatrix = GrTest::TestMatrixRectStaysRect(random);
2016 GrColor color = GrRandomColor(random);
2017 const SkRRect& rrect = GrTest::TestRRectSimple(random);
joshualitt21279c72015-05-11 07:21:37 -07002018 return create_rrect_batch(color, viewMatrix, rrect, GrTest::TestStrokeRec(random));
joshualitt3e708c52015-04-30 13:49:27 -07002019}
2020
2021#endif