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