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