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