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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkStrokeRec.h" |
| 9 | #include "src/core/SkRRectPriv.h" |
| 10 | #include "src/gpu/GrCaps.h" |
| 11 | #include "src/gpu/GrDrawOpTest.h" |
| 12 | #include "src/gpu/GrGeometryProcessor.h" |
| 13 | #include "src/gpu/GrOpFlushState.h" |
| 14 | #include "src/gpu/GrProcessor.h" |
| 15 | #include "src/gpu/GrResourceProvider.h" |
| 16 | #include "src/gpu/GrShaderCaps.h" |
| 17 | #include "src/gpu/GrStyle.h" |
| 18 | #include "src/gpu/GrVertexWriter.h" |
| 19 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
| 20 | #include "src/gpu/glsl/GrGLSLGeometryProcessor.h" |
| 21 | #include "src/gpu/glsl/GrGLSLProgramDataManager.h" |
| 22 | #include "src/gpu/glsl/GrGLSLUniformHandler.h" |
| 23 | #include "src/gpu/glsl/GrGLSLUtil.h" |
| 24 | #include "src/gpu/glsl/GrGLSLVarying.h" |
| 25 | #include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h" |
| 26 | #include "src/gpu/ops/GrMeshDrawOp.h" |
| 27 | #include "src/gpu/ops/GrOvalOpFactory.h" |
| 28 | #include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h" |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 29 | |
Ben Wagner | f08d1d0 | 2018-06-18 15:11:00 -0400 | [diff] [blame] | 30 | #include <utility> |
| 31 | |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 32 | namespace { |
brianosman | bb2ff94 | 2016-02-11 14:15:18 -0800 | [diff] [blame] | 33 | |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 34 | static inline bool circle_stays_circle(const SkMatrix& m) { return m.isSimilarity(); } |
Brian Osman | e3caf2d | 2018-11-21 13:48:36 -0500 | [diff] [blame] | 35 | |
Brian Osman | 2b6e390 | 2018-11-21 15:29:43 -0500 | [diff] [blame] | 36 | // Produces TriStrip vertex data for an origin-centered rectangle from [-x, -y] to [x, y] |
| 37 | static inline GrVertexWriter::TriStrip<float> origin_centered_tri_strip(float x, float y) { |
| 38 | return GrVertexWriter::TriStrip<float>{ -x, -y, x, y }; |
| 39 | }; |
| 40 | |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 41 | } |
| 42 | |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 43 | /////////////////////////////////////////////////////////////////////////////// |
| 44 | |
| 45 | /** |
bsalomon | ce1c886 | 2014-12-15 07:11:22 -0800 | [diff] [blame] | 46 | * The output of this effect is a modulation of the input color and coverage for a circle. It |
| 47 | * 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] | 48 | * with origin at the circle center. Three vertex attributes are used: |
bsalomon | ce1c886 | 2014-12-15 07:11:22 -0800 | [diff] [blame] | 49 | * vec2f : position in device space of the bounding geometry vertices |
bsalomon | cdaa97b | 2016-03-08 08:30:14 -0800 | [diff] [blame] | 50 | * vec4ub: color |
bsalomon | ce1c886 | 2014-12-15 07:11:22 -0800 | [diff] [blame] | 51 | * vec4f : (p.xy, outerRad, innerRad) |
| 52 | * p is the position in the normalized space. |
| 53 | * outerRad is the outerRadius in device space. |
| 54 | * innerRad is the innerRadius in normalized space (ignored if not stroking). |
bsalomon | 4f3a0ca | 2016-08-22 13:14:26 -0700 | [diff] [blame] | 55 | * Additional clip planes are supported for rendering circular arcs. The additional planes are |
| 56 | * either intersected or unioned together. Up to three planes are supported (an initial plane, |
| 57 | * a plane intersected with the initial plane, and a plane unioned with the first two). Only two |
Brian Salomon | 53e4c3c | 2016-12-21 11:38:53 -0500 | [diff] [blame] | 58 | * are useful for any given arc, but having all three in one instance allows combining different |
bsalomon | 4f3a0ca | 2016-08-22 13:14:26 -0700 | [diff] [blame] | 59 | * types of arcs. |
Brian Salomon | 45c9220 | 2018-04-10 10:53:58 -0400 | [diff] [blame] | 60 | * Round caps for stroking are allowed as well. The caps are specified as two circle center points |
| 61 | * in the same space as p.xy. |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 62 | */ |
| 63 | |
bsalomon | cdaa97b | 2016-03-08 08:30:14 -0800 | [diff] [blame] | 64 | class CircleGeometryProcessor : public GrGeometryProcessor { |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 65 | public: |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 66 | CircleGeometryProcessor(bool stroke, bool clipPlane, bool isectPlane, bool unionPlane, |
Brian Osman | e3caf2d | 2018-11-21 13:48:36 -0500 | [diff] [blame] | 67 | bool roundCaps, bool wideColor, const SkMatrix& localMatrix) |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 68 | : INHERITED(kCircleGeometryProcessor_ClassID) |
Brian Salomon | 45c9220 | 2018-04-10 10:53:58 -0400 | [diff] [blame] | 69 | , fLocalMatrix(localMatrix) |
| 70 | , fStroke(stroke) { |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 71 | fInPosition = {"inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType}; |
Brian Osman | c906d25 | 2018-12-04 11:17:46 -0500 | [diff] [blame] | 72 | fInColor = MakeColorAttribute("inColor", wideColor); |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 73 | fInCircleEdge = {"inCircleEdge", kFloat4_GrVertexAttribType, kFloat4_GrSLType}; |
| 74 | |
bsalomon | 4f3a0ca | 2016-08-22 13:14:26 -0700 | [diff] [blame] | 75 | if (clipPlane) { |
Brian Osman | d4c2970 | 2018-09-14 16:16:55 -0400 | [diff] [blame] | 76 | fInClipPlane = {"inClipPlane", kFloat3_GrVertexAttribType, kHalf3_GrSLType}; |
bsalomon | 4f3a0ca | 2016-08-22 13:14:26 -0700 | [diff] [blame] | 77 | } |
| 78 | if (isectPlane) { |
Brian Osman | d4c2970 | 2018-09-14 16:16:55 -0400 | [diff] [blame] | 79 | fInIsectPlane = {"inIsectPlane", kFloat3_GrVertexAttribType, kHalf3_GrSLType}; |
bsalomon | 4f3a0ca | 2016-08-22 13:14:26 -0700 | [diff] [blame] | 80 | } |
| 81 | if (unionPlane) { |
Brian Osman | d4c2970 | 2018-09-14 16:16:55 -0400 | [diff] [blame] | 82 | fInUnionPlane = {"inUnionPlane", kFloat3_GrVertexAttribType, kHalf3_GrSLType}; |
bsalomon | 4f3a0ca | 2016-08-22 13:14:26 -0700 | [diff] [blame] | 83 | } |
Brian Salomon | 45c9220 | 2018-04-10 10:53:58 -0400 | [diff] [blame] | 84 | if (roundCaps) { |
| 85 | SkASSERT(stroke); |
| 86 | SkASSERT(clipPlane); |
Brian Osman | d4c2970 | 2018-09-14 16:16:55 -0400 | [diff] [blame] | 87 | fInRoundCapCenters = |
| 88 | {"inRoundCapCenters", kFloat4_GrVertexAttribType, kFloat4_GrSLType}; |
Brian Salomon | 45c9220 | 2018-04-10 10:53:58 -0400 | [diff] [blame] | 89 | } |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 90 | this->setVertexAttributes(&fInPosition, 7); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Brian Salomon | d3b6597 | 2017-03-22 12:05:03 -0400 | [diff] [blame] | 93 | ~CircleGeometryProcessor() override {} |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 94 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 95 | const char* name() const override { return "CircleEdge"; } |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 96 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 97 | void getGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override { |
bsalomon | 31df31c | 2016-08-17 09:00:24 -0700 | [diff] [blame] | 98 | GLSLProcessor::GenKey(*this, caps, b); |
| 99 | } |
| 100 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 101 | GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override { |
bsalomon | 31df31c | 2016-08-17 09:00:24 -0700 | [diff] [blame] | 102 | return new GLSLProcessor(); |
| 103 | } |
| 104 | |
| 105 | private: |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 106 | class GLSLProcessor : public GrGLSLGeometryProcessor { |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 107 | public: |
brianosman | bb2ff94 | 2016-02-11 14:15:18 -0800 | [diff] [blame] | 108 | GLSLProcessor() {} |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 109 | |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 110 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override { |
bsalomon | cdaa97b | 2016-03-08 08:30:14 -0800 | [diff] [blame] | 111 | const CircleGeometryProcessor& cgp = args.fGP.cast<CircleGeometryProcessor>(); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 112 | GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder; |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 113 | GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler; |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 114 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
Chris Dalton | 6028361 | 2018-02-14 13:38:14 -0700 | [diff] [blame] | 115 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 116 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 117 | // emit attributes |
bsalomon | cdaa97b | 2016-03-08 08:30:14 -0800 | [diff] [blame] | 118 | varyingHandler->emitAttributes(cgp); |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 119 | fragBuilder->codeAppend("float4 circleEdge;"); |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 120 | varyingHandler->addPassThroughAttribute(cgp.fInCircleEdge, "circleEdge"); |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 121 | if (cgp.fInClipPlane.isInitialized()) { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 122 | fragBuilder->codeAppend("half3 clipPlane;"); |
bsalomon | 4f3a0ca | 2016-08-22 13:14:26 -0700 | [diff] [blame] | 123 | varyingHandler->addPassThroughAttribute(cgp.fInClipPlane, "clipPlane"); |
| 124 | } |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 125 | if (cgp.fInIsectPlane.isInitialized()) { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 126 | fragBuilder->codeAppend("half3 isectPlane;"); |
bsalomon | 4f3a0ca | 2016-08-22 13:14:26 -0700 | [diff] [blame] | 127 | varyingHandler->addPassThroughAttribute(cgp.fInIsectPlane, "isectPlane"); |
| 128 | } |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 129 | if (cgp.fInUnionPlane.isInitialized()) { |
| 130 | SkASSERT(cgp.fInClipPlane.isInitialized()); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 131 | fragBuilder->codeAppend("half3 unionPlane;"); |
bsalomon | 4f3a0ca | 2016-08-22 13:14:26 -0700 | [diff] [blame] | 132 | varyingHandler->addPassThroughAttribute(cgp.fInUnionPlane, "unionPlane"); |
| 133 | } |
Brian Salomon | 45c9220 | 2018-04-10 10:53:58 -0400 | [diff] [blame] | 134 | GrGLSLVarying capRadius(kFloat_GrSLType); |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 135 | if (cgp.fInRoundCapCenters.isInitialized()) { |
Brian Salomon | 45c9220 | 2018-04-10 10:53:58 -0400 | [diff] [blame] | 136 | fragBuilder->codeAppend("float4 roundCapCenters;"); |
| 137 | varyingHandler->addPassThroughAttribute(cgp.fInRoundCapCenters, "roundCapCenters"); |
| 138 | varyingHandler->addVarying("capRadius", &capRadius, |
| 139 | GrGLSLVaryingHandler::Interpolation::kCanBeFlat); |
| 140 | // This is the cap radius in normalized space where the outer radius is 1 and |
| 141 | // circledEdge.w is the normalized inner radius. |
| 142 | vertBuilder->codeAppendf("%s = (1.0 - %s.w) / 2.0;", capRadius.vsOut(), |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 143 | cgp.fInCircleEdge.name()); |
Brian Salomon | 45c9220 | 2018-04-10 10:53:58 -0400 | [diff] [blame] | 144 | } |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 145 | |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 146 | // setup pass through color |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 147 | varyingHandler->addPassThroughAttribute(cgp.fInColor, args.fOutputColor); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 148 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 149 | // Setup position |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 150 | this->writeOutputPosition(vertBuilder, gpArgs, cgp.fInPosition.name()); |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 151 | |
| 152 | // emit transforms |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 153 | this->emitTransforms(vertBuilder, |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 154 | varyingHandler, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 155 | uniformHandler, |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 156 | cgp.fInPosition.asShaderVar(), |
bsalomon | 31df31c | 2016-08-17 09:00:24 -0700 | [diff] [blame] | 157 | cgp.fLocalMatrix, |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 158 | args.fFPCoordTransformHandler); |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 159 | |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 160 | fragBuilder->codeAppend("float d = length(circleEdge.xy);"); |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 161 | fragBuilder->codeAppend("half distanceToOuterEdge = half(circleEdge.z * (1.0 - d));"); |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 162 | fragBuilder->codeAppend("half edgeAlpha = saturate(distanceToOuterEdge);"); |
bsalomon | cdaa97b | 2016-03-08 08:30:14 -0800 | [diff] [blame] | 163 | if (cgp.fStroke) { |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 164 | fragBuilder->codeAppend( |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 165 | "half distanceToInnerEdge = half(circleEdge.z * (d - circleEdge.w));"); |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 166 | fragBuilder->codeAppend("half innerAlpha = saturate(distanceToInnerEdge);"); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 167 | fragBuilder->codeAppend("edgeAlpha *= innerAlpha;"); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 168 | } |
commit-bot@chromium.org | 0a6cb60 | 2013-04-11 15:05:37 +0000 | [diff] [blame] | 169 | |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 170 | if (cgp.fInClipPlane.isInitialized()) { |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 171 | fragBuilder->codeAppend( |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 172 | "half clip = half(saturate(circleEdge.z * dot(circleEdge.xy, " |
| 173 | "clipPlane.xy) + clipPlane.z));"); |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 174 | if (cgp.fInIsectPlane.isInitialized()) { |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 175 | fragBuilder->codeAppend( |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 176 | "clip *= half(saturate(circleEdge.z * dot(circleEdge.xy, " |
| 177 | "isectPlane.xy) + isectPlane.z));"); |
bsalomon | 4f3a0ca | 2016-08-22 13:14:26 -0700 | [diff] [blame] | 178 | } |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 179 | if (cgp.fInUnionPlane.isInitialized()) { |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 180 | fragBuilder->codeAppend( |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 181 | "clip = saturate(clip + half(saturate(circleEdge.z * dot(circleEdge.xy," |
| 182 | " unionPlane.xy) + unionPlane.z)));"); |
bsalomon | 4f3a0ca | 2016-08-22 13:14:26 -0700 | [diff] [blame] | 183 | } |
| 184 | fragBuilder->codeAppend("edgeAlpha *= clip;"); |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 185 | if (cgp.fInRoundCapCenters.isInitialized()) { |
Brian Salomon | 33c4422 | 2018-04-19 08:44:21 -0400 | [diff] [blame] | 186 | // We compute coverage of the round caps as circles at the butt caps produced |
| 187 | // by the clip planes. The inverse of the clip planes is applied so that there |
| 188 | // is no double counting. |
Brian Salomon | 45c9220 | 2018-04-10 10:53:58 -0400 | [diff] [blame] | 189 | fragBuilder->codeAppendf( |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 190 | "half dcap1 = half(circleEdge.z * (%s - length(circleEdge.xy - " |
| 191 | " roundCapCenters.xy)));" |
| 192 | "half dcap2 = half(circleEdge.z * (%s - length(circleEdge.xy - " |
| 193 | " roundCapCenters.zw)));" |
Brian Salomon | 33c4422 | 2018-04-19 08:44:21 -0400 | [diff] [blame] | 194 | "half capAlpha = (1 - clip) * (max(dcap1, 0) + max(dcap2, 0));" |
Brian Salomon | 45c9220 | 2018-04-10 10:53:58 -0400 | [diff] [blame] | 195 | "edgeAlpha = min(edgeAlpha + capAlpha, 1.0);", |
| 196 | capRadius.fsIn(), capRadius.fsIn()); |
| 197 | } |
bsalomon | 4f3a0ca | 2016-08-22 13:14:26 -0700 | [diff] [blame] | 198 | } |
Brian Salomon | ea26d6b | 2018-01-23 20:33:21 +0000 | [diff] [blame] | 199 | fragBuilder->codeAppendf("%s = half4(edgeAlpha);", args.fOutputCoverage); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 200 | } |
| 201 | |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 202 | static void GenKey(const GrGeometryProcessor& gp, |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 203 | const GrShaderCaps&, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 204 | GrProcessorKeyBuilder* b) { |
bsalomon | cdaa97b | 2016-03-08 08:30:14 -0800 | [diff] [blame] | 205 | const CircleGeometryProcessor& cgp = gp.cast<CircleGeometryProcessor>(); |
bsalomon | 4f3a0ca | 2016-08-22 13:14:26 -0700 | [diff] [blame] | 206 | uint16_t key; |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 207 | key = cgp.fStroke ? 0x01 : 0x0; |
bsalomon | 4f3a0ca | 2016-08-22 13:14:26 -0700 | [diff] [blame] | 208 | key |= cgp.fLocalMatrix.hasPerspective() ? 0x02 : 0x0; |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 209 | key |= cgp.fInClipPlane.isInitialized() ? 0x04 : 0x0; |
| 210 | key |= cgp.fInIsectPlane.isInitialized() ? 0x08 : 0x0; |
| 211 | key |= cgp.fInUnionPlane.isInitialized() ? 0x10 : 0x0; |
| 212 | key |= cgp.fInRoundCapCenters.isInitialized() ? 0x20 : 0x0; |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 213 | b->add32(key); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 214 | } |
| 215 | |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 216 | void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& primProc, |
| 217 | FPCoordTransformIter&& transformIter) override { |
bsalomon | e4f2461 | 2016-08-17 10:30:17 -0700 | [diff] [blame] | 218 | this->setTransformDataHelper(primProc.cast<CircleGeometryProcessor>().fLocalMatrix, |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 219 | pdman, &transformIter); |
joshualitt | e3ababe | 2015-05-15 07:56:07 -0700 | [diff] [blame] | 220 | } |
| 221 | |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 222 | private: |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 223 | typedef GrGLSLGeometryProcessor INHERITED; |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 224 | }; |
| 225 | |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 226 | SkMatrix fLocalMatrix; |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 227 | |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 228 | Attribute fInPosition; |
| 229 | Attribute fInColor; |
| 230 | Attribute fInCircleEdge; |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 231 | // Optional attributes. |
| 232 | Attribute fInClipPlane; |
| 233 | Attribute fInIsectPlane; |
| 234 | Attribute fInUnionPlane; |
| 235 | Attribute fInRoundCapCenters; |
| 236 | |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 237 | bool fStroke; |
Brian Salomon | 0c26a9d | 2017-07-06 10:09:38 -0400 | [diff] [blame] | 238 | GR_DECLARE_GEOMETRY_PROCESSOR_TEST |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 239 | |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 240 | typedef GrGeometryProcessor INHERITED; |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 241 | }; |
| 242 | |
bsalomon | cdaa97b | 2016-03-08 08:30:14 -0800 | [diff] [blame] | 243 | GR_DEFINE_GEOMETRY_PROCESSOR_TEST(CircleGeometryProcessor); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 244 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 245 | #if GR_TEST_UTILS |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 246 | sk_sp<GrGeometryProcessor> CircleGeometryProcessor::TestCreate(GrProcessorTestData* d) { |
Brian Salomon | 45c9220 | 2018-04-10 10:53:58 -0400 | [diff] [blame] | 247 | bool stroke = d->fRandom->nextBool(); |
| 248 | bool roundCaps = stroke ? d->fRandom->nextBool() : false; |
Brian Osman | e3caf2d | 2018-11-21 13:48:36 -0500 | [diff] [blame] | 249 | bool wideColor = d->fRandom->nextBool(); |
Brian Salomon | 45c9220 | 2018-04-10 10:53:58 -0400 | [diff] [blame] | 250 | bool clipPlane = d->fRandom->nextBool(); |
| 251 | bool isectPlane = d->fRandom->nextBool(); |
| 252 | bool unionPlane = d->fRandom->nextBool(); |
| 253 | const SkMatrix& matrix = GrTest::TestMatrix(d->fRandom); |
Brian Osman | e3caf2d | 2018-11-21 13:48:36 -0500 | [diff] [blame] | 254 | return sk_sp<GrGeometryProcessor>(new CircleGeometryProcessor( |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 255 | stroke, clipPlane, isectPlane, unionPlane, roundCaps, wideColor, matrix)); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 256 | } |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 257 | #endif |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 258 | |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 259 | class ButtCapDashedCircleGeometryProcessor : public GrGeometryProcessor { |
| 260 | public: |
Brian Osman | e3caf2d | 2018-11-21 13:48:36 -0500 | [diff] [blame] | 261 | ButtCapDashedCircleGeometryProcessor(bool wideColor, const SkMatrix& localMatrix) |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 262 | : INHERITED(kButtCapStrokedCircleGeometryProcessor_ClassID), fLocalMatrix(localMatrix) { |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 263 | fInPosition = {"inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType}; |
Brian Osman | c906d25 | 2018-12-04 11:17:46 -0500 | [diff] [blame] | 264 | fInColor = MakeColorAttribute("inColor", wideColor); |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 265 | fInCircleEdge = {"inCircleEdge", kFloat4_GrVertexAttribType, kFloat4_GrSLType}; |
| 266 | fInDashParams = {"inDashParams", kFloat4_GrVertexAttribType, kFloat4_GrSLType}; |
| 267 | this->setVertexAttributes(&fInPosition, 4); |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | ~ButtCapDashedCircleGeometryProcessor() override {} |
| 271 | |
| 272 | const char* name() const override { return "ButtCapDashedCircleGeometryProcessor"; } |
| 273 | |
| 274 | void getGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override { |
| 275 | GLSLProcessor::GenKey(*this, caps, b); |
| 276 | } |
| 277 | |
| 278 | GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override { |
| 279 | return new GLSLProcessor(); |
| 280 | } |
| 281 | |
| 282 | private: |
| 283 | class GLSLProcessor : public GrGLSLGeometryProcessor { |
| 284 | public: |
| 285 | GLSLProcessor() {} |
| 286 | |
| 287 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override { |
| 288 | const ButtCapDashedCircleGeometryProcessor& bcscgp = |
| 289 | args.fGP.cast<ButtCapDashedCircleGeometryProcessor>(); |
| 290 | GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder; |
| 291 | GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler; |
| 292 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
| 293 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 294 | |
| 295 | // emit attributes |
| 296 | varyingHandler->emitAttributes(bcscgp); |
| 297 | fragBuilder->codeAppend("float4 circleEdge;"); |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 298 | varyingHandler->addPassThroughAttribute(bcscgp.fInCircleEdge, "circleEdge"); |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 299 | |
| 300 | fragBuilder->codeAppend("float4 dashParams;"); |
| 301 | varyingHandler->addPassThroughAttribute( |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 302 | bcscgp.fInDashParams, "dashParams", |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 303 | GrGLSLVaryingHandler::Interpolation::kCanBeFlat); |
| 304 | GrGLSLVarying wrapDashes(kHalf4_GrSLType); |
| 305 | varyingHandler->addVarying("wrapDashes", &wrapDashes, |
| 306 | GrGLSLVaryingHandler::Interpolation::kCanBeFlat); |
| 307 | GrGLSLVarying lastIntervalLength(kHalf_GrSLType); |
| 308 | varyingHandler->addVarying("lastIntervalLength", &lastIntervalLength, |
| 309 | GrGLSLVaryingHandler::Interpolation::kCanBeFlat); |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 310 | vertBuilder->codeAppendf("float4 dashParams = %s;", bcscgp.fInDashParams.name()); |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 311 | // Our fragment shader works in on/off intervals as specified by dashParams.xy: |
| 312 | // x = length of on interval, y = length of on + off. |
| 313 | // There are two other parameters in dashParams.zw: |
| 314 | // z = start angle in radians, w = phase offset in radians in range -y/2..y/2. |
| 315 | // Each interval has a "corresponding" dash which may be shifted partially or |
| 316 | // fully out of its interval by the phase. So there may be up to two "visual" |
| 317 | // dashes in an interval. |
| 318 | // When computing coverage in an interval we look at three dashes. These are the |
| 319 | // "corresponding" dashes from the current, previous, and next intervals. Any of these |
| 320 | // may be phase shifted into our interval or even when phase=0 they may be within half a |
| 321 | // pixel distance of a pixel center in the interval. |
| 322 | // When in the first interval we need to check the dash from the last interval. And |
| 323 | // similarly when in the last interval we need to check the dash from the first |
| 324 | // interval. When 2pi is not perfectly divisible dashParams.y this is a boundary case. |
| 325 | // We compute the dash begin/end angles in the vertex shader and apply them in the |
| 326 | // fragment shader when we detect we're in the first/last interval. |
| 327 | vertBuilder->codeAppend(R"( |
| 328 | // The two boundary dash intervals are stored in wrapDashes.xy and .zw and fed |
| 329 | // to the fragment shader as a varying. |
| 330 | float4 wrapDashes; |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 331 | half lastIntervalLength = mod(6.28318530718, half(dashParams.y)); |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 332 | // We can happen to be perfectly divisible. |
| 333 | if (0 == lastIntervalLength) { |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 334 | lastIntervalLength = half(dashParams.y); |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 335 | } |
| 336 | // Let 'l' be the last interval before reaching 2 pi. |
| 337 | // Based on the phase determine whether (l-1)th, l-th, or (l+1)th interval's |
| 338 | // "corresponding" dash appears in the l-th interval and is closest to the 0-th |
| 339 | // interval. |
| 340 | half offset = 0; |
| 341 | if (-dashParams.w >= lastIntervalLength) { |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 342 | offset = half(-dashParams.y); |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 343 | } else if (dashParams.w > dashParams.y - lastIntervalLength) { |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 344 | offset = half(dashParams.y); |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 345 | } |
| 346 | wrapDashes.x = -lastIntervalLength + offset - dashParams.w; |
| 347 | // The end of this dash may be beyond the 2 pi and therefore clipped. Hence the |
| 348 | // min. |
| 349 | wrapDashes.y = min(wrapDashes.x + dashParams.x, 0); |
| 350 | |
| 351 | // Based on the phase determine whether the -1st, 0th, or 1st interval's |
| 352 | // "corresponding" dash appears in the 0th interval and is closest to l. |
| 353 | offset = 0; |
| 354 | if (dashParams.w >= dashParams.x) { |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 355 | offset = half(dashParams.y); |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 356 | } else if (-dashParams.w > dashParams.y - dashParams.x) { |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 357 | offset = half(-dashParams.y); |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 358 | } |
| 359 | wrapDashes.z = lastIntervalLength + offset - dashParams.w; |
| 360 | wrapDashes.w = wrapDashes.z + dashParams.x; |
| 361 | // The start of the dash we're considering may be clipped by the start of the |
| 362 | // circle. |
| 363 | wrapDashes.z = max(wrapDashes.z, lastIntervalLength); |
| 364 | )"); |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 365 | vertBuilder->codeAppendf("%s = half4(wrapDashes);", wrapDashes.vsOut()); |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 366 | vertBuilder->codeAppendf("%s = lastIntervalLength;", lastIntervalLength.vsOut()); |
| 367 | fragBuilder->codeAppendf("half4 wrapDashes = %s;", wrapDashes.fsIn()); |
| 368 | fragBuilder->codeAppendf("half lastIntervalLength = %s;", lastIntervalLength.fsIn()); |
| 369 | |
| 370 | // setup pass through color |
| 371 | varyingHandler->addPassThroughAttribute( |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 372 | bcscgp.fInColor, args.fOutputColor, |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 373 | GrGLSLVaryingHandler::Interpolation::kCanBeFlat); |
| 374 | |
| 375 | // Setup position |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 376 | this->writeOutputPosition(vertBuilder, gpArgs, bcscgp.fInPosition.name()); |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 377 | |
| 378 | // emit transforms |
| 379 | this->emitTransforms(vertBuilder, |
| 380 | varyingHandler, |
| 381 | uniformHandler, |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 382 | bcscgp.fInPosition.asShaderVar(), |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 383 | bcscgp.fLocalMatrix, |
| 384 | args.fFPCoordTransformHandler); |
| 385 | GrShaderVar fnArgs[] = { |
| 386 | GrShaderVar("angleToEdge", kFloat_GrSLType), |
| 387 | GrShaderVar("diameter", kFloat_GrSLType), |
| 388 | }; |
| 389 | SkString fnName; |
| 390 | fragBuilder->emitFunction(kFloat_GrSLType, "coverage_from_dash_edge", |
| 391 | SK_ARRAY_COUNT(fnArgs), fnArgs, R"( |
| 392 | float linearDist; |
| 393 | angleToEdge = clamp(angleToEdge, -3.1415, 3.1415); |
| 394 | linearDist = diameter * sin(angleToEdge / 2); |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 395 | return saturate(linearDist + 0.5); |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 396 | )", |
| 397 | &fnName); |
| 398 | fragBuilder->codeAppend(R"( |
| 399 | float d = length(circleEdge.xy) * circleEdge.z; |
| 400 | |
| 401 | // Compute coverage from outer/inner edges of the stroke. |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 402 | half distanceToOuterEdge = half(circleEdge.z - d); |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 403 | half edgeAlpha = saturate(distanceToOuterEdge); |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 404 | half distanceToInnerEdge = half(d - circleEdge.z * circleEdge.w); |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 405 | half innerAlpha = saturate(distanceToInnerEdge); |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 406 | edgeAlpha *= innerAlpha; |
| 407 | |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 408 | half angleFromStart = half(atan(circleEdge.y, circleEdge.x) - dashParams.z); |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 409 | angleFromStart = mod(angleFromStart, 6.28318530718); |
| 410 | float x = mod(angleFromStart, dashParams.y); |
| 411 | // Convert the radial distance from center to pixel into a diameter. |
| 412 | d *= 2; |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 413 | half2 currDash = half2(half(-dashParams.w), half(dashParams.x) - |
| 414 | half(dashParams.w)); |
| 415 | half2 nextDash = half2(half(dashParams.y) - half(dashParams.w), |
| 416 | half(dashParams.y) + half(dashParams.x) - |
| 417 | half(dashParams.w)); |
| 418 | half2 prevDash = half2(half(-dashParams.y) - half(dashParams.w), |
| 419 | half(-dashParams.y) + half(dashParams.x) - |
| 420 | half(dashParams.w)); |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 421 | half dashAlpha = 0; |
| 422 | )"); |
| 423 | fragBuilder->codeAppendf(R"( |
| 424 | if (angleFromStart - x + dashParams.y >= 6.28318530718) { |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 425 | dashAlpha += half(%s(x - wrapDashes.z, d) * %s(wrapDashes.w - x, d)); |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 426 | currDash.y = min(currDash.y, lastIntervalLength); |
| 427 | if (nextDash.x >= lastIntervalLength) { |
| 428 | // The next dash is outside the 0..2pi range, throw it away |
| 429 | nextDash.xy = half2(1000); |
| 430 | } else { |
| 431 | // Clip the end of the next dash to the end of the circle |
| 432 | nextDash.y = min(nextDash.y, lastIntervalLength); |
| 433 | } |
| 434 | } |
| 435 | )", fnName.c_str(), fnName.c_str()); |
| 436 | fragBuilder->codeAppendf(R"( |
| 437 | if (angleFromStart - x - dashParams.y < -0.01) { |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 438 | dashAlpha += half(%s(x - wrapDashes.x, d) * %s(wrapDashes.y - x, d)); |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 439 | currDash.x = max(currDash.x, 0); |
| 440 | if (prevDash.y <= 0) { |
| 441 | // The previous dash is outside the 0..2pi range, throw it away |
| 442 | prevDash.xy = half2(1000); |
| 443 | } else { |
| 444 | // Clip the start previous dash to the start of the circle |
| 445 | prevDash.x = max(prevDash.x, 0); |
| 446 | } |
| 447 | } |
| 448 | )", fnName.c_str(), fnName.c_str()); |
| 449 | fragBuilder->codeAppendf(R"( |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 450 | dashAlpha += half(%s(x - currDash.x, d) * %s(currDash.y - x, d)); |
| 451 | dashAlpha += half(%s(x - nextDash.x, d) * %s(nextDash.y - x, d)); |
| 452 | dashAlpha += half(%s(x - prevDash.x, d) * %s(prevDash.y - x, d)); |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 453 | dashAlpha = min(dashAlpha, 1); |
| 454 | edgeAlpha *= dashAlpha; |
| 455 | )", fnName.c_str(), fnName.c_str(), fnName.c_str(), fnName.c_str(), fnName.c_str(), |
| 456 | fnName.c_str()); |
| 457 | fragBuilder->codeAppendf("%s = half4(edgeAlpha);", args.fOutputCoverage); |
| 458 | } |
| 459 | |
| 460 | static void GenKey(const GrGeometryProcessor& gp, |
| 461 | const GrShaderCaps&, |
| 462 | GrProcessorKeyBuilder* b) { |
| 463 | const ButtCapDashedCircleGeometryProcessor& bcscgp = |
| 464 | gp.cast<ButtCapDashedCircleGeometryProcessor>(); |
| 465 | b->add32(bcscgp.fLocalMatrix.hasPerspective()); |
| 466 | } |
| 467 | |
| 468 | void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& primProc, |
| 469 | FPCoordTransformIter&& transformIter) override { |
| 470 | this->setTransformDataHelper( |
| 471 | primProc.cast<ButtCapDashedCircleGeometryProcessor>().fLocalMatrix, pdman, |
| 472 | &transformIter); |
| 473 | } |
| 474 | |
| 475 | private: |
| 476 | typedef GrGLSLGeometryProcessor INHERITED; |
| 477 | }; |
| 478 | |
| 479 | SkMatrix fLocalMatrix; |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 480 | Attribute fInPosition; |
| 481 | Attribute fInColor; |
| 482 | Attribute fInCircleEdge; |
| 483 | Attribute fInDashParams; |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 484 | |
| 485 | GR_DECLARE_GEOMETRY_PROCESSOR_TEST |
| 486 | |
| 487 | typedef GrGeometryProcessor INHERITED; |
| 488 | }; |
| 489 | |
| 490 | #if GR_TEST_UTILS |
| 491 | sk_sp<GrGeometryProcessor> ButtCapDashedCircleGeometryProcessor::TestCreate(GrProcessorTestData* d) { |
Brian Osman | e3caf2d | 2018-11-21 13:48:36 -0500 | [diff] [blame] | 492 | bool wideColor = d->fRandom->nextBool(); |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 493 | const SkMatrix& matrix = GrTest::TestMatrix(d->fRandom); |
Brian Osman | e3caf2d | 2018-11-21 13:48:36 -0500 | [diff] [blame] | 494 | return sk_sp<GrGeometryProcessor>(new ButtCapDashedCircleGeometryProcessor(wideColor, matrix)); |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 495 | } |
| 496 | #endif |
| 497 | |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 498 | /////////////////////////////////////////////////////////////////////////////// |
| 499 | |
| 500 | /** |
| 501 | * 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] | 502 | * ellipse, specified as a 2D offset from center, and the reciprocals of the outer and inner radii, |
| 503 | * in both x and y directions. |
| 504 | * |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 505 | * 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] | 506 | */ |
| 507 | |
bsalomon | cdaa97b | 2016-03-08 08:30:14 -0800 | [diff] [blame] | 508 | class EllipseGeometryProcessor : public GrGeometryProcessor { |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 509 | public: |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 510 | EllipseGeometryProcessor(bool stroke, bool wideColor, bool useScale, |
Jim Van Verth | 20ae25c | 2019-03-29 08:50:41 -0400 | [diff] [blame] | 511 | const SkMatrix& localMatrix) |
Brian Salomon | ea26d6b | 2018-01-23 20:33:21 +0000 | [diff] [blame] | 512 | : INHERITED(kEllipseGeometryProcessor_ClassID) |
Jim Van Verth | 20ae25c | 2019-03-29 08:50:41 -0400 | [diff] [blame] | 513 | , fLocalMatrix(localMatrix) |
| 514 | , fStroke(stroke) |
| 515 | , fUseScale(useScale) { |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 516 | fInPosition = {"inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType}; |
Brian Osman | c906d25 | 2018-12-04 11:17:46 -0500 | [diff] [blame] | 517 | fInColor = MakeColorAttribute("inColor", wideColor); |
Jim Van Verth | 20ae25c | 2019-03-29 08:50:41 -0400 | [diff] [blame] | 518 | if (useScale) { |
| 519 | fInEllipseOffset = {"inEllipseOffset", kFloat3_GrVertexAttribType, kFloat3_GrSLType}; |
| 520 | } else { |
| 521 | fInEllipseOffset = {"inEllipseOffset", kFloat2_GrVertexAttribType, kFloat2_GrSLType}; |
| 522 | } |
| 523 | fInEllipseRadii = {"inEllipseRadii", kFloat4_GrVertexAttribType, kFloat4_GrSLType}; |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 524 | this->setVertexAttributes(&fInPosition, 4); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 525 | } |
| 526 | |
Brian Salomon | d3b6597 | 2017-03-22 12:05:03 -0400 | [diff] [blame] | 527 | ~EllipseGeometryProcessor() override {} |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 528 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 529 | const char* name() const override { return "EllipseEdge"; } |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 530 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 531 | void getGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override { |
bsalomon | 31df31c | 2016-08-17 09:00:24 -0700 | [diff] [blame] | 532 | GLSLProcessor::GenKey(*this, caps, b); |
| 533 | } |
| 534 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 535 | GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override { |
bsalomon | 31df31c | 2016-08-17 09:00:24 -0700 | [diff] [blame] | 536 | return new GLSLProcessor(); |
| 537 | } |
| 538 | |
| 539 | private: |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 540 | class GLSLProcessor : public GrGLSLGeometryProcessor { |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 541 | public: |
brianosman | bb2ff94 | 2016-02-11 14:15:18 -0800 | [diff] [blame] | 542 | GLSLProcessor() {} |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 543 | |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 544 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override { |
bsalomon | cdaa97b | 2016-03-08 08:30:14 -0800 | [diff] [blame] | 545 | const EllipseGeometryProcessor& egp = args.fGP.cast<EllipseGeometryProcessor>(); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 546 | GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder; |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 547 | GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler; |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 548 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 549 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 550 | // emit attributes |
bsalomon | cdaa97b | 2016-03-08 08:30:14 -0800 | [diff] [blame] | 551 | varyingHandler->emitAttributes(egp); |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 552 | |
Jim Van Verth | 20ae25c | 2019-03-29 08:50:41 -0400 | [diff] [blame] | 553 | GrSLType offsetType = egp.fUseScale ? kFloat3_GrSLType : kFloat2_GrSLType; |
| 554 | GrGLSLVarying ellipseOffsets(offsetType); |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 555 | varyingHandler->addVarying("EllipseOffsets", &ellipseOffsets); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 556 | vertBuilder->codeAppendf("%s = %s;", ellipseOffsets.vsOut(), |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 557 | egp.fInEllipseOffset.name()); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 558 | |
Jim Van Verth | 20ae25c | 2019-03-29 08:50:41 -0400 | [diff] [blame] | 559 | GrGLSLVarying ellipseRadii(kFloat4_GrSLType); |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 560 | varyingHandler->addVarying("EllipseRadii", &ellipseRadii); |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 561 | vertBuilder->codeAppendf("%s = %s;", ellipseRadii.vsOut(), egp.fInEllipseRadii.name()); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 562 | |
Chris Dalton | 6028361 | 2018-02-14 13:38:14 -0700 | [diff] [blame] | 563 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 564 | // setup pass through color |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 565 | varyingHandler->addPassThroughAttribute(egp.fInColor, args.fOutputColor); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 566 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 567 | // Setup position |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 568 | this->writeOutputPosition(vertBuilder, gpArgs, egp.fInPosition.name()); |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 569 | |
| 570 | // emit transforms |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 571 | this->emitTransforms(vertBuilder, |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 572 | varyingHandler, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 573 | uniformHandler, |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 574 | egp.fInPosition.asShaderVar(), |
bsalomon | 31df31c | 2016-08-17 09:00:24 -0700 | [diff] [blame] | 575 | egp.fLocalMatrix, |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 576 | args.fFPCoordTransformHandler); |
Jim Van Verth | b1b87d9 | 2018-06-28 10:46:05 -0400 | [diff] [blame] | 577 | // For stroked ellipses, we use the full ellipse equation (x^2/a^2 + y^2/b^2 = 1) |
| 578 | // to compute both the edges because we need two separate test equations for |
| 579 | // the single offset. |
| 580 | // For filled ellipses we can use a unit circle equation (x^2 + y^2 = 1), and warp |
| 581 | // the distance by the gradient, non-uniformly scaled by the inverse of the |
| 582 | // ellipse size. |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 583 | |
Jim Van Verth | 20ae25c | 2019-03-29 08:50:41 -0400 | [diff] [blame] | 584 | // On medium precision devices, we scale the denominator of the distance equation |
| 585 | // before taking the inverse square root to minimize the chance that we're dividing |
| 586 | // by zero, then we scale the result back. |
| 587 | |
skia.committer@gmail.com | 8be02fc | 2013-05-17 07:01:11 +0000 | [diff] [blame] | 588 | // for outer curve |
Jim Van Verth | 20ae25c | 2019-03-29 08:50:41 -0400 | [diff] [blame] | 589 | fragBuilder->codeAppendf("float2 offset = %s.xy;", ellipseOffsets.fsIn()); |
Jim Van Verth | b1b87d9 | 2018-06-28 10:46:05 -0400 | [diff] [blame] | 590 | if (egp.fStroke) { |
| 591 | fragBuilder->codeAppendf("offset *= %s.xy;", ellipseRadii.fsIn()); |
| 592 | } |
Jim Van Verth | 20ae25c | 2019-03-29 08:50:41 -0400 | [diff] [blame] | 593 | fragBuilder->codeAppend("float test = dot(offset, offset) - 1.0;"); |
| 594 | if (egp.fUseScale) { |
| 595 | fragBuilder->codeAppendf("float2 grad = 2.0*offset*(%s.z*%s.xy);", |
| 596 | ellipseOffsets.fsIn(), ellipseRadii.fsIn()); |
| 597 | } else { |
| 598 | fragBuilder->codeAppendf("float2 grad = 2.0*offset*%s.xy;", ellipseRadii.fsIn()); |
| 599 | } |
| 600 | fragBuilder->codeAppend("float grad_dot = dot(grad, grad);"); |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 601 | |
commit-bot@chromium.org | 1b035d8 | 2014-04-09 17:11:09 +0000 | [diff] [blame] | 602 | // avoid calling inversesqrt on zero. |
Jim Van Verth | 20ae25c | 2019-03-29 08:50:41 -0400 | [diff] [blame] | 603 | if (args.fShaderCaps->floatIs32Bits()) { |
| 604 | fragBuilder->codeAppend("grad_dot = max(grad_dot, 1.1755e-38);"); |
| 605 | } else { |
| 606 | fragBuilder->codeAppend("grad_dot = max(grad_dot, 6.1036e-5);"); |
| 607 | } |
| 608 | if (egp.fUseScale) { |
| 609 | fragBuilder->codeAppendf("float invlen = %s.z*inversesqrt(grad_dot);", |
| 610 | ellipseOffsets.fsIn()); |
| 611 | } else { |
| 612 | fragBuilder->codeAppend("float invlen = inversesqrt(grad_dot);"); |
| 613 | } |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 614 | fragBuilder->codeAppend("float edgeAlpha = saturate(0.5-test*invlen);"); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 615 | |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 616 | // for inner curve |
bsalomon | cdaa97b | 2016-03-08 08:30:14 -0800 | [diff] [blame] | 617 | if (egp.fStroke) { |
Jim Van Verth | 20ae25c | 2019-03-29 08:50:41 -0400 | [diff] [blame] | 618 | fragBuilder->codeAppendf("offset = %s.xy*%s.zw;", ellipseOffsets.fsIn(), |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 619 | ellipseRadii.fsIn()); |
Jim Van Verth | b1b87d9 | 2018-06-28 10:46:05 -0400 | [diff] [blame] | 620 | fragBuilder->codeAppend("test = dot(offset, offset) - 1.0;"); |
Jim Van Verth | 20ae25c | 2019-03-29 08:50:41 -0400 | [diff] [blame] | 621 | if (egp.fUseScale) { |
| 622 | fragBuilder->codeAppendf("grad = 2.0*offset*(%s.z*%s.zw);", |
| 623 | ellipseOffsets.fsIn(), ellipseRadii.fsIn()); |
| 624 | } else { |
| 625 | fragBuilder->codeAppendf("grad = 2.0*offset*%s.zw;", ellipseRadii.fsIn()); |
| 626 | } |
| 627 | fragBuilder->codeAppend("grad_dot = dot(grad, grad);"); |
| 628 | if (!args.fShaderCaps->floatIs32Bits()) { |
| 629 | fragBuilder->codeAppend("grad_dot = max(grad_dot, 6.1036e-5);"); |
| 630 | } |
| 631 | if (egp.fUseScale) { |
| 632 | fragBuilder->codeAppendf("invlen = %s.z*inversesqrt(grad_dot);", |
| 633 | ellipseOffsets.fsIn()); |
| 634 | } else { |
| 635 | fragBuilder->codeAppend("invlen = inversesqrt(grad_dot);"); |
| 636 | } |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 637 | fragBuilder->codeAppend("edgeAlpha *= saturate(0.5+test*invlen);"); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 638 | } |
| 639 | |
Jim Van Verth | 20ae25c | 2019-03-29 08:50:41 -0400 | [diff] [blame] | 640 | fragBuilder->codeAppendf("%s = half4(half(edgeAlpha));", args.fOutputCoverage); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 641 | } |
| 642 | |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 643 | static void GenKey(const GrGeometryProcessor& gp, |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 644 | const GrShaderCaps&, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 645 | GrProcessorKeyBuilder* b) { |
bsalomon | cdaa97b | 2016-03-08 08:30:14 -0800 | [diff] [blame] | 646 | const EllipseGeometryProcessor& egp = gp.cast<EllipseGeometryProcessor>(); |
| 647 | uint16_t key = egp.fStroke ? 0x1 : 0x0; |
bsalomon | 31df31c | 2016-08-17 09:00:24 -0700 | [diff] [blame] | 648 | key |= egp.fLocalMatrix.hasPerspective() ? 0x2 : 0x0; |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 649 | b->add32(key); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 650 | } |
| 651 | |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 652 | void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& primProc, |
| 653 | FPCoordTransformIter&& transformIter) override { |
| 654 | const EllipseGeometryProcessor& egp = primProc.cast<EllipseGeometryProcessor>(); |
| 655 | this->setTransformDataHelper(egp.fLocalMatrix, pdman, &transformIter); |
joshualitt | e3ababe | 2015-05-15 07:56:07 -0700 | [diff] [blame] | 656 | } |
| 657 | |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 658 | private: |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 659 | typedef GrGLSLGeometryProcessor INHERITED; |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 660 | }; |
| 661 | |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 662 | Attribute fInPosition; |
| 663 | Attribute fInColor; |
| 664 | Attribute fInEllipseOffset; |
| 665 | Attribute fInEllipseRadii; |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 666 | |
joshualitt | e3ababe | 2015-05-15 07:56:07 -0700 | [diff] [blame] | 667 | SkMatrix fLocalMatrix; |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 668 | bool fStroke; |
Jim Van Verth | 20ae25c | 2019-03-29 08:50:41 -0400 | [diff] [blame] | 669 | bool fUseScale; |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 670 | |
Brian Salomon | 0c26a9d | 2017-07-06 10:09:38 -0400 | [diff] [blame] | 671 | GR_DECLARE_GEOMETRY_PROCESSOR_TEST |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 672 | |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 673 | typedef GrGeometryProcessor INHERITED; |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 674 | }; |
| 675 | |
bsalomon | cdaa97b | 2016-03-08 08:30:14 -0800 | [diff] [blame] | 676 | GR_DEFINE_GEOMETRY_PROCESSOR_TEST(EllipseGeometryProcessor); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 677 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 678 | #if GR_TEST_UTILS |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 679 | sk_sp<GrGeometryProcessor> EllipseGeometryProcessor::TestCreate(GrProcessorTestData* d) { |
Brian Salomon | ea26d6b | 2018-01-23 20:33:21 +0000 | [diff] [blame] | 680 | return sk_sp<GrGeometryProcessor>( |
Brian Osman | e3caf2d | 2018-11-21 13:48:36 -0500 | [diff] [blame] | 681 | new EllipseGeometryProcessor(d->fRandom->nextBool(), d->fRandom->nextBool(), |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 682 | d->fRandom->nextBool(), GrTest::TestMatrix(d->fRandom))); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 683 | } |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 684 | #endif |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 685 | |
| 686 | /////////////////////////////////////////////////////////////////////////////// |
| 687 | |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 688 | /** |
skia.committer@gmail.com | 6fc1b49 | 2013-09-06 07:01:45 +0000 | [diff] [blame] | 689 | * 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] | 690 | * specified as a 2D offset from center for both the outer and inner paths (if stroked). The |
| 691 | * implict equation used is for a unit circle (x^2 + y^2 - 1 = 0) and the edge corrected by |
| 692 | * using differentials. |
| 693 | * |
| 694 | * The result is device-independent and can be used with any affine matrix. |
| 695 | */ |
| 696 | |
bsalomon | cdaa97b | 2016-03-08 08:30:14 -0800 | [diff] [blame] | 697 | enum class DIEllipseStyle { kStroke = 0, kHairline, kFill }; |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 698 | |
bsalomon | cdaa97b | 2016-03-08 08:30:14 -0800 | [diff] [blame] | 699 | class DIEllipseGeometryProcessor : public GrGeometryProcessor { |
| 700 | public: |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 701 | DIEllipseGeometryProcessor(bool wideColor, bool useScale, const SkMatrix& viewMatrix, |
Jim Van Verth | 20ae25c | 2019-03-29 08:50:41 -0400 | [diff] [blame] | 702 | DIEllipseStyle style) |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 703 | : INHERITED(kDIEllipseGeometryProcessor_ClassID) |
Jim Van Verth | 20ae25c | 2019-03-29 08:50:41 -0400 | [diff] [blame] | 704 | , fViewMatrix(viewMatrix) |
| 705 | , fUseScale(useScale) |
| 706 | , fStyle(style) { |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 707 | fInPosition = {"inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType}; |
Brian Osman | c906d25 | 2018-12-04 11:17:46 -0500 | [diff] [blame] | 708 | fInColor = MakeColorAttribute("inColor", wideColor); |
Jim Van Verth | 20ae25c | 2019-03-29 08:50:41 -0400 | [diff] [blame] | 709 | if (useScale) { |
| 710 | fInEllipseOffsets0 = {"inEllipseOffsets0", kFloat3_GrVertexAttribType, |
| 711 | kFloat3_GrSLType}; |
| 712 | } else { |
| 713 | fInEllipseOffsets0 = {"inEllipseOffsets0", kFloat2_GrVertexAttribType, |
| 714 | kFloat2_GrSLType}; |
| 715 | } |
| 716 | fInEllipseOffsets1 = {"inEllipseOffsets1", kFloat2_GrVertexAttribType, kFloat2_GrSLType}; |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 717 | this->setVertexAttributes(&fInPosition, 4); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 718 | } |
| 719 | |
Brian Salomon | d3b6597 | 2017-03-22 12:05:03 -0400 | [diff] [blame] | 720 | ~DIEllipseGeometryProcessor() override {} |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 721 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 722 | const char* name() const override { return "DIEllipseEdge"; } |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 723 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 724 | void getGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override { |
bsalomon | 31df31c | 2016-08-17 09:00:24 -0700 | [diff] [blame] | 725 | GLSLProcessor::GenKey(*this, caps, b); |
| 726 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 727 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 728 | GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override { |
bsalomon | 31df31c | 2016-08-17 09:00:24 -0700 | [diff] [blame] | 729 | return new GLSLProcessor(); |
| 730 | } |
| 731 | |
| 732 | private: |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 733 | class GLSLProcessor : public GrGLSLGeometryProcessor { |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 734 | public: |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 735 | GLSLProcessor() : fViewMatrix(SkMatrix::InvalidMatrix()) {} |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 736 | |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 737 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override { |
bsalomon | cdaa97b | 2016-03-08 08:30:14 -0800 | [diff] [blame] | 738 | const DIEllipseGeometryProcessor& diegp = args.fGP.cast<DIEllipseGeometryProcessor>(); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 739 | GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder; |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 740 | GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler; |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 741 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 742 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 743 | // emit attributes |
bsalomon | cdaa97b | 2016-03-08 08:30:14 -0800 | [diff] [blame] | 744 | varyingHandler->emitAttributes(diegp); |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 745 | |
Jim Van Verth | 20ae25c | 2019-03-29 08:50:41 -0400 | [diff] [blame] | 746 | GrSLType offsetType = (diegp.fUseScale) ? kFloat3_GrSLType : kFloat2_GrSLType; |
| 747 | GrGLSLVarying offsets0(offsetType); |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 748 | varyingHandler->addVarying("EllipseOffsets0", &offsets0); |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 749 | vertBuilder->codeAppendf("%s = %s;", offsets0.vsOut(), diegp.fInEllipseOffsets0.name()); |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 750 | |
Jim Van Verth | 20ae25c | 2019-03-29 08:50:41 -0400 | [diff] [blame] | 751 | GrGLSLVarying offsets1(kFloat2_GrSLType); |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 752 | varyingHandler->addVarying("EllipseOffsets1", &offsets1); |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 753 | vertBuilder->codeAppendf("%s = %s;", offsets1.vsOut(), diegp.fInEllipseOffsets1.name()); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 754 | |
Chris Dalton | 6028361 | 2018-02-14 13:38:14 -0700 | [diff] [blame] | 755 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 756 | varyingHandler->addPassThroughAttribute(diegp.fInColor, args.fOutputColor); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 757 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 758 | // Setup position |
Brian Salomon | 7f23543 | 2017-08-16 09:41:48 -0400 | [diff] [blame] | 759 | this->writeOutputPosition(vertBuilder, |
| 760 | uniformHandler, |
| 761 | gpArgs, |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 762 | diegp.fInPosition.name(), |
Brian Salomon | 7f23543 | 2017-08-16 09:41:48 -0400 | [diff] [blame] | 763 | diegp.fViewMatrix, |
| 764 | &fViewMatrixUniform); |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 765 | |
| 766 | // emit transforms |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 767 | this->emitTransforms(vertBuilder, |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 768 | varyingHandler, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 769 | uniformHandler, |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 770 | diegp.fInPosition.asShaderVar(), |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 771 | args.fFPCoordTransformHandler); |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 772 | |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 773 | // for outer curve |
Jim Van Verth | 20ae25c | 2019-03-29 08:50:41 -0400 | [diff] [blame] | 774 | fragBuilder->codeAppendf("float2 scaledOffset = %s.xy;", offsets0.fsIn()); |
| 775 | fragBuilder->codeAppend("float test = dot(scaledOffset, scaledOffset) - 1.0;"); |
| 776 | fragBuilder->codeAppendf("float2 duvdx = dFdx(%s.xy);", offsets0.fsIn()); |
| 777 | fragBuilder->codeAppendf("float2 duvdy = dFdy(%s.xy);", offsets0.fsIn()); |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 778 | fragBuilder->codeAppendf( |
Jim Van Verth | 20ae25c | 2019-03-29 08:50:41 -0400 | [diff] [blame] | 779 | "float2 grad = float2(%s.x*duvdx.x + %s.y*duvdx.y," |
| 780 | " %s.x*duvdy.x + %s.y*duvdy.y);", |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 781 | offsets0.fsIn(), offsets0.fsIn(), offsets0.fsIn(), offsets0.fsIn()); |
Jim Van Verth | 20ae25c | 2019-03-29 08:50:41 -0400 | [diff] [blame] | 782 | if (diegp.fUseScale) { |
| 783 | fragBuilder->codeAppendf("grad *= %s.z;", offsets0.fsIn()); |
| 784 | } |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 785 | |
Jim Van Verth | 20ae25c | 2019-03-29 08:50:41 -0400 | [diff] [blame] | 786 | fragBuilder->codeAppend("float grad_dot = 4.0*dot(grad, grad);"); |
commit-bot@chromium.org | 1b035d8 | 2014-04-09 17:11:09 +0000 | [diff] [blame] | 787 | // avoid calling inversesqrt on zero. |
Jim Van Verth | 20ae25c | 2019-03-29 08:50:41 -0400 | [diff] [blame] | 788 | if (args.fShaderCaps->floatIs32Bits()) { |
| 789 | fragBuilder->codeAppend("grad_dot = max(grad_dot, 1.1755e-38);"); |
| 790 | } else { |
| 791 | fragBuilder->codeAppend("grad_dot = max(grad_dot, 6.1036e-5);"); |
| 792 | } |
| 793 | fragBuilder->codeAppend("float invlen = inversesqrt(grad_dot);"); |
| 794 | if (diegp.fUseScale) { |
| 795 | fragBuilder->codeAppendf("invlen *= %s.z;", offsets0.fsIn()); |
| 796 | } |
bsalomon | cdaa97b | 2016-03-08 08:30:14 -0800 | [diff] [blame] | 797 | if (DIEllipseStyle::kHairline == diegp.fStyle) { |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 798 | // can probably do this with one step |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 799 | fragBuilder->codeAppend("float edgeAlpha = saturate(1.0-test*invlen);"); |
| 800 | fragBuilder->codeAppend("edgeAlpha *= saturate(1.0+test*invlen);"); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 801 | } else { |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 802 | fragBuilder->codeAppend("float edgeAlpha = saturate(0.5-test*invlen);"); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | // for inner curve |
bsalomon | cdaa97b | 2016-03-08 08:30:14 -0800 | [diff] [blame] | 806 | if (DIEllipseStyle::kStroke == diegp.fStyle) { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 807 | fragBuilder->codeAppendf("scaledOffset = %s.xy;", offsets1.fsIn()); |
| 808 | fragBuilder->codeAppend("test = dot(scaledOffset, scaledOffset) - 1.0;"); |
Jim Van Verth | 20ae25c | 2019-03-29 08:50:41 -0400 | [diff] [blame] | 809 | fragBuilder->codeAppendf("duvdx = float2(dFdx(%s));", offsets1.fsIn()); |
| 810 | fragBuilder->codeAppendf("duvdy = float2(dFdy(%s));", offsets1.fsIn()); |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 811 | fragBuilder->codeAppendf( |
Jim Van Verth | 20ae25c | 2019-03-29 08:50:41 -0400 | [diff] [blame] | 812 | "grad = float2(%s.x*duvdx.x + %s.y*duvdx.y," |
| 813 | " %s.x*duvdy.x + %s.y*duvdy.y);", |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 814 | offsets1.fsIn(), offsets1.fsIn(), offsets1.fsIn(), offsets1.fsIn()); |
Jim Van Verth | 20ae25c | 2019-03-29 08:50:41 -0400 | [diff] [blame] | 815 | if (diegp.fUseScale) { |
| 816 | fragBuilder->codeAppendf("grad *= %s.z;", offsets0.fsIn()); |
| 817 | } |
| 818 | fragBuilder->codeAppend("grad_dot = 4.0*dot(grad, grad);"); |
| 819 | if (!args.fShaderCaps->floatIs32Bits()) { |
| 820 | fragBuilder->codeAppend("grad_dot = max(grad_dot, 6.1036e-5);"); |
| 821 | } |
| 822 | fragBuilder->codeAppend("invlen = inversesqrt(grad_dot);"); |
| 823 | if (diegp.fUseScale) { |
| 824 | fragBuilder->codeAppendf("invlen *= %s.z;", offsets0.fsIn()); |
| 825 | } |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 826 | fragBuilder->codeAppend("edgeAlpha *= saturate(0.5+test*invlen);"); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 827 | } |
Brian Salomon | ea26d6b | 2018-01-23 20:33:21 +0000 | [diff] [blame] | 828 | |
Jim Van Verth | 20ae25c | 2019-03-29 08:50:41 -0400 | [diff] [blame] | 829 | fragBuilder->codeAppendf("%s = half4(half(edgeAlpha));", args.fOutputCoverage); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 830 | } |
| 831 | |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 832 | static void GenKey(const GrGeometryProcessor& gp, |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 833 | const GrShaderCaps&, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 834 | GrProcessorKeyBuilder* b) { |
bsalomon | cdaa97b | 2016-03-08 08:30:14 -0800 | [diff] [blame] | 835 | const DIEllipseGeometryProcessor& diegp = gp.cast<DIEllipseGeometryProcessor>(); |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 836 | uint16_t key = static_cast<uint16_t>(diegp.fStyle); |
| 837 | key |= ComputePosKey(diegp.fViewMatrix) << 10; |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 838 | b->add32(key); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 839 | } |
| 840 | |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 841 | void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& gp, |
| 842 | FPCoordTransformIter&& transformIter) override { |
bsalomon | cdaa97b | 2016-03-08 08:30:14 -0800 | [diff] [blame] | 843 | const DIEllipseGeometryProcessor& diegp = gp.cast<DIEllipseGeometryProcessor>(); |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 844 | |
bsalomon | 31df31c | 2016-08-17 09:00:24 -0700 | [diff] [blame] | 845 | if (!diegp.fViewMatrix.isIdentity() && !fViewMatrix.cheapEqualTo(diegp.fViewMatrix)) { |
| 846 | fViewMatrix = diegp.fViewMatrix; |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 847 | float viewMatrix[3 * 3]; |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 848 | GrGLSLGetMatrix<3>(viewMatrix, fViewMatrix); |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 849 | pdman.setMatrix3f(fViewMatrixUniform, viewMatrix); |
| 850 | } |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 851 | this->setTransformDataHelper(SkMatrix::I(), pdman, &transformIter); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | private: |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 855 | SkMatrix fViewMatrix; |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 856 | UniformHandle fViewMatrixUniform; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 857 | |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 858 | typedef GrGLSLGeometryProcessor INHERITED; |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 859 | }; |
| 860 | |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 861 | |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 862 | Attribute fInPosition; |
| 863 | Attribute fInColor; |
| 864 | Attribute fInEllipseOffsets0; |
| 865 | Attribute fInEllipseOffsets1; |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 866 | |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 867 | SkMatrix fViewMatrix; |
Jim Van Verth | 20ae25c | 2019-03-29 08:50:41 -0400 | [diff] [blame] | 868 | bool fUseScale; |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 869 | DIEllipseStyle fStyle; |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 870 | |
Brian Salomon | 0c26a9d | 2017-07-06 10:09:38 -0400 | [diff] [blame] | 871 | GR_DECLARE_GEOMETRY_PROCESSOR_TEST |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 872 | |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 873 | typedef GrGeometryProcessor INHERITED; |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 874 | }; |
| 875 | |
bsalomon | cdaa97b | 2016-03-08 08:30:14 -0800 | [diff] [blame] | 876 | GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DIEllipseGeometryProcessor); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 877 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 878 | #if GR_TEST_UTILS |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 879 | sk_sp<GrGeometryProcessor> DIEllipseGeometryProcessor::TestCreate(GrProcessorTestData* d) { |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 880 | return sk_sp<GrGeometryProcessor>(new DIEllipseGeometryProcessor( |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 881 | d->fRandom->nextBool(), d->fRandom->nextBool(), GrTest::TestMatrix(d->fRandom), |
| 882 | (DIEllipseStyle)(d->fRandom->nextRangeU(0, 2)))); |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 883 | } |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 884 | #endif |
commit-bot@chromium.org | 5242ed7 | 2013-09-05 19:26:51 +0000 | [diff] [blame] | 885 | |
| 886 | /////////////////////////////////////////////////////////////////////////////// |
| 887 | |
jvanverth | 6ca4882 | 2016-10-07 06:57:32 -0700 | [diff] [blame] | 888 | // We have two possible cases for geometry for a circle: |
| 889 | |
| 890 | // In the case of a normal fill, we draw geometry for the circle as an octagon. |
| 891 | static const uint16_t gFillCircleIndices[] = { |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 892 | // enter the octagon |
| 893 | // clang-format off |
| 894 | 0, 1, 8, 1, 2, 8, |
| 895 | 2, 3, 8, 3, 4, 8, |
| 896 | 4, 5, 8, 5, 6, 8, |
| 897 | 6, 7, 8, 7, 0, 8 |
| 898 | // clang-format on |
jvanverth | 6ca4882 | 2016-10-07 06:57:32 -0700 | [diff] [blame] | 899 | }; |
| 900 | |
| 901 | // For stroked circles, we use two nested octagons. |
| 902 | static const uint16_t gStrokeCircleIndices[] = { |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 903 | // enter the octagon |
| 904 | // clang-format off |
| 905 | 0, 1, 9, 0, 9, 8, |
| 906 | 1, 2, 10, 1, 10, 9, |
| 907 | 2, 3, 11, 2, 11, 10, |
| 908 | 3, 4, 12, 3, 12, 11, |
| 909 | 4, 5, 13, 4, 13, 12, |
| 910 | 5, 6, 14, 5, 14, 13, |
| 911 | 6, 7, 15, 6, 15, 14, |
| 912 | 7, 0, 8, 7, 8, 15, |
| 913 | // clang-format on |
jvanverth | 6ca4882 | 2016-10-07 06:57:32 -0700 | [diff] [blame] | 914 | }; |
| 915 | |
Brian Osman | 9d958b5 | 2018-11-13 12:46:56 -0500 | [diff] [blame] | 916 | // Normalized geometry for octagons that circumscribe and lie on a circle: |
| 917 | |
| 918 | static constexpr SkScalar kOctOffset = 0.41421356237f; // sqrt(2) - 1 |
| 919 | static constexpr SkPoint kOctagonOuter[] = { |
| 920 | SkPoint::Make(-kOctOffset, -1), |
| 921 | SkPoint::Make( kOctOffset, -1), |
| 922 | SkPoint::Make( 1, -kOctOffset), |
| 923 | SkPoint::Make( 1, kOctOffset), |
| 924 | SkPoint::Make( kOctOffset, 1), |
| 925 | SkPoint::Make(-kOctOffset, 1), |
| 926 | SkPoint::Make(-1, kOctOffset), |
| 927 | SkPoint::Make(-1, -kOctOffset), |
| 928 | }; |
| 929 | |
| 930 | // cosine and sine of pi/8 |
| 931 | static constexpr SkScalar kCosPi8 = 0.923579533f; |
| 932 | static constexpr SkScalar kSinPi8 = 0.382683432f; |
| 933 | static constexpr SkPoint kOctagonInner[] = { |
| 934 | SkPoint::Make(-kSinPi8, -kCosPi8), |
| 935 | SkPoint::Make( kSinPi8, -kCosPi8), |
| 936 | SkPoint::Make( kCosPi8, -kSinPi8), |
| 937 | SkPoint::Make( kCosPi8, kSinPi8), |
| 938 | SkPoint::Make( kSinPi8, kCosPi8), |
| 939 | SkPoint::Make(-kSinPi8, kCosPi8), |
| 940 | SkPoint::Make(-kCosPi8, kSinPi8), |
| 941 | SkPoint::Make(-kCosPi8, -kSinPi8), |
| 942 | }; |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 943 | |
jvanverth | 6ca4882 | 2016-10-07 06:57:32 -0700 | [diff] [blame] | 944 | static const int kIndicesPerFillCircle = SK_ARRAY_COUNT(gFillCircleIndices); |
| 945 | static const int kIndicesPerStrokeCircle = SK_ARRAY_COUNT(gStrokeCircleIndices); |
| 946 | static const int kVertsPerStrokeCircle = 16; |
| 947 | static const int kVertsPerFillCircle = 9; |
| 948 | |
| 949 | static int circle_type_to_vert_count(bool stroked) { |
| 950 | return stroked ? kVertsPerStrokeCircle : kVertsPerFillCircle; |
| 951 | } |
| 952 | |
| 953 | static int circle_type_to_index_count(bool stroked) { |
| 954 | return stroked ? kIndicesPerStrokeCircle : kIndicesPerFillCircle; |
| 955 | } |
| 956 | |
| 957 | static const uint16_t* circle_type_to_indices(bool stroked) { |
| 958 | return stroked ? gStrokeCircleIndices : gFillCircleIndices; |
| 959 | } |
| 960 | |
| 961 | /////////////////////////////////////////////////////////////////////////////// |
| 962 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 963 | class CircleOp final : public GrMeshDrawOp { |
| 964 | private: |
| 965 | using Helper = GrSimpleMeshDrawOpHelper; |
| 966 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 967 | public: |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 968 | DEFINE_OP_CLASS_ID |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 969 | |
bsalomon | 4f3a0ca | 2016-08-22 13:14:26 -0700 | [diff] [blame] | 970 | /** Optional extra params to render a partial arc rather than a full circle. */ |
| 971 | struct ArcParams { |
| 972 | SkScalar fStartAngleRadians; |
| 973 | SkScalar fSweepAngleRadians; |
| 974 | bool fUseCenter; |
| 975 | }; |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 976 | |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 977 | static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context, |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 978 | GrPaint&& paint, |
| 979 | const SkMatrix& viewMatrix, |
| 980 | SkPoint center, |
| 981 | SkScalar radius, |
| 982 | const GrStyle& style, |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 983 | const ArcParams* arcParams = nullptr) { |
bsalomon | 4f3a0ca | 2016-08-22 13:14:26 -0700 | [diff] [blame] | 984 | SkASSERT(circle_stays_circle(viewMatrix)); |
bsalomon | 4f3a0ca | 2016-08-22 13:14:26 -0700 | [diff] [blame] | 985 | if (style.hasPathEffect()) { |
| 986 | return nullptr; |
| 987 | } |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 988 | const SkStrokeRec& stroke = style.strokeRec(); |
bsalomon | 4f3a0ca | 2016-08-22 13:14:26 -0700 | [diff] [blame] | 989 | SkStrokeRec::Style recStyle = stroke.getStyle(); |
| 990 | if (arcParams) { |
| 991 | // Arc support depends on the style. |
| 992 | switch (recStyle) { |
| 993 | case SkStrokeRec::kStrokeAndFill_Style: |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 994 | // This produces a strange result that this op doesn't implement. |
bsalomon | 4f3a0ca | 2016-08-22 13:14:26 -0700 | [diff] [blame] | 995 | return nullptr; |
| 996 | case SkStrokeRec::kFill_Style: |
| 997 | // This supports all fills. |
| 998 | break; |
Brian Salomon | 45c9220 | 2018-04-10 10:53:58 -0400 | [diff] [blame] | 999 | case SkStrokeRec::kStroke_Style: |
| 1000 | // Strokes that don't use the center point are supported with butt and round |
| 1001 | // caps. |
| 1002 | if (arcParams->fUseCenter || stroke.getCap() == SkPaint::kSquare_Cap) { |
| 1003 | return nullptr; |
| 1004 | } |
| 1005 | break; |
bsalomon | 4f3a0ca | 2016-08-22 13:14:26 -0700 | [diff] [blame] | 1006 | case SkStrokeRec::kHairline_Style: |
Brian Salomon | 45c9220 | 2018-04-10 10:53:58 -0400 | [diff] [blame] | 1007 | // Hairline only supports butt cap. Round caps could be emulated by slightly |
| 1008 | // extending the angle range if we ever care to. |
bsalomon | 4f3a0ca | 2016-08-22 13:14:26 -0700 | [diff] [blame] | 1009 | if (arcParams->fUseCenter || stroke.getCap() != SkPaint::kButt_Cap) { |
| 1010 | return nullptr; |
| 1011 | } |
| 1012 | break; |
| 1013 | } |
| 1014 | } |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 1015 | return Helper::FactoryHelper<CircleOp>(context, std::move(paint), viewMatrix, center, |
| 1016 | radius, style, arcParams); |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 1017 | } |
| 1018 | |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 1019 | CircleOp(const Helper::MakeArgs& helperArgs, const SkPMColor4f& color, |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 1020 | const SkMatrix& viewMatrix, SkPoint center, SkScalar radius, const GrStyle& style, |
| 1021 | const ArcParams* arcParams) |
Brian Salomon | ea26d6b | 2018-01-23 20:33:21 +0000 | [diff] [blame] | 1022 | : GrMeshDrawOp(ClassID()), fHelper(helperArgs, GrAAType::kCoverage) { |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 1023 | const SkStrokeRec& stroke = style.strokeRec(); |
| 1024 | SkStrokeRec::Style recStyle = stroke.getStyle(); |
bsalomon | 4f3a0ca | 2016-08-22 13:14:26 -0700 | [diff] [blame] | 1025 | |
Brian Salomon | 45c9220 | 2018-04-10 10:53:58 -0400 | [diff] [blame] | 1026 | fRoundCaps = false; |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 1027 | |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 1028 | viewMatrix.mapPoints(¢er, 1); |
bsalomon | 4f3a0ca | 2016-08-22 13:14:26 -0700 | [diff] [blame] | 1029 | radius = viewMatrix.mapRadius(radius); |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 1030 | SkScalar strokeWidth = viewMatrix.mapRadius(stroke.getWidth()); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1031 | |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 1032 | bool isStrokeOnly = |
| 1033 | SkStrokeRec::kStroke_Style == recStyle || SkStrokeRec::kHairline_Style == recStyle; |
bsalomon | 4f3a0ca | 2016-08-22 13:14:26 -0700 | [diff] [blame] | 1034 | bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == recStyle; |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 1035 | |
jvanverth | 6ca4882 | 2016-10-07 06:57:32 -0700 | [diff] [blame] | 1036 | SkScalar innerRadius = -SK_ScalarHalf; |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 1037 | SkScalar outerRadius = radius; |
| 1038 | SkScalar halfWidth = 0; |
| 1039 | if (hasStroke) { |
| 1040 | if (SkScalarNearlyZero(strokeWidth)) { |
| 1041 | halfWidth = SK_ScalarHalf; |
| 1042 | } else { |
| 1043 | halfWidth = SkScalarHalf(strokeWidth); |
| 1044 | } |
| 1045 | |
| 1046 | outerRadius += halfWidth; |
| 1047 | if (isStrokeOnly) { |
| 1048 | innerRadius = radius - halfWidth; |
| 1049 | } |
| 1050 | } |
| 1051 | |
| 1052 | // The radii are outset for two reasons. First, it allows the shader to simply perform |
| 1053 | // simpler computation because the computed alpha is zero, rather than 50%, at the radius. |
| 1054 | // Second, the outer radius is used to compute the verts of the bounding box that is |
| 1055 | // rendered and the outset ensures the box will cover all partially covered by the circle. |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 1056 | outerRadius += SK_ScalarHalf; |
| 1057 | innerRadius -= SK_ScalarHalf; |
jvanverth | 6ca4882 | 2016-10-07 06:57:32 -0700 | [diff] [blame] | 1058 | bool stroked = isStrokeOnly && innerRadius > 0.0f; |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 1059 | fViewMatrixIfUsingLocalCoords = viewMatrix; |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 1060 | |
bsalomon | 4f3a0ca | 2016-08-22 13:14:26 -0700 | [diff] [blame] | 1061 | // This makes every point fully inside the intersection plane. |
| 1062 | static constexpr SkScalar kUnusedIsectPlane[] = {0.f, 0.f, 1.f}; |
| 1063 | // This makes every point fully outside the union plane. |
| 1064 | static constexpr SkScalar kUnusedUnionPlane[] = {0.f, 0.f, 0.f}; |
Brian Salomon | 45c9220 | 2018-04-10 10:53:58 -0400 | [diff] [blame] | 1065 | static constexpr SkPoint kUnusedRoundCaps[] = {{1e10f, 1e10f}, {1e10f, 1e10f}}; |
bsalomon | 4f3a0ca | 2016-08-22 13:14:26 -0700 | [diff] [blame] | 1066 | SkRect devBounds = SkRect::MakeLTRB(center.fX - outerRadius, center.fY - outerRadius, |
| 1067 | center.fX + outerRadius, center.fY + outerRadius); |
bsalomon | 4f3a0ca | 2016-08-22 13:14:26 -0700 | [diff] [blame] | 1068 | if (arcParams) { |
| 1069 | // The shader operates in a space where the circle is translated to be centered at the |
| 1070 | // origin. Here we compute points on the unit circle at the starting and ending angles. |
| 1071 | SkPoint startPoint, stopPoint; |
Brian Osman | 4428f2c | 2019-04-02 10:59:28 -0400 | [diff] [blame] | 1072 | startPoint.fY = SkScalarSin(arcParams->fStartAngleRadians); |
| 1073 | startPoint.fX = SkScalarCos(arcParams->fStartAngleRadians); |
bsalomon | 4f3a0ca | 2016-08-22 13:14:26 -0700 | [diff] [blame] | 1074 | SkScalar endAngle = arcParams->fStartAngleRadians + arcParams->fSweepAngleRadians; |
Brian Osman | 4428f2c | 2019-04-02 10:59:28 -0400 | [diff] [blame] | 1075 | stopPoint.fY = SkScalarSin(endAngle); |
| 1076 | stopPoint.fX = SkScalarCos(endAngle); |
Brian Osman | fd773fb | 2017-05-17 11:43:11 -0400 | [diff] [blame] | 1077 | |
| 1078 | // Adjust the start and end points based on the view matrix (to handle rotated arcs) |
| 1079 | startPoint = viewMatrix.mapVector(startPoint.fX, startPoint.fY); |
| 1080 | stopPoint = viewMatrix.mapVector(stopPoint.fX, stopPoint.fY); |
| 1081 | startPoint.normalize(); |
| 1082 | stopPoint.normalize(); |
| 1083 | |
| 1084 | // If the matrix included scale (on one axis) we need to swap our start and end points |
| 1085 | if ((viewMatrix.getScaleX() < 0) != (viewMatrix.getScaleY() < 0)) { |
Ben Wagner | f08d1d0 | 2018-06-18 15:11:00 -0400 | [diff] [blame] | 1086 | using std::swap; |
| 1087 | swap(startPoint, stopPoint); |
Brian Osman | fd773fb | 2017-05-17 11:43:11 -0400 | [diff] [blame] | 1088 | } |
| 1089 | |
Brian Salomon | 45c9220 | 2018-04-10 10:53:58 -0400 | [diff] [blame] | 1090 | fRoundCaps = style.strokeRec().getWidth() > 0 && |
| 1091 | style.strokeRec().getCap() == SkPaint::kRound_Cap; |
| 1092 | SkPoint roundCaps[2]; |
| 1093 | if (fRoundCaps) { |
| 1094 | // Compute the cap center points in the normalized space. |
| 1095 | SkScalar midRadius = (innerRadius + outerRadius) / (2 * outerRadius); |
| 1096 | roundCaps[0] = startPoint * midRadius; |
| 1097 | roundCaps[1] = stopPoint * midRadius; |
| 1098 | } else { |
| 1099 | roundCaps[0] = kUnusedRoundCaps[0]; |
| 1100 | roundCaps[1] = kUnusedRoundCaps[1]; |
| 1101 | } |
| 1102 | |
bsalomon | 4f3a0ca | 2016-08-22 13:14:26 -0700 | [diff] [blame] | 1103 | // Like a fill without useCenter, butt-cap stroke can be implemented by clipping against |
Brian Salomon | 45c9220 | 2018-04-10 10:53:58 -0400 | [diff] [blame] | 1104 | // radial lines. We treat round caps the same way, but tack coverage of circles at the |
| 1105 | // center of the butts. |
| 1106 | // However, in both cases we have to be careful about the half-circle. |
bsalomon | 4f3a0ca | 2016-08-22 13:14:26 -0700 | [diff] [blame] | 1107 | // case. In that case the two radial lines are equal and so that edge gets clipped |
Brian Salomon | 45c9220 | 2018-04-10 10:53:58 -0400 | [diff] [blame] | 1108 | // twice. Since the shared edge goes through the center we fall back on the !useCenter |
bsalomon | 4f3a0ca | 2016-08-22 13:14:26 -0700 | [diff] [blame] | 1109 | // case. |
Brian Salomon | 45c9220 | 2018-04-10 10:53:58 -0400 | [diff] [blame] | 1110 | auto absSweep = SkScalarAbs(arcParams->fSweepAngleRadians); |
| 1111 | bool useCenter = (arcParams->fUseCenter || isStrokeOnly) && |
| 1112 | !SkScalarNearlyEqual(absSweep, SK_ScalarPI); |
bsalomon | 4f3a0ca | 2016-08-22 13:14:26 -0700 | [diff] [blame] | 1113 | if (useCenter) { |
| 1114 | SkVector norm0 = {startPoint.fY, -startPoint.fX}; |
| 1115 | SkVector norm1 = {stopPoint.fY, -stopPoint.fX}; |
Brian Osman | 9a24fee | 2018-08-03 09:48:42 -0400 | [diff] [blame] | 1116 | // This ensures that norm0 is always the clockwise plane, and norm1 is CCW. |
| 1117 | if (arcParams->fSweepAngleRadians < 0) { |
| 1118 | std::swap(norm0, norm1); |
bsalomon | 4f3a0ca | 2016-08-22 13:14:26 -0700 | [diff] [blame] | 1119 | } |
Brian Osman | 9a24fee | 2018-08-03 09:48:42 -0400 | [diff] [blame] | 1120 | norm0.negate(); |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 1121 | fClipPlane = true; |
Brian Salomon | 45c9220 | 2018-04-10 10:53:58 -0400 | [diff] [blame] | 1122 | if (absSweep > SK_ScalarPI) { |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 1123 | fCircles.emplace_back(Circle{ |
bsalomon | 4f3a0ca | 2016-08-22 13:14:26 -0700 | [diff] [blame] | 1124 | color, |
| 1125 | innerRadius, |
| 1126 | outerRadius, |
| 1127 | {norm0.fX, norm0.fY, 0.5f}, |
| 1128 | {kUnusedIsectPlane[0], kUnusedIsectPlane[1], kUnusedIsectPlane[2]}, |
| 1129 | {norm1.fX, norm1.fY, 0.5f}, |
Brian Salomon | 45c9220 | 2018-04-10 10:53:58 -0400 | [diff] [blame] | 1130 | {roundCaps[0], roundCaps[1]}, |
jvanverth | 6ca4882 | 2016-10-07 06:57:32 -0700 | [diff] [blame] | 1131 | devBounds, |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 1132 | stroked}); |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 1133 | fClipPlaneIsect = false; |
| 1134 | fClipPlaneUnion = true; |
bsalomon | 4f3a0ca | 2016-08-22 13:14:26 -0700 | [diff] [blame] | 1135 | } else { |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 1136 | fCircles.emplace_back(Circle{ |
bsalomon | 4f3a0ca | 2016-08-22 13:14:26 -0700 | [diff] [blame] | 1137 | color, |
| 1138 | innerRadius, |
| 1139 | outerRadius, |
| 1140 | {norm0.fX, norm0.fY, 0.5f}, |
| 1141 | {norm1.fX, norm1.fY, 0.5f}, |
| 1142 | {kUnusedUnionPlane[0], kUnusedUnionPlane[1], kUnusedUnionPlane[2]}, |
Brian Salomon | 45c9220 | 2018-04-10 10:53:58 -0400 | [diff] [blame] | 1143 | {roundCaps[0], roundCaps[1]}, |
jvanverth | 6ca4882 | 2016-10-07 06:57:32 -0700 | [diff] [blame] | 1144 | devBounds, |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 1145 | stroked}); |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 1146 | fClipPlaneIsect = true; |
| 1147 | fClipPlaneUnion = false; |
bsalomon | 4f3a0ca | 2016-08-22 13:14:26 -0700 | [diff] [blame] | 1148 | } |
| 1149 | } else { |
| 1150 | // We clip to a secant of the original circle. |
| 1151 | startPoint.scale(radius); |
| 1152 | stopPoint.scale(radius); |
| 1153 | SkVector norm = {startPoint.fY - stopPoint.fY, stopPoint.fX - startPoint.fX}; |
| 1154 | norm.normalize(); |
| 1155 | if (arcParams->fSweepAngleRadians > 0) { |
| 1156 | norm.negate(); |
| 1157 | } |
| 1158 | SkScalar d = -norm.dot(startPoint) + 0.5f; |
| 1159 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 1160 | fCircles.emplace_back( |
| 1161 | Circle{color, |
| 1162 | innerRadius, |
| 1163 | outerRadius, |
| 1164 | {norm.fX, norm.fY, d}, |
| 1165 | {kUnusedIsectPlane[0], kUnusedIsectPlane[1], kUnusedIsectPlane[2]}, |
| 1166 | {kUnusedUnionPlane[0], kUnusedUnionPlane[1], kUnusedUnionPlane[2]}, |
Brian Salomon | 45c9220 | 2018-04-10 10:53:58 -0400 | [diff] [blame] | 1167 | {roundCaps[0], roundCaps[1]}, |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 1168 | devBounds, |
| 1169 | stroked}); |
| 1170 | fClipPlane = true; |
| 1171 | fClipPlaneIsect = false; |
| 1172 | fClipPlaneUnion = false; |
bsalomon | 4f3a0ca | 2016-08-22 13:14:26 -0700 | [diff] [blame] | 1173 | } |
| 1174 | } else { |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 1175 | fCircles.emplace_back( |
| 1176 | Circle{color, |
| 1177 | innerRadius, |
| 1178 | outerRadius, |
| 1179 | {kUnusedIsectPlane[0], kUnusedIsectPlane[1], kUnusedIsectPlane[2]}, |
| 1180 | {kUnusedIsectPlane[0], kUnusedIsectPlane[1], kUnusedIsectPlane[2]}, |
| 1181 | {kUnusedUnionPlane[0], kUnusedUnionPlane[1], kUnusedUnionPlane[2]}, |
Brian Salomon | 45c9220 | 2018-04-10 10:53:58 -0400 | [diff] [blame] | 1182 | {kUnusedRoundCaps[0], kUnusedRoundCaps[1]}, |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 1183 | devBounds, |
| 1184 | stroked}); |
| 1185 | fClipPlane = false; |
| 1186 | fClipPlaneIsect = false; |
| 1187 | fClipPlaneUnion = false; |
bsalomon | 4f3a0ca | 2016-08-22 13:14:26 -0700 | [diff] [blame] | 1188 | } |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 1189 | // Use the original radius and stroke radius for the bounds so that it does not include the |
| 1190 | // AA bloat. |
| 1191 | radius += halfWidth; |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 1192 | this->setBounds( |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 1193 | {center.fX - radius, center.fY - radius, center.fX + radius, center.fY + radius}, |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 1194 | HasAABloat::kYes, IsZeroArea::kNo); |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 1195 | fVertCount = circle_type_to_vert_count(stroked); |
| 1196 | fIndexCount = circle_type_to_index_count(stroked); |
| 1197 | fAllFill = !stroked; |
bsalomon | cdaa97b | 2016-03-08 08:30:14 -0800 | [diff] [blame] | 1198 | } |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 1199 | |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 1200 | const char* name() const override { return "CircleOp"; } |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1201 | |
Chris Dalton | 1706cbf | 2019-05-21 19:35:29 -0600 | [diff] [blame^] | 1202 | void visitProxies(const VisitProxyFunc& func) const override { |
Robert Phillips | b493eeb | 2017-09-13 13:10:52 -0400 | [diff] [blame] | 1203 | fHelper.visitProxies(func); |
| 1204 | } |
| 1205 | |
Brian Osman | 9a390ac | 2018-11-12 09:47:48 -0500 | [diff] [blame] | 1206 | #ifdef SK_DEBUG |
robertphillips | e004bfc | 2015-11-16 09:06:59 -0800 | [diff] [blame] | 1207 | SkString dumpInfo() const override { |
| 1208 | SkString string; |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 1209 | for (int i = 0; i < fCircles.count(); ++i) { |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 1210 | string.appendf( |
| 1211 | "Color: 0x%08x Rect [L: %.2f, T: %.2f, R: %.2f, B: %.2f]," |
| 1212 | "InnerRad: %.2f, OuterRad: %.2f\n", |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 1213 | fCircles[i].fColor.toBytes_RGBA(), fCircles[i].fDevBounds.fLeft, |
Brian Osman | 1be2b7c | 2018-10-29 16:07:15 -0400 | [diff] [blame] | 1214 | fCircles[i].fDevBounds.fTop, fCircles[i].fDevBounds.fRight, |
| 1215 | fCircles[i].fDevBounds.fBottom, fCircles[i].fInnerRadius, |
| 1216 | fCircles[i].fOuterRadius); |
robertphillips | e004bfc | 2015-11-16 09:06:59 -0800 | [diff] [blame] | 1217 | } |
Brian Salomon | 82dfd3d | 2017-06-14 12:30:35 -0400 | [diff] [blame] | 1218 | string += fHelper.dumpInfo(); |
| 1219 | string += INHERITED::dumpInfo(); |
robertphillips | e004bfc | 2015-11-16 09:06:59 -0800 | [diff] [blame] | 1220 | return string; |
| 1221 | } |
Brian Osman | 9a390ac | 2018-11-12 09:47:48 -0500 | [diff] [blame] | 1222 | #endif |
robertphillips | e004bfc | 2015-11-16 09:06:59 -0800 | [diff] [blame] | 1223 | |
Brian Osman | 5ced0bf | 2019-03-15 10:15:29 -0400 | [diff] [blame] | 1224 | GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip, |
| 1225 | GrFSAAType fsaaType, GrClampType clampType) override { |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 1226 | SkPMColor4f* color = &fCircles.front().fColor; |
Brian Osman | 5ced0bf | 2019-03-15 10:15:29 -0400 | [diff] [blame] | 1227 | return fHelper.finalizeProcessors(caps, clip, fsaaType, clampType, |
Brian Osman | 8fa7ab4 | 2019-03-18 10:22:42 -0400 | [diff] [blame] | 1228 | GrProcessorAnalysisCoverage::kSingleChannel, color, |
| 1229 | &fWideColor); |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); } |
| 1233 | |
bsalomon | e46f9fe | 2015-08-18 06:05:14 -0700 | [diff] [blame] | 1234 | private: |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 1235 | void onPrepareDraws(Target* target) override { |
bsalomon | cdaa97b | 2016-03-08 08:30:14 -0800 | [diff] [blame] | 1236 | SkMatrix localMatrix; |
| 1237 | if (!fViewMatrixIfUsingLocalCoords.invert(&localMatrix)) { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1238 | return; |
| 1239 | } |
| 1240 | |
| 1241 | // Setup geometry processor |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 1242 | sk_sp<GrGeometryProcessor> gp(new CircleGeometryProcessor( |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 1243 | !fAllFill, fClipPlane, fClipPlaneIsect, fClipPlaneUnion, fRoundCaps, fWideColor, |
| 1244 | localMatrix)); |
bsalomon | 4f3a0ca | 2016-08-22 13:14:26 -0700 | [diff] [blame] | 1245 | |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 1246 | sk_sp<const GrBuffer> vertexBuffer; |
jvanverth | 6ca4882 | 2016-10-07 06:57:32 -0700 | [diff] [blame] | 1247 | int firstVertex; |
Brian Osman | f9aabff | 2018-11-13 16:11:38 -0500 | [diff] [blame] | 1248 | GrVertexWriter vertices{target->makeVertexSpace(gp->vertexStride(), fVertCount, |
| 1249 | &vertexBuffer, &firstVertex)}; |
| 1250 | if (!vertices.fPtr) { |
jvanverth | 6ca4882 | 2016-10-07 06:57:32 -0700 | [diff] [blame] | 1251 | SkDebugf("Could not allocate vertices\n"); |
joshualitt | 4b31de8 | 2015-03-05 14:33:41 -0800 | [diff] [blame] | 1252 | return; |
| 1253 | } |
| 1254 | |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 1255 | sk_sp<const GrBuffer> indexBuffer = nullptr; |
jvanverth | 6ca4882 | 2016-10-07 06:57:32 -0700 | [diff] [blame] | 1256 | int firstIndex = 0; |
| 1257 | uint16_t* indices = target->makeIndexSpace(fIndexCount, &indexBuffer, &firstIndex); |
| 1258 | if (!indices) { |
| 1259 | SkDebugf("Could not allocate indices\n"); |
| 1260 | return; |
| 1261 | } |
| 1262 | |
| 1263 | int currStartVertex = 0; |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 1264 | for (const auto& circle : fCircles) { |
| 1265 | SkScalar innerRadius = circle.fInnerRadius; |
| 1266 | SkScalar outerRadius = circle.fOuterRadius; |
Brian Osman | e3caf2d | 2018-11-21 13:48:36 -0500 | [diff] [blame] | 1267 | GrVertexColor color(circle.fColor, fWideColor); |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 1268 | const SkRect& bounds = circle.fDevBounds; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1269 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1270 | // The inner radius in the vertex data must be specified in normalized space. |
| 1271 | innerRadius = innerRadius / outerRadius; |
Brian Osman | f9aabff | 2018-11-13 16:11:38 -0500 | [diff] [blame] | 1272 | SkPoint radii = { outerRadius, innerRadius }; |
jvanverth | 6ca4882 | 2016-10-07 06:57:32 -0700 | [diff] [blame] | 1273 | |
| 1274 | SkPoint center = SkPoint::Make(bounds.centerX(), bounds.centerY()); |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 1275 | SkScalar halfWidth = 0.5f * bounds.width(); |
Herb Derby | 60c05f9 | 2016-12-13 15:18:55 -0500 | [diff] [blame] | 1276 | |
Brian Osman | 9a24fee | 2018-08-03 09:48:42 -0400 | [diff] [blame] | 1277 | SkVector geoClipPlane = { 0, 0 }; |
| 1278 | SkScalar offsetClipDist = SK_Scalar1; |
| 1279 | if (!circle.fStroked && fClipPlane && fClipPlaneIsect && |
| 1280 | (circle.fClipPlane[0] * circle.fIsectPlane[0] + |
| 1281 | circle.fClipPlane[1] * circle.fIsectPlane[1]) < 0.0f) { |
| 1282 | // Acute arc. Clip the vertices to the perpendicular half-plane. We've constructed |
| 1283 | // fClipPlane to be clockwise, and fISectPlane to be CCW, so we can can rotate them |
| 1284 | // each 90 degrees to point "out", then average them. We back off by 1/2 pixel so |
| 1285 | // the AA can extend just past the center of the circle. |
| 1286 | geoClipPlane.set(circle.fClipPlane[1] - circle.fIsectPlane[1], |
| 1287 | circle.fIsectPlane[0] - circle.fClipPlane[0]); |
| 1288 | SkAssertResult(geoClipPlane.normalize()); |
| 1289 | offsetClipDist = 0.5f / halfWidth; |
| 1290 | } |
| 1291 | |
Brian Osman | 7d8f82b | 2018-11-08 10:24:09 -0500 | [diff] [blame] | 1292 | for (int i = 0; i < 8; ++i) { |
| 1293 | // This clips the normalized offset to the half-plane we computed above. Then we |
| 1294 | // compute the vertex position from this. |
Brian Osman | 9d958b5 | 2018-11-13 12:46:56 -0500 | [diff] [blame] | 1295 | SkScalar dist = SkTMin(kOctagonOuter[i].dot(geoClipPlane) + offsetClipDist, 0.0f); |
| 1296 | SkVector offset = kOctagonOuter[i] - geoClipPlane * dist; |
Brian Osman | f9aabff | 2018-11-13 16:11:38 -0500 | [diff] [blame] | 1297 | vertices.write(center + offset * halfWidth, |
| 1298 | color, |
| 1299 | offset, |
| 1300 | radii); |
Brian Osman | 7d8f82b | 2018-11-08 10:24:09 -0500 | [diff] [blame] | 1301 | if (fClipPlane) { |
Brian Osman | f9aabff | 2018-11-13 16:11:38 -0500 | [diff] [blame] | 1302 | vertices.write(circle.fClipPlane); |
Brian Osman | 7d8f82b | 2018-11-08 10:24:09 -0500 | [diff] [blame] | 1303 | } |
| 1304 | if (fClipPlaneIsect) { |
Brian Osman | f9aabff | 2018-11-13 16:11:38 -0500 | [diff] [blame] | 1305 | vertices.write(circle.fIsectPlane); |
Brian Osman | 7d8f82b | 2018-11-08 10:24:09 -0500 | [diff] [blame] | 1306 | } |
| 1307 | if (fClipPlaneUnion) { |
Brian Osman | f9aabff | 2018-11-13 16:11:38 -0500 | [diff] [blame] | 1308 | vertices.write(circle.fUnionPlane); |
Brian Osman | 7d8f82b | 2018-11-08 10:24:09 -0500 | [diff] [blame] | 1309 | } |
| 1310 | if (fRoundCaps) { |
Brian Osman | f9aabff | 2018-11-13 16:11:38 -0500 | [diff] [blame] | 1311 | vertices.write(circle.fRoundCapCenters); |
Brian Osman | 7d8f82b | 2018-11-08 10:24:09 -0500 | [diff] [blame] | 1312 | } |
Brian Salomon | 45c9220 | 2018-04-10 10:53:58 -0400 | [diff] [blame] | 1313 | } |
jvanverth | 6ca4882 | 2016-10-07 06:57:32 -0700 | [diff] [blame] | 1314 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 1315 | if (circle.fStroked) { |
jvanverth | 6ca4882 | 2016-10-07 06:57:32 -0700 | [diff] [blame] | 1316 | // compute the inner ring |
jvanverth | 6ca4882 | 2016-10-07 06:57:32 -0700 | [diff] [blame] | 1317 | |
Brian Osman | 7d8f82b | 2018-11-08 10:24:09 -0500 | [diff] [blame] | 1318 | for (int i = 0; i < 8; ++i) { |
Brian Osman | f9aabff | 2018-11-13 16:11:38 -0500 | [diff] [blame] | 1319 | vertices.write(center + kOctagonInner[i] * circle.fInnerRadius, |
| 1320 | color, |
| 1321 | kOctagonInner[i] * innerRadius, |
| 1322 | radii); |
Brian Osman | 7d8f82b | 2018-11-08 10:24:09 -0500 | [diff] [blame] | 1323 | if (fClipPlane) { |
Brian Osman | f9aabff | 2018-11-13 16:11:38 -0500 | [diff] [blame] | 1324 | vertices.write(circle.fClipPlane); |
Brian Osman | 7d8f82b | 2018-11-08 10:24:09 -0500 | [diff] [blame] | 1325 | } |
| 1326 | if (fClipPlaneIsect) { |
Brian Osman | f9aabff | 2018-11-13 16:11:38 -0500 | [diff] [blame] | 1327 | vertices.write(circle.fIsectPlane); |
Brian Osman | 7d8f82b | 2018-11-08 10:24:09 -0500 | [diff] [blame] | 1328 | } |
| 1329 | if (fClipPlaneUnion) { |
Brian Osman | f9aabff | 2018-11-13 16:11:38 -0500 | [diff] [blame] | 1330 | vertices.write(circle.fUnionPlane); |
Brian Osman | 7d8f82b | 2018-11-08 10:24:09 -0500 | [diff] [blame] | 1331 | } |
| 1332 | if (fRoundCaps) { |
Brian Osman | f9aabff | 2018-11-13 16:11:38 -0500 | [diff] [blame] | 1333 | vertices.write(circle.fRoundCapCenters); |
Brian Osman | 7d8f82b | 2018-11-08 10:24:09 -0500 | [diff] [blame] | 1334 | } |
Brian Salomon | 45c9220 | 2018-04-10 10:53:58 -0400 | [diff] [blame] | 1335 | } |
jvanverth | 6ca4882 | 2016-10-07 06:57:32 -0700 | [diff] [blame] | 1336 | } else { |
| 1337 | // filled |
Brian Osman | f9aabff | 2018-11-13 16:11:38 -0500 | [diff] [blame] | 1338 | vertices.write(center, color, SkPoint::Make(0, 0), radii); |
jvanverth | 6ca4882 | 2016-10-07 06:57:32 -0700 | [diff] [blame] | 1339 | if (fClipPlane) { |
Brian Osman | f9aabff | 2018-11-13 16:11:38 -0500 | [diff] [blame] | 1340 | vertices.write(circle.fClipPlane); |
jvanverth | 6ca4882 | 2016-10-07 06:57:32 -0700 | [diff] [blame] | 1341 | } |
jvanverth | 6ca4882 | 2016-10-07 06:57:32 -0700 | [diff] [blame] | 1342 | if (fClipPlaneIsect) { |
Brian Osman | f9aabff | 2018-11-13 16:11:38 -0500 | [diff] [blame] | 1343 | vertices.write(circle.fIsectPlane); |
jvanverth | 6ca4882 | 2016-10-07 06:57:32 -0700 | [diff] [blame] | 1344 | } |
| 1345 | if (fClipPlaneUnion) { |
Brian Osman | f9aabff | 2018-11-13 16:11:38 -0500 | [diff] [blame] | 1346 | vertices.write(circle.fUnionPlane); |
jvanverth | 6ca4882 | 2016-10-07 06:57:32 -0700 | [diff] [blame] | 1347 | } |
Brian Salomon | 5ceda55 | 2018-12-14 16:03:38 -0500 | [diff] [blame] | 1348 | if (fRoundCaps) { |
| 1349 | vertices.write(circle.fRoundCapCenters); |
| 1350 | } |
jvanverth | 6ca4882 | 2016-10-07 06:57:32 -0700 | [diff] [blame] | 1351 | } |
| 1352 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 1353 | const uint16_t* primIndices = circle_type_to_indices(circle.fStroked); |
| 1354 | const int primIndexCount = circle_type_to_index_count(circle.fStroked); |
jvanverth | 6ca4882 | 2016-10-07 06:57:32 -0700 | [diff] [blame] | 1355 | for (int i = 0; i < primIndexCount; ++i) { |
| 1356 | *indices++ = primIndices[i] + currStartVertex; |
| 1357 | } |
| 1358 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 1359 | currStartVertex += circle_type_to_vert_count(circle.fStroked); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1360 | } |
jvanverth | 6ca4882 | 2016-10-07 06:57:32 -0700 | [diff] [blame] | 1361 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 1362 | GrMesh* mesh = target->allocMesh(GrPrimitiveType::kTriangles); |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 1363 | mesh->setIndexed(std::move(indexBuffer), fIndexCount, firstIndex, 0, fVertCount - 1, |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 1364 | GrPrimitiveRestart::kNo); |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 1365 | mesh->setVertexData(std::move(vertexBuffer), firstVertex); |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 1366 | target->recordDraw(std::move(gp), mesh); |
| 1367 | } |
| 1368 | |
| 1369 | void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override { |
| 1370 | fHelper.executeDrawsAndUploads(this, flushState, chainBounds); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1371 | } |
| 1372 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 1373 | CombineResult onCombineIfPossible(GrOp* t, const GrCaps& caps) override { |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 1374 | CircleOp* that = t->cast<CircleOp>(); |
Jim Van Verth | 8cefe40 | 2017-02-09 11:36:37 -0500 | [diff] [blame] | 1375 | |
| 1376 | // can only represent 65535 unique vertices with 16-bit indices |
Jim Van Verth | e549a05 | 2017-02-21 17:55:13 -0500 | [diff] [blame] | 1377 | if (fVertCount + that->fVertCount > 65536) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 1378 | return CombineResult::kCannotCombine; |
Jim Van Verth | 8cefe40 | 2017-02-09 11:36:37 -0500 | [diff] [blame] | 1379 | } |
| 1380 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 1381 | if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 1382 | return CombineResult::kCannotCombine; |
joshualitt | 8cab9a7 | 2015-07-16 09:13:50 -0700 | [diff] [blame] | 1383 | } |
| 1384 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 1385 | if (fHelper.usesLocalCoords() && |
| 1386 | !fViewMatrixIfUsingLocalCoords.cheapEqualTo(that->fViewMatrixIfUsingLocalCoords)) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 1387 | return CombineResult::kCannotCombine; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1388 | } |
| 1389 | |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 1390 | // Because we've set up the ops that don't use the planes with noop values |
| 1391 | // we can just accumulate used planes by later ops. |
bsalomon | 4f3a0ca | 2016-08-22 13:14:26 -0700 | [diff] [blame] | 1392 | fClipPlane |= that->fClipPlane; |
| 1393 | fClipPlaneIsect |= that->fClipPlaneIsect; |
| 1394 | fClipPlaneUnion |= that->fClipPlaneUnion; |
Brian Salomon | 45c9220 | 2018-04-10 10:53:58 -0400 | [diff] [blame] | 1395 | fRoundCaps |= that->fRoundCaps; |
Brian Osman | e3caf2d | 2018-11-21 13:48:36 -0500 | [diff] [blame] | 1396 | fWideColor |= that->fWideColor; |
bsalomon | 4f3a0ca | 2016-08-22 13:14:26 -0700 | [diff] [blame] | 1397 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 1398 | fCircles.push_back_n(that->fCircles.count(), that->fCircles.begin()); |
jvanverth | 6ca4882 | 2016-10-07 06:57:32 -0700 | [diff] [blame] | 1399 | fVertCount += that->fVertCount; |
| 1400 | fIndexCount += that->fIndexCount; |
| 1401 | fAllFill = fAllFill && that->fAllFill; |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 1402 | return CombineResult::kMerged; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1403 | } |
| 1404 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 1405 | struct Circle { |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 1406 | SkPMColor4f fColor; |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 1407 | SkScalar fInnerRadius; |
| 1408 | SkScalar fOuterRadius; |
bsalomon | 4f3a0ca | 2016-08-22 13:14:26 -0700 | [diff] [blame] | 1409 | SkScalar fClipPlane[3]; |
| 1410 | SkScalar fIsectPlane[3]; |
| 1411 | SkScalar fUnionPlane[3]; |
Brian Salomon | 45c9220 | 2018-04-10 10:53:58 -0400 | [diff] [blame] | 1412 | SkPoint fRoundCapCenters[2]; |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 1413 | SkRect fDevBounds; |
| 1414 | bool fStroked; |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 1415 | }; |
| 1416 | |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 1417 | SkMatrix fViewMatrixIfUsingLocalCoords; |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 1418 | Helper fHelper; |
| 1419 | SkSTArray<1, Circle, true> fCircles; |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 1420 | int fVertCount; |
| 1421 | int fIndexCount; |
| 1422 | bool fAllFill; |
| 1423 | bool fClipPlane; |
| 1424 | bool fClipPlaneIsect; |
| 1425 | bool fClipPlaneUnion; |
Brian Salomon | 45c9220 | 2018-04-10 10:53:58 -0400 | [diff] [blame] | 1426 | bool fRoundCaps; |
Brian Osman | e3caf2d | 2018-11-21 13:48:36 -0500 | [diff] [blame] | 1427 | bool fWideColor; |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 1428 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 1429 | typedef GrMeshDrawOp INHERITED; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1430 | }; |
| 1431 | |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 1432 | class ButtCapDashedCircleOp final : public GrMeshDrawOp { |
| 1433 | private: |
| 1434 | using Helper = GrSimpleMeshDrawOpHelper; |
| 1435 | |
| 1436 | public: |
| 1437 | DEFINE_OP_CLASS_ID |
| 1438 | |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 1439 | static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context, |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 1440 | GrPaint&& paint, |
| 1441 | const SkMatrix& viewMatrix, |
| 1442 | SkPoint center, |
| 1443 | SkScalar radius, |
| 1444 | SkScalar strokeWidth, |
| 1445 | SkScalar startAngle, |
| 1446 | SkScalar onAngle, |
| 1447 | SkScalar offAngle, |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 1448 | SkScalar phaseAngle) { |
| 1449 | SkASSERT(circle_stays_circle(viewMatrix)); |
| 1450 | SkASSERT(strokeWidth < 2 * radius); |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 1451 | return Helper::FactoryHelper<ButtCapDashedCircleOp>(context, std::move(paint), viewMatrix, |
| 1452 | center, radius, strokeWidth, startAngle, |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 1453 | onAngle, offAngle, phaseAngle); |
| 1454 | } |
| 1455 | |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 1456 | ButtCapDashedCircleOp(const Helper::MakeArgs& helperArgs, const SkPMColor4f& color, |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 1457 | const SkMatrix& viewMatrix, SkPoint center, SkScalar radius, |
| 1458 | SkScalar strokeWidth, SkScalar startAngle, SkScalar onAngle, |
| 1459 | SkScalar offAngle, SkScalar phaseAngle) |
| 1460 | : GrMeshDrawOp(ClassID()), fHelper(helperArgs, GrAAType::kCoverage) { |
| 1461 | SkASSERT(circle_stays_circle(viewMatrix)); |
| 1462 | viewMatrix.mapPoints(¢er, 1); |
| 1463 | radius = viewMatrix.mapRadius(radius); |
| 1464 | strokeWidth = viewMatrix.mapRadius(strokeWidth); |
| 1465 | |
| 1466 | // Determine the angle where the circle starts in device space and whether its orientation |
| 1467 | // has been reversed. |
| 1468 | SkVector start; |
| 1469 | bool reflection; |
| 1470 | if (!startAngle) { |
| 1471 | start = {1, 0}; |
| 1472 | } else { |
Brian Osman | 4428f2c | 2019-04-02 10:59:28 -0400 | [diff] [blame] | 1473 | start.fY = SkScalarSin(startAngle); |
| 1474 | start.fX = SkScalarCos(startAngle); |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 1475 | } |
| 1476 | viewMatrix.mapVectors(&start, 1); |
| 1477 | startAngle = SkScalarATan2(start.fY, start.fX); |
| 1478 | reflection = (viewMatrix.getScaleX() * viewMatrix.getScaleY() - |
| 1479 | viewMatrix.getSkewX() * viewMatrix.getSkewY()) < 0; |
| 1480 | |
| 1481 | auto totalAngle = onAngle + offAngle; |
| 1482 | phaseAngle = SkScalarMod(phaseAngle + totalAngle / 2, totalAngle) - totalAngle / 2; |
| 1483 | |
| 1484 | SkScalar halfWidth = 0; |
| 1485 | if (SkScalarNearlyZero(strokeWidth)) { |
| 1486 | halfWidth = SK_ScalarHalf; |
| 1487 | } else { |
| 1488 | halfWidth = SkScalarHalf(strokeWidth); |
| 1489 | } |
| 1490 | |
| 1491 | SkScalar outerRadius = radius + halfWidth; |
| 1492 | SkScalar innerRadius = radius - halfWidth; |
| 1493 | |
| 1494 | // The radii are outset for two reasons. First, it allows the shader to simply perform |
| 1495 | // simpler computation because the computed alpha is zero, rather than 50%, at the radius. |
| 1496 | // Second, the outer radius is used to compute the verts of the bounding box that is |
| 1497 | // rendered and the outset ensures the box will cover all partially covered by the circle. |
| 1498 | outerRadius += SK_ScalarHalf; |
| 1499 | innerRadius -= SK_ScalarHalf; |
| 1500 | fViewMatrixIfUsingLocalCoords = viewMatrix; |
| 1501 | |
| 1502 | SkRect devBounds = SkRect::MakeLTRB(center.fX - outerRadius, center.fY - outerRadius, |
| 1503 | center.fX + outerRadius, center.fY + outerRadius); |
| 1504 | |
| 1505 | // We store whether there is a reflection as a negative total angle. |
| 1506 | if (reflection) { |
| 1507 | totalAngle = -totalAngle; |
| 1508 | } |
| 1509 | fCircles.push_back(Circle{ |
| 1510 | color, |
| 1511 | outerRadius, |
| 1512 | innerRadius, |
| 1513 | onAngle, |
| 1514 | totalAngle, |
| 1515 | startAngle, |
| 1516 | phaseAngle, |
| 1517 | devBounds |
| 1518 | }); |
| 1519 | // Use the original radius and stroke radius for the bounds so that it does not include the |
| 1520 | // AA bloat. |
| 1521 | radius += halfWidth; |
| 1522 | this->setBounds( |
| 1523 | {center.fX - radius, center.fY - radius, center.fX + radius, center.fY + radius}, |
| 1524 | HasAABloat::kYes, IsZeroArea::kNo); |
| 1525 | fVertCount = circle_type_to_vert_count(true); |
| 1526 | fIndexCount = circle_type_to_index_count(true); |
| 1527 | } |
| 1528 | |
| 1529 | const char* name() const override { return "ButtCappedDashedCircleOp"; } |
| 1530 | |
Chris Dalton | 1706cbf | 2019-05-21 19:35:29 -0600 | [diff] [blame^] | 1531 | void visitProxies(const VisitProxyFunc& func) const override { |
Brian Salomon | 7d94bb5 | 2018-10-12 14:37:19 -0400 | [diff] [blame] | 1532 | fHelper.visitProxies(func); |
| 1533 | } |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 1534 | |
Brian Osman | 9a390ac | 2018-11-12 09:47:48 -0500 | [diff] [blame] | 1535 | #ifdef SK_DEBUG |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 1536 | SkString dumpInfo() const override { |
| 1537 | SkString string; |
| 1538 | for (int i = 0; i < fCircles.count(); ++i) { |
| 1539 | string.appendf( |
| 1540 | "Color: 0x%08x Rect [L: %.2f, T: %.2f, R: %.2f, B: %.2f]," |
| 1541 | "InnerRad: %.2f, OuterRad: %.2f, OnAngle: %.2f, TotalAngle: %.2f, " |
| 1542 | "Phase: %.2f\n", |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 1543 | fCircles[i].fColor.toBytes_RGBA(), fCircles[i].fDevBounds.fLeft, |
Brian Osman | 1be2b7c | 2018-10-29 16:07:15 -0400 | [diff] [blame] | 1544 | fCircles[i].fDevBounds.fTop, fCircles[i].fDevBounds.fRight, |
| 1545 | fCircles[i].fDevBounds.fBottom, fCircles[i].fInnerRadius, |
| 1546 | fCircles[i].fOuterRadius, fCircles[i].fOnAngle, fCircles[i].fTotalAngle, |
| 1547 | fCircles[i].fPhaseAngle); |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 1548 | } |
| 1549 | string += fHelper.dumpInfo(); |
| 1550 | string += INHERITED::dumpInfo(); |
| 1551 | return string; |
| 1552 | } |
Brian Osman | 9a390ac | 2018-11-12 09:47:48 -0500 | [diff] [blame] | 1553 | #endif |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 1554 | |
Brian Osman | 5ced0bf | 2019-03-15 10:15:29 -0400 | [diff] [blame] | 1555 | GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip, |
| 1556 | GrFSAAType fsaaType, GrClampType clampType) override { |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 1557 | SkPMColor4f* color = &fCircles.front().fColor; |
Brian Osman | 5ced0bf | 2019-03-15 10:15:29 -0400 | [diff] [blame] | 1558 | return fHelper.finalizeProcessors(caps, clip, fsaaType, clampType, |
Brian Osman | 8fa7ab4 | 2019-03-18 10:22:42 -0400 | [diff] [blame] | 1559 | GrProcessorAnalysisCoverage::kSingleChannel, color, |
| 1560 | &fWideColor); |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 1561 | } |
| 1562 | |
| 1563 | FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); } |
| 1564 | |
| 1565 | private: |
| 1566 | void onPrepareDraws(Target* target) override { |
| 1567 | SkMatrix localMatrix; |
| 1568 | if (!fViewMatrixIfUsingLocalCoords.invert(&localMatrix)) { |
| 1569 | return; |
| 1570 | } |
| 1571 | |
| 1572 | // Setup geometry processor |
Brian Osman | e3caf2d | 2018-11-21 13:48:36 -0500 | [diff] [blame] | 1573 | sk_sp<GrGeometryProcessor> gp(new ButtCapDashedCircleGeometryProcessor(fWideColor, |
| 1574 | localMatrix)); |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 1575 | |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 1576 | sk_sp<const GrBuffer> vertexBuffer; |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 1577 | int firstVertex; |
Brian Osman | f9aabff | 2018-11-13 16:11:38 -0500 | [diff] [blame] | 1578 | GrVertexWriter vertices{target->makeVertexSpace(gp->vertexStride(), fVertCount, |
| 1579 | &vertexBuffer, &firstVertex)}; |
| 1580 | if (!vertices.fPtr) { |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 1581 | SkDebugf("Could not allocate vertices\n"); |
| 1582 | return; |
| 1583 | } |
| 1584 | |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 1585 | sk_sp<const GrBuffer> indexBuffer; |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 1586 | int firstIndex = 0; |
| 1587 | uint16_t* indices = target->makeIndexSpace(fIndexCount, &indexBuffer, &firstIndex); |
| 1588 | if (!indices) { |
| 1589 | SkDebugf("Could not allocate indices\n"); |
| 1590 | return; |
| 1591 | } |
| 1592 | |
| 1593 | int currStartVertex = 0; |
| 1594 | for (const auto& circle : fCircles) { |
| 1595 | // The inner radius in the vertex data must be specified in normalized space so that |
| 1596 | // length() can be called with smaller values to avoid precision issues with half |
| 1597 | // floats. |
| 1598 | auto normInnerRadius = circle.fInnerRadius / circle.fOuterRadius; |
| 1599 | const SkRect& bounds = circle.fDevBounds; |
| 1600 | bool reflect = false; |
Brian Osman | 9d958b5 | 2018-11-13 12:46:56 -0500 | [diff] [blame] | 1601 | struct { float onAngle, totalAngle, startAngle, phaseAngle; } dashParams = { |
| 1602 | circle.fOnAngle, circle.fTotalAngle, circle.fStartAngle, circle.fPhaseAngle |
| 1603 | }; |
| 1604 | if (dashParams.totalAngle < 0) { |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 1605 | reflect = true; |
Brian Osman | 9d958b5 | 2018-11-13 12:46:56 -0500 | [diff] [blame] | 1606 | dashParams.totalAngle = -dashParams.totalAngle; |
| 1607 | dashParams.startAngle = -dashParams.startAngle; |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 1608 | } |
| 1609 | |
Brian Osman | e3caf2d | 2018-11-21 13:48:36 -0500 | [diff] [blame] | 1610 | GrVertexColor color(circle.fColor, fWideColor); |
Brian Osman | 1be2b7c | 2018-10-29 16:07:15 -0400 | [diff] [blame] | 1611 | |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 1612 | // The bounding geometry for the circle is composed of an outer bounding octagon and |
| 1613 | // an inner bounded octagon. |
| 1614 | |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 1615 | // Compute the vertices of the outer octagon. |
| 1616 | SkPoint center = SkPoint::Make(bounds.centerX(), bounds.centerY()); |
| 1617 | SkScalar halfWidth = 0.5f * bounds.width(); |
Brian Osman | 9d958b5 | 2018-11-13 12:46:56 -0500 | [diff] [blame] | 1618 | |
| 1619 | auto reflectY = [=](const SkPoint& p) { |
| 1620 | return SkPoint{ p.fX, reflect ? -p.fY : p.fY }; |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 1621 | }; |
Brian Osman | 9d958b5 | 2018-11-13 12:46:56 -0500 | [diff] [blame] | 1622 | |
| 1623 | for (int i = 0; i < 8; ++i) { |
Brian Osman | f9aabff | 2018-11-13 16:11:38 -0500 | [diff] [blame] | 1624 | vertices.write(center + kOctagonOuter[i] * halfWidth, |
| 1625 | color, |
| 1626 | reflectY(kOctagonOuter[i]), |
| 1627 | circle.fOuterRadius, |
| 1628 | normInnerRadius, |
| 1629 | dashParams); |
Brian Osman | 9d958b5 | 2018-11-13 12:46:56 -0500 | [diff] [blame] | 1630 | } |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 1631 | |
| 1632 | // Compute the vertices of the inner octagon. |
Brian Osman | 9d958b5 | 2018-11-13 12:46:56 -0500 | [diff] [blame] | 1633 | for (int i = 0; i < 8; ++i) { |
Brian Osman | f9aabff | 2018-11-13 16:11:38 -0500 | [diff] [blame] | 1634 | vertices.write(center + kOctagonInner[i] * circle.fInnerRadius, |
| 1635 | color, |
| 1636 | reflectY(kOctagonInner[i]) * normInnerRadius, |
| 1637 | circle.fOuterRadius, |
| 1638 | normInnerRadius, |
| 1639 | dashParams); |
Brian Osman | 9d958b5 | 2018-11-13 12:46:56 -0500 | [diff] [blame] | 1640 | } |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 1641 | |
| 1642 | const uint16_t* primIndices = circle_type_to_indices(true); |
| 1643 | const int primIndexCount = circle_type_to_index_count(true); |
| 1644 | for (int i = 0; i < primIndexCount; ++i) { |
| 1645 | *indices++ = primIndices[i] + currStartVertex; |
| 1646 | } |
| 1647 | |
| 1648 | currStartVertex += circle_type_to_vert_count(true); |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 1649 | } |
| 1650 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 1651 | GrMesh* mesh = target->allocMesh(GrPrimitiveType::kTriangles); |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 1652 | mesh->setIndexed(std::move(indexBuffer), fIndexCount, firstIndex, 0, fVertCount - 1, |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 1653 | GrPrimitiveRestart::kNo); |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 1654 | mesh->setVertexData(std::move(vertexBuffer), firstVertex); |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 1655 | target->recordDraw(std::move(gp), mesh); |
| 1656 | } |
| 1657 | |
| 1658 | void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override { |
| 1659 | fHelper.executeDrawsAndUploads(this, flushState, chainBounds); |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 1660 | } |
| 1661 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 1662 | CombineResult onCombineIfPossible(GrOp* t, const GrCaps& caps) override { |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 1663 | ButtCapDashedCircleOp* that = t->cast<ButtCapDashedCircleOp>(); |
| 1664 | |
| 1665 | // can only represent 65535 unique vertices with 16-bit indices |
| 1666 | if (fVertCount + that->fVertCount > 65536) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 1667 | return CombineResult::kCannotCombine; |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 1668 | } |
| 1669 | |
| 1670 | if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 1671 | return CombineResult::kCannotCombine; |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 1672 | } |
| 1673 | |
| 1674 | if (fHelper.usesLocalCoords() && |
| 1675 | !fViewMatrixIfUsingLocalCoords.cheapEqualTo(that->fViewMatrixIfUsingLocalCoords)) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 1676 | return CombineResult::kCannotCombine; |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 1677 | } |
| 1678 | |
| 1679 | fCircles.push_back_n(that->fCircles.count(), that->fCircles.begin()); |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 1680 | fVertCount += that->fVertCount; |
| 1681 | fIndexCount += that->fIndexCount; |
Brian Osman | e3caf2d | 2018-11-21 13:48:36 -0500 | [diff] [blame] | 1682 | fWideColor |= that->fWideColor; |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 1683 | return CombineResult::kMerged; |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 1684 | } |
| 1685 | |
| 1686 | struct Circle { |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 1687 | SkPMColor4f fColor; |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 1688 | SkScalar fOuterRadius; |
| 1689 | SkScalar fInnerRadius; |
| 1690 | SkScalar fOnAngle; |
| 1691 | SkScalar fTotalAngle; |
| 1692 | SkScalar fStartAngle; |
| 1693 | SkScalar fPhaseAngle; |
| 1694 | SkRect fDevBounds; |
| 1695 | }; |
| 1696 | |
| 1697 | SkMatrix fViewMatrixIfUsingLocalCoords; |
| 1698 | Helper fHelper; |
| 1699 | SkSTArray<1, Circle, true> fCircles; |
| 1700 | int fVertCount; |
| 1701 | int fIndexCount; |
Brian Osman | e3caf2d | 2018-11-21 13:48:36 -0500 | [diff] [blame] | 1702 | bool fWideColor; |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 1703 | |
| 1704 | typedef GrMeshDrawOp INHERITED; |
| 1705 | }; |
| 1706 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1707 | /////////////////////////////////////////////////////////////////////////////// |
| 1708 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 1709 | class EllipseOp : public GrMeshDrawOp { |
| 1710 | private: |
| 1711 | using Helper = GrSimpleMeshDrawOpHelper; |
| 1712 | |
| 1713 | struct DeviceSpaceParams { |
| 1714 | SkPoint fCenter; |
| 1715 | SkScalar fXRadius; |
| 1716 | SkScalar fYRadius; |
| 1717 | SkScalar fInnerXRadius; |
| 1718 | SkScalar fInnerYRadius; |
| 1719 | }; |
| 1720 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1721 | public: |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 1722 | DEFINE_OP_CLASS_ID |
Robert Phillips | b493eeb | 2017-09-13 13:10:52 -0400 | [diff] [blame] | 1723 | |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 1724 | static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context, |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 1725 | GrPaint&& paint, |
| 1726 | const SkMatrix& viewMatrix, |
| 1727 | const SkRect& ellipse, |
| 1728 | const SkStrokeRec& stroke) { |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 1729 | DeviceSpaceParams params; |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 1730 | // do any matrix crunching before we reset the draw state for device coords |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 1731 | params.fCenter = SkPoint::Make(ellipse.centerX(), ellipse.centerY()); |
| 1732 | viewMatrix.mapPoints(¶ms.fCenter, 1); |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 1733 | SkScalar ellipseXRadius = SkScalarHalf(ellipse.width()); |
| 1734 | SkScalar ellipseYRadius = SkScalarHalf(ellipse.height()); |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 1735 | params.fXRadius = SkScalarAbs(viewMatrix[SkMatrix::kMScaleX] * ellipseXRadius + |
| 1736 | viewMatrix[SkMatrix::kMSkewX] * ellipseYRadius); |
| 1737 | params.fYRadius = SkScalarAbs(viewMatrix[SkMatrix::kMSkewY] * ellipseXRadius + |
| 1738 | viewMatrix[SkMatrix::kMScaleY] * ellipseYRadius); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1739 | |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 1740 | // do (potentially) anisotropic mapping of stroke |
| 1741 | SkVector scaledStroke; |
| 1742 | SkScalar strokeWidth = stroke.getWidth(); |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 1743 | scaledStroke.fX = SkScalarAbs( |
| 1744 | strokeWidth * (viewMatrix[SkMatrix::kMScaleX] + viewMatrix[SkMatrix::kMSkewY])); |
| 1745 | scaledStroke.fY = SkScalarAbs( |
| 1746 | strokeWidth * (viewMatrix[SkMatrix::kMSkewX] + viewMatrix[SkMatrix::kMScaleY])); |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 1747 | |
| 1748 | SkStrokeRec::Style style = stroke.getStyle(); |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 1749 | bool isStrokeOnly = |
| 1750 | SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style; |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 1751 | bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == style; |
| 1752 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 1753 | params.fInnerXRadius = 0; |
| 1754 | params.fInnerYRadius = 0; |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 1755 | if (hasStroke) { |
| 1756 | if (SkScalarNearlyZero(scaledStroke.length())) { |
| 1757 | scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf); |
| 1758 | } else { |
| 1759 | scaledStroke.scale(SK_ScalarHalf); |
| 1760 | } |
| 1761 | |
| 1762 | // we only handle thick strokes for near-circular ellipses |
| 1763 | if (scaledStroke.length() > SK_ScalarHalf && |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 1764 | (0.5f * params.fXRadius > params.fYRadius || |
| 1765 | 0.5f * params.fYRadius > params.fXRadius)) { |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 1766 | return nullptr; |
| 1767 | } |
| 1768 | |
| 1769 | // we don't handle it if curvature of the stroke is less than curvature of the ellipse |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 1770 | if (scaledStroke.fX * (params.fXRadius * params.fYRadius) < |
| 1771 | (scaledStroke.fY * scaledStroke.fY) * params.fXRadius || |
| 1772 | scaledStroke.fY * (params.fXRadius * params.fXRadius) < |
| 1773 | (scaledStroke.fX * scaledStroke.fX) * params.fYRadius) { |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 1774 | return nullptr; |
| 1775 | } |
| 1776 | |
| 1777 | // this is legit only if scale & translation (which should be the case at the moment) |
| 1778 | if (isStrokeOnly) { |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 1779 | params.fInnerXRadius = params.fXRadius - scaledStroke.fX; |
| 1780 | params.fInnerYRadius = params.fYRadius - scaledStroke.fY; |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 1781 | } |
| 1782 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 1783 | params.fXRadius += scaledStroke.fX; |
| 1784 | params.fYRadius += scaledStroke.fY; |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 1785 | } |
Jim Van Verth | 20ae25c | 2019-03-29 08:50:41 -0400 | [diff] [blame] | 1786 | |
| 1787 | // For large ovals with low precision floats, we fall back to the path renderer. |
| 1788 | // To compute the AA at the edge we divide by the gradient, which is clamped to a |
| 1789 | // minimum value to avoid divides by zero. With large ovals and low precision this |
| 1790 | // leads to blurring at the edge of the oval. |
| 1791 | const SkScalar kMaxOvalRadius = 16384; |
| 1792 | if (!context->priv().caps()->shaderCaps()->floatIs32Bits() && |
| 1793 | (params.fXRadius >= kMaxOvalRadius || params.fYRadius >= kMaxOvalRadius)) { |
| 1794 | return nullptr; |
| 1795 | } |
| 1796 | |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 1797 | return Helper::FactoryHelper<EllipseOp>(context, std::move(paint), viewMatrix, |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 1798 | params, stroke); |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 1799 | } |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 1800 | |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 1801 | EllipseOp(const Helper::MakeArgs& helperArgs, const SkPMColor4f& color, |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 1802 | const SkMatrix& viewMatrix, const DeviceSpaceParams& params, |
Brian Osman | 936fe7d | 2018-10-30 15:30:35 -0400 | [diff] [blame] | 1803 | const SkStrokeRec& stroke) |
Jim Van Verth | 20ae25c | 2019-03-29 08:50:41 -0400 | [diff] [blame] | 1804 | : INHERITED(ClassID()) |
| 1805 | , fHelper(helperArgs, GrAAType::kCoverage) |
| 1806 | , fUseScale(false) { |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 1807 | SkStrokeRec::Style style = stroke.getStyle(); |
| 1808 | bool isStrokeOnly = |
| 1809 | SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style; |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 1810 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 1811 | fEllipses.emplace_back(Ellipse{color, params.fXRadius, params.fYRadius, |
| 1812 | params.fInnerXRadius, params.fInnerYRadius, |
| 1813 | SkRect::MakeLTRB(params.fCenter.fX - params.fXRadius, |
| 1814 | params.fCenter.fY - params.fYRadius, |
| 1815 | params.fCenter.fX + params.fXRadius, |
| 1816 | params.fCenter.fY + params.fYRadius)}); |
| 1817 | |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 1818 | this->setBounds(fEllipses.back().fDevBounds, HasAABloat::kYes, IsZeroArea::kNo); |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 1819 | |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 1820 | // Outset bounds to include half-pixel width antialiasing. |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 1821 | fEllipses[0].fDevBounds.outset(SK_ScalarHalf, SK_ScalarHalf); |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 1822 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 1823 | fStroked = isStrokeOnly && params.fInnerXRadius > 0 && params.fInnerYRadius > 0; |
| 1824 | fViewMatrixIfUsingLocalCoords = viewMatrix; |
bsalomon | cdaa97b | 2016-03-08 08:30:14 -0800 | [diff] [blame] | 1825 | } |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1826 | |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 1827 | const char* name() const override { return "EllipseOp"; } |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1828 | |
Chris Dalton | 1706cbf | 2019-05-21 19:35:29 -0600 | [diff] [blame^] | 1829 | void visitProxies(const VisitProxyFunc& func) const override { |
Robert Phillips | b493eeb | 2017-09-13 13:10:52 -0400 | [diff] [blame] | 1830 | fHelper.visitProxies(func); |
| 1831 | } |
| 1832 | |
Brian Osman | 9a390ac | 2018-11-12 09:47:48 -0500 | [diff] [blame] | 1833 | #ifdef SK_DEBUG |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame] | 1834 | SkString dumpInfo() const override { |
| 1835 | SkString string; |
| 1836 | string.appendf("Stroked: %d\n", fStroked); |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 1837 | for (const auto& geo : fEllipses) { |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 1838 | string.appendf( |
| 1839 | "Color: 0x%08x Rect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], " |
| 1840 | "XRad: %.2f, YRad: %.2f, InnerXRad: %.2f, InnerYRad: %.2f\n", |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 1841 | geo.fColor.toBytes_RGBA(), geo.fDevBounds.fLeft, geo.fDevBounds.fTop, |
Brian Osman | 1be2b7c | 2018-10-29 16:07:15 -0400 | [diff] [blame] | 1842 | geo.fDevBounds.fRight, geo.fDevBounds.fBottom, geo.fXRadius, geo.fYRadius, |
| 1843 | geo.fInnerXRadius, geo.fInnerYRadius); |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame] | 1844 | } |
Brian Salomon | 82dfd3d | 2017-06-14 12:30:35 -0400 | [diff] [blame] | 1845 | string += fHelper.dumpInfo(); |
| 1846 | string += INHERITED::dumpInfo(); |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame] | 1847 | return string; |
| 1848 | } |
Brian Osman | 9a390ac | 2018-11-12 09:47:48 -0500 | [diff] [blame] | 1849 | #endif |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame] | 1850 | |
Brian Osman | 5ced0bf | 2019-03-15 10:15:29 -0400 | [diff] [blame] | 1851 | GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip, |
| 1852 | GrFSAAType fsaaType, GrClampType clampType) override { |
Jim Van Verth | 20ae25c | 2019-03-29 08:50:41 -0400 | [diff] [blame] | 1853 | fUseScale = !caps.shaderCaps()->floatIs32Bits() && |
| 1854 | !caps.shaderCaps()->hasLowFragmentPrecision(); |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 1855 | SkPMColor4f* color = &fEllipses.front().fColor; |
Brian Osman | 5ced0bf | 2019-03-15 10:15:29 -0400 | [diff] [blame] | 1856 | return fHelper.finalizeProcessors(caps, clip, fsaaType, clampType, |
Brian Osman | 8fa7ab4 | 2019-03-18 10:22:42 -0400 | [diff] [blame] | 1857 | GrProcessorAnalysisCoverage::kSingleChannel, color, |
| 1858 | &fWideColor); |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 1859 | } |
| 1860 | |
| 1861 | FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); } |
| 1862 | |
bsalomon | e46f9fe | 2015-08-18 06:05:14 -0700 | [diff] [blame] | 1863 | private: |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 1864 | void onPrepareDraws(Target* target) override { |
bsalomon | cdaa97b | 2016-03-08 08:30:14 -0800 | [diff] [blame] | 1865 | SkMatrix localMatrix; |
| 1866 | if (!fViewMatrixIfUsingLocalCoords.invert(&localMatrix)) { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1867 | return; |
| 1868 | } |
| 1869 | |
| 1870 | // Setup geometry processor |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 1871 | sk_sp<GrGeometryProcessor> gp(new EllipseGeometryProcessor(fStroked, fWideColor, fUseScale, |
| 1872 | localMatrix)); |
Brian Osman | 9d958b5 | 2018-11-13 12:46:56 -0500 | [diff] [blame] | 1873 | QuadHelper helper(target, gp->vertexStride(), fEllipses.count()); |
Brian Osman | f9aabff | 2018-11-13 16:11:38 -0500 | [diff] [blame] | 1874 | GrVertexWriter verts{helper.vertices()}; |
| 1875 | if (!verts.fPtr) { |
joshualitt | 4b31de8 | 2015-03-05 14:33:41 -0800 | [diff] [blame] | 1876 | return; |
| 1877 | } |
| 1878 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 1879 | for (const auto& ellipse : fEllipses) { |
Brian Osman | e3caf2d | 2018-11-21 13:48:36 -0500 | [diff] [blame] | 1880 | GrVertexColor color(ellipse.fColor, fWideColor); |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 1881 | SkScalar xRadius = ellipse.fXRadius; |
| 1882 | SkScalar yRadius = ellipse.fYRadius; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1883 | |
| 1884 | // Compute the reciprocals of the radii here to save time in the shader |
Brian Osman | 9d958b5 | 2018-11-13 12:46:56 -0500 | [diff] [blame] | 1885 | struct { float xOuter, yOuter, xInner, yInner; } invRadii = { |
| 1886 | SkScalarInvert(xRadius), |
| 1887 | SkScalarInvert(yRadius), |
| 1888 | SkScalarInvert(ellipse.fInnerXRadius), |
| 1889 | SkScalarInvert(ellipse.fInnerYRadius) |
| 1890 | }; |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 1891 | SkScalar xMaxOffset = xRadius + SK_ScalarHalf; |
| 1892 | SkScalar yMaxOffset = yRadius + SK_ScalarHalf; |
vjiaoblack | 977996d | 2016-06-30 12:20:54 -0700 | [diff] [blame] | 1893 | |
Jim Van Verth | b1b87d9 | 2018-06-28 10:46:05 -0400 | [diff] [blame] | 1894 | if (!fStroked) { |
| 1895 | // For filled ellipses we map a unit circle in the vertex attributes rather than |
| 1896 | // computing an ellipse and modifying that distance, so we normalize to 1 |
| 1897 | xMaxOffset /= xRadius; |
| 1898 | yMaxOffset /= yRadius; |
| 1899 | } |
| 1900 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1901 | // The inner radius in the vertex data must be specified in normalized space. |
Brian Osman | 2b6e390 | 2018-11-21 15:29:43 -0500 | [diff] [blame] | 1902 | verts.writeQuad(GrVertexWriter::TriStripFromRect(ellipse.fDevBounds), |
| 1903 | color, |
| 1904 | origin_centered_tri_strip(xMaxOffset, yMaxOffset), |
Jim Van Verth | 20ae25c | 2019-03-29 08:50:41 -0400 | [diff] [blame] | 1905 | GrVertexWriter::If(fUseScale, SkTMax(xRadius, yRadius)), |
Brian Osman | 2b6e390 | 2018-11-21 15:29:43 -0500 | [diff] [blame] | 1906 | invRadii); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1907 | } |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 1908 | helper.recordDraw(target, std::move(gp)); |
| 1909 | } |
| 1910 | |
| 1911 | void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override { |
| 1912 | fHelper.executeDrawsAndUploads(this, flushState, chainBounds); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1913 | } |
| 1914 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 1915 | CombineResult onCombineIfPossible(GrOp* t, const GrCaps& caps) override { |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 1916 | EllipseOp* that = t->cast<EllipseOp>(); |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 1917 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 1918 | if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 1919 | return CombineResult::kCannotCombine; |
joshualitt | 8cab9a7 | 2015-07-16 09:13:50 -0700 | [diff] [blame] | 1920 | } |
| 1921 | |
bsalomon | cdaa97b | 2016-03-08 08:30:14 -0800 | [diff] [blame] | 1922 | if (fStroked != that->fStroked) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 1923 | return CombineResult::kCannotCombine; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1924 | } |
| 1925 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 1926 | if (fHelper.usesLocalCoords() && |
| 1927 | !fViewMatrixIfUsingLocalCoords.cheapEqualTo(that->fViewMatrixIfUsingLocalCoords)) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 1928 | return CombineResult::kCannotCombine; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1929 | } |
| 1930 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 1931 | fEllipses.push_back_n(that->fEllipses.count(), that->fEllipses.begin()); |
Brian Osman | e3caf2d | 2018-11-21 13:48:36 -0500 | [diff] [blame] | 1932 | fWideColor |= that->fWideColor; |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 1933 | return CombineResult::kMerged; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1934 | } |
| 1935 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 1936 | struct Ellipse { |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 1937 | SkPMColor4f fColor; |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 1938 | SkScalar fXRadius; |
| 1939 | SkScalar fYRadius; |
| 1940 | SkScalar fInnerXRadius; |
| 1941 | SkScalar fInnerYRadius; |
| 1942 | SkRect fDevBounds; |
| 1943 | }; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1944 | |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 1945 | SkMatrix fViewMatrixIfUsingLocalCoords; |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 1946 | Helper fHelper; |
| 1947 | bool fStroked; |
Brian Osman | e3caf2d | 2018-11-21 13:48:36 -0500 | [diff] [blame] | 1948 | bool fWideColor; |
Jim Van Verth | 20ae25c | 2019-03-29 08:50:41 -0400 | [diff] [blame] | 1949 | bool fUseScale; |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 1950 | SkSTArray<1, Ellipse, true> fEllipses; |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 1951 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 1952 | typedef GrMeshDrawOp INHERITED; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1953 | }; |
| 1954 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1955 | ///////////////////////////////////////////////////////////////////////////////////////////////// |
| 1956 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 1957 | class DIEllipseOp : public GrMeshDrawOp { |
| 1958 | private: |
| 1959 | using Helper = GrSimpleMeshDrawOpHelper; |
| 1960 | |
| 1961 | struct DeviceSpaceParams { |
| 1962 | SkPoint fCenter; |
| 1963 | SkScalar fXRadius; |
| 1964 | SkScalar fYRadius; |
| 1965 | SkScalar fInnerXRadius; |
| 1966 | SkScalar fInnerYRadius; |
| 1967 | DIEllipseStyle fStyle; |
| 1968 | }; |
| 1969 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1970 | public: |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 1971 | DEFINE_OP_CLASS_ID |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 1972 | |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 1973 | static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context, |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 1974 | GrPaint&& paint, |
| 1975 | const SkMatrix& viewMatrix, |
| 1976 | const SkRect& ellipse, |
| 1977 | const SkStrokeRec& stroke) { |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 1978 | DeviceSpaceParams params; |
| 1979 | params.fCenter = SkPoint::Make(ellipse.centerX(), ellipse.centerY()); |
| 1980 | params.fXRadius = SkScalarHalf(ellipse.width()); |
| 1981 | params.fYRadius = SkScalarHalf(ellipse.height()); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 1982 | |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 1983 | SkStrokeRec::Style style = stroke.getStyle(); |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 1984 | params.fStyle = (SkStrokeRec::kStroke_Style == style) |
| 1985 | ? DIEllipseStyle::kStroke |
| 1986 | : (SkStrokeRec::kHairline_Style == style) |
| 1987 | ? DIEllipseStyle::kHairline |
| 1988 | : DIEllipseStyle::kFill; |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 1989 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 1990 | params.fInnerXRadius = 0; |
| 1991 | params.fInnerYRadius = 0; |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 1992 | if (SkStrokeRec::kFill_Style != style && SkStrokeRec::kHairline_Style != style) { |
| 1993 | SkScalar strokeWidth = stroke.getWidth(); |
| 1994 | |
| 1995 | if (SkScalarNearlyZero(strokeWidth)) { |
| 1996 | strokeWidth = SK_ScalarHalf; |
| 1997 | } else { |
| 1998 | strokeWidth *= SK_ScalarHalf; |
| 1999 | } |
| 2000 | |
| 2001 | // we only handle thick strokes for near-circular ellipses |
| 2002 | if (strokeWidth > SK_ScalarHalf && |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2003 | (SK_ScalarHalf * params.fXRadius > params.fYRadius || |
| 2004 | SK_ScalarHalf * params.fYRadius > params.fXRadius)) { |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 2005 | return nullptr; |
| 2006 | } |
| 2007 | |
| 2008 | // we don't handle it if curvature of the stroke is less than curvature of the ellipse |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2009 | if (strokeWidth * (params.fYRadius * params.fYRadius) < |
| 2010 | (strokeWidth * strokeWidth) * params.fXRadius) { |
| 2011 | return nullptr; |
| 2012 | } |
| 2013 | if (strokeWidth * (params.fXRadius * params.fXRadius) < |
| 2014 | (strokeWidth * strokeWidth) * params.fYRadius) { |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 2015 | return nullptr; |
| 2016 | } |
| 2017 | |
| 2018 | // set inner radius (if needed) |
| 2019 | if (SkStrokeRec::kStroke_Style == style) { |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2020 | params.fInnerXRadius = params.fXRadius - strokeWidth; |
| 2021 | params.fInnerYRadius = params.fYRadius - strokeWidth; |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 2022 | } |
| 2023 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2024 | params.fXRadius += strokeWidth; |
| 2025 | params.fYRadius += strokeWidth; |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 2026 | } |
Jim Van Verth | 20ae25c | 2019-03-29 08:50:41 -0400 | [diff] [blame] | 2027 | |
| 2028 | // For large ovals with low precision floats, we fall back to the path renderer. |
| 2029 | // To compute the AA at the edge we divide by the gradient, which is clamped to a |
| 2030 | // minimum value to avoid divides by zero. With large ovals and low precision this |
| 2031 | // leads to blurring at the edge of the oval. |
| 2032 | const SkScalar kMaxOvalRadius = 16384; |
| 2033 | if (!context->priv().caps()->shaderCaps()->floatIs32Bits() && |
| 2034 | (params.fXRadius >= kMaxOvalRadius || params.fYRadius >= kMaxOvalRadius)) { |
| 2035 | return nullptr; |
| 2036 | } |
| 2037 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2038 | if (DIEllipseStyle::kStroke == params.fStyle && |
| 2039 | (params.fInnerXRadius <= 0 || params.fInnerYRadius <= 0)) { |
| 2040 | params.fStyle = DIEllipseStyle::kFill; |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 2041 | } |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 2042 | return Helper::FactoryHelper<DIEllipseOp>(context, std::move(paint), params, viewMatrix); |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2043 | } |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 2044 | |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 2045 | DIEllipseOp(Helper::MakeArgs& helperArgs, const SkPMColor4f& color, |
Brian Osman | 936fe7d | 2018-10-30 15:30:35 -0400 | [diff] [blame] | 2046 | const DeviceSpaceParams& params, const SkMatrix& viewMatrix) |
Jim Van Verth | 20ae25c | 2019-03-29 08:50:41 -0400 | [diff] [blame] | 2047 | : INHERITED(ClassID()) |
| 2048 | , fHelper(helperArgs, GrAAType::kCoverage) |
| 2049 | , fUseScale(false) { |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 2050 | // This expands the outer rect so that after CTM we end up with a half-pixel border |
| 2051 | SkScalar a = viewMatrix[SkMatrix::kMScaleX]; |
| 2052 | SkScalar b = viewMatrix[SkMatrix::kMSkewX]; |
| 2053 | SkScalar c = viewMatrix[SkMatrix::kMSkewY]; |
| 2054 | SkScalar d = viewMatrix[SkMatrix::kMScaleY]; |
| 2055 | SkScalar geoDx = SK_ScalarHalf / SkScalarSqrt(a * a + c * c); |
| 2056 | SkScalar geoDy = SK_ScalarHalf / SkScalarSqrt(b * b + d * d); |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 2057 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2058 | fEllipses.emplace_back( |
| 2059 | Ellipse{viewMatrix, color, params.fXRadius, params.fYRadius, params.fInnerXRadius, |
| 2060 | params.fInnerYRadius, geoDx, geoDy, params.fStyle, |
| 2061 | SkRect::MakeLTRB(params.fCenter.fX - params.fXRadius - geoDx, |
| 2062 | params.fCenter.fY - params.fYRadius - geoDy, |
| 2063 | params.fCenter.fX + params.fXRadius + geoDx, |
| 2064 | params.fCenter.fY + params.fYRadius + geoDy)}); |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 2065 | this->setTransformedBounds(fEllipses[0].fBounds, viewMatrix, HasAABloat::kYes, |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2066 | IsZeroArea::kNo); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2067 | } |
| 2068 | |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 2069 | const char* name() const override { return "DIEllipseOp"; } |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2070 | |
Chris Dalton | 1706cbf | 2019-05-21 19:35:29 -0600 | [diff] [blame^] | 2071 | void visitProxies(const VisitProxyFunc& func) const override { |
Robert Phillips | b493eeb | 2017-09-13 13:10:52 -0400 | [diff] [blame] | 2072 | fHelper.visitProxies(func); |
| 2073 | } |
| 2074 | |
Brian Osman | 9a390ac | 2018-11-12 09:47:48 -0500 | [diff] [blame] | 2075 | #ifdef SK_DEBUG |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame] | 2076 | SkString dumpInfo() const override { |
| 2077 | SkString string; |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2078 | for (const auto& geo : fEllipses) { |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 2079 | string.appendf( |
| 2080 | "Color: 0x%08x Rect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], XRad: %.2f, " |
| 2081 | "YRad: %.2f, InnerXRad: %.2f, InnerYRad: %.2f, GeoDX: %.2f, " |
| 2082 | "GeoDY: %.2f\n", |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 2083 | geo.fColor.toBytes_RGBA(), geo.fBounds.fLeft, geo.fBounds.fTop, |
Brian Osman | 1be2b7c | 2018-10-29 16:07:15 -0400 | [diff] [blame] | 2084 | geo.fBounds.fRight, geo.fBounds.fBottom, geo.fXRadius, geo.fYRadius, |
| 2085 | geo.fInnerXRadius, geo.fInnerYRadius, geo.fGeoDx, geo.fGeoDy); |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame] | 2086 | } |
Brian Salomon | 82dfd3d | 2017-06-14 12:30:35 -0400 | [diff] [blame] | 2087 | string += fHelper.dumpInfo(); |
| 2088 | string += INHERITED::dumpInfo(); |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame] | 2089 | return string; |
| 2090 | } |
Brian Osman | 9a390ac | 2018-11-12 09:47:48 -0500 | [diff] [blame] | 2091 | #endif |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame] | 2092 | |
Brian Osman | 5ced0bf | 2019-03-15 10:15:29 -0400 | [diff] [blame] | 2093 | GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip, |
| 2094 | GrFSAAType fsaaType, GrClampType clampType) override { |
Jim Van Verth | 20ae25c | 2019-03-29 08:50:41 -0400 | [diff] [blame] | 2095 | fUseScale = !caps.shaderCaps()->floatIs32Bits() && |
| 2096 | !caps.shaderCaps()->hasLowFragmentPrecision(); |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 2097 | SkPMColor4f* color = &fEllipses.front().fColor; |
Brian Osman | 5ced0bf | 2019-03-15 10:15:29 -0400 | [diff] [blame] | 2098 | return fHelper.finalizeProcessors(caps, clip, fsaaType, clampType, |
Brian Osman | 8fa7ab4 | 2019-03-18 10:22:42 -0400 | [diff] [blame] | 2099 | GrProcessorAnalysisCoverage::kSingleChannel, color, |
| 2100 | &fWideColor); |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2101 | } |
| 2102 | |
| 2103 | FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); } |
| 2104 | |
bsalomon | e46f9fe | 2015-08-18 06:05:14 -0700 | [diff] [blame] | 2105 | private: |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 2106 | void onPrepareDraws(Target* target) override { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2107 | // Setup geometry processor |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 2108 | sk_sp<GrGeometryProcessor> gp( |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 2109 | new DIEllipseGeometryProcessor(fWideColor, fUseScale, this->viewMatrix(), |
Jim Van Verth | 20ae25c | 2019-03-29 08:50:41 -0400 | [diff] [blame] | 2110 | this->style())); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2111 | |
Brian Osman | 9d958b5 | 2018-11-13 12:46:56 -0500 | [diff] [blame] | 2112 | QuadHelper helper(target, gp->vertexStride(), fEllipses.count()); |
Brian Osman | f9aabff | 2018-11-13 16:11:38 -0500 | [diff] [blame] | 2113 | GrVertexWriter verts{helper.vertices()}; |
| 2114 | if (!verts.fPtr) { |
joshualitt | 4b31de8 | 2015-03-05 14:33:41 -0800 | [diff] [blame] | 2115 | return; |
| 2116 | } |
| 2117 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2118 | for (const auto& ellipse : fEllipses) { |
Brian Osman | e3caf2d | 2018-11-21 13:48:36 -0500 | [diff] [blame] | 2119 | GrVertexColor color(ellipse.fColor, fWideColor); |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2120 | SkScalar xRadius = ellipse.fXRadius; |
| 2121 | SkScalar yRadius = ellipse.fYRadius; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2122 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2123 | // This adjusts the "radius" to include the half-pixel border |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2124 | SkScalar offsetDx = ellipse.fGeoDx / xRadius; |
| 2125 | SkScalar offsetDy = ellipse.fGeoDy / yRadius; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2126 | |
Brian Osman | 9d958b5 | 2018-11-13 12:46:56 -0500 | [diff] [blame] | 2127 | // By default, constructed so that inner offset is (0, 0) for all points |
| 2128 | SkScalar innerRatioX = -offsetDx; |
| 2129 | SkScalar innerRatioY = -offsetDy; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2130 | |
Brian Osman | 9d958b5 | 2018-11-13 12:46:56 -0500 | [diff] [blame] | 2131 | // ... unless we're stroked |
Greg Daniel | 75a1302 | 2018-04-04 08:59:20 -0400 | [diff] [blame] | 2132 | if (DIEllipseStyle::kStroke == this->style()) { |
Brian Osman | 9d958b5 | 2018-11-13 12:46:56 -0500 | [diff] [blame] | 2133 | innerRatioX = xRadius / ellipse.fInnerXRadius; |
| 2134 | innerRatioY = yRadius / ellipse.fInnerYRadius; |
Greg Daniel | 75a1302 | 2018-04-04 08:59:20 -0400 | [diff] [blame] | 2135 | } |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2136 | |
Brian Osman | 2b6e390 | 2018-11-21 15:29:43 -0500 | [diff] [blame] | 2137 | verts.writeQuad(GrVertexWriter::TriStripFromRect(ellipse.fBounds), |
| 2138 | color, |
| 2139 | origin_centered_tri_strip(1.0f + offsetDx, 1.0f + offsetDy), |
Jim Van Verth | 20ae25c | 2019-03-29 08:50:41 -0400 | [diff] [blame] | 2140 | GrVertexWriter::If(fUseScale, SkTMax(xRadius, yRadius)), |
Brian Osman | 2b6e390 | 2018-11-21 15:29:43 -0500 | [diff] [blame] | 2141 | origin_centered_tri_strip(innerRatioX + offsetDx, |
| 2142 | innerRatioY + offsetDy)); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2143 | } |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 2144 | helper.recordDraw(target, std::move(gp)); |
| 2145 | } |
| 2146 | |
| 2147 | void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override { |
| 2148 | fHelper.executeDrawsAndUploads(this, flushState, chainBounds); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2149 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 2150 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 2151 | CombineResult onCombineIfPossible(GrOp* t, const GrCaps& caps) override { |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 2152 | DIEllipseOp* that = t->cast<DIEllipseOp>(); |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2153 | if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 2154 | return CombineResult::kCannotCombine; |
joshualitt | 8cab9a7 | 2015-07-16 09:13:50 -0700 | [diff] [blame] | 2155 | } |
| 2156 | |
bsalomon | cdaa97b | 2016-03-08 08:30:14 -0800 | [diff] [blame] | 2157 | if (this->style() != that->style()) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 2158 | return CombineResult::kCannotCombine; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2159 | } |
| 2160 | |
joshualitt | d96a67b | 2015-05-05 14:09:05 -0700 | [diff] [blame] | 2161 | // TODO rewrite to allow positioning on CPU |
| 2162 | if (!this->viewMatrix().cheapEqualTo(that->viewMatrix())) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 2163 | return CombineResult::kCannotCombine; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2164 | } |
| 2165 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2166 | fEllipses.push_back_n(that->fEllipses.count(), that->fEllipses.begin()); |
Brian Osman | e3caf2d | 2018-11-21 13:48:36 -0500 | [diff] [blame] | 2167 | fWideColor |= that->fWideColor; |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 2168 | return CombineResult::kMerged; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2169 | } |
| 2170 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2171 | const SkMatrix& viewMatrix() const { return fEllipses[0].fViewMatrix; } |
| 2172 | DIEllipseStyle style() const { return fEllipses[0].fStyle; } |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2173 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2174 | struct Ellipse { |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 2175 | SkMatrix fViewMatrix; |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 2176 | SkPMColor4f fColor; |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 2177 | SkScalar fXRadius; |
| 2178 | SkScalar fYRadius; |
| 2179 | SkScalar fInnerXRadius; |
| 2180 | SkScalar fInnerYRadius; |
| 2181 | SkScalar fGeoDx; |
| 2182 | SkScalar fGeoDy; |
| 2183 | DIEllipseStyle fStyle; |
| 2184 | SkRect fBounds; |
| 2185 | }; |
| 2186 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2187 | Helper fHelper; |
Brian Osman | e3caf2d | 2018-11-21 13:48:36 -0500 | [diff] [blame] | 2188 | bool fWideColor; |
Jim Van Verth | 20ae25c | 2019-03-29 08:50:41 -0400 | [diff] [blame] | 2189 | bool fUseScale; |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2190 | SkSTArray<1, Ellipse, true> fEllipses; |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 2191 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2192 | typedef GrMeshDrawOp INHERITED; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2193 | }; |
| 2194 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 2195 | /////////////////////////////////////////////////////////////////////////////// |
| 2196 | |
Mike Klein | fc6c37b | 2016-09-27 09:34:10 -0400 | [diff] [blame] | 2197 | // We have three possible cases for geometry for a roundrect. |
jvanverth | c3d0e42 | 2016-08-25 08:12:35 -0700 | [diff] [blame] | 2198 | // |
| 2199 | // In the case of a normal fill or a stroke, we draw the roundrect as a 9-patch: |
| 2200 | // ____________ |
| 2201 | // |_|________|_| |
| 2202 | // | | | | |
| 2203 | // | | | | |
| 2204 | // | | | | |
| 2205 | // |_|________|_| |
| 2206 | // |_|________|_| |
| 2207 | // |
| 2208 | // For strokes, we don't draw the center quad. |
| 2209 | // |
| 2210 | // For circular roundrects, in the case where the stroke width is greater than twice |
| 2211 | // the corner radius (overstroke), we add additional geometry to mark out the rectangle |
jvanverth | 6a39761 | 2016-08-26 08:15:33 -0700 | [diff] [blame] | 2212 | // in the center. The shared vertices are duplicated so we can set a different outer radius |
| 2213 | // for the fill calculation. |
jvanverth | c3d0e42 | 2016-08-25 08:12:35 -0700 | [diff] [blame] | 2214 | // ____________ |
| 2215 | // |_|________|_| |
| 2216 | // | |\ ____ /| | |
| 2217 | // | | | | | | |
| 2218 | // | | |____| | | |
| 2219 | // |_|/______\|_| |
| 2220 | // |_|________|_| |
| 2221 | // |
| 2222 | // 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] | 2223 | // |
| 2224 | // For filled rrects that need to provide a distance vector we resuse the overstroke |
| 2225 | // geometry but make the inner rect degenerate (either a point or a horizontal or |
| 2226 | // vertical line). |
jvanverth | c3d0e42 | 2016-08-25 08:12:35 -0700 | [diff] [blame] | 2227 | |
jvanverth | 84839f6 | 2016-08-29 10:16:40 -0700 | [diff] [blame] | 2228 | static const uint16_t gOverstrokeRRectIndices[] = { |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 2229 | // clang-format off |
| 2230 | // overstroke quads |
| 2231 | // we place this at the beginning so that we can skip these indices when rendering normally |
| 2232 | 16, 17, 19, 16, 19, 18, |
| 2233 | 19, 17, 23, 19, 23, 21, |
| 2234 | 21, 23, 22, 21, 22, 20, |
| 2235 | 22, 16, 18, 22, 18, 20, |
jvanverth | c3d0e42 | 2016-08-25 08:12:35 -0700 | [diff] [blame] | 2236 | |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 2237 | // corners |
| 2238 | 0, 1, 5, 0, 5, 4, |
| 2239 | 2, 3, 7, 2, 7, 6, |
| 2240 | 8, 9, 13, 8, 13, 12, |
| 2241 | 10, 11, 15, 10, 15, 14, |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 2242 | |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 2243 | // edges |
| 2244 | 1, 2, 6, 1, 6, 5, |
| 2245 | 4, 5, 9, 4, 9, 8, |
| 2246 | 6, 7, 11, 6, 11, 10, |
| 2247 | 9, 10, 14, 9, 14, 13, |
skia.committer@gmail.com | 2cf444f | 2013-04-26 07:00:58 +0000 | [diff] [blame] | 2248 | |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 2249 | // center |
| 2250 | // we place this at the end so that we can ignore these indices when not rendering as filled |
| 2251 | 5, 6, 10, 5, 10, 9, |
| 2252 | // clang-format on |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 2253 | }; |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 2254 | |
jvanverth | 84839f6 | 2016-08-29 10:16:40 -0700 | [diff] [blame] | 2255 | // fill and standard stroke indices skip the overstroke "ring" |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 2256 | static const uint16_t* gStandardRRectIndices = gOverstrokeRRectIndices + 6 * 4; |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 2257 | |
jvanverth | 84839f6 | 2016-08-29 10:16:40 -0700 | [diff] [blame] | 2258 | // overstroke count is arraysize minus the center indices |
| 2259 | static const int kIndicesPerOverstrokeRRect = SK_ARRAY_COUNT(gOverstrokeRRectIndices) - 6; |
| 2260 | // fill count skips overstroke indices and includes center |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 2261 | static const int kIndicesPerFillRRect = kIndicesPerOverstrokeRRect - 6 * 4 + 6; |
jvanverth | 84839f6 | 2016-08-29 10:16:40 -0700 | [diff] [blame] | 2262 | // stroke count is fill count minus center indices |
jvanverth | c3d0e42 | 2016-08-25 08:12:35 -0700 | [diff] [blame] | 2263 | static const int kIndicesPerStrokeRRect = kIndicesPerFillRRect - 6; |
| 2264 | static const int kVertsPerStandardRRect = 16; |
jvanverth | 6a39761 | 2016-08-26 08:15:33 -0700 | [diff] [blame] | 2265 | static const int kVertsPerOverstrokeRRect = 24; |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 2266 | |
jvanverth | c3d0e42 | 2016-08-25 08:12:35 -0700 | [diff] [blame] | 2267 | enum RRectType { |
| 2268 | kFill_RRectType, |
| 2269 | kStroke_RRectType, |
jvanverth | 84839f6 | 2016-08-29 10:16:40 -0700 | [diff] [blame] | 2270 | kOverstroke_RRectType, |
jvanverth | c3d0e42 | 2016-08-25 08:12:35 -0700 | [diff] [blame] | 2271 | }; |
| 2272 | |
jvanverth | 84839f6 | 2016-08-29 10:16:40 -0700 | [diff] [blame] | 2273 | static int rrect_type_to_vert_count(RRectType type) { |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 2274 | switch (type) { |
| 2275 | case kFill_RRectType: |
| 2276 | case kStroke_RRectType: |
| 2277 | return kVertsPerStandardRRect; |
| 2278 | case kOverstroke_RRectType: |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 2279 | return kVertsPerOverstrokeRRect; |
| 2280 | } |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 2281 | SK_ABORT("Invalid type"); |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 2282 | return 0; |
jvanverth | 84839f6 | 2016-08-29 10:16:40 -0700 | [diff] [blame] | 2283 | } |
| 2284 | |
| 2285 | static int rrect_type_to_index_count(RRectType type) { |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 2286 | switch (type) { |
| 2287 | case kFill_RRectType: |
| 2288 | return kIndicesPerFillRRect; |
| 2289 | case kStroke_RRectType: |
| 2290 | return kIndicesPerStrokeRRect; |
| 2291 | case kOverstroke_RRectType: |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 2292 | return kIndicesPerOverstrokeRRect; |
| 2293 | } |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 2294 | SK_ABORT("Invalid type"); |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 2295 | return 0; |
jvanverth | 84839f6 | 2016-08-29 10:16:40 -0700 | [diff] [blame] | 2296 | } |
| 2297 | |
| 2298 | static const uint16_t* rrect_type_to_indices(RRectType type) { |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 2299 | switch (type) { |
| 2300 | case kFill_RRectType: |
| 2301 | case kStroke_RRectType: |
| 2302 | return gStandardRRectIndices; |
| 2303 | case kOverstroke_RRectType: |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 2304 | return gOverstrokeRRectIndices; |
| 2305 | } |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 2306 | SK_ABORT("Invalid type"); |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 2307 | return 0; |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 2308 | } |
| 2309 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2310 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2311 | |
Robert Phillips | 79839d4 | 2016-10-06 15:03:34 -0400 | [diff] [blame] | 2312 | // For distance computations in the interior of filled rrects we: |
| 2313 | // |
| 2314 | // add a interior degenerate (point or line) rect |
| 2315 | // each vertex of that rect gets -outerRad as its radius |
| 2316 | // this makes the computation of the distance to the outer edge be negative |
| 2317 | // negative values are caught and then handled differently in the GP's onEmitCode |
| 2318 | // each vertex is also given the normalized x & y distance from the interior rect's edge |
| 2319 | // the GP takes the min of those depths +1 to get the normalized distance to the outer edge |
| 2320 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2321 | class CircularRRectOp : public GrMeshDrawOp { |
| 2322 | private: |
| 2323 | using Helper = GrSimpleMeshDrawOpHelper; |
| 2324 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2325 | public: |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 2326 | DEFINE_OP_CLASS_ID |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 2327 | |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 2328 | // A devStrokeWidth <= 0 indicates a fill only. If devStrokeWidth > 0 then strokeOnly indicates |
| 2329 | // whether the rrect is only stroked or stroked and filled. |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 2330 | static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context, |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 2331 | GrPaint&& paint, |
| 2332 | const SkMatrix& viewMatrix, |
| 2333 | const SkRect& devRect, |
| 2334 | float devRadius, |
| 2335 | float devStrokeWidth, |
| 2336 | bool strokeOnly) { |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 2337 | return Helper::FactoryHelper<CircularRRectOp>(context, std::move(paint), viewMatrix, |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 2338 | devRect, devRadius, |
| 2339 | devStrokeWidth, strokeOnly); |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2340 | } |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 2341 | CircularRRectOp(Helper::MakeArgs& helperArgs, const SkPMColor4f& color, |
Brian Osman | 936fe7d | 2018-10-30 15:30:35 -0400 | [diff] [blame] | 2342 | const SkMatrix& viewMatrix, const SkRect& devRect, float devRadius, |
| 2343 | float devStrokeWidth, bool strokeOnly) |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2344 | : INHERITED(ClassID()) |
| 2345 | , fViewMatrixIfUsingLocalCoords(viewMatrix) |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 2346 | , fHelper(helperArgs, GrAAType::kCoverage) { |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 2347 | SkRect bounds = devRect; |
| 2348 | SkASSERT(!(devStrokeWidth <= 0 && strokeOnly)); |
| 2349 | SkScalar innerRadius = 0.0f; |
| 2350 | SkScalar outerRadius = devRadius; |
| 2351 | SkScalar halfWidth = 0; |
jvanverth | 84839f6 | 2016-08-29 10:16:40 -0700 | [diff] [blame] | 2352 | RRectType type = kFill_RRectType; |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 2353 | if (devStrokeWidth > 0) { |
| 2354 | if (SkScalarNearlyZero(devStrokeWidth)) { |
| 2355 | halfWidth = SK_ScalarHalf; |
| 2356 | } else { |
| 2357 | halfWidth = SkScalarHalf(devStrokeWidth); |
| 2358 | } |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2359 | |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 2360 | if (strokeOnly) { |
jvanverth | c3d0e42 | 2016-08-25 08:12:35 -0700 | [diff] [blame] | 2361 | // Outset stroke by 1/4 pixel |
| 2362 | devStrokeWidth += 0.25f; |
| 2363 | // If stroke is greater than width or height, this is still a fill |
| 2364 | // Otherwise we compute stroke params |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 2365 | if (devStrokeWidth <= devRect.width() && devStrokeWidth <= devRect.height()) { |
jvanverth | c3d0e42 | 2016-08-25 08:12:35 -0700 | [diff] [blame] | 2366 | innerRadius = devRadius - halfWidth; |
jvanverth | 84839f6 | 2016-08-29 10:16:40 -0700 | [diff] [blame] | 2367 | type = (innerRadius >= 0) ? kStroke_RRectType : kOverstroke_RRectType; |
jvanverth | c3d0e42 | 2016-08-25 08:12:35 -0700 | [diff] [blame] | 2368 | } |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 2369 | } |
| 2370 | outerRadius += halfWidth; |
| 2371 | bounds.outset(halfWidth, halfWidth); |
| 2372 | } |
bsalomon | cdaa97b | 2016-03-08 08:30:14 -0800 | [diff] [blame] | 2373 | |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 2374 | // The radii are outset for two reasons. First, it allows the shader to simply perform |
| 2375 | // simpler computation because the computed alpha is zero, rather than 50%, at the radius. |
| 2376 | // Second, the outer radius is used to compute the verts of the bounding box that is |
| 2377 | // rendered and the outset ensures the box will cover all partially covered by the rrect |
| 2378 | // corners. |
| 2379 | outerRadius += SK_ScalarHalf; |
| 2380 | innerRadius -= SK_ScalarHalf; |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 2381 | |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 2382 | this->setBounds(bounds, HasAABloat::kYes, IsZeroArea::kNo); |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 2383 | |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 2384 | // Expand the rect for aa to generate correct vertices. |
| 2385 | bounds.outset(SK_ScalarHalf, SK_ScalarHalf); |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 2386 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2387 | fRRects.emplace_back(RRect{color, innerRadius, outerRadius, bounds, type}); |
jvanverth | 84839f6 | 2016-08-29 10:16:40 -0700 | [diff] [blame] | 2388 | fVertCount = rrect_type_to_vert_count(type); |
| 2389 | fIndexCount = rrect_type_to_index_count(type); |
| 2390 | fAllFill = (kFill_RRectType == type); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2391 | } |
| 2392 | |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 2393 | const char* name() const override { return "CircularRRectOp"; } |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2394 | |
Chris Dalton | 1706cbf | 2019-05-21 19:35:29 -0600 | [diff] [blame^] | 2395 | void visitProxies(const VisitProxyFunc& func) const override { |
Robert Phillips | b493eeb | 2017-09-13 13:10:52 -0400 | [diff] [blame] | 2396 | fHelper.visitProxies(func); |
| 2397 | } |
| 2398 | |
Brian Osman | 9a390ac | 2018-11-12 09:47:48 -0500 | [diff] [blame] | 2399 | #ifdef SK_DEBUG |
jvanverth | c3d0e42 | 2016-08-25 08:12:35 -0700 | [diff] [blame] | 2400 | SkString dumpInfo() const override { |
| 2401 | SkString string; |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2402 | for (int i = 0; i < fRRects.count(); ++i) { |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 2403 | string.appendf( |
| 2404 | "Color: 0x%08x Rect [L: %.2f, T: %.2f, R: %.2f, B: %.2f]," |
| 2405 | "InnerRad: %.2f, OuterRad: %.2f\n", |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 2406 | fRRects[i].fColor.toBytes_RGBA(), fRRects[i].fDevBounds.fLeft, |
Brian Osman | 1be2b7c | 2018-10-29 16:07:15 -0400 | [diff] [blame] | 2407 | fRRects[i].fDevBounds.fTop, fRRects[i].fDevBounds.fRight, |
| 2408 | fRRects[i].fDevBounds.fBottom, fRRects[i].fInnerRadius, |
| 2409 | fRRects[i].fOuterRadius); |
jvanverth | c3d0e42 | 2016-08-25 08:12:35 -0700 | [diff] [blame] | 2410 | } |
Brian Salomon | 82dfd3d | 2017-06-14 12:30:35 -0400 | [diff] [blame] | 2411 | string += fHelper.dumpInfo(); |
| 2412 | string += INHERITED::dumpInfo(); |
jvanverth | c3d0e42 | 2016-08-25 08:12:35 -0700 | [diff] [blame] | 2413 | return string; |
| 2414 | } |
Brian Osman | 9a390ac | 2018-11-12 09:47:48 -0500 | [diff] [blame] | 2415 | #endif |
jvanverth | c3d0e42 | 2016-08-25 08:12:35 -0700 | [diff] [blame] | 2416 | |
Brian Osman | 5ced0bf | 2019-03-15 10:15:29 -0400 | [diff] [blame] | 2417 | GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip, |
| 2418 | GrFSAAType fsaaType, GrClampType clampType) override { |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 2419 | SkPMColor4f* color = &fRRects.front().fColor; |
Brian Osman | 5ced0bf | 2019-03-15 10:15:29 -0400 | [diff] [blame] | 2420 | return fHelper.finalizeProcessors(caps, clip, fsaaType, clampType, |
Brian Osman | 8fa7ab4 | 2019-03-18 10:22:42 -0400 | [diff] [blame] | 2421 | GrProcessorAnalysisCoverage::kSingleChannel, color, |
| 2422 | &fWideColor); |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2423 | } |
| 2424 | |
| 2425 | FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); } |
| 2426 | |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 2427 | private: |
Brian Osman | a1d4eb9 | 2018-12-06 16:33:10 -0500 | [diff] [blame] | 2428 | static void FillInOverstrokeVerts(GrVertexWriter& verts, const SkRect& bounds, SkScalar smInset, |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 2429 | SkScalar bigInset, SkScalar xOffset, SkScalar outerRadius, |
Brian Osman | a1d4eb9 | 2018-12-06 16:33:10 -0500 | [diff] [blame] | 2430 | SkScalar innerRadius, const GrVertexColor& color) { |
Robert Phillips | 79839d4 | 2016-10-06 15:03:34 -0400 | [diff] [blame] | 2431 | SkASSERT(smInset < bigInset); |
| 2432 | |
| 2433 | // TL |
Brian Osman | a1d4eb9 | 2018-12-06 16:33:10 -0500 | [diff] [blame] | 2434 | verts.write(bounds.fLeft + smInset, bounds.fTop + smInset, |
| 2435 | color, |
| 2436 | xOffset, 0.0f, |
| 2437 | outerRadius, innerRadius); |
Robert Phillips | 79839d4 | 2016-10-06 15:03:34 -0400 | [diff] [blame] | 2438 | |
| 2439 | // TR |
Brian Osman | a1d4eb9 | 2018-12-06 16:33:10 -0500 | [diff] [blame] | 2440 | verts.write(bounds.fRight - smInset, bounds.fTop + smInset, |
| 2441 | color, |
| 2442 | xOffset, 0.0f, |
| 2443 | outerRadius, innerRadius); |
Robert Phillips | 79839d4 | 2016-10-06 15:03:34 -0400 | [diff] [blame] | 2444 | |
Brian Osman | a1d4eb9 | 2018-12-06 16:33:10 -0500 | [diff] [blame] | 2445 | verts.write(bounds.fLeft + bigInset, bounds.fTop + bigInset, |
| 2446 | color, |
| 2447 | 0.0f, 0.0f, |
| 2448 | outerRadius, innerRadius); |
Robert Phillips | 79839d4 | 2016-10-06 15:03:34 -0400 | [diff] [blame] | 2449 | |
Brian Osman | a1d4eb9 | 2018-12-06 16:33:10 -0500 | [diff] [blame] | 2450 | verts.write(bounds.fRight - bigInset, bounds.fTop + bigInset, |
| 2451 | color, |
| 2452 | 0.0f, 0.0f, |
| 2453 | outerRadius, innerRadius); |
Robert Phillips | 79839d4 | 2016-10-06 15:03:34 -0400 | [diff] [blame] | 2454 | |
Brian Osman | a1d4eb9 | 2018-12-06 16:33:10 -0500 | [diff] [blame] | 2455 | verts.write(bounds.fLeft + bigInset, bounds.fBottom - bigInset, |
| 2456 | color, |
| 2457 | 0.0f, 0.0f, |
| 2458 | outerRadius, innerRadius); |
Robert Phillips | 79839d4 | 2016-10-06 15:03:34 -0400 | [diff] [blame] | 2459 | |
Brian Osman | a1d4eb9 | 2018-12-06 16:33:10 -0500 | [diff] [blame] | 2460 | verts.write(bounds.fRight - bigInset, bounds.fBottom - bigInset, |
| 2461 | color, |
| 2462 | 0.0f, 0.0f, |
| 2463 | outerRadius, innerRadius); |
Robert Phillips | 79839d4 | 2016-10-06 15:03:34 -0400 | [diff] [blame] | 2464 | |
| 2465 | // BL |
Brian Osman | a1d4eb9 | 2018-12-06 16:33:10 -0500 | [diff] [blame] | 2466 | verts.write(bounds.fLeft + smInset, bounds.fBottom - smInset, |
| 2467 | color, |
| 2468 | xOffset, 0.0f, |
| 2469 | outerRadius, innerRadius); |
Robert Phillips | 79839d4 | 2016-10-06 15:03:34 -0400 | [diff] [blame] | 2470 | |
| 2471 | // BR |
Brian Osman | a1d4eb9 | 2018-12-06 16:33:10 -0500 | [diff] [blame] | 2472 | verts.write(bounds.fRight - smInset, bounds.fBottom - smInset, |
| 2473 | color, |
| 2474 | xOffset, 0.0f, |
| 2475 | outerRadius, innerRadius); |
Robert Phillips | 79839d4 | 2016-10-06 15:03:34 -0400 | [diff] [blame] | 2476 | } |
| 2477 | |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 2478 | void onPrepareDraws(Target* target) override { |
bsalomon | cdaa97b | 2016-03-08 08:30:14 -0800 | [diff] [blame] | 2479 | // Invert the view matrix as a local matrix (if any other processors require coords). |
| 2480 | SkMatrix localMatrix; |
| 2481 | if (!fViewMatrixIfUsingLocalCoords.invert(&localMatrix)) { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2482 | return; |
| 2483 | } |
| 2484 | |
| 2485 | // Setup geometry processor |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 2486 | sk_sp<GrGeometryProcessor> gp( |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 2487 | new CircleGeometryProcessor(!fAllFill, false, false, false, false, fWideColor, |
| 2488 | localMatrix)); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2489 | |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 2490 | sk_sp<const GrBuffer> vertexBuffer; |
jvanverth | 84839f6 | 2016-08-29 10:16:40 -0700 | [diff] [blame] | 2491 | int firstVertex; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2492 | |
Brian Osman | a1d4eb9 | 2018-12-06 16:33:10 -0500 | [diff] [blame] | 2493 | GrVertexWriter verts{target->makeVertexSpace(gp->vertexStride(), fVertCount, |
| 2494 | &vertexBuffer, &firstVertex)}; |
| 2495 | if (!verts.fPtr) { |
joshualitt | 4b31de8 | 2015-03-05 14:33:41 -0800 | [diff] [blame] | 2496 | SkDebugf("Could not allocate vertices\n"); |
| 2497 | return; |
| 2498 | } |
| 2499 | |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 2500 | sk_sp<const GrBuffer> indexBuffer; |
jvanverth | 84839f6 | 2016-08-29 10:16:40 -0700 | [diff] [blame] | 2501 | int firstIndex = 0; |
| 2502 | uint16_t* indices = target->makeIndexSpace(fIndexCount, &indexBuffer, &firstIndex); |
| 2503 | if (!indices) { |
| 2504 | SkDebugf("Could not allocate indices\n"); |
| 2505 | return; |
| 2506 | } |
| 2507 | |
| 2508 | int currStartVertex = 0; |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2509 | for (const auto& rrect : fRRects) { |
Brian Osman | a1d4eb9 | 2018-12-06 16:33:10 -0500 | [diff] [blame] | 2510 | GrVertexColor color(rrect.fColor, fWideColor); |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2511 | SkScalar outerRadius = rrect.fOuterRadius; |
| 2512 | const SkRect& bounds = rrect.fDevBounds; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2513 | |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 2514 | SkScalar yCoords[4] = {bounds.fTop, bounds.fTop + outerRadius, |
| 2515 | bounds.fBottom - outerRadius, bounds.fBottom}; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2516 | |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 2517 | SkScalar yOuterRadii[4] = {-1, 0, 0, 1}; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2518 | // The inner radius in the vertex data must be specified in normalized space. |
jvanverth | 84839f6 | 2016-08-29 10:16:40 -0700 | [diff] [blame] | 2519 | // For fills, specifying -1/outerRadius guarantees an alpha of 1.0 at the inner radius. |
Brian Salomon | e23bffd | 2017-06-02 11:01:10 -0400 | [diff] [blame] | 2520 | SkScalar innerRadius = rrect.fType != kFill_RRectType |
| 2521 | ? rrect.fInnerRadius / rrect.fOuterRadius |
| 2522 | : -1.0f / rrect.fOuterRadius; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2523 | for (int i = 0; i < 4; ++i) { |
Brian Osman | a1d4eb9 | 2018-12-06 16:33:10 -0500 | [diff] [blame] | 2524 | verts.write(bounds.fLeft, yCoords[i], |
| 2525 | color, |
| 2526 | -1.0f, yOuterRadii[i], |
| 2527 | outerRadius, innerRadius); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2528 | |
Brian Osman | a1d4eb9 | 2018-12-06 16:33:10 -0500 | [diff] [blame] | 2529 | verts.write(bounds.fLeft + outerRadius, yCoords[i], |
| 2530 | color, |
| 2531 | 0.0f, yOuterRadii[i], |
| 2532 | outerRadius, innerRadius); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2533 | |
Brian Osman | a1d4eb9 | 2018-12-06 16:33:10 -0500 | [diff] [blame] | 2534 | verts.write(bounds.fRight - outerRadius, yCoords[i], |
| 2535 | color, |
| 2536 | 0.0f, yOuterRadii[i], |
| 2537 | outerRadius, innerRadius); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2538 | |
Brian Osman | a1d4eb9 | 2018-12-06 16:33:10 -0500 | [diff] [blame] | 2539 | verts.write(bounds.fRight, yCoords[i], |
| 2540 | color, |
| 2541 | 1.0f, yOuterRadii[i], |
| 2542 | outerRadius, innerRadius); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2543 | } |
jvanverth | c3d0e42 | 2016-08-25 08:12:35 -0700 | [diff] [blame] | 2544 | // Add the additional vertices for overstroked rrects. |
jvanverth | a4f1af8 | 2016-08-29 07:17:47 -0700 | [diff] [blame] | 2545 | // Effectively this is an additional stroked rrect, with its |
| 2546 | // outer radius = outerRadius - innerRadius, and inner radius = 0. |
| 2547 | // This will give us correct AA in the center and the correct |
| 2548 | // distance to the outer edge. |
jvanverth | c3d0e42 | 2016-08-25 08:12:35 -0700 | [diff] [blame] | 2549 | // |
jvanverth | a4f1af8 | 2016-08-29 07:17:47 -0700 | [diff] [blame] | 2550 | // Also, the outer offset is a constant vector pointing to the right, which |
| 2551 | // guarantees that the distance value along the outer rectangle is constant. |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2552 | if (kOverstroke_RRectType == rrect.fType) { |
| 2553 | SkASSERT(rrect.fInnerRadius <= 0.0f); |
Robert Phillips | 79839d4 | 2016-10-06 15:03:34 -0400 | [diff] [blame] | 2554 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2555 | SkScalar overstrokeOuterRadius = outerRadius - rrect.fInnerRadius; |
jvanverth | a4f1af8 | 2016-08-29 07:17:47 -0700 | [diff] [blame] | 2556 | // this is the normalized distance from the outer rectangle of this |
| 2557 | // geometry to the outer edge |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2558 | SkScalar maxOffset = -rrect.fInnerRadius / overstrokeOuterRadius; |
jvanverth | a4f1af8 | 2016-08-29 07:17:47 -0700 | [diff] [blame] | 2559 | |
Brian Osman | a1d4eb9 | 2018-12-06 16:33:10 -0500 | [diff] [blame] | 2560 | FillInOverstrokeVerts(verts, bounds, outerRadius, overstrokeOuterRadius, maxOffset, |
Brian Osman | 1be2b7c | 2018-10-29 16:07:15 -0400 | [diff] [blame] | 2561 | overstrokeOuterRadius, 0.0f, color); |
Robert Phillips | 79839d4 | 2016-10-06 15:03:34 -0400 | [diff] [blame] | 2562 | } |
jvanverth | 6a39761 | 2016-08-26 08:15:33 -0700 | [diff] [blame] | 2563 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2564 | const uint16_t* primIndices = rrect_type_to_indices(rrect.fType); |
| 2565 | const int primIndexCount = rrect_type_to_index_count(rrect.fType); |
jvanverth | 84839f6 | 2016-08-29 10:16:40 -0700 | [diff] [blame] | 2566 | for (int i = 0; i < primIndexCount; ++i) { |
| 2567 | *indices++ = primIndices[i] + currStartVertex; |
| 2568 | } |
| 2569 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2570 | currStartVertex += rrect_type_to_vert_count(rrect.fType); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2571 | } |
| 2572 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 2573 | GrMesh* mesh = target->allocMesh(GrPrimitiveType::kTriangles); |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 2574 | mesh->setIndexed(std::move(indexBuffer), fIndexCount, firstIndex, 0, fVertCount - 1, |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 2575 | GrPrimitiveRestart::kNo); |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 2576 | mesh->setVertexData(std::move(vertexBuffer), firstVertex); |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 2577 | target->recordDraw(std::move(gp), mesh); |
| 2578 | } |
| 2579 | |
| 2580 | void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override { |
| 2581 | fHelper.executeDrawsAndUploads(this, flushState, chainBounds); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2582 | } |
| 2583 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 2584 | CombineResult onCombineIfPossible(GrOp* t, const GrCaps& caps) override { |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 2585 | CircularRRectOp* that = t->cast<CircularRRectOp>(); |
Jim Van Verth | 8cefe40 | 2017-02-09 11:36:37 -0500 | [diff] [blame] | 2586 | |
| 2587 | // can only represent 65535 unique vertices with 16-bit indices |
Jim Van Verth | e549a05 | 2017-02-21 17:55:13 -0500 | [diff] [blame] | 2588 | if (fVertCount + that->fVertCount > 65536) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 2589 | return CombineResult::kCannotCombine; |
Jim Van Verth | 8cefe40 | 2017-02-09 11:36:37 -0500 | [diff] [blame] | 2590 | } |
| 2591 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2592 | if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 2593 | return CombineResult::kCannotCombine; |
joshualitt | 8cab9a7 | 2015-07-16 09:13:50 -0700 | [diff] [blame] | 2594 | } |
| 2595 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2596 | if (fHelper.usesLocalCoords() && |
| 2597 | !fViewMatrixIfUsingLocalCoords.cheapEqualTo(that->fViewMatrixIfUsingLocalCoords)) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 2598 | return CombineResult::kCannotCombine; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2599 | } |
| 2600 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2601 | fRRects.push_back_n(that->fRRects.count(), that->fRRects.begin()); |
jvanverth | 84839f6 | 2016-08-29 10:16:40 -0700 | [diff] [blame] | 2602 | fVertCount += that->fVertCount; |
| 2603 | fIndexCount += that->fIndexCount; |
| 2604 | fAllFill = fAllFill && that->fAllFill; |
Brian Osman | a1d4eb9 | 2018-12-06 16:33:10 -0500 | [diff] [blame] | 2605 | fWideColor = fWideColor || that->fWideColor; |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 2606 | return CombineResult::kMerged; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2607 | } |
| 2608 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2609 | struct RRect { |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 2610 | SkPMColor4f fColor; |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 2611 | SkScalar fInnerRadius; |
| 2612 | SkScalar fOuterRadius; |
| 2613 | SkRect fDevBounds; |
jvanverth | 84839f6 | 2016-08-29 10:16:40 -0700 | [diff] [blame] | 2614 | RRectType fType; |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 2615 | }; |
| 2616 | |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 2617 | SkMatrix fViewMatrixIfUsingLocalCoords; |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2618 | Helper fHelper; |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 2619 | int fVertCount; |
| 2620 | int fIndexCount; |
| 2621 | bool fAllFill; |
Brian Osman | a1d4eb9 | 2018-12-06 16:33:10 -0500 | [diff] [blame] | 2622 | bool fWideColor; |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2623 | SkSTArray<1, RRect, true> fRRects; |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 2624 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2625 | typedef GrMeshDrawOp INHERITED; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2626 | }; |
| 2627 | |
jvanverth | 84839f6 | 2016-08-29 10:16:40 -0700 | [diff] [blame] | 2628 | static const int kNumRRectsInIndexBuffer = 256; |
| 2629 | |
| 2630 | GR_DECLARE_STATIC_UNIQUE_KEY(gStrokeRRectOnlyIndexBufferKey); |
| 2631 | GR_DECLARE_STATIC_UNIQUE_KEY(gRRectOnlyIndexBufferKey); |
Brian Salomon | d28a79d | 2017-10-16 13:01:07 -0400 | [diff] [blame] | 2632 | static sk_sp<const GrBuffer> get_rrect_index_buffer(RRectType type, |
| 2633 | GrResourceProvider* resourceProvider) { |
jvanverth | 84839f6 | 2016-08-29 10:16:40 -0700 | [diff] [blame] | 2634 | GR_DEFINE_STATIC_UNIQUE_KEY(gStrokeRRectOnlyIndexBufferKey); |
| 2635 | GR_DEFINE_STATIC_UNIQUE_KEY(gRRectOnlyIndexBufferKey); |
| 2636 | switch (type) { |
| 2637 | case kFill_RRectType: |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 2638 | return resourceProvider->findOrCreatePatternedIndexBuffer( |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 2639 | gStandardRRectIndices, kIndicesPerFillRRect, kNumRRectsInIndexBuffer, |
| 2640 | kVertsPerStandardRRect, gRRectOnlyIndexBufferKey); |
jvanverth | 84839f6 | 2016-08-29 10:16:40 -0700 | [diff] [blame] | 2641 | case kStroke_RRectType: |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 2642 | return resourceProvider->findOrCreatePatternedIndexBuffer( |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 2643 | gStandardRRectIndices, kIndicesPerStrokeRRect, kNumRRectsInIndexBuffer, |
| 2644 | kVertsPerStandardRRect, gStrokeRRectOnlyIndexBufferKey); |
jvanverth | 84839f6 | 2016-08-29 10:16:40 -0700 | [diff] [blame] | 2645 | default: |
| 2646 | SkASSERT(false); |
| 2647 | return nullptr; |
Brian Salomon | 2335644 | 2018-11-30 15:33:19 -0500 | [diff] [blame] | 2648 | } |
jvanverth | 84839f6 | 2016-08-29 10:16:40 -0700 | [diff] [blame] | 2649 | } |
| 2650 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2651 | class EllipticalRRectOp : public GrMeshDrawOp { |
| 2652 | private: |
| 2653 | using Helper = GrSimpleMeshDrawOpHelper; |
| 2654 | |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2655 | public: |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 2656 | DEFINE_OP_CLASS_ID |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 2657 | |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 2658 | // If devStrokeWidths values are <= 0 indicates then fill only. Otherwise, strokeOnly indicates |
| 2659 | // whether the rrect is only stroked or stroked and filled. |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 2660 | static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context, |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 2661 | GrPaint&& paint, |
| 2662 | const SkMatrix& viewMatrix, |
| 2663 | const SkRect& devRect, |
| 2664 | float devXRadius, |
| 2665 | float devYRadius, |
| 2666 | SkVector devStrokeWidths, |
| 2667 | bool strokeOnly) { |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 2668 | SkASSERT(devXRadius > 0.5); |
| 2669 | SkASSERT(devYRadius > 0.5); |
| 2670 | SkASSERT((devStrokeWidths.fX > 0) == (devStrokeWidths.fY > 0)); |
| 2671 | SkASSERT(!(strokeOnly && devStrokeWidths.fX <= 0)); |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 2672 | if (devStrokeWidths.fX > 0) { |
| 2673 | if (SkScalarNearlyZero(devStrokeWidths.length())) { |
| 2674 | devStrokeWidths.set(SK_ScalarHalf, SK_ScalarHalf); |
| 2675 | } else { |
| 2676 | devStrokeWidths.scale(SK_ScalarHalf); |
| 2677 | } |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2678 | |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 2679 | // we only handle thick strokes for near-circular ellipses |
| 2680 | if (devStrokeWidths.length() > SK_ScalarHalf && |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 2681 | (SK_ScalarHalf * devXRadius > devYRadius || |
| 2682 | SK_ScalarHalf * devYRadius > devXRadius)) { |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 2683 | return nullptr; |
| 2684 | } |
| 2685 | |
| 2686 | // we don't handle it if curvature of the stroke is less than curvature of the ellipse |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 2687 | if (devStrokeWidths.fX * (devYRadius * devYRadius) < |
| 2688 | (devStrokeWidths.fY * devStrokeWidths.fY) * devXRadius) { |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 2689 | return nullptr; |
| 2690 | } |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 2691 | if (devStrokeWidths.fY * (devXRadius * devXRadius) < |
| 2692 | (devStrokeWidths.fX * devStrokeWidths.fX) * devYRadius) { |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 2693 | return nullptr; |
| 2694 | } |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2695 | } |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 2696 | return Helper::FactoryHelper<EllipticalRRectOp>(context, std::move(paint), |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 2697 | viewMatrix, devRect, |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2698 | devXRadius, devYRadius, devStrokeWidths, |
| 2699 | strokeOnly); |
| 2700 | } |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 2701 | |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 2702 | EllipticalRRectOp(Helper::MakeArgs helperArgs, const SkPMColor4f& color, |
Brian Osman | 936fe7d | 2018-10-30 15:30:35 -0400 | [diff] [blame] | 2703 | const SkMatrix& viewMatrix, const SkRect& devRect, float devXRadius, |
| 2704 | float devYRadius, SkVector devStrokeHalfWidths, bool strokeOnly) |
Jim Van Verth | 20ae25c | 2019-03-29 08:50:41 -0400 | [diff] [blame] | 2705 | : INHERITED(ClassID()) |
| 2706 | , fHelper(helperArgs, GrAAType::kCoverage) |
| 2707 | , fUseScale(false) { |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2708 | SkScalar innerXRadius = 0.0f; |
| 2709 | SkScalar innerYRadius = 0.0f; |
| 2710 | SkRect bounds = devRect; |
| 2711 | bool stroked = false; |
| 2712 | if (devStrokeHalfWidths.fX > 0) { |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 2713 | // this is legit only if scale & translation (which should be the case at the moment) |
| 2714 | if (strokeOnly) { |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2715 | innerXRadius = devXRadius - devStrokeHalfWidths.fX; |
| 2716 | innerYRadius = devYRadius - devStrokeHalfWidths.fY; |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 2717 | stroked = (innerXRadius >= 0 && innerYRadius >= 0); |
| 2718 | } |
| 2719 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2720 | devXRadius += devStrokeHalfWidths.fX; |
| 2721 | devYRadius += devStrokeHalfWidths.fY; |
| 2722 | bounds.outset(devStrokeHalfWidths.fX, devStrokeHalfWidths.fY); |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 2723 | } |
| 2724 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2725 | fStroked = stroked; |
| 2726 | fViewMatrixIfUsingLocalCoords = viewMatrix; |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 2727 | this->setBounds(bounds, HasAABloat::kYes, IsZeroArea::kNo); |
| 2728 | // Expand the rect for aa in order to generate the correct vertices. |
| 2729 | bounds.outset(SK_ScalarHalf, SK_ScalarHalf); |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2730 | fRRects.emplace_back( |
| 2731 | RRect{color, devXRadius, devYRadius, innerXRadius, innerYRadius, bounds}); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2732 | } |
| 2733 | |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 2734 | const char* name() const override { return "EllipticalRRectOp"; } |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2735 | |
Chris Dalton | 1706cbf | 2019-05-21 19:35:29 -0600 | [diff] [blame^] | 2736 | void visitProxies(const VisitProxyFunc& func) const override { |
Robert Phillips | b493eeb | 2017-09-13 13:10:52 -0400 | [diff] [blame] | 2737 | fHelper.visitProxies(func); |
| 2738 | } |
| 2739 | |
Brian Osman | 9a390ac | 2018-11-12 09:47:48 -0500 | [diff] [blame] | 2740 | #ifdef SK_DEBUG |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame] | 2741 | SkString dumpInfo() const override { |
| 2742 | SkString string; |
| 2743 | string.appendf("Stroked: %d\n", fStroked); |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2744 | for (const auto& geo : fRRects) { |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 2745 | string.appendf( |
| 2746 | "Color: 0x%08x Rect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], " |
| 2747 | "XRad: %.2f, YRad: %.2f, InnerXRad: %.2f, InnerYRad: %.2f\n", |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 2748 | geo.fColor.toBytes_RGBA(), geo.fDevBounds.fLeft, geo.fDevBounds.fTop, |
Brian Osman | 1be2b7c | 2018-10-29 16:07:15 -0400 | [diff] [blame] | 2749 | geo.fDevBounds.fRight, geo.fDevBounds.fBottom, geo.fXRadius, geo.fYRadius, |
| 2750 | geo.fInnerXRadius, geo.fInnerYRadius); |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame] | 2751 | } |
Brian Salomon | 82dfd3d | 2017-06-14 12:30:35 -0400 | [diff] [blame] | 2752 | string += fHelper.dumpInfo(); |
| 2753 | string += INHERITED::dumpInfo(); |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame] | 2754 | return string; |
| 2755 | } |
Brian Osman | 9a390ac | 2018-11-12 09:47:48 -0500 | [diff] [blame] | 2756 | #endif |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame] | 2757 | |
Brian Osman | 5ced0bf | 2019-03-15 10:15:29 -0400 | [diff] [blame] | 2758 | GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip, |
| 2759 | GrFSAAType fsaaType, GrClampType clampType) override { |
Jim Van Verth | 20ae25c | 2019-03-29 08:50:41 -0400 | [diff] [blame] | 2760 | fUseScale = !caps.shaderCaps()->floatIs32Bits(); |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 2761 | SkPMColor4f* color = &fRRects.front().fColor; |
Brian Osman | 5ced0bf | 2019-03-15 10:15:29 -0400 | [diff] [blame] | 2762 | return fHelper.finalizeProcessors(caps, clip, fsaaType, clampType, |
Brian Osman | 8fa7ab4 | 2019-03-18 10:22:42 -0400 | [diff] [blame] | 2763 | GrProcessorAnalysisCoverage::kSingleChannel, color, |
| 2764 | &fWideColor); |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2765 | } |
| 2766 | |
| 2767 | FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); } |
| 2768 | |
bsalomon | e46f9fe | 2015-08-18 06:05:14 -0700 | [diff] [blame] | 2769 | private: |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 2770 | void onPrepareDraws(Target* target) override { |
bsalomon | cdaa97b | 2016-03-08 08:30:14 -0800 | [diff] [blame] | 2771 | SkMatrix localMatrix; |
| 2772 | if (!fViewMatrixIfUsingLocalCoords.invert(&localMatrix)) { |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2773 | return; |
| 2774 | } |
| 2775 | |
| 2776 | // Setup geometry processor |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 2777 | sk_sp<GrGeometryProcessor> gp(new EllipseGeometryProcessor(fStroked, fWideColor, fUseScale, |
Brian Osman | a1d4eb9 | 2018-12-06 16:33:10 -0500 | [diff] [blame] | 2778 | localMatrix)); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2779 | |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 2780 | // drop out the middle quad if we're stroked |
jvanverth | c3d0e42 | 2016-08-25 08:12:35 -0700 | [diff] [blame] | 2781 | int indicesPerInstance = fStroked ? kIndicesPerStrokeRRect : kIndicesPerFillRRect; |
Brian Salomon | d28a79d | 2017-10-16 13:01:07 -0400 | [diff] [blame] | 2782 | sk_sp<const GrBuffer> indexBuffer = get_rrect_index_buffer( |
| 2783 | fStroked ? kStroke_RRectType : kFill_RRectType, target->resourceProvider()); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2784 | |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 2785 | if (!indexBuffer) { |
| 2786 | SkDebugf("Could not allocate indices\n"); |
| 2787 | return; |
| 2788 | } |
Brian Osman | a1d4eb9 | 2018-12-06 16:33:10 -0500 | [diff] [blame] | 2789 | PatternHelper helper(target, GrPrimitiveType::kTriangles, gp->vertexStride(), |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 2790 | std::move(indexBuffer), kVertsPerStandardRRect, indicesPerInstance, |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 2791 | fRRects.count()); |
Brian Osman | a1d4eb9 | 2018-12-06 16:33:10 -0500 | [diff] [blame] | 2792 | GrVertexWriter verts{helper.vertices()}; |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 2793 | if (!verts.fPtr) { |
joshualitt | 4b31de8 | 2015-03-05 14:33:41 -0800 | [diff] [blame] | 2794 | SkDebugf("Could not allocate vertices\n"); |
| 2795 | return; |
| 2796 | } |
| 2797 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2798 | for (const auto& rrect : fRRects) { |
Brian Osman | a1d4eb9 | 2018-12-06 16:33:10 -0500 | [diff] [blame] | 2799 | GrVertexColor color(rrect.fColor, fWideColor); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2800 | // Compute the reciprocals of the radii here to save time in the shader |
Brian Osman | a1d4eb9 | 2018-12-06 16:33:10 -0500 | [diff] [blame] | 2801 | float reciprocalRadii[4] = { |
| 2802 | SkScalarInvert(rrect.fXRadius), |
| 2803 | SkScalarInvert(rrect.fYRadius), |
| 2804 | SkScalarInvert(rrect.fInnerXRadius), |
| 2805 | SkScalarInvert(rrect.fInnerYRadius) |
| 2806 | }; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2807 | |
| 2808 | // Extend the radii out half a pixel to antialias. |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 2809 | SkScalar xOuterRadius = rrect.fXRadius + SK_ScalarHalf; |
| 2810 | SkScalar yOuterRadius = rrect.fYRadius + SK_ScalarHalf; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2811 | |
Jim Van Verth | b1b87d9 | 2018-06-28 10:46:05 -0400 | [diff] [blame] | 2812 | SkScalar xMaxOffset = xOuterRadius; |
| 2813 | SkScalar yMaxOffset = yOuterRadius; |
| 2814 | if (!fStroked) { |
| 2815 | // For filled rrects we map a unit circle in the vertex attributes rather than |
| 2816 | // computing an ellipse and modifying that distance, so we normalize to 1. |
| 2817 | xMaxOffset /= rrect.fXRadius; |
| 2818 | yMaxOffset /= rrect.fYRadius; |
| 2819 | } |
| 2820 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2821 | const SkRect& bounds = rrect.fDevBounds; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2822 | |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 2823 | SkScalar yCoords[4] = {bounds.fTop, bounds.fTop + yOuterRadius, |
| 2824 | bounds.fBottom - yOuterRadius, bounds.fBottom}; |
Jim Van Verth | b1b87d9 | 2018-06-28 10:46:05 -0400 | [diff] [blame] | 2825 | SkScalar yOuterOffsets[4] = {yMaxOffset, |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 2826 | SK_ScalarNearlyZero, // we're using inversesqrt() in |
| 2827 | // shader, so can't be exactly 0 |
Jim Van Verth | b1b87d9 | 2018-06-28 10:46:05 -0400 | [diff] [blame] | 2828 | SK_ScalarNearlyZero, yMaxOffset}; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2829 | |
Jim Van Verth | 20ae25c | 2019-03-29 08:50:41 -0400 | [diff] [blame] | 2830 | auto maybeScale = GrVertexWriter::If(fUseScale, SkTMax(rrect.fXRadius, rrect.fYRadius)); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2831 | for (int i = 0; i < 4; ++i) { |
Brian Osman | a1d4eb9 | 2018-12-06 16:33:10 -0500 | [diff] [blame] | 2832 | verts.write(bounds.fLeft, yCoords[i], |
| 2833 | color, |
| 2834 | xMaxOffset, yOuterOffsets[i], |
Jim Van Verth | 20ae25c | 2019-03-29 08:50:41 -0400 | [diff] [blame] | 2835 | maybeScale, |
Brian Osman | a1d4eb9 | 2018-12-06 16:33:10 -0500 | [diff] [blame] | 2836 | reciprocalRadii); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2837 | |
Brian Osman | a1d4eb9 | 2018-12-06 16:33:10 -0500 | [diff] [blame] | 2838 | verts.write(bounds.fLeft + xOuterRadius, yCoords[i], |
| 2839 | color, |
| 2840 | SK_ScalarNearlyZero, yOuterOffsets[i], |
Jim Van Verth | 20ae25c | 2019-03-29 08:50:41 -0400 | [diff] [blame] | 2841 | maybeScale, |
Brian Osman | a1d4eb9 | 2018-12-06 16:33:10 -0500 | [diff] [blame] | 2842 | reciprocalRadii); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2843 | |
Brian Osman | a1d4eb9 | 2018-12-06 16:33:10 -0500 | [diff] [blame] | 2844 | verts.write(bounds.fRight - xOuterRadius, yCoords[i], |
| 2845 | color, |
| 2846 | SK_ScalarNearlyZero, yOuterOffsets[i], |
Jim Van Verth | 20ae25c | 2019-03-29 08:50:41 -0400 | [diff] [blame] | 2847 | maybeScale, |
Brian Osman | a1d4eb9 | 2018-12-06 16:33:10 -0500 | [diff] [blame] | 2848 | reciprocalRadii); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2849 | |
Brian Osman | a1d4eb9 | 2018-12-06 16:33:10 -0500 | [diff] [blame] | 2850 | verts.write(bounds.fRight, yCoords[i], |
| 2851 | color, |
| 2852 | xMaxOffset, yOuterOffsets[i], |
Jim Van Verth | 20ae25c | 2019-03-29 08:50:41 -0400 | [diff] [blame] | 2853 | maybeScale, |
Brian Osman | a1d4eb9 | 2018-12-06 16:33:10 -0500 | [diff] [blame] | 2854 | reciprocalRadii); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2855 | } |
| 2856 | } |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 2857 | helper.recordDraw(target, std::move(gp)); |
| 2858 | } |
| 2859 | |
| 2860 | void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override { |
| 2861 | fHelper.executeDrawsAndUploads(this, flushState, chainBounds); |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2862 | } |
| 2863 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 2864 | CombineResult onCombineIfPossible(GrOp* t, const GrCaps& caps) override { |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 2865 | EllipticalRRectOp* that = t->cast<EllipticalRRectOp>(); |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 2866 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2867 | if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 2868 | return CombineResult::kCannotCombine; |
joshualitt | 8cab9a7 | 2015-07-16 09:13:50 -0700 | [diff] [blame] | 2869 | } |
| 2870 | |
bsalomon | cdaa97b | 2016-03-08 08:30:14 -0800 | [diff] [blame] | 2871 | if (fStroked != that->fStroked) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 2872 | return CombineResult::kCannotCombine; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2873 | } |
| 2874 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2875 | if (fHelper.usesLocalCoords() && |
| 2876 | !fViewMatrixIfUsingLocalCoords.cheapEqualTo(that->fViewMatrixIfUsingLocalCoords)) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 2877 | return CombineResult::kCannotCombine; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2878 | } |
Brian Salomon | ea26d6b | 2018-01-23 20:33:21 +0000 | [diff] [blame] | 2879 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2880 | fRRects.push_back_n(that->fRRects.count(), that->fRRects.begin()); |
Brian Osman | a1d4eb9 | 2018-12-06 16:33:10 -0500 | [diff] [blame] | 2881 | fWideColor = fWideColor || that->fWideColor; |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 2882 | return CombineResult::kMerged; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2883 | } |
| 2884 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2885 | struct RRect { |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 2886 | SkPMColor4f fColor; |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 2887 | SkScalar fXRadius; |
| 2888 | SkScalar fYRadius; |
| 2889 | SkScalar fInnerXRadius; |
| 2890 | SkScalar fInnerYRadius; |
| 2891 | SkRect fDevBounds; |
| 2892 | }; |
| 2893 | |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 2894 | SkMatrix fViewMatrixIfUsingLocalCoords; |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2895 | Helper fHelper; |
| 2896 | bool fStroked; |
Brian Osman | a1d4eb9 | 2018-12-06 16:33:10 -0500 | [diff] [blame] | 2897 | bool fWideColor; |
Jim Van Verth | 20ae25c | 2019-03-29 08:50:41 -0400 | [diff] [blame] | 2898 | bool fUseScale; |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2899 | SkSTArray<1, RRect, true> fRRects; |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 2900 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2901 | typedef GrMeshDrawOp INHERITED; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 2902 | }; |
| 2903 | |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 2904 | static std::unique_ptr<GrDrawOp> make_rrect_op(GrRecordingContext* context, |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 2905 | GrPaint&& paint, |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 2906 | const SkMatrix& viewMatrix, |
| 2907 | const SkRRect& rrect, |
| 2908 | const SkStrokeRec& stroke) { |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 2909 | SkASSERT(viewMatrix.rectStaysRect()); |
| 2910 | SkASSERT(rrect.isSimple()); |
| 2911 | SkASSERT(!rrect.isOval()); |
commit-bot@chromium.org | e0a868c | 2013-11-22 07:02:11 +0000 | [diff] [blame] | 2912 | |
Brian Salomon | 53e4c3c | 2016-12-21 11:38:53 -0500 | [diff] [blame] | 2913 | // RRect ops only handle simple, but not too simple, rrects. |
| 2914 | // Do any matrix crunching before we reset the draw state for device coords. |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 2915 | const SkRect& rrectBounds = rrect.getBounds(); |
joshualitt | d96a67b | 2015-05-05 14:09:05 -0700 | [diff] [blame] | 2916 | SkRect bounds; |
| 2917 | viewMatrix.mapRect(&bounds, rrectBounds); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 2918 | |
Mike Reed | 242135a | 2018-02-22 13:41:39 -0500 | [diff] [blame] | 2919 | SkVector radii = SkRRectPriv::GetSimpleRadii(rrect); |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 2920 | SkScalar xRadius = SkScalarAbs(viewMatrix[SkMatrix::kMScaleX] * radii.fX + |
| 2921 | viewMatrix[SkMatrix::kMSkewY] * radii.fY); |
| 2922 | SkScalar yRadius = SkScalarAbs(viewMatrix[SkMatrix::kMSkewX] * radii.fX + |
| 2923 | viewMatrix[SkMatrix::kMScaleY] * radii.fY); |
commit-bot@chromium.org | 6bb3efc | 2013-05-16 13:14:46 +0000 | [diff] [blame] | 2924 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 2925 | SkStrokeRec::Style style = stroke.getStyle(); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 2926 | |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 2927 | // Do (potentially) anisotropic mapping of stroke. Use -1s to indicate fill-only draws. |
| 2928 | SkVector scaledStroke = {-1, -1}; |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 2929 | SkScalar strokeWidth = stroke.getWidth(); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 2930 | |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 2931 | bool isStrokeOnly = |
| 2932 | SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style; |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 2933 | bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == style; |
| 2934 | |
jvanverth | c3d0e42 | 2016-08-25 08:12:35 -0700 | [diff] [blame] | 2935 | bool isCircular = (xRadius == yRadius); |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 2936 | if (hasStroke) { |
| 2937 | if (SkStrokeRec::kHairline_Style == style) { |
| 2938 | scaledStroke.set(1, 1); |
| 2939 | } else { |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 2940 | scaledStroke.fX = SkScalarAbs( |
| 2941 | strokeWidth * (viewMatrix[SkMatrix::kMScaleX] + viewMatrix[SkMatrix::kMSkewY])); |
| 2942 | scaledStroke.fY = SkScalarAbs( |
| 2943 | strokeWidth * (viewMatrix[SkMatrix::kMSkewX] + viewMatrix[SkMatrix::kMScaleY])); |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 2944 | } |
| 2945 | |
jvanverth | c3d0e42 | 2016-08-25 08:12:35 -0700 | [diff] [blame] | 2946 | isCircular = isCircular && scaledStroke.fX == scaledStroke.fY; |
| 2947 | // for non-circular rrects, if half of strokewidth is greater than radius, |
| 2948 | // we don't handle that right now |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 2949 | if (!isCircular && (SK_ScalarHalf * scaledStroke.fX > xRadius || |
| 2950 | SK_ScalarHalf * scaledStroke.fY > yRadius)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2951 | return nullptr; |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 2952 | } |
| 2953 | } |
| 2954 | |
Brian Salomon | 8a97f56 | 2019-04-18 14:07:27 -0400 | [diff] [blame] | 2955 | // The matrix may have a rotation by an odd multiple of 90 degrees. |
| 2956 | if (!isCircular && viewMatrix.getScaleX() == 0) { |
| 2957 | std::swap(xRadius, yRadius); |
| 2958 | std::swap(scaledStroke.fX, scaledStroke.fY); |
| 2959 | } |
| 2960 | |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 2961 | // The way the effect interpolates the offset-to-ellipse/circle-center attribute only works on |
| 2962 | // the interior of the rrect if the radii are >= 0.5. Otherwise, the inner rect of the nine- |
| 2963 | // patch will have fractional coverage. This only matters when the interior is actually filled. |
| 2964 | // We could consider falling back to rect rendering here, since a tiny radius is |
| 2965 | // indistinguishable from a square corner. |
| 2966 | if (!isStrokeOnly && (SK_ScalarHalf > xRadius || SK_ScalarHalf > yRadius)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2967 | return nullptr; |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 2968 | } |
| 2969 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 2970 | // if the corners are circles, use the circle renderer |
jvanverth | c3d0e42 | 2016-08-25 08:12:35 -0700 | [diff] [blame] | 2971 | if (isCircular) { |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 2972 | return CircularRRectOp::Make(context, std::move(paint), viewMatrix, bounds, xRadius, |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 2973 | scaledStroke.fX, isStrokeOnly); |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 2974 | // otherwise we use the ellipse renderer |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 2975 | } else { |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 2976 | return EllipticalRRectOp::Make(context, std::move(paint), viewMatrix, bounds, |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 2977 | xRadius, yRadius, scaledStroke, isStrokeOnly); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 2978 | } |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 2979 | } |
| 2980 | |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 2981 | std::unique_ptr<GrDrawOp> GrOvalOpFactory::MakeRRectOp(GrRecordingContext* context, |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 2982 | GrPaint&& paint, |
Brian Salomon | ea26d6b | 2018-01-23 20:33:21 +0000 | [diff] [blame] | 2983 | const SkMatrix& viewMatrix, |
| 2984 | const SkRRect& rrect, |
| 2985 | const SkStrokeRec& stroke, |
| 2986 | const GrShaderCaps* shaderCaps) { |
robertphillips | 0cc2f85 | 2016-02-24 13:36:56 -0800 | [diff] [blame] | 2987 | if (rrect.isOval()) { |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 2988 | return MakeOvalOp(context, std::move(paint), viewMatrix, rrect.getBounds(), |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 2989 | GrStyle(stroke, nullptr), shaderCaps); |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 2990 | } |
| 2991 | |
| 2992 | if (!viewMatrix.rectStaysRect() || !rrect.isSimple()) { |
robertphillips | 0cc2f85 | 2016-02-24 13:36:56 -0800 | [diff] [blame] | 2993 | return nullptr; |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 2994 | } |
| 2995 | |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 2996 | return make_rrect_op(context, std::move(paint), viewMatrix, rrect, stroke); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 2997 | } |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 2998 | |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 2999 | /////////////////////////////////////////////////////////////////////////////// |
| 3000 | |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 3001 | std::unique_ptr<GrDrawOp> GrOvalOpFactory::MakeOvalOp(GrRecordingContext* context, |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 3002 | GrPaint&& paint, |
Brian Salomon | ea26d6b | 2018-01-23 20:33:21 +0000 | [diff] [blame] | 3003 | const SkMatrix& viewMatrix, |
| 3004 | const SkRect& oval, |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 3005 | const GrStyle& style, |
Brian Salomon | ea26d6b | 2018-01-23 20:33:21 +0000 | [diff] [blame] | 3006 | const GrShaderCaps* shaderCaps) { |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 3007 | // we can draw circles |
bsalomon | 4f3a0ca | 2016-08-22 13:14:26 -0700 | [diff] [blame] | 3008 | SkScalar width = oval.width(); |
Jim Van Verth | d952a99 | 2017-04-20 17:25:26 -0400 | [diff] [blame] | 3009 | if (width > SK_ScalarNearlyZero && SkScalarNearlyEqual(width, oval.height()) && |
| 3010 | circle_stays_circle(viewMatrix)) { |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 3011 | auto r = width / 2.f; |
bsalomon | 4f3a0ca | 2016-08-22 13:14:26 -0700 | [diff] [blame] | 3012 | SkPoint center = {oval.centerX(), oval.centerY()}; |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 3013 | if (style.hasNonDashPathEffect()) { |
| 3014 | return nullptr; |
| 3015 | } else if (style.isDashed()) { |
| 3016 | if (style.strokeRec().getCap() != SkPaint::kButt_Cap || |
| 3017 | style.dashIntervalCnt() != 2 || style.strokeRec().getWidth() >= width) { |
| 3018 | return nullptr; |
| 3019 | } |
| 3020 | auto onInterval = style.dashIntervals()[0]; |
| 3021 | auto offInterval = style.dashIntervals()[1]; |
| 3022 | if (offInterval == 0) { |
| 3023 | GrStyle strokeStyle(style.strokeRec(), nullptr); |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 3024 | return MakeOvalOp(context, std::move(paint), viewMatrix, oval, |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 3025 | strokeStyle, shaderCaps); |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 3026 | } else if (onInterval == 0) { |
| 3027 | // There is nothing to draw but we have no way to indicate that here. |
| 3028 | return nullptr; |
| 3029 | } |
| 3030 | auto angularOnInterval = onInterval / r; |
| 3031 | auto angularOffInterval = offInterval / r; |
| 3032 | auto phaseAngle = style.dashPhase() / r; |
| 3033 | // Currently this function doesn't accept ovals with different start angles, though |
| 3034 | // it could. |
| 3035 | static const SkScalar kStartAngle = 0.f; |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 3036 | return ButtCapDashedCircleOp::Make(context, std::move(paint), viewMatrix, center, r, |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 3037 | style.strokeRec().getWidth(), kStartAngle, |
| 3038 | angularOnInterval, angularOffInterval, phaseAngle); |
| 3039 | } |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 3040 | return CircleOp::Make(context, std::move(paint), viewMatrix, center, r, style); |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 3041 | } |
| 3042 | |
| 3043 | if (style.pathEffect()) { |
| 3044 | return nullptr; |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 3045 | } |
| 3046 | |
Stan Iliev | eb868aa | 2017-02-21 11:06:16 -0500 | [diff] [blame] | 3047 | // prefer the device space ellipse op for batchability |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 3048 | if (viewMatrix.rectStaysRect()) { |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 3049 | return EllipseOp::Make(context, std::move(paint), viewMatrix, oval, style.strokeRec()); |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 3050 | } |
| 3051 | |
Stan Iliev | eb868aa | 2017-02-21 11:06:16 -0500 | [diff] [blame] | 3052 | // Otherwise, if we have shader derivative support, render as device-independent |
| 3053 | if (shaderCaps->shaderDerivativeSupport()) { |
Jim Van Verth | a925bb0 | 2018-09-25 10:49:52 -0400 | [diff] [blame] | 3054 | SkScalar a = viewMatrix[SkMatrix::kMScaleX]; |
| 3055 | SkScalar b = viewMatrix[SkMatrix::kMSkewX]; |
| 3056 | SkScalar c = viewMatrix[SkMatrix::kMSkewY]; |
| 3057 | SkScalar d = viewMatrix[SkMatrix::kMScaleY]; |
| 3058 | // Check for near-degenerate matrix |
| 3059 | if (a*a + c*c > SK_ScalarNearlyZero && b*b + d*d > SK_ScalarNearlyZero) { |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 3060 | return DIEllipseOp::Make(context, std::move(paint), viewMatrix, oval, |
Jim Van Verth | a925bb0 | 2018-09-25 10:49:52 -0400 | [diff] [blame] | 3061 | style.strokeRec()); |
| 3062 | } |
Stan Iliev | eb868aa | 2017-02-21 11:06:16 -0500 | [diff] [blame] | 3063 | } |
| 3064 | |
bsalomon | 4b4a7cc | 2016-07-08 04:42:54 -0700 | [diff] [blame] | 3065 | return nullptr; |
| 3066 | } |
| 3067 | |
| 3068 | /////////////////////////////////////////////////////////////////////////////// |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 3069 | |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 3070 | std::unique_ptr<GrDrawOp> GrOvalOpFactory::MakeArcOp(GrRecordingContext* context, |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 3071 | GrPaint&& paint, |
| 3072 | const SkMatrix& viewMatrix, |
Brian Salomon | ea26d6b | 2018-01-23 20:33:21 +0000 | [diff] [blame] | 3073 | const SkRect& oval, SkScalar startAngle, |
| 3074 | SkScalar sweepAngle, bool useCenter, |
| 3075 | const GrStyle& style, |
| 3076 | const GrShaderCaps* shaderCaps) { |
bsalomon | 21af9ca | 2016-08-25 12:29:23 -0700 | [diff] [blame] | 3077 | SkASSERT(!oval.isEmpty()); |
| 3078 | SkASSERT(sweepAngle); |
bsalomon | 4f3a0ca | 2016-08-22 13:14:26 -0700 | [diff] [blame] | 3079 | SkScalar width = oval.width(); |
bsalomon | 21af9ca | 2016-08-25 12:29:23 -0700 | [diff] [blame] | 3080 | if (SkScalarAbs(sweepAngle) >= 360.f) { |
| 3081 | return nullptr; |
| 3082 | } |
bsalomon | 4f3a0ca | 2016-08-22 13:14:26 -0700 | [diff] [blame] | 3083 | if (!SkScalarNearlyEqual(width, oval.height()) || !circle_stays_circle(viewMatrix)) { |
| 3084 | return nullptr; |
| 3085 | } |
| 3086 | SkPoint center = {oval.centerX(), oval.centerY()}; |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 3087 | CircleOp::ArcParams arcParams = {SkDegreesToRadians(startAngle), SkDegreesToRadians(sweepAngle), |
| 3088 | useCenter}; |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 3089 | return CircleOp::Make(context, std::move(paint), viewMatrix, |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 3090 | center, width / 2.f, style, &arcParams); |
bsalomon | 4f3a0ca | 2016-08-22 13:14:26 -0700 | [diff] [blame] | 3091 | } |
| 3092 | |
| 3093 | /////////////////////////////////////////////////////////////////////////////// |
| 3094 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 3095 | #if GR_TEST_UTILS |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 3096 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 3097 | GR_DRAW_OP_TEST_DEFINE(CircleOp) { |
bsalomon | 4f3a0ca | 2016-08-22 13:14:26 -0700 | [diff] [blame] | 3098 | do { |
bsalomon | cadf75a | 2016-08-22 14:24:24 -0700 | [diff] [blame] | 3099 | SkScalar rotate = random->nextSScalar1() * 360.f; |
| 3100 | SkScalar translateX = random->nextSScalar1() * 1000.f; |
| 3101 | SkScalar translateY = random->nextSScalar1() * 1000.f; |
Brian Osman | 4462c04 | 2018-06-08 16:35:44 -0400 | [diff] [blame] | 3102 | SkScalar scale; |
| 3103 | do { |
| 3104 | scale = random->nextSScalar1() * 100.f; |
| 3105 | } while (scale == 0); |
bsalomon | cadf75a | 2016-08-22 14:24:24 -0700 | [diff] [blame] | 3106 | SkMatrix viewMatrix; |
| 3107 | viewMatrix.setRotate(rotate); |
| 3108 | viewMatrix.postTranslate(translateX, translateY); |
| 3109 | viewMatrix.postScale(scale, scale); |
bsalomon | 4f3a0ca | 2016-08-22 13:14:26 -0700 | [diff] [blame] | 3110 | SkRect circle = GrTest::TestSquare(random); |
| 3111 | SkPoint center = {circle.centerX(), circle.centerY()}; |
| 3112 | SkScalar radius = circle.width() / 2.f; |
| 3113 | SkStrokeRec stroke = GrTest::TestStrokeRec(random); |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 3114 | CircleOp::ArcParams arcParamsTmp; |
| 3115 | const CircleOp::ArcParams* arcParams = nullptr; |
bsalomon | 4f3a0ca | 2016-08-22 13:14:26 -0700 | [diff] [blame] | 3116 | if (random->nextBool()) { |
| 3117 | arcParamsTmp.fStartAngleRadians = random->nextSScalar1() * SK_ScalarPI * 2; |
robertphillips | 08197b2 | 2016-08-23 06:19:15 -0700 | [diff] [blame] | 3118 | arcParamsTmp.fSweepAngleRadians = random->nextSScalar1() * SK_ScalarPI * 2 - .01f; |
| 3119 | arcParamsTmp.fUseCenter = random->nextBool(); |
bsalomon | 4f3a0ca | 2016-08-22 13:14:26 -0700 | [diff] [blame] | 3120 | arcParams = &arcParamsTmp; |
| 3121 | } |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 3122 | std::unique_ptr<GrDrawOp> op = CircleOp::Make(context, std::move(paint), viewMatrix, |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 3123 | center, radius, |
Brian Salomon | ea26d6b | 2018-01-23 20:33:21 +0000 | [diff] [blame] | 3124 | GrStyle(stroke, nullptr), arcParams); |
Brian Salomon | 289e3d8 | 2016-12-14 15:52:56 -0500 | [diff] [blame] | 3125 | if (op) { |
Brian Salomon | 5ec9def | 2016-12-20 15:34:05 -0500 | [diff] [blame] | 3126 | return op; |
bsalomon | 4f3a0ca | 2016-08-22 13:14:26 -0700 | [diff] [blame] | 3127 | } |
Mike Klein | 1688507 | 2018-12-11 09:54:31 -0500 | [diff] [blame] | 3128 | assert_alive(paint); |
bsalomon | 4f3a0ca | 2016-08-22 13:14:26 -0700 | [diff] [blame] | 3129 | } while (true); |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 3130 | } |
| 3131 | |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 3132 | GR_DRAW_OP_TEST_DEFINE(ButtCapDashedCircleOp) { |
| 3133 | SkScalar rotate = random->nextSScalar1() * 360.f; |
| 3134 | SkScalar translateX = random->nextSScalar1() * 1000.f; |
| 3135 | SkScalar translateY = random->nextSScalar1() * 1000.f; |
Brian Osman | 4462c04 | 2018-06-08 16:35:44 -0400 | [diff] [blame] | 3136 | SkScalar scale; |
| 3137 | do { |
| 3138 | scale = random->nextSScalar1() * 100.f; |
| 3139 | } while (scale == 0); |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 3140 | SkMatrix viewMatrix; |
| 3141 | viewMatrix.setRotate(rotate); |
| 3142 | viewMatrix.postTranslate(translateX, translateY); |
| 3143 | viewMatrix.postScale(scale, scale); |
| 3144 | SkRect circle = GrTest::TestSquare(random); |
| 3145 | SkPoint center = {circle.centerX(), circle.centerY()}; |
| 3146 | SkScalar radius = circle.width() / 2.f; |
| 3147 | SkScalar strokeWidth = random->nextRangeScalar(0.001f * radius, 1.8f * radius); |
| 3148 | SkScalar onAngle = random->nextRangeScalar(0.01f, 1000.f); |
| 3149 | SkScalar offAngle = random->nextRangeScalar(0.01f, 1000.f); |
| 3150 | SkScalar startAngle = random->nextRangeScalar(-1000.f, 1000.f); |
| 3151 | SkScalar phase = random->nextRangeScalar(-1000.f, 1000.f); |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 3152 | return ButtCapDashedCircleOp::Make(context, std::move(paint), viewMatrix, |
| 3153 | center, radius, strokeWidth, |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 3154 | startAngle, onAngle, offAngle, phase); |
| 3155 | } |
| 3156 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 3157 | GR_DRAW_OP_TEST_DEFINE(EllipseOp) { |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 3158 | SkMatrix viewMatrix = GrTest::TestMatrixRectStaysRect(random); |
joshualitt | 6c89110 | 2015-05-13 08:51:49 -0700 | [diff] [blame] | 3159 | SkRect ellipse = GrTest::TestSquare(random); |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 3160 | return EllipseOp::Make(context, std::move(paint), viewMatrix, ellipse, |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 3161 | GrTest::TestStrokeRec(random)); |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 3162 | } |
| 3163 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 3164 | GR_DRAW_OP_TEST_DEFINE(DIEllipseOp) { |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 3165 | SkMatrix viewMatrix = GrTest::TestMatrix(random); |
joshualitt | 6c89110 | 2015-05-13 08:51:49 -0700 | [diff] [blame] | 3166 | SkRect ellipse = GrTest::TestSquare(random); |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 3167 | return DIEllipseOp::Make(context, std::move(paint), viewMatrix, ellipse, |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 3168 | GrTest::TestStrokeRec(random)); |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 3169 | } |
| 3170 | |
Brian Salomon | 05441c4 | 2017-05-15 16:45:49 -0400 | [diff] [blame] | 3171 | GR_DRAW_OP_TEST_DEFINE(RRectOp) { |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 3172 | SkMatrix viewMatrix = GrTest::TestMatrixRectStaysRect(random); |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 3173 | const SkRRect& rrect = GrTest::TestRRectSimple(random); |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 3174 | return make_rrect_op(context, std::move(paint), viewMatrix, rrect, |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 3175 | GrTest::TestStrokeRec(random)); |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 3176 | } |
| 3177 | |
| 3178 | #endif |