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