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