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