blob: cb1bf375849ea9dd5a8459a2656c53ec079052d4 [file] [log] [blame]
Chris Daltonb832ce62020-01-06 19:49:37 -07001/*
2 * Copyright 2019 Google LLC.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "src/gpu/tessellate/GrTessellatePathOp.h"
9
Chris Daltond081dce2020-01-23 12:09:04 -070010#include "src/gpu/GrEagerVertexAllocator.h"
Chris Daltonb832ce62020-01-06 19:49:37 -070011#include "src/gpu/GrGpu.h"
12#include "src/gpu/GrOpFlushState.h"
Chris Dalton17dc4182020-03-25 16:18:16 -060013#include "src/gpu/GrTriangulator.h"
Chris Dalton4328e922020-01-29 13:16:14 -070014#include "src/gpu/tessellate/GrFillPathShader.h"
Chris Daltonf5132a02020-04-27 23:40:03 -060015#include "src/gpu/tessellate/GrMiddleOutPolygonTriangulator.h"
Chris Dalton42915c22020-04-22 16:24:43 -060016#include "src/gpu/tessellate/GrMidpointContourParser.h"
Chris Daltonb5391d92020-05-24 14:55:54 -060017#include "src/gpu/tessellate/GrResolveLevelCounter.h"
Chris Daltonf9aea7f2020-01-21 11:19:26 -070018#include "src/gpu/tessellate/GrStencilPathShader.h"
Chris Daltonb832ce62020-01-06 19:49:37 -070019
Chris Daltonb5391d92020-05-24 14:55:54 -060020constexpr static int kMaxResolveLevel = GrMiddleOutCubicShader::kMaxResolveLevel;
21constexpr static float kTessellationIntolerance = 4; // 1/4 of a pixel.
22
Chris Daltonb832ce62020-01-06 19:49:37 -070023GrTessellatePathOp::FixedFunctionFlags GrTessellatePathOp::fixedFunctionFlags() const {
24 auto flags = FixedFunctionFlags::kUsesStencil;
25 if (GrAAType::kNone != fAAType) {
26 flags |= FixedFunctionFlags::kUsesHWAA;
27 }
28 return flags;
29}
30
Robert Phillipsc655c3a2020-03-18 13:23:45 -040031void GrTessellatePathOp::onPrePrepare(GrRecordingContext*,
Brian Salomon8afde5f2020-04-01 16:22:00 -040032 const GrSurfaceProxyView* writeView,
Robert Phillipsc655c3a2020-03-18 13:23:45 -040033 GrAppliedClip*,
34 const GrXferProcessor::DstProxyView&) {
35}
36
Chris Daltonb5391d92020-05-24 14:55:54 -060037void GrTessellatePathOp::onPrepare(GrOpFlushState* flushState) {
Chris Dalton4328e922020-01-29 13:16:14 -070038 int numVerbs = fPath.countVerbs();
39 if (numVerbs <= 0) {
40 return;
41 }
Chris Daltonb5391d92020-05-24 14:55:54 -060042
43 // First check if the path is large and/or simple enough that we can actually triangulate the
44 // inner polygon(s) on the CPU. This is our fastest approach. It allows us to stencil only the
45 // curves, and then fill the internal polygons directly to the final render target, thus drawing
46 // the majority of pixels in a single render pass.
47 SkScalar scales[2];
48 SkAssertResult(fViewMatrix.getMinMaxScales(scales)); // Will fail if perspective.
49 const SkRect& bounds = fPath.getBounds();
Chris Dalton4328e922020-01-29 13:16:14 -070050 float gpuFragmentWork = bounds.height() * scales[0] * bounds.width() * scales[1];
51 float cpuTessellationWork = (float)numVerbs * SkNextLog2(numVerbs); // N log N.
52 if (cpuTessellationWork * 500 + (256 * 256) < gpuFragmentWork) { // Don't try below 256x256.
Chris Daltonb5391d92020-05-24 14:55:54 -060053 int numCountedCubics;
Chris Dalton04f9cda2020-04-23 10:04:25 -060054 // This will fail if the inner triangles do not form a simple polygon (e.g., self
55 // intersection, double winding).
Chris Daltonb5391d92020-05-24 14:55:54 -060056 if (this->prepareNonOverlappingInnerTriangles(flushState, &numCountedCubics)) {
57 if (!numCountedCubics) {
58 return;
59 }
60 // Always use indirect draws for cubics instead of tessellation here. Our goal in this
61 // mode is to maximize GPU performance, and the middle-out topology used by our indirect
62 // draws is easier on the rasterizer than a tessellated fan. There also seems to be a
63 // small amount of fixed tessellation overhead that this avoids.
64 //
65 // NOTE: This will count fewer cubics than above if it discards any for resolveLevel=0.
66 GrResolveLevelCounter resolveLevelCounter;
67 numCountedCubics = resolveLevelCounter.reset(fPath, fViewMatrix,
68 kTessellationIntolerance);
69 this->prepareIndirectOuterCubics(flushState, resolveLevelCounter);
Chris Dalton4328e922020-01-29 13:16:14 -070070 return;
71 }
72 }
73
Chris Daltonb5391d92020-05-24 14:55:54 -060074 // When there are only a few verbs, it seems to always be fastest to make a single indirect draw
75 // that contains both the inner triangles and the outer cubics, instead of using hardware
76 // tessellation. Also take this path if tessellation is not supported.
77 bool drawTrianglesAsIndirectCubicDraw = (numVerbs < 50);
78 if (drawTrianglesAsIndirectCubicDraw ||
79 !flushState->caps().shaderCaps()->tessellationSupport()) {
80 // Prepare outer cubics with indirect draws.
81 GrResolveLevelCounter resolveLevelCounter;
82 this->prepareMiddleOutTrianglesAndCubics(flushState, &resolveLevelCounter,
83 drawTrianglesAsIndirectCubicDraw);
84 return;
85 }
86
87 // Next see if we can split up the inner triangles and outer cubics into two draw calls. This
88 // allows for a more efficient inner triangle topology that can reduce the rasterizer load by a
89 // large margin on complex paths, but also causes greater CPU overhead due to the extra shader
90 // switches and draw calls.
Chris Dalton4328e922020-01-29 13:16:14 -070091 // NOTE: Raster-edge work is 1-dimensional, so we sum height and width instead of multiplying.
92 float rasterEdgeWork = (bounds.height() + bounds.width()) * scales[1] * fPath.countVerbs();
Chris Daltonb5391d92020-05-24 14:55:54 -060093 if (rasterEdgeWork > 300 * 300) {
94 this->prepareMiddleOutTrianglesAndCubics(flushState);
Chris Daltonf9aea7f2020-01-21 11:19:26 -070095 return;
96 }
97
98 // Fastest CPU approach: emit one cubic wedge per verb, fanning out from the center.
Chris Daltonb5391d92020-05-24 14:55:54 -060099 this->prepareTessellatedCubicWedges(flushState);
Chris Daltonb832ce62020-01-06 19:49:37 -0700100}
101
Chris Dalton2f2d81c2020-05-13 17:57:37 -0600102bool GrTessellatePathOp::prepareNonOverlappingInnerTriangles(GrMeshDrawOp::Target* target,
Chris Daltonf5132a02020-04-27 23:40:03 -0600103 int* numCountedCurves) {
Chris Dalton04f9cda2020-04-23 10:04:25 -0600104 SkASSERT(!fTriangleBuffer);
105 SkASSERT(!fDoStencilTriangleBuffer);
106 SkASSERT(!fDoFillTriangleBuffer);
107
108 using GrTriangulator::Mode;
109
Chris Dalton2f2d81c2020-05-13 17:57:37 -0600110 GrEagerDynamicVertexAllocator vertexAlloc(target, &fTriangleBuffer, &fBaseTriangleVertex);
Chris Dalton04f9cda2020-04-23 10:04:25 -0600111 fTriangleVertexCount = GrTriangulator::PathToTriangles(fPath, 0, SkRect::MakeEmpty(),
112 &vertexAlloc, Mode::kSimpleInnerPolygons,
113 numCountedCurves);
114 if (fTriangleVertexCount == 0) {
115 // Mode::kSimpleInnerPolygons causes PathToTriangles to fail if the inner polygon(s) are not
116 // simple.
117 return false;
118 }
119 if (((Flags::kStencilOnly | Flags::kWireframe) & fFlags) || GrAAType::kCoverage == fAAType ||
Chris Dalton2f2d81c2020-05-13 17:57:37 -0600120 (target->appliedClip() && target->appliedClip()->hasStencilClip())) {
Chris Dalton04f9cda2020-04-23 10:04:25 -0600121 // If we have certain flags, mixed samples, or a stencil clip then we unfortunately
122 // can't fill the inner polygon directly. Indicate that these triangles need to be
123 // stencilled.
124 fDoStencilTriangleBuffer = true;
125 }
126 if (!(Flags::kStencilOnly & fFlags)) {
127 fDoFillTriangleBuffer = true;
128 }
129 return true;
130}
131
Chris Daltonb5391d92020-05-24 14:55:54 -0600132void GrTessellatePathOp::prepareMiddleOutTrianglesAndCubics(
133 GrMeshDrawOp::Target* target, GrResolveLevelCounter* resolveLevelCounter,
134 bool drawTrianglesAsIndirectCubicDraw) {
Chris Dalton04f9cda2020-04-23 10:04:25 -0600135 SkASSERT(!fTriangleBuffer);
136 SkASSERT(!fDoStencilTriangleBuffer);
137 SkASSERT(!fDoFillTriangleBuffer);
Chris Daltonb5391d92020-05-24 14:55:54 -0600138 SkASSERT(!fCubicBuffer);
139 SkASSERT(!fStencilCubicsShader);
140 SkASSERT(!fIndirectDrawBuffer);
Chris Dalton04f9cda2020-04-23 10:04:25 -0600141
Chris Daltonf5132a02020-04-27 23:40:03 -0600142 // No initial moveTo, plus an implicit close at the end; n-2 triangles fill an n-gon.
Chris Daltonb5391d92020-05-24 14:55:54 -0600143 int maxInnerTriangles = fPath.countVerbs() - 1;
144 int maxCubics = fPath.countVerbs();
Chris Dalton42915c22020-04-22 16:24:43 -0600145
Chris Daltonb5391d92020-05-24 14:55:54 -0600146 SkPoint* vertexData;
147 int vertexAdvancePerTriangle;
148 if (drawTrianglesAsIndirectCubicDraw) {
149 // Allocate the triangles as 4-point instances at the beginning of the cubic buffer.
150 SkASSERT(resolveLevelCounter);
151 vertexAdvancePerTriangle = 4;
152 int baseTriangleInstance;
153 vertexData = static_cast<SkPoint*>(target->makeVertexSpace(
154 sizeof(SkPoint) * 4, maxInnerTriangles + maxCubics, &fCubicBuffer,
155 &baseTriangleInstance));
156 fBaseCubicVertex = baseTriangleInstance * 4;
157 } else {
158 // Allocate the triangles as normal 3-point instances in the triangle buffer.
159 vertexAdvancePerTriangle = 3;
160 vertexData = static_cast<SkPoint*>(target->makeVertexSpace(
161 sizeof(SkPoint), maxInnerTriangles * 3, &fTriangleBuffer, &fBaseTriangleVertex));
162 }
Chris Dalton42915c22020-04-22 16:24:43 -0600163 if (!vertexData) {
Chris Dalton04f9cda2020-04-23 10:04:25 -0600164 return;
Chris Dalton42915c22020-04-22 16:24:43 -0600165 }
Chris Dalton42915c22020-04-22 16:24:43 -0600166
Chris Daltonb5391d92020-05-24 14:55:54 -0600167 GrVectorXform xform(fViewMatrix);
168 GrMiddleOutPolygonTriangulator middleOut(vertexData, vertexAdvancePerTriangle,
169 fPath.countVerbs());
170 if (resolveLevelCounter) {
171 resolveLevelCounter->reset();
172 }
173 int numCountedCurves = 0;
Chris Daltonf7a33072020-05-01 10:33:08 -0600174 for (auto [verb, pts, w] : SkPathPriv::Iterate(fPath)) {
175 switch (verb) {
176 case SkPathVerb::kMove:
Chris Daltonb5391d92020-05-24 14:55:54 -0600177 middleOut.closeAndMove(pts[0]);
Chris Daltonf5132a02020-04-27 23:40:03 -0600178 break;
Chris Daltonf7a33072020-05-01 10:33:08 -0600179 case SkPathVerb::kLine:
180 middleOut.pushVertex(pts[1]);
181 break;
182 case SkPathVerb::kQuad:
183 middleOut.pushVertex(pts[2]);
Chris Daltonb5391d92020-05-24 14:55:54 -0600184 if (resolveLevelCounter) {
185 // Quadratics get converted to cubics before rendering.
186 resolveLevelCounter->countCubic(GrWangsFormula::quadratic_log2(
187 kTessellationIntolerance, pts, xform));
188 break;
189 }
190 ++numCountedCurves;
Chris Daltonf7a33072020-05-01 10:33:08 -0600191 break;
192 case SkPathVerb::kCubic:
193 middleOut.pushVertex(pts[3]);
Chris Daltonb5391d92020-05-24 14:55:54 -0600194 if (resolveLevelCounter) {
195 resolveLevelCounter->countCubic(GrWangsFormula::cubic_log2(
196 kTessellationIntolerance, pts, xform));
197 break;
198 }
199 ++numCountedCurves;
Chris Daltonf7a33072020-05-01 10:33:08 -0600200 break;
201 case SkPathVerb::kClose:
Chris Daltonf5132a02020-04-27 23:40:03 -0600202 middleOut.close();
203 break;
Chris Daltonf7a33072020-05-01 10:33:08 -0600204 case SkPathVerb::kConic:
Chris Daltonf7a33072020-05-01 10:33:08 -0600205 SkUNREACHABLE;
Chris Daltonf5132a02020-04-27 23:40:03 -0600206 }
Chris Dalton42915c22020-04-22 16:24:43 -0600207 }
Chris Daltonb5391d92020-05-24 14:55:54 -0600208 int triangleCount = middleOut.close();
209 SkASSERT(triangleCount <= maxInnerTriangles);
Chris Dalton42915c22020-04-22 16:24:43 -0600210
Chris Daltonb5391d92020-05-24 14:55:54 -0600211 if (drawTrianglesAsIndirectCubicDraw) {
212 SkASSERT(resolveLevelCounter);
213 int totalInstanceCount = triangleCount + resolveLevelCounter->totalCubicInstanceCount();
214 SkASSERT(vertexAdvancePerTriangle == 4);
215 target->putBackVertices(maxInnerTriangles + maxCubics - totalInstanceCount,
216 sizeof(SkPoint) * 4);
217 if (totalInstanceCount) {
218 this->prepareIndirectOuterCubicsAndTriangles(target, *resolveLevelCounter, vertexData,
219 triangleCount);
220 }
221 } else {
222 SkASSERT(vertexAdvancePerTriangle == 3);
223 target->putBackVertices(maxInnerTriangles - triangleCount, sizeof(SkPoint) * 3);
224 fTriangleVertexCount = triangleCount * 3;
225 if (fTriangleVertexCount) {
226 fDoStencilTriangleBuffer = true;
227 }
228 if (resolveLevelCounter) {
229 this->prepareIndirectOuterCubics(target, *resolveLevelCounter);
230 } else {
231 this->prepareTessellatedOuterCubics(target, numCountedCurves);
232 }
Chris Dalton04f9cda2020-04-23 10:04:25 -0600233 }
Chris Dalton42915c22020-04-22 16:24:43 -0600234}
235
236static SkPoint lerp(const SkPoint& a, const SkPoint& b, float T) {
237 SkASSERT(1 != T); // The below does not guarantee lerp(a, b, 1) === b.
238 return (b - a) * T + a;
239}
240
Chris Daltonf7a33072020-05-01 10:33:08 -0600241static void line2cubic(const SkPoint& p0, const SkPoint& p1, SkPoint* out) {
242 out[0] = p0;
243 out[1] = lerp(p0, p1, 1/3.f);
244 out[2] = lerp(p0, p1, 2/3.f);
245 out[3] = p1;
Chris Dalton42915c22020-04-22 16:24:43 -0600246}
247
Chris Daltonf7a33072020-05-01 10:33:08 -0600248static void quad2cubic(const SkPoint pts[], SkPoint* out) {
249 out[0] = pts[0];
250 out[1] = lerp(pts[0], pts[1], 2/3.f);
251 out[2] = lerp(pts[1], pts[2], 1/3.f);
252 out[3] = pts[2];
Chris Dalton42915c22020-04-22 16:24:43 -0600253}
254
Chris Daltonb5391d92020-05-24 14:55:54 -0600255void GrTessellatePathOp::prepareIndirectOuterCubics(
256 GrMeshDrawOp::Target* target, const GrResolveLevelCounter& resolveLevelCounter) {
257 SkASSERT(resolveLevelCounter.totalCubicInstanceCount() >= 0);
258 if (resolveLevelCounter.totalCubicInstanceCount() == 0) {
259 return;
260 }
261 // Allocate a buffer to store the cubic data.
262 SkPoint* cubicData;
263 int baseInstance;
264 cubicData = static_cast<SkPoint*>(target->makeVertexSpace(
265 sizeof(SkPoint) * 4, resolveLevelCounter.totalCubicInstanceCount(), &fCubicBuffer,
266 &baseInstance));
267 if (!cubicData) {
268 return;
269 }
270 fBaseCubicVertex = baseInstance * 4;
271 this->prepareIndirectOuterCubicsAndTriangles(target, resolveLevelCounter, cubicData,
272 /*numTrianglesAtBeginningOfData=*/0);
273}
274
275void GrTessellatePathOp::prepareIndirectOuterCubicsAndTriangles(
276 GrMeshDrawOp::Target* target, const GrResolveLevelCounter& resolveLevelCounter,
277 SkPoint* cubicData, int numTrianglesAtBeginningOfData) {
278 SkASSERT(numTrianglesAtBeginningOfData + resolveLevelCounter.totalCubicInstanceCount() > 0);
279 SkASSERT(!fStencilCubicsShader);
280 SkASSERT(cubicData);
281
282 // Here we treat fCubicBuffer as an instance buffer. It should have been prepared with the base
283 // vertex on an instance boundary in order to accommodate this.
284 SkASSERT(fBaseCubicVertex % 4 == 0);
285 int baseInstance = fBaseCubicVertex >> 2;
286
287 // Start preparing the indirect draw buffer.
288 fIndirectDrawCount = resolveLevelCounter.totalCubicIndirectDrawCount();
289 if (numTrianglesAtBeginningOfData) {
290 ++fIndirectDrawCount; // Add an indirect draw for the triangles at the beginning.
291 }
292
293 // Allocate space for the GrDrawIndexedIndirectCommand structs.
294 GrDrawIndexedIndirectCommand* indirectData = target->makeDrawIndexedIndirectSpace(
295 fIndirectDrawCount, &fIndirectDrawBuffer, &fIndirectDrawOffset);
296 if (!indirectData) {
297 SkASSERT(!fIndirectDrawBuffer);
298 return;
299 }
300
301 // Fill out the GrDrawIndexedIndirectCommand structs and determine the starting instance data
302 // location at each resolve level.
303 SkPoint* instanceLocations[kMaxResolveLevel + 1];
304 int indirectIdx = 0;
305 int runningInstanceCount = 0;
306 if (numTrianglesAtBeginningOfData) {
307 // The caller has already packed "triangleInstanceCount" triangles into 4-point instances
308 // at the beginning of the instance buffer. Add a special-case indirect draw here that will
309 // emit the triangles [P0, P1, P2] from these 4-point instances.
310 indirectData[0] = GrMiddleOutCubicShader::MakeDrawTrianglesIndirectCmd(
311 numTrianglesAtBeginningOfData, baseInstance);
312 indirectIdx = 1;
313 runningInstanceCount = numTrianglesAtBeginningOfData;
314 }
315 for (int resolveLevel = 1; resolveLevel <= kMaxResolveLevel; ++resolveLevel) {
316 instanceLocations[resolveLevel] = cubicData + runningInstanceCount * 4;
317 if (int instanceCountAtCurrLevel = resolveLevelCounter[resolveLevel]) {
318 indirectData[indirectIdx++] = GrMiddleOutCubicShader::MakeDrawCubicsIndirectCmd(
319 resolveLevel, instanceCountAtCurrLevel, baseInstance + runningInstanceCount);
320 runningInstanceCount += instanceCountAtCurrLevel;
321 }
322 }
323
324#ifdef SK_DEBUG
325 SkASSERT(indirectIdx == fIndirectDrawCount);
326 SkASSERT(runningInstanceCount == numTrianglesAtBeginningOfData +
327 resolveLevelCounter.totalCubicInstanceCount());
328 SkASSERT(fIndirectDrawCount > 0);
329
330 SkPoint* endLocations[kMaxResolveLevel + 1];
331 memcpy(endLocations, instanceLocations + 1, kMaxResolveLevel * sizeof(SkPoint*));
332 int totalInstanceCount = numTrianglesAtBeginningOfData +
333 resolveLevelCounter.totalCubicInstanceCount();
334 endLocations[kMaxResolveLevel] = cubicData + totalInstanceCount * 4;
335#endif
336
337 fCubicVertexCount = numTrianglesAtBeginningOfData * 4;
338
339 if (resolveLevelCounter.totalCubicInstanceCount()) {
340 GrVectorXform xform(fViewMatrix);
341 for (auto [verb, pts, w] : SkPathPriv::Iterate(fPath)) {
342 int level;
343 switch (verb) {
344 default:
345 continue;
346 case SkPathVerb::kQuad:
347 level = GrWangsFormula::quadratic_log2(kTessellationIntolerance, pts, xform);
348 if (level == 0) {
349 continue;
350 }
351 level = std::min(level, kMaxResolveLevel);
352 quad2cubic(pts, instanceLocations[level]);
353 break;
354 case SkPathVerb::kCubic:
355 level = GrWangsFormula::cubic_log2(kTessellationIntolerance, pts, xform);
356 if (level == 0) {
357 continue;
358 }
359 level = std::min(level, kMaxResolveLevel);
360 memcpy(instanceLocations[level], pts, sizeof(SkPoint) * 4);
361 break;
362 }
363 instanceLocations[level] += 4;
364 fCubicVertexCount += 4;
365 }
366 }
367
368#ifdef SK_DEBUG
369 for (int i = 1; i <= kMaxResolveLevel; ++i) {
370 SkASSERT(instanceLocations[i] == endLocations[i]);
371 }
372 SkASSERT(fCubicVertexCount == (numTrianglesAtBeginningOfData +
373 resolveLevelCounter.totalCubicInstanceCount()) * 4);
374#endif
375
376 fStencilCubicsShader = target->allocator()->make<GrMiddleOutCubicShader>(fViewMatrix);
377}
378
379void GrTessellatePathOp::prepareTessellatedOuterCubics(GrMeshDrawOp::Target* target,
380 int numCountedCurves) {
381 SkASSERT(numCountedCurves >= 0);
Chris Dalton04f9cda2020-04-23 10:04:25 -0600382 SkASSERT(!fCubicBuffer);
383 SkASSERT(!fStencilCubicsShader);
Chris Dalton42915c22020-04-22 16:24:43 -0600384
385 if (numCountedCurves == 0) {
386 return;
387 }
388
Chris Dalton2f2d81c2020-05-13 17:57:37 -0600389 auto* vertexData = static_cast<SkPoint*>(target->makeVertexSpace(
Chris Daltonb5391d92020-05-24 14:55:54 -0600390 sizeof(SkPoint), numCountedCurves * 4, &fCubicBuffer, &fBaseCubicVertex));
Chris Dalton04f9cda2020-04-23 10:04:25 -0600391 if (!vertexData) {
Chris Dalton42915c22020-04-22 16:24:43 -0600392 return;
393 }
Chris Dalton04f9cda2020-04-23 10:04:25 -0600394 fCubicVertexCount = 0;
Chris Dalton42915c22020-04-22 16:24:43 -0600395
Chris Daltonf7a33072020-05-01 10:33:08 -0600396 for (auto [verb, pts, w] : SkPathPriv::Iterate(fPath)) {
397 switch (verb) {
Chris Daltonb5391d92020-05-24 14:55:54 -0600398 default:
399 continue;
Chris Daltonf7a33072020-05-01 10:33:08 -0600400 case SkPathVerb::kQuad:
401 SkASSERT(fCubicVertexCount < numCountedCurves * 4);
402 quad2cubic(pts, vertexData + fCubicVertexCount);
Chris Daltonf7a33072020-05-01 10:33:08 -0600403 break;
404 case SkPathVerb::kCubic:
405 SkASSERT(fCubicVertexCount < numCountedCurves * 4);
406 memcpy(vertexData + fCubicVertexCount, pts, sizeof(SkPoint) * 4);
Chris Daltonf7a33072020-05-01 10:33:08 -0600407 break;
Chris Dalton42915c22020-04-22 16:24:43 -0600408 }
Chris Daltonb5391d92020-05-24 14:55:54 -0600409 fCubicVertexCount += 4;
Chris Dalton42915c22020-04-22 16:24:43 -0600410 }
Chris Dalton04f9cda2020-04-23 10:04:25 -0600411 SkASSERT(fCubicVertexCount == numCountedCurves * 4);
412
Chris Daltonb5391d92020-05-24 14:55:54 -0600413 fStencilCubicsShader = target->allocator()->make<GrTessellateCubicShader>(fViewMatrix);
Chris Dalton42915c22020-04-22 16:24:43 -0600414}
415
Chris Daltonb5391d92020-05-24 14:55:54 -0600416void GrTessellatePathOp::prepareTessellatedCubicWedges(GrMeshDrawOp::Target* target) {
Chris Dalton04f9cda2020-04-23 10:04:25 -0600417 SkASSERT(!fCubicBuffer);
418 SkASSERT(!fStencilCubicsShader);
419
Chris Dalton42915c22020-04-22 16:24:43 -0600420 // No initial moveTo, one wedge per verb, plus an implicit close at the end.
421 // Each wedge has 5 vertices.
422 int maxVertices = (fPath.countVerbs() + 1) * 5;
423
Chris Dalton2f2d81c2020-05-13 17:57:37 -0600424 GrEagerDynamicVertexAllocator vertexAlloc(target, &fCubicBuffer, &fBaseCubicVertex);
Chris Dalton42915c22020-04-22 16:24:43 -0600425 auto* vertexData = vertexAlloc.lock<SkPoint>(maxVertices);
426 if (!vertexData) {
Chris Dalton04f9cda2020-04-23 10:04:25 -0600427 return;
Chris Dalton42915c22020-04-22 16:24:43 -0600428 }
Chris Dalton04f9cda2020-04-23 10:04:25 -0600429 fCubicVertexCount = 0;
Chris Dalton42915c22020-04-22 16:24:43 -0600430
431 GrMidpointContourParser parser(fPath);
432 while (parser.parseNextContour()) {
Chris Daltonf7a33072020-05-01 10:33:08 -0600433 SkPoint midpoint = parser.currentMidpoint();
434 SkPoint startPoint = {0, 0};
435 SkPoint lastPoint = startPoint;
436 for (auto [verb, pts, w] : parser.currentContour()) {
437 switch (verb) {
438 case SkPathVerb::kMove:
439 startPoint = lastPoint = pts[0];
Chris Dalton42915c22020-04-22 16:24:43 -0600440 continue;
Chris Daltonf7a33072020-05-01 10:33:08 -0600441 case SkPathVerb::kClose:
442 continue; // Ignore. We can assume an implicit close at the end.
Chris Dalton42915c22020-04-22 16:24:43 -0600443 case SkPathVerb::kLine:
Chris Daltonf7a33072020-05-01 10:33:08 -0600444 line2cubic(pts[0], pts[1], vertexData + fCubicVertexCount);
445 lastPoint = pts[1];
Chris Dalton42915c22020-04-22 16:24:43 -0600446 break;
447 case SkPathVerb::kQuad:
Chris Daltonf7a33072020-05-01 10:33:08 -0600448 quad2cubic(pts, vertexData + fCubicVertexCount);
449 lastPoint = pts[2];
Chris Dalton42915c22020-04-22 16:24:43 -0600450 break;
451 case SkPathVerb::kCubic:
Chris Daltonf7a33072020-05-01 10:33:08 -0600452 memcpy(vertexData + fCubicVertexCount, pts, sizeof(SkPoint) * 4);
453 lastPoint = pts[3];
Chris Dalton42915c22020-04-22 16:24:43 -0600454 break;
455 case SkPathVerb::kConic:
456 SkUNREACHABLE;
457 }
Chris Daltonf7a33072020-05-01 10:33:08 -0600458 vertexData[fCubicVertexCount + 4] = midpoint;
459 fCubicVertexCount += 5;
460 }
461 if (lastPoint != startPoint) {
462 line2cubic(lastPoint, startPoint, vertexData + fCubicVertexCount);
463 vertexData[fCubicVertexCount + 4] = midpoint;
Chris Dalton04f9cda2020-04-23 10:04:25 -0600464 fCubicVertexCount += 5;
Chris Dalton42915c22020-04-22 16:24:43 -0600465 }
466 }
467
Chris Dalton04f9cda2020-04-23 10:04:25 -0600468 vertexAlloc.unlock(fCubicVertexCount);
469
470 if (fCubicVertexCount) {
Chris Daltonb5391d92020-05-24 14:55:54 -0600471 fStencilCubicsShader = target->allocator()->make<GrTessellateWedgeShader>(fViewMatrix);
Chris Dalton04f9cda2020-04-23 10:04:25 -0600472 }
Chris Dalton42915c22020-04-22 16:24:43 -0600473}
474
Chris Daltonb5391d92020-05-24 14:55:54 -0600475void GrTessellatePathOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
476 this->drawStencilPass(flushState);
Chris Daltonb832ce62020-01-06 19:49:37 -0700477 if (!(Flags::kStencilOnly & fFlags)) {
Chris Daltonb5391d92020-05-24 14:55:54 -0600478 this->drawCoverPass(flushState);
Chris Daltonb832ce62020-01-06 19:49:37 -0700479 }
480}
481
Chris Daltonb5391d92020-05-24 14:55:54 -0600482void GrTessellatePathOp::drawStencilPass(GrOpFlushState* flushState) {
Chris Daltonb832ce62020-01-06 19:49:37 -0700483 // Increments clockwise triangles and decrements counterclockwise. Used for "winding" fill.
484 constexpr static GrUserStencilSettings kIncrDecrStencil(
485 GrUserStencilSettings::StaticInitSeparate<
486 0x0000, 0x0000,
487 GrUserStencilTest::kAlwaysIfInClip, GrUserStencilTest::kAlwaysIfInClip,
488 0xffff, 0xffff,
489 GrUserStencilOp::kIncWrap, GrUserStencilOp::kDecWrap,
490 GrUserStencilOp::kKeep, GrUserStencilOp::kKeep,
491 0xffff, 0xffff>());
492
493 // Inverts the bottom stencil bit. Used for "even/odd" fill.
494 constexpr static GrUserStencilSettings kInvertStencil(
495 GrUserStencilSettings::StaticInit<
496 0x0000,
497 GrUserStencilTest::kAlwaysIfInClip,
498 0xffff,
499 GrUserStencilOp::kInvert,
500 GrUserStencilOp::kKeep,
501 0x0001>());
502
503 GrPipeline::InitArgs initArgs;
504 if (GrAAType::kNone != fAAType) {
505 initArgs.fInputFlags |= GrPipeline::InputFlags::kHWAntialias;
506 }
Chris Daltonb5391d92020-05-24 14:55:54 -0600507 if (flushState->caps().wireframeSupport() && (Flags::kWireframe & fFlags)) {
Chris Daltonb832ce62020-01-06 19:49:37 -0700508 initArgs.fInputFlags |= GrPipeline::InputFlags::kWireframe;
509 }
510 SkASSERT(SkPathFillType::kWinding == fPath.getFillType() ||
511 SkPathFillType::kEvenOdd == fPath.getFillType());
512 initArgs.fUserStencil = (SkPathFillType::kWinding == fPath.getFillType()) ?
513 &kIncrDecrStencil : &kInvertStencil;
Chris Daltonb5391d92020-05-24 14:55:54 -0600514 initArgs.fCaps = &flushState->caps();
Chris Daltonaa0e45c2020-03-16 10:05:11 -0600515 GrPipeline pipeline(initArgs, GrDisableColorXPFactory::MakeXferProcessor(),
Chris Daltonb5391d92020-05-24 14:55:54 -0600516 flushState->appliedHardClip());
Chris Dalton012f8492020-03-05 11:49:15 -0700517
Chris Dalton04f9cda2020-04-23 10:04:25 -0600518 if (fDoStencilTriangleBuffer) {
519 SkASSERT(fTriangleBuffer);
520 GrStencilTriangleShader stencilTriangleShader(fViewMatrix);
Chris Daltonb5391d92020-05-24 14:55:54 -0600521 GrPathShader::ProgramInfo programInfo(flushState->writeView(), &pipeline,
Chris Dalton04f9cda2020-04-23 10:04:25 -0600522 &stencilTriangleShader);
Chris Daltonb5391d92020-05-24 14:55:54 -0600523 flushState->bindPipelineAndScissorClip(programInfo, this->bounds());
524 flushState->bindBuffers(nullptr, nullptr, fTriangleBuffer.get());
525 flushState->draw(fTriangleVertexCount, fBaseTriangleVertex);
Chris Daltonf9aea7f2020-01-21 11:19:26 -0700526 }
527
Chris Dalton04f9cda2020-04-23 10:04:25 -0600528 if (fStencilCubicsShader) {
Chris Daltonb5391d92020-05-24 14:55:54 -0600529 SkASSERT(fCubicBuffer);
530 GrPathShader::ProgramInfo programInfo(flushState->writeView(), &pipeline,
531 fStencilCubicsShader);
532 flushState->bindPipelineAndScissorClip(programInfo, this->bounds());
533 if (fIndirectDrawBuffer) {
534 auto indexBuffer = GrMiddleOutCubicShader::FindOrMakeMiddleOutIndexBuffer(
535 flushState->resourceProvider());
536 flushState->bindBuffers(indexBuffer.get(), fCubicBuffer.get(), nullptr);
537 flushState->drawIndexedIndirect(fIndirectDrawBuffer.get(), fIndirectDrawOffset,
538 fIndirectDrawCount);
539 } else {
540 flushState->bindBuffers(nullptr, nullptr, fCubicBuffer.get());
541 flushState->draw(fCubicVertexCount, fBaseCubicVertex);
542 if (flushState->caps().requiresManualFBBarrierAfterTessellatedStencilDraw()) {
543 flushState->gpu()->insertManualFramebufferBarrier(); // http://skbug.com/9739
544 }
545 }
Chris Daltonb832ce62020-01-06 19:49:37 -0700546 }
547}
548
Chris Daltonb5391d92020-05-24 14:55:54 -0600549void GrTessellatePathOp::drawCoverPass(GrOpFlushState* flushState) {
Chris Daltonb832ce62020-01-06 19:49:37 -0700550 // Allows non-zero stencil values to pass and write a color, and resets the stencil value back
551 // to zero; discards immediately on stencil values of zero.
552 // NOTE: It's ok to not check the clip here because the previous stencil pass only wrote to
553 // samples already inside the clip.
554 constexpr static GrUserStencilSettings kTestAndResetStencil(
555 GrUserStencilSettings::StaticInit<
556 0x0000,
557 GrUserStencilTest::kNotEqual,
558 0xffff,
559 GrUserStencilOp::kZero,
560 GrUserStencilOp::kKeep,
561 0xffff>());
562
563 GrPipeline::InitArgs initArgs;
564 if (GrAAType::kNone != fAAType) {
565 initArgs.fInputFlags |= GrPipeline::InputFlags::kHWAntialias;
Chris Daltonb5391d92020-05-24 14:55:54 -0600566 if (1 == flushState->proxy()->numSamples()) {
Chris Daltonb832ce62020-01-06 19:49:37 -0700567 SkASSERT(GrAAType::kCoverage == fAAType);
568 // We are mixed sampled. Use conservative raster to make the sample coverage mask 100%
569 // at every fragment. This way we will still get a double hit on shared edges, but
570 // whichever side comes first will cover every sample and will clear the stencil. The
571 // other side will then be discarded and not cause a double blend.
572 initArgs.fInputFlags |= GrPipeline::InputFlags::kConservativeRaster;
573 }
574 }
Chris Daltonb5391d92020-05-24 14:55:54 -0600575 initArgs.fCaps = &flushState->caps();
576 initArgs.fDstProxyView = flushState->drawOpArgs().dstProxyView();
577 initArgs.fWriteSwizzle = flushState->drawOpArgs().writeSwizzle();
578 GrPipeline pipeline(initArgs, std::move(fProcessors), flushState->detachAppliedClip());
Chris Daltonb832ce62020-01-06 19:49:37 -0700579
Chris Dalton04f9cda2020-04-23 10:04:25 -0600580 if (fDoFillTriangleBuffer) {
581 SkASSERT(fTriangleBuffer);
Chris Daltonb832ce62020-01-06 19:49:37 -0700582
Chris Dalton04f9cda2020-04-23 10:04:25 -0600583 // These are a twist on the standard red book stencil settings that allow us to fill the
Chris Dalton4328e922020-01-29 13:16:14 -0700584 // inner polygon directly to the final render target. At this point, the curves are already
585 // stencilled in. So if the stencil value is zero, then it means the path at our sample is
586 // not affected by any curves and we fill the path in directly. If the stencil value is
587 // nonzero, then we don't fill and instead continue the standard red book stencil process.
588 //
589 // NOTE: These settings are currently incompatible with a stencil clip.
590 constexpr static GrUserStencilSettings kFillOrIncrDecrStencil(
591 GrUserStencilSettings::StaticInitSeparate<
592 0x0000, 0x0000,
593 GrUserStencilTest::kEqual, GrUserStencilTest::kEqual,
594 0xffff, 0xffff,
595 GrUserStencilOp::kKeep, GrUserStencilOp::kKeep,
596 GrUserStencilOp::kIncWrap, GrUserStencilOp::kDecWrap,
597 0xffff, 0xffff>());
598
599 constexpr static GrUserStencilSettings kFillOrInvertStencil(
600 GrUserStencilSettings::StaticInit<
601 0x0000,
602 GrUserStencilTest::kEqual,
603 0xffff,
604 GrUserStencilOp::kKeep,
605 GrUserStencilOp::kZero,
606 0xffff>());
607
Chris Dalton04f9cda2020-04-23 10:04:25 -0600608 if (fDoStencilTriangleBuffer) {
Chris Dalton4328e922020-01-29 13:16:14 -0700609 // The path was already stencilled. Here we just need to do a cover pass.
610 pipeline.setUserStencil(&kTestAndResetStencil);
Chris Dalton04f9cda2020-04-23 10:04:25 -0600611 } else if (!fStencilCubicsShader) {
612 // There are no stencilled curves. We can ignore stencil and fill the path directly.
Chris Dalton4328e922020-01-29 13:16:14 -0700613 pipeline.setUserStencil(&GrUserStencilSettings::kUnused);
614 } else if (SkPathFillType::kWinding == fPath.getFillType()) {
615 // Fill in the path pixels not touched by curves, incr/decr stencil otherwise.
616 SkASSERT(!pipeline.hasStencilClip());
617 pipeline.setUserStencil(&kFillOrIncrDecrStencil);
618 } else {
619 // Fill in the path pixels not touched by curves, invert stencil otherwise.
620 SkASSERT(!pipeline.hasStencilClip());
621 pipeline.setUserStencil(&kFillOrInvertStencil);
622 }
Chris Dalton4328e922020-01-29 13:16:14 -0700623
Chris Dalton04f9cda2020-04-23 10:04:25 -0600624 GrFillTriangleShader fillTriangleShader(fViewMatrix, fColor);
Chris Daltonb5391d92020-05-24 14:55:54 -0600625 GrPathShader::ProgramInfo programInfo(flushState->writeView(), &pipeline,
626 &fillTriangleShader);
627 flushState->bindPipelineAndScissorClip(programInfo, this->bounds());
628 flushState->bindTextures(fillTriangleShader, nullptr, pipeline);
629 flushState->bindBuffers(nullptr, nullptr, fTriangleBuffer.get());
630 flushState->draw(fTriangleVertexCount, fBaseTriangleVertex);
Chris Dalton04f9cda2020-04-23 10:04:25 -0600631
632 if (fStencilCubicsShader) {
Chris Daltonb5391d92020-05-24 14:55:54 -0600633 SkASSERT(fCubicBuffer);
634
Chris Dalton4328e922020-01-29 13:16:14 -0700635 // At this point, every pixel is filled in except the ones touched by curves. Issue a
636 // final cover pass over the curves by drawing their convex hulls. This will fill in any
637 // remaining samples and reset the stencil buffer.
Chris Dalton4328e922020-01-29 13:16:14 -0700638 pipeline.setUserStencil(&kTestAndResetStencil);
Chris Dalton04f9cda2020-04-23 10:04:25 -0600639 GrFillCubicHullShader fillCubicHullShader(fViewMatrix, fColor);
Chris Daltonb5391d92020-05-24 14:55:54 -0600640 GrPathShader::ProgramInfo programInfo(flushState->writeView(), &pipeline,
Chris Dalton04f9cda2020-04-23 10:04:25 -0600641 &fillCubicHullShader);
Chris Daltonb5391d92020-05-24 14:55:54 -0600642 flushState->bindPipelineAndScissorClip(programInfo, this->bounds());
643 flushState->bindTextures(fillCubicHullShader, nullptr, pipeline);
Chris Dalton04f9cda2020-04-23 10:04:25 -0600644
645 // Here we treat fCubicBuffer as an instance buffer. It should have been prepared with
646 // the base vertex on an instance boundary in order to accommodate this.
647 SkASSERT((fCubicVertexCount % 4) == 0);
648 SkASSERT((fBaseCubicVertex % 4) == 0);
Chris Daltonb5391d92020-05-24 14:55:54 -0600649 flushState->bindBuffers(nullptr, fCubicBuffer.get(), nullptr);
650 flushState->drawInstanced(fCubicVertexCount >> 2, fBaseCubicVertex >> 2, 4, 0);
Chris Dalton4328e922020-01-29 13:16:14 -0700651 }
Chris Dalton42915c22020-04-22 16:24:43 -0600652 return;
Chris Dalton4328e922020-01-29 13:16:14 -0700653 }
Chris Dalton42915c22020-04-22 16:24:43 -0600654
Chris Dalton04f9cda2020-04-23 10:04:25 -0600655 // There are no triangles to fill. Just draw a bounding box.
Chris Dalton42915c22020-04-22 16:24:43 -0600656 pipeline.setUserStencil(&kTestAndResetStencil);
Chris Dalton04f9cda2020-04-23 10:04:25 -0600657 GrFillBoundingBoxShader fillBoundingBoxShader(fViewMatrix, fColor, fPath.getBounds());
Chris Daltonb5391d92020-05-24 14:55:54 -0600658 GrPathShader::ProgramInfo programInfo(flushState->writeView(), &pipeline,
659 &fillBoundingBoxShader);
660 flushState->bindPipelineAndScissorClip(programInfo, this->bounds());
661 flushState->bindTextures(fillBoundingBoxShader, nullptr, pipeline);
662 flushState->bindBuffers(nullptr, nullptr, nullptr);
663 flushState->draw(4, 0);
Chris Daltonb832ce62020-01-06 19:49:37 -0700664}