Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 Google LLC. |
| 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 | |
| 8 | #include "src/gpu/tessellate/GrTessellatePathOp.h" |
| 9 | |
Chris Dalton | d081dce | 2020-01-23 12:09:04 -0700 | [diff] [blame] | 10 | #include "src/gpu/GrEagerVertexAllocator.h" |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 11 | #include "src/gpu/GrGpu.h" |
| 12 | #include "src/gpu/GrOpFlushState.h" |
Chris Dalton | 17dc418 | 2020-03-25 16:18:16 -0600 | [diff] [blame] | 13 | #include "src/gpu/GrTriangulator.h" |
Chris Dalton | 4328e92 | 2020-01-29 13:16:14 -0700 | [diff] [blame] | 14 | #include "src/gpu/tessellate/GrFillPathShader.h" |
Chris Dalton | f5132a0 | 2020-04-27 23:40:03 -0600 | [diff] [blame] | 15 | #include "src/gpu/tessellate/GrMiddleOutPolygonTriangulator.h" |
Chris Dalton | 42915c2 | 2020-04-22 16:24:43 -0600 | [diff] [blame] | 16 | #include "src/gpu/tessellate/GrMidpointContourParser.h" |
Chris Dalton | f9aea7f | 2020-01-21 11:19:26 -0700 | [diff] [blame] | 17 | #include "src/gpu/tessellate/GrStencilPathShader.h" |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 18 | |
| 19 | GrTessellatePathOp::FixedFunctionFlags GrTessellatePathOp::fixedFunctionFlags() const { |
| 20 | auto flags = FixedFunctionFlags::kUsesStencil; |
| 21 | if (GrAAType::kNone != fAAType) { |
| 22 | flags |= FixedFunctionFlags::kUsesHWAA; |
| 23 | } |
| 24 | return flags; |
| 25 | } |
| 26 | |
Robert Phillips | c655c3a | 2020-03-18 13:23:45 -0400 | [diff] [blame] | 27 | void GrTessellatePathOp::onPrePrepare(GrRecordingContext*, |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame] | 28 | const GrSurfaceProxyView* writeView, |
Robert Phillips | c655c3a | 2020-03-18 13:23:45 -0400 | [diff] [blame] | 29 | GrAppliedClip*, |
| 30 | const GrXferProcessor::DstProxyView&) { |
| 31 | } |
| 32 | |
Chris Dalton | 0e6d4f1 | 2020-05-24 20:48:56 +0000 | [diff] [blame^] | 33 | void GrTessellatePathOp::onPrepare(GrOpFlushState* state) { |
| 34 | // First check if the path is large and/or simple enough that we can actually triangulate the |
| 35 | // inner polygon(s) on the CPU. This is our fastest approach. It allows us to stencil only the |
| 36 | // curves, and then fill the internal polygons directly to the final render target, thus filling |
| 37 | // in the majority of pixels in a single render pass. |
| 38 | SkScalar scales[2]; |
| 39 | SkAssertResult(fViewMatrix.getMinMaxScales(scales)); // Will fail if perspective. |
| 40 | const SkRect& bounds = fPath.getBounds(); |
Chris Dalton | 4328e92 | 2020-01-29 13:16:14 -0700 | [diff] [blame] | 41 | int numVerbs = fPath.countVerbs(); |
| 42 | if (numVerbs <= 0) { |
| 43 | return; |
| 44 | } |
| 45 | float gpuFragmentWork = bounds.height() * scales[0] * bounds.width() * scales[1]; |
| 46 | float cpuTessellationWork = (float)numVerbs * SkNextLog2(numVerbs); // N log N. |
| 47 | if (cpuTessellationWork * 500 + (256 * 256) < gpuFragmentWork) { // Don't try below 256x256. |
Chris Dalton | 0e6d4f1 | 2020-05-24 20:48:56 +0000 | [diff] [blame^] | 48 | int numCountedCurves; |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 49 | // This will fail if the inner triangles do not form a simple polygon (e.g., self |
| 50 | // intersection, double winding). |
Chris Dalton | 0e6d4f1 | 2020-05-24 20:48:56 +0000 | [diff] [blame^] | 51 | if (this->prepareNonOverlappingInnerTriangles(state, &numCountedCurves)) { |
| 52 | // Prepare cubics on an instance boundary so we can use the buffer to fill local convex |
| 53 | // hulls as well. |
| 54 | this->prepareOuterCubics(state, numCountedCurves, |
| 55 | CubicDataAlignment::kInstanceBoundary); |
Chris Dalton | 4328e92 | 2020-01-29 13:16:14 -0700 | [diff] [blame] | 56 | return; |
| 57 | } |
| 58 | } |
| 59 | |
Chris Dalton | 0e6d4f1 | 2020-05-24 20:48:56 +0000 | [diff] [blame^] | 60 | // Next see if we can split up inner polygon triangles and curves, and triangulate the inner |
| 61 | // polygon(s) more efficiently. This causes greater CPU overhead due to the extra shaders and |
| 62 | // draw calls, but the better triangulation can reduce the rasterizer load by a great deal on |
| 63 | // complex paths. |
Chris Dalton | 4328e92 | 2020-01-29 13:16:14 -0700 | [diff] [blame] | 64 | // NOTE: Raster-edge work is 1-dimensional, so we sum height and width instead of multiplying. |
| 65 | float rasterEdgeWork = (bounds.height() + bounds.width()) * scales[1] * fPath.countVerbs(); |
Chris Dalton | 0e6d4f1 | 2020-05-24 20:48:56 +0000 | [diff] [blame^] | 66 | if (rasterEdgeWork > 1000 * 1000) { |
| 67 | int numCountedCurves; |
| 68 | this->prepareMiddleOutInnerTriangles(state, &numCountedCurves); |
| 69 | // We will fill the path with a bounding box instead local cubic convex hulls, so there is |
| 70 | // no need to prepare the cubics on an instance boundary. |
| 71 | this->prepareOuterCubics(state, numCountedCurves, CubicDataAlignment::kVertexBoundary); |
Chris Dalton | f9aea7f | 2020-01-21 11:19:26 -0700 | [diff] [blame] | 72 | return; |
| 73 | } |
| 74 | |
| 75 | // Fastest CPU approach: emit one cubic wedge per verb, fanning out from the center. |
Chris Dalton | 0e6d4f1 | 2020-05-24 20:48:56 +0000 | [diff] [blame^] | 76 | this->prepareCubicWedges(state); |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Chris Dalton | 2f2d81c | 2020-05-13 17:57:37 -0600 | [diff] [blame] | 79 | bool GrTessellatePathOp::prepareNonOverlappingInnerTriangles(GrMeshDrawOp::Target* target, |
Chris Dalton | f5132a0 | 2020-04-27 23:40:03 -0600 | [diff] [blame] | 80 | int* numCountedCurves) { |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 81 | SkASSERT(!fTriangleBuffer); |
| 82 | SkASSERT(!fDoStencilTriangleBuffer); |
| 83 | SkASSERT(!fDoFillTriangleBuffer); |
| 84 | |
| 85 | using GrTriangulator::Mode; |
| 86 | |
Chris Dalton | 2f2d81c | 2020-05-13 17:57:37 -0600 | [diff] [blame] | 87 | GrEagerDynamicVertexAllocator vertexAlloc(target, &fTriangleBuffer, &fBaseTriangleVertex); |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 88 | fTriangleVertexCount = GrTriangulator::PathToTriangles(fPath, 0, SkRect::MakeEmpty(), |
| 89 | &vertexAlloc, Mode::kSimpleInnerPolygons, |
| 90 | numCountedCurves); |
| 91 | if (fTriangleVertexCount == 0) { |
| 92 | // Mode::kSimpleInnerPolygons causes PathToTriangles to fail if the inner polygon(s) are not |
| 93 | // simple. |
| 94 | return false; |
| 95 | } |
| 96 | if (((Flags::kStencilOnly | Flags::kWireframe) & fFlags) || GrAAType::kCoverage == fAAType || |
Chris Dalton | 2f2d81c | 2020-05-13 17:57:37 -0600 | [diff] [blame] | 97 | (target->appliedClip() && target->appliedClip()->hasStencilClip())) { |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 98 | // If we have certain flags, mixed samples, or a stencil clip then we unfortunately |
| 99 | // can't fill the inner polygon directly. Indicate that these triangles need to be |
| 100 | // stencilled. |
| 101 | fDoStencilTriangleBuffer = true; |
| 102 | } |
| 103 | if (!(Flags::kStencilOnly & fFlags)) { |
| 104 | fDoFillTriangleBuffer = true; |
| 105 | } |
| 106 | return true; |
| 107 | } |
| 108 | |
Chris Dalton | 0e6d4f1 | 2020-05-24 20:48:56 +0000 | [diff] [blame^] | 109 | void GrTessellatePathOp::prepareMiddleOutInnerTriangles(GrMeshDrawOp::Target* target, |
| 110 | int* numCountedCurves) { |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 111 | SkASSERT(!fTriangleBuffer); |
| 112 | SkASSERT(!fDoStencilTriangleBuffer); |
| 113 | SkASSERT(!fDoFillTriangleBuffer); |
| 114 | |
Chris Dalton | f5132a0 | 2020-04-27 23:40:03 -0600 | [diff] [blame] | 115 | // No initial moveTo, plus an implicit close at the end; n-2 triangles fill an n-gon. |
Chris Dalton | 0e6d4f1 | 2020-05-24 20:48:56 +0000 | [diff] [blame^] | 116 | // Each triangle has 3 vertices. |
| 117 | int maxVertices = (fPath.countVerbs() - 1) * 3; |
Chris Dalton | 42915c2 | 2020-04-22 16:24:43 -0600 | [diff] [blame] | 118 | |
Chris Dalton | 0e6d4f1 | 2020-05-24 20:48:56 +0000 | [diff] [blame^] | 119 | GrEagerDynamicVertexAllocator vertexAlloc(target, &fTriangleBuffer, &fBaseTriangleVertex); |
| 120 | auto* vertexData = vertexAlloc.lock<SkPoint>(maxVertices); |
Chris Dalton | 42915c2 | 2020-04-22 16:24:43 -0600 | [diff] [blame] | 121 | if (!vertexData) { |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 122 | return; |
Chris Dalton | 42915c2 | 2020-04-22 16:24:43 -0600 | [diff] [blame] | 123 | } |
Chris Dalton | 42915c2 | 2020-04-22 16:24:43 -0600 | [diff] [blame] | 124 | |
Chris Dalton | 0e6d4f1 | 2020-05-24 20:48:56 +0000 | [diff] [blame^] | 125 | constexpr static int kNumVerticesPerTriangle = 3; |
| 126 | GrMiddleOutPolygonTriangulator middleOut(vertexData, kNumVerticesPerTriangle, maxVertices); |
| 127 | int localCurveCount = 0; |
Chris Dalton | f7a3307 | 2020-05-01 10:33:08 -0600 | [diff] [blame] | 128 | for (auto [verb, pts, w] : SkPathPriv::Iterate(fPath)) { |
| 129 | switch (verb) { |
| 130 | case SkPathVerb::kMove: |
Chris Dalton | 0e6d4f1 | 2020-05-24 20:48:56 +0000 | [diff] [blame^] | 131 | middleOut.closeAndMove(*pts++); |
Chris Dalton | f5132a0 | 2020-04-27 23:40:03 -0600 | [diff] [blame] | 132 | break; |
Chris Dalton | f7a3307 | 2020-05-01 10:33:08 -0600 | [diff] [blame] | 133 | case SkPathVerb::kLine: |
| 134 | middleOut.pushVertex(pts[1]); |
| 135 | break; |
| 136 | case SkPathVerb::kQuad: |
| 137 | middleOut.pushVertex(pts[2]); |
Chris Dalton | 0e6d4f1 | 2020-05-24 20:48:56 +0000 | [diff] [blame^] | 138 | ++localCurveCount; |
Chris Dalton | f7a3307 | 2020-05-01 10:33:08 -0600 | [diff] [blame] | 139 | break; |
| 140 | case SkPathVerb::kCubic: |
| 141 | middleOut.pushVertex(pts[3]); |
Chris Dalton | 0e6d4f1 | 2020-05-24 20:48:56 +0000 | [diff] [blame^] | 142 | ++localCurveCount; |
Chris Dalton | f7a3307 | 2020-05-01 10:33:08 -0600 | [diff] [blame] | 143 | break; |
| 144 | case SkPathVerb::kClose: |
Chris Dalton | f5132a0 | 2020-04-27 23:40:03 -0600 | [diff] [blame] | 145 | middleOut.close(); |
| 146 | break; |
Chris Dalton | f7a3307 | 2020-05-01 10:33:08 -0600 | [diff] [blame] | 147 | case SkPathVerb::kConic: |
Chris Dalton | f7a3307 | 2020-05-01 10:33:08 -0600 | [diff] [blame] | 148 | SkUNREACHABLE; |
Chris Dalton | f5132a0 | 2020-04-27 23:40:03 -0600 | [diff] [blame] | 149 | } |
Chris Dalton | 42915c2 | 2020-04-22 16:24:43 -0600 | [diff] [blame] | 150 | } |
Chris Dalton | 0e6d4f1 | 2020-05-24 20:48:56 +0000 | [diff] [blame^] | 151 | fTriangleVertexCount = middleOut.close() * kNumVerticesPerTriangle; |
| 152 | *numCountedCurves = localCurveCount; |
Chris Dalton | 42915c2 | 2020-04-22 16:24:43 -0600 | [diff] [blame] | 153 | |
Chris Dalton | 0e6d4f1 | 2020-05-24 20:48:56 +0000 | [diff] [blame^] | 154 | vertexAlloc.unlock(fTriangleVertexCount); |
| 155 | |
| 156 | if (fTriangleVertexCount) { |
| 157 | fDoStencilTriangleBuffer = true; |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 158 | } |
Chris Dalton | 42915c2 | 2020-04-22 16:24:43 -0600 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | static SkPoint lerp(const SkPoint& a, const SkPoint& b, float T) { |
| 162 | SkASSERT(1 != T); // The below does not guarantee lerp(a, b, 1) === b. |
| 163 | return (b - a) * T + a; |
| 164 | } |
| 165 | |
Chris Dalton | f7a3307 | 2020-05-01 10:33:08 -0600 | [diff] [blame] | 166 | static void line2cubic(const SkPoint& p0, const SkPoint& p1, SkPoint* out) { |
| 167 | out[0] = p0; |
| 168 | out[1] = lerp(p0, p1, 1/3.f); |
| 169 | out[2] = lerp(p0, p1, 2/3.f); |
| 170 | out[3] = p1; |
Chris Dalton | 42915c2 | 2020-04-22 16:24:43 -0600 | [diff] [blame] | 171 | } |
| 172 | |
Chris Dalton | f7a3307 | 2020-05-01 10:33:08 -0600 | [diff] [blame] | 173 | static void quad2cubic(const SkPoint pts[], SkPoint* out) { |
| 174 | out[0] = pts[0]; |
| 175 | out[1] = lerp(pts[0], pts[1], 2/3.f); |
| 176 | out[2] = lerp(pts[1], pts[2], 1/3.f); |
| 177 | out[3] = pts[2]; |
Chris Dalton | 42915c2 | 2020-04-22 16:24:43 -0600 | [diff] [blame] | 178 | } |
| 179 | |
Chris Dalton | 0e6d4f1 | 2020-05-24 20:48:56 +0000 | [diff] [blame^] | 180 | void GrTessellatePathOp::prepareOuterCubics(GrMeshDrawOp::Target* target, int numCountedCurves, |
| 181 | CubicDataAlignment alignment) { |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 182 | SkASSERT(!fCubicBuffer); |
| 183 | SkASSERT(!fStencilCubicsShader); |
Chris Dalton | 42915c2 | 2020-04-22 16:24:43 -0600 | [diff] [blame] | 184 | |
| 185 | if (numCountedCurves == 0) { |
| 186 | return; |
| 187 | } |
| 188 | |
Chris Dalton | 0e6d4f1 | 2020-05-24 20:48:56 +0000 | [diff] [blame^] | 189 | bool instanceAligned = (alignment == CubicDataAlignment::kInstanceBoundary); |
| 190 | int instanceOrVertexStride = (instanceAligned) ? sizeof(SkPoint) * 4 : sizeof(SkPoint); |
| 191 | int instanceOrVertexCount = (instanceAligned) ? numCountedCurves : numCountedCurves * 4; |
| 192 | int baseInstanceOrVertex; |
| 193 | |
Chris Dalton | 2f2d81c | 2020-05-13 17:57:37 -0600 | [diff] [blame] | 194 | auto* vertexData = static_cast<SkPoint*>(target->makeVertexSpace( |
Chris Dalton | 0e6d4f1 | 2020-05-24 20:48:56 +0000 | [diff] [blame^] | 195 | instanceOrVertexStride, instanceOrVertexCount, &fCubicBuffer, &baseInstanceOrVertex)); |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 196 | if (!vertexData) { |
Chris Dalton | 42915c2 | 2020-04-22 16:24:43 -0600 | [diff] [blame] | 197 | return; |
| 198 | } |
Chris Dalton | 0e6d4f1 | 2020-05-24 20:48:56 +0000 | [diff] [blame^] | 199 | fBaseCubicVertex = (instanceAligned) ? baseInstanceOrVertex * 4 : baseInstanceOrVertex; |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 200 | fCubicVertexCount = 0; |
Chris Dalton | 42915c2 | 2020-04-22 16:24:43 -0600 | [diff] [blame] | 201 | |
Chris Dalton | f7a3307 | 2020-05-01 10:33:08 -0600 | [diff] [blame] | 202 | for (auto [verb, pts, w] : SkPathPriv::Iterate(fPath)) { |
| 203 | switch (verb) { |
| 204 | case SkPathVerb::kQuad: |
| 205 | SkASSERT(fCubicVertexCount < numCountedCurves * 4); |
| 206 | quad2cubic(pts, vertexData + fCubicVertexCount); |
Chris Dalton | 0e6d4f1 | 2020-05-24 20:48:56 +0000 | [diff] [blame^] | 207 | fCubicVertexCount += 4; |
Chris Dalton | f7a3307 | 2020-05-01 10:33:08 -0600 | [diff] [blame] | 208 | break; |
| 209 | case SkPathVerb::kCubic: |
| 210 | SkASSERT(fCubicVertexCount < numCountedCurves * 4); |
| 211 | memcpy(vertexData + fCubicVertexCount, pts, sizeof(SkPoint) * 4); |
Chris Dalton | 0e6d4f1 | 2020-05-24 20:48:56 +0000 | [diff] [blame^] | 212 | fCubicVertexCount += 4; |
| 213 | break; |
| 214 | default: |
Chris Dalton | f7a3307 | 2020-05-01 10:33:08 -0600 | [diff] [blame] | 215 | break; |
Chris Dalton | 42915c2 | 2020-04-22 16:24:43 -0600 | [diff] [blame] | 216 | } |
| 217 | } |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 218 | SkASSERT(fCubicVertexCount == numCountedCurves * 4); |
| 219 | |
Chris Dalton | 0e6d4f1 | 2020-05-24 20:48:56 +0000 | [diff] [blame^] | 220 | fStencilCubicsShader = target->allocator()->make<GrStencilCubicShader>(fViewMatrix); |
Chris Dalton | 42915c2 | 2020-04-22 16:24:43 -0600 | [diff] [blame] | 221 | } |
| 222 | |
Chris Dalton | 0e6d4f1 | 2020-05-24 20:48:56 +0000 | [diff] [blame^] | 223 | void GrTessellatePathOp::prepareCubicWedges(GrMeshDrawOp::Target* target) { |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 224 | SkASSERT(!fCubicBuffer); |
| 225 | SkASSERT(!fStencilCubicsShader); |
| 226 | |
Chris Dalton | 42915c2 | 2020-04-22 16:24:43 -0600 | [diff] [blame] | 227 | // No initial moveTo, one wedge per verb, plus an implicit close at the end. |
| 228 | // Each wedge has 5 vertices. |
| 229 | int maxVertices = (fPath.countVerbs() + 1) * 5; |
| 230 | |
Chris Dalton | 2f2d81c | 2020-05-13 17:57:37 -0600 | [diff] [blame] | 231 | GrEagerDynamicVertexAllocator vertexAlloc(target, &fCubicBuffer, &fBaseCubicVertex); |
Chris Dalton | 42915c2 | 2020-04-22 16:24:43 -0600 | [diff] [blame] | 232 | auto* vertexData = vertexAlloc.lock<SkPoint>(maxVertices); |
| 233 | if (!vertexData) { |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 234 | return; |
Chris Dalton | 42915c2 | 2020-04-22 16:24:43 -0600 | [diff] [blame] | 235 | } |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 236 | fCubicVertexCount = 0; |
Chris Dalton | 42915c2 | 2020-04-22 16:24:43 -0600 | [diff] [blame] | 237 | |
| 238 | GrMidpointContourParser parser(fPath); |
| 239 | while (parser.parseNextContour()) { |
Chris Dalton | f7a3307 | 2020-05-01 10:33:08 -0600 | [diff] [blame] | 240 | SkPoint midpoint = parser.currentMidpoint(); |
| 241 | SkPoint startPoint = {0, 0}; |
| 242 | SkPoint lastPoint = startPoint; |
| 243 | for (auto [verb, pts, w] : parser.currentContour()) { |
| 244 | switch (verb) { |
| 245 | case SkPathVerb::kMove: |
| 246 | startPoint = lastPoint = pts[0]; |
Chris Dalton | 42915c2 | 2020-04-22 16:24:43 -0600 | [diff] [blame] | 247 | continue; |
Chris Dalton | f7a3307 | 2020-05-01 10:33:08 -0600 | [diff] [blame] | 248 | case SkPathVerb::kClose: |
| 249 | continue; // Ignore. We can assume an implicit close at the end. |
Chris Dalton | 42915c2 | 2020-04-22 16:24:43 -0600 | [diff] [blame] | 250 | case SkPathVerb::kLine: |
Chris Dalton | f7a3307 | 2020-05-01 10:33:08 -0600 | [diff] [blame] | 251 | line2cubic(pts[0], pts[1], vertexData + fCubicVertexCount); |
| 252 | lastPoint = pts[1]; |
Chris Dalton | 42915c2 | 2020-04-22 16:24:43 -0600 | [diff] [blame] | 253 | break; |
| 254 | case SkPathVerb::kQuad: |
Chris Dalton | f7a3307 | 2020-05-01 10:33:08 -0600 | [diff] [blame] | 255 | quad2cubic(pts, vertexData + fCubicVertexCount); |
| 256 | lastPoint = pts[2]; |
Chris Dalton | 42915c2 | 2020-04-22 16:24:43 -0600 | [diff] [blame] | 257 | break; |
| 258 | case SkPathVerb::kCubic: |
Chris Dalton | f7a3307 | 2020-05-01 10:33:08 -0600 | [diff] [blame] | 259 | memcpy(vertexData + fCubicVertexCount, pts, sizeof(SkPoint) * 4); |
| 260 | lastPoint = pts[3]; |
Chris Dalton | 42915c2 | 2020-04-22 16:24:43 -0600 | [diff] [blame] | 261 | break; |
| 262 | case SkPathVerb::kConic: |
| 263 | SkUNREACHABLE; |
| 264 | } |
Chris Dalton | f7a3307 | 2020-05-01 10:33:08 -0600 | [diff] [blame] | 265 | vertexData[fCubicVertexCount + 4] = midpoint; |
| 266 | fCubicVertexCount += 5; |
| 267 | } |
| 268 | if (lastPoint != startPoint) { |
| 269 | line2cubic(lastPoint, startPoint, vertexData + fCubicVertexCount); |
| 270 | vertexData[fCubicVertexCount + 4] = midpoint; |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 271 | fCubicVertexCount += 5; |
Chris Dalton | 42915c2 | 2020-04-22 16:24:43 -0600 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 275 | vertexAlloc.unlock(fCubicVertexCount); |
| 276 | |
| 277 | if (fCubicVertexCount) { |
Chris Dalton | 0e6d4f1 | 2020-05-24 20:48:56 +0000 | [diff] [blame^] | 278 | fStencilCubicsShader = target->allocator()->make<GrStencilWedgeShader>(fViewMatrix); |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 279 | } |
Chris Dalton | 42915c2 | 2020-04-22 16:24:43 -0600 | [diff] [blame] | 280 | } |
| 281 | |
Chris Dalton | 0e6d4f1 | 2020-05-24 20:48:56 +0000 | [diff] [blame^] | 282 | void GrTessellatePathOp::onExecute(GrOpFlushState* state, const SkRect& chainBounds) { |
| 283 | this->drawStencilPass(state); |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 284 | if (!(Flags::kStencilOnly & fFlags)) { |
Chris Dalton | 0e6d4f1 | 2020-05-24 20:48:56 +0000 | [diff] [blame^] | 285 | this->drawCoverPass(state); |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | |
Chris Dalton | 0e6d4f1 | 2020-05-24 20:48:56 +0000 | [diff] [blame^] | 289 | void GrTessellatePathOp::drawStencilPass(GrOpFlushState* state) { |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 290 | // Increments clockwise triangles and decrements counterclockwise. Used for "winding" fill. |
| 291 | constexpr static GrUserStencilSettings kIncrDecrStencil( |
| 292 | GrUserStencilSettings::StaticInitSeparate< |
| 293 | 0x0000, 0x0000, |
| 294 | GrUserStencilTest::kAlwaysIfInClip, GrUserStencilTest::kAlwaysIfInClip, |
| 295 | 0xffff, 0xffff, |
| 296 | GrUserStencilOp::kIncWrap, GrUserStencilOp::kDecWrap, |
| 297 | GrUserStencilOp::kKeep, GrUserStencilOp::kKeep, |
| 298 | 0xffff, 0xffff>()); |
| 299 | |
| 300 | // Inverts the bottom stencil bit. Used for "even/odd" fill. |
| 301 | constexpr static GrUserStencilSettings kInvertStencil( |
| 302 | GrUserStencilSettings::StaticInit< |
| 303 | 0x0000, |
| 304 | GrUserStencilTest::kAlwaysIfInClip, |
| 305 | 0xffff, |
| 306 | GrUserStencilOp::kInvert, |
| 307 | GrUserStencilOp::kKeep, |
| 308 | 0x0001>()); |
| 309 | |
| 310 | GrPipeline::InitArgs initArgs; |
| 311 | if (GrAAType::kNone != fAAType) { |
| 312 | initArgs.fInputFlags |= GrPipeline::InputFlags::kHWAntialias; |
| 313 | } |
Chris Dalton | 0e6d4f1 | 2020-05-24 20:48:56 +0000 | [diff] [blame^] | 314 | if (state->caps().wireframeSupport() && (Flags::kWireframe & fFlags)) { |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 315 | initArgs.fInputFlags |= GrPipeline::InputFlags::kWireframe; |
| 316 | } |
| 317 | SkASSERT(SkPathFillType::kWinding == fPath.getFillType() || |
| 318 | SkPathFillType::kEvenOdd == fPath.getFillType()); |
| 319 | initArgs.fUserStencil = (SkPathFillType::kWinding == fPath.getFillType()) ? |
| 320 | &kIncrDecrStencil : &kInvertStencil; |
Chris Dalton | 0e6d4f1 | 2020-05-24 20:48:56 +0000 | [diff] [blame^] | 321 | initArgs.fCaps = &state->caps(); |
Chris Dalton | aa0e45c | 2020-03-16 10:05:11 -0600 | [diff] [blame] | 322 | GrPipeline pipeline(initArgs, GrDisableColorXPFactory::MakeXferProcessor(), |
Chris Dalton | 0e6d4f1 | 2020-05-24 20:48:56 +0000 | [diff] [blame^] | 323 | state->appliedHardClip()); |
Chris Dalton | 012f849 | 2020-03-05 11:49:15 -0700 | [diff] [blame] | 324 | |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 325 | if (fDoStencilTriangleBuffer) { |
| 326 | SkASSERT(fTriangleBuffer); |
| 327 | GrStencilTriangleShader stencilTriangleShader(fViewMatrix); |
Chris Dalton | 0e6d4f1 | 2020-05-24 20:48:56 +0000 | [diff] [blame^] | 328 | GrPathShader::ProgramInfo programInfo(state->writeView(), &pipeline, |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 329 | &stencilTriangleShader); |
Chris Dalton | 0e6d4f1 | 2020-05-24 20:48:56 +0000 | [diff] [blame^] | 330 | state->bindPipelineAndScissorClip(programInfo, this->bounds()); |
| 331 | state->bindBuffers(nullptr, nullptr, fTriangleBuffer.get()); |
| 332 | state->draw(fTriangleVertexCount, fBaseTriangleVertex); |
Chris Dalton | f9aea7f | 2020-01-21 11:19:26 -0700 | [diff] [blame] | 333 | } |
| 334 | |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 335 | if (fStencilCubicsShader) { |
Chris Dalton | 0e6d4f1 | 2020-05-24 20:48:56 +0000 | [diff] [blame^] | 336 | GrPathShader::ProgramInfo programInfo(state->writeView(), &pipeline, fStencilCubicsShader); |
| 337 | state->bindPipelineAndScissorClip(programInfo, this->bounds()); |
| 338 | state->bindBuffers(nullptr, nullptr, fCubicBuffer.get()); |
| 339 | state->draw(fCubicVertexCount, fBaseCubicVertex); |
| 340 | } |
| 341 | |
| 342 | // http://skbug.com/9739 |
| 343 | if (state->caps().requiresManualFBBarrierAfterTessellatedStencilDraw()) { |
| 344 | state->gpu()->insertManualFramebufferBarrier(); |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 345 | } |
| 346 | } |
| 347 | |
Chris Dalton | 0e6d4f1 | 2020-05-24 20:48:56 +0000 | [diff] [blame^] | 348 | void GrTessellatePathOp::drawCoverPass(GrOpFlushState* state) { |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 349 | // Allows non-zero stencil values to pass and write a color, and resets the stencil value back |
| 350 | // to zero; discards immediately on stencil values of zero. |
| 351 | // NOTE: It's ok to not check the clip here because the previous stencil pass only wrote to |
| 352 | // samples already inside the clip. |
| 353 | constexpr static GrUserStencilSettings kTestAndResetStencil( |
| 354 | GrUserStencilSettings::StaticInit< |
| 355 | 0x0000, |
| 356 | GrUserStencilTest::kNotEqual, |
| 357 | 0xffff, |
| 358 | GrUserStencilOp::kZero, |
| 359 | GrUserStencilOp::kKeep, |
| 360 | 0xffff>()); |
| 361 | |
| 362 | GrPipeline::InitArgs initArgs; |
| 363 | if (GrAAType::kNone != fAAType) { |
| 364 | initArgs.fInputFlags |= GrPipeline::InputFlags::kHWAntialias; |
Chris Dalton | 0e6d4f1 | 2020-05-24 20:48:56 +0000 | [diff] [blame^] | 365 | if (1 == state->proxy()->numSamples()) { |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 366 | SkASSERT(GrAAType::kCoverage == fAAType); |
| 367 | // We are mixed sampled. Use conservative raster to make the sample coverage mask 100% |
| 368 | // at every fragment. This way we will still get a double hit on shared edges, but |
| 369 | // whichever side comes first will cover every sample and will clear the stencil. The |
| 370 | // other side will then be discarded and not cause a double blend. |
| 371 | initArgs.fInputFlags |= GrPipeline::InputFlags::kConservativeRaster; |
| 372 | } |
| 373 | } |
Chris Dalton | 0e6d4f1 | 2020-05-24 20:48:56 +0000 | [diff] [blame^] | 374 | initArgs.fCaps = &state->caps(); |
| 375 | initArgs.fDstProxyView = state->drawOpArgs().dstProxyView(); |
| 376 | initArgs.fWriteSwizzle = state->drawOpArgs().writeSwizzle(); |
| 377 | GrPipeline pipeline(initArgs, std::move(fProcessors), state->detachAppliedClip()); |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 378 | |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 379 | if (fDoFillTriangleBuffer) { |
| 380 | SkASSERT(fTriangleBuffer); |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 381 | |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 382 | // These are a twist on the standard red book stencil settings that allow us to fill the |
Chris Dalton | 4328e92 | 2020-01-29 13:16:14 -0700 | [diff] [blame] | 383 | // inner polygon directly to the final render target. At this point, the curves are already |
| 384 | // stencilled in. So if the stencil value is zero, then it means the path at our sample is |
| 385 | // not affected by any curves and we fill the path in directly. If the stencil value is |
| 386 | // nonzero, then we don't fill and instead continue the standard red book stencil process. |
| 387 | // |
| 388 | // NOTE: These settings are currently incompatible with a stencil clip. |
| 389 | constexpr static GrUserStencilSettings kFillOrIncrDecrStencil( |
| 390 | GrUserStencilSettings::StaticInitSeparate< |
| 391 | 0x0000, 0x0000, |
| 392 | GrUserStencilTest::kEqual, GrUserStencilTest::kEqual, |
| 393 | 0xffff, 0xffff, |
| 394 | GrUserStencilOp::kKeep, GrUserStencilOp::kKeep, |
| 395 | GrUserStencilOp::kIncWrap, GrUserStencilOp::kDecWrap, |
| 396 | 0xffff, 0xffff>()); |
| 397 | |
| 398 | constexpr static GrUserStencilSettings kFillOrInvertStencil( |
| 399 | GrUserStencilSettings::StaticInit< |
| 400 | 0x0000, |
| 401 | GrUserStencilTest::kEqual, |
| 402 | 0xffff, |
| 403 | GrUserStencilOp::kKeep, |
| 404 | GrUserStencilOp::kZero, |
| 405 | 0xffff>()); |
| 406 | |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 407 | if (fDoStencilTriangleBuffer) { |
Chris Dalton | 4328e92 | 2020-01-29 13:16:14 -0700 | [diff] [blame] | 408 | // The path was already stencilled. Here we just need to do a cover pass. |
| 409 | pipeline.setUserStencil(&kTestAndResetStencil); |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 410 | } else if (!fStencilCubicsShader) { |
| 411 | // There are no stencilled curves. We can ignore stencil and fill the path directly. |
Chris Dalton | 4328e92 | 2020-01-29 13:16:14 -0700 | [diff] [blame] | 412 | pipeline.setUserStencil(&GrUserStencilSettings::kUnused); |
| 413 | } else if (SkPathFillType::kWinding == fPath.getFillType()) { |
| 414 | // Fill in the path pixels not touched by curves, incr/decr stencil otherwise. |
| 415 | SkASSERT(!pipeline.hasStencilClip()); |
| 416 | pipeline.setUserStencil(&kFillOrIncrDecrStencil); |
| 417 | } else { |
| 418 | // Fill in the path pixels not touched by curves, invert stencil otherwise. |
| 419 | SkASSERT(!pipeline.hasStencilClip()); |
| 420 | pipeline.setUserStencil(&kFillOrInvertStencil); |
| 421 | } |
Chris Dalton | 4328e92 | 2020-01-29 13:16:14 -0700 | [diff] [blame] | 422 | |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 423 | GrFillTriangleShader fillTriangleShader(fViewMatrix, fColor); |
Chris Dalton | 0e6d4f1 | 2020-05-24 20:48:56 +0000 | [diff] [blame^] | 424 | GrPathShader::ProgramInfo programInfo(state->writeView(), &pipeline, &fillTriangleShader); |
| 425 | state->bindPipelineAndScissorClip(programInfo, this->bounds()); |
| 426 | state->bindTextures(fillTriangleShader, nullptr, pipeline); |
| 427 | state->bindBuffers(nullptr, nullptr, fTriangleBuffer.get()); |
| 428 | state->draw(fTriangleVertexCount, fBaseTriangleVertex); |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 429 | |
| 430 | if (fStencilCubicsShader) { |
Chris Dalton | 4328e92 | 2020-01-29 13:16:14 -0700 | [diff] [blame] | 431 | // At this point, every pixel is filled in except the ones touched by curves. Issue a |
| 432 | // final cover pass over the curves by drawing their convex hulls. This will fill in any |
| 433 | // remaining samples and reset the stencil buffer. |
Chris Dalton | 4328e92 | 2020-01-29 13:16:14 -0700 | [diff] [blame] | 434 | pipeline.setUserStencil(&kTestAndResetStencil); |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 435 | GrFillCubicHullShader fillCubicHullShader(fViewMatrix, fColor); |
Chris Dalton | 0e6d4f1 | 2020-05-24 20:48:56 +0000 | [diff] [blame^] | 436 | GrPathShader::ProgramInfo programInfo(state->writeView(), &pipeline, |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 437 | &fillCubicHullShader); |
Chris Dalton | 0e6d4f1 | 2020-05-24 20:48:56 +0000 | [diff] [blame^] | 438 | state->bindPipelineAndScissorClip(programInfo, this->bounds()); |
| 439 | state->bindTextures(fillCubicHullShader, nullptr, pipeline); |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 440 | |
| 441 | // Here we treat fCubicBuffer as an instance buffer. It should have been prepared with |
| 442 | // the base vertex on an instance boundary in order to accommodate this. |
| 443 | SkASSERT((fCubicVertexCount % 4) == 0); |
| 444 | SkASSERT((fBaseCubicVertex % 4) == 0); |
Chris Dalton | 0e6d4f1 | 2020-05-24 20:48:56 +0000 | [diff] [blame^] | 445 | state->bindBuffers(nullptr, fCubicBuffer.get(), nullptr); |
| 446 | state->drawInstanced(fCubicVertexCount >> 2, fBaseCubicVertex >> 2, 4, 0); |
Chris Dalton | 4328e92 | 2020-01-29 13:16:14 -0700 | [diff] [blame] | 447 | } |
Chris Dalton | 42915c2 | 2020-04-22 16:24:43 -0600 | [diff] [blame] | 448 | return; |
Chris Dalton | 4328e92 | 2020-01-29 13:16:14 -0700 | [diff] [blame] | 449 | } |
Chris Dalton | 42915c2 | 2020-04-22 16:24:43 -0600 | [diff] [blame] | 450 | |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 451 | // There are no triangles to fill. Just draw a bounding box. |
Chris Dalton | 42915c2 | 2020-04-22 16:24:43 -0600 | [diff] [blame] | 452 | pipeline.setUserStencil(&kTestAndResetStencil); |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 453 | GrFillBoundingBoxShader fillBoundingBoxShader(fViewMatrix, fColor, fPath.getBounds()); |
Chris Dalton | 0e6d4f1 | 2020-05-24 20:48:56 +0000 | [diff] [blame^] | 454 | GrPathShader::ProgramInfo programInfo(state->writeView(), &pipeline, &fillBoundingBoxShader); |
| 455 | state->bindPipelineAndScissorClip(programInfo, this->bounds()); |
| 456 | state->bindTextures(fillBoundingBoxShader, nullptr, pipeline); |
| 457 | state->bindBuffers(nullptr, nullptr, nullptr); |
| 458 | state->draw(4, 0); |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 459 | } |