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