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