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