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 | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 93 | class GLSLProcessor : public GrGLSLGeometryProcessor { |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 94 | public: |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 95 | GLSLProcessor() |
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 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 168 | void getGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override { |
| 169 | GLSLProcessor::GenKey(*this, caps, b); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 170 | } |
| 171 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 172 | GrGLSLPrimitiveProcessor* createGLSLInstance(const GrGLSLCaps&) const override { |
| 173 | return new GLSLProcessor(); |
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 | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 241 | class GLSLProcessor : public GrGLSLGeometryProcessor { |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 242 | public: |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 243 | GLSLProcessor() |
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 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 337 | void getGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override { |
| 338 | GLSLProcessor::GenKey(*this, caps, b); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 339 | } |
| 340 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 341 | GrGLSLPrimitiveProcessor* createGLSLInstance(const GrGLSLCaps&) const override { |
| 342 | return new GLSLProcessor(); |
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 | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 416 | class GLSLProcessor : public GrGLSLGeometryProcessor { |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 417 | public: |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 418 | GLSLProcessor() |
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 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 531 | void getGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override { |
| 532 | GLSLProcessor::GenKey(*this, caps, b); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 533 | } |
| 534 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 535 | GrGLSLPrimitiveProcessor* createGLSLInstance(const GrGLSLCaps&) const override { |
| 536 | return new GLSLProcessor(); |
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 | |
robertphillips | e004bfc | 2015-11-16 09:06:59 -0800 | [diff] [blame^] | 629 | SkString dumpInfo() const override { |
| 630 | SkString string; |
| 631 | for (int i = 0; i < fGeoData.count(); ++i) { |
| 632 | string.appendf("Color: 0x%08x Rect [L: %.2f, T: %.2f, R: %.2f, B: %.2f]," |
| 633 | "InnerRad: %.2f, OuterRad: %.2f\n", |
| 634 | fGeoData[i].fColor, |
| 635 | fGeoData[i].fDevBounds.fLeft, fGeoData[i].fDevBounds.fTop, |
| 636 | fGeoData[i].fDevBounds.fRight, fGeoData[i].fDevBounds.fBottom, |
| 637 | fGeoData[i].fInnerRadius, |
| 638 | fGeoData[i].fOuterRadius); |
| 639 | } |
| 640 | string.append(INHERITED::dumpInfo()); |
| 641 | return string; |
| 642 | } |
| 643 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 644 | void getInvariantOutputColor(GrInitInvariantOutput* out) const override { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 645 | // When this is called on a batch, there is only one geometry bundle |
| 646 | out->setKnownFourComponents(fGeoData[0].fColor); |
| 647 | } |
| 648 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 649 | void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 650 | out->setUnknownSingleComponent(); |
| 651 | } |
| 652 | |
bsalomon | e46f9fe | 2015-08-18 06:05:14 -0700 | [diff] [blame] | 653 | private: |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 654 | void initBatchTracker(const GrPipelineOptimizations& opt) override { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 655 | // Handle any color overrides |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 656 | if (!opt.readsColor()) { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 657 | fGeoData[0].fColor = GrColor_ILLEGAL; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 658 | } |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 659 | opt.getOverrideColorIfSet(&fGeoData[0].fColor); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 660 | |
| 661 | // setup batch properties |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 662 | fBatch.fColorIgnored = !opt.readsColor(); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 663 | fBatch.fColor = fGeoData[0].fColor; |
| 664 | fBatch.fStroke = fGeoData[0].fStroke; |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 665 | fBatch.fUsesLocalCoords = opt.readsLocalCoords(); |
| 666 | fBatch.fCoverageIgnored = !opt.readsCoverage(); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 667 | } |
| 668 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 669 | void onPrepareDraws(Target* target) override { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 670 | SkMatrix invert; |
| 671 | if (!this->viewMatrix().invert(&invert)) { |
| 672 | return; |
| 673 | } |
| 674 | |
| 675 | // Setup geometry processor |
| 676 | SkAutoTUnref<GrGeometryProcessor> gp(CircleEdgeEffect::Create(this->color(), |
| 677 | this->stroke(), |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 678 | invert, |
| 679 | this->usesLocalCoords())); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 680 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 681 | target->initDraw(gp, this->pipeline()); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 682 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 683 | int instanceCount = fGeoData.count(); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 684 | size_t vertexStride = gp->getVertexStride(); |
| 685 | SkASSERT(vertexStride == sizeof(CircleVertex)); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 686 | QuadHelper helper; |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 687 | CircleVertex* verts = reinterpret_cast<CircleVertex*>(helper.init(target, vertexStride, |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 688 | instanceCount)); |
| 689 | if (!verts) { |
joshualitt | 4b31de8 | 2015-03-05 14:33:41 -0800 | [diff] [blame] | 690 | return; |
| 691 | } |
| 692 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 693 | for (int i = 0; i < instanceCount; i++) { |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 694 | Geometry& geom = fGeoData[i]; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 695 | |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 696 | SkScalar innerRadius = geom.fInnerRadius; |
| 697 | SkScalar outerRadius = geom.fOuterRadius; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 698 | |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 699 | const SkRect& bounds = geom.fDevBounds; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 700 | |
| 701 | // The inner radius in the vertex data must be specified in normalized space. |
| 702 | innerRadius = innerRadius / outerRadius; |
| 703 | verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop); |
| 704 | verts[0].fOffset = SkPoint::Make(-1, -1); |
| 705 | verts[0].fOuterRadius = outerRadius; |
| 706 | verts[0].fInnerRadius = innerRadius; |
| 707 | |
| 708 | verts[1].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom); |
| 709 | verts[1].fOffset = SkPoint::Make(-1, 1); |
| 710 | verts[1].fOuterRadius = outerRadius; |
| 711 | verts[1].fInnerRadius = innerRadius; |
| 712 | |
| 713 | verts[2].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom); |
| 714 | verts[2].fOffset = SkPoint::Make(1, 1); |
| 715 | verts[2].fOuterRadius = outerRadius; |
| 716 | verts[2].fInnerRadius = innerRadius; |
| 717 | |
| 718 | verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fTop); |
| 719 | verts[3].fOffset = SkPoint::Make(1, -1); |
| 720 | verts[3].fOuterRadius = outerRadius; |
| 721 | verts[3].fInnerRadius = innerRadius; |
| 722 | |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 723 | verts += kVerticesPerQuad; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 724 | } |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 725 | helper.recordDraw(target); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; } |
| 729 | |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 730 | CircleBatch(const Geometry& geometry) : INHERITED(ClassID()) { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 731 | fGeoData.push_back(geometry); |
joshualitt | 99c7c07 | 2015-05-01 13:43:30 -0700 | [diff] [blame] | 732 | |
| 733 | this->setBounds(geometry.fDevBounds); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 734 | } |
| 735 | |
bsalomon | cb02b38 | 2015-08-12 11:14:50 -0700 | [diff] [blame] | 736 | bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override { |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 737 | CircleBatch* that = t->cast<CircleBatch>(); |
| 738 | if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(), |
| 739 | that->bounds(), caps)) { |
joshualitt | 8cab9a7 | 2015-07-16 09:13:50 -0700 | [diff] [blame] | 740 | return false; |
| 741 | } |
| 742 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 743 | // TODO use vertex color to avoid breaking batches |
| 744 | if (this->color() != that->color()) { |
| 745 | return false; |
| 746 | } |
| 747 | |
| 748 | if (this->stroke() != that->stroke()) { |
| 749 | return false; |
| 750 | } |
| 751 | |
| 752 | SkASSERT(this->usesLocalCoords() == that->usesLocalCoords()); |
| 753 | if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) { |
| 754 | return false; |
| 755 | } |
| 756 | |
| 757 | fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin()); |
joshualitt | 99c7c07 | 2015-05-01 13:43:30 -0700 | [diff] [blame] | 758 | this->joinBounds(that->bounds()); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 759 | return true; |
| 760 | } |
| 761 | |
| 762 | GrColor color() const { return fBatch.fColor; } |
| 763 | bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; } |
| 764 | const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; } |
| 765 | bool stroke() const { return fBatch.fStroke; } |
| 766 | |
| 767 | struct BatchTracker { |
| 768 | GrColor fColor; |
| 769 | bool fStroke; |
| 770 | bool fUsesLocalCoords; |
| 771 | bool fColorIgnored; |
| 772 | bool fCoverageIgnored; |
| 773 | }; |
| 774 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 775 | BatchTracker fBatch; |
| 776 | SkSTArray<1, Geometry, true> fGeoData; |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 777 | |
| 778 | typedef GrVertexBatch INHERITED; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 779 | }; |
| 780 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 781 | static GrDrawBatch* create_circle_batch(GrColor color, |
| 782 | const SkMatrix& viewMatrix, |
| 783 | bool useCoverageAA, |
| 784 | const SkRect& circle, |
| 785 | const SkStrokeRec& stroke) { |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 786 | SkPoint center = SkPoint::Make(circle.centerX(), circle.centerY()); |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 787 | viewMatrix.mapPoints(¢er, 1); |
| 788 | SkScalar radius = viewMatrix.mapRadius(SkScalarHalf(circle.width())); |
| 789 | SkScalar strokeWidth = viewMatrix.mapRadius(stroke.getWidth()); |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 790 | |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 791 | SkStrokeRec::Style style = stroke.getStyle(); |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 792 | bool isStrokeOnly = SkStrokeRec::kStroke_Style == style || |
| 793 | SkStrokeRec::kHairline_Style == style; |
| 794 | bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == style; |
skia.committer@gmail.com | 7e32851 | 2013-03-23 07:01:28 +0000 | [diff] [blame] | 795 | |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 796 | SkScalar innerRadius = 0.0f; |
| 797 | SkScalar outerRadius = radius; |
| 798 | SkScalar halfWidth = 0; |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 799 | if (hasStroke) { |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 800 | if (SkScalarNearlyZero(strokeWidth)) { |
| 801 | halfWidth = SK_ScalarHalf; |
| 802 | } else { |
| 803 | halfWidth = SkScalarHalf(strokeWidth); |
| 804 | } |
| 805 | |
| 806 | outerRadius += halfWidth; |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 807 | if (isStrokeOnly) { |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 808 | innerRadius = radius - halfWidth; |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 809 | } |
| 810 | } |
| 811 | |
bsalomon | ce1c886 | 2014-12-15 07:11:22 -0800 | [diff] [blame] | 812 | // The radii are outset for two reasons. First, it allows the shader to simply perform simpler |
| 813 | // computation because the computed alpha is zero, rather than 50%, at the radius. |
| 814 | // Second, the outer radius is used to compute the verts of the bounding box that is rendered |
| 815 | // 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] | 816 | outerRadius += SK_ScalarHalf; |
| 817 | innerRadius -= SK_ScalarHalf; |
| 818 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 819 | CircleBatch::Geometry geometry; |
| 820 | geometry.fViewMatrix = viewMatrix; |
| 821 | geometry.fColor = color; |
| 822 | geometry.fInnerRadius = innerRadius; |
| 823 | geometry.fOuterRadius = outerRadius; |
| 824 | geometry.fStroke = isStrokeOnly && innerRadius > 0; |
joshualitt | d96a67b | 2015-05-05 14:09:05 -0700 | [diff] [blame] | 825 | geometry.fDevBounds = SkRect::MakeLTRB(center.fX - outerRadius, center.fY - outerRadius, |
| 826 | center.fX + outerRadius, center.fY + outerRadius); |
commit-bot@chromium.org | 0a6cb60 | 2013-04-11 15:05:37 +0000 | [diff] [blame] | 827 | |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 828 | return CircleBatch::Create(geometry); |
| 829 | } |
| 830 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 831 | void GrOvalRenderer::DrawCircle(GrDrawTarget* target, |
joshualitt | ae3d63a | 2015-07-13 08:44:06 -0700 | [diff] [blame] | 832 | const GrPipelineBuilder& pipelineBuilder, |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 833 | GrColor color, |
| 834 | const SkMatrix& viewMatrix, |
| 835 | bool useCoverageAA, |
| 836 | const SkRect& circle, |
| 837 | const SkStrokeRec& stroke) { |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 838 | SkAutoTUnref<GrDrawBatch> batch(create_circle_batch(color, viewMatrix, useCoverageAA, circle, |
| 839 | stroke)); |
joshualitt | ae3d63a | 2015-07-13 08:44:06 -0700 | [diff] [blame] | 840 | target->drawBatch(pipelineBuilder, batch); |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 841 | } |
| 842 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 843 | /////////////////////////////////////////////////////////////////////////////// |
| 844 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 845 | class EllipseBatch : public GrVertexBatch { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 846 | public: |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 847 | DEFINE_BATCH_CLASS_ID |
| 848 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 849 | struct Geometry { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 850 | SkMatrix fViewMatrix; |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 851 | SkRect fDevBounds; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 852 | SkScalar fXRadius; |
| 853 | SkScalar fYRadius; |
| 854 | SkScalar fInnerXRadius; |
| 855 | SkScalar fInnerYRadius; |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 856 | GrColor fColor; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 857 | bool fStroke; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 858 | }; |
| 859 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 860 | static GrDrawBatch* Create(const Geometry& geometry) { return new EllipseBatch(geometry); } |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 861 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 862 | const char* name() const override { return "EllipseBatch"; } |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 863 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 864 | void getInvariantOutputColor(GrInitInvariantOutput* out) const override { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 865 | // When this is called on a batch, there is only one geometry bundle |
| 866 | out->setKnownFourComponents(fGeoData[0].fColor); |
| 867 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 868 | void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 869 | out->setUnknownSingleComponent(); |
| 870 | } |
| 871 | |
bsalomon | e46f9fe | 2015-08-18 06:05:14 -0700 | [diff] [blame] | 872 | private: |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 873 | void initBatchTracker(const GrPipelineOptimizations& opt) override { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 874 | // Handle any color overrides |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 875 | if (!opt.readsCoverage()) { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 876 | fGeoData[0].fColor = GrColor_ILLEGAL; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 877 | } |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 878 | opt.getOverrideColorIfSet(&fGeoData[0].fColor); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 879 | |
| 880 | // setup batch properties |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 881 | fBatch.fColorIgnored = !opt.readsColor(); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 882 | fBatch.fColor = fGeoData[0].fColor; |
| 883 | fBatch.fStroke = fGeoData[0].fStroke; |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 884 | fBatch.fUsesLocalCoords = opt.readsLocalCoords(); |
| 885 | fBatch.fCoverageIgnored = !opt.readsCoverage(); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 886 | } |
| 887 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 888 | void onPrepareDraws(Target* target) override { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 889 | SkMatrix invert; |
| 890 | if (!this->viewMatrix().invert(&invert)) { |
| 891 | return; |
| 892 | } |
| 893 | |
| 894 | // Setup geometry processor |
| 895 | SkAutoTUnref<GrGeometryProcessor> gp(EllipseEdgeEffect::Create(this->color(), |
| 896 | this->stroke(), |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 897 | invert, |
| 898 | this->usesLocalCoords())); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 899 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 900 | target->initDraw(gp, this->pipeline()); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 901 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 902 | int instanceCount = fGeoData.count(); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 903 | QuadHelper helper; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 904 | size_t vertexStride = gp->getVertexStride(); |
joshualitt | 19e0058 | 2015-02-11 17:36:30 -0800 | [diff] [blame] | 905 | SkASSERT(vertexStride == sizeof(EllipseVertex)); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 906 | EllipseVertex* verts = reinterpret_cast<EllipseVertex*>( |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 907 | helper.init(target, vertexStride, instanceCount)); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 908 | if (!verts) { |
joshualitt | 4b31de8 | 2015-03-05 14:33:41 -0800 | [diff] [blame] | 909 | return; |
| 910 | } |
| 911 | |
bsalomon | 8415abe | 2015-05-04 11:41:41 -0700 | [diff] [blame] | 912 | for (int i = 0; i < instanceCount; i++) { |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 913 | Geometry& geom = fGeoData[i]; |
bsalomon | 8415abe | 2015-05-04 11:41:41 -0700 | [diff] [blame] | 914 | |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 915 | SkScalar xRadius = geom.fXRadius; |
| 916 | SkScalar yRadius = geom.fYRadius; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 917 | |
| 918 | // Compute the reciprocals of the radii here to save time in the shader |
| 919 | SkScalar xRadRecip = SkScalarInvert(xRadius); |
| 920 | SkScalar yRadRecip = SkScalarInvert(yRadius); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 921 | SkScalar xInnerRadRecip = SkScalarInvert(geom.fInnerXRadius); |
| 922 | SkScalar yInnerRadRecip = SkScalarInvert(geom.fInnerYRadius); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 923 | |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 924 | const SkRect& bounds = geom.fDevBounds; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 925 | |
| 926 | // The inner radius in the vertex data must be specified in normalized space. |
| 927 | verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop); |
| 928 | verts[0].fOffset = SkPoint::Make(-xRadius, -yRadius); |
| 929 | verts[0].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); |
| 930 | verts[0].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); |
| 931 | |
| 932 | verts[1].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom); |
| 933 | verts[1].fOffset = SkPoint::Make(-xRadius, yRadius); |
| 934 | verts[1].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); |
| 935 | verts[1].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); |
| 936 | |
| 937 | verts[2].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom); |
| 938 | verts[2].fOffset = SkPoint::Make(xRadius, yRadius); |
| 939 | verts[2].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); |
| 940 | verts[2].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); |
| 941 | |
| 942 | verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fTop); |
| 943 | verts[3].fOffset = SkPoint::Make(xRadius, -yRadius); |
| 944 | verts[3].fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); |
| 945 | verts[3].fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); |
| 946 | |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 947 | verts += kVerticesPerQuad; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 948 | } |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 949 | helper.recordDraw(target); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; } |
| 953 | |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 954 | EllipseBatch(const Geometry& geometry) : INHERITED(ClassID()) { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 955 | fGeoData.push_back(geometry); |
joshualitt | 99c7c07 | 2015-05-01 13:43:30 -0700 | [diff] [blame] | 956 | |
| 957 | this->setBounds(geometry.fDevBounds); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 958 | } |
| 959 | |
bsalomon | cb02b38 | 2015-08-12 11:14:50 -0700 | [diff] [blame] | 960 | bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override { |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 961 | EllipseBatch* that = t->cast<EllipseBatch>(); |
| 962 | |
| 963 | if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(), |
| 964 | that->bounds(), caps)) { |
joshualitt | 8cab9a7 | 2015-07-16 09:13:50 -0700 | [diff] [blame] | 965 | return false; |
| 966 | } |
| 967 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 968 | // TODO use vertex color to avoid breaking batches |
| 969 | if (this->color() != that->color()) { |
| 970 | return false; |
| 971 | } |
| 972 | |
| 973 | if (this->stroke() != that->stroke()) { |
| 974 | return false; |
| 975 | } |
| 976 | |
| 977 | SkASSERT(this->usesLocalCoords() == that->usesLocalCoords()); |
| 978 | if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) { |
| 979 | return false; |
| 980 | } |
| 981 | |
| 982 | fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin()); |
joshualitt | 99c7c07 | 2015-05-01 13:43:30 -0700 | [diff] [blame] | 983 | this->joinBounds(that->bounds()); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 984 | return true; |
| 985 | } |
| 986 | |
| 987 | GrColor color() const { return fBatch.fColor; } |
| 988 | bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; } |
| 989 | const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; } |
| 990 | bool stroke() const { return fBatch.fStroke; } |
| 991 | |
| 992 | struct BatchTracker { |
| 993 | GrColor fColor; |
| 994 | bool fStroke; |
| 995 | bool fUsesLocalCoords; |
| 996 | bool fColorIgnored; |
| 997 | bool fCoverageIgnored; |
| 998 | }; |
| 999 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1000 | BatchTracker fBatch; |
| 1001 | SkSTArray<1, Geometry, true> fGeoData; |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 1002 | |
| 1003 | typedef GrVertexBatch INHERITED; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1004 | }; |
| 1005 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 1006 | static GrDrawBatch* create_ellipse_batch(GrColor color, |
| 1007 | const SkMatrix& viewMatrix, |
| 1008 | bool useCoverageAA, |
| 1009 | const SkRect& ellipse, |
| 1010 | const SkStrokeRec& stroke) { |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 1011 | #ifdef SK_DEBUG |
| 1012 | { |
| 1013 | // we should have checked for this previously |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 1014 | bool isAxisAlignedEllipse = viewMatrix.rectStaysRect(); |
commit-bot@chromium.org | e0a868c | 2013-11-22 07:02:11 +0000 | [diff] [blame] | 1015 | SkASSERT(useCoverageAA && isAxisAlignedEllipse); |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 1016 | } |
| 1017 | #endif |
| 1018 | |
commit-bot@chromium.org | 0c88828 | 2013-04-19 19:01:45 +0000 | [diff] [blame] | 1019 | // 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] | 1020 | SkPoint center = SkPoint::Make(ellipse.centerX(), ellipse.centerY()); |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 1021 | viewMatrix.mapPoints(¢er, 1); |
commit-bot@chromium.org | 0c88828 | 2013-04-19 19:01:45 +0000 | [diff] [blame] | 1022 | SkScalar ellipseXRadius = SkScalarHalf(ellipse.width()); |
| 1023 | SkScalar ellipseYRadius = SkScalarHalf(ellipse.height()); |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 1024 | SkScalar xRadius = SkScalarAbs(viewMatrix[SkMatrix::kMScaleX]*ellipseXRadius + |
| 1025 | viewMatrix[SkMatrix::kMSkewY]*ellipseYRadius); |
| 1026 | SkScalar yRadius = SkScalarAbs(viewMatrix[SkMatrix::kMSkewX]*ellipseXRadius + |
| 1027 | viewMatrix[SkMatrix::kMScaleY]*ellipseYRadius); |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 1028 | |
commit-bot@chromium.org | 0c88828 | 2013-04-19 19:01:45 +0000 | [diff] [blame] | 1029 | // do (potentially) anisotropic mapping of stroke |
| 1030 | SkVector scaledStroke; |
| 1031 | SkScalar strokeWidth = stroke.getWidth(); |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 1032 | scaledStroke.fX = SkScalarAbs(strokeWidth*(viewMatrix[SkMatrix::kMScaleX] + |
| 1033 | viewMatrix[SkMatrix::kMSkewY])); |
| 1034 | scaledStroke.fY = SkScalarAbs(strokeWidth*(viewMatrix[SkMatrix::kMSkewX] + |
| 1035 | viewMatrix[SkMatrix::kMScaleY])); |
commit-bot@chromium.org | 0c88828 | 2013-04-19 19:01:45 +0000 | [diff] [blame] | 1036 | |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1037 | SkStrokeRec::Style style = stroke.getStyle(); |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 1038 | bool isStrokeOnly = SkStrokeRec::kStroke_Style == style || |
| 1039 | SkStrokeRec::kHairline_Style == style; |
| 1040 | bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == style; |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1041 | |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 1042 | SkScalar innerXRadius = 0; |
| 1043 | SkScalar innerYRadius = 0; |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 1044 | if (hasStroke) { |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1045 | if (SkScalarNearlyZero(scaledStroke.length())) { |
| 1046 | scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf); |
| 1047 | } else { |
| 1048 | scaledStroke.scale(SK_ScalarHalf); |
| 1049 | } |
| 1050 | |
| 1051 | // we only handle thick strokes for near-circular ellipses |
| 1052 | if (scaledStroke.length() > SK_ScalarHalf && |
| 1053 | (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1054 | return nullptr; |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
| 1057 | // we don't handle it if curvature of the stroke is less than curvature of the ellipse |
| 1058 | if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius || |
| 1059 | scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1060 | return nullptr; |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1061 | } |
| 1062 | |
| 1063 | // 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] | 1064 | if (isStrokeOnly) { |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1065 | innerXRadius = xRadius - scaledStroke.fX; |
| 1066 | innerYRadius = yRadius - scaledStroke.fY; |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | xRadius += scaledStroke.fX; |
| 1070 | yRadius += scaledStroke.fY; |
| 1071 | } |
| 1072 | |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 1073 | // 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] | 1074 | // 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] | 1075 | // TODO: Consider if we should use sqrt(2)/2 instead |
commit-bot@chromium.org | 0a6cb60 | 2013-04-11 15:05:37 +0000 | [diff] [blame] | 1076 | xRadius += SK_ScalarHalf; |
| 1077 | yRadius += SK_ScalarHalf; |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 1078 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1079 | EllipseBatch::Geometry geometry; |
| 1080 | geometry.fViewMatrix = viewMatrix; |
| 1081 | geometry.fColor = color; |
| 1082 | geometry.fXRadius = xRadius; |
| 1083 | geometry.fYRadius = yRadius; |
| 1084 | geometry.fInnerXRadius = innerXRadius; |
| 1085 | geometry.fInnerYRadius = innerYRadius; |
| 1086 | geometry.fStroke = isStrokeOnly && innerXRadius > 0 && innerYRadius > 0; |
joshualitt | d96a67b | 2015-05-05 14:09:05 -0700 | [diff] [blame] | 1087 | geometry.fDevBounds = SkRect::MakeLTRB(center.fX - xRadius, center.fY - yRadius, |
| 1088 | center.fX + xRadius, center.fY + yRadius); |
commit-bot@chromium.org | 0a6cb60 | 2013-04-11 15:05:37 +0000 | [diff] [blame] | 1089 | |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 1090 | return EllipseBatch::Create(geometry); |
| 1091 | } |
| 1092 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1093 | bool GrOvalRenderer::DrawEllipse(GrDrawTarget* target, |
joshualitt | ae3d63a | 2015-07-13 08:44:06 -0700 | [diff] [blame] | 1094 | const GrPipelineBuilder& pipelineBuilder, |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 1095 | GrColor color, |
| 1096 | const SkMatrix& viewMatrix, |
| 1097 | bool useCoverageAA, |
| 1098 | const SkRect& ellipse, |
| 1099 | const SkStrokeRec& stroke) { |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 1100 | SkAutoTUnref<GrDrawBatch> batch(create_ellipse_batch(color, viewMatrix, useCoverageAA, ellipse, |
| 1101 | stroke)); |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 1102 | if (!batch) { |
| 1103 | return false; |
| 1104 | } |
| 1105 | |
joshualitt | ae3d63a | 2015-07-13 08:44:06 -0700 | [diff] [blame] | 1106 | target->drawBatch(pipelineBuilder, batch); |
jvanverth@google.com | c4f2eca | 2013-04-16 12:30:35 +0000 | [diff] [blame] | 1107 | return true; |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 1108 | } |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1109 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1110 | ///////////////////////////////////////////////////////////////////////////////////////////////// |
| 1111 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 1112 | class DIEllipseBatch : public GrVertexBatch { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1113 | public: |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 1114 | DEFINE_BATCH_CLASS_ID |
| 1115 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1116 | struct Geometry { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1117 | SkMatrix fViewMatrix; |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 1118 | SkRect fBounds; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1119 | SkScalar fXRadius; |
| 1120 | SkScalar fYRadius; |
| 1121 | SkScalar fInnerXRadius; |
| 1122 | SkScalar fInnerYRadius; |
| 1123 | SkScalar fGeoDx; |
| 1124 | SkScalar fGeoDy; |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 1125 | GrColor fColor; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1126 | DIEllipseEdgeEffect::Mode fMode; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1127 | }; |
| 1128 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 1129 | static GrDrawBatch* Create(const Geometry& geometry, const SkRect& bounds) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 1130 | return new DIEllipseBatch(geometry, bounds); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1131 | } |
| 1132 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 1133 | const char* name() const override { return "DIEllipseBatch"; } |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1134 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 1135 | void getInvariantOutputColor(GrInitInvariantOutput* out) const override { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1136 | // When this is called on a batch, there is only one geometry bundle |
| 1137 | out->setKnownFourComponents(fGeoData[0].fColor); |
| 1138 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 1139 | void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1140 | out->setUnknownSingleComponent(); |
| 1141 | } |
| 1142 | |
bsalomon | e46f9fe | 2015-08-18 06:05:14 -0700 | [diff] [blame] | 1143 | private: |
| 1144 | |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 1145 | void initBatchTracker(const GrPipelineOptimizations& opt) override { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1146 | // Handle any color overrides |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 1147 | if (!opt.readsColor()) { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1148 | fGeoData[0].fColor = GrColor_ILLEGAL; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1149 | } |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 1150 | opt.getOverrideColorIfSet(&fGeoData[0].fColor); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1151 | |
| 1152 | // setup batch properties |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 1153 | fBatch.fColorIgnored = !opt.readsColor(); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1154 | fBatch.fColor = fGeoData[0].fColor; |
| 1155 | fBatch.fMode = fGeoData[0].fMode; |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 1156 | fBatch.fUsesLocalCoords = opt.readsLocalCoords(); |
| 1157 | fBatch.fCoverageIgnored = !opt.readsCoverage(); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1158 | } |
| 1159 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 1160 | void onPrepareDraws(Target* target) override { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1161 | // Setup geometry processor |
| 1162 | SkAutoTUnref<GrGeometryProcessor> gp(DIEllipseEdgeEffect::Create(this->color(), |
| 1163 | this->viewMatrix(), |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 1164 | this->mode(), |
| 1165 | this->usesLocalCoords())); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1166 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 1167 | target->initDraw(gp, this->pipeline()); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1168 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1169 | int instanceCount = fGeoData.count(); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1170 | size_t vertexStride = gp->getVertexStride(); |
joshualitt | 19e0058 | 2015-02-11 17:36:30 -0800 | [diff] [blame] | 1171 | SkASSERT(vertexStride == sizeof(DIEllipseVertex)); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 1172 | QuadHelper helper; |
| 1173 | DIEllipseVertex* verts = reinterpret_cast<DIEllipseVertex*>( |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 1174 | helper.init(target, vertexStride, instanceCount)); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 1175 | if (!verts) { |
joshualitt | 4b31de8 | 2015-03-05 14:33:41 -0800 | [diff] [blame] | 1176 | return; |
| 1177 | } |
| 1178 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1179 | for (int i = 0; i < instanceCount; i++) { |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 1180 | Geometry& geom = fGeoData[i]; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1181 | |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 1182 | SkScalar xRadius = geom.fXRadius; |
| 1183 | SkScalar yRadius = geom.fYRadius; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1184 | |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 1185 | const SkRect& bounds = geom.fBounds; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1186 | |
| 1187 | // This adjusts the "radius" to include the half-pixel border |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 1188 | SkScalar offsetDx = geom.fGeoDx / xRadius; |
| 1189 | SkScalar offsetDy = geom.fGeoDy / yRadius; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1190 | |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 1191 | SkScalar innerRatioX = xRadius / geom.fInnerXRadius; |
| 1192 | SkScalar innerRatioY = yRadius / geom.fInnerYRadius; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1193 | |
| 1194 | verts[0].fPos = SkPoint::Make(bounds.fLeft, bounds.fTop); |
| 1195 | verts[0].fOuterOffset = SkPoint::Make(-1.0f - offsetDx, -1.0f - offsetDy); |
| 1196 | verts[0].fInnerOffset = SkPoint::Make(-innerRatioX - offsetDx, -innerRatioY - offsetDy); |
| 1197 | |
| 1198 | verts[1].fPos = SkPoint::Make(bounds.fLeft, bounds.fBottom); |
| 1199 | verts[1].fOuterOffset = SkPoint::Make(-1.0f - offsetDx, 1.0f + offsetDy); |
| 1200 | verts[1].fInnerOffset = SkPoint::Make(-innerRatioX - offsetDx, innerRatioY + offsetDy); |
| 1201 | |
| 1202 | verts[2].fPos = SkPoint::Make(bounds.fRight, bounds.fBottom); |
| 1203 | verts[2].fOuterOffset = SkPoint::Make(1.0f + offsetDx, 1.0f + offsetDy); |
| 1204 | verts[2].fInnerOffset = SkPoint::Make(innerRatioX + offsetDx, innerRatioY + offsetDy); |
| 1205 | |
| 1206 | verts[3].fPos = SkPoint::Make(bounds.fRight, bounds.fTop); |
| 1207 | verts[3].fOuterOffset = SkPoint::Make(1.0f + offsetDx, -1.0f - offsetDy); |
| 1208 | verts[3].fInnerOffset = SkPoint::Make(innerRatioX + offsetDx, -innerRatioY - offsetDy); |
| 1209 | |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 1210 | verts += kVerticesPerQuad; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1211 | } |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 1212 | helper.recordDraw(target); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1213 | } |
| 1214 | |
| 1215 | SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; } |
| 1216 | |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 1217 | DIEllipseBatch(const Geometry& geometry, const SkRect& bounds) : INHERITED(ClassID()) { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1218 | fGeoData.push_back(geometry); |
joshualitt | 99c7c07 | 2015-05-01 13:43:30 -0700 | [diff] [blame] | 1219 | |
| 1220 | this->setBounds(bounds); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1221 | } |
| 1222 | |
bsalomon | cb02b38 | 2015-08-12 11:14:50 -0700 | [diff] [blame] | 1223 | bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override { |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 1224 | DIEllipseBatch* that = t->cast<DIEllipseBatch>(); |
| 1225 | if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(), |
| 1226 | that->bounds(), caps)) { |
joshualitt | 8cab9a7 | 2015-07-16 09:13:50 -0700 | [diff] [blame] | 1227 | return false; |
| 1228 | } |
| 1229 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1230 | // TODO use vertex color to avoid breaking batches |
| 1231 | if (this->color() != that->color()) { |
| 1232 | return false; |
| 1233 | } |
| 1234 | |
| 1235 | if (this->mode() != that->mode()) { |
| 1236 | return false; |
| 1237 | } |
| 1238 | |
joshualitt | d96a67b | 2015-05-05 14:09:05 -0700 | [diff] [blame] | 1239 | // TODO rewrite to allow positioning on CPU |
| 1240 | if (!this->viewMatrix().cheapEqualTo(that->viewMatrix())) { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1241 | return false; |
| 1242 | } |
| 1243 | |
| 1244 | fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin()); |
joshualitt | 99c7c07 | 2015-05-01 13:43:30 -0700 | [diff] [blame] | 1245 | this->joinBounds(that->bounds()); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1246 | return true; |
| 1247 | } |
| 1248 | |
| 1249 | GrColor color() const { return fBatch.fColor; } |
| 1250 | bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; } |
| 1251 | const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; } |
| 1252 | DIEllipseEdgeEffect::Mode mode() const { return fBatch.fMode; } |
| 1253 | |
| 1254 | struct BatchTracker { |
| 1255 | GrColor fColor; |
| 1256 | DIEllipseEdgeEffect::Mode fMode; |
| 1257 | bool fUsesLocalCoords; |
| 1258 | bool fColorIgnored; |
| 1259 | bool fCoverageIgnored; |
| 1260 | }; |
| 1261 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1262 | BatchTracker fBatch; |
| 1263 | SkSTArray<1, Geometry, true> fGeoData; |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 1264 | |
| 1265 | typedef GrVertexBatch INHERITED; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1266 | }; |
| 1267 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 1268 | static GrDrawBatch* create_diellipse_batch(GrColor color, |
| 1269 | const SkMatrix& viewMatrix, |
| 1270 | bool useCoverageAA, |
| 1271 | const SkRect& ellipse, |
| 1272 | const SkStrokeRec& stroke) { |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 1273 | SkPoint center = SkPoint::Make(ellipse.centerX(), ellipse.centerY()); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 1274 | SkScalar xRadius = SkScalarHalf(ellipse.width()); |
skia.committer@gmail.com | 6fc1b49 | 2013-09-06 07:01:45 +0000 | [diff] [blame] | 1275 | SkScalar yRadius = SkScalarHalf(ellipse.height()); |
| 1276 | |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 1277 | SkStrokeRec::Style style = stroke.getStyle(); |
skia.committer@gmail.com | 6fc1b49 | 2013-09-06 07:01:45 +0000 | [diff] [blame] | 1278 | DIEllipseEdgeEffect::Mode mode = (SkStrokeRec::kStroke_Style == style) ? |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 1279 | DIEllipseEdgeEffect::kStroke : |
skia.committer@gmail.com | 6fc1b49 | 2013-09-06 07:01:45 +0000 | [diff] [blame] | 1280 | (SkStrokeRec::kHairline_Style == style) ? |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 1281 | DIEllipseEdgeEffect::kHairline : DIEllipseEdgeEffect::kFill; |
| 1282 | |
| 1283 | SkScalar innerXRadius = 0; |
| 1284 | SkScalar innerYRadius = 0; |
| 1285 | if (SkStrokeRec::kFill_Style != style && SkStrokeRec::kHairline_Style != style) { |
| 1286 | SkScalar strokeWidth = stroke.getWidth(); |
| 1287 | |
| 1288 | if (SkScalarNearlyZero(strokeWidth)) { |
| 1289 | strokeWidth = SK_ScalarHalf; |
| 1290 | } else { |
| 1291 | strokeWidth *= SK_ScalarHalf; |
| 1292 | } |
| 1293 | |
| 1294 | // we only handle thick strokes for near-circular ellipses |
| 1295 | if (strokeWidth > SK_ScalarHalf && |
| 1296 | (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1297 | return nullptr; |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 1298 | } |
| 1299 | |
| 1300 | // we don't handle it if curvature of the stroke is less than curvature of the ellipse |
| 1301 | if (strokeWidth*(yRadius*yRadius) < (strokeWidth*strokeWidth)*xRadius || |
| 1302 | strokeWidth*(xRadius*xRadius) < (strokeWidth*strokeWidth)*yRadius) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1303 | return nullptr; |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 1304 | } |
| 1305 | |
| 1306 | // set inner radius (if needed) |
| 1307 | if (SkStrokeRec::kStroke_Style == style) { |
| 1308 | innerXRadius = xRadius - strokeWidth; |
| 1309 | innerYRadius = yRadius - strokeWidth; |
| 1310 | } |
| 1311 | |
| 1312 | xRadius += strokeWidth; |
| 1313 | yRadius += strokeWidth; |
| 1314 | } |
| 1315 | if (DIEllipseEdgeEffect::kStroke == mode) { |
| 1316 | mode = (innerXRadius > 0 && innerYRadius > 0) ? DIEllipseEdgeEffect::kStroke : |
| 1317 | DIEllipseEdgeEffect::kFill; |
| 1318 | } |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 1319 | |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 1320 | // 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] | 1321 | SkScalar a = viewMatrix[SkMatrix::kMScaleX]; |
| 1322 | SkScalar b = viewMatrix[SkMatrix::kMSkewX]; |
| 1323 | SkScalar c = viewMatrix[SkMatrix::kMSkewY]; |
| 1324 | SkScalar d = viewMatrix[SkMatrix::kMScaleY]; |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 1325 | SkScalar geoDx = SK_ScalarHalf / SkScalarSqrt(a*a + c*c); |
| 1326 | SkScalar geoDy = SK_ScalarHalf / SkScalarSqrt(b*b + d*d); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 1327 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1328 | DIEllipseBatch::Geometry geometry; |
| 1329 | geometry.fViewMatrix = viewMatrix; |
| 1330 | geometry.fColor = color; |
| 1331 | geometry.fXRadius = xRadius; |
| 1332 | geometry.fYRadius = yRadius; |
| 1333 | geometry.fInnerXRadius = innerXRadius; |
| 1334 | geometry.fInnerYRadius = innerYRadius; |
| 1335 | geometry.fGeoDx = geoDx; |
| 1336 | geometry.fGeoDy = geoDy; |
| 1337 | geometry.fMode = mode; |
joshualitt | d96a67b | 2015-05-05 14:09:05 -0700 | [diff] [blame] | 1338 | geometry.fBounds = SkRect::MakeLTRB(center.fX - xRadius - geoDx, center.fY - yRadius - geoDy, |
| 1339 | center.fX + xRadius + geoDx, center.fY + yRadius + geoDy); |
egdaniel | 9ef1bb1 | 2015-04-20 12:28:57 -0700 | [diff] [blame] | 1340 | |
joshualitt | d96a67b | 2015-05-05 14:09:05 -0700 | [diff] [blame] | 1341 | SkRect devBounds = geometry.fBounds; |
| 1342 | viewMatrix.mapRect(&devBounds); |
| 1343 | return DIEllipseBatch::Create(geometry, devBounds); |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 1344 | } |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 1345 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1346 | bool GrOvalRenderer::DrawDIEllipse(GrDrawTarget* target, |
joshualitt | ae3d63a | 2015-07-13 08:44:06 -0700 | [diff] [blame] | 1347 | const GrPipelineBuilder& pipelineBuilder, |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 1348 | GrColor color, |
| 1349 | const SkMatrix& viewMatrix, |
| 1350 | bool useCoverageAA, |
| 1351 | const SkRect& ellipse, |
| 1352 | const SkStrokeRec& stroke) { |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 1353 | SkAutoTUnref<GrDrawBatch> batch(create_diellipse_batch(color, viewMatrix, useCoverageAA, |
| 1354 | ellipse, stroke)); |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 1355 | if (!batch) { |
| 1356 | return false; |
| 1357 | } |
joshualitt | ae3d63a | 2015-07-13 08:44:06 -0700 | [diff] [blame] | 1358 | target->drawBatch(pipelineBuilder, batch); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 1359 | return true; |
| 1360 | } |
| 1361 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1362 | /////////////////////////////////////////////////////////////////////////////// |
| 1363 | |
| 1364 | static const uint16_t gRRectIndices[] = { |
| 1365 | // corners |
| 1366 | 0, 1, 5, 0, 5, 4, |
| 1367 | 2, 3, 7, 2, 7, 6, |
| 1368 | 8, 9, 13, 8, 13, 12, |
| 1369 | 10, 11, 15, 10, 15, 14, |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 1370 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1371 | // edges |
| 1372 | 1, 2, 6, 1, 6, 5, |
| 1373 | 4, 5, 9, 4, 9, 8, |
| 1374 | 6, 7, 11, 6, 11, 10, |
| 1375 | 9, 10, 14, 9, 14, 13, |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 1376 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1377 | // center |
| 1378 | // we place this at the end so that we can ignore these indices when rendering stroke-only |
| 1379 | 5, 6, 10, 5, 10, 9 |
| 1380 | }; |
| 1381 | |
joshualitt | 5ead6da | 2014-10-22 16:00:29 -0700 | [diff] [blame] | 1382 | static const int kIndicesPerStrokeRRect = SK_ARRAY_COUNT(gRRectIndices) - 6; |
| 1383 | static const int kIndicesPerRRect = SK_ARRAY_COUNT(gRRectIndices); |
| 1384 | static const int kVertsPerRRect = 16; |
| 1385 | static const int kNumRRectsInIndexBuffer = 256; |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1386 | |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 1387 | GR_DECLARE_STATIC_UNIQUE_KEY(gStrokeRRectOnlyIndexBufferKey); |
| 1388 | GR_DECLARE_STATIC_UNIQUE_KEY(gRRectOnlyIndexBufferKey); |
| 1389 | static const GrIndexBuffer* ref_rrect_index_buffer(bool strokeOnly, |
| 1390 | GrResourceProvider* resourceProvider) { |
| 1391 | GR_DEFINE_STATIC_UNIQUE_KEY(gStrokeRRectOnlyIndexBufferKey); |
| 1392 | GR_DEFINE_STATIC_UNIQUE_KEY(gRRectOnlyIndexBufferKey); |
| 1393 | if (strokeOnly) { |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 1394 | return resourceProvider->findOrCreateInstancedIndexBuffer( |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 1395 | gRRectIndices, kIndicesPerStrokeRRect, kNumRRectsInIndexBuffer, kVertsPerRRect, |
| 1396 | gStrokeRRectOnlyIndexBufferKey); |
| 1397 | } else { |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 1398 | return resourceProvider->findOrCreateInstancedIndexBuffer( |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 1399 | gRRectIndices, kIndicesPerRRect, kNumRRectsInIndexBuffer, kVertsPerRRect, |
| 1400 | gRRectOnlyIndexBufferKey); |
| 1401 | |
| 1402 | } |
| 1403 | } |
| 1404 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1405 | bool GrOvalRenderer::DrawDRRect(GrDrawTarget* target, |
joshualitt | ae3d63a | 2015-07-13 08:44:06 -0700 | [diff] [blame] | 1406 | const GrPipelineBuilder& pipelineBuilder, |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 1407 | GrColor color, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 1408 | const SkMatrix& viewMatrix, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 1409 | bool useAA, |
| 1410 | const SkRRect& origOuter, |
| 1411 | const SkRRect& origInner) { |
joshualitt | ae3d63a | 2015-07-13 08:44:06 -0700 | [diff] [blame] | 1412 | bool applyAA = useAA && !pipelineBuilder.getRenderTarget()->isUnifiedMultisampled(); |
joshualitt | 4421a4c | 2015-07-13 09:36:41 -0700 | [diff] [blame] | 1413 | GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps; |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 1414 | if (!origInner.isEmpty()) { |
| 1415 | SkTCopyOnFirstWrite<SkRRect> inner(origInner); |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 1416 | if (!viewMatrix.isIdentity()) { |
| 1417 | if (!origInner.transform(viewMatrix, inner.writable())) { |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 1418 | return false; |
| 1419 | } |
| 1420 | } |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1421 | GrPrimitiveEdgeType edgeType = applyAA ? |
| 1422 | kInverseFillAA_GrProcessorEdgeType : |
| 1423 | kInverseFillBW_GrProcessorEdgeType; |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 1424 | // TODO this needs to be a geometry processor |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1425 | GrFragmentProcessor* fp = GrRRectEffect::Create(edgeType, *inner); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1426 | if (nullptr == fp) { |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 1427 | return false; |
| 1428 | } |
joshualitt | 4421a4c | 2015-07-13 09:36:41 -0700 | [diff] [blame] | 1429 | arfps.set(&pipelineBuilder); |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 1430 | arfps.addCoverageFragmentProcessor(fp)->unref(); |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 1431 | } |
| 1432 | |
| 1433 | SkStrokeRec fillRec(SkStrokeRec::kFill_InitStyle); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1434 | if (DrawRRect(target, pipelineBuilder, color, viewMatrix, useAA, origOuter, fillRec)) { |
bsalomon | 8af0523 | 2014-06-03 06:34:58 -0700 | [diff] [blame] | 1435 | return true; |
| 1436 | } |
| 1437 | |
| 1438 | SkASSERT(!origOuter.isEmpty()); |
| 1439 | SkTCopyOnFirstWrite<SkRRect> outer(origOuter); |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 1440 | if (!viewMatrix.isIdentity()) { |
| 1441 | if (!origOuter.transform(viewMatrix, outer.writable())) { |
bsalomon | 8af0523 | 2014-06-03 06:34:58 -0700 | [diff] [blame] | 1442 | return false; |
| 1443 | } |
| 1444 | } |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1445 | GrPrimitiveEdgeType edgeType = applyAA ? kFillAA_GrProcessorEdgeType : |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1446 | kFillBW_GrProcessorEdgeType; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1447 | GrFragmentProcessor* effect = GrRRectEffect::Create(edgeType, *outer); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1448 | if (nullptr == effect) { |
bsalomon | 8af0523 | 2014-06-03 06:34:58 -0700 | [diff] [blame] | 1449 | return false; |
| 1450 | } |
joshualitt | 4421a4c | 2015-07-13 09:36:41 -0700 | [diff] [blame] | 1451 | if (!arfps.isSet()) { |
| 1452 | arfps.set(&pipelineBuilder); |
bsalomon | 8af0523 | 2014-06-03 06:34:58 -0700 | [diff] [blame] | 1453 | } |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 1454 | |
| 1455 | SkMatrix invert; |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 1456 | if (!viewMatrix.invert(&invert)) { |
bsalomon | 8af0523 | 2014-06-03 06:34:58 -0700 | [diff] [blame] | 1457 | return false; |
| 1458 | } |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 1459 | |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 1460 | arfps.addCoverageFragmentProcessor(effect)->unref(); |
bsalomon | 8af0523 | 2014-06-03 06:34:58 -0700 | [diff] [blame] | 1461 | SkRect bounds = outer->getBounds(); |
| 1462 | if (applyAA) { |
| 1463 | bounds.outset(SK_ScalarHalf, SK_ScalarHalf); |
| 1464 | } |
joshualitt | d2b23e0 | 2015-08-21 10:53:34 -0700 | [diff] [blame] | 1465 | target->drawNonAARect(pipelineBuilder, color, SkMatrix::I(), bounds, invert); |
bsalomon | 8af0523 | 2014-06-03 06:34:58 -0700 | [diff] [blame] | 1466 | return true; |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 1467 | } |
| 1468 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1469 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 1470 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 1471 | class RRectCircleRendererBatch : public GrVertexBatch { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1472 | public: |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 1473 | DEFINE_BATCH_CLASS_ID |
| 1474 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1475 | struct Geometry { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1476 | SkMatrix fViewMatrix; |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 1477 | SkRect fDevBounds; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1478 | SkScalar fInnerRadius; |
| 1479 | SkScalar fOuterRadius; |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 1480 | GrColor fColor; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1481 | bool fStroke; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1482 | }; |
| 1483 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 1484 | static GrDrawBatch* Create(const Geometry& geometry) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 1485 | return new RRectCircleRendererBatch(geometry); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1486 | } |
| 1487 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 1488 | const char* name() const override { return "RRectCircleBatch"; } |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1489 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 1490 | void getInvariantOutputColor(GrInitInvariantOutput* out) const override { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1491 | // When this is called on a batch, there is only one geometry bundle |
| 1492 | out->setKnownFourComponents(fGeoData[0].fColor); |
| 1493 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 1494 | void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1495 | out->setUnknownSingleComponent(); |
| 1496 | } |
| 1497 | |
bsalomon | e46f9fe | 2015-08-18 06:05:14 -0700 | [diff] [blame] | 1498 | private: |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 1499 | void initBatchTracker(const GrPipelineOptimizations& opt) override { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1500 | // Handle any color overrides |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 1501 | if (!opt.readsColor()) { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1502 | fGeoData[0].fColor = GrColor_ILLEGAL; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1503 | } |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 1504 | opt.getOverrideColorIfSet(&fGeoData[0].fColor); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1505 | |
| 1506 | // setup batch properties |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 1507 | fBatch.fColorIgnored = !opt.readsColor(); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1508 | fBatch.fColor = fGeoData[0].fColor; |
| 1509 | fBatch.fStroke = fGeoData[0].fStroke; |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 1510 | fBatch.fUsesLocalCoords = opt.readsLocalCoords(); |
| 1511 | fBatch.fCoverageIgnored = !opt.readsCoverage(); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1512 | } |
| 1513 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 1514 | void onPrepareDraws(Target* target) override { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1515 | // reset to device coordinates |
| 1516 | SkMatrix invert; |
| 1517 | if (!this->viewMatrix().invert(&invert)) { |
| 1518 | SkDebugf("Failed to invert\n"); |
| 1519 | return; |
| 1520 | } |
| 1521 | |
| 1522 | // Setup geometry processor |
| 1523 | SkAutoTUnref<GrGeometryProcessor> gp(CircleEdgeEffect::Create(this->color(), |
| 1524 | this->stroke(), |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 1525 | invert, |
| 1526 | this->usesLocalCoords())); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1527 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 1528 | target->initDraw(gp, this->pipeline()); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1529 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1530 | int instanceCount = fGeoData.count(); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1531 | size_t vertexStride = gp->getVertexStride(); |
| 1532 | SkASSERT(vertexStride == sizeof(CircleVertex)); |
| 1533 | |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 1534 | // drop out the middle quad if we're stroked |
| 1535 | int indicesPerInstance = this->stroke() ? kIndicesPerStrokeRRect : kIndicesPerRRect; |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 1536 | SkAutoTUnref<const GrIndexBuffer> indexBuffer( |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 1537 | ref_rrect_index_buffer(this->stroke(), target->resourceProvider())); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1538 | |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 1539 | InstancedHelper helper; |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 1540 | CircleVertex* verts = reinterpret_cast<CircleVertex*>(helper.init(target, |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 1541 | kTriangles_GrPrimitiveType, vertexStride, indexBuffer, kVertsPerRRect, |
| 1542 | indicesPerInstance, instanceCount)); |
| 1543 | if (!verts || !indexBuffer) { |
joshualitt | 4b31de8 | 2015-03-05 14:33:41 -0800 | [diff] [blame] | 1544 | SkDebugf("Could not allocate vertices\n"); |
| 1545 | return; |
| 1546 | } |
| 1547 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1548 | for (int i = 0; i < instanceCount; i++) { |
| 1549 | Geometry& args = fGeoData[i]; |
| 1550 | |
| 1551 | SkScalar outerRadius = args.fOuterRadius; |
| 1552 | |
egdaniel | bc22714 | 2015-04-21 06:28:08 -0700 | [diff] [blame] | 1553 | const SkRect& bounds = args.fDevBounds; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1554 | |
| 1555 | SkScalar yCoords[4] = { |
| 1556 | bounds.fTop, |
| 1557 | bounds.fTop + outerRadius, |
| 1558 | bounds.fBottom - outerRadius, |
| 1559 | bounds.fBottom |
| 1560 | }; |
| 1561 | |
| 1562 | SkScalar yOuterRadii[4] = {-1, 0, 0, 1 }; |
| 1563 | // The inner radius in the vertex data must be specified in normalized space. |
| 1564 | SkScalar innerRadius = args.fInnerRadius / args.fOuterRadius; |
| 1565 | for (int i = 0; i < 4; ++i) { |
| 1566 | verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]); |
| 1567 | verts->fOffset = SkPoint::Make(-1, yOuterRadii[i]); |
| 1568 | verts->fOuterRadius = outerRadius; |
| 1569 | verts->fInnerRadius = innerRadius; |
| 1570 | verts++; |
| 1571 | |
| 1572 | verts->fPos = SkPoint::Make(bounds.fLeft + outerRadius, yCoords[i]); |
| 1573 | verts->fOffset = SkPoint::Make(0, yOuterRadii[i]); |
| 1574 | verts->fOuterRadius = outerRadius; |
| 1575 | verts->fInnerRadius = innerRadius; |
| 1576 | verts++; |
| 1577 | |
| 1578 | verts->fPos = SkPoint::Make(bounds.fRight - outerRadius, yCoords[i]); |
| 1579 | verts->fOffset = SkPoint::Make(0, yOuterRadii[i]); |
| 1580 | verts->fOuterRadius = outerRadius; |
| 1581 | verts->fInnerRadius = innerRadius; |
| 1582 | verts++; |
| 1583 | |
| 1584 | verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]); |
| 1585 | verts->fOffset = SkPoint::Make(1, yOuterRadii[i]); |
| 1586 | verts->fOuterRadius = outerRadius; |
| 1587 | verts->fInnerRadius = innerRadius; |
| 1588 | verts++; |
| 1589 | } |
| 1590 | } |
| 1591 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 1592 | helper.recordDraw(target); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1593 | } |
| 1594 | |
| 1595 | SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; } |
| 1596 | |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 1597 | RRectCircleRendererBatch(const Geometry& geometry) : INHERITED(ClassID()) { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1598 | fGeoData.push_back(geometry); |
joshualitt | 99c7c07 | 2015-05-01 13:43:30 -0700 | [diff] [blame] | 1599 | |
| 1600 | this->setBounds(geometry.fDevBounds); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1601 | } |
| 1602 | |
bsalomon | cb02b38 | 2015-08-12 11:14:50 -0700 | [diff] [blame] | 1603 | bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override { |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 1604 | RRectCircleRendererBatch* that = t->cast<RRectCircleRendererBatch>(); |
| 1605 | if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(), |
| 1606 | that->bounds(), caps)) { |
joshualitt | 8cab9a7 | 2015-07-16 09:13:50 -0700 | [diff] [blame] | 1607 | return false; |
| 1608 | } |
| 1609 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1610 | // TODO use vertex color to avoid breaking batches |
| 1611 | if (this->color() != that->color()) { |
| 1612 | return false; |
| 1613 | } |
| 1614 | |
| 1615 | if (this->stroke() != that->stroke()) { |
| 1616 | return false; |
| 1617 | } |
| 1618 | |
| 1619 | SkASSERT(this->usesLocalCoords() == that->usesLocalCoords()); |
| 1620 | if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) { |
| 1621 | return false; |
| 1622 | } |
| 1623 | |
| 1624 | fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin()); |
joshualitt | 99c7c07 | 2015-05-01 13:43:30 -0700 | [diff] [blame] | 1625 | this->joinBounds(that->bounds()); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1626 | return true; |
| 1627 | } |
| 1628 | |
| 1629 | GrColor color() const { return fBatch.fColor; } |
| 1630 | bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; } |
| 1631 | const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; } |
| 1632 | bool stroke() const { return fBatch.fStroke; } |
| 1633 | |
| 1634 | struct BatchTracker { |
| 1635 | GrColor fColor; |
| 1636 | bool fStroke; |
| 1637 | bool fUsesLocalCoords; |
| 1638 | bool fColorIgnored; |
| 1639 | bool fCoverageIgnored; |
| 1640 | }; |
| 1641 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1642 | BatchTracker fBatch; |
| 1643 | SkSTArray<1, Geometry, true> fGeoData; |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 1644 | |
| 1645 | typedef GrVertexBatch INHERITED; |
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 | class RRectEllipseRendererBatch : public GrVertexBatch { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1649 | public: |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 1650 | DEFINE_BATCH_CLASS_ID |
| 1651 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1652 | struct Geometry { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1653 | SkMatrix fViewMatrix; |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 1654 | SkRect fDevBounds; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1655 | SkScalar fXRadius; |
| 1656 | SkScalar fYRadius; |
| 1657 | SkScalar fInnerXRadius; |
| 1658 | SkScalar fInnerYRadius; |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 1659 | GrColor fColor; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1660 | bool fStroke; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1661 | }; |
| 1662 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 1663 | static GrDrawBatch* Create(const Geometry& geometry) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 1664 | return new RRectEllipseRendererBatch(geometry); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1665 | } |
| 1666 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 1667 | const char* name() const override { return "RRectEllipseRendererBatch"; } |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1668 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 1669 | void getInvariantOutputColor(GrInitInvariantOutput* out) const override { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1670 | // When this is called on a batch, there is only one geometry bundle |
| 1671 | out->setKnownFourComponents(fGeoData[0].fColor); |
| 1672 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 1673 | void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1674 | out->setUnknownSingleComponent(); |
| 1675 | } |
| 1676 | |
bsalomon | e46f9fe | 2015-08-18 06:05:14 -0700 | [diff] [blame] | 1677 | private: |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 1678 | void initBatchTracker(const GrPipelineOptimizations& opt) override { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1679 | // Handle any color overrides |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 1680 | if (!opt.readsColor()) { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1681 | fGeoData[0].fColor = GrColor_ILLEGAL; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1682 | } |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 1683 | opt.getOverrideColorIfSet(&fGeoData[0].fColor); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1684 | |
| 1685 | // setup batch properties |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 1686 | fBatch.fColorIgnored = !opt.readsColor(); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1687 | fBatch.fColor = fGeoData[0].fColor; |
| 1688 | fBatch.fStroke = fGeoData[0].fStroke; |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 1689 | fBatch.fUsesLocalCoords = opt.readsLocalCoords(); |
| 1690 | fBatch.fCoverageIgnored = !opt.readsCoverage(); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1691 | } |
| 1692 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 1693 | void onPrepareDraws(Target* target) override { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1694 | // reset to device coordinates |
| 1695 | SkMatrix invert; |
| 1696 | if (!this->viewMatrix().invert(&invert)) { |
| 1697 | SkDebugf("Failed to invert\n"); |
| 1698 | return; |
| 1699 | } |
| 1700 | |
| 1701 | // Setup geometry processor |
| 1702 | SkAutoTUnref<GrGeometryProcessor> gp(EllipseEdgeEffect::Create(this->color(), |
| 1703 | this->stroke(), |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 1704 | invert, |
| 1705 | this->usesLocalCoords())); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1706 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 1707 | target->initDraw(gp, this->pipeline()); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1708 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1709 | int instanceCount = fGeoData.count(); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1710 | size_t vertexStride = gp->getVertexStride(); |
| 1711 | SkASSERT(vertexStride == sizeof(EllipseVertex)); |
| 1712 | |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 1713 | // drop out the middle quad if we're stroked |
| 1714 | int indicesPerInstance = this->stroke() ? kIndicesPerStrokeRRect : kIndicesPerRRect; |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 1715 | SkAutoTUnref<const GrIndexBuffer> indexBuffer( |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 1716 | ref_rrect_index_buffer(this->stroke(), target->resourceProvider())); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1717 | |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 1718 | InstancedHelper helper; |
| 1719 | EllipseVertex* verts = reinterpret_cast<EllipseVertex*>( |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 1720 | helper.init(target, kTriangles_GrPrimitiveType, vertexStride, indexBuffer, |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 1721 | kVertsPerRRect, indicesPerInstance, instanceCount)); |
| 1722 | if (!verts || !indexBuffer) { |
joshualitt | 4b31de8 | 2015-03-05 14:33:41 -0800 | [diff] [blame] | 1723 | SkDebugf("Could not allocate vertices\n"); |
| 1724 | return; |
| 1725 | } |
| 1726 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1727 | for (int i = 0; i < instanceCount; i++) { |
| 1728 | Geometry& args = fGeoData[i]; |
| 1729 | |
| 1730 | // Compute the reciprocals of the radii here to save time in the shader |
| 1731 | SkScalar xRadRecip = SkScalarInvert(args.fXRadius); |
| 1732 | SkScalar yRadRecip = SkScalarInvert(args.fYRadius); |
| 1733 | SkScalar xInnerRadRecip = SkScalarInvert(args.fInnerXRadius); |
| 1734 | SkScalar yInnerRadRecip = SkScalarInvert(args.fInnerYRadius); |
| 1735 | |
| 1736 | // Extend the radii out half a pixel to antialias. |
| 1737 | SkScalar xOuterRadius = args.fXRadius + SK_ScalarHalf; |
| 1738 | SkScalar yOuterRadius = args.fYRadius + SK_ScalarHalf; |
| 1739 | |
egdaniel | bc22714 | 2015-04-21 06:28:08 -0700 | [diff] [blame] | 1740 | const SkRect& bounds = args.fDevBounds; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1741 | |
| 1742 | SkScalar yCoords[4] = { |
| 1743 | bounds.fTop, |
| 1744 | bounds.fTop + yOuterRadius, |
| 1745 | bounds.fBottom - yOuterRadius, |
| 1746 | bounds.fBottom |
| 1747 | }; |
| 1748 | SkScalar yOuterOffsets[4] = { |
| 1749 | yOuterRadius, |
| 1750 | SK_ScalarNearlyZero, // we're using inversesqrt() in shader, so can't be exactly 0 |
| 1751 | SK_ScalarNearlyZero, |
| 1752 | yOuterRadius |
| 1753 | }; |
| 1754 | |
| 1755 | for (int i = 0; i < 4; ++i) { |
| 1756 | verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]); |
| 1757 | verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]); |
| 1758 | verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); |
| 1759 | verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); |
| 1760 | verts++; |
| 1761 | |
| 1762 | verts->fPos = SkPoint::Make(bounds.fLeft + xOuterRadius, yCoords[i]); |
| 1763 | verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]); |
| 1764 | verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); |
| 1765 | verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); |
| 1766 | verts++; |
| 1767 | |
| 1768 | verts->fPos = SkPoint::Make(bounds.fRight - xOuterRadius, yCoords[i]); |
| 1769 | verts->fOffset = SkPoint::Make(SK_ScalarNearlyZero, yOuterOffsets[i]); |
| 1770 | verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); |
| 1771 | verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); |
| 1772 | verts++; |
| 1773 | |
| 1774 | verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]); |
| 1775 | verts->fOffset = SkPoint::Make(xOuterRadius, yOuterOffsets[i]); |
| 1776 | verts->fOuterRadii = SkPoint::Make(xRadRecip, yRadRecip); |
| 1777 | verts->fInnerRadii = SkPoint::Make(xInnerRadRecip, yInnerRadRecip); |
| 1778 | verts++; |
| 1779 | } |
| 1780 | } |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 1781 | helper.recordDraw(target); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1782 | } |
| 1783 | |
| 1784 | SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; } |
| 1785 | |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 1786 | RRectEllipseRendererBatch(const Geometry& geometry) : INHERITED(ClassID()) { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1787 | fGeoData.push_back(geometry); |
joshualitt | 99c7c07 | 2015-05-01 13:43:30 -0700 | [diff] [blame] | 1788 | |
| 1789 | this->setBounds(geometry.fDevBounds); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1790 | } |
| 1791 | |
bsalomon | cb02b38 | 2015-08-12 11:14:50 -0700 | [diff] [blame] | 1792 | bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override { |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 1793 | RRectEllipseRendererBatch* that = t->cast<RRectEllipseRendererBatch>(); |
| 1794 | |
| 1795 | if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(), |
| 1796 | that->bounds(), caps)) { |
joshualitt | 8cab9a7 | 2015-07-16 09:13:50 -0700 | [diff] [blame] | 1797 | return false; |
| 1798 | } |
| 1799 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1800 | // TODO use vertex color to avoid breaking batches |
| 1801 | if (this->color() != that->color()) { |
| 1802 | return false; |
| 1803 | } |
| 1804 | |
| 1805 | if (this->stroke() != that->stroke()) { |
| 1806 | return false; |
| 1807 | } |
| 1808 | |
| 1809 | SkASSERT(this->usesLocalCoords() == that->usesLocalCoords()); |
| 1810 | if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) { |
| 1811 | return false; |
| 1812 | } |
| 1813 | |
| 1814 | fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin()); |
joshualitt | 99c7c07 | 2015-05-01 13:43:30 -0700 | [diff] [blame] | 1815 | this->joinBounds(that->bounds()); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1816 | return true; |
| 1817 | } |
| 1818 | |
| 1819 | GrColor color() const { return fBatch.fColor; } |
| 1820 | bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; } |
| 1821 | const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; } |
| 1822 | bool stroke() const { return fBatch.fStroke; } |
| 1823 | |
| 1824 | struct BatchTracker { |
| 1825 | GrColor fColor; |
| 1826 | bool fStroke; |
| 1827 | bool fUsesLocalCoords; |
| 1828 | bool fColorIgnored; |
| 1829 | bool fCoverageIgnored; |
| 1830 | }; |
| 1831 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1832 | BatchTracker fBatch; |
| 1833 | SkSTArray<1, Geometry, true> fGeoData; |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 1834 | |
| 1835 | typedef GrVertexBatch INHERITED; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1836 | }; |
| 1837 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 1838 | static GrDrawBatch* create_rrect_batch(GrColor color, |
| 1839 | const SkMatrix& viewMatrix, |
| 1840 | const SkRRect& rrect, |
| 1841 | const SkStrokeRec& stroke) { |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 1842 | SkASSERT(viewMatrix.rectStaysRect()); |
| 1843 | SkASSERT(rrect.isSimple()); |
| 1844 | SkASSERT(!rrect.isOval()); |
commit-bot@chromium.org | e0a868c | 2013-11-22 07:02:11 +0000 | [diff] [blame] | 1845 | |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 1846 | // RRect batchs only handle simple, but not too simple, rrects |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1847 | // do any matrix crunching before we reset the draw state for device coords |
| 1848 | const SkRect& rrectBounds = rrect.getBounds(); |
joshualitt | d96a67b | 2015-05-05 14:09:05 -0700 | [diff] [blame] | 1849 | SkRect bounds; |
| 1850 | viewMatrix.mapRect(&bounds, rrectBounds); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1851 | |
| 1852 | SkVector radii = rrect.getSimpleRadii(); |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 1853 | SkScalar xRadius = SkScalarAbs(viewMatrix[SkMatrix::kMScaleX]*radii.fX + |
| 1854 | viewMatrix[SkMatrix::kMSkewY]*radii.fY); |
| 1855 | SkScalar yRadius = SkScalarAbs(viewMatrix[SkMatrix::kMSkewX]*radii.fX + |
| 1856 | viewMatrix[SkMatrix::kMScaleY]*radii.fY); |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1857 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1858 | SkStrokeRec::Style style = stroke.getStyle(); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1859 | |
| 1860 | // do (potentially) anisotropic mapping of stroke |
| 1861 | SkVector scaledStroke; |
| 1862 | SkScalar strokeWidth = stroke.getWidth(); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1863 | |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 1864 | bool isStrokeOnly = SkStrokeRec::kStroke_Style == style || |
| 1865 | SkStrokeRec::kHairline_Style == style; |
| 1866 | bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == style; |
| 1867 | |
| 1868 | if (hasStroke) { |
| 1869 | if (SkStrokeRec::kHairline_Style == style) { |
| 1870 | scaledStroke.set(1, 1); |
| 1871 | } else { |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 1872 | scaledStroke.fX = SkScalarAbs(strokeWidth*(viewMatrix[SkMatrix::kMScaleX] + |
| 1873 | viewMatrix[SkMatrix::kMSkewY])); |
| 1874 | scaledStroke.fY = SkScalarAbs(strokeWidth*(viewMatrix[SkMatrix::kMSkewX] + |
| 1875 | viewMatrix[SkMatrix::kMScaleY])); |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 1876 | } |
| 1877 | |
| 1878 | // if half of strokewidth is greater than radius, we don't handle that right now |
| 1879 | if (SK_ScalarHalf*scaledStroke.fX > xRadius || SK_ScalarHalf*scaledStroke.fY > yRadius) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1880 | return nullptr; |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 1881 | } |
| 1882 | } |
| 1883 | |
| 1884 | // The way the effect interpolates the offset-to-ellipse/circle-center attribute only works on |
| 1885 | // the interior of the rrect if the radii are >= 0.5. Otherwise, the inner rect of the nine- |
| 1886 | // patch will have fractional coverage. This only matters when the interior is actually filled. |
| 1887 | // We could consider falling back to rect rendering here, since a tiny radius is |
| 1888 | // indistinguishable from a square corner. |
| 1889 | if (!isStrokeOnly && (SK_ScalarHalf > xRadius || SK_ScalarHalf > yRadius)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1890 | return nullptr; |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1891 | } |
| 1892 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1893 | // if the corners are circles, use the circle renderer |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 1894 | if ((!hasStroke || scaledStroke.fX == scaledStroke.fY) && xRadius == yRadius) { |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1895 | SkScalar innerRadius = 0.0f; |
| 1896 | SkScalar outerRadius = xRadius; |
| 1897 | SkScalar halfWidth = 0; |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 1898 | if (hasStroke) { |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1899 | if (SkScalarNearlyZero(scaledStroke.fX)) { |
| 1900 | halfWidth = SK_ScalarHalf; |
| 1901 | } else { |
| 1902 | halfWidth = SkScalarHalf(scaledStroke.fX); |
| 1903 | } |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 1904 | |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 1905 | if (isStrokeOnly) { |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1906 | innerRadius = xRadius - halfWidth; |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1907 | } |
| 1908 | outerRadius += halfWidth; |
joshualitt | d96a67b | 2015-05-05 14:09:05 -0700 | [diff] [blame] | 1909 | bounds.outset(halfWidth, halfWidth); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1910 | } |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 1911 | |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 1912 | isStrokeOnly = (isStrokeOnly && innerRadius >= 0); |
commit-bot@chromium.org | cefde6e | 2013-08-30 16:34:52 +0000 | [diff] [blame] | 1913 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1914 | // 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] | 1915 | // simpler computation because the computed alpha is zero, rather than 50%, at the radius. |
| 1916 | // Second, the outer radius is used to compute the verts of the bounding box that is |
| 1917 | // rendered and the outset ensures the box will cover all partially covered by the rrect |
| 1918 | // corners. |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1919 | outerRadius += SK_ScalarHalf; |
| 1920 | innerRadius -= SK_ScalarHalf; |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 1921 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1922 | // Expand the rect so all the pixels will be captured. |
joshualitt | d96a67b | 2015-05-05 14:09:05 -0700 | [diff] [blame] | 1923 | bounds.outset(SK_ScalarHalf, SK_ScalarHalf); |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 1924 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1925 | RRectCircleRendererBatch::Geometry geometry; |
| 1926 | geometry.fViewMatrix = viewMatrix; |
| 1927 | geometry.fColor = color; |
| 1928 | geometry.fInnerRadius = innerRadius; |
| 1929 | geometry.fOuterRadius = outerRadius; |
| 1930 | geometry.fStroke = isStrokeOnly; |
joshualitt | d96a67b | 2015-05-05 14:09:05 -0700 | [diff] [blame] | 1931 | geometry.fDevBounds = bounds; |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 1932 | |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 1933 | return RRectCircleRendererBatch::Create(geometry); |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1934 | // otherwise we use the ellipse renderer |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1935 | } else { |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1936 | SkScalar innerXRadius = 0.0f; |
| 1937 | SkScalar innerYRadius = 0.0f; |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 1938 | if (hasStroke) { |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1939 | if (SkScalarNearlyZero(scaledStroke.length())) { |
| 1940 | scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf); |
| 1941 | } else { |
| 1942 | scaledStroke.scale(SK_ScalarHalf); |
| 1943 | } |
| 1944 | |
| 1945 | // we only handle thick strokes for near-circular ellipses |
skia.committer@gmail.com | 8be02fc | 2013-05-17 07:01:11 +0000 | [diff] [blame] | 1946 | if (scaledStroke.length() > SK_ScalarHalf && |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1947 | (SK_ScalarHalf*xRadius > yRadius || SK_ScalarHalf*yRadius > xRadius)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1948 | return nullptr; |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1949 | } |
| 1950 | |
| 1951 | // we don't handle it if curvature of the stroke is less than curvature of the ellipse |
| 1952 | if (scaledStroke.fX*(yRadius*yRadius) < (scaledStroke.fY*scaledStroke.fY)*xRadius || |
| 1953 | scaledStroke.fY*(xRadius*xRadius) < (scaledStroke.fX*scaledStroke.fX)*yRadius) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1954 | return nullptr; |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1955 | } |
| 1956 | |
| 1957 | // 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] | 1958 | if (isStrokeOnly) { |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1959 | innerXRadius = xRadius - scaledStroke.fX; |
| 1960 | innerYRadius = yRadius - scaledStroke.fY; |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1961 | } |
| 1962 | |
| 1963 | xRadius += scaledStroke.fX; |
| 1964 | yRadius += scaledStroke.fY; |
joshualitt | d96a67b | 2015-05-05 14:09:05 -0700 | [diff] [blame] | 1965 | bounds.outset(scaledStroke.fX, scaledStroke.fY); |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 1966 | } |
jvanverth@google.com | e364741 | 2013-05-08 15:31:43 +0000 | [diff] [blame] | 1967 | |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 1968 | isStrokeOnly = (isStrokeOnly && innerXRadius >= 0 && innerYRadius >= 0); |
commit-bot@chromium.org | cefde6e | 2013-08-30 16:34:52 +0000 | [diff] [blame] | 1969 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1970 | // Expand the rect so all the pixels will be captured. |
joshualitt | d96a67b | 2015-05-05 14:09:05 -0700 | [diff] [blame] | 1971 | bounds.outset(SK_ScalarHalf, SK_ScalarHalf); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1972 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1973 | RRectEllipseRendererBatch::Geometry geometry; |
| 1974 | geometry.fViewMatrix = viewMatrix; |
| 1975 | geometry.fColor = color; |
| 1976 | geometry.fXRadius = xRadius; |
| 1977 | geometry.fYRadius = yRadius; |
| 1978 | geometry.fInnerXRadius = innerXRadius; |
| 1979 | geometry.fInnerYRadius = innerYRadius; |
| 1980 | geometry.fStroke = isStrokeOnly; |
joshualitt | d96a67b | 2015-05-05 14:09:05 -0700 | [diff] [blame] | 1981 | geometry.fDevBounds = bounds; |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1982 | |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 1983 | return RRectEllipseRendererBatch::Create(geometry); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1984 | } |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 1985 | } |
| 1986 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1987 | bool GrOvalRenderer::DrawRRect(GrDrawTarget* target, |
joshualitt | ae3d63a | 2015-07-13 08:44:06 -0700 | [diff] [blame] | 1988 | const GrPipelineBuilder& pipelineBuilder, |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 1989 | GrColor color, |
| 1990 | const SkMatrix& viewMatrix, |
| 1991 | bool useAA, |
| 1992 | const SkRRect& rrect, |
| 1993 | const SkStrokeRec& stroke) { |
| 1994 | if (rrect.isOval()) { |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1995 | return DrawOval(target, pipelineBuilder, color, viewMatrix, useAA, rrect.getBounds(), |
| 1996 | stroke); |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 1997 | } |
| 1998 | |
joshualitt | ae3d63a | 2015-07-13 08:44:06 -0700 | [diff] [blame] | 1999 | bool useCoverageAA = useAA && !pipelineBuilder.getRenderTarget()->isUnifiedMultisampled(); |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 2000 | |
| 2001 | // only anti-aliased rrects for now |
| 2002 | if (!useCoverageAA) { |
| 2003 | return false; |
| 2004 | } |
| 2005 | |
| 2006 | if (!viewMatrix.rectStaysRect() || !rrect.isSimple()) { |
| 2007 | return false; |
| 2008 | } |
| 2009 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 2010 | SkAutoTUnref<GrDrawBatch> batch(create_rrect_batch(color, viewMatrix, rrect, stroke)); |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 2011 | if (!batch) { |
| 2012 | return false; |
| 2013 | } |
| 2014 | |
joshualitt | ae3d63a | 2015-07-13 08:44:06 -0700 | [diff] [blame] | 2015 | target->drawBatch(pipelineBuilder, batch); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 2016 | return true; |
| 2017 | } |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 2018 | |
| 2019 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2020 | |
| 2021 | #ifdef GR_TEST_UTILS |
| 2022 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 2023 | DRAW_BATCH_TEST_DEFINE(CircleBatch) { |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 2024 | SkMatrix viewMatrix = GrTest::TestMatrix(random); |
| 2025 | GrColor color = GrRandomColor(random); |
| 2026 | bool useCoverageAA = random->nextBool(); |
joshualitt | 6c89110 | 2015-05-13 08:51:49 -0700 | [diff] [blame] | 2027 | SkRect circle = GrTest::TestSquare(random); |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 2028 | return create_circle_batch(color, viewMatrix, useCoverageAA, circle, |
| 2029 | GrTest::TestStrokeRec(random)); |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 2030 | } |
| 2031 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 2032 | DRAW_BATCH_TEST_DEFINE(EllipseBatch) { |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 2033 | SkMatrix viewMatrix = GrTest::TestMatrixRectStaysRect(random); |
| 2034 | GrColor color = GrRandomColor(random); |
joshualitt | 6c89110 | 2015-05-13 08:51:49 -0700 | [diff] [blame] | 2035 | SkRect ellipse = GrTest::TestSquare(random); |
| 2036 | return create_ellipse_batch(color, viewMatrix, true, ellipse, |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 2037 | GrTest::TestStrokeRec(random)); |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 2038 | } |
| 2039 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 2040 | DRAW_BATCH_TEST_DEFINE(DIEllipseBatch) { |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 2041 | SkMatrix viewMatrix = GrTest::TestMatrix(random); |
| 2042 | GrColor color = GrRandomColor(random); |
| 2043 | bool useCoverageAA = random->nextBool(); |
joshualitt | 6c89110 | 2015-05-13 08:51:49 -0700 | [diff] [blame] | 2044 | SkRect ellipse = GrTest::TestSquare(random); |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 2045 | return create_diellipse_batch(color, viewMatrix, useCoverageAA, ellipse, |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 2046 | GrTest::TestStrokeRec(random)); |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 2047 | } |
| 2048 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 2049 | DRAW_BATCH_TEST_DEFINE(RRectBatch) { |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 2050 | SkMatrix viewMatrix = GrTest::TestMatrixRectStaysRect(random); |
| 2051 | GrColor color = GrRandomColor(random); |
| 2052 | const SkRRect& rrect = GrTest::TestRRectSimple(random); |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 2053 | return create_rrect_batch(color, viewMatrix, rrect, GrTest::TestStrokeRec(random)); |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 2054 | } |
| 2055 | |
| 2056 | #endif |