Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/ccpr/GrCCStroker.h" |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkStrokeRec.h" |
| 11 | #include "src/core/SkPathPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "src/gpu/GrOnFlushResourceProvider.h" |
Greg Daniel | 2d41d0d | 2019-08-26 11:08:51 -0400 | [diff] [blame] | 13 | #include "src/gpu/GrOpsRenderPass.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "src/gpu/ccpr/GrCCCoverageProcessor.h" |
| 15 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
| 16 | #include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h" |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 17 | |
| 18 | static constexpr int kMaxNumLinearSegmentsLog2 = GrCCStrokeGeometry::kMaxNumLinearSegmentsLog2; |
| 19 | using TriangleInstance = GrCCCoverageProcessor::TriPointInstance; |
| 20 | using ConicInstance = GrCCCoverageProcessor::QuadPointInstance; |
| 21 | |
| 22 | namespace { |
| 23 | |
| 24 | struct LinearStrokeInstance { |
| 25 | float fEndpoints[4]; |
| 26 | float fStrokeRadius; |
| 27 | |
| 28 | inline void set(const SkPoint[2], float dx, float dy, float strokeRadius); |
| 29 | }; |
| 30 | |
| 31 | inline void LinearStrokeInstance::set(const SkPoint P[2], float dx, float dy, float strokeRadius) { |
| 32 | Sk2f X, Y; |
| 33 | Sk2f::Load2(P, &X, &Y); |
| 34 | Sk2f::Store2(fEndpoints, X + dx, Y + dy); |
| 35 | fStrokeRadius = strokeRadius; |
| 36 | } |
| 37 | |
| 38 | struct CubicStrokeInstance { |
| 39 | float fX[4]; |
| 40 | float fY[4]; |
| 41 | float fStrokeRadius; |
| 42 | float fNumSegments; |
| 43 | |
| 44 | inline void set(const SkPoint[4], float dx, float dy, float strokeRadius, int numSegments); |
| 45 | inline void set(const Sk4f& X, const Sk4f& Y, float dx, float dy, float strokeRadius, |
| 46 | int numSegments); |
| 47 | }; |
| 48 | |
| 49 | inline void CubicStrokeInstance::set(const SkPoint P[4], float dx, float dy, float strokeRadius, |
| 50 | int numSegments) { |
| 51 | Sk4f X, Y; |
| 52 | Sk4f::Load2(P, &X, &Y); |
| 53 | this->set(X, Y, dx, dy, strokeRadius, numSegments); |
| 54 | } |
| 55 | |
| 56 | inline void CubicStrokeInstance::set(const Sk4f& X, const Sk4f& Y, float dx, float dy, |
| 57 | float strokeRadius, int numSegments) { |
| 58 | (X + dx).store(&fX); |
| 59 | (Y + dy).store(&fY); |
| 60 | fStrokeRadius = strokeRadius; |
| 61 | fNumSegments = static_cast<float>(numSegments); |
| 62 | } |
| 63 | |
| 64 | // This class draws stroked lines in post-transform device space (a.k.a. rectangles). Rigid-body |
| 65 | // transforms can be achieved by transforming the line ahead of time and adjusting the stroke |
| 66 | // width. Skews of the stroke itself are not yet supported. |
| 67 | // |
| 68 | // Corner coverage is AA-correct, meaning, n^2 attenuation along the diagonals. This is important |
| 69 | // for seamless integration with the connecting geometry. |
| 70 | class LinearStrokeProcessor : public GrGeometryProcessor { |
| 71 | public: |
| 72 | LinearStrokeProcessor() : GrGeometryProcessor(kLinearStrokeProcessor_ClassID) { |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 73 | this->setInstanceAttributes(kInstanceAttribs, 2); |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 74 | #ifdef SK_DEBUG |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 75 | using Instance = LinearStrokeInstance; |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 76 | SkASSERT(this->instanceStride() == sizeof(Instance)); |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 77 | #endif |
| 78 | } |
| 79 | |
| 80 | private: |
| 81 | const char* name() const override { return "LinearStrokeProcessor"; } |
| 82 | void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {} |
| 83 | |
| 84 | static constexpr Attribute kInstanceAttribs[2] = { |
Brian Osman | d4c2970 | 2018-09-14 16:16:55 -0400 | [diff] [blame] | 85 | {"endpts", kFloat4_GrVertexAttribType, kFloat4_GrSLType}, |
| 86 | {"stroke_radius", kFloat_GrVertexAttribType, kFloat_GrSLType} |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 87 | }; |
| 88 | |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 89 | class Impl : public GrGLSLGeometryProcessor { |
| 90 | void setData(const GrGLSLProgramDataManager&, const GrPrimitiveProcessor&, |
| 91 | FPCoordTransformIter&&) override {} |
| 92 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override; |
| 93 | }; |
| 94 | |
| 95 | GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override { |
| 96 | return new Impl(); |
| 97 | } |
| 98 | }; |
| 99 | |
| 100 | void LinearStrokeProcessor::Impl::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) { |
| 101 | GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler; |
| 102 | GrGLSLUniformHandler* uniHandler = args.fUniformHandler; |
| 103 | |
| 104 | varyingHandler->emitAttributes(args.fGP.cast<LinearStrokeProcessor>()); |
| 105 | |
| 106 | GrGLSLVertexBuilder* v = args.fVertBuilder; |
| 107 | v->codeAppend ("float2 tan = normalize(endpts.zw - endpts.xy);"); |
| 108 | v->codeAppend ("float2 n = float2(tan.y, -tan.x);"); |
| 109 | v->codeAppend ("float nwidth = abs(n.x) + abs(n.y);"); |
| 110 | |
| 111 | // Outset the vertex position for AA butt caps. |
| 112 | v->codeAppend ("float2 outset = tan*nwidth/2;"); |
| 113 | v->codeAppend ("float2 position = (sk_VertexID < 2) " |
| 114 | "? endpts.xy - outset : endpts.zw + outset;"); |
| 115 | |
| 116 | // Calculate Manhattan distance from both butt caps, where distance=0 on the actual endpoint and |
| 117 | // distance=-.5 on the outset edge. |
| 118 | GrGLSLVarying edgeDistances(kFloat4_GrSLType); |
| 119 | varyingHandler->addVarying("edge_distances", &edgeDistances); |
| 120 | v->codeAppendf("%s.xz = float2(-.5, dot(endpts.zw - endpts.xy, tan) / nwidth + .5);", |
| 121 | edgeDistances.vsOut()); |
| 122 | v->codeAppendf("%s.xz = (sk_VertexID < 2) ? %s.xz : %s.zx;", |
| 123 | edgeDistances.vsOut(), edgeDistances.vsOut(), edgeDistances.vsOut()); |
| 124 | |
| 125 | // Outset the vertex position for stroke radius plus edge AA. |
| 126 | v->codeAppend ("outset = n * (stroke_radius + nwidth/2);"); |
| 127 | v->codeAppend ("position += (0 == (sk_VertexID & 1)) ? +outset : -outset;"); |
| 128 | |
| 129 | // Calculate Manhattan distance from both edges, where distance=0 on the actual edge and |
| 130 | // distance=-.5 on the outset. |
| 131 | v->codeAppendf("%s.yw = float2(-.5, 2*stroke_radius / nwidth + .5);", edgeDistances.vsOut()); |
| 132 | v->codeAppendf("%s.yw = (0 == (sk_VertexID & 1)) ? %s.yw : %s.wy;", |
| 133 | edgeDistances.vsOut(), edgeDistances.vsOut(), edgeDistances.vsOut()); |
| 134 | |
| 135 | gpArgs->fPositionVar.set(kFloat2_GrSLType, "position"); |
| 136 | this->emitTransforms(v, varyingHandler, uniHandler, GrShaderVar("position", kFloat2_GrSLType), |
| 137 | SkMatrix::I(), args.fFPCoordTransformHandler); |
| 138 | |
| 139 | // Use the 4 edge distances to calculate coverage in the fragment shader. |
| 140 | GrGLSLFPFragmentBuilder* f = args.fFragBuilder; |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 141 | f->codeAppendf("half2 coverages = half2(min(%s.xy, .5) + min(%s.zw, .5));", |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 142 | edgeDistances.fsIn(), edgeDistances.fsIn()); |
| 143 | f->codeAppendf("%s = half4(coverages.x * coverages.y);", args.fOutputColor); |
| 144 | |
| 145 | // This shader doesn't use the built-in Ganesh coverage. |
| 146 | f->codeAppendf("%s = half4(1);", args.fOutputCoverage); |
| 147 | } |
| 148 | |
| 149 | constexpr GrPrimitiveProcessor::Attribute LinearStrokeProcessor::kInstanceAttribs[]; |
| 150 | |
| 151 | // This class draws stroked cubics in post-transform device space. Rigid-body transforms can be |
| 152 | // achieved by transforming the curve ahead of time and adjusting the stroke width. Skews of the |
| 153 | // stroke itself are not yet supported. Quadratics can be drawn by converting them to cubics. |
| 154 | // |
| 155 | // This class works by finding stroke-width line segments orthogonal to the curve at a |
| 156 | // pre-determined number of evenly spaced points along the curve (evenly spaced in the parametric |
| 157 | // sense). It then connects the segments with a triangle strip. As for common in CCPR, clockwise- |
| 158 | // winding triangles from the strip emit positive coverage, counter-clockwise triangles emit |
| 159 | // negative, and we use SkBlendMode::kPlus. |
| 160 | class CubicStrokeProcessor : public GrGeometryProcessor { |
| 161 | public: |
| 162 | CubicStrokeProcessor() : GrGeometryProcessor(kCubicStrokeProcessor_ClassID) { |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 163 | this->setInstanceAttributes(kInstanceAttribs, 3); |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 164 | #ifdef SK_DEBUG |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 165 | using Instance = CubicStrokeInstance; |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 166 | SkASSERT(this->instanceStride() == sizeof(Instance)); |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 167 | #endif |
| 168 | } |
| 169 | |
| 170 | private: |
| 171 | const char* name() const override { return "CubicStrokeProcessor"; } |
| 172 | void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {} |
| 173 | |
| 174 | static constexpr Attribute kInstanceAttribs[3] = { |
Brian Osman | d4c2970 | 2018-09-14 16:16:55 -0400 | [diff] [blame] | 175 | {"X", kFloat4_GrVertexAttribType, kFloat4_GrSLType}, |
| 176 | {"Y", kFloat4_GrVertexAttribType, kFloat4_GrSLType}, |
| 177 | {"stroke_info", kFloat2_GrVertexAttribType, kFloat2_GrSLType} |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 178 | }; |
| 179 | |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 180 | class Impl : public GrGLSLGeometryProcessor { |
| 181 | void setData(const GrGLSLProgramDataManager&, const GrPrimitiveProcessor&, |
| 182 | FPCoordTransformIter&&) override {} |
| 183 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override; |
| 184 | }; |
| 185 | |
| 186 | GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override { |
| 187 | return new Impl(); |
| 188 | } |
| 189 | }; |
| 190 | |
| 191 | void CubicStrokeProcessor::Impl::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) { |
| 192 | GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler; |
| 193 | GrGLSLUniformHandler* uniHandler = args.fUniformHandler; |
| 194 | |
| 195 | varyingHandler->emitAttributes(args.fGP.cast<CubicStrokeProcessor>()); |
| 196 | |
| 197 | GrGLSLVertexBuilder* v = args.fVertBuilder; |
| 198 | v->codeAppend ("float4x2 P = transpose(float2x4(X, Y));"); |
| 199 | v->codeAppend ("float stroke_radius = stroke_info[0];"); |
| 200 | v->codeAppend ("float num_segments = stroke_info[1];"); |
| 201 | |
| 202 | // Find the parametric T value at which we will emit our orthogonal line segment. We emit two |
| 203 | // line segments at T=0 and double at T=1 as well for AA butt caps. |
| 204 | v->codeAppend ("float point_id = float(sk_VertexID/2);"); |
| 205 | v->codeAppend ("float T = max((point_id - 1) / num_segments, 0);"); |
| 206 | v->codeAppend ("T = (point_id >= num_segments + 1) ? 1 : T;"); // In case x/x !== 1. |
| 207 | |
| 208 | // Use De Casteljau's algorithm to find the position and tangent for our orthogonal line |
| 209 | // segment. De Casteljau's is more numerically stable than evaluating the curve and derivative |
| 210 | // directly. |
| 211 | v->codeAppend ("float2 ab = mix(P[0], P[1], T);"); |
| 212 | v->codeAppend ("float2 bc = mix(P[1], P[2], T);"); |
| 213 | v->codeAppend ("float2 cd = mix(P[2], P[3], T);"); |
| 214 | v->codeAppend ("float2 abc = mix(ab, bc, T);"); |
| 215 | v->codeAppend ("float2 bcd = mix(bc, cd, T);"); |
| 216 | v->codeAppend ("float2 position = mix(abc, bcd, T);"); |
| 217 | v->codeAppend ("float2 tan = bcd - abc;"); |
| 218 | |
| 219 | // Find actual tangents for the corner cases when De Casteljau's yields tan=0. (We shouldn't |
| 220 | // encounter other numerically unstable cases where tan ~= 0, because GrCCStrokeGeometry snaps |
| 221 | // control points to endpoints in curves where they are almost equal.) |
| 222 | v->codeAppend ("if (0 == T && P[0] == P[1]) {"); |
| 223 | v->codeAppend ( "tan = P[2] - P[0];"); |
| 224 | v->codeAppend ("}"); |
| 225 | v->codeAppend ("if (1 == T && P[2] == P[3]) {"); |
| 226 | v->codeAppend ( "tan = P[3] - P[1];"); |
| 227 | v->codeAppend ("}"); |
| 228 | v->codeAppend ("tan = normalize(tan);"); |
| 229 | v->codeAppend ("float2 n = float2(tan.y, -tan.x);"); |
| 230 | v->codeAppend ("float nwidth = abs(n.x) + abs(n.y);"); |
| 231 | |
| 232 | // Outset the vertex position for stroke radius plus edge AA. |
| 233 | v->codeAppend ("float2 outset = n * (stroke_radius + nwidth/2);"); |
| 234 | v->codeAppend ("position += (0 == (sk_VertexID & 1)) ? -outset : +outset;"); |
| 235 | |
| 236 | // Calculate the Manhattan distance from both edges, where distance=0 on the actual edge and |
| 237 | // distance=-.5 on the outset. |
| 238 | GrGLSLVarying coverages(kFloat3_GrSLType); |
| 239 | varyingHandler->addVarying("coverages", &coverages); |
| 240 | v->codeAppendf("%s.xy = float2(-.5, 2*stroke_radius / nwidth + .5);", coverages.vsOut()); |
| 241 | v->codeAppendf("%s.xy = (0 == (sk_VertexID & 1)) ? %s.xy : %s.yx;", |
| 242 | coverages.vsOut(), coverages.vsOut(), coverages.vsOut()); |
| 243 | |
| 244 | // Adjust the orthogonal line segments on the endpoints so they straddle the actual endpoint |
| 245 | // at a Manhattan distance of .5 on either side. |
| 246 | v->codeAppend ("if (0 == point_id || num_segments+1 == point_id) {"); |
| 247 | v->codeAppend ( "position -= tan*nwidth/2;"); |
| 248 | v->codeAppend ("}"); |
| 249 | v->codeAppend ("if (1 == point_id || num_segments+2 == point_id) {"); |
| 250 | v->codeAppend ( "position += tan*nwidth/2;"); |
| 251 | v->codeAppend ("}"); |
| 252 | |
| 253 | // Interpolate coverage for butt cap AA from 0 on the outer segment to 1 on the inner. |
| 254 | v->codeAppendf("%s.z = (0 == point_id || num_segments+2 == point_id) ? 0 : 1;", |
| 255 | coverages.vsOut()); |
| 256 | |
| 257 | gpArgs->fPositionVar.set(kFloat2_GrSLType, "position"); |
| 258 | this->emitTransforms(v, varyingHandler, uniHandler, GrShaderVar("position", kFloat2_GrSLType), |
| 259 | SkMatrix::I(), args.fFPCoordTransformHandler); |
| 260 | |
| 261 | // Use the 2 edge distances and interpolated butt cap AA to calculate fragment coverage. |
| 262 | GrGLSLFPFragmentBuilder* f = args.fFragBuilder; |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 263 | f->codeAppendf("half2 edge_coverages = min(half2(%s.xy), .5);", coverages.fsIn()); |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 264 | f->codeAppend ("half coverage = edge_coverages.x + edge_coverages.y;"); |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 265 | f->codeAppendf("coverage *= half(%s.z);", coverages.fsIn()); // Butt cap AA. |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 266 | |
| 267 | // As is common for CCPR, clockwise-winding triangles from the strip emit positive coverage, and |
| 268 | // counter-clockwise triangles emit negative. |
| 269 | f->codeAppendf("%s = half4(sk_Clockwise ? +coverage : -coverage);", args.fOutputColor); |
| 270 | |
| 271 | // This shader doesn't use the built-in Ganesh coverage. |
| 272 | f->codeAppendf("%s = half4(1);", args.fOutputCoverage); |
| 273 | } |
| 274 | |
| 275 | constexpr GrPrimitiveProcessor::Attribute CubicStrokeProcessor::kInstanceAttribs[]; |
| 276 | |
| 277 | } // anonymous namespace |
| 278 | |
| 279 | void GrCCStroker::parseDeviceSpaceStroke(const SkPath& path, const SkPoint* deviceSpacePts, |
| 280 | const SkStrokeRec& stroke, float strokeDevWidth, |
| 281 | GrScissorTest scissorTest, |
| 282 | const SkIRect& clippedDevIBounds, |
| 283 | const SkIVector& devToAtlasOffset) { |
| 284 | SkASSERT(SkStrokeRec::kStroke_Style == stroke.getStyle() || |
| 285 | SkStrokeRec::kHairline_Style == stroke.getStyle()); |
| 286 | SkASSERT(!fInstanceBuffer); |
| 287 | SkASSERT(!path.isEmpty()); |
| 288 | |
| 289 | if (!fHasOpenBatch) { |
| 290 | fBatches.emplace_back(&fTalliesAllocator, *fInstanceCounts[(int)GrScissorTest::kDisabled], |
| 291 | fScissorSubBatches.count()); |
| 292 | fInstanceCounts[(int)GrScissorTest::kDisabled] = fBatches.back().fNonScissorEndInstances; |
| 293 | fHasOpenBatch = true; |
| 294 | } |
| 295 | |
| 296 | InstanceTallies* currStrokeEndIndices; |
| 297 | if (GrScissorTest::kEnabled == scissorTest) { |
| 298 | SkASSERT(fBatches.back().fEndScissorSubBatch == fScissorSubBatches.count()); |
| 299 | fScissorSubBatches.emplace_back( |
| 300 | &fTalliesAllocator, *fInstanceCounts[(int)GrScissorTest::kEnabled], |
| 301 | clippedDevIBounds.makeOffset(devToAtlasOffset.x(), devToAtlasOffset.y())); |
| 302 | fBatches.back().fEndScissorSubBatch = fScissorSubBatches.count(); |
| 303 | fInstanceCounts[(int)GrScissorTest::kEnabled] = |
| 304 | currStrokeEndIndices = fScissorSubBatches.back().fEndInstances; |
| 305 | } else { |
| 306 | currStrokeEndIndices = fBatches.back().fNonScissorEndInstances; |
| 307 | } |
| 308 | |
| 309 | fGeometry.beginPath(stroke, strokeDevWidth, currStrokeEndIndices); |
| 310 | |
| 311 | fPathInfos.push_back() = {devToAtlasOffset, strokeDevWidth/2, scissorTest}; |
| 312 | |
| 313 | int devPtsIdx = 0; |
| 314 | SkPath::Verb previousVerb = SkPath::kClose_Verb; |
| 315 | |
| 316 | for (SkPath::Verb verb : SkPathPriv::Verbs(path)) { |
| 317 | SkASSERT(SkPath::kDone_Verb != previousVerb); |
| 318 | const SkPoint* P = &deviceSpacePts[devPtsIdx - 1]; |
| 319 | switch (verb) { |
| 320 | case SkPath::kMove_Verb: |
| 321 | if (devPtsIdx > 0 && SkPath::kClose_Verb != previousVerb) { |
| 322 | fGeometry.capContourAndExit(); |
| 323 | } |
| 324 | fGeometry.moveTo(deviceSpacePts[devPtsIdx]); |
| 325 | ++devPtsIdx; |
| 326 | break; |
| 327 | case SkPath::kClose_Verb: |
| 328 | SkASSERT(SkPath::kClose_Verb != previousVerb); |
| 329 | fGeometry.closeContour(); |
| 330 | break; |
| 331 | case SkPath::kLine_Verb: |
| 332 | SkASSERT(SkPath::kClose_Verb != previousVerb); |
| 333 | fGeometry.lineTo(P[1]); |
| 334 | ++devPtsIdx; |
| 335 | break; |
| 336 | case SkPath::kQuad_Verb: |
| 337 | SkASSERT(SkPath::kClose_Verb != previousVerb); |
| 338 | fGeometry.quadraticTo(P); |
| 339 | devPtsIdx += 2; |
| 340 | break; |
| 341 | case SkPath::kCubic_Verb: { |
| 342 | SkASSERT(SkPath::kClose_Verb != previousVerb); |
| 343 | fGeometry.cubicTo(P); |
| 344 | devPtsIdx += 3; |
| 345 | break; |
| 346 | } |
| 347 | case SkPath::kConic_Verb: |
| 348 | SkASSERT(SkPath::kClose_Verb != previousVerb); |
| 349 | SK_ABORT("Stroked conics not supported."); |
| 350 | break; |
| 351 | case SkPath::kDone_Verb: |
| 352 | break; |
| 353 | } |
| 354 | previousVerb = verb; |
| 355 | } |
| 356 | |
| 357 | if (devPtsIdx > 0 && SkPath::kClose_Verb != previousVerb) { |
| 358 | fGeometry.capContourAndExit(); |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | // This class encapsulates the process of expanding ready-to-draw geometry from GrCCStrokeGeometry |
| 363 | // directly into GPU instance buffers. |
| 364 | class GrCCStroker::InstanceBufferBuilder { |
| 365 | public: |
| 366 | InstanceBufferBuilder(GrOnFlushResourceProvider* onFlushRP, GrCCStroker* stroker) { |
| 367 | memcpy(fNextInstances, stroker->fBaseInstances, sizeof(fNextInstances)); |
| 368 | #ifdef SK_DEBUG |
| 369 | fEndInstances[0] = stroker->fBaseInstances[0] + *stroker->fInstanceCounts[0]; |
| 370 | fEndInstances[1] = stroker->fBaseInstances[1] + *stroker->fInstanceCounts[1]; |
| 371 | #endif |
| 372 | |
| 373 | int endConicsIdx = stroker->fBaseInstances[1].fConics + |
| 374 | stroker->fInstanceCounts[1]->fConics; |
Brian Salomon | ae64c19 | 2019-02-05 09:41:37 -0500 | [diff] [blame] | 375 | fInstanceBuffer = onFlushRP->makeBuffer(GrGpuBufferType::kVertex, |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 376 | endConicsIdx * sizeof(ConicInstance)); |
| 377 | if (!fInstanceBuffer) { |
| 378 | SkDebugf("WARNING: failed to allocate CCPR stroke instance buffer.\n"); |
| 379 | return; |
| 380 | } |
| 381 | fInstanceBufferData = fInstanceBuffer->map(); |
| 382 | } |
| 383 | |
| 384 | bool isMapped() const { return SkToBool(fInstanceBufferData); } |
| 385 | |
| 386 | void updateCurrentInfo(const PathInfo& pathInfo) { |
| 387 | SkASSERT(this->isMapped()); |
| 388 | fCurrDX = static_cast<float>(pathInfo.fDevToAtlasOffset.x()); |
| 389 | fCurrDY = static_cast<float>(pathInfo.fDevToAtlasOffset.y()); |
| 390 | fCurrStrokeRadius = pathInfo.fStrokeRadius; |
| 391 | fCurrNextInstances = &fNextInstances[(int)pathInfo.fScissorTest]; |
| 392 | SkDEBUGCODE(fCurrEndInstances = &fEndInstances[(int)pathInfo.fScissorTest]); |
| 393 | } |
| 394 | |
| 395 | void appendLinearStroke(const SkPoint endpts[2]) { |
| 396 | SkASSERT(this->isMapped()); |
| 397 | this->appendLinearStrokeInstance().set(endpts, fCurrDX, fCurrDY, fCurrStrokeRadius); |
| 398 | } |
| 399 | |
| 400 | void appendQuadraticStroke(const SkPoint P[3], int numLinearSegmentsLog2) { |
| 401 | SkASSERT(this->isMapped()); |
| 402 | SkASSERT(numLinearSegmentsLog2 > 0); |
| 403 | |
| 404 | Sk4f ptsT[2]; |
| 405 | Sk2f p0 = Sk2f::Load(P); |
| 406 | Sk2f p1 = Sk2f::Load(P+1); |
| 407 | Sk2f p2 = Sk2f::Load(P+2); |
| 408 | |
| 409 | // Convert the quadratic to cubic. |
| 410 | Sk2f c1 = SkNx_fma(Sk2f(2/3.f), p1 - p0, p0); |
| 411 | Sk2f c2 = SkNx_fma(Sk2f(1/3.f), p2 - p1, p1); |
| 412 | Sk2f::Store4(ptsT, p0, c1, c2, p2); |
| 413 | |
| 414 | this->appendCubicStrokeInstance(numLinearSegmentsLog2).set( |
| 415 | ptsT[0], ptsT[1], fCurrDX, fCurrDY, fCurrStrokeRadius, 1 << numLinearSegmentsLog2); |
| 416 | } |
| 417 | |
| 418 | void appendCubicStroke(const SkPoint P[3], int numLinearSegmentsLog2) { |
| 419 | SkASSERT(this->isMapped()); |
| 420 | SkASSERT(numLinearSegmentsLog2 > 0); |
| 421 | this->appendCubicStrokeInstance(numLinearSegmentsLog2).set( |
| 422 | P, fCurrDX, fCurrDY, fCurrStrokeRadius, 1 << numLinearSegmentsLog2); |
| 423 | } |
| 424 | |
| 425 | void appendJoin(Verb joinVerb, const SkPoint& center, const SkVector& leftNorm, |
| 426 | const SkVector& rightNorm, float miterCapHeightOverWidth, float conicWeight) { |
| 427 | SkASSERT(this->isMapped()); |
| 428 | |
| 429 | Sk2f offset = Sk2f::Load(¢er) + Sk2f(fCurrDX, fCurrDY); |
| 430 | Sk2f n0 = Sk2f::Load(&leftNorm); |
| 431 | Sk2f n1 = Sk2f::Load(&rightNorm); |
| 432 | |
| 433 | // Identify the outer edge. |
| 434 | Sk2f cross = n0 * SkNx_shuffle<1,0>(n1); |
| 435 | if (cross[0] < cross[1]) { |
| 436 | Sk2f tmp = n0; |
| 437 | n0 = -n1; |
| 438 | n1 = -tmp; |
| 439 | } |
| 440 | |
| 441 | if (!GrCCStrokeGeometry::IsInternalJoinVerb(joinVerb)) { |
| 442 | // Normal joins are a triangle that connects the outer corners of two adjoining strokes. |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 443 | this->appendTriangleInstance().set( |
| 444 | n1 * fCurrStrokeRadius, Sk2f(0, 0), n0 * fCurrStrokeRadius, offset, |
| 445 | TriangleInstance::Ordering::kXYTransposed); |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 446 | if (Verb::kBevelJoin == joinVerb) { |
| 447 | return; |
| 448 | } |
| 449 | } else { |
| 450 | // Internal joins are coverage-counted, self-intersecting quadrilaterals that tie the |
| 451 | // four corners of two adjoining strokes together a like a shoelace. Coverage is |
| 452 | // negative on the inside half. We implement this geometry with a pair of triangles. |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 453 | this->appendTriangleInstance().set( |
| 454 | -n0 * fCurrStrokeRadius, n0 * fCurrStrokeRadius, n1 * fCurrStrokeRadius, |
| 455 | offset, TriangleInstance::Ordering::kXYTransposed); |
| 456 | if (Verb::kBevelJoin == joinVerb) { |
| 457 | return; |
| 458 | } |
| 459 | this->appendTriangleInstance().set( |
| 460 | -n0 * fCurrStrokeRadius, n1 * fCurrStrokeRadius, -n1 * fCurrStrokeRadius, |
| 461 | offset, TriangleInstance::Ordering::kXYTransposed); |
| 462 | if (Verb::kBevelJoin == joinVerb) { |
| 463 | return; |
| 464 | } |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 465 | if (Verb::kInternalBevelJoin == joinVerb) { |
| 466 | return; |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | // For miter and round joins, we place an additional triangle cap on top of the bevel. This |
| 471 | // triangle is literal for miters and is conic control points for round joins. |
Chris Dalton | ce038dc | 2018-09-14 14:14:49 -0600 | [diff] [blame] | 472 | SkASSERT(miterCapHeightOverWidth >= 0 || SkScalarIsNaN(miterCapHeightOverWidth)); |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 473 | Sk2f base = n1 - n0; |
| 474 | Sk2f baseNorm = Sk2f(base[1], -base[0]); |
| 475 | Sk2f c = (n0 + n1) * .5f + baseNorm * miterCapHeightOverWidth; |
| 476 | |
| 477 | if (Verb::kMiterJoin == joinVerb) { |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 478 | this->appendTriangleInstance().set( |
| 479 | n0 * fCurrStrokeRadius, c * fCurrStrokeRadius, n1 * fCurrStrokeRadius, offset, |
| 480 | TriangleInstance::Ordering::kXYTransposed); |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 481 | } else { |
| 482 | SkASSERT(Verb::kRoundJoin == joinVerb || Verb::kInternalRoundJoin == joinVerb); |
| 483 | this->appendConicInstance().setW(n0 * fCurrStrokeRadius, c * fCurrStrokeRadius, |
| 484 | n1 * fCurrStrokeRadius, offset, conicWeight); |
| 485 | if (Verb::kInternalRoundJoin == joinVerb) { |
| 486 | this->appendConicInstance().setW(-n1 * fCurrStrokeRadius, c * -fCurrStrokeRadius, |
| 487 | -n0 * fCurrStrokeRadius, offset, conicWeight); |
| 488 | } |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | void appendCap(Verb capType, const SkPoint& pt, const SkVector& norm) { |
| 493 | SkASSERT(this->isMapped()); |
| 494 | |
| 495 | Sk2f n = Sk2f::Load(&norm) * fCurrStrokeRadius; |
| 496 | Sk2f v = Sk2f(-n[1], n[0]); |
| 497 | Sk2f offset = Sk2f::Load(&pt) + Sk2f(fCurrDX, fCurrDY); |
| 498 | |
| 499 | if (Verb::kSquareCap == capType) { |
| 500 | SkPoint endPts[2] = {{0, 0}, {v[0], v[1]}}; |
| 501 | this->appendLinearStrokeInstance().set(endPts, offset[0], offset[1], fCurrStrokeRadius); |
| 502 | } else { |
| 503 | SkASSERT(Verb::kRoundCap == capType); |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 504 | this->appendTriangleInstance().set( |
| 505 | n, v, -n, offset, TriangleInstance::Ordering::kXYTransposed); |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 506 | this->appendConicInstance().setW(n, n + v, v, offset, SK_ScalarRoot2Over2); |
| 507 | this->appendConicInstance().setW(v, v - n, -n, offset, SK_ScalarRoot2Over2); |
| 508 | } |
| 509 | } |
| 510 | |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 511 | sk_sp<GrGpuBuffer> finish() { |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 512 | SkASSERT(this->isMapped()); |
| 513 | SkASSERT(!memcmp(fNextInstances, fEndInstances, sizeof(fNextInstances))); |
| 514 | fInstanceBuffer->unmap(); |
| 515 | fInstanceBufferData = nullptr; |
| 516 | SkASSERT(!this->isMapped()); |
| 517 | return std::move(fInstanceBuffer); |
| 518 | } |
| 519 | |
| 520 | private: |
| 521 | LinearStrokeInstance& appendLinearStrokeInstance() { |
| 522 | int instanceIdx = fCurrNextInstances->fStrokes[0]++; |
| 523 | SkASSERT(instanceIdx < fCurrEndInstances->fStrokes[0]); |
| 524 | |
| 525 | return reinterpret_cast<LinearStrokeInstance*>(fInstanceBufferData)[instanceIdx]; |
| 526 | } |
| 527 | |
| 528 | CubicStrokeInstance& appendCubicStrokeInstance(int numLinearSegmentsLog2) { |
| 529 | SkASSERT(numLinearSegmentsLog2 > 0); |
| 530 | SkASSERT(numLinearSegmentsLog2 <= kMaxNumLinearSegmentsLog2); |
| 531 | |
| 532 | int instanceIdx = fCurrNextInstances->fStrokes[numLinearSegmentsLog2]++; |
| 533 | SkASSERT(instanceIdx < fCurrEndInstances->fStrokes[numLinearSegmentsLog2]); |
| 534 | |
| 535 | return reinterpret_cast<CubicStrokeInstance*>(fInstanceBufferData)[instanceIdx]; |
| 536 | } |
| 537 | |
| 538 | TriangleInstance& appendTriangleInstance() { |
| 539 | int instanceIdx = fCurrNextInstances->fTriangles++; |
| 540 | SkASSERT(instanceIdx < fCurrEndInstances->fTriangles); |
| 541 | |
| 542 | return reinterpret_cast<TriangleInstance*>(fInstanceBufferData)[instanceIdx]; |
| 543 | } |
| 544 | |
| 545 | ConicInstance& appendConicInstance() { |
| 546 | int instanceIdx = fCurrNextInstances->fConics++; |
| 547 | SkASSERT(instanceIdx < fCurrEndInstances->fConics); |
| 548 | |
| 549 | return reinterpret_cast<ConicInstance*>(fInstanceBufferData)[instanceIdx]; |
| 550 | } |
| 551 | |
| 552 | float fCurrDX, fCurrDY; |
| 553 | float fCurrStrokeRadius; |
| 554 | InstanceTallies* fCurrNextInstances; |
| 555 | SkDEBUGCODE(const InstanceTallies* fCurrEndInstances); |
| 556 | |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 557 | sk_sp<GrGpuBuffer> fInstanceBuffer; |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 558 | void* fInstanceBufferData = nullptr; |
| 559 | InstanceTallies fNextInstances[2]; |
| 560 | SkDEBUGCODE(InstanceTallies fEndInstances[2]); |
| 561 | }; |
| 562 | |
| 563 | GrCCStroker::BatchID GrCCStroker::closeCurrentBatch() { |
| 564 | if (!fHasOpenBatch) { |
| 565 | return kEmptyBatchID; |
| 566 | } |
| 567 | int start = (fBatches.count() < 2) ? 0 : fBatches[fBatches.count() - 2].fEndScissorSubBatch; |
| 568 | int end = fBatches.back().fEndScissorSubBatch; |
| 569 | fMaxNumScissorSubBatches = SkTMax(fMaxNumScissorSubBatches, end - start); |
| 570 | fHasOpenBatch = false; |
| 571 | return fBatches.count() - 1; |
| 572 | } |
| 573 | |
| 574 | bool GrCCStroker::prepareToDraw(GrOnFlushResourceProvider* onFlushRP) { |
| 575 | SkASSERT(!fInstanceBuffer); |
| 576 | SkASSERT(!fHasOpenBatch); // Call closeCurrentBatch() first. |
| 577 | |
| 578 | // Here we layout a single instance buffer to share with every internal batch. |
| 579 | // |
| 580 | // Rather than place each instance array in its own GPU buffer, we allocate a single |
| 581 | // megabuffer and lay them all out side-by-side. We can offset the "baseInstance" parameter in |
| 582 | // our draw calls to direct the GPU to the applicable elements within a given array. |
| 583 | fBaseInstances[0].fStrokes[0] = 0; |
| 584 | fBaseInstances[1].fStrokes[0] = fInstanceCounts[0]->fStrokes[0]; |
| 585 | int endLinearStrokesIdx = fBaseInstances[1].fStrokes[0] + fInstanceCounts[1]->fStrokes[0]; |
| 586 | |
Brian Salomon | 24d377e | 2019-04-23 15:24:31 -0400 | [diff] [blame] | 587 | int cubicStrokesIdx = GrSizeDivRoundUp(endLinearStrokesIdx * sizeof(LinearStrokeInstance), |
| 588 | sizeof(CubicStrokeInstance)); |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 589 | for (int i = 1; i <= kMaxNumLinearSegmentsLog2; ++i) { |
| 590 | for (int j = 0; j < kNumScissorModes; ++j) { |
| 591 | fBaseInstances[j].fStrokes[i] = cubicStrokesIdx; |
| 592 | cubicStrokesIdx += fInstanceCounts[j]->fStrokes[i]; |
| 593 | } |
| 594 | } |
| 595 | |
Brian Salomon | 24d377e | 2019-04-23 15:24:31 -0400 | [diff] [blame] | 596 | int trianglesIdx = GrSizeDivRoundUp(cubicStrokesIdx * sizeof(CubicStrokeInstance), |
| 597 | sizeof(TriangleInstance)); |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 598 | fBaseInstances[0].fTriangles = trianglesIdx; |
| 599 | fBaseInstances[1].fTriangles = |
| 600 | fBaseInstances[0].fTriangles + fInstanceCounts[0]->fTriangles; |
| 601 | int endTrianglesIdx = |
| 602 | fBaseInstances[1].fTriangles + fInstanceCounts[1]->fTriangles; |
| 603 | |
Brian Salomon | 24d377e | 2019-04-23 15:24:31 -0400 | [diff] [blame] | 604 | int conicsIdx = |
| 605 | GrSizeDivRoundUp(endTrianglesIdx * sizeof(TriangleInstance), sizeof(ConicInstance)); |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 606 | fBaseInstances[0].fConics = conicsIdx; |
| 607 | fBaseInstances[1].fConics = fBaseInstances[0].fConics + fInstanceCounts[0]->fConics; |
| 608 | |
| 609 | InstanceBufferBuilder builder(onFlushRP, this); |
| 610 | if (!builder.isMapped()) { |
| 611 | return false; // Buffer allocation failed. |
| 612 | } |
| 613 | |
| 614 | // Now parse the GrCCStrokeGeometry and expand it into the instance buffer. |
| 615 | int pathIdx = 0; |
| 616 | int ptsIdx = 0; |
| 617 | int paramsIdx = 0; |
| 618 | int normalsIdx = 0; |
| 619 | |
| 620 | const SkTArray<GrCCStrokeGeometry::Parameter, true>& params = fGeometry.params(); |
| 621 | const SkTArray<SkPoint, true>& pts = fGeometry.points(); |
| 622 | const SkTArray<SkVector, true>& normals = fGeometry.normals(); |
| 623 | |
| 624 | float miterCapHeightOverWidth=0, conicWeight=0; |
| 625 | |
| 626 | for (Verb verb : fGeometry.verbs()) { |
| 627 | switch (verb) { |
| 628 | case Verb::kBeginPath: |
| 629 | builder.updateCurrentInfo(fPathInfos[pathIdx]); |
| 630 | ++pathIdx; |
| 631 | continue; |
| 632 | |
| 633 | case Verb::kLinearStroke: |
| 634 | builder.appendLinearStroke(&pts[ptsIdx]); |
| 635 | ++ptsIdx; |
| 636 | continue; |
| 637 | case Verb::kQuadraticStroke: |
| 638 | builder.appendQuadraticStroke(&pts[ptsIdx], |
| 639 | params[paramsIdx++].fNumLinearSegmentsLog2); |
| 640 | ptsIdx += 2; |
| 641 | ++normalsIdx; |
| 642 | continue; |
| 643 | case Verb::kCubicStroke: |
| 644 | builder.appendCubicStroke(&pts[ptsIdx], params[paramsIdx++].fNumLinearSegmentsLog2); |
| 645 | ptsIdx += 3; |
| 646 | ++normalsIdx; |
| 647 | continue; |
| 648 | |
| 649 | case Verb::kRoundJoin: |
| 650 | case Verb::kInternalRoundJoin: |
| 651 | conicWeight = params[paramsIdx++].fConicWeight; |
| 652 | // fallthru |
| 653 | case Verb::kMiterJoin: |
| 654 | miterCapHeightOverWidth = params[paramsIdx++].fMiterCapHeightOverWidth; |
| 655 | // fallthru |
| 656 | case Verb::kBevelJoin: |
| 657 | case Verb::kInternalBevelJoin: |
| 658 | builder.appendJoin(verb, pts[ptsIdx], normals[normalsIdx], normals[normalsIdx + 1], |
| 659 | miterCapHeightOverWidth, conicWeight); |
| 660 | ++normalsIdx; |
| 661 | continue; |
| 662 | |
| 663 | case Verb::kSquareCap: |
| 664 | case Verb::kRoundCap: |
| 665 | builder.appendCap(verb, pts[ptsIdx], normals[normalsIdx]); |
| 666 | continue; |
| 667 | |
| 668 | case Verb::kEndContour: |
| 669 | ++ptsIdx; |
| 670 | ++normalsIdx; |
| 671 | continue; |
| 672 | } |
| 673 | SK_ABORT("Invalid CCPR stroke element."); |
| 674 | } |
| 675 | |
| 676 | fInstanceBuffer = builder.finish(); |
| 677 | SkASSERT(fPathInfos.count() == pathIdx); |
| 678 | SkASSERT(pts.count() == ptsIdx); |
| 679 | SkASSERT(normals.count() == normalsIdx); |
| 680 | |
| 681 | fMeshesBuffer.reserve((1 + fMaxNumScissorSubBatches) * kMaxNumLinearSegmentsLog2); |
| 682 | fScissorsBuffer.reserve((1 + fMaxNumScissorSubBatches) * kMaxNumLinearSegmentsLog2); |
| 683 | return true; |
| 684 | } |
| 685 | |
Chris Dalton | 2c5e011 | 2019-03-29 13:14:18 -0500 | [diff] [blame] | 686 | void GrCCStroker::drawStrokes(GrOpFlushState* flushState, GrCCCoverageProcessor* proc, |
| 687 | BatchID batchID, const SkIRect& drawBounds) const { |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 688 | using PrimitiveType = GrCCCoverageProcessor::PrimitiveType; |
| 689 | SkASSERT(fInstanceBuffer); |
| 690 | |
| 691 | if (kEmptyBatchID == batchID) { |
| 692 | return; |
| 693 | } |
| 694 | const Batch& batch = fBatches[batchID]; |
| 695 | int startScissorSubBatch = (!batchID) ? 0 : fBatches[batchID - 1].fEndScissorSubBatch; |
| 696 | |
| 697 | const InstanceTallies* startIndices[2]; |
| 698 | startIndices[(int)GrScissorTest::kDisabled] = (!batchID) |
| 699 | ? &fZeroTallies : fBatches[batchID - 1].fNonScissorEndInstances; |
| 700 | startIndices[(int)GrScissorTest::kEnabled] = (!startScissorSubBatch) |
| 701 | ? &fZeroTallies : fScissorSubBatches[startScissorSubBatch - 1].fEndInstances; |
| 702 | |
Greg Daniel | 2c3398d | 2019-06-19 11:58:01 -0400 | [diff] [blame] | 703 | GrPipeline pipeline(GrScissorTest::kEnabled, SkBlendMode::kPlus, |
| 704 | flushState->drawOpArgs().fOutputSwizzle); |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 705 | |
| 706 | // Draw linear strokes. |
| 707 | this->appendStrokeMeshesToBuffers(0, batch, startIndices, startScissorSubBatch, drawBounds); |
| 708 | if (!fMeshesBuffer.empty()) { |
| 709 | LinearStrokeProcessor linearProc; |
| 710 | this->flushBufferedMeshesAsStrokes(linearProc, flushState, pipeline, drawBounds); |
| 711 | } |
| 712 | |
| 713 | // Draw cubic strokes. (Quadratics were converted to cubics for GPU processing.) |
| 714 | for (int i = 1; i <= kMaxNumLinearSegmentsLog2; ++i) { |
| 715 | this->appendStrokeMeshesToBuffers(i, batch, startIndices, startScissorSubBatch, drawBounds); |
| 716 | } |
| 717 | if (!fMeshesBuffer.empty()) { |
| 718 | CubicStrokeProcessor cubicProc; |
| 719 | this->flushBufferedMeshesAsStrokes(cubicProc, flushState, pipeline, drawBounds); |
| 720 | } |
| 721 | |
| 722 | // Draw triangles. |
Chris Dalton | 2c5e011 | 2019-03-29 13:14:18 -0500 | [diff] [blame] | 723 | proc->reset(PrimitiveType::kTriangles, flushState->resourceProvider()); |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 724 | this->drawConnectingGeometry<&InstanceTallies::fTriangles>( |
Chris Dalton | 2c5e011 | 2019-03-29 13:14:18 -0500 | [diff] [blame] | 725 | flushState, pipeline, *proc, batch, startIndices, startScissorSubBatch, drawBounds); |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 726 | |
| 727 | // Draw conics. |
Chris Dalton | 2c5e011 | 2019-03-29 13:14:18 -0500 | [diff] [blame] | 728 | proc->reset(PrimitiveType::kConics, flushState->resourceProvider()); |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 729 | this->drawConnectingGeometry<&InstanceTallies::fConics>( |
Chris Dalton | 2c5e011 | 2019-03-29 13:14:18 -0500 | [diff] [blame] | 730 | flushState, pipeline, *proc, batch, startIndices, startScissorSubBatch, drawBounds); |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | void GrCCStroker::appendStrokeMeshesToBuffers(int numSegmentsLog2, const Batch& batch, |
| 734 | const InstanceTallies* startIndices[2], |
| 735 | int startScissorSubBatch, |
| 736 | const SkIRect& drawBounds) const { |
| 737 | // Linear strokes draw a quad. Cubic strokes emit a strip with normals at "numSegments" |
| 738 | // evenly-spaced points along the curve, plus one more for the final endpoint, plus two more for |
| 739 | // AA butt caps. (i.e., 2 vertices * (numSegments + 3).) |
| 740 | int numStripVertices = (0 == numSegmentsLog2) ? 4 : ((1 << numSegmentsLog2) + 3) * 2; |
| 741 | |
| 742 | // Append non-scissored meshes. |
| 743 | int baseInstance = fBaseInstances[(int)GrScissorTest::kDisabled].fStrokes[numSegmentsLog2]; |
| 744 | int startIdx = startIndices[(int)GrScissorTest::kDisabled]->fStrokes[numSegmentsLog2]; |
| 745 | int endIdx = batch.fNonScissorEndInstances->fStrokes[numSegmentsLog2]; |
| 746 | SkASSERT(endIdx >= startIdx); |
| 747 | if (int instanceCount = endIdx - startIdx) { |
| 748 | GrMesh& mesh = fMeshesBuffer.emplace_back(GrPrimitiveType::kTriangleStrip); |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 749 | mesh.setInstanced(fInstanceBuffer, instanceCount, baseInstance + startIdx, |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 750 | numStripVertices); |
| 751 | fScissorsBuffer.push_back(drawBounds); |
| 752 | } |
| 753 | |
| 754 | // Append scissored meshes. |
| 755 | baseInstance = fBaseInstances[(int)GrScissorTest::kEnabled].fStrokes[numSegmentsLog2]; |
| 756 | startIdx = startIndices[(int)GrScissorTest::kEnabled]->fStrokes[numSegmentsLog2]; |
| 757 | for (int i = startScissorSubBatch; i < batch.fEndScissorSubBatch; ++i) { |
| 758 | const ScissorSubBatch& subBatch = fScissorSubBatches[i]; |
| 759 | endIdx = subBatch.fEndInstances->fStrokes[numSegmentsLog2]; |
| 760 | SkASSERT(endIdx >= startIdx); |
| 761 | if (int instanceCount = endIdx - startIdx) { |
| 762 | GrMesh& mesh = fMeshesBuffer.emplace_back(GrPrimitiveType::kTriangleStrip); |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 763 | mesh.setInstanced(fInstanceBuffer, instanceCount, baseInstance + startIdx, |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 764 | numStripVertices); |
| 765 | fScissorsBuffer.push_back(subBatch.fScissor); |
| 766 | startIdx = endIdx; |
| 767 | } |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | void GrCCStroker::flushBufferedMeshesAsStrokes(const GrPrimitiveProcessor& processor, |
| 772 | GrOpFlushState* flushState, |
| 773 | const GrPipeline& pipeline, |
| 774 | const SkIRect& drawBounds) const { |
| 775 | SkASSERT(fMeshesBuffer.count() == fScissorsBuffer.count()); |
| 776 | GrPipeline::DynamicStateArrays dynamicStateArrays; |
| 777 | dynamicStateArrays.fScissorRects = fScissorsBuffer.begin(); |
Greg Daniel | 2d41d0d | 2019-08-26 11:08:51 -0400 | [diff] [blame] | 778 | flushState->opsRenderPass()->draw(processor, pipeline, nullptr, &dynamicStateArrays, |
| 779 | fMeshesBuffer.begin(), fMeshesBuffer.count(), |
| 780 | SkRect::Make(drawBounds)); |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 781 | // Don't call reset(), as that also resets the reserve count. |
| 782 | fMeshesBuffer.pop_back_n(fMeshesBuffer.count()); |
| 783 | fScissorsBuffer.pop_back_n(fScissorsBuffer.count()); |
| 784 | } |
| 785 | |
| 786 | template<int GrCCStrokeGeometry::InstanceTallies::* InstanceType> |
| 787 | void GrCCStroker::drawConnectingGeometry(GrOpFlushState* flushState, const GrPipeline& pipeline, |
| 788 | const GrCCCoverageProcessor& processor, |
| 789 | const Batch& batch, const InstanceTallies* startIndices[2], |
| 790 | int startScissorSubBatch, |
| 791 | const SkIRect& drawBounds) const { |
| 792 | // Append non-scissored meshes. |
| 793 | int baseInstance = fBaseInstances[(int)GrScissorTest::kDisabled].*InstanceType; |
| 794 | int startIdx = startIndices[(int)GrScissorTest::kDisabled]->*InstanceType; |
| 795 | int endIdx = batch.fNonScissorEndInstances->*InstanceType; |
| 796 | SkASSERT(endIdx >= startIdx); |
| 797 | if (int instanceCount = endIdx - startIdx) { |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 798 | processor.appendMesh(fInstanceBuffer, instanceCount, baseInstance + startIdx, |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 799 | &fMeshesBuffer); |
| 800 | fScissorsBuffer.push_back(drawBounds); |
| 801 | } |
| 802 | |
| 803 | // Append scissored meshes. |
| 804 | baseInstance = fBaseInstances[(int)GrScissorTest::kEnabled].*InstanceType; |
| 805 | startIdx = startIndices[(int)GrScissorTest::kEnabled]->*InstanceType; |
| 806 | for (int i = startScissorSubBatch; i < batch.fEndScissorSubBatch; ++i) { |
| 807 | const ScissorSubBatch& subBatch = fScissorSubBatches[i]; |
| 808 | endIdx = subBatch.fEndInstances->*InstanceType; |
| 809 | SkASSERT(endIdx >= startIdx); |
| 810 | if (int instanceCount = endIdx - startIdx) { |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 811 | processor.appendMesh(fInstanceBuffer, instanceCount, baseInstance + startIdx, |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 812 | &fMeshesBuffer); |
| 813 | fScissorsBuffer.push_back(subBatch.fScissor); |
| 814 | startIdx = endIdx; |
| 815 | } |
| 816 | } |
| 817 | |
| 818 | // Flush the geometry. |
| 819 | if (!fMeshesBuffer.empty()) { |
| 820 | SkASSERT(fMeshesBuffer.count() == fScissorsBuffer.count()); |
| 821 | processor.draw(flushState, pipeline, fScissorsBuffer.begin(), fMeshesBuffer.begin(), |
| 822 | fMeshesBuffer.count(), SkRect::Make(drawBounds)); |
| 823 | // Don't call reset(), as that also resets the reserve count. |
| 824 | fMeshesBuffer.pop_back_n(fMeshesBuffer.count()); |
| 825 | fScissorsBuffer.pop_back_n(fScissorsBuffer.count()); |
| 826 | } |
| 827 | } |