blob: 2f7caadc77ebffc66e7f3f3afc44bf63cab8eb88 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/ccpr/GrCCFiller.h"
Chris Dalton9ca27842018-01-18 12:24:50 -07009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkPath.h"
11#include "include/core/SkPoint.h"
12#include "src/core/SkMathPriv.h"
13#include "src/core/SkPathPriv.h"
14#include "src/gpu/GrCaps.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/GrOnFlushResourceProvider.h"
16#include "src/gpu/GrOpFlushState.h"
Chris Dalton84403d72018-02-13 21:46:17 -050017#include <stdlib.h>
Chris Dalton9ca27842018-01-18 12:24:50 -070018
Chris Dalton84403d72018-02-13 21:46:17 -050019using TriPointInstance = GrCCCoverageProcessor::TriPointInstance;
20using QuadPointInstance = GrCCCoverageProcessor::QuadPointInstance;
Chris Dalton9ca27842018-01-18 12:24:50 -070021
Chris Daltonc3318f02019-07-19 14:20:53 -060022GrCCFiller::GrCCFiller(Algorithm algorithm, int numPaths, int numSkPoints, int numSkVerbs,
23 int numConicWeights)
24 : fAlgorithm(algorithm)
25 , fGeometry(numSkPoints, numSkVerbs, numConicWeights)
Chris Daltone1639692018-08-20 14:00:30 -060026 , fPathInfos(numPaths)
Chris Dalton3917b1e2018-05-09 00:40:52 -060027 , fScissorSubBatches(numPaths)
Chris Dalton9ca27842018-01-18 12:24:50 -070028 , fTotalPrimitiveCounts{PrimitiveTallies(), PrimitiveTallies()} {
29 // Batches decide what to draw by looking where the previous one ended. Define initial batches
30 // that "end" at the beginning of the data. These will not be drawn, but will only be be read by
31 // the first actual batch.
32 fScissorSubBatches.push_back() = {PrimitiveTallies(), SkIRect::MakeEmpty()};
Chris Daltone1639692018-08-20 14:00:30 -060033 fBatches.push_back() = {PrimitiveTallies(), fScissorSubBatches.count(), PrimitiveTallies()};
Chris Dalton9ca27842018-01-18 12:24:50 -070034}
35
Chris Daltone1639692018-08-20 14:00:30 -060036void GrCCFiller::parseDeviceSpaceFill(const SkPath& path, const SkPoint* deviceSpacePts,
37 GrScissorTest scissorTest, const SkIRect& clippedDevIBounds,
38 const SkIVector& devToAtlasOffset) {
39 SkASSERT(!fInstanceBuffer); // Can't call after prepareToDraw().
40 SkASSERT(!path.isEmpty());
Chris Dalton9ca27842018-01-18 12:24:50 -070041
Chris Daltone1639692018-08-20 14:00:30 -060042 int currPathPointsIdx = fGeometry.points().count();
43 int currPathVerbsIdx = fGeometry.verbs().count();
44 PrimitiveTallies currPathPrimitiveCounts = PrimitiveTallies();
Chris Dalton9ca27842018-01-18 12:24:50 -070045
46 fGeometry.beginPath();
47
Chris Dalton9f2dab02018-04-18 14:07:03 -060048 const float* conicWeights = SkPathPriv::ConicWeightData(path);
Chris Dalton9ca27842018-01-18 12:24:50 -070049 int ptsIdx = 0;
Chris Dalton9f2dab02018-04-18 14:07:03 -060050 int conicWeightsIdx = 0;
Chris Dalton9ca27842018-01-18 12:24:50 -070051 bool insideContour = false;
52
53 for (SkPath::Verb verb : SkPathPriv::Verbs(path)) {
54 switch (verb) {
55 case SkPath::kMove_Verb:
Chris Daltone1639692018-08-20 14:00:30 -060056 if (insideContour) {
57 currPathPrimitiveCounts += fGeometry.endContour();
58 }
Chris Dalton9ca27842018-01-18 12:24:50 -070059 fGeometry.beginContour(deviceSpacePts[ptsIdx]);
60 ++ptsIdx;
61 insideContour = true;
62 continue;
63 case SkPath::kClose_Verb:
Chris Daltone1639692018-08-20 14:00:30 -060064 if (insideContour) {
65 currPathPrimitiveCounts += fGeometry.endContour();
66 }
Chris Dalton9ca27842018-01-18 12:24:50 -070067 insideContour = false;
68 continue;
69 case SkPath::kLine_Verb:
Chris Dalton6f5e77a2018-04-23 21:14:42 -060070 fGeometry.lineTo(&deviceSpacePts[ptsIdx - 1]);
Chris Dalton9ca27842018-01-18 12:24:50 -070071 ++ptsIdx;
72 continue;
73 case SkPath::kQuad_Verb:
Chris Dalton7ca3b7b2018-04-10 00:21:19 -060074 fGeometry.quadraticTo(&deviceSpacePts[ptsIdx - 1]);
Chris Dalton9ca27842018-01-18 12:24:50 -070075 ptsIdx += 2;
76 continue;
77 case SkPath::kCubic_Verb:
Chris Dalton7ca3b7b2018-04-10 00:21:19 -060078 fGeometry.cubicTo(&deviceSpacePts[ptsIdx - 1]);
Chris Dalton9ca27842018-01-18 12:24:50 -070079 ptsIdx += 3;
80 continue;
81 case SkPath::kConic_Verb:
Chris Dalton9f2dab02018-04-18 14:07:03 -060082 fGeometry.conicTo(&deviceSpacePts[ptsIdx - 1], conicWeights[conicWeightsIdx]);
83 ptsIdx += 2;
84 ++conicWeightsIdx;
85 continue;
Chris Dalton9ca27842018-01-18 12:24:50 -070086 default:
87 SK_ABORT("Unexpected path verb.");
88 }
89 }
Chris Dalton9f2dab02018-04-18 14:07:03 -060090 SkASSERT(ptsIdx == path.countPoints());
91 SkASSERT(conicWeightsIdx == SkPathPriv::ConicWeightCnt(path));
Chris Dalton9ca27842018-01-18 12:24:50 -070092
Chris Dalton9ca27842018-01-18 12:24:50 -070093 if (insideContour) {
Chris Daltone1639692018-08-20 14:00:30 -060094 currPathPrimitiveCounts += fGeometry.endContour();
Chris Dalton9ca27842018-01-18 12:24:50 -070095 }
Chris Dalton9ca27842018-01-18 12:24:50 -070096
Chris Daltone1639692018-08-20 14:00:30 -060097 fPathInfos.emplace_back(scissorTest, devToAtlasOffset);
Chris Dalton84403d72018-02-13 21:46:17 -050098
99 // Tessellate fans from very large and/or simple paths, in order to reduce overdraw.
Chris Daltone1639692018-08-20 14:00:30 -0600100 int numVerbs = fGeometry.verbs().count() - currPathVerbsIdx - 1;
Chris Dalton84403d72018-02-13 21:46:17 -0500101 int64_t tessellationWork = (int64_t)numVerbs * (32 - SkCLZ(numVerbs)); // N log N.
102 int64_t fanningWork = (int64_t)clippedDevIBounds.height() * clippedDevIBounds.width();
103 if (tessellationWork * (50*50) + (100*100) < fanningWork) { // Don't tessellate under 100x100.
Chris Daltonc3318f02019-07-19 14:20:53 -0600104 fPathInfos.back().tessellateFan(
105 fAlgorithm, path, fGeometry, currPathVerbsIdx, currPathPointsIdx, clippedDevIBounds,
106 &currPathPrimitiveCounts);
Chris Dalton84403d72018-02-13 21:46:17 -0500107 }
108
Chris Daltone1639692018-08-20 14:00:30 -0600109 fTotalPrimitiveCounts[(int)scissorTest] += currPathPrimitiveCounts;
Chris Dalton9ca27842018-01-18 12:24:50 -0700110
Chris Dalton916c4982018-08-15 00:53:25 -0600111 if (GrScissorTest::kEnabled == scissorTest) {
112 fScissorSubBatches.push_back() = {fTotalPrimitiveCounts[(int)GrScissorTest::kEnabled],
Chris Dalton9414c962018-06-14 10:14:50 -0600113 clippedDevIBounds.makeOffset(devToAtlasOffset.fX,
114 devToAtlasOffset.fY)};
Chris Dalton9ca27842018-01-18 12:24:50 -0700115 }
Chris Dalton9ca27842018-01-18 12:24:50 -0700116}
117
Chris Daltonc3318f02019-07-19 14:20:53 -0600118void GrCCFiller::PathInfo::tessellateFan(
119 Algorithm algorithm, const SkPath& originalPath, const GrCCFillGeometry& geometry,
120 int verbsIdx, int ptsIdx, const SkIRect& clippedDevIBounds,
121 PrimitiveTallies* newTriangleCounts) {
Chris Daltone1639692018-08-20 14:00:30 -0600122 using Verb = GrCCFillGeometry::Verb;
123 SkASSERT(-1 == fFanTessellationCount);
124 SkASSERT(!fFanTessellation);
125
126 const SkTArray<Verb, true>& verbs = geometry.verbs();
127 const SkTArray<SkPoint, true>& pts = geometry.points();
128
129 newTriangleCounts->fTriangles =
130 newTriangleCounts->fWeightedTriangles = 0;
131
Chris Daltonc3318f02019-07-19 14:20:53 -0600132 // Build an SkPath of the Redbook fan.
Chris Daltone1639692018-08-20 14:00:30 -0600133 SkPath fan;
Chris Daltonc3318f02019-07-19 14:20:53 -0600134 if (Algorithm::kCoverageCount == algorithm) {
135 // We use "winding" fill type right now because we are producing a coverage count, and must
136 // fill in every region that has non-zero wind. The path processor will convert coverage
137 // count to the appropriate fill type later.
Mike Reed4241f5e2019-09-14 19:13:23 +0000138 fan.setFillType(SkPath::kWinding_FillType);
Chris Daltonc3318f02019-07-19 14:20:53 -0600139 } else {
Chris Daltonc0966542019-09-23 20:16:03 -0600140 // When counting winding numbers in the stencil buffer, it works to use even/odd for the fan
141 // tessellation (where applicable). But we need to strip out inverse fill info because
142 // inverse-ness gets accounted for later on.
143 fan.setFillType(SkPath::ConvertToNonInverseFillType(originalPath.getFillType()));
Chris Daltonc3318f02019-07-19 14:20:53 -0600144 }
Chris Daltone1639692018-08-20 14:00:30 -0600145 SkASSERT(Verb::kBeginPath == verbs[verbsIdx]);
146 for (int i = verbsIdx + 1; i < verbs.count(); ++i) {
147 switch (verbs[i]) {
148 case Verb::kBeginPath:
149 SK_ABORT("Invalid GrCCFillGeometry");
150 continue;
151
152 case Verb::kBeginContour:
153 fan.moveTo(pts[ptsIdx++]);
154 continue;
155
156 case Verb::kLineTo:
157 fan.lineTo(pts[ptsIdx++]);
158 continue;
159
160 case Verb::kMonotonicQuadraticTo:
161 case Verb::kMonotonicConicTo:
162 fan.lineTo(pts[ptsIdx + 1]);
163 ptsIdx += 2;
164 continue;
165
166 case Verb::kMonotonicCubicTo:
167 fan.lineTo(pts[ptsIdx + 2]);
168 ptsIdx += 3;
169 continue;
170
171 case Verb::kEndClosedContour:
172 case Verb::kEndOpenContour:
173 fan.close();
174 continue;
175 }
176 }
177
178 GrTessellator::WindingVertex* vertices = nullptr;
Chris Daltonc0966542019-09-23 20:16:03 -0600179 SkASSERT(!fan.isInverseFillType());
180 fFanTessellationCount = GrTessellator::PathToVertices(
181 fan, std::numeric_limits<float>::infinity(), SkRect::Make(clippedDevIBounds),
182 &vertices);
Chris Daltone1639692018-08-20 14:00:30 -0600183 if (fFanTessellationCount <= 0) {
184 SkASSERT(0 == fFanTessellationCount);
185 SkASSERT(nullptr == vertices);
186 return;
187 }
188
189 SkASSERT(0 == fFanTessellationCount % 3);
190 for (int i = 0; i < fFanTessellationCount; i += 3) {
191 int tessWinding = vertices[i].fWinding;
192 SkASSERT(tessWinding == vertices[i + 1].fWinding);
193 SkASSERT(tessWinding == vertices[i + 2].fWinding);
194
195 // Ensure this triangle's points actually wind in the same direction as tessWinding.
196 // CCPR shaders use the sign of wind to determine which direction to bloat, so even for
197 // "wound" triangles the winding sign and point ordering need to agree.
198 float ax = vertices[i].fPos.fX - vertices[i + 1].fPos.fX;
199 float ay = vertices[i].fPos.fY - vertices[i + 1].fPos.fY;
200 float bx = vertices[i].fPos.fX - vertices[i + 2].fPos.fX;
201 float by = vertices[i].fPos.fY - vertices[i + 2].fPos.fY;
202 float wind = ax*by - ay*bx;
203 if ((wind > 0) != (-tessWinding > 0)) { // Tessellator has opposite winding sense.
204 std::swap(vertices[i + 1].fPos, vertices[i + 2].fPos);
205 }
206
Chris Daltonc3318f02019-07-19 14:20:53 -0600207 int weight = abs(tessWinding);
208 SkASSERT(SkPath::kEvenOdd_FillType != fan.getFillType() || weight == 1);
209 if (weight > 1 && Algorithm::kCoverageCount == algorithm) {
Chris Daltone1639692018-08-20 14:00:30 -0600210 ++newTriangleCounts->fWeightedTriangles;
Chris Daltonc3318f02019-07-19 14:20:53 -0600211 } else {
212 newTriangleCounts->fTriangles += weight;
Chris Daltone1639692018-08-20 14:00:30 -0600213 }
214 }
215
216 fFanTessellation.reset(vertices);
Chris Dalton9ca27842018-01-18 12:24:50 -0700217}
218
Chris Daltone1639692018-08-20 14:00:30 -0600219GrCCFiller::BatchID GrCCFiller::closeCurrentBatch() {
Chris Dalton9ca27842018-01-18 12:24:50 -0700220 SkASSERT(!fInstanceBuffer);
Chris Daltone1639692018-08-20 14:00:30 -0600221 SkASSERT(!fBatches.empty());
Chris Dalton9ca27842018-01-18 12:24:50 -0700222
Chris Daltone1639692018-08-20 14:00:30 -0600223 const auto& lastBatch = fBatches.back();
Chris Daltona883aca2018-03-08 23:05:30 -0700224 int maxMeshes = 1 + fScissorSubBatches.count() - lastBatch.fEndScissorSubBatchIdx;
225 fMaxMeshesPerDraw = SkTMax(fMaxMeshesPerDraw, maxMeshes);
226
227 const auto& lastScissorSubBatch = fScissorSubBatches[lastBatch.fEndScissorSubBatchIdx - 1];
Chris Dalton916c4982018-08-15 00:53:25 -0600228 PrimitiveTallies batchTotalCounts = fTotalPrimitiveCounts[(int)GrScissorTest::kDisabled] -
Chris Dalton84403d72018-02-13 21:46:17 -0500229 lastBatch.fEndNonScissorIndices;
Chris Dalton916c4982018-08-15 00:53:25 -0600230 batchTotalCounts += fTotalPrimitiveCounts[(int)GrScissorTest::kEnabled] -
Chris Dalton84403d72018-02-13 21:46:17 -0500231 lastScissorSubBatch.fEndPrimitiveIndices;
Chris Dalton9ca27842018-01-18 12:24:50 -0700232
Chris Daltona883aca2018-03-08 23:05:30 -0700233 // This will invalidate lastBatch.
Chris Daltone1639692018-08-20 14:00:30 -0600234 fBatches.push_back() = {
Chris Dalton916c4982018-08-15 00:53:25 -0600235 fTotalPrimitiveCounts[(int)GrScissorTest::kDisabled],
Chris Dalton84403d72018-02-13 21:46:17 -0500236 fScissorSubBatches.count(),
237 batchTotalCounts
Chris Dalton9ca27842018-01-18 12:24:50 -0700238 };
Chris Daltone1639692018-08-20 14:00:30 -0600239 return fBatches.count() - 1;
Chris Dalton9ca27842018-01-18 12:24:50 -0700240}
241
242// Emits a contour's triangle fan.
243//
244// Classic Redbook fanning would be the triangles: [0 1 2], [0 2 3], ..., [0 n-2 n-1].
245//
246// This function emits the triangle: [0 n/3 n*2/3], and then recurses on all three sides. The
247// advantage to this approach is that for a convex-ish contour, it generates larger triangles.
248// Classic fanning tends to generate long, skinny triangles, which are expensive to draw since they
249// have a longer perimeter to rasterize and antialias.
250//
251// The indices array indexes the fan's points (think: glDrawElements), and must have at least log3
252// elements past the end for this method to use as scratch space.
253//
254// Returns the next triangle instance after the final one emitted.
Chris Daltonc3318f02019-07-19 14:20:53 -0600255static TriPointInstance* emit_recursive_fan(
256 const SkTArray<SkPoint, true>& pts, SkTArray<int32_t, true>& indices, int firstIndex,
257 int indexCount, const Sk2f& devToAtlasOffset, TriPointInstance::Ordering ordering,
258 TriPointInstance out[]) {
Chris Dalton9ca27842018-01-18 12:24:50 -0700259 if (indexCount < 3) {
260 return out;
261 }
262
263 int32_t oneThirdCount = indexCount / 3;
264 int32_t twoThirdsCount = (2 * indexCount) / 3;
265 out++->set(pts[indices[firstIndex]], pts[indices[firstIndex + oneThirdCount]],
Chris Daltonc3318f02019-07-19 14:20:53 -0600266 pts[indices[firstIndex + twoThirdsCount]], devToAtlasOffset, ordering);
Chris Dalton9ca27842018-01-18 12:24:50 -0700267
Chris Daltonc3318f02019-07-19 14:20:53 -0600268 out = emit_recursive_fan(
269 pts, indices, firstIndex, oneThirdCount + 1, devToAtlasOffset, ordering, out);
270 out = emit_recursive_fan(
271 pts, indices, firstIndex + oneThirdCount, twoThirdsCount - oneThirdCount + 1,
272 devToAtlasOffset, ordering, out);
Chris Dalton9ca27842018-01-18 12:24:50 -0700273
274 int endIndex = firstIndex + indexCount;
275 int32_t oldValue = indices[endIndex];
276 indices[endIndex] = indices[firstIndex];
Chris Daltonc3318f02019-07-19 14:20:53 -0600277 out = emit_recursive_fan(
278 pts, indices, firstIndex + twoThirdsCount, indexCount - twoThirdsCount + 1,
279 devToAtlasOffset, ordering, out);
Chris Dalton9ca27842018-01-18 12:24:50 -0700280 indices[endIndex] = oldValue;
281
282 return out;
283}
284
Chris Daltonc3318f02019-07-19 14:20:53 -0600285void GrCCFiller::emitTessellatedFan(
286 const GrTessellator::WindingVertex* vertices, int numVertices, const Sk2f& devToAtlasOffset,
287 TriPointInstance::Ordering ordering, TriPointInstance* triPointInstanceData,
288 QuadPointInstance* quadPointInstanceData, GrCCFillGeometry::PrimitiveTallies* indices) {
Chris Dalton84403d72018-02-13 21:46:17 -0500289 for (int i = 0; i < numVertices; i += 3) {
Chris Daltonc3318f02019-07-19 14:20:53 -0600290 int weight = abs(vertices[i].fWinding);
291 SkASSERT(weight >= 1);
292 if (weight > 1 && Algorithm::kStencilWindingCount != fAlgorithm) {
Chris Dalton703b4762018-04-06 16:11:48 -0600293 quadPointInstanceData[indices->fWeightedTriangles++].setW(
Chris Dalton9414c962018-06-14 10:14:50 -0600294 vertices[i].fPos, vertices[i+1].fPos, vertices[i + 2].fPos, devToAtlasOffset,
Chris Dalton6f5e77a2018-04-23 21:14:42 -0600295 static_cast<float>(abs(vertices[i].fWinding)));
Chris Daltonc3318f02019-07-19 14:20:53 -0600296 } else for (int j = 0; j < weight; ++j) {
297 // Unfortunately, there is not a way to increment stencil values by an amount larger
298 // than 1. Instead we draw the triangle 'weight' times.
299 triPointInstanceData[indices->fTriangles++].set(
300 vertices[i].fPos, vertices[i + 1].fPos, vertices[i + 2].fPos, devToAtlasOffset,
301 ordering);
Chris Dalton84403d72018-02-13 21:46:17 -0500302 }
303 }
304}
305
Chris Daltone1639692018-08-20 14:00:30 -0600306bool GrCCFiller::prepareToDraw(GrOnFlushResourceProvider* onFlushRP) {
307 using Verb = GrCCFillGeometry::Verb;
308 SkASSERT(!fInstanceBuffer);
309 SkASSERT(fBatches.back().fEndNonScissorIndices == // Call closeCurrentBatch().
Chris Dalton916c4982018-08-15 00:53:25 -0600310 fTotalPrimitiveCounts[(int)GrScissorTest::kDisabled]);
Chris Daltone1639692018-08-20 14:00:30 -0600311 SkASSERT(fBatches.back().fEndScissorSubBatchIdx == fScissorSubBatches.count());
Chris Dalton9ca27842018-01-18 12:24:50 -0700312
Chris Daltonc3318f02019-07-19 14:20:53 -0600313 auto triangleOrdering = (Algorithm::kCoverageCount == fAlgorithm)
314 ? TriPointInstance::Ordering::kXYTransposed
315 : TriPointInstance::Ordering::kXYInterleaved;
316
Chris Dalton9ca27842018-01-18 12:24:50 -0700317 // Here we build a single instance buffer to share with every internal batch.
318 //
319 // CCPR processs 3 different types of primitives: triangles, quadratics, cubics. Each primitive
320 // type is further divided into instances that require a scissor and those that don't. This
321 // leaves us with 3*2 = 6 independent instance arrays to build for the GPU.
322 //
323 // Rather than place each instance array in its own GPU buffer, we allocate a single
324 // megabuffer and lay them all out side-by-side. We can offset the "baseInstance" parameter in
325 // our draw calls to direct the GPU to the applicable elements within a given array.
326 //
327 // We already know how big to make each of the 6 arrays from fTotalPrimitiveCounts, so layout is
328 // straightforward. Start with triangles and quadratics. They both view the instance buffer as
Chris Dalton84403d72018-02-13 21:46:17 -0500329 // 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 -0700330 fBaseInstances[0].fTriangles = 0;
331 fBaseInstances[1].fTriangles = fBaseInstances[0].fTriangles +
332 fTotalPrimitiveCounts[0].fTriangles;
333 fBaseInstances[0].fQuadratics = fBaseInstances[1].fTriangles +
334 fTotalPrimitiveCounts[1].fTriangles;
335 fBaseInstances[1].fQuadratics = fBaseInstances[0].fQuadratics +
336 fTotalPrimitiveCounts[0].fQuadratics;
337 int triEndIdx = fBaseInstances[1].fQuadratics + fTotalPrimitiveCounts[1].fQuadratics;
338
Chris Dalton84403d72018-02-13 21:46:17 -0500339 // Wound triangles and cubics both view the same instance buffer as an array of
340 // QuadPointInstance[]. So, reinterpreting the instance data as QuadPointInstance[], we start
341 // them on the first index that will not overwrite previous TriPointInstance data.
342 int quadBaseIdx =
Brian Salomon24d377e2019-04-23 15:24:31 -0400343 GrSizeDivRoundUp(triEndIdx * sizeof(TriPointInstance), sizeof(QuadPointInstance));
Chris Dalton703b4762018-04-06 16:11:48 -0600344 fBaseInstances[0].fWeightedTriangles = quadBaseIdx;
345 fBaseInstances[1].fWeightedTriangles = fBaseInstances[0].fWeightedTriangles +
346 fTotalPrimitiveCounts[0].fWeightedTriangles;
347 fBaseInstances[0].fCubics = fBaseInstances[1].fWeightedTriangles +
348 fTotalPrimitiveCounts[1].fWeightedTriangles;
Chris Dalton9ca27842018-01-18 12:24:50 -0700349 fBaseInstances[1].fCubics = fBaseInstances[0].fCubics + fTotalPrimitiveCounts[0].fCubics;
Chris Dalton9f2dab02018-04-18 14:07:03 -0600350 fBaseInstances[0].fConics = fBaseInstances[1].fCubics + fTotalPrimitiveCounts[1].fCubics;
351 fBaseInstances[1].fConics = fBaseInstances[0].fConics + fTotalPrimitiveCounts[0].fConics;
352 int quadEndIdx = fBaseInstances[1].fConics + fTotalPrimitiveCounts[1].fConics;
Chris Dalton9ca27842018-01-18 12:24:50 -0700353
Brian Salomonae64c192019-02-05 09:41:37 -0500354 fInstanceBuffer =
355 onFlushRP->makeBuffer(GrGpuBufferType::kVertex, quadEndIdx * sizeof(QuadPointInstance));
Chris Dalton9ca27842018-01-18 12:24:50 -0700356 if (!fInstanceBuffer) {
Chris Daltone1639692018-08-20 14:00:30 -0600357 SkDebugf("WARNING: failed to allocate CCPR fill instance buffer.\n");
Chris Dalton9ca27842018-01-18 12:24:50 -0700358 return false;
359 }
360
Chris Dalton84403d72018-02-13 21:46:17 -0500361 TriPointInstance* triPointInstanceData = static_cast<TriPointInstance*>(fInstanceBuffer->map());
362 QuadPointInstance* quadPointInstanceData =
363 reinterpret_cast<QuadPointInstance*>(triPointInstanceData);
364 SkASSERT(quadPointInstanceData);
Chris Dalton9ca27842018-01-18 12:24:50 -0700365
Chris Daltone1639692018-08-20 14:00:30 -0600366 PathInfo* nextPathInfo = fPathInfos.begin();
Chris Dalton9414c962018-06-14 10:14:50 -0600367 Sk2f devToAtlasOffset;
Chris Dalton9ca27842018-01-18 12:24:50 -0700368 PrimitiveTallies instanceIndices[2] = {fBaseInstances[0], fBaseInstances[1]};
369 PrimitiveTallies* currIndices = nullptr;
370 SkSTArray<256, int32_t, true> currFan;
Chris Dalton84403d72018-02-13 21:46:17 -0500371 bool currFanIsTessellated = false;
Chris Dalton9ca27842018-01-18 12:24:50 -0700372
373 const SkTArray<SkPoint, true>& pts = fGeometry.points();
Chris Dalton84403d72018-02-13 21:46:17 -0500374 int ptsIdx = -1;
Chris Dalton9f2dab02018-04-18 14:07:03 -0600375 int nextConicWeightIdx = 0;
Chris Dalton9ca27842018-01-18 12:24:50 -0700376
377 // Expand the ccpr verbs into GPU instance buffers.
Chris Daltone1639692018-08-20 14:00:30 -0600378 for (Verb verb : fGeometry.verbs()) {
Chris Dalton9ca27842018-01-18 12:24:50 -0700379 switch (verb) {
Chris Daltone1639692018-08-20 14:00:30 -0600380 case Verb::kBeginPath:
Chris Dalton9ca27842018-01-18 12:24:50 -0700381 SkASSERT(currFan.empty());
Chris Dalton916c4982018-08-15 00:53:25 -0600382 currIndices = &instanceIndices[(int)nextPathInfo->scissorTest()];
Chris Dalton9414c962018-06-14 10:14:50 -0600383 devToAtlasOffset = Sk2f(static_cast<float>(nextPathInfo->devToAtlasOffset().fX),
384 static_cast<float>(nextPathInfo->devToAtlasOffset().fY));
Chris Daltonad065442018-03-08 22:41:33 -0700385 currFanIsTessellated = nextPathInfo->hasFanTessellation();
Chris Dalton84403d72018-02-13 21:46:17 -0500386 if (currFanIsTessellated) {
Chris Daltonc3318f02019-07-19 14:20:53 -0600387 this->emitTessellatedFan(
388 nextPathInfo->fanTessellation(), nextPathInfo->fanTessellationCount(),
389 devToAtlasOffset, triangleOrdering, triPointInstanceData,
390 quadPointInstanceData, currIndices);
Chris Dalton84403d72018-02-13 21:46:17 -0500391 }
392 ++nextPathInfo;
Chris Dalton9ca27842018-01-18 12:24:50 -0700393 continue;
394
Chris Daltone1639692018-08-20 14:00:30 -0600395 case Verb::kBeginContour:
Chris Dalton9ca27842018-01-18 12:24:50 -0700396 SkASSERT(currFan.empty());
Chris Dalton84403d72018-02-13 21:46:17 -0500397 ++ptsIdx;
398 if (!currFanIsTessellated) {
399 currFan.push_back(ptsIdx);
400 }
Chris Dalton9ca27842018-01-18 12:24:50 -0700401 continue;
402
Chris Daltone1639692018-08-20 14:00:30 -0600403 case Verb::kLineTo:
Chris Dalton84403d72018-02-13 21:46:17 -0500404 ++ptsIdx;
405 if (!currFanIsTessellated) {
406 SkASSERT(!currFan.empty());
407 currFan.push_back(ptsIdx);
408 }
Chris Dalton9ca27842018-01-18 12:24:50 -0700409 continue;
410
Chris Daltone1639692018-08-20 14:00:30 -0600411 case Verb::kMonotonicQuadraticTo:
Chris Daltonc3318f02019-07-19 14:20:53 -0600412 triPointInstanceData[currIndices->fQuadratics++].set(
413 &pts[ptsIdx], devToAtlasOffset, TriPointInstance::Ordering::kXYTransposed);
Chris Dalton84403d72018-02-13 21:46:17 -0500414 ptsIdx += 2;
415 if (!currFanIsTessellated) {
416 SkASSERT(!currFan.empty());
417 currFan.push_back(ptsIdx);
418 }
Chris Dalton9ca27842018-01-18 12:24:50 -0700419 continue;
420
Chris Daltone1639692018-08-20 14:00:30 -0600421 case Verb::kMonotonicCubicTo:
Chris Daltonc3318f02019-07-19 14:20:53 -0600422 quadPointInstanceData[currIndices->fCubics++].set(
423 &pts[ptsIdx], devToAtlasOffset[0], devToAtlasOffset[1]);
Chris Dalton84403d72018-02-13 21:46:17 -0500424 ptsIdx += 3;
425 if (!currFanIsTessellated) {
426 SkASSERT(!currFan.empty());
427 currFan.push_back(ptsIdx);
428 }
Chris Dalton9ca27842018-01-18 12:24:50 -0700429 continue;
430
Chris Daltone1639692018-08-20 14:00:30 -0600431 case Verb::kMonotonicConicTo:
Chris Dalton9f2dab02018-04-18 14:07:03 -0600432 quadPointInstanceData[currIndices->fConics++].setW(
Chris Dalton9414c962018-06-14 10:14:50 -0600433 &pts[ptsIdx], devToAtlasOffset,
434 fGeometry.getConicWeight(nextConicWeightIdx));
Chris Dalton9f2dab02018-04-18 14:07:03 -0600435 ptsIdx += 2;
436 ++nextConicWeightIdx;
437 if (!currFanIsTessellated) {
438 SkASSERT(!currFan.empty());
439 currFan.push_back(ptsIdx);
440 }
441 continue;
442
Chris Daltone1639692018-08-20 14:00:30 -0600443 case Verb::kEndClosedContour: // endPt == startPt.
Chris Dalton84403d72018-02-13 21:46:17 -0500444 if (!currFanIsTessellated) {
445 SkASSERT(!currFan.empty());
446 currFan.pop_back();
447 }
Chris Dalton9ca27842018-01-18 12:24:50 -0700448 // fallthru.
Chris Daltone1639692018-08-20 14:00:30 -0600449 case Verb::kEndOpenContour: // endPt != startPt.
Chris Dalton84403d72018-02-13 21:46:17 -0500450 SkASSERT(!currFanIsTessellated || currFan.empty());
451 if (!currFanIsTessellated && currFan.count() >= 3) {
Chris Dalton9ca27842018-01-18 12:24:50 -0700452 int fanSize = currFan.count();
453 // Reserve space for emit_recursive_fan. Technically this can grow to
454 // fanSize + log3(fanSize), but we approximate with log2.
455 currFan.push_back_n(SkNextLog2(fanSize));
Chris Daltonc3318f02019-07-19 14:20:53 -0600456 SkDEBUGCODE(TriPointInstance* end =) emit_recursive_fan(
457 pts, currFan, 0, fanSize, devToAtlasOffset, triangleOrdering,
458 triPointInstanceData + currIndices->fTriangles);
Chris Dalton9ca27842018-01-18 12:24:50 -0700459 currIndices->fTriangles += fanSize - 2;
Chris Dalton84403d72018-02-13 21:46:17 -0500460 SkASSERT(triPointInstanceData + currIndices->fTriangles == end);
Chris Dalton9ca27842018-01-18 12:24:50 -0700461 }
462 currFan.reset();
463 continue;
464 }
465 }
466
467 fInstanceBuffer->unmap();
468
Chris Daltone1639692018-08-20 14:00:30 -0600469 SkASSERT(nextPathInfo == fPathInfos.end());
Chris Dalton9ca27842018-01-18 12:24:50 -0700470 SkASSERT(ptsIdx == pts.count() - 1);
471 SkASSERT(instanceIndices[0].fTriangles == fBaseInstances[1].fTriangles);
472 SkASSERT(instanceIndices[1].fTriangles == fBaseInstances[0].fQuadratics);
473 SkASSERT(instanceIndices[0].fQuadratics == fBaseInstances[1].fQuadratics);
474 SkASSERT(instanceIndices[1].fQuadratics == triEndIdx);
Chris Dalton703b4762018-04-06 16:11:48 -0600475 SkASSERT(instanceIndices[0].fWeightedTriangles == fBaseInstances[1].fWeightedTriangles);
476 SkASSERT(instanceIndices[1].fWeightedTriangles == fBaseInstances[0].fCubics);
Chris Dalton9ca27842018-01-18 12:24:50 -0700477 SkASSERT(instanceIndices[0].fCubics == fBaseInstances[1].fCubics);
Chris Dalton9f2dab02018-04-18 14:07:03 -0600478 SkASSERT(instanceIndices[1].fCubics == fBaseInstances[0].fConics);
479 SkASSERT(instanceIndices[0].fConics == fBaseInstances[1].fConics);
480 SkASSERT(instanceIndices[1].fConics == quadEndIdx);
Chris Dalton9ca27842018-01-18 12:24:50 -0700481
482 fMeshesScratchBuffer.reserve(fMaxMeshesPerDraw);
Brian Salomon49348902018-06-26 09:12:38 -0400483 fScissorRectScratchBuffer.reserve(fMaxMeshesPerDraw);
Chris Dalton9ca27842018-01-18 12:24:50 -0700484
485 return true;
486}
487
Chris Daltonc3318f02019-07-19 14:20:53 -0600488void GrCCFiller::drawFills(
489 GrOpFlushState* flushState, GrCCCoverageProcessor* proc, const GrPipeline& pipeline,
490 BatchID batchID, const SkIRect& drawBounds) const {
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600491 using PrimitiveType = GrCCCoverageProcessor::PrimitiveType;
Chris Dalton9ca27842018-01-18 12:24:50 -0700492
493 SkASSERT(fInstanceBuffer);
494
Chris Dalton2c5e0112019-03-29 13:14:18 -0500495 GrResourceProvider* rp = flushState->resourceProvider();
Chris Daltone1639692018-08-20 14:00:30 -0600496 const PrimitiveTallies& batchTotalCounts = fBatches[batchID].fTotalPrimitiveCounts;
Chris Dalton84403d72018-02-13 21:46:17 -0500497
Chris Dalton84403d72018-02-13 21:46:17 -0500498 if (batchTotalCounts.fTriangles) {
Chris Dalton2c5e0112019-03-29 13:14:18 -0500499 proc->reset(PrimitiveType::kTriangles, rp);
500 this->drawPrimitives(
501 flushState, *proc, pipeline, batchID, &PrimitiveTallies::fTriangles, drawBounds);
Chris Dalton84403d72018-02-13 21:46:17 -0500502 }
Chris Dalton9ca27842018-01-18 12:24:50 -0700503
Chris Dalton703b4762018-04-06 16:11:48 -0600504 if (batchTotalCounts.fWeightedTriangles) {
Chris Daltonc3318f02019-07-19 14:20:53 -0600505 SkASSERT(Algorithm::kStencilWindingCount != fAlgorithm);
Chris Dalton2c5e0112019-03-29 13:14:18 -0500506 proc->reset(PrimitiveType::kWeightedTriangles, rp);
507 this->drawPrimitives(
508 flushState, *proc, pipeline, batchID, &PrimitiveTallies::fWeightedTriangles,
509 drawBounds);
Chris Dalton84403d72018-02-13 21:46:17 -0500510 }
Chris Dalton9ca27842018-01-18 12:24:50 -0700511
Chris Dalton84403d72018-02-13 21:46:17 -0500512 if (batchTotalCounts.fQuadratics) {
Chris Dalton2c5e0112019-03-29 13:14:18 -0500513 proc->reset(PrimitiveType::kQuadratics, rp);
514 this->drawPrimitives(
515 flushState, *proc, pipeline, batchID, &PrimitiveTallies::fQuadratics, drawBounds);
Chris Dalton84403d72018-02-13 21:46:17 -0500516 }
517
518 if (batchTotalCounts.fCubics) {
Chris Dalton2c5e0112019-03-29 13:14:18 -0500519 proc->reset(PrimitiveType::kCubics, rp);
520 this->drawPrimitives(
521 flushState, *proc, pipeline, batchID, &PrimitiveTallies::fCubics, drawBounds);
Chris Dalton84403d72018-02-13 21:46:17 -0500522 }
Chris Dalton9f2dab02018-04-18 14:07:03 -0600523
524 if (batchTotalCounts.fConics) {
Chris Dalton2c5e0112019-03-29 13:14:18 -0500525 proc->reset(PrimitiveType::kConics, rp);
526 this->drawPrimitives(
527 flushState, *proc, pipeline, batchID, &PrimitiveTallies::fConics, drawBounds);
Chris Dalton9f2dab02018-04-18 14:07:03 -0600528 }
Chris Dalton9ca27842018-01-18 12:24:50 -0700529}
530
Chris Dalton2c5e0112019-03-29 13:14:18 -0500531void GrCCFiller::drawPrimitives(
532 GrOpFlushState* flushState, const GrCCCoverageProcessor& proc, const GrPipeline& pipeline,
533 BatchID batchID, int PrimitiveTallies::*instanceType, const SkIRect& drawBounds) const {
Brian Salomond818ebf2018-07-02 14:08:49 +0000534 SkASSERT(pipeline.isScissorEnabled());
Chris Dalton9ca27842018-01-18 12:24:50 -0700535
Chris Dalton9ca27842018-01-18 12:24:50 -0700536 // Don't call reset(), as that also resets the reserve count.
537 fMeshesScratchBuffer.pop_back_n(fMeshesScratchBuffer.count());
Brian Salomon49348902018-06-26 09:12:38 -0400538 fScissorRectScratchBuffer.pop_back_n(fScissorRectScratchBuffer.count());
Chris Dalton9ca27842018-01-18 12:24:50 -0700539
Chris Dalton9ca27842018-01-18 12:24:50 -0700540 SkASSERT(batchID > 0);
Chris Daltone1639692018-08-20 14:00:30 -0600541 SkASSERT(batchID < fBatches.count());
542 const Batch& previousBatch = fBatches[batchID - 1];
543 const Batch& batch = fBatches[batchID];
Chris Dalton84403d72018-02-13 21:46:17 -0500544 SkDEBUGCODE(int totalInstanceCount = 0);
Chris Dalton9ca27842018-01-18 12:24:50 -0700545
546 if (int instanceCount = batch.fEndNonScissorIndices.*instanceType -
547 previousBatch.fEndNonScissorIndices.*instanceType) {
548 SkASSERT(instanceCount > 0);
Chris Dalton916c4982018-08-15 00:53:25 -0600549 int baseInstance = fBaseInstances[(int)GrScissorTest::kDisabled].*instanceType +
Chris Dalton9ca27842018-01-18 12:24:50 -0700550 previousBatch.fEndNonScissorIndices.*instanceType;
Brian Salomon12d22642019-01-29 14:38:50 -0500551 proc.appendMesh(fInstanceBuffer, instanceCount, baseInstance, &fMeshesScratchBuffer);
Brian Salomon49348902018-06-26 09:12:38 -0400552 fScissorRectScratchBuffer.push_back().setXYWH(0, 0, drawBounds.width(),
553 drawBounds.height());
Chris Dalton84403d72018-02-13 21:46:17 -0500554 SkDEBUGCODE(totalInstanceCount += instanceCount);
Chris Dalton9ca27842018-01-18 12:24:50 -0700555 }
556
557 SkASSERT(previousBatch.fEndScissorSubBatchIdx > 0);
558 SkASSERT(batch.fEndScissorSubBatchIdx <= fScissorSubBatches.count());
Chris Dalton916c4982018-08-15 00:53:25 -0600559 int baseScissorInstance = fBaseInstances[(int)GrScissorTest::kEnabled].*instanceType;
Chris Dalton9ca27842018-01-18 12:24:50 -0700560 for (int i = previousBatch.fEndScissorSubBatchIdx; i < batch.fEndScissorSubBatchIdx; ++i) {
561 const ScissorSubBatch& previousSubBatch = fScissorSubBatches[i - 1];
562 const ScissorSubBatch& scissorSubBatch = fScissorSubBatches[i];
563 int startIndex = previousSubBatch.fEndPrimitiveIndices.*instanceType;
564 int instanceCount = scissorSubBatch.fEndPrimitiveIndices.*instanceType - startIndex;
565 if (!instanceCount) {
566 continue;
567 }
568 SkASSERT(instanceCount > 0);
Brian Salomon12d22642019-01-29 14:38:50 -0500569 proc.appendMesh(fInstanceBuffer, instanceCount, baseScissorInstance + startIndex,
570 &fMeshesScratchBuffer);
Brian Salomon49348902018-06-26 09:12:38 -0400571 fScissorRectScratchBuffer.push_back() = scissorSubBatch.fScissor;
Chris Dalton84403d72018-02-13 21:46:17 -0500572 SkDEBUGCODE(totalInstanceCount += instanceCount);
Chris Dalton9ca27842018-01-18 12:24:50 -0700573 }
574
Brian Salomon49348902018-06-26 09:12:38 -0400575 SkASSERT(fMeshesScratchBuffer.count() == fScissorRectScratchBuffer.count());
Chris Dalton9ca27842018-01-18 12:24:50 -0700576 SkASSERT(fMeshesScratchBuffer.count() <= fMaxMeshesPerDraw);
Chris Dalton84403d72018-02-13 21:46:17 -0500577 SkASSERT(totalInstanceCount == batch.fTotalPrimitiveCounts.*instanceType);
Chris Dalton9ca27842018-01-18 12:24:50 -0700578
579 if (!fMeshesScratchBuffer.empty()) {
Brian Salomon49348902018-06-26 09:12:38 -0400580 proc.draw(flushState, pipeline, fScissorRectScratchBuffer.begin(),
581 fMeshesScratchBuffer.begin(), fMeshesScratchBuffer.count(),
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600582 SkRect::Make(drawBounds));
Chris Dalton9ca27842018-01-18 12:24:50 -0700583 }
584}