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