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