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