Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 | |
| 8 | #include "GrCCPathParser.h" |
| 9 | |
| 10 | #include "GrCaps.h" |
| 11 | #include "GrGpuCommandBuffer.h" |
| 12 | #include "GrOnFlushResourceProvider.h" |
| 13 | #include "GrOpFlushState.h" |
| 14 | #include "SkMathPriv.h" |
| 15 | #include "SkPath.h" |
| 16 | #include "SkPathPriv.h" |
| 17 | #include "SkPoint.h" |
| 18 | #include "ccpr/GrCCGeometry.h" |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 19 | #include <stdlib.h> |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 20 | |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 21 | using TriPointInstance = GrCCCoverageProcessor::TriPointInstance; |
| 22 | using QuadPointInstance = GrCCCoverageProcessor::QuadPointInstance; |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 23 | |
| 24 | GrCCPathParser::GrCCPathParser(int maxTotalPaths, int maxPathPoints, int numSkPoints, |
| 25 | int numSkVerbs) |
| 26 | : fLocalDevPtsBuffer(maxPathPoints + 1) // Overallocate by one point to accomodate for |
| 27 | // overflow with Sk4f. (See parsePath.) |
| 28 | , fGeometry(numSkPoints, numSkVerbs) |
| 29 | , fPathsInfo(maxTotalPaths) |
| 30 | , fScissorSubBatches(maxTotalPaths) |
| 31 | , fTotalPrimitiveCounts{PrimitiveTallies(), PrimitiveTallies()} { |
| 32 | // Batches decide what to draw by looking where the previous one ended. Define initial batches |
| 33 | // that "end" at the beginning of the data. These will not be drawn, but will only be be read by |
| 34 | // the first actual batch. |
| 35 | fScissorSubBatches.push_back() = {PrimitiveTallies(), SkIRect::MakeEmpty()}; |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 36 | fCoverageCountBatches.push_back() = {PrimitiveTallies(), fScissorSubBatches.count(), |
| 37 | PrimitiveTallies()}; |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | void GrCCPathParser::parsePath(const SkMatrix& m, const SkPath& path, SkRect* devBounds, |
| 41 | SkRect* devBounds45) { |
| 42 | const SkPoint* pts = SkPathPriv::PointData(path); |
| 43 | int numPts = path.countPoints(); |
| 44 | SkASSERT(numPts + 1 <= fLocalDevPtsBuffer.count()); |
| 45 | |
| 46 | if (!numPts) { |
| 47 | devBounds->setEmpty(); |
| 48 | devBounds45->setEmpty(); |
| 49 | this->parsePath(path, nullptr); |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | // m45 transforms path points into "45 degree" device space. A bounding box in this space gives |
| 54 | // the circumscribing octagon's diagonals. We could use SK_ScalarRoot2Over2, but an orthonormal |
| 55 | // transform is not necessary as long as the shader uses the correct inverse. |
| 56 | SkMatrix m45; |
| 57 | m45.setSinCos(1, 1); |
| 58 | m45.preConcat(m); |
| 59 | |
| 60 | // X,Y,T are two parallel view matrices that accumulate two bounding boxes as they map points: |
| 61 | // device-space bounds and "45 degree" device-space bounds (| 1 -1 | * devCoords). |
| 62 | // | 1 1 | |
| 63 | Sk4f X = Sk4f(m.getScaleX(), m.getSkewY(), m45.getScaleX(), m45.getSkewY()); |
| 64 | Sk4f Y = Sk4f(m.getSkewX(), m.getScaleY(), m45.getSkewX(), m45.getScaleY()); |
| 65 | Sk4f T = Sk4f(m.getTranslateX(), m.getTranslateY(), m45.getTranslateX(), m45.getTranslateY()); |
| 66 | |
| 67 | // Map the path's points to device space and accumulate bounding boxes. |
| 68 | Sk4f devPt = SkNx_fma(Y, Sk4f(pts[0].y()), T); |
| 69 | devPt = SkNx_fma(X, Sk4f(pts[0].x()), devPt); |
| 70 | Sk4f topLeft = devPt; |
| 71 | Sk4f bottomRight = devPt; |
| 72 | |
| 73 | // Store all 4 values [dev.x, dev.y, dev45.x, dev45.y]. We are only interested in the first two, |
| 74 | // and will overwrite [dev45.x, dev45.y] with the next point. This is why the dst buffer must |
| 75 | // be at least one larger than the number of points. |
| 76 | devPt.store(&fLocalDevPtsBuffer[0]); |
| 77 | |
| 78 | for (int i = 1; i < numPts; ++i) { |
| 79 | devPt = SkNx_fma(Y, Sk4f(pts[i].y()), T); |
| 80 | devPt = SkNx_fma(X, Sk4f(pts[i].x()), devPt); |
| 81 | topLeft = Sk4f::Min(topLeft, devPt); |
| 82 | bottomRight = Sk4f::Max(bottomRight, devPt); |
| 83 | devPt.store(&fLocalDevPtsBuffer[i]); |
| 84 | } |
| 85 | |
| 86 | SkPoint topLeftPts[2], bottomRightPts[2]; |
| 87 | topLeft.store(topLeftPts); |
| 88 | bottomRight.store(bottomRightPts); |
| 89 | devBounds->setLTRB(topLeftPts[0].x(), topLeftPts[0].y(), bottomRightPts[0].x(), |
| 90 | bottomRightPts[0].y()); |
| 91 | devBounds45->setLTRB(topLeftPts[1].x(), topLeftPts[1].y(), bottomRightPts[1].x(), |
| 92 | bottomRightPts[1].y()); |
| 93 | |
| 94 | this->parsePath(path, fLocalDevPtsBuffer.get()); |
| 95 | } |
| 96 | |
| 97 | void GrCCPathParser::parseDeviceSpacePath(const SkPath& deviceSpacePath) { |
| 98 | this->parsePath(deviceSpacePath, SkPathPriv::PointData(deviceSpacePath)); |
| 99 | } |
| 100 | |
| 101 | void GrCCPathParser::parsePath(const SkPath& path, const SkPoint* deviceSpacePts) { |
| 102 | SkASSERT(!fInstanceBuffer); // Can't call after finalize(). |
| 103 | SkASSERT(!fParsingPath); // Call saveParsedPath() or discardParsedPath() for the last one first. |
| 104 | SkDEBUGCODE(fParsingPath = true); |
| 105 | SkASSERT(path.isEmpty() || deviceSpacePts); |
| 106 | |
| 107 | fCurrPathPointsIdx = fGeometry.points().count(); |
| 108 | fCurrPathVerbsIdx = fGeometry.verbs().count(); |
| 109 | fCurrPathPrimitiveCounts = PrimitiveTallies(); |
| 110 | |
| 111 | fGeometry.beginPath(); |
| 112 | |
| 113 | if (path.isEmpty()) { |
| 114 | return; |
| 115 | } |
| 116 | |
| 117 | int ptsIdx = 0; |
| 118 | bool insideContour = false; |
| 119 | |
| 120 | for (SkPath::Verb verb : SkPathPriv::Verbs(path)) { |
| 121 | switch (verb) { |
| 122 | case SkPath::kMove_Verb: |
| 123 | this->endContourIfNeeded(insideContour); |
| 124 | fGeometry.beginContour(deviceSpacePts[ptsIdx]); |
| 125 | ++ptsIdx; |
| 126 | insideContour = true; |
| 127 | continue; |
| 128 | case SkPath::kClose_Verb: |
| 129 | this->endContourIfNeeded(insideContour); |
| 130 | insideContour = false; |
| 131 | continue; |
| 132 | case SkPath::kLine_Verb: |
| 133 | fGeometry.lineTo(deviceSpacePts[ptsIdx]); |
| 134 | ++ptsIdx; |
| 135 | continue; |
| 136 | case SkPath::kQuad_Verb: |
| 137 | fGeometry.quadraticTo(deviceSpacePts[ptsIdx], deviceSpacePts[ptsIdx + 1]); |
| 138 | ptsIdx += 2; |
| 139 | continue; |
| 140 | case SkPath::kCubic_Verb: |
| 141 | fGeometry.cubicTo(deviceSpacePts[ptsIdx], deviceSpacePts[ptsIdx + 1], |
| 142 | deviceSpacePts[ptsIdx + 2]); |
| 143 | ptsIdx += 3; |
| 144 | continue; |
| 145 | case SkPath::kConic_Verb: |
| 146 | SK_ABORT("Conics are not supported."); |
| 147 | default: |
| 148 | SK_ABORT("Unexpected path verb."); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | this->endContourIfNeeded(insideContour); |
| 153 | } |
| 154 | |
| 155 | void GrCCPathParser::endContourIfNeeded(bool insideContour) { |
| 156 | if (insideContour) { |
| 157 | fCurrPathPrimitiveCounts += fGeometry.endContour(); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | void GrCCPathParser::saveParsedPath(ScissorMode scissorMode, const SkIRect& clippedDevIBounds, |
| 162 | int16_t atlasOffsetX, int16_t atlasOffsetY) { |
| 163 | SkASSERT(fParsingPath); |
| 164 | |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 165 | fPathsInfo.emplace_back(scissorMode, atlasOffsetX, atlasOffsetY); |
| 166 | |
| 167 | // Tessellate fans from very large and/or simple paths, in order to reduce overdraw. |
| 168 | int numVerbs = fGeometry.verbs().count() - fCurrPathVerbsIdx - 1; |
| 169 | int64_t tessellationWork = (int64_t)numVerbs * (32 - SkCLZ(numVerbs)); // N log N. |
| 170 | int64_t fanningWork = (int64_t)clippedDevIBounds.height() * clippedDevIBounds.width(); |
| 171 | if (tessellationWork * (50*50) + (100*100) < fanningWork) { // Don't tessellate under 100x100. |
| 172 | fCurrPathPrimitiveCounts.fTriangles = |
Chris Dalton | 703b476 | 2018-04-06 16:11:48 -0600 | [diff] [blame^] | 173 | fCurrPathPrimitiveCounts.fWeightedTriangles = 0; |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 174 | |
| 175 | const SkTArray<GrCCGeometry::Verb, true>& verbs = fGeometry.verbs(); |
| 176 | const SkTArray<SkPoint, true>& pts = fGeometry.points(); |
| 177 | int ptsIdx = fCurrPathPointsIdx; |
| 178 | |
Chris Dalton | 45e4660 | 2018-02-15 12:27:29 -0700 | [diff] [blame] | 179 | // Build an SkPath of the Redbook fan. We use "winding" fill type right now because we are |
| 180 | // producing a coverage count, and must fill in every region that has non-zero wind. The |
| 181 | // path processor will convert coverage count to the appropriate fill type later. |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 182 | SkPath fan; |
Chris Dalton | 45e4660 | 2018-02-15 12:27:29 -0700 | [diff] [blame] | 183 | fan.setFillType(SkPath::kWinding_FillType); |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 184 | SkASSERT(GrCCGeometry::Verb::kBeginPath == verbs[fCurrPathVerbsIdx]); |
| 185 | for (int i = fCurrPathVerbsIdx + 1; i < fGeometry.verbs().count(); ++i) { |
| 186 | switch (verbs[i]) { |
| 187 | case GrCCGeometry::Verb::kBeginPath: |
| 188 | SK_ABORT("Invalid GrCCGeometry"); |
| 189 | continue; |
| 190 | |
| 191 | case GrCCGeometry::Verb::kBeginContour: |
| 192 | fan.moveTo(pts[ptsIdx++]); |
| 193 | continue; |
| 194 | |
| 195 | case GrCCGeometry::Verb::kLineTo: |
| 196 | fan.lineTo(pts[ptsIdx++]); |
| 197 | continue; |
| 198 | |
| 199 | case GrCCGeometry::Verb::kMonotonicQuadraticTo: |
| 200 | fan.lineTo(pts[ptsIdx + 1]); |
| 201 | ptsIdx += 2; |
| 202 | continue; |
| 203 | |
| 204 | case GrCCGeometry::Verb::kMonotonicCubicTo: |
| 205 | fan.lineTo(pts[ptsIdx + 2]); |
| 206 | ptsIdx += 3; |
| 207 | continue; |
| 208 | |
| 209 | case GrCCGeometry::Verb::kEndClosedContour: |
| 210 | case GrCCGeometry::Verb::kEndOpenContour: |
| 211 | fan.close(); |
| 212 | continue; |
| 213 | } |
| 214 | } |
| 215 | GrTessellator::WindingVertex* vertices = nullptr; |
| 216 | int count = GrTessellator::PathToVertices(fan, std::numeric_limits<float>::infinity(), |
| 217 | SkRect::Make(clippedDevIBounds), &vertices); |
| 218 | SkASSERT(0 == count % 3); |
| 219 | for (int i = 0; i < count; i += 3) { |
Chris Dalton | 45e4660 | 2018-02-15 12:27:29 -0700 | [diff] [blame] | 220 | int tessWinding = vertices[i].fWinding; |
| 221 | SkASSERT(tessWinding == vertices[i + 1].fWinding); |
| 222 | SkASSERT(tessWinding == vertices[i + 2].fWinding); |
| 223 | |
| 224 | // Ensure this triangle's points actually wind in the same direction as tessWinding. |
| 225 | // CCPR shaders use the sign of wind to determine which direction to bloat, so even for |
| 226 | // "wound" triangles the winding sign and point ordering need to agree. |
| 227 | float ax = vertices[i].fPos.fX - vertices[i + 1].fPos.fX; |
| 228 | float ay = vertices[i].fPos.fY - vertices[i + 1].fPos.fY; |
| 229 | float bx = vertices[i].fPos.fX - vertices[i + 2].fPos.fX; |
| 230 | float by = vertices[i].fPos.fY - vertices[i + 2].fPos.fY; |
| 231 | float wind = ax*by - ay*bx; |
| 232 | if ((wind > 0) != (-tessWinding > 0)) { // Tessellator has opposite winding sense. |
| 233 | std::swap(vertices[i + 1].fPos, vertices[i + 2].fPos); |
| 234 | } |
| 235 | |
| 236 | if (1 == abs(tessWinding)) { |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 237 | ++fCurrPathPrimitiveCounts.fTriangles; |
| 238 | } else { |
Chris Dalton | 703b476 | 2018-04-06 16:11:48 -0600 | [diff] [blame^] | 239 | ++fCurrPathPrimitiveCounts.fWeightedTriangles; |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | |
Chris Dalton | ad06544 | 2018-03-08 22:41:33 -0700 | [diff] [blame] | 243 | fPathsInfo.back().adoptFanTessellation(vertices, count); |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 244 | } |
| 245 | |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 246 | fTotalPrimitiveCounts[(int)scissorMode] += fCurrPathPrimitiveCounts; |
| 247 | |
| 248 | if (ScissorMode::kScissored == scissorMode) { |
| 249 | fScissorSubBatches.push_back() = {fTotalPrimitiveCounts[(int)ScissorMode::kScissored], |
| 250 | clippedDevIBounds.makeOffset(atlasOffsetX, atlasOffsetY)}; |
| 251 | } |
| 252 | |
| 253 | SkDEBUGCODE(fParsingPath = false); |
| 254 | } |
| 255 | |
| 256 | void GrCCPathParser::discardParsedPath() { |
| 257 | SkASSERT(fParsingPath); |
| 258 | fGeometry.resize_back(fCurrPathPointsIdx, fCurrPathVerbsIdx); |
| 259 | SkDEBUGCODE(fParsingPath = false); |
| 260 | } |
| 261 | |
| 262 | GrCCPathParser::CoverageCountBatchID GrCCPathParser::closeCurrentBatch() { |
| 263 | SkASSERT(!fInstanceBuffer); |
| 264 | SkASSERT(!fCoverageCountBatches.empty()); |
| 265 | |
Chris Dalton | a883aca | 2018-03-08 23:05:30 -0700 | [diff] [blame] | 266 | const auto& lastBatch = fCoverageCountBatches.back(); |
| 267 | int maxMeshes = 1 + fScissorSubBatches.count() - lastBatch.fEndScissorSubBatchIdx; |
| 268 | fMaxMeshesPerDraw = SkTMax(fMaxMeshesPerDraw, maxMeshes); |
| 269 | |
| 270 | const auto& lastScissorSubBatch = fScissorSubBatches[lastBatch.fEndScissorSubBatchIdx - 1]; |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 271 | PrimitiveTallies batchTotalCounts = fTotalPrimitiveCounts[(int)ScissorMode::kNonScissored] - |
| 272 | lastBatch.fEndNonScissorIndices; |
| 273 | batchTotalCounts += fTotalPrimitiveCounts[(int)ScissorMode::kScissored] - |
| 274 | lastScissorSubBatch.fEndPrimitiveIndices; |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 275 | |
Chris Dalton | a883aca | 2018-03-08 23:05:30 -0700 | [diff] [blame] | 276 | // This will invalidate lastBatch. |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 277 | fCoverageCountBatches.push_back() = { |
| 278 | fTotalPrimitiveCounts[(int)ScissorMode::kNonScissored], |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 279 | fScissorSubBatches.count(), |
| 280 | batchTotalCounts |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 281 | }; |
| 282 | return fCoverageCountBatches.count() - 1; |
| 283 | } |
| 284 | |
| 285 | // Emits a contour's triangle fan. |
| 286 | // |
| 287 | // Classic Redbook fanning would be the triangles: [0 1 2], [0 2 3], ..., [0 n-2 n-1]. |
| 288 | // |
| 289 | // This function emits the triangle: [0 n/3 n*2/3], and then recurses on all three sides. The |
| 290 | // advantage to this approach is that for a convex-ish contour, it generates larger triangles. |
| 291 | // Classic fanning tends to generate long, skinny triangles, which are expensive to draw since they |
| 292 | // have a longer perimeter to rasterize and antialias. |
| 293 | // |
| 294 | // The indices array indexes the fan's points (think: glDrawElements), and must have at least log3 |
| 295 | // elements past the end for this method to use as scratch space. |
| 296 | // |
| 297 | // Returns the next triangle instance after the final one emitted. |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 298 | static TriPointInstance* emit_recursive_fan(const SkTArray<SkPoint, true>& pts, |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 299 | SkTArray<int32_t, true>& indices, int firstIndex, |
| 300 | int indexCount, const Sk2f& atlasOffset, |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 301 | TriPointInstance out[]) { |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 302 | if (indexCount < 3) { |
| 303 | return out; |
| 304 | } |
| 305 | |
| 306 | int32_t oneThirdCount = indexCount / 3; |
| 307 | int32_t twoThirdsCount = (2 * indexCount) / 3; |
| 308 | out++->set(pts[indices[firstIndex]], pts[indices[firstIndex + oneThirdCount]], |
| 309 | pts[indices[firstIndex + twoThirdsCount]], atlasOffset); |
| 310 | |
| 311 | out = emit_recursive_fan(pts, indices, firstIndex, oneThirdCount + 1, atlasOffset, out); |
| 312 | out = emit_recursive_fan(pts, indices, firstIndex + oneThirdCount, |
| 313 | twoThirdsCount - oneThirdCount + 1, atlasOffset, out); |
| 314 | |
| 315 | int endIndex = firstIndex + indexCount; |
| 316 | int32_t oldValue = indices[endIndex]; |
| 317 | indices[endIndex] = indices[firstIndex]; |
| 318 | out = emit_recursive_fan(pts, indices, firstIndex + twoThirdsCount, |
| 319 | indexCount - twoThirdsCount + 1, atlasOffset, out); |
| 320 | indices[endIndex] = oldValue; |
| 321 | |
| 322 | return out; |
| 323 | } |
| 324 | |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 325 | static void emit_tessellated_fan(const GrTessellator::WindingVertex* vertices, int numVertices, |
| 326 | const Sk2f& atlasOffset, TriPointInstance* triPointInstanceData, |
| 327 | QuadPointInstance* quadPointInstanceData, |
| 328 | GrCCGeometry::PrimitiveTallies* indices) { |
| 329 | for (int i = 0; i < numVertices; i += 3) { |
| 330 | if (1 == abs(vertices[i].fWinding)) { |
| 331 | triPointInstanceData[indices->fTriangles++].set(vertices[i].fPos, vertices[i + 1].fPos, |
| 332 | vertices[i + 2].fPos, atlasOffset); |
| 333 | } else { |
Chris Dalton | 703b476 | 2018-04-06 16:11:48 -0600 | [diff] [blame^] | 334 | quadPointInstanceData[indices->fWeightedTriangles++].setW( |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 335 | vertices[i].fPos, vertices[i+1].fPos, vertices[i + 2].fPos, atlasOffset, |
Chris Dalton | 45e4660 | 2018-02-15 12:27:29 -0700 | [diff] [blame] | 336 | // Tessellator has opposite winding sense. |
| 337 | -static_cast<float>(vertices[i].fWinding)); |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 338 | } |
| 339 | } |
| 340 | } |
| 341 | |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 342 | bool GrCCPathParser::finalize(GrOnFlushResourceProvider* onFlushRP) { |
| 343 | SkASSERT(!fParsingPath); // Call saveParsedPath() or discardParsedPath(). |
| 344 | SkASSERT(fCoverageCountBatches.back().fEndNonScissorIndices == // Call closeCurrentBatch(). |
| 345 | fTotalPrimitiveCounts[(int)ScissorMode::kNonScissored]); |
| 346 | SkASSERT(fCoverageCountBatches.back().fEndScissorSubBatchIdx == fScissorSubBatches.count()); |
| 347 | |
| 348 | // Here we build a single instance buffer to share with every internal batch. |
| 349 | // |
| 350 | // CCPR processs 3 different types of primitives: triangles, quadratics, cubics. Each primitive |
| 351 | // type is further divided into instances that require a scissor and those that don't. This |
| 352 | // leaves us with 3*2 = 6 independent instance arrays to build for the GPU. |
| 353 | // |
| 354 | // Rather than place each instance array in its own GPU buffer, we allocate a single |
| 355 | // megabuffer and lay them all out side-by-side. We can offset the "baseInstance" parameter in |
| 356 | // our draw calls to direct the GPU to the applicable elements within a given array. |
| 357 | // |
| 358 | // We already know how big to make each of the 6 arrays from fTotalPrimitiveCounts, so layout is |
| 359 | // straightforward. Start with triangles and quadratics. They both view the instance buffer as |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 360 | // an array of TriPointInstance[], so we can begin at zero and lay them out one after the other. |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 361 | fBaseInstances[0].fTriangles = 0; |
| 362 | fBaseInstances[1].fTriangles = fBaseInstances[0].fTriangles + |
| 363 | fTotalPrimitiveCounts[0].fTriangles; |
| 364 | fBaseInstances[0].fQuadratics = fBaseInstances[1].fTriangles + |
| 365 | fTotalPrimitiveCounts[1].fTriangles; |
| 366 | fBaseInstances[1].fQuadratics = fBaseInstances[0].fQuadratics + |
| 367 | fTotalPrimitiveCounts[0].fQuadratics; |
| 368 | int triEndIdx = fBaseInstances[1].fQuadratics + fTotalPrimitiveCounts[1].fQuadratics; |
| 369 | |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 370 | // Wound triangles and cubics both view the same instance buffer as an array of |
| 371 | // QuadPointInstance[]. So, reinterpreting the instance data as QuadPointInstance[], we start |
| 372 | // them on the first index that will not overwrite previous TriPointInstance data. |
| 373 | int quadBaseIdx = |
| 374 | GR_CT_DIV_ROUND_UP(triEndIdx * sizeof(TriPointInstance), sizeof(QuadPointInstance)); |
Chris Dalton | 703b476 | 2018-04-06 16:11:48 -0600 | [diff] [blame^] | 375 | fBaseInstances[0].fWeightedTriangles = quadBaseIdx; |
| 376 | fBaseInstances[1].fWeightedTriangles = fBaseInstances[0].fWeightedTriangles + |
| 377 | fTotalPrimitiveCounts[0].fWeightedTriangles; |
| 378 | fBaseInstances[0].fCubics = fBaseInstances[1].fWeightedTriangles + |
| 379 | fTotalPrimitiveCounts[1].fWeightedTriangles; |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 380 | fBaseInstances[1].fCubics = fBaseInstances[0].fCubics + fTotalPrimitiveCounts[0].fCubics; |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 381 | int quadEndIdx = fBaseInstances[1].fCubics + fTotalPrimitiveCounts[1].fCubics; |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 382 | |
| 383 | fInstanceBuffer = onFlushRP->makeBuffer(kVertex_GrBufferType, |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 384 | quadEndIdx * sizeof(QuadPointInstance)); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 385 | if (!fInstanceBuffer) { |
| 386 | return false; |
| 387 | } |
| 388 | |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 389 | TriPointInstance* triPointInstanceData = static_cast<TriPointInstance*>(fInstanceBuffer->map()); |
| 390 | QuadPointInstance* quadPointInstanceData = |
| 391 | reinterpret_cast<QuadPointInstance*>(triPointInstanceData); |
| 392 | SkASSERT(quadPointInstanceData); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 393 | |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 394 | PathInfo* nextPathInfo = fPathsInfo.begin(); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 395 | float atlasOffsetX = 0.0, atlasOffsetY = 0.0; |
| 396 | Sk2f atlasOffset; |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 397 | PrimitiveTallies instanceIndices[2] = {fBaseInstances[0], fBaseInstances[1]}; |
| 398 | PrimitiveTallies* currIndices = nullptr; |
| 399 | SkSTArray<256, int32_t, true> currFan; |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 400 | bool currFanIsTessellated = false; |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 401 | |
| 402 | const SkTArray<SkPoint, true>& pts = fGeometry.points(); |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 403 | int ptsIdx = -1; |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 404 | |
| 405 | // Expand the ccpr verbs into GPU instance buffers. |
| 406 | for (GrCCGeometry::Verb verb : fGeometry.verbs()) { |
| 407 | switch (verb) { |
| 408 | case GrCCGeometry::Verb::kBeginPath: |
| 409 | SkASSERT(currFan.empty()); |
Chris Dalton | ad06544 | 2018-03-08 22:41:33 -0700 | [diff] [blame] | 410 | currIndices = &instanceIndices[(int)nextPathInfo->scissorMode()]; |
| 411 | atlasOffsetX = static_cast<float>(nextPathInfo->atlasOffsetX()); |
| 412 | atlasOffsetY = static_cast<float>(nextPathInfo->atlasOffsetY()); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 413 | atlasOffset = {atlasOffsetX, atlasOffsetY}; |
Chris Dalton | ad06544 | 2018-03-08 22:41:33 -0700 | [diff] [blame] | 414 | currFanIsTessellated = nextPathInfo->hasFanTessellation(); |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 415 | if (currFanIsTessellated) { |
Chris Dalton | ad06544 | 2018-03-08 22:41:33 -0700 | [diff] [blame] | 416 | emit_tessellated_fan(nextPathInfo->fanTessellation(), |
| 417 | nextPathInfo->fanTessellationCount(), atlasOffset, |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 418 | triPointInstanceData, quadPointInstanceData, currIndices); |
| 419 | } |
| 420 | ++nextPathInfo; |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 421 | continue; |
| 422 | |
| 423 | case GrCCGeometry::Verb::kBeginContour: |
| 424 | SkASSERT(currFan.empty()); |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 425 | ++ptsIdx; |
| 426 | if (!currFanIsTessellated) { |
| 427 | currFan.push_back(ptsIdx); |
| 428 | } |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 429 | continue; |
| 430 | |
| 431 | case GrCCGeometry::Verb::kLineTo: |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 432 | ++ptsIdx; |
| 433 | if (!currFanIsTessellated) { |
| 434 | SkASSERT(!currFan.empty()); |
| 435 | currFan.push_back(ptsIdx); |
| 436 | } |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 437 | continue; |
| 438 | |
| 439 | case GrCCGeometry::Verb::kMonotonicQuadraticTo: |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 440 | triPointInstanceData[currIndices->fQuadratics++].set(&pts[ptsIdx], atlasOffset); |
| 441 | ptsIdx += 2; |
| 442 | if (!currFanIsTessellated) { |
| 443 | SkASSERT(!currFan.empty()); |
| 444 | currFan.push_back(ptsIdx); |
| 445 | } |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 446 | continue; |
| 447 | |
| 448 | case GrCCGeometry::Verb::kMonotonicCubicTo: |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 449 | quadPointInstanceData[currIndices->fCubics++].set(&pts[ptsIdx], atlasOffsetX, |
| 450 | atlasOffsetY); |
| 451 | ptsIdx += 3; |
| 452 | if (!currFanIsTessellated) { |
| 453 | SkASSERT(!currFan.empty()); |
| 454 | currFan.push_back(ptsIdx); |
| 455 | } |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 456 | continue; |
| 457 | |
| 458 | case GrCCGeometry::Verb::kEndClosedContour: // endPt == startPt. |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 459 | if (!currFanIsTessellated) { |
| 460 | SkASSERT(!currFan.empty()); |
| 461 | currFan.pop_back(); |
| 462 | } |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 463 | // fallthru. |
| 464 | case GrCCGeometry::Verb::kEndOpenContour: // endPt != startPt. |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 465 | SkASSERT(!currFanIsTessellated || currFan.empty()); |
| 466 | if (!currFanIsTessellated && currFan.count() >= 3) { |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 467 | int fanSize = currFan.count(); |
| 468 | // Reserve space for emit_recursive_fan. Technically this can grow to |
| 469 | // fanSize + log3(fanSize), but we approximate with log2. |
| 470 | currFan.push_back_n(SkNextLog2(fanSize)); |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 471 | SkDEBUGCODE(TriPointInstance* end =) |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 472 | emit_recursive_fan(pts, currFan, 0, fanSize, atlasOffset, |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 473 | triPointInstanceData + currIndices->fTriangles); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 474 | currIndices->fTriangles += fanSize - 2; |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 475 | SkASSERT(triPointInstanceData + currIndices->fTriangles == end); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 476 | } |
| 477 | currFan.reset(); |
| 478 | continue; |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | fInstanceBuffer->unmap(); |
| 483 | |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 484 | SkASSERT(nextPathInfo == fPathsInfo.end()); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 485 | SkASSERT(ptsIdx == pts.count() - 1); |
| 486 | SkASSERT(instanceIndices[0].fTriangles == fBaseInstances[1].fTriangles); |
| 487 | SkASSERT(instanceIndices[1].fTriangles == fBaseInstances[0].fQuadratics); |
| 488 | SkASSERT(instanceIndices[0].fQuadratics == fBaseInstances[1].fQuadratics); |
| 489 | SkASSERT(instanceIndices[1].fQuadratics == triEndIdx); |
Chris Dalton | 703b476 | 2018-04-06 16:11:48 -0600 | [diff] [blame^] | 490 | SkASSERT(instanceIndices[0].fWeightedTriangles == fBaseInstances[1].fWeightedTriangles); |
| 491 | SkASSERT(instanceIndices[1].fWeightedTriangles == fBaseInstances[0].fCubics); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 492 | SkASSERT(instanceIndices[0].fCubics == fBaseInstances[1].fCubics); |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 493 | SkASSERT(instanceIndices[1].fCubics == quadEndIdx); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 494 | |
| 495 | fMeshesScratchBuffer.reserve(fMaxMeshesPerDraw); |
| 496 | fDynamicStatesScratchBuffer.reserve(fMaxMeshesPerDraw); |
| 497 | |
| 498 | return true; |
| 499 | } |
| 500 | |
| 501 | void GrCCPathParser::drawCoverageCount(GrOpFlushState* flushState, CoverageCountBatchID batchID, |
| 502 | const SkIRect& drawBounds) const { |
Chris Dalton | 8dfc70f | 2018-03-26 19:15:22 -0600 | [diff] [blame] | 503 | using PrimitiveType = GrCCCoverageProcessor::PrimitiveType; |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 504 | |
| 505 | SkASSERT(fInstanceBuffer); |
| 506 | |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 507 | const PrimitiveTallies& batchTotalCounts = fCoverageCountBatches[batchID].fTotalPrimitiveCounts; |
| 508 | |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 509 | GrPipeline pipeline(flushState->drawOpArgs().fProxy, GrPipeline::ScissorState::kEnabled, |
| 510 | SkBlendMode::kPlus); |
| 511 | |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 512 | if (batchTotalCounts.fTriangles) { |
Chris Dalton | 8dfc70f | 2018-03-26 19:15:22 -0600 | [diff] [blame] | 513 | this->drawPrimitives(flushState, pipeline, batchID, PrimitiveType::kTriangles, |
Chris Dalton | 703b476 | 2018-04-06 16:11:48 -0600 | [diff] [blame^] | 514 | &PrimitiveTallies::fTriangles, drawBounds); |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 515 | } |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 516 | |
Chris Dalton | 703b476 | 2018-04-06 16:11:48 -0600 | [diff] [blame^] | 517 | if (batchTotalCounts.fWeightedTriangles) { |
| 518 | this->drawPrimitives(flushState, pipeline, batchID, PrimitiveType::kWeightedTriangles, |
| 519 | &PrimitiveTallies::fWeightedTriangles, drawBounds); |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 520 | } |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 521 | |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 522 | if (batchTotalCounts.fQuadratics) { |
Chris Dalton | 8dfc70f | 2018-03-26 19:15:22 -0600 | [diff] [blame] | 523 | this->drawPrimitives(flushState, pipeline, batchID, PrimitiveType::kQuadratics, |
Chris Dalton | 703b476 | 2018-04-06 16:11:48 -0600 | [diff] [blame^] | 524 | &PrimitiveTallies::fQuadratics, drawBounds); |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | if (batchTotalCounts.fCubics) { |
Chris Dalton | 8dfc70f | 2018-03-26 19:15:22 -0600 | [diff] [blame] | 528 | this->drawPrimitives(flushState, pipeline, batchID, PrimitiveType::kCubics, |
Chris Dalton | 703b476 | 2018-04-06 16:11:48 -0600 | [diff] [blame^] | 529 | &PrimitiveTallies::fCubics, drawBounds); |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 530 | } |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 531 | } |
| 532 | |
Chris Dalton | 8dfc70f | 2018-03-26 19:15:22 -0600 | [diff] [blame] | 533 | void GrCCPathParser::drawPrimitives(GrOpFlushState* flushState, const GrPipeline& pipeline, |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 534 | CoverageCountBatchID batchID, |
Chris Dalton | 8dfc70f | 2018-03-26 19:15:22 -0600 | [diff] [blame] | 535 | GrCCCoverageProcessor::PrimitiveType primitiveType, |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 536 | int PrimitiveTallies::*instanceType, |
| 537 | const SkIRect& drawBounds) const { |
| 538 | SkASSERT(pipeline.getScissorState().enabled()); |
| 539 | |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 540 | // Don't call reset(), as that also resets the reserve count. |
| 541 | fMeshesScratchBuffer.pop_back_n(fMeshesScratchBuffer.count()); |
| 542 | fDynamicStatesScratchBuffer.pop_back_n(fDynamicStatesScratchBuffer.count()); |
| 543 | |
Chris Dalton | 703b476 | 2018-04-06 16:11:48 -0600 | [diff] [blame^] | 544 | GrCCCoverageProcessor proc(flushState->resourceProvider(), primitiveType); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 545 | |
| 546 | SkASSERT(batchID > 0); |
| 547 | SkASSERT(batchID < fCoverageCountBatches.count()); |
| 548 | const CoverageCountBatch& previousBatch = fCoverageCountBatches[batchID - 1]; |
| 549 | const CoverageCountBatch& batch = fCoverageCountBatches[batchID]; |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 550 | SkDEBUGCODE(int totalInstanceCount = 0); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 551 | |
| 552 | if (int instanceCount = batch.fEndNonScissorIndices.*instanceType - |
| 553 | previousBatch.fEndNonScissorIndices.*instanceType) { |
| 554 | SkASSERT(instanceCount > 0); |
| 555 | int baseInstance = fBaseInstances[(int)ScissorMode::kNonScissored].*instanceType + |
| 556 | previousBatch.fEndNonScissorIndices.*instanceType; |
| 557 | proc.appendMesh(fInstanceBuffer.get(), instanceCount, baseInstance, &fMeshesScratchBuffer); |
| 558 | fDynamicStatesScratchBuffer.push_back().fScissorRect.setXYWH(0, 0, drawBounds.width(), |
| 559 | drawBounds.height()); |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 560 | SkDEBUGCODE(totalInstanceCount += instanceCount); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | SkASSERT(previousBatch.fEndScissorSubBatchIdx > 0); |
| 564 | SkASSERT(batch.fEndScissorSubBatchIdx <= fScissorSubBatches.count()); |
| 565 | int baseScissorInstance = fBaseInstances[(int)ScissorMode::kScissored].*instanceType; |
| 566 | for (int i = previousBatch.fEndScissorSubBatchIdx; i < batch.fEndScissorSubBatchIdx; ++i) { |
| 567 | const ScissorSubBatch& previousSubBatch = fScissorSubBatches[i - 1]; |
| 568 | const ScissorSubBatch& scissorSubBatch = fScissorSubBatches[i]; |
| 569 | int startIndex = previousSubBatch.fEndPrimitiveIndices.*instanceType; |
| 570 | int instanceCount = scissorSubBatch.fEndPrimitiveIndices.*instanceType - startIndex; |
| 571 | if (!instanceCount) { |
| 572 | continue; |
| 573 | } |
| 574 | SkASSERT(instanceCount > 0); |
| 575 | proc.appendMesh(fInstanceBuffer.get(), instanceCount, |
| 576 | baseScissorInstance + startIndex, &fMeshesScratchBuffer); |
| 577 | fDynamicStatesScratchBuffer.push_back().fScissorRect = scissorSubBatch.fScissor; |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 578 | SkDEBUGCODE(totalInstanceCount += instanceCount); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | SkASSERT(fMeshesScratchBuffer.count() == fDynamicStatesScratchBuffer.count()); |
| 582 | SkASSERT(fMeshesScratchBuffer.count() <= fMaxMeshesPerDraw); |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 583 | SkASSERT(totalInstanceCount == batch.fTotalPrimitiveCounts.*instanceType); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 584 | |
| 585 | if (!fMeshesScratchBuffer.empty()) { |
Chris Dalton | 8dfc70f | 2018-03-26 19:15:22 -0600 | [diff] [blame] | 586 | proc.draw(flushState, pipeline, fMeshesScratchBuffer.begin(), |
| 587 | fDynamicStatesScratchBuffer.begin(), fMeshesScratchBuffer.count(), |
| 588 | SkRect::Make(drawBounds)); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 589 | } |
| 590 | } |