commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 10 | #include "GrBatchFlushState.h" |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 11 | #include "GrBatchTest.h" |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 12 | #include "GrDrawTarget.h" |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 13 | #include "GrGeometryProcessor.h" |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 14 | #include "GrInvariantOutput.h" |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 15 | #include "GrPipelineBuilder.h" |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 16 | #include "GrProcessor.h" |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 17 | #include "GrResourceProvider.h" |
bsalomon | 72e3ae4 | 2015-04-28 08:08:46 -0700 | [diff] [blame] | 18 | #include "GrVertexBuffer.h" |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 19 | #include "SkRRect.h" |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 20 | #include "SkStrokeRec.h" |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 21 | #include "SkTLazy.h" |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 22 | #include "batches/GrVertexBatch.h" |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 23 | #include "effects/GrRRectEffect.h" |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 24 | #include "gl/GrGLProcessor.h" |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 25 | #include "gl/GrGLGeometryProcessor.h" |
| 26 | #include "gl/builders/GrGLProgramBuilder.h" |
commit-bot@chromium.org | 234d4fb | 2013-09-30 19:55:49 +0000 | [diff] [blame] | 27 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 28 | // TODO(joshualitt) - Break this file up during GrBatch post implementation cleanup |
| 29 | |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 30 | namespace { |
joshualitt | 5ead6da | 2014-10-22 16:00:29 -0700 | [diff] [blame] | 31 | // TODO(joshualitt) add per vertex colors |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 32 | struct CircleVertex { |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 33 | SkPoint fPos; |
| 34 | SkPoint fOffset; |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 35 | SkScalar fOuterRadius; |
| 36 | SkScalar fInnerRadius; |
| 37 | }; |
| 38 | |
| 39 | struct EllipseVertex { |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 40 | SkPoint fPos; |
| 41 | SkPoint fOffset; |
| 42 | SkPoint fOuterRadii; |
| 43 | SkPoint fInnerRadii; |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 44 | }; |
| 45 | |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 46 | struct DIEllipseVertex { |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 47 | SkPoint fPos; |
| 48 | SkPoint fOuterOffset; |
| 49 | SkPoint fInnerOffset; |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 50 | }; |
| 51 | |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 52 | inline bool circle_stays_circle(const SkMatrix& m) { |
| 53 | return m.isSimilarity(); |
| 54 | } |
| 55 | |
| 56 | } |
| 57 | |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 58 | /////////////////////////////////////////////////////////////////////////////// |
| 59 | |
| 60 | /** |
bsalomon | ce1c886 | 2014-12-15 07:11:22 -0800 | [diff] [blame] | 61 | * 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.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 69 | */ |
| 70 | |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 71 | class CircleEdgeEffect : public GrGeometryProcessor { |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 72 | public: |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 73 | static GrGeometryProcessor* Create(GrColor color, bool stroke, const SkMatrix& localMatrix, |
| 74 | bool usesLocalCoords) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 75 | return new CircleEdgeEffect(color, stroke, localMatrix, usesLocalCoords); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 76 | } |
| 77 | |
joshualitt | 71c9260 | 2015-01-14 08:12:47 -0800 | [diff] [blame] | 78 | const Attribute* inPosition() const { return fInPosition; } |
| 79 | const Attribute* inCircleEdge() const { return fInCircleEdge; } |
joshualitt | 88c23fc | 2015-05-13 14:18:07 -0700 | [diff] [blame] | 80 | GrColor color() const { return fColor; } |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 81 | bool colorIgnored() const { return GrColor_ILLEGAL == fColor; } |
joshualitt | e3ababe | 2015-05-15 07:56:07 -0700 | [diff] [blame] | 82 | const SkMatrix& localMatrix() const { return fLocalMatrix; } |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 83 | bool usesLocalCoords() const { return fUsesLocalCoords; } |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 84 | virtual ~CircleEdgeEffect() {} |
| 85 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 86 | const char* name() const override { return "CircleEdge"; } |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 87 | |
| 88 | inline bool isStroked() const { return fStroke; } |
| 89 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 90 | class GLProcessor : public GrGLGeometryProcessor { |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 91 | public: |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 92 | GLProcessor() |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 93 | : fColor(GrColor_ILLEGAL) {} |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 94 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 95 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{ |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 96 | const CircleEdgeEffect& ce = args.fGP.cast<CircleEdgeEffect>(); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 97 | GrGLGPBuilder* pb = args.fPB; |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 98 | GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder(); |
| 99 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 100 | // emit attributes |
| 101 | vsBuilder->emitAttributes(ce); |
| 102 | |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 103 | GrGLVertToFrag v(kVec4f_GrSLType); |
| 104 | args.fPB->addVarying("CircleEdge", &v); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 105 | vsBuilder->codeAppendf("%s = %s;", v.vsOut(), ce.inCircleEdge()->fName); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 106 | |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 107 | // setup pass through color |
| 108 | if (!ce.colorIgnored()) { |
| 109 | this->setupUniformColor(pb, args.fOutputColor, &fColorUniform); |
| 110 | } |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 111 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 112 | // Setup position |
joshualitt | e578a95 | 2015-05-14 10:09:13 -0700 | [diff] [blame] | 113 | this->setupPosition(pb, gpArgs, ce.inPosition()->fName); |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 114 | |
| 115 | // emit transforms |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 116 | this->emitTransforms(args.fPB, gpArgs->fPositionVar, ce.inPosition()->fName, |
tfarina | 567ff2f | 2015-04-27 07:01:44 -0700 | [diff] [blame] | 117 | ce.localMatrix(), args.fTransformsIn, args.fTransformsOut); |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 118 | |
egdaniel | 29bee0f | 2015-04-29 11:54:42 -0700 | [diff] [blame] | 119 | GrGLFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder(); |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 120 | fsBuilder->codeAppendf("float d = length(%s.xy);", v.fsIn()); |
bsalomon | ce1c886 | 2014-12-15 07:11:22 -0800 | [diff] [blame] | 121 | fsBuilder->codeAppendf("float edgeAlpha = clamp(%s.z * (1.0 - d), 0.0, 1.0);", v.fsIn()); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 122 | if (ce.isStroked()) { |
bsalomon | ce1c886 | 2014-12-15 07:11:22 -0800 | [diff] [blame] | 123 | fsBuilder->codeAppendf("float innerAlpha = clamp(%s.z * (d - %s.w), 0.0, 1.0);", |
| 124 | v.fsIn(), v.fsIn()); |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 125 | fsBuilder->codeAppend("edgeAlpha *= innerAlpha;"); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 126 | } |
commit-bot@chromium.org | 0a6cb60 | 2013-04-11 15:05:37 +0000 | [diff] [blame] | 127 | |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 128 | fsBuilder->codeAppendf("%s = vec4(edgeAlpha);", args.fOutputCoverage); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 129 | } |
| 130 | |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 131 | static void GenKey(const GrGeometryProcessor& gp, |
jvanverth | cfc1886 | 2015-04-28 08:48:20 -0700 | [diff] [blame] | 132 | const GrGLSLCaps&, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 133 | GrProcessorKeyBuilder* b) { |
joshualitt | e3ababe | 2015-05-15 07:56:07 -0700 | [diff] [blame] | 134 | const CircleEdgeEffect& ce = gp.cast<CircleEdgeEffect>(); |
| 135 | uint16_t key = ce.isStroked() ? 0x1 : 0x0; |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 136 | key |= ce.usesLocalCoords() && ce.localMatrix().hasPerspective() ? 0x2 : 0x0; |
| 137 | key |= ce.colorIgnored() ? 0x4 : 0x0; |
| 138 | b->add32(key); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 139 | } |
| 140 | |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 141 | void setData(const GrGLProgramDataManager& pdman, const GrPrimitiveProcessor& gp) override { |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 142 | const CircleEdgeEffect& ce = gp.cast<CircleEdgeEffect>(); |
| 143 | if (ce.color() != fColor) { |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 144 | GrGLfloat c[4]; |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 145 | GrColorToRGBAFloat(ce.color(), c); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 146 | pdman.set4fv(fColorUniform, 1, c); |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 147 | fColor = ce.color(); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 148 | } |
| 149 | } |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 150 | |
joshualitt | e3ababe | 2015-05-15 07:56:07 -0700 | [diff] [blame] | 151 | void setTransformData(const GrPrimitiveProcessor& primProc, |
| 152 | const GrGLProgramDataManager& pdman, |
| 153 | int index, |
| 154 | const SkTArray<const GrCoordTransform*, true>& transforms) override { |
| 155 | this->setTransformDataHelper<CircleEdgeEffect>(primProc, pdman, index, transforms); |
| 156 | } |
| 157 | |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 158 | private: |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 159 | GrColor fColor; |
| 160 | UniformHandle fColorUniform; |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 161 | typedef GrGLGeometryProcessor INHERITED; |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 162 | }; |
| 163 | |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 164 | void getGLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override { |
| 165 | GLProcessor::GenKey(*this, caps, b); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 166 | } |
| 167 | |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 168 | GrGLPrimitiveProcessor* createGLInstance(const GrGLSLCaps&) const override { |
| 169 | return new GLProcessor(); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 170 | } |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 171 | |
| 172 | private: |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 173 | CircleEdgeEffect(GrColor color, bool stroke, const SkMatrix& localMatrix, bool usesLocalCoords) |
joshualitt | e3ababe | 2015-05-15 07:56:07 -0700 | [diff] [blame] | 174 | : fColor(color) |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 175 | , fLocalMatrix(localMatrix) |
| 176 | , fUsesLocalCoords(usesLocalCoords) { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 177 | this->initClassID<CircleEdgeEffect>(); |
senorblanco | f2539d5 | 2015-05-20 14:03:42 -0700 | [diff] [blame] | 178 | fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType, |
| 179 | kHigh_GrSLPrecision)); |
joshualitt | 71c9260 | 2015-01-14 08:12:47 -0800 | [diff] [blame] | 180 | fInCircleEdge = &this->addVertexAttrib(Attribute("inCircleEdge", |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 181 | kVec4f_GrVertexAttribType)); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 182 | fStroke = stroke; |
| 183 | } |
| 184 | |
joshualitt | 88c23fc | 2015-05-13 14:18:07 -0700 | [diff] [blame] | 185 | GrColor fColor; |
joshualitt | e3ababe | 2015-05-15 07:56:07 -0700 | [diff] [blame] | 186 | SkMatrix fLocalMatrix; |
joshualitt | 71c9260 | 2015-01-14 08:12:47 -0800 | [diff] [blame] | 187 | const Attribute* fInPosition; |
| 188 | const Attribute* fInCircleEdge; |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 189 | bool fStroke; |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 190 | bool fUsesLocalCoords; |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 191 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 192 | GR_DECLARE_GEOMETRY_PROCESSOR_TEST; |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 193 | |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 194 | typedef GrGeometryProcessor INHERITED; |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 195 | }; |
| 196 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 197 | GR_DEFINE_GEOMETRY_PROCESSOR_TEST(CircleEdgeEffect); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 198 | |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 199 | const GrGeometryProcessor* CircleEdgeEffect::TestCreate(GrProcessorTestData* d) { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 200 | return CircleEdgeEffect::Create(GrRandomColor(d->fRandom), |
| 201 | d->fRandom->nextBool(), |
| 202 | GrTest::TestMatrix(d->fRandom), |
| 203 | d->fRandom->nextBool()); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | /////////////////////////////////////////////////////////////////////////////// |
| 207 | |
| 208 | /** |
| 209 | * The output of this effect is a modulation of the input color and coverage for an axis-aligned |
skia.committer@gmail.com | 8be02fc | 2013-05-17 07:01:11 +0000 | [diff] [blame] | 210 | * ellipse, specified as a 2D offset from center, and the reciprocals of the outer and inner radii, |
| 211 | * in both x and y directions. |
| 212 | * |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 213 | * We are using an implicit function of x^2/a^2 + y^2/b^2 - 1 = 0. |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 214 | */ |
| 215 | |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 216 | class EllipseEdgeEffect : public GrGeometryProcessor { |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 217 | public: |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 218 | static GrGeometryProcessor* Create(GrColor color, bool stroke, const SkMatrix& localMatrix, |
| 219 | bool usesLocalCoords) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 220 | return new EllipseEdgeEffect(color, stroke, localMatrix, usesLocalCoords); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 221 | } |
| 222 | |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 223 | virtual ~EllipseEdgeEffect() {} |
| 224 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 225 | const char* name() const override { return "EllipseEdge"; } |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 226 | |
joshualitt | 71c9260 | 2015-01-14 08:12:47 -0800 | [diff] [blame] | 227 | const Attribute* inPosition() const { return fInPosition; } |
| 228 | const Attribute* inEllipseOffset() const { return fInEllipseOffset; } |
| 229 | const Attribute* inEllipseRadii() const { return fInEllipseRadii; } |
joshualitt | 88c23fc | 2015-05-13 14:18:07 -0700 | [diff] [blame] | 230 | GrColor color() const { return fColor; } |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 231 | bool colorIgnored() const { return GrColor_ILLEGAL == fColor; } |
joshualitt | e3ababe | 2015-05-15 07:56:07 -0700 | [diff] [blame] | 232 | const SkMatrix& localMatrix() const { return fLocalMatrix; } |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 233 | bool usesLocalCoords() const { return fUsesLocalCoords; } |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 234 | |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 235 | inline bool isStroked() const { return fStroke; } |
| 236 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 237 | class GLProcessor : public GrGLGeometryProcessor { |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 238 | public: |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 239 | GLProcessor() |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 240 | : fColor(GrColor_ILLEGAL) {} |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 241 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 242 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{ |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 243 | const EllipseEdgeEffect& ee = args.fGP.cast<EllipseEdgeEffect>(); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 244 | GrGLGPBuilder* pb = args.fPB; |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 245 | GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder(); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 246 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 247 | // emit attributes |
| 248 | vsBuilder->emitAttributes(ee); |
| 249 | |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 250 | GrGLVertToFrag ellipseOffsets(kVec2f_GrSLType); |
| 251 | args.fPB->addVarying("EllipseOffsets", &ellipseOffsets); |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 252 | vsBuilder->codeAppendf("%s = %s;", ellipseOffsets.vsOut(), |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 253 | ee.inEllipseOffset()->fName); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 254 | |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 255 | GrGLVertToFrag ellipseRadii(kVec4f_GrSLType); |
| 256 | args.fPB->addVarying("EllipseRadii", &ellipseRadii); |
| 257 | vsBuilder->codeAppendf("%s = %s;", ellipseRadii.vsOut(), |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 258 | ee.inEllipseRadii()->fName); |
| 259 | |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 260 | // setup pass through color |
| 261 | if (!ee.colorIgnored()) { |
| 262 | this->setupUniformColor(pb, args.fOutputColor, &fColorUniform); |
| 263 | } |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 264 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 265 | // Setup position |
joshualitt | e578a95 | 2015-05-14 10:09:13 -0700 | [diff] [blame] | 266 | this->setupPosition(pb, gpArgs, ee.inPosition()->fName); |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 267 | |
| 268 | // emit transforms |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 269 | this->emitTransforms(args.fPB, gpArgs->fPositionVar, ee.inPosition()->fName, |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 270 | ee.localMatrix(), args.fTransformsIn, args.fTransformsOut); |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 271 | |
skia.committer@gmail.com | 8be02fc | 2013-05-17 07:01:11 +0000 | [diff] [blame] | 272 | // for outer curve |
egdaniel | 29bee0f | 2015-04-29 11:54:42 -0700 | [diff] [blame] | 273 | GrGLFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder(); |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 274 | fsBuilder->codeAppendf("vec2 scaledOffset = %s*%s.xy;", ellipseOffsets.fsIn(), |
| 275 | ellipseRadii.fsIn()); |
| 276 | fsBuilder->codeAppend("float test = dot(scaledOffset, scaledOffset) - 1.0;"); |
| 277 | fsBuilder->codeAppendf("vec2 grad = 2.0*scaledOffset*%s.xy;", ellipseRadii.fsIn()); |
| 278 | fsBuilder->codeAppend("float grad_dot = dot(grad, grad);"); |
| 279 | |
commit-bot@chromium.org | 1b035d8 | 2014-04-09 17:11:09 +0000 | [diff] [blame] | 280 | // avoid calling inversesqrt on zero. |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 281 | fsBuilder->codeAppend("grad_dot = max(grad_dot, 1.0e-4);"); |
| 282 | fsBuilder->codeAppend("float invlen = inversesqrt(grad_dot);"); |
| 283 | fsBuilder->codeAppend("float edgeAlpha = clamp(0.5-test*invlen, 0.0, 1.0);"); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 284 | |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 285 | // for inner curve |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 286 | if (ee.isStroked()) { |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 287 | fsBuilder->codeAppendf("scaledOffset = %s*%s.zw;", |
| 288 | ellipseOffsets.fsIn(), ellipseRadii.fsIn()); |
| 289 | fsBuilder->codeAppend("test = dot(scaledOffset, scaledOffset) - 1.0;"); |
| 290 | fsBuilder->codeAppendf("grad = 2.0*scaledOffset*%s.zw;", |
| 291 | ellipseRadii.fsIn()); |
| 292 | fsBuilder->codeAppend("invlen = inversesqrt(dot(grad, grad));"); |
| 293 | fsBuilder->codeAppend("edgeAlpha *= clamp(0.5+test*invlen, 0.0, 1.0);"); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 294 | } |
| 295 | |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 296 | fsBuilder->codeAppendf("%s = vec4(edgeAlpha);", args.fOutputCoverage); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 297 | } |
| 298 | |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 299 | static void GenKey(const GrGeometryProcessor& gp, |
jvanverth | cfc1886 | 2015-04-28 08:48:20 -0700 | [diff] [blame] | 300 | const GrGLSLCaps&, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 301 | GrProcessorKeyBuilder* b) { |
joshualitt | e3ababe | 2015-05-15 07:56:07 -0700 | [diff] [blame] | 302 | const EllipseEdgeEffect& ee = gp.cast<EllipseEdgeEffect>(); |
| 303 | uint16_t key = ee.isStroked() ? 0x1 : 0x0; |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 304 | key |= ee.usesLocalCoords() && ee.localMatrix().hasPerspective() ? 0x2 : 0x0; |
| 305 | key |= ee.colorIgnored() ? 0x4 : 0x0; |
| 306 | b->add32(key); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 307 | } |
| 308 | |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 309 | void setData(const GrGLProgramDataManager& pdman, const GrPrimitiveProcessor& gp) override { |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 310 | const EllipseEdgeEffect& ee = gp.cast<EllipseEdgeEffect>(); |
| 311 | if (ee.color() != fColor) { |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 312 | GrGLfloat c[4]; |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 313 | GrColorToRGBAFloat(ee.color(), c); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 314 | pdman.set4fv(fColorUniform, 1, c); |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 315 | fColor = ee.color(); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 316 | } |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 317 | } |
| 318 | |
joshualitt | e3ababe | 2015-05-15 07:56:07 -0700 | [diff] [blame] | 319 | void setTransformData(const GrPrimitiveProcessor& primProc, |
| 320 | const GrGLProgramDataManager& pdman, |
| 321 | int index, |
| 322 | const SkTArray<const GrCoordTransform*, true>& transforms) override { |
| 323 | this->setTransformDataHelper<EllipseEdgeEffect>(primProc, pdman, index, transforms); |
| 324 | } |
| 325 | |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 326 | private: |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 327 | GrColor fColor; |
| 328 | UniformHandle fColorUniform; |
| 329 | |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 330 | typedef GrGLGeometryProcessor INHERITED; |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 331 | }; |
| 332 | |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 333 | void getGLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override { |
| 334 | GLProcessor::GenKey(*this, caps, b); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 335 | } |
| 336 | |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 337 | GrGLPrimitiveProcessor* createGLInstance(const GrGLSLCaps&) const override { |
| 338 | return new GLProcessor(); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 339 | } |
| 340 | |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 341 | private: |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 342 | EllipseEdgeEffect(GrColor color, bool stroke, const SkMatrix& localMatrix, |
| 343 | bool usesLocalCoords) |
joshualitt | e3ababe | 2015-05-15 07:56:07 -0700 | [diff] [blame] | 344 | : fColor(color) |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 345 | , fLocalMatrix(localMatrix) |
| 346 | , fUsesLocalCoords(usesLocalCoords) { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 347 | this->initClassID<EllipseEdgeEffect>(); |
joshualitt | 71c9260 | 2015-01-14 08:12:47 -0800 | [diff] [blame] | 348 | fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType)); |
| 349 | fInEllipseOffset = &this->addVertexAttrib(Attribute("inEllipseOffset", |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 350 | kVec2f_GrVertexAttribType)); |
joshualitt | 71c9260 | 2015-01-14 08:12:47 -0800 | [diff] [blame] | 351 | fInEllipseRadii = &this->addVertexAttrib(Attribute("inEllipseRadii", |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 352 | kVec4f_GrVertexAttribType)); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 353 | fStroke = stroke; |
| 354 | } |
| 355 | |
joshualitt | 71c9260 | 2015-01-14 08:12:47 -0800 | [diff] [blame] | 356 | const Attribute* fInPosition; |
| 357 | const Attribute* fInEllipseOffset; |
| 358 | const Attribute* fInEllipseRadii; |
joshualitt | 88c23fc | 2015-05-13 14:18:07 -0700 | [diff] [blame] | 359 | GrColor fColor; |
joshualitt | e3ababe | 2015-05-15 07:56:07 -0700 | [diff] [blame] | 360 | SkMatrix fLocalMatrix; |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 361 | bool fStroke; |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 362 | bool fUsesLocalCoords; |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 363 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 364 | GR_DECLARE_GEOMETRY_PROCESSOR_TEST; |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 365 | |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 366 | typedef GrGeometryProcessor INHERITED; |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 367 | }; |
| 368 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 369 | GR_DEFINE_GEOMETRY_PROCESSOR_TEST(EllipseEdgeEffect); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 370 | |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 371 | const GrGeometryProcessor* EllipseEdgeEffect::TestCreate(GrProcessorTestData* d) { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 372 | return EllipseEdgeEffect::Create(GrRandomColor(d->fRandom), |
| 373 | d->fRandom->nextBool(), |
| 374 | GrTest::TestMatrix(d->fRandom), |
| 375 | d->fRandom->nextBool()); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | /////////////////////////////////////////////////////////////////////////////// |
| 379 | |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 380 | /** |
skia.committer@gmail.com | 6fc1b49 | 2013-09-06 07:01:45 +0000 | [diff] [blame] | 381 | * The output of this effect is a modulation of the input color and coverage for an ellipse, |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 382 | * specified as a 2D offset from center for both the outer and inner paths (if stroked). The |
| 383 | * implict equation used is for a unit circle (x^2 + y^2 - 1 = 0) and the edge corrected by |
| 384 | * using differentials. |
| 385 | * |
| 386 | * The result is device-independent and can be used with any affine matrix. |
| 387 | */ |
| 388 | |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 389 | class DIEllipseEdgeEffect : public GrGeometryProcessor { |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 390 | public: |
| 391 | enum Mode { kStroke = 0, kHairline, kFill }; |
| 392 | |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 393 | static GrGeometryProcessor* Create(GrColor color, const SkMatrix& viewMatrix, Mode mode, |
| 394 | bool usesLocalCoords) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 395 | return new DIEllipseEdgeEffect(color, viewMatrix, mode, usesLocalCoords); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 396 | } |
| 397 | |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 398 | virtual ~DIEllipseEdgeEffect() {} |
| 399 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 400 | const char* name() const override { return "DIEllipseEdge"; } |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 401 | |
joshualitt | 71c9260 | 2015-01-14 08:12:47 -0800 | [diff] [blame] | 402 | const Attribute* inPosition() const { return fInPosition; } |
| 403 | const Attribute* inEllipseOffsets0() const { return fInEllipseOffsets0; } |
| 404 | const Attribute* inEllipseOffsets1() const { return fInEllipseOffsets1; } |
joshualitt | 88c23fc | 2015-05-13 14:18:07 -0700 | [diff] [blame] | 405 | GrColor color() const { return fColor; } |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 406 | bool colorIgnored() const { return GrColor_ILLEGAL == fColor; } |
joshualitt | e578a95 | 2015-05-14 10:09:13 -0700 | [diff] [blame] | 407 | const SkMatrix& viewMatrix() const { return fViewMatrix; } |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 408 | bool usesLocalCoords() const { return fUsesLocalCoords; } |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 409 | |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 410 | inline Mode getMode() const { return fMode; } |
| 411 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 412 | class GLProcessor : public GrGLGeometryProcessor { |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 413 | public: |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 414 | GLProcessor() |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 415 | : fViewMatrix(SkMatrix::InvalidMatrix()), fColor(GrColor_ILLEGAL) {} |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 416 | |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 417 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override { |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 418 | const DIEllipseEdgeEffect& ee = args.fGP.cast<DIEllipseEdgeEffect>(); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 419 | GrGLGPBuilder* pb = args.fPB; |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 420 | GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder(); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 421 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 422 | // emit attributes |
| 423 | vsBuilder->emitAttributes(ee); |
| 424 | |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 425 | GrGLVertToFrag offsets0(kVec2f_GrSLType); |
| 426 | args.fPB->addVarying("EllipseOffsets0", &offsets0); |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 427 | vsBuilder->codeAppendf("%s = %s;", offsets0.vsOut(), |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 428 | ee.inEllipseOffsets0()->fName); |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 429 | |
| 430 | GrGLVertToFrag offsets1(kVec2f_GrSLType); |
| 431 | args.fPB->addVarying("EllipseOffsets1", &offsets1); |
| 432 | vsBuilder->codeAppendf("%s = %s;", offsets1.vsOut(), |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 433 | ee.inEllipseOffsets1()->fName); |
| 434 | |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 435 | // setup pass through color |
| 436 | if (!ee.colorIgnored()) { |
| 437 | this->setupUniformColor(pb, args.fOutputColor, &fColorUniform); |
| 438 | } |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 439 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 440 | // Setup position |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 441 | this->setupPosition(pb, gpArgs, ee.inPosition()->fName, ee.viewMatrix(), |
| 442 | &fViewMatrixUniform); |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 443 | |
| 444 | // emit transforms |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 445 | this->emitTransforms(args.fPB, gpArgs->fPositionVar, ee.inPosition()->fName, |
joshualitt | e3ababe | 2015-05-15 07:56:07 -0700 | [diff] [blame] | 446 | args.fTransformsIn, args.fTransformsOut); |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 447 | |
egdaniel | 29bee0f | 2015-04-29 11:54:42 -0700 | [diff] [blame] | 448 | GrGLFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder(); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 449 | SkAssertResult(fsBuilder->enableFeature( |
| 450 | GrGLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature)); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 451 | // for outer curve |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 452 | fsBuilder->codeAppendf("vec2 scaledOffset = %s.xy;", offsets0.fsIn()); |
| 453 | fsBuilder->codeAppend("float test = dot(scaledOffset, scaledOffset) - 1.0;"); |
| 454 | fsBuilder->codeAppendf("vec2 duvdx = dFdx(%s);", offsets0.fsIn()); |
| 455 | fsBuilder->codeAppendf("vec2 duvdy = dFdy(%s);", offsets0.fsIn()); |
| 456 | fsBuilder->codeAppendf("vec2 grad = vec2(2.0*%s.x*duvdx.x + 2.0*%s.y*duvdx.y," |
| 457 | " 2.0*%s.x*duvdy.x + 2.0*%s.y*duvdy.y);", |
| 458 | offsets0.fsIn(), offsets0.fsIn(), offsets0.fsIn(), offsets0.fsIn()); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 459 | |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 460 | fsBuilder->codeAppend("float grad_dot = dot(grad, grad);"); |
commit-bot@chromium.org | 1b035d8 | 2014-04-09 17:11:09 +0000 | [diff] [blame] | 461 | // avoid calling inversesqrt on zero. |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 462 | fsBuilder->codeAppend("grad_dot = max(grad_dot, 1.0e-4);"); |
| 463 | fsBuilder->codeAppend("float invlen = inversesqrt(grad_dot);"); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 464 | if (kHairline == ee.getMode()) { |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 465 | // can probably do this with one step |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 466 | fsBuilder->codeAppend("float edgeAlpha = clamp(1.0-test*invlen, 0.0, 1.0);"); |
| 467 | fsBuilder->codeAppend("edgeAlpha *= clamp(1.0+test*invlen, 0.0, 1.0);"); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 468 | } else { |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 469 | fsBuilder->codeAppend("float edgeAlpha = clamp(0.5-test*invlen, 0.0, 1.0);"); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | // for inner curve |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 473 | if (kStroke == ee.getMode()) { |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 474 | fsBuilder->codeAppendf("scaledOffset = %s.xy;", offsets1.fsIn()); |
| 475 | fsBuilder->codeAppend("test = dot(scaledOffset, scaledOffset) - 1.0;"); |
| 476 | fsBuilder->codeAppendf("duvdx = dFdx(%s);", offsets1.fsIn()); |
| 477 | fsBuilder->codeAppendf("duvdy = dFdy(%s);", offsets1.fsIn()); |
| 478 | fsBuilder->codeAppendf("grad = vec2(2.0*%s.x*duvdx.x + 2.0*%s.y*duvdx.y," |
| 479 | " 2.0*%s.x*duvdy.x + 2.0*%s.y*duvdy.y);", |
| 480 | offsets1.fsIn(), offsets1.fsIn(), offsets1.fsIn(), |
| 481 | offsets1.fsIn()); |
| 482 | fsBuilder->codeAppend("invlen = inversesqrt(dot(grad, grad));"); |
| 483 | fsBuilder->codeAppend("edgeAlpha *= clamp(0.5+test*invlen, 0.0, 1.0);"); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 484 | } |
| 485 | |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 486 | fsBuilder->codeAppendf("%s = vec4(edgeAlpha);", args.fOutputCoverage); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 487 | } |
| 488 | |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 489 | static void GenKey(const GrGeometryProcessor& gp, |
jvanverth | cfc1886 | 2015-04-28 08:48:20 -0700 | [diff] [blame] | 490 | const GrGLSLCaps&, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 491 | GrProcessorKeyBuilder* b) { |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 492 | const DIEllipseEdgeEffect& ellipseEffect = gp.cast<DIEllipseEdgeEffect>(); |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 493 | uint16_t key = ellipseEffect.getMode(); |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 494 | key |= ellipseEffect.colorIgnored() << 9; |
| 495 | key |= ComputePosKey(ellipseEffect.viewMatrix()) << 10; |
| 496 | b->add32(key); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 497 | } |
| 498 | |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 499 | void setData(const GrGLProgramDataManager& pdman, const GrPrimitiveProcessor& gp) override { |
joshualitt | e578a95 | 2015-05-14 10:09:13 -0700 | [diff] [blame] | 500 | const DIEllipseEdgeEffect& dee = gp.cast<DIEllipseEdgeEffect>(); |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 501 | |
| 502 | if (!dee.viewMatrix().isIdentity() && !fViewMatrix.cheapEqualTo(dee.viewMatrix())) { |
| 503 | fViewMatrix = dee.viewMatrix(); |
| 504 | GrGLfloat viewMatrix[3 * 3]; |
| 505 | GrGLGetMatrix<3>(viewMatrix, fViewMatrix); |
| 506 | pdman.setMatrix3f(fViewMatrixUniform, viewMatrix); |
| 507 | } |
joshualitt | ee2af95 | 2014-12-30 09:04:15 -0800 | [diff] [blame] | 508 | |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 509 | if (dee.color() != fColor) { |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 510 | GrGLfloat c[4]; |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 511 | GrColorToRGBAFloat(dee.color(), c); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 512 | pdman.set4fv(fColorUniform, 1, c); |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 513 | fColor = dee.color(); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 514 | } |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | private: |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 518 | SkMatrix fViewMatrix; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 519 | GrColor fColor; |
| 520 | UniformHandle fColorUniform; |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 521 | UniformHandle fViewMatrixUniform; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 522 | |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 523 | typedef GrGLGeometryProcessor INHERITED; |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 524 | }; |
| 525 | |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 526 | void getGLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override { |
| 527 | GLProcessor::GenKey(*this, caps, b); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 528 | } |
| 529 | |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 530 | GrGLPrimitiveProcessor* createGLInstance(const GrGLSLCaps&) const override { |
| 531 | return new GLProcessor(); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 532 | } |
| 533 | |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 534 | private: |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 535 | DIEllipseEdgeEffect(GrColor color, const SkMatrix& viewMatrix, Mode mode, |
| 536 | bool usesLocalCoords) |
joshualitt | e578a95 | 2015-05-14 10:09:13 -0700 | [diff] [blame] | 537 | : fColor(color) |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 538 | , fViewMatrix(viewMatrix) |
| 539 | , fUsesLocalCoords(usesLocalCoords) { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 540 | this->initClassID<DIEllipseEdgeEffect>(); |
senorblanco | f2539d5 | 2015-05-20 14:03:42 -0700 | [diff] [blame] | 541 | fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType, |
| 542 | kHigh_GrSLPrecision)); |
joshualitt | 71c9260 | 2015-01-14 08:12:47 -0800 | [diff] [blame] | 543 | fInEllipseOffsets0 = &this->addVertexAttrib(Attribute("inEllipseOffsets0", |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 544 | kVec2f_GrVertexAttribType)); |
joshualitt | 71c9260 | 2015-01-14 08:12:47 -0800 | [diff] [blame] | 545 | fInEllipseOffsets1 = &this->addVertexAttrib(Attribute("inEllipseOffsets1", |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 546 | kVec2f_GrVertexAttribType)); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 547 | fMode = mode; |
| 548 | } |
| 549 | |
joshualitt | 71c9260 | 2015-01-14 08:12:47 -0800 | [diff] [blame] | 550 | const Attribute* fInPosition; |
| 551 | const Attribute* fInEllipseOffsets0; |
| 552 | const Attribute* fInEllipseOffsets1; |
joshualitt | 88c23fc | 2015-05-13 14:18:07 -0700 | [diff] [blame] | 553 | GrColor fColor; |
joshualitt | e578a95 | 2015-05-14 10:09:13 -0700 | [diff] [blame] | 554 | SkMatrix fViewMatrix; |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 555 | Mode fMode; |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 556 | bool fUsesLocalCoords; |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 557 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 558 | GR_DECLARE_GEOMETRY_PROCESSOR_TEST; |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 559 | |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 560 | typedef GrGeometryProcessor INHERITED; |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 561 | }; |
| 562 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 563 | GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DIEllipseEdgeEffect); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 564 | |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 565 | const GrGeometryProcessor* DIEllipseEdgeEffect::TestCreate(GrProcessorTestData* d) { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 566 | return DIEllipseEdgeEffect::Create(GrRandomColor(d->fRandom), |
| 567 | GrTest::TestMatrix(d->fRandom), |
| 568 | (Mode)(d->fRandom->nextRangeU(0,2)), |
| 569 | d->fRandom->nextBool()); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | /////////////////////////////////////////////////////////////////////////////// |
| 573 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 574 | bool GrOvalRenderer::DrawOval(GrDrawTarget* target, |
joshualitt | ae3d63a | 2015-07-13 08:44:06 -0700 | [diff] [blame] | 575 | const GrPipelineBuilder& pipelineBuilder, |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 576 | GrColor color, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 577 | const SkMatrix& viewMatrix, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 578 | bool useAA, |
| 579 | const SkRect& oval, |
joshualitt | ae3d63a | 2015-07-13 08:44:06 -0700 | [diff] [blame] | 580 | const SkStrokeRec& stroke) { |
| 581 | bool useCoverageAA = useAA && !pipelineBuilder.getRenderTarget()->isUnifiedMultisampled(); |
commit-bot@chromium.org | e0a868c | 2013-11-22 07:02:11 +0000 | [diff] [blame] | 582 | |
| 583 | if (!useCoverageAA) { |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 584 | return false; |
| 585 | } |
| 586 | |
skia.committer@gmail.com | 7e32851 | 2013-03-23 07:01:28 +0000 | [diff] [blame] | 587 | // we can draw circles |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 588 | if (SkScalarNearlyEqual(oval.width(), oval.height()) && circle_stays_circle(viewMatrix)) { |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 589 | DrawCircle(target, pipelineBuilder, color, viewMatrix, useCoverageAA, oval, stroke); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 590 | // if we have shader derivative support, render as device-independent |
jvanverth | e9c0fc6 | 2015-04-29 11:18:05 -0700 | [diff] [blame] | 591 | } else if (target->caps()->shaderCaps()->shaderDerivativeSupport()) { |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 592 | return DrawDIEllipse(target, pipelineBuilder, color, viewMatrix, useCoverageAA, oval, |
| 593 | stroke); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 594 | // otherwise axis-aligned ellipses only |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 595 | } else if (viewMatrix.rectStaysRect()) { |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 596 | return DrawEllipse(target, pipelineBuilder, color, viewMatrix, useCoverageAA, oval, |
| 597 | stroke); |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 598 | } else { |
| 599 | return false; |
| 600 | } |
| 601 | |
| 602 | return true; |
| 603 | } |
| 604 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 605 | /////////////////////////////////////////////////////////////////////////////// |
| 606 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 607 | class CircleBatch : public GrVertexBatch { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 608 | public: |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame^] | 609 | DEFINE_BATCH_CLASS_ID |
| 610 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 611 | struct Geometry { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 612 | SkMatrix fViewMatrix; |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame^] | 613 | SkRect fDevBounds; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 614 | SkScalar fInnerRadius; |
| 615 | SkScalar fOuterRadius; |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame^] | 616 | GrColor fColor; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 617 | bool fStroke; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 618 | }; |
| 619 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 620 | static GrDrawBatch* Create(const Geometry& geometry) { return new CircleBatch(geometry); } |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 621 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 622 | const char* name() const override { return "CircleBatch"; } |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 623 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 624 | void getInvariantOutputColor(GrInitInvariantOutput* out) const override { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 625 | // When this is called on a batch, there is only one geometry bundle |
| 626 | out->setKnownFourComponents(fGeoData[0].fColor); |
| 627 | } |
| 628 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 629 | void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 630 | out->setUnknownSingleComponent(); |
| 631 | } |
| 632 | |
bsalomon | e46f9fe | 2015-08-18 06:05:14 -0700 | [diff] [blame] | 633 | private: |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 634 | void initBatchTracker(const GrPipelineOptimizations& opt) override { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 635 | // Handle any color overrides |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 636 | if (!opt.readsColor()) { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 637 | fGeoData[0].fColor = GrColor_ILLEGAL; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 638 | } |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 639 | opt.getOverrideColorIfSet(&fGeoData[0].fColor); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 640 | |
| 641 | // setup batch properties |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 642 | fBatch.fColorIgnored = !opt.readsColor(); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 643 | fBatch.fColor = fGeoData[0].fColor; |
| 644 | fBatch.fStroke = fGeoData[0].fStroke; |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 645 | fBatch.fUsesLocalCoords = opt.readsLocalCoords(); |
| 646 | fBatch.fCoverageIgnored = !opt.readsCoverage(); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 647 | } |
| 648 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 649 | void onPrepareDraws(Target* target) override { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 650 | SkMatrix invert; |
| 651 | if (!this->viewMatrix().invert(&invert)) { |
| 652 | return; |
| 653 | } |
| 654 | |
| 655 | // Setup geometry processor |
| 656 | SkAutoTUnref<GrGeometryProcessor> gp(CircleEdgeEffect::Create(this->color(), |
| 657 | this->stroke(), |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 658 | invert, |
| 659 | this->usesLocalCoords())); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 660 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 661 | target->initDraw(gp, this->pipeline()); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 662 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 663 | int instanceCount = fGeoData.count(); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 664 | size_t vertexStride = gp->getVertexStride(); |
| 665 | SkASSERT(vertexStride == sizeof(CircleVertex)); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 666 | QuadHelper helper; |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 667 | CircleVertex* verts = reinterpret_cast<CircleVertex*>(helper.init(target, vertexStride, |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 668 | instanceCount)); |
| 669 | if (!verts) { |
joshualitt | 4b31de8 | 2015-03-05 14:33:41 -0800 | [diff] [blame] | 670 | return; |
| 671 | } |
| 672 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 673 | for (int i = 0; i < instanceCount; i++) { |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 674 | Geometry& geom = fGeoData[i]; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 675 | |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 676 | SkScalar innerRadius = geom.fInnerRadius; |
| 677 | SkScalar outerRadius = geom.fOuterRadius; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 678 | |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 679 | const SkRect& bounds = geom.fDevBounds; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 680 | |
| 681 | // The inner radius in the vertex data must be specified in normalized space. |
| 682 | innerRadius = innerRadius / outerRadius; |
| 683 | verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop); |
| 684 | verts[0].fOffset = SkPoint::Make(-1, -1); |
| 685 | verts[0].fOuterRadius = outerRadius; |
| 686 | verts[0].fInnerRadius = innerRadius; |
| 687 | |
| 688 | verts[1].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom); |
| 689 | verts[1].fOffset = SkPoint::Make(-1, 1); |
| 690 | verts[1].fOuterRadius = outerRadius; |
| 691 | verts[1].fInnerRadius = innerRadius; |
| 692 | |
| 693 | verts[2].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom); |
| 694 | verts[2].fOffset = SkPoint::Make(1, 1); |
| 695 | verts[2].fOuterRadius = outerRadius; |
| 696 | verts[2].fInnerRadius = innerRadius; |
| 697 | |
| 698 | verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fTop); |
| 699 | verts[3].fOffset = SkPoint::Make(1, -1); |
| 700 | verts[3].fOuterRadius = outerRadius; |
| 701 | verts[3].fInnerRadius = innerRadius; |
| 702 | |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 703 | verts += kVerticesPerQuad; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 704 | } |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 705 | helper.recordDraw(target); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; } |
| 709 | |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame^] | 710 | CircleBatch(const Geometry& geometry) : INHERITED(ClassID()) { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 711 | fGeoData.push_back(geometry); |
joshualitt | 99c7c07 | 2015-05-01 13:43:30 -0700 | [diff] [blame] | 712 | |
| 713 | this->setBounds(geometry.fDevBounds); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 714 | } |
| 715 | |
bsalomon | cb02b38 | 2015-08-12 11:14:50 -0700 | [diff] [blame] | 716 | bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override { |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 717 | CircleBatch* that = t->cast<CircleBatch>(); |
| 718 | if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(), |
| 719 | that->bounds(), caps)) { |
joshualitt | 8cab9a7 | 2015-07-16 09:13:50 -0700 | [diff] [blame] | 720 | return false; |
| 721 | } |
| 722 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 723 | // TODO use vertex color to avoid breaking batches |
| 724 | if (this->color() != that->color()) { |
| 725 | return false; |
| 726 | } |
| 727 | |
| 728 | if (this->stroke() != that->stroke()) { |
| 729 | return false; |
| 730 | } |
| 731 | |
| 732 | SkASSERT(this->usesLocalCoords() == that->usesLocalCoords()); |
| 733 | if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) { |
| 734 | return false; |
| 735 | } |
| 736 | |
| 737 | fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin()); |
joshualitt | 99c7c07 | 2015-05-01 13:43:30 -0700 | [diff] [blame] | 738 | this->joinBounds(that->bounds()); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 739 | return true; |
| 740 | } |
| 741 | |
| 742 | GrColor color() const { return fBatch.fColor; } |
| 743 | bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; } |
| 744 | const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; } |
| 745 | bool stroke() const { return fBatch.fStroke; } |
| 746 | |
| 747 | struct BatchTracker { |
| 748 | GrColor fColor; |
| 749 | bool fStroke; |
| 750 | bool fUsesLocalCoords; |
| 751 | bool fColorIgnored; |
| 752 | bool fCoverageIgnored; |
| 753 | }; |
| 754 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 755 | BatchTracker fBatch; |
| 756 | SkSTArray<1, Geometry, true> fGeoData; |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame^] | 757 | |
| 758 | typedef GrVertexBatch INHERITED; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 759 | }; |
| 760 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 761 | static GrDrawBatch* create_circle_batch(GrColor color, |
| 762 | const SkMatrix& viewMatrix, |
| 763 | bool useCoverageAA, |
| 764 | const SkRect& circle, |
| 765 | const SkStrokeRec& stroke) { |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 766 | SkPoint center = SkPoint::Make(circle.centerX(), circle.centerY()); |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 767 | viewMatrix.mapPoints(¢er, 1); |
| 768 | SkScalar radius = viewMatrix.mapRadius(SkScalarHalf(circle.width())); |
| 769 | SkScalar strokeWidth = viewMatrix.mapRadius(stroke.getWidth()); |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 770 | |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 771 | SkStrokeRec::Style style = stroke.getStyle(); |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 772 | bool isStrokeOnly = SkStrokeRec::kStroke_Style == style || |
| 773 | SkStrokeRec::kHairline_Style == style; |
| 774 | bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == style; |
skia.committer@gmail.com | 7e32851 | 2013-03-23 07:01:28 +0000 | [diff] [blame] | 775 | |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 776 | SkScalar innerRadius = 0.0f; |
| 777 | SkScalar outerRadius = radius; |
| 778 | SkScalar halfWidth = 0; |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 779 | if (hasStroke) { |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 780 | if (SkScalarNearlyZero(strokeWidth)) { |
| 781 | halfWidth = SK_ScalarHalf; |
| 782 | } else { |
| 783 | halfWidth = SkScalarHalf(strokeWidth); |
| 784 | } |
| 785 | |
| 786 | outerRadius += halfWidth; |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 787 | if (isStrokeOnly) { |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 788 | innerRadius = radius - halfWidth; |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 789 | } |
| 790 | } |
| 791 | |
bsalomon | ce1c886 | 2014-12-15 07:11:22 -0800 | [diff] [blame] | 792 | // The radii are outset for two reasons. First, it allows the shader to simply perform simpler |
| 793 | // computation because the computed alpha is zero, rather than 50%, at the radius. |
| 794 | // Second, the outer radius is used to compute the verts of the bounding box that is rendered |
| 795 | // and the outset ensures the box will cover all partially covered by the circle. |
bsalomon@google.com | 58e30fe | 2013-04-01 19:01:20 +0000 | [diff] [blame] | 796 | outerRadius += SK_ScalarHalf; |
| 797 | innerRadius -= SK_ScalarHalf; |
| 798 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 799 | CircleBatch::Geometry geometry; |
| 800 | geometry.fViewMatrix = viewMatrix; |
| 801 | geometry.fColor = color; |
| 802 | geometry.fInnerRadius = innerRadius; |
| 803 | geometry.fOuterRadius = outerRadius; |
| 804 | geometry.fStroke = isStrokeOnly && innerRadius > 0; |
joshualitt | d96a67b | 2015-05-05 14:09:05 -0700 | [diff] [blame] | 805 | geometry.fDevBounds = SkRect::MakeLTRB(center.fX - outerRadius, center.fY - outerRadius, |
| 806 | center.fX + outerRadius, center.fY + outerRadius); |
commit-bot@chromium.org | 0a6cb60 | 2013-04-11 15:05:37 +0000 | [diff] [blame] | 807 | |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 808 | return CircleBatch::Create(geometry); |
| 809 | } |
| 810 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 811 | void GrOvalRenderer::DrawCircle(GrDrawTarget* target, |
joshualitt | ae3d63a | 2015-07-13 08:44:06 -0700 | [diff] [blame] | 812 | const GrPipelineBuilder& pipelineBuilder, |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 813 | GrColor color, |
| 814 | const SkMatrix& viewMatrix, |
| 815 | bool useCoverageAA, |
| 816 | const SkRect& circle, |
| 817 | const SkStrokeRec& stroke) { |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 818 | SkAutoTUnref<GrDrawBatch> batch(create_circle_batch(color, viewMatrix, useCoverageAA, circle, |
| 819 | stroke)); |
joshualitt | ae3d63a | 2015-07-13 08:44:06 -0700 | [diff] [blame] | 820 | target->drawBatch(pipelineBuilder, batch); |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 821 | } |
| 822 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 823 | /////////////////////////////////////////////////////////////////////////////// |
| 824 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 825 | class EllipseBatch : public GrVertexBatch { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 826 | public: |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame^] | 827 | DEFINE_BATCH_CLASS_ID |
| 828 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 829 | struct Geometry { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 830 | SkMatrix fViewMatrix; |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame^] | 831 | SkRect fDevBounds; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 832 | SkScalar fXRadius; |
| 833 | SkScalar fYRadius; |
| 834 | SkScalar fInnerXRadius; |
| 835 | SkScalar fInnerYRadius; |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame^] | 836 | GrColor fColor; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 837 | bool fStroke; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 838 | }; |
| 839 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 840 | static GrDrawBatch* Create(const Geometry& geometry) { return new EllipseBatch(geometry); } |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 841 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 842 | const char* name() const override { return "EllipseBatch"; } |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 843 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 844 | void getInvariantOutputColor(GrInitInvariantOutput* out) const override { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 845 | // When this is called on a batch, there is only one geometry bundle |
| 846 | out->setKnownFourComponents(fGeoData[0].fColor); |
| 847 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 848 | void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 849 | out->setUnknownSingleComponent(); |
| 850 | } |
| 851 | |
bsalomon | e46f9fe | 2015-08-18 06:05:14 -0700 | [diff] [blame] | 852 | private: |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 853 | void initBatchTracker(const GrPipelineOptimizations& opt) override { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 854 | // Handle any color overrides |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 855 | if (!opt.readsCoverage()) { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 856 | fGeoData[0].fColor = GrColor_ILLEGAL; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 857 | } |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 858 | opt.getOverrideColorIfSet(&fGeoData[0].fColor); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 859 | |
| 860 | // setup batch properties |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 861 | fBatch.fColorIgnored = !opt.readsColor(); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 862 | fBatch.fColor = fGeoData[0].fColor; |
| 863 | fBatch.fStroke = fGeoData[0].fStroke; |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 864 | fBatch.fUsesLocalCoords = opt.readsLocalCoords(); |
| 865 | fBatch.fCoverageIgnored = !opt.readsCoverage(); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 866 | } |
| 867 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 868 | void onPrepareDraws(Target* target) override { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 869 | SkMatrix invert; |
| 870 | if (!this->viewMatrix().invert(&invert)) { |
| 871 | return; |
| 872 | } |
| 873 | |
| 874 | // Setup geometry processor |
| 875 | SkAutoTUnref<GrGeometryProcessor> gp(EllipseEdgeEffect::Create(this->color(), |
| 876 | this->stroke(), |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 877 | invert, |
| 878 | this->usesLocalCoords())); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 879 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 880 | target->initDraw(gp, this->pipeline()); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 881 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 882 | int instanceCount = fGeoData.count(); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 883 | QuadHelper helper; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 884 | size_t vertexStride = gp->getVertexStride(); |
joshualitt | 19e0058 | 2015-02-11 17:36:30 -0800 | [diff] [blame] | 885 | SkASSERT(vertexStride == sizeof(EllipseVertex)); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 886 | EllipseVertex* verts = reinterpret_cast<EllipseVertex*>( |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 887 | helper.init(target, vertexStride, instanceCount)); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 888 | if (!verts) { |
joshualitt | 4b31de8 | 2015-03-05 14:33:41 -0800 | [diff] [blame] | 889 | return; |
| 890 | } |
| 891 | |
bsalomon | 8415abe | 2015-05-04 11:41:41 -0700 | [diff] [blame] | 892 | for (int i = 0; i < instanceCount; i++) { |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 893 | Geometry& geom = fGeoData[i]; |
bsalomon | 8415abe | 2015-05-04 11:41:41 -0700 | [diff] [blame] | 894 | |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 895 | SkScalar xRadius = geom.fXRadius; |
| 896 | SkScalar yRadius = geom.fYRadius; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 897 | |
| 898 | // Compute the reciprocals of the radii here to save time in the shader |
| 899 | SkScalar xRadRecip = SkScalarInvert(xRadius); |
| 900 | SkScalar yRadRecip = SkScalarInvert(yRadius); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 901 | SkScalar xInnerRadRecip = SkScalarInvert(geom.fInnerXRadius); |
| 902 | SkScalar yInnerRadRecip = SkScalarInvert(geom.fInnerYRadius); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 903 | |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 904 | const SkRect& bounds = geom.fDevBounds; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 905 | |
| 906 | // The inner radius in the vertex data must be specified in normalized space. |
| 907 | verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop); |
| 908 | verts[0].fOffset = SkPoint::Make(-xRadius, -yRadius); |
| 909 | verts[0].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); |
| 910 | verts[0].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); |
| 911 | |
| 912 | verts[1].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom); |
| 913 | verts[1].fOffset = SkPoint::Make(-xRadius, yRadius); |
| 914 | verts[1].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); |
| 915 | verts[1].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); |
| 916 | |
| 917 | verts[2].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom); |
| 918 | verts[2].fOffset = SkPoint::Make(xRadius, yRadius); |
| 919 | verts[2].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); |
| 920 | verts[2].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); |
| 921 | |
| 922 | verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fTop); |
| 923 | verts[3].fOffset = SkPoint::Make(xRadius, -yRadius); |
| 924 | verts[3].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); |
| 925 | verts[3].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); |
| 926 | |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 927 | verts += kVerticesPerQuad; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 928 | } |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 929 | helper.recordDraw(target); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 930 | } |
| 931 | |
| 932 | SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; } |
| 933 | |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame^] | 934 | EllipseBatch(const Geometry& geometry) : INHERITED(ClassID()) { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 935 | fGeoData.push_back(geometry); |
joshualitt | 99c7c07 | 2015-05-01 13:43:30 -0700 | [diff] [blame] | 936 | |
| 937 | this->setBounds(geometry.fDevBounds); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 938 | } |
| 939 | |
bsalomon | cb02b38 | 2015-08-12 11:14:50 -0700 | [diff] [blame] | 940 | bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override { |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 941 | EllipseBatch* that = t->cast<EllipseBatch>(); |
| 942 | |
| 943 | if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(), |
| 944 | that->bounds(), caps)) { |
joshualitt | 8cab9a7 | 2015-07-16 09:13:50 -0700 | [diff] [blame] | 945 | return false; |
| 946 | } |
| 947 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 948 | // TODO use vertex color to avoid breaking batches |
| 949 | if (this->color() != that->color()) { |
| 950 | return false; |
| 951 | } |
| 952 | |
| 953 | if (this->stroke() != that->stroke()) { |
| 954 | return false; |
| 955 | } |
| 956 | |
| 957 | SkASSERT(this->usesLocalCoords() == that->usesLocalCoords()); |
| 958 | if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) { |
| 959 | return false; |
| 960 | } |
| 961 | |
| 962 | fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin()); |
joshualitt | 99c7c07 | 2015-05-01 13:43:30 -0700 | [diff] [blame] | 963 | this->joinBounds(that->bounds()); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 964 | return true; |
| 965 | } |
| 966 | |
| 967 | GrColor color() const { return fBatch.fColor; } |
| 968 | bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; } |
| 969 | const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; } |
| 970 | bool stroke() const { return fBatch.fStroke; } |
| 971 | |
| 972 | struct BatchTracker { |
| 973 | GrColor fColor; |
| 974 | bool fStroke; |
| 975 | bool fUsesLocalCoords; |
| 976 | bool fColorIgnored; |
| 977 | bool fCoverageIgnored; |
| 978 | }; |
| 979 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 980 | BatchTracker fBatch; |
| 981 | SkSTArray<1, Geometry, true> fGeoData; |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame^] | 982 | |
| 983 | typedef GrVertexBatch INHERITED; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 984 | }; |
| 985 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 986 | static GrDrawBatch* create_ellipse_batch(GrColor color, |
| 987 | const SkMatrix& viewMatrix, |
| 988 | bool useCoverageAA, |
| 989 | const SkRect& ellipse, |
| 990 | const SkStrokeRec& stroke) { |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 991 | #ifdef SK_DEBUG |
| 992 | { |
| 993 | // we should have checked for this previously |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 994 | bool isAxisAlignedEllipse = viewMatrix.rectStaysRect(); |
commit-bot@chromium.org | e0a868c | 2013-11-22 07:02:11 +0000 | [diff] [blame] | 995 | SkASSERT(useCoverageAA && isAxisAlignedEllipse); |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 996 | } |
| 997 | #endif |
| 998 | |
commit-bot@chromium.org | 0c88828 | 2013-04-19 19:01:45 +0000 | [diff] [blame] | 999 | // do any matrix crunching before we reset the draw state for device coords |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 1000 | SkPoint center = SkPoint::Make(ellipse.centerX(), ellipse.centerY()); |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 1001 | viewMatrix.mapPoints(¢er, 1); |
commit-bot@chromium.org | 0c88828 | 2013-04-19 19:01:45 +0000 | [diff] [blame] | 1002 | SkScalar ellipseXRadius = SkScalarHalf(ellipse.width()); |
| 1003 | SkScalar ellipseYRadius = SkScalarHalf(ellipse.height()); |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 1004 | SkScalar xRadius = SkScalarAbs(viewMatrix[SkMatrix::kMScaleX]*ellipseXRadius + |
| 1005 | viewMatrix[SkMatrix::kMSkewY]*ellipseYRadius); |
| 1006 | SkScalar yRadius = SkScalarAbs(viewMatrix[SkMatrix::kMSkewX]*ellipseXRadius + |
| 1007 | viewMatrix[SkMatrix::kMScaleY]*ellipseYRadius); |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 1008 | |
commit-bot@chromium.org | 0c88828 | 2013-04-19 19:01:45 +0000 | [diff] [blame] | 1009 | // do (potentially) anisotropic mapping of stroke |
| 1010 | SkVector scaledStroke; |
| 1011 | SkScalar strokeWidth = stroke.getWidth(); |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 1012 | scaledStroke.fX = SkScalarAbs(strokeWidth*(viewMatrix[SkMatrix::kMScaleX] + |
| 1013 | viewMatrix[SkMatrix::kMSkewY])); |
| 1014 | scaledStroke.fY = SkScalarAbs(strokeWidth*(viewMatrix[SkMatrix::kMSkewX] + |
| 1015 | viewMatrix[SkMatrix::kMScaleY])); |
commit-bot@chromium.org | 0c88828 | 2013-04-19 19:01:45 +0000 | [diff] [blame] | 1016 | |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1017 | SkStrokeRec::Style style = stroke.getStyle(); |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 1018 | bool isStrokeOnly = SkStrokeRec::kStroke_Style == style || |
| 1019 | SkStrokeRec::kHairline_Style == style; |
| 1020 | bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == style; |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1021 | |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 1022 | SkScalar innerXRadius = 0; |
| 1023 | SkScalar innerYRadius = 0; |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 1024 | if (hasStroke) { |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1025 | if (SkScalarNearlyZero(scaledStroke.length())) { |
| 1026 | scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf); |
| 1027 | } else { |
| 1028 | scaledStroke.scale(SK_ScalarHalf); |
| 1029 | } |
| 1030 | |
| 1031 | // we only handle thick strokes for near-circular ellipses |
| 1032 | if (scaledStroke.length() > SK_ScalarHalf && |
| 1033 | (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1034 | return nullptr; |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1035 | } |
| 1036 | |
| 1037 | // we don't handle it if curvature of the stroke is less than curvature of the ellipse |
| 1038 | if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius || |
| 1039 | scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1040 | return nullptr; |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1041 | } |
| 1042 | |
| 1043 | // this is legit only if scale & translation (which should be the case at the moment) |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 1044 | if (isStrokeOnly) { |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1045 | innerXRadius = xRadius - scaledStroke.fX; |
| 1046 | innerYRadius = yRadius - scaledStroke.fY; |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1047 | } |
| 1048 | |
| 1049 | xRadius += scaledStroke.fX; |
| 1050 | yRadius += scaledStroke.fY; |
| 1051 | } |
| 1052 | |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 1053 | // We've extended the outer x radius out half a pixel to antialias. |
commit-bot@chromium.org | 0a6cb60 | 2013-04-11 15:05:37 +0000 | [diff] [blame] | 1054 | // This will also expand the rect so all the pixels will be captured. |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1055 | // TODO: Consider if we should use sqrt(2)/2 instead |
commit-bot@chromium.org | 0a6cb60 | 2013-04-11 15:05:37 +0000 | [diff] [blame] | 1056 | xRadius += SK_ScalarHalf; |
| 1057 | yRadius += SK_ScalarHalf; |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 1058 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1059 | EllipseBatch::Geometry geometry; |
| 1060 | geometry.fViewMatrix = viewMatrix; |
| 1061 | geometry.fColor = color; |
| 1062 | geometry.fXRadius = xRadius; |
| 1063 | geometry.fYRadius = yRadius; |
| 1064 | geometry.fInnerXRadius = innerXRadius; |
| 1065 | geometry.fInnerYRadius = innerYRadius; |
| 1066 | geometry.fStroke = isStrokeOnly && innerXRadius > 0 && innerYRadius > 0; |
joshualitt | d96a67b | 2015-05-05 14:09:05 -0700 | [diff] [blame] | 1067 | geometry.fDevBounds = SkRect::MakeLTRB(center.fX - xRadius, center.fY - yRadius, |
| 1068 | center.fX + xRadius, center.fY + yRadius); |
commit-bot@chromium.org | 0a6cb60 | 2013-04-11 15:05:37 +0000 | [diff] [blame] | 1069 | |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 1070 | return EllipseBatch::Create(geometry); |
| 1071 | } |
| 1072 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1073 | bool GrOvalRenderer::DrawEllipse(GrDrawTarget* target, |
joshualitt | ae3d63a | 2015-07-13 08:44:06 -0700 | [diff] [blame] | 1074 | const GrPipelineBuilder& pipelineBuilder, |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 1075 | GrColor color, |
| 1076 | const SkMatrix& viewMatrix, |
| 1077 | bool useCoverageAA, |
| 1078 | const SkRect& ellipse, |
| 1079 | const SkStrokeRec& stroke) { |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 1080 | SkAutoTUnref<GrDrawBatch> batch(create_ellipse_batch(color, viewMatrix, useCoverageAA, ellipse, |
| 1081 | stroke)); |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 1082 | if (!batch) { |
| 1083 | return false; |
| 1084 | } |
| 1085 | |
joshualitt | ae3d63a | 2015-07-13 08:44:06 -0700 | [diff] [blame] | 1086 | target->drawBatch(pipelineBuilder, batch); |
jvanverth@google.com | c4f2eca | 2013-04-16 12:30:35 +0000 | [diff] [blame] | 1087 | return true; |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 1088 | } |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1089 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1090 | ///////////////////////////////////////////////////////////////////////////////////////////////// |
| 1091 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 1092 | class DIEllipseBatch : public GrVertexBatch { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1093 | public: |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame^] | 1094 | DEFINE_BATCH_CLASS_ID |
| 1095 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1096 | struct Geometry { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1097 | SkMatrix fViewMatrix; |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame^] | 1098 | SkRect fBounds; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1099 | SkScalar fXRadius; |
| 1100 | SkScalar fYRadius; |
| 1101 | SkScalar fInnerXRadius; |
| 1102 | SkScalar fInnerYRadius; |
| 1103 | SkScalar fGeoDx; |
| 1104 | SkScalar fGeoDy; |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame^] | 1105 | GrColor fColor; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1106 | DIEllipseEdgeEffect::Mode fMode; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1107 | }; |
| 1108 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 1109 | static GrDrawBatch* Create(const Geometry& geometry, const SkRect& bounds) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 1110 | return new DIEllipseBatch(geometry, bounds); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1111 | } |
| 1112 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 1113 | const char* name() const override { return "DIEllipseBatch"; } |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1114 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 1115 | void getInvariantOutputColor(GrInitInvariantOutput* out) const override { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1116 | // When this is called on a batch, there is only one geometry bundle |
| 1117 | out->setKnownFourComponents(fGeoData[0].fColor); |
| 1118 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 1119 | void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1120 | out->setUnknownSingleComponent(); |
| 1121 | } |
| 1122 | |
bsalomon | e46f9fe | 2015-08-18 06:05:14 -0700 | [diff] [blame] | 1123 | private: |
| 1124 | |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 1125 | void initBatchTracker(const GrPipelineOptimizations& opt) override { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1126 | // Handle any color overrides |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 1127 | if (!opt.readsColor()) { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1128 | fGeoData[0].fColor = GrColor_ILLEGAL; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1129 | } |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 1130 | opt.getOverrideColorIfSet(&fGeoData[0].fColor); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1131 | |
| 1132 | // setup batch properties |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 1133 | fBatch.fColorIgnored = !opt.readsColor(); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1134 | fBatch.fColor = fGeoData[0].fColor; |
| 1135 | fBatch.fMode = fGeoData[0].fMode; |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 1136 | fBatch.fUsesLocalCoords = opt.readsLocalCoords(); |
| 1137 | fBatch.fCoverageIgnored = !opt.readsCoverage(); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1138 | } |
| 1139 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 1140 | void onPrepareDraws(Target* target) override { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1141 | // Setup geometry processor |
| 1142 | SkAutoTUnref<GrGeometryProcessor> gp(DIEllipseEdgeEffect::Create(this->color(), |
| 1143 | this->viewMatrix(), |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 1144 | this->mode(), |
| 1145 | this->usesLocalCoords())); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1146 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 1147 | target->initDraw(gp, this->pipeline()); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1148 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1149 | int instanceCount = fGeoData.count(); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1150 | size_t vertexStride = gp->getVertexStride(); |
joshualitt | 19e0058 | 2015-02-11 17:36:30 -0800 | [diff] [blame] | 1151 | SkASSERT(vertexStride == sizeof(DIEllipseVertex)); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 1152 | QuadHelper helper; |
| 1153 | DIEllipseVertex* verts = reinterpret_cast<DIEllipseVertex*>( |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 1154 | helper.init(target, vertexStride, instanceCount)); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 1155 | if (!verts) { |
joshualitt | 4b31de8 | 2015-03-05 14:33:41 -0800 | [diff] [blame] | 1156 | return; |
| 1157 | } |
| 1158 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1159 | for (int i = 0; i < instanceCount; i++) { |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 1160 | Geometry& geom = fGeoData[i]; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1161 | |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 1162 | SkScalar xRadius = geom.fXRadius; |
| 1163 | SkScalar yRadius = geom.fYRadius; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1164 | |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 1165 | const SkRect& bounds = geom.fBounds; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1166 | |
| 1167 | // This adjusts the "radius" to include the half-pixel border |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 1168 | SkScalar offsetDx = geom.fGeoDx / xRadius; |
| 1169 | SkScalar offsetDy = geom.fGeoDy / yRadius; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1170 | |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 1171 | SkScalar innerRatioX = xRadius / geom.fInnerXRadius; |
| 1172 | SkScalar innerRatioY = yRadius / geom.fInnerYRadius; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1173 | |
| 1174 | verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop); |
| 1175 | verts[0].fOuterOffset = SkPoint::Make(-1.0f - offsetDx, -1.0f - offsetDy); |
| 1176 | verts[0].fInnerOffset = SkPoint::Make(-innerRatioX - offsetDx, -innerRatioY - offsetDy); |
| 1177 | |
| 1178 | verts[1].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom); |
| 1179 | verts[1].fOuterOffset = SkPoint::Make(-1.0f - offsetDx, 1.0f + offsetDy); |
| 1180 | verts[1].fInnerOffset = SkPoint::Make(-innerRatioX - offsetDx, innerRatioY + offsetDy); |
| 1181 | |
| 1182 | verts[2].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom); |
| 1183 | verts[2].fOuterOffset = SkPoint::Make(1.0f + offsetDx, 1.0f + offsetDy); |
| 1184 | verts[2].fInnerOffset = SkPoint::Make(innerRatioX + offsetDx, innerRatioY + offsetDy); |
| 1185 | |
| 1186 | verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fTop); |
| 1187 | verts[3].fOuterOffset = SkPoint::Make(1.0f + offsetDx, -1.0f - offsetDy); |
| 1188 | verts[3].fInnerOffset = SkPoint::Make(innerRatioX + offsetDx, -innerRatioY - offsetDy); |
| 1189 | |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 1190 | verts += kVerticesPerQuad; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1191 | } |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 1192 | helper.recordDraw(target); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1193 | } |
| 1194 | |
| 1195 | SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; } |
| 1196 | |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame^] | 1197 | DIEllipseBatch(const Geometry& geometry, const SkRect& bounds) : INHERITED(ClassID()) { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1198 | fGeoData.push_back(geometry); |
joshualitt | 99c7c07 | 2015-05-01 13:43:30 -0700 | [diff] [blame] | 1199 | |
| 1200 | this->setBounds(bounds); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1201 | } |
| 1202 | |
bsalomon | cb02b38 | 2015-08-12 11:14:50 -0700 | [diff] [blame] | 1203 | bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override { |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 1204 | DIEllipseBatch* that = t->cast<DIEllipseBatch>(); |
| 1205 | if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(), |
| 1206 | that->bounds(), caps)) { |
joshualitt | 8cab9a7 | 2015-07-16 09:13:50 -0700 | [diff] [blame] | 1207 | return false; |
| 1208 | } |
| 1209 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1210 | // TODO use vertex color to avoid breaking batches |
| 1211 | if (this->color() != that->color()) { |
| 1212 | return false; |
| 1213 | } |
| 1214 | |
| 1215 | if (this->mode() != that->mode()) { |
| 1216 | return false; |
| 1217 | } |
| 1218 | |
joshualitt | d96a67b | 2015-05-05 14:09:05 -0700 | [diff] [blame] | 1219 | // TODO rewrite to allow positioning on CPU |
| 1220 | if (!this->viewMatrix().cheapEqualTo(that->viewMatrix())) { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1221 | return false; |
| 1222 | } |
| 1223 | |
| 1224 | fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin()); |
joshualitt | 99c7c07 | 2015-05-01 13:43:30 -0700 | [diff] [blame] | 1225 | this->joinBounds(that->bounds()); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1226 | return true; |
| 1227 | } |
| 1228 | |
| 1229 | GrColor color() const { return fBatch.fColor; } |
| 1230 | bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; } |
| 1231 | const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; } |
| 1232 | DIEllipseEdgeEffect::Mode mode() const { return fBatch.fMode; } |
| 1233 | |
| 1234 | struct BatchTracker { |
| 1235 | GrColor fColor; |
| 1236 | DIEllipseEdgeEffect::Mode fMode; |
| 1237 | bool fUsesLocalCoords; |
| 1238 | bool fColorIgnored; |
| 1239 | bool fCoverageIgnored; |
| 1240 | }; |
| 1241 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1242 | BatchTracker fBatch; |
| 1243 | SkSTArray<1, Geometry, true> fGeoData; |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame^] | 1244 | |
| 1245 | typedef GrVertexBatch INHERITED; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1246 | }; |
| 1247 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 1248 | static GrDrawBatch* create_diellipse_batch(GrColor color, |
| 1249 | const SkMatrix& viewMatrix, |
| 1250 | bool useCoverageAA, |
| 1251 | const SkRect& ellipse, |
| 1252 | const SkStrokeRec& stroke) { |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 1253 | SkPoint center = SkPoint::Make(ellipse.centerX(), ellipse.centerY()); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 1254 | SkScalar xRadius = SkScalarHalf(ellipse.width()); |
skia.committer@gmail.com | 6fc1b49 | 2013-09-06 07:01:45 +0000 | [diff] [blame] | 1255 | SkScalar yRadius = SkScalarHalf(ellipse.height()); |
| 1256 | |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 1257 | SkStrokeRec::Style style = stroke.getStyle(); |
skia.committer@gmail.com | 6fc1b49 | 2013-09-06 07:01:45 +0000 | [diff] [blame] | 1258 | DIEllipseEdgeEffect::Mode mode = (SkStrokeRec::kStroke_Style == style) ? |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 1259 | DIEllipseEdgeEffect::kStroke : |
skia.committer@gmail.com | 6fc1b49 | 2013-09-06 07:01:45 +0000 | [diff] [blame] | 1260 | (SkStrokeRec::kHairline_Style == style) ? |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 1261 | 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)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1277 | return nullptr; |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 1278 | } |
| 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) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1283 | return nullptr; |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 1284 | } |
| 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.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 1299 | |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 1300 | // This expands the outer rect so that after CTM we end up with a half-pixel border |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 1301 | SkScalar a = viewMatrix[SkMatrix::kMScaleX]; |
| 1302 | SkScalar b = viewMatrix[SkMatrix::kMSkewX]; |
| 1303 | SkScalar c = viewMatrix[SkMatrix::kMSkewY]; |
| 1304 | SkScalar d = viewMatrix[SkMatrix::kMScaleY]; |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 1305 | SkScalar geoDx = SK_ScalarHalf / SkScalarSqrt(a*a + c*c); |
| 1306 | SkScalar geoDy = SK_ScalarHalf / SkScalarSqrt(b*b + d*d); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 1307 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1308 | 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; |
joshualitt | d96a67b | 2015-05-05 14:09:05 -0700 | [diff] [blame] | 1318 | geometry.fBounds = SkRect::MakeLTRB(center.fX - xRadius - geoDx, center.fY - yRadius - geoDy, |
| 1319 | center.fX + xRadius + geoDx, center.fY + yRadius + geoDy); |
egdaniel | 9ef1bb1 | 2015-04-20 12:28:57 -0700 | [diff] [blame] | 1320 | |
joshualitt | d96a67b | 2015-05-05 14:09:05 -0700 | [diff] [blame] | 1321 | SkRect devBounds = geometry.fBounds; |
| 1322 | viewMatrix.mapRect(&devBounds); |
| 1323 | return DIEllipseBatch::Create(geometry, devBounds); |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 1324 | } |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 1325 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1326 | bool GrOvalRenderer::DrawDIEllipse(GrDrawTarget* target, |
joshualitt | ae3d63a | 2015-07-13 08:44:06 -0700 | [diff] [blame] | 1327 | const GrPipelineBuilder& pipelineBuilder, |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 1328 | GrColor color, |
| 1329 | const SkMatrix& viewMatrix, |
| 1330 | bool useCoverageAA, |
| 1331 | const SkRect& ellipse, |
| 1332 | const SkStrokeRec& stroke) { |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 1333 | SkAutoTUnref<GrDrawBatch> batch(create_diellipse_batch(color, viewMatrix, useCoverageAA, |
| 1334 | ellipse, stroke)); |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 1335 | if (!batch) { |
| 1336 | return false; |
| 1337 | } |
joshualitt | ae3d63a | 2015-07-13 08:44:06 -0700 | [diff] [blame] | 1338 | target->drawBatch(pipelineBuilder, batch); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 1339 | return true; |
| 1340 | } |
| 1341 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1342 | /////////////////////////////////////////////////////////////////////////////// |
| 1343 | |
| 1344 | static 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.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 1350 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1351 | // 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.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 1356 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1357 | // 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 | |
joshualitt | 5ead6da | 2014-10-22 16:00:29 -0700 | [diff] [blame] | 1362 | static const int kIndicesPerStrokeRRect = SK_ARRAY_COUNT(gRRectIndices) - 6; |
| 1363 | static const int kIndicesPerRRect = SK_ARRAY_COUNT(gRRectIndices); |
| 1364 | static const int kVertsPerRRect = 16; |
| 1365 | static const int kNumRRectsInIndexBuffer = 256; |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1366 | |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 1367 | GR_DECLARE_STATIC_UNIQUE_KEY(gStrokeRRectOnlyIndexBufferKey); |
| 1368 | GR_DECLARE_STATIC_UNIQUE_KEY(gRRectOnlyIndexBufferKey); |
| 1369 | static 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) { |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 1374 | return resourceProvider->findOrCreateInstancedIndexBuffer( |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 1375 | gRRectIndices, kIndicesPerStrokeRRect, kNumRRectsInIndexBuffer, kVertsPerRRect, |
| 1376 | gStrokeRRectOnlyIndexBufferKey); |
| 1377 | } else { |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 1378 | return resourceProvider->findOrCreateInstancedIndexBuffer( |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 1379 | gRRectIndices, kIndicesPerRRect, kNumRRectsInIndexBuffer, kVertsPerRRect, |
| 1380 | gRRectOnlyIndexBufferKey); |
| 1381 | |
| 1382 | } |
| 1383 | } |
| 1384 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1385 | bool GrOvalRenderer::DrawDRRect(GrDrawTarget* target, |
joshualitt | ae3d63a | 2015-07-13 08:44:06 -0700 | [diff] [blame] | 1386 | const GrPipelineBuilder& pipelineBuilder, |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 1387 | GrColor color, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 1388 | const SkMatrix& viewMatrix, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 1389 | bool useAA, |
| 1390 | const SkRRect& origOuter, |
| 1391 | const SkRRect& origInner) { |
joshualitt | ae3d63a | 2015-07-13 08:44:06 -0700 | [diff] [blame] | 1392 | bool applyAA = useAA && !pipelineBuilder.getRenderTarget()->isUnifiedMultisampled(); |
joshualitt | 4421a4c | 2015-07-13 09:36:41 -0700 | [diff] [blame] | 1393 | GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps; |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 1394 | if (!origInner.isEmpty()) { |
| 1395 | SkTCopyOnFirstWrite<SkRRect> inner(origInner); |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 1396 | if (!viewMatrix.isIdentity()) { |
| 1397 | if (!origInner.transform(viewMatrix, inner.writable())) { |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 1398 | return false; |
| 1399 | } |
| 1400 | } |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1401 | GrPrimitiveEdgeType edgeType = applyAA ? |
| 1402 | kInverseFillAA_GrProcessorEdgeType : |
| 1403 | kInverseFillBW_GrProcessorEdgeType; |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 1404 | // TODO this needs to be a geometry processor |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1405 | GrFragmentProcessor* fp = GrRRectEffect::Create(edgeType, *inner); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1406 | if (nullptr == fp) { |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 1407 | return false; |
| 1408 | } |
joshualitt | 4421a4c | 2015-07-13 09:36:41 -0700 | [diff] [blame] | 1409 | arfps.set(&pipelineBuilder); |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 1410 | arfps.addCoverageFragmentProcessor(fp)->unref(); |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 1411 | } |
| 1412 | |
| 1413 | SkStrokeRec fillRec(SkStrokeRec::kFill_InitStyle); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1414 | if (DrawRRect(target, pipelineBuilder, color, viewMatrix, useAA, origOuter, fillRec)) { |
bsalomon | 8af0523 | 2014-06-03 06:34:58 -0700 | [diff] [blame] | 1415 | return true; |
| 1416 | } |
| 1417 | |
| 1418 | SkASSERT(!origOuter.isEmpty()); |
| 1419 | SkTCopyOnFirstWrite<SkRRect> outer(origOuter); |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 1420 | if (!viewMatrix.isIdentity()) { |
| 1421 | if (!origOuter.transform(viewMatrix, outer.writable())) { |
bsalomon | 8af0523 | 2014-06-03 06:34:58 -0700 | [diff] [blame] | 1422 | return false; |
| 1423 | } |
| 1424 | } |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1425 | GrPrimitiveEdgeType edgeType = applyAA ? kFillAA_GrProcessorEdgeType : |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1426 | kFillBW_GrProcessorEdgeType; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1427 | GrFragmentProcessor* effect = GrRRectEffect::Create(edgeType, *outer); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1428 | if (nullptr == effect) { |
bsalomon | 8af0523 | 2014-06-03 06:34:58 -0700 | [diff] [blame] | 1429 | return false; |
| 1430 | } |
joshualitt | 4421a4c | 2015-07-13 09:36:41 -0700 | [diff] [blame] | 1431 | if (!arfps.isSet()) { |
| 1432 | arfps.set(&pipelineBuilder); |
bsalomon | 8af0523 | 2014-06-03 06:34:58 -0700 | [diff] [blame] | 1433 | } |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 1434 | |
| 1435 | SkMatrix invert; |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 1436 | if (!viewMatrix.invert(&invert)) { |
bsalomon | 8af0523 | 2014-06-03 06:34:58 -0700 | [diff] [blame] | 1437 | return false; |
| 1438 | } |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 1439 | |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 1440 | arfps.addCoverageFragmentProcessor(effect)->unref(); |
bsalomon | 8af0523 | 2014-06-03 06:34:58 -0700 | [diff] [blame] | 1441 | SkRect bounds = outer->getBounds(); |
| 1442 | if (applyAA) { |
| 1443 | bounds.outset(SK_ScalarHalf, SK_ScalarHalf); |
| 1444 | } |
joshualitt | d2b23e0 | 2015-08-21 10:53:34 -0700 | [diff] [blame] | 1445 | target->drawNonAARect(pipelineBuilder, color, SkMatrix::I(), bounds, invert); |
bsalomon | 8af0523 | 2014-06-03 06:34:58 -0700 | [diff] [blame] | 1446 | return true; |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 1447 | } |
| 1448 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1449 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 1450 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 1451 | class RRectCircleRendererBatch : public GrVertexBatch { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1452 | public: |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame^] | 1453 | DEFINE_BATCH_CLASS_ID |
| 1454 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1455 | struct Geometry { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1456 | SkMatrix fViewMatrix; |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame^] | 1457 | SkRect fDevBounds; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1458 | SkScalar fInnerRadius; |
| 1459 | SkScalar fOuterRadius; |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame^] | 1460 | GrColor fColor; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1461 | bool fStroke; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1462 | }; |
| 1463 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 1464 | static GrDrawBatch* Create(const Geometry& geometry) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 1465 | return new RRectCircleRendererBatch(geometry); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1466 | } |
| 1467 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 1468 | const char* name() const override { return "RRectCircleBatch"; } |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1469 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 1470 | void getInvariantOutputColor(GrInitInvariantOutput* out) const override { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1471 | // When this is called on a batch, there is only one geometry bundle |
| 1472 | out->setKnownFourComponents(fGeoData[0].fColor); |
| 1473 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 1474 | void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1475 | out->setUnknownSingleComponent(); |
| 1476 | } |
| 1477 | |
bsalomon | e46f9fe | 2015-08-18 06:05:14 -0700 | [diff] [blame] | 1478 | private: |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 1479 | void initBatchTracker(const GrPipelineOptimizations& opt) override { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1480 | // Handle any color overrides |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 1481 | if (!opt.readsColor()) { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1482 | fGeoData[0].fColor = GrColor_ILLEGAL; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1483 | } |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 1484 | opt.getOverrideColorIfSet(&fGeoData[0].fColor); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1485 | |
| 1486 | // setup batch properties |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 1487 | fBatch.fColorIgnored = !opt.readsColor(); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1488 | fBatch.fColor = fGeoData[0].fColor; |
| 1489 | fBatch.fStroke = fGeoData[0].fStroke; |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 1490 | fBatch.fUsesLocalCoords = opt.readsLocalCoords(); |
| 1491 | fBatch.fCoverageIgnored = !opt.readsCoverage(); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1492 | } |
| 1493 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 1494 | void onPrepareDraws(Target* target) override { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1495 | // reset to device coordinates |
| 1496 | SkMatrix invert; |
| 1497 | if (!this->viewMatrix().invert(&invert)) { |
| 1498 | SkDebugf("Failed to invert\n"); |
| 1499 | return; |
| 1500 | } |
| 1501 | |
| 1502 | // Setup geometry processor |
| 1503 | SkAutoTUnref<GrGeometryProcessor> gp(CircleEdgeEffect::Create(this->color(), |
| 1504 | this->stroke(), |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 1505 | invert, |
| 1506 | this->usesLocalCoords())); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1507 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 1508 | target->initDraw(gp, this->pipeline()); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1509 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1510 | int instanceCount = fGeoData.count(); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1511 | size_t vertexStride = gp->getVertexStride(); |
| 1512 | SkASSERT(vertexStride == sizeof(CircleVertex)); |
| 1513 | |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 1514 | // drop out the middle quad if we're stroked |
| 1515 | int indicesPerInstance = this->stroke() ? kIndicesPerStrokeRRect : kIndicesPerRRect; |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 1516 | SkAutoTUnref<const GrIndexBuffer> indexBuffer( |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 1517 | ref_rrect_index_buffer(this->stroke(), target->resourceProvider())); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1518 | |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 1519 | InstancedHelper helper; |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 1520 | CircleVertex* verts = reinterpret_cast<CircleVertex*>(helper.init(target, |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 1521 | kTriangles_GrPrimitiveType, vertexStride, indexBuffer, kVertsPerRRect, |
| 1522 | indicesPerInstance, instanceCount)); |
| 1523 | if (!verts || !indexBuffer) { |
joshualitt | 4b31de8 | 2015-03-05 14:33:41 -0800 | [diff] [blame] | 1524 | SkDebugf("Could not allocate vertices\n"); |
| 1525 | return; |
| 1526 | } |
| 1527 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1528 | for (int i = 0; i < instanceCount; i++) { |
| 1529 | Geometry& args = fGeoData[i]; |
| 1530 | |
| 1531 | SkScalar outerRadius = args.fOuterRadius; |
| 1532 | |
egdaniel | bc22714 | 2015-04-21 06:28:08 -0700 | [diff] [blame] | 1533 | const SkRect& bounds = args.fDevBounds; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1534 | |
| 1535 | SkScalar yCoords[4] = { |
| 1536 | bounds.fTop, |
| 1537 | bounds.fTop + outerRadius, |
| 1538 | bounds.fBottom - outerRadius, |
| 1539 | bounds.fBottom |
| 1540 | }; |
| 1541 | |
| 1542 | SkScalar yOuterRadii[4] = {-1, 0, 0, 1 }; |
| 1543 | // The inner radius in the vertex data must be specified in normalized space. |
| 1544 | SkScalar innerRadius = args.fInnerRadius / args.fOuterRadius; |
| 1545 | for (int i = 0; i < 4; ++i) { |
| 1546 | verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]); |
| 1547 | verts->fOffset = SkPoint::Make(-1, yOuterRadii[i]); |
| 1548 | verts->fOuterRadius = outerRadius; |
| 1549 | verts->fInnerRadius = innerRadius; |
| 1550 | verts++; |
| 1551 | |
| 1552 | verts->fPos = SkPoint::Make(bounds.fLeft + outerRadius, yCoords[i]); |
| 1553 | verts->fOffset = SkPoint::Make(0, yOuterRadii[i]); |
| 1554 | verts->fOuterRadius = outerRadius; |
| 1555 | verts->fInnerRadius = innerRadius; |
| 1556 | verts++; |
| 1557 | |
| 1558 | verts->fPos = SkPoint::Make(bounds.fRight - outerRadius, yCoords[i]); |
| 1559 | verts->fOffset = SkPoint::Make(0, yOuterRadii[i]); |
| 1560 | verts->fOuterRadius = outerRadius; |
| 1561 | verts->fInnerRadius = innerRadius; |
| 1562 | verts++; |
| 1563 | |
| 1564 | verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]); |
| 1565 | verts->fOffset = SkPoint::Make(1, yOuterRadii[i]); |
| 1566 | verts->fOuterRadius = outerRadius; |
| 1567 | verts->fInnerRadius = innerRadius; |
| 1568 | verts++; |
| 1569 | } |
| 1570 | } |
| 1571 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 1572 | helper.recordDraw(target); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1573 | } |
| 1574 | |
| 1575 | SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; } |
| 1576 | |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame^] | 1577 | RRectCircleRendererBatch(const Geometry& geometry) : INHERITED(ClassID()) { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1578 | fGeoData.push_back(geometry); |
joshualitt | 99c7c07 | 2015-05-01 13:43:30 -0700 | [diff] [blame] | 1579 | |
| 1580 | this->setBounds(geometry.fDevBounds); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1581 | } |
| 1582 | |
bsalomon | cb02b38 | 2015-08-12 11:14:50 -0700 | [diff] [blame] | 1583 | bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override { |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 1584 | RRectCircleRendererBatch* that = t->cast<RRectCircleRendererBatch>(); |
| 1585 | if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(), |
| 1586 | that->bounds(), caps)) { |
joshualitt | 8cab9a7 | 2015-07-16 09:13:50 -0700 | [diff] [blame] | 1587 | return false; |
| 1588 | } |
| 1589 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1590 | // TODO use vertex color to avoid breaking batches |
| 1591 | if (this->color() != that->color()) { |
| 1592 | return false; |
| 1593 | } |
| 1594 | |
| 1595 | if (this->stroke() != that->stroke()) { |
| 1596 | return false; |
| 1597 | } |
| 1598 | |
| 1599 | SkASSERT(this->usesLocalCoords() == that->usesLocalCoords()); |
| 1600 | if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) { |
| 1601 | return false; |
| 1602 | } |
| 1603 | |
| 1604 | fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin()); |
joshualitt | 99c7c07 | 2015-05-01 13:43:30 -0700 | [diff] [blame] | 1605 | this->joinBounds(that->bounds()); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1606 | return true; |
| 1607 | } |
| 1608 | |
| 1609 | GrColor color() const { return fBatch.fColor; } |
| 1610 | bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; } |
| 1611 | const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; } |
| 1612 | bool stroke() const { return fBatch.fStroke; } |
| 1613 | |
| 1614 | struct BatchTracker { |
| 1615 | GrColor fColor; |
| 1616 | bool fStroke; |
| 1617 | bool fUsesLocalCoords; |
| 1618 | bool fColorIgnored; |
| 1619 | bool fCoverageIgnored; |
| 1620 | }; |
| 1621 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1622 | BatchTracker fBatch; |
| 1623 | SkSTArray<1, Geometry, true> fGeoData; |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame^] | 1624 | |
| 1625 | typedef GrVertexBatch INHERITED; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1626 | }; |
| 1627 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 1628 | class RRectEllipseRendererBatch : public GrVertexBatch { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1629 | public: |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame^] | 1630 | DEFINE_BATCH_CLASS_ID |
| 1631 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1632 | struct Geometry { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1633 | SkMatrix fViewMatrix; |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame^] | 1634 | SkRect fDevBounds; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1635 | SkScalar fXRadius; |
| 1636 | SkScalar fYRadius; |
| 1637 | SkScalar fInnerXRadius; |
| 1638 | SkScalar fInnerYRadius; |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame^] | 1639 | GrColor fColor; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1640 | bool fStroke; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1641 | }; |
| 1642 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 1643 | static GrDrawBatch* Create(const Geometry& geometry) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 1644 | return new RRectEllipseRendererBatch(geometry); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1645 | } |
| 1646 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 1647 | const char* name() const override { return "RRectEllipseRendererBatch"; } |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1648 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 1649 | void getInvariantOutputColor(GrInitInvariantOutput* out) const override { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1650 | // When this is called on a batch, there is only one geometry bundle |
| 1651 | out->setKnownFourComponents(fGeoData[0].fColor); |
| 1652 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 1653 | void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1654 | out->setUnknownSingleComponent(); |
| 1655 | } |
| 1656 | |
bsalomon | e46f9fe | 2015-08-18 06:05:14 -0700 | [diff] [blame] | 1657 | private: |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 1658 | void initBatchTracker(const GrPipelineOptimizations& opt) override { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1659 | // Handle any color overrides |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 1660 | if (!opt.readsColor()) { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1661 | fGeoData[0].fColor = GrColor_ILLEGAL; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1662 | } |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 1663 | opt.getOverrideColorIfSet(&fGeoData[0].fColor); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1664 | |
| 1665 | // setup batch properties |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 1666 | fBatch.fColorIgnored = !opt.readsColor(); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1667 | fBatch.fColor = fGeoData[0].fColor; |
| 1668 | fBatch.fStroke = fGeoData[0].fStroke; |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 1669 | fBatch.fUsesLocalCoords = opt.readsLocalCoords(); |
| 1670 | fBatch.fCoverageIgnored = !opt.readsCoverage(); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1671 | } |
| 1672 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 1673 | void onPrepareDraws(Target* target) override { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1674 | // reset to device coordinates |
| 1675 | SkMatrix invert; |
| 1676 | if (!this->viewMatrix().invert(&invert)) { |
| 1677 | SkDebugf("Failed to invert\n"); |
| 1678 | return; |
| 1679 | } |
| 1680 | |
| 1681 | // Setup geometry processor |
| 1682 | SkAutoTUnref<GrGeometryProcessor> gp(EllipseEdgeEffect::Create(this->color(), |
| 1683 | this->stroke(), |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 1684 | invert, |
| 1685 | this->usesLocalCoords())); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1686 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 1687 | target->initDraw(gp, this->pipeline()); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1688 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1689 | int instanceCount = fGeoData.count(); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1690 | size_t vertexStride = gp->getVertexStride(); |
| 1691 | SkASSERT(vertexStride == sizeof(EllipseVertex)); |
| 1692 | |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 1693 | // drop out the middle quad if we're stroked |
| 1694 | int indicesPerInstance = this->stroke() ? kIndicesPerStrokeRRect : kIndicesPerRRect; |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 1695 | SkAutoTUnref<const GrIndexBuffer> indexBuffer( |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 1696 | ref_rrect_index_buffer(this->stroke(), target->resourceProvider())); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1697 | |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 1698 | InstancedHelper helper; |
| 1699 | EllipseVertex* verts = reinterpret_cast<EllipseVertex*>( |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 1700 | helper.init(target, kTriangles_GrPrimitiveType, vertexStride, indexBuffer, |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 1701 | kVertsPerRRect, indicesPerInstance, instanceCount)); |
| 1702 | if (!verts || !indexBuffer) { |
joshualitt | 4b31de8 | 2015-03-05 14:33:41 -0800 | [diff] [blame] | 1703 | SkDebugf("Could not allocate vertices\n"); |
| 1704 | return; |
| 1705 | } |
| 1706 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1707 | for (int i = 0; i < instanceCount; i++) { |
| 1708 | Geometry& args = fGeoData[i]; |
| 1709 | |
| 1710 | // Compute the reciprocals of the radii here to save time in the shader |
| 1711 | SkScalar xRadRecip = SkScalarInvert(args.fXRadius); |
| 1712 | SkScalar yRadRecip = SkScalarInvert(args.fYRadius); |
| 1713 | SkScalar xInnerRadRecip = SkScalarInvert(args.fInnerXRadius); |
| 1714 | SkScalar yInnerRadRecip = SkScalarInvert(args.fInnerYRadius); |
| 1715 | |
| 1716 | // Extend the radii out half a pixel to antialias. |
| 1717 | SkScalar xOuterRadius = args.fXRadius + SK_ScalarHalf; |
| 1718 | SkScalar yOuterRadius = args.fYRadius + SK_ScalarHalf; |
| 1719 | |
egdaniel | bc22714 | 2015-04-21 06:28:08 -0700 | [diff] [blame] | 1720 | const SkRect& bounds = args.fDevBounds; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1721 | |
| 1722 | SkScalar yCoords[4] = { |
| 1723 | bounds.fTop, |
| 1724 | bounds.fTop + yOuterRadius, |
| 1725 | bounds.fBottom - yOuterRadius, |
| 1726 | bounds.fBottom |
| 1727 | }; |
| 1728 | SkScalar yOuterOffsets[4] = { |
| 1729 | yOuterRadius, |
| 1730 | SK_ScalarNearlyZero, // we're using inversesqrt() in shader, so can't be exactly 0 |
| 1731 | SK_ScalarNearlyZero, |
| 1732 | yOuterRadius |
| 1733 | }; |
| 1734 | |
| 1735 | for (int i = 0; i < 4; ++i) { |
| 1736 | verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]); |
| 1737 | verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]); |
| 1738 | verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); |
| 1739 | verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); |
| 1740 | verts++; |
| 1741 | |
| 1742 | verts->fPos = SkPoint::Make(bounds.fLeft + xOuterRadius, yCoords[i]); |
| 1743 | verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]); |
| 1744 | verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); |
| 1745 | verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); |
| 1746 | verts++; |
| 1747 | |
| 1748 | verts->fPos = SkPoint::Make(bounds.fRight - xOuterRadius, yCoords[i]); |
| 1749 | verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]); |
| 1750 | verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); |
| 1751 | verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); |
| 1752 | verts++; |
| 1753 | |
| 1754 | verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]); |
| 1755 | verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]); |
| 1756 | verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); |
| 1757 | verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); |
| 1758 | verts++; |
| 1759 | } |
| 1760 | } |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 1761 | helper.recordDraw(target); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1762 | } |
| 1763 | |
| 1764 | SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; } |
| 1765 | |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame^] | 1766 | RRectEllipseRendererBatch(const Geometry& geometry) : INHERITED(ClassID()) { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1767 | fGeoData.push_back(geometry); |
joshualitt | 99c7c07 | 2015-05-01 13:43:30 -0700 | [diff] [blame] | 1768 | |
| 1769 | this->setBounds(geometry.fDevBounds); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1770 | } |
| 1771 | |
bsalomon | cb02b38 | 2015-08-12 11:14:50 -0700 | [diff] [blame] | 1772 | bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override { |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 1773 | RRectEllipseRendererBatch* that = t->cast<RRectEllipseRendererBatch>(); |
| 1774 | |
| 1775 | if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(), |
| 1776 | that->bounds(), caps)) { |
joshualitt | 8cab9a7 | 2015-07-16 09:13:50 -0700 | [diff] [blame] | 1777 | return false; |
| 1778 | } |
| 1779 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1780 | // TODO use vertex color to avoid breaking batches |
| 1781 | if (this->color() != that->color()) { |
| 1782 | return false; |
| 1783 | } |
| 1784 | |
| 1785 | if (this->stroke() != that->stroke()) { |
| 1786 | return false; |
| 1787 | } |
| 1788 | |
| 1789 | SkASSERT(this->usesLocalCoords() == that->usesLocalCoords()); |
| 1790 | if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) { |
| 1791 | return false; |
| 1792 | } |
| 1793 | |
| 1794 | fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin()); |
joshualitt | 99c7c07 | 2015-05-01 13:43:30 -0700 | [diff] [blame] | 1795 | this->joinBounds(that->bounds()); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1796 | return true; |
| 1797 | } |
| 1798 | |
| 1799 | GrColor color() const { return fBatch.fColor; } |
| 1800 | bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; } |
| 1801 | const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; } |
| 1802 | bool stroke() const { return fBatch.fStroke; } |
| 1803 | |
| 1804 | struct BatchTracker { |
| 1805 | GrColor fColor; |
| 1806 | bool fStroke; |
| 1807 | bool fUsesLocalCoords; |
| 1808 | bool fColorIgnored; |
| 1809 | bool fCoverageIgnored; |
| 1810 | }; |
| 1811 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1812 | BatchTracker fBatch; |
| 1813 | SkSTArray<1, Geometry, true> fGeoData; |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame^] | 1814 | |
| 1815 | typedef GrVertexBatch INHERITED; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1816 | }; |
| 1817 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 1818 | static GrDrawBatch* create_rrect_batch(GrColor color, |
| 1819 | const SkMatrix& viewMatrix, |
| 1820 | const SkRRect& rrect, |
| 1821 | const SkStrokeRec& stroke) { |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 1822 | SkASSERT(viewMatrix.rectStaysRect()); |
| 1823 | SkASSERT(rrect.isSimple()); |
| 1824 | SkASSERT(!rrect.isOval()); |
commit-bot@chromium.org | e0a868c | 2013-11-22 07:02:11 +0000 | [diff] [blame] | 1825 | |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 1826 | // RRect batchs only handle simple, but not too simple, rrects |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1827 | // do any matrix crunching before we reset the draw state for device coords |
| 1828 | const SkRect& rrectBounds = rrect.getBounds(); |
joshualitt | d96a67b | 2015-05-05 14:09:05 -0700 | [diff] [blame] | 1829 | SkRect bounds; |
| 1830 | viewMatrix.mapRect(&bounds, rrectBounds); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1831 | |
| 1832 | SkVector radii = rrect.getSimpleRadii(); |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 1833 | SkScalar xRadius = SkScalarAbs(viewMatrix[SkMatrix::kMScaleX]*radii.fX + |
| 1834 | viewMatrix[SkMatrix::kMSkewY]*radii.fY); |
| 1835 | SkScalar yRadius = SkScalarAbs(viewMatrix[SkMatrix::kMSkewX]*radii.fX + |
| 1836 | viewMatrix[SkMatrix::kMScaleY]*radii.fY); |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1837 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1838 | SkStrokeRec::Style style = stroke.getStyle(); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1839 | |
| 1840 | // do (potentially) anisotropic mapping of stroke |
| 1841 | SkVector scaledStroke; |
| 1842 | SkScalar strokeWidth = stroke.getWidth(); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1843 | |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 1844 | bool isStrokeOnly = SkStrokeRec::kStroke_Style == style || |
| 1845 | SkStrokeRec::kHairline_Style == style; |
| 1846 | bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == style; |
| 1847 | |
| 1848 | if (hasStroke) { |
| 1849 | if (SkStrokeRec::kHairline_Style == style) { |
| 1850 | scaledStroke.set(1, 1); |
| 1851 | } else { |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 1852 | scaledStroke.fX = SkScalarAbs(strokeWidth*(viewMatrix[SkMatrix::kMScaleX] + |
| 1853 | viewMatrix[SkMatrix::kMSkewY])); |
| 1854 | scaledStroke.fY = SkScalarAbs(strokeWidth*(viewMatrix[SkMatrix::kMSkewX] + |
| 1855 | viewMatrix[SkMatrix::kMScaleY])); |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 1856 | } |
| 1857 | |
| 1858 | // if half of strokewidth is greater than radius, we don't handle that right now |
| 1859 | if (SK_ScalarHalf*scaledStroke.fX > xRadius || SK_ScalarHalf*scaledStroke.fY > yRadius) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1860 | return nullptr; |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 1861 | } |
| 1862 | } |
| 1863 | |
| 1864 | // The way the effect interpolates the offset-to-ellipse/circle-center attribute only works on |
| 1865 | // the interior of the rrect if the radii are >= 0.5. Otherwise, the inner rect of the nine- |
| 1866 | // patch will have fractional coverage. This only matters when the interior is actually filled. |
| 1867 | // We could consider falling back to rect rendering here, since a tiny radius is |
| 1868 | // indistinguishable from a square corner. |
| 1869 | if (!isStrokeOnly && (SK_ScalarHalf > xRadius || SK_ScalarHalf > yRadius)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1870 | return nullptr; |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1871 | } |
| 1872 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1873 | // if the corners are circles, use the circle renderer |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 1874 | if ((!hasStroke || scaledStroke.fX == scaledStroke.fY) && xRadius == yRadius) { |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1875 | SkScalar innerRadius = 0.0f; |
| 1876 | SkScalar outerRadius = xRadius; |
| 1877 | SkScalar halfWidth = 0; |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 1878 | if (hasStroke) { |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1879 | if (SkScalarNearlyZero(scaledStroke.fX)) { |
| 1880 | halfWidth = SK_ScalarHalf; |
| 1881 | } else { |
| 1882 | halfWidth = SkScalarHalf(scaledStroke.fX); |
| 1883 | } |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 1884 | |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 1885 | if (isStrokeOnly) { |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1886 | innerRadius = xRadius - halfWidth; |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1887 | } |
| 1888 | outerRadius += halfWidth; |
joshualitt | d96a67b | 2015-05-05 14:09:05 -0700 | [diff] [blame] | 1889 | bounds.outset(halfWidth, halfWidth); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1890 | } |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 1891 | |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 1892 | isStrokeOnly = (isStrokeOnly && innerRadius >= 0); |
commit-bot@chromium.org | cefde6e | 2013-08-30 16:34:52 +0000 | [diff] [blame] | 1893 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1894 | // The radii are outset for two reasons. First, it allows the shader to simply perform |
bsalomon | ce1c886 | 2014-12-15 07:11:22 -0800 | [diff] [blame] | 1895 | // simpler computation because the computed alpha is zero, rather than 50%, at the radius. |
| 1896 | // Second, the outer radius is used to compute the verts of the bounding box that is |
| 1897 | // rendered and the outset ensures the box will cover all partially covered by the rrect |
| 1898 | // corners. |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1899 | outerRadius += SK_ScalarHalf; |
| 1900 | innerRadius -= SK_ScalarHalf; |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 1901 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1902 | // Expand the rect so all the pixels will be captured. |
joshualitt | d96a67b | 2015-05-05 14:09:05 -0700 | [diff] [blame] | 1903 | bounds.outset(SK_ScalarHalf, SK_ScalarHalf); |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 1904 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1905 | RRectCircleRendererBatch::Geometry geometry; |
| 1906 | geometry.fViewMatrix = viewMatrix; |
| 1907 | geometry.fColor = color; |
| 1908 | geometry.fInnerRadius = innerRadius; |
| 1909 | geometry.fOuterRadius = outerRadius; |
| 1910 | geometry.fStroke = isStrokeOnly; |
joshualitt | d96a67b | 2015-05-05 14:09:05 -0700 | [diff] [blame] | 1911 | geometry.fDevBounds = bounds; |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 1912 | |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 1913 | return RRectCircleRendererBatch::Create(geometry); |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1914 | // otherwise we use the ellipse renderer |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1915 | } else { |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1916 | SkScalar innerXRadius = 0.0f; |
| 1917 | SkScalar innerYRadius = 0.0f; |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 1918 | if (hasStroke) { |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1919 | if (SkScalarNearlyZero(scaledStroke.length())) { |
| 1920 | scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf); |
| 1921 | } else { |
| 1922 | scaledStroke.scale(SK_ScalarHalf); |
| 1923 | } |
| 1924 | |
| 1925 | // we only handle thick strokes for near-circular ellipses |
skia.committer@gmail.com | 8be02fc | 2013-05-17 07:01:11 +0000 | [diff] [blame] | 1926 | if (scaledStroke.length() > SK_ScalarHalf && |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1927 | (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1928 | return nullptr; |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1929 | } |
| 1930 | |
| 1931 | // we don't handle it if curvature of the stroke is less than curvature of the ellipse |
| 1932 | if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius || |
| 1933 | scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1934 | return nullptr; |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1935 | } |
| 1936 | |
| 1937 | // this is legit only if scale & translation (which should be the case at the moment) |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 1938 | if (isStrokeOnly) { |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1939 | innerXRadius = xRadius - scaledStroke.fX; |
| 1940 | innerYRadius = yRadius - scaledStroke.fY; |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1941 | } |
| 1942 | |
| 1943 | xRadius += scaledStroke.fX; |
| 1944 | yRadius += scaledStroke.fY; |
joshualitt | d96a67b | 2015-05-05 14:09:05 -0700 | [diff] [blame] | 1945 | bounds.outset(scaledStroke.fX, scaledStroke.fY); |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1946 | } |
jvanverth@google.com | e364741 | 2013-05-08 15:31:43 +0000 | [diff] [blame] | 1947 | |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 1948 | isStrokeOnly = (isStrokeOnly && innerXRadius >= 0 && innerYRadius >= 0); |
commit-bot@chromium.org | cefde6e | 2013-08-30 16:34:52 +0000 | [diff] [blame] | 1949 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1950 | // Expand the rect so all the pixels will be captured. |
joshualitt | d96a67b | 2015-05-05 14:09:05 -0700 | [diff] [blame] | 1951 | bounds.outset(SK_ScalarHalf, SK_ScalarHalf); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1952 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1953 | RRectEllipseRendererBatch::Geometry geometry; |
| 1954 | geometry.fViewMatrix = viewMatrix; |
| 1955 | geometry.fColor = color; |
| 1956 | geometry.fXRadius = xRadius; |
| 1957 | geometry.fYRadius = yRadius; |
| 1958 | geometry.fInnerXRadius = innerXRadius; |
| 1959 | geometry.fInnerYRadius = innerYRadius; |
| 1960 | geometry.fStroke = isStrokeOnly; |
joshualitt | d96a67b | 2015-05-05 14:09:05 -0700 | [diff] [blame] | 1961 | geometry.fDevBounds = bounds; |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1962 | |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 1963 | return RRectEllipseRendererBatch::Create(geometry); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1964 | } |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 1965 | } |
| 1966 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1967 | bool GrOvalRenderer::DrawRRect(GrDrawTarget* target, |
joshualitt | ae3d63a | 2015-07-13 08:44:06 -0700 | [diff] [blame] | 1968 | const GrPipelineBuilder& pipelineBuilder, |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 1969 | GrColor color, |
| 1970 | const SkMatrix& viewMatrix, |
| 1971 | bool useAA, |
| 1972 | const SkRRect& rrect, |
| 1973 | const SkStrokeRec& stroke) { |
| 1974 | if (rrect.isOval()) { |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1975 | return DrawOval(target, pipelineBuilder, color, viewMatrix, useAA, rrect.getBounds(), |
| 1976 | stroke); |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 1977 | } |
| 1978 | |
joshualitt | ae3d63a | 2015-07-13 08:44:06 -0700 | [diff] [blame] | 1979 | bool useCoverageAA = useAA && !pipelineBuilder.getRenderTarget()->isUnifiedMultisampled(); |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 1980 | |
| 1981 | // only anti-aliased rrects for now |
| 1982 | if (!useCoverageAA) { |
| 1983 | return false; |
| 1984 | } |
| 1985 | |
| 1986 | if (!viewMatrix.rectStaysRect() || !rrect.isSimple()) { |
| 1987 | return false; |
| 1988 | } |
| 1989 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 1990 | SkAutoTUnref<GrDrawBatch> batch(create_rrect_batch(color, viewMatrix, rrect, stroke)); |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 1991 | if (!batch) { |
| 1992 | return false; |
| 1993 | } |
| 1994 | |
joshualitt | ae3d63a | 2015-07-13 08:44:06 -0700 | [diff] [blame] | 1995 | target->drawBatch(pipelineBuilder, batch); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1996 | return true; |
| 1997 | } |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 1998 | |
| 1999 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2000 | |
| 2001 | #ifdef GR_TEST_UTILS |
| 2002 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 2003 | DRAW_BATCH_TEST_DEFINE(CircleBatch) { |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 2004 | SkMatrix viewMatrix = GrTest::TestMatrix(random); |
| 2005 | GrColor color = GrRandomColor(random); |
| 2006 | bool useCoverageAA = random->nextBool(); |
joshualitt | 6c89110 | 2015-05-13 08:51:49 -0700 | [diff] [blame] | 2007 | SkRect circle = GrTest::TestSquare(random); |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 2008 | return create_circle_batch(color, viewMatrix, useCoverageAA, circle, |
| 2009 | GrTest::TestStrokeRec(random)); |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 2010 | } |
| 2011 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 2012 | DRAW_BATCH_TEST_DEFINE(EllipseBatch) { |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 2013 | SkMatrix viewMatrix = GrTest::TestMatrixRectStaysRect(random); |
| 2014 | GrColor color = GrRandomColor(random); |
joshualitt | 6c89110 | 2015-05-13 08:51:49 -0700 | [diff] [blame] | 2015 | SkRect ellipse = GrTest::TestSquare(random); |
| 2016 | return create_ellipse_batch(color, viewMatrix, true, ellipse, |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 2017 | GrTest::TestStrokeRec(random)); |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 2018 | } |
| 2019 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 2020 | DRAW_BATCH_TEST_DEFINE(DIEllipseBatch) { |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 2021 | SkMatrix viewMatrix = GrTest::TestMatrix(random); |
| 2022 | GrColor color = GrRandomColor(random); |
| 2023 | bool useCoverageAA = random->nextBool(); |
joshualitt | 6c89110 | 2015-05-13 08:51:49 -0700 | [diff] [blame] | 2024 | SkRect ellipse = GrTest::TestSquare(random); |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 2025 | return create_diellipse_batch(color, viewMatrix, useCoverageAA, ellipse, |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 2026 | GrTest::TestStrokeRec(random)); |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 2027 | } |
| 2028 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 2029 | DRAW_BATCH_TEST_DEFINE(RRectBatch) { |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 2030 | SkMatrix viewMatrix = GrTest::TestMatrixRectStaysRect(random); |
| 2031 | GrColor color = GrRandomColor(random); |
| 2032 | const SkRRect& rrect = GrTest::TestRRectSimple(random); |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 2033 | return create_rrect_batch(color, viewMatrix, rrect, GrTest::TestStrokeRec(random)); |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 2034 | } |
| 2035 | |
| 2036 | #endif |