Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021 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/GrPathTessellator.h" |
| 9 | |
| 10 | #include "src/gpu/GrEagerVertexAllocator.h" |
| 11 | #include "src/gpu/GrGpu.h" |
| 12 | #include "src/gpu/geometry/GrPathUtils.h" |
Michael Ludwig | 4e9d5e2 | 2021-05-11 10:00:12 -0400 | [diff] [blame] | 13 | #include "src/gpu/geometry/GrWangsFormula.h" |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 14 | #include "src/gpu/tessellate/GrMiddleOutPolygonTriangulator.h" |
| 15 | #include "src/gpu/tessellate/GrMidpointContourParser.h" |
| 16 | #include "src/gpu/tessellate/GrStencilPathShader.h" |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 17 | |
| 18 | GrPathIndirectTessellator::GrPathIndirectTessellator(const SkMatrix& viewMatrix, const SkPath& path, |
| 19 | DrawInnerFan drawInnerFan) |
| 20 | : fDrawInnerFan(drawInnerFan != DrawInnerFan::kNo) { |
| 21 | // Count the number of instances at each resolveLevel. |
| 22 | GrVectorXform xform(viewMatrix); |
| 23 | for (auto [verb, pts, w] : SkPathPriv::Iterate(path)) { |
| 24 | int level; |
| 25 | switch (verb) { |
| 26 | case SkPathVerb::kConic: |
Chris Dalton | 50f5e68 | 2021-04-16 22:47:03 -0600 | [diff] [blame] | 27 | level = GrWangsFormula::conic_log2(1.f / kLinearizationPrecision, pts, *w, xform); |
Tyler Denniston | bc2fa2b | 2021-02-08 11:39:34 -0500 | [diff] [blame] | 28 | break; |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 29 | case SkPathVerb::kQuad: |
Chris Dalton | 50f5e68 | 2021-04-16 22:47:03 -0600 | [diff] [blame] | 30 | level = GrWangsFormula::quadratic_log2(kLinearizationPrecision, pts, xform); |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 31 | break; |
| 32 | case SkPathVerb::kCubic: |
Chris Dalton | 50f5e68 | 2021-04-16 22:47:03 -0600 | [diff] [blame] | 33 | level = GrWangsFormula::cubic_log2(kLinearizationPrecision, pts, xform); |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 34 | break; |
| 35 | default: |
| 36 | continue; |
| 37 | } |
| 38 | SkASSERT(level >= 0); |
| 39 | // Instances with 2^0=1 segments are empty (zero area). We ignore them completely. |
| 40 | if (level > 0) { |
| 41 | level = std::min(level, kMaxResolveLevel); |
| 42 | ++fResolveLevelCounts[level]; |
| 43 | ++fOuterCurveInstanceCount; |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
Chris Dalton | 2758a31 | 2021-05-20 10:46:25 -0600 | [diff] [blame] | 48 | // Returns an upper bound on the number of segments (lineTo, quadTo, conicTo, cubicTo) in a path, |
| 49 | // also accounting for any implicit lineTos from closing contours. |
| 50 | static int max_segments_in_path(const SkPath& path) { |
| 51 | // There might be an implicit kClose at the end, but the path always begins with kMove. So the |
| 52 | // max number of segments in the path is equal to the number of verbs. |
| 53 | SkASSERT(path.countVerbs() == 0 || SkPathPriv::VerbData(path)[0] == SkPath::kMove_Verb); |
| 54 | return path.countVerbs(); |
| 55 | } |
| 56 | |
| 57 | // Returns an upper bound on the number of triangles it would require to fan a path's inner polygon, |
| 58 | // in the case where no additional vertices are introduced. |
| 59 | static int max_triangles_in_inner_fan(const SkPath& path) { |
| 60 | int maxEdgesInFan = max_segments_in_path(path); |
Chris Dalton | 8017581 | 2021-05-20 18:55:50 -0600 | [diff] [blame] | 61 | return std::max(maxEdgesInFan - 2, 0); // An n-sided polygon is fanned by n-2 triangles. |
Chris Dalton | 2758a31 | 2021-05-20 10:46:25 -0600 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | static int write_breadcrumb_triangles( |
| 65 | GrVertexWriter* writer, |
| 66 | const GrInnerFanTriangulator::BreadcrumbTriangleList* breadcrumbTriangleList) { |
| 67 | int numWritten = 0; |
| 68 | SkDEBUGCODE(int count = 0;) |
| 69 | for (const auto* tri = breadcrumbTriangleList->head(); tri; tri = tri->fNext) { |
| 70 | SkDEBUGCODE(++count;) |
| 71 | const SkPoint* p = tri->fPts; |
| 72 | if ((p[0].fX == p[1].fX && p[1].fX == p[2].fX) || |
| 73 | (p[0].fY == p[1].fY && p[1].fY == p[2].fY)) { |
| 74 | // Completely degenerate triangles have undefined winding. And T-junctions shouldn't |
| 75 | // happen on axis-aligned edges. |
| 76 | continue; |
| 77 | } |
| 78 | writer->writeArray(p, 3); |
| 79 | // Mark this instance as a triangle by setting it to a conic with w=Inf. |
| 80 | writer->fill(GrVertexWriter::kIEEE_32_infinity, 2); |
| 81 | ++numWritten; |
| 82 | } |
| 83 | SkASSERT(count == breadcrumbTriangleList->count()); |
| 84 | return numWritten; |
| 85 | } |
| 86 | |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 87 | void GrPathIndirectTessellator::prepare(GrMeshDrawOp::Target* target, const SkMatrix& viewMatrix, |
Chris Dalton | ebb37e7 | 2021-01-27 17:59:45 -0700 | [diff] [blame] | 88 | const SkPath& path, |
| 89 | const BreadcrumbTriangleList* breadcrumbTriangleList) { |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 90 | SkASSERT(fTotalInstanceCount == 0); |
| 91 | SkASSERT(fIndirectDrawCount == 0); |
Chris Dalton | b849f7a | 2021-02-10 12:55:48 -0700 | [diff] [blame] | 92 | SkASSERT(target->caps().drawInstancedSupport()); |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 93 | |
| 94 | int instanceLockCount = fOuterCurveInstanceCount; |
| 95 | if (fDrawInnerFan) { |
Chris Dalton | 2758a31 | 2021-05-20 10:46:25 -0600 | [diff] [blame] | 96 | instanceLockCount += max_triangles_in_inner_fan(path); |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 97 | } |
Chris Dalton | ebb37e7 | 2021-01-27 17:59:45 -0700 | [diff] [blame] | 98 | if (breadcrumbTriangleList) { |
| 99 | instanceLockCount += breadcrumbTriangleList->count(); |
| 100 | } |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 101 | if (instanceLockCount == 0) { |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | // Allocate a buffer to store the instance data. |
| 106 | GrEagerDynamicVertexAllocator vertexAlloc(target, &fInstanceBuffer, &fBaseInstance); |
Chris Dalton | 8731a71 | 2021-05-14 14:48:54 -0600 | [diff] [blame] | 107 | GrVertexWriter instanceWriter = static_cast<SkPoint*>(vertexAlloc.lock(sizeof(SkPoint) * 4, |
| 108 | instanceLockCount)); |
| 109 | if (!instanceWriter) { |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 110 | return; |
| 111 | } |
| 112 | |
Chris Dalton | df2dbad | 2021-05-14 16:21:15 -0600 | [diff] [blame] | 113 | // Write out any triangles at the beginning of the cubic data. Since this shader draws curves, |
| 114 | // output the triangles as conics with w=infinity (which is equivalent to a triangle). |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 115 | int numTrianglesAtBeginningOfData = 0; |
| 116 | if (fDrawInnerFan) { |
| 117 | numTrianglesAtBeginningOfData = GrMiddleOutPolygonTriangulator::WritePathInnerFan( |
Chris Dalton | df2dbad | 2021-05-14 16:21:15 -0600 | [diff] [blame] | 118 | &instanceWriter, |
| 119 | GrMiddleOutPolygonTriangulator::OutputType::kConicsWithInfiniteWeight, path); |
Chris Dalton | ebb37e7 | 2021-01-27 17:59:45 -0700 | [diff] [blame] | 120 | } |
| 121 | if (breadcrumbTriangleList) { |
Chris Dalton | 2758a31 | 2021-05-20 10:46:25 -0600 | [diff] [blame] | 122 | numTrianglesAtBeginningOfData += write_breadcrumb_triangles(&instanceWriter, |
| 123 | breadcrumbTriangleList); |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 126 | // Allocate space for the GrDrawIndexedIndirectCommand structs. Allocate enough for each |
| 127 | // possible resolve level (kMaxResolveLevel; resolveLevel=0 never has any instances), plus one |
| 128 | // more for the optional inner fan triangles. |
| 129 | int indirectLockCnt = kMaxResolveLevel + 1; |
Chris Dalton | 489fa0b | 2021-05-14 17:41:17 -0600 | [diff] [blame] | 130 | GrDrawIndirectWriter indirectWriter = target->makeDrawIndirectSpace(indirectLockCnt, |
| 131 | &fIndirectDrawBuffer, |
| 132 | &fIndirectDrawOffset); |
Chris Dalton | 8ed7a8d | 2021-03-31 10:40:29 -0600 | [diff] [blame] | 133 | if (!indirectWriter) { |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 134 | SkASSERT(!fIndirectDrawBuffer); |
Chris Dalton | 8ed7a8d | 2021-03-31 10:40:29 -0600 | [diff] [blame] | 135 | vertexAlloc.unlock(0); |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 136 | return; |
| 137 | } |
| 138 | |
| 139 | // Fill out the GrDrawIndexedIndirectCommand structs and determine the starting instance data |
| 140 | // location at each resolve level. |
Chris Dalton | 8731a71 | 2021-05-14 14:48:54 -0600 | [diff] [blame] | 141 | GrVertexWriter instanceLocations[kMaxResolveLevel + 1]; |
| 142 | int currentBaseInstance = fBaseInstance; |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 143 | SkASSERT(fResolveLevelCounts[0] == 0); |
Chris Dalton | 489fa0b | 2021-05-14 17:41:17 -0600 | [diff] [blame] | 144 | for (int resolveLevel=1, numExtraInstances=numTrianglesAtBeginningOfData; |
| 145 | resolveLevel <= kMaxResolveLevel; |
| 146 | ++resolveLevel, numExtraInstances=0) { |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 147 | int instanceCountAtCurrLevel = fResolveLevelCounts[resolveLevel]; |
Chris Dalton | 489fa0b | 2021-05-14 17:41:17 -0600 | [diff] [blame] | 148 | if (!(instanceCountAtCurrLevel + numExtraInstances)) { |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 149 | SkDEBUGCODE(instanceLocations[resolveLevel] = nullptr;) |
| 150 | continue; |
| 151 | } |
Chris Dalton | 8731a71 | 2021-05-14 14:48:54 -0600 | [diff] [blame] | 152 | instanceLocations[resolveLevel] = instanceWriter.makeOffset(0); |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 153 | SkASSERT(fIndirectDrawCount < indirectLockCnt); |
Chris Dalton | a9f759d | 2021-05-18 12:37:08 -0600 | [diff] [blame] | 154 | GrCurveMiddleOutShader::WriteDrawIndirectCmd(&indirectWriter, resolveLevel, |
Chris Dalton | 489fa0b | 2021-05-14 17:41:17 -0600 | [diff] [blame] | 155 | instanceCountAtCurrLevel + numExtraInstances, |
| 156 | currentBaseInstance); |
Chris Dalton | a6a3d05 | 2021-02-07 20:56:36 -0700 | [diff] [blame] | 157 | ++fIndirectDrawCount; |
Chris Dalton | 489fa0b | 2021-05-14 17:41:17 -0600 | [diff] [blame] | 158 | currentBaseInstance += instanceCountAtCurrLevel + numExtraInstances; |
Chris Dalton | 8731a71 | 2021-05-14 14:48:54 -0600 | [diff] [blame] | 159 | instanceWriter = instanceWriter.makeOffset(instanceCountAtCurrLevel * 4 * sizeof(SkPoint)); |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | target->putBackIndirectDraws(indirectLockCnt - fIndirectDrawCount); |
| 163 | |
| 164 | #ifdef SK_DEBUG |
Chris Dalton | 8731a71 | 2021-05-14 14:48:54 -0600 | [diff] [blame] | 165 | SkASSERT(currentBaseInstance == |
| 166 | fBaseInstance + numTrianglesAtBeginningOfData + fOuterCurveInstanceCount); |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 167 | |
Chris Dalton | 8731a71 | 2021-05-14 14:48:54 -0600 | [diff] [blame] | 168 | GrVertexWriter endLocations[kMaxResolveLevel + 1]; |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 169 | int lastResolveLevel = 0; |
| 170 | for (int resolveLevel = 1; resolveLevel <= kMaxResolveLevel; ++resolveLevel) { |
| 171 | if (!instanceLocations[resolveLevel]) { |
| 172 | endLocations[resolveLevel] = nullptr; |
| 173 | continue; |
| 174 | } |
Chris Dalton | 8731a71 | 2021-05-14 14:48:54 -0600 | [diff] [blame] | 175 | endLocations[lastResolveLevel] = instanceLocations[resolveLevel].makeOffset(0); |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 176 | lastResolveLevel = resolveLevel; |
| 177 | } |
Chris Dalton | 8731a71 | 2021-05-14 14:48:54 -0600 | [diff] [blame] | 178 | endLocations[lastResolveLevel] = instanceWriter.makeOffset(0); |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 179 | #endif |
| 180 | |
| 181 | fTotalInstanceCount = numTrianglesAtBeginningOfData; |
| 182 | |
| 183 | // Write out the cubic instances. |
| 184 | if (fOuterCurveInstanceCount) { |
| 185 | GrVectorXform xform(viewMatrix); |
| 186 | for (auto [verb, pts, w] : SkPathPriv::Iterate(path)) { |
| 187 | int level; |
| 188 | switch (verb) { |
| 189 | default: |
| 190 | continue; |
| 191 | case SkPathVerb::kConic: |
Chris Dalton | 50f5e68 | 2021-04-16 22:47:03 -0600 | [diff] [blame] | 192 | level = GrWangsFormula::conic_log2(1.f / kLinearizationPrecision, pts, *w, |
Tyler Denniston | bc2fa2b | 2021-02-08 11:39:34 -0500 | [diff] [blame] | 193 | xform); |
| 194 | break; |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 195 | case SkPathVerb::kQuad: |
Chris Dalton | 50f5e68 | 2021-04-16 22:47:03 -0600 | [diff] [blame] | 196 | level = GrWangsFormula::quadratic_log2(kLinearizationPrecision, pts, xform); |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 197 | break; |
| 198 | case SkPathVerb::kCubic: |
Chris Dalton | 50f5e68 | 2021-04-16 22:47:03 -0600 | [diff] [blame] | 199 | level = GrWangsFormula::cubic_log2(kLinearizationPrecision, pts, xform); |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 200 | break; |
| 201 | } |
| 202 | if (level == 0) { |
| 203 | continue; |
| 204 | } |
| 205 | level = std::min(level, kMaxResolveLevel); |
| 206 | switch (verb) { |
| 207 | case SkPathVerb::kQuad: |
Chris Dalton | 8731a71 | 2021-05-14 14:48:54 -0600 | [diff] [blame] | 208 | GrPathUtils::writeQuadAsCubic(pts, &instanceLocations[level]); |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 209 | break; |
| 210 | case SkPathVerb::kCubic: |
Chris Dalton | 8731a71 | 2021-05-14 14:48:54 -0600 | [diff] [blame] | 211 | instanceLocations[level].writeArray(pts, 4); |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 212 | break; |
| 213 | case SkPathVerb::kConic: |
Chris Dalton | 8731a71 | 2021-05-14 14:48:54 -0600 | [diff] [blame] | 214 | GrPathShader::WriteConicPatch(pts, *w, &instanceLocations[level]); |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 215 | break; |
| 216 | default: |
| 217 | SkUNREACHABLE; |
| 218 | } |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 219 | ++fTotalInstanceCount; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | #ifdef SK_DEBUG |
| 224 | for (int i = 1; i <= kMaxResolveLevel; ++i) { |
| 225 | SkASSERT(instanceLocations[i] == endLocations[i]); |
| 226 | } |
| 227 | SkASSERT(fTotalInstanceCount == numTrianglesAtBeginningOfData + fOuterCurveInstanceCount); |
| 228 | #endif |
| 229 | |
| 230 | vertexAlloc.unlock(fTotalInstanceCount); |
| 231 | } |
| 232 | |
| 233 | void GrPathIndirectTessellator::draw(GrOpFlushState* flushState) const { |
| 234 | if (fIndirectDrawCount) { |
Chris Dalton | 489fa0b | 2021-05-14 17:41:17 -0600 | [diff] [blame] | 235 | flushState->bindBuffers(nullptr, fInstanceBuffer, nullptr); |
| 236 | flushState->drawIndirect(fIndirectDrawBuffer.get(), fIndirectDrawOffset, |
| 237 | fIndirectDrawCount); |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | |
| 241 | void GrPathIndirectTessellator::drawHullInstances(GrOpFlushState* flushState) const { |
| 242 | if (fTotalInstanceCount) { |
| 243 | flushState->bindBuffers(nullptr, fInstanceBuffer, nullptr); |
| 244 | flushState->drawInstanced(fTotalInstanceCount, fBaseInstance, 4, 0); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | void GrPathOuterCurveTessellator::prepare(GrMeshDrawOp::Target* target, const SkMatrix& matrix, |
Chris Dalton | ebb37e7 | 2021-01-27 17:59:45 -0700 | [diff] [blame] | 249 | const SkPath& path, |
| 250 | const BreadcrumbTriangleList* breadcrumbTriangleList) { |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 251 | SkASSERT(target->caps().shaderCaps()->tessellationSupport()); |
| 252 | SkASSERT(!fPatchBuffer); |
| 253 | SkASSERT(fPatchVertexCount == 0); |
| 254 | |
| 255 | int vertexLockCount = path.countVerbs() * 4; |
Chris Dalton | 2758a31 | 2021-05-20 10:46:25 -0600 | [diff] [blame] | 256 | if (fDrawInnerFan) { |
| 257 | vertexLockCount += max_triangles_in_inner_fan(path) * 4; |
| 258 | } |
| 259 | if (breadcrumbTriangleList) { |
| 260 | vertexLockCount += breadcrumbTriangleList->count() * 4; |
| 261 | } |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 262 | GrEagerDynamicVertexAllocator vertexAlloc(target, &fPatchBuffer, &fBasePatchVertex); |
Chris Dalton | 2758a31 | 2021-05-20 10:46:25 -0600 | [diff] [blame] | 263 | GrVertexWriter vertexWriter = vertexAlloc.lock<SkPoint>(vertexLockCount); |
| 264 | if (!vertexWriter) { |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 265 | return; |
| 266 | } |
| 267 | |
Chris Dalton | 2758a31 | 2021-05-20 10:46:25 -0600 | [diff] [blame] | 268 | GrMiddleOutPolygonTriangulator middleOut( |
| 269 | &vertexWriter, GrMiddleOutPolygonTriangulator::OutputType::kConicsWithInfiniteWeight, |
| 270 | path.countVerbs()); |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 271 | for (auto [verb, pts, w] : SkPathPriv::Iterate(path)) { |
| 272 | switch (verb) { |
Chris Dalton | 2758a31 | 2021-05-20 10:46:25 -0600 | [diff] [blame] | 273 | case SkPathVerb::kMove: |
| 274 | if (fDrawInnerFan) { |
| 275 | middleOut.closeAndMove(pts[0]); |
| 276 | } |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 277 | continue; |
Chris Dalton | 2758a31 | 2021-05-20 10:46:25 -0600 | [diff] [blame] | 278 | case SkPathVerb::kClose: |
| 279 | continue; |
| 280 | case SkPathVerb::kLine: |
| 281 | break; |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 282 | case SkPathVerb::kQuad: |
Chris Dalton | 2758a31 | 2021-05-20 10:46:25 -0600 | [diff] [blame] | 283 | GrPathUtils::writeQuadAsCubic(pts, &vertexWriter); |
| 284 | fPatchVertexCount += 4; |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 285 | break; |
| 286 | case SkPathVerb::kCubic: |
Chris Dalton | 2758a31 | 2021-05-20 10:46:25 -0600 | [diff] [blame] | 287 | vertexWriter.writeArray(pts, 4); |
| 288 | fPatchVertexCount += 4; |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 289 | break; |
| 290 | case SkPathVerb::kConic: |
Chris Dalton | 2758a31 | 2021-05-20 10:46:25 -0600 | [diff] [blame] | 291 | GrPathShader::WriteConicPatch(pts, *w, &vertexWriter); |
| 292 | fPatchVertexCount += 4; |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 293 | break; |
| 294 | } |
Chris Dalton | 2758a31 | 2021-05-20 10:46:25 -0600 | [diff] [blame] | 295 | if (fDrawInnerFan) { |
| 296 | middleOut.pushVertex(pts[SkPathPriv::PtsInIter((unsigned)verb) - 1]); |
| 297 | } |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 298 | } |
Chris Dalton | 2758a31 | 2021-05-20 10:46:25 -0600 | [diff] [blame] | 299 | if (fDrawInnerFan) { |
| 300 | fPatchVertexCount += middleOut.close() * 4; |
| 301 | } |
| 302 | if (breadcrumbTriangleList) { |
| 303 | fPatchVertexCount += write_breadcrumb_triangles(&vertexWriter, breadcrumbTriangleList) * 4; |
| 304 | } |
| 305 | SkASSERT(fPatchVertexCount <= vertexLockCount); |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 306 | |
| 307 | vertexAlloc.unlock(fPatchVertexCount); |
| 308 | } |
| 309 | |
| 310 | void GrPathWedgeTessellator::prepare(GrMeshDrawOp::Target* target, const SkMatrix& matrix, |
Chris Dalton | ebb37e7 | 2021-01-27 17:59:45 -0700 | [diff] [blame] | 311 | const SkPath& path, |
| 312 | const BreadcrumbTriangleList* breadcrumbTriangleList) { |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 313 | SkASSERT(target->caps().shaderCaps()->tessellationSupport()); |
Chris Dalton | ebb37e7 | 2021-01-27 17:59:45 -0700 | [diff] [blame] | 314 | SkASSERT(!breadcrumbTriangleList); |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 315 | SkASSERT(!fPatchBuffer); |
| 316 | SkASSERT(fPatchVertexCount == 0); |
| 317 | |
Chris Dalton | 2758a31 | 2021-05-20 10:46:25 -0600 | [diff] [blame] | 318 | // We emit one wedge per path segment. Each wedge has 5 vertices. |
| 319 | int maxVertices = max_segments_in_path(path) * 5; |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 320 | |
| 321 | GrEagerDynamicVertexAllocator vertexAlloc(target, &fPatchBuffer, &fBasePatchVertex); |
| 322 | auto* vertexData = vertexAlloc.lock<SkPoint>(maxVertices); |
| 323 | if (!vertexData) { |
| 324 | return; |
| 325 | } |
| 326 | |
| 327 | GrMidpointContourParser parser(path); |
| 328 | while (parser.parseNextContour()) { |
| 329 | SkPoint midpoint = parser.currentMidpoint(); |
| 330 | SkPoint startPoint = {0, 0}; |
| 331 | SkPoint lastPoint = startPoint; |
| 332 | for (auto [verb, pts, w] : parser.currentContour()) { |
| 333 | switch (verb) { |
| 334 | case SkPathVerb::kMove: |
| 335 | startPoint = lastPoint = pts[0]; |
| 336 | continue; |
| 337 | case SkPathVerb::kClose: |
| 338 | continue; // Ignore. We can assume an implicit close at the end. |
| 339 | case SkPathVerb::kLine: |
| 340 | GrPathUtils::convertLineToCubic(pts[0], pts[1], vertexData + fPatchVertexCount); |
| 341 | lastPoint = pts[1]; |
| 342 | break; |
| 343 | case SkPathVerb::kQuad: |
| 344 | GrPathUtils::convertQuadToCubic(pts, vertexData + fPatchVertexCount); |
| 345 | lastPoint = pts[2]; |
| 346 | break; |
| 347 | case SkPathVerb::kCubic: |
| 348 | memcpy(vertexData + fPatchVertexCount, pts, sizeof(SkPoint) * 4); |
| 349 | lastPoint = pts[3]; |
| 350 | break; |
| 351 | case SkPathVerb::kConic: |
| 352 | GrPathShader::WriteConicPatch(pts, *w, vertexData + fPatchVertexCount); |
| 353 | lastPoint = pts[2]; |
| 354 | break; |
| 355 | } |
| 356 | vertexData[fPatchVertexCount + 4] = midpoint; |
| 357 | fPatchVertexCount += 5; |
| 358 | } |
| 359 | if (lastPoint != startPoint) { |
| 360 | GrPathUtils::convertLineToCubic(lastPoint, startPoint, vertexData + fPatchVertexCount); |
| 361 | vertexData[fPatchVertexCount + 4] = midpoint; |
| 362 | fPatchVertexCount += 5; |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | vertexAlloc.unlock(fPatchVertexCount); |
| 367 | } |
| 368 | |
| 369 | void GrPathHardwareTessellator::draw(GrOpFlushState* flushState) const { |
| 370 | if (fPatchVertexCount) { |
| 371 | flushState->bindBuffers(nullptr, nullptr, fPatchBuffer); |
| 372 | flushState->draw(fPatchVertexCount, fBasePatchVertex); |
| 373 | if (flushState->caps().requiresManualFBBarrierAfterTessellatedStencilDraw()) { |
| 374 | flushState->gpu()->insertManualFramebufferBarrier(); // http://skbug.com/9739 |
| 375 | } |
| 376 | } |
| 377 | } |