blob: f77c52e6f1628ff43f1b86c2ebacacdf7975fdf2 [file] [log] [blame]
Chris Dalton9ca27842018-01-18 12:24:50 -07001/*
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 Dalton84403d72018-02-13 21:46:17 -050019#include <stdlib.h>
Chris Dalton9ca27842018-01-18 12:24:50 -070020
Chris Dalton84403d72018-02-13 21:46:17 -050021using TriPointInstance = GrCCCoverageProcessor::TriPointInstance;
22using QuadPointInstance = GrCCCoverageProcessor::QuadPointInstance;
Chris Dalton9ca27842018-01-18 12:24:50 -070023
24GrCCPathParser::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 Dalton84403d72018-02-13 21:46:17 -050036 fCoverageCountBatches.push_back() = {PrimitiveTallies(), fScissorSubBatches.count(),
37 PrimitiveTallies()};
Chris Dalton9ca27842018-01-18 12:24:50 -070038}
39
40void 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
97void GrCCPathParser::parseDeviceSpacePath(const SkPath& deviceSpacePath) {
98 this->parsePath(deviceSpacePath, SkPathPriv::PointData(deviceSpacePath));
99}
100
101void 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 Dalton7ca3b7b2018-04-10 00:21:19 -0600137 fGeometry.quadraticTo(&deviceSpacePts[ptsIdx - 1]);
Chris Dalton9ca27842018-01-18 12:24:50 -0700138 ptsIdx += 2;
139 continue;
140 case SkPath::kCubic_Verb:
Chris Dalton7ca3b7b2018-04-10 00:21:19 -0600141 fGeometry.cubicTo(&deviceSpacePts[ptsIdx - 1]);
Chris Dalton9ca27842018-01-18 12:24:50 -0700142 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
154void GrCCPathParser::endContourIfNeeded(bool insideContour) {
155 if (insideContour) {
156 fCurrPathPrimitiveCounts += fGeometry.endContour();
157 }
158}
159
160void GrCCPathParser::saveParsedPath(ScissorMode scissorMode, const SkIRect& clippedDevIBounds,
161 int16_t atlasOffsetX, int16_t atlasOffsetY) {
162 SkASSERT(fParsingPath);
163
Chris Dalton84403d72018-02-13 21:46:17 -0500164 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 Dalton703b4762018-04-06 16:11:48 -0600172 fCurrPathPrimitiveCounts.fWeightedTriangles = 0;
Chris Dalton84403d72018-02-13 21:46:17 -0500173
174 const SkTArray<GrCCGeometry::Verb, true>& verbs = fGeometry.verbs();
175 const SkTArray<SkPoint, true>& pts = fGeometry.points();
176 int ptsIdx = fCurrPathPointsIdx;
177
Chris Dalton45e46602018-02-15 12:27:29 -0700178 // 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 Dalton84403d72018-02-13 21:46:17 -0500181 SkPath fan;
Chris Dalton45e46602018-02-15 12:27:29 -0700182 fan.setFillType(SkPath::kWinding_FillType);
Chris Dalton84403d72018-02-13 21:46:17 -0500183 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 Dalton45e46602018-02-15 12:27:29 -0700219 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 Dalton84403d72018-02-13 21:46:17 -0500236 ++fCurrPathPrimitiveCounts.fTriangles;
237 } else {
Chris Dalton703b4762018-04-06 16:11:48 -0600238 ++fCurrPathPrimitiveCounts.fWeightedTriangles;
Chris Dalton84403d72018-02-13 21:46:17 -0500239 }
240 }
241
Chris Daltonad065442018-03-08 22:41:33 -0700242 fPathsInfo.back().adoptFanTessellation(vertices, count);
Chris Dalton84403d72018-02-13 21:46:17 -0500243 }
244
Chris Dalton9ca27842018-01-18 12:24:50 -0700245 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
255void GrCCPathParser::discardParsedPath() {
256 SkASSERT(fParsingPath);
257 fGeometry.resize_back(fCurrPathPointsIdx, fCurrPathVerbsIdx);
258 SkDEBUGCODE(fParsingPath = false);
259}
260
261GrCCPathParser::CoverageCountBatchID GrCCPathParser::closeCurrentBatch() {
262 SkASSERT(!fInstanceBuffer);
263 SkASSERT(!fCoverageCountBatches.empty());
264
Chris Daltona883aca2018-03-08 23:05:30 -0700265 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 Dalton84403d72018-02-13 21:46:17 -0500270 PrimitiveTallies batchTotalCounts = fTotalPrimitiveCounts[(int)ScissorMode::kNonScissored] -
271 lastBatch.fEndNonScissorIndices;
272 batchTotalCounts += fTotalPrimitiveCounts[(int)ScissorMode::kScissored] -
273 lastScissorSubBatch.fEndPrimitiveIndices;
Chris Dalton9ca27842018-01-18 12:24:50 -0700274
Chris Daltona883aca2018-03-08 23:05:30 -0700275 // This will invalidate lastBatch.
Chris Dalton9ca27842018-01-18 12:24:50 -0700276 fCoverageCountBatches.push_back() = {
277 fTotalPrimitiveCounts[(int)ScissorMode::kNonScissored],
Chris Dalton84403d72018-02-13 21:46:17 -0500278 fScissorSubBatches.count(),
279 batchTotalCounts
Chris Dalton9ca27842018-01-18 12:24:50 -0700280 };
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 Dalton84403d72018-02-13 21:46:17 -0500297static TriPointInstance* emit_recursive_fan(const SkTArray<SkPoint, true>& pts,
Chris Dalton9ca27842018-01-18 12:24:50 -0700298 SkTArray<int32_t, true>& indices, int firstIndex,
299 int indexCount, const Sk2f& atlasOffset,
Chris Dalton84403d72018-02-13 21:46:17 -0500300 TriPointInstance out[]) {
Chris Dalton9ca27842018-01-18 12:24:50 -0700301 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 Dalton84403d72018-02-13 21:46:17 -0500324static 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 Dalton703b4762018-04-06 16:11:48 -0600333 quadPointInstanceData[indices->fWeightedTriangles++].setW(
Chris Dalton84403d72018-02-13 21:46:17 -0500334 vertices[i].fPos, vertices[i+1].fPos, vertices[i + 2].fPos, atlasOffset,
Chris Dalton45e46602018-02-15 12:27:29 -0700335 // Tessellator has opposite winding sense.
336 -static_cast<float>(vertices[i].fWinding));
Chris Dalton84403d72018-02-13 21:46:17 -0500337 }
338 }
339}
340
Chris Dalton9ca27842018-01-18 12:24:50 -0700341bool 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 Dalton84403d72018-02-13 21:46:17 -0500359 // an array of TriPointInstance[], so we can begin at zero and lay them out one after the other.
Chris Dalton9ca27842018-01-18 12:24:50 -0700360 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 Dalton84403d72018-02-13 21:46:17 -0500369 // 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 Dalton703b4762018-04-06 16:11:48 -0600374 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 Dalton9ca27842018-01-18 12:24:50 -0700379 fBaseInstances[1].fCubics = fBaseInstances[0].fCubics + fTotalPrimitiveCounts[0].fCubics;
Chris Dalton84403d72018-02-13 21:46:17 -0500380 int quadEndIdx = fBaseInstances[1].fCubics + fTotalPrimitiveCounts[1].fCubics;
Chris Dalton9ca27842018-01-18 12:24:50 -0700381
382 fInstanceBuffer = onFlushRP->makeBuffer(kVertex_GrBufferType,
Chris Dalton84403d72018-02-13 21:46:17 -0500383 quadEndIdx * sizeof(QuadPointInstance));
Chris Dalton9ca27842018-01-18 12:24:50 -0700384 if (!fInstanceBuffer) {
385 return false;
386 }
387
Chris Dalton84403d72018-02-13 21:46:17 -0500388 TriPointInstance* triPointInstanceData = static_cast<TriPointInstance*>(fInstanceBuffer->map());
389 QuadPointInstance* quadPointInstanceData =
390 reinterpret_cast<QuadPointInstance*>(triPointInstanceData);
391 SkASSERT(quadPointInstanceData);
Chris Dalton9ca27842018-01-18 12:24:50 -0700392
Chris Dalton84403d72018-02-13 21:46:17 -0500393 PathInfo* nextPathInfo = fPathsInfo.begin();
Chris Dalton9ca27842018-01-18 12:24:50 -0700394 float atlasOffsetX = 0.0, atlasOffsetY = 0.0;
395 Sk2f atlasOffset;
Chris Dalton9ca27842018-01-18 12:24:50 -0700396 PrimitiveTallies instanceIndices[2] = {fBaseInstances[0], fBaseInstances[1]};
397 PrimitiveTallies* currIndices = nullptr;
398 SkSTArray<256, int32_t, true> currFan;
Chris Dalton84403d72018-02-13 21:46:17 -0500399 bool currFanIsTessellated = false;
Chris Dalton9ca27842018-01-18 12:24:50 -0700400
401 const SkTArray<SkPoint, true>& pts = fGeometry.points();
Chris Dalton84403d72018-02-13 21:46:17 -0500402 int ptsIdx = -1;
Chris Dalton9ca27842018-01-18 12:24:50 -0700403
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 Daltonad065442018-03-08 22:41:33 -0700409 currIndices = &instanceIndices[(int)nextPathInfo->scissorMode()];
410 atlasOffsetX = static_cast<float>(nextPathInfo->atlasOffsetX());
411 atlasOffsetY = static_cast<float>(nextPathInfo->atlasOffsetY());
Chris Dalton9ca27842018-01-18 12:24:50 -0700412 atlasOffset = {atlasOffsetX, atlasOffsetY};
Chris Daltonad065442018-03-08 22:41:33 -0700413 currFanIsTessellated = nextPathInfo->hasFanTessellation();
Chris Dalton84403d72018-02-13 21:46:17 -0500414 if (currFanIsTessellated) {
Chris Daltonad065442018-03-08 22:41:33 -0700415 emit_tessellated_fan(nextPathInfo->fanTessellation(),
416 nextPathInfo->fanTessellationCount(), atlasOffset,
Chris Dalton84403d72018-02-13 21:46:17 -0500417 triPointInstanceData, quadPointInstanceData, currIndices);
418 }
419 ++nextPathInfo;
Chris Dalton9ca27842018-01-18 12:24:50 -0700420 continue;
421
422 case GrCCGeometry::Verb::kBeginContour:
423 SkASSERT(currFan.empty());
Chris Dalton84403d72018-02-13 21:46:17 -0500424 ++ptsIdx;
425 if (!currFanIsTessellated) {
426 currFan.push_back(ptsIdx);
427 }
Chris Dalton9ca27842018-01-18 12:24:50 -0700428 continue;
429
430 case GrCCGeometry::Verb::kLineTo:
Chris Dalton84403d72018-02-13 21:46:17 -0500431 ++ptsIdx;
432 if (!currFanIsTessellated) {
433 SkASSERT(!currFan.empty());
434 currFan.push_back(ptsIdx);
435 }
Chris Dalton9ca27842018-01-18 12:24:50 -0700436 continue;
437
438 case GrCCGeometry::Verb::kMonotonicQuadraticTo:
Chris Dalton84403d72018-02-13 21:46:17 -0500439 triPointInstanceData[currIndices->fQuadratics++].set(&pts[ptsIdx], atlasOffset);
440 ptsIdx += 2;
441 if (!currFanIsTessellated) {
442 SkASSERT(!currFan.empty());
443 currFan.push_back(ptsIdx);
444 }
Chris Dalton9ca27842018-01-18 12:24:50 -0700445 continue;
446
447 case GrCCGeometry::Verb::kMonotonicCubicTo:
Chris Dalton84403d72018-02-13 21:46:17 -0500448 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 Dalton9ca27842018-01-18 12:24:50 -0700455 continue;
456
457 case GrCCGeometry::Verb::kEndClosedContour: // endPt == startPt.
Chris Dalton84403d72018-02-13 21:46:17 -0500458 if (!currFanIsTessellated) {
459 SkASSERT(!currFan.empty());
460 currFan.pop_back();
461 }
Chris Dalton9ca27842018-01-18 12:24:50 -0700462 // fallthru.
463 case GrCCGeometry::Verb::kEndOpenContour: // endPt != startPt.
Chris Dalton84403d72018-02-13 21:46:17 -0500464 SkASSERT(!currFanIsTessellated || currFan.empty());
465 if (!currFanIsTessellated && currFan.count() >= 3) {
Chris Dalton9ca27842018-01-18 12:24:50 -0700466 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 Dalton84403d72018-02-13 21:46:17 -0500470 SkDEBUGCODE(TriPointInstance* end =)
Chris Dalton9ca27842018-01-18 12:24:50 -0700471 emit_recursive_fan(pts, currFan, 0, fanSize, atlasOffset,
Chris Dalton84403d72018-02-13 21:46:17 -0500472 triPointInstanceData + currIndices->fTriangles);
Chris Dalton9ca27842018-01-18 12:24:50 -0700473 currIndices->fTriangles += fanSize - 2;
Chris Dalton84403d72018-02-13 21:46:17 -0500474 SkASSERT(triPointInstanceData + currIndices->fTriangles == end);
Chris Dalton9ca27842018-01-18 12:24:50 -0700475 }
476 currFan.reset();
477 continue;
478 }
479 }
480
481 fInstanceBuffer->unmap();
482
Chris Dalton84403d72018-02-13 21:46:17 -0500483 SkASSERT(nextPathInfo == fPathsInfo.end());
Chris Dalton9ca27842018-01-18 12:24:50 -0700484 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 Dalton703b4762018-04-06 16:11:48 -0600489 SkASSERT(instanceIndices[0].fWeightedTriangles == fBaseInstances[1].fWeightedTriangles);
490 SkASSERT(instanceIndices[1].fWeightedTriangles == fBaseInstances[0].fCubics);
Chris Dalton9ca27842018-01-18 12:24:50 -0700491 SkASSERT(instanceIndices[0].fCubics == fBaseInstances[1].fCubics);
Chris Dalton84403d72018-02-13 21:46:17 -0500492 SkASSERT(instanceIndices[1].fCubics == quadEndIdx);
Chris Dalton9ca27842018-01-18 12:24:50 -0700493
494 fMeshesScratchBuffer.reserve(fMaxMeshesPerDraw);
495 fDynamicStatesScratchBuffer.reserve(fMaxMeshesPerDraw);
496
497 return true;
498}
499
500void GrCCPathParser::drawCoverageCount(GrOpFlushState* flushState, CoverageCountBatchID batchID,
501 const SkIRect& drawBounds) const {
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600502 using PrimitiveType = GrCCCoverageProcessor::PrimitiveType;
Chris Dalton9ca27842018-01-18 12:24:50 -0700503
504 SkASSERT(fInstanceBuffer);
505
Chris Dalton84403d72018-02-13 21:46:17 -0500506 const PrimitiveTallies& batchTotalCounts = fCoverageCountBatches[batchID].fTotalPrimitiveCounts;
507
Chris Dalton9ca27842018-01-18 12:24:50 -0700508 GrPipeline pipeline(flushState->drawOpArgs().fProxy, GrPipeline::ScissorState::kEnabled,
509 SkBlendMode::kPlus);
510
Chris Dalton84403d72018-02-13 21:46:17 -0500511 if (batchTotalCounts.fTriangles) {
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600512 this->drawPrimitives(flushState, pipeline, batchID, PrimitiveType::kTriangles,
Chris Dalton703b4762018-04-06 16:11:48 -0600513 &PrimitiveTallies::fTriangles, drawBounds);
Chris Dalton84403d72018-02-13 21:46:17 -0500514 }
Chris Dalton9ca27842018-01-18 12:24:50 -0700515
Chris Dalton703b4762018-04-06 16:11:48 -0600516 if (batchTotalCounts.fWeightedTriangles) {
517 this->drawPrimitives(flushState, pipeline, batchID, PrimitiveType::kWeightedTriangles,
518 &PrimitiveTallies::fWeightedTriangles, drawBounds);
Chris Dalton84403d72018-02-13 21:46:17 -0500519 }
Chris Dalton9ca27842018-01-18 12:24:50 -0700520
Chris Dalton84403d72018-02-13 21:46:17 -0500521 if (batchTotalCounts.fQuadratics) {
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600522 this->drawPrimitives(flushState, pipeline, batchID, PrimitiveType::kQuadratics,
Chris Dalton703b4762018-04-06 16:11:48 -0600523 &PrimitiveTallies::fQuadratics, drawBounds);
Chris Dalton84403d72018-02-13 21:46:17 -0500524 }
525
526 if (batchTotalCounts.fCubics) {
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600527 this->drawPrimitives(flushState, pipeline, batchID, PrimitiveType::kCubics,
Chris Dalton703b4762018-04-06 16:11:48 -0600528 &PrimitiveTallies::fCubics, drawBounds);
Chris Dalton84403d72018-02-13 21:46:17 -0500529 }
Chris Dalton9ca27842018-01-18 12:24:50 -0700530}
531
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600532void GrCCPathParser::drawPrimitives(GrOpFlushState* flushState, const GrPipeline& pipeline,
Chris Dalton9ca27842018-01-18 12:24:50 -0700533 CoverageCountBatchID batchID,
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600534 GrCCCoverageProcessor::PrimitiveType primitiveType,
Chris Dalton9ca27842018-01-18 12:24:50 -0700535 int PrimitiveTallies::*instanceType,
536 const SkIRect& drawBounds) const {
537 SkASSERT(pipeline.getScissorState().enabled());
538
Chris Dalton9ca27842018-01-18 12:24:50 -0700539 // 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 Dalton703b4762018-04-06 16:11:48 -0600543 GrCCCoverageProcessor proc(flushState->resourceProvider(), primitiveType);
Chris Dalton9ca27842018-01-18 12:24:50 -0700544
545 SkASSERT(batchID > 0);
546 SkASSERT(batchID < fCoverageCountBatches.count());
547 const CoverageCountBatch& previousBatch = fCoverageCountBatches[batchID - 1];
548 const CoverageCountBatch& batch = fCoverageCountBatches[batchID];
Chris Dalton84403d72018-02-13 21:46:17 -0500549 SkDEBUGCODE(int totalInstanceCount = 0);
Chris Dalton9ca27842018-01-18 12:24:50 -0700550
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 Dalton84403d72018-02-13 21:46:17 -0500559 SkDEBUGCODE(totalInstanceCount += instanceCount);
Chris Dalton9ca27842018-01-18 12:24:50 -0700560 }
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 Dalton84403d72018-02-13 21:46:17 -0500577 SkDEBUGCODE(totalInstanceCount += instanceCount);
Chris Dalton9ca27842018-01-18 12:24:50 -0700578 }
579
580 SkASSERT(fMeshesScratchBuffer.count() == fDynamicStatesScratchBuffer.count());
581 SkASSERT(fMeshesScratchBuffer.count() <= fMaxMeshesPerDraw);
Chris Dalton84403d72018-02-13 21:46:17 -0500582 SkASSERT(totalInstanceCount == batch.fTotalPrimitiveCounts.*instanceType);
Chris Dalton9ca27842018-01-18 12:24:50 -0700583
584 if (!fMeshesScratchBuffer.empty()) {
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600585 proc.draw(flushState, pipeline, fMeshesScratchBuffer.begin(),
586 fDynamicStatesScratchBuffer.begin(), fMeshesScratchBuffer.count(),
587 SkRect::Make(drawBounds));
Chris Dalton9ca27842018-01-18 12:24:50 -0700588 }
589}