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