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